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"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/io/prompt"
|
|
|
|
ethpbservice "github.com/prysmaticlabs/prysm/v4/proto/eth/service"
|
2020-08-11 03:54:37 +00:00
|
|
|
)
|
|
|
|
|
2022-05-19 04:38:04 +00:00
|
|
|
// Delete the accounts that the user requests to be deleted from the wallet.
|
2022-05-17 23:13:36 +00:00
|
|
|
func (acm *AccountsCLIManager) Delete(ctx context.Context) error {
|
|
|
|
rawPublicKeys := make([][]byte, len(acm.filteredPubKeys))
|
|
|
|
formattedPubKeys := make([]string, len(acm.filteredPubKeys))
|
|
|
|
for i, pk := range acm.filteredPubKeys {
|
2020-08-11 23:15:06 +00:00
|
|
|
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, ", ")
|
2022-05-17 23:13:36 +00:00
|
|
|
if !acm.deletePublicKeys {
|
|
|
|
if len(acm.filteredPubKeys) == 1 {
|
2020-08-13 15:23:08 +00:00
|
|
|
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"
|
2022-05-17 23:13:36 +00:00
|
|
|
if len(acm.filteredPubKeys) == acm.walletKeyCount {
|
2020-08-13 15:23:08 +00:00
|
|
|
promptText = fmt.Sprintf("Are you sure you want to delete all accounts? Y/N (%s)", au.BrightGreen(allAccountStr))
|
|
|
|
} else {
|
2022-05-17 23:13:36 +00:00
|
|
|
promptText = fmt.Sprintf(promptText, len(acm.filteredPubKeys), au.BrightGreen(allAccountStr))
|
2020-08-13 15:23:08 +00:00
|
|
|
}
|
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
|
|
|
}
|
2022-05-17 23:13:36 +00:00
|
|
|
if err := DeleteAccount(ctx, &DeleteConfig{
|
|
|
|
Keymanager: acm.keymanager,
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-03 18:47:30 +00:00
|
|
|
// DeleteAccount performs the deletion on the Keymanager.
|
2022-03-30 21:52:54 +00:00
|
|
|
func DeleteAccount(ctx context.Context, cfg *DeleteConfig) error {
|
2021-11-19 04:11:54 +00:00
|
|
|
if len(cfg.DeletePublicKeys) == 1 {
|
|
|
|
log.Info("Deleting account...")
|
|
|
|
} else {
|
|
|
|
log.Info("Deleting accounts...")
|
|
|
|
}
|
2022-03-30 21:52:54 +00:00
|
|
|
statuses, err := cfg.Keymanager.DeleteKeystores(ctx, cfg.DeletePublicKeys)
|
2021-11-19 04:11:54 +00:00
|
|
|
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
|
|
|
|
}
|