diff --git a/sign.go b/sign.go index 149bec2..9db5231 100644 --- a/sign.go +++ b/sign.go @@ -1,11 +1,13 @@ package bitcoin import ( + "bytes" "encoding/base64" "errors" "github.com/bitcoinsv/bsvd/bsvec" "github.com/bitcoinsv/bsvd/chaincfg/chainhash" + "github.com/bitcoinsv/bsvd/wire" ) // SignMessage signs a string with the provided private key using Bitcoin Signed Message encoding @@ -15,19 +17,19 @@ func SignMessage(privateKey string, message string) (string, error) { if len(privateKey) == 0 { return "", errors.New("privateKey is empty") } - prefixBytes := []byte(hBSV) - messageBytes := []byte(message) - var bytes []byte - bytes = append(bytes, byte(len(prefixBytes))) - bytes = append(bytes, prefixBytes...) - bytes = append(bytes, byte(len(messageBytes))) - bytes = append(bytes, messageBytes...) + + var buf bytes.Buffer + wire.WriteVarString(&buf, 0, hBSV) + wire.WriteVarString(&buf, 0, message) + + messageHash := chainhash.DoubleHashB(buf.Bytes()) + ecdsaPrivateKey, err := PrivateKeyFromString(privateKey) if err != nil { return "", err } var sigBytes []byte - sigBytes, err = bsvec.SignCompact(bsvec.S256(), ecdsaPrivateKey, chainhash.DoubleHashB(bytes), true) + sigBytes, err = bsvec.SignCompact(bsvec.S256(), ecdsaPrivateKey, messageHash, true) if err != nil { return "", err } diff --git a/sign_test.go b/sign_test.go index 30cc7d4..043253f 100644 --- a/sign_test.go +++ b/sign_test.go @@ -31,7 +31,7 @@ func TestSignMessage(t *testing.T) { "very very very very very very very very very very very very very very very very very very very very " + "very very very very very very very very very very very very very very very very very very very very " + "very very very very very very very very long message", - "IIIjej1rrUaPXYBPWVPjqyO29vbGCAA6SyMVGebk3nUjIypyRnJj58DSchfalVfkUZ+1VxRaKOx92TzC0I7UlKU=", + "Hzy3iX82OKqLHEErf4YyzoZW+SvO7WLBtl6SuK/HfY0zJHguR0JqzurZkSz9kz2RFboGOzKENf2jMUIE/Pm0tIY=", false, }, { diff --git a/verify.go b/verify.go index a894ad5..c96e71f 100644 --- a/verify.go +++ b/verify.go @@ -5,7 +5,6 @@ import ( "encoding/base64" "encoding/hex" "fmt" - "log" "github.com/bitcoinsv/bsvd/bsvec" "github.com/bitcoinsv/bsvd/chaincfg" @@ -35,11 +34,6 @@ func VerifyMessage(address, sig, data string) error { // we will compare it with the key next. var buf bytes.Buffer wire.WriteVarString(&buf, 0, hBSV) - // TODO!!! The 0 here controls the variable length integer - if len(data) > 0xFD { - // https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer - log.Println("Long message! Change varint!") - } wire.WriteVarString(&buf, 0, data) expectedMessageHash := chainhash.DoubleHashB(buf.Bytes()) diff --git a/verify_test.go b/verify_test.go index cf9d601..0ec2b97 100644 --- a/verify_test.go +++ b/verify_test.go @@ -34,14 +34,14 @@ func TestVerifyMessage(t *testing.T) { }, { "1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", - "IIIjej1rrUaPXYBPWVPjqyO29vbGCAA6SyMVGebk3nUjIypyRnJj58DSchfalVfkUZ+1VxRaKOx92TzC0I7UlKU=", + "Hzy3iX82OKqLHEErf4YyzoZW+SvO7WLBtl6SuK/HfY0zJHguR0JqzurZkSz9kz2RFboGOzKENf2jMUIE/Pm0tIY=", "This is very very very very very very very very very very very very very very very very very" + " very very very very very very very very very very very very very very very very very very very very " + "very very very very very very very very very very very very very very very very very very very very " + "very very very very very very very very very very very very very very very very very very very very " + "very very very very very very very very very very very very very very very very very very very very " + "very very very very very very very very long message", - true, + false, }, { "",