feat(#312): skills marketplace x402 micro-payment flow#352
Open
Peolite001 wants to merge 9 commits into
Open
Conversation
- Add x402 payment middleware for skill invocation - Implement full flow: 402 capture → quote → settle → retry with proof - Add invocation ledger to track all skill payments - Add POST /api/marketplace/skills/:skillId/invoke endpoint - Support mock mode for local development - Add comprehensive unit tests (11+ assertions) Closes Bitcoindefi#312
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.




Summary
Implements the x402 micro-payment flow for the skills marketplace. When an agent invokes a skill from another agent, the x402 HTTP payment protocol handles the micro-payment automatically: captures 402 responses, settles payment on-chain, retries with proof, and logs every
Related Issue
Closes #312
Changes
Added lib/marketplace/x402-middleware.ts — Payment middleware:
attemptSkillInvocation() — HTTP call to skill callUrl
invokeSkillWithPayment() — full flow: attempt → 402 → quote → settle → retry with X-Payment header
settleSkillPayment() — wraps x402 settlement (mock-aware)
Added lib/marketplace/invocation-ledger.ts — Payment ledger:
Records every invocation: { agentId, skillId, amountXLM, txHash, invokedAt, status }
Query by agent, skill, or limit; calculate total spent per agent
50k entry cap
Added app/api/marketplace/skills/[skillId]/invoke/route.ts — POST endpoint:
Body: { agentId, payload }
Validates skill exists, invokes with payment flow, records to ledger
Returns 402 with { error: "insufficient_balance" } when payment fails
Added tests/lib/marketplace/x402-middleware.test.ts — 5 integration tests
Added tests/lib/marketplace/invocation-ledger.test.ts — 6 unit tests
API Reference
Invoke a skill
POST /api/marketplace/skills/{skillId}/invoke
Content-Type: application/json
{
"agentId": "agent-1",
"payload": { "input": "hello" }
}
Response 200 (payment succeeded + skill executed)
{
"ok": true,
"response": { "result": "skill executed" },
"receipt": {
"txHash": "mock_tx_...",
"chain": "stellar",
"amountUsd": 0.25
},
"ledgerId": "inv_abc123"
}
Response 402 (insufficient balance)
{
"ok": false,
"error": "insufficient_balance",
"ledgerId": "inv_def456"
}
Testing
[x] Unit tests pass (npm test — 11 assertions, all green)
[x] Type check passes (npx tsc --noEmit)
Test coverage highlights
handles 402 → payment → retry → 200 flowrecords invocation in ledger after successful paymentmock x402 flow → invocation logged with correct txHashreturns insufficient_balance when payment settlement failsreturns non-402 response directly without paymentfilters by skillId and limits results,calculates total spent by agentArchitecture Notes
Uses existing lib/protocols/x402.ts quote/settle functions — no duplication
Mock mode aware: falls back to settleMockX402() when NEXT_PUBLIC_MOCK_MODE=true
Skill registry is currently a mock map pending PR#304; marked with TODO for easy integration
The X-Payment header carries the payment proof as JSON for the skill endpoint to verify