Use bytes32 explicitly for Create2.computeAddress() (#2088)
* Use bytes32 explicitly for Create2.computeAddress(), to force users cache hash of the bytecode * Remove only from test :)
This commit is contained in:
@ -13,15 +13,11 @@ contract Create2Impl {
|
|||||||
Create2.deploy(salt, type(ERC20).creationCode);
|
Create2.deploy(salt, type(ERC20).creationCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
function computeAddress(bytes32 salt, bytes memory code) public view returns (address) {
|
function computeAddress(bytes32 salt, bytes32 codeHash) public view returns (address) {
|
||||||
return Create2.computeAddress(salt, code);
|
return Create2.computeAddress(salt, codeHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
function computeAddress(bytes32 salt, bytes memory code, address deployer) public pure returns (address) {
|
function computeAddressWithDeployer(bytes32 salt, bytes32 codeHash, address deployer) public pure returns (address) {
|
||||||
return Create2.computeAddress(salt, code, deployer);
|
|
||||||
}
|
|
||||||
|
|
||||||
function computeAddress(bytes32 salt, bytes32 codeHash, address deployer) public pure returns (address) {
|
|
||||||
return Create2.computeAddress(salt, codeHash, deployer);
|
return Create2.computeAddress(salt, codeHash, deployer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,19 +28,11 @@ library Create2 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the `bytecode`
|
* @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the `bytecodeHash`
|
||||||
* or `salt` will result in a new destination address.
|
* or `salt` will result in a new destination address.
|
||||||
*/
|
*/
|
||||||
function computeAddress(bytes32 salt, bytes memory bytecode) internal view returns (address) {
|
function computeAddress(bytes32 salt, bytes32 bytecodeHash) internal view returns (address) {
|
||||||
return computeAddress(salt, bytecode, address(this));
|
return computeAddress(salt, bytecodeHash, address(this));
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Returns the address where a contract will be stored if deployed via {deploy} from a contract located at
|
|
||||||
* `deployer`. If `deployer` is this contract's address, returns the same value as {computeAddress}.
|
|
||||||
*/
|
|
||||||
function computeAddress(bytes32 salt, bytes memory bytecode, address deployer) internal pure returns (address) {
|
|
||||||
return computeAddress(salt, keccak256(bytecode), deployer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -22,7 +22,7 @@ describe('Create2', function () {
|
|||||||
|
|
||||||
it('should compute the correct contract address', async function () {
|
it('should compute the correct contract address', async function () {
|
||||||
const onChainComputed = await this.factory
|
const onChainComputed = await this.factory
|
||||||
.computeAddress(saltHex, constructorByteCode);
|
.computeAddress(saltHex, web3.utils.keccak256(constructorByteCode));
|
||||||
const offChainComputed =
|
const offChainComputed =
|
||||||
computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
|
computeCreate2Address(saltHex, constructorByteCode, this.factory.address);
|
||||||
expect(onChainComputed).to.equal(offChainComputed);
|
expect(onChainComputed).to.equal(offChainComputed);
|
||||||
@ -30,17 +30,7 @@ describe('Create2', function () {
|
|||||||
|
|
||||||
it('should compute the correct contract address with deployer', async function () {
|
it('should compute the correct contract address with deployer', async function () {
|
||||||
const onChainComputed = await this.factory
|
const onChainComputed = await this.factory
|
||||||
.methods['computeAddress(bytes32,bytes,address)'](saltHex, constructorByteCode, deployerAccount);
|
.computeAddressWithDeployer(saltHex, web3.utils.keccak256(constructorByteCode), deployerAccount);
|
||||||
const offChainComputed =
|
|
||||||
computeCreate2Address(saltHex, constructorByteCode, deployerAccount);
|
|
||||||
expect(onChainComputed).to.equal(offChainComputed);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should compute the correct contract address with deployer and bytecode hash', async function () {
|
|
||||||
const onChainComputed = await this.factory
|
|
||||||
.methods['computeAddress(bytes32,bytes32,address)'](
|
|
||||||
saltHex, web3.utils.keccak256(constructorByteCode), deployerAccount
|
|
||||||
);
|
|
||||||
const offChainComputed =
|
const offChainComputed =
|
||||||
computeCreate2Address(saltHex, constructorByteCode, deployerAccount);
|
computeCreate2Address(saltHex, constructorByteCode, deployerAccount);
|
||||||
expect(onChainComputed).to.equal(offChainComputed);
|
expect(onChainComputed).to.equal(offChainComputed);
|
||||||
|
|||||||
Reference in New Issue
Block a user