Skip to main content

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.4.11) and currently, it supports private/public keys, all type of addresses and creation of any transaction (incl. segwit) with all SIGHASH types. All script op codes are included. Timelock and non-standanrd transactions are supported. Currently, a simple node proxy exists to enable easy calls to a Bitcoin core node. 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](https://github.com/karask/bitcoin-textbook).

Installation

Python version 3 is required. Then just install with:

$ pip install bitcoin-utils

Examples

Keys and Addresses

https://github.com/karask/python-bitcoin-utils/blob/master/examples/keys_addresses.py - 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.

Segwit Addresses

https://github.com/karask/python-bitcoin-utils/blob/master/examples/keys_segwit_addresses.py - creates P2WPKH, P2SH-P2WPKH, P2WSH and P2SH-P2WSH addresses.

Transaction with P2PKH input and outputs

https://github.com/karask/python-bitcoin-utils/blob/master/examples/p2pkh_transaction.py - creates a simple transaction with one input and two outputs.

Create a P2PKH Transaction with different SIGHASHes

https://github.com/karask/python-bitcoin-utils/blob/master/examples/multi_input_sighash_transaction.py - creates a 2-input 2-output transaction with different signature types.

Create a P2SH Address

https://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2sh_transaction.py - creates a P2SH address that corresponds to a P2PK redeem script and sends some funds to it.

Create (spent) a P2SH Transaction

https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2sh_transaction.py - creates a transaction that spends a P2SH output.

Create a non-standard tx

https://github.com/karask/python-bitcoin-utils/blob/master/examples/create_non_std_tx.py - sends funds to an address with a non-standard tx (script: OP_ADD OP_5 OP_EQUAL)

Spend a non-standard tx

https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_non_std_tx.py - spends funds from script OP_ADD OP_5 OP_EQUAL

Transaction to pay to a P2WPKH

http://github.com/karask/python-bitcoin-utils/blob/master/examples/send_to_p2wpkh_transaction.py - send coins from two P2PKH UTXOs to a native segwit address (P2WPKH)

Spend from a P2SH(P2WPKH) nested segwit address

http://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2sh_p2wpkh_address.py - spend a P2WPKH that is nested into a P2SH for old client compatibility

Create a P2SH address with a relative timelock

https://github.com/karask/python-bitcoin-utils/blob/master/examples/create_p2sh_csv_p2pkh_address.py - 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

https://github.com/karask/python-bitcoin-utils/blob/master/examples/spend_p2sh_csv_p2pkh.py - spends from a P2SH(CSV+P2PKH) address as created from above.

Use NodeProxy to make calls to a Bitcoin node

https://github.com/karask/python-bitcoin-utils/blob/master/examples/node_proxy.py - make Bitcoin command-line interface calls programmatically (NodeProxy wraps jsonrpc-requests library)

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

Release files for bitcoin-utils-fork-minimal 0.4.11.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bitcoin-utils-fork-minimal 0.4.11.6
File Size Uploaded
bitcoin-utils-fork-minimal-0.4.11.6.tar.gz 22.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bitcoin-utils-fork-minimal 0.4.11.6
File Interpreter ABI Platform
bitcoin_utils_fork_minimal-0.4.11.6-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 49.0 kB

Release files / bitcoin-utils-fork-minimal-0.4.11.6.tar.gz

Download URL bitcoin-utils-fork-minimal-0.4.11.6.tar.gz
Size 22.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0f389bbc2f2aaffe729dce7d71f141eed06665690fb3f84a87e7b9382e25704c
BLAKE2b-256 checksum
How to use checksums
1da164e0a8c81b1ebb6b56b3a1ae8c949b2fbddd94a1cfb30e2817a889b8cc7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / bitcoin_utils_fork_minimal-0.4.11.6-py2.py3-none-any.whl

Download URL bitcoin_utils_fork_minimal-0.4.11.6-py2.py3-none-any.whl
Size 26.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8584d42b7d59784c15673683a7cf9f769916d273f241636f0f4487b33041e6be
BLAKE2b-256 checksum
How to use checksums
e3b895d9fd3ff6e4619ce0440102b1d45278b4382c0de04af30c392080b28ba0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.4.11.6 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page