Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/ans/src/bytes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it } from 'vitest';
import { fromHex, toHex } from './bytes.js';

describe('fromHex', () => {
it('decodes valid hex, with and without 0x prefix', () => {
expect([...fromHex('00ff10')]).toEqual([0x00, 0xff, 0x10]);
expect([...fromHex('0xdeadbeef')]).toEqual([0xde, 0xad, 0xbe, 0xef]);
expect([...fromHex('')]).toEqual([]);
});

it('round-trips with toHex', () => {
const bytes = Uint8Array.from([0, 1, 127, 128, 255]);
expect([...fromHex(toHex(bytes))]).toEqual([...bytes]);
});

it('throws on odd-length input', () => {
expect(() => fromHex('abc')).toThrow(/odd-length/);
});

it('throws on non-hex characters instead of silently decoding to 0', () => {
// Previously parseInt('zz', 16) === NaN, which a Uint8Array stores as 0,
// so fromHex('zzzz') silently returned [0, 0].
expect(() => fromHex('zzzz')).toThrow(/invalid hex/);
expect(() => fromHex('00gg')).toThrow(/invalid hex/);
expect(() => fromHex('0xnothex')).toThrow(/invalid hex/);
});
});
3 changes: 3 additions & 0 deletions packages/ans/src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export function toHex(bytes: Uint8Array): string {
export function fromHex(hex: string): Uint8Array {
const clean = hex.startsWith('0x') ? hex.slice(2) : hex;
if (clean.length % 2 !== 0) throw new Error('odd-length hex string');
// Reject non-hex input. Without this, parseInt returns NaN for a bad pair
// and the Uint8Array silently stores 0, producing wrong bytes.
if (!/^[0-9a-fA-F]*$/.test(clean)) throw new Error('invalid hex string');
const out = new Uint8Array(clean.length / 2);
for (let i = 0; i < out.length; i++) out[i] = parseInt(clean.slice(i * 2, i * 2 + 2), 16);
return out;
Expand Down
Loading