Skip to main content

Overview

Building with FHERC20 requires understanding both traditional smart contract best practices and the unique considerations that come with fully homomorphic encryption. This guide provides actionable recommendations for secure, efficient, and privacy-preserving implementations.

Security Best Practices

Operator Permissions - Full Access Risk

Risk: Operators have unlimited access to a user’s balance until expiration.Recommendation:
Best practices:
  • Grant operators only for the minimum necessary time
  • Use specific timeframes: 5-10 minutes for single transactions, 1 day for recurring operations
  • Document operator requirements clearly in your UI
  • Provide easy revocation by calling setOperator(address, block.timestamp)

Zero-Replacement Handling

Risk: Insufficient balance transfers zero tokens instead of reverting, which can lead to unexpected behavior.Recommendation:
Best practices:
  • Always work with the returned euint64 transferred value
  • Implement balance checks when transfer success is critical
  • Use the transferred amount in subsequent operations, not the requested amount
  • Consider using transfer callbacks for atomic operations

Access Control Management

Risk: Improper FHE access control can prevent users from accessing their own balances or expose data to unauthorized parties.Recommendation:
Best practices:
  • Always call FHE.allowThis() for values the contract needs to use
  • Always call FHE.allow(value, user) for values users need to access
  • Grant access immediately after creating or modifying encrypted values
  • Review access control on all encrypted state changes

Reentrancy in Transfer Callbacks

Risk: The onConfidentialTransferReceived callback is executed during the transfer, creating reentrancy opportunities.Recommendation:
Best practices:
  • Use OpenZeppelin’s ReentrancyGuard for all receiver implementations
  • Follow checks-effects-interactions pattern
  • Minimize external calls in callbacks
  • Keep callback logic simple and gas-efficient

Input Validation

Risk: Unvalidated inputs can lead to unexpected behavior or security vulnerabilities.Recommendation:
Best practices:
  • Validate token source (msg.sender)
  • Validate transfer initiator (operator) and source (from) when needed
  • Check data length before decoding
  • Validate decoded parameters for reasonable bounds
  • Return FHE.asEbool(false) to reject invalid transfers

Operators Best Practices

When to Use Operators

Standard User Interactions

Use operators when:
  • User directly approves via wallet transaction
  • Simple on-chain permission grants
Advantages:
  • Simple, direct approach
  • No signature complexity
  • Immediate effect

Long-Lived Permissions

Use operators when:
  • Granting recurring permissions

Comparison Table


Common Pitfalls and Solutions

Problem:
Solution:
Problem:
Solution:
Problem:
Solution:
Problem:
Solution:
Problem:
Solution: