|
| 1 | +from io import BytesIO |
| 2 | + |
1 | 3 | from buidl.helper import (
|
2 | 4 | bits_to_target,
|
3 | 5 | hash256,
|
|
9 | 11 | from buidl.tx import Tx
|
10 | 12 |
|
11 | 13 |
|
12 |
| -GENESIS_BLOCK_HASH = { |
13 |
| - "mainnet": bytes.fromhex( |
14 |
| - "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" |
15 |
| - ), |
16 |
| - "testnet": bytes.fromhex( |
17 |
| - "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943" |
18 |
| - ), |
19 |
| - "signet": bytes.fromhex( |
20 |
| - "00000008819873e925422c1ff0f99f7cc9bbb232af63a077a480a3633bee1ef6" |
21 |
| - ), |
22 |
| -} |
23 |
| - |
24 |
| - |
25 | 14 | class Block:
|
26 | 15 | command = b"block"
|
27 | 16 |
|
@@ -58,21 +47,25 @@ def __repr__(self):
|
58 | 47 | """
|
59 | 48 |
|
60 | 49 | @classmethod
|
61 |
| - def parse_header(cls, s): |
| 50 | + def parse_header(cls, stream=None, hex=None): |
62 | 51 | """Takes a byte stream and parses block headers. Returns a Block object"""
|
63 |
| - # s.read(n) will read n bytes from the stream |
| 52 | + if hex: |
| 53 | + if stream: |
| 54 | + raise RuntimeError("One of stream or hex should be defined") |
| 55 | + stream = BytesIO(bytes.fromhex(hex)) |
| 56 | + # stream.read(n) will read n bytes from the stream |
64 | 57 | # version - 4 bytes, little endian, interpret as int
|
65 |
| - version = little_endian_to_int(s.read(4)) |
| 58 | + version = little_endian_to_int(stream.read(4)) |
66 | 59 | # prev_block - 32 bytes, little endian (use [::-1] to reverse)
|
67 |
| - prev_block = s.read(32)[::-1] |
| 60 | + prev_block = stream.read(32)[::-1] |
68 | 61 | # merkle_root - 32 bytes, little endian (use [::-1] to reverse)
|
69 |
| - merkle_root = s.read(32)[::-1] |
| 62 | + merkle_root = stream.read(32)[::-1] |
70 | 63 | # timestamp - 4 bytes, little endian, interpret as int
|
71 |
| - timestamp = little_endian_to_int(s.read(4)) |
| 64 | + timestamp = little_endian_to_int(stream.read(4)) |
72 | 65 | # bits - 4 bytes
|
73 |
| - bits = s.read(4) |
| 66 | + bits = stream.read(4) |
74 | 67 | # nonce - 4 bytes
|
75 |
| - nonce = s.read(4) |
| 68 | + nonce = stream.read(4) |
76 | 69 | # initialize class
|
77 | 70 | return cls(version, prev_block, merkle_root, timestamp, bits, nonce)
|
78 | 71 |
|
@@ -177,3 +170,18 @@ def get_outpoints(self):
|
177 | 170 | for tx_out in t.tx_outs:
|
178 | 171 | if not tx_out.script_pubkey.has_op_return():
|
179 | 172 | yield (tx_out.script_pubkey.raw_serialize())
|
| 173 | + |
| 174 | + |
| 175 | +GENESIS_BLOCK_MAINNET_HEX = "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c" |
| 176 | +GENESIS_BLOCK_TESTNET_HEX = "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff001d1aa4ae18" |
| 177 | +GENESIS_BLOCK_SIGNET_HEX = "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a008f4d5fae77031e8ad22203" |
| 178 | +GENESIS_BLOCK_HEADERS = { |
| 179 | + "mainnet": Block.parse_header(hex=GENESIS_BLOCK_MAINNET_HEX), |
| 180 | + "testnet": Block.parse_header(hex=GENESIS_BLOCK_TESTNET_HEX), |
| 181 | + "signet": Block.parse_header(hex=GENESIS_BLOCK_SIGNET_HEX), |
| 182 | +} |
| 183 | +GENESIS_BLOCK_HASH = { |
| 184 | + "mainnet": GENESIS_BLOCK_HEADERS["mainnet"].hash(), |
| 185 | + "testnet": GENESIS_BLOCK_HEADERS["testnet"].hash(), |
| 186 | + "signet": GENESIS_BLOCK_HEADERS["signet"].hash(), |
| 187 | +} |
0 commit comments