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
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
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 transferredvalue - 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
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
Reentrancy in Transfer Callbacks
Risk: The Best practices:
onConfidentialTransferReceived callback is executed during the transfer, creating reentrancy opportunities.Recommendation:- 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
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 vs FHERC20Permit
Choose the right permission mechanism for your use case:When to Use Operators
Standard User Interactions
Standard User Interactions
Use operators when:Advantages:
- User directly approves via wallet transaction
- Simple on-chain permission grants
- No meta-transaction support needed
- Simple, direct approach
- No signature complexity
- Immediate effect
Long-Lived Permissions
Long-Lived Permissions
Use operators when:
- Granting recurring permissions
- No need for gasless approvals
When to Use FHERC20Permit
Gasless Approvals
Gasless Approvals
Use permit when:Advantages:
- Users shouldn’t pay gas for approvals
- Implementing meta-transactions
- Better UX for new users
- User pays no gas for approval
- One transaction instead of two
- Better onboarding experience
Atomic Operations
Atomic Operations
Use permit when:
- Combining approval + action in single transaction
- Reducing transaction count
Comparison Table
| Factor | Operator (setOperator) | FHERC20Permit (permit) |
|---|---|---|
| Gas Cost | User pays gas | Relayer can pay gas |
| Transaction Count | 2 (approve + action) | 1 (combined) |
| Complexity | Simple | Requires signatures |
| User Experience | Standard wallet flow | Gasless, seamless |
| Use Case | General permissions | Meta-transactions |
| Implementation | Direct on-chain | EIP-712 signatures |
Common Pitfalls and Solutions
Pitfall: Forgetting to Grant FHE Access
Pitfall: Forgetting to Grant FHE Access
Problem:Solution:
Pitfall: Not Handling Zero Transfers
Pitfall: Not Handling Zero Transfers
Problem:Solution:
Pitfall: Incorrect Claim Management
Pitfall: Incorrect Claim Management
Problem:Solution:
Pitfall: Operator Expiration Timing
Pitfall: Operator Expiration Timing
Problem:Solution:
Pitfall: Not Validating Callback Returns
Pitfall: Not Validating Callback Returns
Problem:Solution:
Related Topics
- Review Core Features for fundamental concepts
- Learn about Operators for permission management
- Explore Transfer Callbacks for safe transfers
- Check the Migration Guide for detailed migration steps