Encrypt an input with the blockchain's FHE public key
Generate a private key and a public key to provide to TFHE.reencrypt()
Instance
Before using these features, you need to create an instance and provide some informations:
The blockchain chainId
The blockchain's FHE public key
You can get this information directly from the blockchain you're using. For example with ethers:
import { BrowserProvider } from"ethers";import { createInstance } from"fhevmjs";constcreateFhevmInstance=async () => {constprovider=newBrowserProvider(window.ethereum);// 1. Get chain idconstnetwork=awaitprovider.getNetwork();constchainId=+network.chainId.toString();// 2. Get blockchain public keyconstret=awaitprovider.call({// fhe lib address, may need to be changed depending on network to:"0x000000000000000000000000000000000000005d",// first four bytes of keccak256('fhePubKey(bytes1)') + 1 byte for library data:"0xd9d47bb001", });constdecoded=ethers.AbiCoder.defaultAbiCoder().decode(["bytes"], ret);constpublicKey= decoded[0];// 3. Create instance instance =createInstance({ chainId, publicKey });return instance;};
Important: Since the instance memorizes user's signature, you need to refresh the instance if the user uses another wallet address. Otherwise, you will encounter issue during reencryption.
Export keypairs
When a user generate and sign an EIP712 token for a contract, you can export these tokens with instance.serializeKeypairs(). This method will return all keypairs and signature associated with a contract.
You can store this object in the user's local storage, enabling you to load it in the next user session. You must save this information per wallet since it contains the user's signature.