Skip to content

A framework-agnostic frontend toolkit that helps developers run confidential dApps with ease.

License

Notifications You must be signed in to change notification settings

starfrich/fhevm-react-template

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

91 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Universal FHEVM SDK

codecov CI

A framework-agnostic frontend toolkit that helps developers run confidential dApps with ease.

🌐 Live Demos

Try the SDK in action on Sepolia testnet:

πŸ“ Note: Live demos require:

  • MetaMask or compatible Web3 wallet
  • Sepolia testnet ETH (Get from faucet)
  • Connect wallet to Sepolia network (Chain ID: 11155111)

πŸ’» Backend Examples: Node.js backend and automation examples are designed for local/server deployment. See Examples Guide for setup instructions.


πŸš€ What is FHEVM?

FHEVM (Fully Homomorphic Encryption Virtual Machine) enables computation on encrypted data directly on Ethereum. This template demonstrates how to build dApps that can perform computations while keeping data private.

✨ Features

  • πŸ” FHEVM Integration: Built-in support for fully homomorphic encryption
  • βš›οΈ React + Next.js: Modern, performant frontend framework
  • 🎨 Tailwind CSS: Utility-first styling for rapid UI development
  • πŸ”— RainbowKit: Seamless wallet connection and management
  • 🌐 Multi-Network Support: Auto-detects localhost and Sepolia networks
  • πŸ“¦ Monorepo Structure: Organized packages for SDK, contracts, and frontend
  • πŸ”„ Auto-Generated Contracts: Type-safe contract ABIs via pnpm generate

πŸ“‹ Prerequisites

Before you begin, ensure you have:

  • Node.js (v20 or higher)
  • pnpm package manager
  • MetaMask browser extension
  • Git for cloning the repository

πŸ“ Project Structure

This template uses a monorepo structure with three main packages:

fhevm-universal-sdk/
β”œβ”€β”€ packages/
β”‚   β”œβ”€β”€ fhevm-sdk/                      # Core universal FHEVM SDK
β”‚   β”‚   β”œβ”€β”€ docs/                       # Internal SDK documentation (debug, migration, validation, etc.)
β”‚   β”‚   β”œβ”€β”€ src/                        # SDK source code
β”‚   β”‚   β”‚   β”œβ”€β”€ core/                   # Core FHE logic (init, encryption, decryption)
β”‚   β”‚   β”‚   β”œβ”€β”€ internal/               # Internal helpers not exposed publicly
β”‚   β”‚   β”‚   β”œβ”€β”€ react/                  # React hooks & adapters
β”‚   β”‚   β”‚   β”œβ”€β”€ storage/                # Encrypted local/session storage utilities
β”‚   β”‚   β”‚   β”œβ”€β”€ types/                  # TypeScript definitions
β”‚   β”‚   β”‚   β”œβ”€β”€ utils/                  # General-purpose utilities
β”‚   β”‚   β”‚   β”œβ”€β”€ vanilla/                # Adapter for Vanilla JS
β”‚   β”‚   β”‚   └── vue/                    # Adapter for Vue
β”‚   β”‚   └── test/                       # Unit & integration tests for all modules
β”‚   β”œβ”€β”€ hardhat/                        # Hardhat environment for contract dev & testing
β”‚   └── nextjs/                         # Next.js (React) showcase application
β”œβ”€β”€ examples/                           # Cross-framework integration examples
β”‚   β”œβ”€β”€ vanilla-js/                     # Plain browser example using SDK
β”‚   β”œβ”€β”€ vue-app/                        # Vue 3 + Vite example
β”‚   β”œβ”€β”€ nodejs-backend/                 # Express backend using SDK
β”‚   └── nodejs-automation/              # Node.js automation & job scheduling
β”œβ”€β”€ scripts/                            # Global build & deploy utilities
β”‚   └── generateTsAbis.ts               # Generates TS typings from Solidity ABIs
β”œβ”€β”€ README.md                           # Project documentation
β”œβ”€β”€ package.json                        # Root dependency manager
└── pnpm-workspace.yaml                 # Workspace definition for monorepo

πŸ› οΈ Quick Start

1. Clone and Setup

# Clone the repository
git clone <repository-url>
cd fhevm-react-template

# Initialize submodules (includes fhevm-hardhat-template)
git submodule update --init --recursive

# Install dependencies
pnpm install

