Skip to content

Commit

Permalink
Merge branch 'main' into feat/body-parsing-with-audit-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraSuegami committed Sep 10, 2024
2 parents 30e99de + 38d9a4b commit 5396607
Show file tree
Hide file tree
Showing 49 changed files with 3,554 additions and 1,005 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-test-fmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly-cafc2606a2187a42b236df4aa65f4e8cdfcea970
version: nightly-0079a1146b79a4aeda58b0258215bedb1f92700b

- name: Run tests
working-directory: packages/contracts
run: forge build
run: yarn build

- name: Free Disk Space (Ubuntu)
uses: jlumbroso/free-disk-space@main
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- name: Install Foundry
uses: foundry-rs/[email protected]
with:
version: nightly-cafc2606a2187a42b236df4aa65f4e8cdfcea970
version: nightly-0079a1146b79a4aeda58b0258215bedb1f92700b

- name: Run tests
working-directory: packages/contracts
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,4 @@ book

# For zksync
zkout
.cache
7 changes: 0 additions & 7 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ First, install foundry by running the following command:
curl -L https://foundry.paradigm.xyz | bash
```

Then, install the specific version of foundry by running the following command:
Note: The latest version of foundry fails some tests.

```sh
foundryup -v nightly-cafc2606a2187a42b236df4aa65f4e8cdfcea970
```

## Clone the repository

```sh
Expand Down
110 changes: 55 additions & 55 deletions packages/contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Then, move `email_auth.zkey` and `email_auth.wasm` in the unzipped directory `pa

Run each integration tests **one by one** as each test will consume lot of memory.
```bash
Eg: forge test --match-test 'testIntegration_Account_Recovery' -vvv --chain 8453 --ffi
Eg: contracts % forge test --skip '*ZKSync*' --match-contract "IntegrationTest" -vvv --chain 8453 --ffi
```
#### Deploy Common Contracts.
You need to deploy common contracts, i.e., `ECDSAOwnedDKIMRegistry`, `Verifier`, and implementations of `EmailAuth` and `SimpleWallet`, only once before deploying each wallet.
Expand All @@ -42,9 +42,9 @@ You need to deploy common contracts, i.e., `ECDSAOwnedDKIMRegistry`, `Verifier`,
4. `forge script script/DeployCommons.s.sol:Deploy --rpc-url $RPC_URL --chain-id $CHAIN_ID --etherscan-api-key $ETHERSCAN_API_KEY --broadcast --verify -vvvv`

#### Deploy Each Wallet.
After deploying common contracts, you can deploy a proxy contract of `SimpleWallet`, which is an example contract supporting our email-based account recovery.
After deploying common contracts, you can deploy a proxy contract of `SimpleWallet`, which is an example contract supporting our email-based account recovery by `RecoveryController`.
1. Check that the env values of `DKIM`, `VERIFIER`, `EMAIL_AUTH_IMPL`, and `SIMPLE_WALLET_IMPL` are the same as those output by the `DeployCommons.s.sol` script.
2. `forge script script/DeploySimpleWallet.s.sol:Deploy --rpc-url $RPC_URL --chain-id $CHAIN_ID --broadcast -vvvv`
2. `forge script script/DeployRecoveryController.s.sol:Deploy --rpc-url $RPC_URL --chain-id $CHAIN_ID --broadcast -vvvv`

## Specification
There are four main contracts that developers should understand: `IDKIMRegistry`, `Verifier`, `EmailAuth` and `EmailAccountRecovery`.
Expand Down Expand Up @@ -251,62 +251,40 @@ Next, you should uncomment the following lines in `foundry.toml`.
# via-ir = true
```

And then you should uncomment the following lines in `src/EmailAccountRecovery.sol`.
Partial comment-out files can be found the following. Please uncomment them.
(Uncomment from `FOR_ZKSYNC:START` to `FOR_ZKSYNC:END`)

```
// import {SystemContractsCaller} from "@matterlabs/zksync-contracts/l2/system-contracts/libraries/SystemContractsCaller.sol";
// import {DEPLOYER_SYSTEM_CONTRACT} from "@matterlabs/zksync-contracts/l2/system-contracts/Constants.sol";
```

And lines 229 - 263 in the `handleAcceptance` function too.
- src/utils/ZKSyncCreate2Factory.sol
- test/helpers/DeploymentHelper.sol

At the first forge build, you got the following warning like the following.
At the first forge build, you need to detect the missing libraries.

```
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Warning: Your code or one of its dependencies uses the 'extcodesize' instruction, which is │
│ usually needed in the following cases: │
│ 1. To detect whether an address belongs to a smart contract. │
│ 2. To detect whether the deploy code execution has finished. │
│ zkSync Era comes with native account abstraction support (so accounts are smart contracts, │
│ including private-key controlled EOAs), and you should avoid differentiating between contracts │
│ and non-contract addresses. │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
--> ../../node_modules/forge-std/src/StdCheats.sol
Failed to compile with zksolc: Missing libraries detected [ZkMissingLibrary { contract_name: "SubjectUtils", contract_path: "src/libraries/SubjectUtils.sol", missing_libraries: ["src/libraries/DecimalUtils.sol:DecimalUtils"] }, ZkMissingLibrary { contract_name: "DecimalUtils", contract_path: "src/libraries/DecimalUtils.sol", missing_libraries: [] }]
forge build --zksync --zk-detect-missing-libraries
```

Please run the following command in order to deploy the missing libraries:
As you saw before, you need to deploy missing libraries.
You can deploy them by the following command for example.

```
forge create --deploy-missing-libraries --private-key <PRIVATE_KEY> --rpc-url <RPC_URL> --chain <CHAIN_ID> --zksync
forge create --deploy-missing-libraries --private-key {YOUR_PRIVATE_KEY} --rpc-url https://sepolia.era.zksync.dev --chain 300 --zksync
$ forge build --zksync --zk-detect-missing-libraries
Missing libraries detected: src/libraries/SubjectUtils.sol:SubjectUtils, src/libraries/DecimalUtils.sol:DecimalUtils
```

The above command output the following(for example):
Run the following command in order to deploy each missing library:

```
[⠊] Compiling...
No files changed, compilation skipped
Deployer: 0xfB1CcCBDa2C41a77cDAC448641006Fc7fcf1f3b9
Deployed to: 0x91cc0f0A227b8dD56794f9391E8Af48B40420A0b
Transaction hash: 0x4f94ab71443d01988105540c3abb09ed66f8af5d0bb6a88691e2dafa88b3583d
[⠢] Compiling...
[⠃] Compiling 68 files with 0.8.26
[⠆] Solc 0.8.26 finished in 12.20s
Compiler run successful!
Deployer: 0xfB1CcCBDa2C41a77cDAC448641006Fc7fcf1f3b9
Deployed to: 0x981E3Df952358A57753C7B85dE7949Da4aBCf54A
Transaction hash: 0xfdca7b9eb3ae933ca123111489572427ee95eb6be74978b24c73fe74cb4988d7
forge create src/libraries/DecimalUtils.sol:DecimalUtils --private-key {YOUR_PRIVATE_KEY} --rpc-url https://sepolia.era.zksync.dev --chain 300 --zksync
forge create src/libraries/SubjectUtils.sol:SubjectUtils --private-key {YOUR_PRIVATE_KEY} --rpc-url https://sepolia.era.zksync.dev --chain 300 --zksync --libraries src/libraries/DecimalUtils.sol:DecimalUtils:{DECIMAL_UTILS_DEPLOYED_ADDRESS}
```

After that, you can see the following line in foundry.toml.
Also, this line is needed only for foundry-zksync, if you use foundry, please remove this line. Otherwise, the test will fail.

```
libraries = ["{PROJECT_DIR}/packages/contracts/src/libraries/DecimalUtils.sol:DecimalUtils:{DEPLOYED_ADDRESS}", "{PROJECT_DIR}/packages/contracts/src/libraries/SubjectUtils.sol:SubjectUtils:{DEPLOYED_ADDRESS}"]
libraries = [
"{PROJECT_DIR}/packages/contracts/src/libraries/DecimalUtils.sol:DecimalUtils:{DEPLOYED_ADDRESS}",
"{PROJECT_DIR}/packages/contracts/src/libraries/SubjectUtils.sol:SubjectUtils:{DEPLOYED_ADDRESS}"]
```

Incidentally, the above line already exists in `foundy.toml` with it commented out, if you uncomment it by replacing `{PROJECT_DIR}` with the appropriate path, it will also work.
Expand Down Expand Up @@ -336,7 +314,7 @@ https://github.com/matter-labs/foundry-zksync/issues/382

Failing test cases are here.

EmailAuthWithUserOverrideableDkim.t.sol
DKIMRegistryUpgrade.t.sol

- testAuthEmail()

Expand All @@ -348,25 +326,47 @@ EmailAuth.t.sol
- testExpectRevertAuthEmailInvalidSubject()
- testExpectRevertAuthEmailInvalidTimestamp()

DeployCommons.t.sol
EmailAuthWithUserOverrideableDkim.t.sol

- testAuthEmail()

- test_run()
# For integration testing

DeployRecoveryController.t.sol
To pass the instegration testing, you should use era-test-node.
See the following URL and install it.
https://github.com/matter-labs/era-test-node

- test_run()
Run the era-test-node

DeploySimpleWallet.t.sol
```
era_test_node fork https://sepolia.era.zksync.dev
```

- test_run()
- test_run_no_dkim()
- test_run_no_email_auth()
- test_run_no_simple_wallet()
- test_run_no_verifier()
You remove .zksolc-libraries-cache directory, and run the following command.

