This repository has been archived by the owner on Dec 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
87 lines (87 loc) · 2.72 KB
/
index.js
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
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.decode = exports.encode = void 0;
function bytesToHex(uint8a) {
let hex = '';
for (let i = 0; i < uint8a.length; i++) {
hex += uint8a[i].toString(16).padStart(2, '0');
}
return hex;
}
const COMMON_B58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
const alphabet = {
ipfs: COMMON_B58_ALPHABET,
btc: COMMON_B58_ALPHABET,
xmr: COMMON_B58_ALPHABET,
flickr: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ',
xrp: 'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz'
};
function encode(source, type = 'ipfs') {
if (source.length === 0)
return '';
if (typeof source === 'string') {
if (typeof TextEncoder !== 'undefined') {
source = new TextEncoder().encode(source);
}
else {
source = new Uint8Array(source.split('').map(c => c.charCodeAt(0)));
}
}
type = type.toLowerCase();
if (type === 'xmr') {
let res = '';
for (let i = 0; i < source.length + 3; i += 8) {
const slice = source.slice(i, i + 8);
res += encode(slice).padStart(slice.length === 8 ? 11 : 7, '1');
}
return res;
}
if (!alphabet.hasOwnProperty(type))
throw new Error('invalid type');
const letters = alphabet[type];
let x = BigInt('0x' + bytesToHex(source));
let output = [];
while (x > 0) {
const mod = Number(x % 58n);
x = x / 58n;
output.push(letters[mod]);
}
for (let i = 0; source[i] === 0; i++) {
output.push(letters[0]);
}
return output.reverse().join('');
}
exports.encode = encode;
function decode(output, type = 'ipfs') {
if (output.length === 0)
return new Uint8Array([]);
const letters = alphabet[type];
const bytes = [0];
for (let i = 0; i < output.length; i++) {
const char = output[i];
const value = letters.indexOf(char);
if (value === undefined) {
throw new Error(`base58.decode received invalid input. Character '${char}' is not in the base58 alphabet.`);
}
for (let j = 0; j < bytes.length; j++) {
bytes[j] *= 58;
}
bytes[0] += value;
let carry = 0;
for (let j = 0; j < bytes.length; j++) {
bytes[j] += carry;
carry = bytes[j] >> 8;
bytes[j] &= 0xff;
}
while (carry > 0) {
bytes.push(carry & 0xff);
carry >>= 8;
}
}
for (let i = 0; i < output.length && output[i] === '1'; i++) {
bytes.push(0);
}
return new Uint8Array(bytes.reverse());
}
exports.decode = decode;
exports.default = encode;