Skip to content

Commit

Permalink
refactor: code organization
Browse files Browse the repository at this point in the history
  • Loading branch information
zkfriendly committed Dec 16, 2024
1 parent cfde680 commit 91e3c9c
Showing 1 changed file with 127 additions and 83 deletions.
210 changes: 127 additions & 83 deletions script/DeployUniversalEmailRecoveryModule.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,117 +8,161 @@ import { EmailRecoveryCommandHandler } from "src/handlers/EmailRecoveryCommandHa
import { UserOverrideableDKIMRegistry } from "@zk-email/contracts/UserOverrideableDKIMRegistry.sol";
import { EmailAuth } from "@zk-email/ether-email-auth-contracts/src/EmailAuth.sol";
import { EmailRecoveryUniversalFactory } from "src/factories/EmailRecoveryUniversalFactory.sol";
import { BaseDeployScript } from "./BaseDeployScript.s.sol";
import { SafeSingletonDeployer } from "safe-singleton-deployer/SafeSingletonDeployer.sol";
import { Verifier } from "@zk-email/ether-email-auth-contracts/src/utils/Verifier.sol";
import { Groth16Verifier } from "@zk-email/ether-email-auth-contracts/src/utils/Groth16Verifier.sol";
import { UserOverrideableDKIMRegistry } from "@zk-email/contracts/UserOverrideableDKIMRegistry.sol";
import { ERC1967Proxy } from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import { Script } from "forge-std/Script.sol";

