Documentation ¶
Overview ¶
Package bip32 implements the bip32 spec https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
Index ¶
- Constants
- Variables
- func IsImpossibleChildError(err error) bool
- func NewError(err error) error
- func NewImpossibleChildError(err error, childNumber uint32) error
- type Error
- type Path
- type PathNode
- type PrivateKey
- func (k PrivateKey) ChildNumber() uint32
- func (k *PrivateKey) DeriveSubpath(nodes []PathNode) (*PrivateKey, error)
- func (k *PrivateKey) Fingerprint() []byte
- func (k *PrivateKey) Identifier() []byte
- func (k *PrivateKey) NewPrivateChildKey(childIdx uint32) (*PrivateKey, error)
- func (k *PrivateKey) NewPublicChildKey(childIdx uint32) (*PublicKey, error)
- func (k *PrivateKey) PublicKey() *PublicKey
- func (k *PrivateKey) Serialize() []byte
- func (k *PrivateKey) String() string
- type PublicKey
Constants ¶
const ( // FirstHardenedChild is the index of the firxt "hardened" child key as per the // bip32 spec FirstHardenedChild = uint32(0x80000000) )
Variables ¶
var ( // PrivateWalletVersion is the version flag for serialized private keys ("xpriv") PrivateWalletVersion = []byte{0x04, 0x88, 0xAD, 0xE4} // PublicWalletVersion is the version flag for serialized public keys ("xpub") PublicWalletVersion = []byte{0x04, 0x88, 0xB2, 0x1E} // ErrSerializedKeyWrongSize is returned when trying to deserialize a key that // has an incorrect length ErrSerializedKeyWrongSize = NewError(errors.New("Serialized keys should be exactly 82 bytes")) // ErrHardenedChildPublicKey is returned when trying to create a harded child // of the public key ErrHardenedChildPublicKey = NewError(errors.New("Can't create hardened child for public key")) // ErrInvalidChecksum is returned when deserializing a key with an incorrect checksum ErrInvalidChecksum = NewError(errors.New("Checksum doesn't match")) // ErrDerivedInvalidPrivateKey is returned when an invalid private key was derived ErrDerivedInvalidPrivateKey = NewError(errors.New("Derived invalid private key")) // ErrDerivedInvalidPublicKey is returned when an invalid public key was derived ErrDerivedInvalidPublicKey = NewError(errors.New("Derived invalid public key")) // ErrInvalidPrivateKeyVersion is returned when a deserializing a private key without the 'xprv' prefix ErrInvalidPrivateKeyVersion = NewError(errors.New("Invalid private key version")) // ErrInvalidPublicKeyVersion is returned when a deserializing a public key without the 'xpub' prefix ErrInvalidPublicKeyVersion = NewError(errors.New("Invalid public key version")) // ErrInvalidSeedLength is returned when generating a master key with an invalid number of seed bits ErrInvalidSeedLength = NewError(errors.New("Invalid master key seed length")) // ErrDeserializePrivateFromPublic attempted to deserialize a private key from an encoded public key ErrDeserializePrivateFromPublic = NewError(errors.New("Cannot deserialize a private key from a public key")) // ErrInvalidKeyVersion is returned if the key version is not 'xpub' or 'xprv' ErrInvalidKeyVersion = NewError(errors.New("Invalid key version")) // ErrInvalidFingerprint is returned if a deserialized key has an invalid fingerprint ErrInvalidFingerprint = NewError(errors.New("Invalid key fingerprint")) // ErrInvalidChildNumber is returned if a deserialized key has an invalid child number ErrInvalidChildNumber = NewError(errors.New("Invalid key child number")) // ErrInvalidPrivateKey is returned if a deserialized xprv key's private key is invalid ErrInvalidPrivateKey = NewError(errors.New("Invalid private key")) // ErrInvalidPublicKey is returned if a deserialized xpub key's public key is invalid ErrInvalidPublicKey = NewError(errors.New("Invalid public key")) // ErrMaxDepthReached maximum allowed depth (255) reached for child key ErrMaxDepthReached = NewError(errors.New("Maximum child depth reached")) )
var ( // ErrPathNoMaster HD wallet path does not start with m ErrPathNoMaster = errors.New("Path must start with m") // ErrPathChildMaster HD wallet path contains m in a child node ErrPathChildMaster = errors.New("Path contains m as a child node") // ErrPathNodeNotNumber HD wallet path node is not a valid uint32 number ErrPathNodeNotNumber = errors.New("Path node is not a valid uint32 number") // ErrPathNodeNumberTooLarge HD wallet path node is >= 2^31 ErrPathNodeNumberTooLarge = errors.New("Path node must be less than 2^31") )
Functions ¶
func IsImpossibleChildError ¶
IsImpossibleChildError returns true if the error is an ImpossibleChild Error
func NewImpossibleChildError ¶
NewImpossibleChildError creates an Error flagged as an ImpossibleChild error
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error wraps bip32 errors
func (Error) ImpossibleChild ¶
ImpossibleChild returns true if this error indicates that a given child number cannot produce a valid key. If the caller receives this error, they should skip this child and go to the next number. The probability of this happening is less than 1 in 2^127.
type PrivateKey ¶
type PrivateKey struct {
// contains filtered or unexported fields
}
PrivateKey represents a bip32 extended private key
func DeserializeEncodedPrivateKey ¶
func DeserializeEncodedPrivateKey(xprv string) (*PrivateKey, error)
DeserializeEncodedPrivateKey deserializes a base58 xprv key to a PrivateKey
func DeserializePrivateKey ¶
func DeserializePrivateKey(data []byte) (*PrivateKey, error)
DeserializePrivateKey deserializes the []byte serialization of a PrivateKey
func NewMasterKey ¶
func NewMasterKey(seed []byte) (*PrivateKey, error)
NewMasterKey creates a new master extended key from a seed. Seed should be between 128 and 512 bits; 256 bits are recommended. https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#master-key-generation
func NewPrivateKeyFromPath ¶
func NewPrivateKeyFromPath(seed []byte, p string) (*PrivateKey, error)
NewPrivateKeyFromPath returns a private key at a given bip32 path. The path must be a full path starting with m/, and the initial seed must be provided. This method can return an ImpossibleChild error.
func (PrivateKey) ChildNumber ¶
func (k PrivateKey) ChildNumber() uint32
func (*PrivateKey) DeriveSubpath ¶
func (k *PrivateKey) DeriveSubpath(nodes []PathNode) (*PrivateKey, error)
DeriveSubpath derives a PrivateKey at at bip32 subpath, e.g. `0'/1'/0`. The nodes argument must not be empty. This method can return an ImpossibleChild error.
func (*PrivateKey) Fingerprint ¶
func (k *PrivateKey) Fingerprint() []byte
Fingerprint returns the key fingerprint
func (*PrivateKey) Identifier ¶
func (k *PrivateKey) Identifier() []byte
Identifier returns the key ID
func (*PrivateKey) NewPrivateChildKey ¶
func (k *PrivateKey) NewPrivateChildKey(childIdx uint32) (*PrivateKey, error)
NewPrivateChildKey derives a private child key from a given parent as outlined by bip32, CDKpriv(). https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#private-parent-key--private-child-key This method can return an ImpossibleChild error.
func (*PrivateKey) NewPublicChildKey ¶
func (k *PrivateKey) NewPublicChildKey(childIdx uint32) (*PublicKey, error)
NewPublicChildKey derives a public child key from an extended public key, N(CKDpriv()). https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#private-parent-key--public-child-key This method can return an ImpossibleChild error.
func (*PrivateKey) PublicKey ¶
func (k *PrivateKey) PublicKey() *PublicKey
PublicKey returns the public version of key or return a copy The 'Neuter' function from the bip32 spec, N((k, c) -> (K, c). https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#private-parent-key--public-child-key
func (*PrivateKey) Serialize ¶
func (k *PrivateKey) Serialize() []byte
Serialize a Key to a 78 byte byte slice
func (*PrivateKey) String ¶
func (k *PrivateKey) String() string
String encodes the Key in the standard Bitcoin base58 encoding
type PublicKey ¶
type PublicKey struct {
// contains filtered or unexported fields
}
PublicKey represents a bip32 extended public key
func DeserializeEncodedPublicKey ¶
DeserializeEncodedPublicKey deserializes a base58 xpub key to a PublicKey
func DeserializePublicKey ¶
DeserializePublicKey deserializes the []byte serialization of a PublicKey
func (PublicKey) ChildNumber ¶
func (k PublicKey) ChildNumber() uint32
func (*PublicKey) Fingerprint ¶
Fingerprint returns the key fingerprint
func (*PublicKey) Identifier ¶
Identifier returns the key ID
func (*PublicKey) NewPublicChildKey ¶
NewPublicChildKey derives a public child key from an extended public key, CKDpub(). Hardened child keys cannot be derived; the value of childIdx must be less than 2^31. https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#public-parent-key--public-child-key This method can return an ImpossibleChild error.