diff --git a/packages/contracts/test/EmailAccountRecovery.t.sol b/packages/contracts/test/EmailAccountRecovery.t.sol index 435ddda3..7d70833b 100644 --- a/packages/contracts/test/EmailAccountRecovery.t.sol +++ b/packages/contracts/test/EmailAccountRecovery.t.sol @@ -33,11 +33,10 @@ contract EmailAccountRecoveryTest is SimpleWallet, Test { assertEq(address(this).balance, 1 ether); assertEq(receiver.balance, 0 ether); - - vm.startPrank(deployer); + vm.startPrank(deployer); this.transfer(receiver, 1 ether); vm.stopPrank(); - + assertEq(address(this).balance, 0 ether); assertEq(receiver.balance, 1 ether); } @@ -77,7 +76,6 @@ contract EmailAccountRecoveryTest is SimpleWallet, Test { assertEq(address(this).balance, 0 ether); assertEq(deployer.balance, 1 ether); - } function testExpectRevertWithdrawOnlyOwner() public { @@ -192,7 +190,7 @@ contract EmailAccountRecoveryTest is SimpleWallet, Test { testRequestGuardian(); require(guardians[guardian] == GuardianStatus.REQUESTED); - + transferOwnership(receiver); vm.startPrank(receiver); uint templateIdx = 0; @@ -217,7 +215,9 @@ contract EmailAccountRecoveryTest is SimpleWallet, Test { vm.stopPrank(); } - function testExpectRevertAcceptGuardianGuardianStatusMustBeRequested() public { + function testExpectRevertAcceptGuardianGuardianStatusMustBeRequested() + public + { testRequestGuardian(); require(guardians[guardian] == GuardianStatus.REQUESTED); @@ -249,7 +249,7 @@ contract EmailAccountRecoveryTest is SimpleWallet, Test { testRequestGuardian(); require(guardians[guardian] == GuardianStatus.REQUESTED); - + vm.startPrank(deployer); uint templateIdx = 0; bytes[] memory subjectParams = new bytes[](2); @@ -260,7 +260,9 @@ contract EmailAccountRecoveryTest is SimpleWallet, Test { vm.stopPrank(); } - function testExpectRevertAcceptGuardianInvalidWalletAddressInEmail() public { + function testExpectRevertAcceptGuardianInvalidWalletAddressInEmail() + public + { testRequestGuardian(); require(guardians[guardian] == GuardianStatus.REQUESTED); @@ -311,10 +313,12 @@ contract EmailAccountRecoveryTest is SimpleWallet, Test { subjectParams[1] = abi.encode(newSigner); vm.expectRevert(bytes("invalid guardian")); processRecovery(address(0x0), templateIdx, subjectParams, 0x0); - vm.stopPrank(); + vm.stopPrank(); } - function testExpectRevertRecoverWalletGuardianStatusMustBeAccepted() public { + function testExpectRevertRecoverWalletGuardianStatusMustBeAccepted() + public + { testAcceptGuardian(); assertEq(isRecovering, false); diff --git a/packages/contracts/test/EmailAuth.t.sol b/packages/contracts/test/EmailAuth.t.sol index 7059ce3e..62661c88 100644 --- a/packages/contracts/test/EmailAuth.t.sol +++ b/packages/contracts/test/EmailAuth.t.sol @@ -13,7 +13,6 @@ import "./helpers/DeploymentHelper.sol"; import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; contract EmailAuthTest is DeploymentHelper { - function setUp() public override { super.setUp(); } @@ -29,44 +28,44 @@ contract EmailAuthTest is DeploymentHelper { } function testUpdateDKIMRegistry() public { - vm.startPrank(deployer); - assertEq(emailAuth.dkimRegistryAddr(), address(dkim)); + + vm.startPrank(deployer); ECDSAOwnedDKIMRegistry newDKIM = new ECDSAOwnedDKIMRegistry(msg.sender); emailAuth.updateDKIMRegistry(address(newDKIM)); - assertEq(emailAuth.dkimRegistryAddr(), address(newDKIM)); - vm.stopPrank(); + + assertEq(emailAuth.dkimRegistryAddr(), address(newDKIM)); } function testExpectRevertUpdateDKIMRegistryInvalidDkimRegistryAddress() public { - vm.startPrank(deployer); + assertEq(emailAuth.dkimRegistryAddr(), address(dkim)); + vm.startPrank(deployer); vm.expectRevert(bytes("invalid dkim registry address")); emailAuth.updateDKIMRegistry(address(0)); - vm.stopPrank(); } function testUpdateVerifier() public { - vm.startPrank(deployer); - assertEq(emailAuth.verifierAddr(), address(verifier)); + + vm.startPrank(deployer); Verifier newVerifier = new Verifier(); emailAuth.updateVerifier(address(newVerifier)); - assertEq(emailAuth.verifierAddr(), address(newVerifier)); - vm.stopPrank(); + + assertEq(emailAuth.verifierAddr(), address(newVerifier)); } function testExpectRevertUpdateVerifierInvalidVerifierAddress() public { - vm.startPrank(deployer); + assertEq(emailAuth.verifierAddr(), address(verifier)); + vm.startPrank(deployer); vm.expectRevert(bytes("invalid verifier address")); emailAuth.updateVerifier(address(0)); - vm.stopPrank(); } @@ -83,6 +82,8 @@ contract EmailAuthTest is DeploymentHelper { function testInsertSubjectTemplate() public { emailAuth.insertSubjectTemplate(templateId, subjectTemplate); + string[] memory result = emailAuth.getSubjectTemplate(templateId); + assertEq(result, subjectTemplate); } function testExpectRevertInsertSubjectTemplateSubjectTemplateIsEmpty() @@ -97,24 +98,37 @@ contract EmailAuthTest is DeploymentHelper { public { emailAuth.insertSubjectTemplate(templateId, subjectTemplate); + string[] memory result = emailAuth.getSubjectTemplate(templateId); + assertEq(result, subjectTemplate); + vm.expectRevert(bytes("template id already exists")); emailAuth.insertSubjectTemplate(templateId, subjectTemplate); } function testUpdateSubjectTemplate() public { - vm.startPrank(deployer); + vm.expectRevert(bytes("template id not exists")); + string[] memory result = emailAuth.getSubjectTemplate(templateId); + vm.startPrank(deployer); this.testInsertSubjectTemplate(); - emailAuth.updateSubjectTemplate(templateId, newSubjectTemplate); + vm.stopPrank(); + result = emailAuth.getSubjectTemplate(templateId); + assertEq(result, subjectTemplate); + + vm.startPrank(deployer); + emailAuth.updateSubjectTemplate(templateId, newSubjectTemplate); vm.stopPrank(); + + result = emailAuth.getSubjectTemplate(templateId); + assertEq(result, newSubjectTemplate); } function testExpectRevertUpdateSubjectTemplateCallerIsNotTheOwner() public { vm.expectRevert( abi.encodeWithSelector( - OwnableUpgradeable.OwnableUnauthorizedAccount.selector, - 0x90193C961A926261B756D1E5bb255e67ff9498A1 + OwnableUpgradeable.OwnableUnauthorizedAccount.selector, + address(this) ) ); emailAuth.updateSubjectTemplate(templateId, subjectTemplate); @@ -143,29 +157,34 @@ contract EmailAuthTest is DeploymentHelper { function testDeleteSubjectTemplate() public { vm.startPrank(deployer); - this.testInsertSubjectTemplate(); - emailAuth.deleteSubjectTemplate(templateId); + vm.stopPrank(); + + string[] memory result = emailAuth.getSubjectTemplate(templateId); + assertEq(result, subjectTemplate); + vm.startPrank(deployer); + emailAuth.deleteSubjectTemplate(templateId); vm.stopPrank(); + + vm.expectRevert(bytes("template id not exists")); + emailAuth.getSubjectTemplate(templateId); } function testExpectRevertDeleteSubjectTemplateCallerIsNotTheOwner() public { vm.expectRevert( abi.encodeWithSelector( - OwnableUpgradeable.OwnableUnauthorizedAccount.selector, - 0x90193C961A926261B756D1E5bb255e67ff9498A1 + OwnableUpgradeable.OwnableUnauthorizedAccount.selector, + address(this) ) - ); + ); emailAuth.deleteSubjectTemplate(templateId); } function testExpectRevertDeleteSubjectTemplateTemplateIdNotExists() public { vm.startPrank(deployer); - vm.expectRevert(bytes("template id not exists")); emailAuth.deleteSubjectTemplate(templateId); - vm.stopPrank(); } @@ -225,24 +244,50 @@ contract EmailAuthTest is DeploymentHelper { function testAuthEmail() public { vm.startPrank(deployer); - this.testInsertSubjectTemplate(); EmailAuthMsg memory emailAuthMsg = buildEmailAuthMsg(); + vm.stopPrank(); + + assertEq( + emailAuth.usedNullifiers(emailAuthMsg.proof.emailNullifier), + false + ); + assertEq(emailAuth.lastTimestamp(), 0); + assertEq(emailAuth.authedHash(emailAuthMsg.proof.emailNullifier), 0x0); + + vm.startPrank(deployer); bytes32 msgHash = emailAuth.authEmail(emailAuthMsg); assertEq( msgHash, 0x97728a843151c01762d4f116e4d630f769faceda03589271805006ab8c512bcb ); - vm.stopPrank(); + + assertEq( + emailAuth.usedNullifiers(emailAuthMsg.proof.emailNullifier), + true + ); + assertEq(emailAuth.lastTimestamp(), emailAuthMsg.proof.timestamp); + assertEq( + emailAuth.authedHash(emailAuthMsg.proof.emailNullifier), + msgHash + ); } function testExpectRevertAuthEmailCallerIsNotTheOwner() public { EmailAuthMsg memory emailAuthMsg = buildEmailAuthMsg(); + + assertEq( + emailAuth.usedNullifiers(emailAuthMsg.proof.emailNullifier), + false + ); + assertEq(emailAuth.lastTimestamp(), 0); + assertEq(emailAuth.authedHash(emailAuthMsg.proof.emailNullifier), 0x0); + vm.expectRevert( abi.encodeWithSelector( - OwnableUpgradeable.OwnableUnauthorizedAccount.selector, - 0x90193C961A926261B756D1E5bb255e67ff9498A1 + OwnableUpgradeable.OwnableUnauthorizedAccount.selector, + address(this) ) ); emailAuth.authEmail(emailAuthMsg); @@ -250,58 +295,100 @@ contract EmailAuthTest is DeploymentHelper { function testExpectRevertAuthEmailTemplateIdNotExists() public { vm.startPrank(deployer); - EmailAuthMsg memory emailAuthMsg = buildEmailAuthMsg(); + vm.stopPrank(); + + assertEq( + emailAuth.usedNullifiers(emailAuthMsg.proof.emailNullifier), + false + ); + assertEq(emailAuth.lastTimestamp(), 0); + assertEq(emailAuth.authedHash(emailAuthMsg.proof.emailNullifier), 0x0); + + vm.startPrank(deployer); vm.expectRevert(bytes("template id not exists")); emailAuth.authEmail(emailAuthMsg); - vm.stopPrank(); } function testExpectRevertAuthEmailInvalidDkimPublicKeyHash() public { vm.startPrank(deployer); - this.testInsertSubjectTemplate(); EmailAuthMsg memory emailAuthMsg = buildEmailAuthMsg(); + vm.stopPrank(); + + assertEq( + emailAuth.usedNullifiers(emailAuthMsg.proof.emailNullifier), + false + ); + assertEq(emailAuth.lastTimestamp(), 0); + assertEq(emailAuth.authedHash(emailAuthMsg.proof.emailNullifier), 0x0); + + vm.startPrank(deployer); emailAuthMsg.proof.domainName = "invalid.com"; vm.expectRevert(bytes("invalid dkim public key hash")); emailAuth.authEmail(emailAuthMsg); - vm.stopPrank(); } function testExpectRevertAuthEmailEmailNullifierAlreadyUsed() public { vm.startPrank(deployer); - this.testInsertSubjectTemplate(); EmailAuthMsg memory emailAuthMsg = buildEmailAuthMsg(); + vm.stopPrank(); + + assertEq( + emailAuth.usedNullifiers(emailAuthMsg.proof.emailNullifier), + false + ); + assertEq(emailAuth.lastTimestamp(), 0); + assertEq(emailAuth.authedHash(emailAuthMsg.proof.emailNullifier), 0x0); + + vm.startPrank(deployer); emailAuth.authEmail(emailAuthMsg); vm.expectRevert(bytes("email nullifier already used")); emailAuth.authEmail(emailAuthMsg); - vm.stopPrank(); } function testExpectRevertAuthEmailInvalidAccountSalt() public { vm.startPrank(deployer); - this.testInsertSubjectTemplate(); EmailAuthMsg memory emailAuthMsg = buildEmailAuthMsg(); + vm.stopPrank(); + + assertEq( + emailAuth.usedNullifiers(emailAuthMsg.proof.emailNullifier), + false + ); + assertEq(emailAuth.lastTimestamp(), 0); + assertEq(emailAuth.authedHash(emailAuthMsg.proof.emailNullifier), 0x0); + + vm.startPrank(deployer); emailAuthMsg.proof.accountSalt = bytes32(uint256(1234)); vm.expectRevert(bytes("invalid account salt")); emailAuth.authEmail(emailAuthMsg); - vm.stopPrank(); } function testExpectRevertAuthEmailInvalidTimestamp() public { vm.startPrank(deployer); - this.testInsertSubjectTemplate(); - EmailAuthMsg memory emailAuthMsg = buildEmailAuthMsg(); - emailAuth.authEmail(emailAuthMsg); + bytes32 msgHash = emailAuth.authEmail(emailAuthMsg); + vm.stopPrank(); + assertEq( + emailAuth.usedNullifiers(emailAuthMsg.proof.emailNullifier), + true + ); + assertEq(emailAuth.lastTimestamp(), emailAuthMsg.proof.timestamp); + assertEq( + emailAuth.authedHash(emailAuthMsg.proof.emailNullifier), + msgHash + ); + + vm.startPrank(deployer); emailAuthMsg.proof.emailNullifier = 0x0; emailAuthMsg.proof.timestamp = 1694989812; vm.expectRevert(bytes("invalid timestamp")); @@ -312,23 +399,38 @@ contract EmailAuthTest is DeploymentHelper { function testExpectRevertAuthEmailInvalidSubject() public { vm.startPrank(deployer); - this.testInsertSubjectTemplate(); - EmailAuthMsg memory emailAuthMsg = buildEmailAuthMsg(); + vm.stopPrank(); + + assertEq( + emailAuth.usedNullifiers(emailAuthMsg.proof.emailNullifier), + false + ); + assertEq(emailAuth.lastTimestamp(), 0); + assertEq(emailAuth.authedHash(emailAuthMsg.proof.emailNullifier), 0x0); + + vm.startPrank(deployer); emailAuthMsg.subjectParams[0] = abi.encode(2 ether); vm.expectRevert(bytes("invalid subject")); emailAuth.authEmail(emailAuthMsg); - vm.stopPrank(); } function testExpectRevertAuthEmailInvalidEmailProof() public { vm.startPrank(deployer); - this.testInsertSubjectTemplate(); - EmailAuthMsg memory emailAuthMsg = buildEmailAuthMsg(); + vm.stopPrank(); + + assertEq( + emailAuth.usedNullifiers(emailAuthMsg.proof.emailNullifier), + false + ); + assertEq(emailAuth.lastTimestamp(), 0); + assertEq(emailAuth.authedHash(emailAuthMsg.proof.emailNullifier), 0x0); + + vm.startPrank(deployer); vm.mockCall( address(verifier), abi.encodeWithSelector( @@ -339,7 +441,6 @@ contract EmailAuthTest is DeploymentHelper { ); vm.expectRevert(bytes("invalid email proof")); emailAuth.authEmail(emailAuthMsg); - vm.stopPrank(); } @@ -372,8 +473,8 @@ contract EmailAuthTest is DeploymentHelper { function testExpectRevertSetTimestampCheckEnabled() public { vm.expectRevert( abi.encodeWithSelector( - OwnableUpgradeable.OwnableUnauthorizedAccount.selector, - 0x90193C961A926261B756D1E5bb255e67ff9498A1 + OwnableUpgradeable.OwnableUnauthorizedAccount.selector, + address(this) ) ); emailAuth.setTimestampCheckEnabled(false);