2020-07-01 21:30:01 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-08-10 18:14:10 +00:00
|
|
|
"strings"
|
2020-07-01 21:30:01 +00:00
|
|
|
|
|
|
|
"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")
|
|
|
|
|
2020-08-31 19:46:45 +00:00
|
|
|
// 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)
|
2020-07-28 16:06:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-31 19:46:45 +00:00
|
|
|
numAccounts := cliCtx.Int64(flags.NumAccountsFlag.Name)
|
|
|
|
log.Info("Creating a new account...")
|
|
|
|
return CreateAccount(cliCtx.Context, &CreateAccountConfig{
|
|
|
|
Wallet: wallet,
|
|
|
|
NumAccounts: numAccounts,
|
|
|
|
})
|
|
|
|
}
|
2020-08-20 19:14:03 +00:00
|
|
|
|
2020-08-31 19:46:45 +00:00
|
|
|
// 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 */)
|
2020-08-10 18:14:10 +00:00
|
|
|
if err != nil && strings.Contains(err.Error(), "invalid checksum") {
|
|
|
|
return errors.New("wrong wallet password entered")
|
|
|
|
}
|
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-08-31 19:46:45 +00:00
|
|
|
switch cfg.Wallet.KeymanagerKind() {
|
2020-07-22 04:49:04 +00:00
|
|
|
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.
|
2020-08-03 18:58:04 +00:00
|
|
|
if _, err := km.CreateAccount(ctx); err != nil {
|
2020-07-22 04:49:04 +00:00
|
|
|
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")
|
|
|
|
}
|
2020-07-27 14:03:30 +00:00
|
|
|
startNum := km.NextAccountNumber(ctx)
|
2020-08-31 19:46:45 +00:00
|
|
|
if cfg.NumAccounts == 1 {
|
2020-07-27 14:03:30 +00:00
|
|
|
if _, err := km.CreateAccount(ctx, true /*logAccountInfo*/); err != nil {
|
|
|
|
return errors.Wrap(err, "could not create account in wallet")
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-31 19:46:45 +00:00
|
|
|
for i := 0; i < int(cfg.NumAccounts); i++ {
|
2020-07-27 14:03:30 +00:00
|
|
|
if _, err := km.CreateAccount(ctx, false /*logAccountInfo*/); err != nil {
|
|
|
|
return errors.Wrap(err, "could not create account in wallet")
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 19:46:45 +00:00
|
|
|
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,
|
|
|
|
)
|
2020-07-22 04:49:04 +00:00
|
|
|
}
|
|
|
|
default:
|
2020-08-31 19:46:45 +00:00
|
|
|
return fmt.Errorf("keymanager kind %s not supported", cfg.Wallet.KeymanagerKind())
|
2020-07-01 21:30:01 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|