Skip to content

Commit dd00e14

Browse files
author
David Case
committed
update bsm to receive and return []byte
1 parent fd6b293 commit dd00e14

File tree

3 files changed

+15
-32
lines changed

3 files changed

+15
-32
lines changed

compat/bsm/sign.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package compat
22

33
import (
44
"bytes"
5-
"encoding/base64"
65
"errors"
76

87
ec "github.com/bitcoin-sv/go-sdk/primitives/ec"
@@ -15,17 +14,12 @@ const hBSV = "Bitcoin Signed Message:\n"
1514
// SignMessage signs a string with the provided PrivateKey using Bitcoin Signed Message encoding
1615
// sigRefCompressedKey bool determines whether the signature will reference a compressed or uncompresed key
1716
// Spec: https://github.com/bitcoin/bitcoin/pull/524
18-
func SignMessage(privateKey *ec.PrivateKey, message string, sigRefCompressedKey bool) (string, error) {
17+
func SignMessage(privateKey *ec.PrivateKey, message []byte, sigRefCompressedKey bool) ([]byte, error) {
1918
if privateKey == nil {
20-
return "", errors.New("private key is required")
21-
}
22-
23-
if len(message) == 0 {
24-
return "", errors.New("message is required")
19+
return nil, errors.New("private key is required")
2520
}
2621

2722
b := new(bytes.Buffer)
28-
var err error
2923

3024
varInt := transaction.VarInt(len(hBSV))
3125
b.Write(varInt.Bytes())
@@ -37,16 +31,11 @@ func SignMessage(privateKey *ec.PrivateKey, message string, sigRefCompressedKey
3731
b.Write(varInt.Bytes())
3832

3933
// append the data to buff
40-
b.WriteString(message)
34+
b.Write(message)
4135

4236
// Create the hash
4337
messageHash := crypto.Sha256d(b.Bytes())
4438

4539
// Sign
46-
var sigBytes []byte
47-
if sigBytes, err = ec.SignCompact(ec.S256(), privateKey, messageHash, sigRefCompressedKey); err != nil {
48-
return "", err
49-
}
50-
51-
return base64.StdEncoding.EncodeToString(sigBytes), nil
40+
return ec.SignCompact(ec.S256(), privateKey, messageHash, sigRefCompressedKey)
5241
}

compat/bsm/sign_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package compat_test
22

33
import (
4+
"encoding/base64"
45
"fmt"
56
"testing"
67

@@ -11,7 +12,7 @@ import (
1112

1213
func TestSigningCompression(t *testing.T) {
1314
testKey, _ := ec.PrivateKeyFromHex("0499f8239bfe10eb0f5e53d543635a423c96529dd85fa4bad42049a0b435ebdd")
14-
testData := "test message"
15+
testData := []byte("test message")
1516

1617
// Test sign compressed
1718
address, err := script.NewAddressFromPublicKey(testKey.PubKey(), true)
@@ -113,11 +114,11 @@ func TestSignMessage(t *testing.T) {
113114

114115
testPk, errKey := ec.PrivateKeyFromHex(test.inputKey)
115116

116-
if signature, err := compat.SignMessage(testPk, test.inputMessage, false); err != nil && !test.expectedError {
117+
if signature, err := compat.SignMessage(testPk, []byte(test.inputMessage), false); err != nil && !test.expectedError {
117118
t.Fatalf("%d %s Failed: [%s] [%s] inputted and error not expected but got: %s", idx, t.Name(), test.inputKey, test.inputMessage, err.Error())
118119
} else if err == nil && errKey == nil && test.expectedError {
119120
t.Fatalf("%d %s Failed: [%s] [%s] inputted and error was expected", idx, t.Name(), test.inputKey, test.inputMessage)
120-
} else if signature != test.expectedSignature {
121+
} else if base64.StdEncoding.EncodeToString(signature) != test.expectedSignature {
121122
t.Fatalf("%d %s Failed: [%s] [%s] inputted [%s] expected but got: %s", idx, t.Name(), test.inputKey, test.inputMessage, test.expectedSignature, signature)
122123
}
123124

@@ -127,19 +128,19 @@ func TestSignMessage(t *testing.T) {
127128
// ExampleSignMessage example using SignMessage()
128129
func ExampleSignMessage() {
129130
pk, _ := ec.PrivateKeyFromHex("ef0b8bad0be285099534277fde328f8f19b3be9cadcd4c08e6ac0b5f863745ac")
130-
signature, err := compat.SignMessage(pk, "This is a test message", false)
131+
signature, err := compat.SignMessage(pk, []byte("This is a test message"), false)
131132
if err != nil {
132133
fmt.Printf("error occurred: %s", err.Error())
133134
return
134135
}
135-
fmt.Printf("signature created: %s", signature)
136+
fmt.Printf("signature created: %s", base64.StdEncoding.EncodeToString(signature))
136137
// Output:signature created: G+zZagsyz7ioC/ZOa5EwsaKice0vs2BvZ0ljgkFHxD3vGsMlGeD4sXHEcfbI4h8lP29VitSBdf4A+nHXih7svf4=
137138
}
138139

139140
// BenchmarkSignMessage benchmarks the method SignMessage()
140141
func BenchmarkSignMessage(b *testing.B) {
141142
key, _ := ec.NewPrivateKey()
142143
for i := 0; i < b.N; i++ {
143-
_, _ = compat.SignMessage(key, "This is a test message", false)
144+
_, _ = compat.SignMessage(key, []byte("This is a test message"), false)
144145
}
145146
}

compat/bsm/verify.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package compat
22

33
import (
44
"bytes"
5-
"encoding/base64"
65
"encoding/hex"
76
"fmt"
87

@@ -13,12 +12,7 @@ import (
1312
)
1413

1514
// PubKeyFromSignature gets a publickey for a signature and tells you whether is was compressed
16-
func PubKeyFromSignature(sig, data string) (pubKey *ec.PublicKey, wasCompressed bool, err error) {
17-
18-
var decodedSig []byte
19-
if decodedSig, err = base64.StdEncoding.DecodeString(sig); err != nil {
20-
return nil, false, err
21-
}
15+
func PubKeyFromSignature(sig, data []byte) (pubKey *ec.PublicKey, wasCompressed bool, err error) {
2216

2317
// Validate the signature - this just shows that it was valid at all
2418
// we will compare it with the key next
@@ -32,11 +26,11 @@ func PubKeyFromSignature(sig, data string) (pubKey *ec.PublicKey, wasCompressed
3226
varInt = transaction.VarInt(len(data))
3327
buf.Write(varInt.Bytes())
3428
// append the data to buff
35-
buf.WriteString(data)
29+
buf.Write(data)
3630

3731
// Create the hash
3832
expectedMessageHash := crypto.Sha256d(buf.Bytes())
39-
return ec.RecoverCompact(decodedSig, expectedMessageHash)
33+
return ec.RecoverCompact(sig, expectedMessageHash)
4034
}
4135

4236
// VerifyMessage verifies a string and address against the provided
@@ -47,8 +41,7 @@ func PubKeyFromSignature(sig, data string) (pubKey *ec.PublicKey, wasCompressed
4741
//
4842
// Error will occur if verify fails or verification is not successful (no bool)
4943
// Spec: https://github.com/bitcoin/bitcoin/pull/524
50-
func VerifyMessage(address, sig, data string) error {
51-
44+
func VerifyMessage(address string, sig, data []byte) error {
5245
// Reconstruct the pubkey
5346
publicKey, wasCompressed, err := PubKeyFromSignature(sig, data)
5447
if err != nil {

0 commit comments

Comments
 (0)