code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_sign_message.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package api 17 18 import ( 19 "context" 20 "encoding/base64" 21 "fmt" 22 23 "code.vegaprotocol.io/vega/libs/jsonrpc" 24 25 "github.com/mitchellh/mapstructure" 26 ) 27 28 type AdminSignMessageParams struct { 29 Wallet string `json:"wallet"` 30 PublicKey string `json:"publicKey"` 31 EncodedMessage string `json:"encodedMessage"` 32 } 33 34 type AdminSignMessageResult struct { 35 EncodedSignature string `json:"encodedSignature"` 36 } 37 38 type AdminSignMessage struct { 39 walletStore WalletStore 40 } 41 42 func (h *AdminSignMessage) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) { 43 params, err := validateAdminSignMessageParams(rawParams) 44 if err != nil { 45 return nil, InvalidParams(err) 46 } 47 48 m, err := base64.StdEncoding.DecodeString(params.EncodedMessage) 49 if err != nil { 50 return nil, InvalidParams(ErrEncodedMessageIsNotValidBase64String) 51 } 52 53 if exist, err := h.walletStore.WalletExists(ctx, params.Wallet); err != nil { 54 return nil, InternalError(fmt.Errorf("could not verify the wallet exists: %w", err)) 55 } else if !exist { 56 return nil, InvalidParams(ErrWalletDoesNotExist) 57 } 58 59 alreadyUnlocked, err := h.walletStore.IsWalletAlreadyUnlocked(ctx, params.Wallet) 60 if err != nil { 61 return nil, InternalError(fmt.Errorf("could not verify whether the wallet is already unlock or not: %w", err)) 62 } 63 if !alreadyUnlocked { 64 return nil, RequestNotPermittedError(ErrWalletIsLocked) 65 } 66 67 w, err := h.walletStore.GetWallet(ctx, params.Wallet) 68 if err != nil { 69 return nil, InternalError(fmt.Errorf("could not retrieve the wallet: %w", err)) 70 } 71 72 signature, err := w.SignAny(params.PublicKey, m) 73 if err != nil { 74 return nil, InternalError(fmt.Errorf("could not sign the message: %w", err)) 75 } 76 77 return AdminSignMessageResult{ 78 EncodedSignature: base64.StdEncoding.EncodeToString(signature), 79 }, nil 80 } 81 82 func NewAdminSignMessage(walletStore WalletStore) *AdminSignMessage { 83 return &AdminSignMessage{ 84 walletStore: walletStore, 85 } 86 } 87 88 func validateAdminSignMessageParams(rawParams jsonrpc.Params) (AdminSignMessageParams, error) { 89 if rawParams == nil { 90 return AdminSignMessageParams{}, ErrParamsRequired 91 } 92 93 params := AdminSignMessageParams{} 94 if err := mapstructure.Decode(rawParams, ¶ms); err != nil { 95 return AdminSignMessageParams{}, ErrParamsDoNotMatch 96 } 97 98 if params.Wallet == "" { 99 return AdminSignMessageParams{}, ErrWalletIsRequired 100 } 101 102 if params.PublicKey == "" { 103 return AdminSignMessageParams{}, ErrPublicKeyIsRequired 104 } 105 106 if params.EncodedMessage == "" { 107 return AdminSignMessageParams{}, ErrMessageIsRequired 108 } 109 110 return AdminSignMessageParams{ 111 Wallet: params.Wallet, 112 PublicKey: params.PublicKey, 113 EncodedMessage: params.EncodedMessage, 114 }, nil 115 }