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