Completed
Pull Request — master (#517)
by thomas
67:46 queued 65:32
created

WitnessProgram::getProgram()   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 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script;
4
5
use BitWasp\Buffertools\BufferInterface;
6
7
class WitnessProgram
8
{
9
    const V0 = 0;
10
11
    /**
12
     * @var int
13
     */
14
    private $version;
15
16
    /**
17
     * @var BufferInterface
18
     */
19
    private $program;
20
21
    /**
22
     * @var
23
     */
24
    private $outputScript;
25
26
    /**
27
     * WitnessProgram constructor.
28
     * @param int $version
29
     * @param BufferInterface $program
30
     */
31 206
    public function __construct($version, BufferInterface $program)
32
    {
33 206
        if ($this->version < 0 || $this->version > 16) {
34
            throw new \RuntimeException("Invalid witness program version");
35
        }
36
37 206
        if ($this->version === 0 && ($program->getSize() !== 20 && $program->getSize() !== 32)) {
38
            throw new \RuntimeException('Invalid size for V0 witness program - must be 20 or 32 bytes');
39
        }
40
41 206
        $this->version = $version;
42 206
        $this->program = $program;
43 206
    }
44
45
    /**
46
     * @param BufferInterface $program
47
     * @return WitnessProgram
48
     */
49 28
    public static function v0(BufferInterface $program)
50
    {
51 28
        if ($program->getSize() === 20) {
52 10
            return new self(self::V0, $program);
53 18
        } else if ($program->getSize() === 32) {
54 16
            return new self(self::V0, $program);
55
        } else {
56 2
            throw new \RuntimeException('Invalid size for V0 witness program - must be 20 or 32 bytes');
57
        }
58
    }
59
60
    /**
61
     * @return int
62
     */
63 184
    public function getVersion()
64
    {
65 184
        return $this->version;
66
    }
67
68
    /**
69
     * @return BufferInterface
70
     */
71 182
    public function getProgram()
72
    {
73 182
        return $this->program;
74
    }
75
76
    /**
77
     * @return ScriptInterface
78
     */
79 38
    public function getScript()
80
    {
81 38
        if (null === $this->outputScript) {
82 36
            $this->outputScript = ScriptFactory::sequence([encodeOpN($this->version), $this->program]);
83
        }
84
85 38
        return $this->outputScript;
86
    }
87
}
88