export namespace base58 { /** * Encode Uint8Array in base58. * @param bytes Byte array of type Uint8Array. */ export function encode(source: Uint8Array): string { // Code converted from: // https://github.com/cryptocoinjs/base-x/blob/master/index.js const iFACTOR = 2; // TODO: Calculate precise value to avoid overallocating const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; let BASE = ALPHABET.length; let LEADER = ALPHABET.charAt(0); // Skip & count leading zeroes. let zeroes = 0 let length = 0 let pbegin = 0 let pend = source.length while (pbegin !== pend && source[pbegin] === 0) { pbegin++ zeroes++ } // Allocate enough space in big-endian base58 representation. let size = ((pend - pbegin) * iFACTOR + 1) >>> 0 let b58 = new Uint8Array(size) // Process the bytes. while (pbegin !== pend) { let carry = i32(source[pbegin]) // Apply "b58 = b58 * 256 + ch". let i = 0 for (let it = size - 1; (carry !== 0 || i < length) && (it !== -1); it--, i++) { carry += (256 * b58[it]) >>> 0 b58[it] = (carry % BASE) >>> 0 carry = (carry / BASE) >>> 0 } assert(carry == 0, 'Non-zero carry'); length = i pbegin++ } // Skip leading zeroes in base58 result. let it = size - length while (it !== size && b58[it] === 0) { it++ } // Translate the result into a string. let str = LEADER.repeat(zeroes) for (; it < size; ++it) str += ALPHABET.charAt(b58[it]) return str } }