Introduction
The zkp2p-contracts hook system provides extensible lifecycle integration points that allow custom logic to execute during intent operations. Hooks enable features like cross-chain bridging, access control, and custom validation without modifying core protocol contracts.Hook Types
The protocol supports two types of hooks:Pre-Intent Hooks
Pre-intent hooks execute duringsignalIntent before state changes are committed. They are used for validation and access control.
Interface: IPreIntentHook
Use Cases:
- Validating taker eligibility
- Enforcing whitelist restrictions
- Signature-based gating
- Custom authorization logic
- Called before intent is created
- Should revert to reject the intent
- Read-only validation (no state modifications expected)
- Can access full intent context via
PreIntentContext
Post-Intent Hooks
Post-intent hooks execute duringfulfillIntent after core protocol logic completes. They handle funds and execute custom actions with the released tokens.
Interfaces: IPostIntentHook (V1) and IPostIntentHookV2 (V2)
Use Cases:
- Bridging tokens to other chains
- Custom fund distribution
- Protocol integrations
- Automated DeFi operations
- Called after intent is fulfilled
- Receives tokens from the orchestrator
- Can perform arbitrary operations with funds
- V2 uses structured context for better composability
Hook Execution Flow
Pre-Intent Hook Flow
Post-Intent Hook Flow
Interface Reference
IPreIntentHook
taker- Address signaling the intentescrow- Escrow contract holding the depositdepositId- ID of the deposit being lockedamount- Amount to lock from the depositto- Recipient address for the fundspaymentMethod- Payment method identifier (e.g., Venmo, PayPal)fiatCurrency- Fiat currency code (e.g., USD, EUR)conversionRate- Exchange rate (scaled)referrer- Referrer address for fee sharingreferrerFee- Fee allocated to referrerpreIntentHookData- Custom data passed from signalIntent call
IPostIntentHook (V1)
_intent- Full intent data structure_amountNetFees- Amount after fees have been deducted_fulfillIntentData- Custom data passed from fulfillIntent call
IPostIntentHookV2
- Structured context instead of raw Intent struct
- Separates signal-time data (
signalHookData) from fulfill-time data (fulfillHookData) - Includes
intentHashfor event correlation - Better typing with dedicated context structs
Hook Configuration
Setting Pre-Intent Hooks (V2 Only)
In OrchestratorV2, depositors can set hooks per deposit:Setting Post-Intent Hooks
Post-intent hooks are specified duringsignalIntent:
V1 (Orchestrator):
Security Considerations
Pre-Intent Hook Security
- Reentrancy Protection: Hooks are called within a reentrancy-protected context
- Gas Limits: Complex validation may hit gas limits; keep logic simple
- DoS Resistance: Hooks should not depend on external state that could be manipulated
- Authorization: Always verify
msg.senderis an authorized orchestrator
Post-Intent Hook Security
- Token Handling: Hooks must pull exact approved amount to prevent stranded funds
- Fallback Logic: Consider graceful degradation instead of reverting (see AcrossBridgeHook)
- Authorization: Verify caller is authorized orchestrator
- Approval Management: Always reset approvals after operations
- External Calls: Use try-catch for external protocol interactions
Implementation Examples
See the following pages for detailed hook implementations:- AcrossBridgeHook - Cross-chain token bridging
- SignatureGatingPreIntentHook - Off-chain signature validation
- WhitelistPreIntentHook - Address whitelist enforcement
Hook Development Best Practices
For Pre-Intent Hooks
- Fail Fast: Revert immediately on validation failure
- Minimal State: Avoid complex state reads when possible
- Clear Errors: Use descriptive custom errors
- Registry Check: Always validate orchestrator authorization
For Post-Intent Hooks
- Exact Pulls: Pull exactly the approved amount
- Approval Hygiene: Reset approvals after operations
- Graceful Degradation: Consider fallback behavior instead of reverting
- Event Emission: Emit detailed events for tracking
- Rescue Functions: Implement admin rescue for stuck funds
Related Contracts
- Orchestrator - V1 orchestrator with post-intent hooks
- OrchestratorV2 - V2 orchestrator with pre and post hooks
- PostIntentHookRegistry - V1 hook registry
- OrchestratorRegistry - Authorized orchestrator registry