Decrypt/SealOutput from Cofhejs
Overview
This document lays out the complete flow of an off-chain sealoutput request. It is recommended to always usecofhejs.unseal rather than cofhejs.decrypt as .unseal internally seals the user’s data before returning it from the Threshold Network, making it inherently more secure (eg. man in the middle attack).
The example below is of
cofhejs.unseal, however cofhejs.decrypt uses the same API and returns the same result.Key Components
| Component | Description |
|---|---|
| CtHash | The encrypted hash representing an encrypted number. Fetched on-chain. |
| Cofhejs | Javascript library handling permits and the unseal / decrypt operations. |
| Threshold Network | Decentralized decryption network that handles the requests |
| ACL | On-chain Access Control List responsible for tracking CtHash access. |
Flow Diagram
The following diagram illustrates the complete flow of an Decrypt/SealOutput request in the CoFHE ecosystem:Step-by-Step Flow
1
Fetching of CtHash
Solidity contract:
- Fetch the user’s
euint64from the chain by callingconst CtHash = await example.getNumber()which returns aneuint64as a js bigint. 2️⃣
All euints, along with ebool and eaddress, are wrappers around uint256. The data returned from
example.getNumber() is in the type bigint, and can be treated as a CtHash directly.2
Integration with Cofhejs
- The decentralized application (dApp) integrates with CoFHE by utilizing Cofhejs for encryption. See in GitHub
-
Create a permit using
cofhejs.createPermit(...). This permit will automatically be used in the following step. -
Unseal using
cofhejs.unseal(CtHash)1️⃣. Calls/sealoutputon the threshold network, unseals the result. 3️⃣
3
Handled by cofhejs.unseal
-
cofhejs.unsealcalls /sealoutput. The user’s Permit is added to the request.Permit.issuershould be themsg.senderin Step 1 for the permit to be valid.https://{ThresholdNetworkUrl}/sealoutput -
Threshold Network makes an on-chain call to the
ACLto verify that the Permit is valid. - ACL verifies that the Permit is valid. 4️⃣
-
ACL verifies that
Permit.issuerhas been granted access toCtHash. (Access is granted byFHE.allowSenderin the Example contract functionsetNumber()) -
Threshold Network seals the data with
Permit.sealingKey -
Threshold Network returns the sealed result to
cofhejs
4
Handling Results
cofhejs receives the result from the Threshold Network and:- Unseals the result using the private_key of the sealing key pair. The result is always unsealed as a bigint regardless of the type of CtHash (euint32 / ebool / eaddress)
- Cofhejs converts the output type as follows:
- The result is returned as a
Resulttype. TheResult<T>type looks like this:
Result type is a discriminated union that represents either:- A successful operation with data (
success: true) - A failed operation with an error message (
success: false)
cofhejs.unseal is determined by the utype passed in as the second argument: