import { createCofheConfig, createCofheClient } from '@cofhe/sdk/web';
import { Encryptable, FheTypes } from '@cofhe/sdk';
import { chains } from '@cofhe/sdk/chains';
import { createPublicClient, createWalletClient, http, custom } from 'viem';
import { sepolia } from 'viem/chains';
// 1. Initialize
const config = createCofheConfig({
supportedChains: [chains.sepolia],
});
const client = createCofheClient(config);
const publicClient = createPublicClient({
chain: sepolia,
transport: http(),
});
const walletClient = createWalletClient({
chain: sepolia,
transport: custom(window.ethereum),
});
await client.connect(publicClient, walletClient);
// 2. Create a permit
await client.permits.getOrCreateSelfPermit();
// 3. Encrypt and deposit
const [encryptedAmount] = await client
.encryptInputs([Encryptable.uint64(100n)])
.onStep((step, ctx) => {
if (ctx?.isStart) console.log(`Encrypting: ${step}...`);
})
.execute();
await contract.deposit(encryptedAmount);
// 4. Decrypt for UI display
const ctHash = await contract.getBalance();
const balance = await client
.decryptForView(ctHash, FheTypes.Uint64)
.execute();
console.log('Balance:', balance); // 100n
// 5. Decrypt for on-chain verification
const { decryptedValue, signature } = await client
.decryptForTx(ctHash)
.withPermit()
.execute();
await contract.publishBalance(ctHash, decryptedValue, signature);