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  | 
            ||
| 36 | { | 
            ||
| 37 | /**  | 
            ||
| 38 | * @var EcAdapterInterface  | 
            ||
| 39 | */  | 
            ||
| 40 | private $ecAdapter;  | 
            ||
| 41 | |||
| 42 | /**  | 
            ||
| 43 | * @var OutputData $scriptPubKey  | 
            ||
| 44 | */  | 
            ||
| 45 | private $scriptPubKey;  | 
            ||
| 46 | |||
| 47 | /**  | 
            ||
| 48 | * @var OutputData $redeemScript  | 
            ||
| 49 | */  | 
            ||
| 50 | private $redeemScript;  | 
            ||
| 51 | |||
| 52 | /**  | 
            ||
| 53 | * @var OutputData $witnessScript  | 
            ||
| 54 | */  | 
            ||
| 55 | private $witnessScript;  | 
            ||
| 56 | |||
| 57 | /**  | 
            ||
| 58 | * @var TransactionInterface  | 
            ||
| 59 | */  | 
            ||
| 60 | private $tx;  | 
            ||
| 61 | |||
| 62 | /**  | 
            ||
| 63 | * @var int  | 
            ||
| 64 | */  | 
            ||
| 65 | private $nInput;  | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * @var TransactionOutputInterface  | 
            ||
| 69 | */  | 
            ||
| 70 | private $txOut;  | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * @var PublicKeyInterface[]  | 
            ||
| 74 | */  | 
            ||
| 75 | private $publicKeys = [];  | 
            ||
| 76 | |||
| 77 | /**  | 
            ||
| 78 | * @var TransactionSignatureInterface[]  | 
            ||
| 79 | */  | 
            ||
| 80 | private $signatures = [];  | 
            ||
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * @var int  | 
            ||
| 84 | */  | 
            ||
| 85 | private $requiredSigs = 0;  | 
            ||
| 86 | |||
| 87 | /**  | 
            ||
| 88 | * @var OutputClassifier  | 
            ||
| 89 | */  | 
            ||
| 90 | private $classifier;  | 
            ||
| 91 | |||
| 92 | /**  | 
            ||
| 93 | * TxInputSigning constructor.  | 
            ||
| 94 | * @param EcAdapterInterface $ecAdapter  | 
            ||
| 95 | * @param TransactionInterface $tx  | 
            ||
| 96 | * @param int $nInput  | 
            ||
| 97 | * @param TransactionOutputInterface $txOut  | 
            ||
| 98 | * @param SignData $signData  | 
            ||
| 99 | */  | 
            ||
| 100 | 84 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $tx, $nInput, TransactionOutputInterface $txOut, SignData $signData)  | 
            |
| 101 |     { | 
            ||
| 102 | 84 | $this->ecAdapter = $ecAdapter;  | 
            |
| 103 | 84 | $this->tx = $tx;  | 
            |
| 104 | 84 | $this->nInput = $nInput;  | 
            |
| 105 | 84 | $this->txOut = $txOut;  | 
            |
| 106 | 84 | $this->classifier = new OutputClassifier();  | 
            |
| 107 | 84 | $this->publicKeys = [];  | 
            |
| 108 | 84 | $this->signatures = [];  | 
            |
| 109 | |||
| 110 | 84 | $this->solve($signData);  | 
            |
| 111 | 84 | $this->extractSignatures();  | 
            |
| 112 | 84 | }  | 
            |
| 113 | |||
| 114 | /**  | 
            ||
| 115 | * @param int $sigVersion  | 
            ||
| 116 | * @param TransactionSignatureInterface[] $stack  | 
            ||
| 117 | * @param ScriptInterface $scriptCode  | 
            ||
| 118 | * @return \SplObjectStorage  | 
            ||
| 119 | */  | 
            ||
| 120 | 26 | private function sortMultiSigs($sigVersion, $stack, ScriptInterface $scriptCode)  | 
            |
