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
|
|
|
* @var \BitWasp\Bitcoin\Script\Parser\Operation[] |
24
|
|
|
*/ |
25
|
|
|
private $decoded; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ScriptInterface |
29
|
|
|
*/ |
30
|
|
|
private $script; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ScriptInterface $script |
34
|
|
|
*/ |
35
|
198 |
|
public function __construct(ScriptInterface $script) |
36
|
|
|
{ |
37
|
198 |
|
$this->script = $script; |
38
|
198 |
|
$this->decoded = $script->getScriptParser()->decode(); |
39
|
198 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param BufferInterface|null $publicKey |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
144 |
|
public function isPayToPublicKey(& $publicKey = null) |
46
|
|
|
{ |
47
|
144 |
|
if (count($this->decoded) < 1 || !$this->decoded[0]->isPush()) { |
48
|
120 |
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
42 |
|
$size = $this->decoded[0]->getDataSize(); |
52
|
42 |
|
if ($size === 33 || $size === 65) { |
53
|
30 |
|
$op = $this->decoded[1]; |
54
|
30 |
|
if (!$op->isPush() && $op->getOp() === Opcodes::OP_CHECKSIG) { |
55
|
30 |
|
$publicKey = $this->decoded[0]->getData(); |
56
|
30 |
|
return true; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
12 |
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param BufferInterface|null $pubKeyHash |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
114 |
|
public function isPayToPublicKeyHash(& $pubKeyHash = null) |
68
|
|
|
{ |
69
|
114 |
|
if (count($this->decoded) !== 5) { |
70
|
54 |
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
66 |
|
$dup = $this->decoded[0]; |
74
|
66 |
|
$hash = $this->decoded[1]; |
75
|
66 |
|
$buf = $this->decoded[2]; |
76
|
66 |
|
$eq = $this->decoded[3]; |
77
|
66 |
|
$checksig = $this->decoded[4]; |
78
|
|
|
|
79
|
66 |
|
foreach ([$dup, $hash, $eq, $checksig] as $op) { |
80
|
|
|
/** @var Operation $op */ |
81
|
66 |
|
if ($op->isPush()) { |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
66 |
|
} |
85
|
|
|
|
86
|
66 |
|
if ($dup->getOp() === Opcodes::OP_DUP |
87
|
66 |
|
&& $hash->getOp() === Opcodes::OP_HASH160 |
88
|
66 |
|
&& $buf->isPush() && $buf->getDataSize() === 20 |
89
|
66 |
|
&& $eq->getOp() === Opcodes::OP_EQUALVERIFY |
90
|
66 |
|
&& $checksig->getOp() === Opcodes::OP_CHECKSIG) { |
91
|
66 |
|
$pubKeyHash = $this->decoded[2]->getData(); |
92
|
66 |
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param BufferInterface|null $scriptHash |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
168 |
|
public function isPayToScriptHash(& $scriptHash = null) |
103
|
|
|
{ |
104
|
168 |
|
if (count($this->decoded) !== 3) { |
105
|
138 |
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
72 |
|
$hash = $this->decoded[0]; |
109
|
72 |
|
if ($hash->isPush() || !$hash->getOp() === Opcodes::OP_HASH160) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
72 |
|
$buffer = $this->decoded[1]; |
114
|
72 |
|
if (!$buffer->isPush() || $buffer->getDataSize() !== 20) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
72 |
|
$eq = $this->decoded[2]; |
120
|
72 |
|
if (!$eq->isPush() && $eq->getOp() === Opcodes::OP_EQUAL) { |
121
|
72 |
|
$scriptHash = $this->decoded[1]->getData(); |
122
|
72 |
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param BufferInterface[] $keys |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
78 |
|
public function isMultisig(& $keys = []) |
133
|
|
|
{ |
134
|
78 |
|
$count = count($this->decoded); |
135
|
78 |
|
if ($count <= 3) { |
136
|
30 |
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
48 |
|
$mOp = $this->decoded[0]; |
140
|
48 |
|
$nOp = $this->decoded[$count - 2]; |
141
|
48 |
|
$checksig = $this->decoded[$count - 1]; |
142
|
48 |
|
if ($mOp->isPush() || $nOp->isPush() || $checksig->isPush()) { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** @var Operation[] $vKeys */ |
147
|
48 |
|
$vKeys = array_slice($this->decoded, 1, -2); |
148
|
48 |
|
$solutions = []; |
149
|
48 |
|
foreach ($vKeys as $key) { |
150
|
48 |
|
if (!$key->isPush() || !PublicKey::isCompressedOrUncompressed($key->getData())) { |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
48 |
|
$solutions[] = $key->getData(); |
154
|
48 |
|
} |
155
|
|
|
|
156
|
48 |
|
if ($mOp->getOp() >= Opcodes::OP_0 |
157
|
48 |
|
&& $nOp->getOp() <= Opcodes::OP_16 |
158
|
48 |
|
&& $checksig->getOp() === Opcodes::OP_CHECKMULTISIG) { |
159
|
48 |
|
$keys = $solutions; |
160
|
48 |
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param BufferInterface $programHash |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
198 |
|
public function isWitness(& $programHash = null) |
171
|
|
|
{ |
172
|
132 |
|
$buffer = $this->script->getBuffer(); |
173
|
132 |
|
$size = $buffer->getSize(); |
174
|
|
|
|
175
|
132 |
|
if ($size < 4 || $size > 34) { |
176
|
72 |
|
return false; |
177
|
|
|
} |
178
|
|
|
|
179
|
78 |
|
$parser = $this->script->getScriptParser(); |
180
|
78 |
|
$script = $parser->decode(); |
181
|
78 |
|
if (count($script) !== 2 || !$script[1]->isPush()) { |
182
|
66 |
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
30 |
|
$version = $script[0]->getOp(); |
186
|
30 |
|
if ($version != Opcodes::OP_0 && ($version < Opcodes::OP_1 || $version > Opcodes::OP_16)) { |
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
|
190
|
30 |
|
$witness = $script[1]; |
191
|
30 |
|
if ($size === $witness->getDataSize() + 2) { |
192
|
30 |
|
$programHash = $witness->getData(); |
193
|
30 |
|
return true; |
194
|
198 |
|
} |
195
|
|
|
|
196
|
|
|
return false; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param BufferInterface|BufferInterface[] $solutions |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
138 |
|
public function classify(&$solutions = null) |
204
|
|
|
{ |
205
|
138 |
|
$type = self::UNKNOWN; |
206
|
138 |
|
$solution = null; |
207
|
138 |
|
if ($this->isPayToScriptHash($solution)) { |
208
|
|
|
/** @var BufferInterface $solution */ |
209
|
42 |
|
$type = self::PAYTOSCRIPTHASH; |
210
|
138 |
|
} elseif ($this->isWitness($solution)) { |
211
|
|
|
/** @var BufferInterface $solution */ |
212
|
30 |
|
if ($solution->getSize() == 20) { |
213
|
12 |
|
$type = self::WITNESS_V0_KEYHASH; |
214
|
12 |
|
} else { |
215
|
18 |
|
$type = self::WITNESS_V0_SCRIPTHASH; |
216
|
|
|
} |
217
|
132 |
|
} elseif ($this->isPayToPublicKey($solution)) { |
218
|
|
|
/** @var BufferInterface $solution */ |
219
|
18 |
|
$type = self::PAYTOPUBKEY; |
220
|
132 |
|
} elseif ($this->isPayToPublicKeyHash($solution)) { |
221
|
|
|
/** @var BufferInterface $solution */ |
222
|
66 |
|
$type = self::PAYTOPUBKEYHASH; |
223
|
114 |
|
} elseif ($this->isMultisig($solution)) { |
|
|
|
|
224
|
|
|
/** @var BufferInterface[] $solution */ |
225
|
36 |
|
$type = self::MULTISIG; |
226
|
36 |
|
} |
227
|
|
|
|
228
|
138 |
|
$solutions = $solution; |
229
|
|
|
|
230
|
138 |
|
return $type; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: