prysm-pulse/validator/accounts/v2/new.go
Ivan Martinez c72db6f96a
Accounts V2: Refactor prompts, deduplicate into directory and password (#6674)
* Refactor input code, deduplicate into directory and password
* Change to filepath
* Rename
* Make consts visible to whole validator package
* Fix
* Try commands and fix
* Fix defaults
* Fixes
* Merge branch 'master' into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Remove consts and add comment
* Merge branch 'refactor-input-prompts' of github.com:prysmaticlabs/prysm into refactor-input-prompts
* Undo
* Fixes
* Update validator/accounts/v2/prompt.go
* Merge refs/heads/master into refactor-input-prompts
* gofmt
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Merge branch 'master' into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Fix
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Accounts V2: Simplify Wallet Save/Read To and From Disk Functions (#6686)

* simplify wallet functions

* fix build

* futher simplify wallet

* simplify read/write methods

* move direct functions to direct keymanager

* further move direct km specific funcs

* cleanup

* simplify the direct tests

* fixed tests

* lint

* further simplify

* tidy

* fix config write

* fixed test

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
* conflict resolution
* trim
2020-07-23 03:10:23 +00:00

80 lines
2.6 KiB
Go

package v2
import (
"context"
"fmt"
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/validator/flags"
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/derived"
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var log = logrus.WithField("prefix", "accounts-v2")
// NewAccount creates a new validator account from user input by opening
// a wallet from the user's specified path.
func NewAccount(cliCtx *cli.Context) error {
ctx := context.Background()
wallet, err := OpenWallet(cliCtx)
if err != nil {
return errors.Wrap(err, "could not open wallet")
}
skipMnemonicConfirm := cliCtx.Bool(flags.SkipMnemonicConfirmFlag.Name)
keymanager, err := wallet.InitializeKeymanager(ctx, skipMnemonicConfirm)
if err != nil {
return errors.Wrap(err, "could not initialize keymanager")
}
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 := inputPassword(cliCtx, newAccountPasswordPromptText, confirmPass)
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())
}
return nil
}
func inputKeymanagerKind(cliCtx *cli.Context) (v2keymanager.Kind, error) {
if cliCtx.IsSet(flags.KeymanagerKindFlag.Name) {
return v2keymanager.ParseKind(cliCtx.String(flags.KeymanagerKindFlag.Name))
}
promptSelect := promptui.Select{
Label: "Select a type of wallet",
Items: []string{
keymanagerKindSelections[v2keymanager.Derived],
keymanagerKindSelections[v2keymanager.Direct],
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
}