@merkleproof/sdk
v0.1.0
Published
Decentralized file notarization with automatic Merkle Tree batching and blockchain anchoring
Maintainers
Readme
@merkleproof/sdk
Decentralized file notarization with automatic Merkle Tree batching and Polygon blockchain anchoring.
Installation
npm install @merkleproof/sdkQuick start
import { MerkleVault } from "@merkleproof/sdk";
import fs from "fs";
const vault = new MerkleVault({
pinataJwt: process.env.PINATA_JWT!,
privateKey: process.env.PRIVATE_KEY!,
contractAddress: process.env.CONTRACT_ADDRESS!,
batchStrategy: "threshold:10", // anchor every 10 documents
});
// 1. Notarize a file (hash + IPFS upload + queue)
const file = fs.readFileSync("./contrat.pdf");
const receipt = await vault.notarize(file, "contrat.pdf");
console.log(`Queued: ${receipt.fileHash} → IPFS ${receipt.cid}`);
// 2. Anchor the batch (build Merkle tree + send root to Polygon)
const batch = await vault.anchor();
console.log(`${batch.count} docs anchored. Tx: ${batch.txHash}`);
// 3. Export the proof for sharing
const proofJson = vault.exportProof(receipt.fileHash);
fs.writeFileSync("proof-contrat.json", proofJson);
// 4. Anyone can verify independently
const proof = JSON.parse(proofJson);
const result = await vault.verify(file, proof);
console.log(result.valid ? "✓ Authentic" : "✗ Tampered");Batch strategies
| Strategy | Trigger | Use case |
|----------|---------|----------|
| "manual" | Call vault.anchor() explicitly | Full control |
| "immediate" | Every file, individually | Critical docs (expensive) |
| "threshold:N" | When N files are queued | Variable volumes |
| "every-N-minutes" | Time-based interval | Periodic batching |
Standalone Merkle Tree
The MerkleTree class can be used independently:
import { MerkleTree, sha256 } from "@merkleproof/sdk";
const hashes = files.map((f) => sha256(f));
const tree = new MerkleTree(hashes);
const proof = tree.getProof(0);
const valid = MerkleTree.verify(hashes[0], proof, tree.root, 0);Architecture
@merkleproof/sdk
├── core/
│ ├── hasher.ts # SHA-256
│ ├── merkle-tree.ts # Tree construction + proof + verify
│ └── queue.ts # Batch queue with strategies
├── storage/
│ └── pinata.ts # IPFS adapter (Pinata Cloud)
├── blockchain/
│ └── contract.ts # Polygon smart contract interaction
└── index.ts # Public API (MerkleVault)License
MIT
