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 |
||
| 29 | class InputSigner |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var EcAdapterInterface |
||
| 33 | */ |
||
| 34 | private $ecAdapter; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var ScriptInterface $redeemScript |
||
| 38 | */ |
||
| 39 | private $redeemScript; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var ScriptInterface $witnessScript |
||
| 43 | */ |
||
| 44 | private $witnessScript; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var TransactionInterface |
||
| 48 | */ |
||
| 49 | private $tx; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | private $nInput; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var TransactionOutputInterface |
||
| 58 | */ |
||
| 59 | private $txOut; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var PublicKeyInterface[] |
||
| 63 | */ |
||
| 64 | private $publicKeys = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | private $sigHashType; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var TransactionSignatureInterface[] |
||
| 73 | */ |
||
| 74 | private $signatures = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var int |
||
| 78 | */ |
||
| 79 | private $requiredSigs = 0; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var OutputClassifier |
||
| 83 | */ |
||
| 84 | private $classifier; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * TxInputSigning constructor. |
||
| 88 | * @param EcAdapterInterface $ecAdapter |
||
| 89 | * @param TransactionInterface $tx |
||
| 90 | * @param int $nInput |
||
| 91 | * @param TransactionOutputInterface $txOut |
||
| 92 | * @param int $sigHashType |
||
| 93 | */ |
||
| 94 | 84 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $tx, $nInput, TransactionOutputInterface $txOut, $sigHashType = SigHashInterface::ALL) |
|
| 95 | { |
||
| 96 | 84 | $this->ecAdapter = $ecAdapter; |
|
| 97 | 84 | $this->tx = $tx; |
|
| 98 | 84 | $this->nInput = $nInput; |
|
| 99 | 84 | $this->txOut = $txOut; |
|
| 100 | 84 | $this->classifier = new OutputClassifier(); |
|
| 101 | 84 | $this->sigHashType = $sigHashType; |
|
| 102 | 84 | $this->publicKeys = []; |
|
| 103 | 84 | $this->signatures = []; |
|
| 104 | |||
| 105 | 84 | $this->extractSignatures(); |
|
| 106 | 84 | } |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @param int $sigVersion |
||
| 110 | * @param $stack |
||
| 111 | * @param ScriptInterface $scriptCode |
||
| 112 | * @return \SplObjectStorage |
||
| 113 | */ |
||
| 114 | 24 | private function sortMultiSigs($sigVersion, $stack, ScriptInterface $scriptCode) |
|
| 115 | { |
||
| 116 | 24 | if ($sigVersion === 1) { |
|
| 117 | 12 | $hasher = new V1Hasher($this->tx, $this->txOut->getValue()); |
|
| 118 | 4 | } else { |
|
| 119 | 12 | $hasher = new Hasher($this->tx); |
|
| 120 | } |
||
| 121 | |||
| 122 | 24 | $sigSort = new SignatureSort($this->ecAdapter); |
|
| 123 | 24 | $sigs = new \SplObjectStorage; |
|
| 124 | |||
| 125 | 24 | foreach ($stack as $txSig) { |
|
| 126 | $hash = $hasher->calculate($scriptCode, $this->nInput, $txSig->getHashType()); |
||
| 127 | $linked = $sigSort->link([$txSig->getSignature()], $this->publicKeys, $hash); |
||
| 128 | |||
| 129 | foreach ($this->publicKeys as $key) { |
||
| 130 | if ($linked->contains($key)) { |
||
| 131 | $sigs[$key] = $txSig; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | 8 | } |
|
| 135 | |||
| 136 | 24 | return $sigs; |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $type |
||
| 141 | * @param ScriptInterface $scriptCode |
||
| 142 | * @param BufferInterface[] $stack |
||
| 143 | * @param int $sigVersion |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | 72 | public function extractFromValues($type, ScriptInterface $scriptCode, array $stack, $sigVersion) |
|
| 147 | { |
||
| 148 | 72 | $size = count($stack); |
|
| 149 | 72 | if ($type === OutputClassifier::PAYTOPUBKEYHASH) { |
|
| 150 | 30 | $this->requiredSigs = 1; |
|
| 151 | 30 | if ($size === 2) { |
|
| 152 | 12 | $this->signatures = [TransactionSignatureFactory::fromHex($stack[0], $this->ecAdapter)]; |
|
| 153 | 12 | $this->publicKeys = [PublicKeyFactory::fromHex($stack[1], $this->ecAdapter)]; |
|
| 154 | 4 | } |
|
| 155 | 10 | } |
|
| 156 | |||
| 157 | 72 | if ($type === OutputClassifier::PAYTOPUBKEY) { |
|
| 158 | 12 | $this->requiredSigs = 1; |
|
| 159 | 12 | if ($size === 1) { |
|
| 160 | 6 | $this->signatures = [TransactionSignatureFactory::fromHex($stack[0], $this->ecAdapter)]; |
|
| 161 | 2 | } |
|
| 162 | 4 | } |
|
| 163 | |||
| 164 | 72 | if ($type === OutputClassifier::MULTISIG) { |
|
| 165 | 24 | $info = new Multisig($scriptCode); |
|
| 166 | 24 | $this->requiredSigs = $info->getRequiredSigCount(); |
|
| 167 | 24 | $this->publicKeys = $info->getKeys(); |
|
| 168 | |||
| 169 | 24 | if ($size > 1) { |
|
| 170 | 24 | $vars = []; |
|
| 171 | 24 | foreach (array_slice($stack, 1, -1) as $sig) { |
|
| 172 | $vars[] = TransactionSignatureFactory::fromHex($sig, $this->ecAdapter); |
||
| 173 | 8 | } |
|
| 174 | |||
| 175 | 24 | $sigs = $this->sortMultiSigs($sigVersion, $vars, $scriptCode); |
|
| 176 | |||
| 177 | 24 | foreach ($this->publicKeys as $idx => $key) { |
|
| 178 | 24 | $this->signatures[$idx] = isset($sigs[$key]) ? $sigs[$key]->getBuffer() : null; |
|
| 179 | 8 | } |
|
| 180 | 8 | } |
|
| 181 | 8 | } |
|
| 182 | |||
| 183 | 72 | return $type; |
|
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | 84 | public function extractSignatures() |
|
| 190 | { |
||
| 191 | 84 | $scriptPubKey = $this->txOut->getScript(); |
|
| 192 | 84 | $scriptSig = $this->tx->getInput($this->nInput)->getScript(); |
|
| 193 | 84 | $type = $this->classifier->classify($scriptPubKey); |
|
| 194 | |||
| 195 | 84 | if ($type === OutputClassifier::PAYTOPUBKEYHASH || $type === OutputClassifier::PAYTOPUBKEY || $type === OutputClassifier::MULTISIG) { |
|
| 196 | 42 | $values = []; |
|
| 197 | 42 | foreach ($scriptSig->getScriptParser()->decode() as $o) { |
|
| 198 | 18 | $values[] = $o->getData(); |
|
| 199 | 14 | } |
|
| 200 | |||
| 201 | 42 | $this->extractFromValues($type, $scriptPubKey, $values, 0); |
|
| 202 | 14 | } |
|
| 203 | |||
| 204 | 84 | if ($type === OutputClassifier::PAYTOSCRIPTHASH) { |
|
| 205 | 24 | $decodeSig = $scriptSig->getScriptParser()->decode(); |
|
| 206 | 24 | if (count($decodeSig) > 0) { |
|
| 207 | 18 | $redeemScript = new Script(end($decodeSig)->getData()); |
|
| 208 | 18 | $p2shType = $this->classifier->classify($redeemScript); |
|
| 209 | |||
| 210 | 18 | if (count($decodeSig) > 1) { |
|
| 211 | 6 | $decodeSig = array_slice($decodeSig, 0, -1); |
|
| 212 | 2 | } |
|
| 213 | |||
| 214 | 18 | $internalSig = []; |
|
| 215 | 18 | foreach ($decodeSig as $operation) { |
|
| 216 | 18 | $internalSig[] = $operation->getData(); |
|
| 217 | 6 | } |
|
| 218 | |||
| 219 | 18 | $this->redeemScript = $redeemScript; |
|
| 220 | 18 | $this->extractFromValues($p2shType, $redeemScript, $internalSig, 0); |
|
| 221 | |||
| 222 | 18 | $type = $p2shType; |
|
| 223 | 6 | } |
|
| 224 | 8 | } |
|
| 225 | |||
| 226 | 84 | $witnesses = $this->tx->getWitnesses(); |
|
| 227 | 84 | if ($type === OutputClassifier::WITNESS_V0_KEYHASH) { |
|
| 228 | 12 | $this->requiredSigs = 1; |
|
| 229 | 12 | if (isset($witnesses[$this->nInput])) { |
|
| 230 | 12 | $witness = $witnesses[$this->nInput]; |
|
| 231 | 12 | $this->signatures = [TransactionSignatureFactory::fromHex($witness[0], $this->ecAdapter)]; |
|
| 232 | 12 | $this->publicKeys = [PublicKeyFactory::fromHex($witness[1], $this->ecAdapter)]; |
|
| 233 | 4 | } |
|
| 234 | 80 | } else if ($type === OutputClassifier::WITNESS_V0_SCRIPTHASH) { |
|
| 235 | 18 | if (isset($witnesses[$this->nInput])) { |
|
| 236 | 18 | $witness = $witnesses[$this->nInput]; |
|
| 237 | 18 | $witCount = count($witnesses[$this->nInput]); |
|
| 238 | 18 | if ($witCount > 0) { |
|
| 239 | 18 | $witnessScript = new Script($witness[$witCount - 1]); |
|
| 240 | 18 | $vWitness = $witness->all(); |
|
| 241 | 18 | if (count($vWitness) > 1) { |
|
| 242 | 18 | $vWitness = array_slice($witness->all(), 0, -1); |
|
| 243 | 6 | } |
|
| 244 | |||
| 245 | 18 | $witnessType = $this->classifier->classify($witnessScript); |
|
| 246 | 18 | $this->extractFromValues($witnessType, $witnessScript, $vWitness, 1); |
|
| 247 | 18 | $this->witnessScript = $witnessScript; |
|
| 248 | 6 | } |
|
| 249 | 6 | } |
|
| 250 | 6 | } |
|
| 251 | |||
| 252 | 84 | return $this; |
|
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param PrivateKeyInterface $key |
||
| 257 | * @param ScriptInterface $scriptCode |
||
| 258 | * @param int $sigVersion |
||
| 259 | * @return TransactionSignature |
||
| 260 | */ |
||
| 261 | 84 | public function calculateSignature(PrivateKeyInterface $key, ScriptInterface $scriptCode, $sigVersion) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | 78 | public function isFullySigned() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * The function only returns true when $scriptPubKey could be classified |
||
| 297 | * |
||
| 298 | * @param PrivateKeyInterface $key |
||
| 299 | * @param ScriptInterface $scriptPubKey |
||
| 300 | * @param string $outputType |
||
| 301 | * @param BufferInterface[] $results |
||
| 302 | * @param int $sigVersion |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | 84 | private function doSignature(PrivateKeyInterface $key, ScriptInterface $scriptPubKey, &$outputType, array &$results, $sigVersion = 0) |
|
| 384 | |||
| 385 | 18 | /** |
|
| 386 | * @param PrivateKeyInterface $key |
||
| 387 | * @param ScriptInterface|null $redeemScript |
||
| 388 | * @param ScriptInterface|null $witnessScript |
||
| 389 | * @return bool |
||
| 390 | */ |
||
| 391 | public function sign(PrivateKeyInterface $key, ScriptInterface $redeemScript = null, ScriptInterface $witnessScript = null) |
||
| 448 | 18 | ||
| 449 | 6 | /** |
|
| 450 | 6 | * @param string $outputType |
|
| 451 | * @param $answer |
||
| 452 | 84 | * @return bool |
|
| 453 | */ |
||
| 454 | private function serializeSimpleSig($outputType, &$answer) |
||
| 485 | 30 | ||
| 486 | 30 | /** |
|
| 487 | * @return SigValues |
||
| 488 | */ |
||
| 489 | 42 | public function serializeSignatures() |
|
| 538 | } |
||
| 539 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: