Skip to content

Commit 49fe016

Browse files
committed
test: add long/short zeros and max char base58 tests
1 parent 03c428e commit 49fe016

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

base58.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ function toBase58core(arr, alphabet, codes) {
121121
return ZERO.repeat(zeros) + out
122122
}
123123

124-
// TODO: test on 'z'.repeat(from 1 to smth)
125124
function fromBase58core(str, alphabet, codes, format = 'uint8') {
126125
if (typeof str !== 'string') throw new TypeError('Input is not a string')
127126
const length = str.length

tests/base58.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import { randomValues } from '@exodus/crypto/randomBytes'
33
import { test } from 'node:test'
44
import bs58 from 'bs58'
55

6+
test('zeros', (t) => {
7+
for (let size = 0; size <= 1024; size++) {
8+
const zeros = new Uint8Array(size)
9+
const expected = '1'.repeat(size)
10+
t.assert.strictEqual(toBase58(zeros), expected, `[0] x${size} toBase58`)
11+
t.assert.strictEqual(bs58.encode(zeros), expected, `[0] x${size} bs58.encode`) // matches bs58
12+
t.assert.deepStrictEqual(fromBase58(expected), zeros, `[0] x${size} fromBase58`)
13+
}
14+
})
15+
616
test('toBase58 matches bs58, static data', (t) => {
717
for (let size = 0; size < 180; size++) {
818
const zeros = new Uint8Array(size)
@@ -16,6 +26,16 @@ test('toBase58 matches bs58, static data', (t) => {
1626
}
1727
})
1828

29+
test('toBase58 matches bs58, maximum char repeated', (t) => {
30+
const maxChar = 'z'
31+
for (let size = 0; size < 300; size++) {
32+
const encoded = maxChar.repeat(size)
33+
const decoded = fromBase58(encoded)
34+
t.assert.strictEqual(toBase58(decoded), encoded, `${maxChar} x${size} toBase58`)
35+
t.assert.strictEqual(bs58.encode(decoded), encoded, `${maxChar} x${size}`) // matches bs58
36+
}
37+
})
38+
1939
test('sizes roundtrip, static data', (t) => {
2040
for (let size = 0; size < 260; size++) {
2141
const zeros = new Uint8Array(size)

0 commit comments

Comments
 (0)