Skip to main content

bitcoin-blockchain-parser Build Status Coverage Status

This Python 3 library provides a parser for the raw data stored by bitcoind.

Features

  • Detects outputs types
  • Detects addresses in outputs
  • Interprets scripts
  • Supports SegWit and Taproot
  • Supports ordered block parsing

Installing

Whether installing using Pip or from source, plyvel requires leveldb development libraries for LevelDB >1.2.X.

On Linux, install libleveldb-dev

sudo apt-get install libleveldb-dev

Using pip

pip install blockchain-parser-2

Using source

Requirements : python-bitcoinlib, plyvel, coverage for tests

Install dependencies contained in requirements.txt:

pip install -r requirements.txt

Then, just run

python setup.py install

Developing

First, setup a virtualenv and install dependencies:

virtualenv -p python3 .venv
source .venv/bin/activate
pip install -r requirements.txt

Run the test suite by lauching

pytest

Examples

Below are two basic examples for parsing the blockchain. More examples are available in the examples directory.

Unordered Blocks

This blockchain parser parses raw blocks saved in Bitcoin Core's .blk file format. Bitcoin Core does not guarantee that these blocks are saved in order. If your application does not require that blocks are parsed in order, the Blockchain.get_unordered_blocks(...) method can be used:

import os
from blockchain_parser.blockchain import Blockchain

# Instantiate the Blockchain by giving the path to the directory
# containing the .blk files created by bitcoind
blockchain = Blockchain(os.path.expanduser('~/.bitcoin/blocks'))
for block in blockchain.get_unordered_blocks():
    for tx in block.transactions:
        for no, output in enumerate(tx.outputs):
            print("tx=%s outputno=%d type=%s value=%s" % (tx.hash, no, output.type, output.value))

Ordered Blocks

If maintaining block order is necessary for your application, you should use the Blockchain.get_ordered_blocks(...) method. This method uses Bitcoin Core's LevelDB index to locate ordered block data in it's .blk files.

import os
from blockchain_parser.blockchain import Blockchain

# To get the blocks ordered by height, you need to provide the path of the
# `index` directory (LevelDB index) being maintained by bitcoind. It contains
# .ldb files and is present inside the `blocks` directory.
blockchain = Blockchain(os.path.expanduser('~/.bitcoin/blocks'))
for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), end=1000):
    print("height=%d block=%s" % (block.height, block.hash))

Blocks can be iterated in reverse by specifying a start parameter that is greater than the end parameter.

for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), start=510000, end=0):
    print("height=%d block=%s" % (block.height, block.hash))

Building the LevelDB index can take a while which can make iterative development and debugging challenging. For this reason, Blockchain.get_ordered_blocks(...) supports caching the LevelDB index database using pickle. To use a cache simply pass cache=filename to the ordered blocks method. If the cached file does not exist it will be created for faster parsing the next time the method is run. If the cached file already exists it will be used instead of re-parsing the LevelDB database.

for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), cache='index-cache.pickle'):
    print("height=%d block=%s" % (block.height, block.hash))

NOTE: You must manually/programmatically delete the cache file in order to rebuild the cache. Don't forget to do this each time you would like to re-parse the blockchain with a higher block height than the first time you saved the cache file as the new blocks will not be included in the cache.

Metadata

Release files for blockchain-parser-2 0.1.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 blockchain-parser-2 0.1.6
File Size Uploaded Provenance
blockchain_parser_2-0.1.6.tar.gz 39.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for blockchain-parser-2 0.1.6
File Interpreter ABI Platform
blockchain_parser_2-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 88.7 kB

Release files / blockchain_parser_2-0.1.6.tar.gz

Download URL blockchain_parser_2-0.1.6.tar.gz
Size 39.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a42617451de50fa50b85aad1e5c750255cf2edb3a35d9549f469781cd2f4e337
BLAKE2b-256 checksum
How to use checksums
4b6497b91f9cea12dceb2e07e776d1216f3cfc06ff6b0099fb1c02f1c7b2c61f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / blockchain_parser_2-0.1.6-py3-none-any.whl

Download URL blockchain_parser_2-0.1.6-py3-none-any.whl
Size 49.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3eae2bf550c0ccdb0356b772eb4f642e758ee78c4ddaab756ccd19b305b64fbb
BLAKE2b-256 checksum
How to use checksums
90373a500559a6c820d7c738173d8bf240015aad2c02c5889131ce33605e9003
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Security for blockchain-parser-2 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files No provenance

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