6
6
7
7
This file is modified from python-bitcoinlib.
8
8
"""
9
-
10
- from .messages import CTransaction , CTxOut , sha256 , hash256 , uint256_from_str , ser_uint256 , ser_string
11
-
12
9
import hashlib
13
10
import struct
14
11
15
12
from .bignum import bn2vch
13
+ from .messages import (
14
+ CTransaction ,
15
+ CTxOut ,
16
+ hash256 ,
17
+ ser_string ,
18
+ ser_uint256 ,
19
+ sha256 ,
20
+ uint256_from_str ,
21
+ )
16
22
17
23
MAX_SCRIPT_ELEMENT_SIZE = 520
18
-
19
24
OPCODE_NAMES = {}
20
25
21
26
def hash160 (s ):
@@ -31,13 +36,13 @@ class CScriptOp(int):
31
36
def encode_op_pushdata (d ):
32
37
"""Encode a PUSHDATA op, returning bytes"""
33
38
if len (d ) < 0x4c :
34
- return b'' + bytes ([len (d )]) + d # OP_PUSHDATA
39
+ return b'' + bytes ([len (d )]) + d # OP_PUSHDATA
35
40
elif len (d ) <= 0xff :
36
- return b'\x4c ' + bytes ([len (d )]) + d # OP_PUSHDATA1
41
+ return b'\x4c ' + bytes ([len (d )]) + d # OP_PUSHDATA1
37
42
elif len (d ) <= 0xffff :
38
- return b'\x4d ' + struct .pack (b'<H' , len (d )) + d # OP_PUSHDATA2
43
+ return b'\x4d ' + struct .pack (b'<H' , len (d )) + d # OP_PUSHDATA2
39
44
elif len (d ) <= 0xffffffff :
40
- return b'\x4e ' + struct .pack (b'<I' , len (d )) + d # OP_PUSHDATA4
45
+ return b'\x4e ' + struct .pack (b'<I' , len (d )) + d # OP_PUSHDATA4
41
46
else :
42
47
raise ValueError ("Data too long to encode in a PUSHDATA op" )
43
48
@@ -50,7 +55,7 @@ def encode_op_n(n):
50
55
if n == 0 :
51
56
return OP_0
52
57
else :
53
- return CScriptOp (OP_1 + n - 1 )
58
+ return CScriptOp (OP_1 + n - 1 )
54
59
55
60
def decode_op_n (self ):
56
61
"""Decode a small integer opcode, returning an integer"""
@@ -60,7 +65,7 @@ def decode_op_n(self):
60
65
if not (self == OP_0 or OP_1 <= self <= OP_16 ):
61
66
raise ValueError ('op %r is not an OP_N' % self )
62
67
63
- return int (self - OP_1 + 1 )
68
+ return int (self - OP_1 + 1 )
64
69
65
70
def is_small_int (self ):
66
71
"""Return true if the op pushes a small integer to the stack"""
@@ -87,7 +92,7 @@ def __new__(cls, n):
87
92
return _opcode_instances [n ]
88
93
89
94
# Populate opcode instance table
90
- for n in range (0xff + 1 ):
95
+ for n in range (0xff + 1 ):
91
96
CScriptOp (n )
92
97
93
98
@@ -100,7 +105,7 @@ def __new__(cls, n):
100
105
OP_1NEGATE = CScriptOp (0x4f )
101
106
OP_RESERVED = CScriptOp (0x50 )
102
107
OP_1 = CScriptOp (0x51 )
103
- OP_TRUE = OP_1
108
+ OP_TRUE = OP_1
104
109
OP_2 = CScriptOp (0x52 )
105
110
OP_3 = CScriptOp (0x53 )
106
111
OP_4 = CScriptOp (0x54 )
@@ -232,122 +237,122 @@ def __new__(cls, n):
232
237
OP_INVALIDOPCODE = CScriptOp (0xff )
233
238
234
239
OPCODE_NAMES .update ({
235
- OP_0 : 'OP_0' ,
236
- OP_PUSHDATA1 : 'OP_PUSHDATA1' ,
237
- OP_PUSHDATA2 : 'OP_PUSHDATA2' ,
238
- OP_PUSHDATA4 : 'OP_PUSHDATA4' ,
239
- OP_1NEGATE : 'OP_1NEGATE' ,
240
- OP_RESERVED : 'OP_RESERVED' ,
241
- OP_1 : 'OP_1' ,
242
- OP_2 : 'OP_2' ,
243
- OP_3 : 'OP_3' ,
244
- OP_4 : 'OP_4' ,
245
- OP_5 : 'OP_5' ,
246
- OP_6 : 'OP_6' ,
247
- OP_7 : 'OP_7' ,
248
- OP_8 : 'OP_8' ,
249
- OP_9 : 'OP_9' ,
250
- OP_10 : 'OP_10' ,
251
- OP_11 : 'OP_11' ,
252
- OP_12 : 'OP_12' ,
253
- OP_13 : 'OP_13' ,
254
- OP_14 : 'OP_14' ,
255
- OP_15 : 'OP_15' ,
256
- OP_16 : 'OP_16' ,
257
- OP_NOP : 'OP_NOP' ,
258
- OP_VER : 'OP_VER' ,
259
- OP_IF : 'OP_IF' ,
260
- OP_NOTIF : 'OP_NOTIF' ,
261
- OP_VERIF : 'OP_VERIF' ,
262
- OP_VERNOTIF : 'OP_VERNOTIF' ,
263
- OP_ELSE : 'OP_ELSE' ,
264
- OP_ENDIF : 'OP_ENDIF' ,
265
- OP_VERIFY : 'OP_VERIFY' ,
266
- OP_RETURN : 'OP_RETURN' ,
267
- OP_TOALTSTACK : 'OP_TOALTSTACK' ,
268
- OP_FROMALTSTACK : 'OP_FROMALTSTACK' ,
269
- OP_2DROP : 'OP_2DROP' ,
270
- OP_2DUP : 'OP_2DUP' ,
271
- OP_3DUP : 'OP_3DUP' ,
272
- OP_2OVER : 'OP_2OVER' ,
273
- OP_2ROT : 'OP_2ROT' ,
274
- OP_2SWAP : 'OP_2SWAP' ,
275
- OP_IFDUP : 'OP_IFDUP' ,
276
- OP_DEPTH : 'OP_DEPTH' ,
277
- OP_DROP : 'OP_DROP' ,
278
- OP_DUP : 'OP_DUP' ,
279
- OP_NIP : 'OP_NIP' ,
280
- OP_OVER : 'OP_OVER' ,
281
- OP_PICK : 'OP_PICK' ,
282
- OP_ROLL : 'OP_ROLL' ,
283
- OP_ROT : 'OP_ROT' ,
284
- OP_SWAP : 'OP_SWAP' ,
285
- OP_TUCK : 'OP_TUCK' ,
286
- OP_CAT : 'OP_CAT' ,
287
- OP_SUBSTR : 'OP_SUBSTR' ,
288
- OP_LEFT : 'OP_LEFT' ,
289
- OP_RIGHT : 'OP_RIGHT' ,
290
- OP_SIZE : 'OP_SIZE' ,
291
- OP_INVERT : 'OP_INVERT' ,
292
- OP_AND : 'OP_AND' ,
293
- OP_OR : 'OP_OR' ,
294
- OP_XOR : 'OP_XOR' ,
295
- OP_EQUAL : 'OP_EQUAL' ,
296
- OP_EQUALVERIFY : 'OP_EQUALVERIFY' ,
297
- OP_RESERVED1 : 'OP_RESERVED1' ,
298
- OP_RESERVED2 : 'OP_RESERVED2' ,
299
- OP_1ADD : 'OP_1ADD' ,
300
- OP_1SUB : 'OP_1SUB' ,
301
- OP_2MUL : 'OP_2MUL' ,
302
- OP_2DIV : 'OP_2DIV' ,
303
- OP_NEGATE : 'OP_NEGATE' ,
304
- OP_ABS : 'OP_ABS' ,
305
- OP_NOT : 'OP_NOT' ,
306
- OP_0NOTEQUAL : 'OP_0NOTEQUAL' ,
307
- OP_ADD : 'OP_ADD' ,
308
- OP_SUB : 'OP_SUB' ,
309
- OP_MUL : 'OP_MUL' ,
310
- OP_DIV : 'OP_DIV' ,
311
- OP_MOD : 'OP_MOD' ,
312
- OP_LSHIFT : 'OP_LSHIFT' ,
313
- OP_RSHIFT : 'OP_RSHIFT' ,
314
- OP_BOOLAND : 'OP_BOOLAND' ,
315
- OP_BOOLOR : 'OP_BOOLOR' ,
316
- OP_NUMEQUAL : 'OP_NUMEQUAL' ,
317
- OP_NUMEQUALVERIFY : 'OP_NUMEQUALVERIFY' ,
318
- OP_NUMNOTEQUAL : 'OP_NUMNOTEQUAL' ,
319
- OP_LESSTHAN : 'OP_LESSTHAN' ,
320
- OP_GREATERTHAN : 'OP_GREATERTHAN' ,
321
- OP_LESSTHANOREQUAL : 'OP_LESSTHANOREQUAL' ,
322
- OP_GREATERTHANOREQUAL : 'OP_GREATERTHANOREQUAL' ,
323
- OP_MIN : 'OP_MIN' ,
324
- OP_MAX : 'OP_MAX' ,
325
- OP_WITHIN : 'OP_WITHIN' ,
326
- OP_RIPEMD160 : 'OP_RIPEMD160' ,
327
- OP_SHA1 : 'OP_SHA1' ,
328
- OP_SHA256 : 'OP_SHA256' ,
329
- OP_HASH160 : 'OP_HASH160' ,
330
- OP_HASH256 : 'OP_HASH256' ,
331
- OP_CODESEPARATOR : 'OP_CODESEPARATOR' ,
332
- OP_CHECKSIG : 'OP_CHECKSIG' ,
333
- OP_CHECKSIGVERIFY : 'OP_CHECKSIGVERIFY' ,
334
- OP_CHECKMULTISIG : 'OP_CHECKMULTISIG' ,
335
- OP_CHECKMULTISIGVERIFY : 'OP_CHECKMULTISIGVERIFY' ,
336
- OP_NOP1 : 'OP_NOP1' ,
337
- OP_CHECKLOCKTIMEVERIFY : 'OP_CHECKLOCKTIMEVERIFY' ,
338
- OP_CHECKSEQUENCEVERIFY : 'OP_CHECKSEQUENCEVERIFY' ,
339
- OP_NOP4 : 'OP_NOP4' ,
340
- OP_NOP5 : 'OP_NOP5' ,
341
- OP_NOP6 : 'OP_NOP6' ,
342
- OP_NOP7 : 'OP_NOP7' ,
343
- OP_NOP8 : 'OP_NOP8' ,
344
- OP_NOP9 : 'OP_NOP9' ,
345
- OP_NOP10 : 'OP_NOP10' ,
346
- OP_SMALLINTEGER : 'OP_SMALLINTEGER' ,
347
- OP_PUBKEYS : 'OP_PUBKEYS' ,
348
- OP_PUBKEYHASH : 'OP_PUBKEYHASH' ,
349
- OP_PUBKEY : 'OP_PUBKEY' ,
350
- OP_INVALIDOPCODE : 'OP_INVALIDOPCODE' ,
240
+ OP_0 : 'OP_0' ,
241
+ OP_PUSHDATA1 : 'OP_PUSHDATA1' ,
242
+ OP_PUSHDATA2 : 'OP_PUSHDATA2' ,
243
+ OP_PUSHDATA4 : 'OP_PUSHDATA4' ,
244
+ OP_1NEGATE : 'OP_1NEGATE' ,
245
+ OP_RESERVED : 'OP_RESERVED' ,
246
+ OP_1 : 'OP_1' ,
247
+ OP_2 : 'OP_2' ,
248
+ OP_3 : 'OP_3' ,
249
+ OP_4 : 'OP_4' ,
250
+ OP_5 : 'OP_5' ,
251
+ OP_6 : 'OP_6' ,
252
+ OP_7 : 'OP_7' ,
253
+ OP_8 : 'OP_8' ,
254
+ OP_9 : 'OP_9' ,
255
+ OP_10 : 'OP_10' ,
256
+ OP_11 : 'OP_11' ,
257
+ OP_12 : 'OP_12' ,
258
+ OP_13 : 'OP_13' ,
259
+ OP_14 : 'OP_14' ,
260
+ OP_15 : 'OP_15' ,
261
+ OP_16 : 'OP_16' ,
262
+ OP_NOP : 'OP_NOP' ,
263
+ OP_VER : 'OP_VER' ,
264
+ OP_IF : 'OP_IF' ,
265
+ OP_NOTIF : 'OP_NOTIF' ,
266
+ OP_VERIF : 'OP_VERIF' ,
267
+ OP_VERNOTIF : 'OP_VERNOTIF' ,
268
+ OP_ELSE : 'OP_ELSE' ,
269
+ OP_ENDIF : 'OP_ENDIF' ,
270
+ OP_VERIFY : 'OP_VERIFY' ,
271
+ OP_RETURN : 'OP_RETURN' ,
272
+ OP_TOALTSTACK : 'OP_TOALTSTACK' ,
273
+ OP_FROMALTSTACK : 'OP_FROMALTSTACK' ,
274
+ OP_2DROP : 'OP_2DROP' ,
275
+ OP_2DUP : 'OP_2DUP' ,
276
+ OP_3DUP : 'OP_3DUP' ,
277
+ OP_2OVER : 'OP_2OVER' ,
278
+ OP_2ROT : 'OP_2ROT' ,
279
+ OP_2SWAP : 'OP_2SWAP' ,
280
+ OP_IFDUP : 'OP_IFDUP' ,
281
+ OP_DEPTH : 'OP_DEPTH' ,
282
+ OP_DROP : 'OP_DROP' ,
283
+ OP_DUP : 'OP_DUP' ,
284
+ OP_NIP : 'OP_NIP' ,
285
+ OP_OVER : 'OP_OVER' ,
286
+ OP_PICK : 'OP_PICK' ,
287
+ OP_ROLL : 'OP_ROLL' ,
288
+ OP_ROT : 'OP_ROT' ,
289
+ OP_SWAP : 'OP_SWAP' ,
290
+ OP_TUCK : 'OP_TUCK' ,
291
+ OP_CAT : 'OP_CAT' ,
292
+ OP_SUBSTR : 'OP_SUBSTR' ,
293
+ OP_LEFT : 'OP_LEFT' ,
294
+ OP_RIGHT : 'OP_RIGHT' ,
295
+ OP_SIZE : 'OP_SIZE' ,
296
+ OP_INVERT : 'OP_INVERT' ,
297
+ OP_AND : 'OP_AND' ,
298
+ OP_OR : 'OP_OR' ,
299
+ OP_XOR : 'OP_XOR' ,
300
+ OP_EQUAL : 'OP_EQUAL' ,
301
+ OP_EQUALVERIFY : 'OP_EQUALVERIFY' ,
302
+ OP_RESERVED1 : 'OP_RESERVED1' ,
303
+ OP_RESERVED2 : 'OP_RESERVED2' ,
304
+ OP_1ADD : 'OP_1ADD' ,
305
+ OP_1SUB : 'OP_1SUB' ,
306
+ OP_2MUL : 'OP_2MUL' ,
307
+ OP_2DIV : 'OP_2DIV' ,
308
+ OP_NEGATE : 'OP_NEGATE' ,
309
+ OP_ABS : 'OP_ABS' ,
310
+ OP_NOT : 'OP_NOT' ,
311
+ OP_0NOTEQUAL : 'OP_0NOTEQUAL' ,
312
+ OP_ADD : 'OP_ADD' ,
313
+ OP_SUB : 'OP_SUB' ,
314
+ OP_MUL : 'OP_MUL' ,
315
+ OP_DIV : 'OP_DIV' ,
316
+ OP_MOD : 'OP_MOD' ,
317
+ OP_LSHIFT : 'OP_LSHIFT' ,
318
+ OP_RSHIFT : 'OP_RSHIFT' ,
319
+ OP_BOOLAND : 'OP_BOOLAND' ,
320
+ OP_BOOLOR : 'OP_BOOLOR' ,
321
+ OP_NUMEQUAL : 'OP_NUMEQUAL' ,
322
+ OP_NUMEQUALVERIFY : 'OP_NUMEQUALVERIFY' ,
323
+ OP_NUMNOTEQUAL : 'OP_NUMNOTEQUAL' ,
324
+ OP_LESSTHAN : 'OP_LESSTHAN' ,
325
+ OP_GREATERTHAN : 'OP_GREATERTHAN' ,
326
+ OP_LESSTHANOREQUAL : 'OP_LESSTHANOREQUAL' ,
327
+ OP_GREATERTHANOREQUAL : 'OP_GREATERTHANOREQUAL' ,
328
+ OP_MIN : 'OP_MIN' ,
329
+ OP_MAX : 'OP_MAX' ,
330
+ OP_WITHIN : 'OP_WITHIN' ,
331
+ OP_RIPEMD160 : 'OP_RIPEMD160' ,
332
+ OP_SHA1 : 'OP_SHA1' ,
333
+ OP_SHA256 : 'OP_SHA256' ,
334
+ OP_HASH160 : 'OP_HASH160' ,
335
+ OP_HASH256 : 'OP_HASH256' ,
336
+ OP_CODESEPARATOR : 'OP_CODESEPARATOR' ,
337
+ OP_CHECKSIG : 'OP_CHECKSIG' ,
338
+ OP_CHECKSIGVERIFY : 'OP_CHECKSIGVERIFY' ,
339
+ OP_CHECKMULTISIG : 'OP_CHECKMULTISIG' ,
340
+ OP_CHECKMULTISIGVERIFY : 'OP_CHECKMULTISIGVERIFY' ,
341
+ OP_NOP1 : 'OP_NOP1' ,
342
+ OP_CHECKLOCKTIMEVERIFY : 'OP_CHECKLOCKTIMEVERIFY' ,
343
+ OP_CHECKSEQUENCEVERIFY : 'OP_CHECKSEQUENCEVERIFY' ,
344
+ OP_NOP4 : 'OP_NOP4' ,
345
+ OP_NOP5 : 'OP_NOP5' ,
346
+ OP_NOP6 : 'OP_NOP6' ,
347
+ OP_NOP7 : 'OP_NOP7' ,
348
+ OP_NOP8 : 'OP_NOP8' ,
349
+ OP_NOP9 : 'OP_NOP9' ,
350
+ OP_NOP10 : 'OP_NOP10' ,
351
+ OP_SMALLINTEGER : 'OP_SMALLINTEGER' ,
352
+ OP_PUBKEYS : 'OP_PUBKEYS' ,
353
+ OP_PUBKEYHASH : 'OP_PUBKEYHASH' ,
354
+ OP_PUBKEY : 'OP_PUBKEY' ,
355
+ OP_INVALIDOPCODE : 'OP_INVALIDOPCODE' ,
351
356
})
352
357
353
358
class CScriptInvalidError (Exception ):
@@ -392,10 +397,10 @@ def decode(vch):
392
397
if len (value ) == 0 :
393
398
return result
394
399
for i , byte in enumerate (value ):
395
- result |= int (byte ) << 8 * i
400
+ result |= int (byte ) << 8 * i
396
401
if value [- 1 ] >= 0x80 :
397
402
# Mask for all but the highest result bit
398
- num_mask = (2 ** (len (value )* 8 ) - 1 ) >> 1
403
+ num_mask = (2 ** (len (value ) * 8 ) - 1 ) >> 1
399
404
result &= num_mask
400
405
result *= - 1
401
406
return result
@@ -493,21 +498,20 @@ def raw_iter(self):
493
498
pushdata_type = 'PUSHDATA2'
494
499
if i + 1 >= len (self ):
495
500
raise CScriptInvalidError ('PUSHDATA2: missing data length' )
496
- datasize = self [i ] + (self [i + 1 ] << 8 )
501
+ datasize = self [i ] + (self [i + 1 ] << 8 )
497
502
i += 2
498
503
499
504
elif opcode == OP_PUSHDATA4 :
500
505
pushdata_type = 'PUSHDATA4'
501
506
if i + 3 >= len (self ):
502
507
raise CScriptInvalidError ('PUSHDATA4: missing data length' )
503
- datasize = self [i ] + (self [i + 1 ] << 8 ) + (self [i + 2 ] << 16 ) + (self [i + 3 ] << 24 )
508
+ datasize = self [i ] + (self [i + 1 ] << 8 ) + (self [i + 2 ] << 16 ) + (self [i + 3 ] << 24 )
504
509
i += 4
505
510
506
511
else :
507
- assert False # shouldn't happen
508
-
512
+ assert False # shouldn't happen
509
513
510
- data = bytes (self [i :i + datasize ])
514
+ data = bytes (self [i :i + datasize ])
511
515
512
516
# Check for truncation
513
517
if len (data ) < datasize :
0 commit comments