Skip to content

feat(x402): payment middleware pipeline and two-phase approval flow#87

Open
zhongeric wants to merge 1 commit intoConway-Research:mainfrom
zhongeric:feat/x402-payment-middleware
Open

feat(x402): payment middleware pipeline and two-phase approval flow#87
zhongeric wants to merge 1 commit intoConway-Research:mainfrom
zhongeric:feat/x402-payment-middleware

Conversation

@zhongeric
Copy link

Summary

  • Two-phase x402_fetch tool: Phase 1 discovers payment details (amount, recipient, network, wallet balance) without signing. Phase 2 (approve_payment: true) runs the middleware chain, signs the USDC payment, and logs the transaction — giving the agent a reasoning turn before committing funds.
  • Extensible middleware pipeline: PaymentMiddleware[] runs before every signing attempt. Phase 1 uses a blocking middleware to force the early return. Phase 2 runs with an empty chain, open for future additions (balance checks, rate limits, allowlists) without changing x402Fetch.
  • Transaction logging: Successful x402 payments are recorded as x402_payment transactions with amount and description, following the existing transfer_credits pattern.

Changes

File What changed
src/types.ts Added "x402_payment" to TransactionType union
src/conway/x402.ts Exported PaymentRequirement/X402PaymentResult; added PaymentContext, PaymentMiddleware, X402FetchOptions types; refactored x402Fetch with middleware pipeline and backward-compatible signature
src/agent/tools.ts Rewrote x402_fetch tool with approve_payment parameter, two-phase handler, and transaction logging
src/__tests__/x402.test.ts 11 new tests: middleware unit tests, tool-level Phase 1/Phase 2 tests, sequential integration test
README.md Added x402 Payments section documenting the two-phase flow

Test plan

  • pnpm build — clean compilation
  • pnpm test — 21/21 tests pass (11 new x402 tests + 10 existing)
  • Manual: call x402_fetch against an x402-gated URL, confirm Phase 1 returns payment details without signing
  • Manual: call x402_fetch with approve_payment: true, confirm payment is signed and response is returned
  • Verify non-402 URLs still return responses directly

🤖 Generated with Claude Code

x402Fetch previously auto-signed any 402 payment without agent evaluation.
This introduces a middleware pipeline and a two-phase tool flow so the agent
reasons about each payment before committing funds:

- Phase 1 (discover): x402_fetch returns payment details without signing
- Phase 2 (pay): x402_fetch with approve_payment: true runs middleware, signs, and logs the transaction

Also adds PaymentMiddleware/PaymentContext types, x402_payment transaction type,
11 new tests, and README documentation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant