Registry / auth-security / bitcoinlib

bitcoinlib

JSON →
library0.7.9pypypi✓ verified 84d ago

bitcoinlib is a comprehensive Python library for handling Bitcoin and other cryptocurrencies. It provides functionalities for creating and managing wallets, keys, addresses, scripts, and transactions, supporting various networks like mainnet, testnet, and regtest. The current stable version is 0.7.8, with updates typically focusing on bug fixes, performance improvements, and feature enhancements as the underlying protocols evolve.

pip install bitcoinlib
INSTALL
IMPORT
SIG · BITCOINLIB
B
bitcoinlib
auth-securitypythonv0.7.9
Install
4.6s avg
Import
1504ms
Disk
62MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.7.9 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
build_error
glibc
py 3.103.920 runs
installs and imports cleanly · install 4.6s · import 1.504s · 62MB
62MB installed
● package 62MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Wallet
from bitcoinlib.wallets import Wallet
Key
from bitcoinlib.keys import Key
Transaction
from bitcoinlib.transactions import Transaction
Script
from bitcoinlib.scripts import Script
Network
from bitcoinlib.networks import Network
Mnemonic
from bitcoinlib.mnemonic import Mnemonic

This quickstart demonstrates how to create or load a persistent wallet, generate a new address, and retrieve its private key (for demonstration purposes only; private keys should be handled with extreme care in production). It emphasizes explicit network selection and proper wallet closure.

from bitcoinlib.wallets import Wallet from bitcoinlib.networks import Network # Define the network (e.g., 'testnet', 'regtest', or 'bitcoin' for mainnet) # Explicitly set for safety and clarity. network_name = 'testnet' # Create a new wallet or load an existing one # Wallets are persistent and stored in a database (default SQLite). wallet_name = 'my_first_bitcoinlib_wallet' try: wallet = Wallet(wallet_name, network=network_name) print(f"Loaded existing wallet '{wallet_name}' on {network_name}.") except Exception: wallet = Wallet.create(wallet_name, network=network_name) print(f"Created new wallet '{wallet_name}' on {network_name}.") # Generate a new address for the wallet key = wallet.get_key() address = key.address print(f"New address: {address}") print(f"Private key (WIF): {key.wif}") # CAUTION: Handle private keys securely! # Close the wallet to ensure all changes are saved wallet.close() print("Wallet closed.")
Debug
Known issues
breakingMajor refactoring occurred in version 0.7.x, particularly affecting Wallet and Network management. Older import paths, object instantiation, and network configuration methods from 0.6.x are likely to break.
fix
Review the official documentation for 0.7.x, especially sections on `bitcoinlib.wallets` and `bitcoinlib.networks`, and update your code to use the new interfaces. Explicitly import `Network` and pass it to wallet creation.
affects: 0.7.x and later
gotchaBy default, `bitcoinlib` often assumes the Bitcoin mainnet ('bitcoin') if no network is explicitly specified during wallet or transaction creation. This can lead to unexpected behavior if you intend to work on testnets or regtest.
fix
Always explicitly specify the desired network using the `network` parameter, e.g., `Wallet.create('mywallet', network='testnet')` or `from bitcoinlib.networks import Network; network = Network('regtest')`.
affects: All versions
gotchaWallets in `bitcoinlib` are persistent and stored in an underlying database (defaulting to SQLite). Changes made to a wallet (e.g., new keys, transactions) are not automatically saved until `wallet.close()` is called.
fix
Ensure `wallet.close()` is called when you are finished with a wallet instance to persist any changes. Consider using a `try...finally` block or a `with` statement if the wallet supports it (check docs) to guarantee closure.
affects: All versions
gotchaHandling private keys directly in code, especially for display or logging, is a significant security risk. `bitcoinlib` provides tools to manage keys, but ultimate security is the user's responsibility.
fix
Never expose private keys in logs, commit them to version control, or store them insecurely. For production, consider using hardware security modules (HSMs) or secure key management services. Only use `key.wif` for demonstration or very specific, secure local operations.
affects: All versions
gotchaCreating transactions without proper fee calculation can lead to transactions getting stuck or being rejected by the network. While `bitcoinlib` can estimate fees, it requires careful consideration.
fix
Always include a transaction fee. Use `transaction.send()` which often handles fee estimation, or manually set `transaction.fee()` after adding inputs and outputs, and before signing and sending. Monitor network fee rates.
affects: All versions
Errors
Common errors & fixes
bitcoinlib.exceptions.WalletError: No wallet with name 'my_wallet'
Attempting to load a wallet that has not been created yet, or trying to load it from a different database path than where it was originally saved.
fix
Before loading, ensure the wallet has been created using `Wallet.create('my_wallet', ...)` at least once. If you use custom database URIs, ensure you're providing the correct `db_uri` when instantiating the Wallet.
bitcoinlib.exceptions.InvalidKeyError: Invalid private key format
The string provided for a private key (e.g., WIF, hex) is not correctly formatted, is an incorrect length, or uses an unsupported encoding.
fix
Double-check the private key string. Ensure it's a valid Wallet Import Format (WIF) string, a correct 64-character hexadecimal string, or the specific format expected by the `Key` constructor you are using.
bitcoinlib.exceptions.InsufficientFundsError: Insufficient funds
The wallet does not contain enough unspent transaction outputs (UTXOs) to cover the amount you are trying to send plus the transaction fee.
fix
Verify the wallet's balance using `wallet.get_balance()`. Ensure that the funds are confirmed on the blockchain. Reduce the transaction amount or add more funds to the wallet.
ModuleNotFoundError: No module named 'bitcoinlib.wallets'
Incorrect import path used for the `Wallet` class, possibly due to outdated code or a typo.
fix
Use the correct import statement: `from bitcoinlib.wallets import Wallet`. Similar issues might arise for `keys`, `transactions`, or `networks` modules if using incorrect paths.
Upgrade
Version history
0.7.9latest on PyPI · released Jun 5, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
52 hits · last 30 days
node
46
OpenAI (training)
1
Resources
bitcoinlib — pip install bitcoinlib · libregistry