You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/relayer/1.4.x/guides/zama-fhevm-counter-guide.mdx
+57-45Lines changed: 57 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,20 +4,29 @@ title: Zama FHEVM Counter Guide
4
4
5
5
## Overview
6
6
7
-
This guide walks through an end-to-end integration between OpenZeppelin Relayer and a Zama FHEVM contract, using the counter example shipped with the [OpenZeppelin Relayer SDK](https://github.com/OpenZeppelin/openzeppelin-relayer-sdk). The counter contract is deployed from [Zama's fhevm-hardhat-template](https://github.com/zama-ai/fhevm-hardhat-template) and demonstrates the two relayer responsibilities in an FHEVM flow:
7
+
This guide walks through an end-to-end integration between the OpenZeppelin Relayer and a Zama FHEVM contract, using the counter example shipped with the [OpenZeppelin Relayer SDK](https://github.com/OpenZeppelin/openzeppelin-relayer-sdk). The counter contract is deployed from [Zama's fhevm-hardhat-template](https://github.com/zama-ai/fhevm-hardhat-template) and demonstrates the two OpenZeppelin Relayer responsibilities in an FHEVM flow:
8
8
9
9
-**Transaction submission**: sending an encrypted `increment()` call on-chain.
10
10
-**EIP-712 signing**: signing the typed-data payload that authorizes user decryption of the counter's encrypted state.
11
11
12
+
<Callouttype="info">
13
+
14
+
**Terminology.** In this guide:
15
+
16
+
-**OpenZeppelin Relayer** (or "OZ Relayer") — this service. It holds an EVM signer, submits on-chain transactions, and signs EIP-712 payloads.
17
+
-**Zama Relayer** — the Zama-operated service that the [`@zama-fhe/relayer-sdk`](https://www.npmjs.com/package/@zama-fhe/relayer-sdk) talks to under the hood. It serves FHE public keys and routes decryption requests to the Zama KMS / coprocessor. Applications interact with it only indirectly through the Zama Relayer SDK. It holds no OpenZeppelin key material and does not sign EIP-712.
18
+
19
+
</Callout>
20
+
12
21
By the end of this guide you will have:
13
22
14
23
- Configured a Zama FHE instance in your application.
15
24
- Read and decrypted an encrypted counter value from the contract.
16
-
- Submitted an encrypted increment through the relayer and waited for confirmation.
25
+
- Submitted an encrypted increment through the OpenZeppelin Relayer and waited for confirmation.
17
26
- Re-read and decrypted the updated value.
18
27
19
28
<Callout>
20
-
The FHE encryption/decryption primitives run in your application using the Zama SDK. The relayer never sees cleartext values or your decryption keypair — it only sends transactions and signs EIP-712 payloads.
29
+
The FHE encryption/decryption primitives run in your application using the Zama Relayer SDK. The OpenZeppelin Relayer never sees cleartext values or your decryption keypair — it only sends transactions and signs EIP-712 payloads.
21
30
</Callout>
22
31
23
32
## Prerequisites
@@ -42,29 +51,32 @@ The rest of this guide explains the moving parts, references the example files,
42
51
43
52
Four components participate in the flow:
44
53
45
-
-**Your script**: orchestrates the flow and prints progress.
46
-
-**OpenZeppelin Relayer**: signs typed data and submits transactions.
-**FHEVM contract**: stores encrypted state on-chain.
54
+
-**Your script** — orchestrates the flow and prints progress.
55
+
-**OpenZeppelin Relayer** — signs typed data and submits transactions. The only component holding an EVM signer.
56
+
-**Zama Relayer SDK (client)** + **Zama Relayer (service)** — the SDK encrypts inputs, generates keypairs for user decryption, and builds EIP-712 payloads; the Zama Relayer service serves FHE public keys and routes decryption requests to the Zama KMS / coprocessor. The application only interacts with it indirectly through the SDK.
57
+
-**FHEVM contract** — stores encrypted state on-chain.
49
58
50
-
The key boundary is that the relayer does not do any encryption. The Zama SDK handles encryption and decryption primitives; the relayer provides transaction execution and signature authorization.
59
+
The key boundary is that the OpenZeppelin Relayer does not do any encryption or decryption. The Zama Relayer SDK (and, behind it, the Zama Relayer service) handles all FHE primitives; the OpenZeppelin Relayer only provides EVM transaction execution and EIP-712 signing.
Zama FHEVM contracts live on standard EVM networks, so the relayer is configured as a regular `evm` relayer.
79
+
Zama FHEVM contracts live on standard EVM networks, so the OpenZeppelin Relayer is configured as a regular `evm` relayer.
68
80
69
81
```json
70
82
{
@@ -96,9 +108,9 @@ Zama FHEVM contracts live on standard EVM networks, so the relayer is configured
96
108
97
109
**Important notes:**
98
110
99
-
- The relayer signer is used both for submitting the encrypted transaction and for signing the EIP-712 payload consumed by the Zama Gateway during user decryption. The same key backs both operations.
111
+
- The OpenZeppelin Relayer's signer is used both for submitting the encrypted transaction and for signing the EIP-712 payload that the Zama Relayer SDK requires for user decryption. The same key backs both operations.
100
112
- For production, prefer a hosted signer (AWS KMS, Google Cloud KMS, Turnkey, CDP) over `local`.
101
-
- The relayer must be funded on the target network so it can pay gas for FHEVM contract calls.
113
+
- The OpenZeppelin Relayer must be funded on the target network so it can pay gas for FHEVM contract calls.
-`RELAYER_BASE_PATH` defaults to `http://localhost:8080` if not set.
145
+
-`RELAYER_BASE_PATH` defaults to `http://localhost:8080` if not set. This points at your OpenZeppelin Relayer.
134
146
-`RPC_URL` defaults to the public Sepolia RPC if not set.
135
147
- If `ZAMA_PUBLIC_KEY` and `ZAMA_PRIVATE_KEY` are not set, the script generates a fresh decryption keypair on each run. Reusing the same keypair is useful for consistent user-decryption behavior across runs.
Copy the printed `ZAMA_PUBLIC_KEY` and `ZAMA_PRIVATE_KEY` values into your `.env` file.
161
173
162
174
<Callout>
163
-
The decryption keypair is an application-side secret. Do not pass the private key to the relayer; it is only used by the Zama SDK to decrypt results returned by the Gateway.
175
+
The decryption keypair is an applicationside secret. Do not pass the private key to the OpenZeppelin Relayer; it is only used by the Zama Relayer SDK to decrypt results returned by the Zama Relayer.
164
176
</Callout>
165
177
166
178
## Walkthrough
167
179
168
-
The following snippets show the relayer-specific integration points from `counter.ts`. Full code is in the [SDK repository](https://github.com/OpenZeppelin/openzeppelin-relayer-sdk/tree/main/examples/relayers/zama).
180
+
The following snippets show the OpenZeppelin Relayer's specific integration points from `counter.ts`. Full code is in the [SDK repository](https://github.com/OpenZeppelin/openzeppelin-relayer-sdk/tree/main/examples/relayers/zama).
169
181
170
-
### 1. Initialize the relayer client and Zama SDK
182
+
### 1. Initialize the OpenZeppelin Relayer client and Zama SDK
The contract exposes a `getCount()` view that returns the encrypted handle. Decoding the handle is a plain EVM call — no relayer involvement.
216
+
The contract exposes a `getCount()` view that returns the encrypted handle. Decoding the handle is a plain EVM call — no OpenZeppelin Relayer involvement.
205
217
206
218
```typescript
207
219
import { Interface } from'ethers';
@@ -218,9 +230,9 @@ async function getCount(contractAddress: string): Promise<string> {
218
230
219
231
The script first attempts public decryption. If the handle is not publicly decryptable, it falls back to user decryption (covered in [Decryption Model](#decryption-model) below).
220
232
221
-
### 4. Submit an encrypted `increment()` via the relayer
233
+
### 4. Submit an encrypted `increment()` via the OpenZeppelin Relayer
222
234
223
-
Encryption happens locally with the Zama SDK. The encrypted handle plus input proof are encoded into a normal EVM transaction and handed to `sendTransaction`.
235
+
Encryption happens locally with the Zama Relayer SDK. The encrypted handle plus input proof are encoded into a normal EVM transaction and handed to the OpenZeppelin Relayer's`sendTransaction`.
@@ -272,16 +284,16 @@ Zama FHEVM supports two decryption paths, and the example tries them in this ord
272
284
273
285
### 1. Public Decryption
274
286
275
-
If an encrypted handle is marked as publicly decryptable on-chain, the Zama SDK can decrypt it directly, with no authorization from the relayer.
287
+
If an encrypted handle is marked as publicly decryptable on-chain, the Zama Relayer SDK can decrypt it directly via the Zama Relayer, with no involvement from the OpenZeppelin Relayer.
276
288
277
289
```typescript
278
290
const result =awaitinstance.publicDecrypt([encryptedHandle]);
### 2. User Decryption (OpenZeppelin Relayer's signed EIP-712)
283
295
284
-
When decryption requires authorization, the Zama SDK builds an EIP-712 payload and the relayer signs it via `signTypedData`. The signature is then passed to `userDecrypt`, which retrieves the cleartext from the Zama Gateway.
296
+
When decryption requires authorization, the Zama Relayer SDK builds an EIP-712 payload and the **OpenZeppelin Relayer**signs it via `signTypedData`. The resulting signature is passed to `userDecrypt`, which uses it to authorize the Zama Relayer to return the cleartext.
The EIP-712 domain separator and message hash are computed locally using `ethers` so the relayer only has to sign the final hashes via its `signTypedData` endpoint.
341
+
The EIP-712 domain separator and message hash are computed locally using `ethers` so the OpenZeppelin Relayer only has to sign the final hashes via its `signTypedData` endpoint.
330
342
331
343
## Using on Mainnet
332
344
333
345
The example defaults to Sepolia. To run against Ethereum mainnet:
334
346
335
-
1. In `counter.ts`, use `MainnetConfig` instead of `SepoliaConfig` and provide the Zama Gateway API key:
347
+
1. In `counter.ts`, use `MainnetConfig` instead of `SepoliaConfig` and provide the Zama Relayer API key:
336
348
337
349
```typescript
338
350
import {
@@ -355,26 +367,26 @@ The example defaults to Sepolia. To run against Ethereum mainnet:
355
367
RPC_URL=https://ethereum-rpc.publicnode.com
356
368
```
357
369
358
-
3. Point `ZAMA_CONTRACT_ADDRESS` at your mainnet contract and configure the relayer for Ethereum mainnet.
370
+
3. Point `ZAMA_CONTRACT_ADDRESS` at your mainnet contract and configure the OpenZeppelin Relayer for Ethereum mainnet.
359
371
360
372
<Callout>
361
-
Mainnet requires API key authentication with the Zama Gateway. See the [Zama mainnet API key guide](https://docs.zama.org/protocol/relayer-sdk-guides/fhevm-relayer/mainnet-api-key) for instructions on obtaining one.
373
+
Mainnet requires API key authentication with the Zama Relayer. See the [Zama mainnet API key guide](https://docs.zama.org/protocol/relayer-sdk-guides/fhevm-relayer/mainnet-api-key) for instructions on obtaining one.
362
374
</Callout>
363
375
364
376
## Current Limitations
365
377
366
378
- The example is hardcoded for Sepolia via `SepoliaConfig` plus an explicit Sepolia RPC URL.
367
379
- It assumes a counter contract shape compatible with the included ABI.
368
380
- Logging and error handling are intentionally simple — this is a demo script, not production code.
369
-
- It does not cover relayer creation or contract deployment.
381
+
- It does not cover OpenZeppelin Relayer creation or contract deployment.
370
382
371
383
## Troubleshooting
372
384
373
385
-**`Missing required environment variable`**: one of the required values in `.env` is unset.
374
-
-**`did not return an address`**: the configured relayer id is valid for the API, but the response did not include an EVM address. Check that the relayer is fully provisioned and the signer is reachable.
386
+
-**`did not return an address`**: the configured `RELAYER_ID`is valid for the API, but the response did not include an EVM address. Check that the OpenZeppelin Relayer is fully provisioned and the signer is reachable.
375
387
-**`Public decryption failed`**: this can be expected depending on the contract and permissions. The script will then try user decryption.
376
-
-**`User decryption failed`**: check that the relayer can sign typed data correctly and that the contract address and decryption keypair are the ones you expect. Confirm the EIP-712 `startTimeStamp`/`durationDays` window is valid.
377
-
-**Transaction polling timeout**: the transaction may still be pending, the relayer may be unhealthy, or the target chain may be slow. Inspect `getTransactionById` directly and check relayer logs.
388
+
-**`User decryption failed`**: check that the OpenZeppelin Relayer can sign typed data correctly and that the contract address and decryption keypair are the ones you expect. Confirm the EIP-712 `startTimeStamp` / `durationDays` window is valid.
389
+
-**Transaction polling timeout**: the transaction may still be pending, the OpenZeppelin Relayer may be unhealthy, or the target chain may be slow. Inspect `getTransactionById` directly and check OpenZeppelin Relayer logs.
0 commit comments