Skip to content

Commit

Permalink
More buffer serialization fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed Nov 26, 2024
1 parent f188e36 commit 0be8a90
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 14 additions & 4 deletions yarn-project/circuit-types/src/logs/l1_payload/payload.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { Fr } from '@aztec/foundation/fields';
import { jsonParseWithSchema, jsonStringify } from '@aztec/foundation/json-rpc';

import times from 'lodash.times';

import { Event, Note } from './payload.js';

describe('note', () => {
let note: Note;

beforeEach(() => {
note = new Note(times(5, Fr.random));
});

it('convert to and from buffer', () => {
const fields = Array.from({ length: 5 }).map(() => Fr.random());
const note = new Note(fields);
const buf = note.toBuffer();
expect(Note.fromBuffer(buf)).toEqual(note);
expect(Note.fromBuffer(note.toBuffer())).toEqual(note);
});

it('converts to and from json', () => {
expect(jsonParseWithSchema(jsonStringify(note), Note.schema)).toEqual(note);
});
});

Expand Down
2 changes: 2 additions & 0 deletions yarn-project/foundation/src/json-rpc/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export function jsonStringify(obj: object, prettify?: boolean): string {
return value.toString();
} else if (typeof value === 'object' && value && value.type === 'Buffer' && Array.isArray(value.data)) {
return Buffer.from(value.data).toString('base64');
} else if (typeof value === 'object' && value && Buffer.isBuffer(value)) {
return value.toString('base64');
} else if (typeof value === 'object' && value instanceof Map) {
return Array.from(value.entries());
} else if (typeof value === 'object' && value instanceof Set) {
Expand Down

0 comments on commit 0be8a90

Please sign in to comment.