MCPcopy Create free account
hub / github.com/BTCGPU/BTCGPU / CScriptOp

Class CScriptOp

test/functional/test_framework/script.py:27–88  ·  view source on GitHub ↗

A single script opcode

Source from the content-addressed store, hash-verified

25
26_opcode_instances = []
27class CScriptOp(int):
28 """A single script opcode"""
29 __slots__ = []
30
31 @staticmethod
32 def encode_op_pushdata(d):
33 """Encode a PUSHDATA op, returning bytes"""
34 if len(d) < 0x4c:
35 return b'' + bytes([len(d)]) + d # OP_PUSHDATA
36 elif len(d) <= 0xff:
37 return b'\x4c' + bytes([len(d)]) + d # OP_PUSHDATA1
38 elif len(d) <= 0xffff:
39 return b'\x4d' + struct.pack(b'<H', len(d)) + d # OP_PUSHDATA2
40 elif len(d) <= 0xffffffff:
41 return b'\x4e' + struct.pack(b'<I', len(d)) + d # OP_PUSHDATA4
42 else:
43 raise ValueError("Data too long to encode in a PUSHDATA op")
44
45 @staticmethod
46 def encode_op_n(n):
47 """Encode a small integer op, returning an opcode"""
48 if not (0 <= n <= 16):
49 raise ValueError('Integer must be in range 0 <= n <= 16, got %d' % n)
50
51 if n == 0:
52 return OP_0
53 else:
54 return CScriptOp(OP_1 + n-1)
55
56 def decode_op_n(self):
57 """Decode a small integer opcode, returning an integer"""
58 if self == OP_0:
59 return 0
60
61 if not (self == OP_0 or OP_1 <= self <= OP_16):
62 raise ValueError('op %r is not an OP_N' % self)
63
64 return int(self - OP_1+1)
65
66 def is_small_int(self):
67 """Return true if the op pushes a small integer to the stack"""
68 if 0x51 <= self <= 0x60 or self == 0:
69 return True
70 else:
71 return False
72
73 def __str__(self):
74 return repr(self)
75
76 def __repr__(self):
77 if self in OPCODE_NAMES:
78 return OPCODE_NAMES[self]
79 else:
80 return 'CScriptOp(0x%x)' % self
81
82 def __new__(cls, n):
83 try:
84 return _opcode_instances[n]

Callers 8

get_p2pkh_scriptFunction · 0.90
test_segwit_versionsMethod · 0.90
encode_op_nMethod · 0.85
script.pyFile · 0.85
__coerce_instanceMethod · 0.85
__iter__Method · 0.85

Calls

no outgoing calls

Tested by 3

test_segwit_versionsMethod · 0.72