Merkle Type Cred

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)

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.

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:

    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.

MerkleClaim Function

The merkleClaim function in the smart contract handles the claiming process:

function merkleClaim(
    bytes32[] calldata proof_,
    bytes calldata encodeData_,
    MintArgs calldata mintArgs_,
    bytes32 leafPart_
) external payable whenNotPaused {
    (address minter_, address ref_, uint256 artId_) = abi.decode(encodeData_, (address, address, uint256));
    PhiArt storage art = arts[artId_];

    bytes32 credMerkleRootHash = credMerkleRoot[art.credChainId][art.credId];
    if (minter_ == address(0) || !art.verificationType.eq("MERKLE") || credMerkleRootHash == bytes32(0)) {
        revert InvalidMerkleProof();
    }
    if (
        !MerkleProofLib.verifyCalldata(
            proof_, credMerkleRootHash, keccak256(bytes.concat(keccak256(abi.encode(minter_, leafPart_))))
        )
    ) {
        revert InvalidMerkleProof();
    }

    _validateAndUpdateClaimState(artId_, minter_, mintArgs_.quantity);
    _processClaim(
        artId_, minter_, ref_, art.credCreator, mintArgs_.quantity, leafPart_, mintArgs_.imageURI, msg.value
    );

    emit ArtClaimedData(artId_, "MERKLE", minter_, ref_, art.credCreator, art.artAddress, mintArgs_.quantity);
}

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.

Last updated