code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_describe_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 AdminDescribeKeyParams struct {
    29  	Wallet    string `json:"wallet"`
    30  	PublicKey string `json:"publicKey"`
    31  }
    32  
    33  type AdminDescribeKeyResult struct {
    34  	PublicKey string            `json:"publicKey"`
    35  	Name      string            `json:"name"`
    36  	Algorithm wallet.Algorithm  `json:"algorithm"`
    37  	Metadata  []wallet.Metadata `json:"metadata"`
    38  	IsTainted bool              `json:"isTainted"`
    39  }
    40  
    41  type AdminDescribeKey struct {
    42  	walletStore WalletStore
    43  }
    44  
    45  // Handle retrieves key's information.
    46  func (h *AdminDescribeKey) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
    47  	params, err := validateDescribeKeyParams(rawParams)
    48  	if err != nil {
    49  		return nil, InvalidParams(err)
    50  	}
    51  
    52  	if exist, err := h.walletStore.WalletExists(ctx, params.Wallet); err != nil {
    53  		return nil, InternalError(fmt.Errorf("could not verify the wallet exists: %w", err))
    54  	} else if !exist {
    55  		return nil, InvalidParams(ErrWalletDoesNotExist)
    56  	}
    57  
    58  	alreadyUnlocked, err := h.walletStore.IsWalletAlreadyUnlocked(ctx, params.Wallet)
    59  	if err != nil {
    60  		return nil, InternalError(fmt.Errorf("could not verify whether the wallet is already unlock or not: %w", err))
    61  	}
    62  	if !alreadyUnlocked {
    63  		return nil, RequestNotPermittedError(ErrWalletIsLocked)
    64  	}
    65  
    66  	w, err := h.walletStore.GetWallet(ctx, params.Wallet)
    67  	if err != nil {
    68  		return nil, InternalError(fmt.Errorf("could not retrieve the wallet: %w", err))
    69  	}
    70  
    71  	if !w.HasPublicKey(params.PublicKey) {
    72  		return nil, InvalidParams(ErrPublicKeyDoesNotExist)
    73  	}
    74  
    75  	publicKey, err := w.DescribePublicKey(params.PublicKey)
    76  	if err != nil {
    77  		return nil, InternalError(fmt.Errorf("could not retrieve the key: %w", err))
    78  	}
    79  
    80  	return AdminDescribeKeyResult{
    81  		PublicKey: publicKey.Key(),
    82  		Name:      publicKey.Name(),
    83  		Algorithm: wallet.Algorithm{
    84  			Name:    publicKey.AlgorithmName(),
    85  			Version: publicKey.AlgorithmVersion(),
    86  		},
    87  		Metadata:  publicKey.Metadata(),
    88  		IsTainted: publicKey.IsTainted(),
    89  	}, nil
    90  }
    91  
    92  func validateDescribeKeyParams(rawParams jsonrpc.Params) (AdminDescribeKeyParams, error) {
    93  	if rawParams == nil {
    94  		return AdminDescribeKeyParams{}, ErrParamsRequired
    95  	}
    96  
    97  	params := AdminDescribeKeyParams{}
    98  	if err := mapstructure.Decode(rawParams, &params); err != nil {
    99  		return AdminDescribeKeyParams{}, ErrParamsDoNotMatch
   100  	}
   101  
   102  	if params.Wallet == "" {
   103  		return AdminDescribeKeyParams{}, ErrWalletIsRequired
   104  	}
   105  
   106  	if params.PublicKey == "" {
   107  		return AdminDescribeKeyParams{}, ErrPublicKeyIsRequired
   108  	}
   109  
   110  	return params, nil
   111  }
   112  
   113  func NewAdminDescribeKey(
   114  	walletStore WalletStore,
   115  ) *AdminDescribeKey {
   116  	return &AdminDescribeKey{
   117  		walletStore: walletStore,
   118  	}
   119  }