skills/storage/merkle-verification/SKILL.md
# Merkle Verification ## Metadata - **Category**: storage - **SDK**: `@0glabs/0g-ts-sdk` ^0.3.3 - **Activation Triggers**: "verify file", "merkle proof", "data integrity", "root hash", "check 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 - Node.js >= 18 - `@0glabs/0g-ts-sdk` installed ## Quick Workflow 1. Create
npx skillsauth add 0gfoundation/0g-agent-skills skills/storage/merkle-verificationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
@0glabs/0g-ts-sdk ^0.3.3Compute 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.
@0glabs/0g-ts-sdk installedZgFile from the file to verifytrue flag) for automatic verificationimport { ZgFile } from '@0glabs/0g-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);
import { ZgFile } from '@0glabs/0g-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!');
}
import { Indexer } from '@0glabs/0g-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');
}
async function filesMatch(filePath1: string, filePath2: string): Promise<boolean> {
const hash1 = await computeRootHash(filePath1);
const hash2 = await computeRootHash(filePath2);
return hash1 === hash2;
}
// 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
| 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 |
development
# Upload File to 0G Storage ## Metadata - **Category**: storage - **SDK**: `@0glabs/0g-ts-sdk` ^0.3.3, `ethers` ^6.13.0 - **Activation Triggers**: "upload file", "store on 0G", "ZgFile", "save to storage" ## Purpose Upload files to 0G decentralized storage using the ZgFile API and Indexer. Files are split into chunks, organized as a Merkle tree, and distributed across storage nodes. Returns a root hash for later retrieval. ## Prerequisites - Node.js >= 18 - `@0glabs/0g-ts-sdk` and `ethers`
development
# Download File from 0G Storage ## Metadata - **Category**: storage - **SDK**: `@0glabs/0g-ts-sdk` ^0.3.3, `ethers` ^6.13.0 - **Activation Triggers**: "download file", "retrieve from 0G", "get file", "fetch from storage" ## Purpose Download and verify files from 0G decentralized storage using a root hash. Supports verified downloads with Merkle proof validation to ensure data integrity. ## Prerequisites - Node.js >= 18 - `@0glabs/0g-ts-sdk` installed - Root hash of the file to download - `
development
# Storage + Chain Integration ## Metadata - **Category**: cross-layer - **SDK**: `@0glabs/0g-ts-sdk` ^0.3.3, `ethers` ^6.13.0 - **Activation Triggers**: "on-chain reference", "NFT metadata on 0G", "store hash on-chain", "registry contract", "chain and storage" ## Purpose Combine 0G Storage with 0G Chain smart contracts to create on-chain references to off-chain data. Common patterns include NFT metadata storage, content registries, and verifiable document systems. ## Prerequisites - Node
development
# Compute + Storage Integration ## Metadata - **Category**: cross-layer - **SDK**: `@0glabs/0g-serving-broker` ^0.6.5, `@0glabs/0g-ts-sdk` ^0.3.3, `ethers` ^6.13.0 - **Activation Triggers**: "AI with storage", "generate and store", "transcribe and store", "inference with storage", "AI pipeline" ## Purpose Combine 0G Compute (AI inference) with 0G Storage for end-to-end AI pipelines: generate content with AI and persist results to decentralized storage, or load data from storage and process