This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
/
OpCode.py
236 lines (212 loc) · 7.7 KB
/
OpCode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Constants
PUSH0 = b'\x00' # An empty array of bytes is pushed onto the stack.
PUSHF = PUSH0
PUSHBYTES1 = b'\x01' # b'\x01-b'\x4B The next opcode bytes is data to be pushed onto the stack
PUSHBYTES2 = b'\x02'
PUSHBYTES3 = b'\x03'
PUSHBYTES4 = b'\x04'
PUSHBYTES5 = b'\x05'
PUSHBYTES6 = b'\x06'
PUSHBYTES7 = b'\x07'
PUSHBYTES8 = b'\x08'
PUSHBYTES9 = b'\x09'
PUSHBYTES10 = b'\x0A'
PUSHBYTES11 = b'\x0B'
PUSHBYTES12 = b'\x0C'
PUSHBYTES13 = b'\x0D'
PUSHBYTES14 = b'\x0E'
PUSHBYTES15 = b'\x0F'
PUSHBYTES16 = b'\x10'
PUSHBYTES17 = b'\x11'
PUSHBYTES18 = b'\x12'
PUSHBYTES19 = b'\x13'
PUSHBYTES20 = b'\x14'
PUSHBYTES21 = b'\x15'
PUSHBYTES22 = b'\x16'
PUSHBYTES23 = b'\x17'
PUSHBYTES24 = b'\x18'
PUSHBYTES25 = b'\x19'
PUSHBYTES26 = b'\x1A'
PUSHBYTES27 = b'\x1B'
PUSHBYTES28 = b'\x1C'
PUSHBYTES29 = b'\x1D'
PUSHBYTES30 = b'\x1E'
PUSHBYTES31 = b'\x1F'
PUSHBYTES32 = b'\x20'
PUSHBYTES33 = b'\x21'
PUSHBYTES34 = b'\x22'
PUSHBYTES35 = b'\x23'
PUSHBYTES36 = b'\x24'
PUSHBYTES37 = b'\x25'
PUSHBYTES38 = b'\x26'
PUSHBYTES39 = b'\x27'
PUSHBYTES40 = b'\x28'
PUSHBYTES41 = b'\x29'
PUSHBYTES42 = b'\x2A'
PUSHBYTES43 = b'\x2B'
PUSHBYTES44 = b'\x2C'
PUSHBYTES45 = b'\x2D'
PUSHBYTES46 = b'\x2E'
PUSHBYTES47 = b'\x2F'
PUSHBYTES48 = b'\x30'
PUSHBYTES49 = b'\x31'
PUSHBYTES50 = b'\x32'
PUSHBYTES51 = b'\x33'
PUSHBYTES52 = b'\x34'
PUSHBYTES53 = b'\x35'
PUSHBYTES54 = b'\x36'
PUSHBYTES55 = b'\x37'
PUSHBYTES56 = b'\x38'
PUSHBYTES57 = b'\x39'
PUSHBYTES58 = b'\x3A'
PUSHBYTES59 = b'\x3B'
PUSHBYTES60 = b'\x3C'
PUSHBYTES61 = b'\x3D'
PUSHBYTES62 = b'\x3E'
PUSHBYTES63 = b'\x3F'
PUSHBYTES64 = b'\x40'
PUSHBYTES65 = b'\x41'
PUSHBYTES66 = b'\x42'
PUSHBYTES67 = b'\x43'
PUSHBYTES68 = b'\x44'
PUSHBYTES69 = b'\x45'
PUSHBYTES70 = b'\x46'
PUSHBYTES71 = b'\x47'
PUSHBYTES72 = b'\x48'
PUSHBYTES73 = b'\x49'
PUSHBYTES74 = b'\x4A'
PUSHBYTES75 = b'\x4B'
PUSHDATA1 = b'\x4C' # The next byte contains the number of bytes to be pushed onto the stack.
PUSHDATA2 = b'\x4D' # The next two bytes contain the number of bytes to be pushed onto the stack.
PUSHDATA4 = b'\x4E' # The next four bytes contain the number of bytes to be pushed onto the stack.
PUSHM1 = b'\x4F' # The number -1 is pushed onto the stack.
PUSH1 = b'\x51' # The number 1 is pushed onto the stack.
PUSHT = PUSH1
PUSH2 = b'\x52' # The number 2 is pushed onto the stack.
PUSH3 = b'\x53' # The number 3 is pushed onto the stack.
PUSH4 = b'\x54' # The number 4 is pushed onto the stack.
PUSH5 = b'\x55' # The number 5 is pushed onto the stack.
PUSH6 = b'\x56' # The number 6 is pushed onto the stack.
PUSH7 = b'\x57' # The number 7 is pushed onto the stack.
PUSH8 = b'\x58' # The number 8 is pushed onto the stack.
PUSH9 = b'\x59' # The number 9 is pushed onto the stack.
PUSH10 = b'\x5A' # The number 10 is pushed onto the stack.
PUSH11 = b'\x5B' # The number 11 is pushed onto the stack.
PUSH12 = b'\x5C' # The number 12 is pushed onto the stack.
PUSH13 = b'\x5D' # The number 13 is pushed onto the stack.
PUSH14 = b'\x5E' # The number 14 is pushed onto the stack.
PUSH15 = b'\x5F' # The number 15 is pushed onto the stack.
PUSH16 = b'\x60' # The number 16 is pushed onto the stack.
# Flow control
NOP = b'\x61' # Does nothing.
JMP = b'\x62'
JMPIF = b'\x63'
JMPIFNOT = b'\x64'
CALL = b'\x65'
RET = b'\x66'
APPCALL = b'\x67'
SYSCALL = b'\x68'
TAILCALL = b'\x69'
# Exceptions
THROW = b'\xf0'
THROWIFNOT = b'\xf1'
# Stack
DUPFROMALTSTACK = b'\x6A'
TOALTSTACK = b'\x6B' # Puts the input onto the top of the alt stack. Removes it from the main stack.
FROMALTSTACK = b'\x6C' # Puts the input onto the top of the main stack. Removes it from the alt stack.
XDROP = b'\x6D'
XSWAP = b'\x72'
XTUCK = b'\x73'
DEPTH = b'\x74' # Puts the number of stack items onto the stack.
DROP = b'\x75' # Removes the top stack item.
DUP = b'\x76' # Duplicates the top stack item.
NIP = b'\x77' # Removes the second-to-top stack item.
OVER = b'\x78' # Copies the second-to-top stack item to the top.
PICK = b'\x79' # The item n back in the stack is copied to the top.
ROLL = b'\x7A' # The item n back in the stack is moved to the top.
ROT = b'\x7B' # The top three items on the stack are rotated to the left.
SWAP = b'\x7C' # The top two items on the stack are swapped.
TUCK = b'\x7D' # The item at the top of the stack is copied and inserted before the second-to-top item.
# Splice
CAT = b'\x7E' # Concatenates two strings.
SUBSTR = b'\x7F' # Returns a section of a string.
LEFT = b'\x80' # Keeps only characters left of the specified point in a string.
RIGHT = b'\x81' # Keeps only characters right of the specified point in a string.
SIZE = b'\x82' # Returns the length of the input string.
# Bitwise logic
INVERT = b'\x83' # Flips all of the bits in the input.
AND = b'\x84' # Boolean and between each bit in the inputs.
OR = b'\x85' # Boolean or between each bit in the inputs.
XOR = b'\x86' # Boolean exclusive or between each bit in the inputs.
EQUAL = b'\x87' # Returns 1 if the inputs are exactly equal' 0 otherwise.
# OP_EQUALVERIFY = b'\x88' # Same as OP_EQUAL' but runs OP_VERIFY afterward.
# OP_RESERVED1 = b'\x89' # Transaction is invalid unless occuring in an unexecuted OP_IF branch
# OP_RESERVED2 = b'\x8A' # Transaction is invalid unless occuring in an unexecuted OP_IF branch
# Arithmetic
# Note: Arithmetic inputs are limited to signed 32-bit integers' but may overflow their output.
INC = b'\x8B' # 1 is added to the input.
DEC = b'\x8C' # 1 is subtracted from the input.
SIGN = b'\x8D'
NEGATE = b'\x8F' # The sign of the input is flipped.
ABS = b'\x90' # The input is made positive.
NOT = b'\x91' # If the input is 0 or 1' it is flipped. Otherwise the output will be 0.
NZ = b'\x92' # Returns 0 if the input is 0. 1 otherwise.
ADD = b'\x93' # a is added to b.
SUB = b'\x94' # b is subtracted from a.
MUL = b'\x95' # a is multiplied by b.
DIV = b'\x96' # a is divided by b.
MOD = b'\x97' # Returns the remainder after dividing a by b.
SHL = b'\x98' # Shifts a left b bits' preserving sign.
SHR = b'\x99' # Shifts a right b bits' preserving sign.
BOOLAND = b'\x9A' # If both a and b are not 0' the output is 1. Otherwise 0.
BOOLOR = b'\x9B' # If a or b is not 0' the output is 1. Otherwise 0.
NUMEQUAL = b'\x9C' # Returns 1 if the numbers are equal' 0 otherwise.
NUMNOTEQUAL = b'\x9E' # Returns 1 if the numbers are not equal' 0 otherwise.
LT = b'\x9F' # Returns 1 if a is less than b' 0 otherwise.
GT = b'\xA0' # Returns 1 if a is greater than b' 0 otherwise.
LTE = b'\xA1' # Returns 1 if a is less than or equal to b' 0 otherwise.
GTE = b'\xA2' # Returns 1 if a is greater than or equal to b' 0 otherwise.
MIN = b'\xA3' # Returns the smaller of a and b.
MAX = b'\xA4' # Returns the larger of a and b.
WITHIN = b'\xA5' # Returns 1 if x is within the specified range (left-inclusive)' 0 otherwise.
# Crypto
# RIPEMD160 = b'\xA6' # The input is hashed using RIPEMD-160.
SHA1 = b'\xA7' # The input is hashed using SHA-1.
SHA256 = b'\xA8' # The input is hashed using SHA-256.
HASH160 = b'\xA9'
HASH256 = b'\xAA'
CHECKSIG = b'\xAC'
CHECKMULTISIG = b'\xAE'
# Array
ARRAYSIZE = b'\xC0'
PACK = b'\xC1'
UNPACK = b'\xC2'
PICKITEM = b'\xC3'
SETITEM = b'\xC4'
NEWARRAY = b'\xC5' # 用作引用類型
NEWSTRUCT = b'\xC6' # 用作值類型
APPEND = b'\xC8'
REVERSE = b'\xC9'
import sys
import importlib
import binascii
module = importlib.import_module('neo.VM.OpCode')
items = dir(sys.modules[__name__])
def ToName(op):
if type(op) is bytes:
op = int.from_bytes(op, 'little')
for item in items:
n = getattr(module, item)
try:
nn = int(binascii.hexlify(n), 16)
if op == nn:
return item
except Exception as e:
pass
try:
nn2 = int.from_bytes(n, 'little')
if op == nn2:
return item
except Exception as e:
pass
return None