useEERC

The useEERC hook is the main entry point for the SDK, responsible for initializing and managing interactions with the Encrypted ERC (eERC) protocol. It connects to the eERC contract, ensuring access to core functionalities such as user registration, encrypted token management, and balance decryption.

Usage

1const { publicClient } = usePublicClient();
2const { walletClient } = useWalletClient();
3
4const {
5 isInitialized,
6 isAllDataFetched,
7 isRegistered,
8 isConverter,
9 publicKey,
10 auditorAddress,
11 owner,
12 auditorPublicKey,
13 isAuditorKeySet,
14 name,
15 symbol,
16 isDecryptionKeySet,
17 areYouAuditor,
18 hasBeenAuditor,
19
20 // actions
21 generateDecryptionKey,
22 register,
23 auditorDecrypt,
24 isAddressRegistered,
25 useEncryptedBalance,
26 refetchEercUser,
27 refetchAuditor,
28 setContractAuditorPublicKey,
29} = useEERC(
30 publicClient,
31 walletClient,
32 contractAddress,
33 circuitURLs,
34 decryptionKey
35);

Parameters

  • publicClient : PublicClient

  • walletClient : Wallet Client for sending transactions

  • contractAddress : Address of the deployed eERC contract

  • circuitURLs :

1 {
2 register: {
3 wasm: string,
4 zkey: string,
5 },
6 transfer: {
7 wasm: string,
8 zkey: string,
9 },
10 mint: {
11 wasm: string,
12 zkey: string,
13 },
14 withdraw: {
15 wasm: string,
16 zkey: string,
17 },
18 burn: {
19 wasm: string,
20 zkey: string,
21 },
22 }

wasm/zkey URLs : The URLs of the files used for generating the proofs.

  • decryptionKey (optional) : string A decryption key associated with the user’s account. If not provided, the decryption key can be generated during registration or using the generateDecryptionKey method.

If the Wasm files are being served locally, ensure the URLs start with / (e.g., /path/to/wasm_file.wasm). For remote files, provide the full URL (e.g., https://example.com/wasm_file.wasm). Incorrect URL formatting may prevent the Wasm modules from loading correctly.

The decryption key can be generated during the registration process or by calling the generateDecryptionKey function. However, it is essential to use a wallet created with a seed and not through an MPC or any other mechanism that generates wallets without a deterministic seed. During decryption key derivation, the user signs a predefined message. If the wallet lacks a consistent seed, the signature will vary, and the user will not be able to use the protocol correctly. Always ensure the wallet is securely and deterministically generated using a seed.

Returns

The useEERC hook returns an object containing both state variables and methods that provide full interaction with the eERC protocol.

  • isInitialized: boolean Indicates whether the SDK has been initialized successfully.
  • isAllDataFetched: boolean Indicates that all required data fetched from protocol.
  • isRegistered: boolean Indicates whether the user is registered with the eERC protocol.
  • isConverter: boolean Specifies if the eERC is converter or not.
  • publicKey: bigint[] Public key of the user.
  • auditorAddress: 0x{string} Protocol auditor address.
  • owner: 0x{string} Protocol owner (deployer) address.
  • auditorPublicKey: bigint[] Public key of the eERC auditor.
  • isAuditorKeySet: boolean Indicates whether the auditor key is set in the protocol.
  • name: string The name of the eERC token (available only in standalone mode).
  • symbol: string The symbol of the eERC token (available only in standalone mode).
  • isDecryptionKeySet: boolean Returns true if the user is registered and decryption key is not set.
  • areYouAuditor: boolean Returns true if the current user is an auditor for the eERC contract.
  • hasBeenAuditor: { isChecking: boolean; isAuditor: boolean } Tracks whether the user has been an auditor and whether it is currently being checked.

eERC transfer app only support standalone eERCs.

Methods

  • generateDecryptionKey(): Promise<string> Generates a decryption key for the user, set the key internally and return the key for storing later use.
  • register(): Promise<{key:string; transactionHash:string}> Registers the user with the eERC protocol. Only one registration per L1 is required.
    • Returns:
      • key: string - The decryption key.
      • transactionHash: string - The transaction hash of the registration.
  • auditorDecrypt(): Promise<DecryptedTransaction[]> Decrypts encrypted transactions using the auditor’s public key. If the user is not an auditor, this method will throw an error.
    • Returns an array of DecryptedTransaction
1type DecryptedTransaction = {
2 type: string;
3 amount: string;
4 sender: `0x${string}`;
5 receiver: `0x${string}` | null;
6 transactionHash: `0x${string}`;
7};
  • isAddressRegistered(address: string): {isRegistered: boolean; error: string } Checks if a specific address is registered with the eERC protocol.
  • useEncryptedBalance Returns a custom hook that enables encrypted operations like mint, burn and transfer privately. The following section will explain detailed usage.
  • refetchEercUser Returns the public key of the registered user.
  • refetchAuditor Returns the public key of the auditor.
  • setContractAuditorPublicKey(address: string): Promise<{transactionHash:string}> Sets the auditor’s public key for the eERC.

Only the owner can set the auditor’s public key.


For any additional questions, please view our other knowledge base articles or contact a support team member via the chat button. Examples are for illustrative purposes only.

Learn More About AvaCloud | Download Case Studies | Schedule an AvaCloud Demo