This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 462
/
eosjs-numeric.ts
370 lines (338 loc) · 11.5 KB
/
eosjs-numeric.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* @module Numeric
*/
// copyright defined in eosjs/LICENSE.txt
// tslint:disable-next-line:no-var-requires
const ripemd160 = require("./ripemd").RIPEMD160.hash as (a: Uint8Array) => ArrayBuffer;
const base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function create_base58_map() {
const base58M = Array(256).fill(-1) as number[];
for (let i = 0; i < base58Chars.length; ++i) {
base58M[base58Chars.charCodeAt(i)] = i;
}
return base58M;
}
const base58Map = create_base58_map();
function create_base64_map() {
const base64M = Array(256).fill(-1) as number[];
for (let i = 0; i < base64Chars.length; ++i) {
base64M[base64Chars.charCodeAt(i)] = i;
}
base64M["=".charCodeAt(0)] = 0;
return base64M;
}
const base64Map = create_base64_map();
/** Is `bignum` a negative number? */
export function isNegative(bignum: Uint8Array) {
return (bignum[bignum.length - 1] & 0x80) !== 0;
}
/** Negate `bignum` */
export function negate(bignum: Uint8Array) {
let carry = 1;
for (let i = 0; i < bignum.length; ++i) {
const x = (~bignum[i] & 0xff) + carry;
bignum[i] = x;
carry = x >> 8;
}
}
/**
* Convert an unsigned decimal number in `s` to a bignum
* @param size bignum size (bytes)
*/
export function decimalToBinary(size: number, s: string) {
const result = new Uint8Array(size);
for (let i = 0; i < s.length; ++i) {
const srcDigit = s.charCodeAt(i);
if (srcDigit < "0".charCodeAt(0) || srcDigit > "9".charCodeAt(0)) {
throw new Error("invalid number");
}
let carry = srcDigit - "0".charCodeAt(0);
for (let j = 0; j < size; ++j) {
const x = result[j] * 10 + carry;
result[j] = x;
carry = x >> 8;
}
if (carry) {
throw new Error("number is out of range");
}
}
return result;
}
/**
* Convert a signed decimal number in `s` to a bignum
* @param size bignum size (bytes)
*/
export function signedDecimalToBinary(size: number, s: string) {
const negative = s[0] === "-";
if (negative) {
s = s.substr(1);
}
const result = decimalToBinary(size, s);
if (negative) {
negate(result);
if (!isNegative(result)) {
throw new Error("number is out of range");
}
} else if (isNegative(result)) {
throw new Error("number is out of range");
}
return result;
}
/**
* Convert `bignum` to an unsigned decimal number
* @param minDigits 0-pad result to this many digits
*/
export function binaryToDecimal(bignum: Uint8Array, minDigits = 1) {
const result = Array(minDigits).fill("0".charCodeAt(0)) as number[];
for (let i = bignum.length - 1; i >= 0; --i) {
let carry = bignum[i];
for (let j = 0; j < result.length; ++j) {
const x = ((result[j] - "0".charCodeAt(0)) << 8) + carry;
result[j] = "0".charCodeAt(0) + x % 10;
carry = (x / 10) | 0;
}
while (carry) {
result.push("0".charCodeAt(0) + carry % 10);
carry = (carry / 10) | 0;
}
}
result.reverse();
return String.fromCharCode(...result);
}
/**
* Convert `bignum` to a signed decimal number
* @param minDigits 0-pad result to this many digits
*/
export function signedBinaryToDecimal(bignum: Uint8Array, minDigits = 1) {
if (isNegative(bignum)) {
const x = bignum.slice();
negate(x);
return "-" + binaryToDecimal(x, minDigits);
}
return binaryToDecimal(bignum, minDigits);
}
/**
* Convert an unsigned base-58 number in `s` to a bignum
* @param size bignum size (bytes)
*/
export function base58ToBinary(size: number, s: string) {
const result = new Uint8Array(size);
for (let i = 0; i < s.length; ++i) {
let carry = base58Map[s.charCodeAt(i)];
if (carry < 0) {
throw new Error("invalid base-58 value");
}
for (let j = 0; j < size; ++j) {
const x = result[j] * 58 + carry;
result[j] = x;
carry = x >> 8;
}
if (carry) {
throw new Error("base-58 value is out of range");
}
}
result.reverse();
return result;
}
/**
* Convert `bignum` to a base-58 number
* @param minDigits 0-pad result to this many digits
*/
export function binaryToBase58(bignum: Uint8Array, minDigits = 1) {
const result = [] as number[];
for (const byte of bignum) {
let carry = byte;
for (let j = 0; j < result.length; ++j) {
const x = (base58Map[result[j]] << 8) + carry;
result[j] = base58Chars.charCodeAt(x % 58);
carry = (x / 58) | 0;
}
while (carry) {
result.push(base58Chars.charCodeAt(carry % 58));
carry = (carry / 58) | 0;
}
}
for (const byte of bignum) {
if (byte) {
break;
} else {
result.push("1".charCodeAt(0));
}
}
result.reverse();
return String.fromCharCode(...result);
}
/** Convert an unsigned base-64 number in `s` to a bignum */
export function base64ToBinary(s: string) {
let len = s.length;
if ((len & 3) === 1 && s[len - 1] === "=") {
len -= 1;
} // fc appends an extra '='
if ((len & 3) !== 0) {
throw new Error("base-64 value is not padded correctly");
}
const groups = len >> 2;
let bytes = groups * 3;
if (len > 0 && s[len - 1] === "=") {
if (s[len - 2] === "=") {
bytes -= 2;
} else {
bytes -= 1;
}
}
const result = new Uint8Array(bytes);
for (let group = 0; group < groups; ++group) {
const digit0 = base64Map[s.charCodeAt(group * 4 + 0)];
const digit1 = base64Map[s.charCodeAt(group * 4 + 1)];
const digit2 = base64Map[s.charCodeAt(group * 4 + 2)];
const digit3 = base64Map[s.charCodeAt(group * 4 + 3)];
result[group * 3 + 0] = (digit0 << 2) | (digit1 >> 4);
if (group * 3 + 1 < bytes) {
result[group * 3 + 1] = ((digit1 & 15) << 4) | (digit2 >> 2);
}
if (group * 3 + 2 < bytes) {
result[group * 3 + 2] = ((digit2 & 3) << 6) | digit3;
}
}
return result;
}
/** Key types this library supports */
export const enum KeyType {
k1 = 0,
r1 = 1,
}
/** Public key data size, excluding type field */
export const publicKeyDataSize = 33;
/** Private key data size, excluding type field */
export const privateKeyDataSize = 32;
/** Signature data size, excluding type field */
export const signatureDataSize = 65;
/** Public key, private key, or signature in binary form */
export interface Key {
type: KeyType;
data: Uint8Array;
}
function digestSuffixRipemd160(data: Uint8Array, suffix: string) {
const d = new Uint8Array(data.length + suffix.length);
for (let i = 0; i < data.length; ++i) {
d[i] = data[i];
}
for (let i = 0; i < suffix.length; ++i) {
d[data.length + i] = suffix.charCodeAt(i);
}
return ripemd160(d);
}
function stringToKey(s: string, type: KeyType, size: number, suffix: string): Key {
const whole = base58ToBinary(size + 4, s);
const result = { type, data: new Uint8Array(whole.buffer, 0, size) };
const digest = new Uint8Array(digestSuffixRipemd160(result.data, suffix));
if (digest[0] !== whole[size + 0] || digest[1] !== whole[size + 1]
|| digest[2] !== whole[size + 2] || digest[3] !== whole[size + 3]) {
throw new Error("checksum doesn't match");
}
return result;
}
function keyToString(key: Key, suffix: string, prefix: string) {
const digest = new Uint8Array(digestSuffixRipemd160(key.data, suffix));
const whole = new Uint8Array(key.data.length + 4);
for (let i = 0; i < key.data.length; ++i) {
whole[i] = key.data[i];
}
for (let i = 0; i < 4; ++i) {
whole[i + key.data.length] = digest[i];
}
return prefix + binaryToBase58(whole);
}
/** Convert key in `s` to binary form */
export function stringToPublicKey(s: string): Key {
if (typeof s !== "string") {
throw new Error("expected string containing public key");
}
if (s.substr(0, 3) === "EOS") {
const whole = base58ToBinary(publicKeyDataSize + 4, s.substr(3));
const key = { type: KeyType.k1, data: new Uint8Array(publicKeyDataSize) };
for (let i = 0; i < publicKeyDataSize; ++i) {
key.data[i] = whole[i];
}
const digest = new Uint8Array(ripemd160(key.data));
if (digest[0] !== whole[publicKeyDataSize] || digest[1] !== whole[34]
|| digest[2] !== whole[35] || digest[3] !== whole[36]) {
throw new Error("checksum doesn't match");
}
return key;
} else if (s.substr(0, 7) === "PUB_K1_") {
return stringToKey(s.substr(7), KeyType.k1, publicKeyDataSize, "K1");
} else if (s.substr(0, 7) === "PUB_R1_") {
return stringToKey(s.substr(7), KeyType.r1, publicKeyDataSize, "R1");
} else {
throw new Error("unrecognized public key format");
}
}
/** Convert `key` to string (base-58) form */
export function publicKeyToString(key: Key) {
if (key.type === KeyType.k1 && key.data.length === publicKeyDataSize) {
return keyToString(key, "K1", "PUB_K1_");
} else if (key.type === KeyType.r1 && key.data.length === publicKeyDataSize) {
return keyToString(key, "R1", "PUB_R1_");
} else {
throw new Error("unrecognized public key format");
}
}
/** If a key is in the legacy format (`EOS` prefix), then convert it to the new format (`PUB_K1_`).
* Leaves other formats untouched
*/
export function convertLegacyPublicKey(s: string) {
if (s.substr(0, 3) === "EOS") {
return publicKeyToString(stringToPublicKey(s));
}
return s;
}
/** If a key is in the legacy format (`EOS` prefix), then convert it to the new format (`PUB_K1_`).
* Leaves other formats untouched
*/
export function convertLegacyPublicKeys(keys: string[]) {
return keys.map(convertLegacyPublicKey);
}
/** Convert key in `s` to binary form */
export function stringToPrivateKey(s: string): Key {
if (typeof s !== "string") {
throw new Error("expected string containing private key");
}
if (s.substr(0, 7) === "PVT_R1_") {
return stringToKey(s.substr(7), KeyType.r1, privateKeyDataSize, "R1");
} else {
throw new Error("unrecognized private key format");
}
}
/** Convert `key` to string (base-58) form */
export function privateKeyToString(key: Key) {
if (key.type === KeyType.r1) {
return keyToString(key, "R1", "PVT_R1_");
} else {
throw new Error("unrecognized private key format");
}
}
/** Convert key in `s` to binary form */
export function stringToSignature(s: string): Key {
if (typeof s !== "string") {
throw new Error("expected string containing signature");
}
if (s.substr(0, 7) === "SIG_K1_") {
return stringToKey(s.substr(7), KeyType.k1, signatureDataSize, "K1");
} else if (s.substr(0, 7) === "SIG_R1_") {
return stringToKey(s.substr(7), KeyType.r1, signatureDataSize, "R1");
} else {
throw new Error("unrecognized signature format");
}
}
/** Convert `signature` to string (base-58) form */
export function signatureToString(signature: Key) {
if (signature.type === KeyType.k1) {
return keyToString(signature, "K1", "SIG_K1_");
} else if (signature.type === KeyType.r1) {
return keyToString(signature, "R1", "SIG_R1_");
} else {
throw new Error("unrecognized signature format");
}
}