code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/nodewallet/generate.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 nodewallet
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"code.vegaprotocol.io/vega/core/config"
    22  	"code.vegaprotocol.io/vega/core/nodewallets"
    23  	vgfmt "code.vegaprotocol.io/vega/libs/fmt"
    24  	vgjson "code.vegaprotocol.io/vega/libs/json"
    25  	"code.vegaprotocol.io/vega/logging"
    26  	"code.vegaprotocol.io/vega/paths"
    27  
    28  	"github.com/jessevdk/go-flags"
    29  )
    30  
    31  type generateCmd struct {
    32  	config.OutputFlag
    33  
    34  	Config nodewallets.Config
    35  
    36  	WalletPassphrase config.Passphrase `long:"wallet-passphrase-file"`
    37  
    38  	Chain string `choice:"vega"                                                                    choice:"ethereum" description:"The chain to be imported" long:"chain" required:"true" short:"c"`
    39  	Force bool   `description:"Should the command generate a new wallet on top of an existing one" long:"force"`
    40  
    41  	// clef options
    42  	EthereumClefAddress string `description:"The URL to the clef instance that Vega will use to generate a clef wallet." long:"ethereum-clef-address"`
    43  }
    44  
    45  const (
    46  	ethereumChain   = "ethereum"
    47  	vegaChain       = "vega"
    48  	tendermintChain = "tendermint"
    49  )
    50  
    51  func (opts *generateCmd) Execute(_ []string) error {
    52  	output, err := opts.GetOutput()
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	if output.IsHuman() && opts.EthereumClefAddress != "" {
    58  		fmt.Println(yellow("Warning: Generating a new account in Clef has to be manually approved, and only the Key Store backend is supported. \nPlease consider using the 'import' command instead."))
    59  	}
    60  
    61  	log := logging.NewLoggerFromConfig(logging.NewDefaultConfig())
    62  	defer log.AtExit()
    63  
    64  	registryPass, err := rootCmd.PassphraseFile.Get("node wallet", false)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	vegaPaths := paths.New(rootCmd.VegaHome)
    70  
    71  	_, conf, err := config.EnsureNodeConfig(vegaPaths)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	opts.Config = conf.NodeWallet
    77  
    78  	if _, err := flags.NewParser(opts, flags.Default|flags.IgnoreUnknown).Parse(); err != nil {
    79  		return err
    80  	}
    81  
    82  	var data map[string]string
    83  	switch opts.Chain {
    84  	case ethereumChain:
    85  		var walletPass string
    86  		if opts.EthereumClefAddress == "" {
    87  			walletPass, err = opts.WalletPassphrase.Get("blockchain wallet", true)
    88  			if err != nil {
    89  				return err
    90  			}
    91  		}
    92  
    93  		data, err = nodewallets.GenerateEthereumWallet(
    94  			vegaPaths,
    95  			registryPass,
    96  			walletPass,
    97  			opts.EthereumClefAddress,
    98  			opts.Force,
    99  		)
   100  		if err != nil {
   101  			return fmt.Errorf("couldn't generate Ethereum node wallet: %w", err)
   102  		}
   103  	case vegaChain:
   104  		walletPass, err := opts.WalletPassphrase.Get("blockchain wallet", true)
   105  		if err != nil {
   106  			return err
   107  		}
   108  
   109  		data, err = nodewallets.GenerateVegaWallet(vegaPaths, registryPass, walletPass, opts.Force)
   110  		if err != nil {
   111  			return fmt.Errorf("couldn't generate Vega node wallet: %w", err)
   112  		}
   113  	default:
   114  		return fmt.Errorf("chain %q is not supported", opts.Chain)
   115  	}
   116  
   117  	if output.IsHuman() {
   118  		fmt.Println(green("generation successful:"))
   119  		vgfmt.PrettyPrint(data)
   120  	} else if output.IsJSON() {
   121  		if err := vgjson.Print(data); err != nil {
   122  			return err
   123  		}
   124  	}
   125  
   126  	return nil
   127  }