Skip to content

LulaPadberg/fhevm-react-template

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

106 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Universal FHEVM SDK

A framework-agnostic SDK for building confidential frontends with Fully Homomorphic Encryption (FHE)

License: MIT TypeScript FHEVM

πŸ† Bounty Program

GitHub Repository: https://github.com/LulaPadberg/fhevm-react-template

Live Example Application: https://fhe-vehicle-insurance.vercel.app/

Example Smart Contract: 0x2A86c562acc0a861A96E4114d7323987e313795F (Sepolia Testnet)

Demo Video: Download demo.mp4 to watch the complete platform demonstration showcasing FHE vehicle insurance claims processing with full privacy protection.

πŸš€ Quick Start (< 10 lines!)

# Install the SDK
npm install @fhevm/universal-sdk

# Initialize in your React app
import { useFhevmClient, useEncrypt } from '@fhevm/universal-sdk/react';

const { client } = useFhevmClient({ network: { chainId: 8009, rpcUrl: 'https://devnet.zama.ai', name: 'Zama Devnet' } });
const { encryptValue } = useEncrypt();

// Encrypt and use!
const encrypted = await encryptValue('uint32', 12345);

✨ Features

  • 🎯 Framework-Agnostic - Works with React, Vue, Node.js, or any JavaScript environment
  • πŸ“¦ All-in-One - Single package wrapping all FHEVM dependencies
  • πŸ”— Wagmi-like API - Familiar interface for web3 developers
  • ⚑ Quick Setup - Less than 10 lines of code to get started
  • πŸ” Type-Safe - Full TypeScript support with intelligent type inference
  • πŸͺ React Hooks - Built-in hooks for React applications
  • 🎨 Zero Configuration - Sensible defaults that just work
  • πŸ“š Well Documented - Comprehensive docs with examples

πŸ“¦ Installation

npm install @fhevm/universal-sdk

# Or with yarn
yarn add @fhevm/universal-sdk

# Or with pnpm
pnpm add @fhevm/universal-sdk

🎯 Why This SDK?

Before (Complex Setup)

// Install multiple packages
npm install fhevmjs @zama-fhe/oracle-solidity ethers

// Complex initialization
import { createInstance } from 'fhevmjs';
const provider = new BrowserProvider(window.ethereum);
const network = await provider.getNetwork();
const publicKeyResponse = await fetch('...');
const instance = await createInstance({ chainId, publicKey });
// ...many more lines

After (Universal SDK)

// Install one package
npm install @fhevm/universal-sdk

// Simple initialization
import { useFhevmClient } from '@fhevm/universal-sdk/react';

const { client } = useFhevmClient({
  network: { chainId: 8009, rpcUrl: 'https://devnet.zama.ai', name: 'Zama' }
});

πŸ› οΈ Usage

React Example

'use client';

import { useFhevmClient, useEncrypt, useDecrypt } from '@fhevm/universal-sdk/react';

export default function App() {
  // Initialize FHEVM
  const { client, isReady } = useFhevmClient({
    network: {
      chainId: 8009,
      rpcUrl: 'https://devnet.zama.ai',
      name: 'Zama Devnet'
    }
  });

  // Encryption
  const { encryptValue, isEncrypting } = useEncrypt({
    onSuccess: (data) => console.log('Encrypted!', data)
  });

  // Decryption
  const { decryptValue, result } = useDecrypt({
    onSuccess: (result) => console.log('Decrypted:', result.value)
  });

  // Use it
  const handleEncrypt = async () => {
    const encrypted = await encryptValue('uint32', 12345);
    // Send encrypted data to your smart contract
  };

  const handleDecrypt = async () => {
    await decryptValue({
      contractAddress: '0x...',
      handle: '0x...'
    });
  };

  if (!isReady) return <div>Loading FHEVM...</div>;

  return (
    <div>
      <button onClick={handleEncrypt} disabled={isEncrypting}>
        Encrypt
      </button>
      <button onClick={handleDecrypt}>
        Decrypt
      </button>
      {result && <p>Result: {result.value.toString()}</p>}
    </div>
  );
}

Node.js / Framework-Agnostic Example

import { createFhevmClient, encrypt, decrypt } from '@fhevm/universal-sdk';

// Initialize client
const client = await createFhevmClient({
  network: {
    chainId: 8009,
    rpcUrl: 'https://devnet.zama.ai',
    name: 'Zama Devnet'
  }
});

// Encrypt
const encryptedValue = await encrypt('uint32', 12345);

