Completed
Pull Request — master (#525)
by thomas
26:53
created

OutPoint::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 2
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 4.0312
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction;
4
5
use BitWasp\Bitcoin\Serializable;
6
use BitWasp\Bitcoin\Serializer\Transaction\OutPointSerializer;
7
use BitWasp\Bitcoin\Util\IntRange;
8
use BitWasp\Buffertools\Buffer;
9
use BitWasp\Buffertools\BufferInterface;
10
use BitWasp\CommonTrait\FunctionAliasArrayAccess;
11
12
class OutPoint extends Serializable implements OutPointInterface
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 2536
    public function __construct(BufferInterface $hashPrevOutput, $nPrevOutput)
30
    {
31 2536
        if ($hashPrevOutput->getSize() !== 32) {
32 2
            throw new \InvalidArgumentException('OutPoint: hashPrevOut must be a 32-byte Buffer');
33
        }
34
35 2534
        if ($nPrevOutput < 0 || $nPrevOutput > IntRange::U32_MAX) {
36
            throw new \InvalidArgumentException('nPrevOut must be between 0 and 0xffffffff');
37
        }
38
39 2534
        $this->hashPrevOutput = $hashPrevOutput;
40 2534
        $this->nPrevOutput = $nPrevOutput;
41 2534
    }
42
43
    /**
44
     * @return static
45
     */
46
    public static function makeCoinbase()
47
    {
48
        return new static(new Buffer("", 32), 0xffffffff);
49
    }
50
51
    /**
52
     * @return BufferInterface
53
     */
54 2610
    public function getTxId()
55
    {
56 2610
        return $this->hashPrevOutput;
57
    }
58
59
    /**
60
     * @return int
61
     */
62 2606
    public function getVout()
63
    {
64 2606
        return $this->nPrevOutput;
65
    }
66
67
    /**
68
     * @param OutPointInterface $outPoint
69
     * @return int
70
     */
71 20
    public function equals(OutPointInterface $outPoint)
72
    {
73 20
        $txid = strcmp($this->getTxId()->getBinary(), $outPoint->getTxId()->getBinary());
74 20
        if ($txid !== 0) {
75 4
            return false;
76
        }
77
78 20
        return gmp_cmp($this->getVout(), $outPoint->getVout()) === 0;
79
    }
80
81
    /**
82
     * @return BufferInterface
83
     */
84 16
    public function getBuffer()
85
    {
86 16
        return (new OutPointSerializer())->serialize($this);
87
    }
88
}
89