prysm-pulse/validator/accounts/v2/accounts_create.go
Raul Jordan 60558b7970
Refactor Accounts and Keymanager Methods to Not Rely on CLI (#7135)
* keymanagers no longer use cli ctx
* rename important values to keymanageropts
* further refactor accounts methods to reduce cli dependency
* separating cli vs non cli methods for various accounts functions
* recover wallet cli vs non-cli mode
* ensure half of tests build
* accounts v2 package now builds
* full revamp wallet creation or opening wallets
* everything builds
* tests pass finally
* Merge branch 'master' into no-cli-keymanagers
* ensure commands work as expected
* Merge branch 'no-cli-keymanagers' of github.com:prysmaticlabs/prysm into no-cli-keymanagers
* further fix build
* account creation comments
* fix imports and comments
* fix up failing test
* fix build
* Update derived.go
* Update direct.go
* Update remote.go
* Fixed variable
* fix up red tests
* use Cli instead of CLI, fix tests, and address Radek's feedback
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
2020-08-31 19:46:45 +00:00

91 lines
2.9 KiB
Go

package v2
import (
"context"
"fmt"
"strings"
"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")
// CreateAccountConfig to run the create account function.
type CreateAccountConfig struct {
Wallet *Wallet
NumAccounts int64
}
// CreateAccountCli creates a new validator account from user input by opening
// a wallet from the user's specified path. This uses the CLI to extract information
// to perform account creation.
func CreateAccountCli(cliCtx *cli.Context) error {
wallet, err := OpenWalletOrElseCli(cliCtx, CreateAndSaveWalletCli)
if err != nil {
return err
}
numAccounts := cliCtx.Int64(flags.NumAccountsFlag.Name)
log.Info("Creating a new account...")
return CreateAccount(cliCtx.Context, &CreateAccountConfig{
Wallet: wallet,
NumAccounts: numAccounts,
})
}
// CreateAccount creates a new validator account from user input by opening
// a wallet from the user's specified path.
func CreateAccount(ctx context.Context, cfg *CreateAccountConfig) error {
keymanager, err := cfg.Wallet.InitializeKeymanager(ctx, 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")
}
switch cfg.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)
if cfg.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(cfg.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",
cfg.NumAccounts,
startNum,
startNum+uint64(cfg.NumAccounts)-1,
)
}
default:
return fmt.Errorf("keymanager kind %s not supported", cfg.Wallet.KeymanagerKind())
}
return nil
}