1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Exceptions\Bech32Exception; |
6
|
|
|
|
7
|
|
|
class Bech32 |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected static $charset = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected static $charsetKey = [ |
18
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
19
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
20
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
21
|
|
|
15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1, |
22
|
|
|
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, |
23
|
|
|
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1, |
24
|
|
|
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, |
25
|
|
|
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1 |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected static $generator = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param int[] $values |
35
|
|
|
* @param int $numValues |
36
|
|
|
* @return int |
37
|
|
|
*/ |
38
|
|
|
public static function polyMod(array $values, $numValues) |
39
|
|
|
{ |
40
|
48 |
|
$chk = 1; |
41
|
|
|
for ($i = 0; $i < $numValues; $i++) { |
42
|
48 |
|
$top = $chk >> 25; |
43
|
48 |
|
$chk = ($chk & 0x1ffffff) << 5 ^ $values[$i]; |
44
|
48 |
|
|
45
|
48 |
|
for ($j = 0; $j < 5; $j++) { |
46
|
|
|
$value = (($top >> $j) & 1) ? self::$generator[$j] : 0; |
47
|
48 |
|
$chk ^= $value; |
48
|
48 |
|
} |
49
|
48 |
|
} |
50
|
|
|
|
51
|
|
|
return $chk; |
52
|
|
|
} |
53
|
48 |
|
|
54
|
|
|
/** |
55
|
|
|
* Expands the human readable part into a character array for checksumming. |
56
|
|
|
* @param string $hrp |
57
|
|
|
* @param int $hrpLen |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public static function hrpExpand($hrp, $hrpLen) |
61
|
|
|
{ |
62
|
48 |
|
$expand1 = []; |
63
|
|
|
$expand2 = []; |
64
|
48 |
|
for ($i = 0; $i < $hrpLen; $i++) { |
65
|
48 |
|
$o = ord($hrp[$i]); |
66
|
48 |
|
$expand1[] = $o >> 5; |
67
|
48 |
|
$expand2[] = $o & 31; |
68
|
48 |
|
} |
69
|
48 |
|
|
70
|
|
|
return array_merge($expand1, [0], $expand2); |
71
|
|
|
} |
72
|
48 |
|
|
73
|
|
|
/** |
74
|
|
|
* Converts words of $fromBits bits to $toBits bits in size. |
75
|
|
|
* |
76
|
|
|
* @param int[] $data - character array of data to convert |
77
|
|
|
* @param int $inLen - number of elements in array |
78
|
|
|
* @param int $fromBits - word (bit count) size of provided data |
79
|
|
|
* @param int $toBits - requested word size (bit count) |
80
|
|
|
* @param bool $pad - whether to pad (only when encoding) |
81
|
|
|
* @return array |
82
|
|
|
* @throws Bech32Exception |
83
|
|
|
*/ |
84
|
|
|
public static function convertBits(array $data, $inLen, $fromBits, $toBits, $pad = true) |
85
|
|
|
{ |
86
|
34 |
|
$acc = 0; |
87
|
|
|
$bits = 0; |
88
|
34 |
|
$ret = []; |
89
|
34 |
|
$maxv = (1 << $toBits) - 1; |
90
|
34 |
|
$maxacc = (1 << ($fromBits + $toBits - 1)) - 1; |
91
|
34 |
|
|
92
|
34 |
|
for ($i = 0; $i < $inLen; $i++) { |
93
|
|
|
$value = $data[$i]; |
94
|
34 |
|
if ($value < 0 || $value >> $fromBits) { |
95
|
34 |
|
throw new Bech32Exception('Invalid value for convert bits'); |
96
|
34 |
|
} |
97
|
2 |
|
|
98
|
|
|
$acc = (($acc << $fromBits) | $value) & $maxacc; |
99
|
|
|
$bits += $fromBits; |
100
|
32 |
|
|
101
|
32 |
|
while ($bits >= $toBits) { |
102
|
|
|
$bits -= $toBits; |
103
|
32 |
|
$ret[] = (($acc >> $bits) & $maxv); |
104
|
32 |
|
} |
105
|
32 |
|
} |
106
|
|
|
|
107
|
|
|
if ($pad) { |
108
|
|
|
if ($bits) { |
109
|
32 |
|
$ret[] = ($acc << $toBits - $bits) & $maxv; |
110
|
12 |
|
} |
111
|
12 |
|
} else if ($bits >= $fromBits || ((($acc << ($toBits - $bits))) & $maxv)) { |
112
|
|
|
throw new Bech32Exception('Invalid data'); |
113
|
32 |
|
} |
114
|
4 |
|
|
115
|
|
|
return $ret; |
116
|
|
|
} |
117
|
28 |
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $hrp |
120
|
|
|
* @param int[] $convertedDataChars |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public static function createChecksum($hrp, array $convertedDataChars) |
124
|
|
|
{ |
125
|
12 |
|
$values = array_merge(self::hrpExpand($hrp, strlen($hrp)), $convertedDataChars); |
126
|
|
|
$polyMod = self::polyMod(array_merge($values, [0, 0, 0, 0, 0, 0]), count($values) + 6) ^ 1; |
127
|
12 |
|
$results = []; |
128
|
12 |
|
for ($i = 0; $i < 6; $i++) { |
129
|
12 |
|
$results[$i] = ($polyMod >> 5 * (5 - $i)) & 31; |
130
|
12 |
|
} |
131
|
12 |
|
|
132
|
|
|
return $results; |
133
|
|
|
} |
134
|
12 |
|
|
135
|
|
|
/** |
136
|
|
|
* Verifies the checksum given $hrp and $convertedDataChars. |
137
|
|
|
* |
138
|
|
|
* @param string $hrp |
139
|
|
|
* @param int[] $convertedDataChars |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public static function verifyChecksum($hrp, array $convertedDataChars) |
143
|
|
|
{ |
144
|
48 |
|
$expandHrp = self::hrpExpand($hrp, strlen($hrp)); |
145
|
|
|
$r = array_merge($expandHrp, $convertedDataChars); |
146
|
48 |
|
$poly = self::polyMod($r, count($r)); |
147
|
48 |
|
return $poly === 1; |
148
|
48 |
|
} |
149
|
48 |
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $hrp |
152
|
|
|
* @param array $combinedDataChars |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public static function encode($hrp, array $combinedDataChars) |
156
|
|
|
{ |
157
|
12 |
|
$checksum = self::createChecksum($hrp, $combinedDataChars); |
158
|
|
|
$characters = array_merge($combinedDataChars, $checksum); |
159
|
12 |
|
|
160
|
12 |
|
$encoded = []; |
161
|
|
|
for ($i = 0, $n = count($characters); $i < $n; $i++) { |
162
|
12 |
|
$encoded[$i] = self::$charset[$characters[$i]]; |
163
|
12 |
|
} |
164
|
12 |
|
|
165
|
|
|
return "{$hrp}1" . implode('', $encoded); |
166
|
|
|
} |
167
|
12 |
|
|
168
|
|
|
/** |
169
|
|
|
* Validates a bech32 string and returns [$hrp, $dataChars] if |
170
|
|
|
* the conversion was successful. An exception is thrown on invalid |
171
|
|
|
* data. |
172
|
|
|
* |
173
|
|
|
* @param string $sBech - the bech32 encoded string |
174
|
|
|
* @return array - returns [$hrp, $dataChars] |
175
|
|
|
* @throws Bech32Exception |
176
|
|
|
*/ |
177
|
|
|
public static function decode($sBech) |
178
|
|
|
{ |
179
|
58 |
|
$length = strlen($sBech); |
180
|
|
|
if ($length > 90) { |
181
|
58 |
|
throw new Bech32Exception('Bech32 string cannot exceed 90 characters in length'); |
182
|
58 |
|
} |
183
|
2 |
|
|
184
|
|
|
$chars = array_values(unpack('C*', $sBech)); |
185
|
|
|
|
186
|
56 |
|
$haveUpper = false; |
187
|
|
|
$haveLower = false; |
188
|
56 |
|
$positionOne = -1; |
189
|
56 |
|
|
190
|
56 |
|
for ($i = 0; $i < $length; $i++) { |
191
|
|
|
$x = $chars[$i]; |
192
|
56 |
|
if ($x < 33 || $x > 126) { |
193
|
56 |
|
throw new Bech32Exception('Out of range character in bech32 string'); |
194
|
56 |
|
} |
195
|
2 |
|
|
196
|
|
|
if ($x >= 0x61 && $x <= 0x7a) { |
197
|
|
|
$haveLower = true; |
198
|
54 |
|
} |
199
|
44 |
|
|
200
|
|
|
if ($x >= 0x41 && $x <= 0x5a) { |
201
|
|
|
$haveUpper = true; |
202
|
54 |
|
$x = $chars[$i] = $x + 0x20; |
203
|
14 |
|
} |
204
|
14 |
|
|
205
|
|
|
// find location of last '1' character |
206
|
|
|
if ($x === 0x31) { |
207
|
|
|
$positionOne = $i; |
208
|
54 |
|
} |
209
|
52 |
|
} |
210
|
|
|
|
211
|
|
|
if ($haveUpper && $haveLower) { |
212
|
|
|
throw new Bech32Exception('Data contains mixture of higher/lower case characters'); |
213
|
54 |
|
} |
214
|
4 |
|
|
215
|
|
|
if ($positionOne < 1 || ($positionOne + 7) > $length) { |
216
|
|
|
throw new Bech32Exception('Invalid location for `1` character'); |
217
|
50 |
|
} |
218
|
2 |
|
|
219
|
|
|
$hrp = []; |
220
|
|
|
for ($i = 0; $i < $positionOne; $i++) { |
221
|
48 |
|
$hrp[$i] = chr($chars[$i]); |
222
|
48 |
|
} |
223
|
48 |
|
|
224
|
|
|
$hrp = implode('', $hrp); |
225
|
|
|
$data = []; |
226
|
48 |
|
for ($i = $positionOne + 1; $i < $length; $i++) { |
227
|
48 |
|
$data[] = ($chars[$i] & 0x80) ? -1 : self::$charsetKey[$chars[$i]]; |
228
|
48 |
|
} |
229
|
48 |
|
|
230
|
|
|
if (!self::verifyChecksum($hrp, $data)) { |
231
|
|
|
throw new Bech32Exception('Invalid bech32 checksum'); |
232
|
48 |
|
} |
233
|
14 |
|
|
234
|
|
|
return [$hrp, array_slice($data, 0, -6)]; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|