Skip to content

Commit

Permalink
Merge pull request #17 from TrueWallet/create2-deployment
Browse files Browse the repository at this point in the history
refactor: migrate wallet deployment to `CREATE2`
  • Loading branch information
scream4ik authored Feb 12, 2024
2 parents f332acc + 71efc7d commit 5a61a35
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/wallet/TrueWalletFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
pragma solidity ^0.8.19;

import {Ownable} from "solady/auth/Ownable.sol";
import {CREATE3} from "solady/utils/CREATE3.sol";
import {Pausable} from "openzeppelin-contracts/security/Pausable.sol";
import {Create2} from "openzeppelin-contracts/utils/Create2.sol";
import {IEntryPoint} from "account-abstraction/interfaces/IEntryPoint.sol";
import {TrueWallet} from "./TrueWallet.sol";
import {TrueWalletProxy} from "./TrueWalletProxy.sol";
import {WalletErrors} from "../common/Errors.sol";

/// @title TrueWalletFactory
/// @notice A factory contract for deploying and managing TrueWallet smart contracts using CREATE2 and CREATE3 for deterministic addresses.
/// @notice A factory contract for deploying and managing TrueWallet smart contracts using CREATE2 for deterministic addresses.
/// @dev This contract allows for the creation of TrueWallet instances with predictable addresses.
contract TrueWalletFactory is Ownable, Pausable, WalletErrors {
/// @notice Address of the wallet implementation contract.
Expand All @@ -35,15 +35,22 @@ contract TrueWalletFactory is Ownable, Pausable, WalletErrors {
_setOwner(_owner);
}

/// @notice Deploy a new TrueWallet smart contract using CREATE3.
/// @notice Deploy a new TrueWallet smart contract using CREATE2.
/// @param _initializer Initialization data for the new wallet.
/// @param _salt A unique salt value used in the CREATE3 operation for deterministic address generation.
/// @param _salt A unique salt value used in the CREATE2 operation for deterministic address generation.
/// @return proxy The address of the newly created TrueWallet contract.
function createWallet(bytes memory _initializer, bytes32 _salt) external whenNotPaused returns (TrueWallet proxy) {
bytes memory deploymentData =
abi.encodePacked(type(TrueWalletProxy).creationCode, uint256(uint160(walletImplementation)));

proxy = TrueWallet(payable(address(CREATE3.deploy(_salt, deploymentData, 0))));
// solhint-disable-next-line no-inline-assembly
assembly {
proxy := create2(0x0, add(deploymentData, 0x20), mload(deploymentData), _salt)
}

if (address(proxy) == address(0)) {
revert();
}

// solhint-disable-next-line no-inline-assembly
assembly {
Expand All @@ -54,12 +61,14 @@ contract TrueWalletFactory is Ownable, Pausable, WalletErrors {
emit TrueWalletCreation(proxy);
}

/// @notice Computes the deterministic address for a potential wallet deployment using CREATE3.
/// @notice Computes the deterministic address for a potential wallet deployment using CREATE2.
/// @dev This doesn't deploy the wallet, just calculates its address using the provided salt.
/// @param _salt A unique salt value used in the CREATE3 operation for deterministic address generation.
/// @param _salt A unique salt value used in the CREATE2 operation for deterministic address generation.
/// @return proxy The address of the wallet that would be created using the provided salt.
function getWalletAddress(bytes32 _salt) public view returns (address proxy) {
proxy = CREATE3.getDeployed(_salt);
bytes memory deploymentData =
abi.encodePacked(type(TrueWalletProxy).creationCode, uint256(uint160(walletImplementation)));
proxy = Create2.computeAddress(_salt, keccak256(deploymentData));
}

/// @notice Constructs the initializer payload for wallet creation.
Expand Down

0 comments on commit 5a61a35

Please sign in to comment.