fhEVM
WebsiteLibrariesProduct & ServicesDevelopersSupport
0.4
0.4
  • Welcome to fhEVM
  • Getting Started
    • What is fhEVM
    • Connecting to Zama Devnet
    • Using Zama Faucet
    • Local dev node
    • Whitepaper
  • Fundamentals
    • Write contract
      • Using Hardhat
      • Using Remix
      • Other development environment
    • Use encrypted types
    • Operations on encrypted types
    • Branching in FHE
    • Decrypt and reencrypt
    • Generate random number
    • Contracts standard library
  • Guides
    • Gas estimation
    • Common pitfalls and best practises
    • How can I break a loop ?
    • Encrypt an input
    • Reencryption
    • Use the CLI
    • Build a web application
    • Build with Node
    • Common webpack errors
  • Tutorials
    • See all tutorials
    • Write confidential smart contract with fhEVM
  • References
    • API Function specifications
    • Repositories
  • Developer
    • Contributing
    • Development roadmap
    • Release note
    • Feature request
    • Bug report
Powered by GitBook

Libraries

  • TFHE-rs
  • Concrete
  • Concrete ML
  • fhEVM

Developers

  • Blog
  • Documentation
  • Github
  • FHE resources

Company

  • About
  • Introduction to FHE
  • Media
  • Careers
On this page
  • Using a template
  • React + TypeScript
  • VueJS + TypeScript
  • NextJS + Typescript
  • Using directly the library

Was this helpful?

Export as PDF
  1. Guides

Build a web application

PreviousUse the CLINextBuild with Node

Last updated 1 year ago

Was this helpful?

Using a template

fhevmjs is working out of the box and we recommend you to use it. We also provide three GitHub templates to start your project with everything set.

React + TypeScript

You can use to start an application with fhevmjs, using Vite + React + TypeScript.

VueJS + TypeScript

You can also use to start an application with fhevmjs, using Vite + Vue + TypeScript.

NextJS + Typescript

You can also use to start an application with fhevmjs, using Next + TypeScript.

Using directly the library

First, you need to install the library.

# Using npm
npm install fhevmjs

# Using Yarn
yarn add fhevmjs

# Using pnpm
pnpm add fhevmjs
import { initFhevm } from "fhevmjs";

const init = async () => {
  await initFhevm(); // Load TFHE
};

init().then((instance) => {
  console.log(instance);
});

Once the WASM is loaded, you can now create an instance. An instance needs two element:

  • The blockchain public key. This key is needed to encrypt inputs

  • The blockchain' chain ID. This value is needed for reencryption process.

import { ethers, BrowserProvider } from "ethers";
import { initFhevm, createInstance, getPublicKeyCallParams } from "fhevmjs";

const createFhevmInstance = async () => {
  const provider = new BrowserProvider(window.ethereum);
  // 1. Get the chain id
  const network = await provider.getNetwork();
  const chainId = +network.chainId.toString();
  // 2. Fetch the FHE public key from the blockchain
  const ret = await provider.call(getPublicKeyCallParams());
  const decoded = ethers.AbiCoder.defaultAbiCoder().decode(["bytes"], ret);
  const publicKey = decoded[0];

  // 3. Create the instance
  return createInstance({ chainId, publicKey });
};

const init = async () => {
  await initFhevm(); // Load TFHE
  return createFhevmInstance();
};

init().then((instance) => {
  console.log(instance);
});

fhevmjs uses ESM format. You need to set the . If your node project use "type": "commonjs" or no type, you can force the loading of the web version by using import { createInstance } from 'fhevmjs/web';

To use the library in your project, you need to load the WASM of first with initFhevm.

You can now use your instance to or do a .

this template
this template
this template
type to "module" in your package.json
TFHE
encrypt parameters
reencryption