bitcoinlib

0.7.8 · active · verified Thu Apr 16

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.

Common errors

Warnings

Install

Imports

Quickstart

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.")

view raw JSON →