Python Bitcoin Library
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.
Common errors
-
ModuleNotFoundError: No module named 'python_bitcoinlib'
cause You are attempting to import the library using its PyPI package name (`python_bitcoinlib`) instead of its actual top-level module name.fixUse `import bitcoin` or `from bitcoin import ...` to access the library's modules. -
ValueError: RIPEMD160 hash not found or AttributeError: module 'OpenSSL.crypto' has no attribute 'RIPEMD160'
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.fixUpgrade `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. -
ValueError: Invalid checksum for base58 string
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).fixCarefully verify the input string for accuracy. Ensure the network context (e.g., `MAINNET`, `TESTNET`) matches the address or key you are trying to parse. -
ConnectionRefusedError: [Errno 111] Connection refused
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.fixEnsure 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.
Warnings
- breaking 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.
- gotcha 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.
- gotcha Private keys grant full control over Bitcoin funds. Improper handling, such as hardcoding, insecure storage, or logging, can lead to irreversible loss of assets.
- gotcha Older Bitcoin transaction formats (pre-SegWit) are susceptible to transaction malleability, which can alter the transaction ID (TXID) before confirmation, impacting dependants.
Install
-
pip install python-bitcoinlib
Imports
- bitcoin
import python_bitcoinlib
import bitcoin
- CTransaction
from bitcoin.core import CTransaction
- COIN
from bitcoin.core import COIN
- CBitcoinAddress
from bitcoin.wallet import CBitcoinAddress
- P2PKHAddress
from bitcoin.wallet import P2PKHAddress
- CBitcoinSecret
from bitcoin.wallet import CBitcoinSecret
- CScript
from bitcoin.script import CScript
- MAINNET
from bitcoin.main import MAINNET
Quickstart
from bitcoin.wallet import CBitcoinSecret, P2PKHAddress
from bitcoin.main import MAINNET # Import specific networks
# 1. Generate a new random private key for the Bitcoin Mainnet
# In a real application, you must store this secret securely.
# For Testnet, replace MAINNET with TESTNET.
private_key = CBitcoinSecret.from_secret_bytes(CBitcoinSecret.generate_secret_bytes(), MAINNET)
# 2. Derive the corresponding public key
public_key = private_key.pub
# 3. Derive a Pay-to-Public-Key-Hash (P2PKH) address from the public key
address = P2PKHAddress.from_pubkey(public_key)
print(f"Generated Private Key (WIF format): {private_key.D.to_string()}")
print(f"Corresponding Bitcoin Address: {address}")
print(f"Network: {'Mainnet' if private_key.is_mainnet else 'Testnet' if private_key.is_testnet else 'Unknown'}")