Skip to content

Commit 2961d98

Browse files
committed
feat: Uint8Array<SharedArrayBuffer> is accepted in toBase58check
1 parent 4630c36 commit 2961d98

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

base58check.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface Base58CheckAsync {
3636
* @param arr - The input bytes to encode
3737
* @returns A Promise that resolves to the base58check encoded string
3838
*/
39-
encode(arr: Uint8ArrayBuffer): Promise<string>;
39+
encode(arr: Uint8Array): Promise<string>;
4040

4141
/**
4242
* Decode a base58check string to bytes asynchronously
@@ -60,7 +60,7 @@ export interface Base58CheckSync extends Base58CheckAsync {
6060
* @param arr - The input bytes to encode
6161
* @returns The base58check encoded string
6262
*/
63-
encodeSync(arr: Uint8ArrayBuffer): string;
63+
encodeSync(arr: Uint8Array): string;
6464

6565
/**
6666
* Decode a base58check string to bytes synchronously
@@ -92,7 +92,7 @@ export function makeBase58check(hashAlgo: HashFunction): Base58CheckAsync;
9292
* @param arr - The input bytes to encode
9393
* @returns A Promise that resolves to the base58check encoded string
9494
*/
95-
export function toBase58check(arr: Uint8ArrayBuffer): Promise<string>;
95+
export function toBase58check(arr: Uint8Array): Promise<string>;
9696

9797
/**
9898
* Decode a base58check string to bytes asynchronously
@@ -115,7 +115,7 @@ export function fromBase58check(string: string, format?: OutputFormat): Promise<
115115
* @param arr - The input bytes to encode
116116
* @returns The base58check encoded string
117117
*/
118-
export function toBase58checkSync(arr: Uint8ArrayBuffer): string;
118+
export function toBase58checkSync(arr: Uint8Array): string;
119119

120120
/**
121121
* Decode a base58check string to bytes synchronously

base58check.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { makeBase58check } from './fallback/base58check.js'
44
// Note: while API is async, we use hashSync for now until we improve webcrypto perf for hash256
55
// Inputs to base58 are typically very small, and that makes a difference
66

7-
// eslint-disable-next-line @exodus/import/no-deprecated
7+
// Note: using native WebCrypto will have to have account for SharedArrayBuffer
8+
89
const hash256sync = (x) => sha256(sha256(x))
910
const hash256 = hash256sync // See note at the top
1011
const {

tests/vendor/bs58check/base.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import { makeBase58check } from '@exodus/bytes/base58check.js'
55
import { hash, hashSync } from '@exodus/crypto/hash' // eslint-disable-line @exodus/import/no-deprecated
66
import { test } from 'node:test'
77

8+
const SharedArrayBuffer = globalThis.SharedArrayBuffer ?? ArrayBuffer
9+
const toShared = (u8) => {
10+
const res = new Uint8Array(new SharedArrayBuffer(u8.length))
11+
res.set(u8)
12+
return res
13+
}
14+
815
// eslint-disable-next-line @exodus/import/no-deprecated
916
const blake256x2sync = (x) => hashSync('blake256', hashSync('blake256', x, 'uint8'), 'uint8')
1017
const blake256x2 = async (x) => hash('blake256', await hash('blake256', x, 'uint8'), 'uint8')
@@ -15,7 +22,9 @@ test('custom checksum function (blake256x2)', async (t) => {
1522
const payload = hexToBytes('073f0415e993935a68154fda7018b887c4e3fe8b4e10')
1623

1724
t.assert.strictEqual(await bs58check.encode(payload), address)
25+
t.assert.strictEqual(await bs58check.encode(toShared(payload)), address)
1826
t.assert.deepStrictEqual(await bs58check.decode(address), payload)
1927
t.assert.strictEqual(bs58check.encodeSync(payload), address)
28+
t.assert.strictEqual(bs58check.encodeSync(toShared(payload)), address)
2029
t.assert.deepStrictEqual(bs58check.decodeSync(address), payload)
2130
})

tests/vendor/bs58check/bs58check.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import { toHex as bytesToHex, fromHex as hexToBytes } from '@exodus/bytes/hex.js
66
import { test } from 'node:test'
77
import fixtures from './fixtures.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
const libs = [auto]
1017
if (js.toBase58check !== auto.toBase58check) libs.push(js)
1118

@@ -36,8 +43,10 @@ for (const f of fixtures.valid) {
3643
for (const lib of libs) {
3744
t.assert.strictEqual(await lib.toBase58check(u8), f.string)
3845
t.assert.strictEqual(await lib.toBase58check(buffer), f.string)
46+
t.assert.strictEqual(await lib.toBase58check(toShared(u8)), f.string)
3947
t.assert.strictEqual(lib.toBase58checkSync(u8), f.string)
4048
t.assert.strictEqual(lib.toBase58checkSync(buffer), f.string)
49+
t.assert.strictEqual(lib.toBase58checkSync(toShared(u8)), f.string)
4150
}
4251
})
4352
}

0 commit comments

Comments
 (0)