1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Key\Deterministic\HdPrefix; |
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\Network\NetworkInterface; |
15
|
|
|
use BitWasp\Bitcoin\Script\ScriptType; |
16
|
|
|
|
17
|
|
|
class Slip132PrefixRegistry |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var PublicKeySerializerInterface |
21
|
|
|
*/ |
22
|
|
|
private $pubKeySer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var mixed |
26
|
|
|
*/ |
27
|
|
|
private $cache = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Slip132PrefixRegistry constructor. |
31
|
|
|
* @param EcAdapterInterface $ecAdapter |
32
|
|
|
*/ |
33
|
39 |
|
public function __construct(EcAdapterInterface $ecAdapter) |
34
|
|
|
{ |
35
|
39 |
|
$this->pubKeySer = EcSerializer::getSerializer(PublicKeySerializerInterface::class, true, $ecAdapter); |
36
|
39 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array ...$scriptPaths |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
19 |
|
private function makeScriptKey(... $scriptPaths) |
43
|
|
|
{ |
44
|
19 |
|
return implode("|", $scriptPaths); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return P2pkhScriptDataFactory |
49
|
|
|
*/ |
50
|
28 |
|
private function getP2pkhFactory() |
51
|
|
|
{ |
52
|
28 |
|
$key = ScriptType::P2PKH; |
53
|
28 |
|
if (!array_key_exists($key, $this->cache)) { |
54
|
28 |
|
$this->cache[$key] = new P2pkhScriptDataFactory($this->pubKeySer); |
55
|
|
|
} |
56
|
28 |
|
return $this->cache[$key]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return P2wpkhScriptDataFactory |
61
|
|
|
*/ |
62
|
11 |
|
private 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
|
15 |
|
private function getP2shFactory(KeyToScriptDataFactory $scriptFactory) |
77
|
|
|
{ |
78
|
15 |
|
$key = $this->makeScriptKey(ScriptType::P2SH, $scriptFactory->getScriptType()); |
79
|
15 |
|
if (!array_key_exists($key, $this->cache)) { |
80
|
15 |
|
$this->cache[$key] = new P2shScriptDecorator($scriptFactory); |
81
|
|
|
} |
82
|
15 |
|
return $this->cache[$key]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param KeyToScriptDataFactory $scriptFactory |
87
|
|
|
* @return P2wshScriptDecorator |
88
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException |
89
|
|
|
*/ |
90
|
2 |
|
private 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 |
|
private function getP2shP2wshFactory(KeyToScriptDataFactory $scriptFactory) |
105
|
|
|
{ |
106
|
2 |
|
$key = $this->makeScriptKey(ScriptType::P2SH, $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
|
|
|
/** |
114
|
|
|
* @param NetworkInterface $network |
115
|
|
|
* @return ScriptPrefix |
116
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\InvalidNetworkParameter |
117
|
|
|
*/ |
118
|
14 |
|
public function xpubP2pkh(NetworkInterface $network) |
119
|
|
|
{ |
120
|
14 |
|
$factory = $this->getP2pkhFactory(); |
121
|
14 |
|
return new ScriptPrefix($factory, $network->getHDPrivByte(), $network->getHDPubByte()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param NetworkInterface $network |
126
|
|
|
* @return ScriptPrefix |
127
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException |
128
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\InvalidNetworkParameter |
129
|
|
|
*/ |
130
|
10 |
|
public function xpubP2shP2pkh(NetworkInterface $network) |
131
|
|
|
{ |
132
|
10 |
|
$factory = $this->getP2shFactory($this->getP2pkhFactory()); |
133
|
10 |
|
return new ScriptPrefix($factory, $network->getHDPrivByte(), $network->getHDPubByte()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return ScriptPrefix |
138
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException |
139
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\InvalidNetworkParameter |
140
|
|
|
*/ |
141
|
5 |
|
public function ypubP2shP2wpkh() |
142
|
|
|
{ |
143
|
5 |
|
$factory = $this->getP2shFactory($this->getP2wpkhFactory()); |
144
|
5 |
|
return new ScriptPrefix($factory, "049d7878", "049d7cb2"); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return ScriptPrefix |
149
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException |
150
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\InvalidNetworkParameter |
151
|
|
|
*/ |
152
|
2 |
|
public function YpubP2shP2wshP2pkh() |
153
|
|
|
{ |
154
|
2 |
|
$factory = $this->getP2shP2wshFactory($this->getP2pkhFactory()); |
155
|
2 |
|
return new ScriptPrefix($factory, "0295b005", "0295b43f"); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return ScriptPrefix |
160
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\InvalidNetworkParameter |
161
|
|
|
*/ |
162
|
6 |
|
public function zpubP2wpkh() |
163
|
|
|
{ |
164
|
6 |
|
$factory = $this->getP2wpkhFactory(); |
165
|
6 |
|
return new ScriptPrefix($factory, "04b2430c", "04b24746"); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return ScriptPrefix |
170
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException |
171
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\InvalidNetworkParameter |
172
|
|
|
*/ |
173
|
2 |
|
public function ZpubP2shP2wshP2pkh() |
174
|
|
|
{ |
175
|
2 |
|
$factory = $this->getP2wshFactory($this->getP2pkhFactory()); |
176
|
2 |
|
return new ScriptPrefix($factory, "02aa7a99", "02aa7ed3"); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|