Skip to main content
Set up a Hardhat project with @cofhe/hardhat-plugin, write a contract that stores an encrypted uint32, and test the encrypt to store to decrypt flow.
Want to skip the setup? Clone the cofhe-hardhat-starter template to get a pre-configured project with everything ready to go.

Prerequisites

  • Node.js 18+
  • An existing Hardhat project (or run npx hardhat init to create one)
  • @nomicfoundation/hardhat-toolbox or @nomicfoundation/hardhat-ethers installed

1. Install dependencies

2. Configure Hardhat

Import the plugin and set evmVersion to cancun (required for the transient storage opcodes used by FHE contracts).
hardhat.config.ts
Without evmVersion: 'cancun', compilation will fail with errors from @fhenixprotocol/cofhe-contracts.

3. Write a contract

Create a minimal contract that accepts an encrypted input and stores it onchain.
contracts/MyContract.sol
  • euint32: an encrypted uint32 stored onchain as a ciphertext handle.
  • externalEuint32: the encrypted input handle produced by the SDK, paired with a bytes proof.
  • FHE.allowThis / FHE.allowSender: grant the contract and caller permission to read the encrypted value (required by the ACL).

4. Write a test

Use hre.cofhe.createClientWithBatteries to get a fully configured SDK client with a self-ACP, then encrypt to send to decrypt.
test/MyContract.test.ts

5. Run

The plugin deploys mock contracts automatically, no extra setup needed.

What just happened?

  1. The Hardhat plugin deployed mock versions of the CoFHE coprocessor contracts (TaskManager, ACL, ZK verifier, threshold network) before the test ran.
  2. createClientWithBatteries created an SDK client connected to the Hardhat network, with a self-ACP ready to go.
  3. encryptInputs encrypted the plaintext 42 into an FHE ciphertext with a ZK proof (simulated by the mock verifier).
  4. The contract stored the ciphertext handle onchain and set ACL permissions.
  5. decryptForView used the ACP to decrypt the handle back to 42n locally.

Next steps

  • Hardhat Plugin: full plugin configuration and features.
  • Mock Contracts: read plaintext directly and assert encrypted state in tests.
  • Logging: inspect every FHE operation your contracts perform.