code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_rename_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  
    24  	"github.com/mitchellh/mapstructure"
    25  )
    26  
    27  type AdminRenameWalletParams struct {
    28  	Wallet  string `json:"wallet"`
    29  	NewName string `json:"newName"`
    30  }
    31  
    32  type AdminRenameWallet struct {
    33  	walletStore WalletStore
    34  }
    35  
    36  // Handle renames a wallet.
    37  func (h *AdminRenameWallet) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
    38  	params, err := validateRenameWalletParams(rawParams)
    39  	if err != nil {
    40  		return nil, InvalidParams(err)
    41  	}
    42  
    43  	if exist, err := h.walletStore.WalletExists(ctx, params.Wallet); err != nil {
    44  		return nil, InternalError(fmt.Errorf("could not verify the wallet exists: %w", err))
    45  	} else if !exist {
    46  		return nil, InvalidParams(ErrWalletDoesNotExist)
    47  	}
    48  
    49  	if exist, err := h.walletStore.WalletExists(ctx, params.NewName); err != nil {
    50  		return nil, InternalError(fmt.Errorf("could not verify the wallet exists: %w", err))
    51  	} else if exist {
    52  		return nil, InvalidParams(ErrWalletAlreadyExists)
    53  	}
    54  
    55  	if err := h.walletStore.RenameWallet(ctx, params.Wallet, params.NewName); err != nil {
    56  		return nil, InternalError(fmt.Errorf("could not rename the wallet: %w", err))
    57  	}
    58  
    59  	return nil, nil
    60  }
    61  
    62  func validateRenameWalletParams(rawParams jsonrpc.Params) (AdminRenameWalletParams, error) {
    63  	if rawParams == nil {
    64  		return AdminRenameWalletParams{}, ErrParamsRequired
    65  	}
    66  
    67  	params := AdminRenameWalletParams{}
    68  	if err := mapstructure.Decode(rawParams, &params); err != nil {
    69  		return AdminRenameWalletParams{}, ErrParamsDoNotMatch
    70  	}
    71  
    72  	if params.Wallet == "" {
    73  		return AdminRenameWalletParams{}, ErrWalletIsRequired
    74  	}
    75  
    76  	if params.NewName == "" {
    77  		return AdminRenameWalletParams{}, ErrNewNameIsRequired
    78  	}
    79  
    80  	return params, nil
    81  }
    82  
    83  func NewAdminRenameWallet(
    84  	walletStore WalletStore,
    85  ) *AdminRenameWallet {
    86  	return &AdminRenameWallet{
    87  		walletStore: walletStore,
    88  	}
    89  }