Completed
Push — master ( f2b04e...8782bc )
by thomas
27:42
created

KeyToScriptHelper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
dl 0
loc 97
ccs 20
cts 30
cp 0.6667
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A makeScriptKey() 0 4 1
A getP2pkhFactory() 0 8 2
A getP2wpkhFactory() 0 8 2
A getP2shFactory() 0 8 2
A getP2wshFactory() 0 8 2
A getP2shP2wshFactory() 0 8 2
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\P2pkhScriptDataFactory;
15
use BitWasp\Bitcoin\Key\KeyToScript\Factory\P2wpkhScriptDataFactory;
16
use BitWasp\Bitcoin\Script\ScriptType;
17
18
class KeyToScriptHelper
19
{
20
21
    /**
22
     * @var mixed
23
     */
24
    private $cache = [];
25
26
    /**
27
     * @var PublicKeySerializerInterface
28
     */
29
    private $pubKeySer;
30
31
    /**
32
     * Slip132PrefixRegistry constructor.
33
     * @param EcAdapterInterface $ecAdapter
34
     */
35 20
    public function __construct(EcAdapterInterface $ecAdapter)
36
    {
37 20
        $this->pubKeySer = EcSerializer::getSerializer(PublicKeySerializerInterface::class, true, $ecAdapter);
38 20
    }
39
40
    /**
41
     * @param array ...$scriptPaths
42
     * @return string
43
     */
44 4
    private function makeScriptKey(... $scriptPaths)
45
    {
46 4
        return implode("|", $scriptPaths);
47
    }
48
49
    /**
50
     * @return P2pkhScriptDataFactory
51
     */
52 6
    public function getP2pkhFactory()
53
    {
54 6
        $key = ScriptType::P2PKH;
55 6
        if (!array_key_exists($key, $this->cache)) {
56 6
            $this->cache[$key] = new P2pkhScriptDataFactory($this->pubKeySer);
57
        }
58 6
        return $this->cache[$key];
59
    }
60
61
    /**
62
     * @return P2wpkhScriptDataFactory
63
     */
64 10
    public function getP2wpkhFactory()
65
    {
66 10
        $key = ScriptType::P2WKH;
67 10
        if (!array_key_exists($key, $this->cache)) {
68 10
            $this->cache[$key] = new P2wpkhScriptDataFactory($this->pubKeySer);
69
        }
70 10
        return $this->cache[$key];
71
    }
72
73
    /**
74
     * @param KeyToScriptDataFactory $scriptFactory
75
     * @return P2shScriptDecorator
76
     * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException
77
     */
78 4
    public function getP2shFactory(KeyToScriptDataFactory $scriptFactory)
79
    {
80 4
        $key = $this->makeScriptKey(ScriptType::P2SH, $scriptFactory->getScriptType());
81 4
        if (!array_key_exists($key, $this->cache)) {
82 4
            $this->cache[$key] = new P2shScriptDecorator($scriptFactory);
83
        }
84 4
        return $this->cache[$key];
85
    }
86
87
    /**
88
     * @param KeyToScriptDataFactory $scriptFactory
89
     * @return P2wshScriptDecorator
90
     * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException
91
     */
92
    public function getP2wshFactory(KeyToScriptDataFactory $scriptFactory)
93
    {
94
        $key = $this->makeScriptKey(ScriptType::P2WSH, $scriptFactory->getScriptType());
95
        if (!array_key_exists($key, $this->cache)) {
96
            $this->cache[$key] = new P2wshScriptDecorator($scriptFactory);
97
        }
98
        return $this->cache[$key];
99
    }
100
101
    /**
102
     * @param KeyToScriptDataFactory $scriptFactory
103
     * @return P2shP2wshScriptDecorator
104
     * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException
105
     */
106
    public function getP2shP2wshFactory(KeyToScriptDataFactory $scriptFactory)
107
    {
108
        $key = $this->makeScriptKey(ScriptType::P2SH, ScriptType::P2WSH, $scriptFactory->getScriptType());
109
        if (!array_key_exists($key, $this->cache)) {
110
            $this->cache[$key] = new P2shP2wshScriptDecorator($scriptFactory);
111
        }
112
        return $this->cache[$key];
113
    }
114
}
115