Completed
Pull Request — master (#476)
by thomas
23:29
created

P2shScript   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 57
ccs 16
cts 16
cp 1
rs 10
wmc 6
lcom 0
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A getWitnessScriptHash() 0 4 1
A getOutputScript() 0 4 1
A getAddress() 0 4 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Script;
4
5
use BitWasp\Bitcoin\Address\ScriptHashAddress;
6
use BitWasp\Bitcoin\Exceptions\P2shScriptException;
7
8
class P2shScript extends Script
9
{
10
11
    /**
12
     * @var ScriptHashAddress
13
     */
14
    private $address;
15
16
    /**
17
     * @var ScriptInterface
18
     */
19
    private $outputScript;
20
21
    /**
22
     * P2shScript constructor.
23
     * @param ScriptInterface $script
24
     * @param Opcodes|null $opcodes
25
     * @throws P2shScriptException
26
     */
27 24
    public function __construct(ScriptInterface $script, Opcodes $opcodes = null)
28
    {
29 24
        if ($script instanceof WitnessScript) {
30 4
            $script = $script->getOutputScript();
31 20
        } else if ($script instanceof self) {
32 2
            throw new P2shScriptException("Cannot nest P2SH scripts.");
33
        }
34
35 22
        parent::__construct($script->getBuffer(), $opcodes);
36 22
        $this->scriptHash = $script->getScriptHash();
37 22
        $this->outputScript = ScriptFactory::scriptPubKey()->p2sh($this->scriptHash);
38 22
        $this->address = new ScriptHashAddress($this->scriptHash);
39 22
    }
40
41
    /**
42
     * @throws P2shScriptException
43
     */
44 2
    public function getWitnessScriptHash()
45
    {
46 2
        throw new P2shScriptException("Cannot compute witness-script-hash for a P2shScript");
47
    }
48
49
    /**
50
     * @return ScriptInterface
51
     */
52 8
    public function getOutputScript()
53
    {
54 8
        return $this->outputScript;
55
    }
56
57
    /**
58
     * @return \BitWasp\Bitcoin\Address\ScriptHashAddress
59
     */
60 10
    public function getAddress()
61
    {
62 10
        return $this->address;
63
    }
64
}
65