-
Notifications
You must be signed in to change notification settings - Fork 94
Description
In the Openzeppelin documentation of CREATE2 it states that:
New addresses are a function of:
0xFF, a constant that prevents collisions withCREATE- The sender’s own address
- A salt (an arbitrary value provided by the sender)
- The to-be-deployed contract’s bytecode
new_address = hash(0xFF, sender, salt, bytecode)
CREATE2guarantees that ifsenderever deploysbytecodeusingCREATE2and the providedsalt, it will be stored innew_address.
This is incorrect as the sender is not used in the calculation of the address by the CREATE2 opcode itself. It's the address of the contract that calls the create2 function (the "CREATE2 factory") that's used for the calculation.
EIP-1014: Skinny CREATE2 doesn't explain what the input address is very clearly, but the Yul documentation of create2 at https://docs.soliditylang.org/en/v0.8.21/yul.html#opcodes is much clearer, stating "current contract’s address":
| create2(v, p, n, s) | C | create new contract with code mem[p…(p+n)) at address keccak256(0xff . this . s . keccak256(mem[p…(p+n))) and send v wei and return the new address, where 0xff is a 1 byte value, this is the current contract’s address as a 20 byte value and s is a big-endian 256-bit value; returns 0 on error |
|---|
Gladly, the Openzeppelin CREATE2 library code correctly uses address(this) (and not msg.sender (or caller() in yul) as the documentation suggests).
However, factory contracts that use CREATE2 or CREATE3 libraries may first hash msg.sender with the user-provided salt before passing that into create2, e.g.:
This helps to prevent address clashes between different accounts that use the same salt. i.e. factoring in the sender may be done on a higher level, not in the library or EVM level. So you can't definitively state
CREATE2guarantees that ifsenderever deploysbytecodeusingCREATE2and the providedsalt, it will be stored innew_address.
as it depends on whether or not msg.sender is hashed with salt in the factory contract.
An example of a CREATE2 factory that doesn't use the sender is this popular one from Ethereum core developers Nick Johnson and Micah Zoltu: deterministic-deployment-proxy. It just passes the user-provided salt (calldataload(0)) straight into create2:
https://github.com/Arachnid/deterministic-deployment-proxy/blob/b3bb19c5aa8af6d9424fd66a234116a1e8adc3bf/source/deterministic-deployment-proxy.yul#L12. In my tests, changing the calling account resulted in the same deployment address. In such a case, it'd then be up to the factory user to ensure uniqueness via the salt.
For more details of using CREATE2 and CREATE3 and the various related issues see my repository: SKYBIT-Keyless-Deployment.