code.vegaprotocol.io/vega@v0.79.0/core/nodewallets/handler.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 nodewallets
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  	"path/filepath"
    22  
    23  	"code.vegaprotocol.io/vega/core/nodewallets/registry"
    24  	"code.vegaprotocol.io/vega/core/nodewallets/vega"
    25  	"code.vegaprotocol.io/vega/paths"
    26  )
    27  
    28  var (
    29  	ErrEthereumWalletAlreadyExists   = errors.New("the Ethereum node wallet already exists")
    30  	ErrVegaWalletAlreadyExists       = errors.New("the Vega node wallet already exists")
    31  	ErrTendermintPubkeyAlreadyExists = errors.New("the Tendermint pubkey already exists")
    32  )
    33  
    34  func GetVegaWallet(vegaPaths paths.Paths, registryPassphrase string) (*vega.Wallet, error) {
    35  	registryLoader, err := registry.NewLoader(vegaPaths, registryPassphrase)
    36  	if err != nil {
    37  		return nil, fmt.Errorf("couldn't initialise node wallet registry: %v", err)
    38  	}
    39  
    40  	registry, err := registryLoader.Get(registryPassphrase)
    41  	if err != nil {
    42  		return nil, fmt.Errorf("couldn't load node wallet registry: %v", err)
    43  	}
    44  
    45  	if registry.Vega == nil {
    46  		return nil, ErrVegaWalletIsMissing
    47  	}
    48  
    49  	walletLoader, err := vega.InitialiseWalletLoader(vegaPaths)
    50  	if err != nil {
    51  		return nil, fmt.Errorf("couldn't initialise Vega node wallet loader: %w", err)
    52  	}
    53  
    54  	wallet, err := walletLoader.Load(registry.Vega.Name, registry.Vega.Passphrase)
    55  	if err != nil {
    56  		return nil, fmt.Errorf("couldn't load Ethereum node wallet: %w", err)
    57  	}
    58  
    59  	return wallet, nil
    60  }
    61  
    62  func GetNodeWallets(config Config, vegaPaths paths.Paths, registryPassphrase string) (*NodeWallets, error) {
    63  	nodeWallets := &NodeWallets{}
    64  
    65  	registryLoader, err := registry.NewLoader(vegaPaths, registryPassphrase)
    66  	if err != nil {
    67  		return nil, fmt.Errorf("couldn't initialise node wallet registry: %v", err)
    68  	}
    69  
    70  	reg, err := registryLoader.Get(registryPassphrase)
    71  	if err != nil {
    72  		return nil, fmt.Errorf("couldn't load node wallet registry: %v", err)
    73  	}
    74  
    75  	if reg.Ethereum != nil {
    76  		w, err := GetEthereumWalletWithRegistry(vegaPaths, reg)
    77  		if err != nil {
    78  			return nil, err
    79  		}
    80  
    81  		nodeWallets.Ethereum = w
    82  	}
    83  
    84  	if reg.Vega != nil {
    85  		vegaWalletLoader, err := vega.InitialiseWalletLoader(vegaPaths)
    86  		if err != nil {
    87  			return nil, fmt.Errorf("couldn't initialise Vega node wallet loader: %w", err)
    88  		}
    89  
    90  		nodeWallets.Vega, err = vegaWalletLoader.Load(reg.Vega.Name, reg.Vega.Passphrase)
    91  		if err != nil {
    92  			return nil, fmt.Errorf("couldn't load Vega node wallet: %w", err)
    93  		}
    94  	}
    95  
    96  	if reg.Tendermint != nil {
    97  		nodeWallets.Tendermint = &TendermintPubkey{
    98  			Pubkey: reg.Tendermint.Pubkey,
    99  		}
   100  	}
   101  
   102  	return nodeWallets, nil
   103  }
   104  
   105  func GenerateVegaWallet(vegaPaths paths.Paths, registryPassphrase, walletPassphrase string, overwrite bool) (map[string]string, error) {
   106  	registryLoader, err := registry.NewLoader(vegaPaths, registryPassphrase)
   107  	if err != nil {
   108  		return nil, fmt.Errorf("couldn't initialise node wallet registry: %v", err)
   109  	}
   110  
   111  	reg, err := registryLoader.Get(registryPassphrase)
   112  	if err != nil {
   113  		return nil, fmt.Errorf("couldn't load node wallet registry: %v", err)
   114  	}
   115  
   116  	if !overwrite && reg.Vega != nil {
   117  		return nil, ErrVegaWalletAlreadyExists
   118  	}
   119  
   120  	vegaWalletLoader, err := vega.InitialiseWalletLoader(vegaPaths)
   121  	if err != nil {
   122  		return nil, fmt.Errorf("couldn't initialise Vega node wallet loader: %w", err)
   123  	}
   124  
   125  	w, data, err := vegaWalletLoader.Generate(walletPassphrase)
   126  	if err != nil {
   127  		return nil, fmt.Errorf("couldn't generate Vega node wallet: %w", err)
   128  	}
   129  
   130  	reg.Vega = &registry.RegisteredVegaWallet{
   131  		Name:       w.Name(),
   132  		Passphrase: walletPassphrase,
   133  	}
   134  
   135  	if err := registryLoader.Save(reg, registryPassphrase); err != nil {
   136  		return nil, fmt.Errorf("couldn't save registry: %w", err)
   137  	}
   138  
   139  	data["registryFilePath"] = registryLoader.RegistryFilePath()
   140  	return data, nil
   141  }
   142  
   143  func ImportVegaWallet(vegaPaths paths.Paths, registryPassphrase, walletPassphrase, sourceFilePath string, overwrite bool) (map[string]string, error) {
   144  	if !filepath.IsAbs(sourceFilePath) {
   145  		return nil, fmt.Errorf("path to the wallet file need to be absolute")
   146  	}
   147  
   148  	registryLoader, err := registry.NewLoader(vegaPaths, registryPassphrase)
   149  	if err != nil {
   150  		return nil, fmt.Errorf("couldn't initialise node wallet registry: %v", err)
   151  	}
   152  
   153  	reg, err := registryLoader.Get(registryPassphrase)
   154  	if err != nil {
   155  		return nil, fmt.Errorf("couldn't load node wallet registry: %v", err)
   156  	}
   157  
   158  	if !overwrite && reg.Vega != nil {
   159  		return nil, ErrVegaWalletAlreadyExists
   160  	}
   161  
   162  	vegaWalletLoader, err := vega.InitialiseWalletLoader(vegaPaths)
   163  	if err != nil {
   164  		return nil, fmt.Errorf("couldn't initialise Vega node wallet loader: %w", err)
   165  	}
   166  
   167  	w, data, err := vegaWalletLoader.Import(sourceFilePath, walletPassphrase)
   168  	if err != nil {
   169  		return nil, fmt.Errorf("couldn't import Vega node wallet: %w", err)
   170  	}
   171  
   172  	reg.Vega = &registry.RegisteredVegaWallet{
   173  		Name:       w.Name(),
   174  		Passphrase: walletPassphrase,
   175  	}
   176  
   177  	if err := registryLoader.Save(reg, registryPassphrase); err != nil {
   178  		return nil, fmt.Errorf("couldn't save registry: %w", err)
   179  	}
   180  
   181  	data["registryFilePath"] = registryLoader.RegistryFilePath()
   182  	return data, nil
   183  }
   184  
   185  func ImportTendermintPubkey(
   186  	vegaPaths paths.Paths,
   187  	registryPassphrase, pubkey string,
   188  	overwrite bool,
   189  ) (map[string]string, error) {
   190  	registryLoader, err := registry.NewLoader(vegaPaths, registryPassphrase)
   191  	if err != nil {
   192  		return nil, fmt.Errorf("couldn't initialise node wallet registry: %v", err)
   193  	}
   194  
   195  	reg, err := registryLoader.Get(registryPassphrase)
   196  	if err != nil {
   197  		return nil, fmt.Errorf("couldn't load node wallet registry: %v", err)
   198  	}
   199  
   200  	if !overwrite && reg.Tendermint != nil {
   201  		return nil, ErrTendermintPubkeyAlreadyExists
   202  	}
   203  
   204  	reg.Tendermint = &registry.RegisteredTendermintPubkey{
   205  		Pubkey: pubkey,
   206  	}
   207  
   208  	if err := registryLoader.Save(reg, registryPassphrase); err != nil {
   209  		return nil, fmt.Errorf("couldn't save registry: %w", err)
   210  	}
   211  
   212  	return map[string]string{
   213  		"registryFilePath": registryLoader.RegistryFilePath(),
   214  		"tendermintPubkey": pubkey,
   215  	}, nil
   216  }