2020-07-21 02:05:23 +00:00
|
|
|
package derived
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-07-29 04:55:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/promptutil"
|
2020-07-21 02:05:23 +00:00
|
|
|
"github.com/tyler-smith/go-bip39"
|
|
|
|
)
|
|
|
|
|
2020-07-29 04:55:26 +00:00
|
|
|
const confirmationText = "Confirm you have written down the recovery words somewhere safe (offline) [y|Y]"
|
|
|
|
|
2020-07-21 02:05:23 +00:00
|
|
|
// SeedPhraseFactory defines a struct which
|
|
|
|
// can generate new seed phrases in human-readable
|
|
|
|
// format from a source of entropy in raw bytes. It
|
|
|
|
// also provides methods for verifying a user has successfully
|
|
|
|
// acknowledged the mnemonic phrase and written it down offline.
|
|
|
|
type SeedPhraseFactory interface {
|
|
|
|
Generate(data []byte) (string, error)
|
|
|
|
ConfirmAcknowledgement(phrase string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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",
|
|
|
|
)
|
2020-07-29 04:55:26 +00:00
|
|
|
fmt.Printf(
|
|
|
|
`=================Wallet Seed Recovery Phrase====================
|
2020-07-21 02:05:23 +00:00
|
|
|
|
|
|
|
%s
|
|
|
|
|
2020-07-29 04:55:26 +00:00
|
|
|
===================================================================`,
|
|
|
|
phrase)
|
|
|
|
fmt.Println("")
|
2020-07-21 02:05:23 +00:00
|
|
|
if m.skipMnemonicConfirm {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Confirm the user has written down the mnemonic phrase offline.
|
2020-07-29 04:55:26 +00:00
|
|
|
_, err := promptutil.ValidatePrompt(confirmationText, promptutil.ValidateConfirmation)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not confirm acknowledgement of prompt, please enter y")
|
2020-07-21 02:05:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|