prysm-pulse/validator/accounts/v2/wallet_create.go
Raul Jordan cbc27e0f2e
Refactor Wallet Password Handling, Preventing Wallet Corruption (#6933)
* wallet should no longer deal with account passwords
* ensure tests are fixed
* Merge branch 'master' into corrupted-pass
* move mnemonic logic into right place
* rem fmts
* add fileutil
* gazelle
* imports
* move seed logic to derived
* fix tests
* imports
* gaz
* Merge refs/heads/master into corrupted-pass
* merge confs
* Merge refs/heads/master into corrupted-pass
* ivan's feedback
* Merge branch 'corrupted-pass' of github.com:prysmaticlabs/prysm into corrupted-pass
* gaz
* fix shared test
* Merge refs/heads/master into corrupted-pass
* resolve conflicts
* fix test build
2020-08-10 18:54:40 +00:00

110 lines
4.0 KiB
Go

package v2
import (
"context"
"github.com/pkg/errors"
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/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) (*Wallet, error) {
keymanagerKind, err := inputKeymanagerKind(cliCtx)
if err != nil {
return nil, err
}
w, err := NewWallet(cliCtx, keymanagerKind)
if err != nil && !errors.Is(err, ErrWalletExists) {
return nil, errors.Wrap(err, "could not check if wallet directory exists")
}
if errors.Is(err, ErrWalletExists) {
return nil, ErrWalletExists
}
switch w.KeymanagerKind() {
case v2keymanager.Direct:
if err = createDirectKeymanagerWallet(cliCtx, w); err != nil {
return nil, errors.Wrap(err, "could not initialize wallet with direct keymanager")
}
log.WithField("--wallet-dir", w.walletDir).Info(
"Successfully created wallet with on-disk keymanager configuration. " +
"Make a new validator account with ./prysm.sh validator accounts-v2 create",
)
case v2keymanager.Derived:
if err = createDerivedKeymanagerWallet(cliCtx, w); err != nil {
return nil, errors.Wrap(err, "could not initialize wallet with derived keymanager")
}
log.WithField("--wallet-dir", w.walletDir).Info(
"Successfully created HD wallet and saved configuration to disk. " +
"Make a new validator account with ./prysm.sh validator accounts-2 create",
)
case v2keymanager.Remote:
if err = createRemoteKeymanagerWallet(cliCtx, w); err != nil {
return nil, errors.Wrap(err, "could not initialize wallet with remote keymanager")
}
log.WithField("--wallet-dir", w.walletDir).Info(
"Successfully created wallet with remote keymanager configuration",
)
default:
return nil, errors.Wrapf(err, "keymanager type %s is not supported", w.KeymanagerKind())
}
return w, nil
}
func createDirectKeymanagerWallet(cliCtx *cli.Context, wallet *Wallet) error {
if wallet == nil {
return errors.New("nil wallet")
}
if err := wallet.SaveWallet(); err != nil {
return errors.Wrap(err, "could not save wallet to disk")
}
defaultConfig := direct.DefaultConfig()
keymanagerConfig, err := direct.MarshalConfigFile(context.Background(), defaultConfig)
if err != nil {
return errors.Wrap(err, "could not marshal keymanager config file")
}
if err := wallet.WriteKeymanagerConfigToDisk(context.Background(), keymanagerConfig); err != nil {
return errors.Wrap(err, "could not write keymanager config to disk")
}
return nil
}
func createDerivedKeymanagerWallet(cliCtx *cli.Context, wallet *Wallet) error {
ctx := context.Background()
keymanagerConfig, err := derived.MarshalConfigFile(ctx, derived.DefaultConfig())
if err != nil {
return errors.Wrap(err, "could not marshal keymanager config file")
}
if err := wallet.SaveWallet(); err != nil {
return errors.Wrap(err, "could not save wallet to disk")
}
if err := wallet.WriteKeymanagerConfigToDisk(ctx, keymanagerConfig); err != nil {
return errors.Wrap(err, "could not write keymanager config to disk")
}
return nil
}
func createRemoteKeymanagerWallet(cliCtx *cli.Context, wallet *Wallet) error {
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")
}
if err := wallet.SaveWallet(); err != nil {
return errors.Wrap(err, "could not save wallet to disk")
}
if err := wallet.WriteKeymanagerConfigToDisk(ctx, keymanagerConfig); err != nil {
return errors.Wrap(err, "could not write keymanager config to disk")
}
return nil
}