Skip to main content

Python library created for teaching and researching purposes.

Project description

bitcoin_tools

Mentioned in Awesome

bitcoin_tools is a Python library created for teaching and researching purposes. It's main objective is twofold. First it aims to ease the understanding of Bitcoin transaction creation, by using well-documented and easy to understand python code. Second, it aims to provide a tool able to create custom transactions / scripts. Either scriptSig and scriptPubKey can be built from human readable strings created using Script syntax. Finally, tools for accessing and analysing interesting data such as the utxo set are also provided, along with several examples.

bitcoin_tools allows you to:

  • Bitcoin keys creation and management.
  • Creation of Bitcoin transactions from scratch.
  • Customize any field of your transaction.
  • Transaction serialization / deserialization.
  • Creation of standard and custom scripts (scriptSig and scriptPubKey).
  • Transaction analysis from hex encoded transactions.

Additionally, bitcoin_tools contains STATUS an STatistical Analysis Tool for Utxo Set under analysis/status

Dependencies

Refer to DEPENCENCIES.md

Installation

Refer to INSTALL.md

Some trouble getting started with the repo?

Refer to FAQ.md

Still not working?

Feel free to open an issue.

Examples

Down below you can find some examples of how to use some of the library functions. More examples can be found in examples/

Key management and Bitcoin address generation

from bitcoin_tools.core.keys import generate_keys, store_keys
from bitcoin_tools.wallet import generate_wif, generate_btc_addr

# First of all the ECDSA keys are generated.
sk, pk = generate_keys()
# Then, the Bitcoin address is derived from the public key created above.
btc_addr = generate_btc_addr(pk)
# Both the public and private key are stored in disk in pem format. The Bitcoin address is used as an identifier in the
# name of the folder that contains both keys.
store_keys(sk.to_pem(), pk.to_pem(), btc_addr)
# Finally, the private key is encoded as WIF and also stored in disk, ready to be imported in a wallet.
generate_wif(btc_addr, sk)

Raw transaction building

from bitcoin_tools.core.keys import load_keys
from bitcoin_tools.core.transaction import TX

# Key loading
btc_addr = "miWdbNn9zDLnKpcNuCLdfRiJx59c93bT8t"
sk, pk = load_keys(btc_addr)

# Reference to the previous transaction output that will be used to redeem and spend the funds, consisting on an id and
# an output index.
prev_tx_id = "7767a9eb2c8adda3ffce86c06689007a903b6f7e78dbc049ef0dbaf9eeebe075"
prev_out_index = 0

# Amount to be spent, in Satoshis, and the fee to be deduced (should be calculated).
value = 6163910
fee = 230 * 240

# Destination Bitcoin address where the value in bitcoins will be sent and locked until the owner redeems it.
destination_btc_addr = "miWdbNn9zDLnKpcNuCLdfRiJx59c93bT8t"

# First, we  build our transaction from io (input/output) using the previous transaction references, the value, and the
# destination.
tx = TX.build_from_io(prev_tx_id, prev_out_index, value - fee, destination_btc_addr)
# Finally, the transaction is signed using the private key associated with the Bitcoin address from each input.
# Input 0 will be signed, since we have only created one.
tx.sign(sk, 0)

# Once created we can display the serialized transaction. Transaction is now ready to be broadcast.
print "hex: " + tx.serialize()

# Finally, we can analyze each field of the transaction.
tx.display()

Raw tx analysis

from bitcoin_tools.core.transaction import TX

# First a transaction object is created (through the deserialize constructor) by deserializing the hex transaction we
# have selected.
hex_tx = "01000000013ca58d2f6fac36602d831ee0cf2bc80031c7472e80a322b57f614c5ce9142b71000000006b483045022100f0331d85cb7f7ec1bedc41f50c695d654489458e88aec0076fbad5d8aeda1673022009e8ca2dda1d6a16bfd7133b0008720145dacccb35c0d5c9fc567e52f26ca5f7012103a164209a7c23227fcd6a71c51efc5b6eb25407f4faf06890f57908425255e42bffffffff0241a20000000000001976a914e44839239ab36f5bc67b2079de00ecf587233ebe88ac74630000000000001976a914dc7016484646168d99e49f907c86c271299441c088ac00000000"
tx = TX.deserialize(hex_tx)

# Then, the transaction can be displayed using the display method to analyze how it's been constructed.
tx.display()

Using STATUS to dump the UTXOs LevelDB

from bitcoin_tools.analysis.status.data_dump import utxo_dump
from bitcoin_tools.analysis.status.utils import parse_ldb

# Set the version of the Bitcoin Core you are using (which defines the chainstate format)
# and the IO files.

f_utxos = "decoded_utxos.txt"
f_parsed_utxos = "parsed_utxos.txt"

# Parse all the data in the chainstate.
parse_ldb(f_utxos)
# Parses transactions and utxos from the dumped data.
utxo_dump(f_utxos, f_parsed_utxos)

# Data is stored in f_utxos and f_parsed_utxos files respectively

Support

If you find this repository useful, show us some love, give us a star!

Small Bitcoin donations to the following address are also welcome:

1srgi8sqPkCKq7gsVfhUZB7dvoi72UsqP

Disclaimer

This library allow you to modify any transaction field as you pleased. However, some modifications can make your transactions non-standard, or even non-spendable. We totally discourage the use of the library outside the testnet if you are not sure about what you are doing, specially when dealing with non-standard scripts. A bad use of the library can lead you to lose some of your bitcoins.

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

python_bitcoin_tools-0.2.2.tar.gz (44.4 kB view details)

Uploaded Source

Built Distribution

python_bitcoin_tools-0.2.2-py2-none-any.whl (55.0 kB view details)

Uploaded Python 2

File details

Details for the file python_bitcoin_tools-0.2.2.tar.gz.

File metadata

  • Download URL: python_bitcoin_tools-0.2.2.tar.gz
  • Upload date:
  • Size: 44.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.8 CPython/2.7.10

File hashes

Hashes for python_bitcoin_tools-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a2016ecb667bbe55dcfaf14dd971bd38d7eaab6944b6c13874204b3f54fd0e79
MD5 467c9741aa7c338da1d905c6712768c7
BLAKE2b-256 646e226b7e5881503498ba220e590441aea68fe37e4e47fd5a5ff1b6918cce5d

See more details on using hashes here.

File details

Details for the file python_bitcoin_tools-0.2.2-py2-none-any.whl.

File metadata

  • Download URL: python_bitcoin_tools-0.2.2-py2-none-any.whl
  • Upload date:
  • Size: 55.0 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.8 CPython/2.7.10

File hashes

Hashes for python_bitcoin_tools-0.2.2-py2-none-any.whl
Algorithm Hash digest
SHA256 71909d8acb10d694149095c6b05c96efb9e76b8433b803c6c2de0844a1416270
MD5 071e596868a50cdb3ffd271e9144709f
BLAKE2b-256 ad1bed4411f1f12ba9de617a9e890f77bb93570b13f55cdad66d757b90100407

See more details on using hashes here.

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