| 121 |     { | 
            ||
| 122 | 24 | $sigSort = new SignatureSort($this->ecAdapter);  | 
            |
| 123 | 24 | $sigs = new \SplObjectStorage;  | 
            |
| 124 | |||
| 125 | 24 |         foreach ($stack as $txSig) { | 
            |
| 126 | $hash = $this->calculateSigHash($scriptCode, $txSig->getHashType(), $sigVersion);  | 
            ||
| 127 | $linked = $sigSort->link([$txSig->getSignature()], $this->publicKeys, $hash);  | 
            ||
| 128 |             foreach ($this->publicKeys as $key) { | 
            ||
| 129 |                 if ($linked->contains($key)) { | 
            ||
| 130 | $sigs[$key] = $txSig;  | 
            ||
| 131 | }  | 
            ||
| 132 | }  | 
            ||
| 133 | 8 | }  | 
            |
| 134 | |||
| 135 | 26 | return $sigs;  | 
            |
| 136 | }  | 
            ||
| 137 | |||
| 138 | /**  | 
            ||
| 139 | * @param string $type  | 
            ||
| 140 | * @param ScriptInterface $scriptCode  | 
            ||
| 141 | * @param BufferInterface[] $stack  | 
            ||
| 142 | * @param int $sigVersion  | 
            ||
| 143 | * @return string  | 
            ||
| 144 | */  | 
            ||
| 145 | 78 | public function extractFromValues($type, ScriptInterface $scriptCode, array $stack, $sigVersion)  | 
            |
| 146 |     { | 
            ||
| 147 | 78 | $size = count($stack);  | 
            |
| 148 | 78 |         if ($type === OutputClassifier::PAYTOPUBKEYHASH) { | 
            |
| 149 | 42 | $this->requiredSigs = 1;  | 
            |
| 150 | 42 |             if ($size === 2) { | 
            |
| 151 | 24 | $this->signatures = [TransactionSignatureFactory::fromHex($stack[0], $this->ecAdapter)];  | 
            |
| 152 | 24 | $this->publicKeys = [PublicKeyFactory::fromHex($stack[1], $this->ecAdapter)];  | 
            |
| 153 | 8 | }  | 
            |
| 154 | 14 | }  | 
            |
| 155 | |||
| 156 | 78 |         if ($type === OutputClassifier::PAYTOPUBKEY) { | 
            |
| 157 | 12 | $this->requiredSigs = 1;  | 
            |
| 158 | 12 |             if ($size === 1) { | 
            |
| 159 | 6 | $this->signatures = [TransactionSignatureFactory::fromHex($stack[0], $this->ecAdapter)];  | 
            |
| 160 | 2 | }  | 
            |
| 161 | 4 | }  | 
            |
| 162 | |||
| 163 | 78 |         if ($type === OutputClassifier::MULTISIG) { | 
            |
| 164 | 24 | $info = new Multisig($scriptCode);  | 
            |
| 165 | 24 | $this->requiredSigs = $info->getRequiredSigCount();  | 
            |
| 166 | 24 | $this->publicKeys = $info->getKeys();  | 
            |
| 167 | |||
| 168 | 24 |             if ($size > 1) { | 
            |
| 169 | 24 | $vars = [];  | 
            |
| 170 | 24 |                 for ($i = 1, $j = $size - 1; $i < $j; $i++) { | 
            |
| 171 | $vars[] = TransactionSignatureFactory::fromHex($stack[$i], $this->ecAdapter);  | 
            ||
| 172 | }  | 
            ||
| 173 | |||
| 174 | 24 | $sigs = $this->sortMultiSigs($sigVersion, $vars, $scriptCode);  | 
            |
| 175 | 24 |                 foreach ($this->publicKeys as $idx => $key) { | 
            |
| 176 | 24 | $this->signatures[$idx] = isset($sigs[$key]) ? $sigs[$key]->getBuffer() : null;  | 
            |
| 177 | 8 | }  | 
            |
| 178 | 8 | }  | 
            |
| 179 | 8 | }  | 
            |
| 180 | |||
| 181 | 78 | return $type;  | 
            |
| 182 | }  | 
            ||
