@wasit-dev/core 0.7.0 → 0.8.0-beta.2
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 +13 -2
- package/dist/baseline.d.ts +109 -0
- package/dist/baseline.js +353 -0
- package/dist/catalogue.js +10 -0
- package/dist/check.d.ts +57 -2
- package/dist/check.js +46 -6
- package/dist/ci-report.d.ts +74 -0
- package/dist/ci-report.js +399 -0
- package/dist/config-writer.d.ts +96 -0
- package/dist/config-writer.js +281 -0
- package/dist/config.d.ts +205 -0
- package/dist/config.js +1011 -0
- package/dist/denied-env.d.ts +33 -0
- package/dist/denied-env.js +86 -0
- package/dist/errors.d.ts +44 -1
- package/dist/errors.js +151 -2
- package/dist/guard/budget.d.ts +138 -0
- package/dist/guard/budget.js +206 -0
- package/dist/guard/ci-event.d.ts +56 -0
- package/dist/guard/ci-event.js +129 -0
- package/dist/guard/ci-mainnet.d.ts +59 -0
- package/dist/guard/ci-mainnet.js +149 -0
- package/dist/guard/horizon.d.ts +30 -0
- package/dist/guard/horizon.js +107 -0
- package/dist/guard/mainnet-payer.d.ts +66 -0
- package/dist/guard/mainnet-payer.js +92 -0
- package/dist/guard/mainnet-plan.d.ts +133 -0
- package/dist/guard/mainnet-plan.js +208 -0
- package/dist/guard/mainnet-run.d.ts +87 -0
- package/dist/guard/mainnet-run.js +225 -0
- package/dist/guard/networks.d.ts +51 -0
- package/dist/guard/networks.js +91 -0
- package/dist/guard/optin.d.ts +106 -0
- package/dist/guard/optin.js +202 -0
- package/dist/guard/rpc-identity.d.ts +45 -0
- package/dist/guard/rpc-identity.js +156 -0
- package/dist/guard/rpc-in-use.d.ts +48 -0
- package/dist/guard/rpc-in-use.js +84 -0
- package/dist/guard/run-ledger.d.ts +212 -0
- package/dist/guard/run-ledger.js +426 -0
- package/dist/guard/run-lock.d.ts +62 -0
- package/dist/guard/run-lock.js +172 -0
- package/dist/guard/signed-terms.d.ts +96 -0
- package/dist/guard/signed-terms.js +207 -0
- package/dist/index.d.ts +44 -4
- package/dist/index.js +42 -2
- package/dist/mpp/channel-commitment.d.ts +48 -2
- package/dist/mpp/channel-commitment.js +80 -4
- package/dist/mpp/channel-credential.d.ts +13 -1
- package/dist/mpp/channel-credential.js +21 -2
- package/dist/mpp/channel.d.ts +6 -0
- package/dist/mpp/channel.js +36 -3
- package/dist/mpp/charge.d.ts +33 -1
- package/dist/mpp/charge.js +90 -20
- package/dist/mpp/network.js +4 -1
- package/dist/mpp/plan.d.ts +77 -0
- package/dist/mpp/plan.js +188 -0
- package/dist/mpp/suite.d.ts +6 -0
- package/dist/mpp/suite.js +12 -3
- package/dist/report.d.ts +163 -0
- package/dist/report.js +282 -0
- package/dist/route-reader.d.ts +124 -0
- package/dist/route-reader.js +349 -0
- package/dist/sanitize.d.ts +52 -0
- package/dist/sanitize.js +106 -0
- package/dist/settlement.d.ts +14 -0
- package/dist/settlement.js +1 -1
- package/dist/verdict.d.ts +24 -0
- package/dist/verdict.js +46 -0
- package/dist/versions.d.ts +50 -0
- package/dist/versions.js +123 -0
- package/dist/wallet.d.ts +58 -1
- package/dist/wallet.js +128 -0
- package/dist/x402/chains/evm.js +3 -0
- package/dist/x402/chains/index.d.ts +8 -0
- package/dist/x402/chains/index.js +13 -2
- package/dist/x402/chains/stellar.d.ts +8 -1
- package/dist/x402/chains/stellar.js +259 -4
- package/dist/x402/chains/types.d.ts +104 -2
- package/dist/x402/plan.d.ts +184 -0
- package/dist/x402/plan.js +520 -0
- package/dist/x402/rejected-request.d.ts +156 -0
- package/dist/x402/rejected-request.js +287 -0
- package/dist/x402/requirements.d.ts +1 -0
- package/dist/x402/requirements.js +37 -4
- package/dist/x402/simulator.d.ts +86 -1
- package/dist/x402/simulator.js +863 -87
- package/package.json +1 -1
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L1, the plan (spec B3, B4): before anything is signed, read each endpoint's
|
|
3
|
+
* unpaid challenge and work out what the run's payment checks would sign, and
|
|
4
|
+
* refuse what the caps or the pin do not allow.
|
|
5
|
+
*
|
|
6
|
+
* The plan is what `--dry-run` prints, and what a paying run hands to each
|
|
7
|
+
* endpoint's checks: L2 then refuses any signature the plan did not allow, so
|
|
8
|
+
* a service that raises its price or changes its payee between the plan and
|
|
9
|
+
* the payment gets a refusal, not a signature.
|
|
10
|
+
*
|
|
11
|
+
* It sends no payment, reads no key and signs nothing.
|
|
12
|
+
*
|
|
13
|
+
* A mainnet run (a live opt-in, spec B5) is planned by stricter rules: first
|
|
14
|
+
* the token's decimals, the payer's USDC (through the RPC and on public
|
|
15
|
+
* Horizon, $10 at most) and the RPC's latest ledger beside Horizon's are read,
|
|
16
|
+
* before the target is asked anything; then every endpoint must plan, by the
|
|
17
|
+
* mainnet rules. Any refusal stops the opt-in. A plan that passes is recorded
|
|
18
|
+
* on it, and is the only plan its checks pay by.
|
|
19
|
+
*/
|
|
20
|
+
import { isLoopbackUrl } from "../config.js";
|
|
21
|
+
import { ConfigurationError } from "../errors.js";
|
|
22
|
+
import { sanitizeText } from "../sanitize.js";
|
|
23
|
+
import { readHorizonAccount, readHorizonLatestLedger } from "../guard/horizon.js";
|
|
24
|
+
import { assertMainnetPayerBalance } from "../guard/mainnet-payer.js";
|
|
25
|
+
import { MAINNET_USDC, MainnetPlanRefusedError, PUBNET_HORIZON_URL, assertAnchorNearHorizon, assertBalancesAgree, assertMainnetDecimals, assertMainnetOption, assertMainnetPin, paidUrlOf, } from "../guard/mainnet-plan.js";
|
|
26
|
+
import { assertPaymentNetworkAllowed } from "../guard/networks.js";
|
|
27
|
+
import { MainnetRunRefusedError, anchorOfOptIn, isLiveOptIn, recordMainnetPlan } from "../guard/optin.js";
|
|
28
|
+
import { PER_PAYMENT_CAP_USD, PER_RUN_CAP_USD, PaymentGuardRefusedError, RunBudget, assertPinned, formatUsd, parseAmount, plannedSignatures, shown, usdAsset, usdFromBaseUnits, usdFromDollars, } from "../guard/budget.js";
|
|
29
|
+
import { paymentChainFor } from "./chains/index.js";
|
|
30
|
+
const STELLAR_PUBNET = "stellar:pubnet";
|
|
31
|
+
import { REJECTED_REQUEST_MAX_LIFETIME_SECONDS, REJECTED_REQUEST_MIN_LIFETIME_SECONDS } from "./rejected-request.js";
|
|
32
|
+
import { readPaymentChallenge } from "./simulator.js";
|
|
33
|
+
function message(error) {
|
|
34
|
+
return sanitizeText(error instanceof Error ? error.message : String(error)).replace(/\s+/g, " ");
|
|
35
|
+
}
|
|
36
|
+
const nothing = (name, status, reason, option) => ({
|
|
37
|
+
name,
|
|
38
|
+
status,
|
|
39
|
+
reason,
|
|
40
|
+
...(option === undefined ? {} : { option }),
|
|
41
|
+
signatures: [],
|
|
42
|
+
worstCase: 0n,
|
|
43
|
+
worstCaseUsd: 0n,
|
|
44
|
+
});
|
|
45
|
+
/**
|
|
46
|
+
* The URL rule of a mainnet endpoint, before it is asked anything: https, or
|
|
47
|
+
* on this machine (a local service, as the validation runs one), and no user
|
|
48
|
+
* name or password in it (they can hide which host it is). Returns where it
|
|
49
|
+
* pays, origin and path.
|
|
50
|
+
*/
|
|
51
|
+
function mainnetEndpointUrl(target) {
|
|
52
|
+
let url;
|
|
53
|
+
try {
|
|
54
|
+
url = new URL(target);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
throw new MainnetPlanRefusedError("endpoint", "Its URL does not parse.");
|
|
58
|
+
}
|
|
59
|
+
if (url.username !== "" || url.password !== "") {
|
|
60
|
+
throw new MainnetPlanRefusedError("endpoint", "Its URL carries a user name or password: a mainnet run pays a URL whose host is plain to read.");
|
|
61
|
+
}
|
|
62
|
+
if (url.protocol !== "https:" && !isLoopbackUrl(target)) {
|
|
63
|
+
throw new MainnetPlanRefusedError("endpoint", `A mainnet run pays an https endpoint only, or one on this machine, and this one is ${url.protocol.replace(/:$/, "")} on ${url.host}.`);
|
|
64
|
+
}
|
|
65
|
+
return paidUrlOf(target);
|
|
66
|
+
}
|
|
67
|
+
async function planEndpoint(network, pin, endpoint, mainnet) {
|
|
68
|
+
if (!mainnet)
|
|
69
|
+
return planEndpointTerms(network, pin, endpoint, undefined);
|
|
70
|
+
let url;
|
|
71
|
+
try {
|
|
72
|
+
url = mainnetEndpointUrl(endpoint.target);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (error instanceof MainnetPlanRefusedError)
|
|
76
|
+
return nothing(endpoint.name, "refused", message(error));
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
return { ...(await planEndpointTerms(network, pin, endpoint, url)), url };
|
|
80
|
+
}
|
|
81
|
+
async function planEndpointTerms(network, pin, endpoint, url) {
|
|
82
|
+
const mainnet = url !== undefined;
|
|
83
|
+
let read;
|
|
84
|
+
try {
|
|
85
|
+
read = await readPaymentChallenge({ ...endpoint, network });
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
// The same reasons the payment checks give when they would not pay.
|
|
89
|
+
return nothing(endpoint.name, "not-paid", message(error));
|
|
90
|
+
}
|
|
91
|
+
const { paymentRequired, selected } = read;
|
|
92
|
+
try {
|
|
93
|
+
// A mainnet endpoint pays the URL in the config, not one a redirect leads to.
|
|
94
|
+
if (url !== undefined && paidUrlOf(read.payUrl) !== url) {
|
|
95
|
+
throw new MainnetPlanRefusedError("endpoint", `It answers through a redirect to ${shown(paidUrlOf(read.payUrl))}: a mainnet run pays the URL in the config only.`);
|
|
96
|
+
}
|
|
97
|
+
assertPinned(network, paymentRequired.accepts, pin);
|
|
98
|
+
const amount = parseAmount(selected.amount);
|
|
99
|
+
const option = {
|
|
100
|
+
scheme: selected.scheme,
|
|
101
|
+
network: selected.network,
|
|
102
|
+
payTo: selected.payTo,
|
|
103
|
+
asset: selected.asset,
|
|
104
|
+
amount,
|
|
105
|
+
maxTimeoutSeconds: selected.maxTimeoutSeconds,
|
|
106
|
+
};
|
|
107
|
+
// Exact, on stellar:pubnet, in USDC, valid for at most 300 s (B3).
|
|
108
|
+
if (mainnet)
|
|
109
|
+
assertMainnetOption(selected);
|
|
110
|
+
const asset = usdAsset(network, selected.asset);
|
|
111
|
+
const price = RunBudget.valueOf(network, selected.asset, amount);
|
|
112
|
+
if (asset === undefined || price === undefined) {
|
|
113
|
+
return nothing(endpoint.name, "refused", `it asks to be paid in an asset Wasit does not value in US dollars, so it cannot be held to the ` +
|
|
114
|
+
`$${PER_PAYMENT_CAP_USD}.00 cap. Nothing was signed.`, option);
|
|
115
|
+
}
|
|
116
|
+
const perPayment = usdFromDollars(PER_PAYMENT_CAP_USD);
|
|
117
|
+
if (price > perPayment) {
|
|
118
|
+
return nothing(endpoint.name, "refused", `it is priced at ${amount} base units of ${asset.symbol} (${formatUsd(price)}), above the ` +
|
|
119
|
+
`${formatUsd(perPayment)} per payment fixed in Wasit. Nothing was signed. Test a route ` +
|
|
120
|
+
`priced at ${formatUsd(perPayment)} or less.`, option);
|
|
121
|
+
}
|
|
122
|
+
// X402-11 signs only when it can reach a verdict: a reject case, a chain whose
|
|
123
|
+
// authorizations can be read, a lifetime it waits out (D-032).
|
|
124
|
+
const rejectedRequest = endpoint.reject !== undefined &&
|
|
125
|
+
paymentChainFor(network)?.authorizations !== undefined &&
|
|
126
|
+
selected.maxTimeoutSeconds >= REJECTED_REQUEST_MIN_LIFETIME_SECONDS &&
|
|
127
|
+
selected.maxTimeoutSeconds <= REJECTED_REQUEST_MAX_LIFETIME_SECONDS;
|
|
128
|
+
const signatures = plannedSignatures(amount, { rejectedRequest });
|
|
129
|
+
const worstCase = signatures.reduce((sum, entry) => sum + entry.amount, 0n);
|
|
130
|
+
return {
|
|
131
|
+
name: endpoint.name,
|
|
132
|
+
status: "planned",
|
|
133
|
+
option,
|
|
134
|
+
signatures,
|
|
135
|
+
worstCase,
|
|
136
|
+
worstCaseUsd: RunBudget.valueOf(network, selected.asset, worstCase) ?? 0n,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
if (error instanceof PaymentGuardRefusedError || error instanceof MainnetPlanRefusedError) {
|
|
141
|
+
return nothing(endpoint.name, "refused", message(error));
|
|
142
|
+
}
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* The plan of a run's x402 payment checks. Throws the guard's refusal for a
|
|
148
|
+
* network that is not a test network (L0), before any request; the mainnet
|
|
149
|
+
* plan (the payer's balance, the token's decimals read on chain, the
|
|
150
|
+
* authorization lifetime) is part of the opt-in, not of this version.
|
|
151
|
+
*/
|
|
152
|
+
export async function planX402Run(options) {
|
|
153
|
+
if (options.mainnet !== undefined)
|
|
154
|
+
return planMainnetRun(options, options.mainnet);
|
|
155
|
+
assertPaymentNetworkAllowed("x402", options.network);
|
|
156
|
+
const budget = new RunBudget(options.maxRunUsd === undefined ? {} : { maxRunUsd: options.maxRunUsd });
|
|
157
|
+
const capLowered = budget.capUsd < usdFromDollars(PER_RUN_CAP_USD);
|
|
158
|
+
const endpoints = [];
|
|
159
|
+
for (const endpoint of options.endpoints) {
|
|
160
|
+
endpoints.push(await planEndpoint(options.network, options.pin, endpoint, false));
|
|
161
|
+
}
|
|
162
|
+
const planned = endpoints.filter((endpoint) => endpoint.status === "planned");
|
|
163
|
+
const worstCaseUsd = planned.reduce((sum, endpoint) => sum + endpoint.worstCaseUsd, 0n);
|
|
164
|
+
const base = {
|
|
165
|
+
network: options.network,
|
|
166
|
+
...(options.pin === undefined ? {} : { pin: options.pin }),
|
|
167
|
+
perPaymentCapUsd: usdFromDollars(PER_PAYMENT_CAP_USD),
|
|
168
|
+
capUsd: budget.capUsd,
|
|
169
|
+
capLowered,
|
|
170
|
+
endpoints,
|
|
171
|
+
worstCaseUsd,
|
|
172
|
+
};
|
|
173
|
+
if (worstCaseUsd <= budget.capUsd)
|
|
174
|
+
return base;
|
|
175
|
+
const parts = planned.map((endpoint) => `${sanitizeText(endpoint.name)} ${formatUsd(endpoint.worstCaseUsd)}`);
|
|
176
|
+
return {
|
|
177
|
+
...base,
|
|
178
|
+
refusal: `This run could spend up to ${formatUsd(worstCaseUsd)} (${parts.join(", ")}), above the ` +
|
|
179
|
+
`${formatUsd(budget.capUsd)} per run${capLowered ? " set by maxRunUsd" : ""}. Nothing was signed. ` +
|
|
180
|
+
`Select fewer endpoints with --endpoint, or test a cheaper route.`,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/** Public Horizon, as a mainnet plan reads it; tests point it at a stand-in. */
|
|
184
|
+
let pubnetHorizon = PUBNET_HORIZON_URL;
|
|
185
|
+
/** Tests only: where a mainnet plan reads Horizon; undefined puts back the public one. Not exported from the package. */
|
|
186
|
+
export function setPubnetHorizonForTesting(base) {
|
|
187
|
+
pubnetHorizon = base ?? PUBNET_HORIZON_URL;
|
|
188
|
+
}
|
|
189
|
+
function originOf(url) {
|
|
190
|
+
try {
|
|
191
|
+
return new URL(url).origin;
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
return "the RPC";
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* The reads of a mainnet plan, in this order, each refusing on its own: the
|
|
199
|
+
* token's decimals (7); the payer's USDC through the RPC ($10 at most); the
|
|
200
|
+
* payer's balances on public Horizon (USDC equal to the RPC's); the RPC's
|
|
201
|
+
* latest ledger beside Horizon's (12 apart at most), which becomes the run's
|
|
202
|
+
* anchor. Nothing is sent to the target, and nothing is signed.
|
|
203
|
+
*/
|
|
204
|
+
async function readMainnet(optIn, chain) {
|
|
205
|
+
const { network, rpcUrl, payer } = optIn;
|
|
206
|
+
const signedTerms = chain.signedTerms;
|
|
207
|
+
const assetDecimals = chain.assetDecimals;
|
|
208
|
+
if (signedTerms === undefined || assetDecimals === undefined) {
|
|
209
|
+
throw new MainnetRunRefusedError("run", `The ${chain.name} adapter cannot read what a mainnet plan reads before it pays.`);
|
|
210
|
+
}
|
|
211
|
+
let decimals;
|
|
212
|
+
try {
|
|
213
|
+
decimals = await assetDecimals.call(chain, network, rpcUrl, MAINNET_USDC.asset);
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
decimals = undefined;
|
|
217
|
+
}
|
|
218
|
+
assertMainnetDecimals(decimals);
|
|
219
|
+
const usdc = await assertMainnetPayerBalance({
|
|
220
|
+
network,
|
|
221
|
+
payer,
|
|
222
|
+
readBalance: () => chain.payerBalance(network, rpcUrl, payer, MAINNET_USDC.asset),
|
|
223
|
+
});
|
|
224
|
+
const horizon = await readHorizonAccount(pubnetHorizon, payer);
|
|
225
|
+
assertBalancesAgree(usdc, horizon.usdc);
|
|
226
|
+
let rpcLatest;
|
|
227
|
+
try {
|
|
228
|
+
rpcLatest = await signedTerms.latestUnit(network, rpcUrl);
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
rpcLatest = undefined;
|
|
232
|
+
}
|
|
233
|
+
const horizonLatest = await readHorizonLatestLedger(pubnetHorizon);
|
|
234
|
+
assertAnchorNearHorizon(rpcLatest, horizonLatest);
|
|
235
|
+
anchorOfOptIn(optIn).set(rpcLatest);
|
|
236
|
+
return {
|
|
237
|
+
payer,
|
|
238
|
+
decimals: decimals,
|
|
239
|
+
usdc: usdc,
|
|
240
|
+
native: horizon.native,
|
|
241
|
+
rpcLatest: rpcLatest,
|
|
242
|
+
horizonLatest,
|
|
243
|
+
rpc: originOf(rpcUrl),
|
|
244
|
+
horizon: originOf(pubnetHorizon),
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Why a mainnet run may not start on these endpoints, or undefined: one
|
|
249
|
+
* endpoint that could not be planned keeps the whole run from starting (B4),
|
|
250
|
+
* and so does a worst case above what is left of the budget.
|
|
251
|
+
*/
|
|
252
|
+
function mainnetRefusal(endpoints, worstCaseUsd, budget, capLowered) {
|
|
253
|
+
const unplanned = endpoints.filter((endpoint) => endpoint.status !== "planned");
|
|
254
|
+
if (unplanned.length > 0) {
|
|
255
|
+
const reasons = unplanned.map((endpoint) => `Endpoint ${sanitizeText(endpoint.name)} could not be planned: ` +
|
|
256
|
+
`${(endpoint.reason ?? endpoint.status).replace(/\s*Nothing was signed\.?/g, "").trim().replace(/\.?$/, ".")}`);
|
|
257
|
+
return `This mainnet run does not start: it pays only endpoints it could plan. ${reasons.join(" ")} Nothing was signed.`;
|
|
258
|
+
}
|
|
259
|
+
const remainingUsd = budget.capUsd - budget.spentUsd;
|
|
260
|
+
if (worstCaseUsd <= remainingUsd)
|
|
261
|
+
return undefined;
|
|
262
|
+
const parts = endpoints.map((endpoint) => `${sanitizeText(endpoint.name)} ${formatUsd(endpoint.worstCaseUsd)}`);
|
|
263
|
+
const left = budget.spentUsd === 0n
|
|
264
|
+
? `the ${formatUsd(budget.capUsd)} per run`
|
|
265
|
+
: `the ${formatUsd(remainingUsd)} left of the ${formatUsd(budget.capUsd)} per run`;
|
|
266
|
+
return (`This run could spend up to ${formatUsd(worstCaseUsd)} (${parts.join(", ")}), above ${left}` +
|
|
267
|
+
`${capLowered ? " set by maxRunUsd" : ""}. Nothing was signed. Select fewer endpoints, or test a cheaper route.`);
|
|
268
|
+
}
|
|
269
|
+
/** USDC base units as dollars, exactly ("$1.2733706"). */
|
|
270
|
+
function usdc(units, decimals) {
|
|
271
|
+
return formatUsd(usdFromBaseUnits(units, decimals));
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* What the planned endpoints need the payer to hold; undefined when no
|
|
275
|
+
* endpoint is planned. With `balance` (a paying run) it carries the balance;
|
|
276
|
+
* the warning is added only once the plan has passed ({@link warnedFunding}).
|
|
277
|
+
*/
|
|
278
|
+
function mainnetFunding(endpoints, decimals, balance) {
|
|
279
|
+
const prices = endpoints.flatMap((endpoint) => (endpoint.status === "planned" && endpoint.option !== undefined ? [endpoint.option.amount] : []));
|
|
280
|
+
if (prices.length === 0)
|
|
281
|
+
return undefined;
|
|
282
|
+
const sum = prices.reduce((total, price) => total + price, 0n);
|
|
283
|
+
const highestPrice = prices.reduce((highest, price) => (price > highest ? price : highest), 0n);
|
|
284
|
+
const minimum = sum + highestPrice;
|
|
285
|
+
const worstCase = endpoints.reduce((total, endpoint) => total + (endpoint.status === "planned" ? endpoint.worstCase : 0n), 0n);
|
|
286
|
+
return { prices: sum, highestPrice, minimum, worstCase, decimals, ...(balance === undefined ? {} : { balance }) };
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* The funds of a paying plan that passed every refusal, with the warning when
|
|
290
|
+
* the balance is at or above the minimum but below the worst case.
|
|
291
|
+
*/
|
|
292
|
+
function warnedFunding(funding) {
|
|
293
|
+
const { balance, decimals, minimum, worstCase } = funding;
|
|
294
|
+
if (balance === undefined || balance < minimum || balance >= worstCase)
|
|
295
|
+
return funding;
|
|
296
|
+
return {
|
|
297
|
+
...funding,
|
|
298
|
+
warning: `Warning: the payer holds ${usdc(balance, decimals)}, less than this run's worst case of ${usdc(worstCase, decimals)}. ` +
|
|
299
|
+
`A service that settles payments it should refuse can take more than the ${usdc(minimum, decimals)} this run needs: ` +
|
|
300
|
+
`funds can be lost that way, and the later checks may then have too little left to run.`,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
/** The prices clause shared by the refusal, the funds line and the dry run's line. */
|
|
304
|
+
function fundingTerms(funding) {
|
|
305
|
+
const { decimals } = funding;
|
|
306
|
+
return (`at least ${usdc(funding.minimum, decimals)} to finish its checks (the prices, ${usdc(funding.prices, decimals)}, ` +
|
|
307
|
+
`plus the highest price again, ${usdc(funding.highestPrice, decimals)})`);
|
|
308
|
+
}
|
|
309
|
+
/** Why a paying mainnet run may not start for lack of funds, or undefined. */
|
|
310
|
+
function fundingRefusal(funding) {
|
|
311
|
+
if (funding?.balance === undefined || funding.balance >= funding.minimum)
|
|
312
|
+
return undefined;
|
|
313
|
+
const { decimals, balance } = funding;
|
|
314
|
+
return (`This mainnet run does not start: the payer holds ${usdc(balance, decimals)} in USDC, and this run needs ` +
|
|
315
|
+
`${fundingTerms(funding)}: ${usdc(funding.minimum - balance, decimals)} short. Nothing was signed. Add USDC to the ` +
|
|
316
|
+
`payer (it may hold at most $10.00), or select fewer or cheaper endpoints.`);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* The dry run of a mainnet run on `stellar:pubnet` (spec B5): each endpoint's
|
|
320
|
+
* unpaid challenge, by the mainnet rules (the URL, exact on pubnet, USDC, a
|
|
321
|
+
* lifetime of at most 300 s, $1 a payment), against the caps. It needs no
|
|
322
|
+
* opt-in because it signs nothing, and it reads no key, so nothing of the
|
|
323
|
+
* payer is read: no balance, and no read of the chain, the RPC or Horizon. A
|
|
324
|
+
* paying run makes those reads first, and refuses on any of them.
|
|
325
|
+
*/
|
|
326
|
+
export async function planMainnetDryRun(options) {
|
|
327
|
+
const budget = new RunBudget(options.maxRunUsd === undefined ? {} : { maxRunUsd: options.maxRunUsd });
|
|
328
|
+
const capLowered = budget.capUsd < usdFromDollars(PER_RUN_CAP_USD);
|
|
329
|
+
const base = {
|
|
330
|
+
network: STELLAR_PUBNET,
|
|
331
|
+
...(options.pin === undefined ? {} : { pin: options.pin }),
|
|
332
|
+
perPaymentCapUsd: usdFromDollars(PER_PAYMENT_CAP_USD),
|
|
333
|
+
capUsd: budget.capUsd,
|
|
334
|
+
capLowered,
|
|
335
|
+
mainnetDryRun: true,
|
|
336
|
+
};
|
|
337
|
+
try {
|
|
338
|
+
// No key is read, so the payee is held to the payer's address only by the paying run.
|
|
339
|
+
assertMainnetPin(options.pin, undefined);
|
|
340
|
+
}
|
|
341
|
+
catch (error) {
|
|
342
|
+
if (error instanceof MainnetPlanRefusedError) {
|
|
343
|
+
return { ...base, endpoints: [], worstCaseUsd: 0n, refusal: `This mainnet run does not start: ${message(error)}` };
|
|
344
|
+
}
|
|
345
|
+
throw error;
|
|
346
|
+
}
|
|
347
|
+
const endpoints = [];
|
|
348
|
+
for (const endpoint of options.endpoints) {
|
|
349
|
+
endpoints.push(await planEndpoint(STELLAR_PUBNET, options.pin, endpoint, true));
|
|
350
|
+
}
|
|
351
|
+
const worstCaseUsd = endpoints
|
|
352
|
+
.filter((endpoint) => endpoint.status === "planned")
|
|
353
|
+
.reduce((sum, endpoint) => sum + endpoint.worstCaseUsd, 0n);
|
|
354
|
+
// No key, so no balance: the plan says what a paying run will hold the payer to.
|
|
355
|
+
const funding = mainnetFunding(endpoints, MAINNET_USDC.decimals);
|
|
356
|
+
const plan = { ...base, endpoints, worstCaseUsd, ...(funding === undefined ? {} : { funding }) };
|
|
357
|
+
const refusal = mainnetRefusal(endpoints, worstCaseUsd, budget, capLowered);
|
|
358
|
+
return refusal === undefined ? plan : { ...plan, refusal };
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* L1 of a mainnet run: the reads, then each endpoint by the mainnet rules.
|
|
362
|
+
* Every endpoint must plan, and the worst case must fit what is left of the
|
|
363
|
+
* run's budget; otherwise the run is refused and the opt-in stopped. A plan
|
|
364
|
+
* that passes is recorded on the opt-in, once.
|
|
365
|
+
*/
|
|
366
|
+
async function planMainnetRun(options, optIn) {
|
|
367
|
+
if (options.network !== optIn.network) {
|
|
368
|
+
throw new MainnetRunRefusedError("run", `A mainnet opt-in was handed to the plan of a run on another network than ${optIn.network}.`);
|
|
369
|
+
}
|
|
370
|
+
assertPaymentNetworkAllowed("x402", options.network, optIn);
|
|
371
|
+
try {
|
|
372
|
+
return await planLiveMainnetRun(options, optIn);
|
|
373
|
+
}
|
|
374
|
+
catch (error) {
|
|
375
|
+
// Fail closed: whatever stops the plan stops the run, so it is never planned twice.
|
|
376
|
+
optIn.stop(error.message);
|
|
377
|
+
throw error;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
async function planLiveMainnetRun(options, optIn) {
|
|
381
|
+
if (optIn.plan !== undefined)
|
|
382
|
+
throw new MainnetRunRefusedError("run", "The mainnet run already has a plan: it is planned once.");
|
|
383
|
+
if (options.pin !== undefined && (options.pin.payTo !== optIn.pin.payTo || options.pin.asset !== optIn.pin.asset)) {
|
|
384
|
+
throw new MainnetRunRefusedError("run", "The plan was handed another pin than the mainnet run's.");
|
|
385
|
+
}
|
|
386
|
+
if (options.maxRunUsd !== undefined) {
|
|
387
|
+
throw new MainnetRunRefusedError("run", "A mainnet run's cap is set when it is opened; the plan takes no maxRunUsd.");
|
|
388
|
+
}
|
|
389
|
+
const chain = paymentChainFor(options.network);
|
|
390
|
+
if (chain === undefined)
|
|
391
|
+
throw new ConfigurationError(`No payment adapter pays on ${options.network}.`);
|
|
392
|
+
const readings = await readMainnet(optIn, chain);
|
|
393
|
+
const endpoints = [];
|
|
394
|
+
for (const endpoint of options.endpoints) {
|
|
395
|
+
endpoints.push(await planEndpoint(options.network, optIn.pin, endpoint, true));
|
|
396
|
+
}
|
|
397
|
+
const planned = endpoints.filter((endpoint) => endpoint.status === "planned");
|
|
398
|
+
const worstCaseUsd = planned.reduce((sum, endpoint) => sum + endpoint.worstCaseUsd, 0n);
|
|
399
|
+
const budget = optIn.budget;
|
|
400
|
+
const remainingUsd = budget.capUsd - budget.spentUsd;
|
|
401
|
+
const capLowered = budget.capUsd < usdFromDollars(PER_RUN_CAP_USD);
|
|
402
|
+
// The payer's funds: the balance the plan read, held to what the run needs to finish its checks.
|
|
403
|
+
const funding = mainnetFunding(endpoints, readings.decimals, readings.usdc);
|
|
404
|
+
const plan = {
|
|
405
|
+
network: options.network,
|
|
406
|
+
pin: optIn.pin,
|
|
407
|
+
perPaymentCapUsd: usdFromDollars(PER_PAYMENT_CAP_USD),
|
|
408
|
+
capUsd: budget.capUsd,
|
|
409
|
+
capLowered,
|
|
410
|
+
endpoints,
|
|
411
|
+
worstCaseUsd,
|
|
412
|
+
mainnet: { ...readings, remainingUsd },
|
|
413
|
+
...(funding === undefined ? {} : { funding }),
|
|
414
|
+
};
|
|
415
|
+
// B4: on mainnet one endpoint that cannot be planned keeps the whole run from starting; then the cap; then the
|
|
416
|
+
// payer's funds: a run that could not finish its checks does not start.
|
|
417
|
+
const refusal = mainnetRefusal(endpoints, worstCaseUsd, budget, capLowered) ?? fundingRefusal(funding);
|
|
418
|
+
if (refusal !== undefined) {
|
|
419
|
+
// A refused run signs nothing, so it carries no warning that funds can be lost.
|
|
420
|
+
optIn.stop(refusal);
|
|
421
|
+
return { ...plan, refusal };
|
|
422
|
+
}
|
|
423
|
+
const passed = funding === undefined ? plan : { ...plan, funding: warnedFunding(funding) };
|
|
424
|
+
recordMainnetPlan(optIn, passed);
|
|
425
|
+
return passed;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* The opening line of a mainnet run (spec B5: a mainnet run says so in its
|
|
429
|
+
* first line): printed when the opt-in opens, before a service is started or
|
|
430
|
+
* the target is asked anything. The network, real funds, the payer, the one
|
|
431
|
+
* payee and the caps; the worst case comes with the plan
|
|
432
|
+
* ({@link mainnetFirstLine}).
|
|
433
|
+
*/
|
|
434
|
+
export function mainnetOpeningLine(optIn) {
|
|
435
|
+
if (!isLiveOptIn(optIn, STELLAR_PUBNET))
|
|
436
|
+
throw new ConfigurationError("Not a mainnet opt-in that is still open.");
|
|
437
|
+
const { budget } = optIn;
|
|
438
|
+
const remaining = budget.capUsd - budget.spentUsd;
|
|
439
|
+
const cap = budget.spentUsd === 0n
|
|
440
|
+
? `at most ${formatUsd(budget.capUsd)} this run`
|
|
441
|
+
: `at most ${formatUsd(remaining)} this run, what is left of the ${formatUsd(budget.capUsd)} cap`;
|
|
442
|
+
return sanitizeText(`MAINNET RUN on ${optIn.network}: real funds. Payer ${optIn.payer}; pays only ${optIn.pin.payTo} in USDC; ` +
|
|
443
|
+
`at most ${formatUsd(usdFromDollars(PER_PAYMENT_CAP_USD))} per payment; ${cap}. ` +
|
|
444
|
+
`The plan is read next; nothing is signed before it passes.`);
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* The first line of every mainnet run (spec B5): the network, real funds, the
|
|
448
|
+
* payer, the one payee, the caps and the run's worst case.
|
|
449
|
+
*/
|
|
450
|
+
export function mainnetFirstLine(plan) {
|
|
451
|
+
const mainnet = plan.mainnet;
|
|
452
|
+
if (mainnet === undefined || plan.pin === undefined)
|
|
453
|
+
throw new ConfigurationError("Not the plan of a mainnet run.");
|
|
454
|
+
const left = mainnet.remainingUsd === plan.capUsd ? `the ${formatUsd(plan.capUsd)} cap` : `the ${formatUsd(mainnet.remainingUsd)} left of the ${formatUsd(plan.capUsd)} cap`;
|
|
455
|
+
return sanitizeText(`MAINNET RUN on ${plan.network}: real funds. Payer ${mainnet.payer}; pays only ${plan.pin.payTo} in USDC; ` +
|
|
456
|
+
`at most ${formatUsd(plan.perPaymentCapUsd)} per payment; this run could spend up to ${formatUsd(plan.worstCaseUsd)} of ${left}.`);
|
|
457
|
+
}
|
|
458
|
+
/** The funds lines of a mainnet plan: the balance against the minimum and the worst case, and the warning. */
|
|
459
|
+
function fundingLines(plan) {
|
|
460
|
+
const funding = plan.funding;
|
|
461
|
+
if (funding === undefined)
|
|
462
|
+
return [];
|
|
463
|
+
const { decimals, balance } = funding;
|
|
464
|
+
const worst = usdc(funding.worstCase, decimals);
|
|
465
|
+
if (balance === undefined) {
|
|
466
|
+
return [
|
|
467
|
+
`Mainnet dry run: a paying run also refuses when the payer holds less than ${usdc(funding.minimum, decimals)} in USDC ` +
|
|
468
|
+
`(the prices, ${usdc(funding.prices, decimals)}, plus the highest price again, ${usdc(funding.highestPrice, decimals)}: ` +
|
|
469
|
+
`what it needs to finish its checks), and warns below the worst case of ${worst}.`,
|
|
470
|
+
];
|
|
471
|
+
}
|
|
472
|
+
const margin = balance >= funding.minimum ? `${usdc(balance - funding.minimum, decimals)} above it` : `${usdc(funding.minimum - balance, decimals)} short`;
|
|
473
|
+
return [
|
|
474
|
+
`Mainnet: funds: the payer holds ${usdc(balance, decimals)} in USDC; this run needs ${fundingTerms(funding)}: ${margin}; ` +
|
|
475
|
+
`worst case ${worst}.`,
|
|
476
|
+
...(funding.warning === undefined ? [] : [funding.warning]),
|
|
477
|
+
];
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* The plan for a terminal or a log: the network, the caps, and per endpoint
|
|
481
|
+
* the destination, the asset, the amount of each signature and the worst case.
|
|
482
|
+
* Sanitised; no header value and no key (the plan holds neither).
|
|
483
|
+
*/
|
|
484
|
+
export function describePlan(plan) {
|
|
485
|
+
const lines = [
|
|
486
|
+
`Plan (dry run): x402 payment checks on ${plan.network}. Nothing is signed or sent.`,
|
|
487
|
+
`Caps: ${formatUsd(plan.perPaymentCapUsd)} a payment, ${formatUsd(plan.capUsd)} this run` +
|
|
488
|
+
`${plan.capLowered ? " (maxRunUsd)" : ""}.`,
|
|
489
|
+
...(plan.pin === undefined ? [] : [`Pinned: payTo ${plan.pin.payTo}, asset ${plan.pin.asset}.`]),
|
|
490
|
+
...(plan.mainnet === undefined
|
|
491
|
+
? []
|
|
492
|
+
: [
|
|
493
|
+
`Mainnet: payer ${plan.mainnet.payer} holds ${formatUsd(usdFromBaseUnits(plan.mainnet.usdc, plan.mainnet.decimals))} in USDC ` +
|
|
494
|
+
`(${plan.mainnet.usdc} base units, the RPC and public Horizon agree) and ${plan.mainnet.native} stroops of XLM.`,
|
|
495
|
+
`Mainnet: hosts contacted beside the service: the RPC ${plan.mainnet.rpc} and public Horizon ${plan.mainnet.horizon}; ` +
|
|
496
|
+
`latest ledger ${plan.mainnet.rpcLatest} (RPC) and ${plan.mainnet.horizonLatest} (Horizon).`,
|
|
497
|
+
]),
|
|
498
|
+
...(plan.mainnetDryRun === true
|
|
499
|
+
? [
|
|
500
|
+
"Mainnet dry run: no key is read, so nothing of the payer was read. A paying mainnet run first reads, and refuses " +
|
|
501
|
+
"on any of them: the payer's USDC through the RPC and on public Horizon (the two equal, $10.00 at most), the " +
|
|
502
|
+
"token's decimals (7), and the RPC's latest ledger beside public Horizon's (12 apart at most).",
|
|
503
|
+
]
|
|
504
|
+
: []),
|
|
505
|
+
...fundingLines(plan),
|
|
506
|
+
];
|
|
507
|
+
for (const endpoint of plan.endpoints) {
|
|
508
|
+
const at = endpoint.url === undefined ? "" : ` at ${endpoint.url}`;
|
|
509
|
+
if (endpoint.status !== "planned" || endpoint.option === undefined) {
|
|
510
|
+
lines.push(` ${endpoint.name}${at}: ${endpoint.status === "refused" ? "refused" : "not paid"}: ${endpoint.reason ?? ""}`);
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
const { option } = endpoint;
|
|
514
|
+
lines.push(` ${endpoint.name}${at}: pays ${shown(option.payTo)} in ${shown(option.asset)}, up to ${formatUsd(endpoint.worstCaseUsd)}`, ` ${endpoint.signatures.map((entry) => `${entry.check} ${entry.amount}`).join(", ")} (base units)`);
|
|
515
|
+
}
|
|
516
|
+
lines.push(`Worst case: ${formatUsd(plan.worstCaseUsd)} of ${formatUsd(plan.capUsd)}.`);
|
|
517
|
+
if (plan.refusal !== undefined)
|
|
518
|
+
lines.push(`Refused: ${plan.refusal}`);
|
|
519
|
+
return lines.map((line) => sanitizeText(line));
|
|
520
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* X402-11, "Rejected Request Not Settled" (spec B7 as D-032 changed it).
|
|
3
|
+
*
|
|
4
|
+
* Wasit sends a valid payment, for the advertised terms exactly, with a request
|
|
5
|
+
* the builder's config declares invalid (`endpoints[].reject`), then reads the
|
|
6
|
+
* chain: the nonce of that payment's authorization must still be unused once
|
|
7
|
+
* its expiration ledger has passed. The verdict comes from one read that asks
|
|
8
|
+
* for that nonce and for X402-06's, which was used and must be found: a read
|
|
9
|
+
* that cannot find X402-06's cannot show that ours is absent. The payer's
|
|
10
|
+
* balance and the transfer events are second readings; when they disagree with
|
|
11
|
+
* the nonce, there is no verdict.
|
|
12
|
+
*
|
|
13
|
+
* The wait (Najmi, 2026-11-16): a payment whose advertised lifetime
|
|
14
|
+
* (`maxTimeoutSeconds`) is at most 300 s is waited out, with a hard stop 360 s
|
|
15
|
+
* after it was sent; a longer one is not signed at all (it is read again on the
|
|
16
|
+
* challenge the payment is signed from), and an authorization the client signs
|
|
17
|
+
* to outlive it is not sent. A lifetime below 30 s is not signed either, and an
|
|
18
|
+
* authorization with fewer than 3 ledgers left is not sent (2026-11-17).
|
|
19
|
+
*
|
|
20
|
+
* This file holds the parts that need no network: the rules, the reject
|
|
21
|
+
* request's shape, the watch loop over a chain's reads, and the verdict. The
|
|
22
|
+
* check itself, which pays, is in `simulator.ts` with the other payment checks.
|
|
23
|
+
*/
|
|
24
|
+
import { type CheckResult } from "../check.js";
|
|
25
|
+
import type { AuthorizationWatch, SignedAuthorization } from "./chains/types.js";
|
|
26
|
+
export declare const REJECTED_REQUEST_ID = "X402-11";
|
|
27
|
+
export declare const REJECTED_REQUEST_NAME = "Rejected Request Not Settled";
|
|
28
|
+
/** The longest advertised lifetime X402-11 waits out on a test network. */
|
|
29
|
+
export declare const REJECTED_REQUEST_MAX_LIFETIME_SECONDS = 300;
|
|
30
|
+
/**
|
|
31
|
+
* The shortest advertised lifetime X402-11 signs for (Najmi, 2026-11-17,
|
|
32
|
+
* screening S-1): a payment valid for a few seconds can expire before a service
|
|
33
|
+
* that settles it has it on chain, and its unused authorization would then show
|
|
34
|
+
* nothing about the service.
|
|
35
|
+
*/
|
|
36
|
+
export declare const REJECTED_REQUEST_MIN_LIFETIME_SECONDS = 30;
|
|
37
|
+
/** The fewest units of validity a signed authorization must have left when X402-11 sends it. */
|
|
38
|
+
export declare const REJECTED_REQUEST_MIN_UNITS_LEFT = 3;
|
|
39
|
+
/**
|
|
40
|
+
* How long after the payment is sent the watch stops, whatever the lifetime:
|
|
41
|
+
* a 300 s lifetime is 60 ledgers of 5 s, and the verdict needs the ledger
|
|
42
|
+
* after it, so about 305 s; the rest is slack for a slow ledger or RPC.
|
|
43
|
+
*/
|
|
44
|
+
export declare const REJECTED_REQUEST_HARD_STOP_MS = 360000;
|
|
45
|
+
/**
|
|
46
|
+
* Tests only: a shorter hard stop, so "still valid when the watch stopped" can
|
|
47
|
+
* be shown without a six-minute test. It can only lower the stop. Not exported
|
|
48
|
+
* from the package index; no flag, setting or tool argument reaches it.
|
|
49
|
+
*/
|
|
50
|
+
export declare function setRejectedRequestHardStopForTesting(ms: number | undefined): void;
|
|
51
|
+
export declare function rejectedRequestHardStopMs(): number;
|
|
52
|
+
/** The request the service must refuse, as the config gives it (`ResolvedReject`). */
|
|
53
|
+
export interface RejectCase {
|
|
54
|
+
readonly method?: string;
|
|
55
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
56
|
+
/** Serialised, as `ResolvedReject` holds it. */
|
|
57
|
+
readonly body?: string;
|
|
58
|
+
readonly note?: string;
|
|
59
|
+
}
|
|
60
|
+
interface Shape {
|
|
61
|
+
readonly method?: string;
|
|
62
|
+
readonly body?: string;
|
|
63
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The endpoint's own request with the reject case over it: its method when it
|
|
67
|
+
* names one, its headers over the endpoint's (by name, ignoring case), and its
|
|
68
|
+
* body instead of the endpoint's. A method that carries no body drops the
|
|
69
|
+
* endpoint's body; the config already refuses a reject body on such a method.
|
|
70
|
+
*/
|
|
71
|
+
export declare function rejectRequestShape(shape: Shape, reject: RejectCase): Shape;
|
|
72
|
+
/** What the watch found, from the chain's reads only. */
|
|
73
|
+
export type WatchOutcome =
|
|
74
|
+
/** The chain records the authorization as used, in `ledger`. */
|
|
75
|
+
{
|
|
76
|
+
readonly kind: "used";
|
|
77
|
+
readonly ledger: number;
|
|
78
|
+
readonly latest: number;
|
|
79
|
+
}
|
|
80
|
+
/** A read at `latest`, past the expiration ledger, found it unused, and found the control. */
|
|
81
|
+
| {
|
|
82
|
+
readonly kind: "unused";
|
|
83
|
+
readonly latest: number;
|
|
84
|
+
}
|
|
85
|
+
/** The hard stop came first: at `latest` it was unused and could still be used. */
|
|
86
|
+
| {
|
|
87
|
+
readonly kind: "still-valid";
|
|
88
|
+
readonly latest: number;
|
|
89
|
+
}
|
|
90
|
+
/** The chain could not be read to the end, or its answer could not be trusted: `why`. */
|
|
91
|
+
| {
|
|
92
|
+
readonly kind: "unread";
|
|
93
|
+
readonly why: string;
|
|
94
|
+
};
|
|
95
|
+
export interface WatchInput {
|
|
96
|
+
readonly watch: AuthorizationWatch;
|
|
97
|
+
readonly network: string;
|
|
98
|
+
readonly rpcUrl: string;
|
|
99
|
+
readonly ours: SignedAuthorization;
|
|
100
|
+
/** X402-06's authorization, which was used: every read must find it. */
|
|
101
|
+
readonly control: SignedAuthorization;
|
|
102
|
+
/** When the payment was sent (ms since the epoch): the hard stop counts from it. */
|
|
103
|
+
readonly sentAt: number;
|
|
104
|
+
readonly now?: () => number;
|
|
105
|
+
readonly sleep?: (ms: number) => Promise<void>;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Reads the chain once a ledger until the authorization is used, or a read
|
|
109
|
+
* past its expiration ledger finds it unused, or the hard stop comes. Every
|
|
110
|
+
* read asks for the control too; one that does not return it ends the watch
|
|
111
|
+
* with no reading. Never throws: a failed read is an `unread` outcome.
|
|
112
|
+
*/
|
|
113
|
+
export declare function watchAuthorization(input: WatchInput): Promise<WatchOutcome>;
|
|
114
|
+
export interface RejectedRequestEvidence {
|
|
115
|
+
/** What the service answered the paid invalid request with. */
|
|
116
|
+
readonly status: number;
|
|
117
|
+
readonly reason?: string;
|
|
118
|
+
/** Base units, as signed: the advertised amount. */
|
|
119
|
+
readonly amount: bigint;
|
|
120
|
+
/** The expiration ledger, decoded from the signed authorization. */
|
|
121
|
+
readonly expiresAt: number;
|
|
122
|
+
/** "ledger". */
|
|
123
|
+
readonly unit: string;
|
|
124
|
+
readonly watch: WatchOutcome;
|
|
125
|
+
/** The payer's balance before signing and after the watch. Missing: not read. */
|
|
126
|
+
readonly balance?: {
|
|
127
|
+
readonly before: bigint;
|
|
128
|
+
readonly after: bigint;
|
|
129
|
+
};
|
|
130
|
+
/** Transfers of exactly `amount` from the payer to `payTo` recorded after the payment was sent. Missing: not read. */
|
|
131
|
+
readonly transfers?: readonly {
|
|
132
|
+
readonly reference: string;
|
|
133
|
+
readonly ledger: number;
|
|
134
|
+
}[];
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The settlement the chain shows for X402-11's payment, for the run ledger:
|
|
138
|
+
* the chain records its authorization as used, and the second readings agree
|
|
139
|
+
* (a transfer of the price from the payer to payTo, and the payer's balance
|
|
140
|
+
* down by at least it). The nonce read is a reading of this payment's own
|
|
141
|
+
* authorization, so the settlement counts as verified. The reference is the
|
|
142
|
+
* one transfer in the ledger the nonce was used in, or the only one the watch
|
|
143
|
+
* saw; when neither names one, undefined, and the ledger leaves it unverified.
|
|
144
|
+
*/
|
|
145
|
+
export declare function rejectedRequestSettlement(evidence: RejectedRequestEvidence): string | undefined;
|
|
146
|
+
/**
|
|
147
|
+
* X402-11's verdict from what the service answered and what the chain shows.
|
|
148
|
+
*
|
|
149
|
+
* - served (2xx): FAIL, whatever the chain shows; the chain only adds whether it settled.
|
|
150
|
+
* - refused, used, the second readings agree: FAIL, the payment was taken.
|
|
151
|
+
* - refused, unused past the expiry, the second readings agree: PASS.
|
|
152
|
+
* - refused and still valid at the hard stop: SKIP, "not settled by ledger N; still valid until ledger M".
|
|
153
|
+
* - anything else (a reading that disagrees or is missing, a read that failed): no verdict, never PASS.
|
|
154
|
+
*/
|
|
155
|
+
export declare function rejectedRequestVerdict(evidence: RejectedRequestEvidence): CheckResult;
|
|
156
|
+
export {};
|