{"id":8550,"library":"python-bitcoinlib","title":"Python Bitcoin Library","description":"python-bitcoinlib is a comprehensive Python library designed to interact with the Bitcoin protocol. It provides utilities for handling cryptographic operations, addresses, scripts, transactions, and more. The current stable version is 0.12.2, with releases occurring sporadically based on needed updates and bug fixes.","status":"active","version":"0.12.2","language":"en","source_language":"en","source_url":"https://github.com/petertodd/python-bitcoinlib","tags":["bitcoin","cryptocurrency","blockchain","wallet","p2p"],"install":[{"cmd":"pip install python-bitcoinlib","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The PyPI package name is `python-bitcoinlib`, but the top-level import is `bitcoin`.","wrong":"import python_bitcoinlib","symbol":"bitcoin","correct":"import bitcoin"},{"symbol":"CTransaction","correct":"from bitcoin.core import CTransaction"},{"symbol":"COIN","correct":"from bitcoin.core import COIN"},{"symbol":"CBitcoinAddress","correct":"from bitcoin.wallet import CBitcoinAddress"},{"note":"P2PKHAddress is a common concrete address type; there are others like P2SHAddress, P2WPKHAddress.","symbol":"P2PKHAddress","correct":"from bitcoin.wallet import P2PKHAddress"},{"symbol":"CBitcoinSecret","correct":"from bitcoin.wallet import CBitcoinSecret"},{"symbol":"CScript","correct":"from bitcoin.script import CScript"},{"note":"Constants for network parameters (e.g., MAINNET, TESTNET, REGTEST) are in `bitcoin.main`.","symbol":"MAINNET","correct":"from bitcoin.main import MAINNET"}],"quickstart":{"code":"from bitcoin.wallet import CBitcoinSecret, P2PKHAddress\nfrom bitcoin.main import MAINNET # Import specific networks\n\n# 1. Generate a new random private key for the Bitcoin Mainnet\n# In a real application, you must store this secret securely.\n# For Testnet, replace MAINNET with TESTNET.\nprivate_key = CBitcoinSecret.from_secret_bytes(CBitcoinSecret.generate_secret_bytes(), MAINNET)\n\n# 2. Derive the corresponding public key\npublic_key = private_key.pub\n\n# 3. Derive a Pay-to-Public-Key-Hash (P2PKH) address from the public key\naddress = P2PKHAddress.from_pubkey(public_key)\n\nprint(f\"Generated Private Key (WIF format): {private_key.D.to_string()}\")\nprint(f\"Corresponding Bitcoin Address: {address}\")\nprint(f\"Network: {'Mainnet' if private_key.is_mainnet else 'Testnet' if private_key.is_testnet else 'Unknown'}\")\n","lang":"python","description":"This quickstart demonstrates how to generate a new Bitcoin private key and derive its corresponding P2PKH address for the Mainnet. It showcases the basic cryptographic primitives used for wallet management. Remember to handle private keys with extreme care in production environments."},"warnings":[{"fix":"Upgrade `python-bitcoinlib` to version 0.11.1 or newer. These versions include a pure-Python RIPEMD-160 implementation, resolving compatibility issues.","message":"Older versions of python-bitcoinlib (prior to 0.11.1) relied on OpenSSL for RIPEMD-160 hashing. Newer OpenSSL versions have removed RIPEMD-160 support, leading to errors.","severity":"breaking","affected_versions":"<0.11.1"},{"fix":"Always explicitly pass the correct network parameter (e.g., `MAINNET`, `TESTNET`) when creating keys, addresses, or transactions. Double-check your chosen network context for all operations.","message":"Mixing up network parameters (e.g., `MAINNET`, `TESTNET`, `REGTEST`) can lead to addresses or transactions valid for the wrong network, potentially resulting in loss of funds or failed transactions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Implement robust security practices for private key generation, storage, and usage. Never hardcode private keys. Utilize secure key management systems or hardware wallets for production environments.","message":"Private keys grant full control over Bitcoin funds. Improper handling, such as hardcoding, insecure storage, or logging, can lead to irreversible loss of assets.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For new transactions, prioritize SegWit formats (e.g., P2WPKH, P2SH-P2WPKH) to mitigate malleability. Be aware of this risk when dealing with unconfirmed legacy transactions or when building systems that rely on stable TXIDs.","message":"Older Bitcoin transaction formats (pre-SegWit) are susceptible to transaction malleability, which can alter the transaction ID (TXID) before confirmation, impacting dependants.","severity":"gotcha","affected_versions":"All versions (when interacting with legacy transaction types)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Use `import bitcoin` or `from bitcoin import ...` to access the library's modules.","cause":"You are attempting to import the library using its PyPI package name (`python_bitcoinlib`) instead of its actual top-level module name.","error":"ModuleNotFoundError: No module named 'python_bitcoinlib'"},{"fix":"Upgrade `python-bitcoinlib` to version 0.11.1 or newer (`pip install --upgrade python-bitcoinlib`). These versions include a pure-Python RIPEMD160 implementation, making it compatible with modern OpenSSL.","cause":"Your version of `python-bitcoinlib` (prior to 0.11.1) is trying to use OpenSSL's RIPEMD160 function, which has been removed in newer OpenSSL libraries.","error":"ValueError: RIPEMD160 hash not found or AttributeError: module 'OpenSSL.crypto' has no attribute 'RIPEMD160'"},{"fix":"Carefully verify the input string for accuracy. Ensure the network context (e.g., `MAINNET`, `TESTNET`) matches the address or key you are trying to parse.","cause":"The Bitcoin address or WIF (Wallet Import Format) string you provided is malformed, contains a typo, or does not correspond to the expected network (e.g., trying to decode a Mainnet address as Testnet).","error":"ValueError: Invalid checksum for base58 string"},{"fix":"Ensure your Bitcoin Core node is running and configured to accept RPC connections (check `bitcoin.conf` for `rpcbind`, `rpcport`, `rpcuser`, `rpcpassword`). Verify firewall rules and network connectivity between your application and the node.","cause":"When using `bitcoin.rpc`, this indicates the Bitcoin Core node's RPC server is not running, is not accessible from your application, or its configuration prevents connection.","error":"ConnectionRefusedError: [Errno 111] Connection refused"}]}