Skip to content

Commit 4625734

Browse files
committed
fix(foundation): Fix Zod serde for Buffer32
## Summary - `Buffer32.fromString` and `fromBuffer28` referenced `this.SIZE`, which broke when the static method was called with a different `this` (e.g. passed unbound to `z.transform` in `schemas.Buffer32`, where `this.SIZE` became `undefined` and the length check threw `"Expected string to be NaN characters long, but was 64"`). - Replace remaining `this.SIZE` uses with `Buffer32.SIZE` so the static methods don't depend on call-site binding. The rest of the class already used `Buffer32.SIZE` — this just removes the mix. - Add a regression test that round-trips a random `Buffer32` through `schemas.Buffer32`.
1 parent c1b4f2e commit 4625734

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

yarn-project/foundation/src/buffer/buffer32.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class Buffer32 extends BaseBuffer32 {
9090
if (buffer.length != 28) {
9191
throw new Error(`Expected Buffer32 input buffer to be 28 bytes`);
9292
}
93-
const padded = Buffer.concat([Buffer.alloc(this.SIZE - 28), buffer]);
93+
const padded = Buffer.concat([Buffer.alloc(Buffer32.SIZE - 28), buffer]);
9494
return new Buffer32(padded);
9595
}
9696

@@ -99,8 +99,8 @@ export class Buffer32 extends BaseBuffer32 {
9999
if (str.startsWith('0x')) {
100100
str = str.slice(2);
101101
}
102-
if (str.length !== this.SIZE * 2) {
103-
throw new Error(`Expected string to be ${this.SIZE * 2} characters long, but was ${str.length}`);
102+
if (str.length !== Buffer32.SIZE * 2) {
103+
throw new Error(`Expected string to be ${Buffer32.SIZE * 2} characters long, but was ${str.length}`);
104104
}
105105
return new Buffer32(Buffer.from(str, 'hex'));
106106
}

yarn-project/foundation/src/schemas/schemas.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
import { Buffer32 } from '../buffer/buffer32.js';
12
import { schemas } from './schemas.js';
23

34
describe('schemas', () => {
5+
describe('Buffer32', () => {
6+
it('parses a valid hex string into a Buffer32', () => {
7+
const buffer32 = Buffer32.random();
8+
const parsed = schemas.Buffer32.parse(buffer32.toString());
9+
expect(parsed).toBeInstanceOf(Buffer32);
10+
expect(parsed.equals(buffer32)).toBe(true);
11+
});
12+
});
13+
414
describe('Boolean', () => {
515
it('accepts a boolean value', () => {
616
expect(schemas.Boolean.parse(true)).toEqual(true);

0 commit comments

Comments
 (0)