2020-07-15 04:05:21 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-07-16 05:08:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
2020-07-15 04:05:21 +00:00
|
|
|
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
|
2020-07-21 02:05:23 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/derived"
|
2020-07-15 04:05:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/remote"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateWallet from user input with a desired keymanager. If a
|
|
|
|
// wallet already exists in the path, it suggests the user alternatives
|
|
|
|
// such as how to edit their existing wallet configuration.
|
|
|
|
func CreateWallet(cliCtx *cli.Context) error {
|
2020-07-24 00:43:01 +00:00
|
|
|
keymanagerKind, err := inputKeymanagerKind(cliCtx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w, err := NewWallet(cliCtx, keymanagerKind)
|
2020-07-28 19:23:53 +00:00
|
|
|
if err != nil && !errors.Is(err, ErrWalletExists) {
|
|
|
|
return errors.Wrap(err, "could not check if wallet directory exists")
|
|
|
|
}
|
|
|
|
if errors.Is(err, ErrWalletExists) {
|
|
|
|
return ErrWalletExists
|
2020-07-15 04:05:21 +00:00
|
|
|
}
|
2020-07-22 04:49:04 +00:00
|
|
|
switch w.KeymanagerKind() {
|
2020-07-15 04:05:21 +00:00
|
|
|
case v2keymanager.Direct:
|
2020-07-22 04:49:04 +00:00
|
|
|
if err = createDirectKeymanagerWallet(cliCtx, w); err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.Wrap(err, "could not initialize wallet with direct keymanager")
|
2020-07-15 04:05:21 +00:00
|
|
|
}
|
2020-07-22 04:49:04 +00:00
|
|
|
log.WithField("wallet-path", w.accountsPath).Infof(
|
2020-07-15 04:05:21 +00:00
|
|
|
"Successfully created wallet with on-disk keymanager configuration. " +
|
2020-07-23 03:10:23 +00:00
|
|
|
"Make a new validator account with ./prysm.sh validator accounts-v2 create",
|
2020-07-15 04:05:21 +00:00
|
|
|
)
|
|
|
|
case v2keymanager.Derived:
|
2020-07-22 04:49:04 +00:00
|
|
|
if err = createDerivedKeymanagerWallet(cliCtx, w); err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.Wrap(err, "could not initialize wallet with derived keymanager")
|
2020-07-21 02:05:23 +00:00
|
|
|
}
|
2020-07-22 04:49:04 +00:00
|
|
|
log.WithField("wallet-path", w.accountsPath).Infof(
|
2020-07-21 02:05:23 +00:00
|
|
|
"Successfully created HD wallet and saved configuration to disk. " +
|
2020-07-23 03:10:23 +00:00
|
|
|
"Make a new validator account with ./prysm.sh validator accounts-2 create",
|
2020-07-21 02:05:23 +00:00
|
|
|
)
|
2020-07-15 04:05:21 +00:00
|
|
|
case v2keymanager.Remote:
|
2020-07-22 04:49:04 +00:00
|
|
|
if err = createRemoteKeymanagerWallet(cliCtx, w); err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.Wrap(err, "could not initialize wallet with remote keymanager")
|
2020-07-15 04:05:21 +00:00
|
|
|
}
|
2020-07-22 04:49:04 +00:00
|
|
|
log.WithField("wallet-path", w.accountsPath).Infof(
|
2020-07-15 04:05:21 +00:00
|
|
|
"Successfully created wallet with remote keymanager configuration",
|
|
|
|
)
|
|
|
|
default:
|
2020-07-22 04:49:04 +00:00
|
|
|
return errors.Wrapf(err, "keymanager type %s is not supported", w.KeymanagerKind())
|
2020-07-15 04:05:21 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 04:49:04 +00:00
|
|
|
func createDirectKeymanagerWallet(cliCtx *cli.Context, wallet *Wallet) error {
|
2020-07-24 00:43:01 +00:00
|
|
|
if err := wallet.SaveWallet(); err != nil {
|
|
|
|
return errors.Wrap(err, "could not save wallet to disk")
|
|
|
|
}
|
2020-07-22 04:49:04 +00:00
|
|
|
keymanagerConfig, err := direct.MarshalConfigFile(context.Background(), direct.DefaultConfig())
|
2020-07-15 04:05:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not marshal keymanager config file")
|
|
|
|
}
|
2020-07-22 04:49:04 +00:00
|
|
|
if err := wallet.WriteKeymanagerConfigToDisk(context.Background(), keymanagerConfig); err != nil {
|
2020-07-15 04:05:21 +00:00
|
|
|
return errors.Wrap(err, "could not write keymanager config to disk")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 04:49:04 +00:00
|
|
|
func createDerivedKeymanagerWallet(cliCtx *cli.Context, wallet *Wallet) error {
|
|
|
|
skipMnemonicConfirm := cliCtx.Bool(flags.SkipMnemonicConfirmFlag.Name)
|
2020-07-21 02:05:23 +00:00
|
|
|
ctx := context.Background()
|
2020-07-24 00:43:01 +00:00
|
|
|
seedConfig, err := derived.InitializeWalletSeedFile(ctx, wallet.walletPassword, skipMnemonicConfirm)
|
2020-07-21 02:05:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not initialize new wallet seed file")
|
|
|
|
}
|
|
|
|
seedConfigFile, err := derived.MarshalEncryptedSeedFile(ctx, seedConfig)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not marshal encrypted wallet seed file")
|
|
|
|
}
|
|
|
|
keymanagerConfig, err := derived.MarshalConfigFile(ctx, derived.DefaultConfig())
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not marshal keymanager config file")
|
|
|
|
}
|
2020-07-24 00:43:01 +00:00
|
|
|
if err := wallet.SaveWallet(); err != nil {
|
|
|
|
return errors.Wrap(err, "could not save wallet to disk")
|
|
|
|
}
|
2020-07-21 02:05:23 +00:00
|
|
|
if err := wallet.WriteKeymanagerConfigToDisk(ctx, keymanagerConfig); err != nil {
|
|
|
|
return errors.Wrap(err, "could not write keymanager config to disk")
|
|
|
|
}
|
|
|
|
if err := wallet.WriteEncryptedSeedToDisk(ctx, seedConfigFile); err != nil {
|
|
|
|
return errors.Wrap(err, "could not write encrypted wallet seed config to disk")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 04:49:04 +00:00
|
|
|
func createRemoteKeymanagerWallet(cliCtx *cli.Context, wallet *Wallet) error {
|
2020-07-15 04:05:21 +00:00
|
|
|
conf, err := inputRemoteKeymanagerConfig(cliCtx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not input remote keymanager config")
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
keymanagerConfig, err := remote.MarshalConfigFile(ctx, conf)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not marshal config file")
|
|
|
|
}
|
2020-07-24 00:43:01 +00:00
|
|
|
if err := wallet.SaveWallet(); err != nil {
|
|
|
|
return errors.Wrap(err, "could not save wallet to disk")
|
|
|
|
}
|
2020-07-15 04:05:21 +00:00
|
|
|
if err := wallet.WriteKeymanagerConfigToDisk(ctx, keymanagerConfig); err != nil {
|
|
|
|
return errors.Wrap(err, "could not write keymanager config to disk")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|