e6c5ca2a6a
This commit adds a new sub-package named hdkeychain which can be used to derive hierarchical deterministic key chains which form the foundation of hd wallets. - Support for private and public extended keys - Convenient cryptographically secure seed generation - Simple creation of master nodes - Support for multi-layer derivation - Easy serialization and deserialization for both private and public extended keys - Support for custom networks by registering them with btcnet - Obtaining the underlying EC pubkeys, EC privkeys, and associated bitcoin addresses ties in seamlessly with existing btcec and btcutil types which provide powerful tools for working with them to do things like sign transactions and generate payment scripts - Makes use of the btcec package which is highly optimized for secp256k1 - Code examples including: - Generating a cryptographically secure random seed and deriving a master node from it - Default HD wallet layout as described by BIP0032 - Audits use case as described by BIP0032 - Comprehensive test coverage including the BIP0032 test vectors - Benchmarks
84 lines
2.2 KiB
Go
84 lines
2.2 KiB
Go
// Copyright (c) 2014 Conformal Systems LLC.
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package hdkeychain_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/conformal/btcutil/hdkeychain"
|
|
)
|
|
|
|
// bip0032MasterPriv1 is the master private extended key from the first set of
|
|
// test vectors in BIP0032.
|
|
const bip0032MasterPriv1 = "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbP" +
|
|
"y6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi"
|
|
|
|
// BenchmarkDeriveHardened benchmarks how long it takes to derive a hardened
|
|
// child from a master private extended key.
|
|
func BenchmarkDeriveHardened(b *testing.B) {
|
|
b.StopTimer()
|
|
masterKey, err := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
|
|
if err != nil {
|
|
b.Errorf("Failed to decode master seed: %v", err)
|
|
}
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
masterKey.Child(hdkeychain.HardenedKeyStart)
|
|
}
|
|
}
|
|
|
|
// BenchmarkDeriveNormal benchmarks how long it takes to derive a normal
|
|
// (non-hardened) child from a master private extended key.
|
|
func BenchmarkDeriveNormal(b *testing.B) {
|
|
b.StopTimer()
|
|
masterKey, err := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
|
|
if err != nil {
|
|
b.Errorf("Failed to decode master seed: %v", err)
|
|
}
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
masterKey.Child(0)
|
|
}
|
|
}
|
|
|
|
// BenchmarkPrivToPub benchmarks how long it takes to convert a private extended
|
|
// key to a public extended key.
|
|
func BenchmarkPrivToPub(b *testing.B) {
|
|
b.StopTimer()
|
|
masterKey, err := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
|
|
if err != nil {
|
|
b.Errorf("Failed to decode master seed: %v", err)
|
|
}
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
masterKey.Neuter()
|
|
}
|
|
}
|
|
|
|
// BenchmarkDeserialize benchmarks how long it takes to deserialize a private
|
|
// extended key.
|
|
func BenchmarkDeserialize(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
hdkeychain.NewKeyFromString(bip0032MasterPriv1)
|
|
}
|
|
}
|
|
|
|
// BenchmarkSerialize benchmarks how long it takes to serialize a private
|
|
// extended key.
|
|
func BenchmarkSerialize(b *testing.B) {
|
|
b.StopTimer()
|
|
masterKey, err := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
|
|
if err != nil {
|
|
b.Errorf("Failed to decode master seed: %v", err)
|
|
}
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
masterKey.String()
|
|
}
|
|
}
|