Skip to content

Commit

Permalink
Added tests and examples for sign
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Sep 30, 2020
1 parent 97c1807 commit 92758cf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
5 changes: 5 additions & 0 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func TestValidA58(t *testing.T) {
}{
{"1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi2", true, false},
{"1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi", false, false},
{"1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi", false, true},
{"1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi", false, true},
{"1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi", false, true},
{"1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi", false, true},
{"1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi", false, true},
{"1KCEAmV", false, false},
{"", false, false},
{"0", false, true},
Expand Down
4 changes: 4 additions & 0 deletions sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package bitcoin

import (
"encoding/base64"
"errors"

"github.com/bitcoinsv/bsvd/bsvec"
"github.com/bitcoinsv/bsvd/chaincfg/chainhash"
)

// SignMessage signs a string with the provided private key using Bitcoin Signed Message encoding
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
Expand Down
53 changes: 45 additions & 8 deletions sign_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,59 @@
package bitcoin

import (
"fmt"
"testing"
)

// Identity Private Key
const privateKey = "E83385AF76B2B1997326B567461FB73DD9C27EAB9E1E86D26779F4650C5F2B75"

// TestSignMessage will test the method SignMessage()
func TestSignMessage(t *testing.T) {

// privateKey string, message string, compress bool
sig, err := SignMessage(privateKey, "Testing!")
t.Parallel()

// Create the list of tests
var tests = []struct {
inputKey string
inputMessage string
expectedSignature string
expectedError bool
}{
{"ef0b8bad0be285099534277fde328f8f19b3be9cadcd4c08e6ac0b5f863745ac", "This is a test message", "H+zZagsyz7ioC/ZOa5EwsaKice0vs2BvZ0ljgkFHxD3vGsMlGeD4sXHEcfbI4h8lP29VitSBdf4A+nHXih7svf4=", false},
{"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},
{"", "This is a test message", "", true},
{"0", "This is a test message", "", true},
{"0000000", "This is a test message", "", true},
{"c7726663147afd1add392d129086e57c0b", "This is a test message", "H6N+iPf23i2YkLsNzF/yyeBm9eSYBoY/HFV1Md1F0ElWBXW5E5mkdRtgjoRuq0yNb1CCFNWWlkn2gZknFJNUFJ8=", false},
}

// Run tests
for _, test := range tests {
if signature, err := SignMessage(test.inputKey, test.inputMessage); err != nil && !test.expectedError {
t.Errorf("%s Failed: [%s] [%s] inputted and error not expected but got: %s", t.Name(), test.inputKey, test.inputMessage, err.Error())
} else if err == nil && test.expectedError {
t.Errorf("%s Failed: [%s] [%s] inputted and error was expected", t.Name(), test.inputKey, test.inputMessage)
} else if signature != test.expectedSignature {
t.Errorf("%s Failed: [%s] [%s] inputted [%s] expected but got: %s", t.Name(), test.inputKey, test.inputMessage, test.expectedSignature, signature)
}
}
}

// ExampleSignMessage example using SignMessage()
func ExampleSignMessage() {
signature, err := SignMessage("ef0b8bad0be285099534277fde328f8f19b3be9cadcd4c08e6ac0b5f863745ac", "This is a test message")
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("signature created: %s", signature)
// Output:signature created: H+zZagsyz7ioC/ZOa5EwsaKice0vs2BvZ0ljgkFHxD3vGsMlGeD4sXHEcfbI4h8lP29VitSBdf4A+nHXih7svf4=
}

if sig != "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=" {
t.Error("Failed to generate expected signature", sig)
// BenchmarkSignMessage benchmarks the method SignMessage()
func BenchmarkSignMessage(b *testing.B) {
key, _ := CreatePrivateKeyString()
for i := 0; i < b.N; i++ {
_, _ = SignMessage(key, "This is a test message")
}
}

0 comments on commit 92758cf

Please sign in to comment.