2. Configure Environment

Setup Hardhat variables (required for both localhost and Sepolia):

cd packages/hardhat

# Set your wallet mnemonic
npx hardhat vars set MNEMONIC
# Example: "test test test test test test test test test test test junk"

# Set Infura API key (required for Sepolia, optional for localhost)
npx hardhat vars set INFURA_API_KEY
# Example: "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"

3. Choose Your Network

🧩 Localhost (Recommended for Testing)
# Terminal 1: Start local Hardhat node
pnpm chain
# RPC URL: http://127.0.0.1:8545 | Chain ID: 31337

# Terminal 2: Deploy contracts
pnpm deploy:localhost

# Terminal 3: Start frontend
pnpm start
🌐 Sepolia Testnet
# Deploy to Sepolia
pnpm deploy:sepolia

# Start frontend
pnpm start

Production Notes:

  • Set NEXT_PUBLIC_ALCHEMY_API_KEY in packages/nextjs/scaffold.config.ts
  • Verify contract addresses in packages/nextjs/contracts/deployedContracts.ts
  • Optional: Set NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID for WalletConnect

πŸ’‘ Auto-Detection: The app automatically detects your network and uses the correct contracts!

4. Connect MetaMask

  1. Open http://localhost:3000
  2. Click "Connect Wallet" and select MetaMask
  3. For localhost: Add Hardhat network to MetaMask:
    • Network Name: Hardhat Local
    • RPC URL: http://127.0.0.1:8545
    • Chain ID: 31337
    • Currency Symbol: ETH

πŸŽ“ Examples

This template includes comprehensive examples showing how to use the FHEVM SDK across different frameworks and environments:

Browser Examples

  • Vanilla JS (examples/vanilla-js/) - Pure JavaScript with Vite
  • Vue 3 (examples/vue-app/) - Vue 3 with Composition API

Backend Examples

  • Express.js (examples/nodejs-backend/) - REST API backend with Node.js
  • Automation (examples/nodejs-automation/) - Scheduled tasks and batch processing

All examples use the same @fhevm-sdk/core - what differs is the framework context.

See Examples Guide for:

  • Detailed overview of each example
  • SDK usage patterns by framework
  • Comparison table
  • Configuration options

Quick start:

# Browser - Vanilla JS
pnpm vanilla:dev

# Browser - Vue 3
pnpm vue:dev

# Backend - Express API
cp examples/nodejs-backend/.env.example examples/nodejs-backend/.env && pnpm backend:dev

# Automation - Scheduled tasks
cp examples/nodejs-automation/.env.example examples/nodejs-automation/.env && pnpm automation:start help
pnpm automation:task:batch # Automation - Batch Encrypt
pnpm automation:task:decrypt # Automation - Daily Decrypt

πŸ”§ Key Components & Concepts

πŸ”„ Contract Synchronization

After deploying contracts, run pnpm generate to auto-generate type-safe ABIs for Next.js, Vue, and Vanilla JS. This keeps your frontend and contracts perfectly in sync!

πŸ’Ύ Storage Options

The SDK uses IndexedDB by default to persist FHEVM decryption signatures (no re-signing after page refresh). Automatic fallback to localStorage or in-memory storage if unavailable.

// Default: IndexedDB (persistent)
import { useIndexedDBStorage } from "~/hooks/helper/useIndexedDBStorage";
const { storage } = useIndexedDBStorage({ dbName: "fhevm-app", storeName: "signatures" });

// Alternative: In-memory (non-persistent, faster)
import { useInMemoryStorage } from "@fhevm-sdk";
const { storage } = useInMemoryStorage();

πŸ”§ Troubleshooting

MetaMask + Hardhat Common Issues

Nonce Mismatch: After restarting Hardhat, clear MetaMask activity:

  • Settings β†’ Advanced β†’ "Clear Activity Tab"

Cached Data: Restart your browser completely (not just refresh) to clear MetaMask's cache.

πŸ’‘ See MetaMask dev guide for details.

πŸ“š Additional Resources

Official Documentation

Alternative Documentation

Development Tools

Community & Support

πŸ“„ License

This project is licensed under the BSD-3-Clause-Clear License. See the LICENSE file for details.

About

A framework-agnostic frontend toolkit that helps developers run confidential dApps with ease.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 99.6%
  • Other 0.4%