Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Create2Address.sol. #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions contracts/test/Create2Address.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
pragma solidity ^0.8.17;

import {L2ContractHelper} from '@matterlabs/zksync-contracts/l2/contracts/L2ContractHelper.sol';
import {DEPLOYER_SYSTEM_CONTRACT, IContractDeployer} from '@matterlabs/zksync-contracts/l2/system-contracts/Constants.sol';
import {SystemContractsCaller} from '@matterlabs/zksync-contracts/l2/system-contracts/libraries/SystemContractsCaller.sol';

import {EmailAuth, ERC1967Proxy, Create2} from '../EmailRecoveryManager.sol';

import "hardhat/console.sol";

contract Create2Address {

EmailAuth emailAuth;
bytes32 proxyBytecodeHash;
address deployedProxyAddress;

constructor() {
console.log("constructor");
emailAuth = new EmailAuth();
proxyBytecodeHash = bytes32(0x010000795a7a1b6b550a8127b91748f90a87ac71b8861dce434b11125da32175);
}

function emailAuthImplementation() public view returns (address) {
console.log("emailAuthImplementation");
return address(emailAuth);
}

function chainId() public view returns (uint256) {
return block.chainid;
}

function getDeployedProxyAddress() public view returns (address) {
return deployedProxyAddress;
}

// These function is copied from EmailRecoveryManager.sol.
// There are some little differences for testing, but the logic is the same.

/// @notice Computes the address for email auth contract using the CREATE2 opcode.
/// @dev This function utilizes the `Create2` library to compute the address. The computation uses a provided account address to be recovered, account salt,
/// and the hash of the encoded ERC1967Proxy creation code concatenated with the encoded email auth contract implementation
/// address and the initialization call data. This ensures that the computed address is deterministic and unique per account salt.
/// @param recoveredAccount The address of the account to be recovered.
/// @param accountSalt A bytes32 salt value, which is assumed to be unique to a pair of the guardian's email address and the wallet address to be recovered.
/// @return address The computed address.
function computeEmailAuthAddress(
address recoveredAccount,
bytes32 accountSalt
) public view returns (address) {
// If on zksync, we use L2ContractHelper.computeCreate2Address
// if (block.chainid == 324 || block.chainid == 300) {
// TODO: The bytecodeHash is hardcoded here because type(ERC1967Proxy).creationCode doesn't work on eraVM currently
// If you failed some test cases, check the bytecodeHash by yourself
// see, test/ComputeCreate2Address.t.sol
return
L2ContractHelper.computeCreate2Address(
address(this),
accountSalt,
bytes32(0x010000795a7a1b6b550a8127b91748f90a87ac71b8861dce434b11125da32175),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bytes32(0x010000795a7a1b6b550a8127b91748f90a87ac71b8861dce434b11125da32175),
proxyBytecodeHash

keccak256(
abi.encode(
emailAuthImplementation(),
abi.encodeCall(
EmailAuth.initialize,
(recoveredAccount, accountSalt, address(this))
)
)
)
);
// } else {
// return
// Create2.computeAddress(
// accountSalt,
// keccak256(
// abi.encodePacked(
// type(ERC1967Proxy).creationCode,
// abi.encode(
// emailAuthImplementation(),
// abi.encodeCall(
// EmailAuth.initialize,
// (recoveredAccount, accountSalt, address(this))
// )
// )
// )
// )
// );
// }
}

function deployProxy(address recoveredAccount, bytes32 accountSalt) public returns (address) {
(bool success, bytes memory returnData) = SystemContractsCaller
.systemCallWithReturndata(
uint32(gasleft()),
address(DEPLOYER_SYSTEM_CONTRACT),
uint128(0),
abi.encodeCall(
DEPLOYER_SYSTEM_CONTRACT.create2,
(
accountSalt,
proxyBytecodeHash,
abi.encode(
emailAuthImplementation(),
abi.encodeCall(
EmailAuth.initialize,
(
recoveredAccount,
accountSalt,
address(this)
)
)
)
)
)
);
address payable proxyAddress = abi.decode(returnData, (address));
console.log("proxyAddress", proxyAddress);
deployedProxyAddress = proxyAddress;
// ERC1967Proxy proxy = ERC1967Proxy(proxyAddress);
// guardianEmailAuth = EmailAuth(address(proxy));
return address(proxyAddress);
}
}
4 changes: 4 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const inMemoryNode: NetworkUserConfig = {
ethNetwork: '', // in-memory node doesn't support eth node; removing this line will cause an error
zksync: true,
chainId: 260,
gas: 90000000,
blockGasLimit: 90000000,
allowUnlimitedContractSize: true
};

const dockerizedNode: NetworkUserConfig = {
Expand Down Expand Up @@ -67,6 +70,7 @@ const config: HardhatUserConfig = {
networks: {
hardhat: {
zksync: true,
allowUnlimitedContractSize: true
},
zkSyncSepolia,
zkSyncMainnet,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"scripts": {
"lint": "npx prettier --write --plugin=prettier-plugin-solidity 'contracts/**/*.sol'",
"lint-check": "npx prettier --list-different --plugin=prettier-plugin-solidity 'contracts/**/*.sol'",
"test": "TEST=true npx hardhat test --network hardhat"
"test": "TEST=true npx hardhat test --network hardhat",
"test:create2": "TEST=true npx hardhat test --network hardhat test/create2address.test.ts"
}
}
80 changes: 80 additions & 0 deletions test/create2address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { assert, expect } from 'chai';
import type { ec } from 'elliptic';
import {
AbiCoder,
WeiPerEther,
ZeroAddress,
concat,
ethers,
parseEther,
randomBytes,
solidityPackedKeccak256,
} from 'ethers';
import * as hre from 'hardhat';
import { Contract, Provider, Wallet, utils } from 'zksync-ethers';

import { LOCAL_RICH_WALLETS, deployContract, getWallet } from '../deploy/utils';
import type { CallStruct } from '../typechain-types/contracts/batch/BatchCaller';
import { encodePublicKey, genKey } from './utils/p256';
import { getGaslessPaymasterInput } from './utils/paymaster';
import { ethTransfer, prepareBatchTx, prepareTeeTx } from './utils/transaction';

let provider: Provider;
let richWallet: Wallet;
let keyPair: ec.KeyPair;

let batchCaller: Contract;
let mockValidator: Contract;
let implementation: Contract;
let factory: Contract;
let account: Contract;
let create2Address: Contract;

beforeEach(async () => {
provider = new Provider(hre.network.config.url, undefined, {
cacheTimeout: -1,
});
richWallet = getWallet(hre, LOCAL_RICH_WALLETS[0].privateKey);
keyPair = genKey();
const publicKey = encodePublicKey(keyPair);
create2Address = await deployContract(hre, 'Create2Address', undefined, {
wallet: richWallet,
silent: true,
});
})

describe('Create2Address', function () {
it('should create a create2 address', async () => {

const recoveredAccount = "0x0000000000000000000000000000000000000001";
const accountSalt = ethers.ZeroHash;

const chainId = await create2Address.chainId();
console.log("chainId", chainId);

const emailAuthAddress = await create2Address.emailAuthImplementation();
console.log("emailAuthAddress", emailAuthAddress);

const computeAddress = await create2Address.computeEmailAuthAddress(recoveredAccount, accountSalt);
console.log("computeAddress", computeAddress);

await deployContract(
hre,
"contracts/EmailRecoveryManager.sol:ERC1967Proxy",
[emailAuthAddress, "0x"],
{
wallet: richWallet,
silent: true,
}
);

await create2Address.deployProxy(recoveredAccount, accountSalt, {
gasLimit: 80000000
});

const deployedProxyAddress = await create2Address.getDeployedProxyAddress();
console.log("deployedProxyAddress", deployedProxyAddress);

expect(computeAddress).to.equal(deployedProxyAddress);
})
})