x402-safe-fetch 0.8.0 → 0.9.0
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 +22 -1
- package/index.d.ts +2 -0
- package/index.js +42 -7
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -45,7 +45,7 @@ try {
|
|
|
45
45
|
|
|
46
46
|
Solana works the same way: `network: "solana"` and register an `ExactSvmScheme` from `@x402/svm/exact/client` with a `@solana/kit` signer.
|
|
47
47
|
|
|
48
|
-
Algorand and the XRP Ledger work the same way, and only their dollar stablecoin is ever paid. On Algorand that is USDC (ASA 31566704), never ALGO or another ASA. On the XRP Ledger it is RLUSD from Ripple's issuer, never XRP or a look-alike token. RLUSD prices are decimal dollars, so the budget is checked in dollars. The Doctor preflight is paid on the same network.
|
|
48
|
+
Algorand and the XRP Ledger work the same way, and only their dollar stablecoin is ever paid. On Algorand that is USDC (ASA 31566704), never ALGO or another ASA. Sellers name Algorand mainnet either by the whole genesis hash or by its CAIP-2 id (`algorand:wGHE2Pwdvd7S12BL5FaOP20EGYesN73k`); both are paid, also with `@x402/avm` 2.29.0, which on its own accepts only the short id. On the XRP Ledger it is RLUSD from Ripple's issuer, never XRP or a look-alike token. RLUSD prices are decimal dollars, so the budget is checked in dollars. The Doctor preflight is paid on the same network.
|
|
49
49
|
|
|
50
50
|
```js
|
|
51
51
|
import { toClientAvmSigner } from "@x402/avm";
|
|
@@ -65,6 +65,27 @@ const safeFetch = createSafeFetch({
|
|
|
65
65
|
// facilitator (PayAI) pays the gas.
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
+
### Escrowed payments (auth-capture)
|
|
69
|
+
|
|
70
|
+
Some sellers on Base take payment through Coinbase's escrow instead of a direct transfer: the x402 scheme
|
|
71
|
+
`auth-capture`. Your wallet signs once, the USDC is held, and the seller captures it only after it has delivered
|
|
72
|
+
(Atom's domain checkout, for instance, captures once the domain is registered, and releases it if registration
|
|
73
|
+
fails). Register that scheme next to the exact one to pay such sellers (`@x402/evm` 2.27 or later):
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
import { AuthCaptureEvmScheme } from "@x402/evm/auth-capture/client";
|
|
77
|
+
|
|
78
|
+
const safeFetch = createSafeFetch({
|
|
79
|
+
register: (client) => {
|
|
80
|
+
client.register("eip155:8453", new ExactEvmScheme(account));
|
|
81
|
+
client.register("eip155:8453", new AuthCaptureEvmScheme(account));
|
|
82
|
+
},
|
|
83
|
+
maxUsd: 25, // the budget caps the most that can be captured
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
When a seller offers both, the exact option is paid. The preflight and the budget apply the same way.
|
|
88
|
+
|
|
68
89
|
## Options
|
|
69
90
|
|
|
70
91
|
| Option | Default | |
|
package/index.d.ts
CHANGED
|
@@ -16,6 +16,8 @@ export declare const XLAYER: "eip155:196";
|
|
|
16
16
|
/** The option paid on `network`: on Algorand only USDC, on Stellar only Circle's USDC contract within maxUsd, on X Layer only USD₮0 or USDC within maxUsd, on the XRP Ledger only RLUSD from Ripple's issuer within maxUsd (in dollars); null when there is none. */
|
|
17
17
|
/** What a 402 offers (per x402 option: scheme, network, asset, amount, payTo) as a stable string; null without a readable x402 challenge. Consumes the body. */
|
|
18
18
|
export declare function offerFingerprint(res: Response): Promise<string | null>;
|
|
19
|
+
/** Wraps an Algorand scheme (e.g. @x402/avm's ExactAvmScheme) so it pays sellers that name Algorand by the whole genesis hash as well as by the CAIP-2 id. createSafeFetch does this for you. */
|
|
20
|
+
export declare function anyAlgorandId<S extends object>(scheme: S): S;
|
|
19
21
|
export declare function payableOption<A extends { network: string; asset?: unknown; amount?: unknown; extra?: Record<string, unknown> }>(accepts: A[], network: string, maxUsd: number | string): A | null;
|
|
20
22
|
|
|
21
23
|
/** The x402 Doctor preflight: see https://x402-doctor.fizzl.eu/openapi.json */
|
package/index.js
CHANGED
|
@@ -34,7 +34,7 @@ export { verifyReceipt, recoverSigner, canonicalJson, inputHash, certMessage, DO
|
|
|
34
34
|
export const DOCTOR_URL = "https://x402-doctor.fizzl.eu";
|
|
35
35
|
export const PREFLIGHT_CAP = "$0.002"; // the preflight costs $0.001; never more than twice that
|
|
36
36
|
export const DIAGNOSE_CAP = "$0.02"; // the diagnosis costs $0.01; never more than twice that
|
|
37
|
-
export const VERSION = "0.
|
|
37
|
+
export const VERSION = "0.9.0";
|
|
38
38
|
// Doctor sees which calls come from this package (in its usage counts), nothing more.
|
|
39
39
|
const DOCTOR_HEADERS = { accept: "application/json", "user-agent": `x402-safe-fetch/${VERSION}` };
|
|
40
40
|
export const BASE = "eip155:8453";
|
|
@@ -50,7 +50,7 @@ const NETWORK_ALIASES = { base: BASE, solana: SOLANA, algorand: ALGORAND, "algor
|
|
|
50
50
|
|
|
51
51
|
// On Algorand and the XRP Ledger only the dollar stablecoin is paid: USDC (an ASA) on Algorand, RLUSD from
|
|
52
52
|
// Ripple's issuer on the XRP Ledger. Never ALGO, XRP or another token, so the dollar budget always holds.
|
|
53
|
-
const ALGORAND_USDC = { [ALGORAND]: "31566704", [ALGORAND_TESTNET]: "10458941" };
|
|
53
|
+
const ALGORAND_USDC = { [algorandId(ALGORAND)]: "31566704", [algorandId(ALGORAND_TESTNET)]: "10458941" };
|
|
54
54
|
const RLUSD_HEX = "524C555344000000000000000000000000000000";
|
|
55
55
|
const RLUSD_ISSUER = { [XRPL]: "rMxCKbEDwqr76QuheSUMdEGf4B9xJ8m5De", [XRPL_TESTNET]: "rQhWct2fv4Vc4KRjRgMrxa8xPN9Zx9iLKV" };
|
|
56
56
|
// On Stellar only Circle's USDC token contract (7 decimals), never XLM or another token named USDC.
|
|
@@ -62,6 +62,23 @@ const XLAYER_DOLLARS = {
|
|
|
62
62
|
[XLAYER]: ["0x779ded0c9e1022225f8e0630b35a9b54be713736", "0x74b7f16337b8972027f6196a17a631ac6de26d22"],
|
|
63
63
|
};
|
|
64
64
|
|
|
65
|
+
// Algorand by its CAIP-2 id: the first 32 characters of the genesis hash. Sellers name it either way (the whole hash,
|
|
66
|
+
// or since @x402/avm 2.29.0 only the short id), so networks are compared by this.
|
|
67
|
+
function algorandId(network) {
|
|
68
|
+
return typeof network === "string" && network.startsWith("algorand:") ? network.slice(0, 41) : network;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// @x402/avm 2.29.0's ExactAvmScheme refuses the whole-hash form, which most Algorand sellers still use (2.28.0 took
|
|
72
|
+
// both). This hands the scheme the short id; the payment still names the seller's own network (x402 puts the
|
|
73
|
+
// seller's option in `accepted`), so either kind of seller can be paid. createSafeFetch wraps Algorand schemes in it.
|
|
74
|
+
export function anyAlgorandId(scheme) {
|
|
75
|
+
return Object.assign(Object.create(scheme), {
|
|
76
|
+
createPaymentPayload: (version, requirements, context) =>
|
|
77
|
+
scheme.createPaymentPayload(version, { ...requirements, network: algorandId(requirements.network) }, context),
|
|
78
|
+
...(typeof scheme.findDefaultAsset === "function" && { findDefaultAsset: (asset, network) => scheme.findDefaultAsset(asset, algorandId(network)) }),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
65
82
|
// The option to pay on `network`, or null. RLUSD prices are decimal dollars ("0.001", and "2" is two dollars),
|
|
66
83
|
// which x402's spend controls would read as base units, so the budget is checked here in dollars.
|
|
67
84
|
// What a 402 offers, as a stable string: per x402 option its scheme, network, asset, amount and payout address
|
|
@@ -85,8 +102,9 @@ const UNREAD_OFFER_MS = 10 * 60 * 1000;
|
|
|
85
102
|
|
|
86
103
|
export function payableOption(accepts, network, maxUsd) {
|
|
87
104
|
const cap = Number(String(maxUsd).replace(/^\$/, ""));
|
|
88
|
-
const on = (accepts ?? []).filter((a) => a?.network === network);
|
|
89
|
-
|
|
105
|
+
const on = (accepts ?? []).filter((a) => algorandId(a?.network) === algorandId(network));
|
|
106
|
+
const usdc = ALGORAND_USDC[algorandId(network)];
|
|
107
|
+
if (usdc) return on.find((a) => String(a.asset) === usdc) ?? null;
|
|
90
108
|
// x402's spend controls cap Stellar USDC too; the budget is checked here as well, in dollars.
|
|
91
109
|
if (STELLAR_USDC[network]) {
|
|
92
110
|
return on.find((a) => a.asset === STELLAR_USDC[network] && /^\d+$/.test(String(a.amount)) && Number(a.amount) > 0 && Number(a.amount) / 1e7 <= cap) ?? null;
|
|
@@ -99,7 +117,11 @@ export function payableOption(accepts, network, maxUsd) {
|
|
|
99
117
|
return on.find((a) => String(a.asset ?? "").toUpperCase() === RLUSD_HEX && a.extra?.issuer === RLUSD_ISSUER[network]
|
|
100
118
|
&& /^\d+(\.\d+)?$/.test(String(a.amount)) && Number(a.amount) > 0 && Number(a.amount) <= cap) ?? null;
|
|
101
119
|
}
|
|
102
|
-
|
|
120
|
+
// Base and other EVM chains: "exact" first (what most clients register), then "auth-capture" (Coinbase's escrow:
|
|
121
|
+
// the buyer signs once, the USDC is held and only captured when the seller delivers, e.g. Atom's domain checkout).
|
|
122
|
+
// Either is paid only if your register() added its scheme, and both stay within the budget (x402's spend controls
|
|
123
|
+
// read the amount, which for auth-capture is the most that can be captured).
|
|
124
|
+
return on.find((a) => a?.scheme === "exact") ?? on.find((a) => a?.scheme === "auth-capture") ?? on[0] ?? null;
|
|
103
125
|
}
|
|
104
126
|
|
|
105
127
|
export class SafePayError extends Error {
|
|
@@ -228,10 +250,23 @@ export function createSafeFetch({
|
|
|
228
250
|
const makePayingFetch = createPayingFetch ?? ((cap) => {
|
|
229
251
|
const client = new x402Client((_version, accepts) => {
|
|
230
252
|
const pick = payableOption(accepts, payNetwork, cap);
|
|
231
|
-
if (!pick) throw new SafePayError(`no payment option on ${payNetwork}${ALGORAND_USDC[payNetwork] ? " in USDC" : STELLAR_USDC[payNetwork] ? ` in USDC within ${cap}` : XLAYER_DOLLARS[payNetwork] ? ` in USD₮0 or USDC within ${cap}` : RLUSD_ISSUER[payNetwork] ? ` in RLUSD within ${cap}` : ""}`, { code: "no_option" });
|
|
253
|
+
if (!pick) throw new SafePayError(`no payment option on ${payNetwork}${ALGORAND_USDC[algorandId(payNetwork)] ? " in USDC" : STELLAR_USDC[payNetwork] ? ` in USDC within ${cap}` : XLAYER_DOLLARS[payNetwork] ? ` in USD₮0 or USDC within ${cap}` : RLUSD_ISSUER[payNetwork] ? ` in RLUSD within ${cap}` : ""}`, { code: "no_option" });
|
|
232
254
|
return pick;
|
|
233
255
|
}).setSpendControls({ maxAmountPerPayment: cap, ...xlayerAllowance(payNetwork, cap) });
|
|
234
|
-
|
|
256
|
+
// Algorand schemes get anyAlgorandId, so sellers with either network id can be paid.
|
|
257
|
+
const registering = new Proxy(client, {
|
|
258
|
+
get(target, prop) {
|
|
259
|
+
if (prop === "register") {
|
|
260
|
+
return (network, scheme) => {
|
|
261
|
+
target.register(network, String(network).startsWith("algorand:") ? anyAlgorandId(scheme) : scheme);
|
|
262
|
+
return registering;
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
const value = Reflect.get(target, prop);
|
|
266
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
register(registering);
|
|
235
270
|
return wrapFetchWithPayment(baseFetch, client);
|
|
236
271
|
});
|
|
237
272
|
let preflightFetch;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "x402-safe-fetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "A fetch for AI agents that checks an unknown x402 endpoint ($0.001 x402 Doctor preflight) before paying it, and never pays above your budget.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@algorandfoundation/algokit-utils": "^10.0.0-alpha.46",
|
|
32
|
-
"@x402/avm": "^2.
|
|
32
|
+
"@x402/avm": "^2.29.0",
|
|
33
33
|
"@x402/evm": "^2.27.0",
|
|
34
34
|
"@x402/fetch": "^2.27.0",
|
|
35
35
|
"@x402/stellar": "^2.28.0",
|