OSDN Git Service

Dev (#1504)
authorPaladz <yzhu101@uottawa.ca>
Wed, 5 Dec 2018 08:38:21 +0000 (16:38 +0800)
committerGitHub <noreply@github.com>
Wed, 5 Dec 2018 08:38:21 +0000 (16:38 +0800)
* modify string into []byte for message

* update testnet checkpoint (#1501)

api/message.go
cmd/bytomcli/commands/key.go
consensus/general.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 {
index 019e33a..16be10d 100644 (file)
@@ -135,6 +135,7 @@ var TestNetParams = Params{
                {78000, bc.NewHash([32]byte{0xa9, 0x03, 0xc0, 0x0c, 0x62, 0x1a, 0x3d, 0x00, 0x7f, 0xd8, 0x5d, 0x51, 0xba, 0x43, 0xe4, 0xd0, 0xe3, 0xc5, 0xd4, 0x8f, 0x30, 0xb5, 0x5f, 0xa5, 0x77, 0x62, 0xd8, 0x8b, 0x11, 0x81, 0x5f, 0xb4})},
                {82000, bc.NewHash([32]byte{0x56, 0xb1, 0xba, 0x23, 0x69, 0x5c, 0x8f, 0x51, 0x4e, 0x23, 0xc0, 0xae, 0xaa, 0x25, 0x08, 0xc5, 0x85, 0xf3, 0x7c, 0xd1, 0xc6, 0x15, 0xa2, 0x51, 0xda, 0x79, 0x4f, 0x08, 0x13, 0x66, 0xc9, 0x85})},
                {83200, bc.NewHash([32]byte{0xb4, 0x6f, 0xc5, 0xcf, 0xa3, 0x3d, 0xe1, 0x11, 0x71, 0x68, 0x40, 0x68, 0x0c, 0xe7, 0x4c, 0xaf, 0x5a, 0x11, 0xfe, 0x82, 0xbc, 0x36, 0x88, 0x0f, 0xbd, 0x04, 0xf0, 0xc4, 0x86, 0xd4, 0xd6, 0xd5})},
+               {93000, bc.NewHash([32]byte{0x6f, 0x4f, 0x37, 0x5f, 0xe9, 0xfb, 0xdf, 0x66, 0x60, 0x0e, 0xf0, 0x39, 0xb7, 0x18, 0x26, 0x75, 0xa0, 0x9a, 0xa5, 0x9b, 0x83, 0xc9, 0x9a, 0x25, 0x45, 0xb8, 0x7d, 0xd4, 0x99, 0x24, 0xa2, 0x8a})},
        },
 }