|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Script; |
|
4
|
|
|
|
|
5
|
|
|
class Opcodes implements \ArrayAccess |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
const OP_0 = 0; |
|
9
|
|
|
const OP_PUSHDATA1 = 76; |
|
10
|
|
|
const OP_PUSHDATA2 = 77; |
|
11
|
|
|
const OP_PUSHDATA4 = 78; |
|
12
|
|
|
const OP_1NEGATE = 79; |
|
13
|
|
|
const OP_1 = 81; |
|
14
|
|
|
const OP_2 = 82; |
|
15
|
|
|
const OP_3 = 83; |
|
16
|
|
|
const OP_4 = 84; |
|
17
|
|
|
const OP_5 = 85; |
|
18
|
|
|
const OP_6 = 86; |
|
19
|
|
|
const OP_7 = 87; |
|
20
|
|
|
const OP_8 = 88; |
|
21
|
|
|
const OP_9 = 89; |
|
22
|
|
|
const OP_10 = 90; |
|
23
|
|
|
const OP_11 = 91; |
|
24
|
|
|
const OP_12 = 92; |
|
25
|
|
|
const OP_13 = 93; |
|
26
|
|
|
const OP_14 = 94; |
|
27
|
|
|
const OP_15 = 95; |
|
28
|
|
|
const OP_16 = 96; |
|
29
|
|
|
|
|
30
|
|
|
// Flow Control |
|
31
|
|
|
const OP_NOP = 97; |
|
32
|
|
|
const OP_IF = 99; |
|
33
|
|
|
const OP_NOTIF = 100; |
|
34
|
|
|
const OP_ELSE = 103; |
|
35
|
|
|
const OP_ENDIF = 104; |
|
36
|
|
|
const OP_VERIFY = 105; |
|
37
|
|
|
const OP_RETURN = 106; |
|
38
|
|
|
const OP_TOALTSTACK = 107; |
|
39
|
|
|
const OP_FROMALTSTACK = 108; |
|
40
|
|
|
const OP_IFDUP = 115; |
|
41
|
|
|
const OP_DEPTH = 116; |
|
42
|
|
|
const OP_DROP = 117; |
|
43
|
|
|
const OP_DUP = 118; |
|
44
|
|
|
const OP_NIP = 119; |
|
45
|
|
|
const OP_OVER = 120; |
|
46
|
|
|
const OP_PICK = 121; |
|
47
|
|
|
const OP_ROLL = 122; |
|
48
|
|
|
const OP_ROT = 123; |
|
49
|
|
|
const OP_SWAP = 124; |
|
50
|
|
|
const OP_TUCK = 125; |
|
51
|
|
|
const OP_2DROP = 109; |
|
52
|
|
|
const OP_2DUP = 110; |
|
53
|
|
|
const OP_3DUP = 111; |
|
54
|
|
|
const OP_2OVER = 112; |
|
55
|
|
|
const OP_2ROT = 113; |
|
56
|
|
|
const OP_2SWAP = 114; |
|
57
|
|
|
|
|
58
|
|
|
// Splice |
|
59
|
|
|
const OP_CAT = 126; /* disabled */ |
|
60
|
|
|
const OP_SUBSTR = 127; /* disabled */ |
|
61
|
|
|
const OP_LEFT = 128; /* disabled */ |
|
62
|
|
|
const OP_RIGHT = 129; /* disabled */ |
|
63
|
|
|
const OP_SIZE = 130; /* disabled */ |
|
64
|
|
|
|
|
65
|
|
|
// Bitwise logic |
|
66
|
|
|
const OP_INVERT = 131; /* Disabled */ |
|
67
|
|
|
const OP_AND = 132; /* Disabled */ |
|
68
|
|
|
const OP_OR = 133; /* Disabled */ |
|
69
|
|
|
const OP_XOR = 134; /* Disabled */ |
|
70
|
|
|
const OP_EQUAL = 135; |
|
71
|
|
|
const OP_EQUALVERIFY = 136; |
|
72
|
|
|
|
|
73
|
|
|
// Arithmetic |
|
74
|
|
|
const OP_1ADD = 139; |
|
75
|
|
|
const OP_1SUB = 140; |
|
76
|
|
|
const OP_2MUL = 141; |
|
77
|
|
|
const OP_2DIV = 142; |
|
78
|
|
|
const OP_NEGATE = 143; |
|
79
|
|
|
const OP_ABS = 144; |
|
80
|
|
|
const OP_NOT = 145; |
|
81
|
|
|
const OP_0NOTEQUAL = 146; |
|
82
|
|
|
const OP_ADD = 147; |
|
83
|
|
|
const OP_SUB = 148; |
|
84
|
|
|
const OP_MUL = 149; |
|
85
|
|
|
const OP_DIV = 150; |
|
86
|
|
|
const OP_MOD = 151; |
|
87
|
|
|
const OP_LSHIFT = 152; |
|
88
|
|
|
const OP_RSHIFT = 153; |
|
89
|
|
|
const OP_BOOLAND = 154; |
|
90
|
|
|
const OP_BOOLOR = 155; |
|
91
|
|
|
const OP_NUMEQUAL = 156; |
|
92
|
|
|
const OP_NUMEQUALVERIFY = 157; |
|
93
|
|
|
const OP_NUMNOTEQUAL = 158; |
|
94
|
|
|
const OP_LESSTHAN = 159; |
|
95
|
|
|
const OP_GREATERTHAN = 160; |
|
96
|
|
|
const OP_LESSTHANOREQUAL = 161; |
|
97
|
|
|
const OP_GREATERTHANOREQUAL = 162; |
|
98
|
|
|
const OP_MIN = 163; |
|
99
|
|
|
const OP_MAX = 164; |
|
100
|
|
|
const OP_WITHIN = 165; |
|
101
|
|
|
|
|
102
|
|
|
// Crypto |
|
103
|
|
|
const OP_RIPEMD160 = 166; |
|
104
|
|
|
const OP_SHA1 = 167; |
|
105
|
|
|
const OP_SHA256 = 168; |
|
106
|
|
|
const OP_HASH160 = 169; |
|
107
|
|
|
const OP_HASH256 = 170; |
|
108
|
|
|
const OP_CODESEPARATOR = 171; |
|
109
|
|
|
const OP_CHECKSIG = 172; |
|
110
|
|
|
const OP_CHECKSIGVERIFY = 173; |
|
111
|
|
|
const OP_CHECKMULTISIG = 174; |
|
112
|
|
|
const OP_CHECKMULTISIGVERIFY = 175; |
|
113
|
|
|
|
|
114
|
|
|
// Pseudo Words |
|
115
|
|
|
const OP_PUBKEYHASH = 253; |
|
116
|
|
|
const OP_PUBKEY = 254; |
|
117
|
|
|
const OP_INVALIDOPCODE = 255; |
|
118
|
|
|
|
|
119
|
|
|
// Reserved |
|
120
|
|
|
// So are any which are not assigned |
|
121
|
|
|
const OP_RESERVED = 80; |
|
122
|
|
|
const OP_VER = 98; |
|
123
|
|
|
const OP_VERIF = 101; |
|
124
|
|
|
const OP_VERNOTIF = 102; |
|
125
|
|
|
const OP_RESERVED1 = 137; |
|
126
|
|
|
const OP_RESERVED2 = 138; |
|
127
|
|
|
const OP_NOP1 = 176; |
|
128
|
|
|
const OP_CHECKLOCKTIMEVERIFY = 177; |
|
129
|
|
|
const OP_CHECKSEQUENCEVERIFY = 178; |
|
130
|
|
|
const OP_NOP3 = 178; |
|
131
|
|
|
const OP_NOP4 = 179; |
|
132
|
|
|
const OP_NOP5 = 180; |
|
133
|
|
|
const OP_NOP6 = 181; |
|
134
|
|
|
const OP_NOP7 = 182; |
|
135
|
|
|
const OP_NOP8 = 183; |
|
136
|
|
|
const OP_NOP9 = 184; |
|
137
|
|
|
const OP_NOP10 = 185; |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @var array |
|
141
|
|
|
*/ |
|
142
|
|
|
private static $names = [ |
|
143
|
|
|
self::OP_0 => 'OP_0', |
|
144
|
|
|
self::OP_PUSHDATA1 => 'OP_PUSHDATA1', |
|
145
|
|
|
self::OP_PUSHDATA2 => 'OP_PUSHDATA2', |
|
146
|
|
|
self::OP_PUSHDATA4 => 'OP_PUSHDATA4', |
|
147
|
|
|
self::OP_1NEGATE => 'OP_1NEGATE', |
|
148
|
|
|
self::OP_1 => 'OP_1', |
|
149
|
|
|
self::OP_2 => 'OP_2', |
|
150
|
|
|
self::OP_3 => 'OP_3', |
|
151
|
|
|
self::OP_4 => 'OP_4', |
|
152
|
|
|
self::OP_5 => 'OP_5', |
|
153
|
|
|
self::OP_6 => 'OP_6', |
|
154
|
|
|
self::OP_7 => 'OP_7', |
|
155
|
|
|
self::OP_8 => 'OP_8', |
|
156
|
|
|
self::OP_9 => 'OP_9', |
|
157
|
|
|
self::OP_10 => 'OP_10', |
|
158
|
|
|
self::OP_11 => 'OP_11', |
|
159
|
|
|
self::OP_12 => 'OP_12', |
|
160
|
|
|
self::OP_13 => 'OP_13', |
|
161
|
|
|
self::OP_14 => 'OP_14', |
|
162
|
|
|
self::OP_15 => 'OP_15', |
|
163
|
|
|
self::OP_16 => 'OP_16', |
|
164
|
|
|
|
|
165
|
|
|
// Flow Control |
|
166
|
|
|
self::OP_NOP => 'OP_NOP', |
|
167
|
|
|
self::OP_IF => 'OP_IF', |
|
168
|
|
|
self::OP_NOTIF => 'OP_NOTIF', |
|
169
|
|
|
self::OP_ELSE => 'OP_ELSE', |
|
170
|
|
|
self::OP_ENDIF => 'OP_ENDIF', |
|
171
|
|
|
self::OP_VERIFY => 'OP_VERIFY', |
|
172
|
|
|
self::OP_RETURN => 'OP_RETURN', |
|
173
|
|
|
self::OP_TOALTSTACK => 'OP_TOALTSTACK', |
|
174
|
|
|
self::OP_FROMALTSTACK => 'OP_FROMALTSTACK', |
|
175
|
|
|
self::OP_IFDUP => 'OP_IFDUP', |
|
176
|
|
|
self::OP_DEPTH => 'OP_DEPTH', |
|
177
|
|
|
self::OP_DROP => 'OP_DROP', |
|
178
|
|
|
self::OP_DUP => 'OP_DUP', |
|
179
|
|
|
self::OP_NIP => 'OP_NIP', |
|
180
|
|
|
self::OP_OVER => 'OP_OVER', |
|
181
|
|
|
self::OP_PICK => 'OP_PICK', |
|
182
|
|
|
self::OP_ROLL => 'OP_ROLL', |
|
183
|
|
|
self::OP_ROT => 'OP_ROT', |
|
184
|
|
|
self::OP_SWAP => 'OP_SWAP', |
|
185
|
|
|
self::OP_TUCK => 'OP_TUCK', |
|
186
|
|
|
self::OP_2DROP => 'OP_2DROP', |
|
187
|
|
|
self::OP_2DUP => 'OP_2DUP', |
|
188
|
|
|
self::OP_3DUP => 'OP_3DUP', |
|
189
|
|
|
self::OP_2OVER => 'OP_2OVER', |
|
190
|
|
|
self::OP_2ROT => 'OP_2ROT', |
|
191
|
|
|
self::OP_2SWAP => 'OP_2SWAP', |
|
192
|
|
|
|
|
193
|
|
|
// Splice |
|
194
|
|
|
self::OP_CAT => 'OP_CAT', /* disabled */ |
|
195
|
|
|
self::OP_SUBSTR => 'OP_SUBSTR', /* disabled */ |
|
196
|
|
|
self::OP_LEFT => 'OP_LEFT', /* disabled */ |
|
197
|
|
|
self::OP_RIGHT => 'OP_RIGHT', /* disabled */ |
|
198
|
|
|
self::OP_SIZE => 'OP_SIZE', /* disabled */ |
|
199
|
|
|
|
|
200
|
|
|
// Bitwise logic |
|
201
|
|
|
self::OP_INVERT => 'OP_INVERT', /* Disabled */ |
|
202
|
|
|
self::OP_AND => 'OP_AND', /* Disabled */ |
|
203
|
|
|
self::OP_OR => 'OP_OR', /* Disabled */ |
|
204
|
|
|
self::OP_XOR => 'OP_XOR', /* Disabled */ |
|
205
|
|
|
self::OP_EQUAL => 'OP_EQUAL', |
|
206
|
|
|
self::OP_EQUALVERIFY => 'OP_EQUALVERIFY', |
|
207
|
|
|
|
|
208
|
|
|
// Arithmetic |
|
209
|
|
|
self::OP_1ADD => 'OP_1ADD', |
|
210
|
|
|
self::OP_1SUB => 'OP_1SUB', |
|
211
|
|
|
self::OP_2MUL => 'OP_2MUL', |
|
212
|
|
|
self::OP_2DIV => 'OP_2DIV', |
|
213
|
|
|
self::OP_NEGATE => 'OP_NEGATE', |
|
214
|
|
|
self::OP_ABS => 'OP_ABS', |
|
215
|
|
|
self::OP_NOT => 'OP_NOT', |
|
216
|
|
|
self::OP_0NOTEQUAL => 'OP_0NOTEQUAL', |
|
217
|
|
|
self::OP_ADD => 'OP_ADD', |
|
218
|
|
|
self::OP_SUB => 'OP_SUB', |
|
219
|
|
|
self::OP_MUL => 'OP_MUL', |
|
220
|
|
|
self::OP_DIV => 'OP_DIV', |
|
221
|
|
|
self::OP_MOD => 'OP_MOD', |
|
222
|
|
|
self::OP_LSHIFT => 'OP_LSHIFT', |
|
223
|
|
|
self::OP_RSHIFT => 'OP_RSHIFT', |
|
224
|
|
|
self::OP_BOOLAND => 'OP_BOOLAND', |
|
225
|
|
|
self::OP_BOOLOR => 'OP_BOOLOR', |
|
226
|
|
|
self::OP_NUMEQUAL => 'OP_NUMEQUAL', |
|
227
|
|
|
self::OP_NUMEQUALVERIFY => 'OP_NUMEQUALVERIFY', |
|
228
|
|
|
self::OP_NUMNOTEQUAL => 'OP_NUMNOTEQUAL', |
|
229
|
|
|
self::OP_LESSTHAN => 'OP_LESSTHAN', |
|
230
|
|
|
self::OP_GREATERTHAN => 'OP_GREATERTHAN', |
|
231
|
|
|
self::OP_LESSTHANOREQUAL => 'OP_LESSTHANOREQUAL', |
|
232
|
|
|
self::OP_GREATERTHANOREQUAL => 'OP_GREATERTHANOREQUAL', |
|
233
|
|
|
self::OP_MIN => 'OP_MIN', |
|
234
|
|
|
self::OP_MAX => 'OP_MAX', |
|
235
|
|
|
self::OP_WITHIN => 'OP_WITHIN', |
|
236
|
|
|
|
|
237
|
|
|
// Crypto |
|
238
|
|
|
self::OP_RIPEMD160 => 'OP_RIPEMD160', |
|
239
|
|
|
self::OP_SHA1 => 'OP_SHA1', |
|
240
|
|
|
self::OP_SHA256 => 'OP_SHA256', |
|
241
|
|
|
self::OP_HASH160 => 'OP_HASH160', |
|
242
|
|
|
self::OP_HASH256 => 'OP_HASH256', |
|
243
|
|
|
self::OP_CODESEPARATOR => 'OP_CODESEPARATOR', |
|
244
|
|
|
self::OP_CHECKSIG => 'OP_CHECKSIG', |
|
245
|
|
|
self::OP_CHECKSIGVERIFY => 'OP_CHECKSIGVERIFY', |
|
246
|
|
|
self::OP_CHECKMULTISIG => 'OP_CHECKMULTISIG', |
|
247
|
|
|
self::OP_CHECKMULTISIGVERIFY => 'OP_CHECKMULTISIGVERIFY', |
|
248
|
|
|
|
|
249
|
|
|
// Pseudo Words |
|
250
|
|
|
self::OP_PUBKEYHASH => 'OP_PUBKEYHASH', |
|
251
|
|
|
self::OP_PUBKEY => 'OP_PUBKEY', |
|
252
|
|
|
self::OP_INVALIDOPCODE => 'OP_INVALIDOPCODE', |
|
253
|
|
|
|
|
254
|
|
|
// Reserved |
|
255
|
|
|
// So are any which are not assigned |
|
256
|
|
|
self::OP_RESERVED => 'OP_RESERVED', |
|
257
|
|
|
self::OP_VER => 'OP_VER', |
|
258
|
|
|
self::OP_VERIF => 'OP_VERIF', |
|
259
|
|
|
self::OP_VERNOTIF => 'OP_VERNOTIF', |
|
260
|
|
|
self::OP_RESERVED1 => 'OP_RESERVED1', |
|
261
|
|
|
self::OP_RESERVED2 => 'OP_RESERVED2', |
|
262
|
|
|
self::OP_NOP1 => 'OP_NOP1', |
|
263
|
|
|
self::OP_CHECKLOCKTIMEVERIFY => 'OP_CHECKLOCKTIMEVERIFY', |
|
264
|
|
|
self::OP_CHECKSEQUENCEVERIFY => 'OP_CHECKSEQUENCEVERIFY', |
|
265
|
|
|
self::OP_NOP3 => 'OP_NOP3', |
|
266
|
|
|
self::OP_NOP4 => 'OP_NOP4', |
|
267
|
|
|
self::OP_NOP5 => 'OP_NOP5', |
|
268
|
|
|
self::OP_NOP6 => 'OP_NOP6', |
|
269
|
|
|
self::OP_NOP7 => 'OP_NOP7', |
|
270
|
|
|
self::OP_NOP8 => 'OP_NOP8', |
|
271
|
|
|
self::OP_NOP9 => 'OP_NOP9', |
|
272
|
|
|
self::OP_NOP10 => 'OP_NOP10', |
|
273
|
|
|
]; |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @var array |
|
277
|
|
|
*/ |
|
278
|
|
|
private $known = []; |
|
279
|
|
|
|
|
280
|
3256 |
|
public function __construct() |
|
281
|
|
|
{ |
|
282
|
3256 |
|
$this->known = array_flip(self::$names); |
|
283
|
3256 |
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @param integer $op |
|
287
|
|
|
*/ |
|
288
|
24 |
|
private function opExists($op) |
|
289
|
|
|
{ |
|
290
|
24 |
|
if (!array_key_exists($op, self::$names)) { |
|
291
|
6 |
|
throw new \RuntimeException("Opcode not found"); |
|
292
|
|
|
} |
|
293
|
18 |
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* @param string $name |
|
297
|
|
|
*/ |
|
298
|
452 |
|
private function opNameExists($name) |
|
299
|
|
|
{ |
|
300
|
452 |
|
if (!array_key_exists($name, $this->known)) { |
|
301
|
12 |
|
throw new \RuntimeException("Opcode by that name not found"); |
|
302
|
|
|
} |
|
303
|
440 |
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* @param int $op |
|
307
|
|
|
* @return string |
|
308
|
|
|
*/ |
|
309
|
24 |
|
public function getOp($op) |
|
310
|
|
|
{ |
|
311
|
24 |
|
$this->opExists($op); |
|
312
|
18 |
|
return self::$names[$op]; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* @param string $name |
|
317
|
|
|
* @return int |
|
318
|
|
|
*/ |
|
319
|
452 |
|
public function getOpByName($name) |
|
320
|
|
|
{ |
|
321
|
452 |
|
$this->opNameExists($name); |
|
322
|
440 |
|
return $this->known[$name]; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* @param int $opcode |
|
327
|
|
|
* @return string |
|
328
|
|
|
*/ |
|
329
|
6 |
|
public function offsetGet($opcode) |
|
330
|
|
|
{ |
|
331
|
6 |
|
return $this->getOp($opcode); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* @param int $opcode |
|
336
|
|
|
* @return bool |
|
337
|
|
|
*/ |
|
338
|
2554 |
|
public function offsetExists($opcode) |
|
339
|
|
|
{ |
|
340
|
2554 |
|
return array_key_exists($opcode, self::$names); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
12 |
|
private function errorNoWrite() |
|
344
|
|
|
{ |
|
345
|
12 |
|
throw new \RuntimeException('Cannot write to Opcodes'); |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* @param int $opcode |
|
350
|
|
|
*/ |
|
351
|
6 |
|
public function offsetUnset($opcode) |
|
352
|
|
|
{ |
|
353
|
6 |
|
$this->errorNoWrite(); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* @param int $opcode |
|
358
|
|
|
* @param mixed $value |
|
359
|
|
|
*/ |
|
360
|
6 |
|
public function offsetSet($opcode, $value) |
|
361
|
|
|
{ |
|
362
|
6 |
|
$this->errorNoWrite(); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* @return array |
|
367
|
|
|
*/ |
|
368
|
|
|
public function __debugInfo() |
|
369
|
|
|
{ |
|
370
|
|
|
return []; |
|
371
|
|
|
} |
|
372
|
|
|
} |
|
373
|
|
|
|