Skip to content

Commit

Permalink
feat: clean leading zeros on zk proofs, removed vaultId checking and
Browse files Browse the repository at this point in the history
delete storage if not empty
  • Loading branch information
yum0e committed Jul 17, 2023
1 parent 3532232 commit 3b363c1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 36 deletions.
16 changes: 5 additions & 11 deletions abi/Airdrop.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,6 @@
"name": "AlreadyClaimed",
"type": "error"
},
{
"inputs": [
{
"internalType": "enum AuthType",
"name": "authType",
"type": "uint8"
}
],
"name": "AuthTypeNotFoundInVerifiedResult",
"type": "error"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -399,6 +388,11 @@
"internalType": "bytes",
"name": "response",
"type": "bytes"
},
{
"internalType": "address",
"name": "to",
"type": "address"
}
],
"name": "claimWithSismo",
Expand Down
2 changes: 1 addition & 1 deletion front/src/app/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Header: React.FC = () => {
the frontend <br />
3. The frontend forwards the response to ERC20 smart contract via claimWithSismo function{" "}
<br />
4. The smart contract the proofs contained in the response, mints ERC20 tokens and stores
4. The smart contract verifies the proofs contained in the response, mints ERC20 tokens and stores
verified claims and auths <br />
5. The frontend reads the verified claims and auths from the contract and displays them
</p>
Expand Down
2 changes: 1 addition & 1 deletion front/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export default function Home() {
{verifiedAuths && (
<>
<p>
{amountClaimed} tokens were claimd in total on {address}.
{amountClaimed} tokens were claimed in total on {address}.
</p>
<h3>Verified Auths</h3>
<table>
Expand Down
11 changes: 7 additions & 4 deletions front/src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ export function readibleHex(userId: string, startLength = 6, endLength = 4, sepa
return userId.substring(0, startLength) + separator + userId.substring(userId.length - endLength);
}

export function getProofDataForAuth(verifiedAuths: VerifiedAuth[], authType: AuthType): string | null {
export function getProofDataForAuth(
verifiedAuths: VerifiedAuth[],
authType: AuthType
): string | null {
for (const auth of verifiedAuths) {
if (auth.proofData && auth.authType === authType) {
return readibleHex("0x" + (auth.proofData as unknown as number).toString(16));
return readibleHex((auth.proofData as unknown as number).toString(16));
}
}

Expand All @@ -56,7 +59,7 @@ export function getProofDataForClaim(
): string | null {
for (const claim of verifiedClaims) {
if (claim.proofData && claim.claimType === claimType && claim.groupId === groupId) {
return readibleHex("0x" + (claim.proofData as unknown as number).toString(16));
return readibleHex((claim.proofData as unknown as number).toString(16));
}
}

Expand All @@ -70,4 +73,4 @@ export function getuserIdFromHex(hexUserId: string) {
} else {
return hexUserId; // returns the original string if '00' is not found
}
}
}
2 changes: 1 addition & 1 deletion front/src/utils/useContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function useContract({
address: transactions[0].contractAddress as `0x${string}`,
abi: [...AirdropABI, ...errorsABI],
functionName: "claimWithSismo",
args: [responseBytes],
args: [responseBytes, address],
chain,
enabled: Boolean(responseBytes),
};
Expand Down
43 changes: 25 additions & 18 deletions src/Airdrop.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,24 @@ contract Airdrop is ERC20, SismoConnect {
_setClaims(claimRequests);
}

function claimWithSismo(bytes memory response) public {
function claimWithSismo(bytes memory response, address to) public {
SismoConnectVerifiedResult memory result = verify({
responseBytes: response,
// checking response against requested auths
auths: _authRequests,
// checking response against requested claims
claims: _claimRequests,
// checking response against requested message signature
signature: buildSignature({message: abi.encode(msg.sender)})
signature: buildSignature({message: abi.encode(to)})
});

// it is the anonymous identifier of a user's vault for a specific app
// --> vaultId = hash(userVaultSecret, appId)
// used to avoid double claims
uint256 vaultId = result.getUserId(AuthType.VAULT);

// checking if the user has already claimed
if (claimed[vaultId]) {
revert AlreadyClaimed();
}

// marking that the user has claimed
claimed[vaultId] = true;

// airdrop amount = number of verified proofs
uint256 airdropAmount = (result.auths.length + result.claims.length) * 10 ** 18;
_mint(msg.sender, airdropAmount);
_mint(to, airdropAmount);

// cleaning previous results of the verification
_cleanVerifiedAuths();
_cleanVerifiedClaims();

// storing the result of the verification
for (uint256 i = 0; i < result.auths.length; i++) {
Expand All @@ -80,11 +71,10 @@ contract Airdrop is ERC20, SismoConnect {
_verifiedClaims.push(result.claims[i]);
emit ClaimVerified(result.claims[i]);
}
_verifiedSignedMessage =result.signedMessage;
_verifiedSignedMessage = result.signedMessage;
emit SignedMessageVerified(result.signedMessage);
}


function getVerifiedClaims() external view returns (VerifiedClaim[] memory) {
return _verifiedClaims;
}
Expand All @@ -109,4 +99,21 @@ contract Airdrop is ERC20, SismoConnect {
}
}

function _cleanVerifiedAuths() private {
uint256 verifiedAuthsLength = _verifiedAuths.length;
if (verifiedAuthsLength != 0) {
for (uint256 i = 0; i < verifiedAuthsLength; i++) {
_verifiedAuths.pop();
}
}
}

function _cleanVerifiedClaims() private {
uint256 verifiedClaimsLength = _verifiedClaims.length;
if (verifiedClaimsLength != 0) {
for (uint256 i = 0; i < verifiedClaimsLength; i++) {
_verifiedClaims.pop();
}
}
}
}

0 comments on commit 3b363c1

Please sign in to comment.