Skip to content

Commit

Permalink
chore: Just nargo compile. (#3775)
Browse files Browse the repository at this point in the history
Ok. Don't be scared. 262 files sounds horrific but it's mostly path
updates and that kind of nonsense. The `noir-contracts` and
`noir-compiler` changes are probably what to focus on.

* We update boxes to use our build of nargo, and the modified
code-generator.
* We update paths in docs, as noir-contracts/src/contracts moved to
noir-contracts/contracts, as src is now pure codegen output.
* Contracts are now imported e.g. `import { ChildContractArtifact } from
'@aztec/noir-contracts/Child';`. You can still just import from top
level index, but it's pretty cruel to ask the runtime to parse all the
artifacts just to get one, they are huge.
* Contract files are now just named as per the name of the contract
(i.e. not snake case). Less moving parts is better here. Given it's
codegen output it's acceptable to allow the output names to be
inconsistent.
* aztec.js is now responsible for copying the account contracts into
itself, as opposed to have some other random module push code into it.
But we just need to get rid of this baked account stuff at some point
anyway.
* Got rid of lodash.zip in one place, and then restrained myself to not
go further. But think we should remove the "trivial" lodash cases at
some point.
* Tidied up yp/bootstrap a bit, it's basically in line with the
dockerfile at this point. Will prob make dockerfile just call bootstrap
as part of some other docker cleanup I'll do later.
* `source-map-support` in cli.
* Remove compile command from cli. We are just going to promote use of
aztec-nargo.
* The ts and noir generators now expect nargo output as input, rather
than our transformed abi. The ts generator outputs the transformed abi
as part of it's generation.
* Delete all the script stuff from `noir-contracts`. src folder is now
just the codegen output, and the codegen is done with a trivial script
to call compile and the ts generator in noir-compiler.
* Added an unused script called `transform_json_abi.sh` that uses a tiny
bit of jq to perform the transform. Probably to be deleted, especially
if we just stop transforming the noir output and use it directly, but it
served me as a useful tool at one point.
  • Loading branch information
charlielye authored Jan 3, 2024
1 parent c58b197 commit 3d08ef9
Show file tree
Hide file tree
Showing 262 changed files with 902 additions and 1,485 deletions.
3 changes: 2 additions & 1 deletion aztec-up/bin/aztec-up
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env bash
set -euo pipefail

export VERSION=${1:-$VERSION}
export SKIP_TITLE=1
bash -i <(curl -s http://install.aztec.network)
bash -i <(curl -s http://install.aztec.network)
7 changes: 5 additions & 2 deletions boxes/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Builds the boxes (they were copied into yarn-project-base so the cli can unbox).
# Produces a container that can be run to test a specific box. See docker-compose.yml.
FROM 278380418400.dkr.ecr.eu-west-2.amazonaws.com/aztec-sandbox AS aztec-sandbox
FROM 278380418400.dkr.ecr.eu-west-2.amazonaws.com/noir as noir

# We need yarn. Start fresh container.
FROM node:18.19.0
COPY --from=aztec-sandbox /usr/src /usr/src
COPY --from=noir /usr/src/noir/target/release/nargo /usr/src/noir/target/release/nargo
WORKDIR /usr/src/boxes
ENV AZTEC_CLI=../../yarn-project/cli/aztec-cli-dest
ENV AZTEC_NARGO=/usr/src/noir/target/release/nargo
ENV AZTEC_CLI=/usr/src/yarn-project/cli/aztec-cli-dest
RUN yarn && yarn build
ENTRYPOINT ["yarn", "workspace"]
ENTRYPOINT ["yarn", "workspace"]
7 changes: 4 additions & 3 deletions boxes/blank-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"type": "module",
"main": "./dest/index.js",
"scripts": {
"build": "yarn clean && yarn compile && tsc -b && webpack",
"clean": "rm -rf ./dest .tsbuildinfo",
"build": "yarn clean && yarn compile && yarn codegen && tsc -b && webpack",
"clean": "rm -rf ./dest .tsbuildinfo ./src/artifacts ./src/contracts/target",
"start": "serve -p 3000 ./dest",
"start:dev": "webpack serve --mode=development",
"formatting": "prettier --check ./src && eslint ./src",
"formatting:fix": "prettier -w ./src",
"compile": "${AZTEC_CLI:-aztec-cli} compile src/contracts --outdir ../artifacts --typescript ../artifacts",
"compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile",
"codegen": "${AZTEC_CLI:-aztec-cli} generate-typescript src/contracts/target --outdir src/artifacts",
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --runInBand"
},
"jest": {
Expand Down
2 changes: 1 addition & 1 deletion boxes/blank-react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"skipLibCheck": true,
"jsx": "react-jsx"
},
"include": ["src", "src/**/*.json"]
"include": ["src", "src/artifacts/Blank.json"]
}
7 changes: 4 additions & 3 deletions boxes/blank/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"type": "module",
"main": "./dest/index.js",
"scripts": {
"build": "yarn clean && yarn compile && tsc -b && webpack",
"clean": "rm -rf ./dest .tsbuildinfo",
"build": "yarn clean && yarn compile && yarn codegen && tsc -b && webpack",
"clean": "rm -rf ./dest .tsbuildinfo ./src/artifacts ./src/contracts/target",
"start": "serve -p 3000 ./dest",
"start:dev": "webpack serve --mode=development",
"formatting": "prettier --check ./src && eslint ./src",
"formatting:fix": "prettier -w ./src",
"compile": "${AZTEC_CLI:-aztec-cli} compile src/contracts --outdir ../artifacts --typescript ../artifacts",
"compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile",
"codegen": "${AZTEC_CLI:-aztec-cli} generate-typescript src/contracts/target --outdir src/artifacts",
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --runInBand"
},
"jest": {
Expand Down
2 changes: 1 addition & 1 deletion boxes/blank/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"skipLibCheck": true,
"jsx": "react-jsx"
},
"include": ["src", "src/**/*.json"]
"include": ["src", "src/artifacts/Blank.json"]
}
7 changes: 4 additions & 3 deletions boxes/token/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"type": "module",
"main": "./dest/index.js",
"scripts": {
"build": "yarn clean && yarn compile && tsc -b && webpack",
"clean": "rm -rf ./dest .tsbuildinfo",
"build": "yarn clean && yarn compile && yarn codegen && tsc -b && webpack",
"clean": "rm -rf ./dest .tsbuildinfo ./src/artifacts ./src/contracts/target",
"start": "serve -p 3000 ./dest",
"start:dev": "webpack serve --mode=development",
"formatting": "prettier --check ./src && eslint ./src",
"formatting:fix": "prettier -w ./src",
"compile": "${AZTEC_CLI:-aztec-cli} compile src/contracts --outdir ../artifacts --typescript ../artifacts",
"compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile",
"codegen": "${AZTEC_CLI:-aztec-cli} generate-typescript src/contracts/target --outdir src/artifacts",
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --runInBand"
},
"jest": {
Expand Down
2 changes: 1 addition & 1 deletion boxes/token/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"skipLibCheck": true,
"jsx": "react-jsx"
},
"include": ["src", "src/**/*.json"]
"include": ["src", "src/artifacts/Token.json"]
}
1 change: 1 addition & 0 deletions build_manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ boxes:
buildDir: boxes
dependencies:
- aztec-sandbox
- noir
runDependencies:
- aztec-sandbox

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/concepts/foundation/accounts/keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Since Aztec implements full [signature abstraction](./main.md), signing keys dep

This is a snippet of our Schnorr Account contract implementation, which uses Schnorr signatures for authentication:

#include_code entrypoint /yarn-project/noir-contracts/src/contracts/schnorr_account_contract/src/main.nr rust
#include_code entrypoint /yarn-project/noir-contracts/contracts/schnorr_account_contract/src/main.nr rust

Still, different accounts may use different signing schemes, may require multi-factor authentication, or _may not even use signing keys_ and instead rely on other authentication mechanisms. Read [how to write an account contract](../../../dev_docs/wallets/writing_an_account_contract.md) for a full example of how to manage authentication.

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/dev_docs/cli/cli-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export ADDRESS2=<Account address printed by the above command>

## Deploying a Token Contract

We will now deploy a token contract using the `deploy` command, and set an address of the admin via a constructor argument. You can find the contract we are deploying [here](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr) (or write it for yourself in [this tutorial!](../tutorials/writing_token_contract.md))
We will now deploy a token contract using the `deploy` command, and set an address of the admin via a constructor argument. You can find the contract we are deploying [here](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/noir-contracts/contracts/token_contract/src/main.nr) (or write it for yourself in [this tutorial!](../tutorials/writing_token_contract.md))
Make sure to replace this address with one of the two you created earlier.

#include_code deploy yarn-project/end-to-end/src/cli_docs_sandbox.test.ts bash
Expand Down Expand Up @@ -123,7 +123,7 @@ The `send` command expect the function name as the first unnamed argument and th

#include_code send yarn-project/end-to-end/src/cli_docs_sandbox.test.ts bash

We called the [`mint_public`](https://github.com/AztecProtocol/aztec-packages/blob/87fa621347e55f82e36c70515c1824161eee5282/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr#L157C10-L157C10) function and provided it with the 2 arguments it expects: the recipient's address and the amount to be minted. Make sure to replace all addresses in this command with yours.
We called the [`mint_public`](https://github.com/AztecProtocol/aztec-packages/blob/87fa621347e55f82e36c70515c1824161eee5282/yarn-project/noir-contracts/contracts/token_contract/src/main.nr#L157C10-L157C10) function and provided it with the 2 arguments it expects: the recipient's address and the amount to be minted. Make sure to replace all addresses in this command with yours.

The command output tells us the details of the transaction such as its hash and status. We can use this hash to query the receipt of the transaction at a later time:

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/dev_docs/cli/sandbox-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ UniswapContractArtifact

> <sup><sub><a href="https://github.com/AztecProtocol/aztec-packages/blob/master//yarn-project/end-to-end/src/cli_docs_sandbox.test.ts#L95-L118" target="_blank" rel="noopener noreferrer">Source code: /yarn-project/end-to-end/src/cli_docs_sandbox.test.ts#L95-L118</a></sub></sup>
You can see all of our example contracts in the monorepo [here](https://github.com/AztecProtocol/aztec-packages/tree/master/yarn-project/noir-contracts/src/contracts).
You can see all of our example contracts in the monorepo [here](https://github.com/AztecProtocol/aztec-packages/tree/master/yarn-project/noir-contracts/contracts).

## Boxes

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/dev_docs/contracts/compiling.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ You can also generate these interfaces from prebuilt artifacts using the `genera
aztec-cli generate-typescript ./path/to/my_aztec_contract_project
```

Below is typescript code generated from the [Token](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr) contract:
Below is typescript code generated from the [Token](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/noir-contracts/contracts/token_contract/src/main.nr) contract:

```ts showLineNumbers
export class TokenContract extends ContractBase {
Expand Down Expand Up @@ -145,7 +145,7 @@ You can also generate these interfaces from prebuilt artifacts using the `genera
aztec-cli generate-noir-interface ./path/to/my_aztec_contract_project
```

Below is an example interface, also generated from the [Token](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr) contract:
Below is an example interface, also generated from the [Token](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/noir-contracts/contracts/token_contract/src/main.nr) contract:

```rust
impl TokenPrivateContextInterface {
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/dev_docs/contracts/example-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: Example Aztec.nr Contract

In keeping with the origins of blockchain, here's an example of a simple private token contract. Everyone's balances are private.

#include_code easy_private_token_contract /yarn-project/noir-contracts/src/contracts/easy_private_token_contract/src/main.nr rust
#include_code easy_private_token_contract /yarn-project/noir-contracts/contracts/easy_private_token_contract/src/main.nr rust

:::info Disclaimer
Please note that any example contract set out herein is provided solely for informational purposes only and does not constitute any inducement to use or deploy. Any implementation of any such contract with an interface or any other infrastructure should be used in accordance with applicable laws and regulations.
Expand Down
10 changes: 5 additions & 5 deletions docs/docs/dev_docs/contracts/portals/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ Note that while the `secret` and the `content` are both hashed, they are actuall

#include_code context_consume_l1_to_l2_message /yarn-project/aztec-nr/aztec/src/context.nr rust

Computing the `content` must be done manually in its current form, as we are still adding a number of bytes utilities. A good example exists within the [Token bridge example](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/util.nr).
Computing the `content` must be done manually in its current form, as we are still adding a number of bytes utilities. A good example exists within the [Token bridge example](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/noir-contracts/contracts/token_bridge_contract/src/util.nr).

#include_code claim_public /yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr rust
#include_code claim_public /yarn-project/noir-contracts/contracts/token_bridge_contract/src/main.nr rust

:::info
The `content_hash` is a sha256 truncated to a field element (~ 254 bits). In Aztec-nr, you can use our `sha256_to_field()` to do a sha256 hash which fits in one field element
:::

#include_code mint_public_content_hash_nr /yarn-project/noir-contracts/src/contracts/token_portal_content_hash_lib/src/lib.nr rust
#include_code mint_public_content_hash_nr /yarn-project/noir-contracts/contracts/token_portal_content_hash_lib/src/lib.nr rust

In Solidity, you can use our `Hash.sha256ToField()` method:

Expand Down Expand Up @@ -88,7 +88,7 @@ Access control on the L1 portal contract is essential to prevent consumption of

As earlier, we can use a token bridge as an example. In this case, we are burning tokens on L2 and sending a message to the portal to free them on L1.

#include_code exit_to_l1_private yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr rust
#include_code exit_to_l1_private yarn-project/noir-contracts/contracts/token_bridge_contract/src/main.nr rust

When the transaction is included in a rollup block the message will be inserted into the `Outbox`, where the recipient portal can consume it from. When consuming, the `msg.sender` must match the `recipient` meaning that only portal can actually consume the message.

Expand Down Expand Up @@ -202,4 +202,4 @@ Designated callers are enforced at the contract level for contracts that are not

- Token bridge (Portal contract built for L1 -> L2, i.e., a non-native L2 asset)
- [Portal contract](https://github.com/AztecProtocol/aztec-packages/blob/master/l1-contracts/test/portals/TokenPortal.sol)
- [Aztec contract](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr)
- [Aztec contract](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/noir-contracts/contracts/token_bridge_contract/src/main.nr)
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ This function computes the message hash, and then forwards the call to the more

#### Example

#include_code assert_current_call_valid_authwit /yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust
#include_code assert_current_call_valid_authwit /yarn-project/noir-contracts/contracts/token_contract/src/main.nr rust

### Utilities for public calls

Very similar to the above, we have variations that work in the public domain. These functions are wrapped to give a similar flow for both cases, but behind the scenes the logic of the account contracts is slightly different since they cannot use the oracle as they are not in the private domain.

#### Example

#include_code assert_current_call_valid_authwit_public /yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust
#include_code assert_current_call_valid_authwit_public /yarn-project/noir-contracts/contracts/token_contract/src/main.nr rust

## Usage

Expand All @@ -136,15 +136,15 @@ authwit = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#includ

Then you will be able to import it into your contracts as follows.

#include_code import_authwit /yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust
#include_code import_authwit /yarn-project/noir-contracts/contracts/token_contract/src/main.nr rust

### Private Functions

#### Checking if the current call is authenticated

Based on the diagram earlier on this page let's take a look at how we can implement the `transfer` function such that it checks if the tokens are to be transferred `from` the caller or needs to be authenticated with an authentication witness.

#include_code transfer /yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust
#include_code transfer /yarn-project/noir-contracts/contracts/token_contract/src/main.nr rust

The first thing we see in the snippet above, is that if `from` is not the call we are calling the `assert_current_call_valid_authwit` function from [earlier](#private-functions). If the call is not throwing, we are all good and can continue with the transfer.

Expand All @@ -162,7 +162,7 @@ With private functions covered, how can we use this in a public function? Well,

#### Checking if the current call is authenticated

#include_code transfer_public /yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust
#include_code transfer_public /yarn-project/noir-contracts/contracts/token_contract/src/main.nr rust

#### Authenticating an action in TypeScript

Expand All @@ -178,13 +178,13 @@ We have cases where we need a non-wallet contract to approve an action to be exe

To support this, we must implement the `is_valid_public` function as seen in the snippet below.

#include_code authwit_uniswap_get /yarn-project/noir-contracts/src/contracts/uniswap_contract/src/main.nr rust
#include_code authwit_uniswap_get /yarn-project/noir-contracts/contracts/uniswap_contract/src/main.nr rust

It also needs a way to update those storage values. Since we want the updates to be trustless, we can compute the action based on the function inputs, and then have the contract compute the key at which it must add a `true` to approve the action.

An example of this would be our Uniswap example which performs a cross chain swap on L1. In here, we both do private and public auth witnesses, where the public is set by the uniswap L2 contract itself. In the below snippet, you can see that we compute the action hash, and then update an `approved_action` mapping with the hash as key and `true` as value. When we then call the `token_bridge` to execute afterwards, it reads this value, burns the tokens, and consumes the authentication.

#include_code authwit_uniswap_set /yarn-project/noir-contracts/src/contracts/uniswap_contract/src/main.nr rust
#include_code authwit_uniswap_set /yarn-project/noir-contracts/contracts/uniswap_contract/src/main.nr rust

Outlining more of the `swap` flow: this simplified diagram shows how it will look for contracts that are not wallets but also need to support authentication witnesses.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ contract Bridge {
amount: Field,
) -> Field {
...
#include_code call_assert_token_is_same /yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr raw
#include_code call_assert_token_is_same /yarn-project/noir-contracts/contracts/token_bridge_contract/src/main.nr raw
}
#include_code assert_token_is_same /yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr raw
#include_code assert_token_is_same /yarn-project/noir-contracts/contracts/token_bridge_contract/src/main.nr raw
}
```
:::danger
Expand Down Expand Up @@ -102,11 +102,11 @@ Hence, it's necessary to add a "randomness" field to your note to prevent such a
Currently, if you have storage defined, the compiler will error if you don't have a `compute_note_hash_and_nullifier()` defined. Without this, the PXE can't process encrypted events and discover your notes.

If your contract doesn't have anything to do with notes (e.g. operates solely in the public domain), you can do the following:
#include_code compute_note_hash_and_nullifier_placeholder /yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr rust
#include_code compute_note_hash_and_nullifier_placeholder /yarn-project/noir-contracts/contracts/token_bridge_contract/src/main.nr rust

Otherwise, you need this method to help the PXE with processing your notes. In our [demo token contract](../../../tutorials/writing_token_contract.md#compute_note_hash_and_nullifier), we work with 2 kinds of notes: `ValueNote` and `TransparentNote`. Hence this method must define how to work with both:

#include_code compute_note_hash_and_nullifier /yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust
#include_code compute_note_hash_and_nullifier /yarn-project/noir-contracts/contracts/token_contract/src/main.nr rust

### L1 -- L2 interactions
Refer to [Token Portal tutorial on bridging tokens between L1 and L2](../../../tutorials/token_portal/main.md) and/or [Uniswap tutorial that shows how to swap on L1 using funds on L2](../../../tutorials/uniswap/main.md). Both examples show how to:
Expand Down
Loading

0 comments on commit 3d08ef9

Please sign in to comment.