Completed
Pull Request — master (#331)
by thomas
34:30
created

TransactionInput   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 80.48%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 130
ccs 33
cts 41
cp 0.8048
rs 10
wmc 13
lcom 1
cbo 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A equals() 0 14 3
A __construct() 0 12 1
A __clone() 0 4 1
A getOutPoint() 0 4 1
A getScript() 0 4 1
A getSequence() 0 4 1
A isCoinbase() 0 7 2
A isFinal() 0 5 1
A getBuffer() 0 4 1
A __debugInfo() 0 8 1
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
use BitWasp\CommonTrait\FunctionAliasArrayAccess;
13
14
class TransactionInput extends Serializable implements TransactionInputInterface
15
{
16
    use FunctionAliasArrayAccess;
17
18
    /**
19
     * @var OutPointInterface
20
     */
21
    private $outPoint;
22
23
    /**
24
     * @var ScriptInterface
25
     */
26
    private $script;
27
28
    /**
29
     * @var string|int
30
     */
31
    private $sequence;
32
33
    /**
34
     * @param OutPointInterface $outPoint
35
     * @param ScriptInterface $script
36
     * @param int $sequence
37
     */
38 3879
    public function __construct(OutPointInterface $outPoint, ScriptInterface $script, $sequence = self::SEQUENCE_FINAL)
39
    {
40 3879
        $this->outPoint = $outPoint;
41 3879
        $this->script = $script;
42 3879
        $this->sequence = $sequence;
43
44 2586
        $this
45 3879
            ->initFunctionAlias('outpoint', 'getOutPoint')
46 3879
            ->initFunctionAlias('script', 'getScript')
47 3879
            ->initFunctionAlias('sequence', 'getSequence');
48
49 3879
    }
50
51
    /**
52
     * @return TransactionInput
53
     */
54
    public function __clone()
55
    {
56
        $this->script = clone $this->script;
57
    }
58
59
    /**
60
     * @return OutPointInterface
61
     */
62 3459
    public function getOutPoint()
63
    {
64 3459
        return $this->outPoint;
65
    }
66
67
    /**
68
     * @return Script
69
     */
70 3789
    public function getScript()
71
    {
72 3789
        return $this->script;
73
    }
74
75
    /**
76
     * @return int
77
     */
78 3447
    public function getSequence()
79
    {
80 3447
        return $this->sequence;
81
    }
82
83
    /**
84
     * @param TransactionInputInterface $input
85
     * @return bool
86
     */
87 6
    public function equals(TransactionInputInterface $input)
88
    {
89 6
        $outPoint = $this->outPoint->equals($input->getOutPoint());
90 6
        if (!$outPoint) {
91 6
            return false;
92
        }
93
94 6
        $script = $this->script->equals($input->getScript());
95 6
        if (!$script) {
96 3
            return false;
97
        }
98
99 6
        return gmp_cmp(gmp_init($this->sequence), gmp_init($input->getSequence())) === 0;
100
    }
101
102
    /**
103
     * Check whether this transaction is a Coinbase transaction
104
     *
105
     * @return boolean
106
     */
107 6
    public function isCoinbase()
108
    {
109 6
        $math = Bitcoin::getMath();
110 6
        $outpoint = $this->outPoint;
111 6
        return $outpoint->getTxId()->getBinary() === str_pad('', 32, "\x00")
112 6
            && $math->cmp(gmp_init($outpoint->getVout()), gmp_init(0xffffffff)) === 0;
113
    }
114
115
    /**
116
     * @return bool
117
     */
118 3
    public function isFinal()
119
    {
120 3
        $math = Bitcoin::getMath();
121 3
        return $math->cmp(gmp_init($this->getSequence()), gmp_init(self::SEQUENCE_FINAL)) === 0;
122
    }
123
124
    /**
125
     * @return BufferInterface
126
     */
127 3420
    public function getBuffer()
128
    {
129 3420
        return (new TransactionInputSerializer(new OutPointSerializer()))->serialize($this);
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function __debugInfo()
136
    {
137
        return [
138
            'outPoint' => $this->outPoint,
139
            'script' => $this->script,
140
            'sequence' => $this->sequence
141
        ];
142
    }
143
}
144