Skip to main content
Connect to an FHE-enabled contract, encrypt a value, send it on-chain, and decrypt the result — all from JavaScript.

Prerequisites

  • Node.js 18+
  • A deployed FHE contract on a supported network (e.g. Sepolia)
  • A wallet with testnet ETH

1. Install

npm install @cofhe/sdk@^0.4.0 viem

2. Create and connect the client

Import from @cofhe/sdk/web for browser apps or @cofhe/sdk/node for Node.js scripts.
import { createCofheConfig, createCofheClient } from '@cofhe/sdk/web';
import { chains } from '@cofhe/sdk/chains';
import { createPublicClient, createWalletClient, http, custom } from 'viem';
import { sepolia } from 'viem/chains';

// 1. Configure
const config = createCofheConfig({
  supportedChains: [chains.sepolia],
});
const client = createCofheClient(config);

// 2. Create viem clients (browser wallet)
const publicClient = createPublicClient({
  chain: sepolia,
  transport: http(),
});
const walletClient = createWalletClient({
  chain: sepolia,
  transport: custom(window.ethereum),
});

// 3. Connect
await client.connect(publicClient, walletClient);
If you use Ethers.js instead of viem, see the Client Setup guide for adapter usage.

3. Encrypt and send

import { Encryptable } from '@cofhe/sdk';

// Encrypt a uint32 value
const [encrypted] = await client
  .encryptInputs([Encryptable.uint32(42n)])
  .execute();

// Pass it to your contract
await contract.setValue(encrypted);

4. Decrypt for display

import { FheTypes } from '@cofhe/sdk';

// Create a permit (one-time per account + chain)
await client.permits.getOrCreateSelfPermit();

// Read the encrypted handle from your contract
const ctHash = await contract.storedValue();

// Decrypt locally
const plaintext = await client
  .decryptForView(ctHash, FheTypes.Uint32)
  .execute();

console.log(plaintext); // 42n

Next steps