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 |
||
| 34 | class InputSigner |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected static $canSign = [ |
||
| 40 | OutputClassifier::PAYTOPUBKEYHASH, |
||
| 41 | OutputClassifier::PAYTOPUBKEY, |
||
| 42 | OutputClassifier::MULTISIG |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected static $validP2sh = [ |
||
| 49 | OutputClassifier::WITNESS_V0_KEYHASH, |
||
| 50 | OutputClassifier::WITNESS_V0_SCRIPTHASH, |
||
| 51 | OutputClassifier::PAYTOPUBKEYHASH, |
||
| 52 | OutputClassifier::PAYTOPUBKEY, |
||
| 53 | OutputClassifier::MULTISIG |
||
| 54 | ]; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var EcAdapterInterface |
||
| 58 | */ |
||
| 59 | private $ecAdapter; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var OutputData $scriptPubKey |
||
| 63 | */ |
||
| 64 | private $scriptPubKey; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var OutputData $redeemScript |
||
| 68 | */ |
||
| 69 | private $redeemScript; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var OutputData $witnessScript |
||
| 73 | */ |
||
| 74 | private $witnessScript; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var OutputData $witnessKeyHash |
||
| 78 | */ |
||
| 79 | private $witnessKeyHash; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var TransactionInterface |
||
| 83 | */ |
||
| 84 | private $tx; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var int |
||
| 88 | */ |
||
| 89 | private $nInput; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var TransactionOutputInterface |
||
| 93 | */ |
||
| 94 | private $txOut; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var PublicKeyInterface[] |
||
| 98 | */ |
||
| 99 | private $publicKeys = []; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var TransactionSignatureInterface[] |
||
| 103 | */ |
||
| 104 | private $signatures = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var int |
||
| 108 | */ |
||
| 109 | private $requiredSigs = 0; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var OutputClassifier |
||
| 113 | */ |
||
| 114 | private $classifier; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var Interpreter |
||
| 118 | */ |
||
| 119 | private $interpreter; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var Checker |
||
| 123 | */ |
||
| 124 | private $signatureChecker; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * TxInputSigning constructor. |
||
| 128 | * @param EcAdapterInterface $ecAdapter |
||
| 129 | * @param TransactionInterface $tx |
||
| 130 | * @param int $nInput |
||
| 131 | * @param TransactionOutputInterface $txOut |
||
| 132 | * @param SignData $signData |
||
| 133 | */ |
||
| 134 | 84 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $tx, $nInput, TransactionOutputInterface $txOut, SignData $signData) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param int $sigVersion |
||
| 157 | * @param TransactionSignatureInterface[] $stack |
||
| 158 | * @param ScriptInterface $scriptCode |
||
| 159 | * @return \SplObjectStorage |
||
| 160 | */ |
||
| 161 | 24 | private function sortMultiSigs($sigVersion, $stack, ScriptInterface $scriptCode) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @param ScriptInterface $script |
||
| 181 | * @return \BitWasp\Buffertools\BufferInterface[] |
||
| 182 | */ |
||
| 183 | 66 | private function evalPushOnly(ScriptInterface $script) |
|
| 184 | { |
||
| 185 | 66 | $stack = new Stack(); |
|
| 186 | 66 | $interpreter = new Interpreter(); |
|
| 187 | 66 | $interpreter->evaluate($script, $stack, SigHash::V0, $this->flags | Interpreter::VERIFY_SIGPUSHONLY, new Checker($this->ecAdapter, $this->tx, $this->nInput, $this->txOut->getValue())); |
|
| 188 | 66 | return $stack->all(); |
|
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param BufferInterface[] $buffers |
||
| 193 | * @return ScriptInterface |
||
| 194 | */ |
||
| 195 | private function pushAll(array $buffers) |
||
| 196 | { |
||
| 197 | 84 | return ScriptFactory::sequence(array_map(function ($buffer) { |
|
| 198 | 66 | if (!($buffer instanceof BufferInterface)) { |
|
| 199 | throw new \RuntimeException('Script contained a non-push opcode'); |
||
| 200 | } |
||
| 201 | |||
| 202 | 66 | $size = $buffer->getSize(); |
|
| 203 | 66 | if ($size === 0) { |
|
| 204 | 18 | return Opcodes::OP_0; |
|
| 205 | } |
||
| 206 | |||
| 207 | 66 | $first = ord($buffer->getBinary()); |
|
| 208 | 66 | if ($size === 1 && $first >= 1 && $first <= 16) { |
|
| 209 | return \BitWasp\Bitcoin\Script\encodeOpN($first); |
||
| 210 | } else { |
||
| 211 | 66 | return $buffer; |
|
| 212 | } |
||
| 213 | 84 | }, $buffers)); |
|
| 214 | } |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * @param ScriptInterface $scriptSig |
||
| 219 | * @param ScriptInterface $scriptPubKey |
||
| 220 | * @param ScriptWitnessInterface|null $scriptWitness |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | 36 | private function verifySolution(ScriptInterface $scriptSig, ScriptInterface $scriptPubKey, ScriptWitnessInterface $scriptWitness = null) |
|
| 224 | { |
||
| 225 | 36 | return $this->interpreter->verify($scriptSig, $scriptPubKey, $this->flags, $this->signatureChecker, $scriptWitness); |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param ScriptInterface $scriptPubKey |
||
| 230 | * @param array $chunks |
||
| 231 | * @param int $sigVersion |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | 54 | private function evaluateSolution(ScriptInterface $scriptPubKey, array $chunks, $sigVersion) |
|
| 235 | { |
||
| 236 | 54 | return $this->interpreter->evaluate($scriptPubKey, new Stack($chunks), $sigVersion, $this->flags, $this->signatureChecker); |
|
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Called upon instance creation. |
||
| 241 | * This function must throw an exception whenever execution |
||
| 242 | * does not yield a signable script. |
||
| 243 | * |
||
| 244 | * It ensures: |
||
| 245 | * - the scriptPubKey can be directly signed, or leads to P2SH/P2WSH/P2WKH |
||
| 246 | * - the P2SH script covers signable types and P2WSH/P2WKH |
||
| 247 | * - the witnessScript covers signable types only. |
||
| 248 | * - violating the above prevents instance creation |
||
| 249 | * @param SignData $signData |
||
| 250 | * @return $this |
||
| 251 | * @throws \Exception |
||
| 252 | */ |
||
| 253 | 84 | private function solve(SignData $signData) |
|
| 254 | { |
||
| 255 | 84 | $scriptPubKey = $this->txOut->getScript(); |
|
| 256 | 84 | $solution = $this->scriptPubKey = $this->classifier->decode($scriptPubKey); |
|
| 257 | 84 | if ($solution->getType() !== OutputClassifier::PAYTOSCRIPTHASH && !in_array($solution->getType(), self::$validP2sh)) { |
|
| 258 | throw new \RuntimeException('scriptPubKey not supported'); |
||
| 259 | } |
||
| 260 | |||
| 261 | 84 | if ($solution->getType() === OutputClassifier::PAYTOSCRIPTHASH) { |
|
| 262 | 24 | $redeemScript = $signData->getRedeemScript(); |
|
| 263 | 24 | if (!$this->verifySolution(ScriptFactory::sequence([$redeemScript->getBuffer()]), $solution->getScript())) { |
|
| 264 | throw new \Exception('Redeem script fails to solve pay-to-script-hash'); |
||
| 265 | } |
||
| 266 | 24 | $solution = $this->redeemScript = $this->classifier->decode($redeemScript); |
|
| 267 | 24 | if (!in_array($solution->getType(), self::$validP2sh)) { |
|
| 268 | throw new \Exception('Unsupported pay-to-script-hash script'); |
||
| 269 | } |
||
| 270 | 8 | } |
|
| 271 | |||
| 272 | 84 | if ($solution->getType() === OutputClassifier::WITNESS_V0_KEYHASH) { |
|
| 273 | 12 | $this->witnessKeyHash = $this->classifier->decode(ScriptFactory::scriptPubKey()->payToPubKeyHash($solution->getSolution())); |
|
| 274 | 76 | } else if ($solution->getType() === OutputClassifier::WITNESS_V0_SCRIPTHASH) { |
|
| 275 | 18 | $witnessScript = $signData->getWitnessScript(); |
|
| 276 | 18 | if (!$this->verifySolution(ScriptFactory::sequence([$witnessScript->getBuffer()]), $solution->getScript())) { |
|
| 277 | throw new \Exception('Witness script fails to solve witness-script-hash'); |
||
| 278 | } |
||
| 279 | 18 | $this->witnessScript = $this->classifier->decode($witnessScript); |
|
| 280 | 18 | if (!in_array($this->witnessScript->getType(), self::$canSign)) { |
|
| 281 | throw new \Exception('Unsupported witness-script-hash script'); |
||
| 282 | } |
||
| 283 | 6 | } |
|
| 284 | |||
| 285 | 84 | return $this; |
|
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * This function is strictly for $canSign types. |
||
| 290 | * It will extract signatures/publicKeys when given $scriptType, $scriptCode, and $stack. |
||
| 291 | * $stack is the result of decompiling a scriptSig, or taking the witness data. |
||
| 292 | * |
||
| 293 | * @param string $type - the scriptCode type |
||
| 294 | * @param ScriptInterface $scriptCode - the script being solved by $stack |
||
| 295 | * @param BufferInterface[] $stack - list of elements which satisfy $scriptCode |
||
| 296 | * @param int $sigVersion - tx signature hashing version |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | 84 | public function extractFromValues($type, ScriptInterface $scriptCode, array $stack, $sigVersion) |
|
| 300 | { |
||
| 301 | 84 | $size = count($stack); |
|
| 302 | 84 | if ($type === OutputClassifier::PAYTOPUBKEYHASH) { |
|
| 303 | 42 | $this->requiredSigs = 1; |
|
| 304 | 42 | if ($size === 2) { |
|
| 305 | 24 | if (!$this->evaluateSolution($scriptCode, $stack, $sigVersion)) { |
|
| 306 | throw new \RuntimeException('Existing signatures are invalid!'); |
||
| 307 | } |
||
| 308 | 24 | $this->signatures = [TransactionSignatureFactory::fromHex($stack[0], $this->ecAdapter)]; |
|
| 309 | 24 | $this->publicKeys = [PublicKeyFactory::fromHex($stack[1], $this->ecAdapter)]; |
|
| 310 | 8 | } |
|
| 311 | 14 | } |
|
| 312 | |||
| 313 | 84 | if ($type === OutputClassifier::PAYTOPUBKEY && count($stack) === 1) { |
|
| 314 | 6 | $this->requiredSigs = 1; |
|
| 315 | 6 | if ($size === 1) { |
|
| 316 | 6 | if (!$this->evaluateSolution($scriptCode, $stack, $sigVersion)) { |
|
| 317 | throw new \RuntimeException('Existing signatures are invalid!'); |
||
| 318 | } |
||
| 319 | 6 | $this->signatures = [TransactionSignatureFactory::fromHex($stack[0], $this->ecAdapter)]; |
|
| 320 | 2 | } |
|
| 321 | 2 | } |
|
| 322 | |||
| 323 | 84 | if ($type === OutputClassifier::MULTISIG) { |
|
| 324 | 24 | $info = new Multisig($scriptCode); |
|
| 325 | 24 | $this->requiredSigs = $info->getRequiredSigCount(); |
|
| 326 | 24 | $this->publicKeys = $info->getKeys(); |
|
| 327 | |||
| 328 | 24 | if ($size > 1) { |
|
| 329 | 24 | $vars = []; |
|
| 330 | 24 | for ($i = 1, $j = $size - 1; $i < $j; $i++) { |
|
| 331 | $vars[] = TransactionSignatureFactory::fromHex($stack[$i], $this->ecAdapter); |
||
| 332 | } |
||
| 333 | |||
| 334 | 24 | $sigs = $this->sortMultiSigs($sigVersion, $vars, $scriptCode); |
|
| 335 | 24 | foreach ($this->publicKeys as $idx => $key) { |
|
| 336 | 24 | $this->signatures[$idx] = isset($sigs[$key]) ? $sigs[$key]->getBuffer() : null; |
|
| 337 | 8 | } |
|
| 338 | |||
| 339 | 24 | if (count(array_filter($this->signatures, 'is_null')) === count($this->publicKeys)) { |
|
| 340 | 24 | if (!$this->evaluateSolution($scriptCode, $stack, $sigVersion)) { |
|
| 341 | throw new \RuntimeException('Existing signatures are invalid!'); |
||
| 342 | } |
||
| 343 | 8 | } |
|
| 344 | 8 | } |
|
| 345 | 8 | } |
|
| 346 | |||
| 347 | 84 | return $type; |
|
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * High level function for extracting signatures from a pre-signed |
||
| 352 | * transaction. |
||
| 353 | * |
||
| 354 | * @return $this |
||
| 355 | */ |
||
| 356 | 84 | public function extractSignatures() |
|
| 357 | { |
||
| 358 | 84 | $scriptSig = $this->tx->getInput($this->nInput)->getScript(); |
|
| 359 | 84 | $witnesses = $this->tx->getWitnesses(); |
|
| 360 | 84 | $witness = isset($witnesses[$this->nInput]) ? $witnesses[$this->nInput]->all() : []; |
|
| 361 | |||
| 362 | 84 | $solution = $this->scriptPubKey; |
|
| 363 | 84 | $sigVersion = SigHash::V0; |
|
| 364 | 84 | $chunks = []; |
|
| 365 | 84 | if ($solution->canSign()) { |
|
| 366 | 42 | $chunks = $this->evalPushOnly($scriptSig); |
|
| 367 | 14 | } |
|
| 368 | |||
| 369 | 84 | if ($solution->getType() === OutputClassifier::PAYTOSCRIPTHASH) { |
|
| 370 | 24 | $chunks = $this->evalPushOnly($scriptSig); |
|
| 371 | 24 | if (count($chunks) > 0) { |
|
| 372 | 18 | if (!end($chunks)->equals($this->redeemScript->getScript()->getBuffer())) { |
|
| 373 | throw new \RuntimeException('Extracted redeemScript did not match script-hash'); |
||
| 374 | } |
||
| 375 | |||
| 376 | 18 | $solution = $this->redeemScript; |
|
| 377 | 18 | $chunks = array_slice($chunks, 0, -1); |
|
| 378 | 6 | } |
|
| 379 | 8 | } |
|
| 380 | |||
| 381 | 84 | if ($solution->getType() === OutputClassifier::WITNESS_V0_KEYHASH) { |
|
| 382 | 12 | $solution = $this->witnessKeyHash; |
|
| 383 | 12 | $sigVersion = SigHash::V1; |
|
| 384 | 12 | $chunks = $witness; |
|
| 385 | 80 | } else if ($solution->getType() === OutputClassifier::WITNESS_V0_SCRIPTHASH) { |
|
| 386 | 18 | if (count($witness) > 0) { |
|
| 387 | 18 | if (!end($witness)->equals($this->witnessScript->getScript()->getBuffer())) { |
|
| 388 | throw new \RuntimeException('Extracted witnessScript did not match witness-script-hash'); |
||
| 389 | } |
||
| 390 | |||
| 391 | 18 | $solution = $this->witnessScript; |
|
| 392 | 18 | $sigVersion = SigHash::V1; |
|
| 393 | 18 | $chunks = array_slice($witness, 0, -1); |
|
| 394 | 6 | } |
|
| 395 | 6 | } |
|
| 396 | |||
| 397 | 84 | $this->extractFromValues($solution->getType(), $solution->getScript(), $chunks, $sigVersion); |
|
| 398 | |||
| 399 | 84 | return $this; |
|
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param ScriptInterface $scriptCode |
||
| 404 | * @param int $sigHashType |
||
| 405 | * @param int $sigVersion |
||
| 406 | * @return BufferInterface |
||
| 407 | */ |
||
| 408 | 84 | public function calculateSigHash(ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
| 409 | { |
||
| 410 | 84 | if (!$this->signatureChecker->isDefinedHashtype($sigHashType)) { |
|
| 411 | throw new \RuntimeException('Invalid sigHashType requested'); |
||
| 412 | } |
||
| 413 | |||
| 414 | 84 | if ($sigVersion === SigHash::V1) { |
|
| 415 | 30 | $hasher = new V1Hasher($this->tx, $this->txOut->getValue()); |
|
| 416 | 10 | } else { |
|
| 417 | 54 | $hasher = new Hasher($this->tx); |
|
| 418 | } |
||
| 419 | |||
| 420 | 84 | return $hasher->calculate($scriptCode, $this->nInput, $sigHashType); |
|
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param PrivateKeyInterface $key |
||
| 425 | * @param ScriptInterface $scriptCode |
||
| 426 | * @param int $sigHashType |
||
| 427 | * @param int $sigVersion |
||
| 428 | * @return TransactionSignature |
||
| 429 | */ |
||
| 430 | 84 | public function calculateSignature(PrivateKeyInterface $key, ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
| 431 | { |
||
| 432 | 84 | $hash = $this->calculateSigHash($scriptCode, $sigHashType, $sigVersion); |
|
| 433 | 84 | $ecSignature = $this->ecAdapter->sign($hash, $key, new Rfc6979($this->ecAdapter, $key, $hash, 'sha256')); |
|
| 434 | 84 | return new TransactionSignature($this->ecAdapter, $ecSignature, $sigHashType); |
|
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return bool |
||
| 439 | */ |
||
| 440 | 84 | public function isFullySigned() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * @return int |
||
| 447 | */ |
||
| 448 | public function getRequiredSigs() |
||
| 449 | { |
||
| 450 | return $this->requiredSigs; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return TransactionSignatureInterface[] |
||
| 455 | */ |
||
| 456 | public function getSignatures() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @return PublicKeyInterface[] |
||
| 463 | */ |
||
| 464 | public function getPublicKeys() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * The function only returns true when $scriptPubKey could be classified |
||
| 471 | * |
||
| 472 | * @param PrivateKeyInterface $key |
||
| 473 | * @param OutputData $solution |
||
| 474 | * @param int $sigHashType |
||
| 475 | * @param int $sigVersion |
||
| 476 | */ |
||
| 477 | 84 | private function doSignature(PrivateKeyInterface $key, OutputData $solution, $sigHashType, $sigVersion = SigHash::V0) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * @param PrivateKeyInterface $key |
||
| 521 | * @param int $sigHashType |
||
| 522 | * @return bool |
||
| 523 | */ |
||
| 524 | 84 | public function sign(PrivateKeyInterface $key, $sigHashType = SigHash::ALL) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * @param string $outputType |
||
| 550 | * @return BufferInterface[] |
||
| 551 | */ |
||
| 552 | 84 | private function serializeSolution($outputType) |
|
| 571 | |||
| 572 | /** |
||
| 573 | * @return SigValues |
||
| 574 | */ |
||
| 575 | 84 | public function serializeSignatures() |
|
| 615 | } |
||
| 616 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: