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.
Files changed (56) hide show
  1. libnexapy_test-0.1.0.dist-info/METADATA +104 -0
  2. libnexapy_test-0.1.0.dist-info/RECORD +56 -0
  3. libnexapy_test-0.1.0.dist-info/WHEEL +5 -0
  4. libnexapy_test-0.1.0.dist-info/licenses/LICENCE +22 -0
  5. libnexapy_test-0.1.0.dist-info/top_level.txt +1 -0
  6. src/__init__.py +0 -0
  7. src/common/__init__.py +2 -0
  8. src/common/class_types.py +60 -0
  9. src/common/types.py +351 -0
  10. src/core/address/address.py +380 -0
  11. src/core/address/address_formatter.py +293 -0
  12. src/core/block/block.py +158 -0
  13. src/core/block/block_header.py +257 -0
  14. src/core/grouptoken.py +363 -0
  15. src/core/message.py +131 -0
  16. src/core/network/network.py +118 -0
  17. src/core/network/network_manager.py +131 -0
  18. src/core/script/opcode.py +312 -0
  19. src/core/script/script.py +841 -0
  20. src/core/script/script_factory.py +385 -0
  21. src/core/transaction/input/input.py +134 -0
  22. src/core/transaction/input/public_key_hash.py +47 -0
  23. src/core/transaction/input/public_key_template.py +47 -0
  24. src/core/transaction/input/script_template.py +154 -0
  25. src/core/transaction/output.py +161 -0
  26. src/core/transaction/sighash_type.py +246 -0
  27. src/core/transaction/transaction.py +506 -0
  28. src/core/transaction/tx_builder.py +228 -0
  29. src/core/transaction/tx_signature.py +85 -0
  30. src/core/transaction/tx_signer.py +164 -0
  31. src/core/transaction/unspent_output.py +79 -0
  32. src/crypto/__init__.py +3 -0
  33. src/crypto/curve_utils.py +43 -0
  34. src/crypto/digital_signature.py +73 -0
  35. src/crypto/ecc_fast.py +175 -0
  36. src/crypto/ecdsa.py +310 -0
  37. src/crypto/hash.py +64 -0
  38. src/crypto/libsecp256k1.so.0 +0 -0
  39. src/crypto/ripemd160.py +408 -0
  40. src/crypto/schnorr.py +509 -0
  41. src/crypto/secp256k1.py +223 -0
  42. src/crypto/signature.py +244 -0
  43. src/encoding/base32.py +51 -0
  44. src/encoding/base58.py +165 -0
  45. src/encoding/base58_check.py +92 -0
  46. src/encoding/buffer_reader.py +159 -0
  47. src/encoding/buffer_writer.py +138 -0
  48. src/keys/hd_private_key.py +473 -0
  49. src/keys/hd_public_key.py +417 -0
  50. src/keys/hdkey_utils.py +139 -0
  51. src/keys/private_key.py +340 -0
  52. src/keys/public_key.py +394 -0
  53. src/utils/buffer_utils.py +47 -0
  54. src/utils/common_utils.py +4 -0
  55. src/utils/num_utils.py +81 -0
  56. src/utils/unit_utils.py +78 -0