contract DeployUniversalEmailRecoveryModuleScript is BaseDeployScript {
function run() public override {
super.run();
contract DeployUniversalEmailRecoveryModuleScript is Script {
function run() public {
// Get environment variables
address initialOwner = vm.addr(vm.envUint("PRIVATE_KEY"));
address verifier = vm.envOr("VERIFIER", address(0));
address dkimRegistrySigner = vm.envOr("DKIM_SIGNER", address(0));
address emailAuthImpl = vm.envOr("EMAIL_AUTH_IMPL", address(0));
uint256 minimumDelay = vm.envOr("MINIMUM_DELAY", uint256(0));
address killSwitchAuthorizer = vm.envAddress("KILL_SWITCH_AUTHORIZER");

address initialOwner = vm.addr(vm.envUint("PRIVATE_KEY"));
uint256 salt = vm.envOr("CREATE2_SALT", uint256(0));
UserOverrideableDKIMRegistry dkim;

// Deploy verifier if not provided
if (verifier == address(0)) {
address verifierImpl = SafeSingletonDeployer.broadcastDeploy(
initialOwner, // any private key will do
type(Verifier).creationCode,
abi.encode(),
keccak256("VERIFIER")
);
console.log("Verifier implementation deployed at: %s", address(verifierImpl));
address groth16Verifier = SafeSingletonDeployer.broadcastDeploy(
initialOwner, // any private key will do
type(Groth16Verifier).creationCode,
abi.encode(),
keccak256("GROTH16_VERIFIER")
);
address verifierProxy = SafeSingletonDeployer.broadcastDeploy(
initialOwner, // any private key will do
type(ERC1967Proxy).creationCode,
abi.encode(
address(verifierImpl),
abi.encodeCall(
Verifier(verifierImpl).initialize, (initialOwner, address(groth16Verifier))
)
),
keccak256("VERIFIER_PROXY")
);
verifier = address(Verifier(address(verifierProxy)));
console.log("Deployed Verifier at", verifier);
verifier = deployVerifierContracts(initialOwner);
}

// Deploy Useroverridable DKIM registry
dkim = UserOverrideableDKIMRegistry(vm.envOr("DKIM_REGISTRY", address(0)));
uint256 setTimeDelay = vm.envOr("DKIM_DELAY", uint256(0));
// Deploy DKIM registry if not provided
UserOverrideableDKIMRegistry dkim =
UserOverrideableDKIMRegistry(vm.envOr("DKIM_REGISTRY", address(0)));
if (address(dkim) == address(0)) {
address userOverrideableDkimImpl = SafeSingletonDeployer.broadcastDeploy(
initialOwner, // any private key will do
type(UserOverrideableDKIMRegistry).creationCode,
abi.encode(),
keccak256("USER_OVERRIDEABLE_DKIM_IMPL")
);
console.log(
"UserOverrideableDKIMRegistry implementation deployed at: %s",
address(userOverrideableDkimImpl)
);
{
address dkimProxy = SafeSingletonDeployer.broadcastDeploy(
initialOwner, // any private key will do
type(ERC1967Proxy).creationCode,
abi.encode(
address(userOverrideableDkimImpl),
abi.encodeCall(
UserOverrideableDKIMRegistry(userOverrideableDkimImpl).initialize,
(initialOwner, dkimRegistrySigner, setTimeDelay)
)
),
keccak256("USER_OVERRIDEABLE_DKIM_PROXY")
);
dkim = UserOverrideableDKIMRegistry(dkimProxy);
}
console.log("UseroverrideableDKIMRegistry proxy deployed at: %s", address(dkim));
dkim = deployDKIMRegistry(initialOwner, dkimRegistrySigner);
}

// Deploy EmailAuth if not provided
if (emailAuthImpl == address(0)) {
emailAuthImpl = SafeSingletonDeployer.broadcastDeploy(
initialOwner, // any private key will do
initialOwner,
type(EmailAuth).creationCode,
abi.encode(),
keccak256("EMAIL_AUTH_IMPL")
);
console.log("Deployed Email Auth at", emailAuthImpl);
}

address _factory = vm.envOr("RECOVERY_FACTORY", address(0));
if (_factory == address(0)) {
_factory = address(
new EmailRecoveryUniversalFactory{ salt: bytes32(salt) }(verifier, emailAuthImpl)
// Deploy and configure factory
address factory = deployAndConfigureFactory(
initialOwner, verifier, emailAuthImpl, minimumDelay, killSwitchAuthorizer, address(dkim)
);

// Deploy recovery module and handler
deployRecoveryContracts(factory, minimumDelay, killSwitchAuthorizer, address(dkim));
}

function deployVerifierContracts(address initialOwner) private returns (address) {
address verifierImpl = SafeSingletonDeployer.broadcastDeploy(
initialOwner, type(Verifier).creationCode, abi.encode(), keccak256("VERIFIER")
);
console.log("Verifier implementation deployed at: %s", address(verifierImpl));

address groth16Verifier = SafeSingletonDeployer.broadcastDeploy(
initialOwner,
type(Groth16Verifier).creationCode,
abi.encode(),
keccak256("GROTH16_VERIFIER")
);

address verifierProxy = SafeSingletonDeployer.broadcastDeploy(
initialOwner,
type(ERC1967Proxy).creationCode,
abi.encode(
address(verifierImpl),
abi.encodeCall(Verifier(verifierImpl).initialize, (initialOwner, groth16Verifier))
),
keccak256("VERIFIER_PROXY")
);

address verifier = address(Verifier(verifierProxy));
console.log("Deployed Verifier at", verifier);
return verifier;
}

function deployDKIMRegistry(
address initialOwner,
address dkimRegistrySigner
)
private
returns (UserOverrideableDKIMRegistry)
{
uint256 setTimeDelay = vm.envOr("DKIM_DELAY", uint256(0));

address impl = SafeSingletonDeployer.broadcastDeploy(
initialOwner,
type(UserOverrideableDKIMRegistry).creationCode,
abi.encode(),
keccak256("USER_OVERRIDEABLE_DKIM_IMPL")
);
console.log("UserOverrideableDKIMRegistry implementation deployed at: %s", address(impl));

address proxy = SafeSingletonDeployer.broadcastDeploy(
initialOwner,
type(ERC1967Proxy).creationCode,
abi.encode(
address(impl),
abi.encodeCall(
UserOverrideableDKIMRegistry(impl).initialize,
(initialOwner, dkimRegistrySigner, setTimeDelay)
)
),
keccak256("USER_OVERRIDEABLE_DKIM_PROXY")
);

UserOverrideableDKIMRegistry dkim = UserOverrideableDKIMRegistry(proxy);
console.log("UseroverrideableDKIMRegistry proxy deployed at: %s", address(dkim));
return dkim;
}

function deployAndConfigureFactory(
address initialOwner,
address verifier,
address emailAuthImpl,
uint256 minimumDelay,
address killSwitchAuthorizer,
address dkim
)
private
returns (address)
{
address factory = vm.envOr("RECOVERY_FACTORY", address(0));
if (factory == address(0)) {
factory = SafeSingletonDeployer.broadcastDeploy(
initialOwner,
type(EmailRecoveryUniversalFactory).creationCode,
abi.encode(verifier, emailAuthImpl),
keccak256("EMAIL_RECOVERY_FACTORY")
);
console.log("Deployed Email Recovery Factory at", _factory);
console.log("Deployed Email Recovery Factory at", factory);
}
{
EmailRecoveryUniversalFactory factory = EmailRecoveryUniversalFactory(_factory);
(address module, address commandHandler) = factory.deployUniversalEmailRecoveryModule(
bytes32(uint256(0)),
bytes32(uint256(0)),
type(EmailRecoveryCommandHandler).creationCode,
minimumDelay,
killSwitchAuthorizer,
address(dkim)
);
return factory;
}

console.log("Deployed Email Recovery Module at", vm.toString(module));
console.log("Deployed Email Recovery Handler at", vm.toString(commandHandler));
}
function deployRecoveryContracts(
address factory,
uint256 minimumDelay,
address killSwitchAuthorizer,
address dkim
)
private
{
EmailRecoveryUniversalFactory recoveryFactory = EmailRecoveryUniversalFactory(factory);
(address module, address commandHandler) = recoveryFactory
.deployUniversalEmailRecoveryModule(
bytes32(uint256(0)),
bytes32(uint256(0)),
type(EmailRecoveryCommandHandler).creationCode,
minimumDelay,
killSwitchAuthorizer,
dkim
);

console.log("Deployed Email Recovery Module at", vm.toString(module));
console.log("Deployed Email Recovery Handler at", vm.toString(commandHandler));
}
}

0 comments on commit 91e3c9c

Please sign in to comment.