prysm-pulse/validator/accounts/v2/wallet_recover.go
Raul Jordan 4017743f7f
Accounts V2: Refactor Wallet Open / Close and Keymanager Initialization (#6668)
* refactoring create account

* dep

* much easier, create a derived account by simply unlocking wallet

* revert changes to new

* make open wallet smarter and utilize cli ctx

* remove the wallet config

* successfully build

* simplify ctx creation for tests

* tests should pass individually

* tests pass

* fixed up to allow for wallet password file input

* fix broken tests

* formatting

* fmt

* simplify recover

* fixed up tests

* implicit use of default wallet path working

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2020-07-21 23:49:04 -05:00

93 lines
2.9 KiB
Go

package v2
import (
"context"
"fmt"
"io/ioutil"
"strings"
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/validator/flags"
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/derived"
"github.com/urfave/cli/v2"
)
const phraseWordCount = 24
// RecoverWallet uses a menmonic seed phrase to recover a wallet into the path provided.
func RecoverWallet(cliCtx *cli.Context) error {
mnemonic, err := inputMnemonic(cliCtx)
if err != nil {
return errors.Wrap(err, "could not get mnemonic phrase")
}
wallet, err := NewWallet(cliCtx)
if err != nil {
return errors.Wrap(err, "could not create new wallet")
}
ctx := context.Background()
seedConfig, err := derived.SeedFileFromMnemonic(ctx, mnemonic, wallet.walletPassword)
if err != nil {
return errors.Wrap(err, "could not initialize new wallet seed file")
}
seedConfigFile, err := derived.MarshalEncryptedSeedFile(ctx, seedConfig)
if err != nil {
return errors.Wrap(err, "could not marshal encrypted wallet seed file")
}
keymanagerConfig, err := derived.MarshalConfigFile(ctx, derived.DefaultConfig())
if err != nil {
return errors.Wrap(err, "could not marshal keymanager config file")
}
if err := wallet.WriteKeymanagerConfigToDisk(ctx, keymanagerConfig); err != nil {
return errors.Wrap(err, "could not write keymanager config to disk")
}
if err := wallet.WriteEncryptedSeedToDisk(ctx, seedConfigFile); err != nil {
return errors.Wrap(err, "could not write encrypted wallet seed config to disk")
}
log.WithField("wallet-path", wallet.AccountsDir()).Infof(
"Successfully recovered HD wallet and saved configuration to disk. " +
"Make a new validator account with ./prysm.sh validator accounts-2 new",
)
return nil
}
func inputMnemonic(cliCtx *cli.Context) (string, error) {
if cliCtx.IsSet(flags.MnemonicFileFlag.Name) {
mnemonicFilePath := cliCtx.String(flags.MnemonicFileFlag.Name)
data, err := ioutil.ReadFile(mnemonicFilePath)
if err != nil {
return "", err
}
enteredMnemonic := string(data)
if err := validateMnemonic(enteredMnemonic); err != nil {
return "", errors.Wrap(err, "mnemonic phrase did not pass validation")
}
return enteredMnemonic, nil
}
prompt := promptui.Prompt{
Label: "Enter the wallet recovery seed phrase you would like to recover",
Validate: validateMnemonic,
}
menmonicPhrase, err := prompt.Run()
if err != nil {
return "", fmt.Errorf("could not determine wallet directory: %v", formatPromptError(err))
}
return menmonicPhrase, nil
}
func validateMnemonic(mnemonic string) error {
if strings.Trim(mnemonic, " ") == "" {
return errors.New("phrase cannot be empty")
}
words := strings.Split(mnemonic, " ")
for i, word := range words {
if strings.Trim(word, " ") == "" {
words = append(words[:i], words[i+1:]...)
}
}
if len(words) != phraseWordCount {
return fmt.Errorf("phrase must be %d words, entered %d", phraseWordCount, len(words))
}
return nil
}