1515is in bitcoin.core.scripteval
1616"""
1717
18- from __future__ import absolute_import , division , print_function
19-
20- import sys
21- _bchr = chr
22- _bord = ord
23- if sys .version > '3' :
24- long = int
25- _bchr = lambda x : bytes ([x ])
26- _bord = lambda x : x
27- from io import BytesIO as _BytesIO
28- else :
29- from cStringIO import StringIO as _BytesIO
18+
19+ from io import BytesIO
3020
3121import struct
3222
@@ -50,9 +40,9 @@ class CScriptOp(int):
5040 def encode_op_pushdata (d ):
5141 """Encode a PUSHDATA op, returning bytes"""
5242 if len (d ) < 0x4c :
53- return b'' + _bchr ( len (d )) + d # OP_PUSHDATA
43+ return bytes ([ len (d )] ) + d # OP_PUSHDATA
5444 elif len (d ) <= 0xff :
55- return b'\x4c ' + _bchr ( len (d )) + d # OP_PUSHDATA1
45+ return b'\x4c ' + bytes ([ len (d )] ) + d # OP_PUSHDATA1
5646 elif len (d ) <= 0xffff :
5747 return b'\x4d ' + struct .pack (b'<H' , len (d )) + d # OP_PUSHDATA2
5848 elif len (d ) <= 0xffffffff :
@@ -524,12 +514,12 @@ class CScript(bytes):
524514 def __coerce_instance (cls , other ):
525515 # Coerce other into bytes
526516 if isinstance (other , CScriptOp ):
527- other = _bchr ( other )
528- elif isinstance (other , ( int , long ) ):
517+ other = bytes ([ other ] )
518+ elif isinstance (other , int ):
529519 if 0 <= other <= 16 :
530- other = bytes (_bchr ( CScriptOp .encode_op_n (other )) )
520+ other = bytes ([ CScriptOp .encode_op_n (other )] )
531521 elif other == - 1 :
532- other = bytes (_bchr ( OP_1NEGATE ) )
522+ other = bytes ([ OP_1NEGATE ] )
533523 else :
534524 other = CScriptOp .encode_op_pushdata (bitcoin .core ._bignum .bn2vch (other ))
535525 elif isinstance (other , (bytes , bytearray )):
@@ -572,7 +562,7 @@ def raw_iter(self):
572562 i = 0
573563 while i < len (self ):
574564 sop_idx = i
575- opcode = _bord ( self [i ])
565+ opcode = self [i ]
576566 i += 1
577567
578568 if opcode > OP_PUSHDATA4 :
@@ -588,21 +578,21 @@ def raw_iter(self):
588578 pushdata_type = 'PUSHDATA1'
589579 if i >= len (self ):
590580 raise CScriptInvalidError ('PUSHDATA1: missing data length' )
591- datasize = _bord ( self [i ])
581+ datasize = self [i ]
592582 i += 1
593583
594584 elif opcode == OP_PUSHDATA2 :
595585 pushdata_type = 'PUSHDATA2'
596586 if i + 1 >= len (self ):
597587 raise CScriptInvalidError ('PUSHDATA2: missing data length' )
598- datasize = _bord ( self [i ]) + (_bord ( self [i + 1 ]) << 8 )
588+ datasize = self [i ] + (self [i + 1 ] << 8 )
599589 i += 2
600590
601591 elif opcode == OP_PUSHDATA4 :
602592 pushdata_type = 'PUSHDATA4'
603593 if i + 3 >= len (self ):
604594 raise CScriptInvalidError ('PUSHDATA4: missing data length' )
605- datasize = _bord ( self [i ]) + (_bord ( self [i + 1 ]) << 8 ) + (_bord ( self [i + 2 ]) << 16 ) + (_bord ( self [i + 3 ]) << 24 )
595+ datasize = self [i ] + (self [i + 1 ] << 8 ) + (self [i + 2 ] << 16 ) + (self [i + 3 ] << 24 )
606596 i += 4
607597
608598 else :
@@ -676,9 +666,9 @@ def is_p2sh(self):
676666 Note that this test is consensus-critical.
677667 """
678668 return (len (self ) == 23 and
679- _bord ( self [0 ]) == OP_HASH160 and
680- _bord ( self [1 ]) == 0x14 and
681- _bord ( self [22 ]) == OP_EQUAL )
669+ self [0 ] == OP_HASH160 and
670+ self [1 ] == 0x14 and
671+ self [22 ] == OP_EQUAL )
682672
683673 def is_witness_scriptpubkey (self ):
684674 """Returns true if this is a scriptpubkey signaling segregated witness data.
@@ -747,7 +737,7 @@ def has_canonical_pushes(self):
747737 if op > OP_16 :
748738 continue
749739
750- elif op < OP_PUSHDATA1 and op > OP_0 and len (data ) == 1 and _bord ( data [0 ]) <= 16 :
740+ elif op < OP_PUSHDATA1 and op > OP_0 and len (data ) == 1 and data [0 ] <= 16 :
751741 # Could have used an OP_n code, rather than a 1-byte push.
752742 return False
753743
@@ -770,7 +760,7 @@ def has_canonical_pushes(self):
770760 def is_unspendable (self ):
771761 """Test if the script is provably unspendable"""
772762 return (len (self ) > 0 and
773- _bord ( self [0 ]) == OP_RETURN )
763+ self [0 ] == OP_RETURN )
774764
775765 def is_valid (self ):
776766 """Return True if the script is valid, False otherwise
@@ -1018,7 +1008,7 @@ def SignatureHash(script, txTo, inIdx, hashtype, amount=None, sigversion=SIGVERS
10181008 serialize_outputs = txTo .vout [inIdx ].serialize ()
10191009 hashOutputs = bitcoin .core .Hash (serialize_outputs )
10201010
1021- f = _BytesIO ()
1011+ f = BytesIO ()
10221012 f .write (struct .pack ("<i" , txTo .nVersion ))
10231013 f .write (hashPrevouts )
10241014 f .write (hashSequence )
0 commit comments