@cofhe/hardhat-3-plugin runs the CoFHE mock contracts inside Hardhat 3’s simulated network and attaches a cofhe object to every network connection. Use it to compile, test, and debug FHE contracts locally without the offchain CoFHE services.
This plugin is for Hardhat 3. For a Hardhat 2 project, use
@cofhe/hardhat-plugin. The two are not interchangeable.What the plugin provides
- Mock contracts deployed on every new connection to the simulated network, standing in for the CoFHE coprocessor.
cofheon the connection object, next to theviemobject from@nomicfoundation/hardhat-viem.cofhe.createClientWithBatteries(), which returns a connected SDK client with a self ACP (Access Control Permission) already signed.- Mock helpers for reading plaintext values, toggling operation logs, and calling the mock contracts directly with viem.
- Network presets for
localcofhe,eth-sepolia, andarb-sepolia.
Install the plugin
These steps assume a Hardhat 3 project that already uses@nomicfoundation/hardhat-viem and @nomicfoundation/hardhat-node-test-runner.
1
Add the dependency overrides
@cofhe/sdk pins viem to an exact version and declares an optional peer dependency on Hardhat 2. Both collide with a Hardhat 3 project. Add these entries to package.json before you install:viem version your project already declares.2
Install the packages
3
Register the plugin
Add the plugin to the The plugin reads the mock bytecode from your project’s build artifacts.
plugins array, and list the mock contract sources in npmFilesToBuild:hardhat.config.ts
npmFilesToBuild makes the mocks part of that build, so the compile step of npx hardhat test keeps their artifacts in place.Run
npx hardhat test. The output prints cofhe-hardhat-3-plugin :: mocks deployed once for each connection your tests open.Why the overrides are needed
Each entry fixes a specific failure:hardhat(npm only):@cofhe/sdkdeclareshardhat@^2as an optional peer, so npm refuses the install withERESOLVEonce Hardhat 3 is in the tree. pnpm and yarn warn and continue.viem:@cofhe/sdkand the plugin pinviem2.38.6, while@nomicfoundation/hardhat-viemrequires 2.47.6 or later. Without the override, two copies of viem are installed. Tests still run, but TypeScript rejects every hardhat-viem client you pass to the plugin with errorTS2345.
Configuration
The plugin adds an optionalcofhe key to the Hardhat config. Every option has a default, so you can leave the key out.
hardhat.config.ts
When the mocks deploy
The plugin hooks into Hardhat’s connection lifecycle. Every call tonetwork.create() returns a new simulated chain, and the plugin deploys the full mock stack on it before your code gets the connection.
test/Counter.test.ts
network.connect() behaves the same way, but Hardhat 3.4.0 deprecated it in favor of network.create(). network.getOrCreate() reuses an existing connection, and its mocks, when one is open.
The plugin checks whether the network answers Hardhat’s hardhat_metadata RPC method before it deploys anything. On a testnet it skips the deployment, so the same connection code works against a live network. There is no environment variable to turn deployment off.
Pre-configured networks
The plugin adds these networks unless your config already defines a network with the same name.
The testnet presets sign with the key in the
PRIVATE_KEY environment variable.
Differences from the Hardhat 2 plugin
The mock contracts themselves are the same package,
@cofhe/mock-contracts, in both plugins.
Next steps
- Client: create and connect an SDK client in a test.
- Mock contracts: read plaintext values and call the mocks directly.
- Logging: print the FHE operations your contracts perform.
- Testing: a complete test file and the patterns around it.