|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Script\Classifier; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PublicKey; |
|
6
|
|
|
use BitWasp\Bitcoin\Script\Opcodes; |
|
7
|
|
|
use BitWasp\Bitcoin\Script\Parser\Operation; |
|
8
|
|
|
use BitWasp\Bitcoin\Script\ScriptFactory; |
|
9
|
|
|
use BitWasp\Bitcoin\Script\ScriptInterface; |
|
10
|
|
|
use BitWasp\Bitcoin\Script\ScriptType; |
|
11
|
|
|
use BitWasp\Buffertools\Buffer; |
|
12
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
13
|
|
|
|
|
14
|
|
|
class OutputClassifier |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @deprecated |
|
18
|
|
|
*/ |
|
19
|
|
|
const PAYTOPUBKEY = 'pubkey'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @deprecated |
|
23
|
|
|
*/ |
|
24
|
|
|
const PAYTOPUBKEYHASH = 'pubkeyhash'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @deprecated |
|
28
|
|
|
*/ |
|
29
|
|
|
const PAYTOSCRIPTHASH = 'scripthash'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @deprecated |
|
33
|
|
|
*/ |
|
34
|
|
|
const WITNESS_V0_KEYHASH = 'witness_v0_keyhash'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @deprecated |
|
38
|
|
|
*/ |
|
39
|
|
|
const WITNESS_V0_SCRIPTHASH = 'witness_v0_scripthash'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @deprecated |
|
43
|
|
|
*/ |
|
44
|
|
|
const MULTISIG = 'multisig'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @deprecated |
|
48
|
|
|
*/ |
|
49
|
|
|
const NULLDATA = 'nulldata'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @deprecated |
|
53
|
|
|
*/ |
|
54
|
|
|
const UNKNOWN = 'nonstandard'; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @deprecated |
|
58
|
|
|
*/ |
|
59
|
|
|
const NONSTANDARD = 'nonstandard'; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @deprecated |
|
63
|
|
|
*/ |
|
64
|
|
|
const P2PK = 'pubkey'; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @deprecated |
|
68
|
|
|
*/ |
|
69
|
|
|
const P2PKH = 'pubkeyhash'; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @deprecated |
|
73
|
|
|
*/ |
|
74
|
|
|
const P2SH = 'scripthash'; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @deprecated |
|
78
|
|
|
*/ |
|
79
|
|
|
const P2WSH = 'witness_v0_scripthash'; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @deprecated |
|
83
|
|
|
*/ |
|
84
|
|
|
const P2WKH = 'witness_v0_keyhash'; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @deprecated |
|
88
|
|
|
*/ |
|
89
|
|
|
const WITNESS_COINBASE_COMMITMENT = 'witness_coinbase_commitment'; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param Operation[] $decoded |
|
93
|
|
|
* @return false|BufferInterface |
|
94
|
|
|
*/ |
|
95
|
206 |
|
private function decodeP2PK(array $decoded) |
|
96
|
|
|
{ |
|
97
|
206 |
|
if (count($decoded) !== 2 || !$decoded[0]->isPush()) { |
|
98
|
174 |
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
80 |
|
$size = $decoded[0]->getDataSize(); |
|
102
|
80 |
|
if ($size === 33 || $size === 65) { |
|
103
|
32 |
|
$op = $decoded[1]; |
|
104
|
32 |
|
if ($op->getOp() === Opcodes::OP_CHECKSIG) { |
|
105
|
30 |
|
return $decoded[0]->getData(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
58 |
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param ScriptInterface $script |
|
114
|
|
|
* @return bool |
|
115
|
|
|
*/ |
|
116
|
20 |
|
public function isPayToPublicKey(ScriptInterface $script) |
|
117
|
|
|
{ |
|
118
|
|
|
try { |
|
119
|
20 |
|
return $this->decodeP2PK($script->getScriptParser()->decode()) !== false; |
|
120
|
|
|
} catch (\Exception $e) { |
|
121
|
|
|
/** Return false later */ |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param Operation[] $decoded |
|
129
|
|
|
* @return BufferInterface|false |
|
130
|
|
|
*/ |
|
131
|
188 |
|
private function decodeP2PKH(array $decoded) |
|
132
|
|
|
{ |
|
133
|
188 |
|
if (count($decoded) !== 5) { |
|
134
|
146 |
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
62 |
|
$dup = $decoded[0]; |
|
138
|
62 |
|
$hash = $decoded[1]; |
|
139
|
62 |
|
$buf = $decoded[2]; |
|
140
|
62 |
|
$eq = $decoded[3]; |
|
141
|
62 |
|
$checksig = $decoded[4]; |
|
142
|
|
|
|
|
143
|
62 |
|
foreach ([$dup, $hash, $eq, $checksig] as $op) { |
|
144
|
|
|
/** @var Operation $op */ |
|
145
|
62 |
|
if ($op->isPush()) { |
|
146
|
62 |
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
52 |
|
if ($dup->getOp() === Opcodes::OP_DUP |
|
151
|
52 |
|
&& $hash->getOp() === Opcodes::OP_HASH160 |
|
152
|
52 |
|
&& $buf->isPush() && $buf->getDataSize() === 20 |
|
153
|
52 |
|
&& $eq->getOp() === Opcodes::OP_EQUALVERIFY |
|
154
|
52 |
|
&& $checksig->getOp() === Opcodes::OP_CHECKSIG) { |
|
155
|
52 |
|
return $decoded[2]->getData(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
2 |
|
return false; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param ScriptInterface $script |
|
163
|
|
|
* @return bool |
|
164
|
|
|
*/ |
|
165
|
16 |
|
public function isPayToPublicKeyHash(ScriptInterface $script) |
|
166
|
|
|
{ |
|
167
|
|
|
try { |
|
168
|
16 |
|
return $this->decodeP2PKH($script->getScriptParser()->decode()) !== false; |
|
169
|
|
|
} catch (\Exception $e) { |
|
170
|
|
|
/** Return false later */ |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param array $decoded |
|
178
|
|
|
* @return bool |
|
179
|
|
|
*/ |
|
180
|
1220 |
|
private function decodeP2SH(array $decoded) |
|
181
|
|
|
{ |
|
182
|
1220 |
|
if (count($decoded) !== 3) { |
|
183
|
930 |
|
return false; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
312 |
|
$op_hash = $decoded[0]; |
|
187
|
312 |
|
if ($op_hash->isPush() || $op_hash->getOp() !== Opcodes::OP_HASH160) { |
|
188
|
124 |
|
return false; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
190 |
|
$buffer = $decoded[1]; |
|
192
|
190 |
|
if (!$buffer->isPush() || $buffer->getOp() !== 20) { |
|
193
|
4 |
|
return false; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
188 |
|
$eq = $decoded[2]; |
|
197
|
188 |
|
if (!$eq->isPush() && $eq->getOp() === Opcodes::OP_EQUAL) { |
|
198
|
188 |
|
return $decoded[1]->getData(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
2 |
|
return false; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param ScriptInterface $script |
|
206
|
|
|
* @return bool |
|
207
|
|
|
*/ |
|
208
|
1156 |
|
public function isPayToScriptHash(ScriptInterface $script) |
|
209
|
|
|
{ |
|
210
|
|
|
try { |
|
211
|
1156 |
|
return $this->decodeP2SH($script->getScriptParser()->decode()) !== false; |
|
212
|
2 |
|
} catch (\Exception $e) { |
|
213
|
|
|
/** Return false later */ |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
2 |
|
return false; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param Operation[] $decoded |
|
221
|
|
|
* @return bool|BufferInterface[] |
|
222
|
|
|
*/ |
|
223
|
162 |
|
private function decodeMultisig(array $decoded) |
|
224
|
|
|
{ |
|
225
|
162 |
|
$count = count($decoded); |
|
226
|
162 |
|
if ($count <= 3) { |
|
227
|
106 |
|
return false; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
94 |
|
$mOp = $decoded[0]; |
|
231
|
94 |
|
$nOp = $decoded[$count - 2]; |
|
232
|
94 |
|
$checksig = $decoded[$count - 1]; |
|
233
|
94 |
|
if ($mOp->isPush() || $nOp->isPush() || $checksig->isPush()) { |
|
234
|
14 |
|
return false; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** @var Operation[] $vKeys */ |
|
238
|
82 |
|
$vKeys = array_slice($decoded, 1, -2); |
|
239
|
82 |
|
$solutions = []; |
|
240
|
82 |
|
foreach ($vKeys as $key) { |
|
241
|
82 |
|
if (!$key->isPush() || !PublicKey::isCompressedOrUncompressed($key->getData())) { |
|
242
|
26 |
|
return false; |
|
243
|
|
|
} |
|
244
|
76 |
|
$solutions[] = $key->getData(); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
58 |
|
if ($mOp->getOp() >= Opcodes::OP_0 |
|
248
|
58 |
|
&& $nOp->getOp() <= Opcodes::OP_16 |
|
249
|
58 |
|
&& $checksig->getOp() === Opcodes::OP_CHECKMULTISIG) { |
|
250
|
58 |
|
return $solutions; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
2 |
|
return false; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @param ScriptInterface $script |
|
258
|
|
|
* @return bool |
|
259
|
|
|
*/ |
|
260
|
20 |
|
public function isMultisig(ScriptInterface $script) |
|
261
|
|
|
{ |
|
262
|
|
|
try { |
|
263
|
20 |
|
return $this->decodeMultisig($script->getScriptParser()->decode()) !== false; |
|
264
|
|
|
} catch (\Exception $e) { |
|
265
|
|
|
/** Return false later */ |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return false; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @param ScriptInterface $script |
|
273
|
|
|
* @param Operation[] $decoded |
|
274
|
|
|
* @return false|BufferInterface |
|
275
|
|
|
*/ |
|
276
|
16 |
|
private function decodeWitnessNoLimit(ScriptInterface $script, array $decoded) |
|
277
|
|
|
{ |
|
278
|
16 |
|
$size = $script->getBuffer()->getSize(); |
|
279
|
16 |
|
if ($size < 4 || $size > 40) { |
|
280
|
6 |
|
return false; |
|
281
|
|
|
} |
|
282
|
12 |
|
if (count($decoded) !== 2 || !$decoded[1]->isPush()) { |
|
283
|
6 |
|
return false; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
8 |
|
$version = $decoded[0]->getOp(); |
|
287
|
8 |
|
if ($version !== Opcodes::OP_0 && ($version < Opcodes::OP_1 || $version > Opcodes::OP_16)) { |
|
288
|
4 |
|
return false; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
6 |
|
$witness = $decoded[1]; |
|
292
|
6 |
|
if ($size === $witness->getDataSize() + 2) { |
|
293
|
6 |
|
return $witness->getData(); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
return false; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* @param ScriptInterface $script |
|
301
|
|
|
* @param int $limit |
|
302
|
|
|
* @param array $decoded |
|
303
|
|
|
* @return BufferInterface|false |
|
304
|
|
|
*/ |
|
305
|
|
|
private function decodeWithLimit(ScriptInterface $script, $limit, array $decoded) |
|
306
|
|
|
{ |
|
307
|
|
|
if (($data = $this->decodeWitnessNoLimit($script, $decoded))) { |
|
308
|
|
|
if ($data->getSize() !== $limit) { |
|
309
|
|
|
return false; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
return $data; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
return false; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* @param ScriptInterface $script |
|
320
|
|
|
* @param Operation[] $decoded |
|
321
|
|
|
* @return BufferInterface|false |
|
322
|
|
|
*/ |
|
323
|
|
|
private function decodeP2WKH(ScriptInterface $script, array $decoded) |
|
|
|
|
|
|
324
|
|
|
{ |
|
325
|
|
|
return $this->decodeWithLimit($script, 20, $decoded); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @param ScriptInterface $script |
|
330
|
|
|
* @param Operation[] $decoded |
|
331
|
|
|
* @return BufferInterface|false |
|
332
|
|
|
*/ |
|
333
|
|
|
private function decodeP2WSH(ScriptInterface $script, array $decoded) |
|
|
|
|
|
|
334
|
|
|
{ |
|
335
|
|
|
return $this->decodeWithLimit($script, 32, $decoded); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* @param Operation[] $decoded |
|
340
|
|
|
* @return BufferInterface|false |
|
341
|
|
|
*/ |
|
342
|
60 |
|
private function decodeP2WKH2(array $decoded) |
|
343
|
|
|
{ |
|
344
|
60 |
|
if (count($decoded) === 2 |
|
345
|
60 |
|
&& $decoded[0]->getOp() === Opcodes::OP_0 |
|
346
|
60 |
|
&& $decoded[1]->isPush() |
|
347
|
60 |
|
&& $decoded[1]->getDataSize() === 20) { |
|
348
|
10 |
|
return $decoded[1]->getData(); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
50 |
|
return false; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* @param Operation[] $decoded |
|
356
|
|
|
* @return BufferInterface|false |
|
357
|
|
|
*/ |
|
358
|
100 |
|
private function decodeP2WSH2(array $decoded) |
|
359
|
|
|
{ |
|
360
|
100 |
|
if (count($decoded) === 2 |
|
361
|
100 |
|
&& $decoded[0]->getOp() === Opcodes::OP_0 |
|
362
|
100 |
|
&& $decoded[1]->isPush() |
|
363
|
100 |
|
&& $decoded[1]->getDataSize() === 32) { |
|
364
|
42 |
|
return $decoded[1]->getData(); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
60 |
|
return false; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* @param ScriptInterface $script |
|
372
|
|
|
* @return bool |
|
373
|
|
|
*/ |
|
374
|
16 |
|
public function isWitness(ScriptInterface $script) |
|
375
|
|
|
{ |
|
376
|
|
|
try { |
|
377
|
16 |
|
return $this->decodeWitnessNoLimit($script, $script->getScriptParser()->decode())!== false; |
|
378
|
2 |
|
} catch (\Exception $e) { |
|
379
|
|
|
/** Return false later */ |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
2 |
|
return false; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* @param Operation[] $decoded |
|
387
|
|
|
* @return false|BufferInterface |
|
388
|
|
|
*/ |
|
389
|
48 |
|
private function decodeNullData(array $decoded) |
|
390
|
|
|
{ |
|
391
|
48 |
|
if (count($decoded) !== 2) { |
|
392
|
46 |
|
return false; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
4 |
|
if ($decoded[0]->getOp() === Opcodes::OP_RETURN && $decoded[1]->isPush()) { |
|
396
|
2 |
|
return $decoded[1]->getData(); |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
2 |
|
return false; |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
/** |
|
403
|
|
|
* @param ScriptInterface $script |
|
404
|
|
|
* @return bool |
|
405
|
|
|
*/ |
|
406
|
2 |
|
public function isNullData(ScriptInterface $script) |
|
407
|
|
|
{ |
|
408
|
|
|
try { |
|
409
|
2 |
|
return $this->decodeNullData($script->getScriptParser()->decode()) !== false; |
|
410
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
return false; |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
|
|
/** |
|
417
|
|
|
* @param array $decoded |
|
418
|
|
|
* @return bool|BufferInterface |
|
419
|
|
|
*/ |
|
420
|
62 |
|
private function decodeWitnessCoinbaseCommitment(array $decoded) |
|
421
|
|
|
{ |
|
422
|
62 |
|
if (count($decoded) !== 2) { |
|
423
|
50 |
|
return false; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
12 |
|
if ($decoded[0]->isPush() || $decoded[0]->getOp() !== Opcodes::OP_RETURN) { |
|
427
|
8 |
|
return false; |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
4 |
|
if ($decoded[1]->isPush()) { |
|
431
|
4 |
|
$data = $decoded[1]->getData()->getBinary(); |
|
432
|
4 |
|
if ($decoded[1]->getDataSize() === 0x24 && substr($data, 0, 4) === "\xaa\x21\xa9\xed") { |
|
433
|
2 |
|
return new Buffer(substr($data, 4)); |
|
434
|
|
|
} |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
2 |
|
return false; |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
/** |
|
441
|
|
|
* @param ScriptInterface $script |
|
442
|
|
|
* @return bool |
|
443
|
|
|
*/ |
|
444
|
14 |
|
public function isWitnessCoinbaseCommitment(ScriptInterface $script) |
|
445
|
|
|
{ |
|
446
|
|
|
try { |
|
447
|
14 |
|
return $this->decodeWitnessCoinbaseCommitment($script->getScriptParser()->decode()) !== false; |
|
448
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
return false; |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* @param array $decoded |
|
456
|
|
|
* @param null $solution |
|
457
|
|
|
* @return string |
|
458
|
|
|
*/ |
|
459
|
200 |
|
private function classifyDecoded(array $decoded, &$solution = null) |
|
460
|
|
|
{ |
|
461
|
200 |
|
$type = ScriptType::NONSTANDARD; |
|
462
|
|
|
|
|
463
|
200 |
|
if (($pubKey = $this->decodeP2PK($decoded))) { |
|
464
|
26 |
|
$type = ScriptType::P2PK; |
|
465
|
26 |
|
$solution = $pubKey; |
|
466
|
184 |
|
} else if (($pubKeyHash = $this->decodeP2PKH($decoded))) { |
|
467
|
50 |
|
$type = ScriptType::P2PKH; |
|
468
|
50 |
|
$solution = $pubKeyHash; |
|
469
|
152 |
|
} else if (($multisig = $this->decodeMultisig($decoded))) { |
|
470
|
54 |
|
$type = ScriptType::MULTISIG; |
|
471
|
54 |
|
$solution = $multisig; |
|
472
|
134 |
|
} else if (($scriptHash = $this->decodeP2SH($decoded))) { |
|
473
|
52 |
|
$type = ScriptType::P2SH; |
|
474
|
52 |
|
$solution = $scriptHash; |
|
475
|
100 |
|
} else if (($witnessScriptHash = $this->decodeP2WSH2($decoded))) { |
|
476
|
42 |
|
$type = ScriptType::P2WSH; |
|
477
|
42 |
|
$solution = $witnessScriptHash; |
|
478
|
60 |
|
} else if (($witnessKeyHash = $this->decodeP2WKH2($decoded))) { |
|
479
|
10 |
|
$type = ScriptType::P2WKH; |
|
480
|
10 |
|
$solution = $witnessKeyHash; |
|
481
|
50 |
|
} else if (($witCommitHash = $this->decodeWitnessCoinbaseCommitment($decoded))) { |
|
482
|
2 |
|
$type = ScriptType::WITNESS_COINBASE_COMMITMENT; |
|
483
|
2 |
|
$solution = $witCommitHash; |
|
484
|
48 |
|
} else if (($nullData = $this->decodeNullData($decoded))) { |
|
485
|
2 |
|
$type = ScriptType::NULLDATA; |
|
486
|
2 |
|
$solution = $nullData; |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
200 |
|
return $type; |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
/** |
|
493
|
|
|
* @param ScriptInterface $script |
|
494
|
|
|
* @param mixed $solution |
|
495
|
|
|
* @return string |
|
496
|
|
|
*/ |
|
497
|
200 |
|
public function classify(ScriptInterface $script, &$solution = null) |
|
498
|
|
|
{ |
|
499
|
200 |
|
$decoded = $script->getScriptParser()->decode(); |
|
500
|
|
|
|
|
501
|
200 |
|
$type = $this->classifyDecoded($decoded, $solution); |
|
502
|
|
|
|
|
503
|
200 |
|
return $type; |
|
504
|
|
|
} |
|
505
|
|
|
|
|
506
|
|
|
/** |
|
507
|
|
|
* @param ScriptInterface $script |
|
508
|
|
|
* @return OutputData |
|
509
|
|
|
*/ |
|
510
|
180 |
|
public function decode(ScriptInterface $script) |
|
511
|
|
|
{ |
|
512
|
180 |
|
$solution = null; |
|
513
|
180 |
|
$type = $this->classify($script, $solution); |
|
514
|
180 |
|
return new OutputData($type, $script, $solution); |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
/** |
|
518
|
|
|
* @param ScriptInterface $script |
|
519
|
|
|
* @return OutputData[] |
|
520
|
|
|
*/ |
|
521
|
|
|
public function decodeSequence(ScriptInterface $script, $allowNonstandard = false) |
|
522
|
|
|
{ |
|
523
|
|
|
$decoded = $script->getScriptParser()->decode(); |
|
524
|
|
|
|
|
525
|
|
|
$j = 0; |
|
526
|
|
|
$l = count($decoded); |
|
527
|
|
|
$result = []; |
|
528
|
|
|
while ($j < $l) { |
|
529
|
|
|
$type = null; |
|
530
|
|
|
$slice = null; |
|
531
|
|
|
$solution = null; |
|
532
|
|
|
|
|
533
|
|
|
// increment the $last, and break if it's valid |
|
534
|
|
|
for ($i = 0; $i < ($l - $j) + 1; $i++) { |
|
535
|
|
|
$slice = array_slice($decoded, $j, $i); |
|
536
|
|
|
$chkType = $this->classifyDecoded($slice, $solution); |
|
537
|
|
|
if ($chkType !== ScriptType::NONSTANDARD) { |
|
538
|
|
|
$type = $chkType; |
|
539
|
|
|
break; |
|
540
|
|
|
} |
|
541
|
|
|
} |
|
542
|
|
|
|
|
543
|
|
|
if (null === $type) { |
|
544
|
|
|
if (!$allowNonstandard) { |
|
545
|
|
|
throw new \RuntimeException("Unable to classify script as a sequence of templated types"); |
|
546
|
|
|
} |
|
547
|
|
|
$j++; |
|
548
|
|
|
} else { |
|
549
|
|
|
$j += $i; |
|
550
|
|
|
/** @var Operation[] $slice */ |
|
551
|
|
|
/** @var mixed $solution */ |
|
552
|
|
|
$result[] = new OutputData($type, ScriptFactory::fromOperations($slice), $solution); |
|
|
|
|
|
|
553
|
|
|
} |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
return $result; |
|
557
|
|
|
} |
|
558
|
|
|
} |
|
559
|
|
|
|