Completed
Pull Request — master (#517)
by thomas
72:19
created

WitnessScript::getAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script;
4
5
use BitWasp\Bitcoin\Address\SegwitAddress;
6
use BitWasp\Bitcoin\Exceptions\WitnessScriptException;
7
8
class WitnessScript extends Script
9
{
10
11
    /**
12
     * @var ScriptInterface
13
     */
14
    private $outputScript;
15
16
    /**
17
     * @var \BitWasp\Buffertools\BufferInterface
18
     */
19
    protected $witnessScriptHash;
20
21
    /**
22
     * @var WitnessProgram|null
23
     */
24
    private $witnessProgram;
25
26
    /**
27
     * @var SegwitAddress
28
     */
29
    private $address;
30
31 8
    /**
32
     * WitnessScript constructor.
33 8
     * @param ScriptInterface $script
34 2
     * @param Opcodes|null $opcodes
35 6
     * @throws WitnessScriptException
36 2
     */
37
    public function __construct(ScriptInterface $script, Opcodes $opcodes = null)
38
    {
39 4
        if ($script instanceof self) {
40
            throw new WitnessScriptException("Cannot nest V0 P2WSH scripts.");
41 4
        } else if ($script instanceof P2shScript) {
42 4
            throw new WitnessScriptException("Cannot embed a P2SH script in a V0 P2WSH script.");
43 4
        }
44
45
        parent::__construct($script->getBuffer(), $opcodes);
46
47
        $this->witnessScriptHash = $script->getWitnessScriptHash();
48 6
        $this->outputScript = ScriptFactory::scriptPubKey()->p2wsh($this->witnessScriptHash);
49
    }
50 6
51 4
    /**
52
     * @return WitnessProgram
53
     */
54 6
    public function getWitnessProgram()
55
    {
56
        if (null === $this->witnessProgram) {
57
            $this->witnessProgram = WitnessProgram::v0($this->witnessScriptHash);
58
        }
59
60 6
        return $this->witnessProgram;
61
    }
62 6
63
    /**
64
     * @return SegwitAddress
65
     */
66
    public function getAddress()
67
    {
68
        if (null === $this->address) {
69
            $this->address = new SegwitAddress($this->getWitnessProgram());
70
        }
71
72
        return $this->address;
73
    }
74
75
    /**
76
     * @return ScriptInterface
77
     */
78
    public function getOutputScript()
79
    {
80
        return $this->getWitnessProgram()->getScript();
81
    }
82
}
83