2018-02-22 07:17:19 -08:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2018-09-23 00:41:41 -07: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-09-23 00:41:41 -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-07-05 18:03:41 -07:00
|
|
|
kb, err := GetKeyBase()
|
2018-02-22 07:17:19 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:03:41 -07:00
|
|
|
_, err = kb.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-09-23 00:41:41 -07:00
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
err := decoder.Decode(&m)
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
2018-10-04 05:27:43 -07:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2018-02-28 23:16:54 -08:00
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
kb, err = GetKeyBase()
|
|
|
|
if err != nil {
|
2018-10-04 05:27:43 -07:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-02-28 23:16:54 -08:00
|
|
|
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 {
|
2018-10-04 05:27:43 -07:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-02-28 23:16:54 -08:00
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-04 05:27:43 -07:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|