code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_list_keys.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 "fmt" 21 22 "code.vegaprotocol.io/vega/libs/jsonrpc" 23 24 "github.com/mitchellh/mapstructure" 25 ) 26 27 type AdminListKeysParams struct { 28 Wallet string `json:"wallet"` 29 } 30 31 type AdminListKeysResult struct { 32 Keys []AdminNamedPublicKey `json:"keys"` 33 } 34 35 type AdminNamedPublicKey struct { 36 Name string `json:"name"` 37 PublicKey string `json:"publicKey"` 38 } 39 40 type AdminListKeys struct { 41 walletStore WalletStore 42 } 43 44 func (h *AdminListKeys) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) { 45 params, err := validateAdminListKeysParams(rawParams) 46 if err != nil { 47 return nil, InvalidParams(err) 48 } 49 50 if exist, err := h.walletStore.WalletExists(ctx, params.Wallet); err != nil { 51 return nil, InternalError(fmt.Errorf("could not verify the wallet exists: %w", err)) 52 } else if !exist { 53 return nil, InvalidParams(ErrWalletDoesNotExist) 54 } 55 56 alreadyUnlocked, err := h.walletStore.IsWalletAlreadyUnlocked(ctx, params.Wallet) 57 if err != nil { 58 return nil, InternalError(fmt.Errorf("could not verify whether the wallet is already unlock or not: %w", err)) 59 } 60 if !alreadyUnlocked { 61 return nil, RequestNotPermittedError(ErrWalletIsLocked) 62 } 63 64 w, err := h.walletStore.GetWallet(ctx, params.Wallet) 65 if err != nil { 66 return nil, InternalError(fmt.Errorf("could not retrieve the wallet: %w", err)) 67 } 68 69 publicKeys := w.ListPublicKeys() 70 if err != nil { 71 return nil, InternalError(fmt.Errorf("could not list the keys: %w", err)) 72 } 73 74 strPublicKeys := make([]AdminNamedPublicKey, 0, len(publicKeys)) 75 for _, publicKey := range publicKeys { 76 strPublicKeys = append(strPublicKeys, AdminNamedPublicKey{ 77 Name: publicKey.Name(), 78 PublicKey: publicKey.Key(), 79 }) 80 } 81 82 return AdminListKeysResult{ 83 Keys: strPublicKeys, 84 }, nil 85 } 86 87 func validateAdminListKeysParams(rawParams jsonrpc.Params) (AdminListKeysParams, error) { 88 if rawParams == nil { 89 return AdminListKeysParams{}, ErrParamsRequired 90 } 91 92 params := AdminListKeysParams{} 93 if err := mapstructure.Decode(rawParams, ¶ms); err != nil { 94 return AdminListKeysParams{}, ErrParamsDoNotMatch 95 } 96 97 if params.Wallet == "" { 98 return AdminListKeysParams{}, ErrWalletIsRequired 99 } 100 101 return params, nil 102 } 103 104 func NewAdminListKeys( 105 walletStore WalletStore, 106 ) *AdminListKeys { 107 return &AdminListKeys{ 108 walletStore: walletStore, 109 } 110 }