bitcoin-utils

raw JSON →
0.8.1 verified Fri May 01 auth: no python

A Python library for Bitcoin utility functions, including key generation, transaction building, and script manipulation. Version 0.8.1 is the latest release. Development and release cadence are intermittent.

pip install bitcoin-utils
error bitcoinutils.exceptions.AddressError: Invalid address checksum
cause Using an address from a different network (mainnet vs testnet) without setting the correct network.
fix
Call setup('testnet') for testnet addresses or setup('mainnet') for mainnet addresses.
error bitcoinutils.setup.NetworkSetupError: Already setup on 'mainnet'
cause Attempting to call setup() more than once.
fix
Do not call setup() multiple times. Alternatively, check if already set up or use a single call at the start.
breaking Network setup must be called before any other operations. Failure to call setup() leads to cryptic errors.
fix Always call setup('mainnet') or setup('testnet') or setup('regtest') first.
gotcha PrivateKey generates new random keys by default. To recover from a WIF, use PrivateKey.from_wif().
fix Use PrivateKey.from_wif('L...') instead of PrivateKey()

Demonstrates basic setup, key generation, and P2PKH address creation on regtest.

from bitcoinutils.setup import setup
from bitcoinutils.keys import PrivateKey
from bitcoinutils.utils import BitcoinAddress
from bitcoinutils.script import Script

# Setup network
setup('regtest')

# Generate private key
priv = PrivateKey()
print('Private key:', priv.to_wif())

# Get public key
pub = priv.get_public_key()
print('Public key:', pub.to_hex())

# Generate P2PKH address
addr = pub.get_address()
print('Address:', addr.to_string())