| 183 | |||
| 184 | /**  | 
            ||
| 185 | * @param SignData $signData  | 
            ||
| 186 | * @return $this  | 
            ||
| 187 | * @throws \Exception  | 
            ||
| 188 | */  | 
            ||
| 189 | 84 | private function solve(SignData $signData)  | 
            |
| 190 |     { | 
            ||
| 191 | 84 | $scriptPubKey = $this->txOut->getScript();  | 
            |
| 192 | 84 | $solution = $this->scriptPubKey = $this->classifier->decode($scriptPubKey);  | 
            |
| 193 | 84 |         if ($solution->getType() === OutputClassifier::UNKNOWN) { | 
            |
| 194 |             throw new \RuntimeException('scriptPubKey type is unknown'); | 
            ||
| 195 | }  | 
            ||
| 196 | |||
| 197 | 84 |         if ($solution->getType() === OutputClassifier::PAYTOSCRIPTHASH) { | 
            |
| 198 | 24 | $redeemScript = $signData->getRedeemScript();  | 
            |
| 199 | 24 |             if (!$solution->getSolution()->equals(Hash::sha256ripe160($redeemScript->getBuffer()))) { | 
            |
| 200 |                 throw new \Exception('Redeem script doesn\'t match script-hash'); | 
            ||
| 201 | }  | 
            ||
| 202 | 24 | $solution = $this->redeemScript = $this->classifier->decode($redeemScript);  | 
            |
| 203 | 24 |             if (!in_array($solution->getType(), [OutputClassifier::WITNESS_V0_SCRIPTHASH, OutputClassifier::WITNESS_V0_KEYHASH, OutputClassifier::PAYTOPUBKEYHASH , OutputClassifier::PAYTOPUBKEY, OutputClassifier::MULTISIG])) { | 
            |
| 204 |                 throw new \Exception('Unsupported pay-to-script-hash script'); | 
            ||
| 205 | }  | 
            ||
| 206 | 8 | }  | 
            |
| 207 | // WitnessKeyHash doesn't require further solving until signing  | 
            ||
| 208 | 84 |         if ($solution->getType() === OutputClassifier::WITNESS_V0_SCRIPTHASH) { | 
            |
| 209 | 18 | $witnessScript = $signData->getWitnessScript();  | 
            |
| 210 | 18 |             if (!$solution->getSolution()->equals(Hash::sha256($witnessScript->getBuffer()))) { | 
            |
| 211 |                 throw new \Exception('Witness script doesn\'t match witness-script-hash'); | 
            ||
| 212 | }  | 
            ||
| 213 | 18 | $solution = $this->witnessScript = $this->classifier->decode($witnessScript);  | 
            |
| 214 | 18 |             if (!in_array($solution->getType(), [OutputClassifier::PAYTOPUBKEYHASH , OutputClassifier::PAYTOPUBKEY, OutputClassifier::MULTISIG])) { | 
            |
| 215 |                 throw new \Exception('Unsupported witness-script-hash script'); | 
            ||
| 216 | }  | 
            ||
| 217 | 6 | }  | 
            |
| 218 | |||
| 219 | 84 | return $this;  | 
            |
| 220 | }  | 
            ||
| 221 | |||
| 222 | /**  | 
            ||
| 223 | * @return $this  | 
            ||
| 224 | */  | 
            ||
| 225 | 84 | public function extractSignatures()  | 
            |
