Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ Encode a `Uint8Array` to a base32hex string (RFC 4648)

### `@exodus/bytes/bech32.js`

Implements [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification)
Implements bech32 and bech32m from
[BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification)
and [BIP-0350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki#specification).

```js
Expand All @@ -460,12 +461,26 @@ import { getPrefix } from '@exodus/bytes/bech32.js'

#### `getPrefix(string, limit = 90)`

Extract the prefix from a bech32 or bech32m string without full validation

This is a quick check that skips most validation.

#### `fromBech32(string, limit = 90)`

Decode a bech32 string to bytes

#### `toBech32(prefix, bytes, limit = 90)`

Encode bytes to a bech32 string

#### `fromBech32m(string, limit = 90)`

Decode a bech32m string to bytes

#### `toBech32m(prefix, bytes, limit = 90)`

Encode bytes to a bech32m string

### `@exodus/bytes/base58.js`

Implements [base58](https://www.ietf.org/archive/id/draft-msporny-base58-03.txt) encoding.
Expand Down
76 changes: 76 additions & 0 deletions bech32.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Implements bech32 and bech32m from
* [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification)
* and [BIP-0350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki#specification).
*
* ```js
* import { fromBech32, toBech32 } from '@exodus/bytes/bech32.js'
* import { fromBech32m, toBech32m } from '@exodus/bytes/bech32.js'
* import { getPrefix } from '@exodus/bytes/bech32.js'
* ```
*
* @module @exodus/bytes/bech32.js
*/

/// <reference types="node" />

import type { Uint8ArrayBuffer } from './array.js';

/**
* Result of decoding a bech32 or bech32m string
*/
export interface Bech32DecodeResult {
/** The human-readable prefix */
prefix: string;
/** The decoded bytes */
bytes: Uint8ArrayBuffer;
}

/**
* Encode bytes to a bech32 string
*
* @param prefix - The human-readable prefix (e.g., 'bc' for Bitcoin)
* @param bytes - The input bytes to encode
* @param limit - Maximum length of the encoded string (default: 90)
* @returns The bech32 encoded string
*/
export function toBech32(prefix: string, bytes: Uint8ArrayBuffer, limit?: number): string;

/**
* Decode a bech32 string to bytes
*
* @param str - The bech32 encoded string
* @param limit - Maximum length of the input string (default: 90)
* @returns The decoded prefix and bytes
*/
export function fromBech32(str: string, limit?: number): Bech32DecodeResult;

/**
* Encode bytes to a bech32m string
*
* @param prefix - The human-readable prefix (e.g., 'bc' for Bitcoin)
* @param bytes - The input bytes to encode
* @param limit - Maximum length of the encoded string (default: 90)
* @returns The bech32m encoded string
*/
export function toBech32m(prefix: string, bytes: Uint8ArrayBuffer, limit?: number): string;

/**
* Decode a bech32m string to bytes
*
* @param str - The bech32m encoded string
* @param limit - Maximum length of the input string (default: 90)
* @returns The decoded prefix and bytes
*/
export function fromBech32m(str: string, limit?: number): Bech32DecodeResult;

/**
* Extract the prefix from a bech32 or bech32m string without full validation
*
* This is a quick check that skips most validation.
*
* @param str - The bech32/bech32m encoded string
* @param limit - Maximum length of the input string (default: 90)
* @returns The lowercase prefix
*/
export function getPrefix(str: string, limit?: number): string;
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"/base64.js",
"/base64.d.ts",
"/bech32.js",
"/bech32.d.ts",
"/bigint.js",
"/bigint.d.ts",
"/encoding-browser.js",
Expand Down Expand Up @@ -149,7 +150,10 @@
"types": "./base64.d.ts",
"default": "./base64.js"
},
"./bech32.js": "./bech32.js",
"./bech32.js": {
"types": "./bech32.d.ts",
"default": "./bech32.js"
},
"./bigint.js": {
"types": "./bigint.d.ts",
"default": "./bigint.js"
Expand Down
Loading