Skip to content

docs: facilitator deployment + SDK x402 integration#98

Open
jhfnetboy wants to merge 3 commits intomainfrom
docs/facilitator-and-sdk-integration
Open

docs: facilitator deployment + SDK x402 integration#98
jhfnetboy wants to merge 3 commits intomainfrom
docs/facilitator-and-sdk-integration

Conversation

@jhfnetboy
Copy link
Copy Markdown
Member

Summary

为 SuperPaymaster x402 体系新增两份运营级文档,独立于 Stage 1 P0 修复 PR:

  • docs/deployment/facilitator-deployment.md (694 行) —— x402 facilitator 服务(packages/x402-facilitator-node/)的端到端部署指南
  • docs/integration/sdk-x402-integration.md (583 行) —— aastar-sdk 的 @aastar/x402 / @aastar/channel / @aastar/cli 的集成手册 + Sepolia 联调清单

两份文档由并行 agent 写成,并对实际仓库状态做了验证(不是凭印象写)。

验证摘要

facilitator-deployment.md

  • pnpm install / typecheck / build 全过
  • node dist/index.js 可启动;//.well-known/x-payment-info 200
  • ⚠️ /health 在公共 Sepolia RPC 上 503(不是服务 bug,公共 RPC 已停服 —— 已写入 §3.2 推荐 Alchemy)
  • ⚠️ pre-existing bug 发现middleware/hmac-challenge.ts:102 只在 status === 402 注入 X-Challenge,但 facilitator 没路径返回 402 → settle 永远拿不到 challenge。已写入文档 §8.4 + §11.2,标为生产前必修(不在本 PR 范围)
  • ⚠️ docker build 未执行(审计机器无容器运行时),已标为 open verification

sdk-x402-integration.md

  • @aastar/x402 37/37 vitest 通过
  • @aastar/channel 11/11 通过
  • @aastar/cli 1/1 通过
  • ⚠️ packages/x402-facilitator-node 没有任何测试,已写入 SDK TODO
  • ⚠️ ABI 同步 gap:P0-13(commit a90c4ac,已在 fix/p0-wave2-funds-price)给 SuperPaymaster 加了 x402NonceKey,但 SDK ABI 未同步。P0-13 合入 main 后,SDK 现有的 x402SettlementNonces({nonce}) 单参数查询会永远返回 false(旧 key 没人写)—— 必须配合更新
  • ⚠️ Schema 不一致:facilitator-node 用 {payment, scheme} 平面字段;SDK FacilitatorClient 发 x402 v2 spec {x402Version, paymentPayload, paymentRequirements}。当前 SDK 不能直接驱动我们自家 facilitator,需要 facilitator 升级到 v2 spec 或 SDK 提供专用 client
  • ⚠️ HMAC 实际是 hono middleware 而不是独立的 /verify/settle round trip,文档已按源码改正

后续动作(不在本 PR 范围)

依赖项:

  • Wave 2 P0-12a/12b(fix/p0-wave2-funds-price):xPNTs approvedFacilitators + isXPNTs 白名单 → facilitator 链上注册路径才完整可用
  • Wave 2 P0-13(已修):合入 main 后,必须重新生成 ABI 并更新 SDK actions
  • Spec 对齐:facilitator-node 升级到 x402 v2 spec,或 SDK 加专用 v1 client
  • HMAC 修复hmac-challenge.ts:102 注入条件
  • e2e 测试基建:anvil 上跑 SP + facilitator + SDK 全链路

关联

  • Audit 基线:docs/security/2026-04-26-p0-prelaunch.md(在 security/audit-2026-04-25 分支)
  • D4 决策:docs/security/2026-04-26-decision-records.md(社区指定 facilitator + xPNTs only)
  • 威胁模型:docs/security/2026-04-26-threat-model.md(T-05 x402 drain)
  • Stage 1 P0 PR(独立审核):
    • fix/p0-wave1-consensus(P0-2/4/17 已完成,P0-1/3 进行中)
    • fix/p0-wave2-funds-price(P0-13/16 已完成,P0-9/10/11/12/14 进行中)
    • fix/p0-wave3-tokens-v4(P0-5/6/7 已完成,P0-8/15 进行中)

Test plan

  • 文档可独立 review
  • facilitator 服务可本地启动(命令在文档 §10)
  • SDK 单元测试通过(命令在文档 §9)
  • SDK 团队按文档 §7(联调清单)在 Sepolia 上跑全流程(需要 Stage 1 P0 合入 main 后)
  • facilitator 上 Cloudflare 反代 + DNS(按 §5)—— Stage 1 完成后

Adds two operations docs alongside the audit/P0 work, written and
verified by parallel research agents against the actual repo state.

## docs/deployment/facilitator-deployment.md (694 lines)

