Skip to content

Commit 70b363c

Browse files
committed
add compressed flag for SignMessage
1 parent 01ae62c commit 70b363c

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

examples/sign_message/sign_message.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ func main() {
1414
}
1515

1616
// Sign the message (returning a signature)
17+
18+
// Note: If your signature references a compressed key,
19+
// the address you provide to verify must also come from a compressed key
1720
var signature string
18-
if signature, err = bitcoin.SignMessage(privateKey, "This is the example message"); err != nil {
21+
if signature, err = bitcoin.SignMessage(privateKey, "This is the example message", false); err != nil {
1922
log.Fatalf("error occurred: %s", err.Error())
2023
}
2124

examples/verify_signature/verify_signature.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func main() {
1919
}
2020

2121
// Get an address from private key
22+
// the compressed flag must match the flag provided during signing
2223
var address string
2324
address, err = bitcoin.GetAddressFromPrivateKey(rawKey, true)
2425
if err != nil {

sign.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
)
1212

1313
// SignMessage signs a string with the provided private key using Bitcoin Signed Message encoding
14-
//
14+
// sigRefCompressedKey bool determines whether the signature will reference a compressed or uncompresed key
1515
// Spec: https://docs.moneybutton.com/docs/bsv-message.html
16-
func SignMessage(privateKey string, message string) (string, error) {
16+
func SignMessage(privateKey string, message string, sigRefCompressedKey bool) (string, error) {
1717
if len(privateKey) == 0 {
1818
return "", errors.New("privateKey is empty")
1919
}
@@ -38,7 +38,7 @@ func SignMessage(privateKey string, message string) (string, error) {
3838

3939
// Sign
4040
var sigBytes []byte
41-
if sigBytes, err = bsvec.SignCompact(bsvec.S256(), ecdsaPrivateKey, messageHash, false); err != nil {
41+
if sigBytes, err = bsvec.SignCompact(bsvec.S256(), ecdsaPrivateKey, messageHash, sigRefCompressedKey); err != nil {
4242
return "", err
4343
}
4444

sign_test.go

+40-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,43 @@ import (
55
"testing"
66
)
77

8+
func TestSigningCompression(t *testing.T) {
9+
testKey := "0499f8239bfe10eb0f5e53d543635a423c96529dd85fa4bad42049a0b435ebdd"
10+
testData := "test message"
11+
12+
// Test sign uncompressed
13+
address, err := GetAddressFromPrivateKeyString(testKey, false)
14+
if err != nil {
15+
t.Errorf("Get address err %s", err)
16+
}
17+
sig, err := SignMessage(testKey, testData, false)
18+
if err != nil {
19+
t.Errorf("Failed to sign uncompressed %s", err)
20+
}
21+
22+
err = VerifyMessage(address, sig, testData)
23+
24+
if err != nil {
25+
t.Errorf("Failed to validate uncompressed %s", err)
26+
}
27+
28+
// Test sign compressed
29+
address, err = GetAddressFromPrivateKeyString(testKey, true)
30+
if err != nil {
31+
t.Errorf("Get address err %s", err)
32+
}
33+
sig, err = SignMessage(testKey, testData, true)
34+
if err != nil {
35+
t.Errorf("Failed to sign compressed %s", err)
36+
}
37+
38+
err = VerifyMessage(address, sig, testData)
39+
40+
if err != nil {
41+
t.Errorf("Failed to validate compressed %s", err)
42+
}
43+
}
44+
845
// TestSignMessage will test the method SignMessage()
946
func TestSignMessage(t *testing.T) {
1047

@@ -87,7 +124,7 @@ func TestSignMessage(t *testing.T) {
87124

88125
// Run tests
89126
for idx, test := range tests {
90-
if signature, err := SignMessage(test.inputKey, test.inputMessage); err != nil && !test.expectedError {
127+
if signature, err := SignMessage(test.inputKey, test.inputMessage, false); err != nil && !test.expectedError {
91128
t.Fatalf("%d %s Failed: [%s] [%s] inputted and error not expected but got: %s", idx, t.Name(), test.inputKey, test.inputMessage, err.Error())
92129
} else if err == nil && test.expectedError {
93130
t.Fatalf("%d %s Failed: [%s] [%s] inputted and error was expected", idx, t.Name(), test.inputKey, test.inputMessage)
@@ -99,7 +136,7 @@ func TestSignMessage(t *testing.T) {
99136

100137
// ExampleSignMessage example using SignMessage()
101138
func ExampleSignMessage() {
102-
signature, err := SignMessage("ef0b8bad0be285099534277fde328f8f19b3be9cadcd4c08e6ac0b5f863745ac", "This is a test message")
139+
signature, err := SignMessage("ef0b8bad0be285099534277fde328f8f19b3be9cadcd4c08e6ac0b5f863745ac", "This is a test message", false)
103140
if err != nil {
104141
fmt.Printf("error occurred: %s", err.Error())
105142
return
@@ -112,6 +149,6 @@ func ExampleSignMessage() {
112149
func BenchmarkSignMessage(b *testing.B) {
113150
key, _ := CreatePrivateKeyString()
114151
for i := 0; i < b.N; i++ {
115-
_, _ = SignMessage(key, "This is a test message")
152+
_, _ = SignMessage(key, "This is a test message", false)
116153
}
117154
}

verify.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ func PubKeyFromSignature(sig, data string) (pubKey *bsvec.PublicKey, wasCompress
4141
}
4242

4343
// VerifyMessage verifies a string and address against the provided
44-
// signature and assumes Bitcoin Signed Message encoding
44+
// signature and assumes Bitcoin Signed Message encoding.
45+
// The key referenced by the signature must relate to the address provided.
46+
// Do not provide an address from an uncompressed key along with
47+
// a signature from a compressed key
4548
//
4649
// Error will occur if verify fails or verification is not successful (no bool)
4750
// Spec: https://docs.moneybutton.com/docs/bsv-message.html

0 commit comments

Comments
 (0)