Skip to content

Commit

Permalink
add registration logic to manager
Browse files Browse the repository at this point in the history
  • Loading branch information
andresaiello committed Oct 19, 2023
1 parent a47e4eb commit fde87ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ contract InvitationManager {
bytes32 r;
bytes32 s;
}
// Records the timestamp when a particular user gets verified.
mapping(address => uint256) public userVerificationTimestamps;

// Records the timestamp when a particular user accepted an invitation from an inviter.
mapping(address => mapping(address => uint256)) public acceptedInvitationsTimestamp;
Expand All @@ -21,12 +23,35 @@ contract InvitationManager {
// Total invites accepted by inviter by day (using the start timestamp of each day as key)
mapping(address => mapping(uint256 => uint256)) public totalInvitesByInviterByDay;

error UserAlreadyVerified();
error UnrecognizedInvitation();
error IndexOutOfBounds();
error InvitationAlreadyAccepted();
error CanNotInviteYourself();

event UserVerified(address indexed userAddress, uint256 verifiedAt);
event InvitationAccepted(address indexed inviter, address indexed invitee, uint256 timestamp);

function verifySignature(address inviter, Signature calldata signature) private pure {
function _markAsVerified(address user) internal {
// Check if the user is already verified
if (userVerificationTimestamps[user] > 0) revert UserAlreadyVerified();

userVerificationTimestamps[user] = block.timestamp;
emit UserVerified(user, block.timestamp);
}

function markAsVerified() external {
_markAsVerified(msg.sender);
}

function hasBeenVerified(address userAddress) external view returns (bool) {
return userVerificationTimestamps[userAddress] > 0;
}

function getVerifiedTimestamp(address userAddress) external view returns (uint256) {
return userVerificationTimestamps[userAddress];
}

function _verifySignature(address inviter, Signature calldata signature) private pure {
bytes32 payloadHash = keccak256(abi.encode(inviter));
bytes32 messageHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", payloadHash));

Expand All @@ -35,11 +60,11 @@ contract InvitationManager {
}

function confirmAndAcceptInvitation(address inviter, Signature calldata signature) external {
if (acceptedInvitationsTimestamp[inviter][msg.sender] != 0) revert InvitationAlreadyAccepted();

verifySignature(inviter, signature);
if (inviter == msg.sender) revert CanNotInviteYourself();
_verifySignature(inviter, signature);

acceptedInvitationsTimestamp[inviter][msg.sender] = block.timestamp;
_markAsVerified(msg.sender);

// Add the invitee to the inviter's list
inviteeLists[inviter].push(msg.sender);
Expand Down

This file was deleted.

0 comments on commit fde87ea

Please sign in to comment.