@exodus/bytes 1.9.0 → 1.10.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 CHANGED
@@ -4,6 +4,7 @@
4
4
  [![](https://flat.badgen.net/github/release/ExodusOSS/bytes?icon=github)](https://github.com/ExodusOSS/bytes/releases)
5
5
  [![](https://flat.badgen.net/npm/dm/@exodus/bytes)](https://www.npmcharts.com/compare/@exodus/bytes?minimal=true)
6
6
  [![](https://flat.badgen.net/npm/license/@exodus/bytes)](https://github.com/ExodusOSS/bytes/blob/HEAD/LICENSE)
7
+ [![](https://flat.badgen.net/github/checks/ExodusOSS/bytes/main?icon=github)](https://github.com/ExodusOSS/bytes/actions/workflows/test.yml?query=branch%3Amain)
7
8
 
8
9
  `Uint8Array` conversion to and from `base64`, `base32`, `base58`, `hex`, `utf8`, `utf16`, `bech32` and `wif`
9
10
 
@@ -99,6 +100,8 @@ See [the list of encodings](https://encoding.spec.whatwg.org/#names-and-labels).
99
100
 
100
101
  ### `@exodus/bytes/utf8.js`
101
102
 
103
+ UTF-8 encoding/decoding
104
+
102
105
  ```js
103
106
  import { utf8fromString, utf8toString } from '@exodus/bytes/utf8.js'
104
107
 
@@ -106,13 +109,45 @@ import { utf8fromString, utf8toString } from '@exodus/bytes/utf8.js'
106
109
  import { utf8fromStringLoose, utf8toStringLoose } from '@exodus/bytes/utf8.js'
107
110
  ```
108
111
 
109
- ##### `utf8fromString(str, format = 'uint8')`
110
- ##### `utf8fromStringLoose(str, format = 'uint8')`
111
- ##### `utf8toString(arr)`
112
- ##### `utf8toStringLoose(arr)`
112
+ _These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._\
113
+ _If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_
114
+
115
+ #### `utf8fromString(string, format = 'uint8')`
116
+
117
+ Encode a string to UTF-8 bytes (strict mode)
118
+
119
+ Throws on invalid Unicode (unpaired surrogates)
120
+
121
+ #### `utf8fromStringLoose(string, format = 'uint8')`
122
+
123
+ Encode a string to UTF-8 bytes (loose mode)
124
+
125
+ Replaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD`
126
+ per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
127
+
128
+ _Such replacement is a non-injective function, is irreversable and causes collisions.\
129
+ Prefer using strict throwing methods for cryptography applications._
130
+
131
+ #### `utf8toString(arr)`
132
+
133
+ Decode UTF-8 bytes to a string (strict mode)
134
+
135
+ Throws on invalid UTF-8 byte sequences
136
+
137
+ #### `utf8toStringLoose(arr)`
138
+
139
+ Decode UTF-8 bytes to a string (loose mode)
140
+
141
+ Replaces invalid UTF-8 byte sequences with replacement codepoints `U+FFFD`
142
+ per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
143
+
144
+ _Such replacement is a non-injective function, is irreversable and causes collisions.\
145
+ Prefer using strict throwing methods for cryptography applications._
113
146
 
114
147
  ### `@exodus/bytes/utf16.js`
115
148
 
149
+ UTF-16 encoding/decoding
150
+
116
151
  ```js
117
152
  import { utf16fromString, utf16toString } from '@exodus/bytes/utf16.js'
118
153
 
@@ -120,18 +155,46 @@ import { utf16fromString, utf16toString } from '@exodus/bytes/utf16.js'
120
155
  import { utf16fromStringLoose, utf16toStringLoose } from '@exodus/bytes/utf16.js'
121
156
  ```
122
157
 
123
- ##### `utf16fromString(str, format = 'uint16')`
124
- ##### `utf16fromStringLoose(str, format = 'uint16')`
125
- ##### `utf16toString(arr, 'uint16')`
126
- ##### `utf16toStringLoose(arr, 'uint16')`
158
+ _These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._\
159
+ _If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_
127
160
 
128
- ### `@exodus/bytes/single-byte.js`
161
+ #### `utf16fromString(string, format = 'uint16')`
129
162
 
130
- ```js
131
- import { createSinglebyteDecoder, createSinglebyteEncoder } from '@exodus/bytes/single-byte.js'
132
- import { windows1252toString, windows1252fromString } from '@exodus/bytes/single-byte.js'
133
- import { latin1toString, latin1fromString } from '@exodus/bytes/single-byte.js'
134
- ```
163
+ Encode a string to UTF-16 bytes (strict mode)
164
+
165
+ Throws on invalid Unicode (unpaired surrogates)
166
+
167
+ #### `utf16fromStringLoose(string, format = 'uint16')`
168
+
169
+ Encode a string to UTF-16 bytes (loose mode)
170
+
171
+ Replaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD`
172
+ per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
173
+
174
+ _Such replacement is a non-injective function, is irreversible and causes collisions.\
175
+ Prefer using strict throwing methods for cryptography applications._
176
+
177
+ #### `utf16toString(arr, format = 'uint16')`
178
+
179
+ Decode UTF-16 bytes to a string (strict mode)
180
+
181
+ Throws on invalid UTF-16 byte sequences
182
+
183
+ Throws on non-even byte length.
184
+
185
+ #### `utf16toStringLoose(arr, format = 'uint16')`
186
+
187
+ Decode UTF-16 bytes to a string (loose mode)
188
+
189
+ Replaces invalid UTF-16 byte sequences with replacement codepoints `U+FFFD`
190
+ per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
191
+
192
+ _Such replacement is a non-injective function, is irreversible and causes collisions.\
193
+ Prefer using strict throwing methods for cryptography applications._
194
+
195
+ Throws on non-even byte length.
196
+
197
+ ### `@exodus/bytes/single-byte.js`
135
198
 
136
199
  Decode / encode the legacy single-byte encodings according to the
137
200
  [Encoding standard](https://encoding.spec.whatwg.org/)
@@ -139,6 +202,12 @@ Decode / encode the legacy single-byte encodings according to the
139
202
  [§14.5](https://encoding.spec.whatwg.org/#x-user-defined)),
140
203
  and [unicode.org](https://unicode.org/Public/MAPPINGS/ISO8859) `iso-8859-*` mappings.
141
204
 
205
+ ```js
206
+ import { createSinglebyteDecoder, createSinglebyteEncoder } from '@exodus/bytes/single-byte.js'
207
+ import { windows1252toString, windows1252fromString } from '@exodus/bytes/single-byte.js'
208
+ import { latin1toString, latin1fromString } from '@exodus/bytes/single-byte.js'
209
+ ```
210
+
142
211
  Supports all single-byte encodings listed in the WHATWG Encoding standard:
143
212
  `ibm866`, `iso-8859-2`, `iso-8859-3`, `iso-8859-4`, `iso-8859-5`, `iso-8859-6`, `iso-8859-7`, `iso-8859-8`,
144
213
  `iso-8859-8-i`, `iso-8859-10`, `iso-8859-13`, `iso-8859-14`, `iso-8859-15`, `iso-8859-16`, `koi8-r`, `koi8-u`,
@@ -174,13 +243,13 @@ corresponding [unicode.org encodings](https://unicode.org/Public/MAPPINGS/VENDOR
174
243
  they encode/decode all the old valid (non-replacement) strings / byte sequences identically, but can also support
175
244
  a wider range of inputs.
176
245
 
177
- ##### `createSinglebyteDecoder(encoding, loose = false)`
246
+ #### `createSinglebyteDecoder(encoding, loose = false)`
178
247
 
179
248
  Create a decoder for a supported one-byte `encoding`, given its lowercased name `encoding`.
180
249
 
181
250
  Returns a function `decode(arr)` that decodes bytes to a string.
182
251
 
183
- ##### `createSinglebyteEncoder(encoding, { mode = 'fatal' })`
252
+ #### `createSinglebyteEncoder(encoding, { mode = 'fatal' })`
184
253
 
185
254
  Create an encoder for a supported one-byte `encoding`, given its lowercased name `encoding`.
186
255
 
@@ -189,7 +258,7 @@ Returns a function `encode(string)` that encodes a string to bytes.
189
258
  In `'fatal'` mode (default), will throw on non well-formed strings or any codepoints which could
190
259
  not be encoded in the target encoding.
191
260
 
192
- ##### `latin1toString(arr)`
261
+ #### `latin1toString(arr)`
193
262
 
194
263
  Decode `iso-8859-1` bytes to a string.
195
264
 
@@ -203,18 +272,18 @@ const latin1toString = createSinglebyteDecoder('iso-8859-1')
203
272
  Note: this is different from `new TextDecoder('iso-8859-1')` and `new TextDecoder('latin1')`, as
204
273
  those alias to `new TextDecoder('windows-1252')`.
205
274
 
206
- ##### `latin1fromString(string)`
275
+ #### `latin1fromString(string)`
207
276
 
208
277
  Encode a string to `iso-8859-1` bytes.
209
278
 
210
- Will throw on non well-formed strings or any codepoints which could not be encoded in `iso-8859-1`.
279
+ Throws on non well-formed strings or any codepoints which could not be encoded in `iso-8859-1`.
211
280
 
212
281
  Same as:
213
282
  ```js
214
283
  const latin1fromString = createSinglebyteEncoder('iso-8859-1', { mode: 'fatal' })
215
284
  ```
216
285
 
217
- ##### `windows1252toString(arr)`
286
+ #### `windows1252toString(arr)`
218
287
 
219
288
  Decode `windows-1252` bytes to a string.
220
289
 
@@ -225,11 +294,11 @@ Same as:
225
294
  const windows1252toString = createSinglebyteDecoder('windows-1252')
226
295
  ```
227
296
 
228
- ##### `windows1252fromString(string)`
297
+ #### `windows1252fromString(string)`
229
298
 
230
299
  Encode a string to `windows-1252` bytes.
231
300
 
232
- Will throw on non well-formed strings or any codepoints which could not be encoded in `windows-1252`.
301
+ Throws on non well-formed strings or any codepoints which could not be encoded in `windows-1252`.
233
302
 
234
303
  Same as:
235
304
  ```js
@@ -238,50 +307,84 @@ const windows1252fromString = createSinglebyteEncoder('windows-1252', { mode: 'f
238
307
 
239
308
  ### `@exodus/bytes/multi-byte.js`
240
309
 
241
- ```js
242
- import { createMultibyteDecoder } from '@exodus/bytes/multi-byte.js'
243
- ```
244
-
245
- Decode the legacy multi-byte encodings according to the [Encoding standard](https://encoding.spec.whatwg.org/)
310
+ Decode / encode the legacy multi-byte encodings according to the
311
+ [Encoding standard](https://encoding.spec.whatwg.org/)
246
312
  ([§10](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(simplified)-encodings),
247
313
  [§11](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(traditional)-encodings),
248
314
  [§12](https://encoding.spec.whatwg.org/#legacy-multi-byte-japanese-encodings),
249
315
  [§13](https://encoding.spec.whatwg.org/#legacy-multi-byte-korean-encodings)).
250
316
 
251
- Supports all legacy multi-byte encodings listed in the standard:
317
+ ```js
318
+ import { createMultibyteDecoder, createMultibyteEncoder } from '@exodus/bytes/multi-byte.js'
319
+ ```
320
+
321
+ Supports all legacy multi-byte encodings listed in the WHATWG Encoding standard:
252
322
  `gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`, `euc-kr`.
253
323
 
254
- ##### `createMultibyteDecoder(encoding, loose = false)`
324
+ #### `createMultibyteDecoder(encoding, loose = false)`
255
325
 
256
326
  Create a decoder for a supported legacy multi-byte `encoding`, given its lowercased name `encoding`.
257
327
 
258
328
  Returns a function `decode(arr, stream = false)` that decodes bytes to a string.
259
329
 
260
- That function will have state while `stream = true` is used.
330
+ The returned function will maintain internal state while `stream = true` is used, allowing it to
331
+ handle incomplete multi-byte sequences across multiple calls.
332
+ State is reset when `stream = false` or when the function is called without the `stream` parameter.
333
+
334
+ #### `createMultibyteEncoder(encoding, { mode = 'fatal' })`
335
+
336
+ Create an encoder for a supported legacy multi-byte `encoding`, given its lowercased name `encoding`.
337
+
338
+ Returns a function `encode(string)` that encodes a string to bytes.
339
+
340
+ In `'fatal'` mode (default), will throw on non well-formed strings or any codepoints which could
341
+ not be encoded in the target encoding.
261
342
 
262
343
  ### `@exodus/bytes/bigint.js`
263
344
 
345
+ Convert between BigInt and Uint8Array
346
+
264
347
  ```js
265
348
  import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
266
349
  ```
267
350
 
268
- ##### `fromBigInt(bigint, { length, format = 'uint8' })`
269
- ##### `toBigInt(arr)`
351
+ #### `fromBigInt(bigint, { length, format = 'uint8' })`
352
+
353
+ Convert a BigInt to a Uint8Array or Buffer
354
+
355
+ The output bytes are in big-endian format.
356
+
357
+ Throws if the BigInt is negative or cannot fit into the specified length.
358
+
359
+ #### `toBigInt(arr)`
360
+
361
+ Convert a Uint8Array or Buffer to a BigInt
362
+
363
+ The bytes are interpreted as a big-endian unsigned integer.
270
364
 
271
365
  ### `@exodus/bytes/hex.js`
272
366
 
273
- Implements Base16 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648) (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
367
+ Implements Base16 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
368
+ (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
274
369
 
275
370
  ```js
276
371
  import { fromHex, toHex } from '@exodus/bytes/hex.js'
277
372
  ```
278
373
 
279
- ##### `fromHex(string)`
280
- ##### `toHex(arr)`
374
+ #### `fromHex(string, format = 'uint8')`
375
+
376
+ Decode a hex string to bytes
377
+
378
+ Unlike `Buffer.from()`, throws on invalid input
379
+
380
+ #### `toHex(arr)`
381
+
382
+ Encode a `Uint8Array` to a lowercase hex string
281
383
 
282
384
  ### `@exodus/bytes/base64.js`
283
385
 
284
- Implements Base64 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648) (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
386
+ Implements base64 and base64url from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
387
+ (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
285
388
 
286
389
  ```js
287
390
  import { fromBase64, toBase64 } from '@exodus/bytes/base64.js'
@@ -289,29 +392,67 @@ import { fromBase64url, toBase64url } from '@exodus/bytes/base64.js'
289
392
  import { fromBase64any } from '@exodus/bytes/base64.js'
290
393
  ```
291
394
 
292
- ##### `fromBase64(str, { format = 'uint8', padding = 'both' })`
293
- ##### `fromBase64url(str, { format = 'uint8', padding = false })`
294
- ##### `fromBase64any(str, { format = 'uint8', padding = 'both' })`
295
- ##### `toBase64(arr, { padding = true })`
296
- ##### `toBase64url(arr, { padding = false })`
395
+ #### `fromBase64(string, { format = 'uint8', padding = 'both' })`
396
+
397
+ Decode a base64 string to bytes
398
+
399
+ Operates in strict mode for last chunk, does not allow whitespace
400
+
401
+ #### `fromBase64url(string, { format = 'uint8', padding = false })`
402
+
403
+ Decode a base64url string to bytes
404
+
405
+ Operates in strict mode for last chunk, does not allow whitespace
406
+
407
+ #### `fromBase64any(string, { format = 'uint8', padding = 'both' })`
408
+
409
+ Decode either base64 or base64url string to bytes
410
+
411
+ Automatically detects the variant based on characters present
412
+
413
+ #### `toBase64(arr, { padding = true })`
414
+
415
+ Encode a `Uint8Array` to a base64 string (RFC 4648)
416
+
417
+ #### `toBase64url(arr, { padding = false })`
418
+
419
+ Encode a `Uint8Array` to a base64url string (RFC 4648)
297
420
 
298
421
  ### `@exodus/bytes/base32.js`
299
422
 
300
- Implements Base32 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648) (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
423
+ Implements base32 and base32hex from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
424
+ (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
301
425
 
302
426
  ```js
303
427
  import { fromBase32, toBase32 } from '@exodus/bytes/base32.js'
304
428
  import { fromBase32hex, toBase32hex } from '@exodus/bytes/base32.js'
305
429
  ```
306
430
 
307
- ##### `fromBase32(str, { format = 'uint8', padding = 'both' })`
308
- ##### `fromBase32hex(str, { format = 'uint8', padding = 'both' })`
309
- ##### `toBase32(arr, { padding = false })`
310
- ##### `toBase32hex(arr, { padding = false })`
431
+ #### `fromBase32(string, { format = 'uint8', padding = 'both' })`
432
+
433
+ Decode a base32 string to bytes
434
+
435
+ Operates in strict mode for last chunk, does not allow whitespace
436
+
437
+ #### `fromBase32hex(string, { format = 'uint8', padding = 'both' })`
438
+
439
+ Decode a base32hex string to bytes
440
+
441
+ Operates in strict mode for last chunk, does not allow whitespace
442
+
443
+ #### `toBase32(arr, { padding = false })`
444
+
445
+ Encode a `Uint8Array` to a base32 string (RFC 4648)
446
+
447
+ #### `toBase32hex(arr, { padding = false })`
448
+
449
+ Encode a `Uint8Array` to a base32hex string (RFC 4648)
311
450
 
312
451
  ### `@exodus/bytes/bech32.js`
313
452
 
314
- Implements [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).
453
+ Implements bech32 and bech32m from
454
+ [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification)
455
+ and [BIP-0350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki#specification).
315
456
 
316
457
  ```js
317
458
  import { fromBech32, toBech32 } from '@exodus/bytes/bech32.js'
@@ -319,29 +460,67 @@ import { fromBech32m, toBech32m } from '@exodus/bytes/bech32.js'
319
460
  import { getPrefix } from '@exodus/bytes/bech32.js'
320
461
  ```
321
462
 
322
- ##### `getPrefix(str, limit = 90)`
463
+ #### `getPrefix(string, limit = 90)`
464
+
465
+ Extract the prefix from a bech32 or bech32m string without full validation
466
+
467
+ This is a quick check that skips most validation.
468
+
469
+ #### `fromBech32(string, limit = 90)`
470
+
471
+ Decode a bech32 string to bytes
472
+
473
+ #### `toBech32(prefix, bytes, limit = 90)`
323
474
 
324
- ##### `fromBech32(str, limit = 90)`
325
- ##### `toBech32(prefix, bytes, limit = 90)`
475
+ Encode bytes to a bech32 string
326
476
 
327
- ##### `fromBech32m(str, limit = 90)`
328
- ##### `toBech32m(prefix, bytes, limit = 90)`
477
+ #### `fromBech32m(string, limit = 90)`
478
+
479
+ Decode a bech32m string to bytes
480
+
481
+ #### `toBech32m(prefix, bytes, limit = 90)`
482
+
483
+ Encode bytes to a bech32m string
329
484
 
330
485
  ### `@exodus/bytes/base58.js`
331
486
 
487
+ Implements [base58](https://www.ietf.org/archive/id/draft-msporny-base58-03.txt) encoding.
488
+
489
+ Supports both standard base58 and XRP variant alphabets.
490
+
332
491
  ```js
333
492
  import { fromBase58, toBase58 } from '@exodus/bytes/base58.js'
334
493
  import { fromBase58xrp, toBase58xrp } from '@exodus/bytes/base58.js'
335
494
  ```
336
495
 
337
- ##### `fromBase58(str, format = 'uint8')`
338
- ##### `toBase58(arr)`
496
+ #### `fromBase58(string, format = 'uint8')`
497
+
498
+ Decode a base58 string to bytes
339
499
 
340
- ##### `fromBase58xrp(str, format = 'uint8')`
341
- ##### `toBase58xrp(arr)`
500
+ Uses the standard Bitcoin base58 alphabet
501
+
502
+ #### `toBase58(arr)`
503
+
504
+ Encode a `Uint8Array` to a base58 string
505
+
506
+ Uses the standard Bitcoin base58 alphabet
507
+
508
+ #### `fromBase58xrp(string, format = 'uint8')`
509
+
510
+ Decode a base58 string to bytes using XRP alphabet
511
+
512
+ Uses the XRP variant base58 alphabet
513
+
514
+ #### `toBase58xrp(arr)`
515
+
516
+ Encode a `Uint8Array` to a base58 string using XRP alphabet
517
+
518
+ Uses the XRP variant base58 alphabet
342
519
 
343
520
  ### `@exodus/bytes/base58check.js`
344
521
 
522
+ Implements [base58check](https://en.bitcoin.it/wiki/Base58Check_encoding) encoding.
523
+
345
524
  ```js
346
525
  import { fromBase58check, toBase58check } from '@exodus/bytes/base58check.js'
347
526
  import { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js'
@@ -350,14 +529,38 @@ import { makeBase58check } from '@exodus/bytes/base58check.js'
350
529
 
351
530
  On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
352
531
 
353
- ##### `async fromBase58check(str, format = 'uint8')`
354
- ##### `async toBase58check(arr)`
355
- ##### `fromBase58checkSync(str, format = 'uint8')`
356
- ##### `toBase58checkSync(arr)`
357
- ##### `makeBase58check(hashAlgo, hashAlgoSync)`
532
+ #### `async fromBase58check(string, format = 'uint8')`
533
+
534
+ Decode a base58check string to bytes asynchronously
535
+
536
+ Validates the checksum using double SHA-256
537
+
538
+ #### `async toBase58check(arr)`
539
+
540
+ Encode bytes to base58check string asynchronously
541
+
542
+ Uses double SHA-256 for checksum calculation
543
+
544
+ #### `fromBase58checkSync(string, format = 'uint8')`
545
+
546
+ Decode a base58check string to bytes synchronously
547
+
548
+ Validates the checksum using double SHA-256
549
+
550
+ #### `toBase58checkSync(arr)`
551
+
552
+ Encode bytes to base58check string synchronously
553
+
554
+ Uses double SHA-256 for checksum calculation
555
+
556
+ #### `makeBase58check(hashAlgo, hashAlgoSync)`
557
+
558
+ Create a base58check encoder/decoder with custom hash functions
358
559
 
359
560
  ### `@exodus/bytes/wif.js`
360
561
 
562
+ Wallet Import Format (WIF) encoding and decoding.
563
+
361
564
  ```js
362
565
  import { fromWifString, toWifString } from '@exodus/bytes/wif.js'
363
566
  import { fromWifStringSync, toWifStringSync } from '@exodus/bytes/wif.js'
@@ -365,40 +568,83 @@ import { fromWifStringSync, toWifStringSync } from '@exodus/bytes/wif.js'
365
568
 
366
569
  On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
367
570
 
368
- ##### `async fromWifString(string, version)`
369
- ##### `fromWifStringSync(string, version)`
370
- ##### `async toWifString({ version, privateKey, compressed })`
371
- ##### `toWifStringSync({ version, privateKey, compressed })`
571
+ #### `async fromWifString(string[, version])`
372
572
 
373
- ### `@exodus/bytes/encoding.js`
573
+ Decode a WIF string to WIF data
374
574
 
375
- ```js
376
- import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding.js'
377
- import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding.js' // Requires Streams
575
+ Returns a promise that resolves to an object with `{ version, privateKey, compressed }`.
378
576
 
379
- // Hooks for standards
380
- import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding.js'
577
+ The optional `version` parameter validates the version byte.
578
+
579
+ Throws if the WIF string is invalid or version doesn't match.
580
+
581
+ #### `fromWifStringSync(string[, version])`
582
+
583
+ Decode a WIF string to WIF data (synchronous)
584
+
585
+ Returns an object with `{ version, privateKey, compressed }`.
586
+
587
+ The optional `version` parameter validates the version byte.
588
+
589
+ Throws if the WIF string is invalid or version doesn't match.
590
+
591
+ #### `async toWifString({ version, privateKey, compressed })`
592
+
593
+ Encode WIF data to a WIF string
594
+
595
+ #### `toWifStringSync({ version, privateKey, compressed })`
596
+
597
+ Encode WIF data to a WIF string (synchronous)
598
+
599
+ ### `@exodus/bytes/array.js`
600
+
601
+ TypedArray utils and conversions.
602
+
603
+ ```js
604
+ import { typedView } from '@exodus/bytes/array.js'
381
605
  ```
382
606
 
607
+ #### `typedView(arr, format = 'uint8')`
608
+
609
+ Create a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)
610
+
611
+ Important: does not copy data, returns a view on the same underlying buffer
612
+
613
+ ### `@exodus/bytes/encoding.js`
614
+
383
615
  Implements the [Encoding standard](https://encoding.spec.whatwg.org/):
384
616
  [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder),
385
617
  [TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder),
386
618
  [TextDecoderStream](https://encoding.spec.whatwg.org/#interface-textdecoderstream),
387
619
  [TextEncoderStream](https://encoding.spec.whatwg.org/#interface-textencoderstream),
388
- some [hooks](https://encoding.spec.whatwg.org/#specification-hooks) (see below).
620
+ some [hooks](https://encoding.spec.whatwg.org/#specification-hooks).
621
+
622
+ ```js
623
+ import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding.js'
624
+ import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding.js' // Requires Streams
625
+
626
+ // Hooks for standards
627
+ import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding.js'
628
+ ```
389
629
 
390
630
  #### `new TextDecoder(label = 'utf-8', { fatal = false, ignoreBOM = false })`
391
631
 
392
632
  [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) implementation/polyfill.
393
633
 
634
+ Decode bytes to strings according to [WHATWG Encoding](https://encoding.spec.whatwg.org) specification.
635
+
394
636
  #### `new TextEncoder()`
395
637
 
396
638
  [TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder) implementation/polyfill.
397
639
 
640
+ Encode strings to UTF-8 bytes according to [WHATWG Encoding](https://encoding.spec.whatwg.org) specification.
641
+
398
642
  #### `new TextDecoderStream(label = 'utf-8', { fatal = false, ignoreBOM = false })`
399
643
 
400
644
  [TextDecoderStream](https://encoding.spec.whatwg.org/#interface-textdecoderstream) implementation/polyfill.
401
645
 
646
+ A [Streams](https://streams.spec.whatwg.org/) wrapper for `TextDecoder`.
647
+
402
648
  Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or
403
649
  [polyfilled](https://npmjs.com/package/web-streams-polyfill).
404
650
 
@@ -406,6 +652,8 @@ Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by t
406
652
 
407
653
  [TextEncoderStream](https://encoding.spec.whatwg.org/#interface-textencoderstream) implementation/polyfill.
408
654
 
655
+ A [Streams](https://streams.spec.whatwg.org/) wrapper for `TextEncoder`.
656
+
409
657
  Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or
410
658
  [polyfilled](https://npmjs.com/package/web-streams-polyfill).
411
659
 
@@ -413,7 +661,7 @@ Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by t
413
661
 
414
662
  Implements [get an encoding from a string `label`](https://encoding.spec.whatwg.org/#concept-encoding-get).
415
663
 
416
- Converts an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
664
+ Convert an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
417
665
  as a case-sensitive string.
418
666
 
419
667
  If an encoding with that label does not exist, returns `null`.
@@ -422,7 +670,7 @@ All encoding names are also valid labels for corresponding encodings.
422
670
 
423
671
  #### `normalizeEncoding(label)`
424
672
 
425
- Converts an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
673
+ Convert an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
426
674
  as an ASCII-lowercased string.
427
675
 
428
676
  If an encoding with that label does not exist, returns `null`.
@@ -445,10 +693,10 @@ All encoding names are also valid labels for corresponding encodings.
445
693
  Implements [BOM sniff](https://encoding.spec.whatwg.org/#bom-sniff) legacy hook.
446
694
 
447
695
  Given a `TypedArray` or an `ArrayBuffer` instance `input`, returns either of:
448
- * `'utf-8'`, if `input` starts with UTF-8 byte order mark.
449
- * `'utf-16le'`, if `input` starts with UTF-16LE byte order mark.
450
- * `'utf-16be'`, if `input` starts with UTF-16BE byte order mark.
451
- * `null` otherwise.
696
+ - `'utf-8'`, if `input` starts with UTF-8 byte order mark.
697
+ - `'utf-16le'`, if `input` starts with UTF-16LE byte order mark.
698
+ - `'utf-16be'`, if `input` starts with UTF-16BE byte order mark.
699
+ - `null` otherwise.
452
700
 
453
701
  #### `legacyHookDecode(input, fallbackEncoding = 'utf-8')`
454
702
 
@@ -461,10 +709,10 @@ decodes the `input` using that encoding, skipping BOM if it was present.
461
709
 
462
710
  Notes:
463
711
 
464
- * BOM-sniffed encoding takes precedence over `fallbackEncoding` option per spec.
465
- Use with care.
466
- * Always operates in non-fatal [mode](https://encoding.spec.whatwg.org/#textdecoder-error-mode),
467
- aka replacement. It can convert different byte sequences to equal strings.
712
+ - BOM-sniffed encoding takes precedence over `fallbackEncoding` option per spec.
713
+ Use with care.
714
+ - Always operates in non-fatal [mode](https://encoding.spec.whatwg.org/#textdecoder-error-mode),
715
+ aka replacement. It can convert different byte sequences to equal strings.
468
716
 
469
717
  This method is similar to the following code, except that it doesn't support encoding labels and
470
718
  only expects lowercased encoding name:
@@ -475,6 +723,10 @@ new TextDecoder(getBOMEncoding(input) ?? fallbackEncoding).decode(input)
475
723
 
476
724
  ### `@exodus/bytes/encoding-lite.js`
477
725
 
726
+ The exact same exports as `@exodus/bytes/encoding.js` are also exported as
727
+ `@exodus/bytes/encoding-lite.js`, with the difference that the lite version does not load
728
+ multi-byte `TextDecoder` encodings by default to reduce bundle size 10x.
729
+
478
730
  ```js
479
731
  import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-lite.js'
480
732
  import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-lite.js' // Requires Streams
@@ -483,10 +735,6 @@ import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-lit
483
735
  import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-lite.js'
484
736
  ```
485
737
 
486
- The exact same exports as `@exodus/bytes/encoding.js` are also exported as
487
- `@exodus/bytes/encoding-lite.js`, with the difference that the lite version does not load
488
- multi-byte `TextDecoder` encodings by default to reduce bundle size 10x.
489
-
490
738
  The only affected encodings are: `gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`
491
739
  and their [labels](https://encoding.spec.whatwg.org/#names-and-labels) when used with `TextDecoder`.
492
740
 
@@ -534,6 +782,9 @@ true
534
782
 
535
783
  ### `@exodus/bytes/encoding-browser.js`
536
784
 
785
+ Same as `@exodus/bytes/encoding.js`, but in browsers instead of polyfilling just uses whatever the
786
+ browser provides, drastically reducing the bundle size (to less than 2 KiB gzipped).
787
+
537
788
  ```js
538
789
  import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-browser.js'
539
790
  import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-browser.js' // Requires Streams
@@ -542,9 +793,6 @@ import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-bro
542
793
  import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-browser.js'
543
794
  ```
544
795
 
545
- Same as `@exodus/bytes/encoding.js`, but in browsers instead of polyfilling just uses whatever the
546
- browser provides, drastically reducing the bundle size (to less than 2 KiB gzipped).
547
-
548
796
  Under non-browser engines (Node.js, React Native, etc.) a full polyfill is used as those platforms
549
797
  do not provide sufficiently complete / non-buggy `TextDecoder` APIs.
550
798
 
@@ -553,6 +801,10 @@ do not provide sufficiently complete / non-buggy `TextDecoder` APIs.
553
801
  > but they are fixing them and the expected update window is short.\
554
802
  > If you want to circumvent browser bugs, use full `@exodus/bytes/encoding.js` import.
555
803
 
804
+ ## Changelog
805
+
806
+ See [GitHub Releases](https://github.com/ExodusOSS/bytes/releases) tab
807
+
556
808
  ## License
557
809
 
558
810
  [MIT](./LICENSE)