1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Network; |
4
|
|
|
|
5
|
|
|
class Network implements NetworkInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
private $addressByte; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $privByte; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $p2shByte; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
private $testnet; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var null|string |
29
|
|
|
*/ |
30
|
|
|
private $xpubByte; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var null|string |
34
|
|
|
*/ |
35
|
|
|
private $xprivByte; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $netMagicBytes; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var null|string |
44
|
|
|
*/ |
45
|
|
|
private $segwitAddrPrefix; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Load basic data, throw exception if it's not provided |
49
|
|
|
* |
50
|
|
|
* @param string $addressByte |
51
|
114 |
|
* @param string $p2shByte |
52
|
|
|
* @param string $privByte |
53
|
114 |
|
* @param bool $testnet |
54
|
2 |
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
|
|
public function __construct($addressByte, $p2shByte, $privByte, $testnet = false) |
57
|
112 |
|
{ |
58
|
2 |
|
if (!(ctype_xdigit($addressByte) && strlen($addressByte) === 2)) { |
59
|
|
|
throw new \InvalidArgumentException('address byte must be 1 hexadecimal byte'); |
60
|
|
|
} |
61
|
110 |
|
|
62
|
2 |
|
if (!(ctype_xdigit($p2shByte) && strlen($p2shByte) === 2)) { |
63
|
|
|
throw new \InvalidArgumentException('p2sh byte must be 1 hexadecimal byte'); |
64
|
|
|
} |
65
|
108 |
|
|
66
|
2 |
|
if (!(ctype_xdigit($privByte) && strlen($privByte) === 2)) { |
67
|
|
|
throw new \InvalidArgumentException('priv byte must be 1 hexadecimal byte'); |
68
|
|
|
} |
69
|
106 |
|
|
70
|
106 |
|
if (!is_bool($testnet)) { |
71
|
106 |
|
throw new \InvalidArgumentException('Testnet parameter must be a boolean'); |
72
|
106 |
|
} |
73
|
106 |
|
|
74
|
|
|
$this->addressByte = $addressByte; |
75
|
|
|
$this->p2shByte = $p2shByte; |
76
|
|
|
$this->privByte = $privByte; |
77
|
|
|
$this->testnet = $testnet; |
78
|
8 |
|
} |
79
|
|
|
|
80
|
8 |
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
*/ |
83
|
|
|
public function isTestnet() |
84
|
|
|
{ |
85
|
|
|
return $this->testnet; |
86
|
46 |
|
} |
87
|
|
|
|
88
|
46 |
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
*/ |
91
|
|
|
public function getAddressByte() |
92
|
|
|
{ |
93
|
|
|
return $this->addressByte; |
94
|
14 |
|
} |
95
|
|
|
|
96
|
14 |
|
/** |
97
|
|
|
* @inheritdoc |
98
|
|
|
*/ |
99
|
|
|
public function getPrivByte() |
100
|
|
|
{ |
101
|
|
|
return $this->privByte; |
102
|
56 |
|
} |
103
|
|
|
|
104
|
56 |
|
/** |
105
|
|
|
* @inheritdoc |
106
|
|
|
*/ |
107
|
|
|
public function getP2shByte() |
108
|
|
|
{ |
109
|
|
|
return $this->p2shByte; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
60 |
|
* Get version bytes for XPUB key |
114
|
|
|
* |
115
|
60 |
|
* @return string |
116
|
2 |
|
* @throws \Exception |
117
|
|
|
*/ |
118
|
|
|
public function getHDPubByte() |
119
|
58 |
|
{ |
120
|
|
|
if ($this->xpubByte === null) { |
121
|
|
|
throw new \Exception('No HD xpub byte was set'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->xpubByte; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
88 |
|
* Set version bytes for XPUB key |
129
|
|
|
* |
130
|
88 |
|
* @param string $bytes |
131
|
88 |
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
public function setHDPubByte($bytes) |
134
|
88 |
|
{ |
135
|
|
|
if (strlen($bytes) === 8 && ctype_xdigit($bytes) === true) { |
136
|
|
|
$this->xpubByte = $bytes; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
60 |
|
* Get version bytes for XPRIV key |
144
|
|
|
* |
145
|
60 |
|
* @return string |
146
|
2 |
|
* @throws \Exception |
147
|
|
|
*/ |
148
|
|
|
public function getHDPrivByte() |
149
|
58 |
|
{ |
150
|
|
|
if ($this->xprivByte === null) { |
151
|
|
|
throw new \Exception('No HD xpriv byte was set'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->xprivByte; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
88 |
|
* Set version bytes for XPRIV key |
159
|
|
|
* |
160
|
88 |
|
* @param string $bytes |
161
|
88 |
|
* @return $this |
162
|
|
|
*/ |
163
|
|
|
public function setHDPrivByte($bytes) |
164
|
88 |
|
{ |
165
|
|
|
if (strlen($bytes) === 8 && ctype_xdigit($bytes) === true) { |
166
|
|
|
$this->xprivByte = $bytes; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
84 |
|
|
172
|
|
|
/** |
173
|
84 |
|
* @param string $bytes |
174
|
84 |
|
* @return $this |
175
|
|
|
*/ |
176
|
|
|
public function setNetMagicBytes($bytes) |
177
|
|
|
{ |
178
|
|
|
$this->netMagicBytes = $bytes; |
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
10 |
|
|
182
|
|
|
/** |
183
|
10 |
|
* @return string |
184
|
2 |
|
* @throws \Exception |
185
|
|
|
*/ |
186
|
|
|
public function getNetMagicBytes() |
187
|
8 |
|
{ |
188
|
|
|
if ($this->netMagicBytes === null) { |
189
|
|
|
throw new \Exception('No network magic bytes were set'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this->netMagicBytes; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $hrp |
197
|
|
|
* @return $this |
198
|
|
|
*/ |
199
|
|
|
public function setSegwitBech32Prefix($hrp) |
200
|
|
|
{ |
201
|
|
|
if ($hrp !== strtoupper($hrp) && $hrp !== strtolower($hrp)) { |
202
|
|
|
throw new \RuntimeException("Bech32 prefix for segwit address contains mixed case characters"); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$this->segwitAddrPrefix = $hrp; |
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return bool |
211
|
|
|
* @throws \Exception |
212
|
|
|
*/ |
213
|
|
|
public function getSegwitBech32Prefix() |
214
|
|
|
{ |
215
|
|
|
if ($this->segwitAddrPrefix === null) { |
216
|
|
|
throw new \Exception("No bech32 prefix for segwit addresses set"); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $this->segwitAddrPrefix; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|