2020-07-01 21:30:01 +00:00
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"
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-07-24 02:18:36 +00:00
// CreateAccount creates a new validator account from user input by opening
2020-07-15 04:05:21 +00:00
// a wallet from the user's specified path.
2020-07-24 02:18:36 +00:00
func CreateAccount ( cliCtx * cli . Context ) error {
2020-07-15 04:05:21 +00:00
ctx := context . Background ( )
2020-07-28 16:06:18 +00:00
walletDir , err := inputDirectory ( cliCtx , walletDirPromptText , flags . WalletDirFlag )
if err != nil {
return errors . Wrapf ( err , "Could not retrieve input directory" )
}
ok , err := hasDir ( walletDir )
if err != nil {
return err
}
// Create a new wallet if no directory exists.
if ! ok {
err = CreateWallet ( cliCtx )
if err != nil {
return errors . Wrapf ( err , "Could not create wallet" )
}
}
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" )
}
2020-07-23 03:10:23 +00:00
password , err := inputPassword ( cliCtx , newAccountPasswordPromptText , confirmPass )
2020-07-22 04:49:04 +00:00
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" )
}
2020-07-27 14:03:30 +00:00
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 )
2020-07-22 04:49:04 +00:00
}
default :
return fmt . Errorf ( "keymanager kind %s not supported" , wallet . KeymanagerKind ( ) )
2020-07-01 21:30:01 +00:00
}
return 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
}