mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
Remove Accounts Enable/Disable Code (#8576)
* rem * remove all enable disable code * fix broken build * fix more tests * fix broken tests Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
@@ -7,7 +7,6 @@ go_library(
|
||||
"accounts.go",
|
||||
"accounts_backup.go",
|
||||
"accounts_delete.go",
|
||||
"accounts_enable_disable.go",
|
||||
"accounts_exit.go",
|
||||
"accounts_helper.go",
|
||||
"accounts_import.go",
|
||||
@@ -63,7 +62,6 @@ go_test(
|
||||
srcs = [
|
||||
"accounts_backup_test.go",
|
||||
"accounts_delete_test.go",
|
||||
"accounts_enable_disable_test.go",
|
||||
"accounts_exit_test.go",
|
||||
"accounts_import_test.go",
|
||||
"accounts_list_test.go",
|
||||
|
||||
@@ -11,11 +11,9 @@ var (
|
||||
ErrCouldNotInitializeKeymanager = "could not initialize keymanager"
|
||||
)
|
||||
|
||||
// Config specifies parameters to run to delete, enable, disable accounts.
|
||||
// Config specifies parameters for accounts commands.
|
||||
type Config struct {
|
||||
Wallet *wallet.Wallet
|
||||
Keymanager keymanager.IKeymanager
|
||||
DisablePublicKeys [][]byte
|
||||
EnablePublicKeys [][]byte
|
||||
DeletePublicKeys [][]byte
|
||||
Wallet *wallet.Wallet
|
||||
Keymanager keymanager.IKeymanager
|
||||
DeletePublicKeys [][]byte
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func BackupAccountsCli(cliCtx *cli.Context) error {
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrCouldNotInitializeKeymanager)
|
||||
}
|
||||
pubKeys, err := km.FetchAllValidatingPublicKeys(cliCtx.Context)
|
||||
pubKeys, err := km.FetchValidatingPublicKeys(cliCtx.Context)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not fetch validating public keys")
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func DeleteAccountCli(cliCtx *cli.Context) error {
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrCouldNotInitializeKeymanager)
|
||||
}
|
||||
validatingPublicKeys, err := kManager.FetchAllValidatingPublicKeys(cliCtx.Context)
|
||||
validatingPublicKeys, err := kManager.FetchValidatingPublicKeys(cliCtx.Context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,173 +0,0 @@
|
||||
package accounts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prysmaticlabs/prysm/cmd/validator/flags"
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/promptutil"
|
||||
"github.com/prysmaticlabs/prysm/validator/accounts/iface"
|
||||
"github.com/prysmaticlabs/prysm/validator/accounts/prompt"
|
||||
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
||||
"github.com/prysmaticlabs/prysm/validator/keymanager/imported"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// DisableAccountsCli disables via CLI the accounts that the user requests to be disabled from the wallet
|
||||
func DisableAccountsCli(cliCtx *cli.Context) error {
|
||||
w, err := wallet.OpenWalletOrElseCli(cliCtx, func(cliCtx *cli.Context) (*wallet.Wallet, error) {
|
||||
return nil, wallet.ErrNoWalletFound
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not open wallet")
|
||||
}
|
||||
km, err := w.InitializeKeymanager(cliCtx.Context, iface.InitKeymanagerConfig{ListenForChanges: false})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrCouldNotInitializeKeymanager)
|
||||
}
|
||||
validatingPublicKeys, err := km.FetchValidatingPublicKeys(cliCtx.Context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(validatingPublicKeys) == 0 {
|
||||
return errors.New("wallet is empty, no accounts to disable")
|
||||
}
|
||||
// Allow the user to interactively select the accounts to disable or optionally
|
||||
// provide them via cli flags as a string of comma-separated, hex strings.
|
||||
filteredPubKeys, err := filterPublicKeysFromUserInput(
|
||||
cliCtx,
|
||||
flags.DisablePublicKeysFlag,
|
||||
validatingPublicKeys,
|
||||
prompt.SelectAccountsDisablePromptText,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not filter public keys for deactivation")
|
||||
}
|
||||
rawPublicKeys := make([][]byte, len(filteredPubKeys))
|
||||
formattedPubKeys := make([]string, len(filteredPubKeys))
|
||||
for i, pk := range filteredPubKeys {
|
||||
pubKeyBytes := pk.Marshal()
|
||||
rawPublicKeys[i] = pubKeyBytes
|
||||
formattedPubKeys[i] = fmt.Sprintf("%#x", bytesutil.Trunc(pubKeyBytes))
|
||||
}
|
||||
allAccountStr := strings.Join(formattedPubKeys, ", ")
|
||||
if !cliCtx.IsSet(flags.DisablePublicKeysFlag.Name) {
|
||||
if len(filteredPubKeys) == 1 {
|
||||
promptText := "Are you sure you want to disable 1 account? (%s) Y/N"
|
||||
resp, err := promptutil.ValidatePrompt(
|
||||
os.Stdin, fmt.Sprintf(promptText, au.BrightGreen(formattedPubKeys[0])), promptutil.ValidateYesOrNo,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.EqualFold(resp, "n") {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
promptText := "Are you sure you want to disable %d accounts? (%s) Y/N"
|
||||
if len(filteredPubKeys) == len(validatingPublicKeys) {
|
||||
promptText = fmt.Sprintf("Are you sure you want to disable all accounts? Y/N (%s)", au.BrightGreen(allAccountStr))
|
||||
} else {
|
||||
promptText = fmt.Sprintf(promptText, len(filteredPubKeys), au.BrightGreen(allAccountStr))
|
||||
}
|
||||
resp, err := promptutil.ValidatePrompt(os.Stdin, promptText, promptutil.ValidateYesOrNo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.EqualFold(resp, "n") {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
importedKM, ok := km.(*imported.Keymanager)
|
||||
if !ok {
|
||||
return errors.New("can only disable accounts for imported wallets")
|
||||
}
|
||||
if err := importedKM.DisableAccounts(cliCtx.Context, rawPublicKeys); err != nil {
|
||||
return err
|
||||
}
|
||||
log.WithField("publicKeys", allAccountStr).Info("Accounts disabled")
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnableAccountsCli enables via CLI the accounts that the user requests to be enabled from the wallet
|
||||
func EnableAccountsCli(cliCtx *cli.Context) error {
|
||||
w, err := wallet.OpenWalletOrElseCli(cliCtx, func(cliCtx *cli.Context) (*wallet.Wallet, error) {
|
||||
return nil, wallet.ErrNoWalletFound
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not open wallet")
|
||||
}
|
||||
km, err := w.InitializeKeymanager(cliCtx.Context, iface.InitKeymanagerConfig{ListenForChanges: false})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrCouldNotInitializeKeymanager)
|
||||
}
|
||||
importedKM, ok := km.(*imported.Keymanager)
|
||||
if !ok {
|
||||
return errors.New("can only enable/disable accounts for imported wallets")
|
||||
}
|
||||
disabledPublicKeys := importedKM.DisabledPublicKeys()
|
||||
if len(disabledPublicKeys) == 0 {
|
||||
return errors.New("no accounts are disabled")
|
||||
}
|
||||
disabledPublicKeys48 := make([][48]byte, len(disabledPublicKeys))
|
||||
for i := range disabledPublicKeys {
|
||||
disabledPublicKeys48[i] = bytesutil.ToBytes48(disabledPublicKeys[i])
|
||||
}
|
||||
|
||||
// Allow the user to interactively select the accounts to enable or optionally
|
||||
// provide them via cli flags as a string of comma-separated, hex strings.
|
||||
filteredPubKeys, err := filterPublicKeysFromUserInput(
|
||||
cliCtx,
|
||||
flags.EnablePublicKeysFlag,
|
||||
disabledPublicKeys48,
|
||||
prompt.SelectAccountsEnablePromptText,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not filter public keys for activation")
|
||||
}
|
||||
rawPublicKeys := make([][]byte, len(filteredPubKeys))
|
||||
formattedPubKeys := make([]string, len(filteredPubKeys))
|
||||
for i, pk := range filteredPubKeys {
|
||||
pubKeyBytes := pk.Marshal()
|
||||
rawPublicKeys[i] = pubKeyBytes
|
||||
formattedPubKeys[i] = fmt.Sprintf("%#x", bytesutil.Trunc(pubKeyBytes))
|
||||
}
|
||||
allAccountStr := strings.Join(formattedPubKeys, ", ")
|
||||
if !cliCtx.IsSet(flags.EnablePublicKeysFlag.Name) {
|
||||
if len(filteredPubKeys) == 1 {
|
||||
promptText := "Are you sure you want to enable 1 account? (%s) Y/N"
|
||||
resp, err := promptutil.ValidatePrompt(
|
||||
os.Stdin, fmt.Sprintf(promptText, au.BrightGreen(formattedPubKeys[0])), promptutil.ValidateYesOrNo,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.EqualFold(resp, "n") {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
promptText := "Are you sure you want to enable %d accounts? (%s) Y/N"
|
||||
if len(filteredPubKeys) == len(disabledPublicKeys48) {
|
||||
promptText = fmt.Sprintf("Are you sure you want to enable all accounts? Y/N (%s)", au.BrightGreen(allAccountStr))
|
||||
} else {
|
||||
promptText = fmt.Sprintf(promptText, len(filteredPubKeys), au.BrightGreen(allAccountStr))
|
||||
}
|
||||
resp, err := promptutil.ValidatePrompt(os.Stdin, promptText, promptutil.ValidateYesOrNo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.EqualFold(resp, "n") {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := importedKM.EnableAccounts(cliCtx.Context, rawPublicKeys); err != nil {
|
||||
return err
|
||||
}
|
||||
log.WithField("publicKeys", allAccountStr).Info("Accounts enabled")
|
||||
return nil
|
||||
}
|
||||
@@ -1,143 +0,0 @@
|
||||
package accounts
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
||||
"github.com/prysmaticlabs/prysm/validator/accounts/iface"
|
||||
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
||||
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
||||
)
|
||||
|
||||
func TestDisableAccounts_Noninteractive(t *testing.T) {
|
||||
walletDir, _, passwordFilePath := setupWalletAndPasswordsDir(t)
|
||||
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
||||
require.NoError(t, err, "Could not generate random file path")
|
||||
// Write a directory where we will import keys from.
|
||||
keysDir := filepath.Join(t.TempDir(), fmt.Sprintf("/%d", randPath), "keysDir")
|
||||
require.NoError(t, os.MkdirAll(keysDir, os.ModePerm))
|
||||
|
||||
// Create 3 keystore files in the keys directory we can then
|
||||
// import from in our wallet.
|
||||
k1, _ := createKeystore(t, keysDir)
|
||||
time.Sleep(time.Second)
|
||||
k2, _ := createKeystore(t, keysDir)
|
||||
time.Sleep(time.Second)
|
||||
k3, _ := createKeystore(t, keysDir)
|
||||
generatedPubKeys := []string{k1.Pubkey, k2.Pubkey, k3.Pubkey}
|
||||
// Only disable keys 0 and 1.
|
||||
disablePublicKeys := strings.Join(generatedPubKeys[0:2], ",")
|
||||
// We initialize a wallet with a imported keymanager.
|
||||
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
||||
// Wallet configuration flags.
|
||||
walletDir: walletDir,
|
||||
keymanagerKind: keymanager.Imported,
|
||||
walletPasswordFile: passwordFilePath,
|
||||
accountPasswordFile: passwordFilePath,
|
||||
// Flags required for ImportAccounts to work.
|
||||
keysDir: keysDir,
|
||||
// Flags required for DisableAccounts to work.
|
||||
disablePublicKeys: disablePublicKeys,
|
||||
})
|
||||
w, err := CreateWalletWithKeymanager(cliCtx.Context, &CreateWalletConfig{
|
||||
WalletCfg: &wallet.Config{
|
||||
WalletDir: walletDir,
|
||||
KeymanagerKind: keymanager.Imported,
|
||||
WalletPassword: password,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// We attempt to import accounts.
|
||||
require.NoError(t, ImportAccountsCli(cliCtx))
|
||||
|
||||
// We attempt to disable the accounts specified.
|
||||
require.NoError(t, DisableAccountsCli(cliCtx))
|
||||
|
||||
keymanager, err := w.InitializeKeymanager(cliCtx.Context, iface.InitKeymanagerConfig{ListenForChanges: false})
|
||||
require.NoError(t, err)
|
||||
remainingAccounts, err := keymanager.FetchValidatingPublicKeys(cliCtx.Context)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(remainingAccounts), 1)
|
||||
remainingPublicKey, err := hex.DecodeString(k3.Pubkey)
|
||||
require.NoError(t, err)
|
||||
assert.DeepEqual(t, remainingAccounts[0], bytesutil.ToBytes48(remainingPublicKey))
|
||||
}
|
||||
|
||||
func TestEnableAccounts_Noninteractive(t *testing.T) {
|
||||
walletDir, _, passwordFilePath := setupWalletAndPasswordsDir(t)
|
||||
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
||||
require.NoError(t, err, "Could not generate random file path")
|
||||
// Write a directory where we will import keys from.
|
||||
keysDir := filepath.Join(t.TempDir(), fmt.Sprintf("/%d", randPath), "keysDir")
|
||||
require.NoError(t, os.MkdirAll(keysDir, os.ModePerm))
|
||||
|
||||
// Create 3 keystore files in the keys directory we can then
|
||||
// import from in our wallet.
|
||||
k1, _ := createKeystore(t, keysDir)
|
||||
time.Sleep(time.Second)
|
||||
k2, _ := createKeystore(t, keysDir)
|
||||
time.Sleep(time.Second)
|
||||
k3, _ := createKeystore(t, keysDir)
|
||||
generatedPubKeys := []string{k1.Pubkey, k2.Pubkey, k3.Pubkey}
|
||||
// Disable all keys.
|
||||
disablePublicKeys := strings.Join(generatedPubKeys, ",")
|
||||
// Only enable keys 0 and 1.
|
||||
enablePublicKeys := strings.Join(generatedPubKeys[0:2], ",")
|
||||
// We initialize a wallet with a imported keymanager.
|
||||
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
||||
walletDir: walletDir,
|
||||
keymanagerKind: keymanager.Imported,
|
||||
walletPasswordFile: passwordFilePath,
|
||||
accountPasswordFile: passwordFilePath,
|
||||
keysDir: keysDir,
|
||||
disablePublicKeys: disablePublicKeys,
|
||||
// Flags required for EnableAccounts to work.
|
||||
enablePublicKeys: enablePublicKeys,
|
||||
})
|
||||
w, err := CreateWalletWithKeymanager(cliCtx.Context, &CreateWalletConfig{
|
||||
WalletCfg: &wallet.Config{
|
||||
WalletDir: walletDir,
|
||||
KeymanagerKind: keymanager.Imported,
|
||||
WalletPassword: password,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// We attempt to import accounts.
|
||||
require.NoError(t, ImportAccountsCli(cliCtx))
|
||||
|
||||
// We attempt to disable the accounts specified.
|
||||
require.NoError(t, DisableAccountsCli(cliCtx))
|
||||
|
||||
km, err := w.InitializeKeymanager(cliCtx.Context, iface.InitKeymanagerConfig{ListenForChanges: false})
|
||||
require.NoError(t, err)
|
||||
remainingAccounts, err := km.FetchValidatingPublicKeys(cliCtx.Context)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(remainingAccounts), 0)
|
||||
|
||||
// We attempt to enable the accounts specified.
|
||||
require.NoError(t, EnableAccountsCli(cliCtx))
|
||||
|
||||
km, err = w.InitializeKeymanager(cliCtx.Context, iface.InitKeymanagerConfig{ListenForChanges: false})
|
||||
require.NoError(t, err)
|
||||
remainingAccounts, err = km.FetchValidatingPublicKeys(cliCtx.Context)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(remainingAccounts), 2)
|
||||
remainingPublicKey1, err := hex.DecodeString(k1.Pubkey)
|
||||
require.NoError(t, err)
|
||||
remainingPublicKey2, err := hex.DecodeString(k2.Pubkey)
|
||||
require.NoError(t, err)
|
||||
assert.DeepEqual(t, remainingAccounts[0], bytesutil.ToBytes48(remainingPublicKey1))
|
||||
assert.DeepEqual(t, remainingAccounts[1], bytesutil.ToBytes48(remainingPublicKey2))
|
||||
}
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
||||
"github.com/prysmaticlabs/prysm/cmd/validator/flags"
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/petnames"
|
||||
"github.com/prysmaticlabs/prysm/validator/accounts/iface"
|
||||
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
||||
@@ -104,12 +103,7 @@ func listImportedKeymanagerAccounts(
|
||||
"by running `validator accounts list --show-deposit-data`"),
|
||||
)
|
||||
|
||||
pubKeys, err := keymanager.FetchAllValidatingPublicKeys(ctx)
|
||||
disabledPublicKeys := keymanager.DisabledPublicKeys()
|
||||
existingDisabledPk := make(map[[48]byte]bool, len(disabledPublicKeys))
|
||||
for _, dpk := range disabledPublicKeys {
|
||||
existingDisabledPk[bytesutil.ToBytes48(dpk)] = true
|
||||
}
|
||||
pubKeys, err := keymanager.FetchValidatingPublicKeys(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not fetch validating public keys")
|
||||
}
|
||||
@@ -122,11 +116,7 @@ func listImportedKeymanagerAccounts(
|
||||
}
|
||||
for i := 0; i < len(accountNames); i++ {
|
||||
fmt.Println("")
|
||||
if existingDisabledPk[pubKeys[i]] {
|
||||
fmt.Printf("%s | %s (%s)\n", au.BrightBlue(fmt.Sprintf("Account %d", i)).Bold(), au.BrightRed(accountNames[i]).Bold(), au.BrightRed("disabled").Bold())
|
||||
} else {
|
||||
fmt.Printf("%s | %s\n", au.BrightBlue(fmt.Sprintf("Account %d", i)).Bold(), au.BrightGreen(accountNames[i]).Bold())
|
||||
}
|
||||
fmt.Printf("%s | %s\n", au.BrightBlue(fmt.Sprintf("Account %d", i)).Bold(), au.BrightGreen(accountNames[i]).Bold())
|
||||
fmt.Printf("%s %#x\n", au.BrightMagenta("[validating public key]").Bold(), pubKeys[i])
|
||||
if showPrivateKeys {
|
||||
if len(privateKeys) > i {
|
||||
@@ -155,7 +145,7 @@ func listDerivedKeymanagerAccounts(
|
||||
au := aurora.NewAurora(true)
|
||||
fmt.Printf("(keymanager kind) %s\n", au.BrightGreen("derived, (HD) hierarchical-deterministic").Bold())
|
||||
fmt.Printf("(derivation format) %s\n", au.BrightGreen(derived.DerivationPathFormat).Bold())
|
||||
validatingPubKeys, err := keymanager.FetchAllValidatingPublicKeys(ctx)
|
||||
validatingPubKeys, err := keymanager.FetchValidatingPublicKeys(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not fetch validating public keys")
|
||||
}
|
||||
@@ -210,7 +200,7 @@ func listRemoteKeymanagerAccounts(
|
||||
fmt.Println(" ")
|
||||
fmt.Printf("%s\n", au.BrightGreen("Configuration options").Bold())
|
||||
fmt.Println(opts)
|
||||
validatingPubKeys, err := keymanager.FetchAllValidatingPublicKeys(ctx)
|
||||
validatingPubKeys, err := keymanager.FetchValidatingPublicKeys(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not fetch validating public keys")
|
||||
}
|
||||
|
||||
@@ -40,10 +40,6 @@ func (m *mockRemoteKeymanager) FetchValidatingPublicKeys(_ context.Context) ([][
|
||||
return m.publicKeys, nil
|
||||
}
|
||||
|
||||
func (m *mockRemoteKeymanager) FetchAllValidatingPublicKeys(_ context.Context) ([][48]byte, error) {
|
||||
return m.publicKeys, nil
|
||||
}
|
||||
|
||||
func (m *mockRemoteKeymanager) Sign(context.Context, *validatorpb.SignRequest) (bls.Signature, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -30,10 +30,6 @@ const (
|
||||
SelectAccountsBackupPromptText = "Select the account(s) you wish to backup"
|
||||
// SelectAccountsVoluntaryExitPromptText --
|
||||
SelectAccountsVoluntaryExitPromptText = "Select the account(s) on which you wish to perform a voluntary exit"
|
||||
// SelectAccountsDisablePromptText --
|
||||
SelectAccountsDisablePromptText = "Select the account(s) you would like to disable"
|
||||
// SelectAccountsEnablePromptText --
|
||||
SelectAccountsEnablePromptText = "Select the account(s) you would like to enable"
|
||||
)
|
||||
|
||||
var au = aurora.NewAurora(true)
|
||||
|
||||
@@ -48,8 +48,6 @@ type testWalletConfig struct {
|
||||
backupPasswordFile string
|
||||
backupPublicKeys string
|
||||
voluntaryExitPublicKeys string
|
||||
disablePublicKeys string
|
||||
enablePublicKeys string
|
||||
deletePublicKeys string
|
||||
keysDir string
|
||||
backupDir string
|
||||
@@ -67,8 +65,6 @@ func setupWalletCtx(
|
||||
set.String(flags.KeysDirFlag.Name, cfg.keysDir, "")
|
||||
set.String(flags.KeymanagerKindFlag.Name, cfg.keymanagerKind.String(), "")
|
||||
set.String(flags.DeletePublicKeysFlag.Name, cfg.deletePublicKeys, "")
|
||||
set.String(flags.DisablePublicKeysFlag.Name, cfg.disablePublicKeys, "")
|
||||
set.String(flags.EnablePublicKeysFlag.Name, cfg.enablePublicKeys, "")
|
||||
set.String(flags.VoluntaryExitPublicKeysFlag.Name, cfg.voluntaryExitPublicKeys, "")
|
||||
set.String(flags.BackupDirFlag.Name, cfg.backupDir, "")
|
||||
set.String(flags.BackupPasswordFile.Name, cfg.backupPasswordFile, "")
|
||||
@@ -90,8 +86,6 @@ func setupWalletCtx(
|
||||
assert.NoError(tb, set.Set(flags.KeysDirFlag.Name, cfg.keysDir))
|
||||
assert.NoError(tb, set.Set(flags.KeymanagerKindFlag.Name, cfg.keymanagerKind.String()))
|
||||
assert.NoError(tb, set.Set(flags.DeletePublicKeysFlag.Name, cfg.deletePublicKeys))
|
||||
assert.NoError(tb, set.Set(flags.DisablePublicKeysFlag.Name, cfg.disablePublicKeys))
|
||||
assert.NoError(tb, set.Set(flags.EnablePublicKeysFlag.Name, cfg.enablePublicKeys))
|
||||
assert.NoError(tb, set.Set(flags.VoluntaryExitPublicKeysFlag.Name, cfg.voluntaryExitPublicKeys))
|
||||
assert.NoError(tb, set.Set(flags.BackupDirFlag.Name, cfg.backupDir))
|
||||
assert.NoError(tb, set.Set(flags.BackupPublicKeysFlag.Name, cfg.backupPublicKeys))
|
||||
|
||||
@@ -56,25 +56,13 @@ type mockKeymanager struct {
|
||||
}
|
||||
|
||||
func (m *mockKeymanager) FetchValidatingPublicKeys(ctx context.Context) ([][48]byte, error) {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
keys := make([][48]byte, 0)
|
||||
if m.fetchNoKeys {
|
||||
// We set the value to `false` to fetch keys the next time.
|
||||
m.fetchNoKeys = false
|
||||
return make([][48]byte, 0), nil
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
keys := make([][48]byte, 0)
|
||||
for pubKey := range m.keysMap {
|
||||
keys = append(keys, pubKey)
|
||||
}
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
func (m *mockKeymanager) FetchAllValidatingPublicKeys(ctx context.Context) ([][48]byte, error) {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
keys := make([][48]byte, 0)
|
||||
for pubKey := range m.keysMap {
|
||||
keys = append(keys, pubKey)
|
||||
}
|
||||
|
||||
@@ -101,11 +101,6 @@ func (km *Keymanager) FetchValidatingPublicKeys(ctx context.Context) ([][48]byte
|
||||
return km.importedKM.FetchValidatingPublicKeys(ctx)
|
||||
}
|
||||
|
||||
// FetchAllValidatingPublicKeys fetches the list of all public keys (including disabled ones) from the keymanager.
|
||||
func (km *Keymanager) FetchAllValidatingPublicKeys(ctx context.Context) ([][48]byte, error) {
|
||||
return km.importedKM.FetchAllValidatingPublicKeys(ctx)
|
||||
}
|
||||
|
||||
// FetchValidatingPrivateKeys fetches the list of validating private keys from the keymanager.
|
||||
func (km *Keymanager) FetchValidatingPrivateKeys(ctx context.Context) ([][32]byte, error) {
|
||||
return km.importedKM.FetchValidatingPrivateKeys(ctx)
|
||||
|
||||
@@ -169,7 +169,7 @@ func TestDerivedKeymanager_Sign(t *testing.T) {
|
||||
err = dr.RecoverAccountsFromMnemonic(ctx, constant.TestMnemonic, "", numAccounts)
|
||||
require.NoError(t, err)
|
||||
|
||||
pubKeys, err := dr.FetchAllValidatingPublicKeys(ctx)
|
||||
pubKeys, err := dr.FetchValidatingPublicKeys(ctx)
|
||||
require.NoError(t, err)
|
||||
// We prepare naive data to sign.
|
||||
data := []byte("eth2data")
|
||||
|
||||
@@ -6,7 +6,6 @@ go_library(
|
||||
srcs = [
|
||||
"backup.go",
|
||||
"doc.go",
|
||||
"enable_disable.go",
|
||||
"import.go",
|
||||
"keymanager.go",
|
||||
"log.go",
|
||||
@@ -45,7 +44,6 @@ go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"backup_test.go",
|
||||
"enable_disable_test.go",
|
||||
"import_test.go",
|
||||
"keymanager_test.go",
|
||||
"refresh_test.go",
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
package imported
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
)
|
||||
|
||||
// DisableAccounts disables public keys from the user's wallet.
|
||||
func (km *Keymanager) DisableAccounts(ctx context.Context, pubKeys [][]byte) error {
|
||||
if pubKeys == nil || len(pubKeys) < 1 {
|
||||
return errors.New("no public keys specified to disable")
|
||||
}
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
for _, pk := range pubKeys {
|
||||
if _, ok := km.disabledPublicKeys[bytesutil.ToBytes48(pk)]; !ok {
|
||||
km.disabledPublicKeys[bytesutil.ToBytes48(pk)] = true
|
||||
}
|
||||
}
|
||||
return km.rewriteDisabledKeysToDisk(ctx)
|
||||
}
|
||||
|
||||
// EnableAccounts enables public keys from a user's wallet if they are disabled.
|
||||
func (km *Keymanager) EnableAccounts(ctx context.Context, pubKeys [][]byte) error {
|
||||
if pubKeys == nil || len(pubKeys) < 1 {
|
||||
return errors.New("no public keys specified to enable")
|
||||
}
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
for _, pk := range pubKeys {
|
||||
delete(km.disabledPublicKeys, bytesutil.ToBytes48(pk))
|
||||
}
|
||||
return km.rewriteDisabledKeysToDisk(ctx)
|
||||
}
|
||||
|
||||
func (km *Keymanager) rewriteDisabledKeysToDisk(ctx context.Context) error {
|
||||
encoded, err := km.wallet.ReadFileAtPath(ctx, AccountsPath, AccountsKeystoreFileName)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not read keystore file for accounts")
|
||||
}
|
||||
keystore := &AccountsKeystoreRepresentation{}
|
||||
if err := json.Unmarshal(encoded, keystore); err != nil {
|
||||
return err
|
||||
}
|
||||
disabledKeysStrings := make([]string, 0)
|
||||
for pk := range km.disabledPublicKeys {
|
||||
disabledKeysStrings = append(disabledKeysStrings, fmt.Sprintf("%x", pk))
|
||||
}
|
||||
keystore.DisabledPublicKeys = disabledKeysStrings
|
||||
encoded, err = json.MarshalIndent(keystore, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := km.wallet.WriteFileAtPath(ctx, AccountsPath, AccountsKeystoreFileName, encoded); err != nil {
|
||||
return errors.Wrap(err, "could not write keystore file for accounts")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,242 +0,0 @@
|
||||
package imported
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/shared/bls"
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
||||
mock "github.com/prysmaticlabs/prysm/validator/accounts/testing"
|
||||
)
|
||||
|
||||
func TestKeymanager_DisableAccounts(t *testing.T) {
|
||||
numKeys := 5
|
||||
randomPrivateKeys := make([][]byte, numKeys)
|
||||
randomPublicKeys := make([][]byte, numKeys)
|
||||
for i := 0; i < numKeys; i++ {
|
||||
key, err := bls.RandKey()
|
||||
require.NoError(t, err)
|
||||
randomPrivateKeys[i] = key.Marshal()
|
||||
randomPublicKeys[i] = key.PublicKey().Marshal()
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
existingDisabledKeys [][]byte
|
||||
keysToDisable [][]byte
|
||||
expectedDisabledKeys [][]byte
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Trying to disable already disabled keys fails silently",
|
||||
existingDisabledKeys: randomPublicKeys,
|
||||
keysToDisable: randomPublicKeys,
|
||||
wantErr: false,
|
||||
expectedDisabledKeys: randomPublicKeys,
|
||||
},
|
||||
{
|
||||
name: "Trying to disable a subset of keys works",
|
||||
existingDisabledKeys: randomPublicKeys[0:2],
|
||||
keysToDisable: randomPublicKeys[2:],
|
||||
wantErr: false,
|
||||
expectedDisabledKeys: randomPublicKeys,
|
||||
},
|
||||
{
|
||||
name: "Nil input keys to disable returns error",
|
||||
existingDisabledKeys: randomPublicKeys,
|
||||
keysToDisable: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "No input keys to disable returns error",
|
||||
existingDisabledKeys: randomPublicKeys,
|
||||
keysToDisable: make([][]byte, 0),
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "No existing disabled keys updates after disabling",
|
||||
existingDisabledKeys: make([][]byte, 0),
|
||||
keysToDisable: randomPublicKeys,
|
||||
expectedDisabledKeys: randomPublicKeys,
|
||||
},
|
||||
{
|
||||
name: "Disjoint sets of already disabled + newly disabled leads to whole set",
|
||||
existingDisabledKeys: randomPublicKeys[0:2],
|
||||
keysToDisable: randomPublicKeys[2:],
|
||||
wantErr: false,
|
||||
expectedDisabledKeys: randomPublicKeys,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
wallet := &mock.Wallet{
|
||||
Files: make(map[string]map[string][]byte),
|
||||
}
|
||||
disabledPubKeys := make(map[[48]byte]bool)
|
||||
for _, pubKey := range tt.existingDisabledKeys {
|
||||
disabledPubKeys[bytesutil.ToBytes48(pubKey)] = true
|
||||
}
|
||||
dr := &Keymanager{
|
||||
disabledPublicKeys: disabledPubKeys,
|
||||
wallet: wallet,
|
||||
}
|
||||
// First we write the accounts store file.
|
||||
ctx := context.Background()
|
||||
store, err := dr.CreateAccountsKeystore(ctx, randomPrivateKeys, randomPublicKeys)
|
||||
require.NoError(t, err)
|
||||
existingDisabledKeysStr := make([]string, len(tt.existingDisabledKeys))
|
||||
for i := 0; i < len(tt.existingDisabledKeys); i++ {
|
||||
existingDisabledKeysStr[i] = fmt.Sprintf("%x", tt.existingDisabledKeys[i])
|
||||
}
|
||||
store.DisabledPublicKeys = existingDisabledKeysStr
|
||||
encoded, err := json.Marshal(store)
|
||||
require.NoError(t, err)
|
||||
err = dr.wallet.WriteFileAtPath(ctx, AccountsPath, AccountsKeystoreFileName, encoded)
|
||||
require.NoError(t, err)
|
||||
|
||||
if err := dr.DisableAccounts(ctx, tt.keysToDisable); (err != nil) != tt.wantErr {
|
||||
t.Errorf("DisableAccounts() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
if !tt.wantErr {
|
||||
wanted := make(map[[48]byte]bool)
|
||||
for _, pubKey := range tt.expectedDisabledKeys {
|
||||
wanted[bytesutil.ToBytes48(pubKey)] = true
|
||||
}
|
||||
// We verify that the updated disabled keys are reflected on disk as well.
|
||||
encoded, err := wallet.ReadFileAtPath(ctx, AccountsPath, AccountsKeystoreFileName)
|
||||
require.NoError(t, err)
|
||||
keystore := &AccountsKeystoreRepresentation{}
|
||||
require.NoError(t, json.Unmarshal(encoded, keystore))
|
||||
|
||||
require.Equal(t, len(wanted), len(keystore.DisabledPublicKeys))
|
||||
for _, pubKey := range keystore.DisabledPublicKeys {
|
||||
pubKeyBytes, err := hex.DecodeString(strings.TrimPrefix(pubKey, "0x"))
|
||||
require.NoError(t, err)
|
||||
if _, ok := wanted[bytesutil.ToBytes48(pubKeyBytes)]; !ok {
|
||||
t.Errorf("Expected %#x in disabled keys, but not found", pubKeyBytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeymanager_EnableAccounts(t *testing.T) {
|
||||
numKeys := 5
|
||||
randomPrivateKeys := make([][]byte, numKeys)
|
||||
randomPublicKeys := make([][]byte, numKeys)
|
||||
for i := 0; i < numKeys; i++ {
|
||||
key, err := bls.RandKey()
|
||||
require.NoError(t, err)
|
||||
randomPrivateKeys[i] = key.Marshal()
|
||||
randomPublicKeys[i] = key.PublicKey().Marshal()
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
existingDisabledKeys [][]byte
|
||||
keysToEnable [][]byte
|
||||
expectedDisabledKeys [][]byte
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Trying to enable already enabled keys fails silently",
|
||||
existingDisabledKeys: make([][]byte, 0),
|
||||
keysToEnable: randomPublicKeys,
|
||||
wantErr: false,
|
||||
expectedDisabledKeys: nil,
|
||||
},
|
||||
{
|
||||
name: "Trying to enable a subset of keys works",
|
||||
existingDisabledKeys: randomPublicKeys[0:2],
|
||||
keysToEnable: randomPublicKeys[0:1],
|
||||
wantErr: false,
|
||||
expectedDisabledKeys: randomPublicKeys[1:2],
|
||||
},
|
||||
{
|
||||
name: "Nil input keys to enable returns error",
|
||||
existingDisabledKeys: randomPublicKeys,
|
||||
keysToEnable: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "No input keys to enable returns error",
|
||||
existingDisabledKeys: randomPublicKeys,
|
||||
keysToEnable: make([][]byte, 0),
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "No existing enabled keys updates after enablng",
|
||||
existingDisabledKeys: randomPublicKeys,
|
||||
keysToEnable: randomPublicKeys,
|
||||
expectedDisabledKeys: make([][]byte, 0),
|
||||
},
|
||||
{
|
||||
name: "Disjoint sets of already enabled + newly enabled leads to whole set",
|
||||
existingDisabledKeys: randomPublicKeys[0:2],
|
||||
keysToEnable: randomPublicKeys[0:2],
|
||||
wantErr: false,
|
||||
expectedDisabledKeys: make([][]byte, 0),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
wallet := &mock.Wallet{
|
||||
Files: make(map[string]map[string][]byte),
|
||||
}
|
||||
disabledPubKeys := make(map[[48]byte]bool)
|
||||
for _, pubKey := range tt.existingDisabledKeys {
|
||||
disabledPubKeys[bytesutil.ToBytes48(pubKey)] = true
|
||||
}
|
||||
dr := &Keymanager{
|
||||
disabledPublicKeys: disabledPubKeys,
|
||||
wallet: wallet,
|
||||
}
|
||||
// First we write the accounts store file.
|
||||
ctx := context.Background()
|
||||
store, err := dr.CreateAccountsKeystore(ctx, randomPrivateKeys, randomPublicKeys)
|
||||
require.NoError(t, err)
|
||||
existingDisabledKeysStr := make([]string, len(tt.existingDisabledKeys))
|
||||
for i := 0; i < len(tt.existingDisabledKeys); i++ {
|
||||
existingDisabledKeysStr[i] = fmt.Sprintf("%x", tt.existingDisabledKeys[i])
|
||||
}
|
||||
store.DisabledPublicKeys = existingDisabledKeysStr
|
||||
encoded, err := json.Marshal(store)
|
||||
require.NoError(t, err)
|
||||
err = dr.wallet.WriteFileAtPath(ctx, AccountsPath, AccountsKeystoreFileName, encoded)
|
||||
require.NoError(t, err)
|
||||
|
||||
if err := dr.EnableAccounts(ctx, tt.keysToEnable); (err != nil) != tt.wantErr {
|
||||
t.Errorf("EnableAccounts() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
if !tt.wantErr {
|
||||
wanted := make(map[[48]byte]bool)
|
||||
for _, pubKey := range tt.expectedDisabledKeys {
|
||||
wanted[bytesutil.ToBytes48(pubKey)] = true
|
||||
}
|
||||
for pubKey := range dr.disabledPublicKeys {
|
||||
if _, ok := wanted[pubKey]; !ok {
|
||||
t.Errorf("Expected %#x in disabled keys, but not found", pubKey)
|
||||
}
|
||||
}
|
||||
// We verify that the updated disabled keys are reflected on disk as well.
|
||||
encoded, err := wallet.ReadFileAtPath(ctx, AccountsPath, AccountsKeystoreFileName)
|
||||
require.NoError(t, err)
|
||||
keystore := &AccountsKeystoreRepresentation{}
|
||||
require.NoError(t, json.Unmarshal(encoded, keystore))
|
||||
|
||||
require.Equal(t, len(wanted), len(keystore.DisabledPublicKeys))
|
||||
for _, pubKey := range keystore.DisabledPublicKeys {
|
||||
pubKeyBytes, err := hex.DecodeString(strings.TrimPrefix(pubKey, "0x"))
|
||||
require.NoError(t, err)
|
||||
if _, ok := wanted[bytesutil.ToBytes48(pubKeyBytes)]; !ok {
|
||||
t.Errorf("Expected %#x in disabled keys, but not found", pubKeyBytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package imported
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -42,7 +41,6 @@ const (
|
||||
type Keymanager struct {
|
||||
wallet iface.Wallet
|
||||
accountsStore *accountStore
|
||||
disabledPublicKeys map[[48]byte]bool
|
||||
accountsChangedFeed *event.Feed
|
||||
}
|
||||
|
||||
@@ -61,14 +59,12 @@ type accountStore struct {
|
||||
}
|
||||
|
||||
// AccountsKeystoreRepresentation defines an internal Prysm representation
|
||||
// of validator accounts, encrypted according to the EIP-2334 standard
|
||||
// but containing extra fields such as markers for disabled public keys.
|
||||
// of validator accounts, encrypted according to the EIP-2334 standard.
|
||||
type AccountsKeystoreRepresentation struct {
|
||||
Crypto map[string]interface{} `json:"crypto"`
|
||||
ID string `json:"uuid"`
|
||||
Version uint `json:"version"`
|
||||
Name string `json:"name"`
|
||||
DisabledPublicKeys []string `json:"disabled_public_keys"`
|
||||
Crypto map[string]interface{} `json:"crypto"`
|
||||
ID string `json:"uuid"`
|
||||
Version uint `json:"version"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// ResetCaches for the keymanager.
|
||||
@@ -85,7 +81,6 @@ func NewKeymanager(ctx context.Context, cfg *SetupConfig) (*Keymanager, error) {
|
||||
wallet: cfg.Wallet,
|
||||
accountsStore: &accountStore{},
|
||||
accountsChangedFeed: new(event.Feed),
|
||||
disabledPublicKeys: make(map[[48]byte]bool),
|
||||
}
|
||||
|
||||
if err := k.initializeAccountKeystore(ctx); err != nil {
|
||||
@@ -142,17 +137,6 @@ func (km *Keymanager) ValidatingAccountNames() ([]string, error) {
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// DisabledPublicKeys returns the currently disabled public keys in the keymanager.
|
||||
func (km *Keymanager) DisabledPublicKeys() [][]byte {
|
||||
disabledPubKeys := make([][]byte, 0)
|
||||
for pubKey := range km.disabledPublicKeys {
|
||||
pubKeyBytes := make([]byte, 48)
|
||||
copy(pubKeyBytes, pubKey[:])
|
||||
disabledPubKeys = append(disabledPubKeys, pubKeyBytes)
|
||||
}
|
||||
return disabledPubKeys
|
||||
}
|
||||
|
||||
// Initialize public and secret key caches that are used to speed up the functions
|
||||
// FetchValidatingPublicKeys and Sign
|
||||
func (km *Keymanager) initializeKeysCachesFromKeystore() error {
|
||||
@@ -224,23 +208,6 @@ func (km *Keymanager) FetchValidatingPublicKeys(ctx context.Context) ([][48]byte
|
||||
ctx, span := trace.StartSpan(ctx, "keymanager.FetchValidatingPublicKeys")
|
||||
defer span.End()
|
||||
|
||||
lock.RLock()
|
||||
keys := orderedPublicKeys
|
||||
result := make([][48]byte, 0)
|
||||
for _, pk := range keys {
|
||||
if _, ok := km.disabledPublicKeys[pk]; !ok {
|
||||
result = append(result, pk)
|
||||
}
|
||||
}
|
||||
lock.RUnlock()
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// FetchAllValidatingPublicKeys fetches the list of all public keys (including disabled ones) from the imported account keystores.
|
||||
func (km *Keymanager) FetchAllValidatingPublicKeys(ctx context.Context) ([][48]byte, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "keymanager.FetchValidatingPublicKeys")
|
||||
defer span.End()
|
||||
|
||||
lock.RLock()
|
||||
keys := orderedPublicKeys
|
||||
result := make([][48]byte, len(keys))
|
||||
@@ -259,13 +226,11 @@ func (km *Keymanager) FetchValidatingPrivateKeys(ctx context.Context) ([][32]byt
|
||||
return nil, errors.Wrap(err, "could not retrieve public keys")
|
||||
}
|
||||
for i, pk := range pubKeys {
|
||||
if _, ok := km.disabledPublicKeys[pk]; !ok {
|
||||
seckey, ok := secretKeysCache[pk]
|
||||
if !ok {
|
||||
return nil, errors.New("Could not fetch private key")
|
||||
}
|
||||
privKeys[i] = bytesutil.ToBytes32(seckey.Marshal())
|
||||
seckey, ok := secretKeysCache[pk]
|
||||
if !ok {
|
||||
return nil, errors.New("Could not fetch private key")
|
||||
}
|
||||
privKeys[i] = bytesutil.ToBytes32(seckey.Marshal())
|
||||
}
|
||||
return privKeys, nil
|
||||
}
|
||||
@@ -323,17 +288,6 @@ func (km *Keymanager) initializeAccountKeystore(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
km.accountsStore = store
|
||||
|
||||
lock.Lock()
|
||||
for _, pubKey := range keystoreFile.DisabledPublicKeys {
|
||||
pubKeyBytes, err := hex.DecodeString(pubKey)
|
||||
if err != nil {
|
||||
lock.Unlock()
|
||||
return err
|
||||
}
|
||||
km.disabledPublicKeys[bytesutil.ToBytes48(pubKeyBytes)] = true
|
||||
}
|
||||
lock.Unlock()
|
||||
err = km.initializeKeysCachesFromKeystore()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to initialize keys caches")
|
||||
@@ -394,17 +348,10 @@ func (km *Keymanager) CreateAccountsKeystore(
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not encrypt accounts")
|
||||
}
|
||||
disabledPubKeys := make([]string, 0)
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
for pubKey := range km.disabledPublicKeys {
|
||||
disabledPubKeys = append(disabledPubKeys, fmt.Sprintf("%x", pubKey))
|
||||
}
|
||||
return &AccountsKeystoreRepresentation{
|
||||
Crypto: cryptoFields,
|
||||
ID: id.String(),
|
||||
Version: encryptor.Version(),
|
||||
Name: encryptor.Name(),
|
||||
DisabledPublicKeys: disabledPubKeys,
|
||||
Crypto: cryptoFields,
|
||||
ID: id.String(),
|
||||
Version: encryptor.Version(),
|
||||
Name: encryptor.Name(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -74,9 +74,8 @@ func TestImportedKeymanager_FetchValidatingPublicKeys(t *testing.T) {
|
||||
WalletPassword: password,
|
||||
}
|
||||
dr := &Keymanager{
|
||||
wallet: wallet,
|
||||
accountsStore: &accountStore{},
|
||||
disabledPublicKeys: make(map[[48]byte]bool),
|
||||
wallet: wallet,
|
||||
accountsStore: &accountStore{},
|
||||
}
|
||||
// First, generate accounts and their keystore.json files.
|
||||
ctx := context.Background()
|
||||
@@ -86,53 +85,15 @@ func TestImportedKeymanager_FetchValidatingPublicKeys(t *testing.T) {
|
||||
privKey, err := bls.RandKey()
|
||||
require.NoError(t, err)
|
||||
pubKey := bytesutil.ToBytes48(privKey.PublicKey().Marshal())
|
||||
if i == 0 {
|
||||
// Manually disable the first public key by adding it to the keymanager options
|
||||
dr.disabledPublicKeys[pubKey] = true
|
||||
} else {
|
||||
wantedPubKeys = append(wantedPubKeys, pubKey)
|
||||
}
|
||||
wantedPubKeys = append(wantedPubKeys, pubKey)
|
||||
dr.accountsStore.PublicKeys = append(dr.accountsStore.PublicKeys, pubKey[:])
|
||||
dr.accountsStore.PrivateKeys = append(dr.accountsStore.PrivateKeys, privKey.Marshal())
|
||||
}
|
||||
require.NoError(t, dr.initializeKeysCachesFromKeystore())
|
||||
publicKeys, err := dr.FetchValidatingPublicKeys(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, numAccounts-1, len(publicKeys))
|
||||
// FetchValidatingPublicKeys is also used in generating the output of account list
|
||||
// therefore the results must be in the same order as the order in which the accounts were derived
|
||||
for i, key := range wantedPubKeys {
|
||||
assert.Equal(t, key, publicKeys[i])
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportedKeymanager_FetchAllValidatingPublicKeys(t *testing.T) {
|
||||
wallet := &mock.Wallet{
|
||||
Files: make(map[string]map[string][]byte),
|
||||
WalletPassword: password,
|
||||
}
|
||||
dr := &Keymanager{
|
||||
wallet: wallet,
|
||||
accountsStore: &accountStore{},
|
||||
disabledPublicKeys: make(map[[48]byte]bool),
|
||||
}
|
||||
// First, generate accounts and their keystore.json files.
|
||||
ctx := context.Background()
|
||||
numAccounts := 10
|
||||
wantedPubKeys := make([][48]byte, numAccounts)
|
||||
for i := 0; i < numAccounts; i++ {
|
||||
privKey, err := bls.RandKey()
|
||||
require.NoError(t, err)
|
||||
pubKey := bytesutil.ToBytes48(privKey.PublicKey().Marshal())
|
||||
wantedPubKeys[i] = pubKey
|
||||
dr.accountsStore.PublicKeys = append(dr.accountsStore.PublicKeys, pubKey[:])
|
||||
dr.accountsStore.PrivateKeys = append(dr.accountsStore.PrivateKeys, privKey.Marshal())
|
||||
}
|
||||
require.NoError(t, dr.initializeKeysCachesFromKeystore())
|
||||
publicKeys, err := dr.FetchAllValidatingPublicKeys(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, numAccounts, len(publicKeys))
|
||||
// FetchAllValidatingPublicKeys is also used in generating the output of account list
|
||||
// FetchValidatingPublicKeys is also used in generating the output of account list
|
||||
// therefore the results must be in the same order as the order in which the accounts were derived
|
||||
for i, key := range wantedPubKeys {
|
||||
assert.Equal(t, key, publicKeys[i])
|
||||
@@ -145,9 +106,8 @@ func TestImportedKeymanager_FetchValidatingPrivateKeys(t *testing.T) {
|
||||
WalletPassword: password,
|
||||
}
|
||||
dr := &Keymanager{
|
||||
wallet: wallet,
|
||||
accountsStore: &accountStore{},
|
||||
disabledPublicKeys: make(map[[48]byte]bool),
|
||||
wallet: wallet,
|
||||
accountsStore: &accountStore{},
|
||||
}
|
||||
// First, generate accounts and their keystore.json files.
|
||||
ctx := context.Background()
|
||||
@@ -180,9 +140,8 @@ func TestImportedKeymanager_Sign(t *testing.T) {
|
||||
WalletPassword: password,
|
||||
}
|
||||
dr := &Keymanager{
|
||||
wallet: wallet,
|
||||
accountsStore: &accountStore{},
|
||||
disabledPublicKeys: make(map[[48]byte]bool),
|
||||
wallet: wallet,
|
||||
accountsStore: &accountStore{},
|
||||
}
|
||||
|
||||
// First, generate accounts and their keystore.json files.
|
||||
|
||||
@@ -2,12 +2,9 @@ package imported
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/pkg/errors"
|
||||
@@ -117,20 +114,6 @@ func (km *Keymanager) reloadAccountsFromKeystore(keystore *AccountsKeystoreRepre
|
||||
pubKeyBytes := privKey.PublicKey().Marshal()
|
||||
pubKeys[i] = bytesutil.ToBytes48(pubKeyBytes)
|
||||
}
|
||||
lock.Lock()
|
||||
for _, pubKey := range keystore.DisabledPublicKeys {
|
||||
pubKeyBytes, err := hex.DecodeString(strings.TrimPrefix(pubKey, "0x"))
|
||||
if err != nil {
|
||||
lock.Unlock()
|
||||
return err
|
||||
}
|
||||
if len(pubKeyBytes) != 48 {
|
||||
lock.Unlock()
|
||||
return fmt.Errorf("public key %s has wrong length", pubKey)
|
||||
}
|
||||
km.disabledPublicKeys[bytesutil.ToBytes48(pubKeyBytes)] = true
|
||||
}
|
||||
lock.Unlock()
|
||||
km.accountsStore = newAccountsStore
|
||||
if err := km.initializeKeysCachesFromKeystore(); err != nil {
|
||||
return err
|
||||
|
||||
@@ -209,11 +209,6 @@ func (km *Keymanager) FetchValidatingPublicKeys(ctx context.Context) ([][48]byte
|
||||
return pubKeys, nil
|
||||
}
|
||||
|
||||
// FetchAllValidatingPublicKeys fetches the list of all public keys, including disabled ones.
|
||||
func (km *Keymanager) FetchAllValidatingPublicKeys(ctx context.Context) ([][48]byte, error) {
|
||||
return km.FetchValidatingPublicKeys(ctx)
|
||||
}
|
||||
|
||||
// Sign signs a message for a validator key via a gRPC request.
|
||||
func (km *Keymanager) Sign(ctx context.Context, req *validatorpb.SignRequest) (bls.Signature, error) {
|
||||
resp, err := km.client.Sign(ctx, req)
|
||||
|
||||
@@ -13,8 +13,6 @@ import (
|
||||
type IKeymanager interface {
|
||||
// FetchValidatingPublicKeys fetches the list of active public keys that should be used to validate with.
|
||||
FetchValidatingPublicKeys(ctx context.Context) ([][48]byte, error)
|
||||
// FetchAllValidatingPublicKeys fetches the list of all public keys, including disabled ones.
|
||||
FetchAllValidatingPublicKeys(ctx context.Context) ([][48]byte, error)
|
||||
// Sign signs a message using a validator key.
|
||||
Sign(context.Context, *validatorpb.SignRequest) (bls.Signature, error)
|
||||
// SubscribeAccountChanges subscribes to changes made to the underlying keys.
|
||||
|
||||
Reference in New Issue
Block a user