OSDN Git Service

modify string into []byte for message
authoroysheng <oysheng@bytom.io>
Tue, 27 Nov 2018 06:03:39 +0000 (14:03 +0800)
committeroysheng <oysheng@bytom.io>
Tue, 27 Nov 2018 06:03:39 +0000 (14:03 +0800)
api/message.go
cmd/bytomcli/commands/key.go

index 996fa1e..4c1ae31 100644 (file)
@@ -11,6 +11,7 @@ import (
        "github.com/bytom/crypto"
        "github.com/bytom/crypto/ed25519"
        "github.com/bytom/crypto/ed25519/chainkd"
+       chainjson "github.com/bytom/encoding/json"
 )
 
 // SignMsgResp is response for sign message
@@ -20,9 +21,9 @@ type SignMsgResp struct {
 }
 
 func (a *API) signMessage(ctx context.Context, ins struct {
-       Address  string `json:"address"`
-       Message  string `json:"message"`
-       Password string `json:"password"`
+       Address  string             `json:"address"`
+       Message  chainjson.HexBytes `json:"message"`
+       Password string             `json:"password"`
 }) Response {
        cp, err := a.wallet.AccountMgr.GetLocalCtrlProgramByAddress(ins.Address)
        if err != nil {
@@ -40,7 +41,7 @@ func (a *API) signMessage(ctx context.Context, ins struct {
        }
        derivedXPubs := chainkd.DeriveXPubs(account.XPubs, path)
 
-       sig, err := a.wallet.Hsm.XSign(account.XPubs[0], path, []byte(ins.Message), ins.Password)
+       sig, err := a.wallet.Hsm.XSign(account.XPubs[0], path, ins.Message, ins.Password)
        if err != nil {
                return NewErrorResponse(err)
        }
@@ -56,10 +57,10 @@ type VerifyMsgResp struct {
 }
 
 func (a *API) verifyMessage(ctx context.Context, ins struct {
-       Address     string       `json:"address"`
-       DerivedXPub chainkd.XPub `json:"derived_xpub"`
-       Message     string       `json:"message"`
-       Signature   string       `json:"signature"`
+       Address     string             `json:"address"`
+       DerivedXPub chainkd.XPub       `json:"derived_xpub"`
+       Message     chainjson.HexBytes `json:"message"`
+       Signature   string             `json:"signature"`
 }) Response {
        sig, err := hex.DecodeString(ins.Signature)
        if err != nil {
@@ -78,7 +79,7 @@ func (a *API) verifyMessage(ctx context.Context, ins struct {
                return NewSuccessResponse(VerifyMsgResp{VerifyResult: false})
        }
 
-       if ed25519.Verify(ins.DerivedXPub.PublicKey(), []byte(ins.Message), sig) {
+       if ed25519.Verify(ins.DerivedXPub.PublicKey(), ins.Message, sig) {
                return NewSuccessResponse(VerifyMsgResp{VerifyResult: true})
        }
        return NewSuccessResponse(VerifyMsgResp{VerifyResult: false})
index 1d6424b..ba96cfe 100644 (file)
@@ -1,12 +1,14 @@
 package commands
 
 import (
+       "encoding/hex"
        "os"
 
        "github.com/spf13/cobra"
        jww "github.com/spf13/jwalterweatherman"
 
        "github.com/bytom/crypto/ed25519/chainkd"
+       chainjson "github.com/bytom/encoding/json"
        "github.com/bytom/util"
 )
 
@@ -145,11 +147,17 @@ var signMsgCmd = &cobra.Command{
        Short: "sign message to generate signature",
        Args:  cobra.ExactArgs(3),
        Run: func(cmd *cobra.Command, args []string) {
+               message, err := hex.DecodeString(args[1])
+               if err != nil {
+                       jww.ERROR.Println("sign-message args not valid:", err)
+                       os.Exit(util.ErrLocalExe)
+               }
+
                var req = struct {
-                       Address  string `json:"address"`
-                       Message  string `json:"message"`
-                       Password string `json:"password"`
-               }{Address: args[0], Message: args[1], Password: args[2]}
+                       Address  string             `json:"address"`
+                       Message  chainjson.HexBytes `json:"message"`
+                       Password string             `json:"password"`
+               }{Address: args[0], Message: message, Password: args[2]}
 
                data, exitCode := util.ClientCall("/sign-message", &req)
                if exitCode != util.Success {
@@ -170,12 +178,18 @@ var verifyMsgCmd = &cobra.Command{
                        os.Exit(util.ErrLocalExe)
                }
 
+               message, err := hex.DecodeString(args[2])
+               if err != nil {
+                       jww.ERROR.Println("sign-message args not valid:", err)
+                       os.Exit(util.ErrLocalExe)
+               }
+
                var req = struct {
-                       Address     string       `json:"address"`
-                       DerivedXPub chainkd.XPub `json:"derived_xpub"`
-                       Message     string       `json:"message"`
-                       Signature   string       `json:"signature"`
-               }{Address: args[0], DerivedXPub: xpub, Message: args[2], Signature: args[3]}
+                       Address     string             `json:"address"`
+                       DerivedXPub chainkd.XPub       `json:"derived_xpub"`
+                       Message     chainjson.HexBytes `json:"message"`
+                       Signature   string             `json:"signature"`
+               }{Address: args[0], DerivedXPub: xpub, Message: message, Signature: args[3]}
 
                data, exitCode := util.ClientCall("/verify-message", &req)
                if exitCode != util.Success {