code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_unlock_wallet.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 "errors" 21 "fmt" 22 23 "code.vegaprotocol.io/vega/libs/jsonrpc" 24 "code.vegaprotocol.io/vega/wallet/wallet" 25 26 "github.com/mitchellh/mapstructure" 27 ) 28 29 type AdminUnlockWalletParams struct { 30 Wallet string `json:"wallet"` 31 Passphrase string `json:"passphrase"` 32 } 33 34 type AdminUnlockWallet struct { 35 walletStore WalletStore 36 } 37 38 func (h *AdminUnlockWallet) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) { 39 params, err := validateUnlockWalletParams(rawParams) 40 if err != nil { 41 return nil, InvalidParams(err) 42 } 43 44 if exist, err := h.walletStore.WalletExists(ctx, params.Wallet); err != nil { 45 return nil, InternalError(fmt.Errorf("could not verify the wallet exists: %w", err)) 46 } else if !exist { 47 return nil, InvalidParams(ErrWalletDoesNotExist) 48 } 49 50 if err := h.walletStore.UnlockWallet(ctx, params.Wallet, params.Passphrase); err != nil { 51 if errors.Is(err, wallet.ErrWrongPassphrase) { 52 return nil, InvalidParams(err) 53 } 54 return nil, InternalError(fmt.Errorf("could not unlock the wallet: %w", err)) 55 } 56 return nil, nil 57 } 58 59 func validateUnlockWalletParams(rawParams jsonrpc.Params) (AdminUnlockWalletParams, error) { 60 if rawParams == nil { 61 return AdminUnlockWalletParams{}, ErrParamsRequired 62 } 63 64 params := AdminUnlockWalletParams{} 65 if err := mapstructure.Decode(rawParams, ¶ms); err != nil { 66 return AdminUnlockWalletParams{}, ErrParamsDoNotMatch 67 } 68 69 if params.Wallet == "" { 70 return AdminUnlockWalletParams{}, ErrWalletIsRequired 71 } 72 73 if params.Passphrase == "" { 74 return AdminUnlockWalletParams{}, ErrPassphraseIsRequired 75 } 76 77 return params, nil 78 } 79 80 func NewAdminUnlockWallet( 81 walletStore WalletStore, 82 ) *AdminUnlockWallet { 83 return &AdminUnlockWallet{ 84 walletStore: walletStore, 85 } 86 }