blockchain-parser-2 0.1.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- blockchain_parser_2-0.1.6/LICENSE +177 -0
- blockchain_parser_2-0.1.6/MANIFEST.in +9 -0
- blockchain_parser_2-0.1.6/PKG-INFO +143 -0
- blockchain_parser_2-0.1.6/README.md +109 -0
- blockchain_parser_2-0.1.6/blockchain_parser/__init__.py +12 -0
- blockchain_parser_2-0.1.6/blockchain_parser/address.py +83 -0
- blockchain_parser_2-0.1.6/blockchain_parser/block.py +104 -0
- blockchain_parser_2-0.1.6/blockchain_parser/block_header.py +90 -0
- blockchain_parser_2-0.1.6/blockchain_parser/blockchain.py +302 -0
- blockchain_parser_2-0.1.6/blockchain_parser/index.py +81 -0
- blockchain_parser_2-0.1.6/blockchain_parser/input.py +78 -0
- blockchain_parser_2-0.1.6/blockchain_parser/output.py +145 -0
- blockchain_parser_2-0.1.6/blockchain_parser/script.py +164 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/__init__.py +10 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/data/bech32_p2wpkh.txt +1 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/data/bech32_p2wsh.txt +1 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/data/bip69_false.txt +1 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/data/bip69_true.txt +1 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/data/genesis_block.txt +1 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/data/invalid_tx.txt +1 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/data/large_tx.txt +1 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/data/scripts_invalid.txt +1 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/data/segwit.txt +1 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/data/size_non_segwit.txt +1 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/data/size_segwit.txt +1 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/test_address.py +45 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/test_block.py +55 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/test_blockchain.py +79 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/test_index.py +48 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/test_output.py +66 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/test_script.py +171 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/test_transaction.py +138 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/test_undo.py +70 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/test_utils.py +55 -0
- blockchain_parser_2-0.1.6/blockchain_parser/tests/utils.py +9 -0
- blockchain_parser_2-0.1.6/blockchain_parser/transaction.py +196 -0
- blockchain_parser_2-0.1.6/blockchain_parser/undo.py +198 -0
- blockchain_parser_2-0.1.6/blockchain_parser/utils.py +152 -0
- blockchain_parser_2-0.1.6/blockchain_parser/utils_taproot.py +143 -0
- blockchain_parser_2-0.1.6/blockchain_parser_2.egg-info/PKG-INFO +143 -0
- blockchain_parser_2-0.1.6/blockchain_parser_2.egg-info/SOURCES.txt +46 -0
- blockchain_parser_2-0.1.6/blockchain_parser_2.egg-info/dependency_links.txt +1 -0
- blockchain_parser_2-0.1.6/blockchain_parser_2.egg-info/requires.txt +3 -0
- blockchain_parser_2-0.1.6/blockchain_parser_2.egg-info/top_level.txt +1 -0
- blockchain_parser_2-0.1.6/pyproject.toml +3 -0
- blockchain_parser_2-0.1.6/requirements.txt +4 -0
- blockchain_parser_2-0.1.6/setup.cfg +4 -0
- blockchain_parser_2-0.1.6/setup.py +51 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
bitcoin-blockchain-parser is free software: you can redistribute it and/or
|
|
2
|
+
modify it under the terms of the GNU Lesser General Public License as
|
|
3
|
+
published by the Free Software Foundation, either version 3 of the License,
|
|
4
|
+
or (at your option) any later version.
|
|
5
|
+
|
|
6
|
+
bitcoin-blockchain-parser is distributed in the hope that it will be useful, but
|
|
7
|
+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
8
|
+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
9
|
+
below for more details.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
14
|
+
Version 3, 29 June 2007
|
|
15
|
+
|
|
16
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
|
17
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
18
|
+
of this license document, but changing it is not allowed.
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
This version of the GNU Lesser General Public License incorporates
|
|
22
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
23
|
+
License, supplemented by the additional permissions listed below.
|
|
24
|
+
|
|
25
|
+
0. Additional Definitions.
|
|
26
|
+
|
|
27
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
28
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
29
|
+
General Public License.
|
|
30
|
+
|
|
31
|
+
"The Library" refers to a covered work governed by this License,
|
|
32
|
+
other than an Application or a Combined Work as defined below.
|
|
33
|
+
|
|
34
|
+
An "Application" is any work that makes use of an interface provided
|
|
35
|
+
by the Library, but which is not otherwise based on the Library.
|
|
36
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
37
|
+
of using an interface provided by the Library.
|
|
38
|
+
|
|
39
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
40
|
+
Application with the Library. The particular version of the Library
|
|
41
|
+
with which the Combined Work was made is also called the "Linked
|
|
42
|
+
Version".
|
|
43
|
+
|
|
44
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
45
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
46
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
47
|
+
based on the Application, and not on the Linked Version.
|
|
48
|
+
|
|
49
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
50
|
+
object code and/or source code for the Application, including any data
|
|
51
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
52
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
53
|
+
|
|
54
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
55
|
+
|
|
56
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
57
|
+
without being bound by section 3 of the GNU GPL.
|
|
58
|
+
|
|
59
|
+
2. Conveying Modified Versions.
|
|
60
|
+
|
|
61
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
62
|
+
facility refers to a function or data to be supplied by an Application
|
|
63
|
+
that uses the facility (other than as an argument passed when the
|
|
64
|
+
facility is invoked), then you may convey a copy of the modified
|
|
65
|
+
version:
|
|
66
|
+
|
|
67
|
+
a) under this License, provided that you make a good faith effort to
|
|
68
|
+
ensure that, in the event an Application does not supply the
|
|
69
|
+
function or data, the facility still operates, and performs
|
|
70
|
+
whatever part of its purpose remains meaningful, or
|
|
71
|
+
|
|
72
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
73
|
+
this License applicable to that copy.
|
|
74
|
+
|
|
75
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
76
|
+
|
|
77
|
+
The object code form of an Application may incorporate material from
|
|
78
|
+
a header file that is part of the Library. You may convey such object
|
|
79
|
+
code under terms of your choice, provided that, if the incorporated
|
|
80
|
+
material is not limited to numerical parameters, data structure
|
|
81
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
82
|
+
(ten or fewer lines in length), you do both of the following:
|
|
83
|
+
|
|
84
|
+
a) Give prominent notice with each copy of the object code that the
|
|
85
|
+
Library is used in it and that the Library and its use are
|
|
86
|
+
covered by this License.
|
|
87
|
+
|
|
88
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
89
|
+
document.
|
|
90
|
+
|
|
91
|
+
4. Combined Works.
|
|
92
|
+
|
|
93
|
+
You may convey a Combined Work under terms of your choice that,
|
|
94
|
+
taken together, effectively do not restrict modification of the
|
|
95
|
+
portions of the Library contained in the Combined Work and reverse
|
|
96
|
+
engineering for debugging such modifications, if you also do each of
|
|
97
|
+
the following:
|
|
98
|
+
|
|
99
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
100
|
+
the Library is used in it and that the Library and its use are
|
|
101
|
+
covered by this License.
|
|
102
|
+
|
|
103
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
104
|
+
document.
|
|
105
|
+
|
|
106
|
+
c) For a Combined Work that displays copyright notices during
|
|
107
|
+
execution, include the copyright notice for the Library among
|
|
108
|
+
these notices, as well as a reference directing the user to the
|
|
109
|
+
copies of the GNU GPL and this license document.
|
|
110
|
+
|
|
111
|
+
d) Do one of the following:
|
|
112
|
+
|
|
113
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
114
|
+
License, and the Corresponding Application Code in a form
|
|
115
|
+
suitable for, and under terms that permit, the user to
|
|
116
|
+
recombine or relink the Application with a modified version of
|
|
117
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
118
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
119
|
+
Corresponding Source.
|
|
120
|
+
|
|
121
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
122
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
123
|
+
a copy of the Library already present on the user's computer
|
|
124
|
+
system, and (b) will operate properly with a modified version
|
|
125
|
+
of the Library that is interface-compatible with the Linked
|
|
126
|
+
Version.
|
|
127
|
+
|
|
128
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
129
|
+
be required to provide such information under section 6 of the
|
|
130
|
+
GNU GPL, and only to the extent that such information is
|
|
131
|
+
necessary to install and execute a modified version of the
|
|
132
|
+
Combined Work produced by recombining or relinking the
|
|
133
|
+
Application with a modified version of the Linked Version. (If
|
|
134
|
+
you use option 4d0, the Installation Information must accompany
|
|
135
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
136
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
137
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
138
|
+
for conveying Corresponding Source.)
|
|
139
|
+
|
|
140
|
+
5. Combined Libraries.
|
|
141
|
+
|
|
142
|
+
You may place library facilities that are a work based on the
|
|
143
|
+
Library side by side in a single library together with other library
|
|
144
|
+
facilities that are not Applications and are not covered by this
|
|
145
|
+
License, and convey such a combined library under terms of your
|
|
146
|
+
choice, if you do both of the following:
|
|
147
|
+
|
|
148
|
+
a) Accompany the combined library with a copy of the same work based
|
|
149
|
+
on the Library, uncombined with any other library facilities,
|
|
150
|
+
conveyed under the terms of this License.
|
|
151
|
+
|
|
152
|
+
b) Give prominent notice with the combined library that part of it
|
|
153
|
+
is a work based on the Library, and explaining where to find the
|
|
154
|
+
accompanying uncombined form of the same work.
|
|
155
|
+
|
|
156
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
157
|
+
|
|
158
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
159
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
160
|
+
versions will be similar in spirit to the present version, but may
|
|
161
|
+
differ in detail to address new problems or concerns.
|
|
162
|
+
|
|
163
|
+
Each version is given a distinguishing version number. If the
|
|
164
|
+
Library as you received it specifies that a certain numbered version
|
|
165
|
+
of the GNU Lesser General Public License "or any later version"
|
|
166
|
+
applies to it, you have the option of following the terms and
|
|
167
|
+
conditions either of that published version or of any later version
|
|
168
|
+
published by the Free Software Foundation. If the Library as you
|
|
169
|
+
received it does not specify a version number of the GNU Lesser
|
|
170
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
171
|
+
General Public License ever published by the Free Software Foundation.
|
|
172
|
+
|
|
173
|
+
If the Library as you received it specifies that a proxy can decide
|
|
174
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
175
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
176
|
+
permanent authorization for you to choose that version for the
|
|
177
|
+
Library.
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: blockchain-parser-2
|
|
3
|
+
Version: 0.1.6
|
|
4
|
+
Summary: Bitcoin blockchain parser
|
|
5
|
+
Home-page: https://github.com/chainlaboratory/blockchain-parser-2.0
|
|
6
|
+
Author: Antoine Le Calvez
|
|
7
|
+
Author-email: antoine@p2sh.info
|
|
8
|
+
License: LGPL-3.0-or-later
|
|
9
|
+
Project-URL: Source, https://github.com/chainlaboratory/blockchain-parser-2.0
|
|
10
|
+
Project-URL: Upstream, https://github.com/alecalve/python-bitcoin-blockchain-parser
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
16
|
+
Requires-Python: >=3.8
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: python-bitcoinlib==0.11.0
|
|
20
|
+
Requires-Dist: plyvel==1.5.1
|
|
21
|
+
Requires-Dist: ripemd-hash==1.0.1
|
|
22
|
+
Dynamic: author
|
|
23
|
+
Dynamic: author-email
|
|
24
|
+
Dynamic: classifier
|
|
25
|
+
Dynamic: description
|
|
26
|
+
Dynamic: description-content-type
|
|
27
|
+
Dynamic: home-page
|
|
28
|
+
Dynamic: license
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
Dynamic: project-url
|
|
31
|
+
Dynamic: requires-dist
|
|
32
|
+
Dynamic: requires-python
|
|
33
|
+
Dynamic: summary
|
|
34
|
+
|
|
35
|
+
# bitcoin-blockchain-parser  [](https://coveralls.io/github/alecalve/python-bitcoin-blockchain-parser?branch=master)
|
|
36
|
+
This Python 3 library provides a parser for the raw data stored by bitcoind.
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
- Detects outputs types
|
|
40
|
+
- Detects addresses in outputs
|
|
41
|
+
- Interprets scripts
|
|
42
|
+
- Supports SegWit and Taproot
|
|
43
|
+
- Supports ordered block parsing
|
|
44
|
+
|
|
45
|
+
## Installing
|
|
46
|
+
|
|
47
|
+
Whether installing using Pip or from source, plyvel requires leveldb development libraries for LevelDB >1.2.X.
|
|
48
|
+
|
|
49
|
+
On Linux, install libleveldb-dev
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
sudo apt-get install libleveldb-dev
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Using pip
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
pip install blockchain-parser-2
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Using source
|
|
62
|
+
|
|
63
|
+
Requirements : python-bitcoinlib, plyvel, coverage for tests
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
Install dependencies contained in `requirements.txt`:
|
|
67
|
+
```
|
|
68
|
+
pip install -r requirements.txt
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Then, just run
|
|
72
|
+
```
|
|
73
|
+
python setup.py install
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Developing
|
|
77
|
+
|
|
78
|
+
First, setup a virtualenv and install dependencies:
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
virtualenv -p python3 .venv
|
|
82
|
+
source .venv/bin/activate
|
|
83
|
+
pip install -r requirements.txt
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Run the test suite by lauching
|
|
87
|
+
```
|
|
88
|
+
pytest
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Examples
|
|
92
|
+
|
|
93
|
+
Below are two basic examples for parsing the blockchain. More examples are available in the examples directory.
|
|
94
|
+
|
|
95
|
+
### Unordered Blocks
|
|
96
|
+
|
|
97
|
+
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:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
import os
|
|
101
|
+
from blockchain_parser.blockchain import Blockchain
|
|
102
|
+
|
|
103
|
+
# Instantiate the Blockchain by giving the path to the directory
|
|
104
|
+
# containing the .blk files created by bitcoind
|
|
105
|
+
blockchain = Blockchain(os.path.expanduser('~/.bitcoin/blocks'))
|
|
106
|
+
for block in blockchain.get_unordered_blocks():
|
|
107
|
+
for tx in block.transactions:
|
|
108
|
+
for no, output in enumerate(tx.outputs):
|
|
109
|
+
print("tx=%s outputno=%d type=%s value=%s" % (tx.hash, no, output.type, output.value))
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Ordered Blocks
|
|
113
|
+
|
|
114
|
+
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.
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
import os
|
|
118
|
+
from blockchain_parser.blockchain import Blockchain
|
|
119
|
+
|
|
120
|
+
# To get the blocks ordered by height, you need to provide the path of the
|
|
121
|
+
# `index` directory (LevelDB index) being maintained by bitcoind. It contains
|
|
122
|
+
# .ldb files and is present inside the `blocks` directory.
|
|
123
|
+
blockchain = Blockchain(os.path.expanduser('~/.bitcoin/blocks'))
|
|
124
|
+
for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), end=1000):
|
|
125
|
+
print("height=%d block=%s" % (block.height, block.hash))
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Blocks can be iterated in reverse by specifying a start parameter that is greater than the end parameter.
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), start=510000, end=0):
|
|
132
|
+
print("height=%d block=%s" % (block.height, block.hash))
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
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](https://docs.python.org/3.6/library/pickle.html). 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.
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), cache='index-cache.pickle'):
|
|
139
|
+
print("height=%d block=%s" % (block.height, block.hash))
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
**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.
|
|
143
|
+
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# bitcoin-blockchain-parser  [](https://coveralls.io/github/alecalve/python-bitcoin-blockchain-parser?branch=master)
|
|
2
|
+
This Python 3 library provides a parser for the raw data stored by bitcoind.
|
|
3
|
+
|
|
4
|
+
## Features
|
|
5
|
+
- Detects outputs types
|
|
6
|
+
- Detects addresses in outputs
|
|
7
|
+
- Interprets scripts
|
|
8
|
+
- Supports SegWit and Taproot
|
|
9
|
+
- Supports ordered block parsing
|
|
10
|
+
|
|
11
|
+
## Installing
|
|
12
|
+
|
|
13
|
+
Whether installing using Pip or from source, plyvel requires leveldb development libraries for LevelDB >1.2.X.
|
|
14
|
+
|
|
15
|
+
On Linux, install libleveldb-dev
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
sudo apt-get install libleveldb-dev
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Using pip
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
pip install blockchain-parser-2
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Using source
|
|
28
|
+
|
|
29
|
+
Requirements : python-bitcoinlib, plyvel, coverage for tests
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
Install dependencies contained in `requirements.txt`:
|
|
33
|
+
```
|
|
34
|
+
pip install -r requirements.txt
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Then, just run
|
|
38
|
+
```
|
|
39
|
+
python setup.py install
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Developing
|
|
43
|
+
|
|
44
|
+
First, setup a virtualenv and install dependencies:
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
virtualenv -p python3 .venv
|
|
48
|
+
source .venv/bin/activate
|
|
49
|
+
pip install -r requirements.txt
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Run the test suite by lauching
|
|
53
|
+
```
|
|
54
|
+
pytest
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Examples
|
|
58
|
+
|
|
59
|
+
Below are two basic examples for parsing the blockchain. More examples are available in the examples directory.
|
|
60
|
+
|
|
61
|
+
### Unordered Blocks
|
|
62
|
+
|
|
63
|
+
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:
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
import os
|
|
67
|
+
from blockchain_parser.blockchain import Blockchain
|
|
68
|
+
|
|
69
|
+
# Instantiate the Blockchain by giving the path to the directory
|
|
70
|
+
# containing the .blk files created by bitcoind
|
|
71
|
+
blockchain = Blockchain(os.path.expanduser('~/.bitcoin/blocks'))
|
|
72
|
+
for block in blockchain.get_unordered_blocks():
|
|
73
|
+
for tx in block.transactions:
|
|
74
|
+
for no, output in enumerate(tx.outputs):
|
|
75
|
+
print("tx=%s outputno=%d type=%s value=%s" % (tx.hash, no, output.type, output.value))
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Ordered Blocks
|
|
79
|
+
|
|
80
|
+
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.
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import os
|
|
84
|
+
from blockchain_parser.blockchain import Blockchain
|
|
85
|
+
|
|
86
|
+
# To get the blocks ordered by height, you need to provide the path of the
|
|
87
|
+
# `index` directory (LevelDB index) being maintained by bitcoind. It contains
|
|
88
|
+
# .ldb files and is present inside the `blocks` directory.
|
|
89
|
+
blockchain = Blockchain(os.path.expanduser('~/.bitcoin/blocks'))
|
|
90
|
+
for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), end=1000):
|
|
91
|
+
print("height=%d block=%s" % (block.height, block.hash))
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Blocks can be iterated in reverse by specifying a start parameter that is greater than the end parameter.
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), start=510000, end=0):
|
|
98
|
+
print("height=%d block=%s" % (block.height, block.hash))
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
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](https://docs.python.org/3.6/library/pickle.html). 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.
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), cache='index-cache.pickle'):
|
|
105
|
+
print("height=%d block=%s" % (block.height, block.hash))
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**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.
|
|
109
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Copyright (C) 2015-2016 The bitcoin-blockchain-parser developers
|
|
2
|
+
#
|
|
3
|
+
# This file is part of bitcoin-blockchain-parser.
|
|
4
|
+
#
|
|
5
|
+
# It is subject to the license terms in the LICENSE file found in the top-level
|
|
6
|
+
# directory of this distribution.
|
|
7
|
+
#
|
|
8
|
+
# No part of bitcoin-blockchain-parser, including this file, may be copied,
|
|
9
|
+
# modified, propagated, or distributed except according to the terms contained
|
|
10
|
+
# in the LICENSE file.
|
|
11
|
+
|
|
12
|
+
__version__ = "0.1.6"
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# Copyright (C) 2015-2016 The bitcoin-blockchain-parser developers
|
|
2
|
+
#
|
|
3
|
+
# This file is part of bitcoin-blockchain-parser.
|
|
4
|
+
#
|
|
5
|
+
# It is subject to the license terms in the LICENSE file found in the top-level
|
|
6
|
+
# directory of this distribution.
|
|
7
|
+
#
|
|
8
|
+
# No part of bitcoin-blockchain-parser, including this file, may be copied,
|
|
9
|
+
# modified, propagated, or distributed except according to the terms contained
|
|
10
|
+
# in the LICENSE file.
|
|
11
|
+
|
|
12
|
+
from bitcoin import base58
|
|
13
|
+
from bitcoin.bech32 import CBech32Data
|
|
14
|
+
from .utils import btc_ripemd160, double_sha256
|
|
15
|
+
from .utils_taproot import from_taproot
|
|
16
|
+
from binascii import b2a_hex
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Address(object):
|
|
20
|
+
"""Represents a bitcoin address"""
|
|
21
|
+
|
|
22
|
+
def __init__(self, hash, public_key, address, type, segwit_version):
|
|
23
|
+
self._hash = hash
|
|
24
|
+
self.public_key = public_key
|
|
25
|
+
self._address = address
|
|
26
|
+
self.type = type
|
|
27
|
+
self._segwit_version = segwit_version
|
|
28
|
+
|
|
29
|
+
def __repr__(self):
|
|
30
|
+
return "Address(addr=%s)" % self.address
|
|
31
|
+
|
|
32
|
+
@classmethod
|
|
33
|
+
def from_public_key(cls, public_key):
|
|
34
|
+
"""Constructs an Address object from a public key"""
|
|
35
|
+
return cls(None, public_key, None, "normal", None)
|
|
36
|
+
|
|
37
|
+
@classmethod
|
|
38
|
+
def from_ripemd160(cls, hash, type="normal"):
|
|
39
|
+
"""Constructs an Address object from a RIPEMD-160 hash, it may be a
|
|
40
|
+
normal address or a P2SH address, the latter is indicated by setting
|
|
41
|
+
type to 'p2sh'"""
|
|
42
|
+
return cls(hash, None, None, type, None)
|
|
43
|
+
|
|
44
|
+
@classmethod
|
|
45
|
+
def from_bech32(cls, hash, segwit_version):
|
|
46
|
+
"""Constructs an Address object from a bech32 hash."""
|
|
47
|
+
return cls(hash, None, None, "bech32", segwit_version)
|
|
48
|
+
|
|
49
|
+
@classmethod
|
|
50
|
+
def from_bech32m(cls, hash, segwit_version):
|
|
51
|
+
"""Constructs an Address object from a bech32m script."""
|
|
52
|
+
return cls(hash, None, None, "bech32m", segwit_version)
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def hash(self):
|
|
56
|
+
"""Returns the RIPEMD-160 hash corresponding to this address"""
|
|
57
|
+
if self.public_key is not None and self._hash is None:
|
|
58
|
+
self._hash = btc_ripemd160(self.public_key)
|
|
59
|
+
return self._hash
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def address(self):
|
|
63
|
+
"""Returns the encoded representation of this address.
|
|
64
|
+
If Taproot, it's encoded using bech32m,
|
|
65
|
+
if SegWit, it's encoded using bech32,
|
|
66
|
+
otherwise using base58
|
|
67
|
+
"""
|
|
68
|
+
if self._address is None:
|
|
69
|
+
if self.type == "bech32m":
|
|
70
|
+
tweaked_pubkey = b2a_hex(self.hash).decode("ascii")
|
|
71
|
+
self._address = from_taproot(tweaked_pubkey)
|
|
72
|
+
elif self.type != "bech32":
|
|
73
|
+
version = b'\x00' if self.type == "normal" else b'\x05'
|
|
74
|
+
checksum = double_sha256(version + self.hash)
|
|
75
|
+
|
|
76
|
+
self._address = base58.encode(version + self.hash + checksum[:4])
|
|
77
|
+
else:
|
|
78
|
+
bech_encoded = CBech32Data.from_bytes(self._segwit_version, self._hash)
|
|
79
|
+
self._address = str(bech_encoded)
|
|
80
|
+
return self._address
|
|
81
|
+
|
|
82
|
+
def is_p2sh(self):
|
|
83
|
+
return self.type == "p2sh"
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Copyright (C) 2015-2016 The bitcoin-blockchain-parser developers
|
|
2
|
+
#
|
|
3
|
+
# This file is part of bitcoin-blockchain-parser.
|
|
4
|
+
#
|
|
5
|
+
# It is subject to the license terms in the LICENSE file found in the top-level
|
|
6
|
+
# directory of this distribution.
|
|
7
|
+
#
|
|
8
|
+
# No part of bitcoin-blockchain-parser, including this file, may be copied,
|
|
9
|
+
# modified, propagated, or distributed except according to the terms contained
|
|
10
|
+
# in the LICENSE file.
|
|
11
|
+
|
|
12
|
+
from .transaction import Transaction
|
|
13
|
+
from .block_header import BlockHeader
|
|
14
|
+
from .utils import format_hash, decode_compactsize, double_sha256
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def get_block_transactions(raw_hex):
|
|
18
|
+
"""Given the raw hexadecimal representation of a block,
|
|
19
|
+
yields the block's transactions
|
|
20
|
+
"""
|
|
21
|
+
# Skipping the header
|
|
22
|
+
transaction_data = raw_hex[80:]
|
|
23
|
+
|
|
24
|
+
# Decoding the number of transactions, offset is the size of
|
|
25
|
+
# the varint (1 to 9 bytes)
|
|
26
|
+
n_transactions, offset = decode_compactsize(transaction_data)
|
|
27
|
+
|
|
28
|
+
for i in range(n_transactions):
|
|
29
|
+
transaction = None
|
|
30
|
+
# Try from 1024 (1KiB) -> 512MiB slice widths
|
|
31
|
+
for j in range(0, 20):
|
|
32
|
+
try:
|
|
33
|
+
offset_e = offset + (1024 * 2 ** j)
|
|
34
|
+
transaction = Transaction.from_hex(
|
|
35
|
+
transaction_data[offset:offset_e])
|
|
36
|
+
break
|
|
37
|
+
except Exception:
|
|
38
|
+
transaction = None
|
|
39
|
+
if transaction is None:
|
|
40
|
+
raise ValueError(
|
|
41
|
+
"failed to parse transaction %d at offset %d" % (i, offset)
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
yield transaction
|
|
45
|
+
# Skipping to the next transaction
|
|
46
|
+
offset += transaction.size
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class Block(object):
|
|
50
|
+
"""
|
|
51
|
+
Represents a Bitcoin block, contains its header and its transactions.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __init__(self, raw_hex, height=None, blk_file=None):
|
|
55
|
+
self.hex = raw_hex
|
|
56
|
+
self._hash = None
|
|
57
|
+
self._transactions = None
|
|
58
|
+
self._header = None
|
|
59
|
+
self._n_transactions = None
|
|
60
|
+
self.size = len(raw_hex)
|
|
61
|
+
self.height = height
|
|
62
|
+
self.blk_file = blk_file
|
|
63
|
+
|
|
64
|
+
def __repr__(self):
|
|
65
|
+
return "Block(%s)" % self.hash
|
|
66
|
+
|
|
67
|
+
@classmethod
|
|
68
|
+
def from_hex(cls, raw_hex):
|
|
69
|
+
"""Builds a block object from its bytes representation"""
|
|
70
|
+
return cls(raw_hex)
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def hash(self):
|
|
74
|
+
"""Returns the block's hash (double sha256 of its 80 bytes header"""
|
|
75
|
+
if self._hash is None:
|
|
76
|
+
self._hash = format_hash(double_sha256(self.hex[:80]))
|
|
77
|
+
return self._hash
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def n_transactions(self):
|
|
81
|
+
"""Return the number of transactions contained in this block,
|
|
82
|
+
it is faster to use this than to use len(block.transactions)
|
|
83
|
+
as there's no need to parse all transactions to get this information
|
|
84
|
+
"""
|
|
85
|
+
if self._n_transactions is None:
|
|
86
|
+
self._n_transactions = decode_compactsize(self.hex[80:])[0]
|
|
87
|
+
|
|
88
|
+
return self._n_transactions
|
|
89
|
+
|
|
90
|
+
@property
|
|
91
|
+
def transactions(self):
|
|
92
|
+
"""Returns a list of the block's transactions represented
|
|
93
|
+
as Transaction objects"""
|
|
94
|
+
if self._transactions is None:
|
|
95
|
+
self._transactions = list(get_block_transactions(self.hex))
|
|
96
|
+
|
|
97
|
+
return self._transactions
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def header(self):
|
|
101
|
+
"""Returns a BlockHeader object corresponding to this block"""
|
|
102
|
+
if self._header is None:
|
|
103
|
+
self._header = BlockHeader.from_hex(self.hex[:80])
|
|
104
|
+
return self._header
|