prysm-pulse/validator/keymanager/derived/mnemonic.go
Raul Jordan 7449eba612
Refactor HD Wallets for Enhanced Security (#7821)
* begin hd wallet refactor

* further simplify the new derived keymanager

* make it almost a full wrapper around an imported keymanager

* fix up the EIP test

* deprecated derived

* fixing keymanager tests

* fix up derived tests

* refactor initialize keymanager

* simplify hd

* pass some tests

* pass accounts list test

* gaz

* regenerate protos without create account privilege

* enforce account recovery on wallet create

* allow accounts delete to work

* remove mentions of accounts create

* resolve comments and go mod

* fix up tests

* build fixes

* remove insecure warning

* revert

* fix proto file

* remove create account message

* gaz

* remove account create

* update web api protos

* fix up imports

* change func sig

* tidy

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2020-11-16 22:26:04 +00:00

82 lines
2.5 KiB
Go

package derived
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/shared/promptutil"
"github.com/prysmaticlabs/prysm/shared/rand"
"github.com/tyler-smith/go-bip39"
)
const confirmationText = "Confirm you have written down the recovery words somewhere safe (offline) [y|Y]"
// EnglishMnemonicGenerator implements methods for creating
// mnemonic seed phrases in english using a given
// source of entropy such as a private key.
type EnglishMnemonicGenerator struct {
skipMnemonicConfirm bool
}
func GenerateAndConfirmMnemonic(
skipMnemonicConfirm bool,
) (string, error) {
mnemonicRandomness := make([]byte, 32)
if _, err := rand.NewGenerator().Read(mnemonicRandomness); err != nil {
return "", errors.Wrap(err, "could not initialize mnemonic source of randomness")
}
m := &EnglishMnemonicGenerator{
skipMnemonicConfirm: skipMnemonicConfirm,
}
phrase, err := m.Generate(mnemonicRandomness)
if err != nil {
return "", errors.Wrap(err, "could not generate wallet seed")
}
if err := m.ConfirmAcknowledgement(phrase); err != nil {
return "", errors.Wrap(err, "could not confirm mnemonic acknowledgement")
}
return phrase, nil
}
// Generate a mnemonic seed phrase in english using a source of
// entropy given as raw bytes.
func (m *EnglishMnemonicGenerator) Generate(data []byte) (string, error) {
return bip39.NewMnemonic(data)
}
// ConfirmAcknowledgement displays the mnemonic phrase to the user
// and confirms the user has written down the phrase securely offline.
func (m *EnglishMnemonicGenerator) ConfirmAcknowledgement(phrase string) error {
log.Info(
"Write down the sentence below, as it is your only " +
"means of recovering your wallet",
)
fmt.Printf(
`=================Wallet Seed Recovery Phrase====================
%s
===================================================================`,
phrase)
fmt.Println("")
if m.skipMnemonicConfirm {
return nil
}
// Confirm the user has written down the mnemonic phrase offline.
_, err := promptutil.ValidatePrompt(os.Stdin, confirmationText, promptutil.ValidateConfirmation)
if err != nil {
log.Errorf("Could not confirm acknowledgement of prompt, please enter y")
}
return nil
}
//Uses the provided mnemonic seed phrase to generate the
//appropriate seed file for recovering a derived wallets.
func seedFromMnemonic(mnemonic, mnemonicPassphrase string) ([]byte, error) {
if ok := bip39.IsMnemonicValid(mnemonic); !ok {
return nil, bip39.ErrInvalidMnemonic
}
return bip39.NewSeed(mnemonic, mnemonicPassphrase), nil
}