prysm-pulse/validator/accounts/wallet_recover.go

77 lines
2.3 KiB
Go
Raw Normal View History

package accounts
import (
"context"
"strings"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v3/validator/accounts/wallet"
"github.com/prysmaticlabs/prysm/v3/validator/keymanager"
"github.com/prysmaticlabs/prysm/v3/validator/keymanager/derived"
)
const (
phraseWordCount = 24
)
var (
ErrIncorrectWordNumber = errors.New("incorrect number of words provided")
ErrEmptyMnemonic = errors.New("phrase cannot be empty")
)
// WalletRecover uses a menmonic seed phrase to recover a wallet into the path provided.
func (acm *AccountsCLIManager) WalletRecover(ctx context.Context) (*wallet.Wallet, error) {
// Ensure that the wallet directory does not contain a wallet already
dirExists, err := wallet.Exists(acm.walletDir)
if err != nil {
return nil, err
}
if dirExists {
return nil, errors.New("a wallet already exists at this location. Please input an" +
" alternative location for the new wallet or remove the current wallet")
}
w := wallet.New(&wallet.Config{
WalletDir: acm.walletDir,
KeymanagerKind: keymanager.Derived,
WalletPassword: acm.walletPassword,
})
if err := w.SaveWallet(); err != nil {
return nil, errors.Wrap(err, "could not save wallet to disk")
}
km, err := derived.NewKeymanager(ctx, &derived.SetupConfig{
Wallet: w,
ListenForChanges: false,
})
if err != nil {
return nil, errors.Wrap(err, "could not make keymanager for given phrase")
}
if err := km.RecoverAccountsFromMnemonic(ctx, acm.mnemonic, acm.mnemonic25thWord, acm.numAccounts); err != nil {
return nil, err
}
log.WithField("wallet-path", w.AccountsDir()).Infof(
"Successfully recovered HD wallet with %d accounts. Please use `accounts list` to view details for your accounts",
acm.numAccounts,
)
return w, nil
}
Web Backend Recover Wallet (#8679) * recover wallet rpc support - first attempt * removed redundant check * separate createwallet into imported and derived. Recover is derived * added unit test for recover. Recover is the createWallet for derived * so proto does CamelCase! * fixed issues related to unit testing * expose ValidateMnemonic from accounts to be used by the rpc module * added Mnemonic25Support and test to ensure it is not et is not empty * added skipMnemonic25thword support and unit test * Update proto/validator/accounts/v2/web_api.proto Proper naming convention Co-authored-by: terence tsao <terence@prysmaticlabs.com> * ran goimports,changed variable to SkipMnemonic_25ThWord * Update validator/rpc/wallet.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update validator/rpc/wallet.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update validator/rpc/wallet.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update validator/rpc/wallet.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update validator/rpc/wallet.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update validator/rpc/wallet.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update validator/rpc/wallet.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * fixed variable and text msgs naming convention as per the review * added unit test for strong password on recover * Update proto/validator/accounts/v2/web_api.proto Co-authored-by: Nishant Das <nish1993@hotmail.com> * Update validator/rpc/wallet.go Co-authored-by: Nishant Das <nish1993@hotmail.com> * goimports -w on root proto * added comments after exposing ValidateMnemonic to by used by rpc.RecoverWallet * language should be case insensitive * need to goimports before update protobuf scripts * Update validator/rpc/wallet.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update validator/rpc/wallet.go comments need to be consistant Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update validator/rpc/wallet_test.go consistent comments Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * fix comments * remove the skipMnemonic from rpc, just check if passphrase is empyt * defer call to set the writePassword flag on web boarding * gofmt * fixed the password write test after recovering a wallet. Needed to have two defers. One to set to true then one back to false * restore powchain.pb.go and finalized_block_root_container.pb.go * revert beacon messages.pb.go * revert unrelated files * revert unrelated files * revert unrelated files * unlreated files * restored the imports * Update validator/rpc/wallet_test.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> Co-authored-by: Nishant Das <nish1993@hotmail.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2021-04-05 20:42:03 +00:00
// ValidateMnemonic ensures that it is not empty and that the count of the words are
// as specified(currently 24).
func ValidateMnemonic(mnemonic string) error {
if strings.Trim(mnemonic, " ") == "" {
return ErrEmptyMnemonic
}
words := strings.Split(mnemonic, " ")
validWordCount := 0
for _, word := range words {
if strings.Trim(word, " ") == "" {
continue
}
validWordCount += 1
}
if validWordCount != phraseWordCount {
return errors.Wrapf(ErrIncorrectWordNumber, "phrase must be %d words, entered %d", phraseWordCount, validWordCount)
}
return nil
}