code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/key_list.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  	listKeysLong = cli.LongDesc(`
    35  		List the keys of a given wallet.
    36  	`)
    37  
    38  	listKeysExample = cli.Examples(`
    39  		# List all keys
    40  		{{.Software}} key list --wallet WALLET
    41  	`)
    42  )
    43  
    44  type ListKeysHandler func(api.AdminListKeysParams, string) (api.AdminListKeysResult, error)
    45  
    46  func NewCmdListKeys(w io.Writer, rf *RootFlags) *cobra.Command {
    47  	h := func(params api.AdminListKeysParams, passphrase string) (api.AdminListKeysResult, error) {
    48  		ctx := context.Background()
    49  
    50  		walletStore, err := wallets.InitialiseStore(rf.Home, false)
    51  		if err != nil {
    52  			return api.AdminListKeysResult{}, 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.AdminListKeysResult{}, errors.New(errDetails.Data)
    61  		}
    62  
    63  		rawResult, errDetails := api.NewAdminListKeys(walletStore).Handle(ctx, params)
    64  		if errDetails != nil {
    65  			return api.AdminListKeysResult{}, errors.New(errDetails.Data)
    66  		}
    67  		return rawResult.(api.AdminListKeysResult), nil
    68  	}
    69  
    70  	return BuildCmdListKeys(w, h, rf)
    71  }
    72  
    73  func BuildCmdListKeys(w io.Writer, handler ListKeysHandler, rf *RootFlags) *cobra.Command {
    74  	f := &ListKeysFlags{}
    75  
    76  	cmd := &cobra.Command{
    77  		Use:     "list",
    78  		Short:   "List the keys of a given wallet",
    79  		Long:    listKeysLong,
    80  		Example: listKeysExample,
    81  		RunE: func(_ *cobra.Command, _ []string) error {
    82  			req, pass, err := f.Validate()
    83  			if err != nil {
    84  				return err
    85  			}
    86  
    87  			resp, err := handler(req, pass)
    88  			if err != nil {
    89  				return err
    90  			}
    91  
    92  			switch rf.Output {
    93  			case flags.InteractiveOutput:
    94  				PrintListKeysResponse(w, resp)
    95  			case flags.JSONOutput:
    96  				return printer.FprintJSON(w, resp)
    97  			}
    98  
    99  			return nil
   100  		},
   101  	}
   102  
   103  	cmd.Flags().StringVarP(&f.Wallet,
   104  		"wallet", "w",
   105  		"",
   106  		"Name of the wallet to use",
   107  	)
   108  	cmd.Flags().StringVarP(&f.PassphraseFile,
   109  		"passphrase-file", "p",
   110  		"",
   111  		"Path to the file containing the wallet's passphrase",
   112  	)
   113  
   114  	autoCompleteWallet(cmd, rf.Home, "wallet")
   115  
   116  	return cmd
   117  }
   118  
   119  type ListKeysFlags struct {
   120  	Wallet         string
   121  	PassphraseFile string
   122  }
   123  
   124  func (f *ListKeysFlags) Validate() (api.AdminListKeysParams, string, error) {
   125  	if len(f.Wallet) == 0 {
   126  		return api.AdminListKeysParams{}, "", flags.MustBeSpecifiedError("wallet")
   127  	}
   128  
   129  	passphrase, err := flags.GetPassphrase(f.PassphraseFile)
   130  	if err != nil {
   131  		return api.AdminListKeysParams{}, "", err
   132  	}
   133  
   134  	return api.AdminListKeysParams{
   135  		Wallet: f.Wallet,
   136  	}, passphrase, nil
   137  }
   138  
   139  func PrintListKeysResponse(w io.Writer, resp api.AdminListKeysResult) {
   140  	p := printer.NewInteractivePrinter(w)
   141  
   142  	str := p.String()
   143  	defer p.Print(str)
   144  
   145  	for i, key := range resp.Keys {
   146  		if i != 0 {
   147  			str.NextLine()
   148  		}
   149  		str.Text("Name:       ").WarningText(key.Name).NextLine()
   150  		str.Text("Public key: ").WarningText(key.PublicKey).NextLine()
   151  	}
   152  }