2020-10-15 22:31:52 +00:00
|
|
|
package accounts
|
2020-08-11 03:54:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-08-20 17:53:09 +00:00
|
|
|
"os"
|
2020-08-11 03:54:37 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-03-02 18:58:40 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/cmd/validator/flags"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-09-17 21:55:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/io/prompt"
|
2021-11-19 04:11:54 +00:00
|
|
|
ethpbservice "github.com/prysmaticlabs/prysm/proto/eth/service"
|
2021-02-24 18:05:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/iface"
|
2021-09-17 21:55:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/userprompt"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
2020-08-11 03:54:37 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2020-08-31 19:46:45 +00:00
|
|
|
// DeleteAccountCli deletes the accounts that the user requests to be deleted from the wallet.
|
|
|
|
// This function uses the CLI to extract necessary values.
|
|
|
|
func DeleteAccountCli(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-10-22 17:31:03 +00:00
|
|
|
return nil, wallet.ErrNoWalletFound
|
2020-08-31 19:46:45 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-08-11 03:54:37 +00:00
|
|
|
return errors.Wrap(err, "could not open wallet")
|
|
|
|
}
|
2021-02-24 18:05:46 +00:00
|
|
|
kManager, err := w.InitializeKeymanager(cliCtx.Context, iface.InitKeymanagerConfig{ListenForChanges: false})
|
2020-08-11 03:54:37 +00:00
|
|
|
if err != nil {
|
2021-01-12 16:52:01 +00:00
|
|
|
return errors.Wrap(err, ErrCouldNotInitializeKeymanager)
|
2020-08-11 03:54:37 +00:00
|
|
|
}
|
2021-03-09 15:46:50 +00:00
|
|
|
validatingPublicKeys, err := kManager.FetchValidatingPublicKeys(cliCtx.Context)
|
2020-08-11 03:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-11 23:15:06 +00:00
|
|
|
if len(validatingPublicKeys) == 0 {
|
2020-08-11 03:54:37 +00:00
|
|
|
return errors.New("wallet is empty, no accounts to delete")
|
|
|
|
}
|
2020-08-20 19:14:03 +00:00
|
|
|
// Allow the user to interactively select the accounts to delete or optionally
|
2020-08-11 23:15:06 +00:00
|
|
|
// provide them via cli flags as a string of comma-separated, hex strings.
|
|
|
|
filteredPubKeys, err := filterPublicKeysFromUserInput(
|
|
|
|
cliCtx,
|
|
|
|
flags.DeletePublicKeysFlag,
|
|
|
|
validatingPublicKeys,
|
2021-09-17 21:55:24 +00:00
|
|
|
userprompt.SelectAccountsDeletePromptText,
|
2020-08-11 23:15:06 +00:00
|
|
|
)
|
2020-08-11 03:54:37 +00:00
|
|
|
if err != nil {
|
2020-08-11 23:15:06 +00:00
|
|
|
return errors.Wrap(err, "could not filter public keys for deletion")
|
2020-08-11 03:54:37 +00:00
|
|
|
}
|
2020-08-11 23:15:06 +00:00
|
|
|
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))
|
2020-08-11 03:54:37 +00:00
|
|
|
}
|
|
|
|
allAccountStr := strings.Join(formattedPubKeys, ", ")
|
2020-08-13 15:23:08 +00:00
|
|
|
if !cliCtx.IsSet(flags.DeletePublicKeysFlag.Name) {
|
|
|
|
if len(filteredPubKeys) == 1 {
|
|
|
|
promptText := "Are you sure you want to delete 1 account? (%s) Y/N"
|
2021-09-17 21:55:24 +00:00
|
|
|
resp, err := prompt.ValidatePrompt(
|
|
|
|
os.Stdin, fmt.Sprintf(promptText, au.BrightGreen(formattedPubKeys[0])), prompt.ValidateYesOrNo,
|
2020-08-13 15:23:08 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-04 16:46:22 +00:00
|
|
|
if strings.EqualFold(resp, "n") {
|
2020-08-13 15:23:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-11 03:54:37 +00:00
|
|
|
} else {
|
2020-08-13 15:23:08 +00:00
|
|
|
promptText := "Are you sure you want to delete %d accounts? (%s) Y/N"
|
|
|
|
if len(filteredPubKeys) == len(validatingPublicKeys) {
|
|
|
|
promptText = fmt.Sprintf("Are you sure you want to delete all accounts? Y/N (%s)", au.BrightGreen(allAccountStr))
|
|
|
|
} else {
|
|
|
|
promptText = fmt.Sprintf(promptText, len(filteredPubKeys), au.BrightGreen(allAccountStr))
|
|
|
|
}
|
2021-09-17 21:55:24 +00:00
|
|
|
resp, err := prompt.ValidatePrompt(os.Stdin, promptText, prompt.ValidateYesOrNo)
|
2020-08-13 15:23:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-04 16:46:22 +00:00
|
|
|
if strings.EqualFold(resp, "n") {
|
2020-08-13 15:23:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-11 23:15:06 +00:00
|
|
|
}
|
2020-08-11 03:54:37 +00:00
|
|
|
}
|
2021-02-03 16:59:17 +00:00
|
|
|
if err := DeleteAccount(cliCtx.Context, &Config{
|
2020-11-13 16:06:24 +00:00
|
|
|
Wallet: w,
|
2021-02-12 23:04:45 +00:00
|
|
|
Keymanager: kManager,
|
2020-11-13 16:06:24 +00:00
|
|
|
DeletePublicKeys: rawPublicKeys,
|
2020-08-31 19:46:45 +00:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-10 21:13:09 +00:00
|
|
|
log.WithField("publicKeys", allAccountStr).Warn(
|
|
|
|
"Attempted to delete accounts. IMPORTANT: please run `validator accounts list` to ensure " +
|
|
|
|
"the public keys are indeed deleted. If they are still there, please file an issue at " +
|
|
|
|
"https://github.com/prysmaticlabs/prysm/issues/new")
|
2020-08-31 19:46:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAccount deletes the accounts that the user requests to be deleted from the wallet.
|
2021-02-03 16:59:17 +00:00
|
|
|
func DeleteAccount(ctx context.Context, cfg *Config) error {
|
2021-11-19 04:11:54 +00:00
|
|
|
deleter, ok := cfg.Keymanager.(keymanager.Deleter)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("keymanager does not implement Deleter interface")
|
|
|
|
}
|
|
|
|
if len(cfg.DeletePublicKeys) == 1 {
|
|
|
|
log.Info("Deleting account...")
|
|
|
|
} else {
|
|
|
|
log.Info("Deleting accounts...")
|
|
|
|
}
|
|
|
|
statuses, err := deleter.DeleteKeystores(ctx, cfg.DeletePublicKeys)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not delete accounts")
|
|
|
|
}
|
|
|
|
for i, status := range statuses {
|
|
|
|
switch status.Status {
|
|
|
|
case ethpbservice.DeletedKeystoreStatus_ERROR:
|
|
|
|
log.Errorf("Error deleting key %#x: %s", bytesutil.Trunc(cfg.DeletePublicKeys[i]), status.Message)
|
|
|
|
case ethpbservice.DeletedKeystoreStatus_NOT_ACTIVE:
|
|
|
|
log.Warnf("Duplicate key %#x found in delete request", bytesutil.Trunc(cfg.DeletePublicKeys[i]))
|
|
|
|
case ethpbservice.DeletedKeystoreStatus_NOT_FOUND:
|
|
|
|
log.Warnf("Could not find keystore for %#x", bytesutil.Trunc(cfg.DeletePublicKeys[i]))
|
2020-11-16 22:26:04 +00:00
|
|
|
}
|
2020-08-11 03:54:37 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|