Skip to content

Commit 515007a

Browse files
committed
test: fromBase58 never returns pooled non-Buffers
1 parent eaafab6 commit 515007a

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

tests/base58.test.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { fromBase58, toBase58, fromBase58xrp, toBase58xrp } from '@exodus/bytes/base58.js'
1+
import {
2+
fromBase58 as fromBase58raw,
3+
toBase58,
4+
fromBase58xrp,
5+
toBase58xrp,
6+
} from '@exodus/bytes/base58.js'
27
import { fromHex } from '@exodus/bytes/hex.js'
38
import { randomValues } from '@exodus/crypto/randomBytes'
49
import { hashSync } from '@exodus/crypto/hash' // eslint-disable-line @exodus/import/no-deprecated
@@ -13,6 +18,14 @@ const toShared = (u8) => {
1318
return res
1419
}
1520

21+
const tracked = []
22+
23+
function fromBase58(...args) {
24+
const res = fromBase58raw(...args)
25+
tracked.push(res)
26+
return res
27+
}
28+
1629
// https://en.bitcoin.it/wiki/Base58Check_encoding#Creating_a_Base58Check_string
1730
const toChecked = (version, pub) => {
1831
const data = [Uint8Array.of(version), pub]
@@ -115,16 +128,24 @@ test('toBase58 matches bs58, maximum char repeated', (t) => {
115128
}
116129
})
117130

118-
test('sizes roundtrip, static data', (t) => {
131+
test('sizes roundtrip, static data + types', (t) => {
119132
for (let size = 0; size < 260; size++) {
120133
const zeros = new Uint8Array(size)
121-
t.assert.deepStrictEqual(fromBase58(toBase58(zeros)), zeros, `[0] x${size}`)
134+
const zerosBase58 = toBase58(zeros)
135+
t.assert.deepStrictEqual(fromBase58(zerosBase58), zeros, `[0] x${size}`)
136+
t.assert.deepStrictEqual(fromBase58(zerosBase58, 'buffer'), Buffer.from(zeros), `[0] x${size}`)
122137
const ones = new Uint8Array(size).fill(1)
123-
t.assert.deepStrictEqual(fromBase58(toBase58(ones)), ones, `[1] x${size}`)
138+
const onesBase58 = toBase58(ones)
139+
t.assert.deepStrictEqual(fromBase58(onesBase58), ones, `[1] x${size}`)
140+
t.assert.deepStrictEqual(fromBase58(onesBase58, 'buffer'), Buffer.from(ones), `[0] x${size}`)
124141
const mid = new Uint8Array(size).fill(42)
125-
t.assert.deepStrictEqual(fromBase58(toBase58(mid)), mid, `[42] x${size}`)
142+
const midBase58 = toBase58(mid)
143+
t.assert.deepStrictEqual(fromBase58(midBase58), mid, `[42] x${size}`)
144+
t.assert.deepStrictEqual(fromBase58(midBase58, 'buffer'), Buffer.from(mid), `[0] x${size}`)
126145
const max = new Uint8Array(size).fill(255)
127-
t.assert.deepStrictEqual(fromBase58(toBase58(max)), max, `[255] x${size}`)
146+
const maxBase58 = toBase58(max)
147+
t.assert.deepStrictEqual(fromBase58(maxBase58), max, `[255] x${size}`)
148+
t.assert.deepStrictEqual(fromBase58(maxBase58, 'buffer'), Buffer.from(max), `[0] x${size}`)
128149
}
129150
})
130151

@@ -165,3 +186,12 @@ test('sizes roundtrip, random data', (t) => {
165186
t.assert.deepStrictEqual(fromBase58(toBase58(arr)), arr, `random x${size}`)
166187
}
167188
})
189+
190+
test('fromBase58 returns non-pooled Uint8Array instances', (t) => {
191+
t.assert.ok(tracked.length > 1000)
192+
193+
for (const u8 of tracked) {
194+
if (Buffer.isBuffer(u8)) continue
195+
t.assert.strictEqual(u8.byteLength, u8.buffer.byteLength)
196+
}
197+
})

0 commit comments

Comments
 (0)