Skip to content

Commit 016946f

Browse files
committed
Added PrivateAndPublicKeys() method
1 parent 2744ec1 commit 016946f

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

private_key.go

+20
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,23 @@ func CreatePrivateKeyString() (string, error) {
4141

4242
return hex.EncodeToString(privateKey.Serialize()), nil
4343
}
44+
45+
// PrivateAndPublicKeys will return both the private and public key in one method
46+
// Expects a hex encoded privateKey
47+
func PrivateAndPublicKeys(privateKey string) (*bsvec.PrivateKey, *bsvec.PublicKey, error) {
48+
49+
// No key?
50+
if len(privateKey) == 0 {
51+
return nil, nil, errors.New("missing privateKey")
52+
}
53+
54+
// Decode the private key into bytes
55+
privateKeyBytes, err := hex.DecodeString(privateKey)
56+
if err != nil {
57+
return nil, nil, err
58+
}
59+
60+
// Get the public and private key from the bytes
61+
rawKey, publicKey := bsvec.PrivKeyFromBytes(bsvec.S256(), privateKeyBytes)
62+
return rawKey, publicKey, nil
63+
}

private_key_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,59 @@ func BenchmarkPrivateKeyFromString(b *testing.B) {
126126
_, _ = PrivateKeyFromString(key)
127127
}
128128
}
129+
130+
// TestPrivateAndPublicKeys will test the method PrivateAndPublicKeys()
131+
func TestPrivateAndPublicKeys(t *testing.T) {
132+
133+
t.Parallel()
134+
135+
// Create the list of tests
136+
var tests = []struct {
137+
input string
138+
expectedPrivateKey string
139+
expectedNil bool
140+
expectedError bool
141+
}{
142+
{"", "", true, true},
143+
{"0", "", true, true},
144+
{"00000", "", true, true},
145+
{"0-0-0-0-0", "", true, true},
146+
{"z4035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abz", "", true, true},
147+
{"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", "54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", false, false},
148+
}
149+
150+
// Run tests
151+
for _, test := range tests {
152+
if privateKey, publicKey, err := PrivateAndPublicKeys(test.input); err != nil && !test.expectedError {
153+
t.Errorf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.input, err.Error())
154+
} else if err == nil && test.expectedError {
155+
t.Errorf("%s Failed: [%s] inputted and error was expected", t.Name(), test.input)
156+
} else if (privateKey == nil || publicKey == nil) && !test.expectedNil {
157+
t.Errorf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.input)
158+
} else if (privateKey != nil || publicKey != nil) && test.expectedNil {
159+
t.Errorf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.input)
160+
} else if privateKey != nil && hex.EncodeToString(privateKey.Serialize()) != test.expectedPrivateKey {
161+
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()))
162+
}
163+
}
164+
}
165+
166+
// ExamplePrivateAndPublicKeys example using PrivateAndPublicKeys()
167+
func ExamplePrivateAndPublicKeys() {
168+
privateKey, publicKey, err := PrivateAndPublicKeys("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd")
169+
if err != nil {
170+
fmt.Printf("error occurred: %s", err.Error())
171+
return
172+
}
173+
fmt.Printf("private key: %s public key: %s", hex.EncodeToString(privateKey.Serialize()), hex.EncodeToString(publicKey.SerializeCompressed()))
174+
175+
// Output:private key: 54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd public key: 031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f
176+
}
177+
178+
// BenchmarkPrivateAndPublicKeys benchmarks the method PrivateAndPublicKeys()
179+
func BenchmarkPrivateAndPublicKeys(b *testing.B) {
180+
key, _ := CreatePrivateKeyString()
181+
for i := 0; i < b.N; i++ {
182+
_, _, _ = PrivateAndPublicKeys(key)
183+
}
184+
}

0 commit comments

Comments
 (0)