export async function bytesToBase64DataUrl(bytes: Uint8Array, type = "application/octet-stream") { return await new Promise((resolve, reject) => { const reader = Object.assign(new FileReader(), { onload: () => resolve(reader.result), onerror: () => reject(reader.error), }); reader.readAsDataURL(new File([bytes], "", { type })); }); } export async function arrayBufferToBase64(buffer: ArrayBuffer) { var bytes = new Uint8Array(buffer); return (await bytesToBase64DataUrl(bytes) as string).replace("data:application/octet-stream;base64,", ""); } // async function base64ToArrayBuffer(base64String: string) { // let response = await fetch("data:application/octet-stream;base64," + base64String); // let arrayBuffer = await response.arrayBuffer(); // return arrayBuffer; // } export async function compressString(input: string) { // Convert the string to a Uint8Array const textEncoder = new TextEncoder(); const inputArray = textEncoder.encode(input); // Create a CompressionStream const compressionStream = new CompressionStream('gzip'); const writer = compressionStream.writable.getWriter(); // Write the data and close the stream writer.write(inputArray); writer.close(); // Read the compressed data from the stream const compressedArray = await new Response(compressionStream.readable).arrayBuffer(); // Convert the compressed data to a Uint8Array return compressedArray; } // Base58 character set // const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; // Base58 encoding // Base58 encoding // function encodeBase58(buffer: Uint8Array): string { // let carry; // const digits = [0]; // for (const byte of buffer) { // carry = byte; // for (let i = 0; i < digits.length; i++) { // carry += digits[i] << 8; // digits[i] = carry % 58; // carry = Math.floor(carry / 58); // } // while (carry > 0) { // digits.push(carry % 58); // carry = Math.floor(carry / 58); // } // } // let result = ''; // for (const digit of digits.reverse()) { // result += BASE58_ALPHABET[digit]; // } // // Handle leading zero bytes // for (const byte of buffer) { // if (byte === 0x00) { // result = BASE58_ALPHABET[0] + result; // } else { // break; // } // } // return result; // } // Convert UUID v4 to Base58 // function uuidToBase58(uuid: string): string { // const bytes = uuidToBytes(uuid); // return encodeBase58(bytes); // }