Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decompose the factory to reduce its contract size. #19

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion script/DeploySafeRecovery.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/console.sol";
import { SafeRecoverySubjectHandler } from "src/handlers/SafeRecoverySubjectHandler.sol";
import { EmailRecoveryFactory } from "src/EmailRecoveryFactory.sol";
import { EmailRecoveryUniversalFactory } from "src/EmailRecoveryUniversalFactory.sol";
import { Verifier } from "ether-email-auth/packages/contracts/src/utils/Verifier.sol";
import { ECDSAOwnedDKIMRegistry } from
"ether-email-auth/packages/contracts/src/utils/ECDSAOwnedDKIMRegistry.sol";
Expand Down Expand Up @@ -44,7 +45,8 @@ contract DeploySafeRecovery_Script is Script {
console.log("Deployed Email Auth at", emailAuthImpl);
}

EmailRecoveryFactory factory = new EmailRecoveryFactory(verifier, emailAuthImpl);
EmailRecoveryUniversalFactory factory =
new EmailRecoveryUniversalFactory(verifier, emailAuthImpl);
(address module, address manager, address subjectHandler) = factory
.deployUniversalEmailRecoveryModule(
bytes32(uint256(0)),
Expand Down
6 changes: 3 additions & 3 deletions script/DeployUniversalEmailRecoveryModule.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Verifier } from "ether-email-auth/packages/contracts/src/utils/Verifier
import { ECDSAOwnedDKIMRegistry } from
"ether-email-auth/packages/contracts/src/utils/ECDSAOwnedDKIMRegistry.sol";
import { EmailAuth } from "ether-email-auth/packages/contracts/src/EmailAuth.sol";
import { EmailRecoveryFactory } from "src/EmailRecoveryFactory.sol";
import { EmailRecoveryUniversalFactory } from "src/EmailRecoveryUniversalFactory.sol";

