|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Script\Classifier; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Script\Parser\Operation; |
|
6
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PublicKey; |
|
7
|
|
|
use BitWasp\Bitcoin\Script\Opcodes; |
|
8
|
|
|
use BitWasp\Bitcoin\Script\ScriptInterface; |
|
9
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
10
|
|
|
|
|
11
|
|
|
class OutputClassifier |
|
12
|
|
|
{ |
|
13
|
|
|
const PAYTOPUBKEY = 'pubkey'; |
|
14
|
|
|
const PAYTOPUBKEYHASH = 'pubkeyhash'; |
|
15
|
|
|
const PAYTOSCRIPTHASH = 'scripthash'; |
|
16
|
|
|
const WITNESS_V0_KEYHASH = 'witness_v0_keyhash'; |
|
17
|
|
|
const WITNESS_V0_SCRIPTHASH = 'witness_v0_scripthash'; |
|
18
|
|
|
const MULTISIG = 'multisig'; |
|
19
|
|
|
const UNKNOWN = 'unknown'; |
|
20
|
|
|
const NONSTANDARD = 'nonstandard'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param ScriptInterface $script |
|
24
|
|
|
* @param BufferInterface $publicKey |
|
25
|
|
|
* @return bool |
|
26
|
|
|
*/ |
|
27
|
|
|
public function isPayToPublicKey(ScriptInterface $script, & $publicKey = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$decoded = $script->getScriptParser()->decode(); |
|
30
|
|
|
if (count($decoded) !== 2 || $decoded[0]->isPush() === false || $decoded[1]->isPush() === true) { |
|
31
|
|
|
return false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$size = $decoded[0]->getDataSize(); |
|
35
|
198 |
|
if ($size === 33 || $size === 65) { |
|
36
|
|
|
$op = $decoded[1]; |
|
37
|
198 |
|
if ($op->getOp() === Opcodes::OP_CHECKSIG) { |
|
38
|
198 |
|
$publicKey = $decoded[0]->getData(); |
|
39
|
198 |
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
144 |
|
|
|
46
|
|
|
/** |
|
47
|
144 |
|
* @param ScriptInterface $script |
|
48
|
120 |
|
* @param null $pubKeyHash |
|
49
|
|
|
* @return bool |
|
50
|
|
|
*/ |
|
51
|
42 |
|
public function isPayToPublicKeyHash(ScriptInterface $script, & $pubKeyHash = null) |
|
52
|
42 |
|
{ |
|
53
|
30 |
|
$decoded = $script->getScriptParser()->decode(); |
|
54
|
30 |
|
if (count($decoded) !== 5) { |
|
55
|
30 |
|
return false; |
|
56
|
30 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
$dup = $decoded[0]; |
|
59
|
|
|
$hash = $decoded[1]; |
|
60
|
12 |
|
$buf = $decoded[2]; |
|
61
|
|
|
$eq = $decoded[3]; |
|
62
|
|
|
$checksig = $decoded[4]; |
|
63
|
|
|
|
|
64
|
|
|
foreach ([$dup, $hash, $eq, $checksig] as $op) { |
|
65
|
|
|
/** @var Operation $op */ |
|
66
|
|
|
if ($op->isPush()) { |
|
67
|
114 |
|
return false; |
|
68
|
|
|
} |
|
69
|
114 |
|
} |
|
70
|
54 |
|
|
|
71
|
|
|
if ($dup->getOp() === Opcodes::OP_DUP |
|
72
|
|
|
&& $hash->getOp() === Opcodes::OP_HASH160 |
|
73
|
66 |
|
&& $buf->isPush() && $buf->getDataSize() === 20 |
|
74
|
66 |
|
&& $eq->getOp() === Opcodes::OP_EQUALVERIFY |
|
75
|
66 |
|
&& $checksig->getOp() === Opcodes::OP_CHECKSIG) { |
|
76
|
66 |
|
$pubKeyHash = $decoded[2]->getData(); |
|
77
|
66 |
|
return true; |
|
78
|
|
|
} |
|
79
|
66 |
|
|
|
80
|
|
|
return false; |
|
81
|
66 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
66 |
|
* @param ScriptInterface $script |
|
85
|
|
|
* @param null $scriptHash |
|
86
|
66 |
|
* @return bool |
|
87
|
66 |
|
*/ |
|
88
|
66 |
|
public function isPayToScriptHash(ScriptInterface $script, & $scriptHash = null) |
|
89
|
66 |
|
{ |
|
90
|
66 |
|
$decoded = $script->getScriptParser()->decode(); |
|
91
|
66 |
|
if (count($decoded) !== 3) { |
|
92
|
66 |
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$hash = $decoded[0]; |
|
96
|
|
|
if ($hash->isPush() || !$hash->getOp() === Opcodes::OP_HASH160) { |
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$buffer = $decoded[1]; |
|
101
|
|
|
if (!$buffer->isPush() || $buffer->getDataSize() !== 20) { |
|
102
|
168 |
|
return false; |
|
103
|
|
|
} |
|
104
|
168 |
|
|
|
105
|
138 |
|
$eq = $decoded[2]; |
|
106
|
|
|
if (!$eq->isPush() && $eq->getOp() === Opcodes::OP_EQUAL) { |
|
107
|
|
|
$scriptHash = $decoded[1]->getData(); |
|
108
|
72 |
|
return true; |
|
109
|
72 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
72 |
|
|
|
114
|
72 |
|
/** |
|
115
|
|
|
* @param ScriptInterface $script |
|
116
|
|
|
* @param array $keys |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
72 |
|
public function isMultisig(ScriptInterface $script, & $keys = []) |
|
120
|
72 |
|
{ |
|
121
|
72 |
|
$decoded = $script->getScriptParser()->decode(); |
|
122
|
72 |
|
$count = count($decoded); |
|
123
|
|
|
if ($count <= 3) { |
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$mOp = $decoded[0]; |
|
128
|
|
|
$nOp = $decoded[$count - 2]; |
|
129
|
|
|
$checksig = $decoded[$count - 1]; |
|
130
|
|
|
if ($mOp->isPush() || $nOp->isPush() || $checksig->isPush()) { |
|
131
|
|
|
return false; |
|
132
|
78 |
|
} |
|
133
|
|
|
|
|
134
|
78 |
|
/** @var Operation[] $vKeys */ |
|
135
|
78 |
|
$vKeys = array_slice($decoded, 1, -2); |
|
136
|
30 |
|
$solutions = []; |
|
137
|
|
|
foreach ($vKeys as $key) { |
|
138
|
|
|
if (!$key->isPush() || !PublicKey::isCompressedOrUncompressed($key->getData())) { |
|
139
|
48 |
|
return false; |
|
140
|
48 |
|
} |
|
141
|
48 |
|
$solutions[] = $key->getData(); |
|
142
|
48 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ($mOp->getOp() >= Opcodes::OP_0 |
|
145
|
|
|
&& $nOp->getOp() <= Opcodes::OP_16 |
|
146
|
|
|
&& $checksig->getOp() === Opcodes::OP_CHECKMULTISIG) { |
|
147
|
48 |
|
$keys = $solutions; |
|
148
|
48 |
|
return true; |
|
149
|
48 |
|
} |
|
150
|
48 |
|
|
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
48 |
|
|
|
154
|
48 |
|
/** |
|
155
|
|
|
* @param ScriptInterface $script |
|
156
|
48 |
|
* @param null $programHash |
|
157
|
48 |
|
* @return bool |
|
158
|
48 |
|
*/ |
|
159
|
48 |
|
public function isWitness(ScriptInterface $script, & $programHash = null) |
|
160
|
48 |
|
{ |
|
161
|
|
|
$decoded = $script->getScriptParser()->decode(); |
|
162
|
|
|
$size = $script->getBuffer()->getSize(); |
|
163
|
|
|
if ($size < 4 || $size > 34) { |
|
164
|
|
|
return false; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if (count($decoded) !== 2 || !$decoded[1]->isPush()) { |
|
168
|
|
|
return false; |
|
169
|
|
|
} |
|
170
|
198 |
|
|
|
171
|
|
|
$version = $decoded[0]->getOp(); |
|
172
|
132 |
|
if ($version != Opcodes::OP_0 && ($version < Opcodes::OP_1 || $version > Opcodes::OP_16)) { |
|
173
|
132 |
|
return false; |
|
174
|
|
|
} |
|
175
|
132 |
|
|
|
176
|
72 |
|
$witness = $decoded[1]; |
|
177
|
|
|
if ($size === $witness->getDataSize() + 2) { |
|
178
|
|
|
$programHash = $witness->getData(); |
|
179
|
78 |
|
return true; |
|
180
|
78 |
|
} |
|
181
|
78 |
|
|
|
182
|
66 |
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
30 |
|
/** |
|
186
|
30 |
|
* @param ScriptInterface $script |
|
187
|
|
|
* @param null $solution |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
30 |
|
public function classify(ScriptInterface $script, &$solution = null) |
|
191
|
30 |
|
{ |
|
192
|
30 |
|
$type = self::UNKNOWN; |
|
193
|
30 |
|
$solution = null; |
|
194
|
198 |
|
if ($this->isPayToScriptHash($script, $solution)) { |
|
195
|
|
|
/** @var BufferInterface $solution */ |
|
196
|
|
|
$type = self::PAYTOSCRIPTHASH; |
|
197
|
|
|
} elseif ($this->isWitness($script, $solution)) { |
|
198
|
|
|
/** @var BufferInterface $solution */ |
|
199
|
|
|
if ($solution->getSize() == 20) { |
|
200
|
|
|
$type = self::WITNESS_V0_KEYHASH; |
|
201
|
|
|
} else { |
|
202
|
|
|
$type = self::WITNESS_V0_SCRIPTHASH; |
|
203
|
138 |
|
} |
|
204
|
|
|
} elseif ($this->isPayToPublicKey($script, $solution)) { |
|
205
|
138 |
|
/** @var BufferInterface $solution */ |
|
206
|
138 |
|
$type = self::PAYTOPUBKEY; |
|
207
|
138 |
|
} elseif ($this->isPayToPublicKeyHash($script, $solution)) { |
|
|
|
|
|
|
208
|
|
|
/** @var BufferInterface $solution */ |
|
209
|
42 |
|
$type = self::PAYTOPUBKEYHASH; |
|
210
|
138 |
|
} elseif ($this->isMultisig($script, $solution)) { |
|
|
|
|
|
|
211
|
|
|
/** @var BufferInterface[] $solution */ |
|
212
|
30 |
|
$type = self::MULTISIG; |
|
213
|
12 |
|
} |
|
214
|
12 |
|
|
|
215
|
18 |
|
return $type; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.