# Merkle Claim Flow

## Create and Claim Cred Merkle Type Process

This document explains the process of creating and claiming a Cred Merkle type, based on the provided sequence diagrams and smart contract code.

### Creation Process

#### Overview

The creation process involves interaction between four main components:

1. Contract
2. Frontend
3. Backend
4. Arweave (decentralized storage)

<figure><img src="https://2819029275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN3SP59WsdH4ATy0OAWIv%2Fuploads%2Fk4AwwC7oP5t9zY9e0gbJ%2Fimage.png?alt=media&#x26;token=e028f033-aae5-4d0d-a809-550d336f6cc4" alt=""><figcaption></figcaption></figure>

#### Step-by-Step Creation Process

1. **Request Signature**: Frontend initiates the process by requesting a signature.
2. **Upload CSV**: Backend uploads a CSV file with addresses to Arweave.
3. **Create and Upload Merkle Tree**: Backend creates a Merkle tree from the CSV data and uploads it to Arweave.
4. **Upload Cred Metadata**: Backend uploads the credential metadata to Arweave.
5. **Create and Response Signature**: Backend creates a signature and sends it to the Frontend.
6. **Call "createCred" Method**: Frontend calls the `createCred` method on the Contract.

### Claim Process

#### Overview

The claim process allows eligible users to claim their credentials using Merkle proofs.

<figure><img src="https://2819029275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN3SP59WsdH4ATy0OAWIv%2Fuploads%2FG7DIKod3jzATnAZBng4U%2Fimage.png?alt=media&#x26;token=584afdb5-4caf-4115-83cf-0a83f1c5d7e9" alt=""><figcaption></figcaption></figure>

#### Step-by-Step Claim Process

1. **Request Proof**: Frontend requests a proof for claiming.
2. **Fetch Cred Metadata**: Backend retrieves credential metadata from Arweave.
3. **Fetch Merkle Tree**: Backend fetches the Merkle tree from Arweave.
4. **Load Merkle Tree & Find Proof**: Backend processes the Merkle tree and finds the proof:

   ```javascript
   javascriptCopyconst json = await (await fetch(cred.verification.merkle_tree)).json();
   const tree = loadMerkleTree(json);

   let proof, data;
   for (const [i, v] of tree.entries()) {
     if (v[0].toLowerCase() == minter) {
       proof = tree.getProof(i) as Hex[string][];
       data = v[1];
     }
   }

   if (!proof) {
     return new Response("Not Eligible", { status: 400 });
   }
   ```
5. **Response Proof**: Backend sends the proof back to the Frontend.
6. **Call "claim" Method**: Frontend calls the `merkleClaim` method on the Contract.

### Key Points of the MerkleClaim Function:

1. **Input Parameters**:
   * `proof_`: Merkle proof
   * `encodeData_`: Encoded data containing minter address, referral address, and art ID
   * `mintArgs_`: Minting arguments including quantity and image URI
   * `leafPart_`: Additional data for the Merkle leaf
2. **Verification**:
   * Checks if the minter address is valid and the verification type is "MERKLE"
   * Verifies the Merkle proof using `MerkleProofLib.verifyCalldata`
3. **Claim Processing**:
   * Validates and updates the claim state
   * Processes the claim, which likely includes minting the NFT
4. **Event Emission**:
   * Emits an `ArtClaimedData` event with relevant information

#### Conclusion

The Cred Merkle type creation and claim process provides a secure and efficient way to distribute credentials to eligible addresses. The use of Merkle trees allows for gas-efficient verification on-chain, while storing larger datasets off-chain on Arweave ensures data availability and reduces on-chain storage costs.
