Skip to main content
This page shows the common patterns for writing Hardhat tests with the CoFHE plugin.

Test setup

Use hre.cofhe.createClientWithBatteries in a before hook. It creates and connects a fully configured CofheClient — including a self-permit — so the client is ready for every test in the suite:
See Client for manual setup options.

Encrypt → store → decrypt

The core test loop: encrypt a value, pass it to a contract, then decrypt the stored handle.

Reading plaintext directly

In tests you can bypass the normal decrypt flow and read the raw plaintext stored by the mock contracts. This is useful for asserting contract state without needing a permit:
See Mock Contracts for details.

Permits

createClientWithBatteries pre-generates a self-permit, so decryptForView and decryptForTx().withPermit() work immediately. For tests that need named permits or multiple signers, create them explicitly:
Alternatively, create a separate client for each signer:

decryptForTx patterns

decryptForTx returns a { ctHash, decryptedValue, signature } tuple for on-chain submission. The permit mode must be selected explicitly.

Globally allowed values (.withoutPermit())

When a contract calls FHE.allowPublic(handle), anyone can decrypt without a permit:

Access-controlled values (.withPermit())

For handles restricted by ACL policy, supply a permit:

Submitting the result on-chain

Pass the result directly to your contract:
For a full walkthrough of decryptForTx, see Decrypt to Transact.