Documentation ¶
Overview ¶
Package bip32 provides BIP 32 related functions.
Spec: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
Index ¶
- Constants
- Variables
- type PrivateKey
- func (p *PrivateKey) B58Serialize() string
- func (p *PrivateKey) ChainCode() [32]byte
- func (p *PrivateKey) ChildNumber() uint32
- func (p *PrivateKey) Depth() byte
- func (p *PrivateKey) GetPublicKey() *PublicKey
- func (p *PrivateKey) NewChildKey(childIdx uint32) (*PrivateKey, error)
- func (p *PrivateKey) ParentFingerprint() [4]byte
- func (p *PrivateKey) PrivateKey() secp256k1.Scalar
- func (p *PrivateKey) Serialize() [KeyLengthInBytes]byte
- type PublicKey
- func (p *PublicKey) B58Serialize() string
- func (p *PublicKey) ChainCode() [32]byte
- func (p *PublicKey) ChildNumber() uint32
- func (p *PublicKey) Depth() byte
- func (p *PublicKey) NewChildKey(childIdx uint32) (*PublicKey, error)
- func (p *PublicKey) ParentFingerprint() [4]byte
- func (p *PublicKey) PublicKey() secp256k1.Compressed
- func (p *PublicKey) Serialize() [KeyLengthInBytes]byte
Constants ¶
const FirstHardenedChildIndex uint32 = 0x80000000
FirstHardenedChildIndex is the first index of hardened child keys. Hardened child keys don't allow for public parent key -> public child key derivation, but provide more security than non-hardened child keys.
const KeyLengthInBytes = 82 // when serialized, public/private keys have this length
Variables ¶
var ( ErrorHardenedPublicChildKey = errors.New("can't create a hardened child key from a public key") ErrorTooDeepKey = errors.New("depth can't be >= 256") ErrorInvalidKeyLength = errors.New("invalid key length") ErrorInvalidVersion = errors.New("version is invalid") ErrorInvalidPublicKey = errors.New("public key is invalid") ErrorInvalidPrivateKey = errors.New("private key is invalid") ErrorChecksumMismatch = errors.New("checksum mismatch") ErrorZeroDepthAndNonZeroParentFingerprint = errors.New("zero depth with non-zero parent fingerprint") ErrorZeroDepthAndNonZeroIndex = errors.New("zero depth with non-zero index") ErrorPrivateKeyNotInRange = errors.New("private key not in range (1 <= p <= n-1)") )
Functions ¶
This section is empty.
Types ¶
type PrivateKey ¶
type PrivateKey struct {
// contains filtered or unexported fields
}
PrivateKey is a private key.
func B58DeserializePrivateKey ¶
func B58DeserializePrivateKey(encoded string) (*PrivateKey, error)
B58DeserializePrivateKey decodes a base58-encoded string and returns a PrivateKey.
func DeserializePrivateKey ¶
func DeserializePrivateKey(data [KeyLengthInBytes]byte) (*PrivateKey, error)
DeserializePrivateKey reads a []byte and returns a PrivateKey.
func NewMasterKey ¶
func NewMasterKey(seed []byte) *PrivateKey
NewMasterKey generates a new master private key with the given seed.
Example:
// the length of a seed should be between 128 and 512 bits; // this length (32 bits) is too short and for illustration purpose only seed, err := hex.DecodeString("01020304") master := NewMasterKey(seed)
func (*PrivateKey) B58Serialize ¶
func (p *PrivateKey) B58Serialize() string
B58Serialize returns the base58 representation of this PrivateKey.
func (*PrivateKey) ChainCode ¶
func (p *PrivateKey) ChainCode() [32]byte
ChainCode returns the chain code of this PrivateKey. This value is used in derivation of child keys.
func (*PrivateKey) ChildNumber ¶
func (p *PrivateKey) ChildNumber() uint32
ChildNumber returns the child index of this PrivateKey. If this PrivateKey is a master key, this function returns 0.
func (*PrivateKey) Depth ¶
func (p *PrivateKey) Depth() byte
Depth returns the depth of this PrivateKey. If the depth is 0, this key is a master key.
func (*PrivateKey) GetPublicKey ¶
func (p *PrivateKey) GetPublicKey() *PublicKey
GetPublicKey finds the corresponding PublicKey from this PrivateKey.
func (*PrivateKey) NewChildKey ¶
func (p *PrivateKey) NewChildKey(childIdx uint32) (*PrivateKey, error)
NewChildKey derives a new child key from this PrivateKey. The following errors may be returned:
- ErrorTooDeepKey: if this PrivateKey has depth 255
- ErrorInvalidPrivateKey: if the derived private key satisfies parse_{256}(I_L) >= n or k_i = 0 (with probability < 2^{-127})
func (*PrivateKey) ParentFingerprint ¶
func (p *PrivateKey) ParentFingerprint() [4]byte
ParentFingerprint returns the fingerprint of this PrivateKey's parent key. If this key is a master key, the fingerprint is filled with zero.
func (*PrivateKey) PrivateKey ¶
func (p *PrivateKey) PrivateKey() secp256k1.Scalar
PrivateKey returns the private key of secp256k1 in this PrivateKey.
func (*PrivateKey) Serialize ¶
func (p *PrivateKey) Serialize() [KeyLengthInBytes]byte
Serialize returns the []byte representation of this PrivateKey.
type PublicKey ¶
type PublicKey struct {
// contains filtered or unexported fields
}
PublicKey is a public key.
func B58DeserializePublicKey ¶
B58DeserializePublicKey decodes a base58-encoded string and returns a PublicKey.
func DeserializePublicKey ¶
func DeserializePublicKey(data [KeyLengthInBytes]byte) (*PublicKey, error)
DeserializePublicKey reads a []byte and returns a PublicKey.
func MasterPublicKeyFromRaw ¶
MasterPublicKeyFromRaw returns a master public key for mainnet with the given public key and the chain code.
func (*PublicKey) B58Serialize ¶
B58Serialize returns the base58 representation of this PublicKey.
func (*PublicKey) ChainCode ¶
ChainCode returns the chain code of this PublicKey. This value is used in derivation of child public keys.
func (*PublicKey) ChildNumber ¶
ChildNumber returns the child index of this PublicKey. If this PublicKey is a master key, this function returns 0.
func (*PublicKey) Depth ¶
Depth returns the depth of this PublicKey. If the depth is 0, this key is a master key.
func (*PublicKey) NewChildKey ¶
NewChildKey derives a new child key from this PublicKey. The following errors may be returned:
- ErrorHardenedPublicChildKey: if childIdx >= FirstHardenedChildIndex = 0x80000000
- ErrorTooDeepKey: if this PublicKey has depth 255
- ErrorInvalidPublicKey: if the derived public key satisfies parse_{256}(I_L) >= n (with probability < 2^{-127})
func (*PublicKey) ParentFingerprint ¶
ParentFingerprint returns the fingerprint of this PublicKey's parent key. If this key is a master key, the fingerprint is filled with zero.
func (*PublicKey) PublicKey ¶
func (p *PublicKey) PublicKey() secp256k1.Compressed
PublicKey returns the public key of secp256k1 (a compressed point) in this PublicKey.
func (*PublicKey) Serialize ¶
func (p *PublicKey) Serialize() [KeyLengthInBytes]byte
Serialize returns the []byte representation of this PublicKey.