Refactor code for key management: use cdc instead of json and remove hardcode http status

This commit is contained in:
HaoyangLiu 2018-09-02 22:00:40 +08:00
parent 9a1dac2f87
commit e742b02d3b
6 changed files with 81 additions and 50 deletions

View File

@ -1,7 +1,6 @@
package keys
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
@ -172,13 +171,24 @@ func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
kb, err := GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
body, err := ioutil.ReadAll(r.Body)
err = json.Unmarshal(body, &m)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
err = cdc.UnmarshalJSON(body, &m)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
if err != nil {
w.WriteHeader(http.StatusBadRequest)
@ -227,7 +237,7 @@ func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
keyOutput.Seed = seed
bz, err := json.Marshal(keyOutput)
bz, err := cdc.MarshalJSON(keyOutput)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
@ -272,8 +282,14 @@ func RecoverKeyResuestHandler(w http.ResponseWriter, r *http.Request) {
name := vars["name"]
var m RecoverKeyBody
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
err = cdc.UnmarshalJSON(body, &m)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
@ -287,7 +303,6 @@ func RecoverKeyResuestHandler(w http.ResponseWriter, r *http.Request) {
return
}
info, err := kb.CreateKey(name, m.Seed, m.Password)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
@ -310,4 +325,4 @@ func RecoverKeyResuestHandler(w http.ResponseWriter, r *http.Request) {
}
w.Write(bz)
}
}

View File

@ -1,15 +1,15 @@
package keys
import (
"encoding/json"
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client"
keys "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
"io/ioutil"
)
func deleteKeyCommand() *cobra.Command {
@ -65,17 +65,23 @@ func DeleteKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
var kb keys.Keybase
var m DeleteKeyBody
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(400)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
err = cdc.UnmarshalJSON(body, &m)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
kb, err = GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
@ -83,10 +89,10 @@ func DeleteKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
// 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.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(200)
w.WriteHeader(http.StatusOK)
}

View File

@ -1,7 +1,6 @@
package keys
import (
"encoding/json"
"net/http"
"github.com/spf13/cobra"
@ -38,13 +37,13 @@ func runListCmd(cmd *cobra.Command, args []string) error {
func QueryKeysRequestHandler(w http.ResponseWriter, r *http.Request) {
kb, err := GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
infos, err := kb.List()
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
@ -55,13 +54,13 @@ func QueryKeysRequestHandler(w http.ResponseWriter, r *http.Request) {
}
keysOutput, err := Bech32KeysOutput(infos)
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
output, err := json.MarshalIndent(keysOutput, "", " ")
output, err := cdc.MarshalJSONIndent(keysOutput, "", " ")
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

View File

@ -1,7 +1,6 @@
package keys
import (
"encoding/json"
"fmt"
"net/http"
@ -108,7 +107,7 @@ func GetKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
bechKeyOut, err := getBechKeyOut(bechPrefix)
if err != nil {
w.WriteHeader(400)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
@ -117,21 +116,21 @@ func GetKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
// 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.WriteHeader(http.StatusNotFound)
w.Write([]byte(err.Error()))
return
}
keyOutput, err := bechKeyOut(info)
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
output, err := json.MarshalIndent(keyOutput, "", " ")
output, err := cdc.MarshalJSONIndent(keyOutput, "", " ")
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

View File

@ -1,19 +1,19 @@
package keys
import (
"encoding/json"
"net/http"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"encoding/base64"
"fmt"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"net/http"
)
const (
flagFrom = "from"
flagFrom = "from"
flagPassword = "password"
flagTx = "tx"
flagTx = "tx"
)
func init() {
@ -52,8 +52,8 @@ var keySignCmd = &cobra.Command{
}
type keySignBody struct {
Tx []byte `json:"tx_bytes"`
Password string `json:"password"`
Tx []byte `json:"tx_bytes"`
Password string `json:"password"`
}
// SignResuest is the handler of creating seed in swagger rest server
@ -62,24 +62,30 @@ func SignResuest(w http.ResponseWriter, r *http.Request) {
name := vars["name"]
var m keySignBody
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(400)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
err = cdc.UnmarshalJSON(body, &m)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
kb, err := GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
sig, _, err := kb.Sign(name, m.Password, m.Tx)
if err != nil {
w.WriteHeader(400)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
@ -87,4 +93,4 @@ func SignResuest(w http.ResponseWriter, r *http.Request) {
encoded := base64.StdEncoding.EncodeToString(sig)
w.Write([]byte(encoded))
}
}

View File

@ -1,15 +1,15 @@
package keys
import (
"encoding/json"
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client"
keys "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
"io/ioutil"
)
func updateKeyCommand() *cobra.Command {
@ -66,17 +66,23 @@ func UpdateKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
var kb keys.Keybase
var m UpdateKeyBody
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(400)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
err = cdc.UnmarshalJSON(body, &m)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
kb, err = GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
@ -86,10 +92,10 @@ func UpdateKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
// TODO check if account exists and if password is correct
err = kb.Update(name, m.OldPassword, getNewpass)
if err != nil {
w.WriteHeader(401)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(200)
w.WriteHeader(http.StatusOK)
}