code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/init.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  	"fmt"
    20  	"io"
    21  	"os"
    22  
    23  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/cli"
    24  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags"
    25  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/printer"
    26  	"code.vegaprotocol.io/vega/paths"
    27  	"code.vegaprotocol.io/vega/wallet/service"
    28  	svcstore "code.vegaprotocol.io/vega/wallet/service/store/v1"
    29  	"code.vegaprotocol.io/vega/wallet/wallets"
    30  
    31  	"github.com/spf13/cobra"
    32  )
    33  
    34  var (
    35  	initLong = cli.LongDesc(`
    36  		Creates the folders, the configuration files and RSA keys needed by the service
    37  		to operate.
    38  	`)
    39  
    40  	initExample = cli.Examples(`
    41  		# Initialise the software
    42  		{{.Software}} init
    43  
    44  		# Re-initialise the software
    45  		{{.Software}} init --force
    46  	`)
    47  )
    48  
    49  type InitHandler func(home string, f *InitFlags) error
    50  
    51  func NewCmdInit(w io.Writer, rf *RootFlags) *cobra.Command {
    52  	return BuildCmdInit(w, Init, rf)
    53  }
    54  
    55  func BuildCmdInit(w io.Writer, handler InitHandler, rf *RootFlags) *cobra.Command {
    56  	f := &InitFlags{}
    57  
    58  	cmd := &cobra.Command{
    59  		Use:     "init",
    60  		Short:   "Initialise the software",
    61  		Long:    initLong,
    62  		Example: initExample,
    63  		RunE: func(_ *cobra.Command, _ []string) error {
    64  			if err := handler(rf.Home, f); err != nil {
    65  				return err
    66  			}
    67  
    68  			switch rf.Output {
    69  			case flags.InteractiveOutput:
    70  				PrintInitResponse(w)
    71  			}
    72  			return nil
    73  		},
    74  	}
    75  
    76  	cmd.Flags().BoolVarP(&f.Force,
    77  		"force", "f",
    78  		false,
    79  		"Overwrite exiting wallet configuration at the specified path",
    80  	)
    81  
    82  	return cmd
    83  }
    84  
    85  type InitFlags struct {
    86  	Force                bool
    87  	TokensPassphraseFile string
    88  }
    89  
    90  func Init(home string, f *InitFlags) error {
    91  	walletStore, err := wallets.InitialiseStore(home, false)
    92  	if err != nil {
    93  		return fmt.Errorf("couldn't initialise wallets store: %w", err)
    94  	}
    95  	defer walletStore.Close()
    96  
    97  	vegaPaths := paths.New(home)
    98  
    99  	svcStore, err := svcstore.InitialiseStore(vegaPaths)
   100  	if err != nil {
   101  		return fmt.Errorf("couldn't initialise service store: %w", err)
   102  	}
   103  
   104  	if err = service.InitialiseService(svcStore, f.Force); err != nil {
   105  		return fmt.Errorf("couldn't initialise the service: %w", err)
   106  	}
   107  
   108  	return nil
   109  }
   110  
   111  func PrintInitResponse(w io.Writer) {
   112  	p := printer.NewInteractivePrinter(w)
   113  
   114  	str := p.String()
   115  	defer p.Print(str)
   116  
   117  	str.CheckMark().SuccessText("Initialisation succeeded").NextSection()
   118  
   119  	str.BlueArrow().InfoText("Create a wallet").NextLine()
   120  	str.Text("To create a wallet, use the following command:").NextSection()
   121  	str.Code(fmt.Sprintf("%s create --wallet \"YOUR_WALLET\"", os.Args[0])).NextSection()
   122  	str.Text("For more information, use ").Bold("--help").Text(" flag.").NextLine()
   123  }