btclib.bip32 package

Submodules

btclib.bip32.bip32 module

BIP32 Hierarchical Deterministic Wallet functions.

A deterministic wallet is a hash-chain of private/public key pairs that derives from a single root, which is the only element requiring backup. Moreover, there are schemes where public keys can be calculated without accessing private keys.

A hierarchical deterministic wallet is a tree of multiple hash-chains, derived from a single root, allowing for selective sharing of keypair chains.

Here, the HD wallet is implemented according to BIP32 bitcoin standard https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki.

A BIP32 extended key is 78 bytes:

  • [ : 4] version

  • [ 4: 5] depth in the derivation path

  • [ 5: 9] parent fingerprint

  • [ 9:13] index

  • [13:45] chain code

  • [45:78] compressed pub_key or [0x00][prv_key]

class btclib.bip32.bip32.BIP32KeyData(version: Union[bytes, str], depth: int, parent_fingerprint: Union[bytes, str], index: int, chain_code: Union[bytes, str], key: Union[bytes, str], check_validity: bool = True)

Bases: object

assert_valid() None
classmethod b58decode(address: Union[bytes, str], check_validity: bool = True) BIP32KeyData
b58encode(check_validity: bool = True) str
chain_code: bytes
depth: int
index: int
property is_hardened: bool
property is_private: bool
property is_root: bool
key: bytes
parent_fingerprint: bytes
classmethod parse(xkey_bin: Union[BytesIO, bytes, str], check_validity: bool = True) BIP32KeyData

Return a BIP32KeyData by parsing 73 bytes from binary data.

serialize(check_validity: bool = True) bytes
version: bytes
btclib.bip32.bip32.crack_prv_key(parent_xpub: Union[BIP32KeyData, bytes, str], child_xprv: Union[BIP32KeyData, bytes, str]) str
btclib.bip32.bip32.derive(xkey: Union[BIP32KeyData, bytes, str], der_path: Union[str, Sequence[int], int, bytes], forced_version: Optional[Union[bytes, str]] = None) str

Derive a BIP32 key across a path spanning multiple depth levels.

Valid BIP32DerPath examples:

  • string like “m/44h/0’/1H/0/10”

  • iterable integer indexes

  • one single integer index

  • bytes in multiples of the 4-bytes index

BIP32DerPath is case/blank/extra-slash insensitive (e.g. “M /44h / 0’ /1H // 0/ 10 / “).

btclib.bip32.bip32.derive_from_account(mxkey: Union[BIP32KeyData, bytes, str], branch: int, address_index: int, branches_0_1_only: bool = True, max_index: int = 65535) str

Derive a key with public derivation at the given branch and index.

It also ensures that the master key is hardened, that the branch is a standard receive or change, and that the index is not arbitrarily high.

btclib.bip32.bip32.rootxprv_from_seed(seed: Union[bytes, str], version: Union[bytes, str] = b'\x04\x88\xad\xe4') str

Return BIP32 root master extended private key from seed.

btclib.bip32.bip32.xpub_from_xprv(xprv: Union[BIP32KeyData, bytes, str]) str

Neutered Derivation (ND).

Derivation of the extended public key corresponding to an extended private key (“neutered” as it removes the ability to sign transactions).

btclib.bip32.der_path module

BIP32 derivation path and key origin.

A BIP 32 derivation path can be represented as:

  • “m/44h/0’/1H/0/10” or “44h/0’/1H/0/10” string

  • sequence of integer indexes (even a single int)

  • bytes (multiples of 4-bytes index)

btclib.bip32.der_path.bytes_from_bip32_path(der_path: Union[str, Sequence[int], int, bytes]) bytes
btclib.bip32.der_path.indexes_from_bip32_path(der_path: Union[str, Sequence[int], int, bytes]) List[int]
btclib.bip32.der_path.int_from_index_str(s: str) int
btclib.bip32.der_path.str_from_bip32_path(der_path: Union[str, Sequence[int], int, bytes], master_fingerprint: Optional[Union[bytes, str]] = None, hardening: str = 'h') str
btclib.bip32.der_path.str_from_index_int(i: int, hardening: str = 'h') str

btclib.bip32.key_origin module

BIP32 key origin.

class btclib.bip32.key_origin.BIP32KeyOrigin(master_fingerprint: Union[bytes, str], der_path: Union[str, Sequence[int], int, bytes], check_validity: bool = True)

Bases: object

assert_valid() None
der_path: Sequence[int]
property description: str
classmethod from_description(data: str, check_validity: bool = True) BIP32KeyOrigin
classmethod from_dict(dict_: Mapping[str, str], check_validity: bool = True) BIP32KeyOrigin
master_fingerprint: bytes
classmethod parse(data: Union[bytes, str], check_validity: bool = True) BIP32KeyOrigin

Return a BIP32KeyOrigin by parsing binary data.

serialize(check_validity: bool = True) bytes
to_dict(check_validity: bool = True) Dict[str, str]
btclib.bip32.key_origin.assert_valid_hd_key_paths(hd_key_paths: Mapping[bytes, BIP32KeyOrigin]) None

Raise an exception if the dataclass element is not valid.

btclib.bip32.key_origin.decode_from_bip32_derivs(bip32_derivs: Sequence[Mapping[str, str]]) Dict[bytes, BIP32KeyOrigin]

Return the dataclass element from its json representation.

btclib.bip32.key_origin.decode_hd_key_paths(map_: Optional[Mapping[Union[bytes, str], BIP32KeyOrigin]]) Dict[bytes, BIP32KeyOrigin]

Return the dataclass element from its json representation.

btclib.bip32.key_origin.encode_to_bip32_derivs(hd_key_paths: Mapping[bytes, BIP32KeyOrigin]) List[Dict[str, str]]

Return the json representation of the dataclass element.

btclib.bip32.slip132 module

SLIP132 address.

https://github.com/satoshilabs/slips/blob/master/slip-0132.md

btclib.bip32.slip132.address_from_xkey(xkey: Union[BIP32KeyData, bytes, str]) str

Return the SLIP132 base58/bech32 address.

The address is always derived from the compressed public key, as this is the default public key representation in BIP32.

btclib.bip32.slip132.address_from_xpub(xpub: Union[BIP32KeyData, bytes, str]) str

Return the SLIP132 base58/bech32 address.

The address is always derived from the compressed public key, as this is the default public key representation in BIP32.

btclib.bip32.slip132.p2pkh_xkey(xkey: Union[BIP32KeyData, bytes, str], der_path: Union[str, Sequence[int], int, bytes] = 'm/44h/0h/0h', check_root_xkey: bool = True) str

Return a p2pkh BIP32 xprv/xpub master key at the derivation path.

btclib.bip32.slip132.p2wpkh_p2sh_xkey(xkey: Union[BIP32KeyData, bytes, str], der_path: Union[str, Sequence[int], int, bytes] = 'm/49h/0h/0h', check_root_xkey: bool = True) str

Return a p2wpkh-p2sh BIP32 yprv/ypub master key at the derivation path.

btclib.bip32.slip132.p2wpkh_xkey(xkey: Union[BIP32KeyData, bytes, str], der_path: Union[str, Sequence[int], int, bytes] = 'm/84h/0h/0h', check_root_xkey: bool = True) str

Return a p2wpkh BIP32 zprv/zpub master key at the derivation path.

Module contents

btclib.bip32 submodule.