@exodus/bytes 1.9.0 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +417 -90
- package/array.d.ts +42 -3
- package/base32.d.ts +83 -0
- package/base58.d.ts +62 -0
- package/base58check.d.ts +131 -0
- package/base58check.js +2 -1
- package/base64.d.ts +40 -19
- package/bech32.d.ts +76 -0
- package/bigint.d.ts +48 -0
- package/encoding-browser.d.ts +23 -0
- package/encoding-lite.d.ts +61 -0
- package/encoding.d.ts +93 -11
- package/encoding.js +4 -3
- package/fallback/_utils.js +14 -11
- package/fallback/encoding.js +34 -42
- package/fallback/encoding.util.js +38 -8
- package/fallback/multi-byte.encodings.json +4 -3
- package/fallback/multi-byte.js +87 -16
- package/fallback/multi-byte.table.js +3 -0
- package/fallback/percent.js +31 -0
- package/hex.d.ts +22 -8
- package/index.d.ts +1 -1
- package/multi-byte.d.ts +64 -0
- package/package.json +63 -9
- package/single-byte.d.ts +159 -0
- package/utf16.d.ts +92 -0
- package/utf16.js +1 -1
- package/utf8.d.ts +72 -18
- package/utf8.js +11 -6
- package/utf8.node.js +1 -1
- package/whatwg.d.ts +48 -0
- package/whatwg.js +76 -0
- package/wif.d.ts +76 -0
- package/wif.js +1 -2
package/array.d.ts
CHANGED
|
@@ -1,21 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypedArray utils and conversions.
|
|
3
|
+
*
|
|
4
|
+
* ```js
|
|
5
|
+
* import { typedView } from '@exodus/bytes/array.js'
|
|
6
|
+
* ```
|
|
7
|
+
*
|
|
8
|
+
* @module @exodus/bytes/array.js
|
|
9
|
+
*/
|
|
10
|
+
|
|
1
11
|
/// <reference types="node" />
|
|
2
12
|
|
|
3
13
|
// >= TypeScript 5.9 made Uint8Array templated with <> and defaulted to ArrayBufferLike
|
|
4
14
|
// which would incorrectly accept SharedArrayBuffer instances.
|
|
5
15
|
// < TypeScript 5.7 doesn't support templates for Uint8Array.
|
|
6
16
|
// So this type is defined as a workaround to evaluate to Uint8Array<ArrayBuffer> on all versions of TypeScript.
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* This is `Uint8Array<ArrayBuffer>`
|
|
20
|
+
* (as opposed to `Uint8Array<SharedArrayBuffer>` and `Uint8Array<ArrayBufferLike>`)
|
|
21
|
+
* on TypeScript versions that support that distinction.
|
|
22
|
+
*
|
|
23
|
+
* On TypeScript < 5.7, this is just `Uint8Array`, as it's not a template there.
|
|
24
|
+
*/
|
|
7
25
|
export type Uint8ArrayBuffer = ReturnType<typeof Uint8Array.from>;
|
|
8
26
|
|
|
27
|
+
/**
|
|
28
|
+
* This is `Uint16Array<ArrayBuffer>`
|
|
29
|
+
* (as opposed to `Uint16Array<SharedArrayBuffer>` and `Uint16Array<ArrayBufferLike>`)
|
|
30
|
+
* on TypeScript versions that support that distinction.
|
|
31
|
+
*
|
|
32
|
+
* On TypeScript < 5.7, this is just `Uint16Array`, as it's not a template there.
|
|
33
|
+
*/
|
|
34
|
+
export type Uint16ArrayBuffer = ReturnType<typeof Uint16Array.from>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* This is `Uint32Array<ArrayBuffer>`
|
|
38
|
+
* (as opposed to `Uint32Array<SharedArrayBuffer>` and `Uint32Array<ArrayBufferLike>`)
|
|
39
|
+
* on TypeScript versions that support that distinction.
|
|
40
|
+
*
|
|
41
|
+
* On TypeScript < 5.7, this is just `Uint32Array`, as it's not a template there.
|
|
42
|
+
*/
|
|
43
|
+
export type Uint32ArrayBuffer = ReturnType<typeof Uint32Array.from>;
|
|
44
|
+
|
|
9
45
|
/**
|
|
10
46
|
* Output format for typed array conversions
|
|
11
47
|
*/
|
|
12
48
|
export type OutputFormat = 'uint8' | 'buffer';
|
|
13
49
|
|
|
14
50
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
51
|
+
* Create a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)
|
|
52
|
+
*
|
|
53
|
+
* > [!IMPORTANT]
|
|
54
|
+
* > Does not copy data, returns a view on the same underlying buffer
|
|
55
|
+
*
|
|
17
56
|
* @param arr - The input TypedArray
|
|
18
|
-
* @param format - The desired output format ('uint8' or 'buffer')
|
|
57
|
+
* @param format - The desired output format (`'uint8'` or `'buffer'`)
|
|
19
58
|
* @returns A view on the same underlying buffer
|
|
20
59
|
*/
|
|
21
60
|
export function typedView(arr: ArrayBufferView, format: 'uint8'): Uint8Array;
|
package/base32.d.ts
ADDED
|
@@ -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: Uint8Array, 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: Uint8Array, 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;
|
package/base58.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implements [base58](https://www.ietf.org/archive/id/draft-msporny-base58-03.txt) encoding.
|
|
3
|
+
*
|
|
4
|
+
* Supports both standard base58 and XRP variant alphabets.
|
|
5
|
+
*
|
|
6
|
+
* ```js
|
|
7
|
+
* import { fromBase58, toBase58 } from '@exodus/bytes/base58.js'
|
|
8
|
+
* import { fromBase58xrp, toBase58xrp } from '@exodus/bytes/base58.js'
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* @module @exodus/bytes/base58.js
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/// <reference types="node" />
|
|
15
|
+
|
|
16
|
+
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Encode a `Uint8Array` to a base58 string
|
|
20
|
+
*
|
|
21
|
+
* Uses the standard Bitcoin base58 alphabet
|
|
22
|
+
*
|
|
23
|
+
* @param arr - The input bytes
|
|
24
|
+
* @returns The base58 encoded string
|
|
25
|
+
*/
|
|
26
|
+
export function toBase58(arr: Uint8Array): string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Decode a base58 string to bytes
|
|
30
|
+
*
|
|
31
|
+
* Uses the standard Bitcoin base58 alphabet
|
|
32
|
+
*
|
|
33
|
+
* @param string - The base58 encoded string
|
|
34
|
+
* @param format - Output format (default: 'uint8')
|
|
35
|
+
* @returns The decoded bytes
|
|
36
|
+
*/
|
|
37
|
+
export function fromBase58(string: string, format?: 'uint8'): Uint8ArrayBuffer;
|
|
38
|
+
export function fromBase58(string: string, format: 'buffer'): Buffer;
|
|
39
|
+
export function fromBase58(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Encode a `Uint8Array` to a base58 string using XRP alphabet
|
|
43
|
+
*
|
|
44
|
+
* Uses the XRP variant base58 alphabet
|
|
45
|
+
*
|
|
46
|
+
* @param arr - The input bytes
|
|
47
|
+
* @returns The base58 encoded string
|
|
48
|
+
*/
|
|
49
|
+
export function toBase58xrp(arr: Uint8Array): string;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Decode a base58 string to bytes using XRP alphabet
|
|
53
|
+
*
|
|
54
|
+
* Uses the XRP variant base58 alphabet
|
|
55
|
+
*
|
|
56
|
+
* @param string - The base58 encoded string
|
|
57
|
+
* @param format - Output format (default: 'uint8')
|
|
58
|
+
* @returns The decoded bytes
|
|
59
|
+
*/
|
|
60
|
+
export function fromBase58xrp(string: string, format?: 'uint8'): Uint8ArrayBuffer;
|
|
61
|
+
export function fromBase58xrp(string: string, format: 'buffer'): Buffer;
|
|
62
|
+
export function fromBase58xrp(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
|
package/base58check.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implements [base58check](https://en.bitcoin.it/wiki/Base58Check_encoding) encoding.
|
|
3
|
+
*
|
|
4
|
+
* ```js
|
|
5
|
+
* import { fromBase58check, toBase58check } from '@exodus/bytes/base58check.js'
|
|
6
|
+
* import { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js'
|
|
7
|
+
* import { makeBase58check } from '@exodus/bytes/base58check.js'
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
|
|
11
|
+
*
|
|
12
|
+
* @module @exodus/bytes/base58check.js
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/// <reference types="node" />
|
|
16
|
+
|
|
17
|
+
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Hash function type that takes Uint8Array and returns a Promise of Uint8Array
|
|
21
|
+
*/
|
|
22
|
+
export type HashFunction = (data: Uint8Array) => Promise<Uint8Array>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Synchronous hash function type that takes Uint8Array and returns Uint8Array
|
|
26
|
+
*/
|
|
27
|
+
export type HashFunctionSync = (data: Uint8Array) => Uint8Array;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Base58Check encoder/decoder instance with async methods
|
|
31
|
+
*/
|
|
32
|
+
export interface Base58CheckAsync {
|
|
33
|
+
/**
|
|
34
|
+
* Encode bytes to base58check string asynchronously
|
|
35
|
+
*
|
|
36
|
+
* @param arr - The input bytes to encode
|
|
37
|
+
* @returns A Promise that resolves to the base58check encoded string
|
|
38
|
+
*/
|
|
39
|
+
encode(arr: Uint8Array): Promise<string>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Decode a base58check string to bytes asynchronously
|
|
43
|
+
*
|
|
44
|
+
* @param string - The base58check encoded string
|
|
45
|
+
* @param format - Output format (default: 'uint8')
|
|
46
|
+
* @returns A Promise that resolves to the decoded bytes
|
|
47
|
+
*/
|
|
48
|
+
decode(string: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
|
|
49
|
+
decode(string: string, format: 'buffer'): Promise<Buffer>;
|
|
50
|
+
decode(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | Buffer>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Base58Check encoder/decoder instance with both async and sync methods
|
|
55
|
+
*/
|
|
56
|
+
export interface Base58CheckSync extends Base58CheckAsync {
|
|
57
|
+
/**
|
|
58
|
+
* Encode bytes to base58check string synchronously
|
|
59
|
+
*
|
|
60
|
+
* @param arr - The input bytes to encode
|
|
61
|
+
* @returns The base58check encoded string
|
|
62
|
+
*/
|
|
63
|
+
encodeSync(arr: Uint8Array): string;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Decode a base58check string to bytes synchronously
|
|
67
|
+
*
|
|
68
|
+
* @param string - The base58check encoded string
|
|
69
|
+
* @param format - Output format (default: 'uint8')
|
|
70
|
+
* @returns The decoded bytes
|
|
71
|
+
*/
|
|
72
|
+
decodeSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
|
|
73
|
+
decodeSync(string: string, format: 'buffer'): Buffer;
|
|
74
|
+
decodeSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Create a base58check encoder/decoder with custom hash functions
|
|
79
|
+
*
|
|
80
|
+
* @param hashAlgo - Async hash function (typically double SHA-256)
|
|
81
|
+
* @param hashAlgoSync - Optional sync hash function
|
|
82
|
+
* @returns Base58Check encoder/decoder instance
|
|
83
|
+
*/
|
|
84
|
+
export function makeBase58check(hashAlgo: HashFunction, hashAlgoSync?: HashFunctionSync): Base58CheckSync;
|
|
85
|
+
export function makeBase58check(hashAlgo: HashFunction): Base58CheckAsync;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Encode bytes to base58check string asynchronously
|
|
89
|
+
*
|
|
90
|
+
* Uses double SHA-256 for checksum calculation
|
|
91
|
+
*
|
|
92
|
+
* @param arr - The input bytes to encode
|
|
93
|
+
* @returns A Promise that resolves to the base58check encoded string
|
|
94
|
+
*/
|
|
95
|
+
export function toBase58check(arr: Uint8Array): Promise<string>;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Decode a base58check string to bytes asynchronously
|
|
99
|
+
*
|
|
100
|
+
* Validates the checksum using double SHA-256
|
|
101
|
+
*
|
|
102
|
+
* @param string - The base58check encoded string
|
|
103
|
+
* @param format - Output format (default: 'uint8')
|
|
104
|
+
* @returns A Promise that resolves to the decoded bytes
|
|
105
|
+
*/
|
|
106
|
+
export function fromBase58check(string: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
|
|
107
|
+
export function fromBase58check(string: string, format: 'buffer'): Promise<Buffer>;
|
|
108
|
+
export function fromBase58check(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | Buffer>;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Encode bytes to base58check string synchronously
|
|
112
|
+
*
|
|
113
|
+
* Uses double SHA-256 for checksum calculation
|
|
114
|
+
*
|
|
115
|
+
* @param arr - The input bytes to encode
|
|
116
|
+
* @returns The base58check encoded string
|
|
117
|
+
*/
|
|
118
|
+
export function toBase58checkSync(arr: Uint8Array): string;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Decode a base58check string to bytes synchronously
|
|
122
|
+
*
|
|
123
|
+
* Validates the checksum using double SHA-256
|
|
124
|
+
*
|
|
125
|
+
* @param string - The base58check encoded string
|
|
126
|
+
* @param format - Output format (default: 'uint8')
|
|
127
|
+
* @returns The decoded bytes
|
|
128
|
+
*/
|
|
129
|
+
export function fromBase58checkSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
|
|
130
|
+
export function fromBase58checkSync(string: string, format: 'buffer'): Buffer;
|
|
131
|
+
export function fromBase58checkSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
|
package/base58check.js
CHANGED
|
@@ -4,7 +4,8 @@ import { makeBase58check } from './fallback/base58check.js'
|
|
|
4
4
|
// Note: while API is async, we use hashSync for now until we improve webcrypto perf for hash256
|
|
5
5
|
// Inputs to base58 are typically very small, and that makes a difference
|
|
6
6
|
|
|
7
|
-
//
|
|
7
|
+
// Note: using native WebCrypto will have to have account for SharedArrayBuffer
|
|
8
|
+
|
|
8
9
|
const hash256sync = (x) => sha256(sha256(x))
|
|
9
10
|
const hash256 = hash256sync // See note at the top
|
|
10
11
|
const {
|
package/base64.d.ts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implements base64 and base64url 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 { fromBase64, toBase64 } from '@exodus/bytes/base64.js'
|
|
7
|
+
* import { fromBase64url, toBase64url } from '@exodus/bytes/base64.js'
|
|
8
|
+
* import { fromBase64any } from '@exodus/bytes/base64.js'
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* @module @exodus/bytes/base64.js
|
|
12
|
+
*/
|
|
13
|
+
|
|
1
14
|
/// <reference types="node" />
|
|
2
15
|
|
|
3
16
|
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
|
|
@@ -12,9 +25,9 @@ export interface ToBase64Options {
|
|
|
12
25
|
|
|
13
26
|
/**
|
|
14
27
|
* Padding mode for base64 decoding
|
|
15
|
-
* - true
|
|
16
|
-
* - false
|
|
17
|
-
* - 'both'
|
|
28
|
+
* - `true`: padding is required
|
|
29
|
+
* - `false`: padding is not allowed (default for base64url)
|
|
30
|
+
* - `'both'`: padding is optional (default for base64)
|
|
18
31
|
*/
|
|
19
32
|
export type PaddingMode = boolean | 'both';
|
|
20
33
|
|
|
@@ -29,47 +42,55 @@ export interface FromBase64Options {
|
|
|
29
42
|
}
|
|
30
43
|
|
|
31
44
|
/**
|
|
32
|
-
*
|
|
45
|
+
* Encode a `Uint8Array` to a base64 string (RFC 4648)
|
|
46
|
+
*
|
|
33
47
|
* @param arr - The input bytes
|
|
34
48
|
* @param options - Encoding options
|
|
35
49
|
* @returns The base64 encoded string
|
|
36
50
|
*/
|
|
37
|
-
export function toBase64(arr:
|
|
51
|
+
export function toBase64(arr: Uint8Array, options?: ToBase64Options): string;
|
|
38
52
|
|
|
39
53
|
/**
|
|
40
|
-
*
|
|
54
|
+
* Encode a `Uint8Array` to a base64url string (RFC 4648)
|
|
55
|
+
*
|
|
41
56
|
* @param arr - The input bytes
|
|
42
57
|
* @param options - Encoding options (padding defaults to false)
|
|
43
58
|
* @returns The base64url encoded string
|
|
44
59
|
*/
|
|
45
|
-
export function toBase64url(arr:
|
|
60
|
+
export function toBase64url(arr: Uint8Array, options?: ToBase64Options): string;
|
|
46
61
|
|
|
47
62
|
/**
|
|
48
|
-
*
|
|
63
|
+
* Decode a base64 string to bytes
|
|
64
|
+
*
|
|
49
65
|
* Operates in strict mode for last chunk, does not allow whitespace
|
|
50
|
-
*
|
|
66
|
+
*
|
|
67
|
+
* @param string - The base64 encoded string
|
|
51
68
|
* @param options - Decoding options
|
|
52
69
|
* @returns The decoded bytes
|
|
53
70
|
*/
|
|
54
|
-
export function fromBase64(
|
|
55
|
-
export function fromBase64(
|
|
71
|
+
export function fromBase64(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
|
|
72
|
+
export function fromBase64(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
|
|
56
73
|
|
|
57
74
|
/**
|
|
58
|
-
*
|
|
75
|
+
* Decode a base64url string to bytes
|
|
76
|
+
*
|
|
59
77
|
* Operates in strict mode for last chunk, does not allow whitespace
|
|
60
|
-
*
|
|
78
|
+
*
|
|
79
|
+
* @param string - The base64url encoded string
|
|
61
80
|
* @param options - Decoding options (padding defaults to false)
|
|
62
81
|
* @returns The decoded bytes
|
|
63
82
|
*/
|
|
64
|
-
export function fromBase64url(
|
|
65
|
-
export function fromBase64url(
|
|
83
|
+
export function fromBase64url(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
|
|
84
|
+
export function fromBase64url(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
|
|
66
85
|
|
|
67
86
|
/**
|
|
68
|
-
*
|
|
87
|
+
* Decode either base64 or base64url string to bytes
|
|
88
|
+
*
|
|
69
89
|
* Automatically detects the variant based on characters present
|
|
70
|
-
*
|
|
90
|
+
*
|
|
91
|
+
* @param string - The base64 or base64url encoded string
|
|
71
92
|
* @param options - Decoding options
|
|
72
93
|
* @returns The decoded bytes
|
|
73
94
|
*/
|
|
74
|
-
export function fromBase64any(
|
|
75
|
-
export function fromBase64any(
|
|
95
|
+
export function fromBase64any(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
|
|
96
|
+
export function fromBase64any(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
|
package/bech32.d.ts
ADDED
|
@@ -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: Uint8Array, limit?: number): string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Decode a bech32 string to bytes
|
|
41
|
+
*
|
|
42
|
+
* @param string - 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(string: 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: Uint8Array, limit?: number): string;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Decode a bech32m string to bytes
|
|
60
|
+
*
|
|
61
|
+
* @param string - 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(string: 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 string - 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(string: string, limit?: number): string;
|
package/bigint.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert between BigInt and Uint8Array
|
|
3
|
+
*
|
|
4
|
+
* ```js
|
|
5
|
+
* import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
|
|
6
|
+
* ```
|
|
7
|
+
*
|
|
8
|
+
* @module @exodus/bytes/bigint.js
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/// <reference types="node" />
|
|
12
|
+
|
|
13
|
+
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Options for converting BigInt to bytes
|
|
17
|
+
*/
|
|
18
|
+
export interface FromBigIntOptions {
|
|
19
|
+
/** The length in bytes of the output array */
|
|
20
|
+
length: number;
|
|
21
|
+
/** Output format (default: 'uint8') */
|
|
22
|
+
format?: OutputFormat;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Convert a BigInt to a Uint8Array or Buffer
|
|
27
|
+
*
|
|
28
|
+
* The output bytes are in big-endian format.
|
|
29
|
+
*
|
|
30
|
+
* Throws if the BigInt is negative or cannot fit into the specified length.
|
|
31
|
+
*
|
|
32
|
+
* @param bigint - The BigInt to convert (must be non-negative)
|
|
33
|
+
* @param options - Conversion options
|
|
34
|
+
* @returns The converted bytes in big-endian format
|
|
35
|
+
*/
|
|
36
|
+
export function fromBigInt(bigint: bigint, options: { length: number; format?: 'uint8' }): Uint8ArrayBuffer;
|
|
37
|
+
export function fromBigInt(bigint: bigint, options: { length: number; format: 'buffer' }): Buffer;
|
|
38
|
+
export function fromBigInt(bigint: bigint, options: FromBigIntOptions): Uint8ArrayBuffer | Buffer;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Convert a Uint8Array or Buffer to a BigInt
|
|
42
|
+
*
|
|
43
|
+
* The bytes are interpreted as a big-endian unsigned integer.
|
|
44
|
+
*
|
|
45
|
+
* @param arr - The bytes to convert
|
|
46
|
+
* @returns The BigInt representation
|
|
47
|
+
*/
|
|
48
|
+
export function toBigInt(arr: Uint8Array): bigint;
|
package/encoding-browser.d.ts
CHANGED
|
@@ -1 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Same as `@exodus/bytes/encoding.js`, but in browsers instead of polyfilling just uses whatever the
|
|
3
|
+
* browser provides, drastically reducing the bundle size (to less than 2 KiB gzipped).
|
|
4
|
+
*
|
|
5
|
+
* ```js
|
|
6
|
+
* import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-browser.js'
|
|
7
|
+
* import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-browser.js' // Requires Streams
|
|
8
|
+
*
|
|
9
|
+
* // Hooks for standards
|
|
10
|
+
* import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-browser.js'
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* Under non-browser engines (Node.js, React Native, etc.) a full polyfill is used as those platforms
|
|
14
|
+
* do not provide sufficiently complete / non-buggy `TextDecoder` APIs.
|
|
15
|
+
*
|
|
16
|
+
* > [!NOTE]
|
|
17
|
+
* > Implementations in browsers [have bugs](https://docs.google.com/spreadsheets/d/1pdEefRG6r9fZy61WHGz0TKSt8cO4ISWqlpBN5KntIvQ/edit),
|
|
18
|
+
* > but they are fixing them and the expected update window is short.\
|
|
19
|
+
* > If you want to circumvent browser bugs, use full `@exodus/bytes/encoding.js` import.
|
|
20
|
+
*
|
|
21
|
+
* @module @exodus/bytes/encoding-browser.js
|
|
22
|
+
*/
|
|
23
|
+
|
|
1
24
|
export * from './encoding.js'
|
package/encoding-lite.d.ts
CHANGED
|
@@ -1 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The exact same exports as `@exodus/bytes/encoding.js` are also exported as
|
|
3
|
+
* `@exodus/bytes/encoding-lite.js`, with the difference that the lite version does not load
|
|
4
|
+
* multi-byte `TextDecoder` encodings by default to reduce bundle size 10x.
|
|
5
|
+
*
|
|
6
|
+
* ```js
|
|
7
|
+
* import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-lite.js'
|
|
8
|
+
* import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-lite.js' // Requires Streams
|
|
9
|
+
*
|
|
10
|
+
* // Hooks for standards
|
|
11
|
+
* import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-lite.js'
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* The only affected encodings are: `gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`
|
|
15
|
+
* and their [labels](https://encoding.spec.whatwg.org/#names-and-labels) when used with `TextDecoder`.
|
|
16
|
+
*
|
|
17
|
+
* Legacy single-byte encodingds are loaded by default in both cases.
|
|
18
|
+
*
|
|
19
|
+
* `TextEncoder` and hooks for standards (including `labelToName` / `normalizeEncoding`) do not have any behavior
|
|
20
|
+
* differences in the lite version and support full range if inputs.
|
|
21
|
+
*
|
|
22
|
+
* To avoid inconsistencies, the exported classes and methods are exactly the same objects.
|
|
23
|
+
*
|
|
24
|
+
* ```console
|
|
25
|
+
* > lite = require('@exodus/bytes/encoding-lite.js')
|
|
26
|
+
* [Module: null prototype] {
|
|
27
|
+
* TextDecoder: [class TextDecoder],
|
|
28
|
+
* TextDecoderStream: [class TextDecoderStream],
|
|
29
|
+
* TextEncoder: [class TextEncoder],
|
|
30
|
+
* TextEncoderStream: [class TextEncoderStream],
|
|
31
|
+
* getBOMEncoding: [Function: getBOMEncoding],
|
|
32
|
+
* labelToName: [Function: labelToName],
|
|
33
|
+
* legacyHookDecode: [Function: legacyHookDecode],
|
|
34
|
+
* normalizeEncoding: [Function: normalizeEncoding]
|
|
35
|
+
* }
|
|
36
|
+
* > new lite.TextDecoder('big5').decode(Uint8Array.of(0x25))
|
|
37
|
+
* Uncaught:
|
|
38
|
+
* Error: Legacy multi-byte encodings are disabled in /encoding-lite.js, use /encoding.js for full encodings range support
|
|
39
|
+
*
|
|
40
|
+
* > full = require('@exodus/bytes/encoding.js')
|
|
41
|
+
* [Module: null prototype] {
|
|
42
|
+
* TextDecoder: [class TextDecoder],
|
|
43
|
+
* TextDecoderStream: [class TextDecoderStream],
|
|
44
|
+
* TextEncoder: [class TextEncoder],
|
|
45
|
+
* TextEncoderStream: [class TextEncoderStream],
|
|
46
|
+
* getBOMEncoding: [Function: getBOMEncoding],
|
|
47
|
+
* labelToName: [Function: labelToName],
|
|
48
|
+
* legacyHookDecode: [Function: legacyHookDecode],
|
|
49
|
+
* normalizeEncoding: [Function: normalizeEncoding]
|
|
50
|
+
* }
|
|
51
|
+
* > full.TextDecoder === lite.TextDecoder
|
|
52
|
+
* true
|
|
53
|
+
* > new full.TextDecoder('big5').decode(Uint8Array.of(0x25))
|
|
54
|
+
* '%'
|
|
55
|
+
* > new lite.TextDecoder('big5').decode(Uint8Array.of(0x25))
|
|
56
|
+
* '%'
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @module @exodus/bytes/encoding-lite.js
|
|
60
|
+
*/
|
|
61
|
+
|
|
1
62
|
export * from './encoding.js'
|