Files
whoswhip.dev/html/assets/js/b58.js
T
2026-12-04 21:43:17 -05:00

31 lines
652 B
JavaScript

const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
function base10ToBase58(value) {
let num = BigInt(value);
let result = "";
if (num < 0n) throw new Error("Negative number");
do {
result = ALPHABET[Number(num % 58n)] + result;
num /= 58n;
} while (num > 0n);
return result;
}
function base58ToBase10(str) {
let num = 0n;
for (const ch of str) {
const digit = ALPHABET.indexOf(ch);
if (digit === -1) {
throw new Error(`Invalid Base58 character: ${ch}`);
}
num = num * 58n + BigInt(digit);
}
return num.toString();
}