Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Cofhe Client

This page covers the core SDK lifecycle:

  1. Create config (createCofheConfig)
  2. Create client (createCofheClient)
  3. Connect to client (client.connect)
  4. Managing 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.

import { createCofheConfig } from '@cofhe/sdk/web';
import { chains } from '@cofhe/sdk/chains';
 
const config = createCofheConfig({
  supportedChains: [chains.sepolia],
 
  // Optional knobs
  // defaultPermitExpiration: 60 * 60 * 24 * 30,
  // useWorkers: true,
});

2) Create cofhe client

Import createCofheClient from the entrypoint that matches your runtime:

  • Browser apps: @cofhe/sdk/web
  • Node.js scripts/backends: @cofhe/sdk/node
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

If you already have viem clients, you can pass them directly to cofheClient.connect(publicClient, walletClient).

If you're using another wallet/provider stack, see below for @cofhe/sdk/adapters.

Connecting initializes the SDK's in-memory connection state (account, chainId, and the viem clients).

const publicClient: PublicClient = createPublicClient({
  chain: sepolia,
  transport: http(),
});
 
const walletClient: WalletClient = createWalletClient({
  chain: sepolia,
  transport: http(),
});
 
await cofheClient.connect(publicClient, walletClient);
 
// Check connection
cofheClient.connected;

Using adapters (optional)

If you're using another wallet/provider stack, @cofhe/sdk/adapters exposes adapters that convert into viem-shaped clients.

We currently support adapters for Ethers v5 and v6 clients and providers, wagmi clients, and smart wallet clients:

Ethers v6
import { ethers } from 'ethers'; // ethers v6
 
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);

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 updated viem clients.

const publicClient = createPublicClient({
  chain: sepolia,
  transport: http(),
});
const bobWalletClient = createWalletClient({
  chain: sepolia,
  transport: http(),
  account: bobAddress,
});
const aliceWalletClient = createWalletClient({
  chain: sepolia,
  transport: http(),
  account: aliceAddress,
});
 
// connect to Bob's wallet
await cofheClient.connect(publicClient, bobWalletClient);
 
cofheClient.connection.account;
 
// connect to Alice's wallet
await cofheClient.connect(publicClient, aliceWalletClient);
 
cofheClient.connection.account;

Disconnecting

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

It does not delete persisted permits or stored FHE keys.

cofheClient.disconnect();
 
cofheClient.connected;