Completed
Pull Request — master (#449)
by thomas
119:48 queued 116:58
created

WitnessProgram::getScript()   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 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
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 ScriptInterface
23
     */
24
    private $script;
25
26
    /**
27
     * WitnessProgram constructor.
28
     * @param int $version
29
     * @param BufferInterface $program
30
     */
31 97
    public function __construct($version, BufferInterface $program)
32
    {
33 97
        if (self::V0 === $version) {
34 96
            $size = $program->getSize();
35 96
            if ($size === 20) {
36 19
                $this->script = ScriptFactory::scriptPubKey()->p2wkh($program);
37 77
            } else if ($size === 32) {
38 76
                $this->script = ScriptFactory::scriptPubKey()->p2wsh($program);
39
            } else {
40 96
                throw new \RuntimeException('Invalid size for V0 witness program - must be 20 or 32 bytes');
41
            }
42
        } else {
43 1
            throw new \InvalidArgumentException('Invalid witness version');
44
        }
45
46 95
        $this->version = $version;
47 95
        $this->program = $program;
48 95
    }
49
50
    /**
51
     * @param BufferInterface $program
52
     * @return WitnessProgram
53
     */
54
    public static function v0(BufferInterface $program)
55
    {
56
        return new self(self::V0, $program);
57
    }
58
59
    /**
60
     * @return int
61
     */
62 89
    public function getVersion()
63
    {
64 89
        return $this->version;
65
    }
66
67
    /**
68
     * @return BufferInterface
69
     */
70 89
    public function getProgram()
71
    {
72 89
        return $this->program;
73
    }
74
75
    /**
76
     * @return ScriptInterface
77
     */
78
    public function getScript()
79
    {
80
        return $this->script;
81
    }
82
}
83