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 $tolerateInvalidPublicKey = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | private $redeemBitcoinCash = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var SignData |
||
| 95 | */ |
||
| 96 | private $signData; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var int |
||
| 100 | */ |
||
| 101 | private $sigVersion; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var int |
||
| 105 | */ |
||
| 106 | private $flags; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var OutputData $witnessKeyHash |
||
| 110 | */ |
||
| 111 | private $witnessKeyHash; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var TransactionInterface |
||
| 115 | */ |
||
| 116 | private $tx; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var int |
||
| 120 | */ |
||
| 121 | private $nInput; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var TransactionOutputInterface |
||
| 125 | */ |
||
| 126 | private $txOut; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var PublicKeyInterface[] |
||
| 130 | */ |
||
| 131 | private $publicKeys = []; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var TransactionSignatureInterface[] |
||
| 135 | */ |
||
| 136 | private $signatures = []; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var int |
||
| 140 | */ |
||
| 141 | private $requiredSigs = 0; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var Interpreter |
||
| 145 | */ |
||
| 146 | private $interpreter; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var Checker |
||
| 150 | */ |
||
| 151 | private $signatureChecker; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var TransactionSignatureSerializer |
||
| 155 | */ |
||
| 156 | private $txSigSerializer; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var PublicKeySerializerInterface |
||
| 160 | */ |
||
| 161 | private $pubKeySerializer; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * InputSigner constructor. |
||
| 165 | * |
||
| 166 | * Note, the implementation of this class is considered internal |
||
| 167 | * and only the methods exposed on InputSignerInterface should |
||
| 168 | * be depended on to avoid BC breaks. |
||
| 169 | * |
||
| 170 | * The only recommended way to produce this class is using Signer::input() |
||
| 171 | * |
||
| 172 | * @param EcAdapterInterface $ecAdapter |
||
| 173 | * @param TransactionInterface $tx |
||
| 174 | * @param int $nInput |
||
| 175 | * @param TransactionOutputInterface $txOut |
||
| 176 | * @param SignData $signData |
||
| 177 | * @param TransactionSignatureSerializer|null $sigSerializer |
||
| 178 | * @param PublicKeySerializerInterface|null $pubKeySerializer |
||
| 179 | */ |
||
| 180 | 96 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $tx, $nInput, TransactionOutputInterface $txOut, SignData $signData, TransactionSignatureSerializer $sigSerializer = null, PublicKeySerializerInterface $pubKeySerializer = null) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @return InputSigner |
||
| 197 | */ |
||
| 198 | 96 | public function extract() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @param bool $setting |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | 66 | public function redeemBitcoinCash($setting) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @param bool $setting |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | 66 | public function tolerateInvalidPublicKey($setting) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @param BufferInterface $vchPubKey |
||
| 250 | * @return PublicKeyInterface|null |
||
| 251 | * @throws \Exception |
||
| 252 | */ |
||
| 253 | 64 | protected function parseStepPublicKey(BufferInterface $vchPubKey) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * A snipped from OP_CHECKMULTISIG - verifies signatures according to the |
||
| 268 | * order of the given public keys (taken from the script). |
||
| 269 | * |
||
| 270 | * @param ScriptInterface $script |
||
| 271 | * @param BufferInterface[] $signatures |
||
| 272 | * @param BufferInterface[] $publicKeys |
||
| 273 | * @param int $sigVersion |
||
| 274 | * @return \SplObjectStorage |
||
| 275 | */ |
||
| 276 | 10 | private function sortMultisigs(ScriptInterface $script, array $signatures, array $publicKeys, $sigVersion) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @param ScriptInterface $script |
||
| 311 | * @return \BitWasp\Buffertools\BufferInterface[] |
||
| 312 | */ |
||
| 313 | 76 | private function evalPushOnly(ScriptInterface $script) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Create a script consisting only of push-data operations. |
||
| 322 | * Suitable for a scriptSig. |
||
| 323 | * |
||
| 324 | * @param BufferInterface[] $buffers |
||
| 325 | * @return ScriptInterface |
||
| 326 | */ |
||
| 327 | private function pushAll(array $buffers) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Verify a scriptSig / scriptWitness against a scriptPubKey. |
||
| 350 | * Useful for checking the outcome of certain things, like hash locks (p2sh) |
||
| 351 | * |
||
| 352 | * @param int $flags |
||
| 353 | * @param ScriptInterface $scriptSig |
||
| 354 | * @param ScriptInterface $scriptPubKey |
||
| 355 | * @param ScriptWitnessInterface|null $scriptWitness |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | 26 | private function verifySolution($flags, ScriptInterface $scriptSig, ScriptInterface $scriptPubKey, ScriptWitnessInterface $scriptWitness = null) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Evaluates a scriptPubKey against the provided chunks. |
||
| 365 | * |
||
| 366 | * @param ScriptInterface $scriptPubKey |
||
| 367 | * @param array $chunks |
||
| 368 | * @param int $sigVersion |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | 56 | private function evaluateSolution(ScriptInterface $scriptPubKey, array $chunks, $sigVersion) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * This function is strictly for $canSign types. |
||
| 391 | * It will extract signatures/publicKeys when given $outputData, and $stack. |
||
| 392 | * $stack is the result of decompiling a scriptSig, or taking the witness data. |
||
| 393 | * |
||
| 394 | * @param OutputData $outputData |
||
| 395 | * @param array $stack |
||
| 396 | * @param int $sigVersion |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | 72 | public function extractFromValues(OutputData $outputData, array $stack, $sigVersion) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Checks $chunks (a decompiled scriptSig) for it's last element, |
||
| 465 | * or defers to SignData. If both are provided, it checks the |
||
| 466 | * value from $chunks against SignData. |
||
| 467 | * |
||
| 468 | * @param BufferInterface[] $chunks |
||
| 469 | * @param SignData $signData |
||
| 470 | * @return ScriptInterface |
||
| 471 | */ |
||
| 472 | 30 | private function findRedeemScript(array $chunks, SignData $signData) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Checks $witness (a witness structure) for it's last element, |
||
| 493 | * or defers to SignData. If both are provided, it checks the |
||
| 494 | * value from $chunks against SignData. |
||
| 495 | * |
||
| 496 | * @param BufferInterface[] $witness |
||
| 497 | * @param SignData $signData |
||
| 498 | * @return ScriptInterface |
||
| 499 | */ |
||
| 500 | 26 | private function findWitnessScript(array $witness, SignData $signData) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Needs to be called before using the instance. By `extract`. |
||
| 521 | * |
||
| 522 | * It ensures that violating the following prevents instance creation |
||
| 523 | * - the scriptPubKey can be directly signed, or leads to P2SH/P2WSH/P2WKH |
||
| 524 | * - the P2SH script covers signable types and P2WSH/P2WKH |
||
| 525 | * - the witnessScript covers signable types only |
||
| 526 | * |
||
| 527 | * @param SignData $signData |
||
| 528 | * @param ScriptInterface $scriptPubKey |
||
| 529 | * @param ScriptInterface $scriptSig |
||
| 530 | * @param BufferInterface[] $witness |
||
| 531 | * @return $this |
||
| 532 | */ |
||
| 533 | 94 | private function solve(SignData $signData, ScriptInterface $scriptPubKey, ScriptInterface $scriptSig, array $witness) |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Pure function to produce a signature hash for a given $scriptCode, $sigHashType, $sigVersion. |
||
| 593 | * |
||
| 594 | * @param ScriptInterface $scriptCode |
||
| 595 | * @param int $sigHashType |
||
| 596 | * @param int $sigVersion |
||
| 597 | * @return BufferInterface |
||
| 598 | */ |
||
| 599 | 54 | public function calculateSigHashUnsafe(ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Calculates the signature hash for the input for the given $sigHashType. |
||
| 610 | * |
||
| 611 | * @param int $sigHashType |
||
| 612 | * @return BufferInterface |
||
| 613 | */ |
||
| 614 | 2 | public function getSigHash($sigHashType) |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Pure function to produce a signature for a given $key, $scriptCode, $sigHashType, $sigVersion. |
||
| 621 | * |
||
| 622 | * @param PrivateKeyInterface $key |
||
| 623 | * @param ScriptInterface $scriptCode |
||
| 624 | * @param int $sigHashType |
||
| 625 | * @param int $sigVersion |
||
| 626 | * @return TransactionSignatureInterface |
||
| 627 | */ |
||
| 628 | 52 | private function calculateSignature(PrivateKeyInterface $key, ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Returns whether all required signatures have been provided. |
||
| 637 | * |
||
| 638 | * @return bool |
||
| 639 | */ |
||
| 640 | 58 | public function isFullySigned() |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Returns the required number of signatures for this input. |
||
| 647 | * |
||
| 648 | * @return int |
||
| 649 | */ |
||
| 650 | 50 | public function getRequiredSigs() |
|
| 654 | |||
| 655 | /** |
||
| 656 | * Returns an array where the values are either null, |
||
| 657 | * or a TransactionSignatureInterface. |
||
| 658 | * |
||
| 659 | * @return TransactionSignatureInterface[] |
||
| 660 | */ |
||
| 661 | 50 | public function getSignatures() |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Returns an array where the values are either null, |
||
| 668 | * or a PublicKeyInterface. |
||
| 669 | * |
||
| 670 | * @return PublicKeyInterface[] |
||
| 671 | */ |
||
| 672 | 52 | public function getPublicKeys() |
|
| 676 | |||
| 677 | /** |
||
| 678 | * OutputData for the script to be signed (will be |
||
| 679 | * equal to getScriptPubKey, or getRedeemScript, or |
||
| 680 | * getWitnessScript. |
||
| 681 | * |
||
| 682 | * @return OutputData |
||
| 683 | */ |
||
| 684 | 50 | public function getSignScript() |
|
| 688 | |||
| 689 | /** |
||
| 690 | * OutputData for the txOut script. |
||
| 691 | * |
||
| 692 | * @return OutputData |
||
| 693 | */ |
||
| 694 | 24 | public function getScriptPubKey() |
|
| 698 | |||
| 699 | /** |
||
| 700 | * Returns OutputData for the P2SH redeemScript. |
||
| 701 | * |
||
| 702 | * @return OutputData |
||
| 703 | */ |
||
| 704 | 18 | public function getRedeemScript() |
|
| 712 | |||
| 713 | /** |
||
| 714 | * Returns OutputData for the P2WSH witnessScript. |
||
| 715 | * |
||
| 716 | * @return OutputData |
||
| 717 | */ |
||
| 718 | 14 | public function getWitnessScript() |
|
| 726 | |||
| 727 | /** |
||
| 728 | * Returns whether the scriptPubKey is P2SH. |
||
| 729 | * |
||
| 730 | * @return bool |
||
| 731 | */ |
||
| 732 | 50 | public function isP2SH() |
|
| 740 | |||
| 741 | /** |
||
| 742 | * Returns whether the scriptPubKey or redeemScript is P2WSH. |
||
| 743 | * |
||
| 744 | * @return bool |
||
| 745 | */ |
||
| 746 | 50 | public function isP2WSH() |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Sign the input using $key and $sigHashTypes |
||
| 763 | * |
||
| 764 | * @param PrivateKeyInterface $privateKey |
||
| 765 | * @param int $sigHashType |
||
| 766 | * @return $this |
||
| 767 | */ |
||
| 768 | 58 | public function sign(PrivateKeyInterface $privateKey, $sigHashType = SigHash::ALL) |
|
| 814 | |||
| 815 | /** |
||
| 816 | * Verifies the input using $flags for script verification |
||
| 817 | * |
||
| 818 | * @param int $flags |
||
| 819 | * @return bool |
||
| 820 | */ |
||
| 821 | 50 | public function verify($flags = null) |
|
| 855 | |||
| 856 | /** |
||
| 857 | * Produces the script stack that solves the $outputType |
||
| 858 | * |
||
| 859 | * @param string $outputType |
||
| 860 | * @return BufferInterface[] |
||
| 861 | */ |
||
| 862 | 52 | private function serializeSolution($outputType) |
|
| 886 | |||
| 887 | /** |
||
| 888 | * Produces a SigValues instance containing the scriptSig & script witness |
||
| 889 | * |
||
| 890 | * @return SigValues |
||
| 891 | */ |
||
| 892 | 52 | public function serializeSignatures() |
|
| 932 | } |
||
| 933 |
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..