Skip to content

Commit

Permalink
feat: update events; add test for events
Browse files Browse the repository at this point in the history
  • Loading branch information
saleel committed Oct 17, 2023
1 parent 6961d1a commit 5bd21f3
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 207 deletions.
4 changes: 2 additions & 2 deletions packages/contracts/src/EmailWalletCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ contract EmailWalletCore is EmailWalletEvents, ReentrancyGuard, OwnableUpgradeab
relayerOfRandHash[randHash] = msg.sender;
relayerOfEmailAddr[emailAddr] = msg.sender;

emit RelayerRegistered(randHash, emailAddr, hostname);
emit RelayerRegistered(msg.sender, randHash, emailAddr, hostname);
}

/// @notice Update relayer's config (hostname only for now)
Expand All @@ -181,7 +181,7 @@ contract EmailWalletCore is EmailWalletEvents, ReentrancyGuard, OwnableUpgradeab

relayers[msg.sender].hostname = hostname;

emit RelayerConfigUpdated(relayers[msg.sender].randHash, hostname);
emit RelayerConfigUpdated(msg.sender, hostname);
}

/// Create new account and wallet for a user
Expand Down
32 changes: 21 additions & 11 deletions packages/contracts/src/interfaces/Events.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
pragma solidity ^0.8.12;

