From 92758cf8fdb6dc2c6a12521424100aa9d4b70a11 Mon Sep 17 00:00:00 2001 From: mrz1836 Date: Wed, 30 Sep 2020 16:21:01 -0400 Subject: [PATCH] Added tests and examples for sign --- address_test.go | 5 +++++ sign.go | 4 ++++ sign_test.go | 53 +++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/address_test.go b/address_test.go index 3ec158a..da6351b 100644 --- a/address_test.go +++ b/address_test.go @@ -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}, diff --git a/sign.go b/sign.go index 85dfce4..1ef7648 100644 --- a/sign.go +++ b/sign.go @@ -2,6 +2,7 @@ package bitcoin import ( "encoding/base64" + "errors" "github.com/bitcoinsv/bsvd/bsvec" "github.com/bitcoinsv/bsvd/chaincfg/chainhash" @@ -9,6 +10,9 @@ import ( // 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 diff --git a/sign_test.go b/sign_test.go index ef3c3c7..f33e9e8 100644 --- a/sign_test.go +++ b/sign_test.go @@ -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") } }