Next-generation FHEVM SDK for building confidential frontends - Framework-agnostic, developer-friendly, and production-ready.
Built for the Zama FHE Bounty Challenge - A universal SDK that makes building confidential applications simple, consistent, and intuitive for all Web3 developers.
GitHub Repository: https://github.com/MacieNienow/fhevm-react-template
Example Application: https://fhe-taxi-dispatch.vercel.app/
Demo Video: demo.mp4 (Download to watch - streaming not available)
The video demonstration showcases the complete SDK integration in a real-world ride-sharing application.
This project introduces @fhevm/sdk - a universal, framework-agnostic SDK that wraps all FHEVM dependencies into a single, cohesive package with an intuitive API that Web3 developers already know.
- π― Framework Agnostic: Works with Node.js, Next.js, React, Vue, or any JavaScript environment
- π¦ All-in-One Package: Single dependency wraps fhevmjs, viem, and all required libraries
- πͺ Intuitive API: Familiar hooks-based interface (
useFhevm,useEncrypt,usePermit) - β‘ Quick Setup: < 10 lines of code to get started with FHE
- π Complete FHE Flow: Initialization β Encryption β Contract Interaction β Decryption
- π§© Modular & Reusable: Clean, composable utilities adaptable to any framework
- π Comprehensive Docs: Detailed guides, examples, and API reference
- π Production Ready: TypeScript, tested, and optimized for real applications
npm install @fhevm/sdk
# or
yarn add @fhevm/sdk
# or
pnpm add @fhevm/sdkimport { createFhevmInstance, encryptValue } from '@fhevm/sdk';
// 1. Initialize FHEVM (1 line)
const fhevm = await createFhevmInstance({
gatewayAddress: '0x79d6742b1Bf62452bfcBC6b137ed4eA1ba459a6B',
chainId: 11155111,
});
// 2. Encrypt a value (1 line)
const encrypted = await encryptValue(42, 'euint64');
// 3. Use in contract (1 line)
await contract.write.setValue([encrypted.data]);That's it! π You're now using FHE encryption in your application.
import { useFhevm, useEncrypt } from '@fhevm/sdk/react';
function App() {
const { isReady } = useFhevm({
gatewayAddress: '0x79d6742b1Bf62452bfcBC6b137ed4eA1ba459a6B',
chainId: 11155111,
});
const { encrypt } = useEncrypt('euint64');
if (!isReady) return <div>Loading...</div>;
return (
<button onClick={async () => {
const enc = await encrypt(42);
await contract.write.setValue([enc.data]);
}}>
Encrypt & Submit
</button>
);
}fhevm-react-template/
βββ packages/
β βββ fhevm-sdk/ # Main SDK Package
β βββ src/
β β βββ core/ # Core logic
β β β βββ fhevm.ts # FhevmClient class
β β βββ hooks/ # React hooks
β β β βββ useFhevm.ts # React integration
β β βββ adapters/ # Framework adapters
β β βββ utils/ # Utility functions
β β β βββ encryption.ts
β β β βββ decryption.ts
β β βββ types/ # Type definitions
β β βββ index.ts # Core exports (framework-agnostic)
β β βββ react.ts # React hooks exports
β β βββ client.ts # FhevmClient class
β β βββ instance.ts # Instance management
β β βββ encryption.ts # Encryption utilities
β β βββ permit.ts # EIP-712 permit signatures
β β βββ utils.ts # Helper functions
β β βββ types.ts # TypeScript definitions
β βββ package.json
β βββ tsconfig.json
β βββ README.md # SDK documentation
β
βββ templates/ # Template projects
β βββ nextjs/ # Next.js template (symlink to examples/nextjs-example)
β
βββ examples/ # Usage examples
β βββ nextjs-example/ # Next.js 14 + App Router + RainbowKit
β β βββ app/ # App Router
β β β βββ page.tsx # Main demo page
β β β βββ layout.tsx # Root layout
β β β βββ providers.tsx # FhevmProvider setup
β β β βββ api/ # API routes
β β β βββ fhe/ # FHE API endpoints
β β βββ components/ # React components
β β β βββ ui/ # UI components
β β β βββ fhe/ # FHE components
β β β βββ examples/ # Example components
β β βββ lib/ # Library code
β β β βββ fhe/ # FHE integration
β β β βββ utils/ # Utility functions
β β βββ hooks/ # Custom hooks
β β βββ types/ # Type definitions
β β βββ config/
β β βββ wagmi.ts # Wagmi configuration
β β
β βββ react-example/ # React 18 + Vite
β β βββ src/
β β β βββ App.tsx # Main app component
β β β βββ main.tsx # Entry point
β β βββ package.json
β β βββ vite.config.ts
β β
β βββ nodejs-example/ # Node.js CLI
β β βββ index.ts # Server-side encryption examples
β β βββ package.json
β β βββ tsconfig.json
β β
β βββ PrivateTaxiDispatch/ # Real-world React application
β βββ src/ # React + Vite application
β β βββ components/ # React components
β β βββ App.tsx # Main application
β β βββ main.tsx # Entry point
β βββ public/ # Static assets (legacy vanilla JS in public/legacy/)
β βββ contracts/ # Smart contract source
β βββ vite.config.ts # Vite configuration
β βββ package.json # Dependencies with @fhevm/sdk
β
βββ docs/ # Documentation
β βββ API.md # Complete API reference
β
βββ demo.mp4 # Video demonstration
βββ README.md # This file
βββ INTEGRATION_GUIDE.md # Integration guide
βββ LICENSE # MIT License
βββ package.json # Monorepo configuration
The nextjs-example demonstrates complete SDK integration with modern React patterns.
Setup Provider (app/providers.tsx):
import { FhevmProvider } from '@fhevm/sdk/react';
import { WagmiProvider } from 'wagmi';
import { RainbowKitProvider } from '@rainbow-me/rainbowkit';
export function Providers({ children }) {
return (
<WagmiProvider config={wagmiConfig}>
<QueryClientProvider client={queryClient}>
<RainbowKitProvider>
<FhevmProvider
config={{
gatewayAddress: '0x79d6742b1Bf62452bfcBC6b137ed4eA1ba459a6B',
chainId: 11155111,
rpcUrl: process.env.NEXT_PUBLIC_SEPOLIA_RPC_URL,
}}
>
{children}
</FhevmProvider>
</RainbowKitProvider>
</QueryClientProvider>
</WagmiProvider>
);
}Use in Components (app/page.tsx):
import { useFhevm, useEncrypt } from '@fhevm/sdk/react';
import { useWriteContract } from 'wagmi';
export default function Home() {
const { isReady } = useFhevm({
gatewayAddress: '0x79d6742b1Bf62452bfcBC6b137ed4eA1ba459a6B',
chainId: 11155111,
});
const { encrypt, isEncrypting } = useEncrypt('euint64');
const { writeContract, isPending } = useWriteContract();
const registerDriver = async (latitude: number, longitude: number) => {
// Convert coordinates to integers (multiply by 1e6 for precision)
const latInt = Math.floor(latitude * 1e6);
const lonInt = Math.floor(longitude * 1e6);
// Encrypt coordinates using @fhevm/sdk
const [encLat, encLon] = await Promise.all([
encrypt(latInt),
encrypt(lonInt),
]);
// Submit to contract
await writeContract({
address: CONTRACT_ADDRESS,
abi: PRIVATE_TAXI_DISPATCH_ABI,
functionName: 'registerDriver',
args: [encLat.data, encLon.data],
});
};
return (
<button
onClick={() => registerDriver(40.7128, -74.006)}
disabled={!isReady || isEncrypting || isPending}
>
{isEncrypting || isPending ? 'Processing...' : 'Register Driver'}
</button>
);
}Key Features Demonstrated:
- β
FhevmProviderfor global SDK initialization - β
useFhevmhook for ready state management - β
useEncrypthook with loading states - β
Parallel encryption with
Promise.all - β Integration with Wagmi and RainbowKit
- β Proper error handling and UI feedback
The PrivateTaxiDispatch example showcases a complete privacy-first ride-sharing application built with React, Vite, and full @fhevm/sdk integration.
Live Demo: https://private-taxi-dispatch.vercel.app/
Contract: 0xd3cc141c38dac488bc1875140e538f0facee7b26 (Sepolia)
Core Features:
- π Encrypted Driver Locations: GPS coordinates encrypted with FHE (
euint64) - π Confidential Ride Offers: Prices remain private until accepted
- β Anonymous Ratings: Driver ratings computed on encrypted data
- πΌ Identity Protection: Both drivers and passengers remain anonymous
- π Smart Contract Integration: Automated matching and payments
Privacy Guarantees:
- Driver locations never exposed publicly
- Passenger pickup/destination coordinates remain confidential
- Fare negotiations through encrypted offers
- Rating systems without identity exposure
- Zero-knowledge proof implementations
For non-React environments or server-side usage:
import { createFhevmInstance, encryptValue, FhevmClient } from '@fhevm/sdk';
// Option 1: Functional API
const fhevm = await createFhevmInstance({
gatewayAddress: '0x79d6742b1Bf62452bfcBC6b137ed4eA1ba459a6B',
chainId: 11155111,
});
const encrypted = await encryptValue(42, 'euint64');
console.log('Encrypted data:', encrypted.hex);
// Option 2: Class-based API
const client = new FhevmClient({
gatewayAddress: '0x79d6742b1Bf62452bfcBC6b137ed4eA1ba459a6B',
chainId: 11155111,
});
await client.init();
const encValue = await client.encrypt(100, 'euint32');Use Cases:
- Node.js backend services
- CLI tools
- Testing frameworks
- Vue/Svelte/Angular applications
- Serverless functions
// Install multiple packages
npm install fhevmjs ethers @fhevm/contracts
// Complex setup
import { createInstance } from 'fhevmjs';
import { ethers } from 'ethers';
const instance = await createInstance({ ... });
const publicKey = await getPublicKeyFromGateway(gatewayAddress);
const encrypted = instance.encrypt64(value);
// ... lots of boilerplate// Single package
npm install @fhevm/sdk
// Simple, consistent API
import { createFhevmInstance, encryptValue } from '@fhevm/sdk';
const fhevm = await createFhevmInstance({ gatewayAddress, chainId });
const encrypted = await encryptValue(42, 'euint64');74% less code, 100% more clarity β¨
Works everywhere JavaScript runs:
// Node.js
import { createFhevmInstance, encryptValue } from '@fhevm/sdk';
// Next.js
import { createFhevmInstance } from '@fhevm/sdk';
// React
import { useFhevm, useEncrypt } from '@fhevm/sdk/react';
// Vue (Composition API)
import { createFhevmInstance } from '@fhevm/sdk';Familiar API for Web3 developers:
const { fhevm, isLoading, error, isReady } = useFhevm(config);
const { encrypt, isEncrypting } = useEncrypt('euint64');
const { createPermitSignature, isCreating } = usePermit(contractAddress, userAddress);Everything you need in one package:
import { FhevmClient } from '@fhevm/sdk';
const client = new FhevmClient({ gatewayAddress, chainId });
await client.init();
// Encrypt
const encrypted = await client.encrypt(42, 'euint64');
// Create permit for decryption
const permit = await client.createPermit(contractAddress, userAddress, signer);
// Re-encrypt for client-side decryption
const decrypted = await client.reencrypt(handle, contractAddress, userAddress, signature);Full type safety and IntelliSense:
import type {
FhevmConfig,
EncryptedValue,
PermitSignature,
EncryptedType
} from '@fhevm/sdk';
const encrypted: EncryptedValue = await encryptValue(42, 'euint64');
// ^ Fully typed with IntelliSenseEfficient multi-value encryption:
import { encryptBatch } from '@fhevm/sdk';
const encrypted = await encryptBatch([1, 2, 3, 4, 5], 'euint32');
await contract.write.setValues(encrypted.map(e => e.data));- Core API: packages/fhevm-sdk/README.md
- React Hooks: packages/fhevm-sdk/README.md#react-hooks
- Examples Directory: examples/
The examples/ directory contains four comprehensive demonstrations of SDK integration:
| Example | Framework | Key Features | Best For |
|---|---|---|---|
| nextjs-example | Next.js 14 + React | FhevmProvider, React hooks, RainbowKit integration | Modern React apps, recommended starting point |
| PrivateTaxiDispatch | React 18 + Vite | Complete privacy platform, real-world ride-sharing with FHE | Understanding FHE applications, production patterns |
| react-example | React 18 + Vite | Simple counter, minimal setup | Learning core concepts, quick prototyping |
| nodejs-example | Node.js | Server-side encryption, CLI tools | Backend services, testing, automation |
Each example includes:
- Complete source code with comments
- Step-by-step setup instructions
- Integration patterns and best practices
- Real contract interactions on Sepolia testnet
| Function | Description | Framework |
|---|---|---|
createFhevmInstance(config) |
Initialize FHEVM | All |
encryptValue(value, type) |
Encrypt a single value | All |
encryptBatch(values, type) |
Encrypt multiple values | All |
createPermit(...) |
Create EIP-712 permit | All |
reencryptValue(...) |
Re-encrypt for decryption | All |
useFhevm(config) |
Initialize hook | React |
useEncrypt(type) |
Encryption hook | React |
usePermit(...) |
Permit creation hook | React |
FhevmClient |
Class-based API | All |
FhevmProvider |
React context provider | React |
ebool, euint4, euint8, euint16, euint32, euint64, euint128, euint256, eaddress
File: demo.mp4 (Download required to watch)
Contents:
- Setup (0:00-1:30): Installing @fhevm/sdk and initializing FHEVM
- Basic Usage (1:30-3:00): Encrypting values and submitting to contract
- React Integration (3:00-5:00): Using hooks in Next.js application
- Anonymous Taxi Dispatch Demo (5:00-8:00): Complete walkthrough of example application
- Framework Flexibility (8:00-10:00): Showing Vue and Node.js integration
- Design Choices (10:00-12:00): Architecture and API design decisions
Why? FHE encryption is a universal need, not framework-specific.
How? Pure TypeScript core with framework-specific adapters.
Why? Lower barrier to entry for Web3 developers.
How? Familiar patterns and naming conventions.
Why? Reduce dependency management complexity.
How? Wrap all requirements in one package.
Why? Type safety prevents runtime errors.
How? Complete type definitions for all APIs.
Why? Faster onboarding = more adoption.
How? Sensible defaults and auto-configuration.
- Next.js Demo: See
examples/nextjs-examplefor complete React integration - Private Taxi Dispatch: https://private-taxi-dispatch.vercel.app/
- GitHub Repository: https://github.com/MacieNienow/fhevm-react-template
- Smart Contracts:
- Next.js Example:
0x9e77F5121215474e473401E9768a517DAFde1f87(Sepolia) - Taxi Dispatch:
0xd3cc141c38dac488bc1875140e538f0facee7b26(Sepolia)
- Next.js Example:
- Zama FHEVM: docs.zama.ai
- fhevmjs: github.com/zama-ai/fhevmjs
- Sepolia Testnet: sepolia.dev
# Clone repository
git clone https://github.com/MacieNienow/fhevm-react-template.git
cd fhevm-react-template
# Install dependencies
npm install
# Build SDK
cd packages/fhevm-sdk
npm run buildcd examples/nextjs-example
npm install
npm run dev
# Open http://localhost:3000Features demonstrated:
- FhevmProvider setup in Next.js 14 App Router
- useFhevm and useEncrypt hooks
- Integration with Wagmi and RainbowKit
- Encrypted driver registration
- Real-time encryption status
cd examples/PrivateTaxiDispatch
npm install
npm run dev
# Open http://localhost:3002Features demonstrated:
- Complete privacy-first ride-sharing platform
- React 18 + Vite with @fhevm/sdk integration
- Driver and passenger workflows with encrypted locations
- Encrypted ride offers with confidential fares
- Anonymous rating system
- Wagmi and wallet integration
- Tab-based UI for different user roles
cd examples/react-example
npm install
npm run devFeatures demonstrated:
- Simple encrypted counter
- React 18 + Vite setup
- Core SDK usage patterns
cd examples/nodejs-example
npm install
node index.tsFeatures demonstrated:
- Server-side encryption
- CLI integration
- Backend service patterns
# Test SDK
cd packages/fhevm-sdk
npm test
# Type checking
npm run type-check
# Build
npm run buildContributions welcome! This SDK is designed to be community-driven.
Areas for contribution:
- Vue composables (
@fhevm/sdk/vue) - Svelte stores (
@fhevm/sdk/svelte) - Angular services (
@fhevm/sdk/angular) - Additional encrypted types
- Performance optimizations
MIT License - see LICENSE for details.
- Zama for FHEVM technology and inspiration
- Ethereum Foundation for testnet infrastructure
- RainbowKit for wallet integration patterns
- Viem for Ethereum interactions
- fhevmjs for core FHE functionality
Built for the Zama FHE Bounty Challenge π
Powered by: Zama FHEVM | Network: Sepolia Testnet
Note: This SDK is a bounty submission demonstrating universal FHEVM integration. The example application showcases an anonymous taxi dispatch system with encrypted driver locations and confidential pricing. Suitable for development and testing. Additional security audits recommended for production use.