Skip to content

Commit 88bb326

Browse files
committed
test: more base58 vector tests
1 parent 49fe016 commit 88bb326

3 files changed

Lines changed: 211 additions & 2 deletions

File tree

tests/base58.test.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,73 @@
1-
import { fromBase58, toBase58 } from '@exodus/bytes/base58.js'
1+
import { fromBase58, toBase58, fromBase58xrp, toBase58xrp } from '@exodus/bytes/base58.js'
2+
import { fromHex } from '@exodus/bytes/hex.js'
23
import { randomValues } from '@exodus/crypto/randomBytes'
3-
import { test } from 'node:test'
4+
import { hash } from '@exodus/crypto/hash'
5+
import { describe, test } from 'node:test'
46
import bs58 from 'bs58'
7+
import xAddressCodecFixtures from './vendor/x-address-codec/fixtures/base58.cjs'
8+
9+
// https://en.bitcoin.it/wiki/Base58Check_encoding#Creating_a_Base58Check_string
10+
const toChecked = async (version, pub) => {
11+
const data = [Uint8Array.of(version), pub]
12+
const hashed = await hash('sha256', await hash('sha256', data, 'uint8'), 'uint8')
13+
return Uint8Array.from(Buffer.concat([...data, hashed.subarray(0, 4)]))
14+
}
15+
16+
describe('static vectors', () => {
17+
test('base58', (t) => {
18+
const fixtures = [
19+
['skip', fromHex('971a55')], // https://github.com/bitcoin/bitcoin/blob/13891a8a68/src/test/base58_tests.cpp
20+
]
21+
22+
for (const [string, u8] of fixtures) {
23+
t.assert.deepStrictEqual(fromBase58(string), u8, string)
24+
t.assert.strictEqual(toBase58(u8), string)
25+
}
26+
})
27+
28+
test('base58xrp', async (t) => {
29+
const fixtures = [
30+
// https://xrpl.org/docs/tutorials/how-tos/use-specialized-payment-types/use-payment-channels#example-values
31+
[
32+
'aB44YfzW24VDEJQ2UuLPV2PvqcPCSoLnL7y5M1EzhdW4LnK5xMS3',
33+
await toChecked(
34+
0x23, // https://xrpl.org/docs/references/protocol/data-types/base58-encodings
35+
fromHex('023693F15967AE357D0327974AD46FE3C127113B1110D6044FD41E723689F81CC6')
36+
),
37+
],
38+
]
39+
40+
for (const [string, u8] of fixtures) {
41+
t.assert.deepStrictEqual(fromBase58xrp(string), u8, string)
42+
t.assert.strictEqual(toBase58xrp(u8), string)
43+
}
44+
})
45+
})
46+
47+
describe('x-address-codec fixtures', () => {
48+
test('base58', (t) => {
49+
for (const { hex, string } of xAddressCodecFixtures.bitcoin) {
50+
const u8 = fromHex(hex)
51+
t.assert.strictEqual(toBase58(u8), string)
52+
t.assert.deepStrictEqual(fromBase58(string), u8, string)
53+
}
54+
})
55+
56+
test('base58xrp', (t) => {
57+
for (const { hex, string } of xAddressCodecFixtures.ripple) {
58+
const u8 = fromHex(hex)
59+
t.assert.strictEqual(toBase58xrp(u8), string)
60+
t.assert.deepStrictEqual(fromBase58xrp(string), u8, string)
61+
}
62+
})
63+
64+
test('invalid', (t) => {
65+
for (const { description, string } of xAddressCodecFixtures.invalid) {
66+
t.assert.throws(() => fromBase58(string), description)
67+
t.assert.throws(() => fromBase58xrp(string), description)
68+
}
69+
})
70+
})
571

