Completed
Pull Request — master (#744)
by thomas
23:27
created

KeyToScriptHelper::getP2wpkhFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Key\KeyToScript;
6
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
10
use BitWasp\Bitcoin\Key\KeyToScript\Decorator\P2shP2wshScriptDecorator;
11
use BitWasp\Bitcoin\Key\KeyToScript\Decorator\P2shScriptDecorator;
12
use BitWasp\Bitcoin\Key\KeyToScript\Decorator\P2wshScriptDecorator;
13
use BitWasp\Bitcoin\Key\KeyToScript\Factory\KeyToScriptDataFactory;
14
use BitWasp\Bitcoin\Key\KeyToScript\Factory\MultisigScriptDataFactory;
15
use BitWasp\Bitcoin\Key\KeyToScript\Factory\P2pkhScriptDataFactory;
16
use BitWasp\Bitcoin\Key\KeyToScript\Factory\P2wpkhScriptDataFactory;
17
use BitWasp\Bitcoin\Script\ScriptType;
18
19
class KeyToScriptHelper
20
{
21
22
    /**
23
     * @var mixed
24
     */
25
    private $cache = [];
26
27
    /**
28
     * @var PublicKeySerializerInterface
29
     */
30
    private $pubKeySer;
31
32
    /**
33
     * Slip132PrefixRegistry constructor.
34
     * @param EcAdapterInterface $ecAdapter
35
     */
36 20
    public function __construct(EcAdapterInterface $ecAdapter)
37
    {
38 20
        $this->pubKeySer = EcSerializer::getSerializer(PublicKeySerializerInterface::class, true, $ecAdapter);
39 20
    }
40
41
    /**
42
     * @param array ...$scriptPaths
43
     * @return string
44
     */
45 4
    private function makeScriptKey(... $scriptPaths)
46
    {
47 4
        return implode("|", $scriptPaths);
48
    }
49
50
    /**
51
     * @return P2pkhScriptDataFactory
52
     */
53 6
    public function getP2pkhFactory()
54
    {
55 6
        $key = ScriptType::P2PKH;
56 6
        if (!array_key_exists($key, $this->cache)) {
57 6
            $this->cache[$key] = new P2pkhScriptDataFactory($this->pubKeySer);
58
        }
59 6
        return $this->cache[$key];
60
    }
61
62
    /**
63
     * @param int $m
64
     * @param int $n
65
     * @param bool $sortKeys
66
     * @return MultisigScriptDataFactory
67
     */
68
    public function getMultisigFactory(int $m, int $n, bool $sortKeys)
69
    {
70
        return new MultisigScriptDataFactory($m, $n, $sortKeys, $this->pubKeySer);
71
    }
72
73
    /**
74
     * @return P2wpkhScriptDataFactory
75
     */
76 10
    public function getP2wpkhFactory()
77
    {
78 10
        $key = ScriptType::P2WKH;
79 10
        if (!array_key_exists($key, $this->cache)) {
80 10
            $this->cache[$key] = new P2wpkhScriptDataFactory($this->pubKeySer);
81
        }
82 10
        return $this->cache[$key];
83
    }
84
85
    /**
86
     * @param KeyToScriptDataFactory $scriptFactory
87
     * @return P2shScriptDecorator
88
     * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException
89
     */
90 4
    public function getP2shFactory(KeyToScriptDataFactory $scriptFactory)
91
    {
92 4
        $key = $this->makeScriptKey(ScriptType::P2SH, $scriptFactory->getScriptType());
93 4
        if (!array_key_exists($key, $this->cache)) {
94 4
            $this->cache[$key] = new P2shScriptDecorator($scriptFactory);
95
        }
96 4
        return $this->cache[$key];
97
    }
98
99
    /**
100
     * @param KeyToScriptDataFactory $scriptFactory
101
     * @return P2wshScriptDecorator
102
     * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException
103
     */
104
    public function getP2wshFactory(KeyToScriptDataFactory $scriptFactory)
105
    {
106
        $key = $this->makeScriptKey(ScriptType::P2WSH, $scriptFactory->getScriptType());
107
        if (!array_key_exists($key, $this->cache)) {
108
            $this->cache[$key] = new P2wshScriptDecorator($scriptFactory);
109
        }
110
        return $this->cache[$key];
111
    }
112
113
    /**
114
     * @param KeyToScriptDataFactory $scriptFactory
115
     * @return P2shP2wshScriptDecorator
116
     * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException
117
     */
118
    public function getP2shP2wshFactory(KeyToScriptDataFactory $scriptFactory)
119
    {
120
        $key = $this->makeScriptKey(ScriptType::P2SH, ScriptType::P2WSH, $scriptFactory->getScriptType());
121
        if (!array_key_exists($key, $this->cache)) {
122
            $this->cache[$key] = new P2shP2wshScriptDecorator($scriptFactory);
123
        }
124
        return $this->cache[$key];
125
    }
126
}
127