@@ -0,0 +1,131 @@
1
+ from typing import Self
2
+
3
+ from src.common.class_types import FrozenClass, TypedClass
4
+ from src.common.types import Networkish
5
+ from src.core.network.network import (
6
+ NetworkParams,
7
+ Network,
8
+ LiveNetwork,
9
+ TestNetwork,
10
+ RegtestNetwork,
11
+ )
12
+
13
+
14
+ class NetworkManager(TypedClass):
15
+ """
16
+ NetworkManager is a singleton service, containing map values that correspond to version
17
+ numbers for each nexa network. Currently only supporting "mainnet"
18
+ (a.k.a. "livenet") and "testnet", with option to add custom networks.
19
+
20
+ @remarks should be used as singleton.
21
+
22
+ @see {@linkcode NetworkManager.getInstance}
23
+ """
24
+
25
+ _instance = None
26
+ _networks = [LiveNetwork, TestNetwork, RegtestNetwork]
27
+
28
+ default_network: Network
29
+
30
+ def __new__(cls):
31
+ if not isinstance(cls._instance, cls):
32
+ cls._instance = object.__new__(cls)
33
+ return cls._instance
34
+
35
+ def __init__(self):
36
+ super().__init__()
37
+ self._default_network = LiveNetwork
38
+ self._enforce_attribute_types()
39
+
40
+ @property
41
+ def mainnet(self) -> Network:
42
+ return LiveNetwork
43
+
44
+ @property
45
+ def testnet(self) -> Network:
46
+ return TestNetwork
47
+
48
+ @property
49
+ def regtest(self) -> Network:
50
+ return RegtestNetwork
51
+
52
+ @property
53
+ def default_network(self) -> Network:
54
+ return self._default_network
55
+
56
+ @default_network.setter
57
+ def default_network(self, network: Network):
58
+ self._default_network = network
59
+
60
+ @classmethod
61
+ def get_instance(cls) -> Self:
62
+ return cls._instance
63
+
64
+ # Networkish = TypeVar('Networkish', str | Network)
65
+ def get(self, arg: Networkish | int = None, key: str = None) -> Network | None:
66
+ if arg is None and key is None:
67
+ return None
68
+
69
+ if isinstance(arg, Network):
70
+ for network in self._networks:
71
+ if network is arg:
72
+ return network
73
+ for network in self._networks:
74
+ if network.name == arg.name:
75
+ return network
76
+
77
+ if isinstance(arg, str):
78
+ for network in self._networks:
79
+ if getattr(network, "name", None) == arg:
80
+ return network
81
+
82
+ if key is not None:
83
+ for network in self._networks:
84
+ attr = getattr(network, key, None)
85
+ if attr is None:
86
+ continue
87
+ # sanity check the types are the same
88
+ if type(arg) != type(attr):
89
+ raise TypeError(
90
+ "the network attribute of 'key' was a different type than the type of arg"
91
+ )
92
+ if attr == arg:
93
+ return network
94
+ return None
95
+ else:
96
+ # hail mary time
97
+ for network in self._networks:
98
+ for attr_name, attribute in network.__dict__.items():
99
+ if attr_name.startswith("_"):
100
+ continue
101
+ try:
102
+ if attribute == arg:
103
+ return network
104
+ except Exception:
105
+ continue
106
+ return None
107
+
108
+ @staticmethod
109
+ def create(network: NetworkParams) -> Network:
110
+ return Network(network)
111
+
112
+ def add(self, network: Network | NetworkParams | dict):
113
+ if isinstance(network, (NetworkParams, dict)):
114
+ network = Network(network)
115
+ self._networks.append(network)
116
+
117
+ def remove(self, network: Networkish | NetworkParams):
118
+ if isinstance(network, str) or isinstance(network, Network):
119
+ network = self.get(network)
120
+ if network is None:
121
+ return
122
+ indexes_to_remove = []
123
+ for i in range(len(self._networks)):
124
+ if self._networks[i] == network:
125
+ indexes_to_remove.append(i)
126
+ indexes_to_remove.sort(reverse=True)
127
+ for index in indexes_to_remove:
128
+ del self._networks[index]
129
+
130
+
131
+ networks = NetworkManager()
@@ -0,0 +1,312 @@
1
+ from typing import Self
2
+ from enum import Enum, unique
3
+
4
+ from src.common.class_types import FrozenClass
5
+
6
+
7
+ class Opcode(Enum):
8
+ # push value
9
+ OP_FALSE = 0
10
+ OP_0 = OP_FALSE
11
+ OP_PUSHDATA1 = 76
12
+ OP_PUSHDATA2 = 77
13
+ OP_PUSHDATA4 = 78
14
+ OP_1NEGATE = 79
15
+ OP_RESERVED = 80
16
+ OP_TRUE = 81
17
+ OP_1 = OP_TRUE
18
+ OP_2 = 82
19
+ OP_3 = 83
20
+ OP_4 = 84
21
+ OP_5 = 85
22
+ OP_6 = 86
23
+ OP_7 = 87
24
+ OP_8 = 88
25
+ OP_9 = 89
26
+ OP_10 = 90
27
+ OP_11 = 91
28
+ OP_12 = 92
29
+ OP_13 = 93
30
+ OP_14 = 94
31
+ OP_15 = 95
32
+ OP_16 = 96
33
+
34
+ # control
35
+ OP_NOP = 97
36
+ OP_INVALID_CONTROL1 = 98
37
+ OP_IF = 99
38
+ OP_NOTIF = 100
39
+ OP_JUMP = 101
40
+ OP_INVALID_CONTROL2 = 102
41
+ OP_ELSE = 103
42
+ OP_ENDIF = 104
43
+ OP_VERIFY = 105
44
+ OP_RETURN = 106
45
+
46
+ # stack ops
47
+ OP_TOALTSTACK = 107
48
+ OP_FROMALTSTACK = 108
49
+ OP_2DROP = 109
50
+ OP_2DUP = 110
51
+ OP_3DUP = 111
52
+ OP_2OVER = 112
53
+ OP_2ROT = 113
54
+ OP_2SWAP = 114
55
+ OP_IFDUP = 115
56
+ OP_DEPTH = 116
57
+ OP_DROP = 117
58
+ OP_DUP = 118
59
+ OP_NIP = 119
60
+ OP_OVER = 120
61
+ OP_PICK = 121
62
+ OP_ROLL = 122
63
+ OP_ROT = 123
64
+ OP_SWAP = 124
65
+ OP_TUCK = 125
66
+
67
+ # splice ops
68
+ OP_CAT = 126
69
+ OP_SPLIT = 127
70
+ OP_NUM2BIN = 128
71
+ OP_BIN2NUM = 129
72
+ OP_SIZE = 130
73
+
74
+ # bit logic
75
+ OP_INVERT = 131
76
+ OP_AND = 132
77
+ OP_OR = 133
78
+ OP_XOR = 134
79
+ OP_EQUAL = 135
80
+ OP_EQUALVERIFY = 136
81
+ OP_RESERVED1 = 137
82
+ OP_RESERVED2 = 138
83
+
84
+ # numeric
85
+ OP_1ADD = 139
86
+ OP_1SUB = 140
87
+ OP_2MUL = 141
88
+ OP_2DIV = 142
89
+ OP_NEGATE = 143
90
+ OP_ABS = 144
91
+ OP_NOT = 145
92
+ OP_0NOTEQUAL = 146
93
+
94
+ OP_ADD = 147
95
+ OP_SUB = 148
96
+ OP_MUL = 149
97
+ OP_DIV = 150
98
+ OP_MOD = 151
99
+ OP_LSHIFT = 152
100
+ OP_RSHIFT = 153
101
+
102
+ OP_BOOLAND = 154
103
+ OP_BOOLOR = 155
104
+ OP_NUMEQUAL = 156
105
+ OP_NUMEQUALVERIFY = 157
106
+ OP_NUMNOTEQUAL = 158
107
+ OP_LESSTHAN = 159
108
+ OP_GREATERTHAN = 160
109
+ OP_LESSTHANOREQUAL = 161
110
+ OP_GREATERTHANOREQUAL = 162
111
+ OP_MIN = 163
112
+ OP_MAX = 164
113
+
114
+ OP_WITHIN = 165
115
+
116
+ # crypto
117
+ OP_RIPEMD160 = 166
118
+ OP_SHA1 = 167
119
+ OP_SHA256 = 168
120
+ OP_HASH160 = 169
121
+ OP_HASH256 = 170
122
+ OP_CODESEPARATOR = 171
123
+ OP_CHECKSIG = 172
124
+ OP_CHECKSIGVERIFY = 173
125
+ OP_CHECKMULTISIG = 174
126
+ OP_CHECKMULTISIGVERIFY = 175
127
+
128
+ # timelocks
129
+ OP_NOP2 = 177
130
+ OP_CHECKLOCKTIMEVERIFY = OP_NOP2
131
+ OP_NOP3 = 178
132
+ OP_CHECKSEQUENCEVERIFY = OP_NOP3
133
+
134
+ # expansion
135
+ OP_NOP1 = 176
136
+ OP_NOP4 = 179
137
+ OP_NOP5 = 180
138
+ OP_NOP6 = 181
139
+ OP_NOP7 = 182
140
+ OP_NOP8 = 183
141
+ OP_NOP9 = 184
142
+ OP_NOP10 = 185
143
+
144
+ # More crypto
145
+ OP_CHECKDATASIG = 186
146
+ OP_CHECKDATASIGVERIFY = 187
147
+
148
+ # additional byte string operations
149
+ OP_REVERSEBYTES = 188
150
+
151
+ # Transaction Introspection Opcodes
152
+ OP_INPUTINDEX = 192
153
+ OP_ACTIVEBYTECODE = 193
154
+ OP_TXVERSION = 194
155
+ OP_TXINPUTCOUNT = 195
156
+ OP_TXOUTPUTCOUNT = 196
157
+ OP_TXLOCKTIME = 197
158
+ OP_UTXOVALUE = 198
159
+ OP_UTXOBYTECODE = 199
160
+ OP_OUTPOINTHASH = 200
161
+ OP_INPUTBYTECODE = 202
162
+ OP_INPUTSEQUENCENUMBER = 203
163
+ OP_OUTPUTVALUE = 204
164
+ OP_OUTPUTBYTECODE = 205
165
+ OP_INPUTTYPE = 206
166
+ OP_OUTPUTTYPE = 207
167
+ OP_INPUTVALUE = 208
168
+
169
+ # NEXA opcodes
170
+ OP_PARSE = 230
171
+ OP_STORE = 231
172
+ OP_LOAD = 232
173
+ OP_PLACE = 233
174
+ OP_PUSH_TX_STATE = 234
175
+ OP_SETBMD = 235
176
+ OP_BIN2BIGNUM = 236
177
+ OP_EXEC = 237
178
+ OP_MERKLEROOT = 238
179
+
180
+ # The first op_code value after all defined opcodes
181
+ FIRST_UNDEFINED_OP_VALUE = 239
182
+ OP_INVALIDOPCODE = 255
183
+
184
+ @classmethod
185
+ def _missing_(cls, value):
186
+ for member in cls:
187
+ if member.value == value:
188
+ return member
189
+ return None
190
+
191
+
192
+ OpcodeNameByValue: dict = {}
193
+ for _name, _member in vars(Opcode).items():
194
+ if isinstance(_member, Opcode):
195
+ OpcodeNameByValue[_member.value] = _name
196
+
197
+
198
+ class ScriptOpcode(FrozenClass):
199
+ def __init__(self, val: int | str | Opcode = None):
200
+ super().__init__()
201
+ self.num = None
202
+ if isinstance(val, Opcode):
203
+ self.num = val.value
204
+ elif isinstance(val, int):
205
+ self.num = val
206
+ elif isinstance(val, str):
207
+ try:
208
+ self.num = Opcode[val].value
209
+ except KeyError:
210
+ self.num = None
211
+ elif val is None:
212
+ raise TypeError('Unrecognized val type: "None" for Opcode')
213
+ else:
214
+ raise TypeError('Unrecognized val type: "' + str(type(val)) + '" for Opcode')
215
+ self._freeze_attributes()
216
+
217
+ @staticmethod
218
+ def from_buffer(buf: bytearray) -> Self:
219
+ if not isinstance(buf, (bytearray, bytes)):
220
+ raise ValueError("Invalid Argument: buf must be Buffer.")
221
+ if isinstance(buf, bytes):
222
+ buf = bytearray(buf)
223
+ return ScriptOpcode(int("0x" + buf.hex(), 16))
224
+
225
+ @staticmethod
226
+ def from_number(num: int | Opcode) -> Self:
227
+ if not isinstance(num, (int, Opcode)):
228
+ raise ValueError("Invalid Argument: num must be number.")
229
+ return ScriptOpcode(num)
230
+
231
+ @staticmethod
232
+ def from_string(opcodestr: str) -> Self:
233
+ if not isinstance(opcodestr, str):
234
+ raise TypeError("Invalid Argument: str must be string.")
235
+ try:
236
+ value = Opcode[opcodestr]
237
+ except KeyError:
238
+ raise TypeError("Invalid opcodestr")
239
+ return ScriptOpcode(value.value)
240
+
241
+ @staticmethod
242
+ def small_int(n: int) -> Self:
243
+ if not isinstance(n, int):
244
+ raise TypeError("Invalid Argument: n should be number.")
245
+ if n < 0 or n > 16:
246
+ raise ValueError("Invalid Argument: n must be between 0 and 16.")
247
+ if n == 0:
248
+ return ScriptOpcode("OP_0")
249
+ return ScriptOpcode(Opcode.OP_1.value + n - 1)
250
+
251
+ @staticmethod
252
+ def is_small_int(opcode: Self | int | Opcode) -> bool:
253
+ """
254
+ returns true if opcode is one of OP_0, OP_1, ..., OP_16
255
+ """
256
+ if isinstance(opcode, ScriptOpcode):
257
+ opcode = opcode.to_number()
258
+ if isinstance(opcode, Opcode):
259
+ opcode = opcode.value
260
+ return opcode == Opcode.OP_0.value or (
261
+ opcode >= Opcode.OP_1.value and opcode <= Opcode.OP_16.value
262
+ )
263
+
264
+ def to_hex(self) -> str:
265
+ return format(self.num, "x")
266
+
267
+ def to_buffer(self) -> bytearray:
268
+ if self.num == 0:
269
+ return bytearray()
270
+ return self.num.to_bytes((self.num.bit_length() + 7) // 8, "big")
271
+
272
+ def to_number(self) -> int:
273
+ return self.num
274
+
275
+ def to_string(self) -> str:
276
+ name = OpcodeNameByValue.get(self.num) if self.num is not None else None
277
+ if name is None:
278
+ raise ValueError("Opcode does not have a string representation")
279
+ return name
280
+
281
+ def __str__(self) -> str:
282
+ return (
283
+ "<Opcode: "
284
+ + self.to_string()
285
+ + ", hex: "
286
+ + self.to_hex()
287
+ + ", decimal: "
288
+ + str(self.num)
289
+ + ">"
290
+ )
291
+
292
+ @staticmethod
293
+ def decodeOP_N(opcode: int | Opcode) -> int:
294
+ """
295
+ Comes from nexad's script DecodeOP_N function
296
+
297
+ Args:
298
+ opcode
299
+
300
+ Returns:
301
+ int in range of 0 to 16
302
+ """
303
+ if isinstance(opcode, Opcode):
304
+ opcode = opcode.value
305
+ if not isinstance(opcode, int):
306
+ raise TypeError("opcode must be of type int")
307
+ if opcode == Opcode.OP_0.value:
308
+ return 0
309
+ elif opcode >= Opcode.OP_1.value and opcode <= Opcode.OP_16.value:
310
+ return opcode - (Opcode.OP_1.value - 1)
311
+ else:
312
+ raise ValueError("Invalid opcode: " + str(opcode))