Skip to content

Commit 47a2cc2

Browse files
committed
refactor: use internal fromUint8 for output conversion
1 parent 887d526 commit 47a2cc2

File tree

6 files changed

+27
-19
lines changed

6 files changed

+27
-19
lines changed

base32.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { assertEmptyRest } from './assert.js'
2-
import { typedView } from './array.js'
3-
import { E_STRING } from './fallback/_utils.js'
2+
import { fromUint8, E_STRING } from './fallback/_utils.js'
43
import * as js from './fallback/base32.js'
54

65
// See https://datatracker.ietf.org/doc/html/rfc4648
@@ -19,7 +18,7 @@ function fromBase32common(str, mode, padding, format, rest) {
1918
throw new TypeError('Invalid padding option')
2019
}
2120

22-
return typedView(js.fromBase32(str, mode), format)
21+
return fromUint8(js.fromBase32(str, mode), format)
2322
}
2423

2524
// By default, valid padding is accepted but not required

base58.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { typedView } from './array.js'
2-
import { assertU8, E_STRING } from './fallback/_utils.js'
1+
import { assertU8, fromUint8, E_STRING } from './fallback/_utils.js'
32
import { nativeDecoder, nativeEncoder, isHermes } from './fallback/platform.js'
43
import { encodeAscii, decodeAscii } from './fallback/latin1.js'
54

@@ -124,7 +123,7 @@ function toBase58core(arr, alphabet, codes) {
124123
function fromBase58core(str, alphabet, codes, format = 'uint8') {
125124
if (typeof str !== 'string') throw new TypeError(E_STRING)
126125
const length = str.length
127-
if (length === 0) return typedView(new Uint8Array(), format)
126+
if (length === 0) return fromUint8(new Uint8Array(), format)
128127

129128
const zeroC = codes[0]
130129
let zeros = 0
@@ -211,7 +210,7 @@ function fromBase58core(str, alphabet, codes, format = 'uint8') {
211210
}
212211
}
213212

214-
return typedView(res.slice(at - zeros), format) // slice is faster for small sizes than subarray
213+
return fromUint8(res.slice(at - zeros), format)
215214
}
216215

217216
export const toBase58 = (arr) => toBase58core(arr, alphabet58, codes58)

fallback/_utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,16 @@ export const toBuf = (x) =>
1818

1919
export const E_STRING = 'Input is not a string'
2020
export const E_STRICT_UNICODE = 'Input is not well-formed Unicode'
21+
22+
export function fromUint8(arr, format) {
23+
switch (format) {
24+
case 'uint8':
25+
if (arr.constructor !== Uint8Array) throw new Error('Unexpected')
26+
return arr
27+
case 'buffer':
28+
if (arr.length <= 64) return Buffer.from(arr)
29+
return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength)
30+
}
31+
32+
throw new TypeError('Unexpected format')
33+
}

fallback/base58check.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { typedView } from '@exodus/bytes/array.js'
21
import { toBase58, fromBase58 } from '@exodus/bytes/base58.js'
3-
import { assertU8 } from './_utils.js'
2+
import { assertU8, fromUint8 } from './_utils.js'
43

54
const E_CHECKSUM = 'Invalid checksum'
65

@@ -34,7 +33,7 @@ export const makeBase58check = (hashAlgo, hashAlgoSync) => {
3433
async decode(str, format = 'uint8') {
3534
const [payload, checksum] = decodeWithChecksum(str)
3635
assertChecksum(checksum, await hashAlgo(payload))
37-
return typedView(payload, format)
36+
return fromUint8(payload, format)
3837
},
3938
}
4039
if (!hashAlgoSync) return apis
@@ -47,7 +46,7 @@ export const makeBase58check = (hashAlgo, hashAlgoSync) => {
4746
decodeSync(str, format = 'uint8') {
4847
const [payload, checksum] = decodeWithChecksum(str)
4948
assertChecksum(checksum, hashAlgoSync(payload))
50-
return typedView(payload, format)
49+
return fromUint8(payload, format)
5150
},
5251
}
5352
}

hex.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { typedView } from './array.js'
2-
import { assertU8 } from './fallback/_utils.js'
1+
import { assertU8, fromUint8 } from './fallback/_utils.js'
32
import * as js from './fallback/hex.js'
43

54
const { toHex: webHex } = Uint8Array.prototype // Modern engines have this
@@ -13,5 +12,5 @@ export function toHex(arr) {
1312

1413
// Unlike Buffer.from(), throws on invalid input
1514
export const fromHex = Uint8Array.fromHex
16-
? (str, format = 'uint8') => typedView(Uint8Array.fromHex(str), format)
17-
: (str, format = 'uint8') => typedView(js.fromHex(str), format)
15+
? (str, format = 'uint8') => fromUint8(Uint8Array.fromHex(str), format)
16+
: (str, format = 'uint8') => fromUint8(js.fromHex(str), format)

utf8.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { typedView } from './array.js'
2-
import { assertU8, E_STRING, E_STRICT_UNICODE } from './fallback/_utils.js'
1+
import { assertU8, fromUint8, E_STRING, E_STRICT_UNICODE } from './fallback/_utils.js'
32
import { nativeDecoder, nativeEncoder } from './fallback/platform.js'
43
import * as js from './fallback/utf8.auto.js'
54

@@ -60,7 +59,7 @@ function decode(arr, loose = false) {
6059
return js.decodeFast(arr, loose)
6160
}
6261

63-
export const utf8fromString = (str, format = 'uint8') => typedView(encode(str, false), format)
64-
export const utf8fromStringLoose = (str, format = 'uint8') => typedView(encode(str, true), format)
62+
export const utf8fromString = (str, format = 'uint8') => fromUint8(encode(str, false), format)
63+
export const utf8fromStringLoose = (str, format = 'uint8') => fromUint8(encode(str, true), format)
6564
export const utf8toString = (arr) => decode(arr, false)
6665
export const utf8toStringLoose = (arr) => decode(arr, true)

0 commit comments

Comments
 (0)