|
| 1 | +# Tool Invocation Flow |
| 2 | + |
| 3 | +Unified 402 challenge-response for both free and paid tools. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Sequence Diagram |
| 8 | + |
| 9 | +``` |
| 10 | +Caller Tool Server ToolRegistry (onchain) Facilitator OpenSea API |
| 11 | + │ │ │ │ │ |
| 12 | + │ 1. POST /tool (no auth) │ │ │ │ |
| 13 | + │ ────────────────────────> │ │ │ │ |
| 14 | + │ │ │ │ │ |
| 15 | + │ 2. 402 PaymentRequirements│ │ │ │ |
| 16 | + │ maxAmountRequired: "0" │ (free) │ │ │ |
| 17 | + │ maxAmountRequired: amt │ (paid) │ │ │ |
| 18 | + │ <──────────────────────── │ │ │ │ |
| 19 | + │ │ │ │ │ |
| 20 | + │ 3. Sign X-Payment │ │ │ │ |
| 21 | + │ (EIP-3009 │ │ │ │ |
| 22 | + │ TransferWithAuthorization│ │ │ │ |
| 23 | + │ from: caller │ │ │ │ |
| 24 | + │ to: payTo from 402 │ │ │ │ |
| 25 | + │ value: 0 or amount │ │ │ │ |
| 26 | + │ nonce: random 32 bytes) │ │ │ │ |
| 27 | + │ │ │ │ │ |
| 28 | + │ 4. POST /tool │ │ │ │ |
| 29 | + │ X-Payment: base64(...) │ │ │ │ |
| 30 | + │ ────────────────────────> │ │ │ │ |
| 31 | + │ │ │ │ │ |
| 32 | + │ │ 5. ecrecover X-Payment │ │ │ |
| 33 | + │ │ recover caller address │ │ │ |
| 34 | + │ │ verify to == operator │ │ │ |
| 35 | + │ │ check TTL │ │ │ |
| 36 | + │ │ │ │ │ |
| 37 | + │ │ 6. tryHasAccess(toolId, │ │ │ |
| 38 | + │ │ callerAddr, data) │ │ │ |
| 39 | + │ │ ────────────────────────> │ │ │ |
| 40 | + │ │ granted: true/false │ │ │ |
| 41 | + │ │ <──────────────────────── │ │ │ |
| 42 | + │ │ false → 403 │ │ │ |
| 43 | + │ │ │ │ │ |
| 44 | + │ │ 7. handler(input, ctx) │ │ │ |
| 45 | + │ │ validate input/output │ │ │ |
| 46 | + │ │ │ │ │ |
| 47 | + │ │ 8. gate.settle() ─────────────────────────────> │ │ |
| 48 | + │ │ (paid only: POST │ │ │ |
| 49 | + │ │ /settle with payload) │ { tx: "0x..." } │ │ |
| 50 | + │ │ <───────────────────────────────────────────────── │ │ |
| 51 | + │ │ │ │ │ |
| 52 | + │ │ 9. POST /api/v2/tools/usage ──────────────────────────────────────> │ |
| 53 | + │ │ (awaited before response) │ │ |
| 54 | + │ │ free: eip3009_authorization (forward signature)│ │ |
| 55 | + │ │ paid: x402_settlement (forward tx hash) │ │ |
| 56 | + │ │ <────────────────────────────────────────────────────────────────── │ |
| 57 | + │ │ │ │ │ |
| 58 | + │ 10. 200 OK (tool output) │ │ │ │ |
| 59 | + │ <──────────────────────── │ │ │ │ |
| 60 | +``` |
| 61 | + |
| 62 | +### Key details |
| 63 | + |
| 64 | +- **Unified 402 pattern** — free and paid tools use the same challenge-response. The only difference is `maxAmountRequired` (`"0"` vs actual USDC price). |
| 65 | +- **X-Payment = EIP-3009 `TransferWithAuthorization`** — signed EIP-712 typed data in the USDC domain. `value=0` for identity proof only; `value=price` for paid tools. |
| 66 | +- **Pure ecrecover** — server-side identity verification is `recoverTypedDataAddress()`, no RPC needed. |
| 67 | +- **Combined predicate + payment** — `paidPredicateGate` resolves identity and payment in a single 402 round trip. The real-value signature proves identity (`from`) and authorizes payment simultaneously. Predicate is checked before settlement — if access denied, 403 and no funds move. |
| 68 | +- **Usage reporting is awaited** — the report completes before the 200 is returned (survives serverless freeze). 5-second timeout; errors never fail the tool response. |
| 69 | +- **10-minute TTL** — `validBefore = now + 600s`, `validAfter = 0`. |
| 70 | +- **Single signature** — the caller's original EIP-3009 auth is forwarded to the usage API as-is; the tool server never re-signs. |
0 commit comments