Skip to main content

Bitcoin utility functions

Project description

This is a bitcoin library that provides tools/utilities to interact with the Bitcoin network. One of the primary goals of the library is to explain the low-level details of Bitcoin. The code is easy to read and properly documented explaining in detail all the thorny aspects of the implementation. It is a low-level library which assumes some high-level understanding of how Bitcoin works. In the future this might change.

This is an early version of the library (v0.6.1) and currently, it supports private/public keys, all type of addresses and creation of any transaction, incl. segwit and taproot default key path, with all SIGHASH types. All script op codes are included. Timelocks and non-standard transactions are supported. Extra functionality will be added continuously and the documentation will be improved as the work progresses.

The API documentation can be build with Sphinx but is also available as a PDF for convenience. One can currently use the library for experimenting and learning the inner workings of Bitcoin. It is not meant for production yet and parts of the API might be updated with new versions.

Complementary to this library is a CC BY-SA 4.0 licensed Bitcoin programming book.


  • For schnorr, bech32[m], ripemd160 the python Bitcoin Core reference implementations are used.

  • For making calls to a Bitcoin node a simple node proxy object exists, which wraps the python-bitcoinrpc library.

  • For Hierarchical Deterministic keys we wrap the python hdwallet library. For now we wrap only some very basic functionality to acquire a PrivateKey object that is used throughtout the library.


Python version 3 is required. Then just install with:

$ pip install bitcoin-utils


Keys and Addresses - creates a private key which we use to derive a public key and in turn an address. We also use the private key to sign a message and then verify it using the public key.

Hierarchical Deterministic Keys - creates an extended private key, from an xpriv/tpriv and path, which we use to derive a public key and in turn all different address.

Segwit Addresses - creates P2WPKH, P2SH-P2WPKH, P2WSH and P2SH-P2WSH addresses.

Transaction with P2PKH input and outputs - creates a simple transaction with one input and two outputs.

Create a P2PKH Transaction with different SIGHASHes - creates a 2-input 2-output transaction with different signature types.

Create a P2SH Address - creates a P2SH address that corresponds to a P2PK redeem script and sends some funds to it.

Create (spent) a P2SH Transaction - creates a transaction that spends a P2SH output.

Create a non-standard tx - sends funds to an address with a non-standard tx (script: OP_ADD OP_5 OP_EQUAL)

Spend a non-standard tx - spends funds from script OP_ADD OP_5 OP_EQUAL

Transaction to pay to a P2WPKH - send coins from two P2PKH UTXOs to a native segwit address (P2WPKH)

Spend from a P2SH(P2WPKH) nested segwit address - spend a P2WPKH that is nested into a P2SH for old client compatibility

Create a P2SH address with a relative timelock - creates a P2SH address that locks funds (sent to it) with a private key (P2PKH) and a relative locktime of 200 blocks in the future.

Spend from a timelocked address - spends from a P2SH(CSV+P2PKH) address as created from above.

Use NodeProxy to make calls to a Bitcoin node - make Bitcoin command-line interface calls programmatically (NodeProxy wraps jsonrpc-requests library)

Spend from a taproot address - single input, single output default key path spending.

Spend a multi input that contains both taproot and legacy UTXOs - three inputs (two taproot and one legacy), single legacy output.

Please explore the codebase or the API documentation (BitcoinUtilities.pdf) for supported functionality and other options.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bitcoin-utils-0.6.1.tar.gz (35.5 kB view hashes)

Uploaded Source

Built Distribution

bitcoin_utils-0.6.1-py3-none-any.whl (39.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page