Mercurial > public > bitcaviar-plus
annotate README.md @ 34:bd0ec5cb2300 default tip
Move to mercurial
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Tue, 03 Jun 2025 14:24:28 +0100 |
parents | e947bfd3db1a |
children |
rev | line source |
---|---|
32 | 1 # bitcaviar-plus |
17 | 2 |
34 | 3 I want to learn more about the technology behind Bitcoin, so what better way to |
4 do so than by making a project? | |
32 | 5 |
34 | 6 Here is a Bitcoin parser written in Python that I made. It was a very nice |
7 learning experience. | |
32 | 8 |
9 ## Installation | |
10 ```bash | |
11 python setup.py install | |
12 ``` | |
13 | |
14 ## Usage | |
15 | |
16 To deserialize the genesis block: | |
17 | |
18 ```python | |
19 from bitcaviar_plus.block import deserialize_block | |
20 | |
21 | |
22 def parse_genesis_block(): | |
23 with open('path/to/file/blk00000.dat', 'rb') as f: | |
24 block = deserialize_block(f) | |
25 print(block) | |
26 To deserialize the entire blockchain: | |
27 | |
28 import os | |
29 from bitcaviar_plus.block import deserialize_block | |
30 from bitcaviar_plus.errors import InvalidMagicBytes | |
31 | |
32 | |
33 def parse_entire_blockchain(): | |
34 file_counter = -1 | |
35 while True: | |
36 file_counter += 1 | |
37 file_name = 'path/to/file/blk{}.dat'.format(str(file_counter).zfill(5)) | |
38 with open(file_name, 'rb') as f: | |
39 file_size = os.path.getsize(file_name) | |
40 while f.tell() < file_size: | |
41 try: | |
42 block = deserialize_block(f) | |
43 except InvalidMagicBytes as e: | |
44 print(e) | |
45 | |
46 ``` | |
47 | |
48 This is the output: | |
49 | |
50 ```json | |
51 { | |
52 "magic_number": "f9beb4d9", | |
53 "size": "0000011d", | |
54 "id": "000000000019d6...", | |
55 "transaction_count": "01", | |
56 "header": { | |
57 "version": "00000001", | |
58 "previous_block_id": "00000000000000...", | |
59 "merkle_root": "4a5e1e4baab89f3a32...", | |
60 "time": "495fab29", | |
61 "bits": "1d00ffff", | |
62 "nonce": "7c2bac1d" | |
63 }, | |
64 "transactions": [ | |
65 { | |
66 "version": "00000001", | |
67 "input_count": "01", | |
68 "output_count": "01", | |
69 "lock_time": "00000000", | |
70 "id": "4a5e1e4baab89f3a32518a8...", | |
71 "inputs": [ | |
72 { | |
73 "id": "0000000000000000000000...", | |
74 "vout": "ffffffff", | |
75 "script_sig_size": "4d", | |
76 "script_sig": "04ffff001d01044554686520546...", | |
77 "sequence": "ffffffff" | |
78 } | |
79 ], | |
80 "outputs": [ | |
81 { | |
82 "value": "000000012a05f200", | |
83 "script_pub_key_size": "43", | |
84 "script_pub_key": "4104678afdb0fe55482719..." | |
85 } | |
86 ] | |
87 } | |
88 ] | |
89 } | |
90 ``` | |
13
4bbe48c95079
add instructions
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
4
diff
changeset
|
91 |
30 | 92 ## Attribution |
28
30535f42d0ff
refactor code
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
21
diff
changeset
|
93 |
30 | 94 - [blockchain-parser](https://github.com/ragestack/blockchain-parser/blob/master/blockchain-parser.py) |
95 - [bitcoinbook](https://github.com/bitcoinbook/bitcoinbook) | |
96 - [learnmeabitcoin.com](https://learnmeabitcoin.com/) | |
13
4bbe48c95079
add instructions
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
4
diff
changeset
|
97 |