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 TransactionSignatureInterface[] |
||
| 68 | */ |
||
| 69 | private $signatures = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | private $requiredSigs = 0; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var OutputClassifier |
||
| 78 | */ |
||
| 79 | private $classifier; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * TxInputSigning constructor. |
||
| 83 | * @param EcAdapterInterface $ecAdapter |
||
| 84 | * @param TransactionInterface $tx |
||
| 85 | * @param int $nInput |
||
| 86 | * @param TransactionOutputInterface $txOut |
||
| 87 | */ |
||
| 88 | 84 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $tx, $nInput, TransactionOutputInterface $txOut) |
|
| 89 | { |
||
| 90 | 84 | $this->ecAdapter = $ecAdapter; |
|
| 91 | 84 | $this->tx = $tx; |
|
| 92 | 84 | $this->nInput = $nInput; |
|
| 93 | 84 | $this->txOut = $txOut; |
|
| 94 | 84 | $this->classifier = new OutputClassifier(); |
|
| 95 | 84 | $this->publicKeys = []; |
|
| 96 | 84 | $this->signatures = []; |
|
| 97 | |||
| 98 | 84 | $this->extractSignatures(); |
|
| 99 | 84 | } |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param int $sigVersion |
||
| 103 | * @param TransactionSignatureInterface[] $stack |
||
| 104 | * @param ScriptInterface $scriptCode |
||
| 105 | * @return \SplObjectStorage |
||
| 106 | */ |
||
| 107 | 24 | private function sortMultiSigs($sigVersion, $stack, ScriptInterface $scriptCode) |
|
| 108 | { |
||
| 109 | 24 | if ($sigVersion === 1) { |
|
| 110 | 12 | $hasher = new V1Hasher($this->tx, $this->txOut->getValue()); |
|
| 111 | 4 | } else { |
|
| 112 | 12 | $hasher = new Hasher($this->tx); |
|
| 113 | } |
||
| 114 | |||
| 115 | 24 | $sigSort = new SignatureSort($this->ecAdapter); |
|
| 116 | 24 | $sigs = new \SplObjectStorage; |
|
| 117 | |||
| 118 | 24 | foreach ($stack as $txSig) { |
|
| 119 | $hash = $hasher->calculate($scriptCode, $this->nInput, $txSig->getHashType()); |
||
| 120 | $linked = $sigSort->link([$txSig->getSignature()], $this->publicKeys, $hash); |
||
| 121 | |||
| 122 | foreach ($this->publicKeys as $key) { |
||
| 123 | if ($linked->contains($key)) { |
||
| 124 | $sigs[$key] = $txSig; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | 8 | } |
|
| 128 | |||
| 129 | 24 | return $sigs; |
|
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $type |
||
| 134 | * @param ScriptInterface $scriptCode |
||
| 135 | * @param BufferInterface[] $stack |
||
| 136 | * @param int $sigVersion |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | 72 | public function extractFromValues($type, ScriptInterface $scriptCode, array $stack, $sigVersion) |
|
| 140 | { |
||
| 141 | 72 | $size = count($stack); |
|
| 142 | 72 | if ($type === OutputClassifier::PAYTOPUBKEYHASH) { |
|
| 143 | 30 | $this->requiredSigs = 1; |
|
| 144 | 30 | if ($size === 2) { |
|
| 145 | 12 | $this->signatures = [TransactionSignatureFactory::fromHex($stack[0], $this->ecAdapter)]; |
|
| 146 | 12 | $this->publicKeys = [PublicKeyFactory::fromHex($stack[1], $this->ecAdapter)]; |
|
| 147 | 4 | } |
|
| 148 | 10 | } |
|
| 149 | |||
| 150 | 72 | if ($type === OutputClassifier::PAYTOPUBKEY) { |
|
| 151 | 12 | $this->requiredSigs = 1; |
|
| 152 | 12 | if ($size === 1) { |
|
| 153 | 6 | $this->signatures = [TransactionSignatureFactory::fromHex($stack[0], $this->ecAdapter)]; |
|
| 154 | 2 | } |
|
| 155 | 4 | } |
|
| 156 | |||
| 157 | 72 | if ($type === OutputClassifier::MULTISIG) { |
|
| 158 | 24 | $info = new Multisig($scriptCode); |
|
| 159 | 24 | $this->requiredSigs = $info->getRequiredSigCount(); |
|
| 160 | 24 | $this->publicKeys = $info->getKeys(); |
|
| 161 | |||
| 162 | 24 | if ($size > 1) { |
|
| 163 | 24 | $vars = []; |
|
| 164 | 24 | foreach (array_slice($stack, 1, -1) as $sig) { |
|
| 165 | $vars[] = TransactionSignatureFactory::fromHex($sig, $this->ecAdapter); |
||
| 166 | 8 | } |
|
| 167 | |||
| 168 | 24 | $sigs = $this->sortMultiSigs($sigVersion, $vars, $scriptCode); |
|
| 169 | |||
| 170 | 24 | foreach ($this->publicKeys as $idx => $key) { |
|
| 171 | 24 | $this->signatures[$idx] = isset($sigs[$key]) ? $sigs[$key]->getBuffer() : null; |
|
| 172 | 8 | } |
|
| 173 | 8 | } |
|
| 174 | 8 | } |
|
| 175 | |||
| 176 | 72 | return $type; |
|
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | 84 | public function extractSignatures() |
|
| 183 | { |
||
| 184 | 84 | $scriptPubKey = $this->txOut->getScript(); |
|
| 185 | 84 | $scriptSig = $this->tx->getInput($this->nInput)->getScript(); |
|
| 186 | 84 | $type = $this->classifier->classify($scriptPubKey); |
|
| 187 | 84 | if ($type === OutputClassifier::PAYTOPUBKEYHASH || $type === OutputClassifier::PAYTOPUBKEY || $type === OutputClassifier::MULTISIG) { |
|
| 188 | 42 | $values = []; |
|
| 189 | 42 | foreach ($scriptSig->getScriptParser()->decode() as $o) { |
|
| 190 | 18 | $values[] = $o->getData(); |
|
| 191 | 14 | } |
|
| 192 | |||
| 193 | 42 | $this->extractFromValues($type, $scriptPubKey, $values, 0); |
|
| 194 | 26 | } |
|
| 195 | |||
| 196 | 84 | if ($type === OutputClassifier::PAYTOSCRIPTHASH) { |
|
| 197 | 24 | $decodeSig = $scriptSig->getScriptParser()->decode(); |
|
| 198 | 24 | if (count($decodeSig) > 0) { |
|
| 199 | 18 | $redeemScript = new Script(end($decodeSig)->getData()); |
|
| 200 | 18 | $type = $this->classifier->classify($redeemScript); |
|
| 201 | 18 | if (count($decodeSig) > 1) { |
|
| 202 | 6 | $decodeSig = array_slice($decodeSig, 0, -1); |
|
| 203 | 2 | } |
|
| 204 | |||
| 205 | 18 | $internalSig = []; |
|
| 206 | 18 | foreach ($decodeSig as $operation) { |
|
| 207 | 18 | $internalSig[] = $operation->getData(); |
|
| 208 | 6 | } |
|
| 209 | |||
| 210 | 18 | $this->redeemScript = $redeemScript; |
|
| 211 | 18 | $this->extractFromValues($type, $redeemScript, $internalSig, 0); |
|
| 212 | 6 | } |
|
| 213 | 8 | } |
|
| 214 | |||
| 215 | 84 | $witnesses = $this->tx->getWitnesses(); |
|
| 216 | 84 | if ($type === OutputClassifier::WITNESS_V0_KEYHASH) { |
|
| 217 | 12 | $this->requiredSigs = 1; |
|
| 218 | 12 | if (isset($witnesses[$this->nInput])) { |
|
| 219 | 12 | $witness = $witnesses[$this->nInput]; |
|
| 220 | 12 | $this->signatures = [TransactionSignatureFactory::fromHex($witness[0], $this->ecAdapter)]; |
|
| 221 | 12 | $this->publicKeys = [PublicKeyFactory::fromHex($witness[1], $this->ecAdapter)]; |
|
| 222 | 4 | } |
|
| 223 | 80 | } else if ($type === OutputClassifier::WITNESS_V0_SCRIPTHASH) { |
|
| 224 | 18 | if (isset($witnesses[$this->nInput])) { |
|
| 225 | 18 | $witness = $witnesses[$this->nInput]; |
|
| 226 | 18 | $witCount = count($witnesses[$this->nInput]); |
|
| 227 | 18 | if ($witCount > 0) { |
|
| 228 | 18 | $witnessScript = new Script($witness[$witCount - 1]); |
|
| 229 | 18 | $vWitness = $witness->all(); |
|
| 230 | 18 | if (count($vWitness) > 1) { |
|
| 231 | 18 | $vWitness = array_slice($witness->all(), 0, -1); |
|
| 232 | 6 | } |
|
| 233 | |||
| 234 | 18 | $type = $this->classifier->classify($witnessScript); |
|
| 235 | 18 | $this->extractFromValues($type, $witnessScript, $vWitness, 1); |
|
| 236 | 18 | $this->witnessScript = $witnessScript; |
|
| 237 | 6 | } |
|
| 238 | 6 | } |
|
| 239 | 6 | } |
|
| 240 | |||
| 241 | 84 | return $this; |
|
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param ScriptInterface $scriptCode |
||
| 246 | * @param int $sigHashType |
||
| 247 | * @param int $sigVersion |
||
| 248 | * @return BufferInterface |
||
| 249 | */ |
||
| 250 | 84 | public function calculateSigHash(ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
| 251 | { |
||
| 252 | 84 | if ($sigVersion === 1) { |
|
| 253 | 30 | $hasher = new V1Hasher($this->tx, $this->txOut->getValue()); |
|
| 254 | 10 | } else { |
|
| 255 | 54 | $hasher = new Hasher($this->tx); |
|
| 256 | } |
||
| 257 | |||
| 258 | 84 | return $hasher->calculate($scriptCode, $this->nInput, $sigHashType); |
|
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param PrivateKeyInterface $key |
||
| 263 | * @param ScriptInterface $scriptCode |
||
| 264 | * @param int $sigHashType |
||
| 265 | * @param int $sigVersion |
||
| 266 | * @return TransactionSignature |
||
| 267 | */ |
||
| 268 | 84 | public function calculateSignature(PrivateKeyInterface $key, ScriptInterface $scriptCode, $sigHashType, $sigVersion) |
|
| 269 | { |
||
| 270 | 84 | $hash = $this->calculateSigHash($scriptCode, $sigHashType, $sigVersion); |
|
| 271 | 84 | return new TransactionSignature( |
|
| 272 | 84 | $this->ecAdapter, |
|
| 273 | 84 | $this->ecAdapter->sign( |
|
| 274 | 28 | $hash, |
|
| 275 | 28 | $key, |
|
| 276 | 84 | new Rfc6979( |
|
| 277 | 84 | $this->ecAdapter, |
|
| 278 | 28 | $key, |
|
| 279 | 28 | $hash, |
|
| 280 | 56 | 'sha256' |
|
| 281 | 28 | ) |
|
| 282 | 28 | ), |
|
| 283 | $sigHashType |
||
| 284 | 28 | ); |
|
| 285 | } |
||
| 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 $sigHashType |
||
| 303 | * @param int $sigVersion |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | 84 | private function doSignature(PrivateKeyInterface $key, ScriptInterface $scriptPubKey, &$outputType, array &$results, $sigHashType, $sigVersion = 0) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * @param PrivateKeyInterface $key |
||
| 388 | * @param ScriptInterface|null $redeemScript |
||
| 389 | * @param ScriptInterface|null $witnessScript |
||
| 390 | * @param int $sigHashType |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | 84 | public function sign(PrivateKeyInterface $key, ScriptInterface $redeemScript = null, ScriptInterface $witnessScript = null, $sigHashType = SigHashInterface::ALL) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * @param string $outputType |
||
| 452 | * @param $answer |
||
| 453 | * @return bool |
||
| 454 | */ |
||
| 455 | 84 | private function serializeSimpleSig($outputType, &$answer) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * @return SigValues |
||
| 489 | */ |
||
| 490 | 84 | 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: