From b376bc084257eea97b6c105168fa5abe74cbbdb2 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Tue, 6 Feb 2024 15:57:37 -0300 Subject: [PATCH] chore: Unhardcode canonical addresses of deployer and registerer contracts --- yarn-project/Dockerfile | 3 +- yarn-project/archiver/package.json | 1 + .../archiver/src/archiver/archiver.ts | 16 +- yarn-project/archiver/tsconfig.json | 3 + yarn-project/bootstrap.sh | 4 +- yarn-project/deploy_npm.sh | 1 + yarn-project/package.json | 1 + yarn-project/protocol-contracts/.eslintrc.cjs | 1 + yarn-project/protocol-contracts/.gitignore | 1 + yarn-project/protocol-contracts/README.md | 7 + yarn-project/protocol-contracts/package.json | 69 +++ .../protocol-contracts/package.local.json | 9 + .../scripts/copy-contracts.sh | 11 + .../__snapshots__/index.test.ts.snap | 555 ++++++++++++++++++ .../src/class-registerer/artifact.ts | 8 + .../src/class-registerer/index.test.ts | 13 + .../src/class-registerer/index.ts | 17 + yarn-project/protocol-contracts/src/index.ts | 1 + .../__snapshots__/index.test.ts.snap | 463 +++++++++++++++ .../src/instance-deployer/artifact.ts | 8 + .../src/instance-deployer/index.test.ts | 15 + .../src/instance-deployer/index.ts | 13 + .../src/protocol_contract.ts | 33 ++ yarn-project/protocol-contracts/tsconfig.json | 20 + yarn-project/tsconfig.json | 1 + yarn-project/yarn.lock | 25 +- 26 files changed, 1284 insertions(+), 15 deletions(-) create mode 100644 yarn-project/protocol-contracts/.eslintrc.cjs create mode 100644 yarn-project/protocol-contracts/.gitignore create mode 100644 yarn-project/protocol-contracts/README.md create mode 100644 yarn-project/protocol-contracts/package.json create mode 100644 yarn-project/protocol-contracts/package.local.json create mode 100755 yarn-project/protocol-contracts/scripts/copy-contracts.sh create mode 100644 yarn-project/protocol-contracts/src/class-registerer/__snapshots__/index.test.ts.snap create mode 100644 yarn-project/protocol-contracts/src/class-registerer/artifact.ts create mode 100644 yarn-project/protocol-contracts/src/class-registerer/index.test.ts create mode 100644 yarn-project/protocol-contracts/src/class-registerer/index.ts create mode 100644 yarn-project/protocol-contracts/src/index.ts create mode 100644 yarn-project/protocol-contracts/src/instance-deployer/__snapshots__/index.test.ts.snap create mode 100644 yarn-project/protocol-contracts/src/instance-deployer/artifact.ts create mode 100644 yarn-project/protocol-contracts/src/instance-deployer/index.test.ts create mode 100644 yarn-project/protocol-contracts/src/instance-deployer/index.ts create mode 100644 yarn-project/protocol-contracts/src/protocol_contract.ts create mode 100644 yarn-project/protocol-contracts/tsconfig.json diff --git a/yarn-project/Dockerfile b/yarn-project/Dockerfile index fcc719fefd1..dc2c7174abf 100644 --- a/yarn-project/Dockerfile +++ b/yarn-project/Dockerfile @@ -13,8 +13,9 @@ COPY . . RUN yarn workspace @aztec/noir-compiler build # Builds noir contracts (TODO: move this stage pre yarn-project). Generates typescript wrappers. RUN yarn workspace @aztec/noir-contracts build:contracts -# We need to build accounts as it needs to copy in account contracts from noir-contracts. +# We need to copy noir contracts into other packages RUN yarn workspace @aztec/accounts build:copy-contracts +RUN yarn workspace @aztec/protocol-contracts build:copy-contracts RUN yarn workspace @aztec/noir-protocol-circuits build RUN yarn tsc -b diff --git a/yarn-project/archiver/package.json b/yarn-project/archiver/package.json index 226a990ee5e..2612739365f 100644 --- a/yarn-project/archiver/package.json +++ b/yarn-project/archiver/package.json @@ -41,6 +41,7 @@ "@aztec/foundation": "workspace:^", "@aztec/kv-store": "workspace:^", "@aztec/l1-artifacts": "workspace:^", + "@aztec/protocol-contracts": "workspace:^", "@aztec/types": "workspace:^", "debug": "^4.3.4", "lmdb": "^2.9.2", diff --git a/yarn-project/archiver/src/archiver/archiver.ts b/yarn-project/archiver/src/archiver/archiver.ts index 3eb5cae25ee..b0568277d7d 100644 --- a/yarn-project/archiver/src/archiver/archiver.ts +++ b/yarn-project/archiver/src/archiver/archiver.ts @@ -31,6 +31,8 @@ import { EthAddress } from '@aztec/foundation/eth-address'; import { Fr } from '@aztec/foundation/fields'; import { DebugLogger, createDebugLogger } from '@aztec/foundation/log'; import { RunningPromise } from '@aztec/foundation/running-promise'; +import { ClassRegistererAddress } from '@aztec/protocol-contracts/class-registerer'; +import { InstanceDeployerAddress } from '@aztec/protocol-contracts/instance-deployer'; import { ContractClass, ContractClassPublic, @@ -71,21 +73,11 @@ export class Archiver implements ArchiveSource { */ private lastLoggedL1BlockNumber = 0n; - // TODO(@spalladino): Calculate this on the fly somewhere else. - // Today this is printed in the logs for end-to-end test at - // end-to-end/src/e2e_deploy_contract.test.ts -t 'registering a new contract class' - // "Added contract ContractClassRegisterer ADDRESS" - // "Added contract ContractInstanceDeployer ADDRESS" - /** Address of the ClassRegisterer contract with a salt=1 */ - private classRegistererAddress = AztecAddress.fromString( - '0x29c0cd0000951bba8af520ad5513cc53d9f0413c5a24a72a4ba8c17894c0bef9', - ); + private classRegistererAddress = ClassRegistererAddress; /** Address of the InstanceDeployer contract with a salt=1 */ - private instanceDeployerAddress = AztecAddress.fromString( - '0x1799c61aa10430bf6fec46679c4cb76c3ed12cd8b6e73ed7389d5ae296ad1b97', - ); + private instanceDeployerAddress = InstanceDeployerAddress; /** * Creates a new instance of the Archiver. diff --git a/yarn-project/archiver/tsconfig.json b/yarn-project/archiver/tsconfig.json index 69fe51229cc..08c5ba86b06 100644 --- a/yarn-project/archiver/tsconfig.json +++ b/yarn-project/archiver/tsconfig.json @@ -24,6 +24,9 @@ { "path": "../l1-artifacts" }, + { + "path": "../protocol-contracts" + }, { "path": "../types" } diff --git a/yarn-project/bootstrap.sh b/yarn-project/bootstrap.sh index 9fe3fd369a8..6df64d6c3af 100755 --- a/yarn-project/bootstrap.sh +++ b/yarn-project/bootstrap.sh @@ -42,9 +42,11 @@ yarn workspace @aztec/noir-compiler build # Builds noir contracts (TODO: move this stage pre yarn-project). Generates typescript wrappers. echo "Building contracts from noir-contracts..." yarn workspace @aztec/noir-contracts build:contracts -# Bundle compiled account contracts into accounts package +# Bundle compiled contracts into other packages echo "Copying account contracts..." yarn workspace @aztec/accounts build:copy-contracts +echo "Copying protocol contracts..." +yarn workspace @aztec/protocol-contracts build:copy-contracts # Build protocol circuits. TODO: move pre yarn-project. echo "Building circuits from noir-protocol-circuits..." yarn workspace @aztec/noir-protocol-circuits build diff --git a/yarn-project/deploy_npm.sh b/yarn-project/deploy_npm.sh index da60b0a70e8..c5a4757f722 100755 --- a/yarn-project/deploy_npm.sh +++ b/yarn-project/deploy_npm.sh @@ -83,6 +83,7 @@ deploy_package foundation deploy_package types deploy_package circuits.js deploy_package circuit-types +deploy_package protocol-contracts deploy_package aztec.js deploy_package accounts deploy_package l1-artifacts diff --git a/yarn-project/package.json b/yarn-project/package.json index cf7ad7b8c91..4d68e44fc0b 100644 --- a/yarn-project/package.json +++ b/yarn-project/package.json @@ -40,6 +40,7 @@ "noir-protocol-circuits", "p2p", "p2p-bootstrap", + "protocol-contracts", "prover-client", "rollup-provider", "sequencer-client", diff --git a/yarn-project/protocol-contracts/.eslintrc.cjs b/yarn-project/protocol-contracts/.eslintrc.cjs new file mode 100644 index 00000000000..e659927475c --- /dev/null +++ b/yarn-project/protocol-contracts/.eslintrc.cjs @@ -0,0 +1 @@ +module.exports = require('@aztec/foundation/eslint'); diff --git a/yarn-project/protocol-contracts/.gitignore b/yarn-project/protocol-contracts/.gitignore new file mode 100644 index 00000000000..7912fc48c7d --- /dev/null +++ b/yarn-project/protocol-contracts/.gitignore @@ -0,0 +1 @@ +/src/artifacts diff --git a/yarn-project/protocol-contracts/README.md b/yarn-project/protocol-contracts/README.md new file mode 100644 index 00000000000..7a863ea8a26 --- /dev/null +++ b/yarn-project/protocol-contracts/README.md @@ -0,0 +1,7 @@ +# Protocol Contracts + +Canonical Noir contracts used to power the Aztec Network protocol. + +Includes: +- Contract class registerer +- Contract instance deployer \ No newline at end of file diff --git a/yarn-project/protocol-contracts/package.json b/yarn-project/protocol-contracts/package.json new file mode 100644 index 00000000000..74f2eb153ea --- /dev/null +++ b/yarn-project/protocol-contracts/package.json @@ -0,0 +1,69 @@ +{ + "name": "@aztec/protocol-contracts", + "homepage": "https://github.com/AztecProtocol/aztec-packages/tree/master/yarn-project/protocol-contracts", + "description": "Canonical Noir contracts for the Aztec Network", + "version": "0.1.0", + "type": "module", + "exports": { + ".": "./dest/index.js", + "./*": "./dest/*/index.js" + }, + "typedocOptions": { + "entryPoints": [ + "./src/index.ts", + "./src/class-registerer/index.ts", + "./src/instance-deployer/index.ts" + ], + "name": "Protocol Contracts", + "tsconfig": "./tsconfig.json" + }, + "scripts": { + "build": "yarn clean && yarn build:copy-contracts && tsc -b", + "build:copy-contracts": "./scripts/copy-contracts.sh", + "build:dev": "tsc -b --watch", + "build:ts": "tsc -b", + "clean": "rm -rf ./dest .tsbuildinfo ./src/artifacts", + "formatting": "run -T prettier --check ./src && run -T eslint ./src", + "formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src", + "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --passWithNoTests" + }, + "inherits": [ + "../package.common.json", + "./package.local.json" + ], + "jest": { + "preset": "ts-jest/presets/default-esm", + "moduleNameMapper": { + "^(\\.{1,2}/.*)\\.[cm]?js$": "$1" + }, + "testRegex": "./src/.*\\.test\\.(js|mjs|ts)$", + "rootDir": "./src" + }, + "dependencies": { + "@aztec/circuits.js": "workspace:^", + "@aztec/foundation": "workspace:^", + "@aztec/types": "workspace:^", + "lodash.omit": "^4.5.0", + "tslib": "^2.4.0" + }, + "devDependencies": { + "@jest/globals": "^29.5.0", + "@types/jest": "^29.5.0", + "@types/lodash.omit": "^4.5.9", + "@types/node": "^18.7.23", + "jest": "^29.5.0", + "jest-mock-extended": "^3.0.3", + "ts-jest": "^29.1.0", + "ts-loader": "^9.4.4", + "ts-node": "^10.9.1", + "typescript": "^5.0.4" + }, + "files": [ + "dest", + "src", + "!*.test.*" + ], + "engines": { + "node": ">=18" + } +} diff --git a/yarn-project/protocol-contracts/package.local.json b/yarn-project/protocol-contracts/package.local.json new file mode 100644 index 00000000000..def45a001a2 --- /dev/null +++ b/yarn-project/protocol-contracts/package.local.json @@ -0,0 +1,9 @@ +{ + "scripts": { + "build": "yarn clean && yarn build:copy-contracts && tsc -b", + "build:copy-contracts": "./scripts/copy-contracts.sh", + "build:dev": "tsc -b --watch", + "build:ts": "tsc -b", + "clean": "rm -rf ./dest .tsbuildinfo ./src/artifacts" + } +} diff --git a/yarn-project/protocol-contracts/scripts/copy-contracts.sh b/yarn-project/protocol-contracts/scripts/copy-contracts.sh new file mode 100755 index 00000000000..51e4f4a5e5b --- /dev/null +++ b/yarn-project/protocol-contracts/scripts/copy-contracts.sh @@ -0,0 +1,11 @@ +#! /bin/bash +set -euo pipefail +mkdir -p ./src/artifacts + +contracts=(contract_class_registerer_contract-ContractClassRegisterer contract_instance_deployer_contract-ContractInstanceDeployer) + +for contract in "${contracts[@]}"; do + cp "../noir-contracts/target/$contract.json" ./src/artifacts/${contract#*-}.json +done + +yarn run -T prettier -w ./src/artifacts \ No newline at end of file diff --git a/yarn-project/protocol-contracts/src/class-registerer/__snapshots__/index.test.ts.snap b/yarn-project/protocol-contracts/src/class-registerer/__snapshots__/index.test.ts.snap new file mode 100644 index 00000000000..6292d1e68c6 --- /dev/null +++ b/yarn-project/protocol-contracts/src/class-registerer/__snapshots__/index.test.ts.snap @@ -0,0 +1,555 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`ClassRegisterer returns canonical protocol contract 1`] = ` +{ + "address": AztecAddress { + "asBigInt": 18885476286413803383198269230770707720385495724494788057562984154330701020921n, + "asBuffer": { + "data": [ + 41, + 192, + 205, + 0, + 0, + 149, + 27, + 186, + 138, + 245, + 32, + 173, + 85, + 19, + 204, + 83, + 217, + 240, + 65, + 60, + 90, + 36, + 167, + 42, + 75, + 168, + 193, + 120, + 148, + 192, + 190, + 249, + ], + "type": "Buffer", + }, + }, + "contractClass": { + "artifactHash": Fr { + "asBigInt": 12987763002449886210249343009930617501836480764589027470816200726753333386077n, + "asBuffer": { + "data": [ + 28, + 182, + 208, + 56, + 31, + 162, + 181, + 120, + 194, + 173, + 145, + 119, + 205, + 23, + 92, + 17, + 148, + 169, + 179, + 132, + 128, + 198, + 168, + 109, + 135, + 242, + 101, + 160, + 56, + 218, + 155, + 93, + ], + "type": "Buffer", + }, + }, + "id": Fr { + "asBigInt": 15610368298223903406763887747663253144646438360806401290390854210044956890867n, + "asBuffer": { + "data": [ + 34, + 131, + 39, + 197, + 16, + 11, + 210, + 53, + 13, + 80, + 129, + 233, + 196, + 47, + 124, + 112, + 85, + 229, + 146, + 176, + 58, + 247, + 80, + 38, + 80, + 100, + 198, + 59, + 105, + 209, + 214, + 243, + ], + "type": "Buffer", + }, + }, + "packedBytecode": { + "data": [ + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + "privateFunctions": [ + { + "isInternal": false, + "selector": FunctionSelector { + "value": 588837637, + }, + "vkHash": Fr { + "asBigInt": 0n, + "asBuffer": { + "data": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + }, + }, + { + "isInternal": false, + "selector": FunctionSelector { + "value": 595568763, + }, + "vkHash": Fr { + "asBigInt": 0n, + "asBuffer": { + "data": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + }, + }, + { + "isInternal": false, + "selector": FunctionSelector { + "value": 1669488881, + }, + "vkHash": Fr { + "asBigInt": 0n, + "asBuffer": { + "data": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + }, + }, + { + "isInternal": false, + "selector": FunctionSelector { + "value": 2432309179, + }, + "vkHash": Fr { + "asBigInt": 0n, + "asBuffer": { + "data": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + }, + }, + ], + "publicFunctions": [], + "version": 1, + }, + "instance": { + "address": AztecAddress { + "asBigInt": 18885476286413803383198269230770707720385495724494788057562984154330701020921n, + "asBuffer": { + "data": [ + 41, + 192, + 205, + 0, + 0, + 149, + 27, + 186, + 138, + 245, + 32, + 173, + 85, + 19, + 204, + 83, + 217, + 240, + 65, + 60, + 90, + 36, + 167, + 42, + 75, + 168, + 193, + 120, + 148, + 192, + 190, + 249, + ], + "type": "Buffer", + }, + }, + "contractClassId": Fr { + "asBigInt": 15610368298223903406763887747663253144646438360806401290390854210044956890867n, + "asBuffer": { + "data": [ + 34, + 131, + 39, + 197, + 16, + 11, + 210, + 53, + 13, + 80, + 129, + 233, + 196, + 47, + 124, + 112, + 85, + 229, + 146, + 176, + 58, + 247, + 80, + 38, + 80, + 100, + 198, + 59, + 105, + 209, + 214, + 243, + ], + "type": "Buffer", + }, + }, + "initializationHash": Fr { + "asBigInt": 5411687428194306823799197943510367074755080310726877398642044502900791103391n, + "asBuffer": { + "data": [ + 11, + 246, + 232, + 18, + 241, + 75, + 176, + 41, + 247, + 203, + 156, + 141, + 168, + 54, + 125, + 217, + 124, + 6, + 142, + 120, + 141, + 79, + 33, + 0, + 127, + 217, + 112, + 20, + 235, + 168, + 207, + 159, + ], + "type": "Buffer", + }, + }, + "portalContractAddress": EthAddress { + "buffer": { + "data": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + }, + "publicKeysHash": Fr { + "asBigInt": 17954372136461894568820199868962143715279932924957662285438325406238856880109n, + "asBuffer": { + "data": [ + 39, + 177, + 208, + 131, + 154, + 91, + 35, + 186, + 241, + 42, + 141, + 25, + 91, + 24, + 172, + 40, + 143, + 207, + 64, + 26, + 251, + 47, + 112, + 184, + 164, + 181, + 41, + 237, + 229, + 250, + 159, + 237, + ], + "type": "Buffer", + }, + }, + "salt": Fr { + "asBigInt": 1n, + "asBuffer": { + "data": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + ], + "type": "Buffer", + }, + }, + "version": 1, + }, +} +`; diff --git a/yarn-project/protocol-contracts/src/class-registerer/artifact.ts b/yarn-project/protocol-contracts/src/class-registerer/artifact.ts new file mode 100644 index 00000000000..2a3ca423ce6 --- /dev/null +++ b/yarn-project/protocol-contracts/src/class-registerer/artifact.ts @@ -0,0 +1,8 @@ +import { loadContractArtifact } from '@aztec/types/abi'; +import { NoirCompiledContract } from '@aztec/types/noir'; + +import ContractClassRegistererJson from '../artifacts/ContractClassRegisterer.json' assert { type: 'json' }; + +export const ContractClassRegistererArtifact = loadContractArtifact( + ContractClassRegistererJson as NoirCompiledContract, +); diff --git a/yarn-project/protocol-contracts/src/class-registerer/index.test.ts b/yarn-project/protocol-contracts/src/class-registerer/index.test.ts new file mode 100644 index 00000000000..bfe56c7340c --- /dev/null +++ b/yarn-project/protocol-contracts/src/class-registerer/index.test.ts @@ -0,0 +1,13 @@ +import omit from 'lodash.omit'; + +import { ClassRegistererAddress, getCanonicalClassRegisterer } from './index.js'; + +describe('ClassRegisterer', () => { + it('returns canonical protocol contract', () => { + const contract = getCanonicalClassRegisterer(); + contract.contractClass.privateFunctions.sort((a, b) => a.selector.value - b.selector.value); + contract.contractClass.publicFunctions.sort((a, b) => a.selector.value - b.selector.value); + expect(omit(contract, 'artifact')).toMatchSnapshot(); + expect(contract.address.toString()).toEqual(ClassRegistererAddress.toString()); + }); +}); diff --git a/yarn-project/protocol-contracts/src/class-registerer/index.ts b/yarn-project/protocol-contracts/src/class-registerer/index.ts new file mode 100644 index 00000000000..a4885c7d975 --- /dev/null +++ b/yarn-project/protocol-contracts/src/class-registerer/index.ts @@ -0,0 +1,17 @@ +import { AztecAddress } from '@aztec/circuits.js'; + +import { ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js'; +import { ContractClassRegistererArtifact } from './artifact.js'; + +/** Returns the canonical deployment of the class registerer contract. */ +export function getCanonicalClassRegisterer(): ProtocolContract { + return getCanonicalProtocolContract(ContractClassRegistererArtifact, 1); +} + +/** + * Address of the canonical class registerer. + * @remarks This should not change often, hence we hardcode it to save from having to recompute it every time. + */ +export const ClassRegistererAddress = AztecAddress.fromString( + '0x29c0cd0000951bba8af520ad5513cc53d9f0413c5a24a72a4ba8c17894c0bef9', +); diff --git a/yarn-project/protocol-contracts/src/index.ts b/yarn-project/protocol-contracts/src/index.ts new file mode 100644 index 00000000000..f8cc9c7109a --- /dev/null +++ b/yarn-project/protocol-contracts/src/index.ts @@ -0,0 +1 @@ +export { ProtocolContract } from './protocol_contract.js'; diff --git a/yarn-project/protocol-contracts/src/instance-deployer/__snapshots__/index.test.ts.snap b/yarn-project/protocol-contracts/src/instance-deployer/__snapshots__/index.test.ts.snap new file mode 100644 index 00000000000..483d619e453 --- /dev/null +++ b/yarn-project/protocol-contracts/src/instance-deployer/__snapshots__/index.test.ts.snap @@ -0,0 +1,463 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`InstanceDeployer returns canonical protocol contract 1`] = ` +{ + "address": AztecAddress { + "asBigInt": 10674890382018500410451173831129216683967953609468942304388116555335197334423n, + "asBuffer": { + "data": [ + 23, + 153, + 198, + 26, + 161, + 4, + 48, + 191, + 111, + 236, + 70, + 103, + 156, + 76, + 183, + 108, + 62, + 209, + 44, + 216, + 182, + 231, + 62, + 215, + 56, + 157, + 90, + 226, + 150, + 173, + 27, + 151, + ], + "type": "Buffer", + }, + }, + "contractClass": { + "artifactHash": Fr { + "asBigInt": 1546915255081493429210407932347415325264105027406908683791229627179136590869n, + "asBuffer": { + "data": [ + 3, + 107, + 133, + 227, + 201, + 147, + 91, + 223, + 147, + 254, + 104, + 143, + 129, + 46, + 252, + 227, + 185, + 74, + 0, + 254, + 227, + 11, + 162, + 57, + 80, + 42, + 239, + 244, + 210, + 18, + 36, + 21, + ], + "type": "Buffer", + }, + }, + "id": Fr { + "asBigInt": 9040462806880011027940587321357143576447825874691049241890511565119666681463n, + "asBuffer": { + "data": [ + 19, + 252, + 184, + 122, + 123, + 192, + 60, + 228, + 9, + 114, + 248, + 85, + 174, + 199, + 235, + 168, + 55, + 50, + 74, + 245, + 238, + 197, + 231, + 41, + 29, + 105, + 17, + 105, + 36, + 126, + 118, + 119, + ], + "type": "Buffer", + }, + }, + "packedBytecode": { + "data": [ + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + "privateFunctions": [ + { + "isInternal": false, + "selector": FunctionSelector { + "value": 2285065643, + }, + "vkHash": Fr { + "asBigInt": 0n, + "asBuffer": { + "data": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + }, + }, + { + "isInternal": false, + "selector": FunctionSelector { + "value": 2432309179, + }, + "vkHash": Fr { + "asBigInt": 0n, + "asBuffer": { + "data": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + }, + }, + ], + "publicFunctions": [], + "version": 1, + }, + "instance": { + "address": AztecAddress { + "asBigInt": 10674890382018500410451173831129216683967953609468942304388116555335197334423n, + "asBuffer": { + "data": [ + 23, + 153, + 198, + 26, + 161, + 4, + 48, + 191, + 111, + 236, + 70, + 103, + 156, + 76, + 183, + 108, + 62, + 209, + 44, + 216, + 182, + 231, + 62, + 215, + 56, + 157, + 90, + 226, + 150, + 173, + 27, + 151, + ], + "type": "Buffer", + }, + }, + "contractClassId": Fr { + "asBigInt": 9040462806880011027940587321357143576447825874691049241890511565119666681463n, + "asBuffer": { + "data": [ + 19, + 252, + 184, + 122, + 123, + 192, + 60, + 228, + 9, + 114, + 248, + 85, + 174, + 199, + 235, + 168, + 55, + 50, + 74, + 245, + 238, + 197, + 231, + 41, + 29, + 105, + 17, + 105, + 36, + 126, + 118, + 119, + ], + "type": "Buffer", + }, + }, + "initializationHash": Fr { + "asBigInt": 5411687428194306823799197943510367074755080310726877398642044502900791103391n, + "asBuffer": { + "data": [ + 11, + 246, + 232, + 18, + 241, + 75, + 176, + 41, + 247, + 203, + 156, + 141, + 168, + 54, + 125, + 217, + 124, + 6, + 142, + 120, + 141, + 79, + 33, + 0, + 127, + 217, + 112, + 20, + 235, + 168, + 207, + 159, + ], + "type": "Buffer", + }, + }, + "portalContractAddress": EthAddress { + "buffer": { + "data": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + }, + "publicKeysHash": Fr { + "asBigInt": 17954372136461894568820199868962143715279932924957662285438325406238856880109n, + "asBuffer": { + "data": [ + 39, + 177, + 208, + 131, + 154, + 91, + 35, + 186, + 241, + 42, + 141, + 25, + 91, + 24, + 172, + 40, + 143, + 207, + 64, + 26, + 251, + 47, + 112, + 184, + 164, + 181, + 41, + 237, + 229, + 250, + 159, + 237, + ], + "type": "Buffer", + }, + }, + "salt": Fr { + "asBigInt": 1n, + "asBuffer": { + "data": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + ], + "type": "Buffer", + }, + }, + "version": 1, + }, +} +`; diff --git a/yarn-project/protocol-contracts/src/instance-deployer/artifact.ts b/yarn-project/protocol-contracts/src/instance-deployer/artifact.ts new file mode 100644 index 00000000000..3ae928ab045 --- /dev/null +++ b/yarn-project/protocol-contracts/src/instance-deployer/artifact.ts @@ -0,0 +1,8 @@ +import { loadContractArtifact } from '@aztec/types/abi'; +import { NoirCompiledContract } from '@aztec/types/noir'; + +import ContractInstanceDeployerJson from '../artifacts/ContractInstanceDeployer.json' assert { type: 'json' }; + +export const ContractInstanceDeployerArtifact = loadContractArtifact( + ContractInstanceDeployerJson as NoirCompiledContract, +); diff --git a/yarn-project/protocol-contracts/src/instance-deployer/index.test.ts b/yarn-project/protocol-contracts/src/instance-deployer/index.test.ts new file mode 100644 index 00000000000..463d2b42e2a --- /dev/null +++ b/yarn-project/protocol-contracts/src/instance-deployer/index.test.ts @@ -0,0 +1,15 @@ +import omit from 'lodash.omit'; + +import { InstanceDeployerAddress, getCanonicalInstanceDeployer } from './index.js'; + +describe('InstanceDeployer', () => { + it('returns canonical protocol contract', () => { + // TODO(@spalladino): Consider sorting functions by selector when constructing the contract + // class, or even better, when calling loadContractArtifact from the Noir output. + const contract = getCanonicalInstanceDeployer(); + contract.contractClass.privateFunctions.sort((a, b) => a.selector.value - b.selector.value); + contract.contractClass.publicFunctions.sort((a, b) => a.selector.value - b.selector.value); + expect(omit(contract, 'artifact')).toMatchSnapshot(); + expect(contract.address.toString()).toEqual(InstanceDeployerAddress.toString()); + }); +}); diff --git a/yarn-project/protocol-contracts/src/instance-deployer/index.ts b/yarn-project/protocol-contracts/src/instance-deployer/index.ts new file mode 100644 index 00000000000..9605acf2def --- /dev/null +++ b/yarn-project/protocol-contracts/src/instance-deployer/index.ts @@ -0,0 +1,13 @@ +import { AztecAddress } from '@aztec/circuits.js'; + +import { ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js'; +import { ContractInstanceDeployerArtifact } from './artifact.js'; + +/** Returns the canonical deployment of the instance deployer contract. */ +export function getCanonicalInstanceDeployer(): ProtocolContract { + return getCanonicalProtocolContract(ContractInstanceDeployerArtifact, 1); +} + +export const InstanceDeployerAddress = AztecAddress.fromString( + '0x1799c61aa10430bf6fec46679c4cb76c3ed12cd8b6e73ed7389d5ae296ad1b97', +); diff --git a/yarn-project/protocol-contracts/src/protocol_contract.ts b/yarn-project/protocol-contracts/src/protocol_contract.ts new file mode 100644 index 00000000000..970c4878f8a --- /dev/null +++ b/yarn-project/protocol-contracts/src/protocol_contract.ts @@ -0,0 +1,33 @@ +import { AztecAddress, getContractClassFromArtifact, getContractInstanceFromDeployParams } from '@aztec/circuits.js'; +import { ContractArtifact } from '@aztec/foundation/abi'; +import { Fr } from '@aztec/foundation/fields'; +import { ContractClassWithId, ContractInstance } from '@aztec/types/contracts'; + +/** Represents a canonical contract in the protocol. */ +export interface ProtocolContract { + /** Canonical deployed instance. */ + instance: ContractInstance; + /** Contract class of this contract. */ + contractClass: ContractClassWithId; + /** Complete contract artifact. */ + artifact: ContractArtifact; + /** Deployment address for the canonical instance. */ + address: AztecAddress; +} + +/** Returns the canonical deployment a given artifact. */ +export function getCanonicalProtocolContract( + artifact: ContractArtifact, + salt: Fr | number | bigint, + initArgs: any[] = [], +): ProtocolContract { + // TODO(@spalladino): This computes the contract class from the artifact twice. + const contractClass = getContractClassFromArtifact(artifact); + const instance = getContractInstanceFromDeployParams(artifact, initArgs, new Fr(salt)); + return { + instance, + contractClass, + artifact, + address: instance.address, + }; +} diff --git a/yarn-project/protocol-contracts/tsconfig.json b/yarn-project/protocol-contracts/tsconfig.json new file mode 100644 index 00000000000..01c876235ce --- /dev/null +++ b/yarn-project/protocol-contracts/tsconfig.json @@ -0,0 +1,20 @@ +{ + "extends": "..", + "compilerOptions": { + "outDir": "dest", + "rootDir": "src", + "tsBuildInfoFile": ".tsbuildinfo" + }, + "references": [ + { + "path": "../circuits.js" + }, + { + "path": "../foundation" + }, + { + "path": "../types" + } + ], + "include": ["src", "src/**/*.json"] +} diff --git a/yarn-project/tsconfig.json b/yarn-project/tsconfig.json index 35b0d6c76b6..3f7c45f343c 100644 --- a/yarn-project/tsconfig.json +++ b/yarn-project/tsconfig.json @@ -39,6 +39,7 @@ { "path": "noir-protocol-circuits/tsconfig.json" }, { "path": "p2p/tsconfig.json" }, { "path": "p2p-bootstrap/tsconfig.json" }, + { "path": "protocol-contracts/tsconfig.json" }, { "path": "prover-client/tsconfig.json" }, { "path": "sequencer-client/tsconfig.json" }, { "path": "types/tsconfig.json" }, diff --git a/yarn-project/yarn.lock b/yarn-project/yarn.lock index 3555e8cfda0..0a8a36a37c3 100644 --- a/yarn-project/yarn.lock +++ b/yarn-project/yarn.lock @@ -97,6 +97,7 @@ __metadata: "@aztec/foundation": "workspace:^" "@aztec/kv-store": "workspace:^" "@aztec/l1-artifacts": "workspace:^" + "@aztec/protocol-contracts": "workspace:^" "@aztec/types": "workspace:^" "@jest/globals": ^29.5.0 "@types/debug": ^4.1.7 @@ -707,6 +708,28 @@ __metadata: languageName: unknown linkType: soft +"@aztec/protocol-contracts@workspace:^, @aztec/protocol-contracts@workspace:protocol-contracts": + version: 0.0.0-use.local + resolution: "@aztec/protocol-contracts@workspace:protocol-contracts" + dependencies: + "@aztec/circuits.js": "workspace:^" + "@aztec/foundation": "workspace:^" + "@aztec/types": "workspace:^" + "@jest/globals": ^29.5.0 + "@types/jest": ^29.5.0 + "@types/lodash.omit": ^4.5.9 + "@types/node": ^18.7.23 + jest: ^29.5.0 + jest-mock-extended: ^3.0.3 + lodash.omit: ^4.5.0 + ts-jest: ^29.1.0 + ts-loader: ^9.4.4 + ts-node: ^10.9.1 + tslib: ^2.4.0 + typescript: ^5.0.4 + languageName: unknown + linkType: soft + "@aztec/prover-client@workspace:prover-client": version: 0.0.0-use.local resolution: "@aztec/prover-client@workspace:prover-client" @@ -3434,7 +3457,7 @@ __metadata: languageName: node linkType: hard -"@types/lodash.omit@npm:^4.5.7": +"@types/lodash.omit@npm:^4.5.7, @types/lodash.omit@npm:^4.5.9": version: 4.5.9 resolution: "@types/lodash.omit@npm:4.5.9" dependencies: