From 016946f1acaabc865c5d94466e81f489361e9742 Mon Sep 17 00:00:00 2001 From: mrz1836 Date: Thu, 1 Oct 2020 11:35:22 -0400 Subject: [PATCH] Added PrivateAndPublicKeys() method --- private_key.go | 20 ++++++++++++++++ private_key_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/private_key.go b/private_key.go index e049104..bb59c4b 100644 --- a/private_key.go +++ b/private_key.go @@ -41,3 +41,23 @@ func CreatePrivateKeyString() (string, error) { return hex.EncodeToString(privateKey.Serialize()), nil } + +// PrivateAndPublicKeys will return both the private and public key in one method +// Expects a hex encoded privateKey +func PrivateAndPublicKeys(privateKey string) (*bsvec.PrivateKey, *bsvec.PublicKey, error) { + + // No key? + if len(privateKey) == 0 { + return nil, nil, errors.New("missing privateKey") + } + + // Decode the private key into bytes + privateKeyBytes, err := hex.DecodeString(privateKey) + if err != nil { + return nil, nil, err + } + + // Get the public and private key from the bytes + rawKey, publicKey := bsvec.PrivKeyFromBytes(bsvec.S256(), privateKeyBytes) + return rawKey, publicKey, nil +} diff --git a/private_key_test.go b/private_key_test.go index 1286ca5..cd77b87 100644 --- a/private_key_test.go +++ b/private_key_test.go @@ -126,3 +126,59 @@ func BenchmarkPrivateKeyFromString(b *testing.B) { _, _ = PrivateKeyFromString(key) } } + +// TestPrivateAndPublicKeys will test the method PrivateAndPublicKeys() +func TestPrivateAndPublicKeys(t *testing.T) { + + t.Parallel() + + // Create the list of tests + var tests = []struct { + input string + expectedPrivateKey string + expectedNil bool + expectedError bool + }{ + {"", "", true, true}, + {"0", "", true, true}, + {"00000", "", true, true}, + {"0-0-0-0-0", "", true, true}, + {"z4035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abz", "", true, true}, + {"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", "54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", false, false}, + } + + // Run tests + for _, test := range tests { + if privateKey, publicKey, err := PrivateAndPublicKeys(test.input); err != nil && !test.expectedError { + t.Errorf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.input, err.Error()) + } else if err == nil && test.expectedError { + t.Errorf("%s Failed: [%s] inputted and error was expected", t.Name(), test.input) + } else if (privateKey == nil || publicKey == nil) && !test.expectedNil { + t.Errorf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.input) + } else if (privateKey != nil || publicKey != nil) && test.expectedNil { + t.Errorf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.input) + } else if privateKey != nil && hex.EncodeToString(privateKey.Serialize()) != test.expectedPrivateKey { + t.Errorf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedPrivateKey, hex.EncodeToString(privateKey.Serialize())) + } + } +} + +// ExamplePrivateAndPublicKeys example using PrivateAndPublicKeys() +func ExamplePrivateAndPublicKeys() { + privateKey, publicKey, err := PrivateAndPublicKeys("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd") + if err != nil { + fmt.Printf("error occurred: %s", err.Error()) + return + } + fmt.Printf("private key: %s public key: %s", hex.EncodeToString(privateKey.Serialize()), hex.EncodeToString(publicKey.SerializeCompressed())) + + // Output:private key: 54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd public key: 031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f +} + +// BenchmarkPrivateAndPublicKeys benchmarks the method PrivateAndPublicKeys() +func BenchmarkPrivateAndPublicKeys(b *testing.B) { + key, _ := CreatePrivateKeyString() + for i := 0; i < b.N; i++ { + _, _, _ = PrivateAndPublicKeys(key) + } +}