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

Class CScriptOp

qa/rpc-tests/test_framework/script.py:39–100  ·  view source on GitHub ↗

A single script opcode

Source from the content-addressed store, hash-verified

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

Callers 4

encode_op_nMethod · 0.85
script.pyFile · 0.85
__coerce_instanceMethod · 0.85
__iter__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected