Chadjective/verdikt

GitHub: Chadjective/verdikt

基于 Solana Subscriptions & Allowances 原语实现的加密威胁情报按次/订阅计费系统,支持 AI agent 的预付费缓存检查与完成后付款的按需调查。

Stars: 0 | Forks: 0

# Verdikt **按次判决付费的加密货币威胁情报,基于 Solana 原生的 Subscriptions & Allowances 进行计费。** 一个付费墙,两款产品:**即时缓存判决**(预付费,x402 风格)或**全新定制的调查**(完成后付款)。专为 AI agents 和人类打造——链上结算,无需信用卡处理商,无需账户。 ## 理念 [Avoid.net](https://avoid.net) 是一个加密货币**威胁情报 API**:输入 token、合约或交易所,它会返回一个确定性的信任评分和判决(`avoid` / `caution` / `clear`)。浏览是免费的;**API 访问是收费的**——这正是 Solana 新的 [Subscriptions & Allowances](https://solana.com/news/subscriptions-and-allowances) 原语的用途。 一个付费墙背后,有两款产品: | 模式 | Endpoint | 结算方式 | Delegatee | 适用场景 | |------|----------|------------|-----------|-----| | **缓存判决** | `GET /api/check` | **预付费** (x402: `402 → 支付 → 重试`) | **agent** | 已调查过的实体;廉价 + 即时 | | **按需调查** | `GET /api/investigate` | **完成后付款**(仅在成功时扣费) | **Avoid.net** | 未知实体;溢价涵盖了计算成本 | | **订阅** | `GET /api/check/subscription` | 周期性**计划** | 商户拉取 | 人类/团队 API 层级;包含查询 + 调查 | 未知实体的情况最为有趣。Agent 不会遇到“无数据”的死胡同,而是**按需定制全新的调查**并支付计算费用——“按需获取情报,优先处理”。在生产环境中,这对应于 Avoid.net 现有的 `investigation_requests` 队列 + `avoid-investigator` agent;付费请求可插队优先处理。 ### 为什么这需要新的原语(而不是原生的 SPL `approve`) Agent 的运行使用了**来自单一 token 账户的两个实时委托**——`user → agent`(廉价检查)和 `user → Avoid.net`(调查),每个都有各自的额度限制,并独立执行。原生的 SPL `approve` 每个 token 账户只允许**一个** delegate;这正是 Subscriptions & Allowances 所消除的限制。 ## 实际效果展示(真实输出,mock 模式) 预算:检查**0.05**(5 次 @ 0.01),调查**0.20**(2 次 @ 0.10): ``` Agent 2ph76FjZLz528KfGyE3WSmZiwfr2pchfMmKZJwj5BeYo Budget owner (delegator): 4suGekXe6j36sqKTAXJsWhEHMFEQwCxVbmchDBTBeTkX check budget (user -> agent) : 0.05 investigation budget (user -> Avoid.net): 0.2 paid 0.01 [cache] [CLEAR] Acme Exchange - trust 88/100 (none) ... paid 0.01 [cache] [AVOID] DrainCoin (DRAIN) - trust 4/100 (critical) ... !! ABORTING interaction with "DrainCoin" paid 0.1 [investigation] [CLEAR] (fresh) PhantomYield - trust 93/100 (none) - Fresh investigation ... paid 0.01 [cache] [AVOID] GhostBridge - trust 23/100 (high) ... !! ABORTING interaction with "GhostBridge" paid 0.1 [investigation] [AVOID] (fresh) NovaSwap - trust 22/100 (high) - Fresh investigation ... !! ABORTING interaction with "NovaSwap" ZyptoVault: investigation budget spent paid 0.01 [cache] [AVOID] DrainCoin (DRAIN) - trust 4/100 (critical) ... Remaining -- checks: 0.01, investigations: 0 ``` Agent 廉价筛查了已知 token,为未知项**定制了全新的调查**,**中止了所有高风险交互**,并在调查预算用尽时**停止了定制**——两个上限都在链上强制执行,全程无需人工干预。之后对 `PhantomYield` 重新执行 `/api/check` 会返回廉价的 `402` 层级:调查结果现已被缓存。 ## 两个流程的结算方式 **缓存判决 —— 预付费 (x402):** ``` agent ─ GET /api/check ─▶ 402 + {price, payTo} agent ─ transferFixed (agent pulls from user→agent budget) ─▶ Solana ─▶ signature agent ─ GET /api/check (x-payment: sig) ─▶ verify ─▶ 200 + cached report ``` **按需调查 —— 完成后付款:** ``` agent ─ GET /api/investigate (x-budget: user) ─▶ server checks user→Avoid.net allowance ├─ insufficient ─▶ 402 (no compute, no charge) └─ ok ─▶ run investigation ├─ fails ─▶ 500 (no charge) └─ succeeds ─▶ transferFixed (Avoid.net pulls) ─▶ 200 + fresh report ``` 您只需为实际交付的计算付费。 ## 架构 ``` src/ avoid/threatApi.ts # the "product": lookup() (cache) + investigate() (on-demand). Pure, no Solana. config.ts # env, mode (mock|chain), prices, mint, keypair paths payments/ types.ts # PaymentBackend interface mock.ts # MockPayments — file-backed, mirrors on-chain semantics, no validator chain.ts # ChainPayments — real @solana/subscriptions calls index.ts # factory (PAYMENTS=mock|chain) solana/client.ts # @solana/kit client, keypair gen/load, plugins server/server.ts # /api/check (pre-pay) · /api/investigate (pay-on-completion) · /api/check/subscription agent/agent.ts # autonomous agent using both budgets scripts/ # setup, grant-allowance (2 delegations), create-plan, subscribe, charge-subscription ``` ### 演示操作 → 链上 instruction (`@solana/subscriptions`) | 操作 | Instruction(s) | |---|---| | 授予额度(×2 个 delegatees) | `initSubscriptionAuthority`(一次)+ `createFixedDelegation` ×2 | | 按次支付缓存检查 (agent) | `transferFixed`(由 agent 签名) | | 调查,完成后付款 (Avoid.net) | `transferFixed`(由商户在工作完成后签名) | | 读取剩余预算 | `fetchMaybeFixedDelegation` | | 创建计划 / 订阅 / 扣费 | `createPlan` / `subscribe` / `transferSubscription` | 程序:**`De1egAFMkMWZSN5rYXRj9CAdheBamobVNubTsi9avR44`**(devnet + mainnet-beta;自 2026-06-02 起上线,由 Moonsong Labs + Solana Foundation 开发;已经过 Cantina/Spearbit 审计)。 ## 快速开始(mock 模式 —— 无需 validator) Node ≥ 20。 ``` npm install cp .env.example .env # defaults to PAYMENTS=mock npm run setup # keypairs (user / agent / merchant) npm run allowance:grant # 2 delegations: user→agent (checks) + user→Avoid.net (investigations) npm run plan:create # publish a monthly plan npm run plan:subscribe # subscribe npm run server # terminal 1 npm run agent # terminal 2 — cached checks + on-demand investigations ``` 订阅路由: ``` npm run plan:charge curl "http://127.0.0.1:4021/api/check/subscription?entity=GhostBridge" \ -H "x-subscriber: " ``` `npm run typecheck` 会根据发布的类型验证每一个 SDK 调用;`npm test` 运行单元测试套件(31 个测试)。 ## 在真实的 devnet 上运行(chain 模式) ``` npm run setup # airdrops devnet SOL; prints spl-token commands # 创建一个原生 SPL 6位小数 token,为用户提供资金(参见 setup 输出),然后在 .env 中: # PAYMENTS=chain # RPC_URL=https://api.devnet.solana.com # TOKEN_MINT= npm run allowance:grant && npm run plan:create && npm run plan:subscribe npm run server # terminal 1 npm run agent # terminal 2 ``` 相同的脚本,真实的程序。**仅限原生 SPL**(该程序的 Token-2022 扩展处理方式有所不同——详见注意事项)。 ## Mock 模式与 chain 模式对比 - **Mock 模式**在 JSON 账本中重现了链上语义(每次拉取的上限、过期时间、单次周期扣费、防重放证明)——包含完整的金额/接收者/重放检查,可在任何地方运行。 - **Chain 模式**发出真实的指令。对于预付费,服务器会确认支付交易已确认 + 防重放;生产环境的网关还会通过 token-balance 差值来断言金额/mint/接收者(上限 + 固定价格已经限制了支出)。对于**完成后付款**,正确性是结构性的:Avoid.net 仅在工作成功*之后*才进行资金拉取。详见 [`chain.ts`](src/payments/chain.ts)。 ## 准确性与注意事项 - `@solana/subscriptions@0.3.0` 属于 **1.0 之前的版本**;版本已锁定。整个代码库均根据已发布的类型进行了类型检查。 - [`threatApi.ts`](src/avoid/threatApi.ts) 中的威胁数据是**合成数据**;`investigate()` 会合成一个确定性的判决来模拟计算。将此模块替换为真实的 Avoid.net pipeline 即可投入生产。 - x402 框架是我们自己的组合——Solana 的文档将 allowances 与 [x402](https://solana.com/x402) 搭配使用,但并未提供官方的 402-gate 代码。 - 根据 Solana Foundation 的公告“已在 mainnet 上线”;此演示针对的是 devnet。 ## 加拿大相关性 - **Verdikt / Avoid.net** 在加拿大构建(Colosseum Frontier 黑客松)。 - **Figment**(多伦多)——Solana 质押/基础设施——可以通过链上分层为节点和数据 API 计量计费,类似于 Helius。 - 能够受益于原生周期性 + 委托式、计量化消费的加拿大 Web2 企业: - **Shopify**(渥太华)——使用稳定币进行商户订阅 / 定期计费。 - **Nuvei** & **Lightspeed**(蒙特利尔)——提供链上订阅收费的支付 / 商业基础设施。 - **Wealthsimple**(多伦多)——将 allowances 作为限额的、可撤销的自动化预算,用于自动投资。 ## 迁移至 Avoid.net 将 [`src/avoid/threatApi.ts`](src/avoid/threatApi.ts) 替换为真实的 Avoid.net pipeline(`lookup()` → 读取缓存/DB,`investigate()` → 优先级的 `avoid-investigator` 运行),整个 `PaymentBackend`(allowance 检查、调查、订阅)即可直接平移到 Avoid.net 的付费 API 层。 ## 许可证 MIT。仅供演示/教育用途;威胁数据仅供参考。
标签:AI代理, MITM代理, Solana, 加密货币, 区块链, 威胁情报, 开发者工具, 支付API, 自动化攻击