From 676ee04ddc25e022e98003bbdb531f6387d945ca Mon Sep 17 00:00:00 2001 From: Chris Priest Date: Fri, 21 Apr 2017 20:23:20 -0600 Subject: [PATCH 1/3] made 'message magic bytes' not hardcoded, so it works with altcoins. --- blockchain_parser/blockchain.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/blockchain_parser/blockchain.py b/blockchain_parser/blockchain.py index ba81237..5803c22 100644 --- a/blockchain_parser/blockchain.py +++ b/blockchain_parser/blockchain.py @@ -16,10 +16,6 @@ from .block import Block -# Constant separating blocks in the .blk files -BITCOIN_CONSTANT = b"\xf9\xbe\xb4\xd9" - - def get_files(path): """ Given the path to the .bitcoin directory, returns the sorted list of .blk @@ -31,7 +27,7 @@ def get_files(path): return sorted(files) -def get_blocks(blockfile): +def get_blocks(blockfile, pchMessageStart=b"\xf9\xbe\xb4\xd9"): """ Given the name of a .blk file, for every block contained in the file, yields its raw hexadecimal value @@ -47,7 +43,7 @@ def get_blocks(blockfile): offset = 0 block_count = 0 while offset < (length - 4): - if raw_data[offset:offset+4] == BITCOIN_CONSTANT: + if raw_data[offset:offset+4] == pchMessageStart: offset += 4 size = struct.unpack(" Date: Fri, 21 Apr 2017 20:25:06 -0600 Subject: [PATCH 2/3] added pchMessageStart to blockchain class --- blockchain_parser/blockchain.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/blockchain_parser/blockchain.py b/blockchain_parser/blockchain.py index 5803c22..5b4a964 100644 --- a/blockchain_parser/blockchain.py +++ b/blockchain_parser/blockchain.py @@ -59,13 +59,14 @@ class Blockchain(object): maintained by bitcoind. """ - def __init__(self, path): + def __init__(self, path, pchMessageStart=b"\xf9\xbe\xb4\xd9"): self.path = path + self.pchMessageStart = pchMessageStart def get_unordered_blocks(self): """Yields the blocks contained in the .blk files as is, without ordering them according to height. """ for blk_file in get_files(self.path): - for raw_block in get_blocks(blk_file): + for raw_block in get_blocks(blk_file, pchMessageStart=self.pchMessageStart): yield Block(raw_block) From 78b42992ad7d15e6bb2fa92ebf893ecd8b07d396 Mon Sep 17 00:00:00 2001 From: Chris Priest Date: Sat, 22 Apr 2017 09:38:54 -0600 Subject: [PATCH 3/3] updated readme --- README.md | 52 ++++++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 709ab38..4efbbf6 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,23 @@ -# bitcoin-blockchain-parser [![Build Status](https://travis-ci.org/alecalve/python-bitcoin-blockchain-parser.svg?branch=master)](https://travis-ci.org/alecalve/python-bitcoin-blockchain-parser) [![Coverage Status](https://coveralls.io/repos/alecalve/python-bitcoin-blockchain-parser/badge.svg?branch=master&service=github)](https://coveralls.io/github/alecalve/python-bitcoin-blockchain-parser?branch=master) -This Python 3 library provides a parser for the raw data stored by bitcoind. +This is a fork of the `bitcoin-blockchain-parser` project found here: +https://github.com/alecalve/python-bitcoin-blockchain-parser -## Features -- Detects outputs types -- Detects addresses in outputs -- Interprets scripts +Differences include: Modifications to make parsing work with altcoins such as +Dash, Dogecoin, Litecoin and Feathercoin. -## Examples +To use with litecoin: -```python -import sys -from blockchain_parser.blockchain import Blockchain + >>> ltc=Blockchain('/media/chris/3033-6537/litecoin/blocks', b"\xfb\xc0\xb6\xdb") + >>> next(ltc.get_unordered_blocks()) + Block(12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2) -# Instanciate the Blockchain by giving the path to the directory -# containing the .blk files created by bitcoind -blockchain = Blockchain(sys.argv[1]) -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)) -``` - -More examples are available in the examples directory. - -## Installing - -Requirements : python-bitcoinlib, coverage for tests - -To install, just run -``` -python setup.py install -``` - -## Tests - -Run the test suite by lauching -``` -./tests.sh -``` +To use with bitcoin: + >>> btc=Blockchain('/home/chris/.bitcoin/blocks/') + >>> next(btc.get_unordered_blocks()) + Block(000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f) +and dash: + >>> dash=Blockchain('/media/chris/3033-6537/dash/blocks', b"\xbf\x0c\x6b\xbd") + >>> next(dash.get_unordered_blocks()) + Block(089fc444b06edd0f70d9fda85f9a3b2e22e549b354a1bdb210ce7804c69eb0a4)