contract DeployUniversalEmailRecoveryModuleScript is Script {
function run() public {
Expand Down Expand Up @@ -38,11 +38,11 @@ contract DeployUniversalEmailRecoveryModuleScript is Script {

address _factory = vm.envOr("RECOVERY_FACTORY", address(0));
if (_factory == address(0)) {
_factory = address(new EmailRecoveryFactory(verifier, emailAuthImpl));
_factory = address(new EmailRecoveryUniversalFactory(verifier, emailAuthImpl));
console.log("Deployed Email Recovery Factory at", _factory);
}
{
EmailRecoveryFactory factory = EmailRecoveryFactory(_factory);
EmailRecoveryUniversalFactory factory = EmailRecoveryUniversalFactory(_factory);
(address module, address manager, address subjectHandler) = factory
.deployUniversalEmailRecoveryModule(
bytes32(uint256(0)),
Expand Down
32 changes: 0 additions & 32 deletions src/EmailRecoveryFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,4 @@ contract EmailRecoveryFactory {

return (emailRecoveryModule, emailRecoveryManager, subjectHandler);
}

function deployUniversalEmailRecoveryModule(
bytes32 subjectHandlerSalt,
bytes32 recoveryManagerSalt,
bytes32 recoveryModuleSalt,
bytes memory subjectHandlerBytecode,
address dkimRegistry
)
external
returns (address, address, address)
{
// Deploy subject handler
address subjectHandler = Create2.deploy(0, subjectHandlerSalt, subjectHandlerBytecode);

// Deploy recovery manager
address emailRecoveryManager = address(
new EmailRecoveryManager{ salt: recoveryManagerSalt }(
verifier, dkimRegistry, emailAuthImpl, subjectHandler
)
);

// Deploy recovery module
address emailRecoveryModule = address(
new UniversalEmailRecoveryModule{ salt: recoveryModuleSalt }(emailRecoveryManager)
);

// Initialize recovery manager with module address
EmailRecoveryManager(emailRecoveryManager).initialize(emailRecoveryModule);
emit EmailRecoveryModuleDeployed(emailRecoveryModule, emailRecoveryManager, subjectHandler);

return (emailRecoveryModule, emailRecoveryManager, subjectHandler);
}
}
52 changes: 52 additions & 0 deletions src/EmailRecoveryUniversalFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { Create2 } from "@openzeppelin/contracts/utils/Create2.sol";
import { EmailRecoveryManager } from "./EmailRecoveryManager.sol";
import { UniversalEmailRecoveryModule } from "./modules/UniversalEmailRecoveryModule.sol";

contract EmailRecoveryUniversalFactory {
address public immutable verifier;
address public immutable emailAuthImpl;

event EmailRecoveryModuleDeployed(
address emailRecoveryModule, address emailRecoveryManager, address subjectHandler
);

constructor(address _verifier, address _emailAuthImpl) {
verifier = _verifier;
emailAuthImpl = _emailAuthImpl;
}

function deployUniversalEmailRecoveryModule(
bytes32 subjectHandlerSalt,
bytes32 recoveryManagerSalt,
bytes32 recoveryModuleSalt,
bytes memory subjectHandlerBytecode,
address dkimRegistry
)
external
returns (address, address, address)
{
// Deploy subject handler
address subjectHandler = Create2.deploy(0, subjectHandlerSalt, subjectHandlerBytecode);

// Deploy recovery manager
address emailRecoveryManager = address(
new EmailRecoveryManager{ salt: recoveryManagerSalt }(
verifier, dkimRegistry, emailAuthImpl, subjectHandler
)
);

// Deploy recovery module
address emailRecoveryModule = address(
new UniversalEmailRecoveryModule{ salt: recoveryModuleSalt }(emailRecoveryManager)
);

// Initialize recovery manager with module address
EmailRecoveryManager(emailRecoveryManager).initialize(emailRecoveryModule);
emit EmailRecoveryModuleDeployed(emailRecoveryModule, emailRecoveryManager, subjectHandler);

return (emailRecoveryModule, emailRecoveryManager, subjectHandler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { SubjectUtils } from "ether-email-auth/packages/contracts/src/libraries/SubjectUtils.sol";
import { EmailRecoverySubjectHandler } from "src/handlers/EmailRecoverySubjectHandler.sol";
import { EmailRecoveryFactory } from "src/EmailRecoveryFactory.sol";
import { EmailRecoveryUniversalFactory } from "src/EmailRecoveryUniversalFactory.sol";
import { EmailRecoveryManager } from "src/EmailRecoveryManager.sol";
import { OwnableValidator } from "src/test/OwnableValidator.sol";
import { IntegrationBase } from "../../IntegrationBase.t.sol";
Expand All @@ -23,7 +24,7 @@ abstract contract OwnableValidatorRecovery_UniversalEmailRecoveryModule_Base is
using Strings for uint256;
using Strings for address;

EmailRecoveryFactory emailRecoveryFactory;
EmailRecoveryUniversalFactory emailRecoveryFactory;
EmailRecoverySubjectHandler emailRecoveryHandler;
EmailRecoveryManager emailRecoveryManager;

Expand All @@ -46,7 +47,8 @@ abstract contract OwnableValidatorRecovery_UniversalEmailRecoveryModule_Base is
function setUp() public virtual override {
super.setUp();

emailRecoveryFactory = new EmailRecoveryFactory(address(verifier), address(emailAuthImpl));
emailRecoveryFactory =
new EmailRecoveryUniversalFactory(address(verifier), address(emailAuthImpl));
emailRecoveryHandler = new EmailRecoverySubjectHandler();

// Deploy EmailRecoveryManager & UniversalEmailRecoveryModule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { console2 } from "forge-std/console2.sol";
import { Create2 } from "@openzeppelin/contracts/utils/Create2.sol";
import { UnitBase } from "../UnitBase.t.sol";
import { EmailRecoveryFactory } from "src/EmailRecoveryFactory.sol";
import { EmailRecoveryUniversalFactory } from "src/EmailRecoveryUniversalFactory.sol";
import { EmailRecoverySubjectHandler } from "src/handlers/EmailRecoverySubjectHandler.sol";
import { EmailRecoveryManager } from "src/EmailRecoveryManager.sol";
import { EmailRecoveryModule } from "src/modules/EmailRecoveryModule.sol";

contract EmailRecoveryFactory_deployAll_Test is UnitBase {
function setUp() public override {
super.setUp();
emailRecoveryFactory = new EmailRecoveryFactory(address(verifier), address(emailAuthImpl));
}

function test_DeployEmailRecoveryModule_Succeeds() public {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { console2 } from "forge-std/console2.sol";
import { Create2 } from "@openzeppelin/contracts/utils/Create2.sol";
import { UnitBase } from "../UnitBase.t.sol";
import { EmailRecoveryFactory } from "src/EmailRecoveryFactory.sol";
import { EmailRecoveryUniversalFactory } from "src/EmailRecoveryUniversalFactory.sol";
import { EmailRecoverySubjectHandler } from "src/handlers/EmailRecoverySubjectHandler.sol";
import { EmailRecoveryManager } from "src/EmailRecoveryManager.sol";
import { UniversalEmailRecoveryModule } from "src/modules/UniversalEmailRecoveryModule.sol";

contract EmailRecoveryFactory_deployUniversalEmailRecoveryModule_Test is UnitBase {
function setUp() public override {
super.setUp();
emailRecoveryFactory = new EmailRecoveryFactory(address(verifier), address(emailAuthImpl));
}

function test_DeployUniversalEmailRecoveryModule_Succeeds() public {
Expand All @@ -22,7 +22,9 @@ contract EmailRecoveryFactory_deployUniversalEmailRecoveryModule_Test is UnitBas

bytes memory subjectHandlerBytecode = type(EmailRecoverySubjectHandler).creationCode;
address expectedSubjectHandler = Create2.computeAddress(
subjectHandlerSalt, keccak256(subjectHandlerBytecode), address(emailRecoveryFactory)
subjectHandlerSalt,
keccak256(subjectHandlerBytecode),
address(emailRecoveryUniversalFactory)
);

bytes memory recoveryManagerBytecode = abi.encodePacked(
Expand All @@ -35,18 +37,22 @@ contract EmailRecoveryFactory_deployUniversalEmailRecoveryModule_Test is UnitBas
)
);
address expectedManager = Create2.computeAddress(
recoveryManagerSalt, keccak256(recoveryManagerBytecode), address(emailRecoveryFactory)
recoveryManagerSalt,
keccak256(recoveryManagerBytecode),
address(emailRecoveryUniversalFactory)
);

bytes memory recoveryModuleBytecode = abi.encodePacked(
type(UniversalEmailRecoveryModule).creationCode, abi.encode(expectedManager)
);
address expectedModule = Create2.computeAddress(
recoveryModuleSalt, keccak256(recoveryModuleBytecode), address(emailRecoveryFactory)
recoveryModuleSalt,
keccak256(recoveryModuleBytecode),
address(emailRecoveryUniversalFactory)
);

(address emailRecoveryModule, address emailRecoveryManager, address subjectHandler) =
emailRecoveryFactory.deployUniversalEmailRecoveryModule(
emailRecoveryUniversalFactory.deployUniversalEmailRecoveryModule(
subjectHandlerSalt,
recoveryManagerSalt,
recoveryModuleSalt,
Expand Down
4 changes: 4 additions & 0 deletions test/unit/UnitBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { EmailRecoverySubjectHandler } from "src/handlers/EmailRecoverySubjectHa
import { UniversalEmailRecoveryModuleHarness } from "./UniversalEmailRecoveryModuleHarness.sol";
import { EmailRecoveryManager } from "src/EmailRecoveryManager.sol";
import { EmailRecoveryFactory } from "src/EmailRecoveryFactory.sol";
import { EmailRecoveryUniversalFactory } from "src/EmailRecoveryUniversalFactory.sol";
import { OwnableValidator } from "src/test/OwnableValidator.sol";
import { MockGroth16Verifier } from "src/test/MockGroth16Verifier.sol";

Expand All @@ -41,6 +42,7 @@ abstract contract UnitBase is RhinestoneModuleKit, Test {
EmailAuth emailAuthImpl;

EmailRecoveryFactory emailRecoveryFactory;
EmailRecoveryUniversalFactory emailRecoveryUniversalFactory;
EmailRecoverySubjectHandler emailRecoveryHandler;
EmailRecoveryManagerHarness emailRecoveryManager;
UniversalEmailRecoveryModuleHarness emailRecoveryModule;
Expand Down Expand Up @@ -110,6 +112,8 @@ abstract contract UnitBase is RhinestoneModuleKit, Test {
// Deploy handler, manager and module
emailRecoveryHandler = new EmailRecoverySubjectHandler();
emailRecoveryFactory = new EmailRecoveryFactory(address(verifier), address(emailAuthImpl));
emailRecoveryUniversalFactory =
new EmailRecoveryUniversalFactory(address(verifier), address(emailAuthImpl));

emailRecoveryManager = new EmailRecoveryManagerHarness(
address(verifier),
Expand Down