1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin; |
6
|
|
|
|
7
|
|
|
use \BitWasp\Bech32\Exception\Bech32Exception; |
8
|
|
|
|
9
|
|
|
class Bech32 |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected static $charset = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected static $charsetKey = [ |
20
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
21
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
22
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
23
|
|
|
15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -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
|
|
|
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, |
27
|
|
|
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1 |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected static $generator = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param int[] $values |
37
|
|
|
* @param int $numValues |
38
|
|
|
* @return int |
39
|
|
|
*/ |
40
|
|
|
public static function polyMod(array $values, int $numValues): int |
41
|
|
|
{ |
42
|
|
|
$chk = 1; |
43
|
|
|
for ($i = 0; $i < $numValues; $i++) { |
44
|
|
|
$top = $chk >> 25; |
45
|
|
|
$chk = ($chk & 0x1ffffff) << 5 ^ $values[$i]; |
46
|
|
|
|
47
|
|
|
for ($j = 0; $j < 5; $j++) { |
48
|
|
|
$value = (($top >> $j) & 1) ? self::$generator[$j] : 0; |
49
|
|
|
$chk ^= $value; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $chk; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Expands the human readable part into a character array for checksumming. |
58
|
|
|
* @param string $hrp |
59
|
|
|
* @param int $hrpLen |
60
|
|
|
* @return int[] |
61
|
|
|
*/ |
62
|
|
|
public static function hrpExpand(string $hrp, int $hrpLen): array |
63
|
|
|
{ |
64
|
|
|
$expand1 = []; |
65
|
|
|
$expand2 = []; |
66
|
|
|
for ($i = 0; $i < $hrpLen; $i++) { |
67
|
|
|
$o = ord($hrp[$i]); |
68
|
|
|
$expand1[] = $o >> 5; |
69
|
|
|
$expand2[] = $o & 31; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return array_merge($expand1, [0], $expand2); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Converts words of $fromBits bits to $toBits bits in size. |
77
|
|
|
* |
78
|
|
|
* @param int[] $data - character array of data to convert |
79
|
|
|
* @param int $inLen - number of elements in array |
80
|
|
|
* @param int $fromBits - word (bit count) size of provided data |
81
|
|
|
* @param int $toBits - requested word size (bit count) |
82
|
|
|
* @param bool $pad - whether to pad (only when encoding) |
83
|
|
|
* @return int[] |
84
|
|
|
* @throws Bech32Exception |
85
|
|
|
*/ |
86
|
1 |
|
public static function convertBits(array $data, int $inLen, int $fromBits, int $toBits, bool $pad = true): array |
87
|
|
|
{ |
88
|
1 |
|
$acc = 0; |
89
|
1 |
|
$bits = 0; |
90
|
1 |
|
$ret = []; |
91
|
1 |
|
$maxv = (1 << $toBits) - 1; |
92
|
1 |
|
$maxacc = (1 << ($fromBits + $toBits - 1)) - 1; |
93
|
|
|
|
94
|
1 |
|
for ($i = 0; $i < $inLen; $i++) { |
95
|
1 |
|
$value = $data[$i]; |
96
|
1 |
|
if ($value < 0 || $value >> $fromBits) { |
97
|
1 |
|
throw new Bech32Exception('Invalid value for convert bits'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$acc = (($acc << $fromBits) | $value) & $maxacc; |
101
|
|
|
$bits += $fromBits; |
102
|
|
|
|
103
|
|
|
while ($bits >= $toBits) { |
104
|
|
|
$bits -= $toBits; |
105
|
|
|
$ret[] = (($acc >> $bits) & $maxv); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($pad) { |
110
|
|
|
if ($bits) { |
111
|
|
|
$ret[] = ($acc << $toBits - $bits) & $maxv; |
112
|
|
|
} |
113
|
|
|
} else if ($bits >= $fromBits || ((($acc << ($toBits - $bits))) & $maxv)) { |
114
|
|
|
throw new \BitWasp\Bech32\Exception\Bech32Exception('Invalid data'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $ret; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $hrp |
122
|
|
|
* @param int[] $convertedDataChars |
123
|
|
|
* @return int[] |
124
|
|
|
*/ |
125
|
|
|
public static function createChecksum(string $hrp, array $convertedDataChars): array |
126
|
|
|
{ |
127
|
|
|
$values = array_merge(self::hrpExpand($hrp, strlen($hrp)), $convertedDataChars); |
128
|
|
|
$polyMod = self::polyMod(array_merge($values, [0, 0, 0, 0, 0, 0]), count($values) + 6) ^ 1; |
129
|
|
|
$results = []; |
130
|
|
|
for ($i = 0; $i < 6; $i++) { |
131
|
|
|
$results[$i] = ($polyMod >> 5 * (5 - $i)) & 31; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $results; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Verifies the checksum given $hrp and $convertedDataChars. |
139
|
|
|
* |
140
|
|
|
* @param string $hrp |
141
|
|
|
* @param int[] $convertedDataChars |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public static function verifyChecksum(string $hrp, array $convertedDataChars): bool |
145
|
|
|
{ |
146
|
|
|
$expandHrp = self::hrpExpand($hrp, strlen($hrp)); |
147
|
|
|
$r = array_merge($expandHrp, $convertedDataChars); |
148
|
|
|
$poly = self::polyMod($r, count($r)); |
149
|
|
|
return $poly === 1; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $hrp |
154
|
|
|
* @param array $combinedDataChars |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public static function encode(string $hrp, array $combinedDataChars): string |
158
|
|
|
{ |
159
|
|
|
$checksum = self::createChecksum($hrp, $combinedDataChars); |
160
|
|
|
$characters = array_merge($combinedDataChars, $checksum); |
161
|
|
|
|
162
|
|
|
$encoded = []; |
163
|
|
|
for ($i = 0, $n = count($characters); $i < $n; $i++) { |
164
|
|
|
$encoded[$i] = self::$charset[$characters[$i]]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return "{$hrp}1" . implode('', $encoded); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Validates a bech32 string and returns [$hrp, $dataChars] if |
172
|
|
|
* the conversion was successful. An exception is thrown on invalid |
173
|
|
|
* data. |
174
|
|
|
* |
175
|
|
|
* @param string $sBech - the bech32 encoded string |
176
|
|
|
* @return array - returns [$hrp, $dataChars] |
177
|
|
|
* @throws Bech32Exception |
178
|
|
|
*/ |
179
|
9 |
|
public static function decode(string $sBech): array |
180
|
|
|
{ |
181
|
9 |
|
return \BitWasp\Bech32\decode($sBech); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|