Skip to content

Commit

Permalink
Rename CREATE2 argument from bytecodeHash to bytecode and add new met…
Browse files Browse the repository at this point in the history
…hod for precomputed bytecode hash (OpenZeppelin#2087)

* Rename CREATE2 argument from bytecodeHash to bytecode and add new method for precomputed bytecode hash

* Remove only from test

* Fix linter error
  • Loading branch information
k06a authored Feb 14, 2020
1 parent 5dfe721 commit 19417c7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions contracts/mocks/Create2Impl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ contract Create2Impl {
function computeAddress(bytes32 salt, bytes memory code, 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);
}
}
13 changes: 10 additions & 3 deletions contracts/utils/Create2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ library Create2 {
* @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 bytecodeHash, address deployer) internal pure returns (address) {
bytes32 bytecodeHashHash = keccak256(bytecodeHash);
function computeAddress(bytes32 salt, bytes memory bytecode, address deployer) internal pure returns (address) {
return computeAddress(salt, keccak256(bytecode), deployer);
}

/**
* @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, bytes32 bytecodeHash, address deployer) internal pure returns (address) {
bytes32 _data = keccak256(
abi.encodePacked(bytes1(0xff), deployer, salt, bytecodeHashHash)
abi.encodePacked(bytes1(0xff), deployer, salt, bytecodeHash)
);
return address(bytes20(_data << 96));
}
Expand Down
12 changes: 11 additions & 1 deletion test/utils/Create2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ describe('Create2', function () {

it('should compute the correct contract address with deployer', async function () {
const onChainComputed = await this.factory
.computeAddress(saltHex, constructorByteCode, deployerAccount);
.methods['computeAddress(bytes32,bytes,address)'](saltHex, 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 =
computeCreate2Address(saltHex, constructorByteCode, deployerAccount);
expect(onChainComputed).to.equal(offChainComputed);
Expand Down

0 comments on commit 19417c7

Please sign in to comment.