|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Script\Interpreter; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
|
6
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer; |
|
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PublicKey; |
|
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface; |
|
9
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\DerSignatureSerializerInterface; |
|
10
|
|
|
use BitWasp\Bitcoin\Exceptions\ScriptRuntimeException; |
|
11
|
|
|
use BitWasp\Bitcoin\Exceptions\SignatureNotCanonical; |
|
12
|
|
|
use BitWasp\Bitcoin\Locktime; |
|
13
|
|
|
use BitWasp\Bitcoin\Script\ScriptInterface; |
|
14
|
|
|
use BitWasp\Bitcoin\Serializer\Signature\TransactionSignatureSerializer; |
|
15
|
|
|
use BitWasp\Bitcoin\Serializer\Transaction\TransactionSerializer; |
|
16
|
|
|
use BitWasp\Bitcoin\Signature\TransactionSignature; |
|
17
|
|
|
use BitWasp\Bitcoin\Transaction\SignatureHash\Hasher; |
|
18
|
|
|
use BitWasp\Bitcoin\Transaction\SignatureHash\SigHash; |
|
19
|
|
|
use BitWasp\Bitcoin\Transaction\SignatureHash\V1Hasher; |
|
20
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionInputInterface; |
|
21
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionInterface; |
|
22
|
|
|
use BitWasp\Buffertools\Buffer; |
|
23
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
24
|
|
|
|
|
25
|
|
|
class Checker |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var EcAdapterInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $adapter; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var TransactionInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $transaction; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var int |
|
39
|
|
|
*/ |
|
40
|
|
|
private $nInput; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var int|string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $amount; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var Hasher |
|
49
|
|
|
*/ |
|
50
|
|
|
private $hasherV0; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
private $sigHashCache = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
private $sigCache = []; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var TransactionSignatureSerializer |
|
64
|
|
|
*/ |
|
65
|
|
|
private $sigSerializer; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var PublicKeySerializerInterface |
|
69
|
|
|
*/ |
|
70
|
|
|
private $pubKeySerializer; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Checker constructor. |
|
74
|
|
|
* @param EcAdapterInterface $ecAdapter |
|
75
|
|
|
* @param TransactionInterface $transaction |
|
76
|
|
|
* @param int $nInput |
|
77
|
|
|
* @param int $amount |
|
78
|
|
|
* @param TransactionSignatureSerializer|null $sigSerializer |
|
79
|
|
|
* @param PublicKeySerializerInterface|null $pubKeySerializer |
|
80
|
|
|
*/ |
|
81
|
2456 |
|
public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $transaction, $nInput, $amount, TransactionSignatureSerializer $sigSerializer = null, PublicKeySerializerInterface $pubKeySerializer = null) |
|
82
|
|
|
{ |
|
83
|
2456 |
|
$this->sigSerializer = $sigSerializer ?: new TransactionSignatureSerializer(EcSerializer::getSerializer(DerSignatureSerializerInterface::class, $ecAdapter)); |
|
|
|
|
|
|
84
|
2456 |
|
$this->pubKeySerializer = $pubKeySerializer ?: EcSerializer::getSerializer(PublicKeySerializerInterface::class, $ecAdapter); |
|
|
|
|
|
|
85
|
2456 |
|
$this->adapter = $ecAdapter; |
|
86
|
2456 |
|
$this->transaction = $transaction; |
|
87
|
2456 |
|
$this->nInput = $nInput; |
|
88
|
2456 |
|
$this->amount = $amount; |
|
89
|
2456 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param BufferInterface $signature |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
140 |
|
public function isValidSignatureEncoding(BufferInterface $signature) |
|
96
|
|
|
{ |
|
97
|
|
|
try { |
|
98
|
140 |
|
TransactionSignature::isDERSignature($signature); |
|
99
|
92 |
|
return true; |
|
100
|
52 |
|
} catch (SignatureNotCanonical $e) { |
|
101
|
|
|
/* In any case, we will return false outside this block */ |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
52 |
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param BufferInterface $signature |
|
109
|
|
|
* @return bool |
|
110
|
|
|
* @throws ScriptRuntimeException |
|
111
|
|
|
* @throws \Exception |
|
112
|
|
|
*/ |
|
113
|
8 |
|
public function isLowDerSignature(BufferInterface $signature) |
|
114
|
|
|
{ |
|
115
|
8 |
|
if (!$this->isValidSignatureEncoding($signature)) { |
|
116
|
2 |
|
throw new ScriptRuntimeException(Interpreter::VERIFY_DERSIG, 'Signature with incorrect encoding'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
6 |
|
$binary = $signature->getBinary(); |
|
120
|
6 |
|
$nLenR = ord($binary[3]); |
|
121
|
6 |
|
$nLenS = ord($binary[5 + $nLenR]); |
|
122
|
6 |
|
$s = $signature->slice(6 + $nLenR, $nLenS)->getGmp(); |
|
123
|
|
|
|
|
124
|
6 |
|
return $this->adapter->validateSignatureElement($s, true); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param int $hashType |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
80 |
|
public function isDefinedHashtype($hashType) |
|
132
|
|
|
{ |
|
133
|
80 |
|
$nHashType = $hashType & (~(SigHash::ANYONECANPAY)); |
|
134
|
|
|
|
|
135
|
80 |
|
return !(($nHashType < SigHash::ALL) || ($nHashType > SigHash::SINGLE)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Determine whether the sighash byte appended to the signature encodes |
|
140
|
|
|
* a valid sighash type. |
|
141
|
|
|
* |
|
142
|
|
|
* @param BufferInterface $signature |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
28 |
|
public function isDefinedHashtypeSignature(BufferInterface $signature) |
|
146
|
|
|
{ |
|
147
|
28 |
|
if ($signature->getSize() === 0) { |
|
148
|
2 |
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
26 |
|
$binary = $signature->getBinary(); |
|
152
|
26 |
|
return $this->isDefinedHashtype(ord(substr($binary, -1))); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param BufferInterface $signature |
|
157
|
|
|
* @param int $flags |
|
158
|
|
|
* @return $this |
|
159
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\ScriptRuntimeException |
|
160
|
|
|
*/ |
|
161
|
310 |
|
public function checkSignatureEncoding(BufferInterface $signature, $flags) |
|
162
|
|
|
{ |
|
163
|
310 |
|
if ($signature->getSize() === 0) { |
|
164
|
54 |
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
270 |
|
if (($flags & (Interpreter::VERIFY_DERSIG | Interpreter::VERIFY_LOW_S | Interpreter::VERIFY_STRICTENC)) !== 0 && !$this->isValidSignatureEncoding($signature)) { |
|
168
|
50 |
|
throw new ScriptRuntimeException(Interpreter::VERIFY_DERSIG, 'Signature with incorrect encoding'); |
|
169
|
224 |
|
} else if (($flags & Interpreter::VERIFY_LOW_S) !== 0 && !$this->isLowDerSignature($signature)) { |
|
170
|
4 |
|
throw new ScriptRuntimeException(Interpreter::VERIFY_LOW_S, 'Signature s element was not low'); |
|
171
|
220 |
|
} else if (($flags & Interpreter::VERIFY_STRICTENC) !== 0 && !$this->isDefinedHashtypeSignature($signature)) { |
|
172
|
6 |
|
throw new ScriptRuntimeException(Interpreter::VERIFY_STRICTENC, 'Signature with invalid hashtype'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
214 |
|
return $this; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param BufferInterface $publicKey |
|
180
|
|
|
* @param int $flags |
|
181
|
|
|
* @return $this |
|
182
|
|
|
* @throws \Exception |
|
183
|
|
|
*/ |
|
184
|
250 |
|
public function checkPublicKeyEncoding(BufferInterface $publicKey, $flags) |
|
185
|
|
|
{ |
|
186
|
250 |
|
if (($flags & Interpreter::VERIFY_STRICTENC) !== 0 && !PublicKey::isCompressedOrUncompressed($publicKey)) { |
|
187
|
12 |
|
throw new ScriptRuntimeException(Interpreter::VERIFY_STRICTENC, 'Public key with incorrect encoding'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
238 |
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param ScriptInterface $script |
|
195
|
|
|
* @param int $sigHashType |
|
196
|
|
|
* @param int $sigVersion |
|
197
|
|
|
* @return BufferInterface |
|
198
|
|
|
*/ |
|
199
|
174 |
|
public function getSigHash(ScriptInterface $script, $sigHashType, $sigVersion) |
|
200
|
|
|
{ |
|
201
|
174 |
|
$cacheCheck = $sigVersion . $sigHashType . $script->getBuffer()->getBinary(); |
|
202
|
174 |
|
if (!isset($this->sigHashCache[$cacheCheck])) { |
|
203
|
174 |
|
if ($sigVersion === 1) { |
|
204
|
46 |
|
$hasher = new V1Hasher($this->transaction, $this->amount); |
|
205
|
|
|
} else { |
|
206
|
130 |
|
if ($this->hasherV0) { |
|
207
|
|
|
$hasher = $this->hasherV0; |
|
208
|
|
|
} else { |
|
209
|
130 |
|
$hasher = $this->hasherV0 = new Hasher($this->transaction, new TransactionSerializer()); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
174 |
|
$hash = $hasher->calculate($script, $this->nInput, $sigHashType); |
|
214
|
174 |
|
$this->sigHashCache[$cacheCheck] = $hash->getBinary(); |
|
215
|
|
|
} else { |
|
216
|
12 |
|
$hash = new Buffer($this->sigHashCache[$cacheCheck], 32, $this->adapter->getMath()); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
174 |
|
return $hash; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @param ScriptInterface $script |
|
224
|
|
|
* @param BufferInterface $sigBuf |
|
225
|
|
|
* @param BufferInterface $keyBuf |
|
226
|
|
|
* @param int $sigVersion |
|
227
|
|
|
* @param int $flags |
|
228
|
|
|
* @return bool |
|
229
|
|
|
* @throws ScriptRuntimeException |
|
230
|
|
|
*/ |
|
231
|
296 |
|
public function checkSig(ScriptInterface $script, BufferInterface $sigBuf, BufferInterface $keyBuf, $sigVersion, $flags) |
|
232
|
|
|
{ |
|
233
|
|
|
$this |
|
234
|
296 |
|
->checkSignatureEncoding($sigBuf, $flags) |
|
235
|
246 |
|
->checkPublicKeyEncoding($keyBuf, $flags); |
|
236
|
|
|
|
|
237
|
|
|
try { |
|
238
|
236 |
|
$cacheCheck = $flags . $sigVersion . $keyBuf->getBinary() . $sigBuf->getBinary(); |
|
239
|
236 |
|
if (!isset($this->sigCache[$cacheCheck])) { |
|
240
|
236 |
|
$txSignature = $this->sigSerializer->parse($sigBuf); |
|
|
|
|
|
|
241
|
184 |
|
$publicKey = $this->pubKeySerializer->parse($keyBuf); |
|
242
|
|
|
|
|
243
|
172 |
|
$hash = $this->getSigHash($script, $txSignature->getHashType(), $sigVersion); |
|
244
|
172 |
|
$result = $this->sigCache[$cacheCheck] = $this->adapter->verify($hash, $publicKey, $txSignature->getSignature()); |
|
245
|
|
|
} else { |
|
246
|
4 |
|
$result = $this->sigCache[$cacheCheck]; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
172 |
|
return $result; |
|
250
|
78 |
|
} catch (\Exception $e) { |
|
251
|
78 |
|
return false; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param int $txLockTime |
|
257
|
|
|
* @param int $nThreshold |
|
258
|
|
|
* @param \BitWasp\Bitcoin\Script\Interpreter\Number $lockTime |
|
259
|
|
|
* @return bool |
|
260
|
|
|
*/ |
|
261
|
|
|
private function verifyLockTime($txLockTime, $nThreshold, \BitWasp\Bitcoin\Script\Interpreter\Number $lockTime) |
|
262
|
|
|
{ |
|
263
|
|
|
$nTime = $lockTime->getInt(); |
|
264
|
|
|
if (($txLockTime < $nThreshold && $nTime < $nThreshold) || |
|
265
|
|
|
($txLockTime >= $nThreshold && $nTime >= $nThreshold) |
|
266
|
|
|
) { |
|
267
|
|
|
return false; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
return $nTime >= $txLockTime; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @param \BitWasp\Bitcoin\Script\Interpreter\Number $lockTime |
|
275
|
|
|
* @return bool |
|
276
|
|
|
*/ |
|
277
|
|
|
public function checkLockTime(\BitWasp\Bitcoin\Script\Interpreter\Number $lockTime) |
|
278
|
|
|
{ |
|
279
|
|
|
if ($this->transaction->getInput($this->nInput)->isFinal()) { |
|
280
|
|
|
return false; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return $this->verifyLockTime($this->transaction->getLockTime(), Locktime::BLOCK_MAX, $lockTime); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* @param \BitWasp\Bitcoin\Script\Interpreter\Number $sequence |
|
289
|
|
|
* @return bool |
|
290
|
|
|
*/ |
|
291
|
4 |
|
public function checkSequence(\BitWasp\Bitcoin\Script\Interpreter\Number $sequence) |
|
292
|
|
|
{ |
|
293
|
4 |
|
$txSequence = $this->transaction->getInput($this->nInput)->getSequence(); |
|
294
|
4 |
|
if ($this->transaction->getVersion() < 2) { |
|
295
|
4 |
|
return false; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
if (($txSequence & TransactionInputInterface::SEQUENCE_LOCKTIME_DISABLE_FLAG) !== 0) { |
|
299
|
|
|
return true; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
$mask = TransactionInputInterface::SEQUENCE_LOCKTIME_TYPE_FLAG | TransactionInputInterface::SEQUENCE_LOCKTIME_MASK; |
|
303
|
|
|
return $this->verifyLockTime( |
|
304
|
|
|
$txSequence & $mask, |
|
305
|
|
|
TransactionInputInterface::SEQUENCE_LOCKTIME_TYPE_FLAG, |
|
306
|
|
|
Number::int($sequence->getInt() & $mask) |
|
307
|
|
|
); |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
|
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: