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
|
|
|
* Load basic data, throw exception if it's not provided |
44
|
|
|
* |
45
|
|
|
* @param string $addressByte |
46
|
|
|
* @param string $p2shByte |
47
|
|
|
* @param string $privByte |
48
|
|
|
* @param bool $testnet |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
|
|
public function __construct($addressByte, $p2shByte, $privByte, $testnet = false) |
52
|
|
|
{ |
53
|
|
|
if (!(ctype_xdigit($addressByte) && strlen($addressByte) === 2)) { |
54
|
|
|
throw new \InvalidArgumentException('address byte must be 1 hexadecimal byte'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!(ctype_xdigit($p2shByte) && strlen($p2shByte) === 2)) { |
58
|
|
|
throw new \InvalidArgumentException('p2sh byte must be 1 hexadecimal byte'); |
59
|
|
|
} |
60
|
|
|
|
61
|
300 |
|
if (!(ctype_xdigit($privByte) && strlen($privByte) === 2)) { |
62
|
|
|
throw new \InvalidArgumentException('priv byte must be 1 hexadecimal byte'); |
63
|
300 |
|
} |
64
|
6 |
|
|
65
|
|
|
if (!is_bool($testnet)) { |
66
|
|
|
throw new \InvalidArgumentException('Testnet parameter must be a boolean'); |
67
|
294 |
|
} |
68
|
6 |
|
|
69
|
|
|
$this->addressByte = $addressByte; |
70
|
|
|
$this->p2shByte = $p2shByte; |
71
|
288 |
|
$this->privByte = $privByte; |
72
|
6 |
|
$this->testnet = $testnet; |
73
|
|
|
} |
74
|
|
|
|
75
|
282 |
|
/** |
76
|
6 |
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function isTestnet() |
79
|
276 |
|
{ |
80
|
276 |
|
return $this->testnet; |
81
|
276 |
|
} |
82
|
276 |
|
|
83
|
276 |
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
|
|
public function getAddressByte() |
87
|
|
|
{ |
88
|
24 |
|
return $this->addressByte; |
89
|
|
|
} |
90
|
24 |
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function getPrivByte() |
95
|
|
|
{ |
96
|
138 |
|
return $this->privByte; |
97
|
|
|
} |
98
|
138 |
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritdoc |
101
|
|
|
*/ |
102
|
|
|
public function getP2shByte() |
103
|
|
|
{ |
104
|
42 |
|
return $this->p2shByte; |
105
|
|
|
} |
106
|
42 |
|
|
107
|
|
|
/** |
108
|
|
|
* Get version bytes for XPUB key |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
* @throws \Exception |
112
|
150 |
|
*/ |
113
|
|
|
public function getHDPubByte() |
114
|
150 |
|
{ |
115
|
|
|
if ($this->xpubByte === null) { |
116
|
|
|
throw new \Exception('No HD xpub byte was set'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->xpubByte; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
138 |
|
* Set version bytes for XPUB key |
124
|
|
|
* |
125
|
138 |
|
* @param string $bytes |
126
|
6 |
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function setHDPubByte($bytes) |
129
|
132 |
|
{ |
130
|
|
|
if (strlen($bytes) === 8 && ctype_xdigit($bytes) === true) { |
131
|
|
|
$this->xpubByte = $bytes; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
222 |
|
* Get version bytes for XPRIV key |
139
|
|
|
* |
140
|
222 |
|
* @return string |
141
|
222 |
|
* @throws \Exception |
142
|
111 |
|
*/ |
143
|
|
|
public function getHDPrivByte() |
144
|
222 |
|
{ |
145
|
|
|
if ($this->xprivByte === null) { |
146
|
|
|
throw new \Exception('No HD xpriv byte was set'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $this->xprivByte; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
138 |
|
* Set version bytes for XPRIV key |
154
|
|
|
* |
155
|
138 |
|
* @param string $bytes |
156
|
6 |
|
* @return $this |
157
|
|
|
*/ |
158
|
|
|
public function setHDPrivByte($bytes) |
159
|
132 |
|
{ |
160
|
|
|
if (strlen($bytes) === 8 && ctype_xdigit($bytes) === true) { |
161
|
|
|
$this->xprivByte = $bytes; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
222 |
|
* @param string $bytes |
169
|
|
|
* @return $this |
170
|
222 |
|
*/ |
171
|
222 |
|
public function setNetMagicBytes($bytes) |
172
|
111 |
|
{ |
173
|
|
|
$this->netMagicBytes = $bytes; |
174
|
222 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string |
179
|
|
|
* @throws \Exception |
180
|
|
|
*/ |
181
|
210 |
|
public function getNetMagicBytes() |
182
|
|
|
{ |
183
|
210 |
|
if ($this->netMagicBytes === null) { |
184
|
|
|
throw new \Exception('No network magic bytes were set'); |
185
|
|
|
} |
186
|
|
|
|
187
|
210 |
|
return $this->netMagicBytes; |
188
|
210 |
|
} |
189
|
|
|
} |
190
|
|
|
|