Completed
Pull Request — master (#403)
by thomas
86:27 queued 84:05
created

Transaction::validator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Crypto\Hash;
7
use BitWasp\Bitcoin\Script\ScriptWitnessInterface;
8
use BitWasp\Bitcoin\Serializable;
9
use BitWasp\Bitcoin\Serializer\Transaction\OldTransactionSerializer;
10
use BitWasp\Bitcoin\Serializer\Transaction\TransactionSerializer;
11
use BitWasp\Bitcoin\Utxo\Utxo;
12
use BitWasp\Buffertools\BufferInterface;
13
14
class Transaction extends Serializable implements TransactionInterface
15
{
16
    /**
17
     * @var int
18
     */
19
    private $version;
20
21
    /**
22
     * @var TransactionInputInterface[]
23
     */
24
    private $inputs;
25
26
    /**
27
     * @var TransactionOutputInterface[]
28
     */
29
    private $outputs;
30
31
    /**
32
     * @var ScriptWitnessInterface[]
33
     */
34
    private $witness;
35
36
    /**
37
     * @var int
38
     */
39
    private $lockTime;
40
41
    /**
42
     * Transaction constructor.
43
     *
44
     * @param int $nVersion
45
     * @param TransactionInputInterface[] $vin
46
     * @param TransactionOutputInterface[] $vout
47
     * @param ScriptWitnessInterface[] $vwit
48
     * @param int $nLockTime
49
     */
50 5144
    public function __construct(
51
        $nVersion = TransactionInterface::DEFAULT_VERSION,
52
        array $vin = [],
53
        array $vout = [],
54
        array $vwit = [],
55
        $nLockTime = 0
56
    ) {
57 5144
        if ($nVersion > TransactionInterface::MAX_VERSION) {
58 6
            throw new \InvalidArgumentException('Version must be less than ' . TransactionInterface::MAX_VERSION);
59
        }
60
61 5138
        if ($nLockTime < 0 || $nLockTime > TransactionInterface::MAX_LOCKTIME) {
62 6
            throw new \InvalidArgumentException('Locktime must be positive and less than ' . TransactionInterface::MAX_LOCKTIME);
63
        }
64
65 5132
        $this->version = $nVersion;
66 5132
        $this->lockTime = $nLockTime;
67
68
        $this->inputs = array_map(function (TransactionInputInterface $input) {
69 4928
            return $input;
70 5132
        }, $vin);
71
        $this->outputs = array_map(function (TransactionOutputInterface $output) {
72 4940
            return $output;
73 5132
        }, $vout);
74 5132
        $this->witness = array_map(function (ScriptWitnessInterface $scriptWitness) {
75 4616
            return $scriptWitness;
76 5132
        }, $vwit);
77 5132
    }
78
79
    /**
80
     * @return Transaction
81
     */
82 452
    public function __clone()
83
    {
84
        //$this->inputs = clone $this->inputs;
85
        //$this->outputs = clone $this->outputs;
86 452
    }
87
88
    /**
89
     * @return BufferInterface
90
     */
91 4508
    public function getTxHash()
92
    {
93 4508
        return Hash::sha256d($this->getBuffer());
94
    }
95
96
    /**
97
     * @return BufferInterface
98
     */
99 4508
    public function getTxId()
100
    {
101 4508
        return $this->getTxHash()->flip();
102
    }
103
104
    /**
105
     * @return BufferInterface
106
     */
107
    public function getWitnessTxId()
108
    {
109
        return Hash::sha256d($this->getWitnessBuffer())->flip();
110
    }
111
112
    /**
113
     * @return int
114
     */
115 4862
    public function getVersion()
116
    {
117 4862
        return $this->version;
118
    }
119
120
    /**
121
     * Get the array of inputs in the transaction
122
     *
123
     * @return TransactionInputInterface[]
124
     */
125 4904
    public function getInputs()
126
    {
127 4904
        return $this->inputs;
128
    }
129
130
    /**
131
     * @param int $index
132
     * @return TransactionInputInterface
133
     */
134 386
    public function getInput($index)
135
    {
136 386
        if (!isset($this->inputs[$index])) {
137 6
            throw new \RuntimeException('No input at this index');
138
        }
139 380
        return $this->inputs[$index];
140
    }
141
142
    /**
143
     * Get Outputs
144
     *
145
     * @return TransactionOutputInterface[]
146
     */
147 4886
    public function getOutputs()
148
    {
149 4886
        return $this->outputs;
150
    }
151
152
    /**
153
     * @param int $vout
154
     * @return TransactionOutputInterface
155
     */
156 4502
    public function getOutput($vout)
157
    {
158 4502
        if (!isset($this->outputs[$vout])) {
159 6
            throw new \RuntimeException('No output at this index');
160
        }
161 4496
        return $this->outputs[$vout];
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    public function hasWitness()
168
    {
169
        for ($l = count($this->inputs), $i = 0; $i < $l; $i++) {
170
            if (isset($this->witness[$i]) && count($this->witness[$i]) > 0) {
171
                return true;
172
            }
173
        }
174
175
        return false;
176
    }
177
178
    /**
179
     * @return ScriptWitnessInterface[]
180
     */
181 464
    public function getWitnesses()
182
    {
183 464
        return $this->witness;
184
    }
185
186
    /**
187
     * @param int $index
188
     * @return ScriptWitnessInterface
189
     */
190 81
    public function getWitness($index)
191
    {
192 81
        if (!isset($this->witness[$index])) {
193
            throw new \RuntimeException('No witness at this index');
194
        }
195 81
        return $this->witness[$index];
196
    }
197
198
    /**
199
     * @param int $vout
200
     * @return OutPointInterface
201
     */
202 4412
    public function makeOutpoint($vout)
203
    {
204 4412
        $this->getOutput($vout);
205 4412
        return new OutPoint($this->getTxId(), $vout);
206
    }
207
208
    /**
209
     * @param int $vout
210
     * @return Utxo
211
     */
212
    public function makeUtxo($vout)
213
    {
214
        return new Utxo(new OutPoint($this->getTxId(), $vout), $this->getOutput($vout));
215
    }
216
217
    /**
218
     * Get Lock Time
219
     *
220
     * @return int
221
     */
222 4868
    public function getLockTime()
223
    {
224 4868
        return $this->lockTime;
225
    }
226
227
    /**
228
     * @return int|string
229
     */
230 6
    public function getValueOut()
231
    {
232 6
        $math = Bitcoin::getMath();
233 6
        $value = gmp_init(0);
234 6
        foreach ($this->outputs as $output) {
235 6
            $value = $math->add($value, gmp_init($output->getValue()));
0 ignored issues
show
Bug introduced by
It seems like $value defined by $math->add($value, gmp_init($output->getValue())) on line 235 can also be of type resource; however, Mdanter\Ecc\Math\GmpMath::add() does only seem to accept object<GMP>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
236 2
        }
237
238 6
        return gmp_strval($value, 10);
239
    }
240
241
    /**
242
     * @return bool
243
     */
244 6
    public function isCoinbase()
245
    {
246 6
        return count($this->inputs) === 1 && $this->getInput(0)->isCoinBase();
247
    }
248
249
    /**
250
     * @param TransactionInterface $tx
251
     * @return bool
252
     */
253 6
    public function equals(TransactionInterface $tx)
254
    {
255 6
        $version = gmp_cmp($this->version, $tx->getVersion());
256 6
        if ($version !== 0) {
257 6
            return false;
258
        }
259
260 6
        $nIn = count($this->inputs);
261 6
        $nOut = count($this->outputs);
262 6
        $nWit = count($this->witness);
263
264
        // Check the length of each field is equal
265 6
        if ($nIn !== count($tx->getInputs()) || $nOut !== count($tx->getOutputs()) || $nWit !== count($tx->getWitnesses())) {
266 6
            return false;
267
        }
268
269
        // Check each field
270 6
        for ($i = 0; $i < $nIn; $i++) {
271 6
            if (false === $this->getInput($i)->equals($tx->getInput($i))) {
272 6
                return false;
273
            }
274 2
        }
275
276 6
        for ($i = 0; $i < $nOut; $i++) {
277 6
            if (false === $this->getOutput($i)->equals($tx->getOutput($i))) {
278 6
                return false;
279
            }
280 2
        }
281
282 6
        for ($i = 0; $i < $nWit; $i++) {
283
            if (false === $this->getWitness($i)->equals($tx->getWitness($i))) {
284
                return false;
285
            }
286
        }
287
288 6
        return gmp_cmp($this->lockTime, $tx->getLockTime()) === 0;
289
    }
290
291
    /**
292
     * @return BufferInterface
293
     */
294 4742
    public function getBuffer()
295
    {
296 4742
        return (new OldTransactionSerializer())->serialize($this);
297
    }
298
299
    /**
300
     * @return BufferInterface
301
     */
302 102
    public function getWitnessBuffer()
303
    {
304 102
        return (new TransactionSerializer())->serialize($this);
305
    }
306
}
307