diff --git a/script/BaseDeployer.sol b/script/BaseDeployer.sol new file mode 100644 index 0000000..399e60a --- /dev/null +++ b/script/BaseDeployer.sol @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: Apache +pragma solidity ^0.8.0; + +import "../src/GenericFactory.sol"; + +contract BaseDeployer { + GenericFactory public genericFactory = GenericFactory(0x9aE7a293de3D5789Cd24324C167B7BC98A0c70B7); +} \ No newline at end of file diff --git a/script/Deploy.s.sol b/script/DeployEntrypoint.s.sol similarity index 62% rename from script/Deploy.s.sol rename to script/DeployEntrypoint.s.sol index 407cb81..9bc0e11 100644 --- a/script/Deploy.s.sol +++ b/script/DeployEntrypoint.s.sol @@ -7,19 +7,19 @@ import "forge-std/console.sol"; import "account-abstraction/core/EntryPoint.sol"; import "../src/WalletFactory.sol"; import "../src/modules/Passkey.sol"; +import "./BaseDeployer.sol"; -contract Deployer is Script { +contract Deployer is Script, BaseDeployer { function setUp() external {} function run() external { uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY"); + bytes32 salt = vm.envBytes32("SALT"); vm.startBroadcast(deployerPrivateKey); - EntryPoint entryPoint = new EntryPoint(); - console.log("EntryPoint: ", address(entryPoint)); - - WalletFactory walletFactory = new WalletFactory(address(entryPoint)); - console.log("WalletFactory: ", address(walletFactory)); + bytes memory code = type(EntryPoint).creationCode; + address entryPoint = genericFactory.create2(code, salt); + console.log("EntryPoint: ", entryPoint); vm.stopBroadcast(); } diff --git a/script/DeployFactory.s.sol b/script/DeployFactory.s.sol index 0237e91..6668cda 100644 --- a/script/DeployFactory.s.sol +++ b/script/DeployFactory.s.sol @@ -8,25 +8,32 @@ import "account-abstraction/core/EntryPoint.sol"; import "../src/WalletFactory.sol"; import "../src/Bootstrap.sol"; import "../src/modules/Passkey.sol"; +import "./BaseDeployer.sol"; -contract Deployer is Script { +contract Deployer is Script, BaseDeployer { function setUp() external {} function run() external { uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY"); address exitedEntryPoint = vm.envAddress("ENTRY_POINT"); + bytes32 salt = vm.envBytes32("SALT"); require(exitedEntryPoint != address(0), "ENTRY_POINT is required"); vm.startBroadcast(deployerPrivateKey); console.log("EntryPoint: ", exitedEntryPoint); - WalletFactory walletFactory = new WalletFactory(exitedEntryPoint); - PasskeyModule passkey = new PasskeyModule(); - Bootstrap bootstrap = new Bootstrap(address(passkey)); + bytes memory walletFactoryCode = abi.encodePacked(type(WalletFactory).creationCode, abi.encode(exitedEntryPoint)); + address walletFactory = genericFactory.create2(walletFactoryCode, salt); - console.log("WalletFactory: ", address(walletFactory)); - console.log("Passkey: ", address(passkey)); - console.log("Bootstrap: ", address(bootstrap)); + bytes memory passkeyCode = type(PasskeyModule).creationCode; + address passkey = genericFactory.create2(passkeyCode, salt); + + bytes memory bootstrapCode = abi.encodePacked(type(Bootstrap).creationCode, abi.encode(passkey)); + address bootstrap = genericFactory.create2(bootstrapCode, salt); + + console.log("WalletFactory: ", walletFactory); + console.log("Passkey: ", passkey); + console.log("Bootstrap: ", bootstrap); vm.stopBroadcast(); } diff --git a/script/DeployGenericFactory.s.sol b/script/DeployGenericFactory.s.sol new file mode 100644 index 0000000..be80384 --- /dev/null +++ b/script/DeployGenericFactory.s.sol @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: Apache +pragma solidity ^0.8.0; + +import {Script, console2} from "forge-std/Script.sol"; + +import "forge-std/console.sol"; +import "account-abstraction/core/EntryPoint.sol"; +import "../src/GenericFactory.sol"; + +contract Deployer is Script { + function setUp() external {} + + function run() external { + uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY"); + address exitedEntryPoint = vm.envAddress("ENTRY_POINT"); + require(exitedEntryPoint != address(0), "ENTRY_POINT is required"); + vm.startBroadcast(deployerPrivateKey); + + GenericFactory factory = new GenericFactory(); + console.log("Generic factory: ", address(factory)); + + vm.stopBroadcast(); + } +} diff --git a/src/GenericFactory.sol b/src/GenericFactory.sol new file mode 100644 index 0000000..cd677a9 --- /dev/null +++ b/src/GenericFactory.sol @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: Apache +pragma solidity ^0.8.0; + +contract GenericFactory { + function create(bytes memory code) external returns (address addr) { + assembly { + addr := create(0, add(code, 0x20), mload(code)) + if iszero(extcodesize(addr)) { + revert(0, 0) + } + } + } + + function create2(bytes memory code, bytes32 salt) external returns (address addr) { + assembly { + addr := create2(0, add(code, 0x20), mload(code), salt) + if iszero(extcodesize(addr)) { + revert(0, 0) + } + } + } +} \ No newline at end of file