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