-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbech32.ts
203 lines (175 loc) · 4.36 KB
/
bech32.ts
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* @hidden
*/
const ALPHABET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
/* tslint:disable:prefer-for-of */
// pre-compute lookup table
/**
* @hidden
*/
const ALPHABET_MAP: { [ch: string]: number } = {};
for (let z = 0; z < ALPHABET.length; z++) {
const x = ALPHABET.charAt(z);
if (ALPHABET_MAP[x] !== undefined) {
throw new TypeError(x + " is ambiguous");
}
ALPHABET_MAP[x] = z;
}
// FIXME: any
/**
* @hidden
*/
function polymodStep(pre: any) {
const b = pre >> 25;
return (
((pre & 0x1ffffff) << 5) ^
(-((b >> 0) & 1) & 0x3b6a57b2) ^
(-((b >> 1) & 1) & 0x26508e6d) ^
(-((b >> 2) & 1) & 0x1ea119fa) ^
(-((b >> 3) & 1) & 0x3d4233dd) ^
(-((b >> 4) & 1) & 0x2a1462b3)
);
}
// FIXME: any
/**
* @hidden
*/
function prefixChk(prefix: any) {
let chk = 1;
for (let i = 0; i < prefix.length; ++i) {
const c = prefix.charCodeAt(i);
if (c < 33 || c > 126) {
throw new Error("Invalid prefix (" + prefix + ")");
}
chk = polymodStep(chk) ^ (c >> 5);
}
chk = polymodStep(chk);
for (let i = 0; i < prefix.length; ++i) {
const v = prefix.charCodeAt(i);
chk = polymodStep(chk) ^ (v & 0x1f);
}
return chk;
}
// FIXME: any
/**
* @hidden
*/
export function encode(prefix: any, words: any, LIMIT?: any) {
LIMIT = LIMIT || 90;
if (prefix.length + 7 + words.length > LIMIT) {
throw new TypeError("Exceeds length limit");
}
prefix = prefix.toLowerCase();
// determine chk mod
let chk = prefixChk(prefix);
let result = prefix;
for (let i = 0; i < words.length; ++i) {
const x = words[i];
if (x >> 5 !== 0) {
throw new Error("Non 5-bit word");
}
chk = polymodStep(chk) ^ x;
result += ALPHABET.charAt(x);
}
for (let i = 0; i < 6; ++i) {
chk = polymodStep(chk);
}
chk ^= 1;
for (let i = 0; i < 6; ++i) {
const v = (chk >> ((5 - i) * 5)) & 0x1f;
result += ALPHABET.charAt(v);
}
return result;
}
// FIXME: any
/**
* @hidden
*/
export function decode(str: string, prefix: string, LIMIT?: number) {
LIMIT = LIMIT || 90;
if (str.length < 8) {
throw new TypeError(str + " too short");
}
if (str.length > LIMIT) {
throw new TypeError("Exceeds length limit");
}
// don't allow mixed case
const lowered = str.toLowerCase();
const uppered = str.toUpperCase();
if (str !== lowered && str !== uppered) {
throw new Error("Mixed-case string " + str);
}
str = lowered;
if (!str.startsWith(prefix)) {
throw new Error("Missing prefix for " + str);
}
const split = prefix.length;
const wordChars = str.slice(split);
if (wordChars.length < 6) {
throw new Error("Data too short");
}
let chk = prefixChk(prefix);
const words = [];
for (let i = 0; i < wordChars.length; ++i) {
const c = wordChars.charAt(i);
const v = ALPHABET_MAP[c];
if (v === undefined) {
throw new Error("Unknown character " + c);
}
chk = polymodStep(chk) ^ v;
// not in the checksum?
if (i + 6 >= wordChars.length) {
continue;
}
words.push(v);
}
if (chk !== 1) {
throw new Error("Invalid checksum for " + str);
}
return { prefix, words };
}
// FIXME: any
/**
* @hidden
*/
function convert(data: any, inBits: any, outBits: any, pad: any) {
let value = 0;
let bits = 0;
const maxV = (1 << outBits) - 1;
const result = [];
for (let i = 0; i < data.length; ++i) {
value = (value << inBits) | data[i];
bits += inBits;
while (bits >= outBits) {
bits -= outBits;
result.push((value >> bits) & maxV);
}
}
if (pad) {
if (bits > 0) {
result.push((value << (outBits - bits)) & maxV);
}
} else {
if (bits >= inBits) {
throw new Error("Excess padding");
}
if ((value << (outBits - bits)) & maxV) {
throw new Error("Non-zero padding");
}
}
return result;
}
// FIXME: any
/**
* @hidden
*/
export function toWords(bytes: any) {
return convert(bytes, 8, 5, true);
}
// FIXME: any
/**
* @hidden
*/
export function fromWords(words: any) {
return convert(words, 5, 8, false);
}