// used for Byron Era Address Support // copied from https://github.com/pur3miish/base58-js import { Buffer } from "buffer"; const base58_chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; export const base58 = { encode(data: Buffer): string { return binary_to_base58(Buffer.from(data)); }, decode(data: string): Buffer { return Buffer.from(base58_to_binary(data)); }, }; function base58_to_binary(base58String:string) { if (!base58String || typeof base58String !== "string") throw new Error(`Expected base58 string but got “${typeof base58String}”`); if (base58String.match(/[IOl0]/gmu)) throw new Error( `Invalid base58 character “${base58String.match(/[IOl0]/gmu)}”` ); const lz = base58String.match(/^1+/gmu); const psz = lz ? lz[0].length : 0; const size = ((base58String.length - psz) * (Math.log(58) / Math.log(256)) + 1) >>> 0; return new Uint8Array([ ...new Uint8Array(psz), ...base58String .match(/.{1}/gmu)! .map((i) => base58_chars.indexOf(i)) .reduce((acc, i) => { acc = acc.map((j) => { const x = j * 58 + i; i = x >> 8; return x; }); return acc; }, new Uint8Array(size)) .reverse() .filter( ( (lastValue) => (value) => // @ts-ignore (lastValue = lastValue || value) )(false) ), ]); } function binary_to_base58(uint8array:Uint8Array) { const result:any[] = []; for (const byte of uint8array) { let carry = byte; for (let j = 0; j < result.length; ++j) { // @ts-ignore const x = (base58Map[result[j]] << 8) + carry; result[j] = base58_chars.charCodeAt(x % 58); carry = (x / 58) | 0; } while (carry) { result.push(base58_chars.charCodeAt(carry % 58)); carry = (carry / 58) | 0; } } for (const byte of uint8array) if (byte) break; else result.push("1".charCodeAt(0)); result.reverse(); return String.fromCharCode(...result); }