|
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 string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $witnessV0KeyHash; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $witnessV0ScriptHash; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var bool |
|
34
|
|
|
*/ |
|
35
|
|
|
private $testnet; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var null|string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $xpubByte; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var null|string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $xprivByte; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
private $netMagicBytes; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Load basic data, throw exception if it's not provided |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $addressByte |
|
56
|
|
|
* @param string $p2shByte |
|
57
|
|
|
* @param string $privByte |
|
58
|
|
|
* @param bool $testnet |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
*/ |
|
61
|
312 |
|
public function __construct($addressByte, $p2shByte, $privByte, $testnet = false) |
|
62
|
|
|
{ |
|
63
|
312 |
|
if (!(ctype_xdigit($addressByte) && strlen($addressByte) === 2)) { |
|
64
|
6 |
|
throw new \InvalidArgumentException('address byte must be 1 hexadecimal byte'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
306 |
|
if (!(ctype_xdigit($p2shByte) && strlen($p2shByte) === 2)) { |
|
68
|
6 |
|
throw new \InvalidArgumentException('p2sh byte must be 1 hexadecimal byte'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
300 |
|
if (!(ctype_xdigit($privByte) && strlen($privByte) === 2)) { |
|
72
|
6 |
|
throw new \InvalidArgumentException('priv byte must be 1 hexadecimal byte'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
294 |
|
if (!is_bool($testnet)) { |
|
76
|
6 |
|
throw new \InvalidArgumentException('Testnet parameter must be a boolean'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
288 |
|
$this->addressByte = $addressByte; |
|
80
|
288 |
|
$this->p2shByte = $p2shByte; |
|
81
|
288 |
|
$this->privByte = $privByte; |
|
82
|
288 |
|
$this->testnet = $testnet; |
|
83
|
288 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritdoc |
|
87
|
|
|
*/ |
|
88
|
24 |
|
public function isTestnet() |
|
89
|
|
|
{ |
|
90
|
24 |
|
return $this->testnet; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @inheritdoc |
|
95
|
|
|
*/ |
|
96
|
174 |
|
public function getAddressByte() |
|
97
|
|
|
{ |
|
98
|
174 |
|
return $this->addressByte; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @inheritdoc |
|
103
|
|
|
*/ |
|
104
|
42 |
|
public function getPrivByte() |
|
105
|
|
|
{ |
|
106
|
42 |
|
return $this->privByte; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @inheritdoc |
|
111
|
|
|
*/ |
|
112
|
186 |
|
public function getP2shByte() |
|
113
|
|
|
{ |
|
114
|
186 |
|
return $this->p2shByte; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get version bytes for XPUB key |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
* @throws \Exception |
|
122
|
|
|
*/ |
|
123
|
132 |
|
public function getHDPubByte() |
|
124
|
|
|
{ |
|
125
|
132 |
|
if ($this->xpubByte === null) { |
|
126
|
6 |
|
throw new \Exception('No HD xpub byte was set'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
126 |
|
return $this->xpubByte; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set version bytes for XPUB key |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $bytes |
|
136
|
|
|
* @return $this |
|
137
|
|
|
*/ |
|
138
|
234 |
|
public function setHDPubByte($bytes) |
|
139
|
|
|
{ |
|
140
|
234 |
|
if (strlen($bytes) === 8 && ctype_xdigit($bytes) === true) { |
|
141
|
234 |
|
$this->xpubByte = $bytes; |
|
142
|
234 |
|
} |
|
143
|
|
|
|
|
144
|
234 |
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get version bytes for XPRIV key |
|
149
|
|
|
* |
|
150
|
|
|
* @return string |
|
151
|
|
|
* @throws \Exception |
|
152
|
|
|
*/ |
|
153
|
132 |
|
public function getHDPrivByte() |
|
154
|
|
|
{ |
|
155
|
132 |
|
if ($this->xprivByte === null) { |
|
156
|
6 |
|
throw new \Exception('No HD xpriv byte was set'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
126 |
|
return $this->xprivByte; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Set version bytes for XPRIV key |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $bytes |
|
166
|
|
|
* @return $this |
|
167
|
|
|
*/ |
|
168
|
234 |
|
public function setHDPrivByte($bytes) |
|
169
|
|
|
{ |
|
170
|
234 |
|
if (strlen($bytes) === 8 && ctype_xdigit($bytes) === true) { |
|
171
|
234 |
|
$this->xprivByte = $bytes; |
|
172
|
234 |
|
} |
|
173
|
|
|
|
|
174
|
234 |
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param string $witnessByte |
|
179
|
|
|
* @return $this |
|
180
|
|
|
*/ |
|
181
|
222 |
|
public function setP2WPKHByte($witnessByte) |
|
182
|
|
|
{ |
|
183
|
222 |
|
if (!(ctype_xdigit($witnessByte) && strlen($witnessByte) === 2)) { |
|
184
|
|
|
throw new \InvalidArgumentException('witness byte must be 1 hexadecimal byte'); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
222 |
|
$this->witnessV0KeyHash = $witnessByte; |
|
188
|
222 |
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @return string |
|
193
|
|
|
* @throws \Exception |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getP2WPKHByte() |
|
196
|
|
|
{ |
|
197
|
|
|
if ($this->witnessV0KeyHash === null) { |
|
198
|
|
|
throw new \Exception('No segnet byte was set'); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $this->witnessV0KeyHash; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param string $witnessByte |
|
206
|
|
|
* @return $this |
|
207
|
|
|
*/ |
|
208
|
222 |
|
public function setP2WSHByte($witnessByte) |
|
209
|
|
|
{ |
|
210
|
222 |
|
if (!(ctype_xdigit($witnessByte) && strlen($witnessByte) === 2)) { |
|
211
|
|
|
throw new \InvalidArgumentException('witness byte must be 1 hexadecimal byte'); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
222 |
|
$this->witnessV0ScriptHash = $witnessByte; |
|
215
|
222 |
|
return $this; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @return string |
|
220
|
|
|
* @throws \Exception |
|
221
|
|
|
*/ |
|
222
|
|
|
public function getP2WSHByte() |
|
223
|
|
|
{ |
|
224
|
|
|
if ($this->witnessV0ScriptHash === null) { |
|
225
|
|
|
throw new \Exception('No P2WPS was set'); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $this->witnessV0ScriptHash; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @param string $bytes |
|
234
|
|
|
* @return $this |
|
235
|
|
|
*/ |
|
236
|
222 |
|
public function setNetMagicBytes($bytes) |
|
237
|
|
|
{ |
|
238
|
222 |
|
$this->netMagicBytes = $bytes; |
|
239
|
222 |
|
return $this; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @return string |
|
244
|
|
|
* @throws \Exception |
|
245
|
|
|
*/ |
|
246
|
30 |
|
public function getNetMagicBytes() |
|
247
|
|
|
{ |
|
248
|
30 |
|
if ($this->netMagicBytes === null) { |
|
249
|
6 |
|
throw new \Exception('No network magic bytes were set'); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
24 |
|
return $this->netMagicBytes; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|