Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Nov 19, 2024
1 parent 1dae760 commit 9ce678a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
11 changes: 11 additions & 0 deletions yarn-project/circuits.js/src/contract/artifact_hash.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type ContractArtifact } from '@aztec/foundation/abi';

import { getTestContractArtifact } from '../tests/fixtures.js';
import { computeArtifactHash } from './artifact_hash.js';

describe('ArtifactHash', () => {
Expand All @@ -19,4 +20,14 @@ describe('ArtifactHash', () => {
`"0x0c6fd9b48570721c5d36f978d084d77cacbfd2814f1344985f40e62bea6e61be"`,
);
});

it('calculates the test contract artifact hash multiple times to ensure deterministic hashing', () => {
const testArtifact = getTestContractArtifact();

for (let i = 0; i < 1000; i++) {
expect(computeArtifactHash(testArtifact).toString()).toMatchInlineSnapshot(
`"0x11ba97d2d4de6335cc86d271d3c4a6237840cf630eaa442cf75d1666ff475f61"`,
);
}
});
});
2 changes: 1 addition & 1 deletion yarn-project/foundation/src/abi/note_selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class NoteSelector extends Selector {
}

static fromString(buf: string) {
const withoutPrefix = buf.replace(/^0x/i, '');
const withoutPrefix = buf.replace(/^0x/i, '').slice(-8);
const buffer = Buffer.from(withoutPrefix, 'hex');
return NoteSelector.fromBuffer(buffer);
}
Expand Down
35 changes: 13 additions & 22 deletions yarn-project/types/src/abi/contract_artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import {
type AbiType,
type BasicValue,
type ContractArtifact,
ContractArtifactSchema,
type ContractNote,
type FieldLayout,
type FunctionArtifact,
FunctionType,
type IntegerValue,
NoteSelector,
type StructValue,
type TypedStructFieldValue,
} from '@aztec/foundation/abi';
import { Fr } from '@aztec/foundation/fields';

import {
AZTEC_INITIALIZER_ATTRIBUTE,
Expand Down Expand Up @@ -53,18 +52,7 @@ export function contractArtifactToBuffer(artifact: ContractArtifact): Buffer {
* @returns Deserialized artifact.
*/
export function contractArtifactFromBuffer(buffer: Buffer): ContractArtifact {
return JSON.parse(buffer.toString('utf-8'), (key, value) => {
if (key === 'bytecode' && typeof value === 'string') {
return Buffer.from(value, 'base64');
}
if (typeof value === 'object' && value !== null && value.type === 'NoteSelector') {
return new NoteSelector(Number(value.value));
}
if (typeof value === 'object' && value !== null && value.type === 'Fr') {
return new Fr(BigInt(value.value));
}
return value;
});
return ContractArtifactSchema.parse(JSON.parse(buffer.toString('utf-8')));
}

/**
Expand Down Expand Up @@ -133,7 +121,10 @@ type NoirCompiledContractFunction = NoirCompiledContract['functions'][number];
* @param contract - Parent contract.
* @returns Function artifact.
*/
function generateFunctionArtifact(fn: NoirCompiledContractFunction, contract: NoirCompiledContract): FunctionArtifact {
function generateFunctionArtifact(
fn: NoirCompiledContractFunction,
contract: NoirCompiledContract,
): Omit<FunctionArtifact, 'bytecode'> & { bytecode: string } {
if (fn.custom_attributes === undefined) {
throw new Error(
`No custom attributes found for contract function ${fn.name}. Try rebuilding the contract with the latest nargo version.`,
Expand Down Expand Up @@ -178,7 +169,7 @@ function generateFunctionArtifact(fn: NoirCompiledContractFunction, contract: No
isInitializer: fn.custom_attributes.includes(AZTEC_INITIALIZER_ATTRIBUTE),
parameters,
returnTypes,
bytecode: Buffer.from(fn.bytecode, 'base64'),
bytecode: fn.bytecode,
debugSymbols: fn.debug_symbols,
errorTypes: fn.abi.error_types,
...(fn.assert_messages ? { assertMessages: fn.assert_messages } : undefined),
Expand Down Expand Up @@ -238,11 +229,11 @@ function getStorageLayout(input: NoirCompiledContract) {
return {};
}

return storageFields.reduce((acc: Record<string, FieldLayout>, field) => {
return storageFields.reduce((acc: Record<string, Omit<FieldLayout, 'slot'> & { slot: string }>, field) => {
const name = field.name;
const slot = field.value.fields[0].value as IntegerValue;
acc[name] = {
slot: Fr.fromString(slot.value),
slot: slot.value,
};
return acc;
}, {});
Expand All @@ -262,7 +253,7 @@ function getNoteTypes(input: NoirCompiledContract) {
return {};
}

return notes.reduce((acc: Record<string, ContractNote>, note) => {
return notes.reduce((acc: Record<string, Omit<ContractNote, 'id'> & { id: string }>, note) => {
const noteFields = note.fields;

// We find note type id by looking for respective kinds as each of them is unique
Expand All @@ -274,7 +265,7 @@ function getNoteTypes(input: NoirCompiledContract) {
throw new Error(`Could not find note type id, name or fields for note ${note}`);
}

const noteTypeId = NoteSelector.fromField(Fr.fromString(rawNoteTypeId.value));
const noteTypeId = rawNoteTypeId.value as string;
const name = rawName.value as string;

// Note type id is encoded as a hex string
Expand All @@ -301,15 +292,15 @@ function getNoteTypes(input: NoirCompiledContract) {
*/
function generateContractArtifact(contract: NoirCompiledContract, aztecNrVersion?: string): ContractArtifact {
try {
return {
return ContractArtifactSchema.parse({
name: contract.name,
functions: contract.functions.map(f => generateFunctionArtifact(f, contract)),
outputs: contract.outputs,
storageLayout: getStorageLayout(contract),
notes: getNoteTypes(contract),
fileMap: contract.file_map,
...(aztecNrVersion ? { aztecNrVersion } : {}),
};
});
} catch (err) {
throw new Error(`Could not generate contract artifact for ${contract.name}: ${err}`);
}
Expand Down

0 comments on commit 9ce678a

Please sign in to comment.