# For integration testing
```
forge build --zksync --zk-detect-missing-libraries
```

As you saw before, you need to deploy missing libraries.
You can deploy them by the following command for example.

```
Missing libraries detected: src/libraries/SubjectUtils.sol:SubjectUtils, src/libraries/DecimalUtils.sol:DecimalUtils
Run the following command in order to deploy each missing library:
forge test --match-test 'testIntegration_Account_Recovery' --zksync --chain 300 -vvv --ffi
forge create src/libraries/DecimalUtils.sol:DecimalUtils --private-key {YOUR_PRIVATE_KEY} --rpc-url http://127.0.0.1:8011 --chain 260 --zksync
forge create src/libraries/SubjectUtils.sol:SubjectUtils --private-key {YOUR_PRIVATE_KEY} --rpc-url http://127.0.0.1:8011 --chain 260 --zksync --libraries src/libraries/DecimalUtils.sol:DecimalUtils:{DECIMAL_UTILS_DEPLOYED_ADDRESS}
```

Set the libraries in foundry.toml using the above deployed address.

And then, run the integration testing.

```
forge test --match-contract "IntegrationZkSyncTest" --system-mode=true --zksync --gas-limit 1000000000 --chain 300 -vvv --ffi
```

# For zkSync deployment (For test net)

Expand All @@ -375,6 +375,6 @@ Second just run the following commands with `--zksync`

```
source .env
forge script script/DeployCommons.s.sol:Deploy --zksync --rpc-url $SEPOLIA_RPC_URL --broadcast -vvvv
forge script script/DeployRecoveryControllerZKSync.s.sol:Deploy --zksync --rpc-url $RPC_URL --broadcast --slow --via-ir --system-mode true -vvvv
```

6 changes: 3 additions & 3 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"version": "1.0.0",
"license": "MIT",
"scripts": {
"build": "forge build",
"build": "forge build --skip '*ZKSync*'",
"zkbuild": "forge build --zksync",
"test": "forge test --no-match-test \"testIntegration\" --no-match-contract \".*Script.*\"",
"zktest": "forge test --no-match-test \"testIntegration\" --no-match-contract \".*Script.*\" --zksync --chain 300",
"test": "forge test --no-match-test \"testIntegration\" --no-match-contract \".*Script.*\" --skip '*ZKSync*'",
"zktest": "forge test --no-match-test \"testIntegration\" --no-match-contract \".*Script.*\" --system-mode=true --zksync --gas-limit 1000000000 --chain 300",
"lint": "solhint 'src/**/*.sol'"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/script/ChangeOwners.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Own

contract ChangeOwners is Script {
function run() external {

uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
if (deployerPrivateKey == 0) {
console.log("PRIVATE_KEY env var not set");
Expand Down
56 changes: 56 additions & 0 deletions packages/contracts/script/DeployEmailAuthWithCreate2ZKSync.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Script.sol";

import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "../src/EmailAuth.sol";
import {ZKSyncCreate2Factory} from "../src/utils/ZKSyncCreate2Factory.sol";

contract Deploy is Script {
using ECDSA for *;

function run() external {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
if (deployerPrivateKey == 0) {
console.log("PRIVATE_KEY env var not set");
return;
}

vm.startBroadcast(deployerPrivateKey);

EmailAuth emailAuth = new EmailAuth();

address recoveredAccount = address(0x1);
bytes32 accountSalt = 0x0;

ERC1967Proxy proxy = new ERC1967Proxy(
address(emailAuth),
abi.encodeCall(emailAuth.initialize, (recoveredAccount, accountSalt, address(this)))
);
console.log("normal deployed proxyAddress %s", address(proxy));

ZKSyncCreate2Factory factory = new ZKSyncCreate2Factory();
string memory artifact = vm.readFile("zkout/ERC1967Proxy.sol/ERC1967Proxy.json");
bytes32 bytecodeHash = vm.parseJsonBytes32(artifact, ".hash");
console.log("bytecodeHash");
console.logBytes32(bytes32(bytecodeHash));

bytes memory emailAuthInit = abi.encode(
address(emailAuth), abi.encodeCall(EmailAuth.initialize, (recoveredAccount, accountSalt, address(this)))
);

address computedAddress = factory.computeAddress(accountSalt, bytecodeHash, emailAuthInit);
console.log("computedAddress", computedAddress);

(bool success, bytes memory returnData) = factory.deploy(accountSalt, bytecodeHash, emailAuthInit);

address payable proxyAddress = abi.decode(returnData, (address));
console.log("proxyAddress %s", proxyAddress);

EmailAuth emailAuthProxy = EmailAuth(proxyAddress);
console.log("emailAuthProxy.controller() %s", emailAuthProxy.controller());
vm.stopBroadcast();
}
}
Loading

0 comments on commit 5396607

Please sign in to comment.