Framework-agnostic FHEVM SDK with wagmi-like developer experience for building privacy-preserving applications on Zama's Fully Homomorphic Encryption Virtual Machine.
π― Built for the Zama Bounty Challenge - A complete, reusable SDK that makes building confidential frontends simple, consistent, and developer-friendly.
π Live Example: https://fhe-pollution-monitor.vercel.app/
π» GitHub: https://github.com/OdaTorphy/fhevm-react-template
πΉ Demo Video: Download demo.mp4 to watch the full demonstration (streaming not available)
- π¨ Framework-Agnostic - Works with React, Next.js, Vue, Node.js, or vanilla JavaScript
- π Wagmi-like API - Intuitive hooks and composables familiar to web3 developers
- π¦ All-in-One Package - Wraps all required dependencies (fhevmjs, ethers, etc.)
- π Quick Setup - Less than 10 lines of code to get started
- π Complete FHEVM Flow - Initialize, encrypt inputs, decrypt outputs, contract interaction
- π§© Modular & Reusable - Clean components for different encryption/decryption scenarios
- π Well Documented - Comprehensive guides and code examples
- π TypeScript First - Full type safety with excellent IntelliSense
- β‘ Zero Configuration - Sensible defaults with easy customization
- π EIP-712 Support - Built-in userDecrypt and publicDecrypt utilities
# Install the SDK
npm install fhevm-sdk
# Or with yarn
yarn add fhevm-sdk
# Or with pnpm
pnpm add fhevm-sdkimport { createFhevmClient, encrypt, decrypt } from 'fhevm-sdk';
// 1. Initialize the client
const client = await createFhevmClient({
network: 'sepolia',
contractAddress: '0x...'
});
// 2. Encrypt sensitive data
const encryptedValue = await encrypt(client, 42);
// 3. Decrypt results (with EIP-712 signature)
const decryptedValue = await decrypt(client, encryptedData);fhevm-sdk/
βββ Core Engine (framework-agnostic)
β βββ FHEVM initialization
β βββ Encryption utilities
β βββ Decryption utilities (EIP-712)
β βββ Contract interaction
β
βββ React Adapters
β βββ useFhevmClient()
β βββ useEncrypt()
β βββ useDecrypt()
β βββ useContract()
β
βββ Vue Adapters
β βββ useFhevmClient()
β βββ useEncrypt()
β βββ useDecrypt()
β
βββ Utilities
βββ Type converters
βββ Network helpers
βββ Error handling
This is a monorepo containing:
fhevm-react-template/
βββ packages/
β βββ fhevm-sdk/ # Core SDK package
β βββ src/
β β βββ core/ # Framework-agnostic core
β β β βββ client.ts # Main FHEVM client
β β β βββ encrypt.ts # Encryption utilities
β β β βββ decrypt.ts # Decryption utilities
β β β βββ types.ts # Type definitions
β β βββ react/ # React hooks & provider
β β β βββ index.tsx # React adapters
β β βββ vue/ # Vue composables
β β β βββ index.tsx # Vue adapters
β β βββ utils/ # Utility functions
β β β βββ errors.ts # Custom error classes
β β β βββ helpers.ts # Helper functions
β β βββ index.ts # Main entry point
β βββ package.json
β
βββ templates/ # Ready-to-use templates
β βββ nextjs/ # Next.js template (symbolic link)
β βββ react/ # React template (symbolic link)
β
βββ examples/
β βββ nextjs-pollution-monitor/ # Full-featured Next.js application
β β βββ components/
β β β βββ examples/ # Banking & Medical examples
β β β βββ fhe/ # FHE components
β β β βββ ui/ # UI components
β β βββ app/ # Next.js App Router
β β βββ hooks/ # Custom hooks
β β βββ lib/ # Utilities
β βββ react-basic/ # Minimal React setup
β βββ node-cli/ # Command-line encryption tool
β βββ PrivacyPollutionMonitor/ # React pollution monitor with full FHE encryption
β
βββ contracts/ # Example smart contracts
βββ docs/ # Documentation files
βββ README.md # This file
import { FhevmClient } from 'fhevm-sdk';
const client = new FhevmClient({
network: 'sepolia',
rpcUrl: 'https://rpc.sepolia.org',
chainId: 11155111,
contractAddress: '0xYourContract',
// Optional: custom gateway for decryption
gatewayUrl: 'https://gateway.zama.ai'
});
await client.init();// Encrypt different types
const encrypted8 = await client.encrypt8(255);
const encrypted16 = await client.encrypt16(65535);
const encrypted32 = await client.encrypt32(1000000);
const encrypted64 = await client.encrypt64(BigInt('9007199254740991'));
const encryptedBool = await client.encryptBool(true);
const encryptedAddress = await client.encryptAddress('0x...');
// Batch encryption
const batch = await client.encryptBatch({
amount: 1000,
threshold: 500,
active: true
});import { Contract } from 'ethers';
// Connect to contract
const contract = new Contract(
contractAddress,
contractABI,
client.getSigner()
);
// Submit encrypted data
const tx = await contract.submitReport(
encrypted64,
encrypted8,
encryptedBool
);
await tx.wait();// User decrypt (requires signature)
const decrypted = await client.userDecrypt(
encryptedData,
contractAddress
);
// Public decrypt (no signature needed)
const publicData = await client.publicDecrypt(encryptedData);
// Batch decrypt
const results = await client.decryptBatch([
encryptedValue1,
encryptedValue2,
encryptedValue3
]);import { FhevmProvider } from 'fhevm-sdk/react';
function App() {
return (
<FhevmProvider
config={{
network: 'sepolia',
contractAddress: '0x...'
}}
>
<YourApp />
</FhevmProvider>
);
}import { useFhevmClient, useEncrypt, useDecrypt } from 'fhevm-sdk/react';
function EncryptionComponent() {
const client = useFhevmClient();
const { encrypt, isEncrypting } = useEncrypt();
const { decrypt, isDecrypting } = useDecrypt();
const handleSubmit = async (value: number) => {
// Encrypt
const encrypted = await encrypt(value, 'uint64');
// Send to contract
const tx = await contract.submit(encrypted);
await tx.wait();
// Decrypt result
const result = await decrypt(encryptedResult);
console.log('Decrypted:', result);
};
return (
<div>
<button onClick={() => handleSubmit(42)} disabled={isEncrypting}>
{isEncrypting ? 'Encrypting...' : 'Submit Encrypted Data'}
</button>
</div>
);
}<script setup lang="ts">
import { useFhevmClient, useEncrypt, useDecrypt } from 'fhevm-sdk/vue';
const client = useFhevmClient({
network: 'sepolia',
contractAddress: '0x...'
});
const { encrypt, isEncrypting } = useEncrypt(client);
const { decrypt, isDecrypting } = useDecrypt(client);
const handleEncrypt = async () => {
const encrypted = await encrypt(42, 'uint64');
console.log('Encrypted:', encrypted);
};
</script>
<template>
<button @click="handleEncrypt" :disabled="isEncrypting">
{{ isEncrypting ? 'Encrypting...' : 'Encrypt Data' }}
</button>
</template>This repository includes four comprehensive examples demonstrating FHEVM SDK integration across different environments:
Location: examples/nextjs-pollution-monitor/ or templates/nextjs/
A complete production-ready Next.js application showcasing:
- β Station registration with encrypted data
- β Confidential pollution reporting (euint64)
- β Homomorphic threshold checking
- β EIP-712 signed decryption
- β Role-based access control
- β Real-time encrypted dashboard
- β MetaMask integration
- β Sepolia testnet deployment
- β Banking Example: Confidential transactions
- β Medical Example: Privacy-preserving health records
Quick Start:
cd examples/nextjs-pollution-monitor
npm install
npm run devVisit http://localhost:3000
See examples/nextjs-pollution-monitor/README.md for details.
Location: examples/react-basic/ or templates/react/
A minimal React setup perfect for learning FHEVM SDK basics:
- β Basic encryption demonstration
- β Simple UI with loading states
- β Error handling
- β Educational info panel
- β Perfect starting point for new projects
Quick Start:
cd examples/react-basic
npm install
npm run devVisit http://localhost:5173
See examples/react-basic/README.md for details.
Location: examples/node-cli/
Command-line tool for FHEVM encryption operations:
- β Command-line encryption
- β Multiple encryption types (uint8, uint16, uint32, uint64, bool)
- β Progress indicators
- β Colored output
- β Perfect for testing and automation
Quick Start:
cd examples/node-cli
npm install
# Encrypt a value
node index.js encrypt 42 --contract 0xYourContractAddress --type uint64See examples/node-cli/README.md for details.
Location: examples/PrivacyPollutionMonitor/
A comprehensive environmental monitoring application with full FHEVM encryption:
- β Station registration and management
- β Encrypted pollution reporting (uint32, uint8)
- β Multiple pollutant types (PM2.5, PM10, SO2, NOx, Ozone, etc.)
- β Encrypted alert thresholds
- β Real-time dashboard with statistics
- β Full FHEVM SDK integration with React hooks
- β Component-based architecture
- β Production-ready React application
Quick Start:
cd examples/PrivacyPollutionMonitor
npm install
npm run devVisit http://localhost:3001
See examples/PrivacyPollutionMonitor/README-REACT.md for details.
Note: This example also includes a legacy static HTML version (index.html + app.js) for comparison. The React version (src/) demonstrates modern best practices with full SDK integration.
# Clone the repository
git clone https://github.com/yourusername/fhevm-react-template.git
cd fhevm-react-template
# Install all dependencies (monorepo)
npm install
# Build the SDK
npm run build:sdk
# Run tests
npm test
# Start development mode
npm run dev# Build
npm run build # Build all packages
npm run build:sdk # Build SDK only
npm run build:examples # Build all examples
# Development
npm run dev # Start all in dev mode
npm run dev:sdk # SDK dev mode with watch
npm run dev:nextjs # Next.js example
# Testing
npm test # Run all tests
npm run test:sdk # SDK tests only
npm run test:e2e # End-to-end tests
# Deployment
npm run deploy:contracts # Deploy to Sepolia
npm run deploy:frontend # Deploy Next.js to Vercel
# Linting & Formatting
npm run lint # Lint all packages
npm run format # Format code- Getting Started - Installation and basic usage
- API Reference - Complete API documentation
- React Guide - React-specific integration
- Vue Guide - Vue-specific integration
- TypeScript Guide - Type definitions and usage
- Encryption Strategies - Best practices for encrypting data
- Decryption Flows - EIP-712 signatures and permissions
- Contract Integration - Working with FHEVM contracts
- Error Handling - Common errors and solutions
- Performance - Optimization tips
- React Basic - Minimal React setup for learning SDK basics
- Next.js Application - Production-ready confidential monitoring system
- Privacy Pollution Monitor - Comprehensive React app with full FHE encryption
- Node.js CLI - Command-line encryption and testing tool
πΉ Watch the Demo: [demo.mp4]
The video demonstrates:
- SDK installation and setup (< 10 lines)
- Full-featured Next.js application integration
- React basic example walkthrough
- Node.js CLI tool usage
- Encryption and decryption flows
- Multi-framework support (React, Vue, Node.js)
- Design decisions and architecture
π Live Demo: https://fhe-pollution-monitor.vercel.app
Example Applications:
- Next.js Full Application: Production-ready confidential environmental monitoring system
- Live Demo: https://fhe-pollution-monitor.vercel.app
- Contract:
0xc61a1997F87156dfC96CA14E66fA9E3A02D36358(Sepolia)
- React Basic: Minimal setup for learning
- Node.js CLI: Command-line encryption tool
Smart Contract (Sepolia Testnet):
- Address:
0xc61a1997F87156dfC96CA14E66fA9E3A02D36358 - Network: Sepolia (Chain ID: 11155111)
- Explorer: View on Etherscan
The SDK separates core functionality from framework adapters, allowing:
- β Use in any JavaScript environment
- β Consistent behavior across frameworks
- β Easy testing and maintenance
- β Smaller bundle sizes (tree-shaking)
Web3 developers are familiar with wagmi's hook-based approach:
- β Intuitive for React developers
- β Composable and reusable
- β Built-in state management
- β TypeScript-first
Keeps SDK and examples in sync:
- β Shared dependencies
- β Easier testing
- β Consistent versioning
- β Simplified development workflow
// Client Management
createFhevmClient(config: FhevmConfig): Promise<FhevmClient>
FhevmClient.init(): Promise<void>
FhevmClient.getPublicKey(): Promise<string>
// Encryption
encrypt8(value: number): Promise<Uint8Array>
encrypt16(value: number): Promise<Uint8Array>
encrypt32(value: number): Promise<Uint8Array>
encrypt64(value: bigint): Promise<Uint8Array>
encryptBool(value: boolean): Promise<Uint8Array>
encryptAddress(address: string): Promise<Uint8Array>
// Decryption
userDecrypt(data: Uint8Array, contract: string): Promise<any>
publicDecrypt(data: Uint8Array): Promise<any>
decryptBatch(data: Uint8Array[]): Promise<any[]>
// Contract Helpers
getContract(address: string, abi: any): Contract
getSigner(): Signer
getProvider(): Provider// Provider
<FhevmProvider config={...}>
// Hooks
useFhevmClient(): FhevmClient
useEncrypt(): { encrypt, isEncrypting, error }
useDecrypt(): { decrypt, isDecrypting, error }
useContract(address, abi): Contract
useFhevmTransaction(contract, method): { send, isLoading, error }useFhevmClient(config): FhevmClient
useEncrypt(client): { encrypt, isEncrypting, error }
useDecrypt(client): { decrypt, isDecrypting, error }
useContract(client, address, abi): Contract- β Encryption happens in the browser/client
- β Keys never leave the client
- β Uses Zama's official fhevmjs library
- β User must sign to decrypt own data
- β Prevents unauthorized decryption
- β Domain-specific signatures
// β
Good: Encrypt sensitive data
const encrypted = await encrypt(socialSecurityNumber);
// β Bad: Send sensitive data in plain text
await contract.submit(socialSecurityNumber); // NEVER!
// β
Good: Verify decryption permissions
if (await hasPermission(address)) {
const decrypted = await decrypt(data);
}
// β
Good: Handle errors properly
try {
const result = await decrypt(data);
} catch (error) {
if (error.code === 'SIGNATURE_REQUIRED') {
// Request user signature
}
}npm run test:unitnpm run test:integrationnpm run test:e2enpm run test:coverageTarget: >80% code coverage
We welcome contributions! See CONTRIBUTING.md for guidelines.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Run tests:
npm test - Commit:
git commit -m 'feat: add amazing feature' - Push:
git push origin feature/amazing-feature - Open a Pull Request
- Core SDK with framework-agnostic API
- React and Vue adapters
- Three comprehensive examples (Next.js, React, Node.js)
- Comprehensive documentation
- Angular support
- Svelte support
- CLI tools for quick scaffolding
- Additional example applications
- Performance optimizations
- Batch encryption/decryption improvements
- Advanced caching strategies
- Developer DevTools extension
- Mobile SDK (React Native)
- Wallet integrations (MetaMask Snaps)
- FHEVM testing utilities
- Production-ready templates
MIT License - see LICENSE file for details.
- Zama - For pioneering Fully Homomorphic Encryption and the FHEVM platform
- fhevmjs - Official Zama FHE library for JavaScript
- wagmi - Inspiration for the hook-based API design
- Viem - Modern Ethereum library patterns
- Community - For feedback and contributions
- SDK Docs: docs.fhevm-sdk.dev
- Zama FHEVM: docs.zama.ai/fhevm
- API Reference: api.fhevm-sdk.dev
- GitHub Issues: Report bugs or request features
- Discussions: Ask questions and share ideas
- Discord: Join the Zama community
- Email: fhevm-sdk@protonmail.com
- Twitter: @FhevmSDK
FHEVM SDK - Making Confidential dApps Simple π
Built with β€οΈ for the Zama Bounty Challenge
Documentation β’ Examples β’ API Reference β’ Contributing