> ## 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 Setup

> Configure and connect the @cofhe/sdk client with viem, Ethers, or wagmi

This page covers the core SDK lifecycle:

1. Create config (`createCofheConfig`)
2. Create client (`createCofheClient`)
3. Connect (`client.connect`)
4. Manage connection (change account / disconnect)

## 1. Create config

Import `createCofheConfig` from the entrypoint that matches your runtime:

* Browser apps: `@cofhe/sdk/web`
* Node.js scripts/backends: `@cofhe/sdk/node`

The only required field is `supportedChains`.

```typescript theme={null}
import { createCofheConfig } from '@cofhe/sdk/web';
import { chains } from '@cofhe/sdk/chains';

const config = createCofheConfig({
  supportedChains: [chains.sepolia],

  // Optional knobs
  // defaultACPExpiration: 60 * 60 * 24 * 30,
  // useWorkers: true,
});
```

## 2. Create the client

```typescript theme={null}
import { createCofheConfig, createCofheClient } from '@cofhe/sdk/web';
import { chains } from '@cofhe/sdk/chains';

const config = createCofheConfig({ supportedChains: [chains.sepolia] });
const cofheClient = createCofheClient(config);

cofheClient.connected; // false
cofheClient.connecting; // false
```

## 3. Connect

The SDK connects to CoFHE using viem clients:

* `PublicClient`: read-only chain access
* `WalletClient`: signing + sending transactions

The wallet client must carry an account. `connect` reads the connected address from `walletClient.account`, and falls back to `eth_accounts` on the transport. A wallet client created with `http()` and no `account` has neither, so `connect` throws `PublicWalletGetAddressesFailed` and the client stays disconnected.

In the browser, take the account from the injected wallet. In Node.js, build it from a private key.

<CodeGroup>
  ```typescript Browser wallet theme={null}
  import { createPublicClient, createWalletClient, custom, http } from 'viem';
  import { sepolia } from 'viem/chains';

  const [account] = await window.ethereum.request({
    method: 'eth_requestAccounts',
  });

  const publicClient = createPublicClient({
    chain: sepolia,
    transport: http(),
  });

  const walletClient = createWalletClient({
    chain: sepolia,
    transport: custom(window.ethereum),
    account,
  });

  await cofheClient.connect(publicClient, walletClient);

  cofheClient.connected; // true
  cofheClient.connection.account; // the address selected in the wallet
  ```

  ```typescript Node.js theme={null}
  import { createPublicClient, createWalletClient, http } from 'viem';
  import { sepolia } from 'viem/chains';
  import { privateKeyToAccount } from 'viem/accounts';

  const account = privateKeyToAccount('0xYOUR_PRIVATE_KEY');

  const publicClient = createPublicClient({
    chain: sepolia,
    transport: http(),
  });

  const walletClient = createWalletClient({
    chain: sepolia,
    transport: http(),
    account,
  });

  await cofheClient.connect(publicClient, walletClient);

  cofheClient.connected; // true
  cofheClient.connection.account; // account.address
  ```
</CodeGroup>

### Using adapters

If you use Ethers, `@cofhe/sdk/adapters` converts an Ethers provider and signer into viem-shaped clients, and derives the account from the signer. wagmi already gives you viem clients, so pass them to `connect` directly. The wallet client from `useWalletClient` carries the connected account.

<CodeGroup>
  ```typescript Ethers v6 theme={null}
  import { Ethers6Adapter } from '@cofhe/sdk/adapters';
  import { ethers } from 'ethers';

  const provider = new ethers.JsonRpcProvider('https://rpc.sepolia.org');
  const signer = new ethers.Wallet('0xYOUR_PRIVATE_KEY', provider);

  const { publicClient, walletClient } = await Ethers6Adapter(
    provider,
    signer
  );

  await cofheClient.connect(publicClient, walletClient);
  ```

  ```typescript Ethers v5 theme={null}
  import { Ethers5Adapter } from '@cofhe/sdk/adapters';
  import { ethers } from 'ethers5';

  const provider = new ethers.providers.JsonRpcProvider(
    'https://rpc.sepolia.org'
  );
  const signer = new ethers.Wallet('0xYOUR_PRIVATE_KEY').connect(provider);

  const { publicClient, walletClient } = await Ethers5Adapter(
    provider,
    signer
  );

  await cofheClient.connect(publicClient, walletClient);
  ```

  ```tsx Wagmi theme={null}
  import { useEffect } from 'react';
  import { usePublicClient, useWalletClient } from 'wagmi';

  function useConnectCofhe() {
    const publicClient = usePublicClient();
    const { data: walletClient } = useWalletClient();

    useEffect(() => {
      // useWalletClient resolves only after the user connects a wallet
      if (!publicClient || !walletClient) return;
      cofheClient.connect(publicClient, walletClient);
    }, [publicClient, walletClient]);
  }
  ```
</CodeGroup>

## 4. Managing connections

### Reconnect behavior

Calling `connect` again with the same clients is a no-op. Calling it with new clients replaces the connection state.

### Changing connected account

To switch the client's connected account, call `cofheClient.connect()` with a wallet client that carries the new account.

```typescript theme={null}
import { privateKeyToAccount } from 'viem/accounts';

const bob = privateKeyToAccount('0xBOB_PRIVATE_KEY');
const alice = privateKeyToAccount('0xALICE_PRIVATE_KEY');

const bobWalletClient = createWalletClient({
  chain: sepolia,
  transport: http(),
  account: bob,
});
const aliceWalletClient = createWalletClient({
  chain: sepolia,
  transport: http(),
  account: alice,
});

// Connect as Bob
await cofheClient.connect(publicClient, bobWalletClient);
cofheClient.connection.account; // bob.address

// Switch to Alice
await cofheClient.connect(publicClient, aliceWalletClient);
cofheClient.connection.account; // alice.address
```

### Disconnecting

To manually disconnect, call `cofheClient.disconnect()`. This clears the in-memory connection state (clients/account/chainId) and marks the client as disconnected.

It does **not** delete persisted ACPs or stored FHE keys.

```typescript theme={null}
cofheClient.disconnect();
cofheClient.connected; // false
```
