2020-07-08 19:21:54 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-07-23 03:10:23 +00:00
|
|
|
"path/filepath"
|
2020-07-08 19:21:54 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2020-07-21 05:06:11 +00:00
|
|
|
"github.com/dustin/go-humanize"
|
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-07-08 19:21:54 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
|
|
|
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
|
2020-07-21 05:06:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/derived"
|
2020-07-08 19:21:54 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct"
|
2020-07-24 00:43:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/remote"
|
2020-07-08 19:21:54 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ListAccounts displays all available validator accounts in a Prysm wallet.
|
|
|
|
func ListAccounts(cliCtx *cli.Context) error {
|
|
|
|
// Read the wallet from the specified path.
|
|
|
|
ctx := context.Background()
|
2020-07-22 04:49:04 +00:00
|
|
|
wallet, err := OpenWallet(cliCtx)
|
2020-07-08 19:21:54 +00:00
|
|
|
if err != nil {
|
2020-07-22 04:49:04 +00:00
|
|
|
return errors.Wrapf(err, "could not read wallet at specified path %s", wallet.AccountsDir())
|
2020-07-08 19:21:54 +00:00
|
|
|
}
|
2020-07-22 04:49:04 +00:00
|
|
|
keymanager, err := wallet.InitializeKeymanager(ctx, true /* skip mnemonic confirm */)
|
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)
|
|
|
|
switch wallet.KeymanagerKind() {
|
|
|
|
case v2keymanager.Direct:
|
2020-07-21 05:06:11 +00:00
|
|
|
km, ok := keymanager.(*direct.Keymanager)
|
|
|
|
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
|
|
|
}
|
|
|
|
if err := listDirectKeymanagerAccounts(showDepositData, wallet, 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-07-21 05:06:11 +00:00
|
|
|
case v2keymanager.Derived:
|
|
|
|
km, ok := keymanager.(*derived.Keymanager)
|
|
|
|
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
|
|
|
}
|
|
|
|
if err := listDerivedKeymanagerAccounts(showDepositData, wallet, 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-07-24 00:43:01 +00:00
|
|
|
case v2keymanager.Remote:
|
|
|
|
km, ok := keymanager.(*remote.Keymanager)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("could not assert keymanager interface to concrete type")
|
|
|
|
}
|
|
|
|
if err := listRemoteKeymanagerAccounts(wallet, km, km.Config()); err != nil {
|
|
|
|
return errors.Wrap(err, "could not list validator accounts with remote keymanager")
|
|
|
|
}
|
2020-07-08 19:21:54 +00:00
|
|
|
default:
|
2020-07-22 02:04:08 +00:00
|
|
|
return fmt.Errorf("keymanager kind %s not yet supported", wallet.KeymanagerKind().String())
|
2020-07-08 19:21:54 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listDirectKeymanagerAccounts(
|
|
|
|
showDepositData bool,
|
|
|
|
wallet *Wallet,
|
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")
|
|
|
|
}
|
|
|
|
au := aurora.NewAurora(true)
|
|
|
|
numAccounts := au.BrightYellow(len(accountNames))
|
|
|
|
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 " +
|
|
|
|
"by running `validator accounts-v2 list --show-deposit-data"),
|
|
|
|
)
|
|
|
|
|
2020-07-23 02:12:51 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
for i := 0; i < len(accountNames); i++ {
|
|
|
|
fmt.Println("")
|
|
|
|
|
|
|
|
// Retrieve the account creation timestamp.
|
2020-07-28 00:02:29 +00:00
|
|
|
keystoreFileName, err := wallet.FileNameAtPath(ctx, accountNames[i], direct.KeystoreFileName)
|
2020-07-08 19:21:54 +00:00
|
|
|
if err != nil {
|
2020-07-28 00:02:29 +00:00
|
|
|
return errors.Wrapf(err, "could not get keystore file name for account: %s", accountNames[i])
|
2020-07-08 19:21:54 +00:00
|
|
|
}
|
2020-07-28 00:02:29 +00:00
|
|
|
unixTimestamp, err := AccountTimestamp(keystoreFileName)
|
2020-07-08 19:21:54 +00:00
|
|
|
if err != nil {
|
2020-07-28 00:02:29 +00:00
|
|
|
return errors.Wrap(err, "could not get timestamp from keystore file name")
|
2020-07-08 19:21:54 +00:00
|
|
|
}
|
2020-07-28 22:58:11 +00:00
|
|
|
fmt.Printf("%s | Created %s\n", au.BrightGreen(accountNames[i]).Bold(), humanize.Time(unixTimestamp))
|
2020-07-28 19:23:53 +00:00
|
|
|
fmt.Printf("%s %#x\n", au.BrightMagenta("[validating public key]").Bold(), pubKeys[i])
|
2020-07-08 19:21:54 +00:00
|
|
|
if !showDepositData {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-23 02:12:51 +00:00
|
|
|
enc, err := wallet.ReadFileAtPath(ctx, accountNames[i], direct.DepositTransactionFileName)
|
2020-07-08 19:21:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not read file for account: %s", direct.DepositTransactionFileName)
|
|
|
|
}
|
|
|
|
fmt.Printf(
|
|
|
|
"%s %s\n",
|
|
|
|
"(deposit tx file)",
|
2020-07-23 03:10:23 +00:00
|
|
|
filepath.Join(wallet.AccountsDir(), accountNames[i], direct.DepositTransactionFileName),
|
2020-07-08 19:21:54 +00:00
|
|
|
)
|
|
|
|
fmt.Printf(`
|
|
|
|
======================Deposit Transaction Data=====================
|
|
|
|
|
|
|
|
%#x
|
|
|
|
|
|
|
|
===================================================================`, enc)
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
fmt.Println("")
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-21 05:06:11 +00:00
|
|
|
|
|
|
|
func listDerivedKeymanagerAccounts(
|
|
|
|
showDepositData bool,
|
|
|
|
wallet *Wallet,
|
|
|
|
keymanager *derived.Keymanager,
|
|
|
|
) error {
|
|
|
|
au := aurora.NewAurora(true)
|
|
|
|
fmt.Println(
|
|
|
|
au.BrightRed("View the eth1 deposit transaction data for your accounts " +
|
|
|
|
"by running `validator accounts-v2 list --show-deposit-data"),
|
|
|
|
)
|
|
|
|
fmt.Printf("(keymanager kind) %s\n", au.BrightGreen("derived, (HD) hierarchical-deterministic").Bold())
|
|
|
|
fmt.Printf("(derivation format) %s\n", au.BrightGreen(keymanager.Config().DerivedPathStructure).Bold())
|
|
|
|
ctx := context.Background()
|
|
|
|
validatingPubKeys, err := keymanager.FetchValidatingPublicKeys(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch validating public keys")
|
|
|
|
}
|
|
|
|
withdrawalPublicKeys, err := keymanager.FetchWithdrawalPublicKeys(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch validating public keys")
|
|
|
|
}
|
|
|
|
nextAccountNumber := keymanager.NextAccountNumber(ctx)
|
|
|
|
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.
|
|
|
|
createdAtBytes, err := wallet.ReadFileAtPath(ctx, validatingKeyPath, derived.TimestampFileName)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not read file for account: %s", derived.TimestampFileName)
|
|
|
|
}
|
|
|
|
unixTimestampInt, err := strconv.ParseInt(string(createdAtBytes), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not parse account created at timestamp: %s", createdAtBytes)
|
|
|
|
}
|
|
|
|
unixTimestamp := time.Unix(unixTimestampInt, 0)
|
2020-07-27 14:03:30 +00:00
|
|
|
fmt.Printf("%s | %s | Created %s\n", au.BrightBlue(fmt.Sprintf("Account %d", i)).Bold(), au.BrightGreen(accountNames[i]).Bold(), humanize.Time(unixTimestamp))
|
2020-07-21 05:06:11 +00:00
|
|
|
fmt.Printf("%s %#x\n", au.BrightMagenta("[withdrawal public key]").Bold(), withdrawalPublicKeys[i])
|
|
|
|
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])
|
|
|
|
fmt.Printf("%s %s\n", au.BrightCyan("[derivation path]").Bold(), validatingKeyPath)
|
|
|
|
|
|
|
|
if !showDepositData {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
enc, err := wallet.ReadFileAtPath(ctx, withdrawalKeyPath, derived.DepositTransactionFileName)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not read file for account: %s", direct.DepositTransactionFileName)
|
|
|
|
}
|
|
|
|
fmt.Printf(
|
|
|
|
"%s %s\n",
|
|
|
|
"(deposit tx file)",
|
2020-07-23 03:10:23 +00:00
|
|
|
filepath.Join(wallet.AccountsDir(), withdrawalKeyPath, derived.DepositTransactionFileName),
|
2020-07-21 05:06:11 +00:00
|
|
|
)
|
|
|
|
fmt.Printf(`
|
|
|
|
======================Deposit Transaction Data=====================
|
|
|
|
|
|
|
|
%#x
|
|
|
|
|
|
|
|
===================================================================`, enc)
|
|
|
|
fmt.Println(" ")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-24 00:43:01 +00:00
|
|
|
|
|
|
|
func listRemoteKeymanagerAccounts(
|
|
|
|
wallet *Wallet,
|
|
|
|
keymanager v2keymanager.IKeymanager,
|
|
|
|
cfg *remote.Config,
|
|
|
|
) error {
|
|
|
|
au := aurora.NewAurora(true)
|
|
|
|
fmt.Printf("(keymanager kind) %s\n", au.BrightGreen("remote signer").Bold())
|
|
|
|
fmt.Printf(
|
|
|
|
"(configuration file path) %s\n",
|
|
|
|
au.BrightGreen(filepath.Join(wallet.AccountsDir(), KeymanagerConfigFileName)).Bold(),
|
|
|
|
)
|
|
|
|
ctx := context.Background()
|
|
|
|
fmt.Println(" ")
|
|
|
|
fmt.Printf("%s\n", au.BrightGreen("Configuration options").Bold())
|
|
|
|
fmt.Println(cfg)
|
|
|
|
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
|
|
|
|
}
|