2020-07-01 21:30:01 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-07-14 23:00:58 +00:00
|
|
|
"io/ioutil"
|
2020-07-01 21:30:01 +00:00
|
|
|
"path"
|
|
|
|
"unicode"
|
|
|
|
|
2020-07-22 04:49:04 +00:00
|
|
|
"github.com/logrusorgru/aurora"
|
2020-07-01 21:30:01 +00:00
|
|
|
"github.com/manifoldco/promptui"
|
|
|
|
strongPasswords "github.com/nbutton23/zxcvbn-go"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
|
|
|
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
|
2020-07-22 04:49:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/derived"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct"
|
2020-07-01 21:30:01 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logrus.WithField("prefix", "accounts-v2")
|
|
|
|
|
|
|
|
const (
|
|
|
|
minPasswordLength = 8
|
|
|
|
// Min password score of 3 out of 5 based on the https://github.com/nbutton23/zxcvbn-go
|
|
|
|
// library for strong-entropy password computation.
|
|
|
|
minPasswordScore = 3
|
|
|
|
)
|
|
|
|
|
2020-07-15 04:05:21 +00:00
|
|
|
// NewAccount creates a new validator account from user input by opening
|
|
|
|
// a wallet from the user's specified path.
|
2020-07-01 21:30:01 +00:00
|
|
|
func NewAccount(cliCtx *cli.Context) error {
|
2020-07-15 04:05:21 +00:00
|
|
|
ctx := context.Background()
|
2020-07-22 04:49:04 +00:00
|
|
|
wallet, err := OpenWallet(cliCtx)
|
2020-07-15 04:05:21 +00:00
|
|
|
if err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.Wrap(err, "could not open wallet")
|
2020-07-01 21:30:01 +00:00
|
|
|
}
|
2020-07-21 02:05:23 +00:00
|
|
|
skipMnemonicConfirm := cliCtx.Bool(flags.SkipMnemonicConfirmFlag.Name)
|
2020-07-15 04:05:21 +00:00
|
|
|
keymanager, err := wallet.InitializeKeymanager(ctx, skipMnemonicConfirm)
|
2020-07-01 21:30:01 +00:00
|
|
|
if err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.Wrap(err, "could not initialize keymanager")
|
2020-07-01 21:30:01 +00:00
|
|
|
}
|
2020-07-22 04:49:04 +00:00
|
|
|
switch wallet.KeymanagerKind() {
|
|
|
|
case v2keymanager.Remote:
|
|
|
|
return errors.New("cannot create a new account for a remote keymanager")
|
|
|
|
case v2keymanager.Direct:
|
|
|
|
km, ok := keymanager.(*direct.Keymanager)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("not a direct keymanager")
|
|
|
|
}
|
|
|
|
password, err := inputNewAccountPassword(cliCtx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not input new account password")
|
|
|
|
}
|
|
|
|
// Create a new validator account using the specified keymanager.
|
|
|
|
if _, err := km.CreateAccount(ctx, password); err != nil {
|
|
|
|
return errors.Wrap(err, "could not create account in wallet")
|
|
|
|
}
|
|
|
|
case v2keymanager.Derived:
|
|
|
|
km, ok := keymanager.(*derived.Keymanager)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("not a derived keymanager")
|
|
|
|
}
|
|
|
|
if _, err := km.CreateAccount(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "could not create account in wallet")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("keymanager kind %s not supported", wallet.KeymanagerKind())
|
2020-07-01 21:30:01 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func inputWalletDir(cliCtx *cli.Context) (string, error) {
|
|
|
|
walletDir := cliCtx.String(flags.WalletDirFlag.Name)
|
2020-07-14 23:00:58 +00:00
|
|
|
if cliCtx.IsSet(flags.WalletDirFlag.Name) {
|
|
|
|
return walletDir, nil
|
|
|
|
}
|
|
|
|
|
2020-07-01 21:30:01 +00:00
|
|
|
if walletDir == flags.DefaultValidatorDir() {
|
2020-07-08 05:01:09 +00:00
|
|
|
walletDir = path.Join(walletDir, WalletDefaultDirName)
|
2020-07-22 04:49:04 +00:00
|
|
|
ok, err := hasDir(walletDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "could not check if wallet dir %s exists", walletDir)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
au := aurora.NewAurora(true)
|
|
|
|
log.Infof("%s %s", au.BrightMagenta("(wallet path)"), walletDir)
|
|
|
|
return walletDir, nil
|
|
|
|
}
|
2020-07-01 21:30:01 +00:00
|
|
|
}
|
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: "Enter a wallet directory",
|
|
|
|
Validate: validateDirectoryPath,
|
|
|
|
Default: walletDir,
|
|
|
|
}
|
|
|
|
walletPath, err := prompt.Run()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not determine wallet directory: %v", formatPromptError(err))
|
|
|
|
}
|
2020-07-15 04:05:21 +00:00
|
|
|
ok, err := hasDir(walletPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "could not check if wallet dir %s exists", walletDir)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return walletPath, ErrNoWalletFound
|
|
|
|
}
|
2020-07-01 21:30:01 +00:00
|
|
|
return walletPath, nil
|
|
|
|
}
|
|
|
|
|
2020-07-17 08:21:16 +00:00
|
|
|
func inputKeymanagerKind(cliCtx *cli.Context) (v2keymanager.Kind, error) {
|
|
|
|
if cliCtx.IsSet(flags.KeymanagerKindFlag.Name) {
|
|
|
|
return v2keymanager.ParseKind(cliCtx.String(flags.KeymanagerKindFlag.Name))
|
|
|
|
}
|
2020-07-01 21:30:01 +00:00
|
|
|
promptSelect := promptui.Select{
|
|
|
|
Label: "Select a type of wallet",
|
|
|
|
Items: []string{
|
|
|
|
keymanagerKindSelections[v2keymanager.Derived],
|
2020-07-22 04:49:04 +00:00
|
|
|
keymanagerKindSelections[v2keymanager.Direct],
|
2020-07-01 21:30:01 +00:00
|
|
|
keymanagerKindSelections[v2keymanager.Remote],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
selection, _, err := promptSelect.Run()
|
|
|
|
if err != nil {
|
|
|
|
return v2keymanager.Direct, fmt.Errorf("could not select wallet type: %v", formatPromptError(err))
|
|
|
|
}
|
|
|
|
return v2keymanager.Kind(selection), nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 02:41:39 +00:00
|
|
|
func inputNewWalletPassword(cliCtx *cli.Context) (string, error) {
|
|
|
|
if cliCtx.IsSet(flags.PasswordFileFlag.Name) {
|
|
|
|
passwordFilePath := cliCtx.String(flags.PasswordFileFlag.Name)
|
|
|
|
data, err := ioutil.ReadFile(passwordFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
enteredPassword := string(data)
|
|
|
|
if err := validatePasswordInput(enteredPassword); err != nil {
|
|
|
|
return "", errors.Wrap(err, "password did not pass validation")
|
|
|
|
}
|
|
|
|
return enteredPassword, nil
|
|
|
|
}
|
|
|
|
|
2020-07-21 02:05:23 +00:00
|
|
|
var hasValidPassword bool
|
|
|
|
var walletPassword string
|
|
|
|
var err error
|
|
|
|
for !hasValidPassword {
|
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: "New wallet password",
|
|
|
|
Validate: validatePasswordInput,
|
|
|
|
Mask: '*',
|
|
|
|
}
|
|
|
|
|
|
|
|
walletPassword, err = prompt.Run()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not read wallet password: %v", formatPromptError(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt = promptui.Prompt{
|
|
|
|
Label: "Confirm password",
|
|
|
|
Mask: '*',
|
|
|
|
}
|
|
|
|
confirmPassword, err := prompt.Run()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not read password confirmation: %v", formatPromptError(err))
|
|
|
|
}
|
|
|
|
if walletPassword != confirmPassword {
|
|
|
|
log.Error("Passwords do not match")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hasValidPassword = true
|
|
|
|
}
|
|
|
|
return walletPassword, nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 04:49:04 +00:00
|
|
|
func inputExistingWalletPassword(cliCtx *cli.Context) (string, error) {
|
|
|
|
if cliCtx.IsSet(flags.PasswordFileFlag.Name) {
|
|
|
|
passwordFilePath := cliCtx.String(flags.PasswordFileFlag.Name)
|
|
|
|
data, err := ioutil.ReadFile(passwordFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(data), nil
|
|
|
|
}
|
2020-07-21 02:05:23 +00:00
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: "Wallet password",
|
|
|
|
Validate: validatePasswordInput,
|
|
|
|
Mask: '*',
|
|
|
|
}
|
|
|
|
|
|
|
|
walletPassword, err := prompt.Run()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not read wallet password: %v", formatPromptError(err))
|
|
|
|
}
|
|
|
|
return walletPassword, nil
|
|
|
|
}
|
|
|
|
|
2020-07-14 23:00:58 +00:00
|
|
|
func inputNewAccountPassword(cliCtx *cli.Context) (string, error) {
|
|
|
|
if cliCtx.IsSet(flags.PasswordFileFlag.Name) {
|
|
|
|
passwordFilePath := cliCtx.String(flags.PasswordFileFlag.Name)
|
|
|
|
data, err := ioutil.ReadFile(passwordFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
enteredPassword := string(data)
|
|
|
|
if err := validatePasswordInput(enteredPassword); err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return "", errors.Wrap(err, "password did not pass validation")
|
2020-07-14 23:00:58 +00:00
|
|
|
}
|
|
|
|
return enteredPassword, nil
|
|
|
|
}
|
|
|
|
|
2020-07-01 21:30:01 +00:00
|
|
|
var hasValidPassword bool
|
|
|
|
var walletPassword string
|
2020-07-03 18:49:16 +00:00
|
|
|
var err error
|
2020-07-01 21:30:01 +00:00
|
|
|
for !hasValidPassword {
|
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: "New account password",
|
|
|
|
Validate: validatePasswordInput,
|
|
|
|
Mask: '*',
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:49:16 +00:00
|
|
|
walletPassword, err = prompt.Run()
|
2020-07-01 21:30:01 +00:00
|
|
|
if err != nil {
|
2020-07-21 02:05:23 +00:00
|
|
|
return "", fmt.Errorf("could not read account password: %v", formatPromptError(err))
|
2020-07-01 21:30:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prompt = promptui.Prompt{
|
|
|
|
Label: "Confirm password",
|
|
|
|
Mask: '*',
|
|
|
|
}
|
|
|
|
confirmPassword, err := prompt.Run()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not read password confirmation: %v", formatPromptError(err))
|
|
|
|
}
|
|
|
|
if walletPassword != confirmPassword {
|
|
|
|
log.Error("Passwords do not match")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hasValidPassword = true
|
|
|
|
}
|
|
|
|
return walletPassword, nil
|
|
|
|
}
|
|
|
|
|
2020-07-13 21:37:18 +00:00
|
|
|
func inputPasswordForAccount(_ *cli.Context, accountName string) (string, error) {
|
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: fmt.Sprintf("Enter password for account %s", accountName),
|
|
|
|
Mask: '*',
|
|
|
|
}
|
|
|
|
|
|
|
|
walletPassword, err := prompt.Run()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not read wallet password: %v", formatPromptError(err))
|
|
|
|
}
|
|
|
|
return walletPassword, nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 04:49:04 +00:00
|
|
|
func inputPasswordsDirectory(cliCtx *cli.Context) (string, error) {
|
2020-07-01 21:30:01 +00:00
|
|
|
passwordsDir := cliCtx.String(flags.WalletPasswordsDirFlag.Name)
|
2020-07-14 23:00:58 +00:00
|
|
|
if cliCtx.IsSet(flags.WalletPasswordsDirFlag.Name) {
|
2020-07-22 04:49:04 +00:00
|
|
|
return passwordsDir, nil
|
2020-07-14 23:00:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 21:30:01 +00:00
|
|
|
if passwordsDir == flags.DefaultValidatorDir() {
|
2020-07-08 05:01:09 +00:00
|
|
|
passwordsDir = path.Join(passwordsDir, PasswordsDefaultDirName)
|
2020-07-22 04:49:04 +00:00
|
|
|
ok, err := hasDir(passwordsDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "could not check if passwords directory exists")
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
au := aurora.NewAurora(true)
|
|
|
|
log.Infof("%s %s", au.BrightMagenta("(account passwords path)"), passwordsDir)
|
|
|
|
return passwordsDir, nil
|
|
|
|
}
|
2020-07-01 21:30:01 +00:00
|
|
|
}
|
|
|
|
prompt := promptui.Prompt{
|
2020-07-15 04:05:21 +00:00
|
|
|
Label: "Directory where passwords will be stored",
|
2020-07-01 21:30:01 +00:00
|
|
|
Validate: validateDirectoryPath,
|
|
|
|
Default: passwordsDir,
|
|
|
|
}
|
|
|
|
passwordsPath, err := prompt.Run()
|
|
|
|
if err != nil {
|
2020-07-22 04:49:04 +00:00
|
|
|
return "", fmt.Errorf("could not determine passwords directory: %v", formatPromptError(err))
|
2020-07-01 21:30:01 +00:00
|
|
|
}
|
2020-07-22 04:49:04 +00:00
|
|
|
return passwordsPath, nil
|
2020-07-01 21:30:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate a strong password input for new accounts,
|
|
|
|
// including a min length, at least 1 number and at least
|
|
|
|
// 1 special character.
|
|
|
|
func validatePasswordInput(input string) error {
|
|
|
|
var (
|
|
|
|
hasMinLen = false
|
|
|
|
hasLetter = false
|
|
|
|
hasNumber = false
|
|
|
|
hasSpecial = false
|
|
|
|
)
|
|
|
|
if len(input) >= minPasswordLength {
|
|
|
|
hasMinLen = true
|
|
|
|
}
|
|
|
|
for _, char := range input {
|
|
|
|
switch {
|
|
|
|
case unicode.IsLetter(char):
|
|
|
|
hasLetter = true
|
|
|
|
case unicode.IsNumber(char):
|
|
|
|
hasNumber = true
|
|
|
|
case unicode.IsPunct(char) || unicode.IsSymbol(char):
|
|
|
|
hasSpecial = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !(hasMinLen && hasLetter && hasNumber && hasSpecial) {
|
|
|
|
return errors.New(
|
|
|
|
"password must have more than 8 characters, at least 1 special character, and 1 number",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
strength := strongPasswords.PasswordStrength(input, nil)
|
|
|
|
if strength.Score < minPasswordScore {
|
|
|
|
return errors.New(
|
|
|
|
"password is too easy to guess, try a stronger password",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateDirectoryPath(input string) error {
|
|
|
|
if len(input) == 0 {
|
|
|
|
return errors.New("directory path must not be empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatPromptError(err error) error {
|
|
|
|
switch err {
|
|
|
|
case promptui.ErrAbort:
|
|
|
|
return errors.New("wallet creation aborted, closing")
|
|
|
|
case promptui.ErrInterrupt:
|
|
|
|
return errors.New("keyboard interrupt, closing")
|
|
|
|
case promptui.ErrEOF:
|
|
|
|
return errors.New("no input received, closing")
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|