candidateblock_bitcoin_library.hd_wallet module
Bitcoin Wallets The word “wallet” refers to the data structure used to store and manage a users keys. In a sense the “wallet” is just a keychain. We will only implement industry-standard-based hierarchical deterministic (HD BIP-32/BIP-44) wallet with a mnemonic seed (BIP-39) for backup.
- class candidateblock_bitcoin_library.hd_wallet.HdWallet
Bases:
objectHierarchical Deterministic Wallet Class
The word “wallet” refers to the data structure used to store and manage a users keys. In a sense the “wallet” is just a keychain. We will only implement industry-standard-based hierarchical deterministic (HD BIP-32/BIP-44) wallet with a mnemonic seed (BIP-39) for backup.
- HARDEND = 2147483648
- classmethod child_key_derivation(parent_key: bytes, parent_chaincode: bytes, index: int, is_private: bool)
- static encode(key: bytes = b'', chain_code: bytes = b'', parent_key: bytes = b'', depth: int = 0, child: int = 0, is_master: bool = False, is_private: bool = True, is_mainnet: bool = True) str
Extended public and private keys are serialized as follows:
4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; testnet: 0x043587CF public, 0x04358394 private) 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 derived keys, …. 4 bytes: the fingerprint of the parent’s key (0x00000000 if master key) 4 bytes: child number. This is ser32(i) for i in xi = xpar/i, with xi the key being serialized. (0x00000000 if master key) 32 bytes: the chain code 33 bytes: the public key or private key data (serP(K) for public keys, 0x00 || ser256(k) for private keys) This 78 byte structure can be encoded like other Bitcoin data in Base58, by first adding 32 checksum bits (derived from the double SHA-256 checksum), and then converting to the Base58 representation. This results in a Base58-encoded string of up to 112 characters. Because of the choice of the version bytes, the Base58 representation will start with “xprv” or “xpub” on mainnet, “tprv” or “tpub” on testnet.
- Parameters:
key (bytes) – Private 256-bits (32-Bytes) or COMPRESSED Private key 264-bits (33-Bytes)
chain_code (bytes) – Private or Public Chain Code 256-bits (32-Bytes)
parent_key (bytes) – Parent Key, Private 256-bits (32-Bytes) or COMPRESSED Private key 264-bits (33-Bytes), or nohting if master node
depth (int) – 0 for master nodes, 1 for level-1 derived keys, ….
child (int) – child number. This is ser32(i) for i in xi = xpar/i, with xi the key being serialized. (0x00000000 if master key)
is_master (bool) – Are we encoding (serializing) the master / root key
is_private (bool) – Are we encoding (serializing) as Public or Private key
is_mainnet (bool) – Are we encoding for Mainnet or Testnet
- Returns:
Base58Check encoded
- Return type:
str
- static master_key_generation(seed: bytes = b'')
- classmethod parse_path(path: str = '') List[int]
Parse Path BIP-32 HD wallet path parse and clean hardended char ‘ to H and all upper case
- Parameters:
path (str) – in form m/84’/0’/1’ or m/84h/0h/1h
- Returns:
Array of integers with two dimensions 1. depth, 2. index + hardened [0x800000] if applicable
- Return type:
List[int]