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

WitnessProgram   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 0
cbo 5
dl 0
loc 54
ccs 0
cts 25
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 3
A getVersion() 0 4 1
A getProgram() 0 4 1
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