code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_import_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  	"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 AdminImportWalletParams struct {
    29  	Wallet               string `json:"wallet"`
    30  	RecoveryPhrase       string `json:"recoveryPhrase"`
    31  	KeyDerivationVersion uint32 `json:"keyDerivationVersion"`
    32  	Passphrase           string `json:"passphrase"`
    33  }
    34  
    35  type AdminImportWalletResult struct {
    36  	Wallet AdminImportedWallet `json:"wallet"`
    37  	Key    AdminFirstPublicKey `json:"key"`
    38  }
    39  
    40  type AdminImportedWallet struct {
    41  	Name                 string `json:"name"`
    42  	KeyDerivationVersion uint32 `json:"keyDerivationVersion"`
    43  }
    44  
    45  type AdminImportWallet struct {
    46  	walletStore WalletStore
    47  }
    48  
    49  func (h *AdminImportWallet) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
    50  	params, err := validateImportWalletParams(rawParams)
    51  	if err != nil {
    52  		return nil, InvalidParams(err)
    53  	}
    54  
    55  	if exist, err := h.walletStore.WalletExists(ctx, params.Wallet); err != nil {
    56  		return nil, InternalError(fmt.Errorf("could not verify the wallet exists: %w", err))
    57  	} else if exist {
    58  		return nil, InvalidParams(ErrWalletAlreadyExists)
    59  	}
    60  
    61  	w, err := wallet.ImportHDWallet(params.Wallet, params.RecoveryPhrase, params.KeyDerivationVersion)
    62  	if err != nil {
    63  		return nil, InternalError(fmt.Errorf("could not import the wallet: %w", err))
    64  	}
    65  
    66  	kp, err := w.GenerateKeyPair(nil)
    67  	if err != nil {
    68  		return nil, InternalError(fmt.Errorf("could not generate first key: %w", err))
    69  	}
    70  
    71  	if err := h.walletStore.CreateWallet(ctx, w, params.Passphrase); err != nil {
    72  		return nil, InternalError(fmt.Errorf("could not save the wallet: %w", err))
    73  	}
    74  
    75  	return AdminImportWalletResult{
    76  		Wallet: AdminImportedWallet{
    77  			Name:                 w.Name(),
    78  			KeyDerivationVersion: w.KeyDerivationVersion(),
    79  		},
    80  		Key: AdminFirstPublicKey{
    81  			PublicKey: kp.PublicKey(),
    82  			Algorithm: wallet.Algorithm{
    83  				Name:    kp.AlgorithmName(),
    84  				Version: kp.AlgorithmVersion(),
    85  			},
    86  			Metadata: kp.Metadata(),
    87  		},
    88  	}, nil
    89  }
    90  
    91  func validateImportWalletParams(rawParams jsonrpc.Params) (AdminImportWalletParams, error) {
    92  	if rawParams == nil {
    93  		return AdminImportWalletParams{}, ErrParamsRequired
    94  	}
    95  
    96  	params := AdminImportWalletParams{}
    97  	if err := mapstructure.Decode(rawParams, &params); err != nil {
    98  		return AdminImportWalletParams{}, ErrParamsDoNotMatch
    99  	}
   100  
   101  	if params.Wallet == "" {
   102  		return AdminImportWalletParams{}, ErrWalletIsRequired
   103  	}
   104  
   105  	if params.Passphrase == "" {
   106  		return AdminImportWalletParams{}, ErrPassphraseIsRequired
   107  	}
   108  
   109  	if params.RecoveryPhrase == "" {
   110  		return AdminImportWalletParams{}, ErrRecoveryPhraseIsRequired
   111  	}
   112  
   113  	if params.KeyDerivationVersion == 0 {
   114  		return AdminImportWalletParams{}, ErrWalletKeyDerivationVersionIsRequired
   115  	}
   116  
   117  	return params, nil
   118  }
   119  
   120  func NewAdminImportWallet(
   121  	walletStore WalletStore,
   122  ) *AdminImportWallet {
   123  	return &AdminImportWallet{
   124  		walletStore: walletStore,
   125  	}
   126  }