interface EmailWalletEvents {
event RelayerRegistered(bytes32 randHash, string emailAddr, string hostname);
event RelayerRegistered(address indexed addr, bytes32 randHash, string emailAddr, string hostname);

event RelayerConfigUpdated(bytes32 randHash, string hostname);
event RelayerConfigUpdated(address indexed addr, string hostname);

event AccountCreated(bytes32 emailAddrPointer, bytes32 accountKeyCommit, bytes32 walletSalt, bytes psiPoint);
event AccountCreated(
bytes32 emailAddrPointer,
bytes32 accountKeyCommit,
bytes32 indexed walletSalt,
bytes psiPoint
);

event AccountInitialized(bytes32 emailAddrPointer, bytes32 accountKeyCommit, bytes32 walletSalt);
event AccountInitialized(bytes32 emailAddrPointer, bytes32 accountKeyCommit, bytes32 indexed walletSalt);

event AccountTransported(bytes32 oldAccountKeyCommit, bytes32 newEmailAddrPointer, bytes32 newAccountKeyCommit);

event UnclaimedFundRegistered(
bytes32 emailAddrCommit,
bytes32 indexed emailAddrCommit,
address tokenAddr,
uint256 amount,
address sender,
Expand All @@ -22,12 +27,12 @@ interface EmailWalletEvents {
string emailAddr
);

event UnclaimedFundClaimed(bytes32 emailAddrCommit, address tokenAddr, uint256 amount, address recipient);
event UnclaimedFundClaimed(bytes32 indexed emailAddrCommit, address tokenAddr, uint256 amount, address recipient);

event UnclaimedFundVoided(bytes32 emailAddrCommit, address tokenAddr, uint256 amount, address sender);
event UnclaimedFundVoided(bytes32 indexed emailAddrCommit, address tokenAddr, uint256 amount, address sender);

event UnclaimedStateRegistered(
bytes32 emailAddrCommit,
bytes32 indexed emailAddrCommit,
address extensionAddr,
address sender,
uint256 expiryTime,
Expand All @@ -36,9 +41,14 @@ interface EmailWalletEvents {
string emailAddr
);

event UnclaimedStateClaimed(bytes32 emailAddrCommit, address recipient);
event UnclaimedStateClaimed(bytes32 indexed emailAddrCommit, address recipient);

event UnclaimedStateVoided(bytes32 emailAddrCommit, address sender);
event UnclaimedStateVoided(bytes32 indexed emailAddrCommit, address sender);

event ExtensionPublished(string name, address extensionAddr, string[][] subjectTemplates, uint256 maxExecutionGas);
event ExtensionPublished(
string indexed name,
address indexed extensionAddr,
string[][] subjectTemplates,
uint256 maxExecutionGas
);
}
2 changes: 1 addition & 1 deletion packages/contracts/src/interfaces/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ struct EmailOp {
uint256 feePerGas; // Amount of ETH to be charged per gas
bytes executeCallData; // data if the the command is "Execute"
address newWalletOwner; // Address of the new owner if the command is "Exit Email Wallet"
address newDkimRegistry; // Address of the new dkim registry if the command is "DKIM"
WalletParams walletParams; // Params when command = "Transfer" / "Send"
ExtensionParams extensionParams; // Serialized params for the extension based on the template
ExtensionManagerParams extManagerParams; // Params when command = "Install Extension" / "Uninstall Extension"
address newDkimRegistry; // Address of the new dkim registry if the command is "DKIM"
bytes emailProof; // ZK Proof of Email receipt
}

Expand Down
15 changes: 12 additions & 3 deletions packages/contracts/test/EmailWalletCore.account.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ contract AccountTest is EmailWalletCoreTestHelper {

function test_CreateAccount() public {
vm.startPrank(relayer);
vm.expectEmit();
emit AccountCreated(emailAddrPointer, accountKeyCommit, walletSalt, psiPoint);

core.createAccount(emailAddrPointer, accountKeyCommit, walletSalt, psiPoint, mockProof);
vm.stopPrank();

Wallet wallet = Wallet(payable(core.getWalletOfSalt(walletSalt)));
assertEq(wallet.owner(), address(core), "wallet owner is not walletAddr");

assertEq(core.accountKeyCommitOfPointer(emailAddrPointer), accountKeyCommit);
(address akRelayer, bool initialized, bytes32 akWalletSalt) = core.infoOfAccountKeyCommit(
accountKeyCommit
);
(address akRelayer, bool initialized, bytes32 akWalletSalt) = core.infoOfAccountKeyCommit(accountKeyCommit);
assertEq(akRelayer, relayer);
assertEq(core.pointerOfPSIPoint(psiPoint), emailAddrPointer);
assertTrue(!initialized);
Expand Down Expand Up @@ -92,6 +93,10 @@ contract AccountTest is EmailWalletCoreTestHelper {
function test_AccountInitailization() public {
vm.startPrank(relayer);
core.createAccount(emailAddrPointer, accountKeyCommit, walletSalt, psiPoint, mockProof);

vm.expectEmit();
emit AccountInitialized(emailAddrPointer, accountKeyCommit, walletSalt);

core.initializeAccount(emailAddrPointer, emailDomain, block.timestamp, emailNullifier, mockProof);
vm.stopPrank();

Expand Down Expand Up @@ -120,6 +125,10 @@ contract AccountTest is EmailWalletCoreTestHelper {

vm.startPrank(relayer2);
core.registerRelayer(relayer2RandHash, "mail@relayer2", "relayer2.com");

vm.expectEmit();
emit AccountTransported(accountKeyCommit, newEmailAddrPointer, newAccountKeyCommit);

core.transportAccount(
accountKeyCommit,
newEmailAddrPointer,
Expand Down
16 changes: 13 additions & 3 deletions packages/contracts/test/EmailWalletCore.relayer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import "./helpers/EmailWalletCoreTestHelper.sol";
contract RelayerTest is EmailWalletCoreTestHelper {
function test_RegisterRelayer() public {
bytes32 randHash = keccak256(abi.encodePacked(uint(1001)));
string memory emailAddr = "[email protected]";
string memory hostname = "relayer.xyz";

vm.startPrank(relayer);
core.registerRelayer(randHash, "[email protected]", "relayer.xyz");
vm.expectEmit();
emit RelayerRegistered(relayer, randHash, emailAddr, hostname);

core.registerRelayer(randHash, emailAddr, hostname);
vm.stopPrank();

(bytes32 deployedRandHash, , ) = core.relayers(relayer);
Expand Down Expand Up @@ -59,13 +64,18 @@ contract RelayerTest is EmailWalletCoreTestHelper {
// Update relayer hostname
function test_UpdateRelayerHostname() public {
bytes32 randHash = keccak256(abi.encodePacked(uint(1001)));
string memory newHostname = "newdomain.xyz";

vm.startPrank(relayer);
core.registerRelayer(randHash, "[email protected]", "relayer.xyz");
core.updateRelayerConfig("newdomain.xyz");

vm.expectEmit();
emit RelayerConfigUpdated(relayer, newHostname);
core.updateRelayerConfig(newHostname);
vm.stopPrank();

(, , string memory hostname) = core.relayers(relayer);
assertTrue(Strings.equal(hostname, "newdomain.xyz"));

assertTrue(Strings.equal(hostname, newHostname));
}
}
6 changes: 3 additions & 3 deletions packages/contracts/test/EmailWalletCore.uf.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,6 @@ contract UnclaimedFundTest is EmailWalletCoreTestHelper {

vm.startPrank(sender);
daiToken.approve(address(core), 100 ether);

// vm.expectEmit();
// emit UnclaimedFundClaimed(recipientEmailAddrCommit, address(daiToken), 100 ether, walletAddr);
core.registerUnclaimedFund{value: unclaimedFundClaimGas * maxFeePerGas}(
recipientEmailAddrCommit,
address(daiToken),
Expand All @@ -278,6 +275,9 @@ contract UnclaimedFundTest is EmailWalletCoreTestHelper {

// Relayer claim the unclaimed fund to account
vm.startPrank(relayer);
vm.expectEmit();
emit UnclaimedFundClaimed(recipientEmailAddrCommit, address(daiToken), 100 ether, walletAddr);

core.claimUnclaimedFund(recipientEmailAddrCommit, emailAddrPointer, mockProof);
vm.stopPrank();

Expand Down
6 changes: 6 additions & 0 deletions packages/contracts/test/EmailWalletCore.us.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ contract UnclaimedStateTest is EmailWalletCoreTestHelper {

// Relayer claim the unclaimed state to account
vm.startPrank(relayer);
vm.expectEmit();
emit UnclaimedStateClaimed(recipientEmailAddrCommit, walletAddr);

core.claimUnclaimedState(recipientEmailAddrCommit, emailAddrPointer, mockProof);
vm.stopPrank();

Expand Down Expand Up @@ -554,6 +557,9 @@ contract UnclaimedStateTest is EmailWalletCoreTestHelper {
core.createAccount(newEmailAddrPointer, newAccountKeyCommit, newWalletSalt, newPSIPoint, mockProof);
core.initializeAccount(newEmailAddrPointer, emailDomain, block.timestamp, emailNullifier2, mockProof);

vm.expectEmit();
emit UnclaimedStateClaimed(recipientEmailAddrCommit, newWalletAddr);

core.claimUnclaimedState(recipientEmailAddrCommit, newEmailAddrPointer, mockProof);
vm.stopPrank();

Expand Down
40 changes: 10 additions & 30 deletions packages/contracts/test/Integration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ contract IntegrationTest is IntegrationTestHelper {
);
require(relayerHash == relayer1RandHash, "Relayer hash mismatch");
require(emailAddrPointer == user1.emailAddrPointer, "Email address pointer mismatch");
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(
core.accountKeyCommitOfPointer(user1.emailAddrPointer)
);
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(core.accountKeyCommitOfPointer(user1.emailAddrPointer));
address user1Wallet = core.getWalletOfSalt(walletSalt);
vm.stopPrank();
vm.startPrank(user1Wallet);
Expand Down Expand Up @@ -179,9 +177,7 @@ contract IntegrationTest is IntegrationTestHelper {
);
require(relayerHash == relayer1RandHash, "Relayer hash mismatch");
require(emailAddrPointer == user1.emailAddrPointer, "Email address pointer mismatch");
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(
core.accountKeyCommitOfPointer(user1.emailAddrPointer)
);
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(core.accountKeyCommitOfPointer(user1.emailAddrPointer));
address user1Wallet = core.getWalletOfSalt(walletSalt);
vm.stopPrank();
vm.startPrank(user1Wallet);
Expand Down Expand Up @@ -225,9 +221,7 @@ contract IntegrationTest is IntegrationTestHelper {
);
require(relayerHash == relayer1RandHash, "Relayer hash mismatch");
require(emailAddrPointer == user1.emailAddrPointer, "Email address pointer mismatch");
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(
core.accountKeyCommitOfPointer(user1.emailAddrPointer)
);
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(core.accountKeyCommitOfPointer(user1.emailAddrPointer));
address user1Wallet = core.getWalletOfSalt(walletSalt);
(relayerHash, emailAddrPointer) = accountCreation(user2.emailAddr, relayer1Rand, user2.accountKey);
require(relayerHash == relayer1RandHash, "Relayer hash mismatch");
Expand Down Expand Up @@ -331,9 +325,7 @@ contract IntegrationTest is IntegrationTestHelper {
);
require(relayerHash == relayer1RandHash, "Relayer hash mismatch");
require(emailAddrPointer == user1.emailAddrPointer, "Email address pointer mismatch");
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(
core.accountKeyCommitOfPointer(user1.emailAddrPointer)
);
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(core.accountKeyCommitOfPointer(user1.emailAddrPointer));
address user1Wallet = core.getWalletOfSalt(walletSalt);
// address recipient = vm.addr(4);
vm.stopPrank();
Expand Down Expand Up @@ -468,9 +460,7 @@ contract IntegrationTest is IntegrationTestHelper {
);
require(relayerHash == relayer1RandHash, "Relayer hash mismatch");
require(emailAddrPointer == user1.emailAddrPointer, "Email address pointer mismatch");
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(
core.accountKeyCommitOfPointer(user1.emailAddrPointer)
);
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(core.accountKeyCommitOfPointer(user1.emailAddrPointer));
address user1Wallet = core.getWalletOfSalt(walletSalt);
claimFund(user1.emailAddr, relayer1Rand, rand1);
require(
Expand Down Expand Up @@ -539,9 +529,7 @@ contract IntegrationTest is IntegrationTestHelper {
);
require(relayerHash == relayer1RandHash, "Relayer hash mismatch");
require(emailAddrPointer == user1.emailAddrPointer, "Email address pointer mismatch");
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(
core.accountKeyCommitOfPointer(user1.emailAddrPointer)
);
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(core.accountKeyCommitOfPointer(user1.emailAddrPointer));
address user1Wallet = core.getWalletOfSalt(walletSalt);
vm.stopPrank();
vm.startPrank(user1Wallet);
Expand Down Expand Up @@ -607,9 +595,7 @@ contract IntegrationTest is IntegrationTestHelper {
);
require(relayerHash == relayer1RandHash, "Relayer hash mismatch");
require(emailAddrPointer == user1.emailAddrPointer, "Email address pointer mismatch");
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(
core.accountKeyCommitOfPointer(user1.emailAddrPointer)
);
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(core.accountKeyCommitOfPointer(user1.emailAddrPointer));
address user1Wallet = core.getWalletOfSalt(walletSalt);
DummyApes ape = DummyApes(nftExtension.addressOfNFTName("APE"));
ape.freeMint(user1Wallet, 1);
Expand Down Expand Up @@ -684,9 +670,7 @@ contract IntegrationTest is IntegrationTestHelper {
);
require(relayerHash == relayer1RandHash, "Relayer hash mismatch");
require(emailAddrPointer == user1.emailAddrPointer, "Email address pointer mismatch");
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(
core.accountKeyCommitOfPointer(user1.emailAddrPointer)
);
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(core.accountKeyCommitOfPointer(user1.emailAddrPointer));
address user1Wallet = core.getWalletOfSalt(walletSalt);
DummyApes ape = DummyApes(nftExtension.addressOfNFTName("APE"));
ape.freeMint(user1Wallet, 1);
Expand Down Expand Up @@ -785,9 +769,7 @@ contract IntegrationTest is IntegrationTestHelper {
);
require(relayerHash == relayer1RandHash, "Relayer hash mismatch");
require(emailAddrPointer == user1.emailAddrPointer, "Email address pointer mismatch");
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(
core.accountKeyCommitOfPointer(user1.emailAddrPointer)
);
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(core.accountKeyCommitOfPointer(user1.emailAddrPointer));
address user1Wallet = core.getWalletOfSalt(walletSalt);
claimState(user1.emailAddr, relayer1Rand, rand1);
require(ape.ownerOf(1) == user1Wallet, "User1 wallet does not own APE");
Expand All @@ -811,9 +793,7 @@ contract IntegrationTest is IntegrationTestHelper {
);
require(relayerHash == relayer1RandHash, "Relayer hash mismatch");
require(emailAddrPointer == user1.emailAddrPointer, "Email address pointer mismatch");
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(
core.accountKeyCommitOfPointer(user1.emailAddrPointer)
);
(, , bytes32 walletSalt) = core.infoOfAccountKeyCommit(core.accountKeyCommitOfPointer(user1.emailAddrPointer));
address user1Wallet = core.getWalletOfSalt(walletSalt);
DummyApes ape = DummyApes(nftExtension.addressOfNFTName("APE"));
ape.freeMint(user1Wallet, 1);
Expand Down
1 change: 0 additions & 1 deletion packages/contracts/test/mocks/TestDKIMRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity ^0.8.9;
import "@zk-email/contracts/interfaces/IDKIMRegistry.sol";

contract TestDKIMRegistry is IDKIMRegistry {

function getDKIMPublicKeyHash(string memory domainName) public view returns (bytes32) {
return bytes32(uint256(123));
}
Expand Down
Loading

0 comments on commit 5bd21f3

Please sign in to comment.