1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Transaction; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
6
|
|
|
use BitWasp\Bitcoin\Collection\Transaction\TransactionInputCollection; |
7
|
|
|
use BitWasp\Bitcoin\Collection\Transaction\TransactionOutputCollection; |
8
|
|
|
use BitWasp\Bitcoin\Collection\Transaction\TransactionWitnessCollection; |
9
|
|
|
use BitWasp\Bitcoin\Crypto\Hash; |
10
|
|
|
use BitWasp\Bitcoin\Script\ScriptWitnessInterface; |
11
|
|
|
use BitWasp\Bitcoin\Serializable; |
12
|
|
|
use BitWasp\Bitcoin\Serializer\Transaction\OldTransactionSerializer; |
13
|
|
|
use BitWasp\Bitcoin\Serializer\Transaction\TransactionSerializer; |
14
|
|
|
use BitWasp\Bitcoin\Transaction\SignatureHash\Hasher; |
15
|
|
|
use BitWasp\Bitcoin\Utxo\Utxo; |
16
|
|
|
use BitWasp\Buffertools\BufferInterface; |
17
|
|
|
use BitWasp\CommonTrait\FunctionAliasArrayAccess; |
18
|
|
|
|
19
|
|
|
class Transaction extends Serializable implements TransactionInterface |
20
|
|
|
{ |
21
|
|
|
use FunctionAliasArrayAccess; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int|string |
25
|
|
|
*/ |
26
|
|
|
private $version; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TransactionInputCollection |
30
|
|
|
*/ |
31
|
|
|
private $inputs; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var TransactionOutputCollection |
35
|
|
|
*/ |
36
|
|
|
private $outputs; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var TransactionWitnessCollection |
40
|
|
|
*/ |
41
|
|
|
private $witness; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int|string |
45
|
|
|
*/ |
46
|
|
|
private $lockTime; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Transaction constructor. |
50
|
|
|
* |
51
|
|
|
* @param int $nVersion |
52
|
|
|
* @param TransactionInputCollection|null $inputs |
53
|
|
|
* @param TransactionOutputCollection|null $outputs |
54
|
|
|
* @param TransactionWitnessCollection|null $witness |
55
|
|
|
* @param int $nLockTime |
56
|
|
|
*/ |
57
|
1284 |
|
public function __construct( |
58
|
|
|
$nVersion = TransactionInterface::DEFAULT_VERSION, |
59
|
|
|
TransactionInputCollection $inputs = null, |
60
|
|
|
TransactionOutputCollection $outputs = null, |
61
|
|
|
TransactionWitnessCollection $witness = null, |
62
|
|
|
$nLockTime = 0 |
63
|
|
|
) { |
64
|
1284 |
|
$math = Bitcoin::getMath(); |
65
|
1284 |
|
if ($math->cmp($nVersion, TransactionInterface::MAX_VERSION) > 0) { |
66
|
6 |
|
throw new \InvalidArgumentException('Version must be less than ' . TransactionInterface::MAX_VERSION); |
67
|
|
|
} |
68
|
|
|
|
69
|
1278 |
|
if ($math->cmp($nLockTime, 0) < 0 || $math->cmp($nLockTime, TransactionInterface::MAX_LOCKTIME) > 0) { |
70
|
6 |
|
throw new \InvalidArgumentException('Locktime must be positive and less than ' . TransactionInterface::MAX_LOCKTIME); |
71
|
|
|
} |
72
|
|
|
|
73
|
1272 |
|
$this->version = $nVersion; |
74
|
1272 |
|
$this->inputs = $inputs ?: new TransactionInputCollection(); |
75
|
1272 |
|
$this->outputs = $outputs ?: new TransactionOutputCollection(); |
76
|
1272 |
|
$this->witness = $witness ?: new TransactionWitnessCollection(); |
77
|
1272 |
|
$this->lockTime = $nLockTime; |
78
|
|
|
|
79
|
1272 |
|
$this |
80
|
1272 |
|
->initFunctionAlias('version', 'getVersion') |
81
|
1272 |
|
->initFunctionAlias('inputs', 'getInputs') |
82
|
1272 |
|
->initFunctionAlias('outputs', 'getOutputs') |
83
|
1272 |
|
->initFunctionAlias('locktime', 'getLockTime'); |
84
|
1272 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return Transaction |
88
|
|
|
*/ |
89
|
120 |
|
public function __clone() |
90
|
|
|
{ |
91
|
120 |
|
$this->inputs = clone $this->inputs; |
92
|
120 |
|
$this->outputs = clone $this->outputs; |
93
|
120 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return BufferInterface |
97
|
|
|
*/ |
98
|
132 |
|
public function getTxHash() |
99
|
|
|
{ |
100
|
132 |
|
return Hash::sha256d($this->getBuffer()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return BufferInterface |
105
|
|
|
*/ |
106
|
132 |
|
public function getTxId() |
107
|
|
|
{ |
108
|
132 |
|
return $this->getTxHash()->flip(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return \BitWasp\Buffertools\BufferInterface |
113
|
|
|
*/ |
114
|
|
|
public function getWitnessTxId() |
115
|
|
|
{ |
116
|
|
|
return Hash::sha256d($this->getWitnessBuffer())->flip(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return int|string |
121
|
|
|
*/ |
122
|
330 |
|
public function getVersion() |
123
|
|
|
{ |
124
|
330 |
|
return $this->version; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the array of inputs in the transaction |
129
|
|
|
* |
130
|
|
|
* @return TransactionInputCollection |
131
|
|
|
*/ |
132
|
1026 |
|
public function getInputs() |
133
|
|
|
{ |
134
|
1026 |
|
return $this->inputs; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param int $index |
139
|
|
|
* @return TransactionInputInterface |
140
|
|
|
*/ |
141
|
108 |
|
public function getInput($index) |
142
|
|
|
{ |
143
|
108 |
|
return $this->inputs[$index]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get Outputs |
148
|
|
|
* |
149
|
|
|
* @return TransactionOutputCollection |
150
|
|
|
*/ |
151
|
336 |
|
public function getOutputs() |
152
|
|
|
{ |
153
|
336 |
|
return $this->outputs; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param int $vout |
158
|
|
|
* @return TransactionOutputInterface |
159
|
|
|
*/ |
160
|
102 |
|
public function getOutput($vout) |
161
|
|
|
{ |
162
|
102 |
|
return $this->outputs[$vout]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return TransactionWitnessCollection |
167
|
|
|
*/ |
168
|
102 |
|
public function getWitnesses() |
169
|
|
|
{ |
170
|
102 |
|
return $this->witness; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return ScriptWitnessInterface |
175
|
|
|
*/ |
176
|
|
|
public function getWitness($index) |
177
|
|
|
{ |
178
|
|
|
return $this->witness[$index]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param int $vout |
183
|
|
|
* @return OutPointInterface |
184
|
|
|
*/ |
185
|
24 |
|
public function makeOutpoint($vout) |
186
|
|
|
{ |
187
|
24 |
|
$this->getOutput($vout); |
|
|
|
|
188
|
24 |
|
return new OutPoint($this->getTxId(), $vout); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param int $vout |
193
|
|
|
* @return Utxo |
194
|
|
|
*/ |
195
|
|
|
public function makeUtxo($vout) |
196
|
|
|
{ |
197
|
|
|
$output = $this->getOutput($vout); |
198
|
|
|
|
199
|
|
|
return new Utxo( |
200
|
|
|
new OutPoint($this->getTxId(), $vout), |
201
|
|
|
$output |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get Lock Time |
207
|
|
|
* |
208
|
|
|
* @return int|string |
209
|
|
|
*/ |
210
|
336 |
|
public function getLockTime() |
211
|
|
|
{ |
212
|
336 |
|
return $this->lockTime; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return \BitWasp\Bitcoin\Transaction\SignatureHash\SigHash |
217
|
|
|
*/ |
218
|
6 |
|
public function getSignatureHash() |
219
|
|
|
{ |
220
|
6 |
|
return new Hasher($this); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return int|string |
225
|
|
|
*/ |
226
|
6 |
|
public function getValueOut() |
227
|
|
|
{ |
228
|
6 |
|
$math = Bitcoin::getMath(); |
229
|
6 |
|
$value = 0; |
230
|
6 |
|
foreach ($this->outputs as $output) { |
231
|
6 |
|
$value = $math->add($value, $output->getValue()); |
232
|
6 |
|
} |
233
|
|
|
|
234
|
6 |
|
return $value; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
6 |
|
public function isCoinbase() |
241
|
|
|
{ |
242
|
6 |
|
return count($this->inputs) === 1 && $this->getInput(0)->isCoinBase(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param TransactionInterface $tx |
247
|
|
|
* @return bool |
248
|
54 |
|
*/ |
249
|
|
|
public function equals(TransactionInterface $tx) |
250
|
54 |
|
{ |
251
|
|
|
$version = gmp_cmp($this->version, $tx->getVersion()); |
252
|
|
|
if ($version !== 0) { |
253
|
|
|
return false; |
254
|
|
|
} |
255
|
|
|
|
256
|
264 |
|
$nIn = count($this->inputs); |
257
|
|
|
$nOut = count($this->outputs); |
258
|
264 |
|
$nWit = count($this->witness); |
259
|
|
|
if ($nIn !== count($tx->getInputs()) || $nOut !== count($tx->getOutputs()) || $nWit !== count($tx->getWitnesses())) { |
260
|
|
|
return false; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
for ($i = 0; $i < $nIn; $i++) { |
264
|
54 |
|
if (false === $this->getInput($i)->equals($tx->getInput($i))) { |
265
|
|
|
return false; |
266
|
54 |
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
for ($i = 0; $i < $nOut; $i++) { |
270
|
|
|
if (false === $this->getOutput($i)->equals($tx->getOutput($i))) { |
271
|
|
|
return false; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
for ($i = 0; $i < $nWit; $i++) { |
276
|
|
|
if (false === $this->getWitness($i)->equals($tx->getWitness($i))) { |
277
|
|
|
return false; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return gmp_cmp($this->lockTime, $tx->getLockTime()) === 0; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @return Validator |
286
|
|
|
*/ |
287
|
|
|
public function validator() |
288
|
|
|
{ |
289
|
|
|
return new Validator($this); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return BufferInterface |
294
|
|
|
*/ |
295
|
|
|
public function getBuffer() |
296
|
|
|
{ |
297
|
|
|
return (new OldTransactionSerializer())->serialize($this); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @return BufferInterface |
302
|
|
|
*/ |
303
|
|
|
public function getWitnessBuffer() |
304
|
|
|
{ |
305
|
|
|
return (new TransactionSerializer())->serialize($this); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: