{"id":8882,"library":"bitcoinlib","title":"bitcoinlib","description":"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.","status":"active","version":"0.7.8","language":"en","source_language":"en","source_url":"http://github.com/1200wd/bitcoinlib","tags":["bitcoin","cryptocurrency","blockchain","wallet","transaction","key management"],"install":[{"cmd":"pip install bitcoinlib","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"Wallet","correct":"from bitcoinlib.wallets import Wallet"},{"symbol":"Key","correct":"from bitcoinlib.keys import Key"},{"symbol":"Transaction","correct":"from bitcoinlib.transactions import Transaction"},{"symbol":"Script","correct":"from bitcoinlib.scripts import Script"},{"symbol":"Network","correct":"from bitcoinlib.networks import Network"},{"symbol":"Mnemonic","correct":"from bitcoinlib.mnemonic import Mnemonic"}],"quickstart":{"code":"from bitcoinlib.wallets import Wallet\nfrom bitcoinlib.networks import Network\n\n# Define the network (e.g., 'testnet', 'regtest', or 'bitcoin' for mainnet)\n# Explicitly set for safety and clarity.\nnetwork_name = 'testnet'\n\n# Create a new wallet or load an existing one\n# Wallets are persistent and stored in a database (default SQLite).\nwallet_name = 'my_first_bitcoinlib_wallet'\ntry:\n    wallet = Wallet(wallet_name, network=network_name)\n    print(f\"Loaded existing wallet '{wallet_name}' on {network_name}.\")\nexcept Exception:\n    wallet = Wallet.create(wallet_name, network=network_name)\n    print(f\"Created new wallet '{wallet_name}' on {network_name}.\")\n\n# Generate a new address for the wallet\nkey = wallet.get_key()\naddress = key.address\n\nprint(f\"New address: {address}\")\nprint(f\"Private key (WIF): {key.wif}\") # CAUTION: Handle private keys securely!\n\n# Close the wallet to ensure all changes are saved\nwallet.close()\nprint(\"Wallet closed.\")","lang":"python","description":"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."},"warnings":[{"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.","message":"Major 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.","severity":"breaking","affected_versions":"0.7.x and later"},{"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')`.","message":"By 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Wallets 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Handling 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Creating 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"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.","cause":"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.","error":"bitcoinlib.exceptions.WalletError: No wallet with name 'my_wallet'"},{"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.","cause":"The string provided for a private key (e.g., WIF, hex) is not correctly formatted, is an incorrect length, or uses an unsupported encoding.","error":"bitcoinlib.exceptions.InvalidKeyError: Invalid private key format"},{"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.","cause":"The wallet does not contain enough unspent transaction outputs (UTXOs) to cover the amount you are trying to send plus the transaction fee.","error":"bitcoinlib.exceptions.InsufficientFundsError: Insufficient funds"},{"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.","cause":"Incorrect import path used for the `Wallet` class, possibly due to outdated code or a typo.","error":"ModuleNotFoundError: No module named 'bitcoinlib.wallets'"}]}