MCPcopy Index your code
hub / github.com/petertodd/python-bitcoinlib / CScriptOp

Class CScriptOp

bitcoin/core/script.py:35–96  ·  view source on GitHub ↗

A single script opcode

Source from the content-addressed store, hash-verified

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

Callers 7

encode_op_nMethod · 0.85
script.pyFile · 0.85
__iter__Method · 0.85
test_is_singletonMethod · 0.85

Calls

no outgoing calls

Tested by 3

test_is_singletonMethod · 0.68