code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/wallet_describe.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 cmd
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  	"fmt"
    22  	"io"
    23  
    24  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/cli"
    25  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags"
    26  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/printer"
    27  	"code.vegaprotocol.io/vega/wallet/api"
    28  	"code.vegaprotocol.io/vega/wallet/wallets"
    29  
    30  	"github.com/spf13/cobra"
    31  )
    32  
    33  var (
    34  	describeWalletLong = cli.LongDesc(`
    35  		Get wallet information such as wallet ID, key derivation version and type.
    36  	`)
    37  
    38  	describeWalletExample = cli.Examples(`
    39  		# Get the wallet information
    40  		{{.Software}} describe --wallet WALLET
    41  	`)
    42  )
    43  
    44  type DescribeWalletHandler func(api.AdminDescribeWalletParams, string) (api.AdminDescribeWalletResult, error)
    45  
    46  func NewCmdDescribeWallet(w io.Writer, rf *RootFlags) *cobra.Command {
    47  	h := func(params api.AdminDescribeWalletParams, passphrase string) (api.AdminDescribeWalletResult, error) {
    48  		ctx := context.Background()
    49  
    50  		walletStore, err := wallets.InitialiseStore(rf.Home, false)
    51  		if err != nil {
    52  			return api.AdminDescribeWalletResult{}, fmt.Errorf("couldn't initialise wallets store: %w", err)
    53  		}
    54  		defer walletStore.Close()
    55  
    56  		if _, errDetails := api.NewAdminUnlockWallet(walletStore).Handle(ctx, api.AdminUnlockWalletParams{
    57  			Wallet:     params.Wallet,
    58  			Passphrase: passphrase,
    59  		}); errDetails != nil {
    60  			return api.AdminDescribeWalletResult{}, errors.New(errDetails.Data)
    61  		}
    62  
    63  		rawResult, errorDetails := api.NewAdminDescribeWallet(walletStore).Handle(ctx, params)
    64  		if errorDetails != nil {
    65  			return api.AdminDescribeWalletResult{}, errors.New(errorDetails.Data)
    66  		}
    67  		return rawResult.(api.AdminDescribeWalletResult), nil
    68  	}
    69  	return BuildCmdDescribeWallet(w, h, rf)
    70  }
    71  
    72  func BuildCmdDescribeWallet(w io.Writer, handler DescribeWalletHandler, rf *RootFlags) *cobra.Command {
    73  	f := &DescribeWalletFlags{}
    74  
    75  	cmd := &cobra.Command{
    76  		Use:     "describe",
    77  		Short:   "Describe the specified wallet",
    78  		Long:    describeWalletLong,
    79  		Example: describeWalletExample,
    80  		RunE: func(_ *cobra.Command, _ []string) error {
    81  			req, pass, err := f.Validate()
    82  			if err != nil {
    83  				return err
    84  			}
    85  
    86  			resp, err := handler(req, pass)
    87  			if err != nil {
    88  				return err
    89  			}
    90  
    91  			switch rf.Output {
    92  			case flags.InteractiveOutput:
    93  				PrintDescribeWalletResponse(w, resp)
    94  			case flags.JSONOutput:
    95  				return printer.FprintJSON(w, resp)
    96  			}
    97  
    98  			return nil
    99  		},
   100  	}
   101  
   102  	cmd.Flags().StringVarP(&f.Wallet,
   103  		"wallet", "w",
   104  		"",
   105  		"Name of the wallet to use",
   106  	)
   107  	cmd.Flags().StringVarP(&f.PassphraseFile,
   108  		"passphrase-file", "p",
   109  		"",
   110  		"Path to the file containing the wallet's passphrase",
   111  	)
   112  
   113  	autoCompleteWallet(cmd, rf.Home, "wallet")
   114  
   115  	return cmd
   116  }
   117  
   118  type DescribeWalletFlags struct {
   119  	Wallet         string
   120  	PassphraseFile string
   121  }
   122  
   123  func (f *DescribeWalletFlags) Validate() (api.AdminDescribeWalletParams, string, error) {
   124  	req := api.AdminDescribeWalletParams{}
   125  
   126  	if len(f.Wallet) == 0 {
   127  		return api.AdminDescribeWalletParams{}, "", flags.MustBeSpecifiedError("wallet")
   128  	}
   129  	req.Wallet = f.Wallet
   130  
   131  	passphrase, err := flags.GetPassphrase(f.PassphraseFile)
   132  	if err != nil {
   133  		return api.AdminDescribeWalletParams{}, "", err
   134  	}
   135  
   136  	return req, passphrase, nil
   137  }
   138  
   139  func PrintDescribeWalletResponse(w io.Writer, resp api.AdminDescribeWalletResult) {
   140  	p := printer.NewInteractivePrinter(w)
   141  
   142  	str := p.String()
   143  	defer p.Print(str)
   144  
   145  	str.Text("Type:").NextLine().WarningText(resp.Type).NextLine()
   146  	str.Text("Key derivation version:").NextLine().WarningText(fmt.Sprintf("%d", resp.KeyDerivationVersion)).NextLine()
   147  	str.Text("ID:").NextLine().WarningText(resp.ID).NextLine()
   148  }