End-to-end deployment guide for the x402 facilitator service in
`packages/x402-facilitator-node/`. Covers RPC selection, env config,
Docker / systemd / managed-platform options, DNS + TLS, on-chain
whitelist registration (deferred to P0-12b), HMAC challenge usage,
health monitoring, and emergency rollback.

Verified locally:
- pnpm install / typecheck / build all pass
- node dist/index.js boots; well-known and root endpoints respond
- /health returns 503 on a public Sepolia RPC that no longer accepts
  requests (recommend Alchemy in production)
- Found pre-existing bug in middleware/hmac-challenge.ts: hmacChallengeInjector
  only injects X-Challenge on status === 402, but the facilitator never
  returns 402, so clients can never obtain a challenge. Documented as a
  pre-launch fix item; not in scope for this docs PR.

Docker build was not exercised because no container runtime is
available on the audit machine; documented as an open verification.

## docs/integration/sdk-x402-integration.md (583 lines)

Integration manual for `@aastar/x402`, `@aastar/channel`,
`@aastar/cli`, and `@aastar/core`'s x402 actions, plus how all of
them sit on top of the facilitator service and SuperPaymaster's
on-chain settle paths. Includes three integration scenarios (server
SDK, wallet auto-settle, agent micropayments), full EIP-3009 typed
data, HMAC client implementation, and a Sepolia integration checklist.

Verified by reading SDK source at /Users/jason/Dev/aastar/aastar-sdk:
- @aastar/x402: 37/37 vitest pass
- @aastar/channel: 11/11 pass
- @aastar/cli: 1/1 pass
- packages/x402-facilitator-node has no tests (recommended in TODO)

Notable findings flagged in the doc:
- ABI sync gap: P0-13 (a90c4ac in fix/p0-wave2-funds-price) added
  x402NonceKey to SuperPaymaster but ABI is not yet synced to
  aastar-sdk; SDK still queries x402SettlementNonces with single-key,
  which will silently return false after P0-13 lands on main.
- Schema mismatch: facilitator-node uses {payment, scheme} flat
  fields; SDK FacilitatorClient sends x402 v2 spec
  {x402Version, paymentPayload, paymentRequirements}. Need spec
  alignment before SDK can drive our own facilitator end-to-end.
- HMAC is implemented as a hono middleware pair, not a separate
  /verify-then-/settle round trip; doc reflects actual behavior.

## Cross-references

- Audit baseline: docs/security/2026-04-26-p0-prelaunch.md (audit branch)
- Decision history: docs/security/2026-04-26-decision-records.md (D4)
- Threat model: docs/security/2026-04-26-threat-model.md
- Pending P0 dependencies (Wave 2): P0-12a / P0-12b for on-chain
  approvedFacilitators whitelist + isXPNTs check; P0-13 (already on
  fix/p0-wave2-funds-price) for nonce key change

## Why a separate PR

- Docs are independent of any single P0 fix; reviewable on their own
- Stage 1 P0 PRs stay focused on contract changes + tests
- SDK team can begin integration work against this doc in parallel
  with Wave 2 contract reviews
@jhfnetboy jhfnetboy requested a review from fanhousanbu as a code owner April 28, 2026 07:05
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ff6101b9fb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +216 to +217
pnpm install --prod
pnpm build
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Install full dependencies before invoking build

The systemd setup installs with pnpm install --prod and immediately runs pnpm build; in this package, build is tsc, which is only in devDependencies. On a clean server this sequence fails because the TypeScript compiler is not installed, so Option B deployment cannot complete as written.

Useful? React with 👍 / 👎.

Comment on lines +342 to +344
const trigger = await fetch('https://facilitator.example.com/some-paid-endpoint');
const challenge = trigger.headers.get('X-Challenge');
if (!challenge) throw new Error('Facilitator did not issue challenge');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Get HMAC challenge from a real 402 source

This snippet fetches https://facilitator.example.com/some-paid-endpoint to obtain X-Challenge, but facilitator-node does not expose a paid endpoint that returns 402, so this call returns 404/no challenge and the subsequent HMAC /settle flow cannot proceed. The challenge needs to come from an endpoint that actually emits a 402 response (or a dedicated challenge route).

Useful? React with 👍 / 👎.

@fanhousanbu
Copy link
Copy Markdown
Contributor

代码审查

警告 — 需修复后合并

必须修复(合并前)

H1 — 文档中硬编码了开发者本地绝对路径
sdk-x402-integration.md §7(联调清单)和 §11(关键路径速查)中包含 /Users/jason/Dev/aastar/... 等绝对路径,对其他开发者无效,且暴露了内部开发机用户名。应全部替换为仓库相对路径(./scripts/...)或环境变量占位符($AASTAR_SDK_DIR)。

