@exodus/bytes 1.2.0 → 1.4.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 +141 -33
- package/base58check.js +4 -54
- package/base58check.node.js +14 -0
- package/encoding-lite.js +1 -0
- package/encoding.js +1 -0
- package/fallback/_utils.js +1 -1
- package/fallback/base58check.js +53 -0
- package/fallback/encoding.js +23 -2
- package/fallback/multi-byte.js +51 -69
- package/package.json +7 -2
- package/utf16.js +3 -2
- package/utf16.node.js +8 -7
- package/wif.js +1 -1
package/README.md
CHANGED
|
@@ -78,6 +78,13 @@ See [the list of encodings](https://encoding.spec.whatwg.org/#names-and-labels).
|
|
|
78
78
|
|
|
79
79
|
### `@exodus/bytes/utf8.js`
|
|
80
80
|
|
|
81
|
+
```js
|
|
82
|
+
import { utf8fromString, utf8toString } from '@exodus/bytes/utf8.js'
|
|
83
|
+
|
|
84
|
+
// loose
|
|
85
|
+
import { utf8fromStringLoose, utf8toStringLoose } from '@exodus/bytes/utf8.js'
|
|
86
|
+
```
|
|
87
|
+
|
|
81
88
|
##### `utf8fromString(str, format = 'uint8')`
|
|
82
89
|
##### `utf8fromStringLoose(str, format = 'uint8')`
|
|
83
90
|
##### `utf8toString(arr)`
|
|
@@ -85,6 +92,13 @@ See [the list of encodings](https://encoding.spec.whatwg.org/#names-and-labels).
|
|
|
85
92
|
|
|
86
93
|
### `@exodus/bytes/utf16.js`
|
|
87
94
|
|
|
95
|
+
```js
|
|
96
|
+
import { utf16fromString, utf16toString } from '@exodus/bytes/utf16.js'
|
|
97
|
+
|
|
98
|
+
// loose
|
|
99
|
+
import { utf16fromStringLoose, utf16toStringLoose } from '@exodus/bytes/utf16.js'
|
|
100
|
+
```
|
|
101
|
+
|
|
88
102
|
##### `utf16fromString(str, format = 'uint16')`
|
|
89
103
|
##### `utf16fromStringLoose(str, format = 'uint16')`
|
|
90
104
|
##### `utf16toString(arr, 'uint16')`
|
|
@@ -92,21 +106,26 @@ See [the list of encodings](https://encoding.spec.whatwg.org/#names-and-labels).
|
|
|
92
106
|
|
|
93
107
|
### `@exodus/bytes/single-byte.js`
|
|
94
108
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
Returns a function `decode(arr)` that decodes bytes to a string.
|
|
109
|
+
```js
|
|
110
|
+
import { createSinglebyteDecoder } from '@exodus/bytes/single-byte.js'
|
|
111
|
+
import { windows1252toString } from '@exodus/bytes/single-byte.js'
|
|
112
|
+
```
|
|
100
113
|
|
|
101
|
-
|
|
114
|
+
Decode the legacy single-byte encodings according to the [Encoding standard](https://encoding.spec.whatwg.org/)
|
|
115
|
+
([§9](https://encoding.spec.whatwg.org/#legacy-single-byte-encodings) and
|
|
116
|
+
[§14.5](https://encoding.spec.whatwg.org/#x-user-defined)).
|
|
102
117
|
|
|
103
|
-
|
|
118
|
+
Supports all single-byte encodings listed in the standard:
|
|
119
|
+
`ibm866`, `iso-8859-2`, `iso-8859-3`, `iso-8859-4`, `iso-8859-5`, `iso-8859-6`, `iso-8859-7`, `iso-8859-8`,
|
|
120
|
+
`iso-8859-8-i`, `iso-8859-10`, `iso-8859-13`, `iso-8859-14`, `iso-8859-15`, `iso-8859-16`, `koi8-r`, `koi8-u`,
|
|
121
|
+
`macintosh`, `windows-874`, `windows-1250`, `windows-1251`, `windows-1252`, `windows-1253`, `windows-1254`,
|
|
122
|
+
`windows-1255`, `windows-1256`, `windows-1257`, `windows-1258`, `x-mac-cyrillic` and `x-user-defined`.
|
|
104
123
|
|
|
105
|
-
|
|
124
|
+
##### `createSinglebyteDecoder(encoding, loose = false)`
|
|
106
125
|
|
|
107
|
-
|
|
126
|
+
Create a decoder for a supported one-byte `encoding`, given it's lowercased name `encoding`.
|
|
108
127
|
|
|
109
|
-
|
|
128
|
+
Returns a function `decode(arr)` that decodes bytes to a string.
|
|
110
129
|
|
|
111
130
|
##### `windows1252toString(arr)`
|
|
112
131
|
|
|
@@ -116,59 +135,132 @@ Also supports `ascii` and `latin-1` as those are strict subsets of `windows-1252
|
|
|
116
135
|
|
|
117
136
|
There is no loose variant for this encoding, all bytes can be decoded.
|
|
118
137
|
|
|
119
|
-
Same as
|
|
138
|
+
Same as:
|
|
139
|
+
```js
|
|
140
|
+
const windows1252toString = createSinglebyteDecoder('windows-1252')
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### `@exodus/bytes/multi-byte.js`
|
|
144
|
+
|
|
145
|
+
```js
|
|
146
|
+
import { createMultibyteDecoder } from '@exodus/bytes/multi-byte.js'
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Decode the legacy multi-byte encodings according to the [Encoding standard](https://encoding.spec.whatwg.org/)
|
|
150
|
+
([§10](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(simplified)-encodings),
|
|
151
|
+
[§11](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(traditional)-encodings),
|
|
152
|
+
[§12](https://encoding.spec.whatwg.org/#legacy-multi-byte-japanese-encodings),
|
|
153
|
+
[§13](https://encoding.spec.whatwg.org/#legacy-multi-byte-korean-encodings)).
|
|
154
|
+
|
|
155
|
+
Supports all legacy multi-byte encodings listed in the standard:
|
|
156
|
+
`gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`, `euc-kr`.
|
|
157
|
+
|
|
158
|
+
##### `createMultibyteDecoder(encoding, loose = false)`
|
|
159
|
+
|
|
160
|
+
Create a decoder for a supported legacy multi-byte `encoding`, given it's lowercased name `encoding`.
|
|
161
|
+
|
|
162
|
+
Returns a function `decode(arr, stream = false)` that decodes bytes to a string.
|
|
163
|
+
|
|
164
|
+
That function will have state while `stream = true` is used.
|
|
120
165
|
|
|
121
166
|
### `@exodus/bytes/bigint.js`
|
|
122
167
|
|
|
168
|
+
```js
|
|
169
|
+
import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
|
|
170
|
+
```
|
|
171
|
+
|
|
123
172
|
##### `fromBigInt(bigint, { length, format = 'uint8' })`
|
|
124
173
|
##### `toBigInt(arr)`
|
|
125
174
|
|
|
126
175
|
### `@exodus/bytes/hex.js`
|
|
127
176
|
|
|
128
|
-
|
|
177
|
+
```js
|
|
178
|
+
import { fromHex, toHex } from '@exodus/bytes/hex.js'
|
|
179
|
+
```
|
|
180
|
+
|
|
129
181
|
##### `fromHex(string)`
|
|
182
|
+
##### `toHex(arr)`
|
|
130
183
|
|
|
131
184
|
### `@exodus/bytes/base64.js`
|
|
132
185
|
|
|
133
|
-
|
|
134
|
-
|
|
186
|
+
```js
|
|
187
|
+
import { fromBase64, toBase64 } from '@exodus/bytes/base64.js'
|
|
188
|
+
import { fromBase64url, toBase64url } from '@exodus/bytes/base64.js'
|
|
189
|
+
import { fromBase64any } from '@exodus/bytes/base64.js'
|
|
190
|
+
```
|
|
191
|
+
|
|
135
192
|
##### `fromBase64(str, { format = 'uint8', padding = 'both' })`
|
|
136
193
|
##### `fromBase64url(str, { format = 'uint8', padding = false })`
|
|
137
194
|
##### `fromBase64any(str, { format = 'uint8', padding = 'both' })`
|
|
195
|
+
##### `toBase64(arr, { padding = true })`
|
|
196
|
+
##### `toBase64url(arr, { padding = false })`
|
|
138
197
|
|
|
139
198
|
### `@exodus/bytes/base32.js`
|
|
140
199
|
|
|
141
|
-
|
|
142
|
-
|
|
200
|
+
```js
|
|
201
|
+
import { fromBase32, toBase32 } from '@exodus/bytes/base32.js'
|
|
202
|
+
import { fromBase32hex, toBase32hex } from '@exodus/bytes/base32.js'
|
|
203
|
+
```
|
|
204
|
+
|
|
143
205
|
##### `fromBase32(str, { format = 'uint8', padding = 'both' })`
|
|
144
206
|
##### `fromBase32hex(str, { format = 'uint8', padding = 'both' })`
|
|
207
|
+
##### `toBase32(arr, { padding = false })`
|
|
208
|
+
##### `toBase32hex(arr, { padding = false })`
|
|
145
209
|
|
|
146
210
|
### `@exodus/bytes/bech32.js`
|
|
147
211
|
|
|
212
|
+
```js
|
|
213
|
+
import { fromBech32, toBech32 } from '@exodus/bytes/bech32.js'
|
|
214
|
+
import { fromBech32m, toBech32m } from '@exodus/bytes/base32.js'
|
|
215
|
+
import { getPrefix } from '@exodus/bytes/base32.js'
|
|
216
|
+
```
|
|
217
|
+
|
|
148
218
|
##### `getPrefix(str, limit = 90)`
|
|
149
|
-
|
|
219
|
+
|
|
150
220
|
##### `fromBech32(str, limit = 90)`
|
|
151
|
-
##### `
|
|
221
|
+
##### `toBech32(prefix, bytes, limit = 90)`
|
|
222
|
+
|
|
152
223
|
##### `fromBech32m(str, limit = 90)`
|
|
224
|
+
##### `toBech32m(prefix, bytes, limit = 90)`
|
|
153
225
|
|
|
154
226
|
### `@exodus/bytes/base58.js`
|
|
155
227
|
|
|
156
|
-
|
|
228
|
+
```js
|
|
229
|
+
import { fromBase58, toBase58 } from '@exodus/bytes/base58.js'
|
|
230
|
+
import { fromBase58xrp, toBase58xrp } from '@exodus/bytes/base58.js'
|
|
231
|
+
```
|
|
232
|
+
|
|
157
233
|
##### `fromBase58(str, format = 'uint8')`
|
|
234
|
+
##### `toBase58(arr)`
|
|
158
235
|
|
|
159
|
-
##### `toBase58xrp(arr)`
|
|
160
236
|
##### `fromBase58xrp(str, format = 'uint8')`
|
|
237
|
+
##### `toBase58xrp(arr)`
|
|
161
238
|
|
|
162
239
|
### `@exodus/bytes/base58check.js`
|
|
163
240
|
|
|
164
|
-
|
|
165
|
-
|
|
241
|
+
```js
|
|
242
|
+
import { fromBase58check, toBase58check } from '@exodus/bytes/base58check.js'
|
|
243
|
+
import { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js'
|
|
244
|
+
import { makeBase58check } from '@exodus/bytes/base58check.js'
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
On non-Node.js, requires peer dependency [@exodus/crypto](https://www.npmjs.com/package/@exodus/crypto) to be installed.
|
|
248
|
+
|
|
166
249
|
##### `async fromBase58check(str, format = 'uint8')`
|
|
250
|
+
##### `async toBase58check(arr)`
|
|
167
251
|
##### `fromBase58checkSync(str, format = 'uint8')`
|
|
252
|
+
##### `toBase58checkSync(arr)`
|
|
168
253
|
##### `makeBase58check(hashAlgo, hashAlgoSync)`
|
|
169
254
|
|
|
170
255
|
### `@exodus/bytes/wif.js`
|
|
171
256
|
|
|
257
|
+
```js
|
|
258
|
+
import { fromWifString, toWifString } from '@exodus/bytes/wif.js'
|
|
259
|
+
import { fromWifStringSync, toWifStringSync } from '@exodus/bytes/wif.js'
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
On non-Node.js, requires peer dependency [@exodus/crypto](https://www.npmjs.com/package/@exodus/crypto) to be installed.
|
|
263
|
+
|
|
172
264
|
##### `async fromWifString(string, version)`
|
|
173
265
|
##### `fromWifStringSync(string, version)`
|
|
174
266
|
##### `async toWifString({ version, privateKey, compressed })`
|
|
@@ -176,18 +268,18 @@ Same as `windows1252toString = createSinglebyteDecoder('windows-1252')`.
|
|
|
176
268
|
|
|
177
269
|
### `@exodus/bytes/encoding.js`
|
|
178
270
|
|
|
179
|
-
Implements the [Encoding standard](https://encoding.spec.whatwg.org/):
|
|
180
|
-
[TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder),
|
|
181
|
-
[TextEncoder](https://encoding.spec.whatwg.org/#interface-textdecoder),
|
|
182
|
-
some [hooks](https://encoding.spec.whatwg.org/#specification-hooks) (see below).
|
|
183
|
-
|
|
184
271
|
```js
|
|
185
|
-
import { TextDecoder,
|
|
272
|
+
import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding.js'
|
|
186
273
|
|
|
187
274
|
// Hooks for standards
|
|
188
|
-
import { getBOMEncoding, legacyHookDecode, normalizeEncoding } from '@exodus/bytes/encoding.js'
|
|
275
|
+
import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding.js'
|
|
189
276
|
```
|
|
190
277
|
|
|
278
|
+
Implements the [Encoding standard](https://encoding.spec.whatwg.org/):
|
|
279
|
+
[TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder),
|
|
280
|
+
[TextEncoder](https://encoding.spec.whatwg.org/#interface-textdecoder),
|
|
281
|
+
some [hooks](https://encoding.spec.whatwg.org/#specification-hooks) (see below).
|
|
282
|
+
|
|
191
283
|
#### `new TextDecoder(label = 'utf-8', { fatal = false, ignoreBOM = false })`
|
|
192
284
|
|
|
193
285
|
[TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) implementation/polyfill.
|
|
@@ -196,10 +288,19 @@ import { getBOMEncoding, legacyHookDecode, normalizeEncoding } from '@exodus/byt
|
|
|
196
288
|
|
|
197
289
|
[TextEncoder](https://encoding.spec.whatwg.org/#interface-textdecoder) implementation/polyfill.
|
|
198
290
|
|
|
199
|
-
#### `
|
|
291
|
+
#### `labelToName(label)`
|
|
200
292
|
|
|
201
293
|
Implements [get an encoding from a string `label`](https://encoding.spec.whatwg.org/#concept-encoding-get).
|
|
202
294
|
|
|
295
|
+
Converts an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
|
|
296
|
+
as a case-sensitive string.
|
|
297
|
+
|
|
298
|
+
If an encoding with that label does not exist, returns `null`.
|
|
299
|
+
|
|
300
|
+
All encoding names are also valid labels for corresponding encodings.
|
|
301
|
+
|
|
302
|
+
#### `normalizeEncoding(label)`
|
|
303
|
+
|
|
203
304
|
Converts an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
|
|
204
305
|
as an ASCII-lowercased string.
|
|
205
306
|
|
|
@@ -211,6 +312,11 @@ except that it:
|
|
|
211
312
|
[labels](https://encoding.spec.whatwg.org/#ref-for-replacement%E2%91%A1)
|
|
212
313
|
2. Does not throw for invalid labels and instead returns `null`
|
|
213
314
|
|
|
315
|
+
It is identical to:
|
|
316
|
+
```js
|
|
317
|
+
labelToName(label)?.toLowerCase() ?? null
|
|
318
|
+
```
|
|
319
|
+
|
|
214
320
|
All encoding names are also valid labels for corresponding encodings.
|
|
215
321
|
|
|
216
322
|
#### `getBOMEncoding(input)`
|
|
@@ -249,10 +355,10 @@ new TextDecoder(getBOMEncoding(input) ?? fallbackEncoding).decode(input)
|
|
|
249
355
|
### `@exodus/bytes/encoding-lite.js`
|
|
250
356
|
|
|
251
357
|
```js
|
|
252
|
-
import { TextDecoder,
|
|
358
|
+
import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-lite.js'
|
|
253
359
|
|
|
254
360
|
// Hooks for standards
|
|
255
|
-
import { getBOMEncoding, legacyHookDecode, normalizeEncoding } from '@exodus/bytes/encoding-lite.js'
|
|
361
|
+
import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-lite.js'
|
|
256
362
|
```
|
|
257
363
|
|
|
258
364
|
The exact same exports as `@exodus/bytes/encoding.js` are also exported as
|
|
@@ -264,7 +370,7 @@ and their [labels](https://encoding.spec.whatwg.org/#names-and-labels) when used
|
|
|
264
370
|
|
|
265
371
|
Legacy single-byte encodingds are loaded by default in both cases.
|
|
266
372
|
|
|
267
|
-
`TextEncoder` and hooks for standards (including `normalizeEncoding`) do not have any behavior
|
|
373
|
+
`TextEncoder` and hooks for standards (including `labelToName` / `normalizeEncoding`) do not have any behavior
|
|
268
374
|
differences in the lite version and support full range if inputs.
|
|
269
375
|
|
|
270
376
|
To avoid inconsistencies, the exported classes and methods are exactly the same objects.
|
|
@@ -275,6 +381,7 @@ To avoid inconsistencies, the exported classes and methods are exactly the same
|
|
|
275
381
|
TextDecoder: [class TextDecoder],
|
|
276
382
|
TextEncoder: [class TextEncoder],
|
|
277
383
|
getBOMEncoding: [Function: getBOMEncoding],
|
|
384
|
+
labelToName: [Function: labelToName],
|
|
278
385
|
legacyHookDecode: [Function: legacyHookDecode],
|
|
279
386
|
normalizeEncoding: [Function: normalizeEncoding]
|
|
280
387
|
}
|
|
@@ -287,6 +394,7 @@ Error: Legacy multi-byte encodings are disabled in /encoding-lite.js, use /encod
|
|
|
287
394
|
TextDecoder: [class TextDecoder],
|
|
288
395
|
TextEncoder: [class TextEncoder],
|
|
289
396
|
getBOMEncoding: [Function: getBOMEncoding],
|
|
397
|
+
labelToName: [Function: labelToName],
|
|
290
398
|
legacyHookDecode: [Function: legacyHookDecode],
|
|
291
399
|
normalizeEncoding: [Function: normalizeEncoding]
|
|
292
400
|
}
|
package/base58check.js
CHANGED
|
@@ -1,63 +1,12 @@
|
|
|
1
|
-
import { typedView } from './array.js'
|
|
2
|
-
import { assertUint8 } from './assert.js'
|
|
3
|
-
import { toBase58, fromBase58 } from './base58.js'
|
|
4
1
|
import { hashSync } from '@exodus/crypto/hash' // eslint-disable-line @exodus/import/no-deprecated
|
|
2
|
+
import { makeBase58check } from './fallback/base58check.js'
|
|
5
3
|
|
|
6
4
|
// Note: while API is async, we use hashSync for now until we improve webcrypto perf for hash256
|
|
7
5
|
// Inputs to base58 are typically very small, and that makes a difference
|
|
8
6
|
|
|
9
|
-
const E_CHECKSUM = 'Invalid checksum'
|
|
10
|
-
|
|
11
|
-
// checksum length is 4, i.e. only the first 4 bytes of the hash are used
|
|
12
|
-
|
|
13
|
-
function encodeWithChecksum(arr, checksum) {
|
|
14
|
-
// arr type in already validated in input
|
|
15
|
-
const res = new Uint8Array(arr.length + 4)
|
|
16
|
-
res.set(arr, 0)
|
|
17
|
-
res.set(checksum.subarray(0, 4), arr.length)
|
|
18
|
-
return toBase58(res)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function decodeWithChecksum(str) {
|
|
22
|
-
const arr = fromBase58(str) // checks input
|
|
23
|
-
const payloadSize = arr.length - 4
|
|
24
|
-
if (payloadSize < 0) throw new Error(E_CHECKSUM)
|
|
25
|
-
return [arr.subarray(0, payloadSize), arr.subarray(payloadSize)]
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function assertChecksum(c, r) {
|
|
29
|
-
if ((c[0] ^ r[0]) | (c[1] ^ r[1]) | (c[2] ^ r[2]) | (c[3] ^ r[3])) throw new Error(E_CHECKSUM)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const makeBase58check = (hashAlgo, hashAlgoSync) => {
|
|
33
|
-
const apis = {
|
|
34
|
-
async encode(arr) {
|
|
35
|
-
assertUint8(arr)
|
|
36
|
-
return encodeWithChecksum(arr, await hashAlgo(arr))
|
|
37
|
-
},
|
|
38
|
-
async decode(str, format = 'uint8') {
|
|
39
|
-
const [payload, checksum] = decodeWithChecksum(str)
|
|
40
|
-
assertChecksum(checksum, await hashAlgo(payload))
|
|
41
|
-
return typedView(payload, format)
|
|
42
|
-
},
|
|
43
|
-
}
|
|
44
|
-
if (!hashAlgoSync) return apis
|
|
45
|
-
return {
|
|
46
|
-
...apis,
|
|
47
|
-
encodeSync(arr) {
|
|
48
|
-
assertUint8(arr)
|
|
49
|
-
return encodeWithChecksum(arr, hashAlgoSync(arr))
|
|
50
|
-
},
|
|
51
|
-
decodeSync(str, format = 'uint8') {
|
|
52
|
-
const [payload, checksum] = decodeWithChecksum(str)
|
|
53
|
-
assertChecksum(checksum, hashAlgoSync(payload))
|
|
54
|
-
return typedView(payload, format)
|
|
55
|
-
},
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
7
|
// eslint-disable-next-line @exodus/import/no-deprecated
|
|
60
|
-
const
|
|
8
|
+
const sha256 = (x) => hashSync('sha256', x, 'uint8')
|
|
9
|
+
const hash256sync = (x) => sha256(sha256(x))
|
|
61
10
|
const hash256 = hash256sync // See note at the top
|
|
62
11
|
const {
|
|
63
12
|
encode: toBase58check,
|
|
@@ -66,4 +15,5 @@ const {
|
|
|
66
15
|
decodeSync: fromBase58checkSync,
|
|
67
16
|
} = makeBase58check(hash256, hash256sync)
|
|
68
17
|
|
|
18
|
+
export { makeBase58check } from './fallback/base58check.js'
|
|
69
19
|
export { toBase58check, fromBase58check, toBase58checkSync, fromBase58checkSync }
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { hash } from 'node:crypto'
|
|
2
|
+
import { makeBase58check } from './fallback/base58check.js'
|
|
3
|
+
|
|
4
|
+
const sha256 = (x) => hash('sha256', x, 'buffer')
|
|
5
|
+
const hash256 = (x) => sha256(sha256(x))
|
|
6
|
+
const {
|
|
7
|
+
encode: toBase58check,
|
|
8
|
+
decode: fromBase58check,
|
|
9
|
+
encodeSync: toBase58checkSync,
|
|
10
|
+
decodeSync: fromBase58checkSync,
|
|
11
|
+
} = makeBase58check(hash256, hash256)
|
|
12
|
+
|
|
13
|
+
export { makeBase58check } from './fallback/base58check.js'
|
|
14
|
+
export { toBase58check, fromBase58check, toBase58checkSync, fromBase58checkSync }
|
package/encoding-lite.js
CHANGED
package/encoding.js
CHANGED
package/fallback/_utils.js
CHANGED
|
@@ -106,7 +106,7 @@ function decodePartTemplates(a, start, end, m) {
|
|
|
106
106
|
|
|
107
107
|
const decodePart = isHermes ? decodePartTemplates : decodePartAddition
|
|
108
108
|
export function decode2string(arr, start, end, m) {
|
|
109
|
-
if (
|
|
109
|
+
if (end - start > 30_000) {
|
|
110
110
|
// Limit concatenation to avoid excessive GC
|
|
111
111
|
// Thresholds checked on Hermes for toHex
|
|
112
112
|
const concat = []
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { typedView } from '@exodus/bytes/array.js'
|
|
2
|
+
import { toBase58, fromBase58 } from '@exodus/bytes/base58.js'
|
|
3
|
+
import { assertUint8 } from '../assert.js'
|
|
4
|
+
|
|
5
|
+
const E_CHECKSUM = 'Invalid checksum'
|
|
6
|
+
|
|
7
|
+
// checksum length is 4, i.e. only the first 4 bytes of the hash are used
|
|
8
|
+
|
|
9
|
+
function encodeWithChecksum(arr, checksum) {
|
|
10
|
+
// arr type in already validated in input
|
|
11
|
+
const res = new Uint8Array(arr.length + 4)
|
|
12
|
+
res.set(arr, 0)
|
|
13
|
+
res.set(checksum.subarray(0, 4), arr.length)
|
|
14
|
+
return toBase58(res)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function decodeWithChecksum(str) {
|
|
18
|
+
const arr = fromBase58(str) // checks input
|
|
19
|
+
const payloadSize = arr.length - 4
|
|
20
|
+
if (payloadSize < 0) throw new Error(E_CHECKSUM)
|
|
21
|
+
return [arr.subarray(0, payloadSize), arr.subarray(payloadSize)]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function assertChecksum(c, r) {
|
|
25
|
+
if ((c[0] ^ r[0]) | (c[1] ^ r[1]) | (c[2] ^ r[2]) | (c[3] ^ r[3])) throw new Error(E_CHECKSUM)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const makeBase58check = (hashAlgo, hashAlgoSync) => {
|
|
29
|
+
const apis = {
|
|
30
|
+
async encode(arr) {
|
|
31
|
+
assertUint8(arr)
|
|
32
|
+
return encodeWithChecksum(arr, await hashAlgo(arr))
|
|
33
|
+
},
|
|
34
|
+
async decode(str, format = 'uint8') {
|
|
35
|
+
const [payload, checksum] = decodeWithChecksum(str)
|
|
36
|
+
assertChecksum(checksum, await hashAlgo(payload))
|
|
37
|
+
return typedView(payload, format)
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
if (!hashAlgoSync) return apis
|
|
41
|
+
return {
|
|
42
|
+
...apis,
|
|
43
|
+
encodeSync(arr) {
|
|
44
|
+
assertUint8(arr)
|
|
45
|
+
return encodeWithChecksum(arr, hashAlgoSync(arr))
|
|
46
|
+
},
|
|
47
|
+
decodeSync(str, format = 'uint8') {
|
|
48
|
+
const [payload, checksum] = decodeWithChecksum(str)
|
|
49
|
+
assertChecksum(checksum, hashAlgoSync(payload))
|
|
50
|
+
return typedView(payload, format)
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
}
|
package/fallback/encoding.js
CHANGED
|
@@ -47,11 +47,18 @@ export function normalizeEncoding(label) {
|
|
|
47
47
|
|
|
48
48
|
const define = (obj, key, value) => Object.defineProperty(obj, key, { value, writable: false })
|
|
49
49
|
|
|
50
|
+
function isAnyArrayBuffer(x) {
|
|
51
|
+
if (x instanceof ArrayBuffer) return true
|
|
52
|
+
if (globalThis.SharedArrayBuffer && x instanceof SharedArrayBuffer) return true
|
|
53
|
+
if (!x || typeof x.byteLength !== 'number') return false
|
|
54
|
+
const s = Object.prototype.toString.call(x)
|
|
55
|
+
return s === '[object ArrayBuffer]' || s === '[object SharedArrayBuffer]'
|
|
56
|
+
}
|
|
57
|
+
|
|
50
58
|
const fromSource = (x) => {
|
|
51
59
|
if (x instanceof Uint8Array) return x
|
|
52
|
-
if (x instanceof ArrayBuffer) return new Uint8Array(x)
|
|
53
60
|
if (ArrayBuffer.isView(x)) return new Uint8Array(x.buffer, x.byteOffset, x.byteLength)
|
|
54
|
-
if (
|
|
61
|
+
if (isAnyArrayBuffer(x)) return new Uint8Array(x)
|
|
55
62
|
throw new TypeError('Argument must be a SharedArrayBuffer, ArrayBuffer or ArrayBufferView')
|
|
56
63
|
}
|
|
57
64
|
|
|
@@ -288,3 +295,17 @@ export function legacyHookDecode(input, fallbackEncoding = 'utf-8') {
|
|
|
288
295
|
|
|
289
296
|
return createSinglebyteDecoder(enc, true)(u8)
|
|
290
297
|
}
|
|
298
|
+
|
|
299
|
+
const uppercasePrefixes = new Set(['utf', 'iso', 'koi', 'euc', 'ibm', 'gbk'])
|
|
300
|
+
|
|
301
|
+
// Unlike normalizeEncoding, case-sensitive
|
|
302
|
+
// https://encoding.spec.whatwg.org/#names-and-labels
|
|
303
|
+
export function labelToName(label) {
|
|
304
|
+
const enc = normalizeEncoding(label)
|
|
305
|
+
if (enc === 'utf-8') return 'UTF-8' // fast path
|
|
306
|
+
if (!enc) return enc
|
|
307
|
+
if (uppercasePrefixes.has(enc.slice(0, 3))) return enc.toUpperCase()
|
|
308
|
+
if (enc === 'big5') return 'Big5'
|
|
309
|
+
if (enc === 'shift_jis') return 'Shift_JIS'
|
|
310
|
+
return enc
|
|
311
|
+
}
|
package/fallback/multi-byte.js
CHANGED
|
@@ -13,7 +13,7 @@ export const E_STRICT = 'Input is not well-formed for this encoding'
|
|
|
13
13
|
const REP = 0xff_fd
|
|
14
14
|
const mappers = {
|
|
15
15
|
// https://encoding.spec.whatwg.org/#euc-kr-decoder
|
|
16
|
-
'euc-kr': () => {
|
|
16
|
+
'euc-kr': (err) => {
|
|
17
17
|
const euc = getTable('euc-kr')
|
|
18
18
|
let lead = 0
|
|
19
19
|
|
|
@@ -24,25 +24,24 @@ const mappers = {
|
|
|
24
24
|
lead = 0
|
|
25
25
|
if (cp !== undefined && cp !== REP) return cp
|
|
26
26
|
if (b < 128) pushback.push(b)
|
|
27
|
-
return
|
|
27
|
+
return err()
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
if (b < 128) return b
|
|
31
|
-
if (b < 0x81 || b === 0xff) return
|
|
31
|
+
if (b < 0x81 || b === 0xff) return err()
|
|
32
32
|
lead = b
|
|
33
|
-
return -1
|
|
34
33
|
}
|
|
35
34
|
|
|
36
35
|
const eof = () => {
|
|
37
36
|
if (!lead) return null
|
|
38
37
|
lead = 0
|
|
39
|
-
return
|
|
38
|
+
return err()
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
return { bytes, eof, pushback }
|
|
43
42
|
},
|
|
44
43
|
// https://encoding.spec.whatwg.org/#euc-jp-decoder
|
|
45
|
-
'euc-jp': () => {
|
|
44
|
+
'euc-jp': (err) => {
|
|
46
45
|
const jis0208 = getTable('jis0208')
|
|
47
46
|
const jis0212 = getTable('jis0212')
|
|
48
47
|
let j12 = false
|
|
@@ -58,7 +57,7 @@ const mappers = {
|
|
|
58
57
|
if (lead === 0x8f && b >= 0xa1 && b <= 0xfe) {
|
|
59
58
|
j12 = true
|
|
60
59
|
lead = b
|
|
61
|
-
return
|
|
60
|
+
return
|
|
62
61
|
}
|
|
63
62
|
|
|
64
63
|
if (lead) {
|
|
@@ -71,27 +70,26 @@ const mappers = {
|
|
|
71
70
|
j12 = false
|
|
72
71
|
if (cp !== undefined && cp !== REP) return cp
|
|
73
72
|
if (b < 128) pushback.push(b)
|
|
74
|
-
return
|
|
73
|
+
return err()
|
|
75
74
|
}
|
|
76
75
|
|
|
77
76
|
if (b < 128) return b
|
|
78
|
-
if ((b < 0xa1 && b !== 0x8e && b !== 0x8f) || b === 0xff) return
|
|
77
|
+
if ((b < 0xa1 && b !== 0x8e && b !== 0x8f) || b === 0xff) return err()
|
|
79
78
|
lead = b
|
|
80
|
-
return -1
|
|
81
79
|
}
|
|
82
80
|
|
|
83
81
|
// eslint-disable-next-line sonarjs/no-identical-functions
|
|
84
82
|
const eof = () => {
|
|
85
83
|
if (!lead) return null
|
|
86
84
|
lead = 0
|
|
87
|
-
return
|
|
85
|
+
return err()
|
|
88
86
|
}
|
|
89
87
|
|
|
90
88
|
return { bytes, eof, pushback }
|
|
91
89
|
},
|
|
92
90
|
// https://encoding.spec.whatwg.org/#iso-2022-jp-decoder
|
|
93
91
|
// Per-letter of the spec, don't shortcut on state changes on EOF. Some code is regrouped but preserving the logic
|
|
94
|
-
'iso-2022-jp': () => {
|
|
92
|
+
'iso-2022-jp': (err) => {
|
|
95
93
|
const jis0208 = getTable('jis0208')
|
|
96
94
|
const EOF = -1
|
|
97
95
|
let dState = 1
|
|
@@ -105,7 +103,7 @@ const mappers = {
|
|
|
105
103
|
if (b === EOF) return null
|
|
106
104
|
if (b === 0x1b) {
|
|
107
105
|
dState = 6 // escape start
|
|
108
|
-
return
|
|
106
|
+
return
|
|
109
107
|
}
|
|
110
108
|
}
|
|
111
109
|
|
|
@@ -120,49 +118,46 @@ const mappers = {
|
|
|
120
118
|
}
|
|
121
119
|
|
|
122
120
|
if (b <= 0x7f && b !== 0x0e && b !== 0x0f) return b
|
|
123
|
-
return
|
|
121
|
+
return err()
|
|
124
122
|
case 3:
|
|
125
123
|
// Katakana
|
|
126
124
|
out = false
|
|
127
125
|
if (b >= 0x21 && b <= 0x5f) return 0xff_40 + b
|
|
128
|
-
return
|
|
126
|
+
return err()
|
|
129
127
|
case 4:
|
|
130
128
|
// Leading byte
|
|
131
129
|
out = false
|
|
132
|
-
if (
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return -2
|
|
130
|
+
if (b < 0x21 || b > 0x7e) return err()
|
|
131
|
+
lead = b
|
|
132
|
+
dState = 5
|
|
133
|
+
return
|
|
139
134
|
case 5:
|
|
140
135
|
// Trailing byte
|
|
141
136
|
out = false
|
|
142
137
|
if (b === 0x1b) {
|
|
143
138
|
dState = 6 // escape start
|
|
144
|
-
return
|
|
139
|
+
return err()
|
|
145
140
|
}
|
|
146
141
|
|
|
147
142
|
dState = 4
|
|
148
143
|
if (b >= 0x21 && b <= 0x7e) {
|
|
149
144
|
const cp = jis0208[(lead - 0x21) * 94 + b - 0x21]
|
|
150
|
-
|
|
145
|
+
if (cp !== undefined && cp !== REP) return cp
|
|
151
146
|
}
|
|
152
147
|
|
|
153
|
-
return
|
|
148
|
+
return err()
|
|
154
149
|
case 6:
|
|
155
150
|
// Escape start
|
|
156
151
|
if (b === 0x24 || b === 0x28) {
|
|
157
152
|
lead = b
|
|
158
153
|
dState = 7
|
|
159
|
-
return
|
|
154
|
+
return
|
|
160
155
|
}
|
|
161
156
|
|
|
162
157
|
out = false
|
|
163
158
|
dState = oState
|
|
164
159
|
if (b !== EOF) pushback.push(b)
|
|
165
|
-
return
|
|
160
|
+
return err()
|
|
166
161
|
case 7: {
|
|
167
162
|
// Escape
|
|
168
163
|
const l = lead
|
|
@@ -185,14 +180,14 @@ const mappers = {
|
|
|
185
180
|
dState = oState = s
|
|
186
181
|
const output = out
|
|
187
182
|
out = true
|
|
188
|
-
return output ?
|
|
183
|
+
return output ? err() : undefined
|
|
189
184
|
}
|
|
190
185
|
|
|
191
186
|
out = false
|
|
192
187
|
dState = oState
|
|
193
188
|
if (b !== EOF) pushback.push(b)
|
|
194
189
|
pushback.push(l)
|
|
195
|
-
return
|
|
190
|
+
return err()
|
|
196
191
|
}
|
|
197
192
|
}
|
|
198
193
|
}
|
|
@@ -202,7 +197,7 @@ const mappers = {
|
|
|
202
197
|
return { bytes, eof, pushback }
|
|
203
198
|
},
|
|
204
199
|
// https://encoding.spec.whatwg.org/#shift_jis-decoder
|
|
205
|
-
shift_jis: () => {
|
|
200
|
+
shift_jis: (err) => {
|
|
206
201
|
const jis0208 = getTable('jis0208')
|
|
207
202
|
let lead = 0
|
|
208
203
|
|
|
@@ -219,29 +214,28 @@ const mappers = {
|
|
|
219
214
|
}
|
|
220
215
|
|
|
221
216
|
if (b < 128) pushback.push(b)
|
|
222
|
-
return
|
|
217
|
+
return err()
|
|
223
218
|
}
|
|
224
219
|
|
|
225
220
|
if (b <= 0x80) return b // 0x80 is allowed
|
|
226
221
|
if (b >= 0xa1 && b <= 0xdf) return 0xff_61 - 0xa1 + b
|
|
227
|
-
if (b < 0x81 || (b > 0x9f && b < 0xe0) || b > 0xfc) return
|
|
222
|
+
if (b < 0x81 || (b > 0x9f && b < 0xe0) || b > 0xfc) return err()
|
|
228
223
|
lead = b
|
|
229
|
-
return -1
|
|
230
224
|
}
|
|
231
225
|
|
|
232
226
|
// eslint-disable-next-line sonarjs/no-identical-functions
|
|
233
227
|
const eof = () => {
|
|
234
228
|
if (!lead) return null
|
|
235
229
|
lead = 0 // this clears state completely on EOF
|
|
236
|
-
return
|
|
230
|
+
return err()
|
|
237
231
|
}
|
|
238
232
|
|
|
239
233
|
return { bytes, eof, pushback }
|
|
240
234
|
},
|
|
241
235
|
// https://encoding.spec.whatwg.org/#gbk-decoder
|
|
242
|
-
gbk: () => mappers.gb18030(), // 10.1.1. GBK’s decoder is gb18030’s decoder
|
|
236
|
+
gbk: (err) => mappers.gb18030(err), // 10.1.1. GBK’s decoder is gb18030’s decoder
|
|
243
237
|
// https://encoding.spec.whatwg.org/#gb18030-decoder
|
|
244
|
-
gb18030: () => {
|
|
238
|
+
gb18030: (err) => {
|
|
245
239
|
const gb18030 = getTable('gb18030')
|
|
246
240
|
const gb18030r = getTable('gb18030-ranges')
|
|
247
241
|
let g1 = 0, g2 = 0, g3 = 0 // prettier-ignore
|
|
@@ -264,30 +258,30 @@ const mappers = {
|
|
|
264
258
|
if (b < 0x30 || b > 0x39) {
|
|
265
259
|
pushback.push(b, g3, g2)
|
|
266
260
|
g1 = g2 = g3 = 0
|
|
267
|
-
return
|
|
261
|
+
return err()
|
|
268
262
|
}
|
|
269
263
|
|
|
270
264
|
const cp = index((g1 - 0x81) * 12_600 + (g2 - 0x30) * 1260 + (g3 - 0x81) * 10 + b - 0x30)
|
|
271
265
|
g1 = g2 = g3 = 0
|
|
272
266
|
if (cp !== undefined) return cp // Can validly return replacement
|
|
273
|
-
return
|
|
267
|
+
return err()
|
|
274
268
|
}
|
|
275
269
|
|
|
276
270
|
if (g2) {
|
|
277
271
|
if (b >= 0x81 && b <= 0xfe) {
|
|
278
272
|
g3 = b
|
|
279
|
-
return
|
|
273
|
+
return
|
|
280
274
|
}
|
|
281
275
|
|
|
282
276
|
pushback.push(b, g2)
|
|
283
277
|
g1 = g2 = 0
|
|
284
|
-
return
|
|
278
|
+
return err()
|
|
285
279
|
}
|
|
286
280
|
|
|
287
281
|
if (g1) {
|
|
288
282
|
if (b >= 0x30 && b <= 0x39) {
|
|
289
283
|
g2 = b
|
|
290
|
-
return
|
|
284
|
+
return
|
|
291
285
|
}
|
|
292
286
|
|
|
293
287
|
let cp
|
|
@@ -298,20 +292,19 @@ const mappers = {
|
|
|
298
292
|
g1 = 0
|
|
299
293
|
if (cp !== undefined && cp !== REP) return cp
|
|
300
294
|
if (b < 128) pushback.push(b)
|
|
301
|
-
return
|
|
295
|
+
return err()
|
|
302
296
|
}
|
|
303
297
|
|
|
304
298
|
if (b < 128) return b
|
|
305
299
|
if (b === 0x80) return 0x20_ac
|
|
306
|
-
if (b === 0xff) return
|
|
300
|
+
if (b === 0xff) return err()
|
|
307
301
|
g1 = b
|
|
308
|
-
return -1
|
|
309
302
|
}
|
|
310
303
|
|
|
311
304
|
const eof = () => {
|
|
312
305
|
if (!g1 && !g2 && !g3) return null
|
|
313
306
|
g1 = g2 = g3 = 0
|
|
314
|
-
return
|
|
307
|
+
return err()
|
|
315
308
|
}
|
|
316
309
|
|
|
317
310
|
return { bytes, eof, pushback }
|
|
@@ -329,7 +322,7 @@ export function multibyteDecoder(enc, loose = false) {
|
|
|
329
322
|
const asciiSuperset = isAsciiSuperset(enc)
|
|
330
323
|
return (arr, stream = false) => {
|
|
331
324
|
const onErr = loose
|
|
332
|
-
? () =>
|
|
325
|
+
? () => REP
|
|
333
326
|
: () => {
|
|
334
327
|
mapper.pushback.length = 0 // the queue is cleared on returning an error
|
|
335
328
|
// The correct way per spec seems to be not destoying the decoder state in stream mode, even when fatal
|
|
@@ -346,7 +339,7 @@ export function multibyteDecoder(enc, loose = false) {
|
|
|
346
339
|
if (res.length === arr.length) return res // ascii
|
|
347
340
|
}
|
|
348
341
|
|
|
349
|
-
if (!mapper) mapper = mappers[enc]()
|
|
342
|
+
if (!mapper) mapper = mappers[enc](onErr)
|
|
350
343
|
const { bytes, eof, pushback } = mapper
|
|
351
344
|
let i = res.length
|
|
352
345
|
|
|
@@ -354,11 +347,8 @@ export function multibyteDecoder(enc, loose = false) {
|
|
|
354
347
|
// Same as the full loop, but without EOF handling
|
|
355
348
|
while (i < length || pushback.length > 0) {
|
|
356
349
|
const c = bytes(pushback.length > 0 ? pushback.pop() : arr[i++])
|
|
357
|
-
if (c
|
|
358
|
-
|
|
359
|
-
} else if (c === -2) {
|
|
360
|
-
res += onErr()
|
|
361
|
-
}
|
|
350
|
+
if (c === undefined) continue // consuming
|
|
351
|
+
res += String.fromCodePoint(c) // gb18030 returns codepoints above 0xFFFF from ranges
|
|
362
352
|
}
|
|
363
353
|
|
|
364
354
|
// Then, dump EOF. This needs the same loop as the characters can be pushed back
|
|
@@ -368,12 +358,8 @@ export function multibyteDecoder(enc, loose = false) {
|
|
|
368
358
|
const isEOF = i === length && pushback.length === 0
|
|
369
359
|
const c = isEOF ? eof() : bytes(pushback.length > 0 ? pushback.pop() : arr[i++])
|
|
370
360
|
if (isEOF && c === null) break // clean exit
|
|
371
|
-
if (c ===
|
|
372
|
-
|
|
373
|
-
res += onErr()
|
|
374
|
-
} else {
|
|
375
|
-
res += String.fromCodePoint(c) // gb18030 returns codepoints above 0xFFFF from ranges
|
|
376
|
-
}
|
|
361
|
+
if (c === undefined) continue // consuming
|
|
362
|
+
res += String.fromCodePoint(c) // gb18030 returns codepoints above 0xFFFF from ranges
|
|
377
363
|
}
|
|
378
364
|
}
|
|
379
365
|
|
|
@@ -392,12 +378,10 @@ function big5decoder(loose) {
|
|
|
392
378
|
// Input is assumed to be typechecked already
|
|
393
379
|
let lead = 0
|
|
394
380
|
let big5
|
|
395
|
-
const pushback = []
|
|
396
381
|
return (arr, stream = false) => {
|
|
397
382
|
const onErr = loose
|
|
398
383
|
? () => '\uFFFD'
|
|
399
384
|
: () => {
|
|
400
|
-
pushback.length = 0 // the queue is cleared on returning an error
|
|
401
385
|
// Lead is always already cleared before throwing
|
|
402
386
|
throw new TypeError(E_STRICT)
|
|
403
387
|
}
|
|
@@ -410,8 +394,8 @@ function big5decoder(loose) {
|
|
|
410
394
|
}
|
|
411
395
|
|
|
412
396
|
if (!big5) big5 = getTable('big5')
|
|
413
|
-
for (let i = res.length; i < length
|
|
414
|
-
const b =
|
|
397
|
+
for (let i = res.length; i < length; i++) {
|
|
398
|
+
const b = arr[i]
|
|
415
399
|
if (lead) {
|
|
416
400
|
let cp
|
|
417
401
|
if ((b >= 0x40 && b <= 0x7e) || (b >= 0xa1 && b !== 0xff)) {
|
|
@@ -423,7 +407,8 @@ function big5decoder(loose) {
|
|
|
423
407
|
res += cp // strings
|
|
424
408
|
} else {
|
|
425
409
|
res += onErr()
|
|
426
|
-
|
|
410
|
+
// same as pushing it back: lead is cleared, pushed back can't contain more than 1 byte
|
|
411
|
+
if (b < 128) res += String.fromCharCode(b)
|
|
427
412
|
}
|
|
428
413
|
} else if (b < 128) {
|
|
429
414
|
res += String.fromCharCode(b)
|
|
@@ -434,13 +419,10 @@ function big5decoder(loose) {
|
|
|
434
419
|
}
|
|
435
420
|
}
|
|
436
421
|
|
|
437
|
-
if (!stream) {
|
|
422
|
+
if (!stream && lead) {
|
|
438
423
|
// Destroy decoder state
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
lead = 0
|
|
442
|
-
res += onErr()
|
|
443
|
-
}
|
|
424
|
+
lead = 0
|
|
425
|
+
res += onErr()
|
|
444
426
|
}
|
|
445
427
|
|
|
446
428
|
return res
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/bytes",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Various operations on Uint8Array data",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"lint": "eslint .",
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
"files": [
|
|
46
46
|
"/fallback/_utils.js",
|
|
47
47
|
"/fallback/base32.js",
|
|
48
|
+
"/fallback/base58check.js",
|
|
48
49
|
"/fallback/base64.js",
|
|
49
50
|
"/fallback/encoding.js",
|
|
50
51
|
"/fallback/encoding.labels.js",
|
|
@@ -65,6 +66,7 @@
|
|
|
65
66
|
"/base32.js",
|
|
66
67
|
"/base58.js",
|
|
67
68
|
"/base58check.js",
|
|
69
|
+
"/base58check.node.js",
|
|
68
70
|
"/base64.js",
|
|
69
71
|
"/base64.d.ts",
|
|
70
72
|
"/bech32.js",
|
|
@@ -92,7 +94,10 @@
|
|
|
92
94
|
},
|
|
93
95
|
"./base32.js": "./base32.js",
|
|
94
96
|
"./base58.js": "./base58.js",
|
|
95
|
-
"./base58check.js":
|
|
97
|
+
"./base58check.js": {
|
|
98
|
+
"node": "./base58check.node.js",
|
|
99
|
+
"default": "./base58check.js"
|
|
100
|
+
},
|
|
96
101
|
"./base64.js": {
|
|
97
102
|
"types": "./base64.d.ts",
|
|
98
103
|
"default": "./base64.js"
|
package/utf16.js
CHANGED
|
@@ -9,7 +9,7 @@ const decoderFatalBE = canDecoders ? new TextDecoder('utf-16be', { ignoreBOM, fa
|
|
|
9
9
|
const decoderLooseBE = canDecoders ? new TextDecoder('utf-16be', { ignoreBOM }) : null
|
|
10
10
|
const decoderFatal16 = isLE ? decoderFatalLE : decoderFatalBE
|
|
11
11
|
const decoderLoose16 = isLE ? decoderLooseLE : decoderFatalBE
|
|
12
|
-
const { isWellFormed } = String.prototype
|
|
12
|
+
const { isWellFormed, toWellFormed } = String.prototype
|
|
13
13
|
|
|
14
14
|
const { E_STRICT, E_STRICT_UNICODE } = js
|
|
15
15
|
|
|
@@ -61,8 +61,9 @@ function decode(input, loose = false, format = 'uint16') {
|
|
|
61
61
|
throw new TypeError('Unknown format')
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
const str = js.decode(u16, loose, !loose && isWellFormed)
|
|
64
|
+
const str = js.decode(u16, loose, (!loose && isWellFormed) || (loose && toWellFormed))
|
|
65
65
|
if (!loose && isWellFormed && !isWellFormed.call(str)) throw new TypeError(E_STRICT)
|
|
66
|
+
if (loose && toWellFormed) return toWellFormed.call(str)
|
|
66
67
|
|
|
67
68
|
return str
|
|
68
69
|
}
|
package/utf16.node.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isDeno, isLE } from './fallback/_utils.js'
|
|
2
2
|
import { E_STRICT, E_STRICT_UNICODE } from './fallback/utf16.js'
|
|
3
3
|
|
|
4
4
|
if (Buffer.TYPED_ARRAY_SUPPORT) throw new Error('Unexpected Buffer polyfill')
|
|
5
5
|
|
|
6
|
-
const { isWellFormed } = String.prototype
|
|
6
|
+
const { isWellFormed, toWellFormed } = String.prototype
|
|
7
7
|
const to8 = (a) => new Uint8Array(a.buffer, a.byteOffset, a.byteLength)
|
|
8
8
|
|
|
9
9
|
// Unlike utf8, operates on Uint16Arrays by default
|
|
@@ -14,9 +14,10 @@ function encode(str, loose = false, format = 'uint16') {
|
|
|
14
14
|
throw new TypeError('Unknown format')
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
if (
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
if (loose) {
|
|
18
|
+
str = toWellFormed.call(str) // Buffer doesn't do this with utf16 encoding
|
|
19
|
+
} else if (!isWellFormed.call(str)) {
|
|
20
|
+
throw new TypeError(E_STRICT_UNICODE)
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
const ble = Buffer.from(str, 'utf-16le')
|
|
@@ -51,9 +52,9 @@ function decodeNode(input, loose = false, format = 'uint16') {
|
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
const str = ble.ucs2Slice(0, ble.byteLength)
|
|
55
|
+
if (loose) return toWellFormed.call(str)
|
|
54
56
|
if (isWellFormed.call(str)) return str
|
|
55
|
-
|
|
56
|
-
return nativeDecoder.decode(Buffer.from(str)) // fixup (see above)
|
|
57
|
+
throw new TypeError(E_STRICT)
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
function decodeDecoder(input, loose = false, format = 'uint16') {
|
package/wif.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { toBase58checkSync, fromBase58checkSync } from '
|
|
1
|
+
import { toBase58checkSync, fromBase58checkSync } from '@exodus/bytes/base58check.js'
|
|
2
2
|
import { assertUint8 } from './assert.js'
|
|
3
3
|
|
|
4
4
|
// Mostly matches npmjs.com/wif, but with extra checks + using our base58check
|