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 |
||
| 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 | * @param EcAdapterInterface $ecAdapter |
|
| 160 | 86 | * @param TransactionInterface $tx |
|
| 161 | 86 | * @param $nInput |
|
| 162 | 86 | * @param TransactionOutputInterface $txOut |
|
| 163 | 86 | * @param SignData $signData |
|
| 164 | 86 | * @param TransactionSignatureSerializer|null $sigSerializer |
|
| 165 | 86 | * @param PublicKeySerializerInterface|null $pubKeySerializer |
|
| 166 | */ |
||
| 167 | 86 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $tx, $nInput, TransactionOutputInterface $txOut, SignData $signData, TransactionSignatureSerializer $sigSerializer = null, PublicKeySerializerInterface $pubKeySerializer = null) |
|
| 183 | |||
| 184 | /** |
||
| 185 | 10 | * @return InputSigner |
|
| 186 | */ |
||
| 187 | 10 | public function extract() |
|
| 199 | 10 | ||
| 200 | 10 | /** |
|
| 201 | 10 | * @param bool $setting |
|
| 202 | * @return $this |
||
| 203 | */ |
||
| 204 | 10 | public function tolerateInvalidPublicKey($setting) |
|
| 209 | |||
| 210 | 10 | /** |
|
| 211 | * @param BufferInterface $vchPubKey |
||
| 212 | * @return PublicKeyInterface|null |
||
| 213 | * @throws \Exception |
||
| 214 | */ |
||
| 215 | 10 | protected function parseStepPublicKey(BufferInterface $vchPubKey) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * A snipped from OP_CHECKMULTISIG - verifies signatures according to the |
||
| 230 | * order of the given public keys (taken from the script). |
||
| 231 | * |
||
| 232 | * @param ScriptInterface $script |
||
| 233 | * @param BufferInterface[] $signatures |
||
| 234 | * @param BufferInterface[] $publicKeys |
||
| 235 | * @param int $sigVersion |
||
| 236 | * @return \SplObjectStorage |
||
| 237 | */ |
||
| 238 | 50 | private function sortMultisigs(ScriptInterface $script, array $signatures, array $publicKeys, $sigVersion) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @param ScriptInterface $script |
||
| 273 | * @return \BitWasp\Buffertools\BufferInterface[] |
||
| 274 | */ |
||
| 275 | private function evalPushOnly(ScriptInterface $script) |
||
| 281 | |||
| 282 | 56 | /** |
|
| 283 | 56 | * Create a script consisting only of push-data operations. |
|
| 284 | 2 | * Suitable for a scriptSig. |
|
| 285 | * |
||
| 286 | * @param BufferInterface[] $buffers |
||
| 287 | 54 | * @return ScriptInterface |
|
| 288 | */ |
||
| 289 | private function pushAll(array $buffers) |
||
| 309 | |||
| 310 | 64 | /** |
|
| 311 | 64 | * Verify a scriptSig / scriptWitness against a scriptPubKey. |
|
| 312 | * Useful for checking the outcome of certain things, like hash locks (p2sh) |
||
| 313 | 64 | * |
|
| 314 | 38 | * @param int $flags |
|
| 315 | 38 | * @param ScriptInterface $scriptSig |
|
| 316 | 34 | * @param ScriptInterface $scriptPubKey |
|
| 317 | 2 | * @param ScriptWitnessInterface|null $scriptWitness |
|
| 318 | * @return bool |
||
| 319 | 32 | */ |
|
| 320 | 36 | private function verifySolution($flags, ScriptInterface $scriptSig, ScriptInterface $scriptPubKey, ScriptWitnessInterface $scriptWitness = null) |
|
| 324 | 14 | ||
| 325 | 12 | /** |
|
| 326 | 2 | * Evaluates a scriptPubKey against the provided chunks. |
|
| 327 | * |
||
| 328 | 10 | * @param ScriptInterface $scriptPubKey |
|
| 329 | * @param array $chunks |
||
| 330 | 12 | * @param int $sigVersion |
|
| 331 | 14 | * @return bool |
|
| 332 | 14 | */ |
|
| 333 | 14 | private function evaluateSolution(ScriptInterface $scriptPubKey, array $chunks, $sigVersion) |
|
| 350 | 10 | ||
| 351 | /** |
||
| 352 | * This function is strictly for $canSign types. |
||
| 353 | * It will extract signatures/publicKeys when given $outputData, and $stack. |
||
| 354 | 10 | * $stack is the result of decompiling a scriptSig, or taking the witness data. |
|
| 355 | 10 | * |
|
| 356 | 12 | * @param OutputData $outputData |
|
| 357 | * @param array $stack |
||
| 358 | * @param int $sigVersion |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function extractFromValues(OutputData $outputData, array $stack, $sigVersion) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Checks $chunks (a decompiled scriptSig) for it's last element, |
||
| 427 | * or defers to SignData. If both are provided, it checks the |
||
| 428 | * value from $chunks against SignData. |
||
| 429 | * |
||
| 430 | * @param BufferInterface[] $chunks |
||
| 431 | * @param SignData $signData |
||
| 432 | * @return ScriptInterface |
||
| 433 | */ |
||
| 434 | private function findRedeemScript(array $chunks, SignData $signData) |
||
| 452 | 30 | ||
| 453 | 30 | /** |
|
| 454 | 26 | * Checks $witness (a witness structure) for it's last element, |
|
| 455 | 2 | * or defers to SignData. If both are provided, it checks the |
|
| 456 | * value from $chunks against SignData. |
||
| 457 | * |
||
| 458 | 24 | * @param BufferInterface[] $witness |
|
| 459 | 24 | * @param SignData $signData |
|
| 460 | 2 | * @return ScriptInterface |
|
| 461 | */ |
||
| 462 | private function findWitnessScript(array $witness, SignData $signData) |
||
| 480 | 16 | ||
| 481 | 2 | /** |
|
| 482 | * Called upon instance creation. |
||
| 483 | * |
||
| 484 | 14 | * It ensures that violating the following prevents instance creation |
|
| 485 | * - the scriptPubKey can be directly signed, or leads to P2SH/P2WSH/P2WKH |
||
| 486 | * - the P2SH script covers signable types and P2WSH/P2WKH |
||
| 487 | 64 | * - the witnessScript covers signable types only |
|
| 488 | 64 | * |
|
| 489 | * @param SignData $signData |
||
| 490 | 64 | * @param ScriptInterface $scriptPubKey |
|
| 491 | * @param ScriptInterface $scriptSig |
||
| 492 | 58 | * @param BufferInterface[] $witness |
|
| 493 | * @return $this |
||
| 494 | */ |
||
| 495 | private function solve(SignData $signData, ScriptInterface $scriptPubKey, ScriptInterface $scriptSig, array $witness) |
||
| 552 | 50 | ||
| 553 | /** |
||
| 554 | 50 | * @param ScriptInterface $scriptCode |
|
| 555 | * @param int $sigHashType |
||
| 556 | * @param int $sigVersion |
||
| 557 | * @return BufferInterface |
||
| 558 | */ |
||
| 559 | public function calculateSigHashUnsafe(ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
||
| 567 | |||
| 568 | 50 | /** |
|
| 569 | * @param int $sigHashType |
||
| 570 | 50 | * @return BufferInterface |
|
| 571 | */ |
||
| 572 | public function getSigHash($sigHashType) |
||
| 576 | 24 | ||
| 577 | /** |
||
| 578 | 24 | * @param PrivateKeyInterface $key |
|
| 579 | * @param ScriptInterface $scriptCode |
||
| 580 | * @param int $sigHashType |
||
| 581 | * @param int $sigVersion |
||
| 582 | * @return TransactionSignatureInterface |
||
| 583 | */ |
||
| 584 | 18 | private function calculateSignature(PrivateKeyInterface $key, ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
| 590 | 18 | ||
| 591 | /** |
||
| 592 | * @return bool |
||
| 593 | */ |
||
| 594 | public function isFullySigned() |
||
| 598 | 14 | ||
| 599 | /** |
||
| 600 | * @return int |
||
| 601 | */ |
||
| 602 | 14 | public function getRequiredSigs() |
|
| 606 | |||
| 607 | /** |
||
| 608 | 50 | * @return TransactionSignatureInterface[] |
|
| 609 | */ |
||
| 610 | 50 | public function getSignatures() |
|
| 614 | 32 | ||
| 615 | /** |
||
| 616 | * @return PublicKeyInterface[] |
||
| 617 | */ |
||
| 618 | public function getPublicKeys() |
||
| 622 | 50 | ||
| 623 | 18 | /** |
|
| 624 | 8 | * @return OutputData |
|
| 625 | */ |
||
| 626 | public function getSignScript() |
||
| 630 | |||
| 631 | /** |
||
| 632 | 36 | * @return OutputData |
|
| 633 | */ |
||
| 634 | public function getScriptPubKey() |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @return OutputData |
||
| 641 | */ |
||
| 642 | 56 | public function getRedeemScript() |
|
| 650 | |||
| 651 | /** |
||
| 652 | 56 | * @return OutputData |
|
| 653 | 12 | */ |
|
| 654 | 2 | public function getWitnessScript() |
|
| 662 | 32 | ||
| 663 | 12 | /** |
|
| 664 | 12 | * @return bool |
|
| 665 | 12 | */ |
|
| 666 | 12 | public function isP2SH() |
|
| 674 | |||
| 675 | /** |
||
| 676 | * @return bool |
||
| 677 | */ |
||
| 678 | public function isP2WSH() |
||
| 692 | 50 | ||
| 693 | 50 | /** |
|
| 694 | * Sign the input using $key and $sigHashTypes |
||
| 695 | * |
||
| 696 | 50 | * @param PrivateKeyInterface $privateKey |
|
| 697 | 50 | * @param int $sigHashType |
|
| 698 | 22 | * @return $this |
|
| 699 | */ |
||
| 700 | public function sign(PrivateKeyInterface $privateKey, $sigHashType = SigHash::ALL) |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Verifies the input using $flags for script verification |
||
| 749 | * |
||
| 750 | * @param int $flags |
||
| 751 | 50 | * @return bool |
|
| 752 | */ |
||
| 753 | public function verify($flags = null) |
||
| 787 | 14 | ||
| 788 | 14 | /** |
|
| 789 | 14 | * Produces the script stack that solves the $outputType |
|
| 790 | * |
||
| 791 | * @param string $outputType |
||
| 792 | * @return BufferInterface[] |
||
| 793 | 50 | */ |
|
| 794 | 18 | private function serializeSolution($outputType) |
|
| 818 | |||
| 819 | /** |
||
| 820 | * Produces a SigValues instance containing the scriptSig & script witness |
||
| 821 | * |
||
| 822 | * @return SigValues |
||
| 823 | */ |
||
| 824 | public function serializeSignatures() |
||
| 864 | } |
||
| 865 |
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..