| 226 |     { | 
            ||
| 227 | 84 | $solution = $this->scriptPubKey;  | 
            |
| 228 | 84 | $scriptSig = $this->tx->getInput($this->nInput)->getScript();  | 
            |
| 229 | 84 |         if (in_array($solution->getType(), [OutputClassifier::PAYTOPUBKEYHASH , OutputClassifier::PAYTOPUBKEY, OutputClassifier::MULTISIG])) { | 
            |
| 230 | 42 | $this->extractFromValues($solution->getType(), $solution->getScript(), $this->evalPushOnly($scriptSig), 0);  | 
            |
| 231 | 14 | }  | 
            |
| 232 | |||
| 233 | 84 |         if ($solution->getType() === OutputClassifier::PAYTOSCRIPTHASH) { | 
            |
| 234 | 24 | $stack = $this->evalPushOnly($scriptSig);  | 
            |
| 235 | 24 |             if (count($stack) > 0) { | 
            |
| 236 | 18 | $redeemScript = new Script(end($stack));  | 
            |
| 
                                                                                                    
                        
                         | 
                |||
| 237 | 18 |                 if (!$redeemScript->getBuffer()->equals($this->redeemScript->getScript()->getBuffer())) { | 
            |
| 238 |                     throw new \RuntimeException('Redeem script from scriptSig doesn\'t match script-hash'); | 
            ||
| 239 | }  | 
            ||
| 240 | |||
| 241 | 18 | $solution = $this->redeemScript;  | 
            |
| 242 | 18 | $this->extractFromValues($solution->getType(), $solution->getScript(), array_slice($stack, 0, -1), 0);  | 
            |
| 243 | 6 | }  | 
            |
| 244 | 8 | }  | 
            |
| 245 | |||
| 246 | 84 | $witnesses = $this->tx->getWitnesses();  | 
            |
| 247 | 84 |         if ($solution->getType() === OutputClassifier::WITNESS_V0_KEYHASH) { | 
            |
| 248 | 12 | $wit = isset($witnesses[$this->nInput]) ? $witnesses[$this->nInput]->all() : [];  | 
            |
| 249 | 12 | $keyHashCode = ScriptFactory::scriptPubKey()->payToPubKeyHashFromHash($solution->getSolution());  | 
            |
| 250 | 12 | $this->extractFromValues(OutputClassifier::PAYTOPUBKEYHASH, $keyHashCode, $wit, 1);  | 
            |
| 251 | 80 |         } else if ($solution->getType() === OutputClassifier::WITNESS_V0_SCRIPTHASH) { | 
            |
| 252 | 18 |             if (isset($witnesses[$this->nInput])) { | 
            |
| 253 | 18 | $witness = $witnesses[$this->nInput];  | 
            |
| 254 | 18 | $witCount = count($witnesses[$this->nInput]);  | 
            |
| 255 | 18 |                 if ($witCount > 0) { | 
            |
| 256 | 18 |                     if (!$witness[$witCount - 1]->equals($this->witnessScript->getScript()->getBuffer())) { | 
            |
| 257 |                         throw new \RuntimeException('Redeem script from scriptSig doesn\'t match script-hash'); | 
            ||
| 258 | }  | 
            ||
| 259 | |||
| 260 | 18 | $solution = $this->witnessScript;  | 
            |
| 261 | 18 | $this->extractFromValues($solution->getType(), $solution->getScript(), array_slice($witness->all(), 0, -1), 1);  | 
            |
| 262 | 6 | }  | 
            |
| 263 | 6 | }  | 
            |
| 264 | 6 | }  | 
            |
| 265 | |||
| 266 | 84 | return $this;  | 
            |
| 267 | }  | 
            ||
| 268 | |||
| 269 | /**  | 
            ||
| 270 | * @param ScriptInterface $scriptCode  | 
            ||
| 271 | * @param int $sigHashType  | 
            ||
| 272 | * @param int $sigVersion  | 
            ||
| 273 | * @return BufferInterface  | 
            ||
| 274 | */  | 
            ||
| 275 | 84 | public function calculateSigHash(ScriptInterface $scriptCode, $sigHashType, $sigVersion)  | 
            |
