Skip to content

Commit 887d526

Browse files
committed
feat: fromBase58check never returns pooled non-Buffers
This is also slightly faster, as subarray is slower than slice on Uint8Array instances allocated in heap (length <= 64)
1 parent bbcb036 commit 887d526

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

fallback/base58check.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ function encodeWithChecksum(arr, checksum) {
1010
// arr type in already validated in input
1111
const res = new Uint8Array(arr.length + 4)
1212
res.set(arr, 0)
13-
res.set(checksum.subarray(0, 4), arr.length)
13+
res.set(checksum.slice(0, 4), arr.length)
1414
return toBase58(res)
1515
}
1616

1717
function decodeWithChecksum(str) {
1818
const arr = fromBase58(str) // checks input
1919
const payloadSize = arr.length - 4
2020
if (payloadSize < 0) throw new Error(E_CHECKSUM)
21-
return [arr.subarray(0, payloadSize), arr.subarray(payloadSize)]
21+
return [arr.slice(0, payloadSize), arr.slice(payloadSize)]
2222
}
2323

2424
function assertChecksum(c, r) {

tests/base58check.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js'
1+
import { fromBase58checkSync as fromBase58c, toBase58checkSync } from '@exodus/bytes/base58check.js'
22
import { randomValues } from '@exodus/crypto/randomBytes'
33
import { test } from 'node:test'
44
import bs58check from 'bs58check'
@@ -10,6 +10,14 @@ const toShared = (u8) => {
1010
return res
1111
}
1212

13+
const tracked = []
14+
15+
function fromBase58checkSync(...args) {
16+
const res = fromBase58c(...args)
17+
tracked.push(res)
18+
return res
19+
}
20+
1321
test('zeros', (t) => {
1422
for (let size = 0; size <= 1024; size++) {
1523
const zeros = new Uint8Array(size)
@@ -91,3 +99,12 @@ test('sizes roundtrip, random data', (t) => {
9199
t.assert.deepStrictEqual(fromBase58checkSync(toBase58checkSync(arr)), arr, `random x${size}`)
92100
}
93101
})
102+
103+
test('fromBase58check returns non-pooled Uint8Array instances', (t) => {
104+
t.assert.ok(tracked.length > 1000)
105+
106+
for (const u8 of tracked) {
107+
if (Buffer.isBuffer(u8)) continue
108+
t.assert.strictEqual(u8.byteLength, u8.buffer.byteLength)
109+
}
110+
})

0 commit comments

Comments
 (0)