2020-07-21 02:05:23 +00:00
|
|
|
package derived
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-08-20 17:53:09 +00:00
|
|
|
"os"
|
2020-07-21 02:05:23 +00:00
|
|
|
|
2020-11-16 22:26:04 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-09-15 22:55:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/crypto/rand"
|
2021-09-17 21:55:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/io/prompt"
|
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
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// GenerateAndConfirmMnemonic requires confirming the generated mnemonics.
|
2020-11-16 22:26:04 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-21 02:05:23 +00:00
|
|
|
// Generate a mnemonic seed phrase in english using a source of
|
|
|
|
// entropy given as raw bytes.
|
2021-12-07 17:52:39 +00:00
|
|
|
func (_ *EnglishMnemonicGenerator) Generate(data []byte) (string, error) {
|
2020-07-21 02:05:23 +00:00
|
|
|
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.
|
2021-09-17 21:55:24 +00:00
|
|
|
_, err := prompt.ValidatePrompt(os.Stdin, confirmationText, prompt.ValidateConfirmation)
|
2020-07-29 04:55:26 +00:00
|
|
|
if err != nil {
|
2021-09-17 21:55:24 +00:00
|
|
|
log.Errorf("Could not confirm acknowledgement of userprompt, please enter y")
|
2020-07-21 02:05:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-16 22:26:04 +00:00
|
|
|
|
2021-01-05 13:09:41 +00:00
|
|
|
// Uses the provided mnemonic seed phrase to generate the
|
|
|
|
// appropriate seed file for recovering a derived wallets.
|
2020-11-16 22:26:04 +00:00
|
|
|
func seedFromMnemonic(mnemonic, mnemonicPassphrase string) ([]byte, error) {
|
|
|
|
if ok := bip39.IsMnemonicValid(mnemonic); !ok {
|
|
|
|
return nil, bip39.ErrInvalidMnemonic
|
|
|
|
}
|
|
|
|
return bip39.NewSeed(mnemonic, mnemonicPassphrase), nil
|
|
|
|
}
|