1 | <?php |
||
12 | class TxSigHashSerializer |
||
13 | { |
||
14 | /** |
||
15 | * @var TransactionInterface |
||
16 | */ |
||
17 | private $tx; |
||
18 | |||
19 | /** |
||
20 | * @var ScriptInterface |
||
21 | */ |
||
22 | private $scriptCode; |
||
23 | |||
24 | /** |
||
25 | * @var |
||
26 | */ |
||
27 | private $nIn; |
||
28 | |||
29 | /** |
||
30 | * @var \BitWasp\Buffertools\Types\VarInt |
||
31 | */ |
||
32 | private $varint; |
||
33 | |||
34 | /** |
||
35 | * @var \BitWasp\Buffertools\Types\ByteString |
||
36 | */ |
||
37 | private $bs32le; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | private $anyoneCanPay = false; |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | private $hashSingle = false; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | private $hashNone = false; |
||
53 | |||
54 | /** |
||
55 | * @param TransactionInterface $tx |
||
56 | * @param ScriptInterface $scriptCode |
||
57 | * @param int $nIn |
||
58 | * @param int $nHashTypeIn |
||
59 | */ |
||
60 | 195 | public function __construct(TransactionInterface $tx, ScriptInterface $scriptCode, int $nIn, int $nHashTypeIn) |
|
74 | |||
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | 195 | private function serializeScript(): string |
|
109 | |||
110 | /** |
||
111 | * @param int $nInput |
||
112 | * @return string |
||
113 | */ |
||
114 | 195 | public function serializeInput(int $nInput): string |
|
115 | { |
||
116 | 195 | if ($this->anyoneCanPay) { |
|
117 | 4 | $nInput = $this->nIn; |
|
118 | } |
||
119 | |||
120 | 195 | $txIn = $this->tx->getInput($nInput); |
|
121 | 195 | $outpoint = $txIn->getOutPoint(); |
|
122 | 195 | $out = $this->bs32le->write($outpoint->getTxId()) . pack('V', $outpoint->getVout()); |
|
123 | |||
124 | 195 | if ($nInput !== $this->nIn) { |
|
125 | // script length is zero |
||
126 | $out .= "\x00"; |
||
127 | } else { |
||
128 | 195 | $out .= $this->serializeScript(); |
|
129 | } |
||
130 | |||
131 | 195 | if ($nInput !== $this->nIn && ($this->hashSingle || $this->hashNone)) { |
|
132 | $out .= pack('V', 0); |
||
133 | } else { |
||
134 | 195 | $out .= pack('V', $txIn->getSequence()); |
|
135 | } |
||
136 | |||
137 | 195 | return $out; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param int $nOutput |
||
142 | * @return string |
||
143 | */ |
||
144 | 192 | public function serializeOutput(int $nOutput): string |
|
145 | { |
||
146 | 192 | if ($this->hashSingle && $nOutput != $this->nIn) { |
|
147 | $out = pack('P', -1) . "\x00"; |
||
148 | } else { |
||
149 | 192 | $txOut = $this->tx->getOutput($nOutput); |
|
150 | 192 | $scriptBuf = $txOut->getScript()->getBuffer(); |
|
151 | 192 | $out = pack('P', $txOut->getValue()) . $this->varint->write($scriptBuf->getSize()) . $scriptBuf->getBinary(); |
|
152 | } |
||
153 | |||
154 | 192 | return $out; |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * @return string |
||
159 | */ |
||
160 | 195 | public function serializeTransaction(): string |
|
179 | } |
||
180 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.