Completed
Pull Request — 0.0.35 (#659)
by thomas
26:42
created

NestedP2shP2wshScriptDecorator::convertKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 11
ccs 0
cts 11
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Key\KeyToScript\Decorator;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface;
6
use BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData;
7
use BitWasp\Bitcoin\Script\P2shScript;
8
use BitWasp\Bitcoin\Script\ScriptType;
9
use BitWasp\Bitcoin\Script\WitnessScript;
10
use BitWasp\Bitcoin\Transaction\Factory\SignData;
11
12
class NestedP2shP2wshScriptDecorator extends ScriptHashDecorator
13
{
14
    /**
15
     * @var string[]
16
     */
17
    protected $allowedScriptTypes = [
18
        ScriptType::P2PKH,
19
        ScriptType::P2PK,
20
    ];
21
22
    /**
23
     * @var string
24
     */
25
    protected $decorateType = "p2sh|p2wsh";
26
27
    /**
28
     * @param KeyInterface $key
29
     * @return ScriptAndSignData
30
     * @throws \BitWasp\Bitcoin\Exceptions\P2shScriptException
31
     * @throws \BitWasp\Bitcoin\Exceptions\WitnessScriptException
32
     */
33
    public function convertKey(KeyInterface $key)
34
    {
35
        $witnessScript = new WitnessScript($this->scriptDataFactory->convertKey($key)->getScriptPubKey());
36
        $redeemScript = new P2shScript($witnessScript);
37
        return new ScriptAndSignData(
38
            $redeemScript->getOutputScript(),
39
            (new SignData())
40
                ->p2sh($redeemScript)
41
                ->p2wsh($witnessScript)
42
        );
43
    }
44
}
45