Skip to content

Commit 257f5d4

Browse files
CopilotChALkeR
andcommitted
Add TypeScript definitions for bech32.js module
Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
1 parent a4815c6 commit 257f5d4

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
coverage
44
playground
55
*.tgz
6+
package-lock.json

bech32.d.ts

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

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)