aiobitcoin.tools.key package

aiobitcoin.tools.key.BIP32Node module

A BIP0032-style hierarchical wallet.

Implement a BIP0032-style hierarchical wallet which can create public or private wallet keys. Each key can create many child nodes. Each node has a wallet key and a corresponding private & public key, which can be used to generate Bitcoin addresses or WIF private keys.

At any stage, the private information can be stripped away, after which descendants can only produce public keys.

Private keys can also generate “hardened” children, which cannot be generated by the corresponding public keys. This is useful for generating “change” addresses, for example, which there is no need to share with people you give public keys to.

class aiobitcoin.tools.key.BIP32Node.BIP32Node(netcode, chain_code, depth=0, parent_fingerprint=b'x00x00x00x00', child_index=0, secret_exponent=None, public_pair=None)[source]

Bases: aiobitcoin.tools.key.Key.Key

This is a deterministic wallet that complies with BIP0032 https://en.bitcoin.it/wiki/BIP_0032

as_text(as_private=False)

Yield a 111-byte string corresponding to this node.

chain_code()[source]
child_index()[source]
children(max_level=50, start_index=0, include_hardened=True)[source]
fingerprint()[source]
classmethod from_hwif(b58_str, allow_subkey_suffix=True)[source]

Generate a Wallet from a base58 string in a standard way.

classmethod from_master_secret(master_secret, netcode='BTC')[source]

Generate a Wallet from a master password.

classmethod from_wallet_key(b58_str, allow_subkey_suffix=True)

Generate a Wallet from a base58 string in a standard way.

hwif(as_private=False)[source]

Yield a 111-byte string corresponding to this node.

parent_fingerprint()[source]
public_copy()[source]

Yield the corresponding public node for this node.

serialize(as_private=None)[source]

Yield a 78-byte binary blob corresponding to this node.

subkey(i=0, is_hardened=False, as_private=None)[source]

Yield a child node for this node.

i: the index for this node. is_hardened: use “hardened key derivation”. That is, the public version

of this node cannot calculate this child.

as_private: set to True to get a private subkey.

subkey_for_path(path)[source]
path: a path of subkeys denoted by numbers and slashes. Use H or p
for private key derivation. End with .pub to force the key public.
Examples: 1H/5/2/1 would call subkey(i=1, is_hardened=True)
.subkey(i=5).subkey(i=2).subkey(i=1) and then yield the private key 0/0/458.pub would call subkey(i=0).subkey(i=0) .subkey(i=458) and then yield the public key

You should choose one of the H or p convention for private key derivation and stick with it.

subkeys(path)[source]

A generalized form that can return multiple subkeys.

tree_depth()[source]
wallet_key(as_private=False)

Yield a 111-byte string corresponding to this node.

exception aiobitcoin.tools.key.BIP32Node.PublicPrivateMismatchError[source]

Bases: Exception

aiobitcoin.tools.key.Key module

exception aiobitcoin.tools.key.Key.InvalidPublicPairError[source]

Bases: ValueError

exception aiobitcoin.tools.key.Key.InvalidSecretExponentError[source]

Bases: ValueError

class aiobitcoin.tools.key.Key.Key(secret_exponent=None, public_pair=None, hash160=None, prefer_uncompressed=None, is_compressed=None, is_pay_to_script=False, netcode=None)[source]

Bases: object

address(use_uncompressed=None)[source]

Return the public address representation of this key, if available. If use_uncompressed is not set, the preferred representation is returned.

as_text()[source]

Return a textual representation of this key.

bitcoin_address(use_uncompressed=None)

Return the public address representation of this key, if available. If use_uncompressed is not set, the preferred representation is returned.

classmethod from_sec(sec, netcode=None)[source]

Create a key from an sec bytestream (which is an encoding of a public pair).

classmethod from_text(text, is_compressed=False)[source]

This function will accept a BIP0032 wallet string, a WIF, or a bitcoin address.

The “is_compressed” parameter is ignored unless a public address is passed in.

hash160(use_uncompressed=None)[source]

Return the hash160 representation of this key, if available. If use_uncompressed is not set, the preferred representation is returned.

is_private()[source]
netcode()[source]

Return the netcode

public_copy()[source]
public_pair()[source]

Return a pair of integers representing the public key (or None).

sec(use_uncompressed=None)[source]

Return the SEC representation of this key, if available. If use_uncompressed is not set, the preferred representation is returned.

sec_as_hex(use_uncompressed=None)[source]

Return the SEC representation of this key as hex text. If use_uncompressed is not set, the preferred representation is returned.

secret_exponent()[source]

Return an integer representing the secret exponent (or None).

sign(h)[source]

Return a der-encoded signature for a hash h. Will throw a RuntimeError if this key is not a private key

subkey(path_to_subkey)[source]

Return the Key corresponding to the hierarchical wallet’s subkey

subkeys(path_to_subkeys)[source]

Return an iterator yielding Keys corresponding to the hierarchical wallet’s subkey path (or just this key).

verify(h, sig)[source]

Return whether a signature is valid for hash h using this key.

wif(use_uncompressed=None)[source]

Return the WIF representation of this key, if available. If use_uncompressed is not set, the preferred representation is returned.