| 276 |     { | 
            ||
| 277 | 84 |         if ($sigVersion === 1) { | 
            |
| 278 | 30 | $hasher = new V1Hasher($this->tx, $this->txOut->getValue());  | 
            |
| 279 | 10 |         } else { | 
            |
| 280 | 54 | $hasher = new Hasher($this->tx);  | 
            |
| 281 | }  | 
            ||
| 282 | |||
| 283 | 84 | return $hasher->calculate($scriptCode, $this->nInput, $sigHashType);  | 
            |
| 284 | }  | 
            ||
| 285 | |||
| 286 | /**  | 
            ||
| 287 | * @param PrivateKeyInterface $key  | 
            ||
| 288 | * @param ScriptInterface $scriptCode  | 
            ||
| 289 | * @param int $sigHashType  | 
            ||
| 290 | * @param int $sigVersion  | 
            ||
| 291 | * @return TransactionSignature  | 
            ||
| 292 | */  | 
            ||
| 293 | 84 | public function calculateSignature(PrivateKeyInterface $key, ScriptInterface $scriptCode, $sigHashType, $sigVersion)  | 
            |
| 294 |     { | 
            ||
| 295 | 84 | $hash = $this->calculateSigHash($scriptCode, $sigHashType, $sigVersion);  | 
            |
| 296 | 84 | $ecSignature = $this->ecAdapter->sign($hash, $key, new Rfc6979($this->ecAdapter, $key, $hash, 'sha256'));  | 
            |
| 297 | 84 | return new TransactionSignature($this->ecAdapter, $ecSignature, $sigHashType);  | 
            |
| 298 | }  | 
            ||
| 299 | |||
| 300 | /**  | 
            ||
| 301 | * @return bool  | 
            ||
| 302 | */  | 
            ||
| 303 | 54 | public function isFullySigned()  | 
            |
| 307 | |||
| 308 | /**  | 
            ||
| 309 | * The function only returns true when $scriptPubKey could be classified  | 
            ||
| 310 | *  | 
            ||
| 311 | * @param PrivateKeyInterface $key  | 
            ||
| 312 | * @param OutputData $solution  | 
            ||
| 313 | * @param int $sigHashType  | 
            ||
| 314 | * @param int $sigVersion  | 
            ||
| 315 | */  | 
            ||
| 316 | 84 | private function doSignature(PrivateKeyInterface $key, OutputData $solution, $sigHashType, $sigVersion = 0)  | 
            |
| 353 | |||
| 354 | /**  | 
            ||
| 355 | * @param PrivateKeyInterface $key  | 
            ||
| 356 | * @param int $sigHashType  | 
            ||
| 357 | * @return bool  | 
            ||
| 358 | */  | 
            ||
| 359 | 84 | public function sign(PrivateKeyInterface $key, $sigHashType = SigHashInterface::ALL)  | 
            |
| 387 | |||
| 388 | /**  | 
            ||
| 389 | * @param string $outputType  | 
            ||
| 390 | * @return BufferInterface[]  | 
            ||
| 391 | */  | 
            ||
| 392 | 84 | private function serializeSolution($outputType)  | 
            |
| 411 | |||
| 412 | /**  | 
            ||
| 413 | * @param ScriptInterface $script  | 
            ||
| 414 | * @param int $flags  | 
            ||
| 415 | * @return \BitWasp\Buffertools\BufferInterface[]  | 
            ||
| 416 | */  | 
            ||
| 417 | 66 | private function evalPushOnly(ScriptInterface $script, $flags = Interpreter::VERIFY_NONE)  | 
            |
| 424 | |||
| 425 | /**  | 
            ||
| 426 | * @param BufferInterface[] $buffers  | 
            ||
| 427 | * @return ScriptInterface  | 
            ||
| 428 | */  | 
            ||
| 429 | public function pushAll(array $buffers)  | 
            ||
| 449 | |||
| 450 | /**  | 
            ||
| 451 | * @return SigValues  | 
            ||
| 452 | */  | 
            ||
| 453 | 84 | public function serializeSignatures()  | 
            |
| 495 | }  | 
            ||
| 496 |