Decrypt values from event logs
Decrypt an FHE encrypted value pulled straight off an event log — the common server-side pattern for indexers, wallet history, and bridges.
const cleartext = await sdk.decryption.decryptValues([
{ encryptedValue: transfer.encryptedAmount, contractAddress: tokenAddress },
]);Example
import { decodeConfidentialTransfer, TOKEN_TOPICS } from "@zama-fhe/sdk";
import type { Address } from "viem";
// `sdk` and `publicClient` come from your Node.js backend setup (createConfig +
// node() relayer). `./client` stands in for wherever you export them — the
// Node.js backend guide builds them as inline consts, so extract them there.
import { sdk, publicClient } from "./client";
const tokenAddress = "0xYourConfidentialToken" as Address;
// The token's deployment block. Providers cap getLogs block ranges, so large
// backfills page forward from here instead of fetching everything in one call.
const startBlock = 0n;
// 1. Fetch raw logs for the confidential token events.
const logs = await publicClient.getLogs({
address: tokenAddress,
topics: [TOKEN_TOPICS],
fromBlock: startBlock,
toBlock: "latest",
});
// 2. Decode the ConfidentialTransfer logs. Each carries an `encryptedAmount` —
// the same kind of encrypted value you get from a balance. The decoder
// returns null for non-matching logs, so `flatMap(... ?? [])` drops them.
const transfers = logs.flatMap((log) => decodeConfidentialTransfer(log) ?? []);
// 3. Decrypt every amount in a single call. `decryptValues` groups inputs by
// contract and returns a record keyed by the encrypted value.
const cleartext = await sdk.decryption.decryptValues(
transfers.map((t) => ({ encryptedValue: t.encryptedAmount, contractAddress: tokenAddress })),
);
for (const transfer of transfers) {
console.log(`${transfer.from} → ${transfer.to}: ${cleartext[transfer.encryptedAmount]}`);
}Steps
1. Fetch and decode the logs
Event
Encrypted value field
Meaning
2. Decrypt the encrypted values
3. Make sure your signer is allowed to decrypt
In the browser (React)
Next steps
Last updated