Completed
Push — master ( c2aa7d...f68fb5 )
by thomas
21:12
created

WitnessScript   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 78.95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 75
ccs 15
cts 19
cp 0.7895
rs 10
c 1
b 0
f 0
wmc 8
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A getWitnessProgram() 0 8 2
A getAddress() 0 8 2
A getOutputScript() 0 4 1
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
    /**
32
     * WitnessScript constructor.
33
     * @param ScriptInterface $script
34
     * @param Opcodes|null $opcodes
35
     * @throws WitnessScriptException
36
     */
37 8
    public function __construct(ScriptInterface $script, Opcodes $opcodes = null)
38
    {
39 8
        if ($script instanceof self) {
40 2
            throw new WitnessScriptException("Cannot nest V0 P2WSH scripts.");
41 6
        } else if ($script instanceof P2shScript) {
42 2
            throw new WitnessScriptException("Cannot embed a P2SH script in a V0 P2WSH script.");
43
        }
44
45 4
        parent::__construct($script->getBuffer(), $opcodes);
46
47 4
        $this->witnessScriptHash = $script->getWitnessScriptHash();
48 4
        $this->outputScript = ScriptFactory::scriptPubKey()->p2wsh($this->witnessScriptHash);
49 4
    }
50
51
    /**
52
     * @return WitnessProgram
53
     */
54 6
    public function getWitnessProgram()
55
    {
56 6
        if (null === $this->witnessProgram) {
57 4
            $this->witnessProgram = WitnessProgram::v0($this->witnessScriptHash);
58
        }
59
60 6
        return $this->witnessProgram;
61
    }
62
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 6
    public function getOutputScript()
79
    {
80 6
        return $this->getWitnessProgram()->getScript();
81
    }
82
}
83