forked from Bit-Wasp/bitcoin-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransaction.php
338 lines (293 loc) · 7.88 KB
/
Transaction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
declare(strict_types=1);
namespace BitWasp\Bitcoin\Transaction;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\Hash;
use BitWasp\Bitcoin\Script\ScriptWitnessInterface;
use BitWasp\Bitcoin\Serializable;
use BitWasp\Bitcoin\Serializer\Transaction\TransactionSerializer;
use BitWasp\Bitcoin\Util\IntRange;
use BitWasp\Bitcoin\Utxo\Utxo;
use BitWasp\Buffertools\BufferInterface;
class Transaction extends Serializable implements TransactionInterface
{
/**
* @var int
*/
private $version;
/**
* @var TransactionInputInterface[]
*/
private $inputs;
/**
* @var TransactionOutputInterface[]
*/
private $outputs;
/**
* @var ScriptWitnessInterface[]
*/
private $witness;
/**
* @var int
*/
private $lockTime;
/**
* @var BufferInterface
*/
private $wtxid;
/**
* @var BufferInterface
*/
private $hash;
/**
* Transaction constructor.
*
* @param int $nVersion
* @param TransactionInputInterface[] $vin
* @param TransactionOutputInterface[] $vout
* @param ScriptWitnessInterface[] $vwit
* @param int $nLockTime
*/
public function __construct(
int $nVersion = TransactionInterface::DEFAULT_VERSION,
array $vin = [],
array $vout = [],
array $vwit = [],
int $nLockTime = 0
) {
if ($nVersion < IntRange::I32_MIN || $nVersion > IntRange::I32_MAX) {
throw new \InvalidArgumentException('Transaction version is outside valid range');
}
if ($nLockTime < 0 || $nLockTime > TransactionInterface::MAX_LOCKTIME) {
throw new \InvalidArgumentException('Locktime must be positive and less than ' . TransactionInterface::MAX_LOCKTIME);
}
$this->version = $nVersion;
$this->lockTime = $nLockTime;
$this->inputs = array_map(function (TransactionInputInterface $input) {
return $input;
}, $vin);
$this->outputs = array_map(function (TransactionOutputInterface $output) {
return $output;
}, $vout);
$this->witness = array_map(function (ScriptWitnessInterface $scriptWitness) {
return $scriptWitness;
}, $vwit);
}
/**
* @return BufferInterface
*/
public function getTxHash(): BufferInterface
{
if (null === $this->hash) {
$this->hash = Hash::sha256d($this->getBaseSerialization());
}
return $this->hash;
}
/**
* @return BufferInterface
*/
public function getTxId(): BufferInterface
{
return $this->getTxHash()->flip();
}
/**
* @return BufferInterface
*/
public function getWitnessTxId(): BufferInterface
{
if (null === $this->wtxid) {
$this->wtxid = Hash::sha256d($this->getBuffer())->flip();
}
return $this->wtxid;
}
/**
* @return int
*/
public function getVersion(): int
{
return $this->version;
}
/**
* Get the array of inputs in the transaction
*
* @return TransactionInputInterface[]
*/
public function getInputs(): array
{
return $this->inputs;
}
/**
* @param int $index
* @return TransactionInputInterface
*/
public function getInput(int $index): TransactionInputInterface
{
if (!isset($this->inputs[$index])) {
throw new \RuntimeException('No input at this index');
}
return $this->inputs[$index];
}
/**
* Get Outputs
*
* @return TransactionOutputInterface[]
*/
public function getOutputs(): array
{
return $this->outputs;
}
/**
* @param int $vout
* @return TransactionOutputInterface
*/
public function getOutput(int $vout): TransactionOutputInterface
{
if (!isset($this->outputs[$vout])) {
throw new \RuntimeException('No output at this index');
}
return $this->outputs[$vout];
}
/**
* @return bool
*/
public function hasWitness(): bool
{
for ($l = count($this->inputs), $i = 0; $i < $l; $i++) {
if (isset($this->witness[$i]) && count($this->witness[$i]) > 0) {
return true;
}
}
return false;
}
/**
* @return ScriptWitnessInterface[]
*/
public function getWitnesses(): array
{
return $this->witness;
}
/**
* @param int $index
* @return ScriptWitnessInterface
*/
public function getWitness(int $index): ScriptWitnessInterface
{
if (!isset($this->witness[$index])) {
throw new \RuntimeException('No witness at this index');
}
return $this->witness[$index];
}
/**
* @param int $vout
* @return OutPointInterface
*/
public function makeOutpoint(int $vout): OutPointInterface
{
$this->getOutput($vout);
return new OutPoint($this->getTxId(), $vout);
}
/**
* @param int $vout
* @return Utxo
*/
public function makeUtxo(int $vout): Utxo
{
return new Utxo(new OutPoint($this->getTxId(), $vout), $this->getOutput($vout));
}
/**
* Get Lock Time
*
* @return int
*/
public function getLockTime(): int
{
return $this->lockTime;
}
/**
* @return int
*/
public function getValueOut(): int
{
$value = 0;
foreach ($this->outputs as $output) {
$value = $value + $output->getValue();
}
return $value;
}
/**
* @return bool
*/
public function isCoinbase(): bool
{
return count($this->inputs) === 1 && $this->getInput(0)->isCoinBase();
}
/**
* @param TransactionInterface $tx
* @return bool
*/
public function equals(TransactionInterface $tx): bool
{
$version = gmp_cmp($this->version, $tx->getVersion());
if ($version !== 0) {
return false;
}
$nIn = count($this->inputs);
$nOut = count($this->outputs);
$nWit = count($this->witness);
// Check the length of each field is equal
if ($nIn !== count($tx->getInputs()) || $nOut !== count($tx->getOutputs()) || $nWit !== count($tx->getWitnesses())) {
return false;
}
// Check each field
for ($i = 0; $i < $nIn; $i++) {
if (false === $this->getInput($i)->equals($tx->getInput($i))) {
return false;
}
}
for ($i = 0; $i < $nOut; $i++) {
if (false === $this->getOutput($i)->equals($tx->getOutput($i))) {
return false;
}
}
for ($i = 0; $i < $nWit; $i++) {
if (false === $this->getWitness($i)->equals($tx->getWitness($i))) {
return false;
}
}
return gmp_cmp($this->lockTime, $tx->getLockTime()) === 0;
}
/**
* @return BufferInterface
*/
public function getBuffer(): BufferInterface
{
return (new TransactionSerializer())->serialize($this);
}
/**
* @return BufferInterface
*/
public function getBaseSerialization(): BufferInterface
{
return (new TransactionSerializer())->serialize($this, TransactionSerializer::NO_WITNESS);
}
/**
* @return BufferInterface
*/
public function getWitnessSerialization(): BufferInterface
{
if (!$this->hasWitness()) {
throw new \RuntimeException('Cannot get witness serialization for transaction without witnesses');
}
return $this->getBuffer();
}
/**
* {@inheritdoc}
* @see TransactionInterface::getWitnessBuffer()
* @see TransactionInterface::getWitnessSerialization()
* @deprecated
*/
public function getWitnessBuffer(): BufferInterface
{
return $this->getWitnessSerialization();
}
}