1 | <?php |
||
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 | 1328 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $transaction, $nInput, $amount, TransactionSignatureSerializer $sigSerializer = null, PublicKeySerializerInterface $pubKeySerializer = null) |
|
82 | { |
||
83 | 1328 | $this->sigSerializer = $sigSerializer ?: new TransactionSignatureSerializer(EcSerializer::getSerializer(DerSignatureSerializerInterface::class, $ecAdapter)); |
|
|
|||
84 | 1328 | $this->pubKeySerializer = $pubKeySerializer ?: EcSerializer::getSerializer(PublicKeySerializerInterface::class, $ecAdapter); |
|
85 | 1328 | $this->adapter = $ecAdapter; |
|
86 | 1328 | $this->transaction = $transaction; |
|
87 | 1328 | $this->nInput = $nInput; |
|
88 | 1328 | $this->amount = $amount; |
|
89 | 1328 | } |
|
90 | |||
91 | /** |
||
92 | * @param BufferInterface $signature |
||
93 | * @return bool |
||
94 | */ |
||
95 | 127 | public function isValidSignatureEncoding(BufferInterface $signature) |
|
96 | { |
||
97 | try { |
||
98 | 127 | TransactionSignature::isDERSignature($signature); |
|
99 | 81 | return true; |
|
100 | 50 | } catch (SignatureNotCanonical $e) { |
|
101 | /* In any case, we will return false outside this block */ |
||
102 | } |
||
103 | |||
104 | 50 | return false; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param BufferInterface $signature |
||
109 | * @return bool |
||
110 | * @throws ScriptRuntimeException |
||
111 | * @throws \Exception |
||
112 | */ |
||
113 | 7 | public function isLowDerSignature(BufferInterface $signature) |
|
114 | { |
||
115 | 7 | if (!$this->isValidSignatureEncoding($signature)) { |
|
116 | 2 | throw new ScriptRuntimeException(Interpreter::VERIFY_DERSIG, 'Signature with incorrect encoding'); |
|
117 | } |
||
118 | |||
119 | 5 | $binary = $signature->getBinary(); |
|
120 | 5 | $nLenR = ord($binary[3]); |
|
121 | 5 | $nLenS = ord($binary[5 + $nLenR]); |
|
122 | 5 | $s = $signature->slice(6 + $nLenR, $nLenS)->getGmp(); |
|
123 | |||
124 | 5 | return $this->adapter->validateSignatureElement($s, true); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param int $hashType |
||
129 | * @return bool |
||
130 | */ |
||
131 | 68 | public function isDefinedHashtype($hashType) |
|
132 | { |
||
133 | 68 | $nHashType = $hashType & (~(SigHash::ANYONECANPAY)); |
|
134 | |||
135 | 68 | 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 | 18 | public function isDefinedHashtypeSignature(BufferInterface $signature) |
|
146 | { |
||
147 | 18 | if ($signature->getSize() === 0) { |
|
148 | 2 | return false; |
|
149 | } |
||
150 | |||
151 | 16 | $binary = $signature->getBinary(); |
|
152 | 16 | 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 | 227 | public function checkSignatureEncoding(BufferInterface $signature, $flags) |
|
162 | { |
||
163 | 227 | if ($signature->getSize() === 0) { |
|
164 | 44 | return $this; |
|
165 | } |
||
166 | |||
167 | 195 | if (($flags & (Interpreter::VERIFY_DERSIG | Interpreter::VERIFY_LOW_S | Interpreter::VERIFY_STRICTENC)) !== 0 && !$this->isValidSignatureEncoding($signature)) { |
|
168 | 48 | throw new ScriptRuntimeException(Interpreter::VERIFY_DERSIG, 'Signature with incorrect encoding'); |
|
169 | 151 | } else if (($flags & Interpreter::VERIFY_LOW_S) !== 0 && !$this->isLowDerSignature($signature)) { |
|
170 | 3 | throw new ScriptRuntimeException(Interpreter::VERIFY_LOW_S, 'Signature s element was not low'); |
|
171 | 148 | } else if (($flags & Interpreter::VERIFY_STRICTENC) !== 0 && !$this->isDefinedHashtypeSignature($signature)) { |
|
172 | 4 | throw new ScriptRuntimeException(Interpreter::VERIFY_STRICTENC, 'Signature with invalid hashtype'); |
|
173 | } |
||
174 | |||
175 | 144 | return $this; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param BufferInterface $publicKey |
||
180 | * @param int $flags |
||
181 | * @return $this |
||
182 | * @throws \Exception |
||
183 | */ |
||
184 | 172 | public function checkPublicKeyEncoding(BufferInterface $publicKey, $flags) |
|
192 | |||
193 | /** |
||
194 | * @param ScriptInterface $script |
||
195 | * @param int $sigHashType |
||
196 | * @param int $sigVersion |
||
197 | * @return BufferInterface |
||
198 | */ |
||
199 | 118 | public function getSigHash(ScriptInterface $script, $sigHashType, $sigVersion) |
|
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 | 213 | public function checkSig(ScriptInterface $script, BufferInterface $sigBuf, BufferInterface $keyBuf, $sigVersion, $flags) |
|
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) |
||
272 | |||
273 | /** |
||
274 | * @param \BitWasp\Bitcoin\Script\Interpreter\Number $lockTime |
||
275 | * @return bool |
||
276 | */ |
||
277 | public function checkLockTime(\BitWasp\Bitcoin\Script\Interpreter\Number $lockTime) |
||
285 | |||
286 | |||
287 | /** |
||
288 | * @param \BitWasp\Bitcoin\Script\Interpreter\Number $sequence |
||
289 | * @return bool |
||
290 | */ |
||
291 | 2 | public function checkSequence(\BitWasp\Bitcoin\Script\Interpreter\Number $sequence) |
|
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: