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() |
|
199 | { |
||
200 | 96 | $defaultFlags = Interpreter::VERIFY_DERSIG | Interpreter::VERIFY_P2SH | Interpreter::VERIFY_CHECKLOCKTIMEVERIFY | Interpreter::VERIFY_CHECKSEQUENCEVERIFY | Interpreter::VERIFY_WITNESS; |
|
201 | 96 | $checker = new Checker($this->ecAdapter, $this->tx, $this->nInput, $this->txOut->getValue(), $this->txSigSerializer, $this->pubKeySerializer); |
|
202 | |||
203 | 96 | if ($this->redeemBitcoinCash) { |
|
204 | // unset VERIFY_WITNESS default |
||
205 | 2 | $defaultFlags = $defaultFlags & (~Interpreter::VERIFY_WITNESS); |
|
206 | |||
207 | 2 | if ($this->signData->hasSignaturePolicy()) { |
|
208 | if ($this->signData->getSignaturePolicy() & Interpreter::VERIFY_WITNESS) { |
||
209 | throw new \RuntimeException("VERIFY_WITNESS is not possible for bitcoin cash"); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | 2 | $checker = new BitcoinCashChecker($this->ecAdapter, $this->tx, $this->nInput, $this->txOut->getValue(), $this->txSigSerializer, $this->pubKeySerializer); |
|
214 | } |
||
215 | |||
216 | 96 | $this->flags = $this->signData->hasSignaturePolicy() ? $this->signData->getSignaturePolicy() : $defaultFlags; |
|
217 | 96 | $this->signatureChecker = $checker; |
|
218 | |||
219 | 96 | $witnesses = $this->tx->getWitnesses(); |
|
220 | 96 | $witness = array_key_exists($this->nInput, $witnesses) ? $witnesses[$this->nInput]->all() : []; |
|
221 | |||
222 | 96 | return $this->solve( |
|
223 | 96 | $this->signData, |
|
224 | 96 | $this->txOut->getScript(), |
|
225 | 96 | $this->tx->getInput($this->nInput)->getScript(), |
|
226 | 94 | $witness |
|
227 | ); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @param bool $setting |
||
232 | * @return $this |
||
233 | */ |
||
234 | 66 | public function tolerateInvalidPublicKey($setting) |
|
239 | |||
240 | /** |
||
241 | * @param bool $setting |
||
242 | * @return $this |
||
243 | */ |
||
244 | 66 | public function redeemBitcoinCash($setting) |
|
249 | |||
250 | /** |
||
251 | * @param BufferInterface $vchPubKey |
||
252 | * @return PublicKeyInterface|null |
||
253 | * @throws \Exception |
||
254 | */ |
||
255 | 64 | protected function parseStepPublicKey(BufferInterface $vchPubKey) |
|
267 | |||
268 | /** |
||
269 | * A snipped from OP_CHECKMULTISIG - verifies signatures according to the |
||
270 | * order of the given public keys (taken from the script). |
||
271 | * |
||
272 | * @param ScriptInterface $script |
||
273 | * @param BufferInterface[] $signatures |
||
274 | * @param BufferInterface[] $publicKeys |
||
275 | * @param int $sigVersion |
||
276 | * @return \SplObjectStorage |
||
277 | */ |
||
278 | 10 | private function sortMultisigs(ScriptInterface $script, array $signatures, array $publicKeys, $sigVersion) |
|
310 | |||
311 | /** |
||
312 | * @param ScriptInterface $script |
||
313 | * @return \BitWasp\Buffertools\BufferInterface[] |
||
314 | */ |
||
315 | 76 | private function evalPushOnly(ScriptInterface $script) |
|
321 | |||
322 | /** |
||
323 | * Create a script consisting only of push-data operations. |
||
324 | * Suitable for a scriptSig. |
||
325 | * |
||
326 | * @param BufferInterface[] $buffers |
||
327 | * @return ScriptInterface |
||
328 | */ |
||
329 | private function pushAll(array $buffers) |
||
330 | { |
||
331 | 52 | return ScriptFactory::sequence(array_map(function ($buffer) { |
|
332 | 44 | if (!($buffer instanceof BufferInterface)) { |
|
333 | throw new \RuntimeException('Script contained a non-push opcode'); |
||
334 | } |
||
335 | |||
336 | 44 | $size = $buffer->getSize(); |
|
337 | 44 | if ($size === 0) { |
|
338 | 4 | return Opcodes::OP_0; |
|
339 | } |
||
340 | |||
341 | 44 | $first = ord($buffer->getBinary()); |
|
342 | 44 | if ($size === 1 && $first >= 1 && $first <= 16) { |
|
343 | return \BitWasp\Bitcoin\Script\encodeOpN($first); |
||
344 | } else { |
||
345 | 44 | return $buffer; |
|
346 | } |
||
347 | 52 | }, $buffers)); |
|
348 | } |
||
349 | |||
350 | /** |
||
351 | * Verify a scriptSig / scriptWitness against a scriptPubKey. |
||
352 | * Useful for checking the outcome of certain things, like hash locks (p2sh) |
||
353 | * |
||
354 | * @param int $flags |
||
355 | * @param ScriptInterface $scriptSig |
||
356 | * @param ScriptInterface $scriptPubKey |
||
357 | * @param ScriptWitnessInterface|null $scriptWitness |
||
358 | * @return bool |
||
359 | */ |
||
360 | 26 | private function verifySolution($flags, ScriptInterface $scriptSig, ScriptInterface $scriptPubKey, ScriptWitnessInterface $scriptWitness = null) |
|
364 | |||
365 | /** |
||
366 | * Evaluates a scriptPubKey against the provided chunks. |
||
367 | * |
||
368 | * @param ScriptInterface $scriptPubKey |
||
369 | * @param array $chunks |
||
370 | * @param int $sigVersion |
||
371 | * @return bool |
||
372 | */ |
||
373 | 56 | private function evaluateSolution(ScriptInterface $scriptPubKey, array $chunks, $sigVersion) |
|
390 | |||
391 | /** |
||
392 | * This function is strictly for $canSign types. |
||
393 | * It will extract signatures/publicKeys when given $outputData, and $stack. |
||
394 | * $stack is the result of decompiling a scriptSig, or taking the witness data. |
||
395 | * |
||
396 | * @param OutputData $outputData |
||
397 | * @param array $stack |
||
398 | * @param int $sigVersion |
||
399 | * @return string |
||
400 | */ |
||
401 | 72 | public function extractFromValues(OutputData $outputData, array $stack, $sigVersion) |
|
464 | |||
465 | /** |
||
466 | * Checks $chunks (a decompiled scriptSig) 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[] $chunks |
||
471 | * @param SignData $signData |
||
472 | * @return ScriptInterface |
||
473 | */ |
||
474 | 30 | private function findRedeemScript(array $chunks, SignData $signData) |
|
492 | |||
493 | /** |
||
494 | * Checks $witness (a witness structure) for it's last element, |
||
495 | * or defers to SignData. If both are provided, it checks the |
||
496 | * value from $chunks against SignData. |
||
497 | * |
||
498 | * @param BufferInterface[] $witness |
||
499 | * @param SignData $signData |
||
500 | * @return ScriptInterface |
||
501 | */ |
||
502 | 26 | private function findWitnessScript(array $witness, SignData $signData) |
|
520 | |||
521 | /** |
||
522 | * Needs to be called before using the instance. By `extract`. |
||
523 | * |
||
524 | * It ensures that violating the following prevents instance creation |
||
525 | * - the scriptPubKey can be directly signed, or leads to P2SH/P2WSH/P2WKH |
||
526 | * - the P2SH script covers signable types and P2WSH/P2WKH |
||
527 | * - the witnessScript covers signable types only |
||
528 | * |
||
529 | * @param SignData $signData |
||
530 | * @param ScriptInterface $scriptPubKey |
||
531 | * @param ScriptInterface $scriptSig |
||
532 | * @param BufferInterface[] $witness |
||
533 | * @return $this |
||
534 | */ |
||
535 | 94 | private function solve(SignData $signData, ScriptInterface $scriptPubKey, ScriptInterface $scriptSig, array $witness) |
|
592 | |||
593 | /** |
||
594 | * Pure function to produce a signature hash for a given $scriptCode, $sigHashType, $sigVersion. |
||
595 | * |
||
596 | * @param ScriptInterface $scriptCode |
||
597 | * @param int $sigHashType |
||
598 | * @param int $sigVersion |
||
599 | * @return BufferInterface |
||
600 | */ |
||
601 | 54 | public function calculateSigHashUnsafe(ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
609 | |||
610 | /** |
||
611 | * Calculates the signature hash for the input for the given $sigHashType. |
||
612 | * |
||
613 | * @param int $sigHashType |
||
614 | * @return BufferInterface |
||
615 | */ |
||
616 | 2 | public function getSigHash($sigHashType) |
|
620 | |||
621 | /** |
||
622 | * Pure function to produce a signature for a given $key, $scriptCode, $sigHashType, $sigVersion. |
||
623 | * |
||
624 | * @param PrivateKeyInterface $key |
||
625 | * @param ScriptInterface $scriptCode |
||
626 | * @param int $sigHashType |
||
627 | * @param int $sigVersion |
||
628 | * @return TransactionSignatureInterface |
||
629 | */ |
||
630 | 52 | private function calculateSignature(PrivateKeyInterface $key, ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
636 | |||
637 | /** |
||
638 | * Returns whether all required signatures have been provided. |
||
639 | * |
||
640 | * @return bool |
||
641 | */ |
||
642 | 58 | public function isFullySigned() |
|
646 | |||
647 | /** |
||
648 | * Returns the required number of signatures for this input. |
||
649 | * |
||
650 | * @return int |
||
651 | */ |
||
652 | 50 | public function getRequiredSigs() |
|
656 | |||
657 | /** |
||
658 | * Returns an array where the values are either null, |
||
659 | * or a TransactionSignatureInterface. |
||
660 | * |
||
661 | * @return TransactionSignatureInterface[] |
||
662 | */ |
||
663 | 50 | public function getSignatures() |
|
667 | |||
668 | /** |
||
669 | * Returns an array where the values are either null, |
||
670 | * or a PublicKeyInterface. |
||
671 | * |
||
672 | * @return PublicKeyInterface[] |
||
673 | */ |
||
674 | 52 | public function getPublicKeys() |
|
678 | |||
679 | /** |
||
680 | * OutputData for the script to be signed (will be |
||
681 | * equal to getScriptPubKey, or getRedeemScript, or |
||
682 | * getWitnessScript. |
||
683 | * |
||
684 | * @return OutputData |
||
685 | */ |
||
686 | 50 | public function getSignScript() |
|
690 | |||
691 | /** |
||
692 | * OutputData for the txOut script. |
||
693 | * |
||
694 | * @return OutputData |
||
695 | */ |
||
696 | 24 | public function getScriptPubKey() |
|
700 | |||
701 | /** |
||
702 | * Returns OutputData for the P2SH redeemScript. |
||
703 | * |
||
704 | * @return OutputData |
||
705 | */ |
||
706 | 18 | public function getRedeemScript() |
|
714 | |||
715 | /** |
||
716 | * Returns OutputData for the P2WSH witnessScript. |
||
717 | * |
||
718 | * @return OutputData |
||
719 | */ |
||
720 | 14 | public function getWitnessScript() |
|
728 | |||
729 | /** |
||
730 | * Returns whether the scriptPubKey is P2SH. |
||
731 | * |
||
732 | * @return bool |
||
733 | */ |
||
734 | 50 | public function isP2SH() |
|
742 | |||
743 | /** |
||
744 | * Returns whether the scriptPubKey or redeemScript is P2WSH. |
||
745 | * |
||
746 | * @return bool |
||
747 | */ |
||
748 | 50 | public function isP2WSH() |
|
762 | |||
763 | /** |
||
764 | * Sign the input using $key and $sigHashTypes |
||
765 | * |
||
766 | * @param PrivateKeyInterface $privateKey |
||
767 | * @param int $sigHashType |
||
768 | * @return $this |
||
769 | */ |
||
770 | 58 | public function sign(PrivateKeyInterface $privateKey, $sigHashType = SigHash::ALL) |
|
816 | |||
817 | /** |
||
818 | * Verifies the input using $flags for script verification |
||
819 | * |
||
820 | * @param int $flags |
||
821 | * @return bool |
||
822 | */ |
||
823 | 50 | public function verify($flags = null) |
|
857 | |||
858 | /** |
||
859 | * Produces the script stack that solves the $outputType |
||
860 | * |
||
861 | * @param string $outputType |
||
862 | * @return BufferInterface[] |
||
863 | */ |
||
864 | 52 | private function serializeSolution($outputType) |
|
888 | |||
889 | /** |
||
890 | * Produces a SigValues instance containing the scriptSig & script witness |
||
891 | * |
||
892 | * @return SigValues |
||
893 | */ |
||
894 | 52 | public function serializeSignatures() |
|
934 | } |
||
935 |
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..