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 a very early version of the library (v0.0.2) and currently, it only supports private/public keys, addresses and creation of P2PKH transactions. More 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.


$ pip install bitcoin-utils

Keys and Addresses Examples

Please explore the API documentation for supported functionality.

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.

>>> from bitcoinutils.keys import PrivateKey, PublicKey
>>> from bitcoinutils.setup import setup, get_network
>>> setup('mainnet')
>>> p = PrivateKey()
>>> p.to_wif()
>>> pubkey = p.get_public_key()
>>> pubkey.to_hex()
>>> pubkey.to_hex(False)
>>> a = pubkey.get_address()
>>> a.to_hash160()
>>> a.to_address()
>>> s = p.sign_message('test')
>>> s
>>> PublicKey.verify_message(a.to_address(), s, 'test')

Create P2PKH Transaction

Please explore the API documentation for supported functionality.

Creates a simple transaction with one input and two outputs.

>>> from bitcoinutils.setup import setup, get_network
>>> from bitcoinutils.transactions import Transaction, TxInput, TxOutput
>>> from bitcoinutils.keys import Address, PrivateKey
>>> setup('testnet')
>>> txin = TxInput('fb48f4e23bf6ddf606714141ac78c3e921c8c0bebeb7c8abb2c799e9ff96ce6c', 0)
>>> addr = Address('n4bkvTyU1dVdzsrhWBqBw8fEMbHjJvtmJR')
>>> txout = TxOutput(0.1, ['OP_DUP', 'OP_HASH160', addr.to_hash160(), 'OP_EQUALVERIFY', 'OP_CHECKSIG']    )
>>> change_addr = Address('mmYNBho9BWQB2dSniP1NJvnPoj5EVWw89w')
>>> change_txout = TxOutput(0.29, ['OP_DUP', 'OP_HASH160', change_addr.to_hash160(), 'OP_EQUALVERIFY',     'OP_CHECKSIG'])
>>> tx = Transaction([txin], [txout, change_txout])
>>> print(tx.serialize())
>>> sk = PrivateKey('cRvyLwCPLU88jsyj94L7iJjQX5C2f8koG4G2gevN4BeSGcEvfKe9')
>>> from_addr = Address('myPAE9HwPeKHh8FjKwBNBaHnemApo3dw6e')
>>> sig = sk.sign_input(tx, 0, ['OP_DUP', 'OP_HASH160', from_addr.to_hash160(), 'OP_EQUALVERIFY', 'OP_CHECKSIG'])
>>> pk = sk.get_public_key()
>>> pk = pk.to_hex()
>>> txin.script_sig = [sig, pk]
>>> signed_tx = tx.serialize()
>>> print(signed_tx)

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.0.2.tar.gz (15.7 kB view hashes)

Uploaded Source

Built Distribution

bitcoin_utils-0.0.2-py3-none-any.whl (16.3 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