Completed
Push — master ( 883b2a...6b2c83 )
by thomas
44:21 queued 41:03
created

OutPoint::getTxId()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
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\Serializable;
6
use BitWasp\Bitcoin\Serializer\Transaction\OutPointSerializer;
7
use BitWasp\Buffertools\BufferInterface;
8
use BitWasp\CommonTrait\FunctionAliasArrayAccess;
9
10
class OutPoint extends Serializable implements OutPointInterface
11
{
12
    use FunctionAliasArrayAccess;
13
14
    /**
15
     * @var BufferInterface
16
     */
17
    private $hashPrevOutput;
18
19
    /**
20
     * @var int
21
     */
22
    private $nPrevOutput;
23
24
    /**
25
     * OutPoint constructor.
26
     * @param BufferInterface $hashPrevOutput
27
     * @param int $nPrevOutput
28
     */
29 1158
    public function __construct(BufferInterface $hashPrevOutput, $nPrevOutput)
30
    {
31 1158
        if ($hashPrevOutput->getSize() !== 32) {
32 6
            throw new \InvalidArgumentException('OutPoint: hashPrevOut must be a 32-byte Buffer');
33
        }
34
        
35 1152
        $this->hashPrevOutput = $hashPrevOutput;
36 1152
        $this->nPrevOutput = $nPrevOutput;
37
38 1152
        $this
39 1152
            ->initFunctionAlias('txid', 'getTxId')
40 1152
            ->initFunctionAlias('vout', 'getVout');
41 1152
    }
42
43
    /**
44
     * @return BufferInterface
45
     */
46 360
    public function getTxId()
47
    {
48 360
        return $this->hashPrevOutput;
49
    }
50
51
    /**
52
     * @return int
53
     */
54 366
    public function getVout()
55
    {
56 366
        return $this->nPrevOutput;
57
    }
58
59
    /**
60
     * @param OutPointInterface $outPoint
61
     * @return int
62
     */
63 18
    public function equals(OutPointInterface $outPoint)
64
    {
65 18
        $txid = strcmp($this->getTxId()->getBinary(), $outPoint->getTxId()->getBinary());
66 18
        if ($txid !== 0) {
67 12
            return false;
68
        }
69
70 18
        return gmp_cmp($this->getVout(), $outPoint->getVout()) === 0;
71
    }
72
73
    /**
74
     * @return BufferInterface
75
     */
76 72
    public function getBuffer()
77
    {
78 72
        return (new OutPointSerializer())->serialize($this);
79
    }
80
}
81