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) |
|
188 | |||
189 | /** |
||
190 | * @return InputSigner |
||
191 | */ |
||
192 | 94 | public function extract() |
|
210 | |||
211 | /** |
||
212 | * @param bool $setting |
||
213 | * @return $this |
||
214 | */ |
||
215 | 64 | public function tolerateInvalidPublicKey($setting) |
|
220 | |||
221 | /** |
||
222 | * @param BufferInterface $vchPubKey |
||
223 | * @return PublicKeyInterface|null |
||
224 | * @throws \Exception |
||
225 | */ |
||
226 | 62 | protected function parseStepPublicKey(BufferInterface $vchPubKey) |
|
238 | |||
239 | /** |
||
240 | * A snipped from OP_CHECKMULTISIG - verifies signatures according to the |
||
241 | * order of the given public keys (taken from the script). |
||
242 | * |
||
243 | * @param ScriptInterface $script |
||
244 | * @param BufferInterface[] $signatures |
||
245 | * @param BufferInterface[] $publicKeys |
||
246 | * @param int $sigVersion |
||
247 | * @return \SplObjectStorage |
||
248 | */ |
||
249 | 10 | private function sortMultisigs(ScriptInterface $script, array $signatures, array $publicKeys, $sigVersion) |
|
281 | |||
282 | /** |
||
283 | * @param ScriptInterface $script |
||
284 | * @return \BitWasp\Buffertools\BufferInterface[] |
||
285 | */ |
||
286 | 74 | private function evalPushOnly(ScriptInterface $script) |
|
292 | |||
293 | /** |
||
294 | * Create a script consisting only of push-data operations. |
||
295 | * Suitable for a scriptSig. |
||
296 | * |
||
297 | * @param BufferInterface[] $buffers |
||
298 | * @return ScriptInterface |
||
299 | */ |
||
300 | private function pushAll(array $buffers) |
||
320 | |||
321 | /** |
||
322 | * Verify a scriptSig / scriptWitness against a scriptPubKey. |
||
323 | * Useful for checking the outcome of certain things, like hash locks (p2sh) |
||
324 | * |
||
325 | * @param int $flags |
||
326 | * @param ScriptInterface $scriptSig |
||
327 | * @param ScriptInterface $scriptPubKey |
||
328 | * @param ScriptWitnessInterface|null $scriptWitness |
||
329 | * @return bool |
||
330 | */ |
||
331 | 26 | private function verifySolution($flags, ScriptInterface $scriptSig, ScriptInterface $scriptPubKey, ScriptWitnessInterface $scriptWitness = null) |
|
335 | |||
336 | /** |
||
337 | * Evaluates a scriptPubKey against the provided chunks. |
||
338 | * |
||
339 | * @param ScriptInterface $scriptPubKey |
||
340 | * @param array $chunks |
||
341 | * @param int $sigVersion |
||
342 | * @return bool |
||
343 | */ |
||
344 | 56 | private function evaluateSolution(ScriptInterface $scriptPubKey, array $chunks, $sigVersion) |
|
361 | |||
362 | /** |
||
363 | * This function is strictly for $canSign types. |
||
364 | * It will extract signatures/publicKeys when given $outputData, and $stack. |
||
365 | * $stack is the result of decompiling a scriptSig, or taking the witness data. |
||
366 | * |
||
367 | * @param OutputData $outputData |
||
368 | * @param array $stack |
||
369 | * @param int $sigVersion |
||
370 | * @return string |
||
371 | */ |
||
372 | 70 | public function extractFromValues(OutputData $outputData, array $stack, $sigVersion) |
|
435 | |||
436 | /** |
||
437 | * Checks $chunks (a decompiled scriptSig) for it's last element, |
||
438 | * or defers to SignData. If both are provided, it checks the |
||
439 | * value from $chunks against SignData. |
||
440 | * |
||
441 | * @param BufferInterface[] $chunks |
||
442 | * @param SignData $signData |
||
443 | * @return ScriptInterface |
||
444 | */ |
||
445 | 30 | private function findRedeemScript(array $chunks, SignData $signData) |
|
463 | |||
464 | /** |
||
465 | * Checks $witness (a witness structure) for it's last element, |
||
466 | * or defers to SignData. If both are provided, it checks the |
||
467 | * value from $chunks against SignData. |
||
468 | * |
||
469 | * @param BufferInterface[] $witness |
||
470 | * @param SignData $signData |
||
471 | * @return ScriptInterface |
||
472 | */ |
||
473 | 26 | private function findWitnessScript(array $witness, SignData $signData) |
|
491 | |||
492 | /** |
||
493 | * Needs to be called before using the instance. By `extract`. |
||
494 | * |
||
495 | * It ensures that violating the following prevents instance creation |
||
496 | * - the scriptPubKey can be directly signed, or leads to P2SH/P2WSH/P2WKH |
||
497 | * - the P2SH script covers signable types and P2WSH/P2WKH |
||
498 | * - the witnessScript covers signable types only |
||
499 | * |
||
500 | * @param SignData $signData |
||
501 | * @param ScriptInterface $scriptPubKey |
||
502 | * @param ScriptInterface $scriptSig |
||
503 | * @param BufferInterface[] $witness |
||
504 | * @return $this |
||
505 | */ |
||
506 | 92 | private function solve(SignData $signData, ScriptInterface $scriptPubKey, ScriptInterface $scriptSig, array $witness) |
|
563 | |||
564 | /** |
||
565 | * Pure function to produce a signature hash for a given $scriptCode, $sigHashType, $sigVersion. |
||
566 | * |
||
567 | * @param ScriptInterface $scriptCode |
||
568 | * @param int $sigHashType |
||
569 | * @param int $sigVersion |
||
570 | * @return BufferInterface |
||
571 | */ |
||
572 | 52 | public function calculateSigHashUnsafe(ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
580 | |||
581 | /** |
||
582 | * Calculates the signature hash for the input for the given $sigHashType. |
||
583 | * |
||
584 | * @param int $sigHashType |
||
585 | * @return BufferInterface |
||
586 | */ |
||
587 | 2 | public function getSigHash($sigHashType) |
|
591 | |||
592 | /** |
||
593 | * Pure function to produce a signature for a given $key, $scriptCode, $sigHashType, $sigVersion. |
||
594 | * |
||
595 | * @param PrivateKeyInterface $key |
||
596 | * @param ScriptInterface $scriptCode |
||
597 | * @param int $sigHashType |
||
598 | * @param int $sigVersion |
||
599 | * @return TransactionSignatureInterface |
||
600 | */ |
||
601 | 50 | private function calculateSignature(PrivateKeyInterface $key, ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
607 | |||
608 | /** |
||
609 | * Returns whether all required signatures have been provided. |
||
610 | * |
||
611 | * @return bool |
||
612 | */ |
||
613 | 56 | public function isFullySigned() |
|
617 | |||
618 | /** |
||
619 | * Returns the required number of signatures for this input. |
||
620 | * |
||
621 | * @return int |
||
622 | */ |
||
623 | 50 | public function getRequiredSigs() |
|
627 | |||
628 | /** |
||
629 | * Returns an array where the values are either null, |
||
630 | * or a TransactionSignatureInterface. |
||
631 | * |
||
632 | * @return TransactionSignatureInterface[] |
||
633 | */ |
||
634 | 50 | public function getSignatures() |
|
638 | |||
639 | /** |
||
640 | * Returns an array where the values are either null, |
||
641 | * or a PublicKeyInterface. |
||
642 | * |
||
643 | * @return PublicKeyInterface[] |
||
644 | */ |
||
645 | 52 | public function getPublicKeys() |
|
649 | |||
650 | /** |
||
651 | * OutputData for the script to be signed (will be |
||
652 | * equal to getScriptPubKey, or getRedeemScript, or |
||
653 | * getWitnessScript. |
||
654 | * |
||
655 | * @return OutputData |
||
656 | */ |
||
657 | 50 | public function getSignScript() |
|
661 | |||
662 | /** |
||
663 | * OutputData for the txOut script. |
||
664 | * |
||
665 | * @return OutputData |
||
666 | */ |
||
667 | 24 | public function getScriptPubKey() |
|
671 | |||
672 | /** |
||
673 | * Returns OutputData for the P2SH redeemScript. |
||
674 | * |
||
675 | * @return OutputData |
||
676 | */ |
||
677 | 18 | public function getRedeemScript() |
|
685 | |||
686 | /** |
||
687 | * Returns OutputData for the P2WSH witnessScript. |
||
688 | * |
||
689 | * @return OutputData |
||
690 | */ |
||
691 | 14 | public function getWitnessScript() |
|
699 | |||
700 | /** |
||
701 | * Returns whether the scriptPubKey is P2SH. |
||
702 | * |
||
703 | * @return bool |
||
704 | */ |
||
705 | 50 | public function isP2SH() |
|
713 | |||
714 | /** |
||
715 | * Returns whether the scriptPubKey or redeemScript is P2WSH. |
||
716 | * |
||
717 | * @return bool |
||
718 | */ |
||
719 | 50 | public function isP2WSH() |
|
733 | |||
734 | /** |
||
735 | * Sign the input using $key and $sigHashTypes |
||
736 | * |
||
737 | * @param PrivateKeyInterface $privateKey |
||
738 | * @param int $sigHashType |
||
739 | * @return $this |
||
740 | */ |
||
741 | 56 | public function sign(PrivateKeyInterface $privateKey, $sigHashType = SigHash::ALL) |
|
787 | |||
788 | /** |
||
789 | * Verifies the input using $flags for script verification |
||
790 | * |
||
791 | * @param int $flags |
||
792 | * @return bool |
||
793 | */ |
||
794 | 50 | public function verify($flags = null) |
|
828 | |||
829 | /** |
||
830 | * Produces the script stack that solves the $outputType |
||
831 | * |
||
832 | * @param string $outputType |
||
833 | * @return BufferInterface[] |
||
834 | */ |
||
835 | 50 | private function serializeSolution($outputType) |
|
859 | |||
860 | /** |
||
861 | * Produces a SigValues instance containing the scriptSig & script witness |
||
862 | * |
||
863 | * @return SigValues |
||
864 | */ |
||
865 | 50 | public function serializeSignatures() |
|
905 | } |
||
906 |
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..