forked from getclave/clave-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
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
wshino
wants to merge
4
commits into
master
Choose a base branch
from
feat/check-create2address
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.