SDK Reference
The ZKFair SDK provides two variants for different use cases:
| SDK | Environment | Use Case |
|---|---|---|
ProviderSDK | Server (Node/Bun) | Model inference providers |
BrowserSDK | Browser | Web applications, fraud detection |
Installation
Section titled “Installation”bun add @zkfair/sdkProviderSDK
Section titled “ProviderSDK”Server-side SDK for model inference providers.
import { ProviderSDK } from "@zkfair/sdk";import { zkfairSchema } from "@zkfair/sdk/schema";import { drizzle } from "drizzle-orm/bun-sqlite";import { Database } from "bun:sqlite";
// Initialize database with ZKFair schemaconst sqlite = new Database("provider.db");const db = drizzle(sqlite, { schema: zkfairSchema });
// Create provider SDKconst provider = new ProviderSDK({ privateKey: process.env.PRIVATE_KEY as `0x${string}`, db, batchConfig: { batchSize: 100, // Queries per batch maxBatchAgeMs: 30 * 60 * 1000 // 30 minutes }});Query Operations
Section titled “Query Operations”// Store inference queryconst seqNum = await provider.insertQuery({ modelId: 1, features: [0.5, 1.2, 0.8, ...], // 14 features sensitiveAttr: 0, // Protected attribute prediction: 0.73, timestamp: Date.now()});
// Retrieve queryconst query = await provider.getQuery(seqNum);Receipt Operations
Section titled “Receipt Operations”// Create signed receipt for userconst receipt = await provider.createSignedReceipt({ seqNum, modelId: 1, features: [0.5, 1.2, 0.8, ...], sensitiveAttr: 0, prediction: 0.73, timestamp: Date.now()});
// Verify a receiptconst isValid = await provider.verifyReceipt(receipt, signerAddress);Batch Operations
Section titled “Batch Operations”// Check if batching neededconst shouldBatch = await provider.shouldBatch();
// Create and commit batch if thresholds metconst result = await provider.createBatchIfNeeded();if (result) { console.log(`Batch ${result.batchId} committed: ${result.txHash}`);}
// Generate Merkle proof for a queryconst proof = await provider.generateProof(seqNum);Audit Handling
Section titled “Audit Handling”// Auto-handle audit requestsprovider.watchAuditRequests((result) => { console.log(`Audit ${result.auditId}: ${result.passed ? "PASSED" : "FAILED"}`);});
// Start periodic batch checkingprovider.startPeriodicBatchCheck(5 * 60 * 1000); // 5 minutesBrowserSDK
Section titled “BrowserSDK”Client-side SDK for web applications.
import { BrowserSDK } from "@zkfair/sdk/browser";
const sdk = new BrowserSDK();Model API
Section titled “Model API”// List all registered modelsconst models = await sdk.model.list();
// Get model by weights hashconst model = await sdk.model.get("0x...");
// Get model by numeric IDconst model = await sdk.model.getById(1n);Batch API
Section titled “Batch API”// Get batch IDs for a modelconst batchIds = await sdk.batch.getIdsByModel(1n);
// Get batch detailsconst batch = await sdk.batch.get(batchIds[0]);// { modelId, merkleRoot, queryCount, seqNumStart, seqNumEnd, ... }Events API
Section titled “Events API”// Watch for new modelssdk.events.watchModelRegistered((event) => { console.log("New model:", event.modelId, event.weightsHash);});
// Watch for batch commitssdk.events.watchBatchCommitted((event) => { console.log("Batch committed:", event.batchId, event.merkleRoot);});
// Watch for audit requestssdk.events.watchAuditRequested((event) => { console.log("Audit requested:", event.auditId, event.sampleIndices);});Utility Exports
Section titled “Utility Exports”Leaf Hashing (Poseidon)
Section titled “Leaf Hashing (Poseidon)”import { hashRecordLeaf } from "@zkfair/sdk/browser";
// Compute leaf hash matching ZK circuitconst leafHash = hashRecordLeaf({ seqNum: 42, modelId: 1, features: [0.5, 1.2, ...], sensitiveAttr: 0, prediction: 0.73, timestamp: 1703260800000});Merkle Operations
Section titled “Merkle Operations”import { merkleRoot, verifyMerkleProof } from "@zkfair/sdk/merkle";
// Build Merkle rootconst root = await merkleRoot(leaves);
// Verify proofconst isValid = await verifyMerkleProof(leafHash, root, proof.siblings);Schema (for Drizzle)
Section titled “Schema (for Drizzle)”import { zkfairSchema, zkfairQueryLogs, zkfairBatches } from "@zkfair/sdk/schema";
// Merge with your own tablesconst db = drizzle(sqlite, { schema: { ...zkfairSchema, ...myTables }});