Skip to main content
The plugin extends hre with hre.cofhe, providing three ways to create and connect a CofheClient in your Hardhat tests. hre.cofhe.createClientWithBatteries(signer?) is the one-liner that handles everything:
  1. Creates a CoFHE config with environment: 'hardhat' and supportedChains: [hardhat]
  2. Creates a CofheClient
  3. Connects it using the provided Hardhat signer (defaults to the first signer)
  4. Generates a self-usage permit for the signer
import hre from 'hardhat';

const [signer] = await hre.ethers.getSigners();
const cofheClient = await hre.cofhe.createClientWithBatteries(signer);

cofheClient.connected; // true
createClientWithBatteries generates a self-permit automatically, so most test operations (encrypting inputs, decrypting for view, decrypting for tx) work immediately without any extra setup.
If signer is omitted, the first signer from hre.ethers.getSigners() is used.

Manual setup

For more control — custom config options, multiple signers, or adjusting encryptDelay — you can set up the client step by step.
1

Create config

import hre from 'hardhat';
import { hardhat } from '@cofhe/sdk/chains';

const config = await hre.cofhe.createConfig({
  supportedChains: [hardhat],
});
hre.cofhe.createConfig wraps createCofheConfig from @cofhe/sdk/node with two Hardhat-specific additions:
  • Sets environment: 'hardhat' automatically
  • Defaults mocks.encryptDelay to 0 so tests run without artificial wait times
2

Create the client

const cofheClient = hre.cofhe.createClient(config);
3

Connect with a Hardhat signer

await hre.cofhe.connectWithHardhatSigner(cofheClient, signer);
connectWithHardhatSigner uses HardhatSignerAdapter under the hood to convert a HardhatEthersSigner into the viem PublicClient + WalletClient pair that the SDK expects.

Low-level adapter

If you need direct access to the underlying viem clients, call the adapter directly:
import hre from 'hardhat';

const [signer] = await hre.ethers.getSigners();

const { publicClient, walletClient } =
  await hre.cofhe.hardhatSignerAdapter(signer);

await cofheClient.connect(publicClient, walletClient);

Using the client

Once connected, the client works identically to the standard SDK client. See: