Completed
Pull Request — master (#260)
by thomas
20:01 queued 16:45
created

Transaction::getLockTime()   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
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 1986
    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 1986
        $math = Bitcoin::getMath();
65 1986
        if ($math->cmp($nVersion, TransactionInterface::MAX_VERSION) > 0) {
66 6
            throw new \InvalidArgumentException('Version must be less than ' . TransactionInterface::MAX_VERSION);
67
        }
68
69 1974
        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 1962
        $this->version = $nVersion;
74 1962
        $this->inputs = $inputs ?: new TransactionInputCollection();
75 1962
        $this->outputs = $outputs ?: new TransactionOutputCollection();
76 1962
        $this->witness = $witness ?: new TransactionWitnessCollection();
77 1962
        $this->lockTime = $nLockTime;
78
79 1962
        $this
80 1962
            ->initFunctionAlias('version', 'getVersion')
81 1962
            ->initFunctionAlias('inputs', 'getInputs')
82 1962
            ->initFunctionAlias('outputs', 'getOutputs')
83 1962
            ->initFunctionAlias('locktime', 'getLockTime');
84 1962
    }
85
86
    /**
87
     * @return Transaction
88
     */
89 165
    public function __clone()
90
    {
91 165
        $this->inputs = clone $this->inputs;
92 165
        $this->outputs = clone $this->outputs;
93 165
    }
94
95
    /**
96
     * @return BufferInterface
97
     */
98 183
    public function getTxHash()
99
    {
100 183
        return Hash::sha256d($this->getBuffer());
101
    }
102
103
    /**
104
     * @return BufferInterface
105
     */
106 183
    public function getTxId()
107
    {
108 183
        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 375
    public function getVersion()
123
    {
124 375
        return $this->version;
125
    }
126
127
    /**
128
     * Get the array of inputs in the transaction
129
     *
130
     * @return TransactionInputCollection
131
     */
132 1050
    public function getInputs()
133
    {
134 1050
        return $this->inputs;
135
    }
136
137
    /**
138
     * @param int $index
139
     * @return TransactionInputInterface
140
     */
141 159
    public function getInput($index)
142
    {
143 159
        return $this->inputs[$index];
144
    }
145
146
    /**
147
     * Get Outputs
148
     *
149
     * @return TransactionOutputCollection
150
     */
151 384
    public function getOutputs()
152
    {
153 384
        return $this->outputs;
154
    }
155
156
    /**
157
     * @param int $vout
158
     * @return TransactionOutputInterface
159
     */
160 153
    public function getOutput($vout)
161
    {
162 153
        return $this->outputs[$vout];
163
    }
164
165
    /**
166
     * @return TransactionWitnessCollection
167
     */
168 147
    public function getWitnesses()
169
    {
170 147
        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);
0 ignored issues
show
Unused Code introduced by
The call to the method BitWasp\Bitcoin\Transact...ransaction::getOutput() seems un-needed as the method has no side-effects.

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:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

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:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

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:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
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 381
    public function getLockTime()
211
    {
212 381
        return $this->lockTime;
213
    }
214
215
    /**
216
     * @return \BitWasp\Bitcoin\Transaction\SignatureHash\SigHash
217
     */
218 96
    public function getSignatureHash()
219
    {
220 96
        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
     * @return Validator
247
     */
248 54
    public function validator()
249
    {
250 54
        return new Validator($this);
251
    }
252
253
    /**
254
     * @return BufferInterface
255
     */
256 315
    public function getBuffer()
257
    {
258 315
        return (new OldTransactionSerializer())->serialize($this);
259
    }
260
261
    /**
262
     * @return BufferInterface
263
     */
264 54
    public function getWitnessBuffer()
265
    {
266 54
        return (new TransactionSerializer())->serialize($this);
267
    }
268
}
269