2018-02-22 07:17:19 -08:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2018-11-29 12:55:23 -08:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
2018-02-22 07:17:19 -08:00
|
|
|
"fmt"
|
2018-11-29 12:55:23 -08:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2018-02-22 07:17:19 -08:00
|
|
|
|
2018-02-23 01:40:10 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-02-05 08:22:56 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
2018-02-22 07:17:19 -08:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2018-02-22 08:20:25 -08:00
|
|
|
func deleteKeyCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "delete <name>",
|
|
|
|
Short: "Delete the given key",
|
2018-11-29 12:55:23 -08:00
|
|
|
Long: `Delete a key from the store.
|
|
|
|
|
|
|
|
Note that removing offline or ledger keys will remove
|
|
|
|
only the public key references stored locally, i.e.
|
|
|
|
private keys stored in a ledger device cannot be deleted with
|
|
|
|
gaiacli.
|
|
|
|
`,
|
|
|
|
RunE: runDeleteCmd,
|
|
|
|
Args: cobra.ExactArgs(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,
|
|
|
|
"Remove the key unconditionally without asking for the passphrase")
|
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 {
|
|
|
|
name := args[0]
|
|
|
|
|
2019-02-06 11:23:49 -08:00
|
|
|
kb, err := NewKeyBaseFromHomeFlag()
|
2018-02-22 07:17:19 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-29 12:55:23 -08:00
|
|
|
info, err := kb.Get(name)
|
2018-07-05 18:03:41 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := client.BufferStdin()
|
2018-11-29 12:55:23 -08:00
|
|
|
if info.GetType() == keys.TypeLedger || info.GetType() == keys.TypeOffline {
|
|
|
|
if !viper.GetBool(flagYes) {
|
|
|
|
if err := confirmDeletion(buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
fmt.Fprintln(os.Stderr, "Public key reference deleted")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-30 12:36:55 -08:00
|
|
|
// skip passphrase check if run with --force
|
|
|
|
skipPass := viper.GetBool(flagForce)
|
|
|
|
var oldpass string
|
|
|
|
if !skipPass {
|
|
|
|
if oldpass, err = client.GetPassword(
|
|
|
|
"DANGER - enter password to permanently delete key:", buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
|
|
|
|
2018-11-30 12:36:55 -08:00
|
|
|
err = kb.Delete(name, oldpass, skipPass)
|
2018-02-22 07:17:19 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-29 12:55:23 -08:00
|
|
|
fmt.Fprintln(os.Stderr, "Key deleted forever (uh oh!)")
|
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 {
|
|
|
|
answer, err := client.GetConfirmation("Key reference will be deleted. Continue?", buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !answer {
|
|
|
|
return errors.New("aborted")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|