From 41f9cd1aff57aa087ced9f45ff72ad2a7d0c3ddd Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 2 Oct 2024 07:25:49 +0000 Subject: [PATCH] cleanup --- .../types/src/abi/contract_artifact.ts | 47 ++++++++----------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/yarn-project/types/src/abi/contract_artifact.ts b/yarn-project/types/src/abi/contract_artifact.ts index 506beddd8a8d..a474920eddf3 100644 --- a/yarn-project/types/src/abi/contract_artifact.ts +++ b/yarn-project/types/src/abi/contract_artifact.ts @@ -254,38 +254,31 @@ function getStorageLayout(input: NoirCompiledContract) { * @return A record of the note types and their ids */ function getNoteTypes(input: NoirCompiledContract) { - type t = { - kind: string; - fields: [ - { kind: string; sign: boolean; value: string }, - { kind: string; value: string }, - { - fields: { - name: string; - value: { - kind: string; - value: string | boolean; - fields: [ - { name: string; value: { kind: string; sign: boolean; value: string } }, - { name: string; value: { kind: string; value: boolean } }, - ]; - }; - }[]; - }, - ]; - }; - - const notes = input.outputs.globals.notes as t[]; + // The type is useless here as it does not give us any guarantee (e.g. `AbiValue` can be one of many different + // types) so we nuke it and later we manually check the values are as we expect. + const notes = input.outputs.globals.notes as any[]; if (!notes) { return {}; } return notes.reduce((acc: Record, note) => { - const name = note.fields[1].value as string; - // Note id is encoded as a hex string - const id = NoteSelector.fromField(Fr.fromString(note.fields[0].value)); - const fields = note.fields[2].fields.map(field => { + const noteFields = note.fields; + + // We find note type id by looking for respective kinds as each of them is unique + const rawNoteTypeId = noteFields.find((field: any) => field.kind === 'integer'); + const rawName = noteFields.find((field: any) => field.kind === 'string'); + const rawNoteFields = noteFields.find((field: any) => field.kind === 'struct'); + + if (!rawNoteTypeId || !rawName || !rawNoteFields) { + throw new Error(`Could not find note type id, name or fields for note ${note}`); + } + + const noteTypeId = NoteSelector.fromField(Fr.fromString(rawNoteTypeId.value)); + const name = rawName.value as string; + + // Note type id is encoded as a hex string + const fields = rawNoteFields.fields.map((field: any) => { return { name: field.name, index: parseInt(field.value.fields[0].value.value, 16), @@ -293,7 +286,7 @@ function getNoteTypes(input: NoirCompiledContract) { }; }); acc[name] = { - id, + id: noteTypeId, typ: name, fields, };