fhevmjs function specifications
fhevmjs is designed to assist in creating encrypted inputs and retrieving reencryption data off-chain through a gateway. The library works with any fhEVM and fhEVM Coprocessors.
Init (browser)
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.
Create instance
This function returns an instance of fhevmjs, which accepts an object containing:
chainId
(optional): the chainId of the networknetwork
(optional): the Eip1193 object provided bywindow.ethereum
(used to fetch the public key and/or chain id)networkUrl
(optional): the URL of the network (used to fetch the public key and/or chain id)publicKey
(optional): if the public key has been fetched separately (cache), you can provide itgatewayUrl
(optional): the URL of the gateway to retrieve a reencryptioncoprocessorUrl
(optional): the URL of the coprocessor
Using window.ethereum
object:
Input
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.addBool, input.add8, ...
Input object has different method to add values:
addBool
add4
add8
add16
add32
add64
addAddress
input.encrypt and input.send
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.
Reencryption
Keypair
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.
Reencryption
Reencrypt method will use the gatewayUrl
to get the reencryption of a ciphertext and decrypt it.
Last updated