Skip to content

Commit 8790664

Browse files
authored
feat: add TypeScript definitions for bech32.js (#34)
1 parent 54ed642 commit 8790664

3 files changed

Lines changed: 97 additions & 2 deletions

File tree

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ Encode a `Uint8Array` to a base32hex string (RFC 4648)
449449

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

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

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

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

464+
Extract the prefix from a bech32 or bech32m string without full validation
465+
466+
This is a quick check that skips most validation.
467+
463468
#### `fromBech32(string, limit = 90)`
469+
470+
Decode a bech32 string to bytes
471+
464472
#### `toBech32(prefix, bytes, limit = 90)`
465473

474+
Encode bytes to a bech32 string
475+
466476
#### `fromBech32m(string, limit = 90)`
477+
478+
Decode a bech32m string to bytes
479+
467480
#### `toBech32m(prefix, bytes, limit = 90)`
468481

482+
Encode bytes to a bech32m string
483+
469484
### `@exodus/bytes/base58.js`
470485

471486
Implements [base58](https://www.ietf.org/archive/id/draft-msporny-base58-03.txt) encoding.

bech32.d.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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;

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"/base64.js",
9292
"/base64.d.ts",
9393
"/bech32.js",
94+
"/bech32.d.ts",
9495
"/bigint.js",
9596
"/bigint.d.ts",
9697
"/encoding-browser.js",
@@ -149,7 +150,10 @@
149150
"types": "./base64.d.ts",
150151
"default": "./base64.js"
151152
},
152-
"./bech32.js": "./bech32.js",
153+
"./bech32.js": {
154+
"types": "./bech32.d.ts",
155+
"default": "./bech32.js"
156+
},
153157
"./bigint.js": {
154158
"types": "./bigint.d.ts",
155159
"default": "./bigint.js"

0 commit comments

Comments
 (0)