Documentation ¶
Overview ¶
package extended implements hierarchical deterministic key generation, i.e. extended keys.
Based on btcsuite's hdkeychain, copyright (c) 2014-2016 The btcsuite developers. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Index ¶
- Constants
- Variables
- func GenerateSeed(length uint8) ([]byte, error)
- func PrivateKeyVer() [4]byte
- func PublicKeyVer() [4]byte
- func SeedToHex(s []byte) string
- type Key
- func (k *Key) Bytes() []byte
- func (k *Key) Child(i uint32) (*Key, error)
- func (k *Key) Depth() uint8
- func (k *Key) ECPrivKey() (sign.PrivateKey, error)
- func (k *Key) ECPubKey() (sign.PublicKey, error)
- func (k *Key) Fingerprint() sign.Fingerprint
- func (k *Key) IsPrivate() bool
- func (k *Key) MaybeFingerprint() (sign.Fingerprint, error)
- func (k *Key) Neuter() (*Key, error)
- func (k *Key) ParentFingerprint() sign.Fingerprint
- func (k *Key) String() string
- func (k *Key) Zero()
- type KeyPath
Constants ¶
const ( // RecommendedSeedLen is the recommended length in bytes for a seed // to a master node. RecommendedSeedLen = 64 // 512 bits // HardenedKeyStart is the index at which a hardended key starts. Each // extended key has 2^31 normal child keys and 2^31 hardned child keys. // Thus the range for normal child keys is [0, 2^31 - 1] and the range // for hardened child keys is [2^31, 2^32 - 1]. HardenedKeyStart = 0x80000000 // 2^31 // MinSeedBytes is the minimum number of bytes allowed for a seed to // a master node. MinSeedBytes = 16 // 128 bits // MaxSeedBytes is the maximum number of bytes allowed for a seed to // a master node. MaxSeedBytes = 64 // 512 bits )
Variables ¶
var ( // ErrDeriveHardFromPublic describes an error in which the caller // attempted to derive a hardened extended key from a public key. ErrDeriveHardFromPublic = errors.New("cannot derive a hardened key " + "from a public key") // ErrDeriveBeyondMaxDepth describes an error in which the caller // has attempted to derive more than 255 keys from a root key. ErrDeriveBeyondMaxDepth = errors.New("cannot derive a key with more than " + "255 indices in its path") // ErrNotPrivExtKey describes an error in which the caller attempted // to extract a private key from a public extended key. ErrNotPrivExtKey = errors.New("unable to create private keys from a " + "public extended key") // ErrInvalidChild describes an error in which the child at a specific // index is invalid due to the derived key falling outside of the valid // range for secp256k1 private keys. This error indicates the caller // should simply ignore the invalid child extended key at this index and // increment to the next index. ErrInvalidChild = errors.New("the extended key at this index is invalid") // ErrUnusableSeed describes an error in which the provided seed is not // usable due to the derived key falling outside of the valid range for // secp256k1 private keys. This error indicates the caller must choose // another seed. ErrUnusableSeed = errors.New("unusable seed") // ErrInvalidSeedLen describes an error in which the provided seed or // seed length is not in the allowed range. ErrInvalidSeedLen = fmt.Errorf("seed length must be between %d and %d "+ "bits", MinSeedBytes*8, MaxSeedBytes*8) // ErrInvalidKeyLen describes an error in which the provided serialized // key is not the expected length. ErrInvalidKeyLen = errors.New("the provided serialized extended key " + "length is invalid") )
Functions ¶
func GenerateSeed ¶
GenerateSeed returns a cryptographically secure random seed that can be used as the input for the NewMaster function to generate a new master node.
The length is in bytes and it must be between 16 and 64 (128 to 512 bits). The recommended length is 32 (256 bits) as defined by the RecommendedSeedLen constant.
func PrivateKeyVer ¶
func PrivateKeyVer() [4]byte
PrivateKeyVer returns private key version bytes. When base58-encoded, each private key starts with PPRV
func PublicKeyVer ¶
func PublicKeyVer() [4]byte
PublicKeyVer returns public key version bytes. When base58-encoded, each public key starts with PPUB
Types ¶
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key houses all the information needed to support a hierarchical deterministic extended key. See the package overview documentation for more details on how to use extended keys.
func NewKey ¶
func NewKey(version [4]byte, key, chainCode []byte, parentFP sign.Fingerprint, depth uint8, childNum uint32, isPrivate bool) *Key
NewKey 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 convenience method used to create a populated struct. This function should only by used by applications that need to create custom ExtendedKeys. All other applications should just use NewMaster, Child, or Neuter.
func NewKeyFromBytes ¶
func NewKeyFromString ¶
NewKeyFromString returns a new extended key instance from a base58-encoded extended key.
func NewMaster ¶
NewMaster creates a new master node for use in creating a hierarchical deterministic key chain. The seed must be between 128 and 512 bits and should be generated by a cryptographically secure random generation source.
NOTE: There is an extremely small chance (< 1 in 2^127) the provided seed will derive to an unusable secret key. The ErrUnusable error will be returned if this should occur, so the caller must check for it and generate a new seed accordingly.
func (*Key) Child ¶
Child returns a derived child extended key at the given index. When this extended key is a private extended key (as determined by the IsPrivate function), a private extended key will be derived. Otherwise, the derived extended key will be also be a public extended key.
When the index is greater to or equal than the HardenedKeyStart constant, the derived extended key will be a hardened extended key. It is only possible to derive a hardended extended key from a private extended key. Consequently, this function will return ErrDeriveHardFromPublic if a hardened child extended key is requested from a public extended key.
A hardened extended key is useful since, as previously mentioned, it requires a parent private extended key to derive. In other words, normal child extended public keys can be derived from a parent public extended key (no knowledge of the parent private key) whereas hardened extended keys may not be.
NOTE: There is an extremely small chance (< 1 in 2^127) the specific child index does not derive to a usable child. The ErrInvalidChild error will be returned if this should occur, and the caller is expected to ignore the invalid child and simply increment to the next index.
func (*Key) Depth ¶
Depth returns the current derivation level with respect to the root.
The root key has depth zero, and the field has a maximum of 255 due to how depth is serialized.
func (*Key) ECPrivKey ¶
func (k *Key) ECPrivKey() (sign.PrivateKey, error)
ECPrivKey converts the extended key to a private signature key and returns it. As you might imagine this is only possible if the extended key is a private extended key (as determined by the IsPrivate function). The ErrNotPrivExtKey error will be returned if this function is called on a public extended key.
func (*Key) Fingerprint ¶
func (k *Key) Fingerprint() sign.Fingerprint
Fingerprint returns this key's fingerprint. If it fails, it will panic. See MaybeFingerprint.
func (*Key) IsPrivate ¶
IsPrivate returns whether or not the extended key is a private extended key.
A private extended key can be used to derive both hardened and non-hardened child private and public extended keys. A public extended key can only be used to derive non-hardened child public extended keys.
func (*Key) MaybeFingerprint ¶
func (k *Key) MaybeFingerprint() (sign.Fingerprint, error)
MaybeFingerprint returns the fingerprint for this key, or an error if unsuccessful.
func (*Key) Neuter ¶
Neuter returns a new extended public key from this extended private key. The same extended key will be returned unaltered if it is already an extended public key.
As the name implies, an extended public key does not have access to the private key, so it is not capable of signing transactions or deriving child extended private keys. However, it is capable of deriving further child extended public keys.
func (*Key) ParentFingerprint ¶
func (k *Key) ParentFingerprint() sign.Fingerprint
ParentFingerprint returns a fingerprint of the parent extended key from which this one was derived.
func (*Key) Zero ¶
func (k *Key) Zero()
Zero manually clears all fields and bytes in the extended key. This can be used to explicitly clear key material from memory for enhanced security against memory scraping. This function only clears this particular key and not any children that have already been derived.