672
test('zeros', (t) => {
773
for (let size = 0; size <= 1024; size++) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Fixture from https://github.com/ripple/x-address-codec/blob/master/test/fixtures/base58.json
2+
module.exports = require('./base58.json')
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
{
2+
"ripple": [
3+
{
4+
"hex": "",
5+
"string": ""
6+
},
7+
{
8+
"hex": "61",
9+
"string": "pg"
10+
},
11+
{
12+
"hex": "626262",
13+
"string": "2sgV"
14+
},
15+
{
16+
"hex": "636363",
17+
"string": "2PNi"
18+
},
19+
{
20+
"hex": "73696d706c792061206c6f6e6720737472696e67",
21+
"string": "pcEuFj68N1S8n9qHX1tmKpCCFLvp"
22+
},
23+
{
24+
"hex": "00eb15231dfceb60925886b67d065299925915aeb172c06647",
25+
"string": "r4Srf52g9jJgTHDrVXjvLUN8ZuQsiJDN9L"
26+
},
27+
{
28+
"hex": "516b6fcd0f",
29+
"string": "wB8LTmg"
30+
},
31+
{
32+
"hex": "bf4f89001e670274dd",
33+
"string": "sSNosLWLoP8tU"
34+
},
35+
{
36+
"hex": "572e4794",
37+
"string": "sNE7fm"
38+
},
39+
{
40+
"hex": "ecac89cad93923c02321",
41+
"string": "NJDM3diCXwauyw"
42+
},
43+
{
44+
"hex": "10c8511e",
45+
"string": "Rtnzm"
46+
},
47+
{
48+
"hex": "00000000000000000000",
49+
"string": "rrrrrrrrrr"
50+
},
51+
{
52+
"hex": "801184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd206ec97e",
53+
"string": "nHxrnHEGyeFpUCPx1JKepCXJ1UV8nDN5yoeGGEaJZjGbTR8qC5D"
54+
},
55+
{
56+
"hex": "003c176e659bea0f29a3e9bf7880c112b1b31b4dc826268187",
57+
"string": "ra7jcY4BG9GTKhuqpCfyYNbu5CqUzoLMGS"
58+
}
59+
],
60+
"bitcoin": [
61+
{
62+
"hex": "",
63+
"string": ""
64+
},
65+
{
66+
"hex": "61",
67+
"string": "2g"
68+
},
69+
{
70+
"hex": "626262",
71+
"string": "a3gV"
72+
},
73+
{
74+
"hex": "636363",
75+
"string": "aPEr"
76+
},
77+
{
78+
"hex": "73696d706c792061206c6f6e6720737472696e67",
79+
"string": "2cFupjhnEsSn59qHXstmK2ffpLv2"
80+
},
81+
{
82+
"hex": "00eb15231dfceb60925886b67d065299925915aeb172c06647",
83+
"string": "1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L"
84+
},
85+
{
86+
"hex": "516b6fcd0f",
87+
"string": "ABnLTmg"
88+
},
89+
{
90+
"hex": "bf4f89001e670274dd",
91+
"string": "3SEo3LWLoPntC"
92+
},
93+
{
94+
"hex": "572e4794",
95+
"string": "3EFU7m"
96+
},
97+
{
98+
"hex": "ecac89cad93923c02321",
99+
"string": "EJDM8drfXA6uyA"
100+
},
101+
{
102+
"hex": "10c8511e",
103+
"string": "Rt5zm"
104+
},
105+
{
106+
"hex": "00000000000000000000",
107+
"string": "1111111111"
108+
},
109+
{
110+
"hex": "801184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd206ec97e",
111+
"string": "5Hx15HFGyep2CfPxsJKe2fXJsCVn5DEiyoeGGF6JZjGbTRnqfiD"
112+
},
113+
{
114+
"hex": "003c176e659bea0f29a3e9bf7880c112b1b31b4dc826268187",
115+
"string": "16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS"
116+
}
117+
],
118+
"invalid": [
119+
{
120+
"description": "non-base58 string",
121+
"string": "invalid"
122+
},
123+
{
124+
"description": "non-base58 alphabet",
125+
"string": "c2F0b3NoaQo="
126+
},
127+
{
128+
"description": "leading whitespace",
129+
"string": " 1111111111"
130+
},
131+
{
132+
"description": "trailing whitespace",
133+
"string": "1111111111 "
134+
},
135+
{
136+
"description": "unexpected character after whitespace",
137+
"string": " \t\n\u000b\f\r skip \r\f\u000b\n\t a"
138+
}
139+
]
140+
141+
}

0 commit comments

Comments
 (0)