Skip to content

Commit

Permalink
Pubkey from String method
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 1, 2020
1 parent bc3e04b commit 15f901c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package bitcoin

import (
"encoding/hex"
"errors"

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

// PubKeyFromPrivateKey will derive a pubKey (hex encoded) from a given private key
Expand All @@ -13,3 +16,21 @@ func PubKeyFromPrivateKey(privateKey string) (string, error) {

return hex.EncodeToString(rawKey.PubKey().SerializeCompressed()), nil
}

// PubKeyFromString will convert a pubKey (string) into a pubkey (*bsvec.PublicKey)
func PubKeyFromString(pubKey string) (*bsvec.PublicKey, error) {

// Invalid pubKey
if len(pubKey) == 0 {
return nil, errors.New("missing pubkey")
}

// Decode from hex string
decoded, err := hex.DecodeString(pubKey)
if err != nil {
return nil, err
}

// Parse into a pubKey
return bsvec.ParsePubKey(decoded, bsvec.S256())
}
53 changes: 53 additions & 0 deletions pubkey_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bitcoin

import (
"encoding/hex"
"fmt"
"testing"
)
Expand Down Expand Up @@ -50,3 +51,55 @@ func BenchmarkPubKeyFromPrivateKey(b *testing.B) {
_, _ = PubKeyFromPrivateKey(key)
}
}

// TestPubKeyFromString will test the method PubKeyFromString()
func TestPubKeyFromString(t *testing.T) {

t.Parallel()

// Create the list of tests
var tests = []struct {
inputKey string
expectedPubKey string
expectedNil bool
expectedError bool
}{
{"", "", true, true},
{"0", "", true, true},
{"00000", "", true, true},
{"031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f", "031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f", false, false},
}

// Run tests
for _, test := range tests {
if pubKey, err := PubKeyFromString(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 != nil && test.expectedNil {
t.Errorf("%s Failed: [%s] inputted and nil was expected", t.Name(), test.inputKey)
} else if pubKey == nil && !test.expectedNil {
t.Errorf("%s Failed: [%s] inputted and nil was NOT expected", t.Name(), test.inputKey)
} else if pubKey != nil && hex.EncodeToString(pubKey.SerializeCompressed()) != test.expectedPubKey {
t.Errorf("%s Failed: [%s] inputted and [%s] expected, but got: %s", t.Name(), test.inputKey, test.expectedPubKey, hex.EncodeToString(pubKey.SerializeCompressed()))
}
}
}

// ExamplePubKeyFromString example using PubKeyFromString()
func ExamplePubKeyFromString() {
pubKey, err := PubKeyFromString("031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("pubkey from string: %s", hex.EncodeToString(pubKey.SerializeCompressed()))
// Output:pubkey from string: 031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f
}

// BenchmarkPubKeyFromString benchmarks the method PubKeyFromString()
func BenchmarkPubKeyFromString(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = PubKeyFromString("031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f")
}
}

0 comments on commit 15f901c

Please sign in to comment.