🇧🇷 BrBitcoin Python SDK
Features
- Automatic Zeroization: Sensitive data wiped from memory using context managers
- Multi-Layer Security: Hierarchical deterministic wallets with encrypted backups
- Network Agnostic: Supports Regtest/Testnet/Mainnet via multiple backends
- Full RPC Support: Direct access to Bitcoin Core JSON-RPC API
📦 Installation
Via Poetry (Recommended)
poetry add brbitcoin
Via pip
pip install brbitcoin
🚀 Quick Start
1. Wallet Management
from brbitcoin import Wallet, Network
# Create random HD wallet (testnet by default)
with Wallet.create(network=Network.TESTNET) as wallet:
print(f"New address: {wallet.address}")
# Always store encrypted backups
wallet.export_encrypted("wallet.bak", password=os.environ["WALLET_PASS"])
# Import from existing hex private key
with Wallet.from_private_key("beefcafe...") as wallet:
print(f"Imported address: {wallet.address}")
# Create from BIP39 mnemonic
mnemonic = "absorb lecture valley scissors giant evolve planet rotate siren chaos"
with Wallet.from_mnemonic(mnemonic, network=Network.MAINNET) as wallet:
print(f"Mainnet address: {wallet.address}")
2. Blockchain Interaction
2.1 Address Information
from brbitcoin import Wallet, get_address_info
info = get_address_info("bc1q...", Network.MAINNET)
print(f"Balance: {info.balance} satoshis")
print(f"UTXOs: {len(info.utxos)}")
with Wallet(network=Network.TESTNET) as wallet:
print(f"Wallet balance: {wallet.balance} sats")
2.2 Transaction Inspection
from brbitcoin import Wallet, get_transaction
tx = get_transaction("aabb...", Network.REGTEST)
print(f"Confirmations: {tx.confirmations}")
for output in tx.outputs:
print(f"Output value: {output.value}")
with Wallet.from_private_key("beef...") as wallet:
utxos = wallet.utxos()
for utxo in utxos:
print(f"UTXO: {utxo.txid}:{utxo.vout} - {utxo.value} sats")
2.3 Block Exploration
from brbitcoin import get_block
block = get_block_by_hash("000000000019d6...", Network.MAINNET)
print(f"The Block has {len(block.txn)} transactions")
genesis = get_block_by_number(0, Network.MAINNET)
print(f"Genesis block timestamp: {genesis.timestamp}")
3. Transaction Building
3.1 High-Level (Recommended)
from brbitcoin import Wallet, Fee
RECEIVER = "tb123..."
AMOUNT = 0.001 # BTC
with Wallet(network=Network.REGTEST) as wallet:
txid = wallet.send(to=RECEIVER, amount=AMOUNT)
print(f"Broadcasted TX ID: {txid}")
3.2 Mid-Level Control
from brbitcoin import Wallet, Transaction, to_btc
RECEIVER = "tb123..."
AMOUNT = 100_000 # Satoshis == 0.001 BTC
FEE = 500 # Satoshi == 0.0000005 BTC
with Wallet.from_private_key("fff...") as wallet:
utxos = wallet.utxos()
txid = (
Transaction(network=wallet.network)
.add_input(utxos[0])
.add_output(RECEIVER, to_btc(AMOUNT))
.fee(to_btc(FEE))
# .estimate_fee()
.sign(wallet)
.broadcast()
)
print(f"Broadcasted TX ID: {txid}")
3.3 Low-Level Scripting
from brbitcoin import Wallet, Script, Transaction
# Create a P2SH lock script
lock_script = (
Script()
.push_op_dup()
.push_op_hash_160()
.push_bytes(pubkey_hash)
.push_op_equal_verify()
.push_op_check_sig()
)
with Wallet(network=Network.REGTEST) as wallet:
inputs = wallet.utxos()
AMOUNT = 0.0001 # BTC
txid = (
Transaction(network=Network.REGTEST)
.add_input(inputs[0])
.add_output_script(lock_script, AMOUNT)
.sign(wallet)
.broadcast()
)
print(f"Broadcasted TX ID: {txid}")
4. Security Practices
4.1 Encrypted Private Key Backup
from brbitcoin import Wallet
with Wallet.create() as wallet:
wallet.export_encrypted(path="wallet.json",password="pass123")
4.2 Restore from Encrypted backup
from brbitcoin import Wallet
with Wallet.from_encrypted("wallet.json", password="pass123") as wallet:
print(f"Recovered address: {w.address}")
4.3 Zeroization Guarantees
# Keys are wiped:
# - When context manager exits
# - After signing/broadcast
# - On object destruction
with Wallet.from_private_key("c0ffee...") as wallet:
txid = wallet.send("bc1q...", 0.001)
# Key no longer in memory here
5. Node Management
5.1 Network Configuration
from brbitcoin import ClientNode
# Connect to Bitcoin Core
client = ClientNode(
network=Network.REGTEST,
rpc_user="user",
rpc_password="pass",
host="localhost",
port=18444
)
5.2 Node Operations
# Get blockchain info
info = client.get_blockchain_info()
print(f"Blocks: {info.blocks}, Difficulty: {info.difficulty}")
# Generate regtest blocks
if client.network == Network.REGTEST:
blocks = client.generate_to_address(10, "bcrt1q...")
print(f"Mined block: {blocks[-1]}")
# Get fee estimates
fees = electrum_client.estimate_fee(targets=[1, 3, 6])
print(f"1-block fee: {fees[1]} BTC/kvB")
5.3 Direct RPC Access
# Raw RPC commands
mempool = client.rpc("getmempoolinfo")
print(f"Mempool size: {mempool['size']}")
# Batch requests
results = client.batch_rpc([
("getblockcount", []),
("getblockhash", [0]),
("getblockheader", ["000000000019d6..."])
])
print(f"Block count: {results[0]}")
5.4 Bitcoin Core RPC Command Reference (Partial)
| Category | Command | Description | Example Usage |
|---|---|---|---|
| Blockchain | getblockchaininfo |
Returns blockchain state | getblockchaininfo |
getblock |
Get block data by hash/height | getblock "blockhash" 2 |
|
gettxoutsetinfo |
UTXO set statistics | gettxoutsetinfo |
|
| Wallet | listtransactions |
Wallet transaction history | listtransactions "*" 10 0 |
sendtoaddress |
Send to Bitcoin address | sendtoaddress "addr" 0.01 |
|
backupwallet |
Backup wallet.dat | backupwallet "/path/backup.dat" |
|
| Network | getnetworkinfo |
Network connections/version | getnetworkinfo |
addnode |
Manage peer connections | addnode "ip:port" "add" |
|
| Mining | getblocktemplate |
Get mining template | getblocktemplate {"rules":["segwit"]} |
submitblock |
Submit mined block | submitblock "hexdata" |
|
| Utility | validateaddress |
Validate address | validateaddress "bc1q..." |
estimatesmartfee |
Estimate transaction fee | estimatesmartfee 6 |
|
| Raw Tx | createrawtransaction |
Create raw transaction | createrawtransaction '[{"txid":"...","vout":0}]' '{"addr":0.01}' |
signrawtransaction |
Sign raw transaction | signrawtransaction "hex" |
|
| Control | stop |
Shut down node | stop |
uptime |
Node uptime | uptime |
Metadata
Release files for brbitcoin 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| brbitcoin-0.1.0.tar.gz | 3.6 kB | Without provenance | Details for brbitcoin-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| brbitcoin-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for brbitcoin-0.1.0-py3-none-any.whl |
Total release size: 7.4 kB
Release files / brbitcoin-0.1.0.tar.gz
| Download URL | brbitcoin-0.1.0.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d68dbc8adc32b229908c03890e560664fc9bb98315e7070ac5a35fb5394d05a6
|
|
BLAKE2b-256 checksum How to use checksums |
cf155afc2e7bfb1e79727791a285f245d540f5df40c963f6c42830e45df4023f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.9.20 Darwin/24.1.0
|
Release files / brbitcoin-0.1.0-py3-none-any.whl
| Download URL | brbitcoin-0.1.0-py3-none-any.whl |
|---|---|
| Size | 3.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
22a8abd66e83876bba0b859f7b69358b6686551afefb6784b390ae1965c99ebc
|
|
BLAKE2b-256 checksum How to use checksums |
6ab149ae5672bf725a82aa2ce3b6c551b6a28f85b7c0b216a3a0ef74f1c77bf6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.9.20 Darwin/24.1.0
|
Security for brbitcoin 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.