Skip to content

Commit 40f1bf6

Browse files
committed
feat: add types for base32
1 parent 8f308ba commit 40f1bf6

File tree

4 files changed

+107
-5
lines changed

4 files changed

+107
-5
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ Encode a `Uint8Array` to a lowercase hex string
326326

327327
### `@exodus/bytes/base64.js`
328328

329-
Implements Base64 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
329+
Implements base64 and base64url from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
330330
(no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
331331

332332
```js
@@ -363,7 +363,7 @@ Encode a `Uint8Array` to a base64url string (RFC 4648)
363363

364364
### `@exodus/bytes/base32.js`
365365

366-
Implements Base32 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
366+
Implements base32 and base32hex from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
367367
(no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
368368

369369
```js
@@ -372,10 +372,25 @@ import { fromBase32hex, toBase32hex } from '@exodus/bytes/base32.js'
372372
```
373373

374374
#### `fromBase32(string, { format = 'uint8', padding = 'both' })`
375+
376+
Decode a base32 string to bytes
377+
378+
Operates in strict mode for last chunk, does not allow whitespace
379+
375380
#### `fromBase32hex(string, { format = 'uint8', padding = 'both' })`
381+
382+
Decode a base32hex string to bytes
383+
384+
Operates in strict mode for last chunk, does not allow whitespace
385+
376386
#### `toBase32(arr, { padding = false })`
387+
388+
Encode a `Uint8Array` to a base32 string (RFC 4648)
389+
377390
#### `toBase32hex(arr, { padding = false })`
378391

392+
Encode a `Uint8Array` to a base32hex string (RFC 4648)
393+
379394
### `@exodus/bytes/bech32.js`
380395

381396
Implements [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification)

base32.d.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Implements base32 and base32hex from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
3+
* (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
4+
*
5+
* ```js
6+
* import { fromBase32, toBase32 } from '@exodus/bytes/base32.js'
7+
* import { fromBase32hex, toBase32hex } from '@exodus/bytes/base32.js'
8+
* ```
9+
*
10+
* @module @exodus/bytes/base32.js
11+
*/
12+
13+
/// <reference types="node" />
14+
15+
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
16+
17+
/**
18+
* Options for base32 encoding
19+
*/
20+
export interface ToBase32Options {
21+
/** Whether to include padding characters (default: false) */
22+
padding?: boolean;
23+
}
24+
25+
/**
26+
* Padding mode for base32 decoding
27+
* - `true`: padding is required
28+
* - `false`: padding is not allowed
29+
* - `'both'`: padding is optional (default)
30+
*/
31+
export type PaddingMode = boolean | 'both';
32+
33+
/**
34+
* Options for base32 decoding
35+
*/
36+
export interface FromBase32Options {
37+
/** Output format (default: 'uint8') */
38+
format?: OutputFormat;
39+
/** Padding mode */
40+
padding?: PaddingMode;
41+
}
42+
43+
/**
44+
* Encode a `Uint8Array` to a base32 string (RFC 4648)
45+
*
46+
* @param arr - The input bytes
47+
* @param options - Encoding options
48+
* @returns The base32 encoded string
49+
*/
50+
export function toBase32(arr: Uint8ArrayBuffer, options?: ToBase32Options): string;
51+
52+
/**
53+
* Encode a `Uint8Array` to a base32hex string (RFC 4648)
54+
*
55+
* @param arr - The input bytes
56+
* @param options - Encoding options (padding defaults to false)
57+
* @returns The base32hex encoded string
58+
*/
59+
export function toBase32hex(arr: Uint8ArrayBuffer, options?: ToBase32Options): string;
60+
61+
/**
62+
* Decode a base32 string to bytes
63+
*
64+
* Operates in strict mode for last chunk, does not allow whitespace
65+
*
66+
* @param string - The base32 encoded string
67+
* @param options - Decoding options
68+
* @returns The decoded bytes
69+
*/
70+
export function fromBase32(string: string, options?: FromBase32Options): Uint8ArrayBuffer;
71+
export function fromBase32(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
72+
73+
/**
74+
* Decode a base32hex string to bytes
75+
*
76+
* Operates in strict mode for last chunk, does not allow whitespace
77+
*
78+
* @param string - The base32hex encoded string
79+
* @param options - Decoding options
80+
* @returns The decoded bytes
81+
*/
82+
export function fromBase32hex(string: string, options?: FromBase32Options): Uint8ArrayBuffer;
83+
export function fromBase32hex(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;

base64.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Implements Base64 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
2+
* Implements base64 and base64url from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
33
* (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
44
*
55
* ```js
@@ -26,7 +26,7 @@ export interface ToBase64Options {
2626
/**
2727
* Padding mode for base64 decoding
2828
* - `true`: padding is required
29-
* - `false`: padding is not allowed
29+
* - `false`: padding is not allowed (default for base64url)
3030
* - `'both'`: padding is optional (default for base64)
3131
*/
3232
export type PaddingMode = boolean | 'both';

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"/array.d.ts",
8383
"/assert.js",
8484
"/base32.js",
85+
"/base32.d.ts",
8586
"/base58.js",
8687
"/base58check.js",
8788
"/base58check.node.js",
@@ -125,7 +126,10 @@
125126
"types": "./array.d.ts",
126127
"default": "./array.js"
127128
},
128-
"./base32.js": "./base32.js",
129+
"./base32.js": {
130+
"types": "./base32.d.ts",
131+
"default": "./base32.js"
132+
},
129133
"./base58.js": "./base58.js",
130134
"./base58check.js": {
131135
"node": "./base58check.node.js",

0 commit comments

Comments
 (0)