Skip to content

Commit

Permalink
Added basic pubkey generation from private key
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Sep 30, 2020
1 parent 3ce5059 commit e08b15a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


<details>
Expand Down
25 changes: 25 additions & 0 deletions examples/create_pubkey/create_pubkey.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions pubkey.go
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 52 additions & 0 deletions pubkey_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit e08b15a

Please sign in to comment.