diff --git a/README.md b/README.md index f69e4c8..cf17cf5 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ View the generated [documentation](https://pkg.go.dev/github.com/bitcoinschema/g - [Private key (string) to Address (string)](address.go) - [Address from Private key (bsvec.PrivateKey)](address.go) - [Create Private Key](private_key.go) +- [Create PubKey From Private Key](pubkey.go)
diff --git a/examples/create_pubkey/create_pubkey.go b/examples/create_pubkey/create_pubkey.go new file mode 100644 index 0000000..622e8d7 --- /dev/null +++ b/examples/create_pubkey/create_pubkey.go @@ -0,0 +1,25 @@ +package main + +import ( + "log" + + "github.com/bitcoinschema/go-bitcoin" +) + +func main() { + + // Start with a private key + privateKey, err := bitcoin.CreatePrivateKeyString() + if err != nil { + log.Fatalf("error occurred: %s", err.Error()) + } + + // Create a pubkey + var pubKey string + if pubKey, err = bitcoin.PubKeyFromPrivateKey(privateKey); err != nil { + log.Fatalf("error occurred: %s", err.Error()) + } + + // Success! + log.Printf("created pubkey: %s from private key: %s", pubKey, privateKey) +} diff --git a/pubkey.go b/pubkey.go new file mode 100644 index 0000000..9231522 --- /dev/null +++ b/pubkey.go @@ -0,0 +1,15 @@ +package bitcoin + +import ( + "encoding/hex" +) + +// PubKeyFromPrivateKey will derive a pubKey (hex encoded) from a given private key +func PubKeyFromPrivateKey(privateKey string) (string, error) { + rawKey, err := PrivateKeyFromString(privateKey) + if err != nil { + return "", err + } + + return hex.EncodeToString(rawKey.PubKey().SerializeCompressed()), nil +} diff --git a/pubkey_test.go b/pubkey_test.go new file mode 100644 index 0000000..b4b71e5 --- /dev/null +++ b/pubkey_test.go @@ -0,0 +1,52 @@ +package bitcoin + +import ( + "fmt" + "testing" +) + +// TestPubKeyFromPrivateKey will test the method PubKeyFromPrivateKey() +func TestPubKeyFromPrivateKey(t *testing.T) { + t.Parallel() + + // Create the list of tests + var tests = []struct { + inputKey string + expectedPubKey string + expectedError bool + }{ + {"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", "031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f", false}, + {"0", "", true}, + {"", "", true}, + } + + // Run tests + for _, test := range tests { + if pubKey, err := PubKeyFromPrivateKey(test.inputKey); err != nil && !test.expectedError { + t.Errorf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.inputKey, err.Error()) + } else if err == nil && test.expectedError { + t.Errorf("%s Failed: [%s] inputted and error was expected", t.Name(), test.inputKey) + } else if pubKey != test.expectedPubKey { + t.Errorf("%s Failed: [%s] inputted and [%s] expected, but got: %s", t.Name(), test.inputKey, test.expectedPubKey, pubKey) + } + } +} + +// ExamplePubKeyFromPrivateKey example using PubKeyFromPrivateKey() +func ExamplePubKeyFromPrivateKey() { + pubKey, err := PubKeyFromPrivateKey("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd") + if err != nil { + fmt.Printf("error occurred: %s", err.Error()) + return + } + fmt.Printf("pubkey generated: %s", pubKey) + // Output:pubkey generated: 031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f +} + +// BenchmarkPubKeyFromPrivateKey benchmarks the method PubKeyFromPrivateKey() +func BenchmarkPubKeyFromPrivateKey(b *testing.B) { + key, _ := CreatePrivateKeyString() + for i := 0; i < b.N; i++ { + _, _ = PubKeyFromPrivateKey(key) + } +}