Skip to content

Commit

Permalink
Add more logic to base test & more linting
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnGuilding committed Sep 19, 2024
1 parent 15be706 commit fcde8e2
Show file tree
Hide file tree
Showing 63 changed files with 493 additions and 586 deletions.
3 changes: 2 additions & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"not-rely-on-time": "off",
"one-contract-per-file": "off",
"var-name-mixedcase": "off",
"immutable-vars-naming": "off"
"immutable-vars-naming": "off",
"no-console": "warn"
}
}
4 changes: 1 addition & 3 deletions .solhintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
node_modules/
test/
script/
node_modules/
2 changes: 2 additions & 0 deletions script/Compute7579RecoveryDataHash.s.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

/* solhint-disable no-console */

import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/console.sol";

Expand Down
2 changes: 2 additions & 0 deletions script/ComputeSafeRecoveryCalldata.s.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

/* solhint-disable no-console */

import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/console.sol";

Expand Down
44 changes: 23 additions & 21 deletions script/Deploy7579TestAccount.s.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

/* solhint-disable no-console, gas-custom-errors, max-states-count */

import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/console.sol";
import { EmailAccountRecovery } from
Expand All @@ -27,27 +29,27 @@ contract Deploy7579TestAccountScript is RhinestoneModuleKit, Script {
using Strings for uint256;
using Strings for address;

uint256 privKey;
address deployer;
MSABasic msaBasicImpl;
MSAFactory msaFactory;
Bootstrap bootstrap;
MockHook hook;
MockTarget mockTarget;

bytes32 accountSalt;
address validatorAddr;
address recoveryModuleAddr;
address[] guardians = new address[](0);
uint256[] guardianWeights = new uint256[](0);

address account;
bytes initCode;
bytes userOpCalldata;
PackedUserOperation userOp;
bytes32 userOpHash;

bytes4 functionSelector = bytes4(keccak256(bytes("changeOwner(address)")));
uint256 public privKey;
address public deployer;
MSABasic public msaBasicImpl;
MSAFactory public msaFactory;
Bootstrap public bootstrap;
MockHook public hook;
MockTarget public mockTarget;

bytes32 public accountSalt;
address public validatorAddr;
address public recoveryModuleAddr;
address[] public guardians = new address[](0);
uint256[] public guardianWeights = new uint256[](0);

address public account;
bytes public initCode;
bytes public userOpCalldata;
PackedUserOperation public userOp;
bytes32 public userOpHash;

bytes4 public functionSelector = bytes4(keccak256(bytes("changeOwner(address)")));

function run() public {
privKey = vm.envUint("PRIVATE_KEY");
Expand Down
2 changes: 2 additions & 0 deletions script/DeployEmailRecoveryModule.s.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

/* solhint-disable no-console, gas-custom-errors */

import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/console.sol";
import { EmailRecoveryCommandHandler } from "src/handlers/EmailRecoveryCommandHandler.sol";
Expand Down
2 changes: 2 additions & 0 deletions script/DeploySafeNativeRecovery.s.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

/* solhint-disable no-console, gas-custom-errors */

import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/console.sol";
import { Verifier } from "@zk-email/ether-email-auth-contracts/src/utils/Verifier.sol";
Expand Down
3 changes: 2 additions & 1 deletion script/DeploySafeRecovery.s.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

/* solhint-disable no-console, gas-custom-errors */

import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/console.sol";
import { SafeRecoveryCommandHandler } from "src/handlers/SafeRecoveryCommandHandler.sol";
import { EmailRecoveryFactory } from "src/factories/EmailRecoveryFactory.sol";
import { EmailRecoveryUniversalFactory } from "src/factories/EmailRecoveryUniversalFactory.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";
Expand Down
2 changes: 2 additions & 0 deletions script/DeployUniversalEmailRecoveryModule.s.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

/* solhint-disable no-console, gas-custom-errors */

import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/console.sol";
import { EmailRecoveryCommandHandler } from "src/handlers/EmailRecoveryCommandHandler.sol";
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/StringUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pragma solidity ^0.8.13;

import { strings } from "solidity-stringutils/src/strings.sol";

/* solhint-disable gas-custom-errors */

// Extracted from https://github.com/zkemail/email-wallet-sdk/blob/main/src/helpers/StringUtils.sol
library StringUtils {
using strings for *;
Expand Down
181 changes: 135 additions & 46 deletions test/Base.t.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

/* solhint-disable no-console, no-unused-import */

import { Test } from "forge-std/Test.sol";
import { console2 } from "forge-std/console2.sol";
import { RhinestoneModuleKit, AccountInstance } from "modulekit/ModuleKit.sol";
import {
EmailAuth,
Expand All @@ -16,13 +13,18 @@ import { ECDSAOwnedDKIMRegistry } from
"@zk-email/ether-email-auth-contracts/src/utils/ECDSAOwnedDKIMRegistry.sol";
import { ERC1967Proxy } from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import { ECDSA } from "solady/utils/ECDSA.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";

import { MockGroth16Verifier } from "src/test/MockGroth16Verifier.sol";
import { OwnableValidator } from "src/test/OwnableValidator.sol";

/* solhint-disable gas-custom-errors, custom-errors, reason-string, max-states-count */

interface IEmailRecoveryModule {
function computeAcceptanceTemplateId(uint256 templateIdx) external pure returns (uint256);

function computeRecoveryTemplateId(uint256 templateIdx) external pure returns (uint256);

function handleAcceptance(EmailAuthMsg memory emailAuthMsg, uint256 templateIdx) external;

function handleRecovery(EmailAuthMsg memory emailAuthMsg, uint256 templateIdx) external;
Expand All @@ -31,50 +33,53 @@ interface IEmailRecoveryModule {
}

abstract contract BaseTest is RhinestoneModuleKit, Test {
using Strings for uint256;

// ZK Email contracts and variables
address zkEmailDeployer;
ECDSAOwnedDKIMRegistry dkimRegistry;
MockGroth16Verifier verifier;
EmailAuth emailAuthImpl;

OwnableValidator validator;
address validatorAddress;

// account and owners
address owner1;
address owner2;
address owner3;
address newOwner1;
address newOwner2;
address newOwner3;
AccountInstance instance1;
AccountInstance instance2;
AccountInstance instance3;
address accountAddress1;
address accountAddress2;
address accountAddress3;

// Account salts
bytes32 accountSalt1;
bytes32 accountSalt2;
bytes32 accountSalt3;

// recovery config
address[] guardians1;
address[] guardians2;
address[] guardians3;
uint256[] guardianWeights;
uint256 totalWeight;
uint256 delay;
uint256 expiry;
uint256 threshold;
uint256 templateIdx;

string selector = "12345";
string domainName = "gmail.com";
bytes32 publicKeyHash = 0x0ea9c777dc7110e5a9e89b13f0cfc540e3845ba120b2b6dc24024d61488d4788;

uint256 nullifierCount;
address public zkEmailDeployer;
ECDSAOwnedDKIMRegistry public dkimRegistry;
MockGroth16Verifier public verifier;
EmailAuth public emailAuthImpl;

OwnableValidator public validator;
address public validatorAddress;

// public account and owners
address public owner1;
address public owner2;
address public owner3;
address public newOwner1;
address public newOwner2;
address public newOwner3;
AccountInstance public instance1;
AccountInstance public instance2;
AccountInstance public instance3;
address public accountAddress1;
address public accountAddress2;
address public accountAddress3;

// public Account salts
bytes32 public accountSalt1;
bytes32 public accountSalt2;
bytes32 public accountSalt3;

// public recovery config
address[] public guardians1;
address[] public guardians2;
address[] public guardians3;
uint256[] public guardianWeights;
uint256 public totalWeight;
uint256 public delay;
uint256 public expiry;
uint256 public threshold;
uint256 public templateIdx;

string public selector = "12345";
string public domainName = "gmail.com";
bytes32 public publicKeyHash =
0x0ea9c777dc7110e5a9e89b13f0cfc540e3845ba120b2b6dc24024d61488d4788;

uint256 public nullifierCount;

function setUp() public virtual {
init();
Expand Down Expand Up @@ -270,6 +275,90 @@ abstract contract BaseTest is RhinestoneModuleKit, Test {
});
}

function handleRecovery(
address account,
address guardian,
bytes32 recoveryDataHash,
address emailRecoveryModule
)
public
{
EmailAuthMsg memory emailAuthMsg =
getRecoveryEmailAuthMessage(account, guardian, recoveryDataHash, emailRecoveryModule);
IEmailRecoveryModule(emailRecoveryModule).handleRecovery(emailAuthMsg, templateIdx);
}

// WithAccountSalt variation - used for creating incorrect recovery setups
// FIXME: not used???
function handleRecoveryWithAccountSalt(
address account,
address guardian,
bytes32 recoveryDataHash,
address emailRecoveryModule,
bytes32 optionalAccountSalt
)
public
{
EmailAuthMsg memory emailAuthMsg = getRecoveryEmailAuthMessageWithAccountSalt(
account, guardian, recoveryDataHash, emailRecoveryModule, optionalAccountSalt
);
IEmailRecoveryModule(emailRecoveryModule).handleRecovery(emailAuthMsg, templateIdx);
}

function getRecoveryEmailAuthMessage(
address account,
address guardian,
bytes32 recoveryDataHash,
address emailRecoveryModule
)
public
returns (EmailAuthMsg memory)
{
return getRecoveryEmailAuthMessageWithAccountSalt(
account, guardian, recoveryDataHash, emailRecoveryModule, bytes32(0)
);
}

// WithAccountSalt variation - used for creating incorrect recovery setups
function getRecoveryEmailAuthMessageWithAccountSalt(
address account,
address guardian,
bytes32 recoveryDataHash,
address emailRecoveryModule,
bytes32 optionalAccountSalt
)
public
returns (EmailAuthMsg memory)
{
string memory accountString = CommandUtils.addressToChecksumHexString(account);
string memory recoveryDataHashString = uint256(recoveryDataHash).toHexString(32);
string memory commandPart1 = string.concat("Recover account ", accountString);
string memory commandPart2 = string.concat(" using recovery hash ", recoveryDataHashString);

string memory command = string.concat(commandPart1, commandPart2);
bytes32 nullifier = generateNewNullifier();

bytes32 accountSalt;
if (optionalAccountSalt == bytes32(0)) {
accountSalt = getAccountSaltForGuardian(account, guardian);
} else {
accountSalt = optionalAccountSalt;
}

EmailProof memory emailProof = generateMockEmailProof(command, nullifier, accountSalt);

bytes[] memory commandParamsForRecovery = new bytes[](2);
commandParamsForRecovery[0] = abi.encode(account);
commandParamsForRecovery[1] = abi.encode(recoveryDataHashString);

return EmailAuthMsg({
templateId: IEmailRecoveryModule(emailRecoveryModule).computeRecoveryTemplateId(templateIdx),
commandParams: commandParamsForRecovery,
skippedCommandPrefix: 0,
proof: emailProof
});
}

function getAccountSaltForGuardian(
address account,
address guardian
Expand Down
Loading

0 comments on commit fcde8e2

Please sign in to comment.