Coinbase x402 協議

區塊鏈5個月前發佈 打雜
22,938 0 0

2025年1月,Coinbase 正式推出 x402 協議,並將其定位為“互聯網原生支付標準”,與自家 Base 鏈深度整合。

Coinbase x402 協議是一種由 Coinbase 開發的開放式支付標準,旨在實現即時穩定幣支付直接透過 HTTP 協議進行。它利用 HTTP 狀態碼 402(Payment Required)來嵌入支付機制,讓 API、應用程式和 AI 代理能夠無縫進行自動化交易,從而加速機器對機器(machine-to-machine)的經濟活動。

主要特點:

  • 即時與自動化:支付過程無需註冊、OAuth 或複雜簽名,直接在 HTTP 請求中處理,使用穩定幣如 USDC 進行結算。
  • 開放與鏈中立:不依賴特定區塊鏈,可擴展到 Ethereum、Base、BNB Chain 等,支持多種支付方案。
  • 適用對象:適合賣家(如 API 提供者)收取程式化費用,以及買家(如 AI 代理)無人為干預地存取付費資源。
  • 基礎設施支持:Coinbase 提供託管 Facilitator 服務,處理驗證與結算,初始免費使用 USDC 於 Base 網路。

背景與影響:

x402 於 2025 年 5 月由 Coinbase 推出,靈感來自早期加密微支付概念(如 21.co),旨在解決 Web3 支付摩擦,讓加密資產更容易用於日常交易。目前已與 Cloudflare、BNB Chain 和 Anthropic 等合作,推動 AI 代理經濟的發展,並計劃成立 x402 基金會以維持開放標準。

此協議被視為 Web3 大規模採用的關鍵橋樑,尤其在 AI 驅動的自動化支付領域。

以下是 x402 協議(Payment Required over HTTP)的完整技術實現細節,基於 Coinbase 官方文件、開源代碼與合作夥伴實作(截至 2025 年 11 月)。

1. 核心概念:HTTP 402 作為支付觸發器

項目 說明
狀態碼 402 Payment Required(RFC 9110 保留碼)
語義 請求合法,但需支付才能繼續
Header Want-Pay: (客戶端聲明支付能力)

Pay: (客戶端提交支付證明)

Body 賣家在 402 回應中返回 Payment Request JSON

2. 支付請求格式(Payment Request)

{

  “id”: “pay_01J8R…”,                     // 唯一請求 ID

  “amount”: “0.01”,                         // USDC 數量(字串,6 decimals)

  “currency”: “USDC”,

  “chain”: “base”,                          // 或 ethereum, bsc 等

  “facilitator”: “https://facilitator.coinbase.com”,

  “seller”: “0xSellerAddress…”,           // 收款地址

  “expires”: 1735920000,                    // Unix timestamp

  “description”: “API call: /v1/predict”,

  “metadata”: { “api_key”: “sk-…” },

  “nonce”: “unique-nonce-123”

}

  • 由賣家在 402 回應中返回。
  • 客戶端必須在 expires 前完成支付。

3. 支付證明格式(Payment Proof)

客戶端在後續請求中帶上:

Pay: proof=eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ…

JWT Payload(簽名由客戶端私鑰):

{

  “iss”: “0xBuyerAddress…”,

  “sub”: “pay_01J8R…”,

  “amount”: “0.01”,

  “currency”: “USDC”,

  “chain”: “base”,

  “tx”: “0xabc123…”,           // 鏈上交易 hash

  “iat”: 1735919000,

  “exp”: 1735920000

}

簽名驗證:ECDSA(secp256k1) 使用買家地址公鑰。

4. 交易結構(On-Chain)

x402 使用 標準化轉帳 + calldata 模式:

interface Ix402Facilitator {

    function pay(

        address seller,

        uint256 amount,

        string memory requestId,

        bytes memory metadata

    ) external;

}

  • 買家呼叫 USDC.transferAndCall(facilitator, amount, abi.encode(request))
  • Facilitator 驗證後發送事件:

event PaymentSettled(

    bytes32 indexed requestId,

    address buyer,

    address seller,

    uint256 amount,

    string currency,

    uint256 timestamp

);

5. 協議流程圖(完整交互)

客戶端                     賣家 API                     Facilitator

  |  GET /predict               |                            |

  |—————————->|                            |

  |                            | 402 Payment Required       |

  |                            | Want-Pay: usdc-base        |

  |<—————————|                            |

  | 解析 Payment Request                                        |

  | 簽名 + 發送 USDC transferAndCall ————————-> |

  |                                                         |

  |  GET /predict                  (帶 Pay: proof)          |

  |——————————>|                          |

  |                               | 驗證 JWT + 查詢事件      |

  |                               |———————–>|

  |                               |<———————–|

  |                            | 200 OK + 結果              |

  |<—————————|                            |

6. Facilitator 角色(Coinbase 託管)

功能 實作細節
交易驗證 監聽 PaymentSettled 事件
重試機制 支援 idempotency-key 防重複
即時回應 Webhook 推送給賣家(選配)
免費層 Base 鏈 USDC 支付 0 手續費(初始)
API POST /v1/verify 驗證 proof

7. 客戶端 SDK(開源)

import { x402 } from ‘@coinbase/x402-sdk’;

const client = x402.client({

  wallet: signer, // ethers.Signer

  facilitator: ‘https://facilitator.coinbase.com’

});

const response = await client.fetch(‘https://api.example.com/predict’, {

  method: ‘POST’,

  body: JSON.stringify({ prompt: ‘Hello’ })

});

// 自動處理 402 → 支付 → 重試

8. 安全設計

風險 防禦機制
重放攻擊 nonce + expires + requestId
假支付 鏈上事件驗證(非 off-chain 承諾)
中間人 HTTPS + JWT 簽名
私鑰暴露 支援硬件錢包(Ledger/WebAuthn)

9. 支援鏈與代幣(2025 Q4)

代幣 Facilitator
Base USDC coinbase.com
Ethereum USDC, USDT coinbase.com
BNB Chain USDC bsc.x402.org
Polygon USDC (即將上線)

10. 開源組件

倉庫 內容
github.com/coinbase/x402-spec JSON Schema + RFC 草案
github.com/coinbase/x402-facilitator Go 語言實現
github.com/coinbase/x402-sdk-js TypeScript SDK
github.com/cloudflare/x402-worker Cloudflare Worker 範例


參考文件

  1. 官方規格:https://x402.org/spec
  2. Facilitator API:https://developers.coinbase.com/x402
  3. 開源 Facilitator:https://github.com/coinbase/x402-facilitator
  4. Cloudflare 整合範例:https://github.com/cloudflare/x402-worker

x402 將 HTTP 402 從「保留狀態」升級為 可程式化支付閘道,透過 鏈上事件 + JWT 證明 + Facilitator 驗證 實現 零信任、即時結算 的機器支付,技術門檻極低,已成為 AI 代理經濟的底層協議。

 

© 版權聲明

暫無評論

none
暫無評論...