-
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
【开发小记】create2的使用方式 #6
Comments
Overview
具体的计算公式是这样的:
|
如何计算如上所说,此处需要注意的是所谓合约的部署代码的构成,部署代码指的是,合约在被部署时传入账户的数据,而非他的runtime code,也非其代码直接编译出的结果。直接的来说,这个 在合约端: //calculate initCode
bytes memory initCode = abi.encodePacked(
type(DeployContract).creationCode,
abi.encode(constructorArgs1, constructorArgs2)
);
//compute address
bytes32 hash = keccak256(
abi.encodePacked(
bytes1(0xff),
deployer,
salt,
keccak256(initCode)
)
);
// OR use Openzeppelin Create2
Create2.computeAddress(salt, keccak256(initCode));
// Deploy
DeployContract contract = new DeployContract{salt: _salt}(constructorArgs1, constructorArgs2); 在ts测试端: //get creationCode
const creationCode = (await ethers.getContractFactory("DeployedContract")).bytecode;
//encode constructorArgs
const args = AbiCoder.defaultAbiCoder().encode(
["type", "type"],
[constructorArgs1, constructorArgs2]
);
// Combine initCode
const initCode = solidityPacked(["bytes", "bytes"], [creationCode, args]); |
One Little Trick在前面部分,重点强调了在计算地址时,我们用到的参数是部署代码。这个代码是用来创建合约的,合约创建完成后将返回 AHA Moment
Example上面的文字表述可能不够直接,那么我们使用一组代码,来直截了当的展示,这是一个怎样的trick。代码来源是:2019 Balsn CTF 的 Creativity 的 WP 提供的 PoC pragma solidity ^0.5.10;
contract Deployer {
bytes public deployBytecode;
address public deployedAddr;
function deploy(bytes memory code) public {
deployBytecode = code;
address a;
// Compile Dumper to get this bytecode
bytes memory dumperBytecode = hex'6080604052348015600f57600080fd5b50600033905060608173ffffffffffffffffffffffffffffffffffffffff166331d191666040518163ffffffff1660e01b815260040160006040518083038186803b158015605c57600080fd5b505afa158015606f573d6000803e3d6000fd5b505050506040513d6000823e3d601f19601f820116820180604052506020811015609857600080fd5b81019080805164010000000081111560af57600080fd5b8281019050602081018481111560c457600080fd5b815185600182028301116401000000008211171560e057600080fd5b50509291905050509050805160208201f3fe';
assembly {
a := create2(callvalue, add(0x20, dumperBytecode), mload(dumperBytecode), 0x9453)
}
deployedAddr = a;
}
}
contract Dumper {
constructor() public {
Deployer dp = Deployer(msg.sender);
bytes memory bytecode = dp.deployBytecode();
assembly {
return (add(bytecode, 0x20), mload(bytecode))
}
}
} 每次我们使用 |
本issue用于记录在合约开发过程中,合约端,以及链下部分如何计算出匹配的create2地址,以及这两个部分需要注意的点。
The text was updated successfully, but these errors were encountered: