From d7b1f24cc068180cb328308898edc8fb8b0c89fa Mon Sep 17 00:00:00 2001 From: oysheng Date: Tue, 27 Nov 2018 14:03:39 +0800 Subject: [PATCH] modify string into []byte for message --- api/message.go | 19 ++++++++++--------- cmd/bytomcli/commands/key.go | 32 +++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/api/message.go b/api/message.go index 996fa1e5..4c1ae316 100644 --- a/api/message.go +++ b/api/message.go @@ -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}) diff --git a/cmd/bytomcli/commands/key.go b/cmd/bytomcli/commands/key.go index 1d6424b9..ba96cfe9 100644 --- a/cmd/bytomcli/commands/key.go +++ b/cmd/bytomcli/commands/key.go @@ -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 { -- 2.11.0