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