H2 — facilitator-deployment.md §8.2 流程描述与 §8.4 已知 bug 互相矛盾
§8.2 描述 client 通过 POST /verify 获取 X-Challenge(200 OK),而 §8.4 明确指出中间件只在 status === 402 时注入 X-Challenge/verify 返回 200 时 client 实际拿不到 challenge。两个子节描述了互相矛盾的流程,会误导实现者。应将 §8.2 直接改为当前带 bug 的实际行为,不要先描述理想流程再在下面纠正。


建议修复

M1 — facilitator-node ↔ SDK schema 不兼容优先级标注不足
§9.4 的表格精确列出了两端 schema 差异(SDK 的 FacilitatorClient 无法直接对接 SuperPaymaster facilitator-node),但 §8 TODO 表将其标为 P1。当前 SDK 根本无法完成真实集成,应升级为 P0。

M2 — verify.ts 在 P0-13 部署后 nonce 查询失效
§9.3 提到 verify.ts 仍使用单参数 nonce 查询,P0-13 合并后这个查询将永远返回 false。但 §11.2 的 TODO 列表没有把这条服务端 bug 的修复单独列出。需在 facilitator-node TODO 中补充:verify.ts 的 nonce 查询改为 x402NonceKey(asset, from, nonce) 三元组哈希。

L1 — docker-compose.yml 示例中的废弃 version 字段
Docker Compose v2 已废弃顶层 version: "3.9" 字段,建议移除以避免 deprecation 警告。

…tator docs

H1: Replace all /Users/jason/Dev/aastar/... absolute paths in
sdk-x402-integration.md and facilitator-deployment.md with environment
variable placeholders ($SUPERPAYMASTER_ROOT, $AASTAR_SDK_ROOT) so the
docs are portable across developer machines.

H2: Resolve contradiction between facilitator-deployment.md §8.2 and §8.4.
Add a prominent warning block in §8.2 explaining that hmacChallengeInjector
only fires on HTTP 402 responses while /verify returns 200/400, so
X-Challenge is never injected in practice. The client flow steps are
updated to reflect this current behavior while preserving the design intent.

Closes #98
@jhfnetboy
Copy link
Copy Markdown
Member Author

修复说明

本次 commit 修复了两类问题:

H1:硬编码绝对路径

sdk-x402-integration.mdfacilitator-deployment.md 中所有 /Users/jason/Dev/aastar/... 路径已替换为环境变量占位符:

  • /Users/jason/Dev/aastar/SuperPaymaster/...$SUPERPAYMASTER_ROOT/...
  • /Users/jason/Dev/aastar/aastar-sdk/...$AASTAR_SDK_ROOT/...
  • find /Users/jason/Dev -maxdepth 5 ...find $HOME/Dev -maxdepth 5 ...
  • §10 验证记录 中的工作目录路径也已替换

文档顶部建议开发者在环境中设置这两个变量(e.g. export SUPERPAYMASTER_ROOT=~/Dev/aastar/SuperPaymaster)。

H2:§8.2 与 §8.4 HMAC 流程矛盾

facilitator-deployment.md §8.2 描述了 /verify 返回 X-Challenge header 的客户端流程,而 §8.4 指出 hmacChallengeInjector 实际只在 status=402 时注入,导致 /verify(返回 200/400)从不注入该 header,两者相互矛盾。

修复:在 §8.2 顶部添加醒目的注意块,明确说明当前实现行为与设计意图的差异,并在步骤 1 注释中注明"当前实现:X-Challenge 不注入"。§8.4 已知 Bug 保持不变,两者现在描述一致。

@jhfnetboy
Copy link
Copy Markdown
Member Author

已处理剩余三条 reviewer 建议:

M1 — P1 → P0(schema 不兼容)
sdk-x402-integration.md §8 TODO 表 中的「facilitator-node schema 与 x402 v2 spec 对齐」已升级为 P0,并补充说明「当前状态下任何端到端 x402 流程均无法工作」,确保接入方不会误判优先级。

M2 — verify.ts nonce 查询需改为三元组哈希
facilitator-deployment.md §11.2 的 facilitator 服务 TODO 表中新增一条 bug(P0-13 后必修):P0-13 合并后 x402SettlementNonces key 已变为三元组哈希,verify.ts 中的单参数查询将永远返回 false,导致已结算 tuple 可被重放。明确指出修复方向:改用合约 x402NonceKey(asset, from, nonce) helper。

L1 — 移除废弃的 docker-compose version 字段
facilitator-deployment.md §4.1.2 示例中的 version: "3.9" 已移除,替换为注释说明(Docker Compose v2 已废弃该字段)。

请 re-review,谢谢 🙏

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.

2 participants