-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbase58.test.js
More file actions
201 lines (178 loc) · 7.5 KB
/
base58.test.js
File metadata and controls
201 lines (178 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import {
fromBase58 as fromBase58raw,
toBase58,
fromBase58xrp,
toBase58xrp,
} from '@exodus/bytes/base58.js'
import { fromHex } from '@exodus/bytes/hex.js'
import { randomValues } from '@exodus/crypto/randomBytes'
import { hashSync } from '@exodus/crypto/hash' // eslint-disable-line @exodus/import/no-deprecated
import { describe, test } from 'node:test'
import bs58 from 'bs58'
import xAddressCodecFixtures from './vendor/x-address-codec/fixtures/base58.cjs'
const SharedArrayBuffer = globalThis.SharedArrayBuffer ?? ArrayBuffer
const toShared = (u8) => {
const res = new Uint8Array(new SharedArrayBuffer(u8.length))
res.set(u8)
return res
}
const tracked = []
function fromBase58(...args) {
const res = fromBase58raw(...args)
tracked.push(res)
return res
}
// https://en.bitcoin.it/wiki/Base58Check_encoding#Creating_a_Base58Check_string
const toChecked = (version, pub) => {
const data = [Uint8Array.of(version), pub]
// eslint-disable-next-line @exodus/import/no-deprecated
const hashed = hashSync('sha256', hashSync('sha256', data, 'uint8'), 'uint8')
return Uint8Array.from(Buffer.concat([...data, hashed.subarray(0, 4)]))
}
describe('static vectors', () => {
test('base58', (t) => {
const fixtures = [
['skip', fromHex('971a55')], // https://github.com/bitcoin/bitcoin/blob/13891a8a68/src/test/base58_tests.cpp
]
for (const [string, u8] of fixtures) {
t.assert.deepStrictEqual(fromBase58(string), u8, string)
t.assert.strictEqual(toBase58(u8), string)
t.assert.strictEqual(toBase58(toShared(u8)), string)
}
})
test('base58xrp', (t) => {
const fixtures = [
// https://xrpl.org/docs/tutorials/how-tos/use-specialized-payment-types/use-payment-channels#example-values
[
'aB44YfzW24VDEJQ2UuLPV2PvqcPCSoLnL7y5M1EzhdW4LnK5xMS3',
toChecked(
0x23, // https://xrpl.org/docs/references/protocol/data-types/base58-encodings
fromHex('023693F15967AE357D0327974AD46FE3C127113B1110D6044FD41E723689F81CC6')
),
],
]
for (const [string, u8] of fixtures) {
t.assert.deepStrictEqual(fromBase58xrp(string), u8, string)
t.assert.strictEqual(toBase58xrp(u8), string)
t.assert.strictEqual(toBase58xrp(toShared(u8)), string)
}
})
})
describe('x-address-codec fixtures', () => {
test('base58', (t) => {
for (const { hex, string } of xAddressCodecFixtures.bitcoin) {
const u8 = fromHex(hex)
t.assert.strictEqual(toBase58(u8), string)
t.assert.strictEqual(toBase58(toShared(u8)), string)
t.assert.deepStrictEqual(fromBase58(string), u8, string)
}
})
test('base58xrp', (t) => {
for (const { hex, string } of xAddressCodecFixtures.ripple) {
const u8 = fromHex(hex)
t.assert.strictEqual(toBase58xrp(u8), string)
t.assert.strictEqual(toBase58xrp(toShared(u8)), string)
t.assert.deepStrictEqual(fromBase58xrp(string), u8, string)
}
})
test('invalid', (t) => {
for (const { description, string } of xAddressCodecFixtures.invalid) {
t.assert.throws(() => fromBase58(string), description)
t.assert.throws(() => fromBase58xrp(string), description)
}
})
})
test('zeros', (t) => {
for (let size = 0; size <= 1024; size++) {
const zeros = new Uint8Array(size)
const expected = '1'.repeat(size)
t.assert.strictEqual(toBase58(zeros), expected, `[0] x${size} toBase58`)
t.assert.strictEqual(toBase58(toShared(zeros)), expected, `[0] x${size} toBase58`)
t.assert.strictEqual(bs58.encode(zeros), expected, `[0] x${size} bs58.encode`) // matches bs58
t.assert.deepStrictEqual(fromBase58(expected), zeros, `[0] x${size} fromBase58`)
}
})
test('toBase58 matches bs58, static data', (t) => {
for (let size = 0; size < 180; size++) {
const zeros = new Uint8Array(size)
t.assert.strictEqual(toBase58(zeros), bs58.encode(zeros), `[0] x${size}`)
const ones = new Uint8Array(size).fill(1)
t.assert.strictEqual(toBase58(ones), bs58.encode(ones), `[1] x${size}`)
const mid = new Uint8Array(size).fill(42)
t.assert.strictEqual(toBase58(mid), bs58.encode(mid), `[42] x${size}`)
const max = new Uint8Array(size).fill(255)
t.assert.strictEqual(toBase58(max), bs58.encode(max), `[255] x${size}`)
}
})
test('toBase58 matches bs58, maximum char repeated', (t) => {
const maxChar = 'z'
for (let size = 0; size < 300; size++) {
const encoded = maxChar.repeat(size)
const decoded = fromBase58(encoded)
t.assert.strictEqual(toBase58(decoded), encoded, `${maxChar} x${size} toBase58`)
t.assert.strictEqual(bs58.encode(decoded), encoded, `${maxChar} x${size}`) // matches bs58
}
})
test('sizes roundtrip, static data + types', (t) => {
for (let size = 0; size < 260; size++) {
const zeros = new Uint8Array(size)
const zerosBase58 = toBase58(zeros)
t.assert.deepStrictEqual(fromBase58(zerosBase58), zeros, `[0] x${size}`)
t.assert.deepStrictEqual(fromBase58(zerosBase58, 'buffer'), Buffer.from(zeros))
t.assert.deepStrictEqual(fromBase58(zerosBase58, 'arraybuffer'), zeros.buffer)
const ones = new Uint8Array(size).fill(1)
const onesBase58 = toBase58(ones)
t.assert.deepStrictEqual(fromBase58(onesBase58), ones, `[1] x${size}`)
t.assert.deepStrictEqual(fromBase58(onesBase58, 'buffer'), Buffer.from(ones))
t.assert.deepStrictEqual(fromBase58(onesBase58, 'arraybuffer'), ones.buffer)
const mid = new Uint8Array(size).fill(42)
const midBase58 = toBase58(mid)
t.assert.deepStrictEqual(fromBase58(midBase58), mid, `[42] x${size}`)
t.assert.deepStrictEqual(fromBase58(midBase58, 'buffer'), Buffer.from(mid))
t.assert.deepStrictEqual(fromBase58(midBase58, 'arraybuffer'), mid.buffer)
const max = new Uint8Array(size).fill(255)
const maxBase58 = toBase58(max)
t.assert.deepStrictEqual(fromBase58(maxBase58), max, `[255] x${size}`)
t.assert.deepStrictEqual(fromBase58(maxBase58, 'buffer'), Buffer.from(max))
t.assert.deepStrictEqual(fromBase58(maxBase58, 'arraybuffer'), max.buffer)
}
})
test('toBase58 matches bs58, random data', (t) => {
const seed = randomValues(260)
// more samples for small sizes
for (let size = 1; size < 100; size++) {
const samples = size < 60 ? 100 : 10
for (let start = 0, i = 0; start < seed.length - size && i < samples; start++, i++) {
const arr = seed.subarray(start, start + size)
t.assert.strictEqual(toBase58(arr), bs58.encode(arr), `random x${size}`)
}
}
// and one sample for all sizes in range
for (let size = 0; size < seed.length; size++) {
const arr = seed.subarray(seed.length - size)
t.assert.strictEqual(toBase58(arr), bs58.encode(arr), `random x${size}`)
}
})
test('sizes roundtrip, random data', (t) => {
const seed = randomValues(300)
// more samples for small sizes
for (let size = 1; size < 100; size++) {
const samples = size < 60 ? 100 : 10
for (let start = 0, i = 0; start < seed.length - size && i < samples; start++, i++) {
const arr = seed.subarray(start, start + size)
t.assert.deepStrictEqual(fromBase58(toBase58(arr)), arr, `random x${size}`)
}
}
// and one sample for all sizes in range
for (let size = 0; size < seed.length; size++) {
const arr = seed.subarray(seed.length - size)
t.assert.deepStrictEqual(fromBase58(toBase58(arr)), arr, `random x${size}`)
}
})
test('fromBase58 returns non-pooled Uint8Array instances', (t) => {
t.assert.ok(tracked.length > 1000)
for (const u8 of tracked) {
if (Buffer.isBuffer(u8) || u8 instanceof ArrayBuffer) continue
t.assert.strictEqual(u8.byteLength, u8.buffer.byteLength)
}
})