-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathencoding.js
More file actions
189 lines (170 loc) · 5.38 KB
/
Copy pathencoding.js
File metadata and controls
189 lines (170 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* @flow */
import { createBitArray } from './bit-converter';
import type { BitArray } from './bit-converter';
type FiveBitArray = BitArray<5>;
/** Encoding variation. Influences checksum computations. */
export type Encoding = 'bech32' | 'bech32m';
// Alphabet for Bech32
const CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
// Checksum constant for Bech32m.
const BECH32M_CHECKSUM = 0x2bc830a3;
// Minimum char code that could be present in the encoded message
const MIN_CHAR_CODE = 33;
// Maximum char code that could be present in the encoded message
const MAX_CHAR_CODE = 126;
export const CHECKSUM_LENGTH = 6;
// Reverse lookup for characters
const CHAR_LOOKUP = (() => {
const lookup = new Map<string, number>();
for (let i = 0; i < CHARSET.length; i += 1) {
lookup.set(CHARSET[i], i);
}
return lookup;
})();
// Poly generators
const GEN = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
function polymod(values: FiveBitArray): number {
return values.reduce((checksum, value) => {
const bits = checksum >> 25;
const newChecksum = ((checksum & 0x1ffffff) << 5) ^ value;
return GEN.reduce(
(chk, gen, i) => (((bits >> i) & 1) === 0 ? chk : (chk ^ gen)),
newChecksum,
);
}, /* initial checksum */ 1);
}
/**
* Expands a prefix into the specified output buffer.
*/
export function expandPrefix(prefix: string, outBuffer: FiveBitArray): void {
for (let i = 0; i < prefix.length; i += 1) {
const code = prefix.charCodeAt(i);
outBuffer[i] = code >> 5;
outBuffer[i + prefix.length + 1] = code & 31;
}
outBuffer[prefix.length] = 0;
}
/**
* Verifies the checksum for a particular buffer.
*/
export function verifyChecksum(buffer: FiveBitArray): Encoding | void {
switch (polymod(buffer)) {
case 1:
return 'bech32';
case BECH32M_CHECKSUM:
return 'bech32m';
default:
return undefined;
}
}
/**
* Creates a checksum for a buffer and writes it to the last 6 5-bit groups
* of the buffer.
*/
export function createChecksum(buffer: FiveBitArray, encoding: Encoding): void {
let checksumConstant;
switch (encoding) {
case 'bech32':
checksumConstant = 1; break;
case 'bech32m':
checksumConstant = BECH32M_CHECKSUM; break;
default:
throw Error(`Invalid encoding value: ${encoding}; expected bech32 or bech32m`);
}
const mod = polymod(buffer) ^ checksumConstant;
for (let i = 0; i < CHECKSUM_LENGTH; i += 1) {
const shift = 5 * (5 - i);
buffer[buffer.length - CHECKSUM_LENGTH + i] = (mod >> shift) & 31;
}
}
/**
* Encodes an array of 5-bit groups into a string.
*
* @param {Uint8Array} buffer
* @returns {string}
*
* @api private
*/
export function encode(buffer: FiveBitArray): string {
return buffer.reduce((acc, bits) => acc + CHARSET[bits], '');
}
/**
* Decodes a string into an array of 5-bit groups.
*
* @param {string} message
* @param {Uint8Array} [dst]
* Optional array to write the output to. If not specified, the array is created.
* @returns {Uint8Array}
* Array with the result of decoding
*
* @throws {Error}
* if there are characters in `message` not present in the encoding alphabet
*
* @api private
*/
export function decode(message: string, dst?: FiveBitArray): FiveBitArray {
const realDst = dst || createBitArray(message.length);
for (let i = 0; i < message.length; i += 1) {
const idx = CHAR_LOOKUP.get(message[i]);
if (idx === undefined) {
throw new Error(`Invalid char in message: ${message[i]}`);
}
realDst[i] = idx;
}
return realDst;
}
/**
* Decodes a string and a human-readable prefix into an array of 5-bit groups.
* The prefix is expanded as specified by Bech32.
*
* @param {string} prefix
* @param {string} message
* @returns {Uint8Array}
* Array with the result of decoding
*
* @api private
*/
export function decodeWithPrefix(prefix: string, message: string): FiveBitArray {
const len = message.length + 2 * prefix.length + 1;
const dst = createBitArray<5>(len);
expandPrefix(prefix, dst.subarray(0, 2 * prefix.length + 1));
decode(message, dst.subarray(2 * prefix.length + 1));
return dst;
}
/**
* Detects the character case used in `message`. If the message doesn't
* contain either lower-case or upper-case chars, returns `null`.
*
* @param {string} message
* @param {string} messageDescription
* Human-readable description of the message to put into an error message, should any occur
* @returns {'lower'|'upper'|null}
* @throws if the message contains both lowercase and uppercase chars,
* or contains chars not valid for Bech32(m) encoding
*/
export function detectCase(
message: string,
messageDescription: string = 'message',
): 'lower' | 'upper' | null {
let hasLowerCase = false;
let hasUpperCase = false;
for (let i = 0; i < message.length; i += 1) {
const ord = message.charCodeAt(i);
// 3. Allowed chars in the encoding
if (ord < MIN_CHAR_CODE || ord > MAX_CHAR_CODE) {
throw new TypeError(`Invalid char in ${messageDescription}: ${ord}; `
+ `should be in ASCII range ${MIN_CHAR_CODE}-${MAX_CHAR_CODE}`);
}
hasUpperCase = hasUpperCase || (ord >= 65 && ord <= 90);
hasLowerCase = hasLowerCase || (ord >= 97 && ord <= 122);
}
if (hasLowerCase && hasUpperCase) {
throw new TypeError(`Mixed-case ${messageDescription}`);
} else if (hasUpperCase) {
return 'upper';
} else if (hasLowerCase) {
return 'lower';
} else {
return null;
}
}