This document provides an overview of the fhevmjs
library, detailing its initialization, instance creation, input handling, encryption, and re-encryption processes.
fhevmjs is designed to assist in creating encrypted inputs and retrieving re-encryption data off-chain through a gateway. The library works with any fhEVM and fhEVM Coprocessors.
If you are using fhevmjs
in a web application, you need to initialize it before creating an instance. To do this, you should call initFhevm
and wait for the promise to resolve.
This function returns an instance of fhevmjs, which accepts an object containing:
kmsContractAddress
: the address of the KMSVerifier contract;
aclContractAddress
: the address of the ACL contract;
networkUrl
or network
: the URL or Eip1193 object provided by window.ethereum
- used to fetch chainId and KMS nodes' public key
gatewayUrl
: the URL of the gateway - used to retrieve the public key, ZKPoK public parameters and send inputs and get reencryption
chainId
(optional): the chainId of the network
publicKey
(optional): if the public key has been fetched separately or stored in cache, you can provide it
publicParams
(optional): if the public params has been fetched separately or stored in cache, you can provide it
Using window.ethereum
object:
This method creates an encrypted input and returns an input object. It requires both the user address and the contract address to ensure the encrypted input isn't reused inappropriately in a different context. An input can include multiple values of various types, resulting in a single ciphertext that packs these values.
Input object has different method to add values:
addBool
add4
add8
add16
add32
add64
add128
add256
addBytes64
addBytes128
addBytes256
addAddress
These methods process values and return the necessary data for use on the blockchain. The encrypt
method encrypts these values and provides parameters for use. The send
method encrypts, dispatches the ciphertext and proof to the coprocessor, and returns the required parameters.
A keypair consists of a private key and a public key, both generated by the dApp. These keys are used to reencrypt a blockchain ciphertext, allowing it to be securely transferred to user-specific keypairs.
Verifying that the public key used in the reencryption process belongs to the user requires the user to sign the public key linked to a specific contract address. This signature allows any ciphertext allowed for the user and the contract can be reencrypted using the signed public key. To streamline user interaction during the signature process, we utilize the EIP712 standard as the object to be signed.
This eip712
can be signed using eth_signTypedData_v4
for example in a browser:
Note: it is recommended to store the keypair and the signature in the user's browser to avoid re-requesting signature on every user connection.
Reencrypt method will use the gatewayUrl
to get the reencryption of a ciphertext and decrypt it.