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 |
||
| 36 | class InputSigner implements InputSignerInterface |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected static $canSign = [ |
||
| 42 | ScriptType::P2PKH, |
||
| 43 | ScriptType::P2PK, |
||
| 44 | ScriptType::MULTISIG |
||
| 45 | ]; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected static $validP2sh = [ |
||
| 51 | ScriptType::P2WKH, |
||
| 52 | ScriptType::P2WSH, |
||
| 53 | ScriptType::P2PKH, |
||
| 54 | ScriptType::P2PK, |
||
| 55 | ScriptType::MULTISIG |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var EcAdapterInterface |
||
| 60 | */ |
||
| 61 | private $ecAdapter; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var OutputData $scriptPubKey |
||
| 65 | */ |
||
| 66 | private $scriptPubKey; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var OutputData $redeemScript |
||
| 70 | */ |
||
| 71 | private $redeemScript; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var OutputData $witnessScript |
||
| 75 | */ |
||
| 76 | private $witnessScript; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var OutputData |
||
| 80 | */ |
||
| 81 | private $signScript; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | private $padUnsignedMultisigs = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | private $tolerateInvalidPublicKey = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | private $redeemBitcoinCash = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var SignData |
||
| 100 | */ |
||
| 101 | private $signData; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var int |
||
| 105 | */ |
||
| 106 | private $sigVersion; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var int |
||
| 110 | */ |
||
| 111 | private $flags; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var OutputData $witnessKeyHash |
||
| 115 | */ |
||
| 116 | private $witnessKeyHash; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var TransactionInterface |
||
| 120 | */ |
||
| 121 | private $tx; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var int |
||
| 125 | */ |
||
| 126 | private $nInput; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var TransactionOutputInterface |
||
| 130 | */ |
||
| 131 | private $txOut; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var PublicKeyInterface[] |
||
| 135 | */ |
||
| 136 | private $publicKeys = []; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var TransactionSignatureInterface[] |
||
| 140 | */ |
||
| 141 | private $signatures = []; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var int |
||
| 145 | */ |
||
| 146 | private $requiredSigs = 0; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var Interpreter |
||
| 150 | */ |
||
| 151 | private $interpreter; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var Checker |
||
| 155 | */ |
||
| 156 | private $signatureChecker; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var TransactionSignatureSerializer |
||
| 160 | */ |
||
| 161 | private $txSigSerializer; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var PublicKeySerializerInterface |
||
| 165 | */ |
||
| 166 | private $pubKeySerializer; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * InputSigner constructor. |
||
| 170 | * |
||
| 171 | * Note, the implementation of this class is considered internal |
||
| 172 | * and only the methods exposed on InputSignerInterface should |
||
| 173 | * be depended on to avoid BC breaks. |
||
| 174 | * |
||
| 175 | * The only recommended way to produce this class is using Signer::input() |
||
| 176 | * |
||
| 177 | * @param EcAdapterInterface $ecAdapter |
||
| 178 | * @param TransactionInterface $tx |
||
| 179 | * @param int $nInput |
||
| 180 | 96 | * @param TransactionOutputInterface $txOut |
|
| 181 | * @param SignData $signData |
||
| 182 | 96 | * @param TransactionSignatureSerializer|null $sigSerializer |
|
| 183 | 96 | * @param PublicKeySerializerInterface|null $pubKeySerializer |
|
| 184 | 96 | */ |
|
| 185 | 96 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $tx, $nInput, TransactionOutputInterface $txOut, SignData $signData, TransactionSignatureSerializer $sigSerializer = null, PublicKeySerializerInterface $pubKeySerializer = null) |
|
| 199 | |||
| 200 | 96 | /** |
|
| 201 | 96 | * @return InputSigner |
|
| 202 | */ |
||
| 203 | 96 | public function extract() |
|
| 234 | 66 | ||
| 235 | /** |
||
| 236 | 66 | * @param bool $setting |
|
| 237 | 66 | * @return $this |
|
| 238 | */ |
||
| 239 | public function padUnsignedMultisigs($setting) |
||
| 244 | 66 | ||
| 245 | /** |
||
| 246 | 66 | * @param bool $setting |
|
| 247 | 66 | * @return $this |
|
| 248 | */ |
||
| 249 | public function tolerateInvalidPublicKey($setting) |
||
| 254 | |||
| 255 | 64 | /** |
|
| 256 | * @param bool $setting |
||
| 257 | * @return $this |
||
| 258 | 64 | */ |
|
| 259 | 6 | public function redeemBitcoinCash($setting) |
|
| 264 | 4 | ||
| 265 | /** |
||
| 266 | * @param BufferInterface $vchPubKey |
||
| 267 | * @return PublicKeyInterface|null |
||
| 268 | * @throws \Exception |
||
| 269 | */ |
||
| 270 | protected function parseStepPublicKey(BufferInterface $vchPubKey) |
||
| 282 | 10 | ||
| 283 | 10 | /** |
|
| 284 | 10 | * A snipped from OP_CHECKMULTISIG - verifies signatures according to the |
|
| 285 | * order of the given public keys (taken from the script). |
||
| 286 | 10 | * |
|
| 287 | * @param ScriptInterface $script |
||
| 288 | 10 | * @param BufferInterface[] $signatures |
|
| 289 | 10 | * @param BufferInterface[] $publicKeys |
|
| 290 | * @param int $sigVersion |
||
| 291 | 10 | * @return \SplObjectStorage |
|
| 292 | 10 | */ |
|
| 293 | 10 | private function sortMultisigs(ScriptInterface $script, array $signatures, array $publicKeys, $sigVersion) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param ScriptInterface $script |
||
| 328 | * @return \BitWasp\Buffertools\BufferInterface[] |
||
| 329 | */ |
||
| 330 | private function evalPushOnly(ScriptInterface $script) |
||
| 336 | 44 | ||
| 337 | 44 | /** |
|
| 338 | 4 | * Create a script consisting only of push-data operations. |
|
| 339 | * Suitable for a scriptSig. |
||
| 340 | * |
||
| 341 | 44 | * @param BufferInterface[] $buffers |
|
| 342 | 44 | * @return ScriptInterface |
|
| 343 | */ |
||
| 344 | private function pushAll(array $buffers) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Verify a scriptSig / scriptWitness against a scriptPubKey. |
||
| 367 | * Useful for checking the outcome of certain things, like hash locks (p2sh) |
||
| 368 | * |
||
| 369 | * @param int $flags |
||
| 370 | * @param ScriptInterface $scriptSig |
||
| 371 | * @param ScriptInterface $scriptPubKey |
||
| 372 | * @param ScriptWitnessInterface|null $scriptWitness |
||
| 373 | 56 | * @return bool |
|
| 374 | */ |
||
| 375 | 56 | private function verifySolution($flags, ScriptInterface $scriptSig, ScriptInterface $scriptPubKey, ScriptWitnessInterface $scriptWitness = null) |
|
| 379 | |||
| 380 | 54 | /** |
|
| 381 | * Evaluates a scriptPubKey against the provided chunks. |
||
| 382 | * |
||
| 383 | * @param ScriptInterface $scriptPubKey |
||
| 384 | 54 | * @param array $chunks |
|
| 385 | 4 | * @param int $sigVersion |
|
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | 50 | private function evaluateSolution(ScriptInterface $scriptPubKey, array $chunks, $sigVersion) |
|
| 405 | |||
| 406 | 72 | /** |
|
| 407 | 38 | * This function is strictly for $canSign types. |
|
| 408 | 38 | * It will extract signatures/publicKeys when given $outputData, and $stack. |
|
| 409 | 34 | * $stack is the result of decompiling a scriptSig, or taking the witness data. |
|
| 410 | 2 | * |
|
| 411 | * @param OutputData $outputData |
||
| 412 | 32 | * @param array $stack |
|
| 413 | 36 | * @param int $sigVersion |
|
| 414 | * @return string |
||
| 415 | 36 | */ |
|
| 416 | 16 | public function extractFromValues(OutputData $outputData, array $stack, $sigVersion) |
|
| 511 | |||
| 512 | 22 | /** |
|
| 513 | 4 | * Checks $chunks (a decompiled scriptSig) for it's last element, |
|
| 514 | * or defers to SignData. If both are provided, it checks the |
||
| 515 | 18 | * value from $chunks against SignData. |
|
| 516 | * |
||
| 517 | * @param BufferInterface[] $chunks |
||
| 518 | 18 | * @param SignData $signData |
|
| 519 | * @return ScriptInterface |
||
| 520 | */ |
||
| 521 | private function findRedeemScript(array $chunks, SignData $signData) |
||
| 539 | 94 | ||
| 540 | 94 | /** |
|
| 541 | 94 | * Checks $witness (a witness structure) for it's last element, |
|
| 542 | 2 | * or defers to SignData. If both are provided, it checks the |
|
| 543 | * value from $chunks against SignData. |
||
| 544 | * |
||
| 545 | 92 | * @param BufferInterface[] $witness |
|
| 546 | 46 | * @param SignData $signData |
|
| 547 | * @return ScriptInterface |
||
| 548 | */ |
||
| 549 | 92 | private function findWitnessScript(array $witness, SignData $signData) |
|
| 567 | 8 | ||
| 568 | 78 | /** |
|
| 569 | 26 | * Needs to be called before using the instance. By `extract`. |
|
| 570 | 26 | * |
|
| 571 | * It ensures that violating the following prevents instance creation |
||
| 572 | * - the scriptPubKey can be directly signed, or leads to P2SH/P2WSH/P2WKH |
||
| 573 | 18 | * - the P2SH script covers signable types and P2WSH/P2WKH |
|
| 574 | 2 | * - the witnessScript covers signable types only |
|
| 575 | * |
||
| 576 | * @param SignData $signData |
||
| 577 | 16 | * @param ScriptInterface $scriptPubKey |
|
| 578 | 16 | * @param ScriptInterface $scriptSig |
|
| 579 | 2 | * @param BufferInterface[] $witness |
|
| 580 | * @return $this |
||
| 581 | */ |
||
| 582 | 14 | private function solve(SignData $signData, ScriptInterface $scriptPubKey, ScriptInterface $scriptSig, array $witness) |
|
| 639 | |||
| 640 | /** |
||
| 641 | * Pure function to produce a signature hash for a given $scriptCode, $sigHashType, $sigVersion. |
||
| 642 | 58 | * |
|
| 643 | * @param ScriptInterface $scriptCode |
||
| 644 | 58 | * @param int $sigHashType |
|
| 645 | * @param int $sigVersion |
||
| 646 | * @return BufferInterface |
||
| 647 | */ |
||
| 648 | public function calculateSigHashUnsafe(ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Calculates the signature hash for the input for the given $sigHashType. |
||
| 659 | * |
||
| 660 | * @param int $sigHashType |
||
| 661 | * @return BufferInterface |
||
| 662 | */ |
||
| 663 | 50 | public function getSigHash($sigHashType) |
|
| 667 | |||
| 668 | /** |
||
| 669 | * Pure function to produce a signature for a given $key, $scriptCode, $sigHashType, $sigVersion. |
||
| 670 | * |
||
| 671 | * @param PrivateKeyInterface $key |
||
| 672 | * @param ScriptInterface $scriptCode |
||
| 673 | * @param int $sigHashType |
||
| 674 | 52 | * @param int $sigVersion |
|
| 675 | * @return TransactionSignatureInterface |
||
| 676 | 52 | */ |
|
| 677 | private function calculateSignature(PrivateKeyInterface $key, ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Returns whether all required signatures have been provided. |
||
| 686 | 50 | * |
|
| 687 | * @return bool |
||
| 688 | 50 | */ |
|
| 689 | public function isFullySigned() |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Returns the required number of signatures for this input. |
||
| 696 | 24 | * |
|
| 697 | * @return int |
||
| 698 | 24 | */ |
|
| 699 | public function getRequiredSigs() |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Returns an array where the values are either null, |
||
| 706 | 18 | * or a TransactionSignatureInterface. |
|
| 707 | * |
||
| 708 | 18 | * @return TransactionSignatureInterface[] |
|
| 709 | */ |
||
| 710 | public function getSignatures() |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Returns an array where the values are either null, |
||
| 717 | * or a PublicKeyInterface. |
||
| 718 | * |
||
| 719 | * @return PublicKeyInterface[] |
||
| 720 | 14 | */ |
|
| 721 | public function getPublicKeys() |
||
| 725 | |||
| 726 | 14 | /** |
|
| 727 | * OutputData for the script to be signed (will be |
||
| 728 | * equal to getScriptPubKey, or getRedeemScript, or |
||
| 729 | * getWitnessScript. |
||
| 730 | * |
||
| 731 | * @return OutputData |
||
| 732 | */ |
||
| 733 | public function getSignScript() |
||
| 737 | 18 | ||
| 738 | /** |
||
| 739 | * OutputData for the txOut script. |
||
| 740 | 32 | * |
|
| 741 | * @return OutputData |
||
| 742 | */ |
||
| 743 | public function getScriptPubKey() |
||
| 747 | |||
| 748 | 50 | /** |
|
| 749 | * Returns OutputData for the P2SH redeemScript. |
||
| 750 | 50 | * |
|
| 751 | 18 | * @return OutputData |
|
| 752 | 8 | */ |
|
| 753 | public function getRedeemScript() |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Returns OutputData for the P2WSH witnessScript. |
||
| 764 | * |
||
| 765 | * @return OutputData |
||
| 766 | */ |
||
| 767 | public function getWitnessScript() |
||
| 775 | |||
| 776 | 58 | /** |
|
| 777 | * Returns whether the scriptPubKey is P2SH. |
||
| 778 | * |
||
| 779 | * @return bool |
||
| 780 | 58 | */ |
|
| 781 | 14 | public function isP2SH() |
|
| 789 | |||
| 790 | /** |
||
| 791 | 32 | * Returns whether the scriptPubKey or redeemScript is P2WSH. |
|
| 792 | 32 | * |
|
| 793 | * @return bool |
||
| 794 | */ |
||
| 795 | 32 | public function isP2WSH() |
|
| 809 | |||
| 810 | /** |
||
| 811 | * Sign the input using $key and $sigHashTypes |
||
| 812 | * |
||
| 813 | * @param PrivateKeyInterface $privateKey |
||
| 814 | 52 | * @param int $sigHashType |
|
| 815 | * @return $this |
||
| 816 | */ |
||
| 817 | public function sign(PrivateKeyInterface $privateKey, $sigHashType = SigHash::ALL) |
||
| 863 | |||
| 864 | 52 | /** |
|
| 865 | * Verifies the input using $flags for script verification |
||
| 866 | 52 | * |
|
| 867 | 52 | * @param int $flags |
|
| 868 | 12 | * @return bool |
|
| 869 | 12 | */ |
|
| 870 | public function verify($flags = null) |
||
| 904 | 52 | ||
| 905 | 52 | /** |
|
| 906 | 26 | * Produces the script stack that solves the $outputType |
|
| 907 | * |
||
| 908 | * @param string $outputType |
||
| 909 | 52 | * @return BufferInterface[] |
|
| 910 | 52 | */ |
|
| 911 | 52 | private function serializeSolution($outputType) |
|
| 939 | |||
| 940 | /** |
||
| 941 | * Produces a SigValues instance containing the scriptSig & script witness |
||
| 942 | * |
||
| 943 | * @return SigValues |
||
| 944 | */ |
||
| 945 | public function serializeSignatures() |
||
| 985 | } |
||
| 986 |
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..