@keelcodes/x402-gate 0.1.2 → 0.1.3
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/README.md +8 -0
- package/dist/index.js +14 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -151,6 +151,14 @@ npx tsx scripts/permit2-budget.ts --chain bsc --budget 5 --broadcast # 真广
|
|
|
151
151
|
npx tsx scripts/permit2-budget.ts --chain bsc --revoke --broadcast # 撤销
|
|
152
152
|
```
|
|
153
153
|
|
|
154
|
+
### per-key / per-agent 预算(可选身份头)
|
|
155
|
+
|
|
156
|
+
网关会把调用方的 `x-keel-key-id` / `x-keel-agent-id` 转发给支付引擎(裁剪 256 字符),
|
|
157
|
+
引擎据此把消费记到 `key:<sha256 前 16 位>` 或 `agent:<小写 id>` 主体;两者都缺省时退回按付款方地址。
|
|
158
|
+
运营可用 `GET/PUT /v1/payment/spend-policies[/:subject]` 为这些主体配置日限额、单次上限与收款方白名单。
|
|
159
|
+
|
|
160
|
+
> 这两个头只应由**受信服务端**设置(例如你们自己的 API 网关),不要直接透传外部用户输入,否则调用方可伪造主体。
|
|
161
|
+
|
|
154
162
|
## 状态码
|
|
155
163
|
|
|
156
164
|
| 场景 | 响应 |
|
package/dist/index.js
CHANGED
|
@@ -209,6 +209,19 @@ function hasPlatformCredential(req, extra) {
|
|
|
209
209
|
return typeof v === 'string' && v.trim().length > 0;
|
|
210
210
|
});
|
|
211
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* B5.1:透传调用方身份头,供支付引擎按 `key:<hash>` / `agent:<id>` 做预算主体。
|
|
214
|
+
* 只转发这两个命名空间内的头,并裁剪长度。
|
|
215
|
+
*/
|
|
216
|
+
function identityHeaders(req) {
|
|
217
|
+
const out = {};
|
|
218
|
+
for (const name of ['x-keel-key-id', 'x-keel-agent-id']) {
|
|
219
|
+
const value = (req.header(name) ?? '').trim();
|
|
220
|
+
if (value)
|
|
221
|
+
out[name] = value.slice(0, 256);
|
|
222
|
+
}
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
212
225
|
/**
|
|
213
226
|
* 本次调用的价格(最小单位)。
|
|
214
227
|
* 优先级:`amountFor(path, req)` > 顶层 `amount` > 第一个带价格的 accept。
|
|
@@ -263,6 +276,7 @@ async function authorizeX402(opts, req) {
|
|
|
263
276
|
headers: {
|
|
264
277
|
'Content-Type': 'application/json',
|
|
265
278
|
...(opts.paymentsKey ? { Authorization: `Bearer ${opts.paymentsKey}` } : {}),
|
|
279
|
+
...identityHeaders(req),
|
|
266
280
|
},
|
|
267
281
|
// 转发完整挑战:引擎按载荷在 `accepts[]` 中匹配(EIP-3009 / Permit2)。
|
|
268
282
|
body: JSON.stringify({ paymentPayload: signature, paymentRequirements: challenge }),
|
package/package.json
CHANGED