diff --git a/api/server/models/account_config.go b/api/server/models/account_config.go new file mode 100644 index 00000000..1442dcb2 --- /dev/null +++ b/api/server/models/account_config.go @@ -0,0 +1,124 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package models + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "context" + "strconv" + + "github.com/go-openapi/errors" + "github.com/go-openapi/strfmt" + "github.com/go-openapi/swag" + "github.com/go-openapi/validate" +) + +// AccountConfig Wallet Account Config. +// +// swagger:model AccountConfig +type AccountConfig struct { + + // sign rules + // Required: true + SignRules []*SignRule `json:"signRules"` +} + +// Validate validates this account config +func (m *AccountConfig) Validate(formats strfmt.Registry) error { + var res []error + + if err := m.validateSignRules(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (m *AccountConfig) validateSignRules(formats strfmt.Registry) error { + + if err := validate.Required("signRules", "body", m.SignRules); err != nil { + return err + } + + for i := 0; i < len(m.SignRules); i++ { + if swag.IsZero(m.SignRules[i]) { // not required + continue + } + + if m.SignRules[i] != nil { + if err := m.SignRules[i].Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("signRules" + "." + strconv.Itoa(i)) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("signRules" + "." + strconv.Itoa(i)) + } + return err + } + } + + } + + return nil +} + +// ContextValidate validate this account config based on the context it is used +func (m *AccountConfig) ContextValidate(ctx context.Context, formats strfmt.Registry) error { + var res []error + + if err := m.contextValidateSignRules(ctx, formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (m *AccountConfig) contextValidateSignRules(ctx context.Context, formats strfmt.Registry) error { + + for i := 0; i < len(m.SignRules); i++ { + + if m.SignRules[i] != nil { + + if swag.IsZero(m.SignRules[i]) { // not required + return nil + } + + if err := m.SignRules[i].ContextValidate(ctx, formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("signRules" + "." + strconv.Itoa(i)) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("signRules" + "." + strconv.Itoa(i)) + } + return err + } + } + + } + + return nil +} + +// MarshalBinary interface implementation +func (m *AccountConfig) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *AccountConfig) UnmarshalBinary(b []byte) error { + var res AccountConfig + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +} diff --git a/api/server/models/config.go b/api/server/models/config.go new file mode 100644 index 00000000..e721cd72 --- /dev/null +++ b/api/server/models/config.go @@ -0,0 +1,111 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package models + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "context" + + "github.com/go-openapi/errors" + "github.com/go-openapi/strfmt" + "github.com/go-openapi/swag" + "github.com/go-openapi/validate" +) + +// Config Wallet Config. +// +// swagger:model Config +type Config struct { + + // accounts + Accounts map[string]AccountConfig `json:"accounts,omitempty"` +} + +// Validate validates this config +func (m *Config) Validate(formats strfmt.Registry) error { + var res []error + + if err := m.validateAccounts(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (m *Config) validateAccounts(formats strfmt.Registry) error { + if swag.IsZero(m.Accounts) { // not required + return nil + } + + for k := range m.Accounts { + + if err := validate.Required("accounts"+"."+k, "body", m.Accounts[k]); err != nil { + return err + } + if val, ok := m.Accounts[k]; ok { + if err := val.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("accounts" + "." + k) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("accounts" + "." + k) + } + return err + } + } + + } + + return nil +} + +// ContextValidate validate this config based on the context it is used +func (m *Config) ContextValidate(ctx context.Context, formats strfmt.Registry) error { + var res []error + + if err := m.contextValidateAccounts(ctx, formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (m *Config) contextValidateAccounts(ctx context.Context, formats strfmt.Registry) error { + + for k := range m.Accounts { + + if val, ok := m.Accounts[k]; ok { + if err := val.ContextValidate(ctx, formats); err != nil { + return err + } + } + + } + + return nil +} + +// MarshalBinary interface implementation +func (m *Config) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *Config) UnmarshalBinary(b []byte) error { + var res Config + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +} diff --git a/api/server/models/sign_rule.go b/api/server/models/sign_rule.go new file mode 100644 index 00000000..19cf03d7 --- /dev/null +++ b/api/server/models/sign_rule.go @@ -0,0 +1,105 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package models + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "context" + + "github.com/go-openapi/errors" + "github.com/go-openapi/strfmt" + "github.com/go-openapi/swag" + "github.com/go-openapi/validate" +) + +// SignRule Account Sign rule. +// +// swagger:model SignRule +type SignRule struct { + + // auto sign + // Required: true + AutoSign *bool `json:"autoSign"` + + // contract + // Required: true + Contract *string `json:"contract"` + + // password prompt + // Required: true + PasswordPrompt *bool `json:"passwordPrompt"` +} + +// Validate validates this sign rule +func (m *SignRule) Validate(formats strfmt.Registry) error { + var res []error + + if err := m.validateAutoSign(formats); err != nil { + res = append(res, err) + } + + if err := m.validateContract(formats); err != nil { + res = append(res, err) + } + + if err := m.validatePasswordPrompt(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (m *SignRule) validateAutoSign(formats strfmt.Registry) error { + + if err := validate.Required("autoSign", "body", m.AutoSign); err != nil { + return err + } + + return nil +} + +func (m *SignRule) validateContract(formats strfmt.Registry) error { + + if err := validate.Required("contract", "body", m.Contract); err != nil { + return err + } + + return nil +} + +func (m *SignRule) validatePasswordPrompt(formats strfmt.Registry) error { + + if err := validate.Required("passwordPrompt", "body", m.PasswordPrompt); err != nil { + return err + } + + return nil +} + +// ContextValidate validates this sign rule based on context it is used +func (m *SignRule) ContextValidate(ctx context.Context, formats strfmt.Registry) error { + return nil +} + +// MarshalBinary interface implementation +func (m *SignRule) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *SignRule) UnmarshalBinary(b []byte) error { + var res SignRule + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +} diff --git a/api/server/restapi/embedded_spec.go b/api/server/restapi/embedded_spec.go index c642b353..76769667 100644 --- a/api/server/restapi/embedded_spec.go +++ b/api/server/restapi/embedded_spec.go @@ -779,6 +779,29 @@ func init() { } } }, + "/api/config": { + "get": { + "description": "Get wallet config", + "produces": [ + "application/json" + ], + "operationId": "GetConfig", + "responses": { + "200": { + "description": "Config retrieved.", + "schema": { + "$ref": "#/definitions/Config" + } + }, + "500": { + "description": "Internal Server Error - The server has encountered a situation it does not know how to handle.", + "schema": { + "$ref": "#/definitions/Error" + } + } + } + } + }, "/web-app/{resource}": { "get": { "description": "Route for the ReactJS front-end web application (in /web-frontend)", @@ -851,6 +874,21 @@ func init() { } } }, + "AccountConfig": { + "description": "Wallet Account Config.", + "type": "object", + "required": [ + "signRules" + ], + "properties": { + "signRules": { + "type": "array", + "items": { + "$ref": "#/definitions/SignRule" + } + } + } + }, "Address": { "description": "Account's address.", "type": "string", @@ -904,6 +942,19 @@ func init() { } ] }, + "Config": { + "description": "Wallet Config.", + "type": "object", + "properties": { + "accounts": { + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/AccountConfig" + } + } + }, + "x-nullable": false + }, "Error": { "description": "Error object.", "type": "object", @@ -1066,6 +1117,26 @@ func init() { } } }, + "SignRule": { + "description": "Account Sign rule.", + "type": "object", + "required": [ + "contract", + "passwordPrompt", + "autoSign" + ], + "properties": { + "autoSign": { + "type": "boolean" + }, + "contract": { + "type": "string" + }, + "passwordPrompt": { + "type": "boolean" + } + } + }, "TransferRequest": { "type": "object", "required": [ @@ -1946,6 +2017,29 @@ func init() { } } }, + "/api/config": { + "get": { + "description": "Get wallet config", + "produces": [ + "application/json" + ], + "operationId": "GetConfig", + "responses": { + "200": { + "description": "Config retrieved.", + "schema": { + "$ref": "#/definitions/Config" + } + }, + "500": { + "description": "Internal Server Error - The server has encountered a situation it does not know how to handle.", + "schema": { + "$ref": "#/definitions/Error" + } + } + } + } + }, "/web-app/{resource}": { "get": { "description": "Route for the ReactJS front-end web application (in /web-frontend)", @@ -2018,6 +2112,21 @@ func init() { } } }, + "AccountConfig": { + "description": "Wallet Account Config.", + "type": "object", + "required": [ + "signRules" + ], + "properties": { + "signRules": { + "type": "array", + "items": { + "$ref": "#/definitions/SignRule" + } + } + } + }, "Address": { "description": "Account's address.", "type": "string", @@ -2073,6 +2182,19 @@ func init() { } ] }, + "Config": { + "description": "Wallet Config.", + "type": "object", + "properties": { + "accounts": { + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/AccountConfig" + } + } + }, + "x-nullable": false + }, "Error": { "description": "Error object.", "type": "object", @@ -2235,6 +2357,26 @@ func init() { } } }, + "SignRule": { + "description": "Account Sign rule.", + "type": "object", + "required": [ + "contract", + "passwordPrompt", + "autoSign" + ], + "properties": { + "autoSign": { + "type": "boolean" + }, + "contract": { + "type": "string" + }, + "passwordPrompt": { + "type": "boolean" + } + } + }, "TransferRequest": { "type": "object", "required": [ diff --git a/api/server/restapi/operations/get_config.go b/api/server/restapi/operations/get_config.go new file mode 100644 index 00000000..393016af --- /dev/null +++ b/api/server/restapi/operations/get_config.go @@ -0,0 +1,56 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the generate command + +import ( + "net/http" + + "github.com/go-openapi/runtime/middleware" +) + +// GetConfigHandlerFunc turns a function with the right signature into a get config handler +type GetConfigHandlerFunc func(GetConfigParams) middleware.Responder + +// Handle executing the request and returning a response +func (fn GetConfigHandlerFunc) Handle(params GetConfigParams) middleware.Responder { + return fn(params) +} + +// GetConfigHandler interface for that can handle valid get config params +type GetConfigHandler interface { + Handle(GetConfigParams) middleware.Responder +} + +// NewGetConfig creates a new http.Handler for the get config operation +func NewGetConfig(ctx *middleware.Context, handler GetConfigHandler) *GetConfig { + return &GetConfig{Context: ctx, Handler: handler} +} + +/* + GetConfig swagger:route GET /api/config getConfig + +Get wallet config +*/ +type GetConfig struct { + Context *middleware.Context + Handler GetConfigHandler +} + +func (o *GetConfig) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + route, rCtx, _ := o.Context.RouteInfo(r) + if rCtx != nil { + *r = *rCtx + } + var Params = NewGetConfigParams() + if err := o.Context.BindValidRequest(r, route, &Params); err != nil { // bind params + o.Context.Respond(rw, r, route.Produces, route, err) + return + } + + res := o.Handler.Handle(Params) // actually handle the request + o.Context.Respond(rw, r, route.Produces, route, res) + +} diff --git a/api/server/restapi/operations/get_config_parameters.go b/api/server/restapi/operations/get_config_parameters.go new file mode 100644 index 00000000..7501ecf5 --- /dev/null +++ b/api/server/restapi/operations/get_config_parameters.go @@ -0,0 +1,46 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "net/http" + + "github.com/go-openapi/errors" + "github.com/go-openapi/runtime/middleware" +) + +// NewGetConfigParams creates a new GetConfigParams object +// +// There are no default values defined in the spec. +func NewGetConfigParams() GetConfigParams { + + return GetConfigParams{} +} + +// GetConfigParams contains all the bound params for the get config operation +// typically these are obtained from a http.Request +// +// swagger:parameters GetConfig +type GetConfigParams struct { + + // HTTP Request Object + HTTPRequest *http.Request `json:"-"` +} + +// BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface +// for simple values it will use straight method calls. +// +// To ensure default values, the struct must have been initialized with NewGetConfigParams() beforehand. +func (o *GetConfigParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error { + var res []error + + o.HTTPRequest = r + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} diff --git a/api/server/restapi/operations/get_config_responses.go b/api/server/restapi/operations/get_config_responses.go new file mode 100644 index 00000000..c805e7c5 --- /dev/null +++ b/api/server/restapi/operations/get_config_responses.go @@ -0,0 +1,103 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "net/http" + + "github.com/go-openapi/runtime" + "github.com/massalabs/station-massa-wallet/api/server/models" +) + +// GetConfigOKCode is the HTTP code returned for type GetConfigOK +const GetConfigOKCode int = 200 + +/* +GetConfigOK Config retrieved. + +swagger:response getConfigOK +*/ +type GetConfigOK struct { + + /* + In: Body + */ + Payload *models.Config `json:"body,omitempty"` +} + +// NewGetConfigOK creates GetConfigOK with default headers values +func NewGetConfigOK() *GetConfigOK { + + return &GetConfigOK{} +} + +// WithPayload adds the payload to the get config o k response +func (o *GetConfigOK) WithPayload(payload *models.Config) *GetConfigOK { + o.Payload = payload + return o +} + +// SetPayload sets the payload to the get config o k response +func (o *GetConfigOK) SetPayload(payload *models.Config) { + o.Payload = payload +} + +// WriteResponse to the client +func (o *GetConfigOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.WriteHeader(200) + if o.Payload != nil { + payload := o.Payload + if err := producer.Produce(rw, payload); err != nil { + panic(err) // let the recovery middleware deal with this + } + } +} + +// GetConfigInternalServerErrorCode is the HTTP code returned for type GetConfigInternalServerError +const GetConfigInternalServerErrorCode int = 500 + +/* +GetConfigInternalServerError Internal Server Error - The server has encountered a situation it does not know how to handle. + +swagger:response getConfigInternalServerError +*/ +type GetConfigInternalServerError struct { + + /* + In: Body + */ + Payload *models.Error `json:"body,omitempty"` +} + +// NewGetConfigInternalServerError creates GetConfigInternalServerError with default headers values +func NewGetConfigInternalServerError() *GetConfigInternalServerError { + + return &GetConfigInternalServerError{} +} + +// WithPayload adds the payload to the get config internal server error response +func (o *GetConfigInternalServerError) WithPayload(payload *models.Error) *GetConfigInternalServerError { + o.Payload = payload + return o +} + +// SetPayload sets the payload to the get config internal server error response +func (o *GetConfigInternalServerError) SetPayload(payload *models.Error) { + o.Payload = payload +} + +// WriteResponse to the client +func (o *GetConfigInternalServerError) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.WriteHeader(500) + if o.Payload != nil { + payload := o.Payload + if err := producer.Produce(rw, payload); err != nil { + panic(err) // let the recovery middleware deal with this + } + } +} diff --git a/api/server/restapi/operations/get_config_urlbuilder.go b/api/server/restapi/operations/get_config_urlbuilder.go new file mode 100644 index 00000000..9025f7d4 --- /dev/null +++ b/api/server/restapi/operations/get_config_urlbuilder.go @@ -0,0 +1,84 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the generate command + +import ( + "errors" + "net/url" + golangswaggerpaths "path" +) + +// GetConfigURL generates an URL for the get config operation +type GetConfigURL struct { + _basePath string +} + +// WithBasePath sets the base path for this url builder, only required when it's different from the +// base path specified in the swagger spec. +// When the value of the base path is an empty string +func (o *GetConfigURL) WithBasePath(bp string) *GetConfigURL { + o.SetBasePath(bp) + return o +} + +// SetBasePath sets the base path for this url builder, only required when it's different from the +// base path specified in the swagger spec. +// When the value of the base path is an empty string +func (o *GetConfigURL) SetBasePath(bp string) { + o._basePath = bp +} + +// Build a url path and query string +func (o *GetConfigURL) Build() (*url.URL, error) { + var _result url.URL + + var _path = "/api/config" + + _basePath := o._basePath + _result.Path = golangswaggerpaths.Join(_basePath, _path) + + return &_result, nil +} + +// Must is a helper function to panic when the url builder returns an error +func (o *GetConfigURL) Must(u *url.URL, err error) *url.URL { + if err != nil { + panic(err) + } + if u == nil { + panic("url can't be nil") + } + return u +} + +// String returns the string representation of the path with query string +func (o *GetConfigURL) String() string { + return o.Must(o.Build()).String() +} + +// BuildFull builds a full url with scheme, host, path and query string +func (o *GetConfigURL) BuildFull(scheme, host string) (*url.URL, error) { + if scheme == "" { + return nil, errors.New("scheme is required for a full url on GetConfigURL") + } + if host == "" { + return nil, errors.New("host is required for a full url on GetConfigURL") + } + + base, err := o.Build() + if err != nil { + return nil, err + } + + base.Scheme = scheme + base.Host = host + return base, nil +} + +// StringFull returns the string representation of a complete url +func (o *GetConfigURL) StringFull(scheme, host string) string { + return o.Must(o.BuildFull(scheme, host)).String() +} diff --git a/api/server/restapi/operations/massa_wallet_api.go b/api/server/restapi/operations/massa_wallet_api.go index c478a5bf..22e4307b 100644 --- a/api/server/restapi/operations/massa_wallet_api.go +++ b/api/server/restapi/operations/massa_wallet_api.go @@ -83,6 +83,9 @@ func NewMassaWalletAPI(spec *loads.Document) *MassaWalletAPI { GetAllAssetsHandler: GetAllAssetsHandlerFunc(func(params GetAllAssetsParams) middleware.Responder { return middleware.NotImplemented("operation GetAllAssets has not yet been implemented") }), + GetConfigHandler: GetConfigHandlerFunc(func(params GetConfigParams) middleware.Responder { + return middleware.NotImplemented("operation GetConfig has not yet been implemented") + }), ImportAccountHandler: ImportAccountHandlerFunc(func(params ImportAccountParams) middleware.Responder { return middleware.NotImplemented("operation ImportAccount has not yet been implemented") }), @@ -177,6 +180,8 @@ type MassaWalletAPI struct { GetAccountHandler GetAccountHandler // GetAllAssetsHandler sets the operation handler for the get all assets operation GetAllAssetsHandler GetAllAssetsHandler + // GetConfigHandler sets the operation handler for the get config operation + GetConfigHandler GetConfigHandler // ImportAccountHandler sets the operation handler for the import account operation ImportAccountHandler ImportAccountHandler // SignHandler sets the operation handler for the sign operation @@ -312,6 +317,9 @@ func (o *MassaWalletAPI) Validate() error { if o.GetAllAssetsHandler == nil { unregistered = append(unregistered, "GetAllAssetsHandler") } + if o.GetConfigHandler == nil { + unregistered = append(unregistered, "GetConfigHandler") + } if o.ImportAccountHandler == nil { unregistered = append(unregistered, "ImportAccountHandler") } @@ -472,6 +480,10 @@ func (o *MassaWalletAPI) initHandlerCache() { o.handlers["GET"] = make(map[string]http.Handler) } o.handlers["GET"]["/api/accounts/{nickname}/assets"] = NewGetAllAssets(o.context, o.GetAllAssetsHandler) + if o.handlers["GET"] == nil { + o.handlers["GET"] = make(map[string]http.Handler) + } + o.handlers["GET"]["/api/config"] = NewGetConfig(o.context, o.GetConfigHandler) if o.handlers["PUT"] == nil { o.handlers["PUT"] = make(map[string]http.Handler) } diff --git a/api/walletApi-V0.yml b/api/walletApi-V0.yml index 8906f647..1eca8438 100644 --- a/api/walletApi-V0.yml +++ b/api/walletApi-V0.yml @@ -42,6 +42,21 @@ paths: description: Resource not found. schema: $ref: "#/definitions/Error" + /api/config: + get: + operationId: GetConfig + description: Get wallet config + produces: + - application/json + responses: + 200: + description: Config retrieved. + schema: + $ref: "#/definitions/Config" + 500: + description: Internal Server Error - The server has encountered a situation it does not know how to handle. + schema: + $ref: "#/definitions/Error" /api/accounts: get: operationId: AccountList @@ -749,3 +764,39 @@ definitions: type: boolean dollarValue: type: string + + SignRule: + type: object + description: Account Sign rule. + properties: + contract: + type: string + passwordPrompt: + type: boolean + autoSign: + type: boolean + required: + - contract + - passwordPrompt + - autoSign + + AccountConfig: + type: object + description: Wallet Account Config. + properties: + signRules: + type: array + items: + $ref: "#/definitions/SignRule" + required: + - signRules + + Config: + type: object + description: Wallet Config. + properties: + accounts: + type: object + additionalProperties: + $ref: "#/definitions/AccountConfig" + x-nullable: false diff --git a/internal/handler/wallet/api.go b/internal/handler/wallet/api.go index 1207fd44..3fd8c170 100644 --- a/internal/handler/wallet/api.go +++ b/internal/handler/wallet/api.go @@ -37,6 +37,7 @@ func AppendEndpoints(api *operations.MassaWalletAPI, prompterApp prompt.WalletPr api.AddAssetHandler = NewAddAsset(AssetsStore, massaClient) api.GetAllAssetsHandler = NewGetAllAssets(prompterApp.App().Wallet, AssetsStore, massaClient) api.DeleteAssetHandler = NewDeleteAsset(AssetsStore) + api.GetConfigHandler = NewGetConfig() } // loadAccount loads a wallet from the file system or returns an error. diff --git a/internal/handler/wallet/errors.go b/internal/handler/wallet/errors.go index 664eeebd..36e710de 100644 --- a/internal/handler/wallet/errors.go +++ b/internal/handler/wallet/errors.go @@ -31,4 +31,5 @@ const ( errorDeleteAssetJSON errorAssetNotFound errorTradeRoll + internalError ) diff --git a/internal/handler/wallet/get_config.go b/internal/handler/wallet/get_config.go new file mode 100644 index 00000000..952b6832 --- /dev/null +++ b/internal/handler/wallet/get_config.go @@ -0,0 +1,51 @@ +package wallet + +import ( + "github.com/go-openapi/runtime/middleware" + "github.com/massalabs/station-massa-wallet/api/server/models" + "github.com/massalabs/station-massa-wallet/api/server/restapi/operations" + "github.com/massalabs/station-massa-wallet/pkg/config" +) + +func NewGetConfig() operations.GetConfigHandler { + return &getConfig{} +} + +type getConfig struct{} + +func (w *getConfig) Handle(_ operations.GetConfigParams) middleware.Responder { + cfg := config.Get() + modelConfig, err := newConfigModel(cfg) + //nolint:wsl + if err != nil { + return operations.NewGetConfigInternalServerError().WithPayload( + &models.Error{ + Code: internalError, + Message: "Unable to create config model", + }) + } + + return operations.NewGetConfigOK().WithPayload(modelConfig) +} + +func newConfigModel(cfg *config.Config) (*models.Config, error) { + modelAccounts := make(map[string]models.AccountConfig) + + for nickname, accountConfig := range cfg.Accounts { + modelSignRules := make([]*models.SignRule, len(accountConfig.SignRules)) + for i, rule := range accountConfig.SignRules { + modelSignRules[i] = &models.SignRule{ + Contract: &rule.Contract, + PasswordPrompt: &rule.PasswordPrompt, + AutoSign: &rule.AutoSign, + } + } + modelAccounts[nickname] = models.AccountConfig{ + SignRules: modelSignRules, + } + } + + return &models.Config{ + Accounts: modelAccounts, + }, nil +}