Completed
Push — master ( 1989a1...e79d55 )
by thomas
37:47
created

TransactionInput::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Serializer\Transaction\OutPointSerializer;
7
use BitWasp\Bitcoin\Script\Script;
8
use BitWasp\Bitcoin\Script\ScriptInterface;
9
use BitWasp\Bitcoin\Serializable;
10
use BitWasp\Bitcoin\Serializer\Transaction\TransactionInputSerializer;
11
use BitWasp\Buffertools\BufferInterface;
12
13
class TransactionInput extends Serializable implements TransactionInputInterface
14
{
15
16
    /**
17
     * @var OutPointInterface
18
     */
19
    private $outPoint;
20
21
    /**
22
     * @var ScriptInterface
23
     */
24
    private $script;
25
26
    /**
27
     * @var string|int
28
     */
29
    private $sequence;
30
31
    /**
32
     * @param OutPointInterface $outPoint
33
     * @param ScriptInterface $script
34
     * @param int $sequence
35
     */
36 2428
    public function __construct(OutPointInterface $outPoint, ScriptInterface $script, $sequence = self::SEQUENCE_FINAL)
37
    {
38 2428
        $this->outPoint = $outPoint;
39 2428
        $this->script = $script;
40 2428
        $this->sequence = $sequence;
41 2428
    }
42
43
    /**
44
     * @return TransactionInput
45
     */
46
    public function __clone()
47
    {
48
        $this->script = clone $this->script;
49
    }
50
51
    /**
52
     * @return OutPointInterface
53
     */
54 2353
    public function getOutPoint()
55
    {
56 2353
        return $this->outPoint;
57
    }
58
59
    /**
60
     * @return Script
61
     */
62 2338
    public function getScript()
63
    {
64 2338
        return $this->script;
65
    }
66
67
    /**
68
     * @return int
69
     */
70 2341
    public function getSequence()
71
    {
72 2341
        return $this->sequence;
73
    }
74
75
    /**
76
     * @param TransactionInputInterface $input
77
     * @return bool
78
     */
79 6
    public function equals(TransactionInputInterface $input)
80
    {
81 6
        $outPoint = $this->outPoint->equals($input->getOutPoint());
82 6
        if (!$outPoint) {
83 6
            return false;
84
        }
85
86 6
        $script = $this->script->equals($input->getScript());
87 6
        if (!$script) {
88 3
            return false;
89
        }
90
91 6
        return gmp_cmp(gmp_init($this->sequence), gmp_init($input->getSequence())) === 0;
92
    }
93
94
    /**
95
     * Check whether this transaction is a Coinbase transaction
96
     *
97
     * @return boolean
98
     */
99 6
    public function isCoinbase()
100
    {
101 6
        $math = Bitcoin::getMath();
102 6
        $outpoint = $this->outPoint;
103 6
        return $outpoint->getTxId()->getBinary() === str_pad('', 32, "\x00")
104 6
            && $math->cmp(gmp_init($outpoint->getVout()), gmp_init(0xffffffff)) === 0;
105
    }
106
107
    /**
108
     * @return bool
109
     */
110 3
    public function isFinal()
111
    {
112 3
        $math = Bitcoin::getMath();
113 3
        return $math->cmp(gmp_init($this->getSequence()), gmp_init(self::SEQUENCE_FINAL)) === 0;
114
    }
115
116
    /**
117
     * @return BufferInterface
118
     */
119 2314
    public function getBuffer()
120
    {
121 2314
        return (new TransactionInputSerializer(new OutPointSerializer()))->serialize($this);
122
    }
123
}
124