@aztec/builder 0.0.0-test.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # Aztec builder
2
+
3
+ The Aztec builder generates typescript classes for Noir contract, as well as Aztec.nr interfaces for calling external functions.
4
+ It can also be used to update aztec project dependencies.
5
+
6
+ ## Installation
7
+
8
+ To install the package, run:
9
+
10
+ ```bash
11
+ yarn add @aztec/builder
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ To run the tool, first install the package and then run:
17
+
18
+ ```bash
19
+ yarn aztec-builder --help
20
+ ```
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../../src/bin/cli.ts"],"names":[],"mappings":""}
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env node
2
+ import { createConsoleLogger } from '@aztec/foundation/log';
3
+ import { Command } from 'commander';
4
+ import { injectCommands as injectBuilderCommands } from '../index.js';
5
+ const log = createConsoleLogger('aztec:builder');
6
+ const main = async ()=>{
7
+ const program = new Command('aztec-builder');
8
+ injectBuilderCommands(program);
9
+ await program.parseAsync(process.argv);
10
+ // I force exit here because spawnSync in npm.ts just blocks the process from exiting. Spent a bit of time debugging
11
+ // it without success and I think it doesn't make sense to invest more time in this.
12
+ process.exit(0);
13
+ };
14
+ main().catch((err)=>{
15
+ log(`Error running command`);
16
+ log(err);
17
+ process.exit(1);
18
+ });
@@ -0,0 +1,9 @@
1
+ /** Generate code options */
2
+ export type GenerateCodeOptions = {
3
+ force?: boolean;
4
+ };
5
+ /**
6
+ * Generates Noir interface or Typescript interface for a folder or single file from a Noir compilation artifact.
7
+ */
8
+ export declare function generateCode(outputPath: string, fileOrDirPath: string, opts?: GenerateCodeOptions): Promise<string[]>;
9
+ //# sourceMappingURL=codegen.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codegen.d.ts","sourceRoot":"","sources":["../../src/contract-interface-gen/codegen.ts"],"names":[],"mappings":"AAYA,4BAA4B;AAC5B,MAAM,MAAM,mBAAmB,GAAG;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAEtD;;GAEG;AACH,wBAAsB,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,IAAI,GAAE,mBAAwB,qBAkB3G"}
@@ -0,0 +1,88 @@
1
+ /* eslint-disable no-console */ import { loadContractArtifact } from '@aztec/stdlib/abi';
2
+ import crypto from 'crypto';
3
+ import { access, mkdir, readFile, readdir, stat, writeFile } from 'fs/promises';
4
+ import path from 'path';
5
+ import { generateTypescriptContractInterface } from './typescript.js';
6
+ const cacheFilePath = './codegenCache.json';
7
+ let cache = {};
8
+ /**
9
+ * Generates Noir interface or Typescript interface for a folder or single file from a Noir compilation artifact.
10
+ */ export async function generateCode(outputPath, fileOrDirPath, opts = {}) {
11
+ await readCache();
12
+ const results = [];
13
+ const stats = await stat(fileOrDirPath);
14
+ if (stats.isDirectory()) {
15
+ const files = (await readdir(fileOrDirPath, {
16
+ recursive: true,
17
+ encoding: 'utf-8'
18
+ })).filter((file)=>file.endsWith('.json') && !file.startsWith('debug_'));
19
+ for (const file of files){
20
+ const fullPath = path.join(fileOrDirPath, file);
21
+ results.push(await generateFromNoirAbi(outputPath, fullPath, opts));
22
+ }
23
+ } else if (stats.isFile()) {
24
+ results.push(await generateFromNoirAbi(outputPath, fileOrDirPath, opts));
25
+ }
26
+ await writeCache();
27
+ return results;
28
+ }
29
+ /**
30
+ * Generates Noir interface or Typescript interface for a single file Noir compilation artifact.
31
+ */ async function generateFromNoirAbi(outputPath, noirAbiPath, opts = {}) {
32
+ const fileName = path.basename(noirAbiPath);
33
+ const currentHash = await generateFileHash(noirAbiPath);
34
+ const cachedInstance = isCacheValid(fileName, currentHash);
35
+ if (cachedInstance && !opts.force) {
36
+ console.log(`${fileName} has not changed. Skipping generation.`);
37
+ return `${outputPath}/${cachedInstance.contractName}.ts`;
38
+ }
39
+ const file = await readFile(noirAbiPath, 'utf8');
40
+ const contract = JSON.parse(file);
41
+ const aztecAbi = loadContractArtifact(contract);
42
+ await mkdir(outputPath, {
43
+ recursive: true
44
+ });
45
+ let relativeArtifactPath = path.relative(outputPath, noirAbiPath);
46
+ if (relativeArtifactPath === path.basename(noirAbiPath)) {
47
+ // Prepend ./ for local import if the folder is the same
48
+ relativeArtifactPath = `./${relativeArtifactPath}`;
49
+ }
50
+ const tsWrapper = await generateTypescriptContractInterface(aztecAbi, relativeArtifactPath);
51
+ const outputFilePath = `${outputPath}/${aztecAbi.name}.ts`;
52
+ await writeFile(outputFilePath, tsWrapper);
53
+ updateCache(fileName, aztecAbi.name, currentHash);
54
+ return outputFilePath;
55
+ }
56
+ async function generateFileHash(filePath) {
57
+ const fileBuffer = await readFile(filePath);
58
+ const hashSum = crypto.createHash('sha256');
59
+ hashSum.update(fileBuffer);
60
+ const hex = hashSum.digest('hex');
61
+ return hex;
62
+ }
63
+ async function readCache() {
64
+ if (await exists(cacheFilePath)) {
65
+ const cacheRaw = await readFile(cacheFilePath, 'utf8');
66
+ cache = JSON.parse(cacheRaw);
67
+ }
68
+ }
69
+ async function writeCache() {
70
+ await writeFile(cacheFilePath, JSON.stringify(cache, null, 2), 'utf8');
71
+ }
72
+ function isCacheValid(contractName, currentHash) {
73
+ return cache[contractName]?.hash === currentHash && cache[contractName];
74
+ }
75
+ function updateCache(fileName, contractName, hash) {
76
+ cache[fileName] = {
77
+ contractName,
78
+ hash
79
+ };
80
+ }
81
+ async function exists(filePath) {
82
+ try {
83
+ await access(filePath);
84
+ return true;
85
+ } catch {
86
+ return false;
87
+ }
88
+ }
@@ -0,0 +1,9 @@
1
+ import { type ContractArtifact } from '@aztec/stdlib/abi';
2
+ /**
3
+ * Generates the typescript code to represent a contract.
4
+ * @param input - The compiled Noir artifact.
5
+ * @param artifactImportPath - Optional path to import the artifact (if not set, will be required in the constructor).
6
+ * @returns The corresponding ts code.
7
+ */
8
+ export declare function generateTypescriptContractInterface(input: ContractArtifact, artifactImportPath?: string): Promise<string>;
9
+ //# sourceMappingURL=typescript.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typescript.d.ts","sourceRoot":"","sources":["../../src/contract-interface-gen/typescript.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,gBAAgB,EAStB,MAAM,mBAAmB,CAAC;AAyR3B;;;;;GAKG;AACH,wBAAsB,mCAAmC,CAAC,KAAK,EAAE,gBAAgB,EAAE,kBAAkB,CAAC,EAAE,MAAM,mBA8E7G"}
@@ -0,0 +1,316 @@
1
+ import { EventSelector, decodeFunctionSignature, getDefaultInitializer, isAztecAddressStruct, isEthAddressStruct, isFunctionSelectorStruct, isWrappedFieldStruct } from '@aztec/stdlib/abi';
2
+ /**
3
+ * Returns the corresponding typescript type for a given Noir type.
4
+ * @param type - The input Noir type.
5
+ * @returns An equivalent typescript type.
6
+ */ function abiTypeToTypescript(type) {
7
+ switch(type.kind){
8
+ case 'field':
9
+ return 'FieldLike';
10
+ case 'boolean':
11
+ return 'boolean';
12
+ case 'integer':
13
+ return '(bigint | number)';
14
+ case 'string':
15
+ return 'string';
16
+ case 'array':
17
+ return `${abiTypeToTypescript(type.type)}[]`;
18
+ case 'struct':
19
+ if (isEthAddressStruct(type)) {
20
+ return 'EthAddressLike';
21
+ }
22
+ if (isAztecAddressStruct(type)) {
23
+ return 'AztecAddressLike';
24
+ }
25
+ if (isFunctionSelectorStruct(type)) {
26
+ return 'FunctionSelectorLike';
27
+ }
28
+ if (isWrappedFieldStruct(type)) {
29
+ return 'WrappedFieldLike';
30
+ }
31
+ return `{ ${type.fields.map((f)=>`${f.name}: ${abiTypeToTypescript(f.type)}`).join(', ')} }`;
32
+ default:
33
+ throw new Error(`Unknown type ${type}`);
34
+ }
35
+ }
36
+ /**
37
+ * Generates the typescript code to represent a Noir parameter.
38
+ * @param param - A Noir parameter with name and type.
39
+ * @returns The corresponding ts code.
40
+ */ function generateParameter(param) {
41
+ return `${param.name}: ${abiTypeToTypescript(param.type)}`;
42
+ }
43
+ /**
44
+ * Generates the typescript code to represent a Noir function as a type.
45
+ * @param param - A Noir function.
46
+ * @returns The corresponding ts code.
47
+ */ function generateMethod(entry) {
48
+ const args = entry.parameters.map(generateParameter).join(', ');
49
+ return `
50
+ /** ${entry.name}(${entry.parameters.map((p)=>`${p.name}: ${p.type.kind}`).join(', ')}) */
51
+ ${entry.name}: ((${args}) => ContractFunctionInteraction) & Pick<ContractMethod, 'selector'>;`;
52
+ }
53
+ /**
54
+ * Generates a deploy method for this contract.
55
+ * @param input - Build artifact of the contract.
56
+ * @returns A type-safe deploy method in ts.
57
+ */ function generateDeploy(input) {
58
+ const ctor = getDefaultInitializer(input);
59
+ const args = (ctor?.parameters ?? []).map(generateParameter).join(', ');
60
+ const contractName = `${input.name}Contract`;
61
+ const artifactName = `${contractName}Artifact`;
62
+ return `
63
+ /**
64
+ * Creates a tx to deploy a new instance of this contract.
65
+ */
66
+ public static deploy(wallet: Wallet, ${args}) {
67
+ return new DeployMethod<${contractName}>(PublicKeys.default(), wallet, ${artifactName}, ${contractName}.at, Array.from(arguments).slice(1));
68
+ }
69
+
70
+ /**
71
+ * Creates a tx to deploy a new instance of this contract using the specified public keys hash to derive the address.
72
+ */
73
+ public static deployWithPublicKeys(publicKeys: PublicKeys, wallet: Wallet, ${args}) {
74
+ return new DeployMethod<${contractName}>(publicKeys, wallet, ${artifactName}, ${contractName}.at, Array.from(arguments).slice(2));
75
+ }
76
+
77
+ /**
78
+ * Creates a tx to deploy a new instance of this contract using the specified constructor method.
79
+ */
80
+ public static deployWithOpts<M extends keyof ${contractName}['methods']>(
81
+ opts: { publicKeys?: PublicKeys; method?: M; wallet: Wallet },
82
+ ...args: Parameters<${contractName}['methods'][M]>
83
+ ) {
84
+ return new DeployMethod<${contractName}>(
85
+ opts.publicKeys ?? PublicKeys.default(),
86
+ opts.wallet,
87
+ ${artifactName},
88
+ ${contractName}.at,
89
+ Array.from(arguments).slice(1),
90
+ opts.method ?? 'constructor',
91
+ );
92
+ }
93
+ `;
94
+ }
95
+ /**
96
+ * Generates the constructor by supplying the ABI to the parent class so the user doesn't have to.
97
+ * @param name - Name of the contract to derive the ABI name from.
98
+ * @returns A constructor method.
99
+ * @remarks The constructor is private because we want to force the user to use the create method.
100
+ */ function generateConstructor(name) {
101
+ return `
102
+ private constructor(
103
+ instance: ContractInstanceWithAddress,
104
+ wallet: Wallet,
105
+ ) {
106
+ super(instance, ${name}ContractArtifact, wallet);
107
+ }
108
+ `;
109
+ }
110
+ /**
111
+ * Generates the at method for this contract.
112
+ * @param name - Name of the contract to derive the ABI name from.
113
+ * @returns An at method.
114
+ * @remarks We don't use constructor directly because of the async `wallet.getContractData` call.
115
+ */ function generateAt(name) {
116
+ return `
117
+ /**
118
+ * Creates a contract instance.
119
+ * @param address - The deployed contract's address.
120
+ * @param wallet - The wallet to use when interacting with the contract.
121
+ * @returns A promise that resolves to a new Contract instance.
122
+ */
123
+ public static async at(
124
+ address: AztecAddress,
125
+ wallet: Wallet,
126
+ ) {
127
+ return Contract.at(address, ${name}Contract.artifact, wallet) as Promise<${name}Contract>;
128
+ }`;
129
+ }
130
+ /**
131
+ * Generates a static getter for the contract's artifact.
132
+ * @param name - Name of the contract used to derive name of the artifact import.
133
+ */ function generateArtifactGetter(name) {
134
+ const artifactName = `${name}ContractArtifact`;
135
+ return `
136
+ /**
137
+ * Returns this contract's artifact.
138
+ */
139
+ public static get artifact(): ContractArtifact {
140
+ return ${artifactName};
141
+ }
142
+ `;
143
+ }
144
+ /**
145
+ * Generates statements for importing the artifact from json and re-exporting it.
146
+ * @param name - Name of the contract.
147
+ * @param artifactImportPath - Path to load the ABI from.
148
+ * @returns Code.
149
+ */ function generateAbiStatement(name, artifactImportPath) {
150
+ const stmts = [
151
+ `import ${name}ContractArtifactJson from '${artifactImportPath}' assert { type: 'json' };`,
152
+ `export const ${name}ContractArtifact = loadContractArtifact(${name}ContractArtifactJson as NoirCompiledContract);`
153
+ ];
154
+ return stmts.join('\n');
155
+ }
156
+ /**
157
+ * Generates a getter for the contract's storage layout.
158
+ * @param input - The contract artifact.
159
+ */ function generateStorageLayoutGetter(input) {
160
+ const entries = Object.entries(input.storageLayout);
161
+ if (entries.length === 0) {
162
+ return '';
163
+ }
164
+ const storageFieldsUnionType = entries.map(([name])=>`'${name}'`).join(' | ');
165
+ const layout = entries.map(([name, { slot }])=>`${name}: {
166
+ slot: new Fr(${slot.toBigInt()}n),
167
+ }`).join(',\n');
168
+ return `public static get storage(): ContractStorageLayout<${storageFieldsUnionType}> {
169
+ return {
170
+ ${layout}
171
+ } as ContractStorageLayout<${storageFieldsUnionType}>;
172
+ }
173
+ `;
174
+ }
175
+ /**
176
+ * Generates a getter for the contract notes
177
+ * @param input - The contract artifact.
178
+ */ function generateNotesGetter(input) {
179
+ const entries = Object.entries(input.notes);
180
+ if (entries.length === 0) {
181
+ return '';
182
+ }
183
+ const notesUnionType = entries.map(([name])=>`'${name}'`).join(' | ');
184
+ const noteMetadata = entries.map(([name, { id }])=>`${name}: {
185
+ id: new NoteSelector(${id.value}),
186
+ }`).join(',\n');
187
+ return `public static get notes(): ContractNotes<${notesUnionType}> {
188
+ return {
189
+ ${noteMetadata}
190
+ } as ContractNotes<${notesUnionType}>;
191
+ }
192
+ `;
193
+ }
194
+ // events is of type AbiType
195
+ async function generateEvents(events) {
196
+ if (events === undefined) {
197
+ return {
198
+ events: '',
199
+ eventDefs: ''
200
+ };
201
+ }
202
+ const eventsMetadata = await Promise.all(events.map(async (event)=>{
203
+ const eventName = event.path.split('::').at(-1);
204
+ const eventDefProps = event.fields.map((field)=>`${field.name}: ${abiTypeToTypescript(field.type)}`);
205
+ const eventDef = `
206
+ export type ${eventName} = {
207
+ ${eventDefProps.join('\n')}
208
+ }
209
+ `;
210
+ const fieldNames = event.fields.map((field)=>`"${field.name}"`);
211
+ const eventType = `${eventName}: {abiType: AbiType, eventSelector: EventSelector, fieldNames: string[] }`;
212
+ // Reusing the decodeFunctionSignature
213
+ const eventSignature = decodeFunctionSignature(eventName, event.fields);
214
+ const eventSelector = await EventSelector.fromSignature(eventSignature);
215
+ const eventImpl = `${eventName}: {
216
+ abiType: ${JSON.stringify(event, null, 4)},
217
+ eventSelector: EventSelector.fromString("${eventSelector}"),
218
+ fieldNames: [${fieldNames}],
219
+ }`;
220
+ return {
221
+ eventDef,
222
+ eventType,
223
+ eventImpl
224
+ };
225
+ }));
226
+ return {
227
+ eventDefs: eventsMetadata.map(({ eventDef })=>eventDef).join('\n'),
228
+ events: `
229
+ public static get events(): { ${eventsMetadata.map(({ eventType })=>eventType).join(', ')} } {
230
+ return {
231
+ ${eventsMetadata.map(({ eventImpl })=>eventImpl).join(',\n')}
232
+ };
233
+ }
234
+ `
235
+ };
236
+ }
237
+ /**
238
+ * Generates the typescript code to represent a contract.
239
+ * @param input - The compiled Noir artifact.
240
+ * @param artifactImportPath - Optional path to import the artifact (if not set, will be required in the constructor).
241
+ * @returns The corresponding ts code.
242
+ */ export async function generateTypescriptContractInterface(input, artifactImportPath) {
243
+ const methods = input.functions.filter((f)=>!f.isInternal).sort((a, b)=>a.name.localeCompare(b.name)).map(generateMethod);
244
+ const deploy = artifactImportPath && generateDeploy(input);
245
+ const ctor = artifactImportPath && generateConstructor(input.name);
246
+ const at = artifactImportPath && generateAt(input.name);
247
+ const artifactStatement = artifactImportPath && generateAbiStatement(input.name, artifactImportPath);
248
+ const artifactGetter = artifactImportPath && generateArtifactGetter(input.name);
249
+ const storageLayoutGetter = artifactImportPath && generateStorageLayoutGetter(input);
250
+ const notesGetter = artifactImportPath && generateNotesGetter(input);
251
+ const { eventDefs, events } = await generateEvents(input.outputs.structs?.events);
252
+ return `
253
+ /* Autogenerated file, do not edit! */
254
+
255
+ /* eslint-disable */
256
+ import {
257
+ type AbiType,
258
+ AztecAddress,
259
+ type AztecAddressLike,
260
+ CompleteAddress,
261
+ Contract,
262
+ type ContractArtifact,
263
+ ContractBase,
264
+ ContractFunctionInteraction,
265
+ type ContractInstanceWithAddress,
266
+ type ContractMethod,
267
+ type ContractStorageLayout,
268
+ type ContractNotes,
269
+ decodeFromAbi,
270
+ DeployMethod,
271
+ EthAddress,
272
+ type EthAddressLike,
273
+ EventSelector,
274
+ type FieldLike,
275
+ Fr,
276
+ type FunctionSelectorLike,
277
+ L1EventPayload,
278
+ loadContractArtifact,
279
+ type NoirCompiledContract,
280
+ NoteSelector,
281
+ Point,
282
+ type PublicKey,
283
+ PublicKeys,
284
+ type Wallet,
285
+ type U128Like,
286
+ type WrappedFieldLike,
287
+ } from '@aztec/aztec.js';
288
+ ${artifactStatement}
289
+
290
+ ${eventDefs}
291
+
292
+ /**
293
+ * Type-safe interface for contract ${input.name};
294
+ */
295
+ export class ${input.name}Contract extends ContractBase {
296
+ ${ctor}
297
+
298
+ ${at}
299
+
300
+ ${deploy}
301
+
302
+ ${artifactGetter}
303
+
304
+ ${storageLayoutGetter}
305
+
306
+ ${notesGetter}
307
+
308
+ /** Type-safe wrappers for the public methods exposed by the contract. */
309
+ public declare methods: {
310
+ ${methods.join('\n')}
311
+ };
312
+
313
+ ${events}
314
+ }
315
+ `;
316
+ }
@@ -0,0 +1,3 @@
1
+ import type { Command } from 'commander';
2
+ export declare function injectCommands(program: Command): Command;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGzC,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,WAY9C"}
package/dest/index.js ADDED
@@ -0,0 +1,10 @@
1
+ import { dirname } from 'path';
2
+ export function injectCommands(program) {
3
+ program.command('codegen').argument('<noir-abi-path>', 'Path to the Noir ABI or project dir.').option('-o, --outdir <path>', 'Output folder for the generated code.').option('-f, --force', 'Force code generation even when the contract has not changed.').description('Validates and generates an Aztec Contract ABI from Noir ABI.').action(async (noirAbiPath, { outdir, force })=>{
4
+ const { generateCode } = await import('./contract-interface-gen/codegen.js');
5
+ await generateCode(outdir || dirname(noirAbiPath), noirAbiPath, {
6
+ force
7
+ });
8
+ });
9
+ return program;
10
+ }
package/package.json ADDED
@@ -0,0 +1,91 @@
1
+ {
2
+ "name": "@aztec/builder",
3
+ "version": "0.0.0-test.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./dest/index.js",
7
+ "./cli": "./dest/bin/cli.js"
8
+ },
9
+ "typedocOptions": {
10
+ "entryPoints": [
11
+ "./src/index.ts"
12
+ ],
13
+ "name": "Aztec builder",
14
+ "tsconfig": "./tsconfig.json"
15
+ },
16
+ "bin": {
17
+ "aztec-builder": "dest/bin/cli.js"
18
+ },
19
+ "scripts": {
20
+ "build": "yarn clean && tsc -b",
21
+ "build:dev": "tsc -b --watch",
22
+ "generate": "tsc -b",
23
+ "clean": "rm -rf ./dest .tsbuildinfo",
24
+ "formatting": "run -T prettier --check ./src && run -T eslint ./src",
25
+ "formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src",
26
+ "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
27
+ },
28
+ "inherits": [
29
+ "../package.common.json"
30
+ ],
31
+ "jest": {
32
+ "moduleNameMapper": {
33
+ "^(\\.{1,2}/.*)\\.[cm]?js$": "$1"
34
+ },
35
+ "moduleFileExtensions": [
36
+ "js",
37
+ "ts",
38
+ "cts"
39
+ ],
40
+ "testRegex": "./src/.*\\.test\\.(js|mjs|ts)$",
41
+ "rootDir": "./src",
42
+ "transform": {
43
+ "^.+\\.tsx?$": [
44
+ "@swc/jest",
45
+ {
46
+ "jsc": {
47
+ "parser": {
48
+ "syntax": "typescript",
49
+ "decorators": true
50
+ },
51
+ "transform": {
52
+ "decoratorVersion": "2022-03"
53
+ }
54
+ }
55
+ }
56
+ ]
57
+ },
58
+ "extensionsToTreatAsEsm": [
59
+ ".ts"
60
+ ],
61
+ "reporters": [
62
+ "default"
63
+ ],
64
+ "testTimeout": 120000,
65
+ "setupFiles": [
66
+ "../../foundation/src/jest/setup.mjs"
67
+ ]
68
+ },
69
+ "dependencies": {
70
+ "@aztec/foundation": "0.0.0-test.0",
71
+ "@aztec/stdlib": "0.0.0-test.0",
72
+ "commander": "^12.1.0"
73
+ },
74
+ "devDependencies": {
75
+ "@jest/globals": "^29.5.0",
76
+ "@types/jest": "^29.5.0",
77
+ "@types/node": "^18.7.23",
78
+ "jest": "^29.5.0",
79
+ "ts-node": "^10.9.1",
80
+ "typescript": "^5.0.4"
81
+ },
82
+ "files": [
83
+ "dest",
84
+ "src",
85
+ "!*.test.*"
86
+ ],
87
+ "types": "./dest/index.d.ts",
88
+ "engines": {
89
+ "node": ">=18"
90
+ }
91
+ }
package/src/bin/cli.ts ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env node
2
+ import { createConsoleLogger } from '@aztec/foundation/log';
3
+
4
+ import { Command } from 'commander';
5
+
6
+ import { injectCommands as injectBuilderCommands } from '../index.js';
7
+
8
+ const log = createConsoleLogger('aztec:builder');
9
+
10
+ const main = async () => {
11
+ const program = new Command('aztec-builder');
12
+
13
+ injectBuilderCommands(program);
14
+ await program.parseAsync(process.argv);
15
+ // I force exit here because spawnSync in npm.ts just blocks the process from exiting. Spent a bit of time debugging
16
+ // it without success and I think it doesn't make sense to invest more time in this.
17
+ process.exit(0);
18
+ };
19
+
20
+ main().catch(err => {
21
+ log(`Error running command`);
22
+ log(err);
23
+ process.exit(1);
24
+ });
@@ -0,0 +1,106 @@
1
+ /* eslint-disable no-console */
2
+ import { loadContractArtifact } from '@aztec/stdlib/abi';
3
+
4
+ import crypto from 'crypto';
5
+ import { access, mkdir, readFile, readdir, stat, writeFile } from 'fs/promises';
6
+ import path from 'path';
7
+
8
+ import { generateTypescriptContractInterface } from './typescript.js';
9
+
10
+ const cacheFilePath = './codegenCache.json';
11
+ let cache: Record<string, { contractName: string; hash: string }> = {};
12
+
13
+ /** Generate code options */
14
+ export type GenerateCodeOptions = { force?: boolean };
15
+
16
+ /**
17
+ * Generates Noir interface or Typescript interface for a folder or single file from a Noir compilation artifact.
18
+ */
19
+ export async function generateCode(outputPath: string, fileOrDirPath: string, opts: GenerateCodeOptions = {}) {
20
+ await readCache();
21
+ const results = [];
22
+ const stats = await stat(fileOrDirPath);
23
+
24
+ if (stats.isDirectory()) {
25
+ const files = (await readdir(fileOrDirPath, { recursive: true, encoding: 'utf-8' })).filter(
26
+ file => file.endsWith('.json') && !file.startsWith('debug_'),
27
+ );
28
+ for (const file of files) {
29
+ const fullPath = path.join(fileOrDirPath, file);
30
+ results.push(await generateFromNoirAbi(outputPath, fullPath, opts));
31
+ }
32
+ } else if (stats.isFile()) {
33
+ results.push(await generateFromNoirAbi(outputPath, fileOrDirPath, opts));
34
+ }
35
+ await writeCache();
36
+ return results;
37
+ }
38
+
39
+ /**
40
+ * Generates Noir interface or Typescript interface for a single file Noir compilation artifact.
41
+ */
42
+ async function generateFromNoirAbi(outputPath: string, noirAbiPath: string, opts: GenerateCodeOptions = {}) {
43
+ const fileName = path.basename(noirAbiPath);
44
+ const currentHash = await generateFileHash(noirAbiPath);
45
+ const cachedInstance = isCacheValid(fileName, currentHash);
46
+ if (cachedInstance && !opts.force) {
47
+ console.log(`${fileName} has not changed. Skipping generation.`);
48
+ return `${outputPath}/${cachedInstance.contractName}.ts`;
49
+ }
50
+
51
+ const file = await readFile(noirAbiPath, 'utf8');
52
+ const contract = JSON.parse(file);
53
+ const aztecAbi = loadContractArtifact(contract);
54
+
55
+ await mkdir(outputPath, { recursive: true });
56
+
57
+ let relativeArtifactPath = path.relative(outputPath, noirAbiPath);
58
+ if (relativeArtifactPath === path.basename(noirAbiPath)) {
59
+ // Prepend ./ for local import if the folder is the same
60
+ relativeArtifactPath = `./${relativeArtifactPath}`;
61
+ }
62
+
63
+ const tsWrapper = await generateTypescriptContractInterface(aztecAbi, relativeArtifactPath);
64
+ const outputFilePath = `${outputPath}/${aztecAbi.name}.ts`;
65
+
66
+ await writeFile(outputFilePath, tsWrapper);
67
+
68
+ updateCache(fileName, aztecAbi.name, currentHash);
69
+ return outputFilePath;
70
+ }
71
+
72
+ async function generateFileHash(filePath: string) {
73
+ const fileBuffer = await readFile(filePath);
74
+ const hashSum = crypto.createHash('sha256');
75
+ hashSum.update(fileBuffer);
76
+ const hex = hashSum.digest('hex');
77
+ return hex;
78
+ }
79
+
80
+ async function readCache() {
81
+ if (await exists(cacheFilePath)) {
82
+ const cacheRaw = await readFile(cacheFilePath, 'utf8');
83
+ cache = JSON.parse(cacheRaw);
84
+ }
85
+ }
86
+
87
+ async function writeCache() {
88
+ await writeFile(cacheFilePath, JSON.stringify(cache, null, 2), 'utf8');
89
+ }
90
+
91
+ function isCacheValid(contractName: string, currentHash: string) {
92
+ return cache[contractName]?.hash === currentHash && cache[contractName];
93
+ }
94
+
95
+ function updateCache(fileName: string, contractName: string, hash: string): void {
96
+ cache[fileName] = { contractName, hash };
97
+ }
98
+
99
+ async function exists(filePath: string) {
100
+ try {
101
+ await access(filePath);
102
+ return true;
103
+ } catch {
104
+ return false;
105
+ }
106
+ }
@@ -0,0 +1,378 @@
1
+ import {
2
+ type ABIParameter,
3
+ type ABIVariable,
4
+ type ContractArtifact,
5
+ EventSelector,
6
+ type FunctionArtifact,
7
+ decodeFunctionSignature,
8
+ getDefaultInitializer,
9
+ isAztecAddressStruct,
10
+ isEthAddressStruct,
11
+ isFunctionSelectorStruct,
12
+ isWrappedFieldStruct,
13
+ } from '@aztec/stdlib/abi';
14
+
15
+ /**
16
+ * Returns the corresponding typescript type for a given Noir type.
17
+ * @param type - The input Noir type.
18
+ * @returns An equivalent typescript type.
19
+ */
20
+ function abiTypeToTypescript(type: ABIParameter['type']): string {
21
+ switch (type.kind) {
22
+ case 'field':
23
+ return 'FieldLike';
24
+ case 'boolean':
25
+ return 'boolean';
26
+ case 'integer':
27
+ return '(bigint | number)';
28
+ case 'string':
29
+ return 'string';
30
+ case 'array':
31
+ return `${abiTypeToTypescript(type.type)}[]`;
32
+ case 'struct':
33
+ if (isEthAddressStruct(type)) {
34
+ return 'EthAddressLike';
35
+ }
36
+ if (isAztecAddressStruct(type)) {
37
+ return 'AztecAddressLike';
38
+ }
39
+ if (isFunctionSelectorStruct(type)) {
40
+ return 'FunctionSelectorLike';
41
+ }
42
+ if (isWrappedFieldStruct(type)) {
43
+ return 'WrappedFieldLike';
44
+ }
45
+ return `{ ${type.fields.map(f => `${f.name}: ${abiTypeToTypescript(f.type)}`).join(', ')} }`;
46
+ default:
47
+ throw new Error(`Unknown type ${type}`);
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Generates the typescript code to represent a Noir parameter.
53
+ * @param param - A Noir parameter with name and type.
54
+ * @returns The corresponding ts code.
55
+ */
56
+ function generateParameter(param: ABIParameter) {
57
+ return `${param.name}: ${abiTypeToTypescript(param.type)}`;
58
+ }
59
+
60
+ /**
61
+ * Generates the typescript code to represent a Noir function as a type.
62
+ * @param param - A Noir function.
63
+ * @returns The corresponding ts code.
64
+ */
65
+ function generateMethod(entry: FunctionArtifact) {
66
+ const args = entry.parameters.map(generateParameter).join(', ');
67
+ return `
68
+ /** ${entry.name}(${entry.parameters.map(p => `${p.name}: ${p.type.kind}`).join(', ')}) */
69
+ ${entry.name}: ((${args}) => ContractFunctionInteraction) & Pick<ContractMethod, 'selector'>;`;
70
+ }
71
+
72
+ /**
73
+ * Generates a deploy method for this contract.
74
+ * @param input - Build artifact of the contract.
75
+ * @returns A type-safe deploy method in ts.
76
+ */
77
+ function generateDeploy(input: ContractArtifact) {
78
+ const ctor = getDefaultInitializer(input);
79
+ const args = (ctor?.parameters ?? []).map(generateParameter).join(', ');
80
+ const contractName = `${input.name}Contract`;
81
+ const artifactName = `${contractName}Artifact`;
82
+
83
+ return `
84
+ /**
85
+ * Creates a tx to deploy a new instance of this contract.
86
+ */
87
+ public static deploy(wallet: Wallet, ${args}) {
88
+ return new DeployMethod<${contractName}>(PublicKeys.default(), wallet, ${artifactName}, ${contractName}.at, Array.from(arguments).slice(1));
89
+ }
90
+
91
+ /**
92
+ * Creates a tx to deploy a new instance of this contract using the specified public keys hash to derive the address.
93
+ */
94
+ public static deployWithPublicKeys(publicKeys: PublicKeys, wallet: Wallet, ${args}) {
95
+ return new DeployMethod<${contractName}>(publicKeys, wallet, ${artifactName}, ${contractName}.at, Array.from(arguments).slice(2));
96
+ }
97
+
98
+ /**
99
+ * Creates a tx to deploy a new instance of this contract using the specified constructor method.
100
+ */
101
+ public static deployWithOpts<M extends keyof ${contractName}['methods']>(
102
+ opts: { publicKeys?: PublicKeys; method?: M; wallet: Wallet },
103
+ ...args: Parameters<${contractName}['methods'][M]>
104
+ ) {
105
+ return new DeployMethod<${contractName}>(
106
+ opts.publicKeys ?? PublicKeys.default(),
107
+ opts.wallet,
108
+ ${artifactName},
109
+ ${contractName}.at,
110
+ Array.from(arguments).slice(1),
111
+ opts.method ?? 'constructor',
112
+ );
113
+ }
114
+ `;
115
+ }
116
+
117
+ /**
118
+ * Generates the constructor by supplying the ABI to the parent class so the user doesn't have to.
119
+ * @param name - Name of the contract to derive the ABI name from.
120
+ * @returns A constructor method.
121
+ * @remarks The constructor is private because we want to force the user to use the create method.
122
+ */
123
+ function generateConstructor(name: string) {
124
+ return `
125
+ private constructor(
126
+ instance: ContractInstanceWithAddress,
127
+ wallet: Wallet,
128
+ ) {
129
+ super(instance, ${name}ContractArtifact, wallet);
130
+ }
131
+ `;
132
+ }
133
+
134
+ /**
135
+ * Generates the at method for this contract.
136
+ * @param name - Name of the contract to derive the ABI name from.
137
+ * @returns An at method.
138
+ * @remarks We don't use constructor directly because of the async `wallet.getContractData` call.
139
+ */
140
+ function generateAt(name: string) {
141
+ return `
142
+ /**
143
+ * Creates a contract instance.
144
+ * @param address - The deployed contract's address.
145
+ * @param wallet - The wallet to use when interacting with the contract.
146
+ * @returns A promise that resolves to a new Contract instance.
147
+ */
148
+ public static async at(
149
+ address: AztecAddress,
150
+ wallet: Wallet,
151
+ ) {
152
+ return Contract.at(address, ${name}Contract.artifact, wallet) as Promise<${name}Contract>;
153
+ }`;
154
+ }
155
+
156
+ /**
157
+ * Generates a static getter for the contract's artifact.
158
+ * @param name - Name of the contract used to derive name of the artifact import.
159
+ */
160
+ function generateArtifactGetter(name: string) {
161
+ const artifactName = `${name}ContractArtifact`;
162
+ return `
163
+ /**
164
+ * Returns this contract's artifact.
165
+ */
166
+ public static get artifact(): ContractArtifact {
167
+ return ${artifactName};
168
+ }
169
+ `;
170
+ }
171
+
172
+ /**
173
+ * Generates statements for importing the artifact from json and re-exporting it.
174
+ * @param name - Name of the contract.
175
+ * @param artifactImportPath - Path to load the ABI from.
176
+ * @returns Code.
177
+ */
178
+ function generateAbiStatement(name: string, artifactImportPath: string) {
179
+ const stmts = [
180
+ `import ${name}ContractArtifactJson from '${artifactImportPath}' assert { type: 'json' };`,
181
+ `export const ${name}ContractArtifact = loadContractArtifact(${name}ContractArtifactJson as NoirCompiledContract);`,
182
+ ];
183
+ return stmts.join('\n');
184
+ }
185
+
186
+ /**
187
+ * Generates a getter for the contract's storage layout.
188
+ * @param input - The contract artifact.
189
+ */
190
+ function generateStorageLayoutGetter(input: ContractArtifact) {
191
+ const entries = Object.entries(input.storageLayout);
192
+
193
+ if (entries.length === 0) {
194
+ return '';
195
+ }
196
+
197
+ const storageFieldsUnionType = entries.map(([name]) => `'${name}'`).join(' | ');
198
+ const layout = entries
199
+ .map(
200
+ ([name, { slot }]) =>
201
+ `${name}: {
202
+ slot: new Fr(${slot.toBigInt()}n),
203
+ }`,
204
+ )
205
+ .join(',\n');
206
+
207
+ return `public static get storage(): ContractStorageLayout<${storageFieldsUnionType}> {
208
+ return {
209
+ ${layout}
210
+ } as ContractStorageLayout<${storageFieldsUnionType}>;
211
+ }
212
+ `;
213
+ }
214
+
215
+ /**
216
+ * Generates a getter for the contract notes
217
+ * @param input - The contract artifact.
218
+ */
219
+ function generateNotesGetter(input: ContractArtifact) {
220
+ const entries = Object.entries(input.notes);
221
+
222
+ if (entries.length === 0) {
223
+ return '';
224
+ }
225
+
226
+ const notesUnionType = entries.map(([name]) => `'${name}'`).join(' | ');
227
+ const noteMetadata = entries
228
+ .map(
229
+ ([name, { id }]) =>
230
+ `${name}: {
231
+ id: new NoteSelector(${id.value}),
232
+ }`,
233
+ )
234
+ .join(',\n');
235
+
236
+ return `public static get notes(): ContractNotes<${notesUnionType}> {
237
+ return {
238
+ ${noteMetadata}
239
+ } as ContractNotes<${notesUnionType}>;
240
+ }
241
+ `;
242
+ }
243
+
244
+ // events is of type AbiType
245
+ async function generateEvents(events: any[] | undefined) {
246
+ if (events === undefined) {
247
+ return { events: '', eventDefs: '' };
248
+ }
249
+
250
+ const eventsMetadata = await Promise.all(
251
+ events.map(async event => {
252
+ const eventName = event.path.split('::').at(-1);
253
+
254
+ const eventDefProps = event.fields.map(
255
+ (field: ABIVariable) => `${field.name}: ${abiTypeToTypescript(field.type)}`,
256
+ );
257
+ const eventDef = `
258
+ export type ${eventName} = {
259
+ ${eventDefProps.join('\n')}
260
+ }
261
+ `;
262
+
263
+ const fieldNames = event.fields.map((field: any) => `"${field.name}"`);
264
+ const eventType = `${eventName}: {abiType: AbiType, eventSelector: EventSelector, fieldNames: string[] }`;
265
+ // Reusing the decodeFunctionSignature
266
+ const eventSignature = decodeFunctionSignature(eventName, event.fields);
267
+ const eventSelector = await EventSelector.fromSignature(eventSignature);
268
+ const eventImpl = `${eventName}: {
269
+ abiType: ${JSON.stringify(event, null, 4)},
270
+ eventSelector: EventSelector.fromString("${eventSelector}"),
271
+ fieldNames: [${fieldNames}],
272
+ }`;
273
+
274
+ return {
275
+ eventDef,
276
+ eventType,
277
+ eventImpl,
278
+ };
279
+ }),
280
+ );
281
+
282
+ return {
283
+ eventDefs: eventsMetadata.map(({ eventDef }) => eventDef).join('\n'),
284
+ events: `
285
+ public static get events(): { ${eventsMetadata.map(({ eventType }) => eventType).join(', ')} } {
286
+ return {
287
+ ${eventsMetadata.map(({ eventImpl }) => eventImpl).join(',\n')}
288
+ };
289
+ }
290
+ `,
291
+ };
292
+ }
293
+
294
+ /**
295
+ * Generates the typescript code to represent a contract.
296
+ * @param input - The compiled Noir artifact.
297
+ * @param artifactImportPath - Optional path to import the artifact (if not set, will be required in the constructor).
298
+ * @returns The corresponding ts code.
299
+ */
300
+ export async function generateTypescriptContractInterface(input: ContractArtifact, artifactImportPath?: string) {
301
+ const methods = input.functions
302
+ .filter(f => !f.isInternal)
303
+ .sort((a, b) => a.name.localeCompare(b.name))
304
+ .map(generateMethod);
305
+ const deploy = artifactImportPath && generateDeploy(input);
306
+ const ctor = artifactImportPath && generateConstructor(input.name);
307
+ const at = artifactImportPath && generateAt(input.name);
308
+ const artifactStatement = artifactImportPath && generateAbiStatement(input.name, artifactImportPath);
309
+ const artifactGetter = artifactImportPath && generateArtifactGetter(input.name);
310
+ const storageLayoutGetter = artifactImportPath && generateStorageLayoutGetter(input);
311
+ const notesGetter = artifactImportPath && generateNotesGetter(input);
312
+ const { eventDefs, events } = await generateEvents(input.outputs.structs?.events);
313
+
314
+ return `
315
+ /* Autogenerated file, do not edit! */
316
+
317
+ /* eslint-disable */
318
+ import {
319
+ type AbiType,
320
+ AztecAddress,
321
+ type AztecAddressLike,
322
+ CompleteAddress,
323
+ Contract,
324
+ type ContractArtifact,
325
+ ContractBase,
326
+ ContractFunctionInteraction,
327
+ type ContractInstanceWithAddress,
328
+ type ContractMethod,
329
+ type ContractStorageLayout,
330
+ type ContractNotes,
331
+ decodeFromAbi,
332
+ DeployMethod,
333
+ EthAddress,
334
+ type EthAddressLike,
335
+ EventSelector,
336
+ type FieldLike,
337
+ Fr,
338
+ type FunctionSelectorLike,
339
+ L1EventPayload,
340
+ loadContractArtifact,
341
+ type NoirCompiledContract,
342
+ NoteSelector,
343
+ Point,
344
+ type PublicKey,
345
+ PublicKeys,
346
+ type Wallet,
347
+ type U128Like,
348
+ type WrappedFieldLike,
349
+ } from '@aztec/aztec.js';
350
+ ${artifactStatement}
351
+
352
+ ${eventDefs}
353
+
354
+ /**
355
+ * Type-safe interface for contract ${input.name};
356
+ */
357
+ export class ${input.name}Contract extends ContractBase {
358
+ ${ctor}
359
+
360
+ ${at}
361
+
362
+ ${deploy}
363
+
364
+ ${artifactGetter}
365
+
366
+ ${storageLayoutGetter}
367
+
368
+ ${notesGetter}
369
+
370
+ /** Type-safe wrappers for the public methods exposed by the contract. */
371
+ public declare methods: {
372
+ ${methods.join('\n')}
373
+ };
374
+
375
+ ${events}
376
+ }
377
+ `;
378
+ }
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ import type { Command } from 'commander';
2
+ import { dirname } from 'path';
3
+
4
+ export function injectCommands(program: Command) {
5
+ program
6
+ .command('codegen')
7
+ .argument('<noir-abi-path>', 'Path to the Noir ABI or project dir.')
8
+ .option('-o, --outdir <path>', 'Output folder for the generated code.')
9
+ .option('-f, --force', 'Force code generation even when the contract has not changed.')
10
+ .description('Validates and generates an Aztec Contract ABI from Noir ABI.')
11
+ .action(async (noirAbiPath: string, { outdir, force }) => {
12
+ const { generateCode } = await import('./contract-interface-gen/codegen.js');
13
+ await generateCode(outdir || dirname(noirAbiPath), noirAbiPath, { force });
14
+ });
15
+ return program;
16
+ }