> ## Documentation Index
> Fetch the complete documentation index at: https://cofhe-docs.fhenix.zone/llms.txt
> Use this file to discover all available pages before exploring further.

# Client

> Create and connect a CofheClient in Hardhat tests

The plugin extends `hre` with `hre.cofhe`, providing three ways to create and connect a `CofheClient` in your Hardhat tests.

## Batteries included (recommended)

`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

```typescript theme={null}
import hre from 'hardhat';

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

cofheClient.connected; // true
```

<Info>
  `createClientWithBatteries` generates a self-permit automatically, so most test operations (encrypting inputs, decrypting for view, decrypting for tx) work immediately without any extra setup.
</Info>

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.

<Steps>
  <Step title="Create config">
    ```typescript theme={null}
    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
  </Step>

  <Step title="Create the client">
    ```typescript theme={null}
    const cofheClient = hre.cofhe.createClient(config);
    ```
  </Step>

  <Step title="Connect with a Hardhat signer">
    ```typescript theme={null}
    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.
  </Step>
</Steps>

## Low-level adapter

If you need direct access to the underlying viem clients, call the adapter directly:

```typescript theme={null}
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:

* [Encrypting Inputs](/client-sdk/guides/encrypting-inputs)
* [Decrypt to View](/client-sdk/guides/decrypt-to-view)
* [Decrypt to Transact](/client-sdk/guides/decrypt-to-tx)
* [Permits](/client-sdk/guides/permits)
