const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
function base58Encode(bytes) {
if (!bytes || bytes.length === 0) return '';
let digits = [0];
for (let i = 0; i < bytes.length; i++) {
let carry = bytes[i];
for (let j = 0; j < digits.length; j++) {
carry += digits[j] << 8;
digits[j] = carry % 58;
carry = (carry / 58) | 0;
}
while (carry > 0) {
digits.push(carry % 58);
carry = (carry / 58) | 0;
}
}
let result = '';
for (let i = 0; i < bytes.length && bytes[i] === 0; i++) result += BASE58_ALPHABET[0];
for (let i = digits.length - 1; i >= 0; i--) result += BASE58_ALPHABET[digits[i]];
return result;
}
function base58Decode(str) {
if (!str || str.length === 0) return new Uint8Array();
// Use BigInt for correct base58 arithmetic (avoids JS number precision issues)
let num = BigInt(0);
const base = BigInt(58);
for (let i = 0; i < str.length; i++) {
const value = BASE58_ALPHABET.indexOf(str[i]);
if (value === -1) throw new Error('Invalid base58 character in Phantom response');
num = num * base + BigInt(value);
}
// Count leading '1's (which represent leading zero bytes)
let leadingZeros = 0;
for (let i = 0; i < str.length && str[i] === BASE58_ALPHABET[0]; i++) leadingZeros++;
// Convert BigInt to bytes
const bytes = [];
let temp = num;
if (temp === BigInt(0) && leadingZeros > 0) {
// All zeros case
for (let i = 0; i < leadingZeros; i++) bytes.push(0);
return new Uint8Array(bytes);
}
while (temp > BigInt(0)) {
bytes.push(Number(temp & BigInt(0xff)));
temp = temp >> BigInt(8);
}
// Add leading zeros and reverse
for (let i = 0; i < leadingZeros; i++) bytes.push(0);
bytes.reverse();
return new Uint8Array(bytes);
}
∞ IFC: 0
Distance: 0m