// Decrypt
const result = await decrypt({
  contractAddress: '0x...',
  handle: '0x...'
});

console.log('Decrypted value:', result.value);

Vue Example

<script setup>
import { ref, onMounted } from 'vue';
import { createFhevmClient, encrypt } from '@fhevm/universal-sdk';

const isReady = ref(false);
const encryptedData = ref(null);

onMounted(async () => {
  await createFhevmClient({
    network: {
      chainId: 8009,
      rpcUrl: 'https://devnet.zama.ai',
      name: 'Zama Devnet'
    }
  });
  isReady.value = true;
});

async function handleEncrypt() {
  encryptedData.value = await encrypt('uint32', 12345);
}
</script>

<template>
  <div v-if="isReady">
    <button @click="handleEncrypt">Encrypt</button>
  </div>
</template>

πŸ“– API Reference

Core Functions

createFhevmClient(config)

Initialize the FHEVM client.

const client = await createFhevmClient({
  network: {
    chainId: 8009,
    rpcUrl: 'https://devnet.zama.ai',
    name: 'Zama Devnet'
  },
  gateway: {
    url: 'https://gateway.zama.ai/decrypt', // optional
    relayerAddress: '0x...' // optional
  },
  aclAddress: '0x...' // optional
});

encrypt(type, value)

Encrypt a value with automatic type handling.

// Supported types: 'bool', 'uint8', 'uint16', 'uint32', 'uint64', 'address'
const encrypted = await encrypt('uint32', 12345);
const encryptedBool = await encrypt('bool', true);
const encryptedAddr = await encrypt('address', '0x...');

decrypt(request)

Decrypt an encrypted value.

const result = await decrypt({
  contractAddress: '0x...',
  handle: '0x...'
});
console.log(result.value); // bigint

React Hooks

useFhevmClient(config)

Hook for initializing and managing the FHEVM client.

const { client, isLoading, error, isReady } = useFhevmClient(config);

useEncrypt(options?)

Hook for encrypting values.

const { encryptValue, isEncrypting, error, data } = useEncrypt({
  onSuccess: (data) => {},
  onError: (error) => {}
});

useDecrypt(options?)

Hook for decrypting values.

const { decryptValue, decryptBatch, isDecrypting, error, result } = useDecrypt({
  onSuccess: (result) => {},
  onError: (error) => {}
});

🎨 Examples & Templates

Templates

The templates/ directory contains ready-to-use starter templates:

# Use a template to start your project
cd templates/nextjs
npm install
npm run dev

Working Examples

The examples/ directory contains comprehensive examples:

# Run an example
cd examples/nextjs-showcase
npm install
npm run dev

# Or try the private vehicle insurance app
cd examples/private-vehicle-insurance
npm install
npm run dev

Live Deployment

See the live insurance platform example at https://fhe-vehicle-insurance.vercel.app/ using smart contract 0x2A86c562acc0a861A96E4114d7323987e313795F on Sepolia Testnet.

πŸ—οΈ Architecture

@fhevm/universal-sdk
β”œβ”€β”€ Core (Framework-Agnostic)
β”‚   β”œβ”€β”€ Client Management
β”‚   β”œβ”€β”€ Encryption (all FHE types)
β”‚   └── Decryption (with EIP-712 signing)
β”œβ”€β”€ React Integration
β”‚   β”œβ”€β”€ useFhevmClient
β”‚   β”œβ”€β”€ useEncrypt
β”‚   └── useDecrypt
└── Utilities
    β”œβ”€β”€ Type Conversions
    └── Helper Functions

🚒 Deployment

The showcase is deployed at: https://fhe-vehicle-insurance.vercel.app/

πŸ“ Documentation

πŸ§ͺ Testing

# Run SDK tests
cd packages/fhevm-sdk
npm test

# Run Next.js example
cd examples/nextjs-showcase
npm run dev

🀝 Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

πŸ“„ License

MIT License - see LICENSE for details.

πŸ™ Acknowledgments

Built with:

  • fhevmjs - Official Zama FHEVM JavaScript library
  • Zama Oracle - Decryption oracle
  • TypeScript, React, Next.js

πŸ“ž Support

🎯 Roadmap

  • Vue.js composables
  • Angular services
  • Svelte stores
  • React Native support
  • Enhanced error handling
  • Batch operations optimization
  • Caching layer
  • DevTools integration

Built with ❀️ for the Fully Homomorphic Encryption community

About

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • TypeScript 97.7%
  • CSS 1.5%
  • JavaScript 0.8%