Facing Error When Calling initFhevm() in Web Extension

Hi Team,

I’m currently working on a web extension where I need to access the fhevmInstance. However, I’m encountering an error while calling the initFhevm() function. Below are the error logs I’m facing:


Error Logs

  1. Primary Error Log

Copy code

index.html:6896 TypeError#1: Cannot read properties of undefined (reading 'instantiate')
  1. Stack Trace

Copy code

at B (eval at <anonymous> (eval at makeEvaluate (chrome-extension://ljfoeinjpaedjfecbmggjgodbgkmjkjk/snaps/index.html:6368:27)), <anonymous>:1:253033)
at _Q (eval at <anonymous> (eval at makeEvaluate (chrome-extension://ljfoeinjpaedjfecbmggjgodbgkmjkjk/snaps/index.html:6368:27)), <anonymous>:1:253359)
at AE (eval at <anonymous> (eval at makeEvaluate (chrome-extension://ljfoeinjpaedjfecbmggjgodbgkmjkjk/snaps/index.html:6368:27)), <anonymous>:1:253457)
at Q (eval at <anonymous> (eval at makeEvaluate (chrome-extension://ljfoeinjpaedjfecbmggjgodbgkmjkjk/snaps/index.html:6368:27)), <anonymous>:1:3500910)
at E (eval at <anonymous> (eval at makeEvaluate (chrome-extension://ljfoeinjpaedjfecbmggjgodbgkmjkjk/snaps/index.html:6368:27)), <anonymous>:1:3501017)
at gE (eval at <anonymous> (eval at makeEvaluate (chrome-extension://ljfoeinjpaedjfecbmggjgodbgkmjkjk/snaps/index.html:6368:27)), <anonymous>:1:3501215)
at chrome-extension://ljfoeinjpaedjfecbmggjgodbgkmjkjk/snaps/bundle.js:9:46693
at l.executeInSnapContext (chrome-extension://ljfoeinjpaedjfecbmggjgodbgkmjkjk/snaps/bundle.js:9:51180)
at chrome-extension://ljfoeinjpaedjfecbmggjgodbgkmjkjk/snaps/bundle.js:9:46665
at Object.snapRpc (chrome-extension://ljfoeinjpaedjfecbmggjgodbgkmjkjk/snaps/bundle.js:9:51887)

Environment Details

  • Extension Framework: Chrome Extension
  • Error Context: Occurs when invoking initFhevm()
  • Browser: Chrome
  • Relevant Libraries:
    • fhevm-related package
    • ethers.js

Using fhevmjs in an extension will be very tricky because fhevmjs is using WASM and webworkers. I don’t think it will be doable out of the box, especially if you’re using Metamask snaps. You can try to use our bundled version which is working totally fine in a website and maybe could work in a extension Build a web application | fhEVM

1 Like

I noticed that. Now, I tried using the bundled version of fhevmjs, but it’s still not creating any instance. The createInstance method doesn’t seem to return a valid object, and no errors are being thrown, making it challenging to debug.

Code Snippet:

import {
  OnHomePageHandler,
  OnUserInputHandler,
  UserInputEventType,
} from '@metamask/snaps-sdk';
import {
  Box,
  Container,
  Footer,
  Heading,
  Text,
  Button,
} from '@metamask/snaps-sdk/jsx';
import { initFhevm, createInstance, FhevmInstance } from 'fhevmjs/bundle';

export const onHomePage: OnHomePageHandler = async () => {
  let instanceStatus = 'Not Created';
  let chainId: number | null = null;
  let instance: FhevmInstance | null = null;
  let publicKey, privateKey;

  try {
    // await initFhevm();

    const instance = await createInstance({
      network: ethereum,
      chainId: 11155111,
      gatewayUrl: 'https://gateway.sepolia.zama.ai',
      kmsContractAddress: '0x9D6891A6240D6130c54ae243d8005063D05fE14b',
      aclContractAddress: '0xFee8407e2f5e3Ee68ad77cAE98c434e637f516e5',
    });

    instanceStatus = 'Created Successfully';
    const int = instance.generateKeypair();
    (publicKey = int.publicKey), (privateKey = int.privateKey);

    chainId = 11155111;
  } catch (error: any) {
    console.log(error);
    instanceStatus = `Error: ${error.message}`;
  }

  return {
    content: (
      <Container>
        <Box>
          <Heading>FHEVM Test</Heading>
          <Text>Instance Status: {instanceStatus}</Text>
          <Box>
            <Text>Chain ID: {String(chainId) || 'Not available'}</Text>
            <Text>Public Key: {publicKey || 'Not available'}</Text>
            <Text>Private Key : {privateKey || 'Not available'}</Text>
          </Box>
        </Box>
        <Footer>
          <Button name="encrypt_button">Encrypt Input</Button>
        </Footer>
      </Container>
    ),
  };
};

Observed Behavior

  • The createInstance method does not create an instance. The instance variable remains null.
  • No errors are thrown inside the try-catch block, which makes it challenging to understand what might be failing.

Expected Behavior

The createInstance method should return a valid FhevmInstance or at least throw a meaningful error if something is wrong.