Skip to content

Commit

Permalink
Added more verify message tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Sep 30, 2020
1 parent 92758cf commit dbd5bc3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestSignMessage(t *testing.T) {
{"93596babb564cbbdc84f2370c710b9bcc94333495b60af719b5fcf9ba00ba82c", "This is a test message", "IIuDw09ffPgEDuxEw5yHVp1+mi4QpuhAwLyQdpMTfsHCOkMqTKXuP7dSNWMEJqZsiQ8eKMDRvf2wZ4e5bxcu4O0=", false},
{"50381cf8f52936faae4a05a073a03d688a9fa206d005e87a39da436c75476d78", "This is a test message", "ILBmbjCY2Z7eSXGXZoBI3x2ZRaYUYOGtEaDjXetaY+zNDtMOvagsOGEHnVT3f5kXlEbuvmPydHqLnyvZP3cDOWk=", false},
{"c7726663147afd1add392d129086e57c0b05aa66a6ded564433c04bd55741434", "This is a test message", "IOI207QUnTLr2Ll+s4kUxNgLgorkc/Z5Pc+XNvUBYLy2TxaU6oHEJ2TTJ1mZVrtUyHm6e315v1tIjeosW3Odfqw=", false},
{"c7726663147afd1add392d129086e57c0b05aa66a6ded564433c04bd55741434", "1", "IMcRFG1VNN9TDGXpCU+9CqKLNOuhwQiXI5hZpkTOuYHKBDOWayNuAABofYLqUHYTMiMf9mYFQ0sPgFJZz3F7ELQ=", false},
{"", "This is a test message", "", true},
{"0", "This is a test message", "", true},
{"0000000", "This is a test message", "", true},
Expand Down
12 changes: 12 additions & 0 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bitcoin
import (
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"math/big"

Expand All @@ -18,6 +19,11 @@ const (

// VerifyMessage verifies a string and address against the provided signature and assumes Bitcoin Signed Message encoding
func VerifyMessage(address, signature, data string) error {
if len(address) == 0 {
return errors.New("address is missing")
} else if len(signature) == 0 {
return errors.New("signature is missing")
}
addresses, err := sigMessageToAddress(signature, data)
if err != nil {
return err
Expand Down Expand Up @@ -56,6 +62,12 @@ func messageHash(message, header string) ([]byte, error) {

// parseSignature will parse the given signature
func parseSignature(signature string) (sig secp256k1.Signature, recID int, err error) {
// todo: is this 64 or 65? is it always the same?
// panic was occurring: sig.R.SetBytes(sigRaw[1 : 1+32])
if len(signature) < 64 {
err = fmt.Errorf("signature is less than %d characters", 64)
return
}
var sigRaw []byte
if sigRaw, err = base64.StdEncoding.DecodeString(signature); err != nil {
return
Expand Down
42 changes: 27 additions & 15 deletions verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,38 @@ import (
// TestVerifyMessage will test the method VerifyMessage()
func TestVerifyMessage(t *testing.T) {

var sig = "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8="
var address = "1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ"
var data = "Testing!"
t.Parallel()

// Create the list of tests
var tests = []struct {
inputAddress string
inputSignature string
inputData string
expectedError bool
}{
{"1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=", "Testing!", false},
{"", "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=", "Testing!", true},
{"1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", "", "Testing!", true},
{"1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=", "", true},
{"0", "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=", "Testing!", true},
{"1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", "GBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4naZ=", "Testing!", true},
{"1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", "GBD=", "Testing!", true},
{"1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", "GBse5w0f839t8wej8f2D=", "Testing!", true},
}

if err := VerifyMessage(address, sig, data); err != nil {
t.Fatalf("failed to verify message: %s", err.Error())
// Run tests
for _, test := range tests {
if err := VerifyMessage(test.inputAddress, test.inputSignature, test.inputData); err != nil && !test.expectedError {
t.Errorf("%s Failed: [%s] [%s] [%s] inputted and error not expected but got: %s", t.Name(), test.inputAddress, test.inputSignature, test.inputData, err.Error())
} else if err == nil && test.expectedError {
t.Errorf("%s Failed: [%s] [%s] [%s] inputted and error was expected", t.Name(), test.inputAddress, test.inputSignature, test.inputData)
}
}
}

// ExampleVerifyMessage example using VerifyMessage()
func ExampleVerifyMessage() {
var sig = "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8="
var address = "1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ"
var data = "Testing!"

if err := VerifyMessage(address, sig, data); err != nil {
if err := VerifyMessage("1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=", "Testing!"); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
Expand All @@ -33,11 +49,7 @@ func ExampleVerifyMessage() {

// BenchmarkVerifyMessage benchmarks the method VerifyMessage()
func BenchmarkVerifyMessage(b *testing.B) {
var sig = "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8="
var address = "1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ"
var data = "Testing!"

for i := 0; i < b.N; i++ {
_ = VerifyMessage(address, sig, data)
_ = VerifyMessage("1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ", "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=", "Testing!")
}
}

0 comments on commit dbd5bc3

Please sign in to comment.