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