code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_taint_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 AdminTaintKeyParams struct { 28 Wallet string `json:"wallet"` 29 PublicKey string `json:"publicKey"` 30 } 31 32 type AdminTaintKey struct { 33 walletStore WalletStore 34 } 35 36 // Handle marks the specified public key as tainted. It makes it unusable for 37 // transaction signing and sending. 38 func (h *AdminTaintKey) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) { 39 params, err := validateTaintKeyParams(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 alreadyUnlocked, err := h.walletStore.IsWalletAlreadyUnlocked(ctx, params.Wallet) 51 if err != nil { 52 return nil, InternalError(fmt.Errorf("could not verify whether the wallet is already unlock or not: %w", err)) 53 } 54 if !alreadyUnlocked { 55 return nil, RequestNotPermittedError(ErrWalletIsLocked) 56 } 57 58 w, err := h.walletStore.GetWallet(ctx, params.Wallet) 59 if err != nil { 60 return nil, InternalError(fmt.Errorf("could not retrieve the wallet: %w", err)) 61 } 62 63 if !w.HasPublicKey(params.PublicKey) { 64 return nil, InvalidParams(ErrPublicKeyDoesNotExist) 65 } 66 67 if err := w.TaintKey(params.PublicKey); err != nil { 68 return nil, InternalError(fmt.Errorf("could not taint the key: %w", err)) 69 } 70 71 if err := h.walletStore.UpdateWallet(ctx, w); err != nil { 72 return nil, InternalError(fmt.Errorf("could not save the wallet: %w", err)) 73 } 74 75 return nil, nil 76 } 77 78 func validateTaintKeyParams(rawParams jsonrpc.Params) (AdminTaintKeyParams, error) { 79 if rawParams == nil { 80 return AdminTaintKeyParams{}, ErrParamsRequired 81 } 82 83 params := AdminTaintKeyParams{} 84 if err := mapstructure.Decode(rawParams, ¶ms); err != nil { 85 return AdminTaintKeyParams{}, ErrParamsDoNotMatch 86 } 87 88 if params.Wallet == "" { 89 return AdminTaintKeyParams{}, ErrWalletIsRequired 90 } 91 92 if params.PublicKey == "" { 93 return AdminTaintKeyParams{}, ErrPublicKeyIsRequired 94 } 95 96 return params, nil 97 } 98 99 func NewAdminTaintKey( 100 walletStore WalletStore, 101 ) *AdminTaintKey { 102 return &AdminTaintKey{ 103 walletStore: walletStore, 104 } 105 }