libnexapy-test 0.1.0__py3-none-any.whl
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.
- libnexapy_test-0.1.0.dist-info/METADATA +104 -0
- libnexapy_test-0.1.0.dist-info/RECORD +56 -0
- libnexapy_test-0.1.0.dist-info/WHEEL +5 -0
- libnexapy_test-0.1.0.dist-info/licenses/LICENCE +22 -0
- libnexapy_test-0.1.0.dist-info/top_level.txt +1 -0
- src/__init__.py +0 -0
- src/common/__init__.py +2 -0
- src/common/class_types.py +60 -0
- src/common/types.py +351 -0
- src/core/address/address.py +380 -0
- src/core/address/address_formatter.py +293 -0
- src/core/block/block.py +158 -0
- src/core/block/block_header.py +257 -0
- src/core/grouptoken.py +363 -0
- src/core/message.py +131 -0
- src/core/network/network.py +118 -0
- src/core/network/network_manager.py +131 -0
- src/core/script/opcode.py +312 -0
- src/core/script/script.py +841 -0
- src/core/script/script_factory.py +385 -0
- src/core/transaction/input/input.py +134 -0
- src/core/transaction/input/public_key_hash.py +47 -0
- src/core/transaction/input/public_key_template.py +47 -0
- src/core/transaction/input/script_template.py +154 -0
- src/core/transaction/output.py +161 -0
- src/core/transaction/sighash_type.py +246 -0
- src/core/transaction/transaction.py +506 -0
- src/core/transaction/tx_builder.py +228 -0
- src/core/transaction/tx_signature.py +85 -0
- src/core/transaction/tx_signer.py +164 -0
- src/core/transaction/unspent_output.py +79 -0
- src/crypto/__init__.py +3 -0
- src/crypto/curve_utils.py +43 -0
- src/crypto/digital_signature.py +73 -0
- src/crypto/ecc_fast.py +175 -0
- src/crypto/ecdsa.py +310 -0
- src/crypto/hash.py +64 -0
- src/crypto/libsecp256k1.so.0 +0 -0
- src/crypto/ripemd160.py +408 -0
- src/crypto/schnorr.py +509 -0
- src/crypto/secp256k1.py +223 -0
- src/crypto/signature.py +244 -0
- src/encoding/base32.py +51 -0
- src/encoding/base58.py +165 -0
- src/encoding/base58_check.py +92 -0
- src/encoding/buffer_reader.py +159 -0
- src/encoding/buffer_writer.py +138 -0
- src/keys/hd_private_key.py +473 -0
- src/keys/hd_public_key.py +417 -0
- src/keys/hdkey_utils.py +139 -0
- src/keys/private_key.py +340 -0
- src/keys/public_key.py +394 -0
- src/utils/buffer_utils.py +47 -0
- src/utils/common_utils.py +4 -0
- src/utils/num_utils.py +81 -0
- src/utils/unit_utils.py +78 -0
src/core/block/block.py
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
from typing import Self
|
|
2
|
+
|
|
3
|
+
from src.common.class_types import TypedClass
|
|
4
|
+
from src.common.types import IBlock, IBlockHeader
|
|
5
|
+
from src.core.block.block_header import BlockHeader
|
|
6
|
+
from src.core.transaction.transaction import Transaction
|
|
7
|
+
from src.encoding.buffer_reader import BufferReader
|
|
8
|
+
from src.encoding.buffer_writer import BufferWriter
|
|
9
|
+
from src.crypto.hash import Hash
|
|
10
|
+
from src.utils.buffer_utils import concat
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Block(TypedClass):
|
|
14
|
+
header: BlockHeader
|
|
15
|
+
transactions: list
|
|
16
|
+
|
|
17
|
+
def __init__(self, data: bytearray | IBlock | dict):
|
|
18
|
+
super().__init__()
|
|
19
|
+
self.header = None
|
|
20
|
+
self.transactions = None
|
|
21
|
+
self._enforce_attribute_types()
|
|
22
|
+
|
|
23
|
+
if data is None:
|
|
24
|
+
raise Exception("data is required")
|
|
25
|
+
|
|
26
|
+
if isinstance(data, (bytes, bytearray)):
|
|
27
|
+
data = Block._from_buffer_reader(BufferReader(bytearray(data)))
|
|
28
|
+
elif not isinstance(data, (IBlock, dict)):
|
|
29
|
+
raise TypeError("Unrecognized argument for Block")
|
|
30
|
+
|
|
31
|
+
header = data["header"] if isinstance(data, dict) else data.header
|
|
32
|
+
if not isinstance(header, BlockHeader):
|
|
33
|
+
if isinstance(header, dict):
|
|
34
|
+
header = Block._iblock_header_from_dict(header)
|
|
35
|
+
header = BlockHeader.from_object(header)
|
|
36
|
+
self.header = header
|
|
37
|
+
|
|
38
|
+
transactions = data["transactions"] if isinstance(data, dict) else data.transactions
|
|
39
|
+
self.transactions = []
|
|
40
|
+
for tx in transactions:
|
|
41
|
+
if isinstance(tx, Transaction):
|
|
42
|
+
self.transactions.append(tx)
|
|
43
|
+
else:
|
|
44
|
+
self.transactions.append(Transaction().from_object(tx))
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def hash(self) -> str:
|
|
48
|
+
return self.header.hash
|
|
49
|
+
|
|
50
|
+
@staticmethod
|
|
51
|
+
def from_object(obj: IBlock) -> Self:
|
|
52
|
+
return Block(obj)
|
|
53
|
+
|
|
54
|
+
@staticmethod
|
|
55
|
+
def _from_buffer_reader(br: BufferReader) -> IBlock:
|
|
56
|
+
if br.finished():
|
|
57
|
+
raise Exception("No block data received")
|
|
58
|
+
ib = IBlock()
|
|
59
|
+
ib.header = BlockHeader.from_buffer_reader(br).to_object()
|
|
60
|
+
tx_num = br.read_varint()
|
|
61
|
+
ib.transactions = []
|
|
62
|
+
for _ in range(tx_num):
|
|
63
|
+
ib.transactions.append(Transaction().from_buffer_reader(br))
|
|
64
|
+
return ib
|
|
65
|
+
|
|
66
|
+
@staticmethod
|
|
67
|
+
def from_buffer_reader(br: BufferReader) -> Self:
|
|
68
|
+
if br is None:
|
|
69
|
+
raise Exception("br is required")
|
|
70
|
+
info = Block._from_buffer_reader(br)
|
|
71
|
+
return Block(info)
|
|
72
|
+
|
|
73
|
+
@staticmethod
|
|
74
|
+
def from_buffer(buf: bytearray) -> Self:
|
|
75
|
+
return Block.from_buffer_reader(BufferReader(bytearray(buf)))
|
|
76
|
+
|
|
77
|
+
@staticmethod
|
|
78
|
+
def from_string(string: str) -> Self:
|
|
79
|
+
buf = bytearray.fromhex(string)
|
|
80
|
+
return Block.from_buffer(buf)
|
|
81
|
+
|
|
82
|
+
def to_json(self):
|
|
83
|
+
return self.to_object()
|
|
84
|
+
|
|
85
|
+
def to_object(self) -> IBlock:
|
|
86
|
+
ib = IBlock()
|
|
87
|
+
ib.header = self.header.to_object()
|
|
88
|
+
ib.transactions = [tx.to_object() for tx in self.transactions]
|
|
89
|
+
return ib
|
|
90
|
+
|
|
91
|
+
def to_buffer_writer(self, bw: BufferWriter | None = None) -> BufferWriter:
|
|
92
|
+
if bw is None:
|
|
93
|
+
bw = BufferWriter()
|
|
94
|
+
bw.write(self.header.to_buffer())
|
|
95
|
+
bw.write_varint(len(self.transactions))
|
|
96
|
+
for tx in self.transactions:
|
|
97
|
+
tx.to_buffer_writer(bw)
|
|
98
|
+
return bw
|
|
99
|
+
|
|
100
|
+
def to_buffer(self) -> bytearray:
|
|
101
|
+
return self.to_buffer_writer().to_buffer()
|
|
102
|
+
|
|
103
|
+
def to_string(self) -> str:
|
|
104
|
+
return self.to_buffer().hex()
|
|
105
|
+
|
|
106
|
+
def __str__(self) -> str:
|
|
107
|
+
return f"<Block {self.hash}>"
|
|
108
|
+
|
|
109
|
+
def get_transaction_hashes(self):
|
|
110
|
+
return [bytearray(reversed(bytearray.fromhex(tx.id))) for tx in self.transactions]
|
|
111
|
+
|
|
112
|
+
def get_merkle_tree(self):
|
|
113
|
+
tree = self.get_transaction_hashes()
|
|
114
|
+
j = 0
|
|
115
|
+
size = len(self.transactions)
|
|
116
|
+
while size > 1:
|
|
117
|
+
for i in range(0, size, 2):
|
|
118
|
+
i2 = min(i + 1, size - 1)
|
|
119
|
+
buf = concat([tree[j + i], tree[j + i2]])
|
|
120
|
+
tree.append(Hash.sha256sha256(buf))
|
|
121
|
+
j += size
|
|
122
|
+
size = (size + 1) // 2
|
|
123
|
+
return tree
|
|
124
|
+
|
|
125
|
+
def get_merkle_root(self) -> bytearray:
|
|
126
|
+
tree = self.get_merkle_tree()
|
|
127
|
+
return tree[len(tree) - 1]
|
|
128
|
+
|
|
129
|
+
def valid_merkle_root(self) -> bool:
|
|
130
|
+
h = int.from_bytes(bytearray(reversed(self.header.merkle_root)), "big")
|
|
131
|
+
c = int.from_bytes(self.get_merkle_root(), "big")
|
|
132
|
+
return h == c
|
|
133
|
+
|
|
134
|
+
@staticmethod
|
|
135
|
+
def _iblock_header_from_dict(d: dict) -> IBlockHeader:
|
|
136
|
+
def _bytes(key: str) -> bytearray:
|
|
137
|
+
v = d.get(key)
|
|
138
|
+
if v is None:
|
|
139
|
+
return bytearray()
|
|
140
|
+
return bytearray.fromhex(v) if isinstance(v, str) else bytearray(v)
|
|
141
|
+
|
|
142
|
+
ibh = IBlockHeader()
|
|
143
|
+
ibh.hash = d.get("hash", "")
|
|
144
|
+
ibh.prev_hash = _bytes("prevHash")
|
|
145
|
+
ibh.bits = int(d.get("bits", 0))
|
|
146
|
+
ibh.ancestor_hash = _bytes("ancestorHash")
|
|
147
|
+
ibh.merkle_root = _bytes("merkleRoot")
|
|
148
|
+
ibh.tx_filter = _bytes("txFilter")
|
|
149
|
+
ibh.time = int(d.get("time", 0))
|
|
150
|
+
ibh.height = int(d.get("height", 0))
|
|
151
|
+
ibh.chain_work = _bytes("chainWork")
|
|
152
|
+
ibh.size = int(d.get("size", 0))
|
|
153
|
+
ibh.tx_count = int(d.get("txCount", 0))
|
|
154
|
+
ibh.pool_fee = int(d.get("poolFee", 0))
|
|
155
|
+
ibh.utxo_commitment = _bytes("utxoCommitment")
|
|
156
|
+
ibh.miner_data = _bytes("minerData")
|
|
157
|
+
ibh.nonce = _bytes("nonce")
|
|
158
|
+
return ibh
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
from typing import Self
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
from src.common.class_types import TypedClass
|
|
6
|
+
from src.common.types import IBlockHeader
|
|
7
|
+
from src.crypto.hash import Hash
|
|
8
|
+
from src.encoding.buffer_reader import BufferReader
|
|
9
|
+
from src.encoding.buffer_writer import BufferWriter
|
|
10
|
+
import src.utils.buffer_utils
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BlockHeader(TypedClass):
|
|
14
|
+
MAX_TIME_OFFSET: int = 2 * 60 * 60
|
|
15
|
+
|
|
16
|
+
prev_hash: bytearray
|
|
17
|
+
bits: int
|
|
18
|
+
ancestor_hash: bytearray
|
|
19
|
+
merkle_root: bytearray
|
|
20
|
+
tx_filter: bytearray
|
|
21
|
+
time: int
|
|
22
|
+
height: int
|
|
23
|
+
chain_work: bytearray
|
|
24
|
+
size: int
|
|
25
|
+
tx_count: int
|
|
26
|
+
pool_fee: int
|
|
27
|
+
utxo_commitment: bytearray
|
|
28
|
+
miner_data: bytearray
|
|
29
|
+
nonce: bytearray
|
|
30
|
+
|
|
31
|
+
def __init__(self, data: bytearray | IBlockHeader | Self):
|
|
32
|
+
super().__init__()
|
|
33
|
+
self.prev_hash = None
|
|
34
|
+
self.bits = None
|
|
35
|
+
self.ancestor_hash = None
|
|
36
|
+
self.merkle_root = None
|
|
37
|
+
self.tx_filter = None
|
|
38
|
+
self.time = None
|
|
39
|
+
self.height = None
|
|
40
|
+
self.chain_work = None
|
|
41
|
+
self.size = None
|
|
42
|
+
self.tx_count = None
|
|
43
|
+
self.pool_fee = None
|
|
44
|
+
self.utxo_commitment = None
|
|
45
|
+
self.miner_data = None
|
|
46
|
+
self.nonce = None
|
|
47
|
+
self._enforce_attribute_types()
|
|
48
|
+
|
|
49
|
+
if isinstance(data, bytearray):
|
|
50
|
+
data = BlockHeader._from_buffer_reader(BufferReader(data))
|
|
51
|
+
if not isinstance(data, IBlockHeader):
|
|
52
|
+
raise TypeError("data must be a bytearray, BlockHeader, or IBlockHeader")
|
|
53
|
+
|
|
54
|
+
# if isinstance(data.prev_hash, str):
|
|
55
|
+
# data.prev_hash = data.prev_hash
|
|
56
|
+
self.prev_hash = data.prev_hash
|
|
57
|
+
self.bits = data.bits
|
|
58
|
+
|
|
59
|
+
# if isinstance(data.ancestor_hash, str):
|
|
60
|
+
# data.ancestor_hash = data.ancestor_hash
|
|
61
|
+
self.ancestor_hash = data.ancestor_hash
|
|
62
|
+
|
|
63
|
+
# if isinstance(data.merkle_root, str):
|
|
64
|
+
# data.merkle_root = data.merkle_root
|
|
65
|
+
self.merkle_root = data.merkle_root
|
|
66
|
+
|
|
67
|
+
# if isinstance(data.tx_filter, str):
|
|
68
|
+
# data.tx_filter = data.tx_filter
|
|
69
|
+
self.tx_filter = data.tx_filter
|
|
70
|
+
|
|
71
|
+
self.time = data.time
|
|
72
|
+
self.height = data.height
|
|
73
|
+
|
|
74
|
+
# if isinstance(data.chain_work, str):
|
|
75
|
+
# data.chain_work = data.chain_work
|
|
76
|
+
self.chain_work = data.chain_work
|
|
77
|
+
|
|
78
|
+
self.size = data.size
|
|
79
|
+
self.tx_count = data.tx_count
|
|
80
|
+
self.pool_fee = data.pool_fee
|
|
81
|
+
|
|
82
|
+
# if isinstance(data.utxo_commitment, str):
|
|
83
|
+
# data.utxo_commitment = data.utxo_commitment
|
|
84
|
+
self.utxo_commitment = data.utxo_commitment
|
|
85
|
+
|
|
86
|
+
# if isinstance(data.miner_data, str):
|
|
87
|
+
# data.miner_data = data.miner_data
|
|
88
|
+
self.miner_data = data.miner_data
|
|
89
|
+
|
|
90
|
+
# if isinstance(data.nonce, str):
|
|
91
|
+
# data.nonce = data.nonce
|
|
92
|
+
self.nonce = data.nonce
|
|
93
|
+
|
|
94
|
+
if data.hash:
|
|
95
|
+
if self.hash != data.hash:
|
|
96
|
+
raise ValueError("Argument object hash property does not match block hash")
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def hash(self) -> str:
|
|
100
|
+
hash = self._get_hash()
|
|
101
|
+
hash.reverse()
|
|
102
|
+
return hash.hex()
|
|
103
|
+
|
|
104
|
+
def _get_hash(self) -> bytearray:
|
|
105
|
+
mini_header = BufferWriter()
|
|
106
|
+
mini_header.write_reverse(self.prev_hash)
|
|
107
|
+
mini_header.write_int32_le(self.bits)
|
|
108
|
+
|
|
109
|
+
mini_hash = bytearray(Hash.sha256(mini_header.to_buffer()))
|
|
110
|
+
|
|
111
|
+
ext_header = BufferWriter()
|
|
112
|
+
ext_header.write_reverse(self.ancestor_hash)
|
|
113
|
+
ext_header.write_reverse(self.tx_filter)
|
|
114
|
+
ext_header.write_reverse(self.merkle_root)
|
|
115
|
+
ext_header.write_int32_le(self.time)
|
|
116
|
+
ext_header.write_uint64_le(self.height)
|
|
117
|
+
ext_header.write_reverse(self.chain_work)
|
|
118
|
+
ext_header.write_uint64_le(self.size)
|
|
119
|
+
ext_header.write_uint64_le(self.tx_count)
|
|
120
|
+
ext_header.write_uint64_le(self.pool_fee)
|
|
121
|
+
ext_header.write_var_length_buf(self.utxo_commitment)
|
|
122
|
+
ext_header.write_var_length_buf(self.miner_data)
|
|
123
|
+
ext_header.write_var_length_buf(self.nonce)
|
|
124
|
+
|
|
125
|
+
ext_hash = bytearray(Hash.sha256(ext_header.to_buffer()))
|
|
126
|
+
|
|
127
|
+
commintment = BufferWriter()
|
|
128
|
+
commintment.write(mini_hash)
|
|
129
|
+
commintment.write(ext_hash)
|
|
130
|
+
|
|
131
|
+
return bytearray(Hash.sha256(commintment.to_buffer()))
|
|
132
|
+
|
|
133
|
+
@staticmethod
|
|
134
|
+
def _from_buffer_reader(br: BufferReader) -> IBlockHeader:
|
|
135
|
+
header = IBlockHeader()
|
|
136
|
+
header.prev_hash = br.read_reverse(32)
|
|
137
|
+
header.bits = br.read_uint32_le()
|
|
138
|
+
header.ancestor_hash = br.read_reverse(32)
|
|
139
|
+
header.merkle_root = br.read_reverse(32)
|
|
140
|
+
header.tx_filter = br.read_reverse(32)
|
|
141
|
+
header.time = br.read_uint32_le()
|
|
142
|
+
header.height = br.read_core_varint()
|
|
143
|
+
header.chain_work = br.read_reverse(32)
|
|
144
|
+
header.size = br.read_uint64_le()
|
|
145
|
+
header.tx_count = br.read_core_varint()
|
|
146
|
+
header.pool_fee = br.read_core_varint()
|
|
147
|
+
header.utxo_commitment = br.read_var_length_buffer()
|
|
148
|
+
header.miner_data = br.read_var_length_buffer()
|
|
149
|
+
header.nonce = br.read_var_length_buffer()
|
|
150
|
+
return header
|
|
151
|
+
|
|
152
|
+
@staticmethod
|
|
153
|
+
def from_buffer_reader(br: BufferReader) -> Self:
|
|
154
|
+
info = BlockHeader._from_buffer_reader(br)
|
|
155
|
+
return BlockHeader(info)
|
|
156
|
+
|
|
157
|
+
@staticmethod
|
|
158
|
+
def from_buffer(buf: bytearray) -> Self:
|
|
159
|
+
br = BufferReader(buf)
|
|
160
|
+
return BlockHeader.from_buffer_reader(br)
|
|
161
|
+
|
|
162
|
+
@staticmethod
|
|
163
|
+
def from_string(hex: str) -> Self:
|
|
164
|
+
buf = bytes.fromhex(hex)
|
|
165
|
+
return BlockHeader.from_buffer(buf)
|
|
166
|
+
|
|
167
|
+
@staticmethod
|
|
168
|
+
def from_object(header: IBlockHeader) -> Self:
|
|
169
|
+
return BlockHeader(header)
|
|
170
|
+
|
|
171
|
+
def to_JSON(self) -> IBlockHeader:
|
|
172
|
+
return self.to_object()
|
|
173
|
+
|
|
174
|
+
def to_object(self) -> IBlockHeader:
|
|
175
|
+
ibh = IBlockHeader()
|
|
176
|
+
ibh.hash = self.hash
|
|
177
|
+
ibh.prev_hash = self.prev_hash
|
|
178
|
+
ibh.bits = self.bits
|
|
179
|
+
ibh.ancestor_hash = self.ancestor_hash
|
|
180
|
+
ibh.merkle_root = self.merkle_root
|
|
181
|
+
ibh.tx_filter = self.tx_filter
|
|
182
|
+
ibh.time = self.time
|
|
183
|
+
ibh.height = self.height
|
|
184
|
+
ibh.chain_work = self.chain_work
|
|
185
|
+
ibh.size = self.size
|
|
186
|
+
ibh.tx_count = self.tx_count
|
|
187
|
+
ibh.pool_fee = self.pool_fee
|
|
188
|
+
ibh.utxo_commitment = self.utxo_commitment
|
|
189
|
+
ibh.miner_data = self.miner_data
|
|
190
|
+
ibh.nonce = self.nonce
|
|
191
|
+
return ibh
|
|
192
|
+
|
|
193
|
+
def to_buffer_writer(self, bw: BufferWriter | None = None) -> BufferWriter:
|
|
194
|
+
if bw is None:
|
|
195
|
+
bw = BufferWriter()
|
|
196
|
+
if not isinstance(bw, BufferWriter):
|
|
197
|
+
raise TypeError("bw must be a BufferWriter or None")
|
|
198
|
+
bw.write_reverse(self.prev_hash)
|
|
199
|
+
bw.write_int32_le(self.bits)
|
|
200
|
+
bw.write_reverse(self.ancestor_hash)
|
|
201
|
+
bw.write_reverse(self.merkle_root)
|
|
202
|
+
bw.write_reverse(self.tx_filter)
|
|
203
|
+
bw.write_int32_le(self.time)
|
|
204
|
+
bw.write_core_varint(self.height)
|
|
205
|
+
bw.write_reverse(self.chain_work)
|
|
206
|
+
bw.write_uint64_le(self.size)
|
|
207
|
+
bw.write_core_varint(self.tx_count)
|
|
208
|
+
bw.write_core_varint(self.pool_fee)
|
|
209
|
+
bw.write_var_length_buf(self.utxo_commitment)
|
|
210
|
+
bw.write_var_length_buf(self.miner_data)
|
|
211
|
+
bw.write_var_length_buf(self.nonce)
|
|
212
|
+
return bw
|
|
213
|
+
|
|
214
|
+
def to_buffer(self) -> bytearray:
|
|
215
|
+
bw = self.to_buffer_writer()
|
|
216
|
+
return bw.to_buffer()
|
|
217
|
+
|
|
218
|
+
def to_string(self) -> str:
|
|
219
|
+
return self.to_buffer().hex()
|
|
220
|
+
|
|
221
|
+
def get_target_difficulty(self, bits: int | None = None) -> int:
|
|
222
|
+
bits_buf = self.bits
|
|
223
|
+
if isinstance(bits, int):
|
|
224
|
+
bits_buf = bits
|
|
225
|
+
elif bits is not None:
|
|
226
|
+
raise TypeError("bits must be an int or None")
|
|
227
|
+
|
|
228
|
+
bits_buf = bytearray(bits_buf.to_bytes(4, "big"))
|
|
229
|
+
exponent = int(bits_buf[0])
|
|
230
|
+
significand_bytes = bits_buf[1:]
|
|
231
|
+
significand_bytes[0] = significand_bytes[0] & 0x7F
|
|
232
|
+
significand = int.from_bytes(significand_bytes, "big")
|
|
233
|
+
|
|
234
|
+
target = 0
|
|
235
|
+
|
|
236
|
+
if exponent <= 3:
|
|
237
|
+
target = significand // (2 ** (8 * (3 - exponent)))
|
|
238
|
+
else:
|
|
239
|
+
target = significand * (2 ** (8 * (exponent - 3)))
|
|
240
|
+
|
|
241
|
+
target = (2**256) // (target + 1)
|
|
242
|
+
return int(target)
|
|
243
|
+
|
|
244
|
+
def get_difficulty(self) -> int:
|
|
245
|
+
nShift = (self.bits >> 24) & 0xFF
|
|
246
|
+
dDiff = 0x0000FFFF / (self.bits & 0x00FFFFFF)
|
|
247
|
+
while nShift < 29:
|
|
248
|
+
dDiff = dDiff * 256.0
|
|
249
|
+
nShift = nShift + 1
|
|
250
|
+
while nShift > 29:
|
|
251
|
+
dDiff = dDiff / 256.0
|
|
252
|
+
nShift = nShift - 1
|
|
253
|
+
return dDiff
|
|
254
|
+
|
|
255
|
+
def valid_timestamp(self) -> bool:
|
|
256
|
+
current_time = int(time.time())
|
|
257
|
+
return self.time <= current_time + BlockHeader.MAX_TIME_OFFSET
|