Skip to content

Commit

Permalink
fix sign method
Browse files Browse the repository at this point in the history
  • Loading branch information
rohenaz committed Oct 30, 2020
1 parent 6f6949a commit e3ed0ba
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
18 changes: 10 additions & 8 deletions sign.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
{
Expand Down
6 changes: 0 additions & 6 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/hex"
"fmt"
"log"

"github.com/bitcoinsv/bsvd/bsvec"
"github.com/bitcoinsv/bsvd/chaincfg"
Expand Down Expand Up @@ -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())

Expand Down
4 changes: 2 additions & 2 deletions verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
{
"",
Expand Down

0 comments on commit e3ed0ba

Please sign in to comment.