|
| 1 | +/** |
| 2 | + * Implements [base58check](https://en.bitcoin.it/wiki/Base58Check_encoding) encoding. |
| 3 | + * |
| 4 | + * ```js |
| 5 | + * import { fromBase58check, toBase58check } from '@exodus/bytes/base58check.js' |
| 6 | + * import { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js' |
| 7 | + * import { makeBase58check } from '@exodus/bytes/base58check.js' |
| 8 | + * ``` |
| 9 | + * |
| 10 | + * On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed. |
| 11 | + * |
| 12 | + * @module @exodus/bytes/base58check.js |
| 13 | + */ |
| 14 | + |
| 15 | +/// <reference types="node" /> |
| 16 | + |
| 17 | +import type { OutputFormat, Uint8ArrayBuffer } from './array.js'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Hash function type that takes Uint8Array and returns a Promise of Uint8Array |
| 21 | + */ |
| 22 | +export type HashFunction = (data: Uint8Array) => Promise<Uint8Array>; |
| 23 | + |
| 24 | +/** |
| 25 | + * Synchronous hash function type that takes Uint8Array and returns Uint8Array |
| 26 | + */ |
| 27 | +export type HashFunctionSync = (data: Uint8Array) => Uint8Array; |
| 28 | + |
| 29 | +/** |
| 30 | + * Base58Check encoder/decoder instance with async methods |
| 31 | + */ |
| 32 | +export interface Base58CheckAsync { |
| 33 | + /** |
| 34 | + * Encode bytes to base58check string asynchronously |
| 35 | + * |
| 36 | + * @param arr - The input bytes to encode |
| 37 | + * @returns A Promise that resolves to the base58check encoded string |
| 38 | + */ |
| 39 | + encode(arr: Uint8ArrayBuffer): Promise<string>; |
| 40 | + |
| 41 | + /** |
| 42 | + * Decode a base58check string to bytes asynchronously |
| 43 | + * |
| 44 | + * @param str - The base58check encoded string |
| 45 | + * @param format - Output format (default: 'uint8') |
| 46 | + * @returns A Promise that resolves to the decoded bytes |
| 47 | + */ |
| 48 | + decode(str: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>; |
| 49 | + decode(str: string, format: 'buffer'): Promise<Buffer>; |
| 50 | + decode(str: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | Buffer>; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Base58Check encoder/decoder instance with both async and sync methods |
| 55 | + */ |
| 56 | +export interface Base58CheckSync extends Base58CheckAsync { |
| 57 | + /** |
| 58 | + * Encode bytes to base58check string synchronously |
| 59 | + * |
| 60 | + * @param arr - The input bytes to encode |
| 61 | + * @returns The base58check encoded string |
| 62 | + */ |
| 63 | + encodeSync(arr: Uint8ArrayBuffer): string; |
| 64 | + |
| 65 | + /** |
| 66 | + * Decode a base58check string to bytes synchronously |
| 67 | + * |
| 68 | + * @param str - The base58check encoded string |
| 69 | + * @param format - Output format (default: 'uint8') |
| 70 | + * @returns The decoded bytes |
| 71 | + */ |
| 72 | + decodeSync(str: string, format?: 'uint8'): Uint8ArrayBuffer; |
| 73 | + decodeSync(str: string, format: 'buffer'): Buffer; |
| 74 | + decodeSync(str: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Create a base58check encoder/decoder with custom hash functions |
| 79 | + * |
| 80 | + * @param hashAlgo - Async hash function (typically double SHA-256) |
| 81 | + * @param hashAlgoSync - Optional sync hash function |
| 82 | + * @returns Base58Check encoder/decoder instance |
| 83 | + */ |
| 84 | +export function makeBase58check(hashAlgo: HashFunction, hashAlgoSync?: HashFunctionSync): Base58CheckSync; |
| 85 | +export function makeBase58check(hashAlgo: HashFunction): Base58CheckAsync; |
| 86 | + |
| 87 | +/** |
| 88 | + * Encode bytes to base58check string asynchronously |
| 89 | + * |
| 90 | + * Uses double SHA-256 for checksum calculation |
| 91 | + * |
| 92 | + * @param arr - The input bytes to encode |
| 93 | + * @returns A Promise that resolves to the base58check encoded string |
| 94 | + */ |
| 95 | +export function toBase58check(arr: Uint8ArrayBuffer): Promise<string>; |
| 96 | + |
| 97 | +/** |
| 98 | + * Decode a base58check string to bytes asynchronously |
| 99 | + * |
| 100 | + * Validates the checksum using double SHA-256 |
| 101 | + * |
| 102 | + * @param str - The base58check encoded string |
| 103 | + * @param format - Output format (default: 'uint8') |
| 104 | + * @returns A Promise that resolves to the decoded bytes |
| 105 | + */ |
| 106 | +export function fromBase58check(str: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>; |
| 107 | +export function fromBase58check(str: string, format: 'buffer'): Promise<Buffer>; |
| 108 | +export function fromBase58check(str: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | Buffer>; |
| 109 | + |
| 110 | +/** |
| 111 | + * Encode bytes to base58check string synchronously |
| 112 | + * |
| 113 | + * Uses double SHA-256 for checksum calculation |
| 114 | + * |
| 115 | + * @param arr - The input bytes to encode |
| 116 | + * @returns The base58check encoded string |
| 117 | + */ |
| 118 | +export function toBase58checkSync(arr: Uint8ArrayBuffer): string; |
| 119 | + |
| 120 | +/** |
| 121 | + * Decode a base58check string to bytes synchronously |
| 122 | + * |
| 123 | + * Validates the checksum using double SHA-256 |
| 124 | + * |
| 125 | + * @param str - The base58check encoded string |
| 126 | + * @param format - Output format (default: 'uint8') |
| 127 | + * @returns The decoded bytes |
| 128 | + */ |
| 129 | +export function fromBase58checkSync(str: string, format?: 'uint8'): Uint8ArrayBuffer; |
| 130 | +export function fromBase58checkSync(str: string, format: 'buffer'): Buffer; |
| 131 | +export function fromBase58checkSync(str: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer; |
0 commit comments