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 | /** |
||
| 153 | * @var PublicKeySerializerInterface |
||
| 154 | */ |
||
| 155 | private $pubKeySerializer; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * InputSigner constructor. |
||
| 159 | * |
||
| 160 | * Note, the implementation of this class is considered internal |
||
| 161 | * and only the methods exposed on InputSignerInterface should |
||
| 162 | * be depended on to avoid BC breaks. |
||
| 163 | * |
||
| 164 | * The only recommended way to produce this class is using Signer::input() |
||
| 165 | * |
||
| 166 | * @param EcAdapterInterface $ecAdapter |
||
| 167 | * @param TransactionInterface $tx |
||
| 168 | * @param int $nInput |
||
| 169 | * @param TransactionOutputInterface $txOut |
||
| 170 | * @param SignData $signData |
||
| 171 | * @param TransactionSignatureSerializer|null $sigSerializer |
||
| 172 | * @param PublicKeySerializerInterface|null $pubKeySerializer |
||
| 173 | */ |
||
| 174 | 94 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $tx, $nInput, TransactionOutputInterface $txOut, SignData $signData, TransactionSignatureSerializer $sigSerializer = null, PublicKeySerializerInterface $pubKeySerializer = null) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @return InputSigner |
||
| 193 | */ |
||
| 194 | 94 | public function extract() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param bool $setting |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | 64 | public function tolerateInvalidPublicKey($setting) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @param BufferInterface $vchPubKey |
||
| 219 | * @return PublicKeyInterface|null |
||
| 220 | * @throws \Exception |
||
| 221 | */ |
||
| 222 | 62 | protected function parseStepPublicKey(BufferInterface $vchPubKey) |
|
| 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 | * |
||
| 239 | * @param ScriptInterface $script |
||
| 240 | * @param BufferInterface[] $signatures |
||
| 241 | * @param BufferInterface[] $publicKeys |
||
| 242 | * @param int $sigVersion |
||
| 243 | * @return \SplObjectStorage |
||
| 244 | */ |
||
| 245 | 10 | private function sortMultisigs(ScriptInterface $script, array $signatures, array $publicKeys, $sigVersion) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param ScriptInterface $script |
||
| 280 | * @return \BitWasp\Buffertools\BufferInterface[] |
||
| 281 | */ |
||
| 282 | 74 | private function evalPushOnly(ScriptInterface $script) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Create a script consisting only of push-data operations. |
||
| 291 | * Suitable for a scriptSig. |
||
| 292 | * |
||
| 293 | * @param BufferInterface[] $buffers |
||
| 294 | * @return ScriptInterface |
||
| 295 | */ |
||
| 296 | private function pushAll(array $buffers) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Verify a scriptSig / scriptWitness against a scriptPubKey. |
||
| 319 | * Useful for checking the outcome of certain things, like hash locks (p2sh) |
||
| 320 | * |
||
| 321 | * @param int $flags |
||
| 322 | * @param ScriptInterface $scriptSig |
||
| 323 | * @param ScriptInterface $scriptPubKey |
||
| 324 | * @param ScriptWitnessInterface|null $scriptWitness |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | 26 | private function verifySolution($flags, ScriptInterface $scriptSig, ScriptInterface $scriptPubKey, ScriptWitnessInterface $scriptWitness = null) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Evaluates a scriptPubKey against the provided chunks. |
||
| 334 | * |
||
| 335 | * @param ScriptInterface $scriptPubKey |
||
| 336 | * @param array $chunks |
||
| 337 | * @param int $sigVersion |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | 56 | 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 | * @param array $stack |
||
| 365 | * @param int $sigVersion |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | 70 | public function extractFromValues(OutputData $outputData, array $stack, $sigVersion) |
|
| 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 | * @param BufferInterface[] $chunks |
||
| 438 | * @param SignData $signData |
||
| 439 | * @return ScriptInterface |
||
| 440 | */ |
||
| 441 | 30 | private function findRedeemScript(array $chunks, SignData $signData) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Checks $witness (a witness structure) for it's last element, |
||
| 462 | * or defers to SignData. If both are provided, it checks the |
||
| 463 | * value from $chunks against SignData. |
||
| 464 | * |
||
| 465 | * @param BufferInterface[] $witness |
||
| 466 | * @param SignData $signData |
||
| 467 | * @return ScriptInterface |
||
| 468 | */ |
||
| 469 | 26 | private function findWitnessScript(array $witness, SignData $signData) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Needs to be called before using the instance. By `extract`. |
||
| 490 | * |
||
| 491 | * It ensures that violating the following prevents instance creation |
||
| 492 | * - 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 | */ |
||
| 502 | 92 | private function solve(SignData $signData, ScriptInterface $scriptPubKey, ScriptInterface $scriptSig, array $witness) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Pure function to produce a signature hash for a given $scriptCode, $sigHashType, $sigVersion. |
||
| 562 | * |
||
| 563 | * @param ScriptInterface $scriptCode |
||
| 564 | * @param int $sigHashType |
||
| 565 | * @param int $sigVersion |
||
| 566 | * @return BufferInterface |
||
| 567 | */ |
||
| 568 | 52 | public function calculateSigHashUnsafe(ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Calculates the signature hash for the input for the given $sigHashType. |
||
| 579 | * |
||
| 580 | * @param int $sigHashType |
||
| 581 | * @return BufferInterface |
||
| 582 | */ |
||
| 583 | 2 | public function getSigHash($sigHashType) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Pure function to produce a signature for a given $key, $scriptCode, $sigHashType, $sigVersion. |
||
| 590 | * |
||
| 591 | * @param PrivateKeyInterface $key |
||
| 592 | * @param ScriptInterface $scriptCode |
||
| 593 | * @param int $sigHashType |
||
| 594 | * @param int $sigVersion |
||
| 595 | * @return TransactionSignatureInterface |
||
| 596 | */ |
||
| 597 | 50 | private function calculateSignature(PrivateKeyInterface $key, ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Returns whether all required signatures have been provided. |
||
| 606 | * |
||
| 607 | * @return bool |
||
| 608 | */ |
||
| 609 | 56 | public function isFullySigned() |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Returns the required number of signatures for this input. |
||
| 616 | * |
||
| 617 | * @return int |
||
| 618 | */ |
||
| 619 | 50 | public function getRequiredSigs() |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Returns an array where the values are either null, |
||
| 626 | * or a TransactionSignatureInterface. |
||
| 627 | * |
||
| 628 | * @return TransactionSignatureInterface[] |
||
| 629 | */ |
||
| 630 | 50 | public function getSignatures() |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Returns an array where the values are either null, |
||
| 637 | * or a PublicKeyInterface. |
||
| 638 | * |
||
| 639 | * @return PublicKeyInterface[] |
||
| 640 | */ |
||
| 641 | 52 | public function getPublicKeys() |
|
| 645 | |||
| 646 | /** |
||
| 647 | * OutputData for the script to be signed (will be |
||
| 648 | * equal to getScriptPubKey, or getRedeemScript, or |
||
| 649 | * getWitnessScript. |
||
| 650 | * |
||
| 651 | * @return OutputData |
||
| 652 | */ |
||
| 653 | 50 | public function getSignScript() |
|
| 657 | |||
| 658 | /** |
||
| 659 | * OutputData for the txOut script. |
||
| 660 | * |
||
| 661 | * @return OutputData |
||
| 662 | */ |
||
| 663 | 24 | public function getScriptPubKey() |
|
| 667 | |||
| 668 | /** |
||
| 669 | * Returns OutputData for the P2SH redeemScript. |
||
| 670 | * |
||
| 671 | * @return OutputData |
||
| 672 | */ |
||
| 673 | 18 | public function getRedeemScript() |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Returns OutputData for the P2WSH witnessScript. |
||
| 684 | * |
||
| 685 | * @return OutputData |
||
| 686 | */ |
||
| 687 | 14 | public function getWitnessScript() |
|
| 695 | |||
| 696 | /** |
||
| 697 | * Returns whether the scriptPubKey is P2SH. |
||
| 698 | * |
||
| 699 | * @return bool |
||
| 700 | */ |
||
| 701 | 50 | public function isP2SH() |
|
| 709 | |||
| 710 | /** |
||
| 711 | * Returns whether the scriptPubKey or redeemScript is P2WSH. |
||
| 712 | * |
||
| 713 | * @return bool |
||
| 714 | */ |
||
| 715 | 50 | public function isP2WSH() |
|
| 729 | |||
| 730 | /** |
||
| 731 | * Sign the input using $key and $sigHashTypes |
||
| 732 | * |
||
| 733 | * @param PrivateKeyInterface $privateKey |
||
| 734 | * @param int $sigHashType |
||
| 735 | * @return $this |
||
| 736 | */ |
||
| 737 | 56 | public function sign(PrivateKeyInterface $privateKey, $sigHashType = SigHash::ALL) |
|
| 783 | |||
| 784 | /** |
||
| 785 | * Verifies the input using $flags for script verification |
||
| 786 | * |
||
| 787 | * @param int $flags |
||
| 788 | * @return bool |
||
| 789 | */ |
||
| 790 | 50 | 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 | 50 | private function serializeSolution($outputType) |
|
| 855 | |||
| 856 | /** |
||
| 857 | * Produces a SigValues instance containing the scriptSig & script witness |
||
| 858 | * |
||
| 859 | * @return SigValues |
||
| 860 | */ |
||
| 861 | 50 | 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..