|
| 1 | +/** |
| 2 | + * Implements bech32 and bech32m from |
| 3 | + * [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification) |
| 4 | + * and [BIP-0350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki#specification). |
| 5 | + * |
| 6 | + * ```js |
| 7 | + * import { fromBech32, toBech32 } from '@exodus/bytes/bech32.js' |
| 8 | + * import { fromBech32m, toBech32m } from '@exodus/bytes/bech32.js' |
| 9 | + * import { getPrefix } from '@exodus/bytes/bech32.js' |
| 10 | + * ``` |
| 11 | + * |
| 12 | + * @module @exodus/bytes/bech32.js |
| 13 | + */ |
| 14 | + |
| 15 | +/// <reference types="node" /> |
| 16 | + |
| 17 | +import type { Uint8ArrayBuffer } from './array.js'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Result of decoding a bech32 or bech32m string |
| 21 | + */ |
| 22 | +export interface Bech32DecodeResult { |
| 23 | + /** The human-readable prefix */ |
| 24 | + prefix: string; |
| 25 | + /** The decoded bytes */ |
| 26 | + bytes: Uint8ArrayBuffer; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Encode bytes to a bech32 string |
| 31 | + * |
| 32 | + * @param prefix - The human-readable prefix (e.g., 'bc' for Bitcoin) |
| 33 | + * @param bytes - The input bytes to encode |
| 34 | + * @param limit - Maximum length of the encoded string (default: 90) |
| 35 | + * @returns The bech32 encoded string |
| 36 | + */ |
| 37 | +export function toBech32(prefix: string, bytes: Uint8ArrayBuffer, limit?: number): string; |
| 38 | + |
| 39 | +/** |
| 40 | + * Decode a bech32 string to bytes |
| 41 | + * |
| 42 | + * @param str - The bech32 encoded string |
| 43 | + * @param limit - Maximum length of the input string (default: 90) |
| 44 | + * @returns The decoded prefix and bytes |
| 45 | + */ |
| 46 | +export function fromBech32(str: string, limit?: number): Bech32DecodeResult; |
| 47 | + |
| 48 | +/** |
| 49 | + * Encode bytes to a bech32m string |
| 50 | + * |
| 51 | + * @param prefix - The human-readable prefix (e.g., 'bc' for Bitcoin) |
| 52 | + * @param bytes - The input bytes to encode |
| 53 | + * @param limit - Maximum length of the encoded string (default: 90) |
| 54 | + * @returns The bech32m encoded string |
| 55 | + */ |
| 56 | +export function toBech32m(prefix: string, bytes: Uint8ArrayBuffer, limit?: number): string; |
| 57 | + |
| 58 | +/** |
| 59 | + * Decode a bech32m string to bytes |
| 60 | + * |
| 61 | + * @param str - The bech32m encoded string |
| 62 | + * @param limit - Maximum length of the input string (default: 90) |
| 63 | + * @returns The decoded prefix and bytes |
| 64 | + */ |
| 65 | +export function fromBech32m(str: string, limit?: number): Bech32DecodeResult; |
| 66 | + |
| 67 | +/** |
| 68 | + * Extract the prefix from a bech32 or bech32m string without full validation |
| 69 | + * |
| 70 | + * This is a quick check that skips most validation. |
| 71 | + * |
| 72 | + * @param str - The bech32/bech32m encoded string |
| 73 | + * @param limit - Maximum length of the input string (default: 90) |
| 74 | + * @returns The lowercase prefix |
| 75 | + */ |
| 76 | +export function getPrefix(str: string, limit?: number): string; |
0 commit comments