* Encode a 128-bit unsigned integer as a fixed-length base58 string.
(n: bigint)
| 17 | * Encode a 128-bit unsigned integer as a fixed-length base58 string. |
| 18 | */ |
| 19 | function base58Encode(n: bigint): string { |
| 20 | const base = BigInt(BASE_58_CHARS.length) |
| 21 | const result = new Array<string>(ENCODED_LENGTH).fill(BASE_58_CHARS[0]!) |
| 22 | let i = ENCODED_LENGTH - 1 |
| 23 | let value = n |
| 24 | while (value > 0n) { |
| 25 | const rem = Number(value % base) |
| 26 | result[i] = BASE_58_CHARS[rem]! |
| 27 | value = value / base |
| 28 | i-- |
| 29 | } |
| 30 | return result.join('') |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Parse a UUID string (with or without hyphens) into a 128-bit bigint. |