mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-24 12:27:18 +00:00
a15e0797e4
* add option to log rejected gossip message * add bip39 supported mnemonic languages * Revert "add option to log rejected gossip message" This reverts commit 9a3d4486f6c8a623d9eb6b3af1a5a89aa62ecf2f. * Add mnemonic language flag * Update go.mod * Simplify language mapping * Add test for setBip39Lang * Update go.mod * Improve language matching Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Run gazelle + fix maligned struct Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
104 lines
3.3 KiB
Go
104 lines
3.3 KiB
Go
package derived
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/v3/crypto/rand"
|
|
"github.com/prysmaticlabs/prysm/v3/io/prompt"
|
|
"github.com/tyler-smith/go-bip39"
|
|
"github.com/tyler-smith/go-bip39/wordlists"
|
|
)
|
|
|
|
const confirmationText = "Confirm you have written down the recovery words somewhere safe (offline) [y|Y]"
|
|
|
|
// MnemonicGenerator implements methods for creating
|
|
// mnemonic seed phrases in english using a given
|
|
// source of entropy such as a private key.
|
|
type MnemonicGenerator struct {
|
|
skipMnemonicConfirm bool
|
|
}
|
|
|
|
// GenerateAndConfirmMnemonic requires confirming the generated mnemonics.
|
|
func GenerateAndConfirmMnemonic(mnemonicLanguage string, 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")
|
|
}
|
|
setBip39Lang(mnemonicLanguage)
|
|
m := &MnemonicGenerator{
|
|
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 (_ *MnemonicGenerator) 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 *MnemonicGenerator) 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 := prompt.ValidatePrompt(os.Stdin, confirmationText, prompt.ValidateConfirmation)
|
|
if err != nil {
|
|
log.Errorf("Could not confirm acknowledgement of userprompt, 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, mnemonicLanguage, mnemonicPassphrase string) ([]byte, error) {
|
|
setBip39Lang(mnemonicLanguage)
|
|
if ok := bip39.IsMnemonicValid(mnemonic); !ok {
|
|
return nil, bip39.ErrInvalidMnemonic
|
|
}
|
|
return bip39.NewSeed(mnemonic, mnemonicPassphrase), nil
|
|
}
|
|
|
|
func setBip39Lang(lang string) {
|
|
wordlist := wordlists.English
|
|
allowedLanguages := map[string][]string{
|
|
"chinese_simplified": wordlists.ChineseSimplified,
|
|
"chinese_traditional": wordlists.ChineseTraditional,
|
|
"czech": wordlists.Czech,
|
|
"english": wordlists.English,
|
|
"french": wordlists.French,
|
|
"japanese": wordlists.Japanese,
|
|
"korean": wordlists.Korean,
|
|
"italian": wordlists.Italian,
|
|
"spanish": wordlists.Spanish,
|
|
}
|
|
|
|
if wl, ok := allowedLanguages[lang]; ok {
|
|
wordlist = wl
|
|
}
|
|
bip39.SetWordList(wordlist)
|
|
}
|