2018-02-22 07:17:19 -08:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2018-02-28 23:16:54 -08:00
|
|
|
"encoding/json"
|
2018-02-22 07:17:19 -08:00
|
|
|
"fmt"
|
2018-02-28 23:16:54 -08:00
|
|
|
"net/http"
|
2018-02-22 07:17:19 -08:00
|
|
|
|
2018-02-23 01:40:10 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2018-06-28 17:54:47 -07:00
|
|
|
keys "github.com/cosmos/cosmos-sdk/crypto/keys"
|
2018-02-28 23:16:54 -08:00
|
|
|
"github.com/gorilla/mux"
|
2018-02-22 07:17:19 -08:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-02-22 08:20:25 -08:00
|
|
|
func deleteKeyCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "delete <name>",
|
|
|
|
Short: "Delete the given key",
|
|
|
|
RunE: runDeleteCmd,
|
2018-04-26 14:32:20 -07:00
|
|
|
Args: cobra.ExactArgs(1),
|
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]
|
|
|
|
|
2018-02-28 06:20:46 -08:00
|
|
|
buf := client.BufferStdin()
|
|
|
|
oldpass, err := client.GetPassword(
|
|
|
|
"DANGER - enter password to permanently delete key:", buf)
|
2018-02-22 07:17:19 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
kb, err := GetKeyBase()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = kb.Delete(name, oldpass)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("Password deleted forever (uh oh!)")
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-28 23:16:54 -08:00
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
////////////////////////
|
2018-02-28 23:16:54 -08:00
|
|
|
// REST
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// delete key request REST body
|
2018-02-28 23:16:54 -08:00
|
|
|
type DeleteKeyBody struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// delete key REST handler
|
2018-02-28 23:16:54 -08:00
|
|
|
func DeleteKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
var kb keys.Keybase
|
|
|
|
var m DeleteKeyBody
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
err := decoder.Decode(&m)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
kb, err = GetKeyBase()
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO handle error if key is not available or pass is wrong
|
|
|
|
err = kb.Delete(name, m.Password)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}
|