Skip to content

Commit 4630c36

Browse files
committed
feat: Uint8Array<SharedArrayBuffer> is accepted in toBase58
1 parent d4b7b67 commit 4630c36

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

base58.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
2323
* @param arr - The input bytes
2424
* @returns The base58 encoded string
2525
*/
26-
export function toBase58(arr: Uint8ArrayBuffer): string;
26+
export function toBase58(arr: Uint8Array): string;
2727

2828
/**
2929
* Decode a base58 string to bytes
@@ -46,7 +46,7 @@ export function fromBase58(string: string, format?: OutputFormat): Uint8ArrayBuf
4646
* @param arr - The input bytes
4747
* @returns The base58 encoded string
4848
*/
49-
export function toBase58xrp(arr: Uint8ArrayBuffer): string;
49+
export function toBase58xrp(arr: Uint8Array): string;
5050

5151
/**
5252
* Decode a base58 string to bytes using XRP alphabet

tests/base58.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import { describe, test } from 'node:test'
66
import bs58 from 'bs58'
77
import xAddressCodecFixtures from './vendor/x-address-codec/fixtures/base58.cjs'
88

9+
const SharedArrayBuffer = globalThis.SharedArrayBuffer ?? ArrayBuffer
10+
const toShared = (u8) => {
11+
const res = new Uint8Array(new SharedArrayBuffer(u8.length))
12+
res.set(u8)
13+
return res
14+
}
15+
916
// https://en.bitcoin.it/wiki/Base58Check_encoding#Creating_a_Base58Check_string
1017
const toChecked = (version, pub) => {
1118
const data = [Uint8Array.of(version), pub]
@@ -23,6 +30,7 @@ describe('static vectors', () => {
2330
for (const [string, u8] of fixtures) {
2431
t.assert.deepStrictEqual(fromBase58(string), u8, string)
2532
t.assert.strictEqual(toBase58(u8), string)
33+
t.assert.strictEqual(toBase58(toShared(u8)), string)
2634
}
2735
})
2836

@@ -41,6 +49,7 @@ describe('static vectors', () => {
4149
for (const [string, u8] of fixtures) {
4250
t.assert.deepStrictEqual(fromBase58xrp(string), u8, string)
4351
t.assert.strictEqual(toBase58xrp(u8), string)
52+
t.assert.strictEqual(toBase58xrp(toShared(u8)), string)
4453
}
4554
})
4655
})
@@ -50,6 +59,7 @@ describe('x-address-codec fixtures', () => {
5059
for (const { hex, string } of xAddressCodecFixtures.bitcoin) {
5160
const u8 = fromHex(hex)
5261
t.assert.strictEqual(toBase58(u8), string)
62+
t.assert.strictEqual(toBase58(toShared(u8)), string)
5363
t.assert.deepStrictEqual(fromBase58(string), u8, string)
5464
}
5565
})
@@ -58,6 +68,7 @@ describe('x-address-codec fixtures', () => {
5868
for (const { hex, string } of xAddressCodecFixtures.ripple) {
5969
const u8 = fromHex(hex)
6070
t.assert.strictEqual(toBase58xrp(u8), string)
71+
t.assert.strictEqual(toBase58xrp(toShared(u8)), string)
6172
t.assert.deepStrictEqual(fromBase58xrp(string), u8, string)
6273
}
6374
})
@@ -75,6 +86,7 @@ test('zeros', (t) => {
7586
const zeros = new Uint8Array(size)
7687
const expected = '1'.repeat(size)
7788
t.assert.strictEqual(toBase58(zeros), expected, `[0] x${size} toBase58`)
89+
t.assert.strictEqual(toBase58(toShared(zeros)), expected, `[0] x${size} toBase58`)
7890
t.assert.strictEqual(bs58.encode(zeros), expected, `[0] x${size} bs58.encode`) // matches bs58
7991
t.assert.deepStrictEqual(fromBase58(expected), zeros, `[0] x${size} fromBase58`)
8092
}

0 commit comments

Comments
 (0)