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

Modify check for no guardians & add tests #15

Merged
merged 2 commits into from
Jul 2, 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
9 changes: 7 additions & 2 deletions src/EmailRecoveryManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,17 @@ contract EmailRecoveryManager is EmailAccountRecovery, Initializable, IEmailReco
revert SetupAlreadyCalled();
}

setupGuardians(account, guardians, weights, threshold);

if (!IEmailRecoveryModule(emailRecoveryModule).isAuthorizedToRecover(account)) {
revert RecoveryModuleNotAuthorized();
}

// Allow recovery configuration without configuring guardians
if (guardians.length == 0 && weights.length == 0 && threshold == 0) {
guardianConfigs[account].initialized = true;
} else {
setupGuardians(account, guardians, weights, threshold);
}

RecoveryConfig memory recoveryConfig = RecoveryConfig(delay, expiry);
updateRecoveryConfig(recoveryConfig);

Expand Down
12 changes: 8 additions & 4 deletions src/libraries/GuardianUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ library GuardianUtils {
revert IncorrectNumberOfWeights();
}

if (threshold == 0 && guardianCount > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You removed && guardianCount > 0 because setupGuardians will be never called in configureRecovery when no guardian is set, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly

if (threshold == 0) {
revert ThresholdCannotBeZero();
}

Expand Down Expand Up @@ -85,8 +85,12 @@ library GuardianUtils {
revert ThresholdCannotExceedTotalWeight();
}

guardianConfigs[account] =
IEmailRecoveryManager.GuardianConfig(guardianCount, totalWeight, threshold, true);
guardianConfigs[account] = IEmailRecoveryManager.GuardianConfig({
guardianCount: guardianCount,
totalWeight: totalWeight,
threshold: threshold,
initialized: true
});
}

function updateGuardianStatus(
Expand Down Expand Up @@ -202,7 +206,7 @@ library GuardianUtils {
revert ThresholdCannotExceedTotalWeight();
}

// There has to be at least one Account guardian.
// Guardian weight should be at least 1
if (threshold == 0) {
revert ThresholdCannotBeZero();
}
Expand Down
1 change: 1 addition & 0 deletions test/unit/EmailRecoveryManager/addGuardian.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ contract EmailRecoveryManager_addGuardian_Test is UnitBase {
assertEq(guardianConfig.guardianCount, expectedGuardianCount);
assertEq(guardianConfig.totalWeight, expectedTotalWeight);
assertEq(guardianConfig.threshold, expectedThreshold);
assertEq(guardianConfig.initialized, true);
}
}
85 changes: 85 additions & 0 deletions test/unit/EmailRecoveryManager/configureRecovery.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ModuleKitHelpers } from "modulekit/ModuleKit.sol";
import { MODULE_TYPE_EXECUTOR } from "modulekit/external/ERC7579.sol";
import { IEmailRecoveryManager } from "src/interfaces/IEmailRecoveryManager.sol";
import { GuardianStorage, GuardianStatus } from "src/libraries/EnumerableGuardianMap.sol";
import { GuardianUtils } from "src/libraries/GuardianUtils.sol";
import { UnitBase } from "../UnitBase.t.sol";
import { IModule } from "erc7579/interfaces/IERC7579Module.sol";

Expand Down Expand Up @@ -73,10 +74,94 @@ contract EmailRecoveryManager_configureRecovery_Test is UnitBase {
assertEq(guardianConfig.guardianCount, guardians.length);
assertEq(guardianConfig.totalWeight, totalWeight);
assertEq(guardianConfig.threshold, threshold);
assertEq(guardianConfig.initialized, true);

GuardianStorage memory guardian =
emailRecoveryManager.getGuardian(accountAddress, guardians[0]);
assertEq(uint256(guardian.status), uint256(GuardianStatus.REQUESTED));
assertEq(guardian.weight, guardianWeights[0]);
}

function test_ConfigureRecovery_RevertWhen_ZeroGuardians() public {
instance.uninstallModule(MODULE_TYPE_EXECUTOR, recoveryModuleAddress, "");
vm.startPrank(accountAddress);
emailRecoveryModule.allowValidatorRecovery(
validatorAddress, isInstalledContext, functionSelector
);
address[] memory zeroGuardians;

vm.expectRevert(GuardianUtils.IncorrectNumberOfWeights.selector);
emailRecoveryManager.configureRecovery(
zeroGuardians, guardianWeights, threshold, delay, expiry
);
}

