2018-02-22 07:17:19 -08:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2018-02-28 23:16:54 -08:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
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
|
|
|
var showKeysCmd = &cobra.Command{
|
|
|
|
Use: "show <name>",
|
|
|
|
Short: "Show key info for the given name",
|
2018-02-22 07:17:19 -08:00
|
|
|
Long: `Return public details of one local key.`,
|
2018-04-26 14:32:20 -07:00
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
name := args[0]
|
|
|
|
info, err := getKey(name)
|
|
|
|
if err == nil {
|
|
|
|
printInfo(info)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
},
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
func getKey(name string) (keys.Info, error) {
|
|
|
|
kb, err := GetKeyBase()
|
|
|
|
if err != nil {
|
2018-06-28 17:54:47 -07:00
|
|
|
return nil, err
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return kb.Get(name)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// get key REST handler
|
2018-02-28 23:16:54 -08:00
|
|
|
func GetKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
|
|
|
|
info, err := getKey(name)
|
|
|
|
// TODO check for the error if key actually does not exist, instead of assuming this as the reason
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-05 21:53:04 -07:00
|
|
|
keyOutput, err := Bech32KeyOutput(info)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2018-02-28 23:16:54 -08:00
|
|
|
output, err := json.MarshalIndent(keyOutput, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(output)
|
|
|
|
}
|