Merkle verification

From 0gfoundation/0g-agent-skills · View interactive version

Merkle verification is an AI agent skill from the GitHub repository 0gfoundation/0g-agent-skills. Compute 0G Storage root hashes and verify file integrity with Merkle proofs, before upload or after download. Use when the user wants to verify a file, check data integrity, compute a root hash or work with Merkle proofs on 0G.

How to use this skill

Skill file

Merkle Verification

Metadata

file"

Purpose

Compute root hashes and verify data integrity for files stored on 0G Storage. Uses Merkle tree

proofs to cryptographically verify that downloaded data matches what was originally uploaded.

Prerequisites

Quick Workflow

  1. Create a ZgFile from the file to verify
  2. Generate the Merkle tree
  3. Extract the root hash
  4. Compare against the expected root hash
  5. Close the file handle

Core Rules

ALWAYS

NEVER

Code Examples

Compute Root Hash

import { ZgFile } from '@0gfoundation/0g-storage-ts-sdk';

async function computeRootHash(filePath: string): Promise<string> {
  const file = await ZgFile.fromFilePath(filePath);
  try {
    const [tree, err] = await file.merkleTree();
    if (err) throw new Error(`Merkle tree error: ${err}`);
    return tree.rootHash();
  } finally {
    await file.close();
  }
}

// Usage
const hash = await computeRootHash('./my-file.pdf');
console.log('Root hash:', hash);

Verify File Integrity

import { ZgFile } from '@0gfoundation/0g-storage-ts-sdk';

async function verifyFile(filePath: string, expectedHash: string): Promise<boolean> {
  const file = await ZgFile.fromFilePath(filePath);
  try {
    const [tree, err] = await file.merkleTree();
    if (err) return false;

    const actualHash = tree.rootHash();
    const isValid = actualHash === expectedHash;

    console.log(`Expected: ${expectedHash}`);
    console.log(`Actual:   ${actualHash}`);
    console.log(`Valid:    ${isValid}`);

    return isValid;
  } finally {
    await file.close();
  }
}

// Usage
const isValid = await verifyFile('./downloaded-file.pdf', '0xabc123...');
if (!isValid) {
  console.error('File integrity check failed!');
}

Verified Download (Automatic Verification)

import { Indexer } from '@0gfoundation/0g-storage-ts-sdk';

async function downloadAndVerify(rootHash: string, outputPath: string): Promise<void> {
  const indexer = new Indexer(process.env.STORAGE_INDEXER!);

  // The third parameter enables automatic Merkle proof verification
  // Throws an error if verification fails
  await indexer.download(rootHash, outputPath, true);
  console.log('Downloaded and verified successfully');
}

Compare Two Files

async function filesMatch(filePath1: string, filePath2: string): Promise<boolean> {
  const hash1 = await computeRootHash(filePath1);
  const hash2 = await computeRootHash(filePath2);
  return hash1 === hash2;
}

Anti-Patterns

// BAD: Not closing file handle
const file = await ZgFile.fromFilePath('data.txt');
const [tree] = await file.merkleTree();
const hash = tree.rootHash();
// file.close() never called — memory leak!

// BAD: Unverified download in production
await indexer.download(rootHash, outputPath, false); // No verification!

// BAD: Comparing hashes case-sensitively when format may differ
if (hash1 === hash2) // Could fail if one is checksummed

Common Errors & Fixes

| Error | Cause | Fix |

| --------------------- | ----------------------- | --------------------------- |

| Merkle tree error | Empty or corrupted file | Verify file has content |

| Verification failed | Data was tampered with | Re-download from 0G Storage |

| ENOENT | File path doesn't exist | Check file path |

Related Skills

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.