OSDN Git Service

add API check-key-password (#1233)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Mon, 6 Aug 2018 10:24:41 +0000 (18:24 +0800)
committerPaladz <yzhu101@uottawa.ca>
Mon, 6 Aug 2018 10:24:41 +0000 (18:24 +0800)
api/api.go
api/hsm.go
cmd/bytomcli/commands/bytomcli.go
cmd/bytomcli/commands/key.go

index 0024e00..41eaf8d 100644 (file)
@@ -217,6 +217,7 @@ func (a *API) buildHandler() {
                m.Handle("/list-keys", jsonHandler(a.pseudohsmListKeys))
                m.Handle("/delete-key", jsonHandler(a.pseudohsmDeleteKey))
                m.Handle("/reset-key-password", jsonHandler(a.pseudohsmResetPassword))
+               m.Handle("/check-key-password", jsonHandler(a.pseudohsmCheckPassword))
                m.Handle("/sign-message", jsonHandler(a.signMessage))
 
                m.Handle("/build-transaction", jsonHandler(a.build))
index 7e47676..27857ce 100644 (file)
@@ -55,7 +55,7 @@ func (a *API) pseudohsmSignTemplate(ctx context.Context, xpub chainkd.XPub, path
        return a.wallet.Hsm.XSign(xpub, path, data[:], password)
 }
 
-// ResetPasswordResp is response for reset password password
+// ResetPasswordResp is response for reset key password
 type ResetPasswordResp struct {
        Changed bool `json:"changed"`
 }
@@ -72,3 +72,20 @@ func (a *API) pseudohsmResetPassword(ctx context.Context, ins struct {
        resp.Changed = true
        return NewSuccessResponse(resp)
 }
+
+// CheckPasswordResp is response for check key password
+type CheckPasswordResp struct {
+       CheckResult bool `json:"check_result"`
+}
+
+func (a *API) pseudohsmCheckPassword(ctx context.Context, ins struct {
+       XPub     chainkd.XPub `json:"xpub"`
+       Password string       `json:"password"`
+}) Response {
+       resp := &CheckPasswordResp{CheckResult: false}
+       if _, err := a.wallet.Hsm.LoadChainKDKey(ins.XPub, ins.Password); err != nil {
+               return NewSuccessResponse(resp)
+       }
+       resp.CheckResult = true
+       return NewSuccessResponse(resp)
+}
index 5be02b3..fe45b2c 100644 (file)
@@ -153,6 +153,7 @@ func AddCommands() {
        BytomcliCmd.AddCommand(deleteKeyCmd)
        BytomcliCmd.AddCommand(listKeysCmd)
        BytomcliCmd.AddCommand(resetKeyPwdCmd)
+       BytomcliCmd.AddCommand(checkKeyPwdCmd)
 
        BytomcliCmd.AddCommand(signMsgCmd)
        BytomcliCmd.AddCommand(verifyMsgCmd)
@@ -193,6 +194,7 @@ func AddTemplateFunc() {
                deleteKeyCmd.Name(),
                listKeysCmd.Name(),
                resetKeyPwdCmd.Name(),
+               checkKeyPwdCmd.Name(),
                signMsgCmd.Name(),
 
                buildTransactionCmd.Name(),
index 262a02c..388074f 100644 (file)
@@ -92,6 +92,31 @@ var resetKeyPwdCmd = &cobra.Command{
        },
 }
 
+var checkKeyPwdCmd = &cobra.Command{
+       Use:   "check-key-password <xpub> <password>",
+       Short: "check key password",
+       Args:  cobra.ExactArgs(2),
+       Run: func(cmd *cobra.Command, args []string) {
+               xpub := new(chainkd.XPub)
+               if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
+                       jww.ERROR.Println("check-key-password args not valid:", err)
+                       os.Exit(util.ErrLocalExe)
+               }
+
+               ins := struct {
+                       XPub     chainkd.XPub `json:"xpub"`
+                       Password string       `json:"password"`
+               }{XPub: *xpub, Password: args[1]}
+
+               data, exitCode := util.ClientCall("/check-key-password", &ins)
+               if exitCode != util.Success {
+                       os.Exit(exitCode)
+               }
+
+               printJSON(data)
+       },
+}
+
 var signMsgCmd = &cobra.Command{
        Use:   "sign-message <address> <message> <password>",
        Short: "sign message to generate signature",