2020-10-15 22:31:52 +00:00
|
|
|
package accounts
|
2020-07-08 19:21:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-07-23 03:10:23 +00:00
|
|
|
"path/filepath"
|
2020-08-10 18:14:10 +00:00
|
|
|
"strings"
|
2020-07-08 19:21:54 +00:00
|
|
|
|
|
|
|
"github.com/logrusorgru/aurora"
|
|
|
|
"github.com/pkg/errors"
|
2020-07-24 00:43:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/petnames"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
2020-07-08 19:21:54 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/derived"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/direct"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/remote"
|
2020-07-08 19:21:54 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2020-08-31 19:46:45 +00:00
|
|
|
// ListAccountsCli displays all available validator accounts in a Prysm wallet.
|
|
|
|
func ListAccountsCli(cliCtx *cli.Context) error {
|
2020-09-17 01:34:42 +00:00
|
|
|
w, err := wallet.OpenWalletOrElseCli(cliCtx, func(cliCtx *cli.Context) (*wallet.Wallet, error) {
|
2020-08-31 19:46:45 +00:00
|
|
|
return nil, errors.New(
|
|
|
|
"no wallet found, no accounts to list",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-07-29 01:20:13 +00:00
|
|
|
return errors.Wrap(err, "could not open wallet")
|
2020-07-08 19:21:54 +00:00
|
|
|
}
|
2020-10-15 22:31:52 +00:00
|
|
|
km, err := w.InitializeKeymanager(cliCtx.Context, true /* 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-08 19:21:54 +00:00
|
|
|
if err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.Wrap(err, "could not initialize keymanager")
|
2020-07-08 19:21:54 +00:00
|
|
|
}
|
|
|
|
showDepositData := cliCtx.Bool(flags.ShowDepositDataFlag.Name)
|
2020-10-15 16:08:52 +00:00
|
|
|
showPrivateKeys := cliCtx.Bool(flags.ShowPrivateKeysFlag.Name)
|
2020-09-17 01:34:42 +00:00
|
|
|
switch w.KeymanagerKind() {
|
2020-10-15 22:31:52 +00:00
|
|
|
case keymanager.Direct:
|
|
|
|
km, ok := km.(*direct.Keymanager)
|
2020-07-21 05:06:11 +00:00
|
|
|
if !ok {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.New("could not assert keymanager interface to concrete type")
|
2020-07-21 05:06:11 +00:00
|
|
|
}
|
2020-10-15 16:08:52 +00:00
|
|
|
if err := listDirectKeymanagerAccounts(cliCtx.Context, showDepositData, showPrivateKeys, km); err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.Wrap(err, "could not list validator accounts with direct keymanager")
|
2020-07-08 19:21:54 +00:00
|
|
|
}
|
2020-10-15 22:31:52 +00:00
|
|
|
case keymanager.Derived:
|
|
|
|
km, ok := km.(*derived.Keymanager)
|
2020-07-21 05:06:11 +00:00
|
|
|
if !ok {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.New("could not assert keymanager interface to concrete type")
|
2020-07-21 05:06:11 +00:00
|
|
|
}
|
2020-10-15 16:08:52 +00:00
|
|
|
if err := listDerivedKeymanagerAccounts(cliCtx.Context, showDepositData, showPrivateKeys, km); err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.Wrap(err, "could not list validator accounts with derived keymanager")
|
2020-07-21 05:06:11 +00:00
|
|
|
}
|
2020-10-15 22:31:52 +00:00
|
|
|
case keymanager.Remote:
|
|
|
|
km, ok := km.(*remote.Keymanager)
|
2020-07-24 00:43:01 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("could not assert keymanager interface to concrete type")
|
|
|
|
}
|
2020-09-17 01:34:42 +00:00
|
|
|
if err := listRemoteKeymanagerAccounts(cliCtx.Context, w, km, km.KeymanagerOpts()); err != nil {
|
2020-07-24 00:43:01 +00:00
|
|
|
return errors.Wrap(err, "could not list validator accounts with remote keymanager")
|
|
|
|
}
|
2020-07-08 19:21:54 +00:00
|
|
|
default:
|
2020-09-17 01:34:42 +00:00
|
|
|
return fmt.Errorf("keymanager kind %s not yet supported", w.KeymanagerKind().String())
|
2020-07-08 19:21:54 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listDirectKeymanagerAccounts(
|
2020-09-09 09:48:52 +00:00
|
|
|
ctx context.Context,
|
2020-10-15 22:31:52 +00:00
|
|
|
showDepositData,
|
2020-10-15 16:08:52 +00:00
|
|
|
showPrivateKeys bool,
|
2020-07-21 05:06:11 +00:00
|
|
|
keymanager *direct.Keymanager,
|
2020-07-08 19:21:54 +00:00
|
|
|
) error {
|
|
|
|
// We initialize the wallet's keymanager.
|
2020-07-23 02:12:51 +00:00
|
|
|
accountNames, err := keymanager.ValidatingAccountNames()
|
2020-07-08 19:21:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch account names")
|
|
|
|
}
|
|
|
|
numAccounts := au.BrightYellow(len(accountNames))
|
2020-08-02 20:02:04 +00:00
|
|
|
fmt.Printf("(keymanager kind) %s\n", au.BrightGreen("non-HD wallet").Bold())
|
2020-07-08 19:21:54 +00:00
|
|
|
fmt.Println("")
|
|
|
|
if len(accountNames) == 1 {
|
|
|
|
fmt.Printf("Showing %d validator account\n", numAccounts)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Showing %d validator accounts\n", numAccounts)
|
|
|
|
}
|
|
|
|
fmt.Println(
|
|
|
|
au.BrightRed("View the eth1 deposit transaction data for your accounts " +
|
2020-10-15 22:31:52 +00:00
|
|
|
"by running `validator accounts list --show-deposit-data"),
|
2020-07-08 19:21:54 +00:00
|
|
|
)
|
|
|
|
|
2020-07-23 02:12:51 +00:00
|
|
|
pubKeys, err := keymanager.FetchValidatingPublicKeys(ctx)
|
2020-07-08 19:21:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch validating public keys")
|
|
|
|
}
|
2020-10-15 16:08:52 +00:00
|
|
|
var privateKeys [][32]byte
|
|
|
|
if showPrivateKeys {
|
|
|
|
privateKeys, err = keymanager.FetchValidatingPrivateKeys(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch private keys")
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 19:21:54 +00:00
|
|
|
for i := 0; i < len(accountNames); i++ {
|
|
|
|
fmt.Println("")
|
2020-08-02 20:02:04 +00:00
|
|
|
fmt.Printf("%s | %s\n", au.BrightBlue(fmt.Sprintf("Account %d", i)).Bold(), au.BrightGreen(accountNames[i]).Bold())
|
2020-07-28 19:23:53 +00:00
|
|
|
fmt.Printf("%s %#x\n", au.BrightMagenta("[validating public key]").Bold(), pubKeys[i])
|
2020-10-15 16:08:52 +00:00
|
|
|
if showPrivateKeys {
|
|
|
|
fmt.Printf("%s %#x\n", au.BrightRed("[validating private key]").Bold(), privateKeys[i])
|
|
|
|
}
|
2020-07-08 19:21:54 +00:00
|
|
|
if !showDepositData {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf(
|
2020-08-02 20:02:04 +00:00
|
|
|
"%s\n",
|
|
|
|
au.BrightRed("If you imported your account coming from the eth2 launchpad, you will find your "+
|
|
|
|
"deposit_data.json in the eth2.0-deposit-cli's validator_keys folder"),
|
2020-07-08 19:21:54 +00:00
|
|
|
)
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
fmt.Println("")
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-21 05:06:11 +00:00
|
|
|
|
|
|
|
func listDerivedKeymanagerAccounts(
|
2020-09-09 09:48:52 +00:00
|
|
|
ctx context.Context,
|
2020-10-15 22:31:52 +00:00
|
|
|
showDepositData,
|
2020-10-15 16:08:52 +00:00
|
|
|
showPrivateKeys bool,
|
2020-07-21 05:06:11 +00:00
|
|
|
keymanager *derived.Keymanager,
|
|
|
|
) error {
|
|
|
|
au := aurora.NewAurora(true)
|
|
|
|
fmt.Printf("(keymanager kind) %s\n", au.BrightGreen("derived, (HD) hierarchical-deterministic").Bold())
|
2020-08-31 19:46:45 +00:00
|
|
|
fmt.Printf("(derivation format) %s\n", au.BrightGreen(keymanager.KeymanagerOpts().DerivedPathStructure).Bold())
|
2020-07-21 05:06:11 +00:00
|
|
|
validatingPubKeys, err := keymanager.FetchValidatingPublicKeys(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch validating public keys")
|
|
|
|
}
|
2020-10-15 16:08:52 +00:00
|
|
|
var validatingPrivateKeys [][32]byte
|
|
|
|
if showPrivateKeys {
|
|
|
|
validatingPrivateKeys, err = keymanager.FetchValidatingPrivateKeys(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch validating private keys")
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 05:06:11 +00:00
|
|
|
withdrawalPublicKeys, err := keymanager.FetchWithdrawalPublicKeys(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch validating public keys")
|
|
|
|
}
|
2020-10-15 16:08:52 +00:00
|
|
|
var withdrawalPrivateKeys [][32]byte
|
|
|
|
if showPrivateKeys {
|
|
|
|
withdrawalPrivateKeys, err = keymanager.FetchWithdrawalPrivateKeys(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch withdrawal private keys")
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 08:11:05 +00:00
|
|
|
nextAccountNumber := keymanager.NextAccountNumber()
|
2020-07-21 05:06:11 +00:00
|
|
|
currentAccountNumber := nextAccountNumber
|
|
|
|
if nextAccountNumber > 0 {
|
|
|
|
currentAccountNumber--
|
|
|
|
}
|
2020-07-21 21:15:47 +00:00
|
|
|
accountNames, err := keymanager.ValidatingAccountNames(ctx)
|
2020-07-21 05:06:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-24 00:43:01 +00:00
|
|
|
if len(accountNames) == 1 {
|
|
|
|
fmt.Print("Showing 1 validator account\n")
|
|
|
|
} else if len(accountNames) == 0 {
|
|
|
|
fmt.Print("No accounts found\n")
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Showing %d validator accounts\n", len(accountNames))
|
|
|
|
}
|
2020-07-21 05:06:11 +00:00
|
|
|
for i := uint64(0); i <= currentAccountNumber; i++ {
|
|
|
|
fmt.Println("")
|
|
|
|
validatingKeyPath := fmt.Sprintf(derived.ValidatingKeyDerivationPathTemplate, i)
|
|
|
|
withdrawalKeyPath := fmt.Sprintf(derived.WithdrawalKeyDerivationPathTemplate, i)
|
|
|
|
|
|
|
|
// Retrieve the withdrawal key account metadata.
|
2020-07-30 23:23:18 +00:00
|
|
|
fmt.Printf("%s | %s\n", au.BrightBlue(fmt.Sprintf("Account %d", i)).Bold(), au.BrightGreen(accountNames[i]).Bold())
|
2020-07-21 05:06:11 +00:00
|
|
|
fmt.Printf("%s %#x\n", au.BrightMagenta("[withdrawal public key]").Bold(), withdrawalPublicKeys[i])
|
2020-10-15 16:08:52 +00:00
|
|
|
if showPrivateKeys {
|
|
|
|
fmt.Printf("%s %#x\n", au.BrightRed("[withdrawal private key]").Bold(), withdrawalPrivateKeys[i])
|
|
|
|
}
|
2020-07-21 05:06:11 +00:00
|
|
|
fmt.Printf("%s %s\n", au.BrightMagenta("[derivation path]").Bold(), withdrawalKeyPath)
|
|
|
|
|
|
|
|
// Retrieve the validating key account metadata.
|
|
|
|
fmt.Printf("%s %#x\n", au.BrightCyan("[validating public key]").Bold(), validatingPubKeys[i])
|
2020-10-15 16:08:52 +00:00
|
|
|
if showPrivateKeys {
|
|
|
|
fmt.Printf("%s %#x\n", au.BrightRed("[validating private key]").Bold(), validatingPrivateKeys[i])
|
|
|
|
}
|
2020-07-21 05:06:11 +00:00
|
|
|
fmt.Printf("%s %s\n", au.BrightCyan("[derivation path]").Bold(), validatingKeyPath)
|
|
|
|
|
|
|
|
if !showDepositData {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-30 23:23:18 +00:00
|
|
|
enc, err := keymanager.DepositDataForAccount(i)
|
2020-07-21 05:06:11 +00:00
|
|
|
if err != nil {
|
2020-08-02 20:02:04 +00:00
|
|
|
return errors.Wrapf(err, "could not deposit data for account: %s", accountNames[i])
|
2020-07-21 05:06:11 +00:00
|
|
|
}
|
|
|
|
fmt.Printf(`
|
2020-08-03 20:17:34 +00:00
|
|
|
======================Eth1 Deposit Transaction Data=====================
|
2020-07-21 05:06:11 +00:00
|
|
|
|
|
|
|
%#x
|
|
|
|
|
|
|
|
===================================================================`, enc)
|
|
|
|
fmt.Println(" ")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-24 00:43:01 +00:00
|
|
|
|
|
|
|
func listRemoteKeymanagerAccounts(
|
2020-09-09 09:48:52 +00:00
|
|
|
ctx context.Context,
|
2020-09-17 01:34:42 +00:00
|
|
|
w *wallet.Wallet,
|
2020-10-15 22:31:52 +00:00
|
|
|
keymanager keymanager.IKeymanager,
|
2020-08-31 19:46:45 +00:00
|
|
|
opts *remote.KeymanagerOpts,
|
2020-07-24 00:43:01 +00:00
|
|
|
) error {
|
|
|
|
au := aurora.NewAurora(true)
|
|
|
|
fmt.Printf("(keymanager kind) %s\n", au.BrightGreen("remote signer").Bold())
|
|
|
|
fmt.Printf(
|
|
|
|
"(configuration file path) %s\n",
|
2020-09-17 01:34:42 +00:00
|
|
|
au.BrightGreen(filepath.Join(w.AccountsDir(), wallet.KeymanagerConfigFileName)).Bold(),
|
2020-07-24 00:43:01 +00:00
|
|
|
)
|
|
|
|
fmt.Println(" ")
|
|
|
|
fmt.Printf("%s\n", au.BrightGreen("Configuration options").Bold())
|
2020-08-31 19:46:45 +00:00
|
|
|
fmt.Println(opts)
|
2020-07-24 00:43:01 +00:00
|
|
|
validatingPubKeys, err := keymanager.FetchValidatingPublicKeys(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch validating public keys")
|
|
|
|
}
|
|
|
|
if len(validatingPubKeys) == 1 {
|
|
|
|
fmt.Print("Showing 1 validator account\n")
|
|
|
|
} else if len(validatingPubKeys) == 0 {
|
|
|
|
fmt.Print("No accounts found\n")
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Showing %d validator accounts\n", len(validatingPubKeys))
|
|
|
|
}
|
|
|
|
for i := 0; i < len(validatingPubKeys); i++ {
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Printf(
|
|
|
|
"%s\n", au.BrightGreen(petnames.DeterministicName(validatingPubKeys[i][:], "-")).Bold(),
|
|
|
|
)
|
|
|
|
// Retrieve the validating key account metadata.
|
|
|
|
fmt.Printf("%s %#x\n", au.BrightCyan("[validating public key]").Bold(), validatingPubKeys[i])
|
|
|
|
fmt.Println(" ")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|