// @ts-ignore import bs58 from "bs58" import { base64 } from "./base64" /** 编码数据到 base58 格式 * base58 格式是一种比 base64 更紧凑的编码格式,它不使用大小写字母 O 和 I,数字 0 和 1,以及 + 和 / 符号 * 更利于人类使用(可识别性更高,因为没有标点符号分隔便于浏览器中双击复制) * 详见:https://en.wikipedia.org/wiki/Base58 */ export const base58 = { /** 编码数据到 base58 格式 * base58 格式是一种比 base64 更紧凑的编码格式,它不使用大小写字母 O 和 I,数字 0 和 1,以及 + 和 / 符号 * 更利于人类使用(可识别性更高,因为没有标点符号分隔便于浏览器中双击复制) * 详见:https://en.wikipedia.org/wiki/Base58 */ encode(data: string | ArrayBuffer | Uint8Array): string { if (typeof data === "string") { data = new TextEncoder().encode(data) } else if (data instanceof ArrayBuffer) { data = new Uint8Array(data) } return bs58.encode(data) }, /** 解码一个 base58 字符串,返回字符串形式的结果 */ decode(data: string): string { let re = bs58.decode(data) return new TextDecoder().decode(re) }, /** 解码一个 base58 字符串,返回 Uint8Array 格式的数据 */ decodeUnit8Array(data: string): Uint8Array { return bs58.decode(data) }, /** 把一个 base64 字符串转换为 base58 */ base64toBase58(base64str: string) { let u8a = base64.decodeToUint8Array(base64str) return base58.encode(u8a) }, } const BASE58 = base58 export { BASE58 }