Skip to content

Commit 3ebd4de

Browse files
committed
feat: Uint8Array<SharedArrayBuffer> is accepted in toBech32
1 parent aace11c commit 3ebd4de

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

bech32.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface Bech32DecodeResult {
3434
* @param limit - Maximum length of the encoded string (default: 90)
3535
* @returns The bech32 encoded string
3636
*/
37-
export function toBech32(prefix: string, bytes: Uint8ArrayBuffer, limit?: number): string;
37+
export function toBech32(prefix: string, bytes: Uint8Array, limit?: number): string;
3838

3939
/**
4040
* Decode a bech32 string to bytes
@@ -53,7 +53,7 @@ export function fromBech32(string: string, limit?: number): Bech32DecodeResult;
5353
* @param limit - Maximum length of the encoded string (default: 90)
5454
* @returns The bech32m encoded string
5555
*/
56-
export function toBech32m(prefix: string, bytes: Uint8ArrayBuffer, limit?: number): string;
56+
export function toBech32m(prefix: string, bytes: Uint8Array, limit?: number): string;
5757

5858
/**
5959
* Decode a bech32m string to bytes

tests/bech32.test.js

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

6+
const SharedArrayBuffer = globalThis.SharedArrayBuffer ?? ArrayBuffer
7+
const toShared = (u8) => {
8+
const res = new Uint8Array(new SharedArrayBuffer(u8.length))
9+
res.set(u8)
10+
return res
11+
}
12+
613
const alt = {
714
toBech32: (prefix, bytes) => bech32.bech32.encode(prefix, bech32.bech32.toWords(bytes)),
815
toBech32m: (prefix, bytes) => bech32.bech32m.encode(prefix, bech32.bech32m.toWords(bytes)),
@@ -21,40 +28,48 @@ const maxBytes = 46
2128
const round = (p, x) => fromBech32(toBech32(p, x))
2229
const roundM = (p, x) => fromBech32m(toBech32m(p, x))
2330

24-
test('fromBech32 matches bech32, static data', (t) => {
31+
test('toBech32 matches bech32, static data', (t) => {
32+
const r = (p, u8, desc) => {
33+
const s8 = toShared(u8)
34+
t.assert.strictEqual(toBech32(p, u8), alt.toBech32(p, u8), desc)
35+
t.assert.strictEqual(toBech32m(p, u8), alt.toBech32m(p, u8), desc)
36+
t.assert.strictEqual(toBech32(p, s8), alt.toBech32(p, u8), desc)
37+
t.assert.strictEqual(toBech32m(p, s8), alt.toBech32m(p, u8), desc)
38+
}
39+
2540
for (let size = 0; size <= maxBytes; size++) {
2641
const zeros = new Uint8Array(size)
2742
const ones = new Uint8Array(size).fill(1)
2843
const mid = new Uint8Array(size).fill(42)
2944
const max = new Uint8Array(size).fill(255)
3045
for (const p of ['bc', 'whatever']) {
31-
t.assert.strictEqual(toBech32(p, zeros), alt.toBech32(p, zeros), `[0] x${size}, ${p}`)
32-
t.assert.strictEqual(toBech32m(p, zeros), alt.toBech32m(p, zeros), `[0] x${size}, ${p}`)
33-
t.assert.strictEqual(toBech32(p, ones), alt.toBech32(p, ones), `[1] x${size}, ${p}`)
34-
t.assert.strictEqual(toBech32m(p, ones), alt.toBech32m(p, ones), `[1] x${size}, ${p}`)
35-
t.assert.strictEqual(toBech32(p, mid), alt.toBech32(p, mid), `[42] x${size}, ${p}`)
36-
t.assert.strictEqual(toBech32m(p, mid), alt.toBech32m(p, mid), `[42] x${size}, ${p}`)
37-
t.assert.strictEqual(toBech32(p, max), alt.toBech32(p, max), `[255] x${size}, ${p}`)
38-
t.assert.strictEqual(toBech32m(p, max), alt.toBech32m(p, max), `[255] x${size}, ${p}`)
46+
r(p, zeros, `[0] x${size}, ${p}`)
47+
r(p, ones, `[1] x${size}, ${p}`)
48+
r(p, mid, `[42] x${size}, ${p}`)
49+
r(p, max, `[255] x${size}, ${p}`)
3950
}
4051
}
4152
})
4253

4354
test('sizes roundtrip, static data', (t) => {
55+
const r = (p, u8, desc) => {
56+
const s8 = toShared(u8)
57+
t.assert.deepStrictEqual(round(p, u8), { prefix: p, bytes: u8 }, desc)
58+
t.assert.deepStrictEqual(roundM(p, u8), { prefix: p, bytes: u8 }, desc)
59+
t.assert.deepStrictEqual(round(p, s8), { prefix: p, bytes: u8 }, desc)
60+
t.assert.deepStrictEqual(roundM(p, s8), { prefix: p, bytes: u8 }, desc)
61+
}
62+
4463
for (let size = 0; size < 47; size++) {
4564
const zeros = new Uint8Array(size)
4665
const ones = new Uint8Array(size).fill(1)
4766
const mid = new Uint8Array(size).fill(42)
4867
const max = new Uint8Array(size).fill(255)
4968
for (const p of ['bc', 'whatever']) {
50-
t.assert.deepStrictEqual(round(p, zeros), { prefix: p, bytes: zeros }, `[0] x${size}, ${p}`)
51-
t.assert.deepStrictEqual(roundM(p, zeros), { prefix: p, bytes: zeros }, `[0] x${size}, ${p}`)
52-
t.assert.deepStrictEqual(round(p, ones), { prefix: p, bytes: ones }, `[1] x${size}, ${p}`)
53-
t.assert.deepStrictEqual(roundM(p, ones), { prefix: p, bytes: ones }, `[1] x${size}, ${p}`)
54-
t.assert.deepStrictEqual(round(p, mid), { prefix: p, bytes: mid }, `[42] x${size}, ${p}`)
55-
t.assert.deepStrictEqual(roundM(p, mid), { prefix: p, bytes: mid }, `[42] x${size}, ${p}`)
56-
t.assert.deepStrictEqual(round(p, max), { prefix: p, bytes: max }, `[255] x${size}, ${p}`)
57-
t.assert.deepStrictEqual(roundM(p, max), { prefix: p, bytes: max }, `[255] x${size}, ${p}`)
69+
r(p, zeros, `[0] x${size}, ${p}`)
70+
r(p, ones, `[1] x${size}, ${p}`)
71+
r(p, mid, `[42] x${size}, ${p}`)
72+
r(p, max, `[255] x${size}, ${p}`)
5873
}
5974
}
6075
})
@@ -66,8 +81,11 @@ test('toBech32 matches bech32, random data', (t) => {
6681
for (const p of ['bc', 'whatever']) {
6782
for (let start = 0, i = 0; start < seed.length - size && i < 100; start++, i++) {
6883
const bytes = seed.subarray(start, start + size)
84+
const shared = toShared(bytes)
6985
t.assert.strictEqual(toBech32(p, bytes), alt.toBech32(p, bytes), `random x${size}, ${p}`)
7086
t.assert.strictEqual(toBech32m(p, bytes), alt.toBech32m(p, bytes), `random x${size}, ${p}`)
87+
t.assert.strictEqual(toBech32(p, shared), alt.toBech32(p, bytes), `random x${size}, ${p}`)
88+
t.assert.strictEqual(toBech32m(p, shared), alt.toBech32m(p, bytes), `random x${size}, ${p}`)
7189
}
7290
}
7391
}
@@ -81,8 +99,11 @@ test('sizes roundtrip, random data', (t) => {
8199
for (const p of ['bc', 'whatever']) {
82100
for (let start = 0, i = 0; start < seed.length - size && i < 100; start++, i++) {
83101
const bytes = seed.subarray(start, start + size)
102+
const shared = toShared(bytes)
84103
t.assert.deepStrictEqual(round(p, bytes), { prefix: p, bytes }, `random x${size}`)
85104
t.assert.deepStrictEqual(roundM(p, bytes), { prefix: p, bytes }, `random x${size}`)
105+
t.assert.deepStrictEqual(round(p, shared), { prefix: p, bytes }, `random x${size}`)
106+
t.assert.deepStrictEqual(roundM(p, shared), { prefix: p, bytes }, `random x${size}`)
86107
}
87108
}
88109
}

0 commit comments

Comments
 (0)