code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_annotate_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 AdminAnnotateKeyParams struct {
    29  	Wallet    string            `json:"wallet"`
    30  	PublicKey string            `json:"publicKey"`
    31  	Metadata  []wallet.Metadata `json:"metadata"`
    32  }
    33  
    34  type AdminAnnotateKeyResult struct {
    35  	Metadata []wallet.Metadata `json:"metadata"`
    36  }
    37  
    38  type AdminAnnotateKey struct {
    39  	walletStore WalletStore
    40  }
    41  
    42  // Handle attaches metadata to the specified public key. It doesn't update in
    43  // place. It overwrites. All existing metadata have to be specified to not
    44  // lose them.
    45  func (h *AdminAnnotateKey) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
    46  	params, err := validateAnnotateKeyParams(rawParams)
    47  	if err != nil {
    48  		return nil, InvalidParams(err)
    49  	}
    50  
    51  	if exist, err := h.walletStore.WalletExists(ctx, params.Wallet); err != nil {
    52  		return nil, InternalError(fmt.Errorf("could not verify the wallet exists: %w", err))
    53  	} else if !exist {
    54  		return nil, InvalidParams(ErrWalletDoesNotExist)
    55  	}
    56  
    57  	alreadyUnlocked, err := h.walletStore.IsWalletAlreadyUnlocked(ctx, params.Wallet)
    58  	if err != nil {
    59  		return nil, InternalError(fmt.Errorf("could not verify whether the wallet is already unlock or not: %w", err))
    60  	}
    61  	if !alreadyUnlocked {
    62  		return nil, RequestNotPermittedError(ErrWalletIsLocked)
    63  	}
    64  
    65  	w, err := h.walletStore.GetWallet(ctx, params.Wallet)
    66  	if err != nil {
    67  		return nil, InternalError(fmt.Errorf("could not retrieve the wallet: %w", err))
    68  	}
    69  
    70  	if !w.HasPublicKey(params.PublicKey) {
    71  		return nil, InvalidParams(ErrPublicKeyDoesNotExist)
    72  	}
    73  
    74  	updatedMeta, err := w.AnnotateKey(params.PublicKey, params.Metadata)
    75  	if err != nil {
    76  		return nil, InternalError(fmt.Errorf("could not annotate the key: %w", err))
    77  	}
    78  
    79  	if err := h.walletStore.UpdateWallet(ctx, w); err != nil {
    80  		return nil, InternalError(fmt.Errorf("could not save the wallet: %w", err))
    81  	}
    82  
    83  	return AdminAnnotateKeyResult{
    84  		Metadata: updatedMeta,
    85  	}, nil
    86  }
    87  
    88  func validateAnnotateKeyParams(rawParams jsonrpc.Params) (AdminAnnotateKeyParams, error) {
    89  	if rawParams == nil {
    90  		return AdminAnnotateKeyParams{}, ErrParamsRequired
    91  	}
    92  
    93  	params := AdminAnnotateKeyParams{}
    94  	if err := mapstructure.Decode(rawParams, &params); err != nil {
    95  		return AdminAnnotateKeyParams{}, ErrParamsDoNotMatch
    96  	}
    97  
    98  	if params.Wallet == "" {
    99  		return AdminAnnotateKeyParams{}, ErrWalletIsRequired
   100  	}
   101  
   102  	if params.PublicKey == "" {
   103  		return AdminAnnotateKeyParams{}, ErrPublicKeyIsRequired
   104  	}
   105  
   106  	return params, nil
   107  }
   108  
   109  func NewAdminAnnotateKey(
   110  	walletStore WalletStore,
   111  ) *AdminAnnotateKey {
   112  	return &AdminAnnotateKey{
   113  		walletStore: walletStore,
   114  	}
   115  }