prysm-pulse/validator/accounts/v2/accounts_create.go
rkapka 89e279f9c8
Account commands cleanup (#7065)
* initial implementation of command
* Add second confirmation step
* Merge branch 'origin-master' into voluntary-exit-command
* fix variable name
* added unit tests
* Merge branch 'origin-master' into voluntary-exit-command
* add comment about stdin
* remove backup-related code from accounts_delete_test
* fix comments
* move filterPublicKeysFromUserInput to new account helper
* remove SkipMnemonicConfirmFlag
* Merge refs/heads/master into voluntary-exit-command
* Merge refs/heads/master into voluntary-exit-command
* Merge refs/heads/master into voluntary-exit-command
* Merge refs/heads/master into voluntary-exit-command
* Merge refs/heads/master into voluntary-exit-command
* Merge refs/heads/master into voluntary-exit-command
* Merge refs/heads/master into voluntary-exit-command
* code review fixes
* removed commented-out code
* Merge branch 'voluntary-exit-command' into account-commands-cleanup

# Conflicts:
#	shared/promptutil/prompt.go
* Merge branch 'origin-master' into account-commands-cleanup

# Conflicts:
#	shared/promptutil/prompt.go
#	validator/accounts/v2/BUILD.bazel
#	validator/accounts/v2/accounts_exit.go
#	validator/accounts/v2/accounts_exit_test.go
* Merge refs/heads/master into account-commands-cleanup
2020-08-20 19:14:03 +00:00

92 lines
3.1 KiB
Go

package v2
import (
"context"
"fmt"
"strings"
"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")
// CreateAccount creates a new validator account from user input by opening
// a wallet from the user's specified path.
func CreateAccount(cliCtx *cli.Context) error {
ctx := context.Background()
wallet, err := openOrCreateWallet(cliCtx, CreateWallet)
if err != nil {
return err
}
keymanager, err := wallet.InitializeKeymanager(cliCtx, false /* skip mnemonic confirm */)
if err != nil && strings.Contains(err.Error(), "invalid checksum") {
return errors.New("wrong wallet password entered")
}
if err != nil {
return errors.Wrap(err, "could not initialize keymanager")
}
log.Info("Creating a new account...")
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")
}
// Create a new validator account using the specified keymanager.
if _, err := km.CreateAccount(ctx); 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")
}
startNum := km.NextAccountNumber(ctx)
numAccounts := cliCtx.Int64(flags.NumAccountsFlag.Name)
if numAccounts == 1 {
if _, err := km.CreateAccount(ctx, true /*logAccountInfo*/); err != nil {
return errors.Wrap(err, "could not create account in wallet")
}
} else {
for i := 0; i < int(numAccounts); i++ {
if _, err := km.CreateAccount(ctx, false /*logAccountInfo*/); err != nil {
return errors.Wrap(err, "could not create account in wallet")
}
}
log.Infof("Successfully created %d accounts. Please use accounts-v2 list to view details for accounts %d through %d.", numAccounts, startNum, startNum+uint64(numAccounts)-1)
}
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
}