Skip to content

MacieNienow/fhevm-react-template

Β 
Β 

Repository files navigation

Universal FHEVM SDK

Next-generation FHEVM SDK for building confidential frontends - Framework-agnostic, developer-friendly, and production-ready.

License Zama TypeScript

Built for the Zama FHE Bounty Challenge - A universal SDK that makes building confidential applications simple, consistent, and intuitive for all Web3 developers.


🌐 Live Demonstration

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.


🎯 Overview

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.

✨ Key Features

  • 🎯 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

πŸš€ Quick Start

Installation

npm install @fhevm/sdk
# or
yarn add @fhevm/sdk
# or
pnpm add @fhevm/sdk

Basic Usage (< 10 lines)

import { 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.

React Usage

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>
  );
}

πŸ“¦ What's Included

Package Structure

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

🎯 SDK Integration Examples

Example 1: Next.js with React Hooks (Recommended)

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:

  • βœ… FhevmProvider for global SDK initialization
  • βœ… useFhevm hook for ready state management
  • βœ… useEncrypt hook with loading states
  • βœ… Parallel encryption with Promise.all
  • βœ… Integration with Wagmi and RainbowKit
  • βœ… Proper error handling and UI feedback

Example 2: Private Taxi Dispatch Platform

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

Example 3: Framework-Agnostic Core API

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

🎯 Why This SDK?

Before (Scattered Dependencies)

// 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

After (@fhevm/sdk)

// 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 ✨


πŸ”§ SDK Features

1. Framework Agnostic Core

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';

2. Intuitive Hooks (React)

Familiar API for Web3 developers:

const { fhevm, isLoading, error, isReady } = useFhevm(config);
const { encrypt, isEncrypting } = useEncrypt('euint64');
const { createPermitSignature, isCreating } = usePermit(contractAddress, userAddress);

3. Complete FHE Flow

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);

4. TypeScript First

Full type safety and IntelliSense:

import type {
  FhevmConfig,
  EncryptedValue,
  PermitSignature,
  EncryptedType
} from '@fhevm/sdk';

const encrypted: EncryptedValue = await encryptValue(42, 'euint64');
//    ^ Fully typed with IntelliSense

5. Batch Operations

Efficient 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));

πŸ“š Documentation

Complete API Reference

Examples Overview

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

Key Functions

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

Supported Encrypted Types

ebool, euint4, euint8, euint16, euint32, euint64, euint128, euint256, eaddress


🎬 Demo Video

File: demo.mp4 (Download required to watch)

Contents:

  1. Setup (0:00-1:30): Installing @fhevm/sdk and initializing FHEVM
  2. Basic Usage (1:30-3:00): Encrypting values and submitting to contract
  3. React Integration (3:00-5:00): Using hooks in Next.js application
  4. Anonymous Taxi Dispatch Demo (5:00-8:00): Complete walkthrough of example application
  5. Framework Flexibility (8:00-10:00): Showing Vue and Node.js integration
  6. Design Choices (10:00-12:00): Architecture and API design decisions

πŸ’‘ Design Choices

1. Framework Agnostic Core

Why? FHE encryption is a universal need, not framework-specific.

How? Pure TypeScript core with framework-specific adapters.

2. Intuitive API

Why? Lower barrier to entry for Web3 developers.

How? Familiar patterns and naming conventions.

3. Single Package

Why? Reduce dependency management complexity.

How? Wrap all requirements in one package.

4. TypeScript First

Why? Type safety prevents runtime errors.

How? Complete type definitions for all APIs.

5. Minimal Setup

Why? Faster onboarding = more adoption.

How? Sensible defaults and auto-configuration.


πŸ”— Project Links

Example Applications

Resources


πŸ› οΈ Development

Setup

# 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 build

Running Examples

Next.js Example (Recommended)

cd examples/nextjs-example
npm install
npm run dev
# Open http://localhost:3000

Features 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

Private Taxi Dispatch Example

cd examples/PrivateTaxiDispatch
npm install
npm run dev
# Open http://localhost:3002

Features 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

React Example

cd examples/react-example
npm install
npm run dev

Features demonstrated:

  • Simple encrypted counter
  • React 18 + Vite setup
  • Core SDK usage patterns

Node.js Example

cd examples/nodejs-example
npm install
node index.ts

Features demonstrated:

  • Server-side encryption
  • CLI integration
  • Backend service patterns

Testing

# Test SDK
cd packages/fhevm-sdk
npm test

# Type checking
npm run type-check

# Build
npm run build

🀝 Contributing

Contributions 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

πŸ“„ License

MIT License - see LICENSE for details.


πŸ™ Acknowledgments

  • 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.

About

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • TypeScript 97.9%
  • CSS 1.6%
  • JavaScript 0.5%