code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_generate_key.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 "code.vegaprotocol.io/vega/wallet/wallet" 24 25 "github.com/mitchellh/mapstructure" 26 ) 27 28 type AdminGenerateKeyParams struct { 29 Wallet string `json:"wallet"` 30 Metadata []wallet.Metadata `json:"metadata"` 31 } 32 33 type AdminGenerateKeyResult struct { 34 PublicKey string `json:"publicKey"` 35 Algorithm wallet.Algorithm `json:"algorithm"` 36 Metadata []wallet.Metadata `json:"metadata"` 37 } 38 39 type AdminGenerateKey struct { 40 walletStore WalletStore 41 } 42 43 // Handle generates a key on the specified wallet. 44 func (h *AdminGenerateKey) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) { 45 params, err := validateGenerateKeyParams(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 kp, err := w.GenerateKeyPair(params.Metadata) 70 if err != nil { 71 return nil, InternalError(fmt.Errorf("could not generate a new key: %w", err)) 72 } 73 74 if err := h.walletStore.UpdateWallet(ctx, w); err != nil { 75 return nil, InternalError(fmt.Errorf("could not save the wallet: %w", err)) 76 } 77 78 return AdminGenerateKeyResult{ 79 PublicKey: kp.PublicKey(), 80 Algorithm: wallet.Algorithm{ 81 Name: kp.AlgorithmName(), 82 Version: kp.AlgorithmVersion(), 83 }, 84 Metadata: kp.Metadata(), 85 }, nil 86 } 87 88 func validateGenerateKeyParams(rawParams jsonrpc.Params) (AdminGenerateKeyParams, error) { 89 if rawParams == nil { 90 return AdminGenerateKeyParams{}, ErrParamsRequired 91 } 92 93 params := AdminGenerateKeyParams{} 94 if err := mapstructure.Decode(rawParams, ¶ms); err != nil { 95 return AdminGenerateKeyParams{}, ErrParamsDoNotMatch 96 } 97 98 if params.Wallet == "" { 99 return AdminGenerateKeyParams{}, ErrWalletIsRequired 100 } 101 102 return params, nil 103 } 104 105 func NewAdminGenerateKey( 106 walletStore WalletStore, 107 ) *AdminGenerateKey { 108 return &AdminGenerateKey{ 109 walletStore: walletStore, 110 } 111 }