Export NewExtendedKey

Allow users to create custom ExtendedKeys by exporting the newExtendedKey function.
The comment is updated to reflect that this function is only intended to be used
by custom applications.
This commit is contained in:
Chris Pacia 2017-06-05 14:41:43 -04:00
parent fac8a9a582
commit 61ec18f9c8

View file

@ -116,10 +116,12 @@ type ExtendedKey struct {
isPrivate bool isPrivate bool
} }
// newExtendedKey returns a new instance of an extended key with the given // NewExtendedKey returns a new instance of an extended key with the given
// fields. No error checking is performed here as it's only intended to be a // fields. No error checking is performed here as it's only intended to be a
// convenience method used to create a populated struct. // convenience method used to create a populated struct. This function should
func newExtendedKey(version, key, chainCode, parentFP []byte, depth uint8, // only by used by applications that need to create custom ExtendedKeys. All
// other applications should just use NewMaster, Child, or Neuter.
func NewExtendedKey(version, key, chainCode, parentFP []byte, depth uint8,
childNum uint32, isPrivate bool) *ExtendedKey { childNum uint32, isPrivate bool) *ExtendedKey {
// NOTE: The pubKey field is intentionally left nil so it is only // NOTE: The pubKey field is intentionally left nil so it is only
@ -315,7 +317,7 @@ func (k *ExtendedKey) Child(i uint32) (*ExtendedKey, error) {
// The fingerprint of the parent for the derived child is the first 4 // The fingerprint of the parent for the derived child is the first 4
// bytes of the RIPEMD160(SHA256(parentPubKey)). // bytes of the RIPEMD160(SHA256(parentPubKey)).
parentFP := btcutil.Hash160(k.pubKeyBytes())[:4] parentFP := btcutil.Hash160(k.pubKeyBytes())[:4]
return newExtendedKey(k.version, childKey, childChainCode, parentFP, return NewExtendedKey(k.version, childKey, childChainCode, parentFP,
k.depth+1, i, isPrivate), nil k.depth+1, i, isPrivate), nil
} }
@ -343,7 +345,7 @@ func (k *ExtendedKey) Neuter() (*ExtendedKey, error) {
// key will simply be the pubkey of the current extended private key. // key will simply be the pubkey of the current extended private key.
// //
// This is the function N((k,c)) -> (K, c) from [BIP32]. // This is the function N((k,c)) -> (K, c) from [BIP32].
return newExtendedKey(version, k.pubKeyBytes(), k.chainCode, k.parentFP, return NewExtendedKey(version, k.pubKeyBytes(), k.chainCode, k.parentFP,
k.depth, k.childNum, false), nil k.depth, k.childNum, false), nil
} }
@ -487,7 +489,7 @@ func NewMaster(seed []byte, net *chaincfg.Params) (*ExtendedKey, error) {
} }
parentFP := []byte{0x00, 0x00, 0x00, 0x00} parentFP := []byte{0x00, 0x00, 0x00, 0x00}
return newExtendedKey(net.HDPrivateKeyID[:], secretKey, chainCode, return NewExtendedKey(net.HDPrivateKeyID[:], secretKey, chainCode,
parentFP, 0, 0, true), nil parentFP, 0, 0, true), nil
} }
@ -541,7 +543,7 @@ func NewKeyFromString(key string) (*ExtendedKey, error) {
} }
} }
return newExtendedKey(version, keyData, chainCode, parentFP, depth, return NewExtendedKey(version, keyData, chainCode, parentFP, depth,
childNum, isPrivate), nil childNum, isPrivate), nil
} }