x402-engineer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENT.md +102 -0
- package/README.md +43 -0
- package/dist/cli.cjs +137 -0
- package/package.json +51 -0
- package/skills/stellar-dev/SKILL.md +146 -0
- package/skills/stellar-dev/advanced-patterns.md +188 -0
- package/skills/stellar-dev/api-rpc-horizon.md +521 -0
- package/skills/stellar-dev/common-pitfalls.md +510 -0
- package/skills/stellar-dev/contracts-soroban.md +565 -0
- package/skills/stellar-dev/ecosystem.md +430 -0
- package/skills/stellar-dev/frontend-stellar-sdk.md +651 -0
- package/skills/stellar-dev/resources.md +306 -0
- package/skills/stellar-dev/security.md +491 -0
- package/skills/stellar-dev/standards-reference.md +94 -0
- package/skills/stellar-dev/stellar-assets.md +419 -0
- package/skills/stellar-dev/testing.md +786 -0
- package/skills/stellar-dev/zk-proofs.md +136 -0
- package/skills/x402-add-paywall/SKILL.md +208 -0
- package/skills/x402-add-paywall/references/patterns.md +132 -0
- package/skills/x402-debug/SKILL.md +92 -0
- package/skills/x402-debug/references/checklist.md +146 -0
- package/skills/x402-explain/SKILL.md +136 -0
- package/skills/x402-init/SKILL.md +129 -0
- package/skills/x402-init/templates/env-example.md +17 -0
- package/skills/x402-init/templates/express/config.ts.md +29 -0
- package/skills/x402-init/templates/express/server.ts.md +30 -0
- package/skills/x402-init/templates/fastify/adapter.ts.md +66 -0
- package/skills/x402-init/templates/fastify/config.ts.md +29 -0
- package/skills/x402-init/templates/fastify/server.ts.md +90 -0
- package/skills/x402-init/templates/hono/config.ts.md +29 -0
- package/skills/x402-init/templates/hono/server.ts.md +31 -0
- package/skills/x402-init/templates/next-app-router/config.ts.md +29 -0
- package/skills/x402-init/templates/next-app-router/server.ts.md +31 -0
- package/skills/x402-stellar/SKILL.md +139 -0
- package/skills/x402-stellar/references/api.md +237 -0
- package/skills/x402-stellar/references/patterns.md +276 -0
- package/skills/x402-stellar/references/setup.md +138 -0
- package/skills/x402-stellar/scripts/check-deps.js +218 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# Zero-Knowledge Proofs on Stellar (Status-Sensitive)
|
|
2
|
+
|
|
3
|
+
## When to use this guide
|
|
4
|
+
Use this guide when the user asks for:
|
|
5
|
+
- On-chain ZK proof verification patterns
|
|
6
|
+
- Privacy-preserving smart contract architecture
|
|
7
|
+
- BN254/Poseidon readiness planning
|
|
8
|
+
- Groth16 or PLONK integration strategy
|
|
9
|
+
- Cross-chain proof verification design
|
|
10
|
+
|
|
11
|
+
This guide is intentionally status-aware. ZK capabilities on Stellar evolve with protocol and SDK releases.
|
|
12
|
+
|
|
13
|
+
## Source-of-truth checks (required)
|
|
14
|
+
Before implementation, always verify:
|
|
15
|
+
1. CAP status in `stellar/stellar-protocol` (`Accepted`/`Implemented` vs draft/awaiting decision)
|
|
16
|
+
2. Target network protocol/software version
|
|
17
|
+
3. `soroban-sdk` support for required cryptographic host functions
|
|
18
|
+
4. Availability of production examples matching your proving system
|
|
19
|
+
|
|
20
|
+
Primary references:
|
|
21
|
+
- [CAP-0059](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0059.md) (BLS12-381)
|
|
22
|
+
- [CAP-0074](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0074.md) (BN254 proposal)
|
|
23
|
+
- [CAP-0075](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0075.md) (Poseidon/Poseidon2 proposal)
|
|
24
|
+
- [Stellar protocol/software versions](https://developers.stellar.org/docs/networks/software-versions)
|
|
25
|
+
- [RPC providers](https://developers.stellar.org/docs/data/apis/rpc/providers)
|
|
26
|
+
|
|
27
|
+
## Capability model
|
|
28
|
+
Treat advanced cryptography as capability-gated:
|
|
29
|
+
- Capability A: proof verification primitive support
|
|
30
|
+
- Capability B: hash primitive support
|
|
31
|
+
- Capability C: SDK ergonomics and bindings
|
|
32
|
+
- Capability D: operational cost envelope
|
|
33
|
+
|
|
34
|
+
Do not assume all capabilities are present on all networks/environments.
|
|
35
|
+
|
|
36
|
+
## Architecture patterns
|
|
37
|
+
|
|
38
|
+
### 1) Verification gateway
|
|
39
|
+
Use a dedicated verifier contract (or module) for cryptographic checks:
|
|
40
|
+
- Normalize and validate inputs
|
|
41
|
+
- Enforce domain separation for statements
|
|
42
|
+
- Verify proof
|
|
43
|
+
- Emit explicit success/failure events
|
|
44
|
+
|
|
45
|
+
Benefits:
|
|
46
|
+
- Smaller audit surface
|
|
47
|
+
- Easier upgrades/migrations
|
|
48
|
+
- Cleaner operational telemetry
|
|
49
|
+
|
|
50
|
+
### 2) Policy-and-proof split
|
|
51
|
+
Separate concerns:
|
|
52
|
+
- `Verifier`: cryptographic validity only
|
|
53
|
+
- `Policy`: business/risk/compliance logic
|
|
54
|
+
- `Application`: state transition after verifier + policy pass
|
|
55
|
+
|
|
56
|
+
Benefits:
|
|
57
|
+
- Better testability
|
|
58
|
+
- Safer upgrades
|
|
59
|
+
- Clearer incident response
|
|
60
|
+
|
|
61
|
+
### 3) Feature flags and graceful fallback
|
|
62
|
+
Gate advanced paths by environment support:
|
|
63
|
+
- Enable ZK flows only where required primitives are verified available
|
|
64
|
+
- Keep deterministic fallback behavior for unsupported environments
|
|
65
|
+
- Document supported network/protocol matrix in deployment notes
|
|
66
|
+
|
|
67
|
+
## Integration checklist
|
|
68
|
+
- [ ] Target network supports required primitives
|
|
69
|
+
- [ ] SDK pin supports required APIs
|
|
70
|
+
- [ ] Proof statement includes anti-replay binding (nonce/context)
|
|
71
|
+
- [ ] Full simulation path is covered (proof + policy + state transition)
|
|
72
|
+
- [ ] Negative-path tests exist for malformed/tampered inputs
|
|
73
|
+
- [ ] Resource budget checks are documented for realistic proof sizes
|
|
74
|
+
- [ ] Security review documents all cryptographic assumptions
|
|
75
|
+
|
|
76
|
+
## Common pitfalls
|
|
77
|
+
|
|
78
|
+
### Over-trusting proof payload shape
|
|
79
|
+
A payload that parses is not equivalent to a valid statement for your application.
|
|
80
|
+
|
|
81
|
+
Mitigation:
|
|
82
|
+
- Validate public-input semantics and statement domain explicitly.
|
|
83
|
+
|
|
84
|
+
### Missing anti-replay controls
|
|
85
|
+
Valid proofs can be replayed without context binding.
|
|
86
|
+
|
|
87
|
+
Mitigation:
|
|
88
|
+
- Bind proofs to session/nonce/action scope and persist replay guards.
|
|
89
|
+
|
|
90
|
+
### Monolithic contract design
|
|
91
|
+
Combining verifier, policy, and state logic increases audit complexity.
|
|
92
|
+
|
|
93
|
+
Mitigation:
|
|
94
|
+
- Keep verifier logic isolated and narrow.
|
|
95
|
+
|
|
96
|
+
### Hardcoded protocol assumptions
|
|
97
|
+
Assuming primitive availability across all networks causes runtime failures.
|
|
98
|
+
|
|
99
|
+
Mitigation:
|
|
100
|
+
- Capability-gate and verify at deployment time.
|
|
101
|
+
|
|
102
|
+
## Testing strategy
|
|
103
|
+
|
|
104
|
+
### Unit tests
|
|
105
|
+
- Input domain validation
|
|
106
|
+
- Replay protection behavior
|
|
107
|
+
- Event correctness
|
|
108
|
+
|
|
109
|
+
### Integration tests
|
|
110
|
+
- End-to-end proof submission flow
|
|
111
|
+
- Negative cases: tampered input, stale nonce, unsupported feature path
|
|
112
|
+
- Network-configuration differences (local/testnet/mainnet)
|
|
113
|
+
|
|
114
|
+
### Operational tests
|
|
115
|
+
- Cost/resource envelope under realistic proof sizes
|
|
116
|
+
- Load behavior on verifier hot paths
|
|
117
|
+
- Upgrade/migration safety tests for verifier changes
|
|
118
|
+
|
|
119
|
+
## Security review focus
|
|
120
|
+
- Authorization and anti-replay guarantees
|
|
121
|
+
- Statement domain separation
|
|
122
|
+
- Upgrade controls around verifier/policy modules
|
|
123
|
+
- Denial-of-service resistance and bounded workloads
|
|
124
|
+
- Event/log coverage for forensic traceability
|
|
125
|
+
|
|
126
|
+
## Example starting points
|
|
127
|
+
- [Soroban examples](https://github.com/stellar/soroban-examples)
|
|
128
|
+
- [Groth16 verifier example](https://github.com/stellar/soroban-examples/tree/main/groth16_verifier)
|
|
129
|
+
- [Security guide](security.md)
|
|
130
|
+
- [Advanced patterns](advanced-patterns.md)
|
|
131
|
+
- [Standards reference](standards-reference.md)
|
|
132
|
+
|
|
133
|
+
## What not to do
|
|
134
|
+
- Do not claim specific primitives are production-ready without checking CAP status and network support.
|
|
135
|
+
- Do not hardcode draft-spec behavior as guaranteed runtime behavior.
|
|
136
|
+
- Do not skip simulation and negative-path testing for verifier flows.
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: x402-add-paywall
|
|
3
|
+
description: Wrap an API endpoint with x402 payment protection. Detects endpoints, adds middleware wrapping, and updates route config.
|
|
4
|
+
user-invocable: true
|
|
5
|
+
disable-model-invocation: true
|
|
6
|
+
argument-hint: "[endpoint-path]"
|
|
7
|
+
allowed-tools: [Read, Write, Edit, Bash, Grep, Glob]
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# /x402:add-paywall
|
|
11
|
+
|
|
12
|
+
> Protect an API endpoint with x402 payment middleware. I'll detect your endpoints, let you choose which to protect, and handle the wrapping.
|
|
13
|
+
|
|
14
|
+
## Step 1 -- Verify x402 is initialized
|
|
15
|
+
|
|
16
|
+
Use Grep to check if `@x402/core` exists in `package.json` dependencies:
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
Grep: pattern="@x402/core" in package.json
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**If NOT found:** Output exactly:
|
|
23
|
+
|
|
24
|
+
> "No x402 configuration detected. Run /x402:init first."
|
|
25
|
+
|
|
26
|
+
Then STOP. Do not proceed.
|
|
27
|
+
|
|
28
|
+
**If found:** Continue. Use Glob and Grep to locate the project's x402 files:
|
|
29
|
+
|
|
30
|
+
1. Find the routesConfig file: Use Grep to search for files containing `RoutesConfig` or `routesConfig`:
|
|
31
|
+
```
|
|
32
|
+
Grep: pattern="routesConfig" glob="**/*.{ts,js}" (exclude node_modules)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
2. Find the server/middleware file: Use Grep to search for files importing `x402ResourceServer` or `paymentMiddleware` or `withPayment`:
|
|
36
|
+
```
|
|
37
|
+
Grep: pattern="withPayment|paymentMiddleware|x402ResourceServer" glob="**/*.{ts,js}" (exclude node_modules)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Store both file paths for later use.
|
|
41
|
+
|
|
42
|
+
## Step 2 -- Detect framework
|
|
43
|
+
|
|
44
|
+
Read `package.json` and check `dependencies` for the framework:
|
|
45
|
+
|
|
46
|
+
1. `"next"` in dependencies -> **Next.js App Router**
|
|
47
|
+
2. `"express"` in dependencies -> **Express**
|
|
48
|
+
3. `"fastify"` in dependencies -> **Fastify**
|
|
49
|
+
4. `"hono"` in dependencies -> **Hono**
|
|
50
|
+
|
|
51
|
+
Priority if multiple detected: Next.js > Express > Fastify > Hono.
|
|
52
|
+
|
|
53
|
+
If no supported framework found, output:
|
|
54
|
+
|
|
55
|
+
> "Could not detect framework from package.json. Supported: Next.js, Express, Fastify, Hono."
|
|
56
|
+
|
|
57
|
+
Then STOP.
|
|
58
|
+
|
|
59
|
+
## Step 3 -- Discover API endpoints
|
|
60
|
+
|
|
61
|
+
Read [references/patterns.md](references/patterns.md) for the framework-specific endpoint detection approach.
|
|
62
|
+
|
|
63
|
+
**Next.js App Router:**
|
|
64
|
+
- Use Glob to find `**/app/api/**/route.{ts,js}` files
|
|
65
|
+
- For each found file, Read it and extract the exported HTTP method handlers (look for `export async function GET`, `export async function POST`, `export const GET`, `export const POST`, etc. for GET, POST, PUT, PATCH, DELETE)
|
|
66
|
+
- Build a list of `"METHOD /path"` entries where the path is derived from the file's directory structure relative to `app/` (e.g., `app/api/content/route.ts` -> `/api/content`)
|
|
67
|
+
|
|
68
|
+
**Express:**
|
|
69
|
+
- Use Grep to find route registrations: `app.get(`, `app.post(`, `app.put(`, `app.patch(`, `app.delete(`, `router.get(`, `router.post(`, `router.put(`, `router.patch(`, `router.delete(`
|
|
70
|
+
- Extract the path argument (first string argument) from each match
|
|
71
|
+
- Build a list of `"METHOD /path"` entries
|
|
72
|
+
|
|
73
|
+
**Fastify:**
|
|
74
|
+
- Use Grep to find route registrations: `fastify.get(`, `fastify.post(`, `fastify.put(`, `fastify.patch(`, `fastify.delete(`, `fastify.route(`
|
|
75
|
+
- Extract the path argument from each match
|
|
76
|
+
- Build a list of `"METHOD /path"` entries
|
|
77
|
+
|
|
78
|
+
**Hono:**
|
|
79
|
+
- Use Grep to find route registrations: `app.get(`, `app.post(`, `app.put(`, `app.patch(`, `app.delete(`, `app.route(`
|
|
80
|
+
- Extract the path argument from each match
|
|
81
|
+
- Build a list of `"METHOD /path"` entries
|
|
82
|
+
|
|
83
|
+
**If no endpoints found:** Output exactly:
|
|
84
|
+
|
|
85
|
+
> "No API endpoints found in this project. Create an endpoint first, then run /x402:add-paywall."
|
|
86
|
+
|
|
87
|
+
Then STOP.
|
|
88
|
+
|
|
89
|
+
## Step 4 -- Check protection status
|
|
90
|
+
|
|
91
|
+
For each discovered endpoint, determine if it is already protected:
|
|
92
|
+
|
|
93
|
+
**Next.js (inline wrap):**
|
|
94
|
+
- Read the route file
|
|
95
|
+
- Check if it contains the `// x402: payment-protected endpoint` comment
|
|
96
|
+
- Check if it imports `withPayment` or `withX402`
|
|
97
|
+
- If either found: mark as **PROTECTED**
|
|
98
|
+
|
|
99
|
+
**Express / Fastify / Hono (middleware):**
|
|
100
|
+
- Read the routesConfig file found in Step 1
|
|
101
|
+
- Check if `"METHOD /path"` already exists as a key in routesConfig
|
|
102
|
+
- If found: mark as **PROTECTED** and note the current price and network
|
|
103
|
+
|
|
104
|
+
**All frameworks:**
|
|
105
|
+
- Always check routesConfig for an existing `"METHOD /path"` key regardless of framework
|
|
106
|
+
|
|
107
|
+
## Step 5 -- Present endpoint list
|
|
108
|
+
|
|
109
|
+
Output exactly (replacing `{N}` with the count):
|
|
110
|
+
|
|
111
|
+
> "Found {N} API endpoints. Select which to protect:"
|
|
112
|
+
|
|
113
|
+
Then display a numbered list showing protection status:
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
1. [PROTECTED] GET /api/content ($0.001 USDC)
|
|
117
|
+
2. [ ] POST /api/data
|
|
118
|
+
3. [ ] GET /api/users
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Protected endpoints show the current price. Unprotected endpoints have empty brackets.
|
|
122
|
+
|
|
123
|
+
**Argument handling:** If `$ARGUMENTS` provides an endpoint path (e.g., the user ran `/x402:add-paywall /api/data`), auto-select that endpoint instead of presenting the full list. If the argument matches multiple methods for the same path, select all of them.
|
|
124
|
+
|
|
125
|
+
**If ALL endpoints are already protected:** Output exactly:
|
|
126
|
+
|
|
127
|
+
> "All selected endpoints are already protected. No changes made."
|
|
128
|
+
|
|
129
|
+
Then STOP.
|
|
130
|
+
|
|
131
|
+
## Step 6 -- Wrap selected endpoint(s)
|
|
132
|
+
|
|
133
|
+
Read [references/patterns.md](references/patterns.md) for the framework-specific wrapping approach.
|
|
134
|
+
|
|
135
|
+
### Next.js App Router (inline wrap)
|
|
136
|
+
|
|
137
|
+
For each selected unprotected endpoint:
|
|
138
|
+
|
|
139
|
+
1. **Read the route file** -- always read before editing (never assume file state)
|
|
140
|
+
2. **Add import** -- if `withPayment` or `withX402` is not already imported, add the import:
|
|
141
|
+
```typescript
|
|
142
|
+
import { withPayment } from "{relative_path_to_server}";
|
|
143
|
+
```
|
|
144
|
+
Use the path to the x402 server file found in Step 1. Preserve existing imports -- do not duplicate.
|
|
145
|
+
3. **Add marker comment** -- add `// x402: payment-protected endpoint` above the handler function
|
|
146
|
+
4. **Wrap the handler** -- wrap the handler's body with `withPayment(req, () => { ... original handler ... })`:
|
|
147
|
+
```typescript
|
|
148
|
+
// x402: payment-protected endpoint
|
|
149
|
+
export async function GET(req: Request) {
|
|
150
|
+
return withPayment(req, () => {
|
|
151
|
+
// ... original handler body ...
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
5. **Use Edit tool** to apply changes to the route file
|
|
156
|
+
|
|
157
|
+
### Express / Fastify / Hono (middleware)
|
|
158
|
+
|
|
159
|
+
For middleware-based frameworks, no route file changes are needed -- the middleware intercepts all routes listed in routesConfig.
|
|
160
|
+
|
|
161
|
+
1. **Check middleware registration** -- Read the app setup file and verify middleware is registered:
|
|
162
|
+
- Express: `app.use(paymentMiddleware(...))` or `app.use(x402Middleware)`
|
|
163
|
+
- Fastify: `fastify.register(x402PaymentPlugin, ...)`
|
|
164
|
+
- Hono: `app.use(paymentMiddleware(...))` or `app.use(x402Middleware)`
|
|
165
|
+
2. **If middleware not registered** -- add it to the app setup file (check first for idempotency)
|
|
166
|
+
|
|
167
|
+
### All frameworks -- Add routesConfig entry
|
|
168
|
+
|
|
169
|
+
For EVERY selected unprotected endpoint, regardless of framework:
|
|
170
|
+
|
|
171
|
+
1. **Read the routesConfig file** found in Step 1
|
|
172
|
+
2. **Add a new entry** using the Edit tool:
|
|
173
|
+
```typescript
|
|
174
|
+
"METHOD /path": {
|
|
175
|
+
accepts: [
|
|
176
|
+
{
|
|
177
|
+
scheme: "exact",
|
|
178
|
+
price: "$0.001",
|
|
179
|
+
network: NETWORK,
|
|
180
|
+
payTo: SERVER_ADDRESS,
|
|
181
|
+
},
|
|
182
|
+
],
|
|
183
|
+
description: "Description of endpoint",
|
|
184
|
+
mimeType: "application/json",
|
|
185
|
+
},
|
|
186
|
+
```
|
|
187
|
+
3. **Price default:** Use `"$0.001"` (dollar-sign prefix required) unless the user specifies a different price
|
|
188
|
+
4. **Key format:** `"METHOD /path"` (e.g., `"GET /api/content"`, `"POST /api/data"`)
|
|
189
|
+
|
|
190
|
+
## Step 7 -- Confirm
|
|
191
|
+
|
|
192
|
+
For each endpoint that was successfully protected, output exactly (replacing `{path}` with the endpoint path):
|
|
193
|
+
|
|
194
|
+
> "Protected {path} with x402 paywall. Price: $0.001 USDC per request."
|
|
195
|
+
|
|
196
|
+
If an endpoint was already protected and skipped, output exactly:
|
|
197
|
+
|
|
198
|
+
> "Endpoint {path} is already protected (price: {price} {asset}, network: {network}). No changes made."
|
|
199
|
+
|
|
200
|
+
## Important Rules
|
|
201
|
+
|
|
202
|
+
- **Idempotency is critical.** Always check for existing protection before making changes. Running this command twice on the same endpoint must produce no changes the second time.
|
|
203
|
+
- **Always read before editing.** Use the Read tool on every file before modifying it. Never assume file contents.
|
|
204
|
+
- **Preserve existing code.** When wrapping Next.js handlers, keep all existing imports, types, and logic intact. Only add the `withPayment` wrapper and marker comment.
|
|
205
|
+
- **For Express/Hono/Fastify:** Only modify routesConfig to add the route entry. Do NOT modify individual route handler files -- the middleware handles interception automatically.
|
|
206
|
+
- **Price format:** Always use dollar-prefixed strings (e.g., `"$0.001"`, not `"0.001"`).
|
|
207
|
+
- **routesConfig key format:** Always use `"METHOD /path"` (e.g., `"GET /api/content"`).
|
|
208
|
+
- **Marker comment:** Use exactly `// x402: payment-protected endpoint` -- this is the idempotency detection marker for Next.js routes.
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# x402 Endpoint Wrapping Patterns
|
|
2
|
+
|
|
3
|
+
Reference for framework-specific x402 payment protection patterns. Read this before modifying any route files or middleware configuration.
|
|
4
|
+
|
|
5
|
+
## Wrapping Strategy by Framework
|
|
6
|
+
|
|
7
|
+
### Next.js App Router -- Inline Wrap
|
|
8
|
+
|
|
9
|
+
Next.js route handlers must be wrapped individually because there is no global middleware interception point in App Router.
|
|
10
|
+
|
|
11
|
+
**Pattern:** Import `withX402` (or `withPayment`) and wrap the exported handler function.
|
|
12
|
+
|
|
13
|
+
**Using @x402/next (official):**
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { withX402 } from "../lib/x402/server";
|
|
17
|
+
import { routesConfig, resourceServer } from "../lib/x402/server";
|
|
18
|
+
|
|
19
|
+
// x402: payment-protected endpoint
|
|
20
|
+
export const GET = withX402(
|
|
21
|
+
async (req: Request) => {
|
|
22
|
+
return Response.json({ data: "protected content" });
|
|
23
|
+
},
|
|
24
|
+
routesConfig,
|
|
25
|
+
resourceServer,
|
|
26
|
+
);
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Using custom withPayment (demo pattern):**
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { withPayment } from "@/lib/x402/server";
|
|
33
|
+
|
|
34
|
+
// x402: payment-protected endpoint
|
|
35
|
+
export async function GET(req: Request) {
|
|
36
|
+
return withPayment(req, () => {
|
|
37
|
+
return Response.json({ data: "protected content" });
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**Endpoint detection:** Scan `app/api/` (or `src/app/api/`) recursively for `route.ts` or `route.js` files. Each file may export GET, POST, PUT, PATCH, DELETE handlers.
|
|
43
|
+
|
|
44
|
+
### Express -- Middleware Registration
|
|
45
|
+
|
|
46
|
+
Express uses global middleware. No changes to route handler files needed.
|
|
47
|
+
|
|
48
|
+
**Pattern:** The `paymentMiddleware` from `@x402/express` intercepts all routes defined in `routesConfig`.
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
// In app setup (e.g., server.ts or app.ts)
|
|
52
|
+
import { paymentMiddleware } from "@x402/express";
|
|
53
|
+
import { routesConfig, resourceServer } from "./lib/x402/server";
|
|
54
|
+
|
|
55
|
+
app.use(paymentMiddleware(routesConfig, resourceServer));
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**Endpoint detection:** Grep for `app.get(`, `app.post(`, `app.put(`, `app.patch(`, `app.delete(`, `router.get(`, `router.post(`, `router.put(`, `router.patch(`, `router.delete(` etc.
|
|
59
|
+
|
|
60
|
+
### Fastify -- Plugin Registration
|
|
61
|
+
|
|
62
|
+
Fastify uses a plugin. No changes to route handler files needed.
|
|
63
|
+
|
|
64
|
+
**Pattern:** The custom `x402PaymentPlugin` intercepts all routes defined in `routesConfig`. Since `@x402/fastify` is not published on npm, use `@x402/core` directly with a custom `FastifyAdapter`.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
// In app setup (e.g., server.ts or app.ts)
|
|
68
|
+
import { x402PaymentPlugin } from "./lib/x402/server";
|
|
69
|
+
|
|
70
|
+
fastify.register(x402PaymentPlugin, { routesConfig, resourceServer });
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**Endpoint detection:** Grep for `fastify.get(`, `fastify.post(`, `fastify.put(`, `fastify.patch(`, `fastify.delete(`, `fastify.route(` etc.
|
|
74
|
+
|
|
75
|
+
### Hono -- Middleware Registration
|
|
76
|
+
|
|
77
|
+
Hono uses global middleware. No changes to route handler files needed.
|
|
78
|
+
|
|
79
|
+
**Pattern:** The `paymentMiddleware` from `@x402/hono` intercepts all routes defined in `routesConfig`.
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
// In app setup (e.g., index.ts or app.ts)
|
|
83
|
+
import { paymentMiddleware } from "@x402/hono";
|
|
84
|
+
import { routesConfig, resourceServer } from "./lib/x402/server";
|
|
85
|
+
|
|
86
|
+
app.use(paymentMiddleware(routesConfig, resourceServer));
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**Endpoint detection:** Grep for `app.get(`, `app.post(`, `app.put(`, `app.patch(`, `app.delete(`, `app.route(` etc.
|
|
90
|
+
|
|
91
|
+
## routesConfig Entry Format
|
|
92
|
+
|
|
93
|
+
Every protected endpoint needs an entry in routesConfig:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
"METHOD /path": {
|
|
97
|
+
accepts: [
|
|
98
|
+
{
|
|
99
|
+
scheme: "exact",
|
|
100
|
+
price: "$0.001",
|
|
101
|
+
network: "stellar:testnet",
|
|
102
|
+
payTo: SERVER_ADDRESS,
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
description: "Description of what this endpoint does",
|
|
106
|
+
mimeType: "application/json",
|
|
107
|
+
},
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**Key details:**
|
|
111
|
+
- The key format is `"METHOD /path"` (e.g., `"GET /api/content"`, `"POST /api/data"`)
|
|
112
|
+
- `price` must include the dollar-sign prefix (e.g., `"$0.001"`, not `"0.001"`)
|
|
113
|
+
- `network` is `"stellar:testnet"` for development, `"stellar:mainnet"` for production
|
|
114
|
+
- `payTo` is the Stellar public key (G...) that receives USDC payments
|
|
115
|
+
- `scheme` is `"exact"` for fixed-price payments
|
|
116
|
+
|
|
117
|
+
## Idempotency Detection
|
|
118
|
+
|
|
119
|
+
Before making any changes, check if an endpoint is already protected.
|
|
120
|
+
|
|
121
|
+
**For Next.js (inline wrap):**
|
|
122
|
+
- Check if route file contains `// x402: payment-protected endpoint` comment
|
|
123
|
+
- Check if route file imports `withPayment` or `withX402`
|
|
124
|
+
- If either found: endpoint is already protected -- do not wrap again
|
|
125
|
+
|
|
126
|
+
**For Express/Fastify/Hono (middleware):**
|
|
127
|
+
- Check if routesConfig already contains the `"METHOD /path"` entry
|
|
128
|
+
- If found: endpoint is already protected -- do not add duplicate entry
|
|
129
|
+
|
|
130
|
+
**For all frameworks:**
|
|
131
|
+
- Check routesConfig for existing `"METHOD /path"` key before adding a new entry
|
|
132
|
+
- If the key exists, report the current price and network, skip with no changes
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: x402-debug
|
|
3
|
+
description: Diagnose x402 payment configuration issues. Runs static analysis checks on environment, dependencies, code structure, and framework setup.
|
|
4
|
+
user-invocable: true
|
|
5
|
+
disable-model-invocation: true
|
|
6
|
+
allowed-tools: [Read, Grep, Glob, Bash]
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# /x402:debug
|
|
10
|
+
|
|
11
|
+
> Run a comprehensive diagnostic check on your x402 payment configuration. I'll check everything: env vars, dependencies, code structure, and framework setup.
|
|
12
|
+
|
|
13
|
+
## Instructions
|
|
14
|
+
|
|
15
|
+
1. **Read the full checklist** from [references/checklist.md](references/checklist.md) to get all checks with their PASS/FAIL/WARN conditions and fix instructions.
|
|
16
|
+
|
|
17
|
+
2. **Run EVERY check** in the checklist. No arguments, no scope narrowing, full checklist every time. Do not skip checks.
|
|
18
|
+
|
|
19
|
+
3. **Static analysis only:** Use Read, Grep, and Glob to inspect files. Do NOT make network calls (no curl, no fetch, no API requests). Do NOT use Write or Edit -- this command is read-only.
|
|
20
|
+
|
|
21
|
+
4. **Early exit -- no x402 setup detected:** Before running the checklist, check if `@x402/core` exists in `package.json` dependencies. If NOT found, output exactly:
|
|
22
|
+
|
|
23
|
+
"No x402 configuration detected. Run /x402:init first."
|
|
24
|
+
|
|
25
|
+
Then STOP. Do not run remaining checks.
|
|
26
|
+
|
|
27
|
+
5. **Environment variable handling:**
|
|
28
|
+
- Read `.env.local` first. If `.env.local` doesn't exist, fall back to `.env`.
|
|
29
|
+
- If neither `.env.local` nor `.env` exists, report all env var checks as FAIL with fix instruction to create `.env.local`.
|
|
30
|
+
- Do NOT output env var values (security). Only report whether each variable is set/not set and whether its format is valid.
|
|
31
|
+
|
|
32
|
+
6. **Framework detection order:**
|
|
33
|
+
- Detect framework FIRST (Category 4), then use detected framework for framework-specific checks in other categories.
|
|
34
|
+
- Detection priority when multiple frameworks found: Next.js > Express > Fastify > Hono.
|
|
35
|
+
|
|
36
|
+
## Output Format
|
|
37
|
+
|
|
38
|
+
Group results by category with markdown headers:
|
|
39
|
+
|
|
40
|
+
### Environment Variables
|
|
41
|
+
|
|
42
|
+
Report each env var check result on its own line.
|
|
43
|
+
|
|
44
|
+
### Dependencies
|
|
45
|
+
|
|
46
|
+
Report each dependency check result on its own line.
|
|
47
|
+
|
|
48
|
+
### Code Structure
|
|
49
|
+
|
|
50
|
+
Report each code structure check result on its own line.
|
|
51
|
+
|
|
52
|
+
### Framework Detection
|
|
53
|
+
|
|
54
|
+
Report each framework check result on its own line.
|
|
55
|
+
|
|
56
|
+
**Formatting rules:**
|
|
57
|
+
|
|
58
|
+
- Each check result on its own line using `[PASS]`, `[FAIL]`, or `[WARN]` prefix
|
|
59
|
+
- FAIL items include `Fix:` on the next line (indented with two spaces) with the exact instruction from the checklist
|
|
60
|
+
- Order within each category: FAIL items first, then WARN, then PASS
|
|
61
|
+
- End with the summary line from the checklist (pass/fail/warning counts or "All checks passed. x402 is ready.")
|
|
62
|
+
|
|
63
|
+
## Example Output
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
### Environment Variables
|
|
67
|
+
[FAIL] Environment variable FACILITATOR_API_KEY: Not set
|
|
68
|
+
Fix: Add FACILITATOR_API_KEY to .env.local (see .env.example). Get a key at: https://channels.openzeppelin.com/testnet/gen
|
|
69
|
+
[PASS] SERVER_STELLAR_ADDRESS
|
|
70
|
+
[PASS] FACILITATOR_URL
|
|
71
|
+
|
|
72
|
+
### Dependencies
|
|
73
|
+
[PASS] @x402/core: Installed
|
|
74
|
+
[PASS] @x402/stellar: Installed
|
|
75
|
+
[PASS] Framework adapter: @x402/next installed
|
|
76
|
+
[PASS] @x402/core version: 2.9.0
|
|
77
|
+
|
|
78
|
+
### Code Structure
|
|
79
|
+
[WARN] No protected endpoints in routesConfig
|
|
80
|
+
Fix: Run /x402:add-paywall to protect an endpoint
|
|
81
|
+
[PASS] Route config: lib/x402/config.ts
|
|
82
|
+
[PASS] Server middleware: lib/x402/server.ts
|
|
83
|
+
[PASS] ExactStellarScheme: Correctly imported from server path
|
|
84
|
+
[PASS] Price format: All prices use $ prefix
|
|
85
|
+
|
|
86
|
+
### Framework Detection
|
|
87
|
+
[PASS] Framework detected: Next.js
|
|
88
|
+
[PASS] Adapter matches framework: Next.js
|
|
89
|
+
|
|
90
|
+
### Summary
|
|
91
|
+
10 passed, 1 failed, 1 warning. Fix the FAIL items above to complete your x402 setup.
|
|
92
|
+
```
|