x402-safe-pay 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +86 -3
- package/index.mjs +95 -0
- package/package.json +13 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 P0
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,86 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# x402-safe-pay
|
|
2
|
+
|
|
3
|
+
Check a payee before your AI agent pays it.
|
|
4
|
+
|
|
5
|
+
Agents that pay x402 or MPP endpoints send money to addresses they have never seen. `x402-safe-pay` wraps the paying `fetch` your agent already uses. Before the first payment to an address it asks a payee check:
|
|
6
|
+
|
|
7
|
+
- is the address on the OFAC SDN, UK or EU sanctions lists (wallet entries);
|
|
8
|
+
- what is the rule-based risk of the wallet (EVM);
|
|
9
|
+
- is it a known x402 seller (seen as `payTo` in a seller's own 402 answer);
|
|
10
|
+
- does the endpoint really ask for payment to that same address.
|
|
11
|
+
|
|
12
|
+
If the answer is `do_not_pay`, the payment is not made and a `PayeeRefusedError` is thrown. Verdicts are cached per address (1 hour by default).
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install x402-safe-pay
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Use
|
|
21
|
+
|
|
22
|
+
`withPayeeCheck(paidFetch, options)` takes the paying fetch your x402 or MPP client gives you and returns a fetch with the same signature.
|
|
23
|
+
|
|
24
|
+
### x402-fetch (x402 v1)
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
import { createWalletClient, http } from "viem";
|
|
28
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
29
|
+
import { base } from "viem/chains";
|
|
30
|
+
import { wrapFetchWithPayment } from "x402-fetch";
|
|
31
|
+
import { withPayeeCheck } from "x402-safe-pay";
|
|
32
|
+
|
|
33
|
+
const client = createWalletClient({ account: privateKeyToAccount(process.env.PRIVATE_KEY), transport: http(), chain: base });
|
|
34
|
+
const safeFetch = withPayeeCheck(wrapFetchWithPayment(fetch, client));
|
|
35
|
+
|
|
36
|
+
const res = await safeFetch("https://api.example.com/v1/quote?symbol=ETH");
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### @x402/fetch (x402 v2)
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
import { wrapFetchWithPaymentFromConfig } from "@x402/fetch";
|
|
43
|
+
import { ExactEvmScheme } from "@x402/evm";
|
|
44
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
45
|
+
import { withPayeeCheck } from "x402-safe-pay";
|
|
46
|
+
|
|
47
|
+
const paidFetch = wrapFetchWithPaymentFromConfig(fetch, {
|
|
48
|
+
schemes: [{ network: "eip155:8453", client: new ExactEvmScheme(privateKeyToAccount(process.env.PRIVATE_KEY)) }],
|
|
49
|
+
});
|
|
50
|
+
const safeFetch = withPayeeCheck(paidFetch);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### mppx (MPP)
|
|
54
|
+
|
|
55
|
+
`Mppx.create` makes the global `fetch` pay 402 answers. Keep the original fetch first and pass it as `plainFetch`, so the probe that reads the 402 does not pay:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
import { Mppx, tempo } from "mppx/client";
|
|
59
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
60
|
+
import { withPayeeCheck } from "x402-safe-pay";
|
|
61
|
+
|
|
62
|
+
const plainFetch = globalThis.fetch; // before Mppx.create
|
|
63
|
+
Mppx.create({ methods: [tempo({ account: privateKeyToAccount(process.env.PRIVATE_KEY) })] });
|
|
64
|
+
const safeFetch = withPayeeCheck(globalThis.fetch, { plainFetch });
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Options:
|
|
68
|
+
|
|
69
|
+
| Option | Default | Meaning |
|
|
70
|
+
|---|---|---|
|
|
71
|
+
| `plainFetch` | `globalThis.fetch` | Unpaid fetch used to read the target's 402 (must not pay: see the mppx example) |
|
|
72
|
+
| `checkUrl` | P0 `/check/payee` | Payee check endpoint |
|
|
73
|
+
| `cacheMs` | `3600000` | How long a verdict is reused for the same address |
|
|
74
|
+
| `blockOnReview` | `false` | Also refuse addresses marked `review` |
|
|
75
|
+
| `onVerdict` | none | `(address, verdict, fullAnswer) => {}` after each check |
|
|
76
|
+
|
|
77
|
+
## Cost
|
|
78
|
+
|
|
79
|
+
The check is itself a paid x402 call: **$0.005** per new payee address (USDC on Base and other networks the check accepts), paid by the same `paidFetch`. No account, no API key. Price and terms: https://x402-bazaar-rank.x402-bazaar-rank-worker.workers.dev/tools
|
|
80
|
+
|
|
81
|
+
## Limits
|
|
82
|
+
|
|
83
|
+
- The request body must be a string or bytes: the wrapper sends the request twice (an unpaid read of the 402, then the paid call).
|
|
84
|
+
- The check states facts from its sources with their dates; a `no_flags_found` answer is not a guarantee.
|
|
85
|
+
|
|
86
|
+
MIT licence.
|
package/index.mjs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// x402-safe-pay: check a payee before your agent pays it.
|
|
2
|
+
//
|
|
3
|
+
// Wrap the paying fetch your agent already uses (for example the one from x402-fetch / @x402/fetch / mppx). Before the first payment to an
|
|
4
|
+
// address, the wrapper reads the 402 of the target, asks P0's payee check about the address it would pay (sanctions lists, wallet risk,
|
|
5
|
+
// whether it is a known x402 seller, whether the endpoint really asks for payment to that address) and only then lets your fetch pay.
|
|
6
|
+
// The check itself is a paid x402 call ($0.005, paid by the same fetch); its verdict is cached per address.
|
|
7
|
+
//
|
|
8
|
+
// MIT licence. Source and terms: https://x402-bazaar-rank.x402-bazaar-rank-worker.workers.dev/tools
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_CHECK_URL = "https://x402-bazaar-rank.x402-bazaar-rank-worker.workers.dev/check/payee";
|
|
11
|
+
export const VERSION = "0.1.1";
|
|
12
|
+
|
|
13
|
+
export class PayeeRefusedError extends Error {
|
|
14
|
+
constructor(message, details) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "PayeeRefusedError";
|
|
17
|
+
this.details = details;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function b64json(v) {
|
|
22
|
+
try {
|
|
23
|
+
const s = typeof atob === "function" ? atob(v) : Buffer.from(v, "base64").toString("utf8");
|
|
24
|
+
return JSON.parse(s);
|
|
25
|
+
} catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Addresses a 402 response asks to be paid: x402 v2 PAYMENT-REQUIRED header, x402 v1 body accepts[], MPP WWW-Authenticate request. */
|
|
31
|
+
export async function payeesOf(res) {
|
|
32
|
+
const out = new Set();
|
|
33
|
+
const add = (a) => { if (typeof a === "string" && a.trim()) out.add(a.trim()); };
|
|
34
|
+
const pr = res.headers.get("payment-required");
|
|
35
|
+
if (pr) { const j = b64json(pr); for (const a of (j && j.accepts) || []) add(a.payTo || a.pay_to); }
|
|
36
|
+
const wa = res.headers.get("www-authenticate") || "";
|
|
37
|
+
for (const m of wa.matchAll(/request="([A-Za-z0-9_-]+)"/g)) {
|
|
38
|
+
const t = m[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
39
|
+
const j = b64json(t + "=".repeat((4 - (t.length % 4)) % 4));
|
|
40
|
+
if (j) add(j.recipient);
|
|
41
|
+
}
|
|
42
|
+
if (!out.size) {
|
|
43
|
+
try { const j = await res.clone().json(); for (const a of (j && j.accepts) || []) add(a.payTo || a.pay_to); } catch { /* not JSON */ }
|
|
44
|
+
}
|
|
45
|
+
return [...out];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Wrap a paying fetch with a payee check.
|
|
50
|
+
* @param {Function} paidFetch - a fetch that pays 402 answers (x402 / MPP client)
|
|
51
|
+
* @param {object} [opts]
|
|
52
|
+
* @param {Function} [opts.plainFetch] - an unpaid fetch used to read the target's 402 (default: globalThis.fetch)
|
|
53
|
+
* @param {string} [opts.checkUrl] - payee check endpoint (default: P0)
|
|
54
|
+
* @param {number} [opts.cacheMs] - how long a verdict is reused for the same address (default: 1 hour)
|
|
55
|
+
* @param {boolean} [opts.blockOnReview] - refuse addresses the check marks "review" too (default: false, only "do_not_pay" is refused)
|
|
56
|
+
* @param {Function} [opts.onVerdict] - called with (address, verdict) after each check
|
|
57
|
+
*/
|
|
58
|
+
export function withPayeeCheck(paidFetch, opts = {}) {
|
|
59
|
+
if (typeof paidFetch !== "function") throw new TypeError("withPayeeCheck: paidFetch must be a function");
|
|
60
|
+
const plainFetch = opts.plainFetch || globalThis.fetch;
|
|
61
|
+
const checkUrl = opts.checkUrl || DEFAULT_CHECK_URL;
|
|
62
|
+
const cacheMs = Number.isFinite(opts.cacheMs) ? opts.cacheMs : 3600000;
|
|
63
|
+
const cache = new Map(); // address -> { at, verdict }
|
|
64
|
+
|
|
65
|
+
async function check(address, target) {
|
|
66
|
+
const key = address.toLowerCase();
|
|
67
|
+
const hit = cache.get(key);
|
|
68
|
+
if (hit && Date.now() - hit.at < cacheMs) return hit.verdict;
|
|
69
|
+
const u = checkUrl + "?address=" + encodeURIComponent(address) + "&url=" + encodeURIComponent(target);
|
|
70
|
+
const r = await paidFetch(u, { method: "GET", headers: { accept: "application/json" } });
|
|
71
|
+
if (!r.ok) throw new PayeeRefusedError("payee check unavailable (HTTP " + r.status + "); payment not made", { address, status: r.status });
|
|
72
|
+
const body = await r.json();
|
|
73
|
+
const verdict = (body && body.verdict) || {};
|
|
74
|
+
cache.set(key, { at: Date.now(), verdict });
|
|
75
|
+
if (typeof opts.onVerdict === "function") opts.onVerdict(address, verdict, body);
|
|
76
|
+
return verdict;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return async function safeFetch(input, init = {}) {
|
|
80
|
+
const target = typeof input === "string" ? input : input.url;
|
|
81
|
+
if (init.body && typeof init.body !== "string" && !(init.body instanceof ArrayBuffer) && !ArrayBuffer.isView(init.body))
|
|
82
|
+
throw new TypeError("x402-safe-pay: pass the request body as a string or bytes, so it can be sent twice (probe, then paid call)");
|
|
83
|
+
const probe = await plainFetch(input, init);
|
|
84
|
+
if (probe.status !== 402) return probe; // nothing to pay
|
|
85
|
+
for (const address of await payeesOf(probe)) {
|
|
86
|
+
const v = await check(address, target);
|
|
87
|
+
const rec = v.recommendation;
|
|
88
|
+
if (rec === "do_not_pay" || (rec === "review" && opts.blockOnReview))
|
|
89
|
+
throw new PayeeRefusedError("payee " + address + " refused by the payee check: " + rec, { address, verdict: v });
|
|
90
|
+
}
|
|
91
|
+
return paidFetch(input, init);
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export default withPayeeCheck;
|
package/package.json
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "x402-safe-pay",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "x402-safe-pay",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Check a payee before your AI agent pays it: wraps an x402/MPP paying fetch with a sanctions, wallet-risk and known-seller check on every new payee address.",
|
|
5
|
+
"keywords": ["x402", "mpp", "ai-agents", "agent-payments", "payee-check", "sanctions", "ofac", "wallet-risk", "fetch", "usdc", "base"],
|
|
6
|
+
"homepage": "https://x402-bazaar-rank.x402-bazaar-rank-worker.workers.dev/tools",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "index.mjs",
|
|
10
|
+
"exports": { ".": "./index.mjs" },
|
|
11
|
+
"files": ["index.mjs", "README.md", "LICENSE"],
|
|
12
|
+
"engines": { "node": ">=18" }
|
|
13
|
+
}
|