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/grouptoken.py
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from enum import Enum, unique
|
|
3
|
+
|
|
4
|
+
from src.common.class_types import FrozenClass
|
|
5
|
+
from src.common.types import GroupIdData
|
|
6
|
+
from src.core.address.address import Address
|
|
7
|
+
from src.core.message import Message
|
|
8
|
+
from src.core.script.script import Script
|
|
9
|
+
from src.crypto.hash import Hash
|
|
10
|
+
from src.encoding.buffer_writer import BufferWriter
|
|
11
|
+
from src.keys.private_key import PrivateKey
|
|
12
|
+
from src.utils.buffer_utils import *
|
|
13
|
+
from src.utils.common_utils import is_hex_string
|
|
14
|
+
from src.utils.num_utils import to_buffer
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class GroupIdFlag(Enum):
|
|
18
|
+
NONE = 0
|
|
19
|
+
COVENANT = 1 # covenants/encumberances -- output script template must match input
|
|
20
|
+
HOLDS_NEX = 2 # group inputs and outputs must balance NEX, token quantity MUST be 0
|
|
21
|
+
GROUP_RESERVED_BITS = 0xFFFF & ~(1 | 2)
|
|
22
|
+
DEFAULT = NONE
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@unique
|
|
26
|
+
class GroupIdType(Enum):
|
|
27
|
+
"""
|
|
28
|
+
Represent Group creation id type in OP_RETURN script
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
LEGACY = 88888888
|
|
32
|
+
NRC1 = 88888890
|
|
33
|
+
NRC2 = 88888891
|
|
34
|
+
NRC3 = 88888892
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
AUTH_FLAGS = {
|
|
38
|
+
# Is this a controller utxo (forces negative number in amount)
|
|
39
|
+
"AUTHORITY": 1 << 63,
|
|
40
|
+
# Can mint tokens
|
|
41
|
+
"MINT": 1 << 62,
|
|
42
|
+
# Can melt tokens
|
|
43
|
+
"MELT": 1 << 61,
|
|
44
|
+
# Can create authorities
|
|
45
|
+
"BATON": 1 << 60,
|
|
46
|
+
# Can change the redeem script
|
|
47
|
+
"RESCRIPT": 1 << 59,
|
|
48
|
+
# Can create subgroups
|
|
49
|
+
"SUBGROUP": 1 << 58,
|
|
50
|
+
"NONE": 0,
|
|
51
|
+
"ALL_FLAG_BITS": 0xFFFF << (64 - 16),
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
ACTIVE_FLAG_BITS = (
|
|
55
|
+
AUTH_FLAGS["AUTHORITY"]
|
|
56
|
+
| AUTH_FLAGS["MINT"]
|
|
57
|
+
| AUTH_FLAGS["MELT"]
|
|
58
|
+
| AUTH_FLAGS["BATON"]
|
|
59
|
+
| AUTH_FLAGS["RESCRIPT"]
|
|
60
|
+
| AUTH_FLAGS["SUBGROUP"]
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class GroupToken(FrozenClass):
|
|
65
|
+
"""
|
|
66
|
+
A util class with methods for group tokenization.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
PARENT_GROUP_ID_SIZE = 32
|
|
70
|
+
|
|
71
|
+
authFlags = {
|
|
72
|
+
**AUTH_FLAGS,
|
|
73
|
+
# Has all permissions
|
|
74
|
+
"ACTIVE_FLAG_BITS": ACTIVE_FLAG_BITS,
|
|
75
|
+
"RESERVED_FLAG_BITS": ACTIVE_FLAG_BITS & ~AUTH_FLAGS["ALL_FLAG_BITS"],
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
def __init__(self):
|
|
79
|
+
super().__init__()
|
|
80
|
+
self._freeze_attributes()
|
|
81
|
+
|
|
82
|
+
@staticmethod
|
|
83
|
+
def find_group_id(
|
|
84
|
+
outpoint, op_return_script, auth_flag, id_flag=GroupIdFlag.DEFAULT
|
|
85
|
+
) -> GroupIdData:
|
|
86
|
+
"""
|
|
87
|
+
Calculate a group ID based on the provided inputs. Pass 'null' to opReturnScript if there is not
|
|
88
|
+
going to be an OP_RETURN output in the transaction.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
outpoint The input outpoint hash hex or buffer
|
|
92
|
+
opReturnScript opReturn output script
|
|
93
|
+
authFlag group authority flag (use {@link GroupToken.authFlags})
|
|
94
|
+
idFlag group id flag
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
Object with group id hash buffer and the nonce bigint
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
group_flags = 0
|
|
101
|
+
nonce = 0
|
|
102
|
+
|
|
103
|
+
if isinstance(outpoint, str):
|
|
104
|
+
outpoint = bytearray.fromhex(outpoint)[::-1]
|
|
105
|
+
if isinstance(op_return_script, Script):
|
|
106
|
+
op_return_script = op_return_script.to_buffer()
|
|
107
|
+
|
|
108
|
+
while True:
|
|
109
|
+
writer = BufferWriter()
|
|
110
|
+
nonce += 1
|
|
111
|
+
nonce = (nonce & ~GroupToken.authFlags["ALL_FLAG_BITS"]) | auth_flag
|
|
112
|
+
|
|
113
|
+
writer.write(outpoint)
|
|
114
|
+
if op_return_script is not None:
|
|
115
|
+
writer.write_var_length_buf(op_return_script)
|
|
116
|
+
writer.write_uint64_le(nonce)
|
|
117
|
+
|
|
118
|
+
hash = Hash.sha256sha256(writer.to_buffer())
|
|
119
|
+
group_flags = (hash[30] << 8) | hash[31]
|
|
120
|
+
if group_flags == id_flag.value:
|
|
121
|
+
break
|
|
122
|
+
|
|
123
|
+
data = GroupIdData()
|
|
124
|
+
data.hash_buffer = bytearray(hash)
|
|
125
|
+
data.nonce = nonce
|
|
126
|
+
return data
|
|
127
|
+
|
|
128
|
+
@staticmethod
|
|
129
|
+
def _get_group_address_buffer(group) -> bytearray:
|
|
130
|
+
if isinstance(group, (bytes, bytearray)):
|
|
131
|
+
return group
|
|
132
|
+
if isinstance(group, str) and is_hex_string(group):
|
|
133
|
+
return bytearray.fromhex(group)
|
|
134
|
+
|
|
135
|
+
group_address = Address(group)
|
|
136
|
+
if not group_address.is_group_identifier_address():
|
|
137
|
+
raise TypeError("Invalid group address")
|
|
138
|
+
return group_address.data
|
|
139
|
+
|
|
140
|
+
@staticmethod
|
|
141
|
+
def generate_subgroup_id(group, data) -> bytearray:
|
|
142
|
+
"""
|
|
143
|
+
Translates a group and additional data into a subgroup identifier
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
group the group/token address or data buffer
|
|
147
|
+
data the additional data
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
the subgroup id buffer
|
|
151
|
+
"""
|
|
152
|
+
if group is None:
|
|
153
|
+
raise TypeError("group is missing")
|
|
154
|
+
if data is None:
|
|
155
|
+
raise TypeError("data is missing")
|
|
156
|
+
|
|
157
|
+
group = GroupToken._get_group_address_buffer(group)
|
|
158
|
+
|
|
159
|
+
if isinstance(data, (bytes, bytearray)):
|
|
160
|
+
return concat([group, data])
|
|
161
|
+
|
|
162
|
+
if isinstance(data, int):
|
|
163
|
+
bn = data.to_bytes(8, "little")
|
|
164
|
+
return concat([group, bn])
|
|
165
|
+
|
|
166
|
+
if is_hex_string(data):
|
|
167
|
+
return concat([group, bytearray.fromhex(data)])
|
|
168
|
+
|
|
169
|
+
return concat([group, data.encode("utf-8")])
|
|
170
|
+
|
|
171
|
+
@staticmethod
|
|
172
|
+
def get_parent_group_id(subgroup) -> bytearray:
|
|
173
|
+
"""
|
|
174
|
+
Extract the parent group from the provided subgroup.
|
|
175
|
+
If the input is a group but not subgroup, the group itself return.
|
|
176
|
+
|
|
177
|
+
Args:
|
|
178
|
+
subgroup the subgroup address or data buffer
|
|
179
|
+
|
|
180
|
+
Returns:
|
|
181
|
+
the GroupId buffer
|
|
182
|
+
"""
|
|
183
|
+
buf = GroupToken._get_group_address_buffer(subgroup)
|
|
184
|
+
if len(buf) < GroupToken.PARENT_GROUP_ID_SIZE:
|
|
185
|
+
raise Exception("Invalid subgroup")
|
|
186
|
+
return bytearray(buf[:32])
|
|
187
|
+
|
|
188
|
+
@staticmethod
|
|
189
|
+
def get_amount_buffer(amount: int) -> bytearray:
|
|
190
|
+
"""
|
|
191
|
+
Will return the token amount as a little endian buffer using the
|
|
192
|
+
minimum number of bytes needed to represent it.
|
|
193
|
+
|
|
194
|
+
Args:
|
|
195
|
+
amount: the token amount (can be negative)
|
|
196
|
+
|
|
197
|
+
Returns:
|
|
198
|
+
a little endian buffer of the amount
|
|
199
|
+
"""
|
|
200
|
+
if amount < 0:
|
|
201
|
+
return (amount & 0xFFFFFFFFFFFFFFFF).to_bytes(8, "little")
|
|
202
|
+
elif amount < 0x10000:
|
|
203
|
+
return amount.to_bytes(2, "little")
|
|
204
|
+
elif amount < 0x100000000:
|
|
205
|
+
return amount.to_bytes(4, "little")
|
|
206
|
+
else:
|
|
207
|
+
return amount.to_bytes(8, "little")
|
|
208
|
+
|
|
209
|
+
@staticmethod
|
|
210
|
+
def get_amount_value(amount_buf: bytearray, unsigned: bool = False) -> int:
|
|
211
|
+
"""
|
|
212
|
+
Get group amount value from Buffer
|
|
213
|
+
|
|
214
|
+
@param amount_buf the amount buffer
|
|
215
|
+
@param unsigned return value as unsigned bigint, default to false
|
|
216
|
+
"""
|
|
217
|
+
amount = int.from_bytes(bytes(amount_buf), "little")
|
|
218
|
+
return amount if unsigned else (amount - (1 << 64) if amount >= (1 << 63) else amount)
|
|
219
|
+
|
|
220
|
+
@staticmethod
|
|
221
|
+
def get_nonce(auth_flag: int) -> int:
|
|
222
|
+
auth_flag = auth_flag & 0xFFFFFFFFFFFFFFFF
|
|
223
|
+
return auth_flag & ~GroupToken.authFlags["ALL_FLAG_BITS"]
|
|
224
|
+
|
|
225
|
+
@staticmethod
|
|
226
|
+
def has_id_flag(group_id, group_id_flag: GroupIdFlag) -> bool:
|
|
227
|
+
"""
|
|
228
|
+
Check if the group id has the flag
|
|
229
|
+
|
|
230
|
+
Args:
|
|
231
|
+
groupId the group id address or data buffer
|
|
232
|
+
groupIdFlag the group id flag
|
|
233
|
+
|
|
234
|
+
Return:
|
|
235
|
+
true if this group id has the flag
|
|
236
|
+
"""
|
|
237
|
+
group_id = GroupToken._get_group_address_buffer(group_id)
|
|
238
|
+
return (
|
|
239
|
+
len(group_id) >= GroupToken.PARENT_GROUP_ID_SIZE
|
|
240
|
+
and (((group_id[30] << 8) | group_id[31]) & group_id_flag.value) == group_id_flag.value
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
@staticmethod
|
|
244
|
+
def is_group_creation(group_id, auth_flag, group_id_flag=GroupIdFlag.DEFAULT) -> bool:
|
|
245
|
+
"""
|
|
246
|
+
Check if this authority and flag fit to this group creation
|
|
247
|
+
|
|
248
|
+
Args:
|
|
249
|
+
groupId the group id address or data buffer
|
|
250
|
+
authFlag the output group quantity/authority
|
|
251
|
+
groupIdFlag optional. the group id flag
|
|
252
|
+
|
|
253
|
+
Returns:
|
|
254
|
+
true if this is group creation data
|
|
255
|
+
"""
|
|
256
|
+
group_id = GroupToken._get_group_address_buffer(group_id)
|
|
257
|
+
auth_flag = auth_flag & 0xFFFFFFFFFFFFFFFF
|
|
258
|
+
has_nonce = GroupToken.get_nonce(auth_flag) != 0
|
|
259
|
+
is_auth = GroupToken.is_authority(auth_flag)
|
|
260
|
+
has_flag = GroupToken.has_id_flag(group_id, group_id_flag)
|
|
261
|
+
return is_auth and has_nonce and has_flag
|
|
262
|
+
|
|
263
|
+
@staticmethod
|
|
264
|
+
def is_subgroup(group_id) -> bool:
|
|
265
|
+
"""
|
|
266
|
+
returns true if this group id is subgroup
|
|
267
|
+
"""
|
|
268
|
+
group_id = GroupToken._get_group_address_buffer(group_id)
|
|
269
|
+
return len(group_id) > GroupToken.PARENT_GROUP_ID_SIZE
|
|
270
|
+
|
|
271
|
+
@staticmethod
|
|
272
|
+
def is_authority(auth_flag: int) -> bool:
|
|
273
|
+
"""
|
|
274
|
+
returns true if this is authority flag
|
|
275
|
+
"""
|
|
276
|
+
return (auth_flag & GroupToken.authFlags["AUTHORITY"]) == GroupToken.authFlags["AUTHORITY"]
|
|
277
|
+
|
|
278
|
+
@staticmethod
|
|
279
|
+
def allows_mint(auth_flag: int) -> bool:
|
|
280
|
+
"""
|
|
281
|
+
returns true if this flag allows minting
|
|
282
|
+
"""
|
|
283
|
+
return (auth_flag & (GroupToken.authFlags["AUTHORITY"] | GroupToken.authFlags["MINT"])) == (
|
|
284
|
+
GroupToken.authFlags["AUTHORITY"] | GroupToken.authFlags["MINT"]
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
@staticmethod
|
|
288
|
+
def allows_melt(auth_flag: int) -> bool:
|
|
289
|
+
"""
|
|
290
|
+
returns true if this flag allows melting.
|
|
291
|
+
"""
|
|
292
|
+
return (auth_flag & (GroupToken.authFlags["AUTHORITY"] | GroupToken.authFlags["MELT"])) == (
|
|
293
|
+
GroupToken.authFlags["AUTHORITY"] | GroupToken.authFlags["MELT"]
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
@staticmethod
|
|
297
|
+
def allows_renew(auth_flag: int) -> bool:
|
|
298
|
+
"""
|
|
299
|
+
returns true if this flag allows creation of authorities.
|
|
300
|
+
"""
|
|
301
|
+
return (
|
|
302
|
+
auth_flag & (GroupToken.authFlags["AUTHORITY"] | GroupToken.authFlags["BATON"])
|
|
303
|
+
) == (GroupToken.authFlags["AUTHORITY"] | GroupToken.authFlags["BATON"])
|
|
304
|
+
|
|
305
|
+
@staticmethod
|
|
306
|
+
def allows_rescript(auth_flag: int) -> bool:
|
|
307
|
+
"""
|
|
308
|
+
returns true if this flag allows rescripting.
|
|
309
|
+
"""
|
|
310
|
+
return (
|
|
311
|
+
auth_flag & (GroupToken.authFlags["AUTHORITY"] | GroupToken.authFlags["RESCRIPT"])
|
|
312
|
+
) == (GroupToken.authFlags["AUTHORITY"] | GroupToken.authFlags["RESCRIPT"])
|
|
313
|
+
|
|
314
|
+
@staticmethod
|
|
315
|
+
def allows_subgroup(auth_flag: int) -> bool:
|
|
316
|
+
"""
|
|
317
|
+
returns true if this flag allows subgroups
|
|
318
|
+
"""
|
|
319
|
+
return (
|
|
320
|
+
auth_flag & (GroupToken.authFlags["AUTHORITY"] | GroupToken.authFlags["SUBGROUP"])
|
|
321
|
+
) == (GroupToken.authFlags["AUTHORITY"] | GroupToken.authFlags["SUBGROUP"])
|
|
322
|
+
|
|
323
|
+
@staticmethod
|
|
324
|
+
def verify_json_doc(json_doc, address, signature=None) -> bool:
|
|
325
|
+
"""
|
|
326
|
+
Verify token description document json signature
|
|
327
|
+
|
|
328
|
+
Args:@returns
|
|
329
|
+
jsonDoc the json TDD as string
|
|
330
|
+
address nexa address that signed the doc
|
|
331
|
+
signature the signature string. optional - if empty, extract from jsonDoc
|
|
332
|
+
|
|
333
|
+
Returns:
|
|
334
|
+
true if signature match
|
|
335
|
+
"""
|
|
336
|
+
if json_doc == "":
|
|
337
|
+
raise TypeError("jsonDoc is missing")
|
|
338
|
+
if address is None:
|
|
339
|
+
raise TypeError("group is missing")
|
|
340
|
+
|
|
341
|
+
json_str = json_doc[json_doc.index("{") : json_doc.rindex("}") + 1]
|
|
342
|
+
if signature is None or signature == "":
|
|
343
|
+
signature = json.loads(json_doc)[1]
|
|
344
|
+
msg = Message(json_str)
|
|
345
|
+
return msg.verify(address, signature)
|
|
346
|
+
|
|
347
|
+
@staticmethod
|
|
348
|
+
def sign_json_doc(json_doc, priv_key: PrivateKey) -> str:
|
|
349
|
+
"""
|
|
350
|
+
Sign token description document json
|
|
351
|
+
|
|
352
|
+
Args:
|
|
353
|
+
jsonDoc the json TDD as string
|
|
354
|
+
privKey private key to sign on the doc
|
|
355
|
+
|
|
356
|
+
Returns:
|
|
357
|
+
the signature string
|
|
358
|
+
"""
|
|
359
|
+
if json_doc == "":
|
|
360
|
+
raise TypeError("jsonDoc is missing")
|
|
361
|
+
json_str = json_doc[json_doc.index("{") : json_doc.rindex("}") + 1] # trimming
|
|
362
|
+
msg = Message(json_str)
|
|
363
|
+
return msg.sign(priv_key)
|
src/core/message.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import json
|
|
3
|
+
|
|
4
|
+
from src.common.class_types import TypedClass
|
|
5
|
+
from src.core.address.address import Address
|
|
6
|
+
from src.crypto.ecdsa import ECDSA
|
|
7
|
+
from src.crypto.hash import Hash
|
|
8
|
+
from src.crypto.signature import Signature
|
|
9
|
+
from src.encoding.buffer_writer import BufferWriter
|
|
10
|
+
from src.keys.private_key import PrivateKey
|
|
11
|
+
from src.keys.public_key import PublicKey
|
|
12
|
+
from src.utils.buffer_utils import *
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Message(TypedClass):
|
|
16
|
+
message: str
|
|
17
|
+
error: str
|
|
18
|
+
|
|
19
|
+
MAGIC_BYTES = bytearray(b"Bitcoin Signed Message:\n")
|
|
20
|
+
|
|
21
|
+
def __init__(self, message: str):
|
|
22
|
+
if not isinstance(message, str):
|
|
23
|
+
raise TypeError("First argument should be a string")
|
|
24
|
+
super().__init__()
|
|
25
|
+
self._enforce_attribute_types()
|
|
26
|
+
self.message = message
|
|
27
|
+
self.error = None
|
|
28
|
+
|
|
29
|
+
# Will sign a message with a given private key.
|
|
30
|
+
#
|
|
31
|
+
# @param privateKey An instance of PrivateKey
|
|
32
|
+
# @returns A base64 encoded compact signature
|
|
33
|
+
def sign(self, private_key: PrivateKey) -> str:
|
|
34
|
+
signature = self._sign(private_key)
|
|
35
|
+
return base64.b64encode(bytes(signature.to_compact())).decode("ascii")
|
|
36
|
+
|
|
37
|
+
# Will return a boolean of the signature is valid for a given nexa address.
|
|
38
|
+
# If it isn't valid, the specific reason is accessible via the "error" member.
|
|
39
|
+
#
|
|
40
|
+
# @param nexaAddress A nexa address
|
|
41
|
+
# @param signatureString A base64 encoded compact signature
|
|
42
|
+
def verify(self, nexa_address, signature_string: str) -> bool:
|
|
43
|
+
if nexa_address is None:
|
|
44
|
+
raise TypeError("nexaAddress")
|
|
45
|
+
if not isinstance(signature_string, str) or len(signature_string) == 0:
|
|
46
|
+
raise TypeError("signatureString")
|
|
47
|
+
|
|
48
|
+
if isinstance(nexa_address, str):
|
|
49
|
+
nexa_address = Address.from_string(nexa_address)
|
|
50
|
+
|
|
51
|
+
signature = Signature.from_compact(bytearray(base64.b64decode(signature_string)))
|
|
52
|
+
|
|
53
|
+
# recover the public key
|
|
54
|
+
ecdsa = ECDSA()
|
|
55
|
+
ecdsa.hashbuf = self._magic_hash()
|
|
56
|
+
ecdsa.sig = signature
|
|
57
|
+
public_key = ecdsa.to_public_key()
|
|
58
|
+
|
|
59
|
+
signature_address = Address.from_public_key(
|
|
60
|
+
public_key, nexa_address.network, nexa_address.type
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
# check that the recovered address and specified address match
|
|
64
|
+
if nexa_address.to_string() != signature_address.to_string():
|
|
65
|
+
self.error = "The signature did not match the message digest"
|
|
66
|
+
return False
|
|
67
|
+
|
|
68
|
+
return self._verify(public_key, signature)
|
|
69
|
+
|
|
70
|
+
def _sign(self, private_key: PrivateKey) -> Signature:
|
|
71
|
+
if not isinstance(private_key, PrivateKey):
|
|
72
|
+
raise TypeError("First argument should be an instance of PrivateKey")
|
|
73
|
+
hash = self._magic_hash()
|
|
74
|
+
ecdsa = ECDSA()
|
|
75
|
+
ecdsa.hashbuf = hash
|
|
76
|
+
ecdsa.privkey = private_key
|
|
77
|
+
ecdsa.pubkey = private_key.to_public_key()
|
|
78
|
+
ecdsa.sign_random_k()
|
|
79
|
+
ecdsa.calc_i()
|
|
80
|
+
return ecdsa.sig
|
|
81
|
+
|
|
82
|
+
def _magic_hash(self) -> bytearray:
|
|
83
|
+
prefix1 = BufferWriter.write_varint_buf(len(Message.MAGIC_BYTES))
|
|
84
|
+
message_buffer = self.message.encode("utf-8")
|
|
85
|
+
prefix2 = BufferWriter.write_varint_buf(len(message_buffer))
|
|
86
|
+
buf = concat([prefix1, Message.MAGIC_BYTES, prefix2, message_buffer])
|
|
87
|
+
return Hash.sha256sha256(buf)
|
|
88
|
+
|
|
89
|
+
def _verify(self, public_key: PublicKey, signature: Signature) -> bool:
|
|
90
|
+
if not isinstance(public_key, PublicKey):
|
|
91
|
+
raise TypeError("First argument should be an instance of PublicKey")
|
|
92
|
+
if not isinstance(signature, Signature):
|
|
93
|
+
raise TypeError("Second argument should be an instance of Signature")
|
|
94
|
+
hash = self._magic_hash()
|
|
95
|
+
verified = ECDSA.verify(hash, signature, public_key)
|
|
96
|
+
if not verified:
|
|
97
|
+
self.error = "The signature was invalid"
|
|
98
|
+
return verified
|
|
99
|
+
|
|
100
|
+
# Instantiate a message from a message string
|
|
101
|
+
#
|
|
102
|
+
# @param str A string of the message
|
|
103
|
+
# @returns A new instance of a Message
|
|
104
|
+
@staticmethod
|
|
105
|
+
def from_string(string: str) -> "Message":
|
|
106
|
+
return Message(string)
|
|
107
|
+
|
|
108
|
+
# Instantiate a message from JSON
|
|
109
|
+
#
|
|
110
|
+
# @param json An JSON string or Object with keys: message
|
|
111
|
+
# @returns A new instance of a Message
|
|
112
|
+
@staticmethod
|
|
113
|
+
def from_json(json_data) -> "Message":
|
|
114
|
+
if isinstance(json_data, str):
|
|
115
|
+
json_data = json.loads(json_data)
|
|
116
|
+
return Message(json_data["message"])
|
|
117
|
+
|
|
118
|
+
# @returns A plain object with the message information
|
|
119
|
+
def to_object(self) -> dict:
|
|
120
|
+
return {"message": self.message}
|
|
121
|
+
|
|
122
|
+
# @returns A JSON representation as string of the message information
|
|
123
|
+
def to_json(self) -> str:
|
|
124
|
+
return json.dumps(self.to_object())
|
|
125
|
+
|
|
126
|
+
# Will return a the string representation of the message
|
|
127
|
+
def to_string(self) -> str:
|
|
128
|
+
return self.message
|
|
129
|
+
|
|
130
|
+
def __str__(self) -> str:
|
|
131
|
+
return f"<Message: {self}>"
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
from src.common.class_types import FrozenClass, TypedClass
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class NetworkParams(TypedClass):
|
|
5
|
+
name: str
|
|
6
|
+
alias: str
|
|
7
|
+
prefix: str
|
|
8
|
+
pubkey_hash: int
|
|
9
|
+
private_key: int
|
|
10
|
+
script_hash: int
|
|
11
|
+
xpubkey: int
|
|
12
|
+
xprivkey: int
|
|
13
|
+
network_magic: int
|
|
14
|
+
port: int
|
|
15
|
+
dns_seeds: list
|
|
16
|
+
|
|
17
|
+
def __init__(self):
|
|
18
|
+
super().__init__()
|
|
19
|
+
self.name = None
|
|
20
|
+
self.alias = None
|
|
21
|
+
self.prefix = None
|
|
22
|
+
self.pubkey_hash = None
|
|
23
|
+
self.private_key = None
|
|
24
|
+
self.script_hash = None
|
|
25
|
+
self.xpubkey = None
|
|
26
|
+
self.xprivkey = None
|
|
27
|
+
self.network_magic = None
|
|
28
|
+
self.port = None
|
|
29
|
+
self.dns_seeds = None
|
|
30
|
+
self._enforce_attribute_types()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Network(FrozenClass):
|
|
34
|
+
def __init__(self, params: NetworkParams | dict):
|
|
35
|
+
super().__init__()
|
|
36
|
+
if isinstance(params, dict):
|
|
37
|
+
network_params = NetworkParams()
|
|
38
|
+
network_params.name = params.get("name")
|
|
39
|
+
network_params.alias = params.get("alias")
|
|
40
|
+
network_params.prefix = params.get("prefix")
|
|
41
|
+
network_params.pubkey_hash = params.get("pubkey_hash")
|
|
42
|
+
network_params.private_key = params.get("private_key")
|
|
43
|
+
network_params.script_hash = params.get("script_hash")
|
|
44
|
+
network_params.xpubkey = params.get("xpubkey")
|
|
45
|
+
network_params.xprivkey = params.get("xprivkey")
|
|
46
|
+
network_params.network_magic = params.get("network_magic")
|
|
47
|
+
network_params.port = params.get("port")
|
|
48
|
+
network_params.dns_seeds = params.get("dns_seeds")
|
|
49
|
+
params = network_params
|
|
50
|
+
self.name: str = params.name
|
|
51
|
+
self.alias: str = params.alias
|
|
52
|
+
self.prefix: str = params.prefix
|
|
53
|
+
self.pubkey_hash: int = params.pubkey_hash
|
|
54
|
+
self.private_key: int = params.private_key
|
|
55
|
+
self.script_hash: int = params.script_hash
|
|
56
|
+
self.xpubkey: int = params.xpubkey
|
|
57
|
+
self.xprivkey: int = params.xprivkey
|
|
58
|
+
self.network_magic: int = params.network_magic
|
|
59
|
+
self.port: int = params.port
|
|
60
|
+
self.dns_seeds: list() = params.dns_seeds
|
|
61
|
+
self._freeze_attributes()
|
|
62
|
+
|
|
63
|
+
def __str__(self):
|
|
64
|
+
return self.name
|
|
65
|
+
|
|
66
|
+
def to_string(self) -> str:
|
|
67
|
+
return self.name
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
live_network_params = NetworkParams()
|
|
71
|
+
live_network_params.name = "mainnet"
|
|
72
|
+
live_network_params.alias = "livenet"
|
|
73
|
+
live_network_params.prefix = "nexa"
|
|
74
|
+
live_network_params.pubkey_hash = 0x19
|
|
75
|
+
live_network_params.private_key = 0x23
|
|
76
|
+
live_network_params.script_hash = 0x44
|
|
77
|
+
live_network_params.xpubkey = 0x42696720
|
|
78
|
+
live_network_params.xprivkey = 0x426C6B73
|
|
79
|
+
live_network_params.network_magic = 0x72271221
|
|
80
|
+
live_network_params.port = 7228
|
|
81
|
+
live_network_params.dns_seeds = [ # from https://gitlab.com/nexa/nexa/-/blob/dev/src/chainparams.cpp#L592
|
|
82
|
+
"seed.nextchain.cash",
|
|
83
|
+
"seeder.nexa.org",
|
|
84
|
+
"nexa-seeder.bitcoinunlimited.info",
|
|
85
|
+
]
|
|
86
|
+
LiveNetwork = Network(live_network_params)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
testnet_params = NetworkParams()
|
|
90
|
+
testnet_params.name = "testnet"
|
|
91
|
+
testnet_params.alias = "testnet"
|
|
92
|
+
testnet_params.prefix = "nexatest"
|
|
93
|
+
testnet_params.pubkey_hash = 0x6F
|
|
94
|
+
testnet_params.private_key = 0xEF
|
|
95
|
+
testnet_params.script_hash = 0xC4
|
|
96
|
+
testnet_params.xpubkey = 0x043587CF
|
|
97
|
+
testnet_params.xprivkey = 0x04358394
|
|
98
|
+
testnet_params.network_magic = 0x72271222
|
|
99
|
+
testnet_params.port = 7230
|
|
100
|
+
testnet_params.dns_seeds = ["nexa-testnet-seeder.bitcoinunlimited.info", "testnetseeder.nexa.org"]
|
|
101
|
+
TestNetwork = Network(testnet_params)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
regtest_params = NetworkParams()
|
|
105
|
+
regtest_params.name = "regtest"
|
|
106
|
+
regtest_params.alias = "regtest"
|
|
107
|
+
regtest_params.prefix = "nexareg"
|
|
108
|
+
regtest_params.pubkey_hash = 0x6F
|
|
109
|
+
regtest_params.private_key = 0xEF
|
|
110
|
+
regtest_params.script_hash = 0xC4
|
|
111
|
+
regtest_params.xpubkey = 0x043587CF
|
|
112
|
+
regtest_params.xprivkey = 0x04358394
|
|
113
|
+
regtest_params.network_magic = 0xEAE5EFEA
|
|
114
|
+
regtest_params.port = 18444
|
|
115
|
+
regtest_params.dns_seeds: [
|
|
116
|
+
# Regtest mode doesn't have any DNS seeds.
|
|
117
|
+
]
|
|
118
|
+
RegtestNetwork = Network(regtest_params)
|