From 093c4ada5c2ad7d6bebad8daecff4e065726be9e Mon Sep 17 00:00:00 2001 From: HaoyangLiu Date: Mon, 3 Sep 2018 09:56:53 +0800 Subject: [PATCH] remove useless fmt.Sprintf and add paramCheck to recover key --- client/keys/add.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index de6039391d..bca47883a4 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -164,12 +164,12 @@ type NewKeyBody struct { Seed string `json:"seed"` } -func paramCheck(m NewKeyBody, kb keys.Keybase) (int, string) { - if m.Name == "" { - return http.StatusBadRequest, fmt.Sprintf("You have to specify a name for the locally stored account.") +func paramCheck(name, password string, kb keys.Keybase) (int, string) { + if name == "" { + return http.StatusBadRequest, "You have to specify a name for the locally stored account." } - if m.Password == "" { - return http.StatusBadRequest, fmt.Sprintf("You have to specify a password for the locally stored account.") + if password == "" { + return http.StatusBadRequest, "You have to specify a password for the locally stored account." } // check if already exists @@ -178,8 +178,8 @@ func paramCheck(m NewKeyBody, kb keys.Keybase) (int, string) { return http.StatusInternalServerError, err.Error() } for _, i := range infos { - if i.GetName() == m.Name { - return http.StatusConflict, fmt.Sprintf("Account with name %s already exists.", m.Name) + if i.GetName() == name { + return http.StatusConflict, fmt.Sprintf("Account with name %s already exists.", name) } } return http.StatusOK, "" @@ -211,7 +211,7 @@ func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) { return } - code, errMsg := paramCheck(m, kb) + code, errMsg := paramCheck(m.Name, m.Password, kb) if code != http.StatusOK { w.WriteHeader(code) w.Write([]byte(errMsg)) @@ -304,6 +304,13 @@ func RecoverKeyResuestHandler(w http.ResponseWriter, r *http.Request) { return } + code, errMsg := paramCheck(name, m.Password, kb) + if code != http.StatusOK { + w.WriteHeader(code) + w.Write([]byte(errMsg)) + return + } + info, err := kb.CreateKey(name, m.Seed, m.Password) if err != nil { w.WriteHeader(http.StatusInternalServerError)