2018-02-22 07:17:19 -08:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2018-11-29 12:55:23 -08:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
|
2020-01-22 09:54:56 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/input"
|
2020-03-25 08:20:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2020-01-22 09:54:56 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-02-22 07:17:19 -08:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2019-11-14 06:17:21 -08:00
|
|
|
"github.com/spf13/viper"
|
2018-02-22 07:17:19 -08:00
|
|
|
)
|
|
|
|
|
2018-11-29 12:55:23 -08:00
|
|
|
const (
|
2018-11-30 12:36:55 -08:00
|
|
|
flagYes = "yes"
|
|
|
|
flagForce = "force"
|
2018-11-29 12:55:23 -08:00
|
|
|
)
|
|
|
|
|
2019-12-12 13:52:24 -08:00
|
|
|
// DeleteKeyCommand deletes a key from the key store.
|
|
|
|
func DeleteKeyCommand() *cobra.Command {
|
2018-02-22 08:20:25 -08:00
|
|
|
cmd := &cobra.Command{
|
2019-12-05 07:07:29 -08:00
|
|
|
Use: "delete <name>...",
|
|
|
|
Short: "Delete the given keys",
|
|
|
|
Long: `Delete keys from the Keybase backend.
|
2018-11-29 12:55:23 -08:00
|
|
|
|
|
|
|
Note that removing offline or ledger keys will remove
|
|
|
|
only the public key references stored locally, i.e.
|
2019-05-18 01:42:24 -07:00
|
|
|
private keys stored in a ledger device cannot be deleted with the CLI.
|
2018-11-29 12:55:23 -08:00
|
|
|
`,
|
|
|
|
RunE: runDeleteCmd,
|
2019-12-05 07:07:29 -08:00
|
|
|
Args: cobra.MinimumNArgs(1),
|
2018-02-22 08:20:25 -08:00
|
|
|
}
|
2018-11-29 12:55:23 -08:00
|
|
|
|
|
|
|
cmd.Flags().BoolP(flagYes, "y", false,
|
|
|
|
"Skip confirmation prompt when deleting offline or ledger key references")
|
2018-11-30 12:36:55 -08:00
|
|
|
cmd.Flags().BoolP(flagForce, "f", false,
|
2019-12-05 07:07:29 -08:00
|
|
|
"Remove the key unconditionally without asking for the passphrase. Deprecated.")
|
2018-02-22 08:20:25 -08:00
|
|
|
return cmd
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func runDeleteCmd(cmd *cobra.Command, args []string) error {
|
2019-11-14 06:17:21 -08:00
|
|
|
buf := bufio.NewReader(cmd.InOrStdin())
|
2018-02-22 07:17:19 -08:00
|
|
|
|
2020-03-25 08:20:36 -07:00
|
|
|
kb, err := keyring.NewKeyring(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
|
2018-02-22 07:17:19 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-05 07:07:29 -08:00
|
|
|
for _, name := range args {
|
|
|
|
info, err := kb.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-05 18:03:41 -07:00
|
|
|
|
2020-03-25 08:20:36 -07:00
|
|
|
if info.GetType() == keyring.TypeLedger || info.GetType() == keyring.TypeOffline {
|
2019-12-05 07:07:29 -08:00
|
|
|
// confirm deletion, unless -y is passed
|
|
|
|
if !viper.GetBool(flagYes) {
|
|
|
|
if err := confirmDeletion(buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := kb.Delete(name, "", true); err != nil {
|
2018-11-29 12:55:23 -08:00
|
|
|
return err
|
|
|
|
}
|
2019-12-05 07:07:29 -08:00
|
|
|
cmd.PrintErrln("Public key reference deleted")
|
|
|
|
return nil
|
2018-11-29 12:55:23 -08:00
|
|
|
}
|
2019-11-14 06:17:21 -08:00
|
|
|
|
2019-12-05 07:07:29 -08:00
|
|
|
// old password and skip flag arguments are ignored
|
2018-11-30 12:36:55 -08:00
|
|
|
if err := kb.Delete(name, "", true); err != nil {
|
2018-11-29 12:55:23 -08:00
|
|
|
return err
|
|
|
|
}
|
2019-12-05 07:07:29 -08:00
|
|
|
cmd.PrintErrln("Key deleted forever (uh oh!)")
|
2018-11-29 12:55:23 -08:00
|
|
|
}
|
|
|
|
|
2018-02-22 07:17:19 -08:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-28 23:16:54 -08:00
|
|
|
|
2019-02-08 12:45:23 -08:00
|
|
|
func confirmDeletion(buf *bufio.Reader) error {
|
2019-05-28 01:44:04 -07:00
|
|
|
answer, err := input.GetConfirmation("Key reference will be deleted. Continue?", buf)
|
2019-02-08 12:45:23 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !answer {
|
|
|
|
return errors.New("aborted")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|