fhEVM
WebsiteLibrariesProduct & ServicesDevelopersSupport
0.1
0.1
  • What is Zama's fhEVM?
  • Getting started
    • Connect wallet
      • Metamask
    • Faucet
      • Zama Devnet
      • Local node
  • Writing contract
    • Getting started
      • Hardhat
      • Remix
      • Docker
    • TFHE Library
    • Function specifications
    • Decryption and control structures
    • Examples
  • Client
    • Getting started
      • Node
      • Browser
      • Template
      • CLI
    • Setup an instance
    • Inputs
    • Reencryption
    • Examples
      • Transfer tokens (node)
      • Get balance (node)
  • Resources
    • Tutorials
    • Repositories
    • Whitepaper
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
  • Troubleshooting
  • Webpack: "Module not found: Error: Can't resolve 'tfhe_bg.wasm'"
  • Issue with importing ESM version
  • Use bundled version

Was this helpful?

Export as PDF
  1. Client
  2. Getting started

Browser

PreviousNodeNextTemplate

Last updated 1 year ago

Was this helpful?

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

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

const createFhevmInstance = async () => {
  const provider = new BrowserProvider(window.ethereum);
  const network = await provider.getNetwork();
  const chainId = +network.chainId.toString();
  const publicKey = await provider.call({
    from: null,
    to: "0x0000000000000000000000000000000000000044",
  });
  return createInstance({ chainId, publicKey });
};

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

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

Troubleshooting

Webpack: "Module not found: Error: Can't resolve 'tfhe_bg.wasm'"

In the codebase, there is a new URL('tfhe_bg.wasm') which triggers a resolve by Webpack. If yo u encounter an issue, you can add a fallback for this file by adding a resolve configuration in y our webpack.config.js:

    resolve: {
      fallback: {
        'tfhe_bg.wasm': require.resolve('tfhe/tfhe_bg.wasm'),
      },
    },

Issue with importing ESM version

If you encounter any other issue, you can force import of the browser package.

import { initFhevm, createInstance } from "fhevmjs/web";

Use bundled version

If you have an issue with bundling the library (for example with some SSR framework), you can use the prebundled version available in fhevmjs/bundle. Just embed the library with a <script> tag and you're good to go.

const start = async () => {
  await window.fhevm.initFhevm(); // load wasm needed
  const instance = window.fhevm.createInstance({ chainId, publicKey }).then((instance) => {
    console.log(instance);
  });
};

With a bundler such as Webpack or Rollup, imports will be replaced with the version mentioned in the "browser" field of the package.json. If you encounter issue with typing, you can use this using TypeScript 5.

TFHE
tsconfig.json