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 SignData |
||
| 90 | */ |
||
| 91 | private $signData; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var int |
||
| 95 | */ |
||
| 96 | private $sigVersion; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var int |
||
| 100 | */ |
||
| 101 | private $flags; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var OutputData $witnessKeyHash |
||
| 105 | */ |
||
| 106 | private $witnessKeyHash; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var TransactionInterface |
||
| 110 | */ |
||
| 111 | private $tx; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var int |
||
| 115 | */ |
||
| 116 | private $nInput; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var TransactionOutputInterface |
||
| 120 | */ |
||
| 121 | private $txOut; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var PublicKeyInterface[] |
||
| 125 | */ |
||
| 126 | private $publicKeys = []; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var TransactionSignatureInterface[] |
||
| 130 | */ |
||
| 131 | private $signatures = []; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var int |
||
| 135 | */ |
||
| 136 | private $requiredSigs = 0; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var Interpreter |
||
| 140 | */ |
||
| 141 | private $interpreter; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var CheckerBase |
||
| 145 | */ |
||
| 146 | private $signatureChecker; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var TransactionSignatureSerializer |
||
| 150 | */ |
||
| 151 | private $txSigSerializer; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var PublicKeySerializerInterface |
||
| 155 | */ |
||
| 156 | private $pubKeySerializer; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * InputSigner constructor. |
||
| 160 | * |
||
| 161 | * Note, the implementation of this class is considered internal |
||
| 162 | * and only the methods exposed on InputSignerInterface should |
||
| 163 | * be depended on to avoid BC breaks. |
||
| 164 | * |
||
| 165 | * The only recommended way to produce this class is using Signer::input() |
||
| 166 | * |
||
| 167 | * @param EcAdapterInterface $ecAdapter |
||
| 168 | * @param TransactionInterface $tx |
||
| 169 | * @param int $nInput |
||
| 170 | * @param TransactionOutputInterface $txOut |
||
| 171 | * @param SignData $signData |
||
| 172 | * @param CheckerBase $checker |
||
| 173 | * @param TransactionSignatureSerializer|null $sigSerializer |
||
| 174 | * @param PublicKeySerializerInterface|null $pubKeySerializer |
||
| 175 | */ |
||
| 176 | 72 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $tx, $nInput, TransactionOutputInterface $txOut, SignData $signData, CheckerBase $checker, TransactionSignatureSerializer $sigSerializer = null, PublicKeySerializerInterface $pubKeySerializer = null) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @return InputSigner |
||
| 195 | */ |
||
| 196 | 72 | public function extract() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param bool $setting |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | 57 | public function tolerateInvalidPublicKey($setting) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param BufferInterface $vchPubKey |
||
| 221 | * @return PublicKeyInterface|null |
||
| 222 | * @throws \Exception |
||
| 223 | */ |
||
| 224 | 55 | protected function parseStepPublicKey(BufferInterface $vchPubKey) |
|
| 225 | { |
||
| 226 | try { |
||
| 227 | 55 | return $this->pubKeySerializer->parse($vchPubKey); |
|
| 228 | } catch (\Exception $e) { |
||
| 229 | if ($this->tolerateInvalidPublicKey) { |
||
| 230 | return null; |
||
| 231 | } |
||
| 232 | |||
| 233 | throw $e; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * A snipped from OP_CHECKMULTISIG - verifies signatures according to the |
||
| 239 | * order of the given public keys (taken from the script). |
||
| 240 | * |
||
| 241 | * @param ScriptInterface $script |
||
| 242 | * @param BufferInterface[] $signatures |
||
| 243 | * @param BufferInterface[] $publicKeys |
||
| 244 | * @param int $sigVersion |
||
| 245 | * @return \SplObjectStorage |
||
| 246 | * @throws \BitWasp\Bitcoin\Exceptions\ScriptRuntimeException |
||
| 247 | */ |
||
| 248 | 10 | private function sortMultisigs(ScriptInterface $script, array $signatures, array $publicKeys, $sigVersion) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @param ScriptInterface $script |
||
| 283 | * @return \BitWasp\Buffertools\BufferInterface[] |
||
| 284 | */ |
||
| 285 | 58 | private function evalPushOnly(ScriptInterface $script) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Create a script consisting only of push-data operations. |
||
| 294 | * Suitable for a scriptSig. |
||
| 295 | * |
||
| 296 | * @param BufferInterface[] $buffers |
||
| 297 | * @return ScriptInterface |
||
| 298 | */ |
||
| 299 | private function pushAll(array $buffers) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Verify a scriptSig / scriptWitness against a scriptPubKey. |
||
| 322 | * Useful for checking the outcome of certain things, like hash locks (p2sh) |
||
| 323 | * |
||
| 324 | * @param int $flags |
||
| 325 | * @param ScriptInterface $scriptSig |
||
| 326 | * @param ScriptInterface $scriptPubKey |
||
| 327 | * @param ScriptWitnessInterface|null $scriptWitness |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | 22 | private function verifySolution($flags, ScriptInterface $scriptSig, ScriptInterface $scriptPubKey, ScriptWitnessInterface $scriptWitness = null) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Evaluates a scriptPubKey against the provided chunks. |
||
| 337 | * |
||
| 338 | * @param ScriptInterface $scriptPubKey |
||
| 339 | * @param array $chunks |
||
| 340 | * @param int $sigVersion |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | 53 | private function evaluateSolution(ScriptInterface $scriptPubKey, array $chunks, $sigVersion) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * This function is strictly for $canSign types. |
||
| 363 | * It will extract signatures/publicKeys when given $outputData, and $stack. |
||
| 364 | * $stack is the result of decompiling a scriptSig, or taking the witness data. |
||
| 365 | * |
||
| 366 | * @param OutputData $outputData |
||
| 367 | * @param array $stack |
||
| 368 | * @param int $sigVersion |
||
| 369 | * @return string |
||
| 370 | * @throws SignerException |
||
| 371 | * @throws \Exception |
||
| 372 | */ |
||
| 373 | 60 | public function extractFromValues(OutputData $outputData, array $stack, $sigVersion) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Checks $chunks (a decompiled scriptSig) for it's last element, |
||
| 439 | * or defers to SignData. If both are provided, it checks the |
||
| 440 | * value from $chunks against SignData. |
||
| 441 | * |
||
| 442 | * @param BufferInterface[] $chunks |
||
| 443 | * @param SignData $signData |
||
| 444 | * @return ScriptInterface |
||
| 445 | */ |
||
| 446 | 24 | private function findRedeemScript(array $chunks, SignData $signData) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Checks $witness (a witness structure) for it's last element, |
||
| 467 | * or defers to SignData. If both are provided, it checks the |
||
| 468 | * value from $chunks against SignData. |
||
| 469 | * |
||
| 470 | * @param BufferInterface[] $witness |
||
| 471 | * @param SignData $signData |
||
| 472 | * @return ScriptInterface |
||
| 473 | */ |
||
| 474 | 20 | private function findWitnessScript(array $witness, SignData $signData) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Needs to be called before using the instance. By `extract`. |
||
| 495 | * |
||
| 496 | * It ensures that violating the following prevents instance creation |
||
| 497 | * - the scriptPubKey can be directly signed, or leads to P2SH/P2WSH/P2WKH |
||
| 498 | * - the P2SH script covers signable types and P2WSH/P2WKH |
||
| 499 | * - the witnessScript covers signable types only |
||
| 500 | * |
||
| 501 | * @param SignData $signData |
||
| 502 | * @param ScriptInterface $scriptPubKey |
||
| 503 | * @param ScriptInterface $scriptSig |
||
| 504 | * @param BufferInterface[] $witness |
||
| 505 | * @return $this |
||
| 506 | * @throws SignerException |
||
| 507 | */ |
||
| 508 | 71 | private function solve(SignData $signData, ScriptInterface $scriptPubKey, ScriptInterface $scriptSig, array $witness) |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Pure function to produce a signature hash for a given $scriptCode, $sigHashType, $sigVersion. |
||
| 568 | * |
||
| 569 | * @param ScriptInterface $scriptCode |
||
| 570 | * @param $sigHashType |
||
| 571 | * @param $sigVersion |
||
| 572 | * @return BufferInterface |
||
| 573 | * @throws SignerException |
||
| 574 | */ |
||
| 575 | 51 | public function calculateSigHashUnsafe(ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * Calculates the signature hash for the input for the given $sigHashType. |
||
| 586 | * |
||
| 587 | * @param int $sigHashType |
||
| 588 | * @return BufferInterface |
||
| 589 | * @throws SignerException |
||
| 590 | */ |
||
| 591 | 1 | public function getSigHash($sigHashType) |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Pure function to produce a signature for a given $key, $scriptCode, $sigHashType, $sigVersion. |
||
| 598 | * |
||
| 599 | * @param PrivateKeyInterface $key |
||
| 600 | * @param ScriptInterface $scriptCode |
||
| 601 | * @param int $sigHashType |
||
| 602 | * @param int $sigVersion |
||
| 603 | * @return TransactionSignatureInterface |
||
| 604 | * @throws SignerException |
||
| 605 | */ |
||
| 606 | 50 | private function calculateSignature(PrivateKeyInterface $key, ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Returns whether all required signatures have been provided. |
||
| 615 | * |
||
| 616 | * @return bool |
||
| 617 | */ |
||
| 618 | 56 | public function isFullySigned() |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Returns the required number of signatures for this input. |
||
| 625 | * |
||
| 626 | * @return int |
||
| 627 | */ |
||
| 628 | 50 | public function getRequiredSigs() |
|
| 632 | |||
| 633 | /** |
||
| 634 | * Returns an array where the values are either null, |
||
| 635 | * or a TransactionSignatureInterface. |
||
| 636 | * |
||
| 637 | * @return TransactionSignatureInterface[] |
||
| 638 | */ |
||
| 639 | 50 | public function getSignatures() |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Returns an array where the values are either null, |
||
| 646 | * or a PublicKeyInterface. |
||
| 647 | * |
||
| 648 | * @return PublicKeyInterface[] |
||
| 649 | */ |
||
| 650 | 50 | public function getPublicKeys() |
|
| 654 | |||
| 655 | /** |
||
| 656 | * OutputData for the script to be signed (will be |
||
| 657 | * equal to getScriptPubKey, or getRedeemScript, or |
||
| 658 | * getWitnessScript. |
||
| 659 | * |
||
| 660 | * @return OutputData |
||
| 661 | */ |
||
| 662 | 50 | public function getSignScript() |
|
| 666 | |||
| 667 | /** |
||
| 668 | * OutputData for the txOut script. |
||
| 669 | * |
||
| 670 | * @return OutputData |
||
| 671 | */ |
||
| 672 | 24 | public function getScriptPubKey() |
|
| 676 | |||
| 677 | /** |
||
| 678 | * Returns OutputData for the P2SH redeemScript. |
||
| 679 | * |
||
| 680 | * @return OutputData |
||
| 681 | */ |
||
| 682 | 18 | public function getRedeemScript() |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Returns OutputData for the P2WSH witnessScript. |
||
| 693 | * |
||
| 694 | * @return OutputData |
||
| 695 | */ |
||
| 696 | 14 | public function getWitnessScript() |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Returns whether the scriptPubKey is P2SH. |
||
| 707 | * |
||
| 708 | * @return bool |
||
| 709 | */ |
||
| 710 | 50 | public function isP2SH() |
|
| 718 | |||
| 719 | /** |
||
| 720 | * Returns whether the scriptPubKey or redeemScript is P2WSH. |
||
| 721 | * |
||
| 722 | * @return bool |
||
| 723 | */ |
||
| 724 | 50 | public function isP2WSH() |
|
| 738 | |||
| 739 | /** |
||
| 740 | * Sign the input using $key and $sigHashTypes |
||
| 741 | * |
||
| 742 | * @param PrivateKeyInterface $privateKey |
||
| 743 | * @param int $sigHashType |
||
| 744 | * @return $this |
||
| 745 | * @throws SignerException |
||
| 746 | */ |
||
| 747 | 56 | public function sign(PrivateKeyInterface $privateKey, $sigHashType = SigHash::ALL) |
|
| 793 | |||
| 794 | /** |
||
| 795 | * Verifies the input using $flags for script verification |
||
| 796 | * |
||
| 797 | * @param int $flags |
||
| 798 | * @return bool |
||
| 799 | */ |
||
| 800 | 50 | public function verify($flags = null) |
|
| 834 | |||
| 835 | /** |
||
| 836 | * Produces the script stack that solves the $outputType |
||
| 837 | * |
||
| 838 | * @param string $outputType |
||
| 839 | * @return BufferInterface[] |
||
| 840 | */ |
||
| 841 | 50 | private function serializeSolution($outputType) |
|
| 865 | |||
| 866 | /** |
||
| 867 | * Produces a SigValues instance containing the scriptSig & script witness |
||
| 868 | * |
||
| 869 | * @return SigValues |
||
| 870 | */ |
||
| 871 | 50 | public function serializeSignatures() |
|
| 911 | } |
||
| 912 |
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..