function test_ConfigureRecovery_RevertWhen_ZeroGuardianWeights() public {
instance.uninstallModule(MODULE_TYPE_EXECUTOR, recoveryModuleAddress, "");
vm.startPrank(accountAddress);
emailRecoveryModule.allowValidatorRecovery(
validatorAddress, isInstalledContext, functionSelector
);
uint256[] memory zeroGuardianWeights;

vm.expectRevert(GuardianUtils.IncorrectNumberOfWeights.selector);
emailRecoveryManager.configureRecovery(
guardians, zeroGuardianWeights, threshold, delay, expiry
);
}

function test_ConfigureRecovery_RevertWhen_ZeroThreshold() public {
instance.uninstallModule(MODULE_TYPE_EXECUTOR, recoveryModuleAddress, "");
vm.startPrank(accountAddress);
emailRecoveryModule.allowValidatorRecovery(
validatorAddress, isInstalledContext, functionSelector
);
uint256 zeroThreshold = 0;

vm.expectRevert(GuardianUtils.ThresholdCannotBeZero.selector);
emailRecoveryManager.configureRecovery(
guardians, guardianWeights, zeroThreshold, delay, expiry
);
}

function test_ConfigureRecovery_SucceedsWithNoGuardians() public {
instance.uninstallModule(MODULE_TYPE_EXECUTOR, recoveryModuleAddress, "");

address[] memory zeroGuardians;
uint256[] memory zeroGuardianWeights;
uint256 zeroThreshold = 0;

// Install recovery module - configureRecovery is called on `onInstall`
instance.installModule({
moduleTypeId: MODULE_TYPE_EXECUTOR,
module: recoveryModuleAddress,
data: abi.encode(
validatorAddress,
isInstalledContext,
functionSelector,
zeroGuardians,
zeroGuardianWeights,
zeroThreshold,
delay,
expiry
)
});

IEmailRecoveryManager.RecoveryConfig memory recoveryConfig =
emailRecoveryManager.getRecoveryConfig(accountAddress);
assertEq(recoveryConfig.delay, delay);
assertEq(recoveryConfig.expiry, expiry);

IEmailRecoveryManager.GuardianConfig memory guardianConfig =
emailRecoveryManager.getGuardianConfig(accountAddress);
assertEq(guardianConfig.guardianCount, zeroGuardians.length);
assertEq(guardianConfig.totalWeight, 0);
assertEq(guardianConfig.threshold, zeroThreshold);
assertEq(guardianConfig.initialized, true);

GuardianStorage memory guardian =
emailRecoveryManager.getGuardian(accountAddress, guardians[0]);
assertEq(uint256(guardian.status), uint256(GuardianStatus.NONE));
assertEq(guardian.weight, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ contract EmailRecoveryManager_deInitRecoveryFromModule_Test is UnitBase {
assertEq(guardianConfig.guardianCount, 0);
assertEq(guardianConfig.totalWeight, 0);
assertEq(guardianConfig.threshold, 0);
assertEq(guardianConfig.initialized, false);
}
}
1 change: 0 additions & 1 deletion test/unit/EmailRecoveryManager/getGuardianConfig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ contract EmailRecoveryManager_getGuardianConfig_Test is UnitBase {
function test_GetGuardianConfig_Succeeds() public {
IEmailRecoveryManager.GuardianConfig memory guardianConfig =
emailRecoveryManager.getGuardianConfig(accountAddress);
console2.log(expectedGuardianCount);
assertEq(guardianConfig.guardianCount, expectedGuardianCount);
assertEq(guardianConfig.totalWeight, expectedTotalWeight);
assertEq(guardianConfig.threshold, expectedThreshold);
Expand Down
1 change: 1 addition & 0 deletions test/unit/libraries/GuardianUtils/setupGuardians.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,6 @@ contract GuardianUtils_setupGuardians_Test is UnitBase {
assertEq(guardianConfig.guardianCount, expectedGuardianCount);
assertEq(guardianConfig.totalWeight, expectedTotalWeight);
assertEq(guardianConfig.threshold, expectedThreshold);
assertEq(guardianConfig.initialized, true);
}
}