Complex classes like InputSigner often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InputSigner, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class InputSigner implements InputSignerInterface |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected static $canSign = [ |
||
| 41 | ScriptType::P2PKH, |
||
| 42 | ScriptType::P2PK, |
||
| 43 | ScriptType::MULTISIG |
||
| 44 | ]; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected static $validP2sh = [ |
||
| 50 | ScriptType::P2WKH, |
||
| 51 | ScriptType::P2WSH, |
||
| 52 | ScriptType::P2PKH, |
||
| 53 | ScriptType::P2PK, |
||
| 54 | ScriptType::MULTISIG |
||
| 55 | ]; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var EcAdapterInterface |
||
| 59 | */ |
||
| 60 | private $ecAdapter; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var OutputData $scriptPubKey |
||
| 64 | */ |
||
| 65 | private $scriptPubKey; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var OutputData $redeemScript |
||
| 69 | */ |
||
| 70 | private $redeemScript; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var OutputData $witnessScript |
||
| 74 | */ |
||
| 75 | private $witnessScript; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var OutputData |
||
| 79 | */ |
||
| 80 | private $signScript; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var bool |
||
| 84 | */ |
||
| 85 | private $tolerateInvalidPublicKey = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var SignData |
||
| 89 | */ |
||
| 90 | private $signData; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var int |
||
| 94 | */ |
||
| 95 | private $sigVersion; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var int |
||
| 99 | */ |
||
| 100 | private $flags; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var OutputData $witnessKeyHash |
||
| 104 | */ |
||
| 105 | private $witnessKeyHash; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var TransactionInterface |
||
| 109 | */ |
||
| 110 | private $tx; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var int |
||
| 114 | */ |
||
| 115 | private $nInput; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var TransactionOutputInterface |
||
| 119 | */ |
||
| 120 | private $txOut; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var PublicKeyInterface[] |
||
| 124 | */ |
||
| 125 | private $publicKeys = []; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var TransactionSignatureInterface[] |
||
| 129 | */ |
||
| 130 | private $signatures = []; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var int |
||
| 134 | */ |
||
| 135 | private $requiredSigs = 0; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var Interpreter |
||
| 139 | */ |
||
| 140 | private $interpreter; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var Checker |
||
| 144 | */ |
||
| 145 | private $signatureChecker; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var TransactionSignatureSerializer |
||
| 149 | */ |
||
| 150 | private $txSigSerializer; |
||
| 151 | |||
| 152 | 88 | /** |
|
| 153 | * @var PublicKeySerializerInterface |
||
| 154 | 88 | */ |
|
| 155 | 88 | private $pubKeySerializer; |
|
| 156 | 2 | ||
| 157 | /** |
||
| 158 | * InputSigner constructor. |
||
| 159 | 86 | * |
|
| 160 | 86 | * Note, the implementation of this class is considered internal |
|
| 161 | 86 | * and only the methods exposed on InputSignerInterface should |
|
| 162 | 86 | * be depended on to avoid BC breaks. |
|
| 163 | 86 | * |
|
| 164 | 86 | * The only recommended way to produce this class is using Signer::input() |
|
| 165 | 86 | * |
|
| 166 | * @param EcAdapterInterface $ecAdapter |
||
| 167 | 86 | * @param TransactionInterface $tx |
|
| 168 | 86 | * @param int $nInput |
|
| 169 | 86 | * @param TransactionOutputInterface $txOut |
|
| 170 | 86 | * @param SignData $signData |
|
| 171 | * @param TransactionSignatureSerializer|null $sigSerializer |
||
| 172 | 86 | * @param PublicKeySerializerInterface|null $pubKeySerializer |
|
| 173 | 58 | */ |
|
| 174 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $tx, $nInput, TransactionOutputInterface $txOut, SignData $signData, TransactionSignatureSerializer $sigSerializer = null, PublicKeySerializerInterface $pubKeySerializer = null) |
||
| 175 | { |
||
| 176 | $this->ecAdapter = $ecAdapter; |
||
| 177 | $this->tx = $tx; |
||
| 178 | $this->nInput = $nInput; |
||
| 179 | $this->txOut = $txOut; |
||
| 180 | $this->signData = $signData; |
||
| 181 | $this->flags = $signData->hasSignaturePolicy() ? $signData->getSignaturePolicy() : Interpreter::VERIFY_NONE; |
||
| 182 | $this->publicKeys = []; |
||
| 183 | $this->signatures = []; |
||
| 184 | |||
| 185 | 10 | $this->txSigSerializer = $sigSerializer ?: new TransactionSignatureSerializer(EcSerializer::getSerializer(DerSignatureSerializerInterface::class, true, $ecAdapter)); |
|
| 186 | $this->pubKeySerializer = $pubKeySerializer ?: EcSerializer::getSerializer(PublicKeySerializerInterface::class, true, $ecAdapter); |
||
| 187 | 10 | $this->signatureChecker = new Checker($this->ecAdapter, $this->tx, $nInput, $txOut->getValue(), $this->txSigSerializer, $this->pubKeySerializer); |
|
| 188 | 10 | $this->interpreter = new Interpreter($this->ecAdapter); |
|
| 189 | 10 | } |
|
| 190 | 10 | ||
| 191 | 10 | /** |
|
| 192 | * @return InputSigner |
||
| 193 | 10 | */ |
|
| 194 | public function extract() |
||
| 195 | 10 | { |
|
| 196 | 10 | $witnesses = $this->tx->getWitnesses(); |
|
| 197 | $witness = array_key_exists($this->nInput, $witnesses) ? $witnesses[$this->nInput]->all() : []; |
||
| 198 | 10 | ||
| 199 | 10 | return $this->solve( |
|
| 200 | 10 | $this->signData, |
|
| 201 | 10 | $this->txOut->getScript(), |
|
| 202 | $this->tx->getInput($this->nInput)->getScript(), |
||
| 203 | $witness |
||
| 204 | 10 | ); |
|
| 205 | 10 | } |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param bool $setting |
||
| 209 | * @return $this |
||
| 210 | 10 | */ |
|
| 211 | public function tolerateInvalidPublicKey($setting) |
||
| 212 | { |
||
| 213 | $this->tolerateInvalidPublicKey = (bool) $setting; |
||
| 214 | return $this; |
||
| 215 | 10 | } |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @param BufferInterface $vchPubKey |
||
| 219 | * @return PublicKeyInterface|null |
||
| 220 | * @throws \Exception |
||
| 221 | */ |
||
| 222 | 68 | protected function parseStepPublicKey(BufferInterface $vchPubKey) |
|
| 223 | { |
||
| 224 | 68 | try { |
|
| 225 | 68 | return $this->pubKeySerializer->parse($vchPubKey); |
|
| 226 | 68 | } catch (\Exception $e) { |
|
| 227 | if ($this->tolerateInvalidPublicKey) { |
||
| 228 | return null; |
||
| 229 | } |
||
| 230 | |||
| 231 | throw $e; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * A snipped from OP_CHECKMULTISIG - verifies signatures according to the |
||
| 237 | * order of the given public keys (taken from the script). |
||
| 238 | 50 | * |
|
| 239 | 42 | * @param ScriptInterface $script |
|
| 240 | * @param BufferInterface[] $signatures |
||
| 241 | * @param BufferInterface[] $publicKeys |
||
| 242 | * @param int $sigVersion |
||
| 243 | 42 | * @return \SplObjectStorage |
|
| 244 | 42 | */ |
|
| 245 | 4 | private function sortMultisigs(ScriptInterface $script, array $signatures, array $publicKeys, $sigVersion) |
|
| 246 | { |
||
| 247 | $sigCount = count($signatures); |
||
| 248 | 42 | $keyCount = count($publicKeys); |
|
| 249 | 42 | $ikey = $isig = 0; |
|
| 250 | $fSuccess = true; |
||
| 251 | $result = new \SplObjectStorage; |
||
| 252 | 42 | ||
| 253 | while ($fSuccess && $sigCount > 0) { |
||
| 254 | 50 | // Fetch the signature and public key |
|
| 255 | $sig = $signatures[$isig]; |
||
| 256 | $pubkey = $publicKeys[$ikey]; |
||
| 257 | |||
| 258 | if ($this->signatureChecker->checkSig($script, $sig, $pubkey, $sigVersion, $this->flags)) { |
||
| 259 | $result[$pubkey] = $sig; |
||
| 260 | $isig++; |
||
| 261 | $sigCount--; |
||
| 262 | } |
||
| 263 | |||
| 264 | $ikey++; |
||
| 265 | $keyCount--; |
||
| 266 | |||
| 267 | 26 | // If there are more signatures left than keys left, |
|
| 268 | // then too many signatures have failed. Exit early, |
||
| 269 | 26 | // without checking any further signatures. |
|
| 270 | if ($sigCount > $keyCount) { |
||
| 271 | $fSuccess = false; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | return $result; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param ScriptInterface $script |
||
| 280 | 56 | * @return \BitWasp\Buffertools\BufferInterface[] |
|
| 281 | */ |
||
| 282 | 56 | private function evalPushOnly(ScriptInterface $script) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Create a script consisting only of push-data operations. |
||
| 291 | 54 | * Suitable for a scriptSig. |
|
| 292 | 4 | * |
|
| 293 | * @param BufferInterface[] $buffers |
||
| 294 | * @return ScriptInterface |
||
| 295 | 50 | */ |
|
| 296 | private function pushAll(array $buffers) |
||
| 297 | { |
||
| 298 | return ScriptFactory::sequence(array_map(function ($buffer) { |
||
| 299 | if (!($buffer instanceof BufferInterface)) { |
||
| 300 | throw new \RuntimeException('Script contained a non-push opcode'); |
||
| 301 | } |
||
| 302 | |||
| 303 | $size = $buffer->getSize(); |
||
| 304 | if ($size === 0) { |
||
| 305 | return Opcodes::OP_0; |
||
| 306 | } |
||
| 307 | |||
| 308 | 64 | $first = ord($buffer->getBinary()); |
|
| 309 | if ($size === 1 && $first >= 1 && $first <= 16) { |
||
| 310 | 64 | return \BitWasp\Bitcoin\Script\encodeOpN($first); |
|
| 311 | 64 | } else { |
|
| 312 | return $buffer; |
||
| 313 | 64 | } |
|
| 314 | 38 | }, $buffers)); |
|
| 315 | 38 | } |
|
| 316 | 34 | ||
| 317 | 2 | /** |
|
| 318 | * Verify a scriptSig / scriptWitness against a scriptPubKey. |
||
| 319 | 32 | * Useful for checking the outcome of certain things, like hash locks (p2sh) |
|
| 320 | 36 | * |
|
| 321 | * @param int $flags |
||
| 322 | 28 | * @param ScriptInterface $scriptSig |
|
| 323 | 14 | * @param ScriptInterface $scriptPubKey |
|
| 324 | 14 | * @param ScriptWitnessInterface|null $scriptWitness |
|
| 325 | 12 | * @return bool |
|
| 326 | 2 | */ |
|
| 327 | private function verifySolution($flags, ScriptInterface $scriptSig, ScriptInterface $scriptPubKey, ScriptWitnessInterface $scriptWitness = null) |
||
| 331 | 14 | ||
| 332 | 14 | /** |
|
| 333 | 14 | * Evaluates a scriptPubKey against the provided chunks. |
|
| 334 | 14 | * |
|
| 335 | 14 | * @param ScriptInterface $scriptPubKey |
|
| 336 | 14 | * @param array $chunks |
|
| 337 | * @param int $sigVersion |
||
| 338 | 12 | * @return bool |
|
| 339 | 12 | */ |
|
| 340 | 12 | private function evaluateSolution(ScriptInterface $scriptPubKey, array $chunks, $sigVersion) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * This function is strictly for $canSign types. |
||
| 360 | * It will extract signatures/publicKeys when given $outputData, and $stack. |
||
| 361 | * $stack is the result of decompiling a scriptSig, or taking the witness data. |
||
| 362 | * |
||
| 363 | * @param OutputData $outputData |
||
| 364 | 58 | * @param array $stack |
|
| 365 | * @param int $sigVersion |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | public function extractFromValues(OutputData $outputData, array $stack, $sigVersion) |
||
| 369 | { |
||
| 370 | $type = $outputData->getType(); |
||
| 371 | $size = count($stack); |
||
| 372 | |||
| 373 | if (ScriptType::P2PKH === $type) { |
||
| 374 | $this->requiredSigs = 1; |
||
| 375 | if ($size === 2) { |
||
| 376 | 30 | if (!$this->evaluateSolution($outputData->getScript(), $stack, $sigVersion)) { |
|
| 377 | throw new \RuntimeException('Existing signatures are invalid!'); |
||
| 378 | 30 | } |
|
| 379 | 22 | $this->signatures = [$this->txSigSerializer->parse($stack[0])]; |
|
| 380 | 22 | $this->publicKeys = [$this->parseStepPublicKey($stack[1])]; |
|
|
|
|||
| 381 | 22 | } |
|
| 382 | 22 | } else if (ScriptType::P2PK === $type) { |
|
| 383 | $this->requiredSigs = 1; |
||
| 384 | if ($size === 1) { |
||
| 385 | if (!$this->evaluateSolution($outputData->getScript(), $stack, $sigVersion)) { |
||
| 386 | 26 | throw new \RuntimeException('Existing signatures are invalid!'); |
|
| 387 | 2 | } |
|
| 388 | $this->signatures = [$this->txSigSerializer->parse($stack[0])]; |
||
| 389 | 24 | } |
|
| 390 | $this->publicKeys = [$this->parseStepPublicKey($outputData->getSolution())]; |
||
| 391 | } else if (ScriptType::MULTISIG === $type) { |
||
| 392 | 26 | $info = new Multisig($outputData->getScript(), $this->pubKeySerializer); |
|
| 393 | $this->requiredSigs = $info->getRequiredSigCount(); |
||
| 394 | |||
| 395 | $keyBuffers = $info->getKeyBuffers(); |
||
| 396 | $this->publicKeys = []; |
||
| 397 | for ($i = 0; $i < $info->getKeyCount(); $i++) { |
||
| 398 | $this->publicKeys[$i] = $this->parseStepPublicKey($keyBuffers[$i]); |
||
| 399 | } |
||
| 400 | |||
| 401 | if ($size > 1) { |
||
| 402 | // Check signatures irrespective of scriptSig size, primes Checker cache, and need info |
||
| 403 | $check = $this->evaluateSolution($outputData->getScript(), $stack, $sigVersion); |
||
| 404 | 26 | $sigBufs = array_slice($stack, 1, $size - 1); |
|
| 405 | $sigBufCount = count($sigBufs); |
||
| 406 | 26 | ||
| 407 | 18 | // If we seem to have all signatures but fail evaluation, abort |
|
| 408 | 18 | if ($sigBufCount === $this->requiredSigs && !$check) { |
|
| 409 | 18 | throw new \RuntimeException('Existing signatures are invalid!'); |
|
| 410 | 18 | } |
|
| 411 | |||
| 412 | $keyToSigMap = $this->sortMultiSigs($outputData->getScript(), $sigBufs, $keyBuffers, $sigVersion); |
||
| 413 | |||
| 414 | 22 | // Here we learn if any signatures were invalid, it won't be in the map. |
|
| 415 | 4 | if ($sigBufCount !== count($keyToSigMap)) { |
|
| 416 | throw new \RuntimeException('Existing signatures are invalid!'); |
||
| 417 | 18 | } |
|
| 418 | |||
| 419 | foreach ($keyBuffers as $idx => $key) { |
||
| 420 | 18 | if (isset($keyToSigMap[$key])) { |
|
| 421 | $this->signatures[$idx] = $this->txSigSerializer->parse($keyToSigMap[$key]); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 | } else { |
||
| 426 | throw new \RuntimeException('Unsupported output type passed to extractFromValues'); |
||
| 427 | } |
||
| 428 | |||
| 429 | return $type; |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Checks $chunks (a decompiled scriptSig) for it's last element, |
||
| 434 | * or defers to SignData. If both are provided, it checks the |
||
| 435 | * value from $chunks against SignData. |
||
| 436 | * |
||
| 437 | 86 | * @param BufferInterface[] $chunks |
|
| 438 | * @param SignData $signData |
||
| 439 | 86 | * @return ScriptInterface |
|
| 440 | 86 | */ |
|
| 441 | 86 | private function findRedeemScript(array $chunks, SignData $signData) |
|
| 442 | 86 | { |
|
| 443 | 86 | if (count($chunks) > 0) { |
|
| 444 | 2 | $redeemScript = new Script($chunks[count($chunks) - 1]); |
|
| 445 | if ($signData->hasRedeemScript()) { |
||
| 446 | if (!$redeemScript->equals($signData->getRedeemScript())) { |
||
| 447 | 84 | throw new \RuntimeException('Extracted redeemScript did not match sign data'); |
|
| 448 | 38 | } |
|
| 449 | } |
||
| 450 | } else { |
||
| 451 | 84 | if (!$signData->hasRedeemScript()) { |
|
| 452 | 30 | throw new \RuntimeException('Redeem script not provided in sign data or scriptSig'); |
|
| 453 | 30 | } |
|
| 454 | 26 | $redeemScript = $signData->getRedeemScript(); |
|
| 455 | 2 | } |
|
| 456 | |||
| 457 | return $redeemScript; |
||
| 458 | 24 | } |
|
| 459 | 24 | ||
| 460 | 2 | /** |
|
| 461 | * Checks $witness (a witness structure) for it's last element, |
||
| 462 | * or defers to SignData. If both are provided, it checks the |
||
| 463 | 22 | * value from $chunks against SignData. |
|
| 464 | * |
||
| 465 | * @param BufferInterface[] $witness |
||
| 466 | 76 | * @param SignData $signData |
|
| 467 | 8 | * @return ScriptInterface |
|
| 468 | 8 | */ |
|
| 469 | 8 | private function findWitnessScript(array $witness, SignData $signData) |
|
| 487 | 64 | ||
| 488 | 64 | /** |
|
| 489 | * Needs to be called before using the instance. By `extract`. |
||
| 490 | 64 | * |
|
| 491 | * It ensures that violating the following prevents instance creation |
||
| 492 | 58 | * - the scriptPubKey can be directly signed, or leads to P2SH/P2WSH/P2WKH |
|
| 493 | * - the P2SH script covers signable types and P2WSH/P2WKH |
||
| 494 | * - the witnessScript covers signable types only |
||
| 495 | * |
||
| 496 | * @param SignData $signData |
||
| 497 | * @param ScriptInterface $scriptPubKey |
||
| 498 | * @param ScriptInterface $scriptSig |
||
| 499 | * @param BufferInterface[] $witness |
||
| 500 | * @return $this |
||
| 501 | 52 | */ |
|
| 502 | private function solve(SignData $signData, ScriptInterface $scriptPubKey, ScriptInterface $scriptSig, array $witness) |
||
| 503 | 52 | { |
|
| 504 | 2 | $classifier = new OutputClassifier(); |
|
| 505 | $sigVersion = SigHash::V0; |
||
| 506 | $sigChunks = []; |
||
| 507 | 50 | $solution = $this->scriptPubKey = $classifier->decode($scriptPubKey); |
|
| 508 | if ($solution->getType() !== ScriptType::P2SH && !in_array($solution->getType(), self::$validP2sh)) { |
||
| 509 | throw new \RuntimeException('scriptPubKey not supported'); |
||
| 510 | } |
||
| 511 | |||
| 512 | if ($solution->canSign()) { |
||
| 513 | $sigChunks = $this->evalPushOnly($scriptSig); |
||
| 514 | 2 | } |
|
| 515 | |||
| 516 | 2 | if ($solution->getType() === ScriptType::P2SH) { |
|
| 517 | $chunks = $this->evalPushOnly($scriptSig); |
||
| 518 | $redeemScript = $this->findRedeemScript($chunks, $signData); |
||
| 519 | if (!$this->verifySolution(Interpreter::VERIFY_SIGPUSHONLY, ScriptFactory::sequence([$redeemScript->getBuffer()]), $solution->getScript())) { |
||
| 520 | throw new \RuntimeException('Redeem script fails to solve pay-to-script-hash'); |
||
| 521 | } |
||
| 522 | |||
| 523 | $solution = $this->redeemScript = $classifier->decode($redeemScript); |
||
| 524 | if (!in_array($solution->getType(), self::$validP2sh)) { |
||
| 525 | throw new \RuntimeException('Unsupported pay-to-script-hash script'); |
||
| 526 | 50 | } |
|
| 527 | |||
| 528 | 50 | $sigChunks = array_slice($chunks, 0, -1); |
|
| 529 | 50 | } |
|
| 530 | 50 | ||
| 531 | if ($solution->getType() === ScriptType::P2WKH) { |
||
| 532 | $sigVersion = SigHash::V1; |
||
| 533 | $solution = $this->witnessKeyHash = $classifier->decode(ScriptFactory::scriptPubKey()->payToPubKeyHash($solution->getSolution())); |
||
| 534 | $sigChunks = $witness; |
||
| 535 | } else if ($solution->getType() === ScriptType::P2WSH) { |
||
| 536 | 56 | $sigVersion = SigHash::V1; |
|
| 537 | $witnessScript = $this->findWitnessScript($witness, $signData); |
||
| 538 | 56 | ||
| 539 | // Essentially all the reference implementation does |
||
| 540 | if (!$witnessScript->getWitnessScriptHash()->equals($solution->getSolution())) { |
||
| 541 | throw new \RuntimeException('Witness script fails to solve witness-script-hash'); |
||
| 542 | } |
||
| 543 | |||
| 544 | 50 | $solution = $this->witnessScript = $classifier->decode($witnessScript); |
|
| 545 | if (!in_array($this->witnessScript->getType(), self::$canSign)) { |
||
| 546 | 50 | throw new \RuntimeException('Unsupported witness-script-hash script'); |
|
| 547 | } |
||
| 548 | |||
| 549 | $sigChunks = array_slice($witness, 0, -1); |
||
| 550 | } |
||
| 551 | |||
| 552 | 50 | $this->sigVersion = $sigVersion; |
|
| 553 | $this->signScript = $solution; |
||
| 554 | 50 | ||
| 555 | $this->extractFromValues($solution, $sigChunks, $this->sigVersion); |
||
| 556 | |||
| 557 | return $this; |
||
| 558 | } |
||
| 559 | |||
| 560 | 50 | /** |
|
| 561 | * Pure function to produce a signature hash for a given $scriptCode, $sigHashType, $sigVersion. |
||
| 562 | 50 | * |
|
| 563 | * @param ScriptInterface $scriptCode |
||
| 564 | * @param int $sigHashType |
||
| 565 | * @param int $sigVersion |
||
| 566 | * @return BufferInterface |
||
| 567 | */ |
||
| 568 | 50 | public function calculateSigHashUnsafe(ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
| 569 | { |
||
| 570 | 50 | if (!$this->signatureChecker->isDefinedHashtype($sigHashType)) { |
|
| 571 | throw new \RuntimeException('Invalid sigHashType requested'); |
||
| 572 | } |
||
| 573 | |||
| 574 | return $this->signatureChecker->getSigHash($scriptCode, $sigHashType, $sigVersion); |
||
| 575 | } |
||
| 576 | 24 | ||
| 577 | /** |
||
| 578 | 24 | * Calculates the signature hash for the input for the given $sigHashType. |
|
| 579 | * |
||
| 580 | * @param int $sigHashType |
||
| 581 | * @return BufferInterface |
||
| 582 | */ |
||
| 583 | public function getSigHash($sigHashType) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Pure function to produce a signature for a given $key, $scriptCode, $sigHashType, $sigVersion. |
||
| 590 | 18 | * |
|
| 591 | * @param PrivateKeyInterface $key |
||
| 592 | * @param ScriptInterface $scriptCode |
||
| 593 | * @param int $sigHashType |
||
| 594 | * @param int $sigVersion |
||
| 595 | * @return TransactionSignatureInterface |
||
| 596 | 14 | */ |
|
| 597 | private function calculateSignature(PrivateKeyInterface $key, ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
||
| 598 | 14 | { |
|
| 599 | $hash = $this->calculateSigHashUnsafe($scriptCode, $sigHashType, $sigVersion); |
||
| 600 | $ecSignature = $this->ecAdapter->sign($hash, $key, new Rfc6979($this->ecAdapter, $key, $hash, 'sha256')); |
||
| 601 | return new TransactionSignature($this->ecAdapter, $ecSignature, $sigHashType); |
||
| 602 | 14 | } |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Returns whether all required signatures have been provided. |
||
| 606 | * |
||
| 607 | * @return bool |
||
| 608 | 50 | */ |
|
| 609 | public function isFullySigned() |
||
| 613 | |||
| 614 | 32 | /** |
|
| 615 | * Returns the required number of signatures for this input. |
||
| 616 | * |
||
| 617 | * @return int |
||
| 618 | */ |
||
| 619 | public function getRequiredSigs() |
||
| 623 | 18 | ||
| 624 | 8 | /** |
|
| 625 | * Returns an array where the values are either null, |
||
| 626 | * or a TransactionSignatureInterface. |
||
| 627 | * |
||
| 628 | 42 | * @return TransactionSignatureInterface[] |
|
| 629 | 6 | */ |
|
| 630 | public function getSignatures() |
||
| 631 | { |
||
| 632 | 36 | return $this->signatures; |
|
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Returns an array where the values are either null, |
||
| 637 | * or a PublicKeyInterface. |
||
| 638 | * |
||
| 639 | * @return PublicKeyInterface[] |
||
| 640 | */ |
||
| 641 | public function getPublicKeys() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * OutputData for the script to be signed (will be |
||
| 648 | 56 | * equal to getScriptPubKey, or getRedeemScript, or |
|
| 649 | * getWitnessScript. |
||
| 650 | * |
||
| 651 | * @return OutputData |
||
| 652 | 56 | */ |
|
| 653 | 12 | public function getSignScript() |
|
| 654 | 2 | { |
|
| 655 | return $this->signScript; |
||
| 656 | 10 | } |
|
| 657 | 46 | ||
| 658 | 34 | /** |
|
| 659 | 2 | * OutputData for the txOut script. |
|
| 660 | * |
||
| 661 | 32 | * @return OutputData |
|
| 662 | 32 | */ |
|
| 663 | 12 | public function getScriptPubKey() |
|
| 667 | 10 | ||
| 668 | 12 | /** |
|
| 669 | * Returns OutputData for the P2SH redeemScript. |
||
| 670 | * |
||
| 671 | * @return OutputData |
||
| 672 | 12 | */ |
|
| 673 | 12 | public function getRedeemScript() |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Returns OutputData for the P2WSH witnessScript. |
||
| 684 | * |
||
| 685 | * @return OutputData |
||
| 686 | */ |
||
| 687 | public function getWitnessScript() |
||
| 695 | |||
| 696 | 50 | /** |
|
| 697 | 50 | * Returns whether the scriptPubKey is P2SH. |
|
| 698 | 22 | * |
|
| 699 | * @return bool |
||
| 700 | */ |
||
| 701 | 50 | public function isP2SH() |
|
| 709 | 22 | ||
| 710 | 22 | /** |
|
| 711 | 22 | * Returns whether the scriptPubKey or redeemScript is P2WSH. |
|
| 712 | * |
||
| 713 | 2 | * @return bool |
|
| 714 | */ |
||
| 715 | public function isP2WSH() |
||
| 729 | 50 | ||
| 730 | /** |
||
| 731 | 50 | * Sign the input using $key and $sigHashTypes |
|
| 732 | 50 | * |
|
| 733 | 10 | * @param PrivateKeyInterface $privateKey |
|
| 734 | 10 | * @param int $sigHashType |
|
| 735 | * @return $this |
||
| 736 | 42 | */ |
|
| 737 | 32 | public function sign(PrivateKeyInterface $privateKey, $sigHashType = SigHash::ALL) |
|
| 783 | |||
| 784 | 50 | /** |
|
| 785 | 8 | * Verifies the input using $flags for script verification |
|
| 786 | 44 | * |
|
| 787 | 14 | * @param int $flags |
|
| 788 | 14 | * @return bool |
|
| 789 | 14 | */ |
|
| 790 | public function verify($flags = null) |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Produces the script stack that solves the $outputType |
||
| 827 | * |
||
| 828 | * @param string $outputType |
||
| 829 | * @return BufferInterface[] |
||
| 830 | */ |
||
| 831 | private function serializeSolution($outputType) |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Produces a SigValues instance containing the scriptSig & script witness |
||
| 858 | * |
||
| 859 | * @return SigValues |
||
| 860 | */ |
||
| 861 | public function serializeSignatures() |
||
| 901 | } |
||
| 902 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..