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