code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_isolate_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 24 "github.com/mitchellh/mapstructure" 25 ) 26 27 type AdminIsolateKeyParams struct { 28 Wallet string `json:"wallet"` 29 PublicKey string `json:"publicKey"` 30 IsolatedWalletPassphrase string `json:"isolatedWalletPassphrase"` 31 } 32 33 type AdminIsolateKeyResult struct { 34 Wallet string `json:"wallet"` 35 } 36 37 type AdminIsolateKey struct { 38 walletStore WalletStore 39 } 40 41 // Handle isolates a key in a specific wallet. 42 func (h *AdminIsolateKey) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) { 43 params, err := validateAdminIsolateKeyParams(rawParams) 44 if err != nil { 45 return nil, InvalidParams(err) 46 } 47 48 if exist, err := h.walletStore.WalletExists(ctx, params.Wallet); err != nil { 49 return nil, InternalError(fmt.Errorf("could not verify the wallet exists: %w", err)) 50 } else if !exist { 51 return nil, InvalidParams(ErrWalletDoesNotExist) 52 } 53 54 alreadyUnlocked, err := h.walletStore.IsWalletAlreadyUnlocked(ctx, params.Wallet) 55 if err != nil { 56 return nil, InternalError(fmt.Errorf("could not verify whether the wallet is already unlock or not: %w", err)) 57 } 58 if !alreadyUnlocked { 59 return nil, RequestNotPermittedError(ErrWalletIsLocked) 60 } 61 62 w, err := h.walletStore.GetWallet(ctx, params.Wallet) 63 if err != nil { 64 return nil, InternalError(fmt.Errorf("could not retrieve the wallet: %w", err)) 65 } 66 67 if !w.HasPublicKey(params.PublicKey) { 68 return nil, InvalidParams(ErrPublicKeyDoesNotExist) 69 } 70 71 isolatedWallet, err := w.IsolateWithKey(params.PublicKey) 72 if err != nil { 73 return nil, InternalError(fmt.Errorf("could not isolate the key: %w", err)) 74 } 75 76 if err := h.walletStore.CreateWallet(ctx, isolatedWallet, params.IsolatedWalletPassphrase); err != nil { 77 return nil, InternalError(fmt.Errorf("could not save the wallet with isolated key: %w", err)) 78 } 79 80 return AdminIsolateKeyResult{ 81 Wallet: isolatedWallet.Name(), 82 }, nil 83 } 84 85 func validateAdminIsolateKeyParams(rawParams jsonrpc.Params) (AdminIsolateKeyParams, error) { 86 if rawParams == nil { 87 return AdminIsolateKeyParams{}, ErrParamsRequired 88 } 89 90 params := AdminIsolateKeyParams{} 91 if err := mapstructure.Decode(rawParams, ¶ms); err != nil { 92 return AdminIsolateKeyParams{}, ErrParamsDoNotMatch 93 } 94 95 if params.Wallet == "" { 96 return AdminIsolateKeyParams{}, ErrWalletIsRequired 97 } 98 99 if params.PublicKey == "" { 100 return AdminIsolateKeyParams{}, ErrPublicKeyIsRequired 101 } 102 103 if params.IsolatedWalletPassphrase == "" { 104 return AdminIsolateKeyParams{}, ErrIsolatedWalletPassphraseIsRequired 105 } 106 107 return params, nil 108 } 109 110 func NewAdminIsolateKey( 111 walletStore WalletStore, 112 ) *AdminIsolateKey { 113 return &AdminIsolateKey{ 114 walletStore: walletStore, 115 } 116 }