Skip to content

Commit

Permalink
Fix create3 salt derivation
Browse files Browse the repository at this point in the history
  • Loading branch information
Lohann committed Sep 26, 2024
1 parent f99775a commit 2e88095
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 225 deletions.
46 changes: 45 additions & 1 deletion src/FactoryUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ library FactoryUtils {
hex"763318602e57363d3d37363d34f080915215602e57f35bfd6017526460203d3d7360a01b33173d5260306007f3";
bytes32 internal constant PROXY_INITCODE_HASH = keccak256(PROXY_INITCODE);

/**
* @dev Compute the create2 address of an contract created by `UniversalFactory`.
*/
function computeCreateAddress(IUniversalFactory factory, uint256 nonce) internal pure returns (address addr) {
assembly ("memory-safe") {
nonce := or(nonce, shl(7, iszero(nonce)))
// Cache the free memory pointer.
let free_ptr := mload(0x40)
{
mstore(0x14, factory)
mstore(0x00, 0xd694)
mstore8(0x34, nonce)
addr := shr(96, shl(96, keccak256(30, 23)))
}
// Restore the free memory pointer.
mstore(0x40, free_ptr)
}
}

/**
* @dev Compute the create2 address of an contract created by `UniversalFactory`.
*/
Expand Down Expand Up @@ -61,17 +80,42 @@ library FactoryUtils {
}
}

/**
* @dev Compute the create3 salt.
*/
function computeCreate3Salt(address deployer, uint256 salt) internal pure returns (bytes32 create3salt) {
// The code below is equivalent to the Solidity code:
// ```solidity
// create3salt = keccak256(abi.encodePacked(deployer, salt));
// ```
assembly ("memory-safe") {
mstore(0x00, deployer)
mstore(0x20, salt)
create3salt := keccak256(12, 52)
}
}

/**
* @dev Compute the create3 address of an contract created by `UniversalFactory`.
*/
function computeCreate3Address(IUniversalFactory factory, uint256 salt) internal pure returns (address addr) {
function computeCreate3Address(IUniversalFactory factory, address deployer, uint256 salt)
internal
pure
returns (address addr)
{
// The code below is equivalent to the following Solidity code:
// ```solidity
// salt = keccak256(abi.encodePacked(deployer, salt));
// address create2addr = computeCreate2Address(factory, salt, PROXY_INITCODE_HASH);
// bytes32 create3hash = keccak256(abi.encodePacked(bytes2(0xd694), create2addr, uint8(0x01)));
// return address(uint160(uint256(create3hash)));
// ```
assembly ("memory-safe") {
// Compute keccak256(abi.encodePacked(deployer, salt));
mstore(0x00, deployer)
mstore(0x20, salt)
salt := keccak256(12, 52)

// Cache the free memory pointer.
let free_ptr := mload(0x40)
{
Expand Down
Loading

0 comments on commit 2e88095

Please sign in to comment.