code.vegaprotocol.io/vega@v0.79.0/core/faucet/faucet.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  	"errors"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/paths"
    23  )
    24  
    25  var ErrFaucetConfigAlreadyExists = errors.New("faucet configuration already exists")
    26  
    27  type InitialisationResult struct {
    28  	Wallet         *WalletGenerationResult
    29  	ConfigFilePath string
    30  }
    31  
    32  func Initialise(vegaPaths paths.Paths, passphrase string, rewrite bool) (*InitialisationResult, error) {
    33  	walletGenResult, err := GenerateWallet(vegaPaths, passphrase)
    34  	if err != nil {
    35  		return nil, fmt.Errorf("couldn't generate a new faucet wallet: %w", err)
    36  	}
    37  
    38  	cfgLoader, err := InitialiseConfigLoader(vegaPaths)
    39  	if err != nil {
    40  		return nil, fmt.Errorf("couldn't initialise faucet configuration loader: %w", err)
    41  	}
    42  
    43  	configExists, err := cfgLoader.ConfigExists()
    44  	if err != nil {
    45  		return nil, fmt.Errorf("couldn't verify faucet configuration presence: %w", err)
    46  	}
    47  
    48  	if configExists {
    49  		if rewrite {
    50  			cfgLoader.RemoveConfig()
    51  		} else {
    52  			return nil, ErrFaucetConfigAlreadyExists
    53  		}
    54  	}
    55  
    56  	cfg := NewDefaultConfig()
    57  	cfg.WalletName = walletGenResult.Name
    58  
    59  	if err := cfgLoader.SaveConfig(&cfg); err != nil {
    60  		return nil, fmt.Errorf("couldn't save faucet configuration: %w", err)
    61  	}
    62  
    63  	return &InitialisationResult{
    64  		Wallet:         walletGenResult,
    65  		ConfigFilePath: cfgLoader.ConfigFilePath(),
    66  	}, nil
    67  }