OSDN Git Service

adjust message type
authoroysheng <oysheng@bytom.io>
Tue, 8 May 2018 02:13:49 +0000 (10:13 +0800)
committeroysheng <oysheng@bytom.io>
Tue, 8 May 2018 02:59:52 +0000 (10:59 +0800)
api/message.go
cmd/bytomcli/commands/key.go

index bca693b..213ea04 100644 (file)
@@ -21,7 +21,7 @@ type SignMsgResp struct {
 
 func (a *API) signMessage(ctx context.Context, ins struct {
        Address  string `json:"address"`
-       Message  []byte `json:"message"`
+       Message  string `json:"message"`
        Password string `json:"password"`
 }) Response {
        cp, err := a.wallet.AccountMgr.GetProgramByAddress(ins.Address)
@@ -37,7 +37,7 @@ func (a *API) signMessage(ctx context.Context, ins struct {
        path := signers.Path(account.Signer, signers.AccountKeySpace, cp.KeyIndex)
        derivedXPubs := chainkd.DeriveXPubs(account.XPubs, path)
 
-       sig, err := a.wallet.Hsm.XSign(account.XPubs[0], path, ins.Message, ins.Password)
+       sig, err := a.wallet.Hsm.XSign(account.XPubs[0], path, []byte(ins.Message), ins.Password)
        if err != nil {
                return NewErrorResponse(err)
        }
@@ -49,15 +49,20 @@ func (a *API) signMessage(ctx context.Context, ins struct {
 
 // VerifyMsgResp is response for verify message
 type VerifyMsgResp struct {
-       VerifyResult bool `json:" result"`
+       VerifyResult bool `json:"result"`
 }
 
 func (a *API) verifyMessage(ctx context.Context, ins struct {
        Address     string       `json:"address"`
        DerivedXPub chainkd.XPub `json:"derived_xpub"`
-       Message     []byte       `json:"message"`
-       Signature   []byte       `json:"signature"`
+       Message     string       `json:"message"`
+       Signature   string       `json:"signature"`
 }) Response {
+       sig, err := hex.DecodeString(ins.Signature)
+       if err != nil {
+               return NewErrorResponse(err)
+       }
+
        derivedPK := ins.DerivedXPub.PublicKey()
        pubHash := crypto.Ripemd160(derivedPK)
        addressPubHash, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
@@ -70,7 +75,7 @@ func (a *API) verifyMessage(ctx context.Context, ins struct {
                return NewSuccessResponse(VerifyMsgResp{VerifyResult: false})
        }
 
-       if ed25519.Verify(ins.DerivedXPub.PublicKey(), ins.Message, ins.Signature) {
+       if ed25519.Verify(ins.DerivedXPub.PublicKey(), []byte(ins.Message), sig) {
                return NewSuccessResponse(VerifyMsgResp{VerifyResult: true})
        }
        return NewSuccessResponse(VerifyMsgResp{VerifyResult: false})
index bda6f55..49fb57d 100644 (file)
@@ -1,7 +1,6 @@
 package commands
 
 import (
-       "encoding/hex"
        "os"
 
        "github.com/spf13/cobra"
@@ -98,9 +97,9 @@ var signMsgCmd = &cobra.Command{
        Run: func(cmd *cobra.Command, args []string) {
                var req = struct {
                        Address  string `json:"address"`
-                       Message  []byte `json:"message"`
+                       Message  string `json:"message"`
                        Password string `json:"password"`
-               }{Address: args[0], Message: []byte(args[1]), Password: args[2]}
+               }{Address: args[0], Message: args[1], Password: args[2]}
 
                data, exitCode := util.ClientCall("/sign-message", &req)
                if exitCode != util.Success {
@@ -121,18 +120,12 @@ var verifyMsgCmd = &cobra.Command{
                        os.Exit(util.ErrLocalExe)
                }
 
-               signature, err := hex.DecodeString(args[3])
-               if err != nil {
-                       jww.ERROR.Println("convert signature error:", err)
-                       os.Exit(util.ErrLocalExe)
-               }
-
                var req = struct {
                        Address     string       `json:"address"`
                        DerivedXPub chainkd.XPub `json:"derived_xpub"`
-                       Message     []byte       `json:"message"`
-                       Signature   []byte       `json:"signature"`
-               }{Address: args[0], DerivedXPub: xpub, Message: []byte(args[2]), Signature: signature}
+                       Message     string       `json:"message"`
+                       Signature   string       `json:"signature"`
+               }{Address: args[0], DerivedXPub: xpub, Message: args[2], Signature: args[3]}
 
                data, exitCode := util.ClientCall("/verify-message", &req)
                if exitCode != util.Success {