Encrypting Input Data
This step secures your data before sending it to the smart contract. Remember - all data sent to a smart contract on a blockchain is inherently public, which means that anyone can see it. However, Fhenix operates differently. To maintain user confidentiality and protect sensitive input data, Fhenix utilizes Cofhejs to provide built-in encryption methods that you must apply before sending any data to an FHE-enabled contract. Cofhejs provides an easy-to-use function to encrypt your inputs before sending them to the Fhenix Co-Processor.Prerequisites
Before encrypting data, ensure you have:- Cofhejs installed and initialized
- A deployed FHE-enabled smart contract
- Access to a Fhenix network (local or testnet)
euint (Encrypted Integer) you want to create. This should match the expected type in the Solidity contract you are working with.
Setup
1
Initialize Cofhejs
First, initialize the library with your provider and signer:
Encryption Examples
Single Value Encryption
You can encrypt individual values by specifying the type:Multiple Values Encryption
You can encrypt multiple values at once using the nested form:Return Types
The returned types from the encrypt function will be an array of the typeCoFheInBool, CoFheInUint8, CoFheInUint16, CoFheInUint32 (or 64/128) or CoFheInAddress depending on the type you specified.
These encrypted types have the following structure:
setState Callback
ThesetState function is used to monitor the state of the encryption process. Since the process is asynchronous, you can use this function to get the state of the encryption process for better UI experience.
- Extract - Getting all the data ready for encryption (values to encrypt, chain information, etc.).
- Pack - Preparing the data for the encryption process.
- Prove - Signing the data.
- Verify - Verifies the user’s input, ensuring that it is safe to use (read more about this here).
- Replace - Preparing the result and replacing the old values with encrypted ones.
- Done - Process is finished.