code.vegaprotocol.io/vega@v0.79.0/core/nodewallets/eth/keystore/loader.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 keystore
    17  
    18  import (
    19  	"fmt"
    20  	"io/fs"
    21  	"os"
    22  	"path/filepath"
    23  
    24  	vgfs "code.vegaprotocol.io/vega/libs/fs"
    25  	"code.vegaprotocol.io/vega/paths"
    26  
    27  	"github.com/ethereum/go-ethereum/accounts/keystore"
    28  )
    29  
    30  type WalletLoader struct {
    31  	walletHome string
    32  }
    33  
    34  func InitialiseWalletLoader(vegaPaths paths.Paths) (*WalletLoader, error) {
    35  	walletHome, err := vegaPaths.CreateDataDirFor(paths.EthereumNodeWalletsDataHome)
    36  	if err != nil {
    37  		return nil, fmt.Errorf("couldn't get the directory path for %s: %w", paths.EthereumNodeWalletsDataHome, err)
    38  	}
    39  
    40  	return &WalletLoader{
    41  		walletHome: walletHome,
    42  	}, nil
    43  }
    44  
    45  func (l *WalletLoader) Generate(passphrase string) (*Wallet, map[string]string, error) {
    46  	ks := keystore.NewKeyStore(l.walletHome, keystore.StandardScryptN, keystore.StandardScryptP)
    47  	acc, err := ks.NewAccount(passphrase)
    48  	if err != nil {
    49  		return nil, nil, err
    50  	}
    51  
    52  	_, fileName := filepath.Split(acc.URL.Path)
    53  
    54  	content, err := vgfs.ReadFile(acc.URL.Path)
    55  	if err != nil {
    56  		return nil, nil, fmt.Errorf("couldn't read file %s: %w", acc.URL.Path, err)
    57  	}
    58  
    59  	w, err := newWallet(l, fileName, passphrase, content)
    60  	if err != nil {
    61  		return nil, nil, fmt.Errorf("couldn't create wallet: %w", err)
    62  	}
    63  
    64  	data := map[string]string{
    65  		"walletFilePath":  acc.URL.Path,
    66  		"ethereumAddress": w.address.Hex(),
    67  	}
    68  
    69  	return w, data, nil
    70  }
    71  
    72  func (l *WalletLoader) Load(walletName, passphrase string) (*Wallet, error) {
    73  	data, err := fs.ReadFile(os.DirFS(l.walletHome), walletName)
    74  	if err != nil {
    75  		return nil, fmt.Errorf("couldn't read wallet file: %v", err)
    76  	}
    77  
    78  	w, err := newWallet(l, walletName, passphrase, data)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	return w, nil
    84  }
    85  
    86  func (l *WalletLoader) Import(sourceFilePath, passphrase string) (*Wallet, map[string]string, error) {
    87  	content, err := vgfs.ReadFile(sourceFilePath)
    88  	if err != nil {
    89  		return nil, nil, fmt.Errorf("couldn't read file %s: %w", sourceFilePath, err)
    90  	}
    91  
    92  	_, fileName := filepath.Split(sourceFilePath)
    93  
    94  	walletFilePath := filepath.Join(l.walletHome, fileName)
    95  	err = vgfs.WriteFile(walletFilePath, content)
    96  	if err != nil {
    97  		return nil, nil, fmt.Errorf("couldn't write file %s: %w", walletFilePath, err)
    98  	}
    99  
   100  	w, err := newWallet(l, fileName, passphrase, content)
   101  	if err != nil {
   102  		return nil, nil, fmt.Errorf("couldn't create wallet: %w", err)
   103  	}
   104  
   105  	data := map[string]string{
   106  		"walletFilePath":  walletFilePath,
   107  		"ethereumAddress": w.address.Hex(),
   108  	}
   109  
   110  	return w, data, nil
   111  }