Bit-Wasp /
bitcoin-php
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace BitWasp\Bitcoin\Transaction\Factory; |
||
| 4 | |||
| 5 | use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
||
| 6 | use BitWasp\Bitcoin\Key\PublicKeyFactory; |
||
| 7 | use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface; |
||
| 8 | use BitWasp\Bitcoin\Script\Classifier\OutputClassifier; |
||
| 9 | use BitWasp\Bitcoin\Script\ScriptFactory; |
||
| 10 | use BitWasp\Bitcoin\Script\ScriptInfo\ScriptHash; |
||
| 11 | use BitWasp\Bitcoin\Script\ScriptInterface; |
||
| 12 | use BitWasp\Bitcoin\Signature\SignatureSort; |
||
| 13 | use BitWasp\Bitcoin\Signature\TransactionSignatureInterface; |
||
| 14 | use BitWasp\Bitcoin\Signature\TransactionSignatureFactory; |
||
| 15 | use BitWasp\Bitcoin\Transaction\TransactionInterface; |
||
| 16 | |||
| 17 | class TxSignerContext |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var \BitWasp\Bitcoin\Script\ScriptInfo\ScriptInfoInterface |
||
| 21 | */ |
||
| 22 | private $scriptInfo; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var null|ScriptInterface |
||
| 26 | */ |
||
| 27 | private $redeemScript; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var ScriptInterface |
||
| 31 | */ |
||
| 32 | private $prevOutScript; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $prevOutType; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $scriptType; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var TransactionSignatureInterface[] |
||
| 46 | */ |
||
| 47 | private $signatures = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var PublicKeyInterface[] |
||
| 51 | */ |
||
| 52 | private $publicKeys = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var EcAdapterInterface |
||
| 56 | */ |
||
| 57 | private $ecAdapter; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * TxSignerContext constructor. |
||
| 61 | * @param EcAdapterInterface $ecAdapter |
||
| 62 | * @param ScriptInterface $outputScript |
||
| 63 | * @param ScriptInterface|null $redeemScript |
||
| 64 | 130 | */ |
|
| 65 | public function __construct( |
||
| 66 | 6 | EcAdapterInterface $ecAdapter, |
|
| 67 | ScriptInterface $outputScript, |
||
| 68 | ScriptInterface $redeemScript = null |
||
| 69 | 130 | ) { |
|
| 70 | 118 | $handler = ScriptFactory::info($outputScript, $redeemScript); |
|
| 71 | 118 | $handler->getKeys(); |
|
| 72 | 36 | $this->scriptType = $this->prevOutType = $handler->classification(); |
|
| 73 | 42 | if ($handler instanceof ScriptHash) { |
|
| 74 | $this->scriptType = $handler->getInfo()->classification(); |
||
| 75 | } |
||
| 76 | 118 | ||
| 77 | 118 | // Gather public keys from redeemScript / outputScript |
|
| 78 | 118 | $this->ecAdapter = $ecAdapter; |
|
| 79 | 118 | $this->redeemScript = $redeemScript; |
|
| 80 | $this->prevOutScript = $outputScript; |
||
| 81 | $this->scriptInfo = $handler; |
||
| 82 | 118 | ||
| 83 | 118 | // According to scriptType, extract public keys |
|
| 84 | $this->publicKeys = $this->scriptInfo->getKeys(); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @return ScriptInterface |
||
| 89 | 36 | * @throws \RuntimeException |
|
| 90 | */ |
||
| 91 | 36 | public function getRedeemScript() |
|
| 92 | 6 | { |
|
| 93 | if (null === $this->redeemScript) { |
||
| 94 | throw new \RuntimeException('No redeem script was set'); |
||
| 95 | 30 | } |
|
| 96 | |||
| 97 | return $this->redeemScript; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * |
||
| 102 | 106 | * @return string |
|
| 103 | */ |
||
| 104 | 106 | public function getPrevOutType() |
|
| 105 | { |
||
| 106 | return $this->prevOutType; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | 88 | * @return string |
|
| 111 | */ |
||
| 112 | 88 | public function getScriptType() |
|
| 113 | { |
||
| 114 | return $this->scriptType; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | 88 | * @return array|\BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface[] |
|
| 119 | */ |
||
| 120 | 88 | public function getPublicKeys() |
|
| 121 | { |
||
| 122 | return $this->publicKeys; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | 70 | * @return array |
|
| 127 | */ |
||
| 128 | 70 | public function getSignatures() |
|
| 129 | { |
||
| 130 | return $this->signatures; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param integer $idx |
||
| 135 | * @param TransactionSignatureInterface $signature |
||
| 136 | 82 | * @return $this |
|
| 137 | */ |
||
| 138 | 82 | public function setSignature($idx, TransactionSignatureInterface $signature = null) |
|
| 139 | 82 | { |
|
| 140 | $this->signatures[$idx] = $signature; |
||
| 141 | return $this; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param PublicKeyInterface[] $publicKeys |
||
| 146 | 40 | * @return $this |
|
| 147 | */ |
||
| 148 | 40 | public function setPublicKeys(array $publicKeys) |
|
| 149 | 40 | { |
|
| 150 | $this->publicKeys = $publicKeys; |
||
| 151 | return $this; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param TransactionInterface $tx |
||
| 156 | * @param int $inputToExtract |
||
| 157 | 82 | * @return $this |
|
| 158 | */ |
||
| 159 | 82 | public function extractSigs(TransactionInterface $tx, $inputToExtract) |
|
| 160 | 82 | { |
|
| 161 | 82 | $parsed = $tx->getInput($inputToExtract) |
|
| 162 | 82 | ->getScript() |
|
| 163 | ->getScriptParser() |
||
| 164 | 82 | ->decode(); |
|
| 165 | |||
| 166 | 82 | $size = count($parsed); |
|
| 167 | 82 | ||
| 168 | switch ($this->getScriptType()) { |
||
| 169 | 40 | case OutputClassifier::PAYTOPUBKEYHASH: |
|
| 170 | // Supply signature and public key in scriptSig |
||
| 171 | if ($size === 2) { |
||
| 172 | $this->signatures = [TransactionSignatureFactory::fromHex($parsed[0]->getData(), $this->ecAdapter)]; |
||
|
0 ignored issues
–
show
|
|||
| 173 | $this->publicKeys = [PublicKeyFactory::fromHex($parsed[1]->getData(), $this->ecAdapter)]; |
||
| 174 | 40 | } |
|
| 175 | 42 | ||
| 176 | break; |
||
| 177 | 18 | case OutputClassifier::PAYTOPUBKEY: |
|
| 178 | // Only has a signature in the scriptSig |
||
| 179 | if ($size === 1) { |
||
| 180 | $this->signatures = [TransactionSignatureFactory::fromHex($parsed[0]->getData(), $this->ecAdapter)]; |
||
|
0 ignored issues
–
show
$parsed[0]->getData() is of type object<BitWasp\Buffertools\BufferInterface>, but the function expects a object<BitWasp\Buffertools\Buffer>|string.
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: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 181 | 18 | } |
|
| 182 | 24 | ||
| 183 | 24 | break; |
|
| 184 | 24 | case OutputClassifier::MULTISIG: |
|
| 185 | $redeemScript = $this->getRedeemScript(); |
||
| 186 | 24 | $this->signatures = array_fill(0, count($this->publicKeys), null); |
|
| 187 | 6 | ||
| 188 | 6 | if ($size > 2 && $size <= $this->scriptInfo->getKeyCount() + 2) { |
|
| 189 | 6 | $sigHash = $tx->getSignatureHash(); |
|
| 190 | $sigSort = new SignatureSort($this->ecAdapter); |
||
| 191 | 6 | $sigs = new \SplObjectStorage; |
|
| 192 | |||
| 193 | 6 | foreach (array_slice($parsed, 1, -1) as $item) { |
|
| 194 | 82 | /** @var \BitWasp\Bitcoin\Script\Parser\Operation $item */ |
|
| 195 | 6 | if ($item->isPush()) { |
|
| 196 | 6 | $txSig = TransactionSignatureFactory::fromHex($item->getData(), $this->ecAdapter); |
|
| 197 | $hash = $sigHash->calculate($redeemScript, $inputToExtract, $txSig->getHashType()); |
||
| 198 | 6 | $linked = $sigSort->link([$txSig->getSignature()], $this->publicKeys, $hash); |
|
| 199 | 6 | ||
| 200 | 6 | foreach ($this->publicKeys as $key) { |
|
| 201 | 6 | if ($linked->contains($key)) { |
|
| 202 | 6 | $sigs[$key] = $txSig; |
|
| 203 | 6 | } |
|
| 204 | 6 | } |
|
| 205 | } |
||
| 206 | } |
||
| 207 | 6 | ||
| 208 | 6 | // We have all the signatures from the input now. array_shift the sigs for a public key, as it's encountered. |
|
| 209 | 6 | foreach ($this->publicKeys as $idx => $key) { |
|
| 210 | 6 | $this->signatures[$idx] = isset($sigs[$key]) ? $sigs[$key] : null; |
|
| 211 | } |
||
| 212 | 24 | } |
|
| 213 | 82 | ||
| 214 | break; |
||
| 215 | 82 | } |
|
| 216 | |||
| 217 | return $this; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | 64 | * @return \BitWasp\Bitcoin\Script\Script |
|
| 222 | */ |
||
| 223 | 64 | public function regenerateScript() |
|
| 224 | 64 | { |
|
| 225 | $signatures = array_filter($this->getSignatures()); |
||
| 226 | return $this->scriptInfo->makeScriptSig($signatures, $this->publicKeys); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | 48 | * @return int |
|
| 231 | */ |
||
| 232 | 48 | public function getRequiredSigCount() |
|
| 233 | { |
||
| 234 | return $this->scriptInfo->getRequiredSigCount(); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | 54 | * @return int |
|
| 239 | */ |
||
| 240 | 54 | public function getSigCount() |
|
| 241 | { |
||
| 242 | return count(array_filter($this->signatures)); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | 30 | * @return bool |
|
| 247 | */ |
||
| 248 | public function isFullySigned() |
||
| 249 | 30 | { |
|
| 250 | // Compare the number of signatures with the required sig count |
||
| 251 | return $this->getSigCount() === $this->getRequiredSigCount(); |
||
| 252 | } |
||
| 253 | } |
||
| 254 |
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: