Completed
Pull Request — master (#242)
by thomas
126:48 queued 122:17
created

WitnessProgram::getVersion()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Script;
4
5
use BitWasp\Bitcoin\Crypto\Hash;
6
7
class WitnessProgram extends Script
8
{
9
    /**
10
     * @var int
11
     */
12
    private $version;
13
14
    /**
15
     * @var ScriptInterface
16
     */
17
    private $program;
18
19
    /**
20
     * WitnessProgram constructor.
21
     * @param int $version
22
     * @param ScriptInterface $program
23
     * @param Opcodes $opcodes
24
     */
25
    public function __construct($version, ScriptInterface $program, Opcodes $opcodes = null)
26
    {
27
        $internal = ScriptFactory::create()->int($version);
28
        switch ($version) {
29
            case 0:
30
                $internal->push($program->getBuffer());
31
                break;
32
            case 1:
33
                $internal->push(Hash::sha256($program->getBuffer()));
34
                break;
35
            default:
36
                throw new \RuntimeException('Only version 0 and version 1 scripts are supported');
37
        }
38
39
        $this->version = $version;
40
        $this->program = $program;
41
42
        parent::__construct($internal->getScript()->getBuffer(), $opcodes);
43
    }
44
45
    /**
46
     * @return int
47
     */
48
    public function getVersion()
49
    {
50
        return $this->version;
51
    }
52
53
    /**
54
     * @return ScriptInterface
55
     */
56
    public function getProgram()
57
    {
58
        return $this->program;
59
    }
60
}
61