libcrypto.bip32

BIP32 hierarchical deterministic wallets, including private (CKDpriv) and public (CKDpub) child derivation and xprv/xpub serialization.

class HDWallet

HDWallet(seed, network="mainnet")

Build a wallet from a binary seed. Raises BIP32Error if the seed produces an out-of-range master key.

HDWallet.from_mnemonic(mnemonic, passphrase="", network="mainnet") -> HDWallet

Build a wallet from a BIP39 mnemonic (seed derived with PBKDF2-HMAC-SHA512).

Members

Member Description
master_node The root HDNode.
derive_from_path(path) -> HDNode Derive a node from a path such as "m/44'/0'/0'/0/0".
from libcrypto import HDWallet

hd = HDWallet.from_mnemonic("abandon abandon ... about")
node = hd.derive_from_path("m/84'/0'/0'/0/0")

class HDNode

A single node in the tree. Nodes are either private (hold a private key) or public/watch-only (hold only a compressed public key and chain code).

Construction

HDNode(private_key, chain_code, depth=0,
       parent_fingerprint=b"\x00\x00\x00\x00",
       child_number=0, network="mainnet", public_key=None)

Pass private_key=None together with public_key= to build a watch-only node.

Properties

Property Description
private_key 32-byte private key, or None for watch-only nodes.
public_key 33-byte compressed public key.
is_private True when a private key is present.
fingerprint First 4 bytes of hash160(public_key).

Methods

Method Description
derive_child(index) -> HDNode CKDpriv for private nodes; CKDpub for public nodes. Hardened indices require a private node.
derive_path(path) -> HDNode Derive along a path string.
neuter() -> HDNode Return a watch-only copy (drops the private key).
serialize_private() -> str Serialize as an extended private key (xprv).
serialize_public() -> str Serialize as an extended public key (xpub).
HDNode.deserialize(extended_key) -> HDNode Parse an xprv or xpub.

Public (CKDpub) derivation

Watch-only nodes derive non-hardened children through elliptic-curve point addition, exactly matching the public key that private derivation would produce:

from libcrypto import HDWallet

hd = HDWallet(bytes.fromhex("000102030405060708090a0b0c0d0e0f"))
account = hd.derive_from_path("m/0'")

watch_only = account.neuter()             # no private key inside
child_pub = watch_only.derive_child(1)    # CKDpub
child_priv = account.derive_child(1)      # CKDpriv
assert child_pub.public_key == child_priv.public_key

Hardened derivation from a public node is impossible by design and raises BIP32Error.

class BIP32Error

Subclass of ValueError raised for all BIP32 failures.