code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/faucet/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 faucet
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"code.vegaprotocol.io/vega/core/config"
    22  	"code.vegaprotocol.io/vega/core/faucet"
    23  	vgjson "code.vegaprotocol.io/vega/libs/json"
    24  	"code.vegaprotocol.io/vega/logging"
    25  	"code.vegaprotocol.io/vega/paths"
    26  )
    27  
    28  type faucetInit struct {
    29  	config.VegaHomeFlag
    30  	config.PassphraseFlag
    31  	config.OutputFlag
    32  
    33  	Force         bool `description:"Erase existing configuration at specified path"                long:"force"           short:"f"`
    34  	UpdateInPlace bool `description:"Update the Vega node configuration with the faucet public key" long:"update-in-place"`
    35  }
    36  
    37  func (opts *faucetInit) Execute(_ []string) error {
    38  	logDefaultConfig := logging.NewDefaultConfig()
    39  	log := logging.NewLoggerFromConfig(logDefaultConfig)
    40  	defer log.AtExit()
    41  
    42  	output, err := opts.OutputFlag.GetOutput()
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	pass, err := opts.PassphraseFile.Get("faucet wallet", true)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	vegaPaths := paths.New(opts.VegaHome)
    53  
    54  	initResult, err := faucet.Initialise(vegaPaths, pass, opts.Force)
    55  	if err != nil {
    56  		return fmt.Errorf("couldn't initialise faucet: %w", err)
    57  	}
    58  
    59  	var nodeCfgFilePath string
    60  	if opts.UpdateInPlace {
    61  		nodeCfgLoader, nodeCfg, err := config.EnsureNodeConfig(vegaPaths)
    62  		if err != nil {
    63  			return err
    64  		}
    65  
    66  		// add the faucet public key to the allowlist
    67  		nodeCfg.EvtForward.BlockchainQueueAllowlist = append(
    68  			nodeCfg.EvtForward.BlockchainQueueAllowlist, initResult.Wallet.PublicKey)
    69  
    70  		if err := nodeCfgLoader.Save(nodeCfg); err != nil {
    71  			return fmt.Errorf("couldn't update node configuration: %w", err)
    72  		}
    73  
    74  		nodeCfgFilePath = nodeCfgLoader.ConfigFilePath()
    75  	}
    76  
    77  	result := struct {
    78  		PublicKey            string `json:"publicKey"`
    79  		NodeConfigFilePath   string `json:"nodeConfigFilePath,omitempty"`
    80  		FaucetConfigFilePath string `json:"faucetConfigFilePath"`
    81  		FaucetWalletFilePath string `json:"faucetWalletFilePath"`
    82  	}{
    83  		NodeConfigFilePath:   nodeCfgFilePath,
    84  		FaucetConfigFilePath: initResult.ConfigFilePath,
    85  		FaucetWalletFilePath: initResult.Wallet.FilePath,
    86  		PublicKey:            initResult.Wallet.PublicKey,
    87  	}
    88  
    89  	if output.IsHuman() {
    90  		log.Info("faucet initialised successfully", logging.String("public-key", initResult.Wallet.PublicKey))
    91  		err := vgjson.PrettyPrint(result)
    92  		if err != nil {
    93  			return fmt.Errorf("couldn't pretty print result: %w", err)
    94  		}
    95  	} else if output.IsJSON() {
    96  		return vgjson.Print(result)
    97  	}
    98  
    99  	return nil
   100  }