@wasit-dev/core 0.7.0 → 0.8.0-beta.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/README.md +1 -1
- package/dist/baseline.d.ts +109 -0
- package/dist/baseline.js +351 -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 +391 -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 +135 -0
- package/dist/guard/budget.js +199 -0
- package/dist/guard/ci-event.d.ts +50 -0
- package/dist/guard/ci-event.js +125 -0
- package/dist/guard/mainnet-optin.d.ts +58 -0
- package/dist/guard/mainnet-optin.js +95 -0
- package/dist/guard/networks.d.ts +50 -0
- package/dist/guard/networks.js +80 -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/index.d.ts +31 -2
- package/dist/index.js +27 -1
- package/dist/mpp/channel-commitment.d.ts +48 -2
- package/dist/mpp/channel-commitment.js +80 -4
- package/dist/mpp/channel-credential.js +3 -0
- package/dist/mpp/channel.js +29 -0
- package/dist/mpp/charge.d.ts +2 -0
- package/dist/mpp/charge.js +17 -1
- package/dist/mpp/network.js +4 -1
- package/dist/mpp/suite.js +10 -2
- package/dist/report.d.ts +163 -0
- package/dist/report.js +282 -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/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 +125 -1
- package/dist/x402/chains/types.d.ts +68 -0
- package/dist/x402/plan.d.ts +82 -0
- package/dist/x402/plan.js +146 -0
- package/dist/x402/rejected-request.d.ts +146 -0
- package/dist/x402/rejected-request.js +269 -0
- package/dist/x402/requirements.d.ts +1 -0
- package/dist/x402/requirements.js +37 -4
- package/dist/x402/simulator.d.ts +52 -1
- package/dist/x402/simulator.js +452 -82
- package/package.json +1 -1
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The environment variables Wasit keeps away from the service it starts, and
|
|
3
|
+
* whose values it keeps out of every output.
|
|
4
|
+
*
|
|
5
|
+
* The names are fixed in code, not configurable (spec A1): a changed config must
|
|
6
|
+
* never be able to make a run read another key, or let one reach a service. They
|
|
7
|
+
* are the five variables the checks read their keys from, the one mainnet payer
|
|
8
|
+
* variable, and the whole `WASIT_` namespace, which covers `WASIT_HEADER_*`,
|
|
9
|
+
* `WASIT_RPC_URL` and anything added later.
|
|
10
|
+
*
|
|
11
|
+
* A deny-list, not an allow-list, on purpose: the service is the builder's own
|
|
12
|
+
* app and needs variables Wasit cannot list (PATH, HOME, NODE_OPTIONS, its own
|
|
13
|
+
* facilitator and database settings). The threat is Wasit's own variable names,
|
|
14
|
+
* and those are known. A key under a name Wasit does not know is not removed.
|
|
15
|
+
*/
|
|
16
|
+
/** The variables the checks read a key from. */
|
|
17
|
+
export declare const DENIED_ENV_NAMES: readonly string[];
|
|
18
|
+
/**
|
|
19
|
+
* True when a service must not see this variable.
|
|
20
|
+
*
|
|
21
|
+
* Compared in upper case: a Windows environment is case-insensitive, so a
|
|
22
|
+
* variable spelt `Stellar_Private_Key` is the same one there. Dropping a
|
|
23
|
+
* differently spelt variable on other systems costs nothing.
|
|
24
|
+
*/
|
|
25
|
+
export declare function isDeniedEnvName(name: string): boolean;
|
|
26
|
+
/** A copy of `env` without the denied variables and without unset ones. */
|
|
27
|
+
export declare function scrubEnv(env: Readonly<Record<string, string | undefined>>): Record<string, string>;
|
|
28
|
+
/**
|
|
29
|
+
* The values no output may carry: the keys, the header values and the RPC url
|
|
30
|
+
* (which often carries a key). Pass them to the sanitiser's `redact` option.
|
|
31
|
+
* Empty values are left out.
|
|
32
|
+
*/
|
|
33
|
+
export declare function deniedEnvValues(env: Readonly<Record<string, string | undefined>>): string[];
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The environment variables Wasit keeps away from the service it starts, and
|
|
3
|
+
* whose values it keeps out of every output.
|
|
4
|
+
*
|
|
5
|
+
* The names are fixed in code, not configurable (spec A1): a changed config must
|
|
6
|
+
* never be able to make a run read another key, or let one reach a service. They
|
|
7
|
+
* are the five variables the checks read their keys from, the one mainnet payer
|
|
8
|
+
* variable, and the whole `WASIT_` namespace, which covers `WASIT_HEADER_*`,
|
|
9
|
+
* `WASIT_RPC_URL` and anything added later.
|
|
10
|
+
*
|
|
11
|
+
* A deny-list, not an allow-list, on purpose: the service is the builder's own
|
|
12
|
+
* app and needs variables Wasit cannot list (PATH, HOME, NODE_OPTIONS, its own
|
|
13
|
+
* facilitator and database settings). The threat is Wasit's own variable names,
|
|
14
|
+
* and those are known. A key under a name Wasit does not know is not removed.
|
|
15
|
+
*/
|
|
16
|
+
/** The variables the checks read a key from. */
|
|
17
|
+
export const DENIED_ENV_NAMES = [
|
|
18
|
+
"STELLAR_PRIVATE_KEY",
|
|
19
|
+
"EVM_PRIVATE_KEY",
|
|
20
|
+
"SVM_PRIVATE_KEY",
|
|
21
|
+
"MPP_PAYER_SECRET",
|
|
22
|
+
"COMMITMENT_SECRET_HEX",
|
|
23
|
+
"WASIT_MAINNET_PAYER_SECRET",
|
|
24
|
+
];
|
|
25
|
+
/**
|
|
26
|
+
* The runner's own variables, kept from the service as well. The command files
|
|
27
|
+
* are read by the runner after the step: a service that writes `GITHUB_ENV` or
|
|
28
|
+
* `GITHUB_PATH` changes the environment or the PATH of the later steps, which
|
|
29
|
+
* hold the job's token. The tokens are the job's own credentials. This is
|
|
30
|
+
* hygiene, not isolation: the service runs as the job's user and can still look
|
|
31
|
+
* in the runner's directories. Not in {@link DENIED_ENV_NAMES}: their values are
|
|
32
|
+
* paths, not keys, and `GITHUB_STEP_SUMMARY` is what `wasit ci-report` writes to.
|
|
33
|
+
*/
|
|
34
|
+
const RUNNER_ENV_NAMES = [
|
|
35
|
+
"GITHUB_ENV",
|
|
36
|
+
"GITHUB_PATH",
|
|
37
|
+
"GITHUB_OUTPUT",
|
|
38
|
+
"GITHUB_STATE",
|
|
39
|
+
"GITHUB_STEP_SUMMARY",
|
|
40
|
+
"GH_TOKEN",
|
|
41
|
+
"GITHUB_TOKEN",
|
|
42
|
+
"ACTIONS_RUNTIME_TOKEN",
|
|
43
|
+
"ACTIONS_ID_TOKEN_REQUEST_TOKEN",
|
|
44
|
+
"ACTIONS_ID_TOKEN_REQUEST_URL",
|
|
45
|
+
];
|
|
46
|
+
/** Every variable in this namespace is Wasit's own. */
|
|
47
|
+
const WASIT_PREFIX = "WASIT_";
|
|
48
|
+
/** The variables whose value is a header Wasit sends or the RPC url, in the `WASIT_` namespace. */
|
|
49
|
+
const HEADER_PREFIX = "WASIT_HEADER_";
|
|
50
|
+
const RPC_VARIABLE = "WASIT_RPC_URL";
|
|
51
|
+
/**
|
|
52
|
+
* True when a service must not see this variable.
|
|
53
|
+
*
|
|
54
|
+
* Compared in upper case: a Windows environment is case-insensitive, so a
|
|
55
|
+
* variable spelt `Stellar_Private_Key` is the same one there. Dropping a
|
|
56
|
+
* differently spelt variable on other systems costs nothing.
|
|
57
|
+
*/
|
|
58
|
+
export function isDeniedEnvName(name) {
|
|
59
|
+
const upper = name.toUpperCase();
|
|
60
|
+
return DENIED_ENV_NAMES.includes(upper) || RUNNER_ENV_NAMES.includes(upper) || upper.startsWith(WASIT_PREFIX);
|
|
61
|
+
}
|
|
62
|
+
/** A copy of `env` without the denied variables and without unset ones. */
|
|
63
|
+
export function scrubEnv(env) {
|
|
64
|
+
const clean = {};
|
|
65
|
+
for (const [name, value] of Object.entries(env)) {
|
|
66
|
+
if (value !== undefined && !isDeniedEnvName(name))
|
|
67
|
+
clean[name] = value;
|
|
68
|
+
}
|
|
69
|
+
return clean;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The values no output may carry: the keys, the header values and the RPC url
|
|
73
|
+
* (which often carries a key). Pass them to the sanitiser's `redact` option.
|
|
74
|
+
* Empty values are left out.
|
|
75
|
+
*/
|
|
76
|
+
export function deniedEnvValues(env) {
|
|
77
|
+
const values = [];
|
|
78
|
+
for (const [name, value] of Object.entries(env)) {
|
|
79
|
+
if (value === undefined || value === "")
|
|
80
|
+
continue;
|
|
81
|
+
const upper = name.toUpperCase();
|
|
82
|
+
if (DENIED_ENV_NAMES.includes(upper) || upper.startsWith(HEADER_PREFIX) || upper === RPC_VARIABLE)
|
|
83
|
+
values.push(value);
|
|
84
|
+
}
|
|
85
|
+
return values;
|
|
86
|
+
}
|
package/dist/errors.d.ts
CHANGED
|
@@ -54,6 +54,14 @@ export declare class ConfigurationError extends Error {
|
|
|
54
54
|
export declare class CheckSetupError extends Error {
|
|
55
55
|
constructor(message: string);
|
|
56
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* The target answered with a redirect that Wasit will not follow, or will not
|
|
59
|
+
* follow again. Nothing is learned about the target's conformance, only that the
|
|
60
|
+
* route does not stay inside the origin the run was pointed at.
|
|
61
|
+
*/
|
|
62
|
+
export declare class RedirectRefusedError extends ConfigurationError {
|
|
63
|
+
constructor(message: string);
|
|
64
|
+
}
|
|
57
65
|
/** No HTTP conversation took place: the connection never succeeded. */
|
|
58
66
|
export declare class TargetUnreachableError extends Error {
|
|
59
67
|
readonly target: string;
|
|
@@ -70,6 +78,41 @@ export declare class TargetUnreachableError extends Error {
|
|
|
70
78
|
* under N identical copies.
|
|
71
79
|
*/
|
|
72
80
|
export declare function assertHttpUrl(target: string): void;
|
|
73
|
-
|
|
81
|
+
/** How `fetchTarget` treats a redirect. */
|
|
82
|
+
export interface FetchTargetOptions {
|
|
83
|
+
/**
|
|
84
|
+
* `"never"` (the default): one request, and a redirect answer is refused
|
|
85
|
+
* with a {@link RedirectRefusedError}; nothing is sent to its Location. Every
|
|
86
|
+
* request that carries a payment uses it (D-022c): `fetch` would keep a
|
|
87
|
+
* header such as `PAYMENT-SIGNATURE` on the way, and deliver the signed
|
|
88
|
+
* payment to a second URL that can settle it while the check grades the
|
|
89
|
+
* first. Being the default, a call that forgets to choose cannot leak one.
|
|
90
|
+
* `"same-origin"` is the rule for the read-only tier: a redirect inside the
|
|
91
|
+
* origin of `target` is followed once, anything else is refused and nothing
|
|
92
|
+
* is sent to the other origin.
|
|
93
|
+
*/
|
|
94
|
+
readonly redirects?: "never" | "same-origin";
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The unpaid request a payment check reads its fresh challenge from, and the
|
|
98
|
+
* URL the payment is then sent to (D-022c).
|
|
99
|
+
*
|
|
100
|
+
* One redirect inside the origin is followed, as the read-only tier does, but
|
|
101
|
+
* only when it keeps the request: a POST that a 301, 302 or 303 turns into a
|
|
102
|
+
* GET would have the payment signed for one request and sent with another. The
|
|
103
|
+
* payment goes to `url`, with redirects refused ({@link fetchTarget}'s default).
|
|
104
|
+
*/
|
|
105
|
+
export declare function fetchPaymentChallenge(target: string, init: RequestInit | undefined): Promise<{
|
|
106
|
+
readonly response: Response;
|
|
107
|
+
readonly url: string;
|
|
108
|
+
}>;
|
|
109
|
+
/**
|
|
110
|
+
* A `fetch` that never follows a redirect, for a payment client that makes
|
|
111
|
+
* its own requests (mppx): every request it makes refuses a redirect answer.
|
|
112
|
+
* MPP pays at the URL it read the challenge from, so a route that redirects
|
|
113
|
+
* gets no verdict rather than a payment delivered to a second URL.
|
|
114
|
+
*/
|
|
115
|
+
export declare const fetchWithoutRedirects: typeof fetch;
|
|
116
|
+
export declare function fetchTarget(target: string, init?: Parameters<typeof fetch>[1], options?: FetchTargetOptions): Promise<Awaited<ReturnType<typeof fetch>>>;
|
|
74
117
|
/** Maps a thrown value onto the taxonomy. */
|
|
75
118
|
export declare function classifyCheckError(error: unknown): ClassifiedError;
|
package/dist/errors.js
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
* documented error types, so sniffing at the catch site would be guesswork;
|
|
14
14
|
* the call site knows what it was doing and wraps accordingly.
|
|
15
15
|
*/
|
|
16
|
+
import { sanitizeText } from "./sanitize.js";
|
|
16
17
|
/**
|
|
17
18
|
* The target was reached and replied, but the reply violates the spec.
|
|
18
19
|
* This is a conformance finding, not an operational error.
|
|
@@ -48,6 +49,17 @@ export class CheckSetupError extends Error {
|
|
|
48
49
|
this.name = "CheckSetupError";
|
|
49
50
|
}
|
|
50
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* The target answered with a redirect that Wasit will not follow, or will not
|
|
54
|
+
* follow again. Nothing is learned about the target's conformance, only that the
|
|
55
|
+
* route does not stay inside the origin the run was pointed at.
|
|
56
|
+
*/
|
|
57
|
+
export class RedirectRefusedError extends ConfigurationError {
|
|
58
|
+
constructor(message) {
|
|
59
|
+
super(message);
|
|
60
|
+
this.name = "RedirectRefusedError";
|
|
61
|
+
}
|
|
62
|
+
}
|
|
51
63
|
/** No HTTP conversation took place: the connection never succeeded. */
|
|
52
64
|
export class TargetUnreachableError extends Error {
|
|
53
65
|
target;
|
|
@@ -140,14 +152,151 @@ function baseFetch() {
|
|
|
140
152
|
}
|
|
141
153
|
return current;
|
|
142
154
|
}
|
|
143
|
-
|
|
155
|
+
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
|
|
156
|
+
/** Request-body headers that must not outlive a redirect that drops the body. */
|
|
157
|
+
const BODY_HEADERS = ["content-type", "content-length", "content-encoding", "content-language", "content-location"];
|
|
158
|
+
/** The origin of a URL for a message: http(s) origins only, never a path, query or credentials. */
|
|
159
|
+
function describeOrigin(url) {
|
|
160
|
+
return url.protocol === "http:" || url.protocol === "https:" ? url.origin : `a ${url.protocol} URL`;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Where a response sends the request next, or undefined when it is not a
|
|
164
|
+
* redirect. A 3xx without a `Location` (or a 300 or 304) is not a redirect to
|
|
165
|
+
* follow: it is returned as the answer, and the check reports its status.
|
|
166
|
+
*/
|
|
167
|
+
function redirectLocation(response, from) {
|
|
168
|
+
const location = response.headers.get("location");
|
|
169
|
+
if (!REDIRECT_STATUSES.has(response.status) || location === null)
|
|
170
|
+
return undefined;
|
|
171
|
+
let next;
|
|
172
|
+
try {
|
|
173
|
+
next = new URL(location, from);
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
throw new RedirectRefusedError(`The route answers ${response.status} with a Location that is not a valid URL, so ` +
|
|
177
|
+
`Wasit did not follow it.`);
|
|
178
|
+
}
|
|
179
|
+
if (next.username !== "" || next.password !== "") {
|
|
180
|
+
throw new RedirectRefusedError(`The route redirects to a URL that carries credentials, so Wasit did not follow it.`);
|
|
181
|
+
}
|
|
182
|
+
return next;
|
|
183
|
+
}
|
|
184
|
+
/** Whether fetch would turn the request into a GET on this redirect: a POST on 301 to 303, any non-HEAD on 303. */
|
|
185
|
+
function redirectBecomesGet(init, status) {
|
|
186
|
+
const method = (init?.method ?? "GET").toUpperCase();
|
|
187
|
+
return ((status === 303 && method !== "GET" && method !== "HEAD") ||
|
|
188
|
+
((status === 301 || status === 302) && method === "POST"));
|
|
189
|
+
}
|
|
190
|
+
/** The request a redirect leads to: a POST on 301 to 303, and any non-HEAD on 303, becomes a GET without a body. */
|
|
191
|
+
function redirectedInit(init, status) {
|
|
192
|
+
if (!redirectBecomesGet(init, status))
|
|
193
|
+
return init ?? {};
|
|
194
|
+
const { body: _body, headers, ...rest } = init ?? {};
|
|
195
|
+
const kept = new Headers(headers);
|
|
196
|
+
for (const name of BODY_HEADERS)
|
|
197
|
+
kept.delete(name);
|
|
198
|
+
return { ...rest, method: "GET", headers: kept };
|
|
199
|
+
}
|
|
200
|
+
/** One request, never following a redirect, with the same "never connected" wrapping as `fetchTarget`. */
|
|
201
|
+
async function fetchOnce(target, init) {
|
|
202
|
+
try {
|
|
203
|
+
return await baseFetch()(target, { ...init, redirect: "manual" });
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
throw new TargetUnreachableError(target, error);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/** Frees the connection of a response whose body nobody will read. */
|
|
210
|
+
async function discard(response) {
|
|
211
|
+
try {
|
|
212
|
+
await response.body?.cancel();
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
// The connection is closed either way.
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The same-origin rule: follow one redirect inside the origin, refuse any other.
|
|
220
|
+
*
|
|
221
|
+
* A second redirect is refused even when it stays inside the origin: a chain
|
|
222
|
+
* is not something a paywall needs, and a loop must end.
|
|
223
|
+
*/
|
|
224
|
+
async function followSameOrigin(target, init, keepRequest) {
|
|
225
|
+
const from = new URL(target);
|
|
226
|
+
const first = await fetchOnce(target, init);
|
|
227
|
+
const next = redirectLocation(first, from);
|
|
228
|
+
if (next === undefined)
|
|
229
|
+
return { response: first, url: target };
|
|
230
|
+
await discard(first);
|
|
231
|
+
if (next.origin !== from.origin || (next.protocol !== "http:" && next.protocol !== "https:")) {
|
|
232
|
+
throw new RedirectRefusedError(`The route redirects to ${sanitizeText(describeOrigin(next))}, which is not the origin ` +
|
|
233
|
+
`${sanitizeText(from.origin)} this run was pointed at. Wasit did not follow it. ` +
|
|
234
|
+
`Point the run at the URL the redirect leads to, if that is the service you operate.`);
|
|
235
|
+
}
|
|
236
|
+
if (keepRequest && redirectBecomesGet(init, first.status)) {
|
|
237
|
+
throw new RedirectRefusedError(`The route answers ${first.status} with a redirect that turns this ` +
|
|
238
|
+
`${(init?.method ?? "GET").toUpperCase()} into a GET. Wasit pays only where the request ` +
|
|
239
|
+
`arrives unchanged, so it did not follow it and nothing was paid. Point the run at the ` +
|
|
240
|
+
`URL the route answers on.`);
|
|
241
|
+
}
|
|
242
|
+
const second = await fetchOnce(next.href, redirectedInit(init, first.status));
|
|
243
|
+
const again = redirectLocation(second, next);
|
|
244
|
+
if (again === undefined)
|
|
245
|
+
return { response: second, url: next.href };
|
|
246
|
+
await discard(second);
|
|
247
|
+
throw new RedirectRefusedError(again.origin === from.origin
|
|
248
|
+
? `The route answers with more than one redirect (${first.status}, then ${second.status}). ` +
|
|
249
|
+
`Wasit follows one redirect inside the origin and did not follow the second.`
|
|
250
|
+
: `The route redirects to ${sanitizeText(describeOrigin(again))} after one redirect inside ` +
|
|
251
|
+
`${sanitizeText(from.origin)}. Wasit did not follow it.`);
|
|
252
|
+
}
|
|
253
|
+
/** Refuses `response` when it is a redirect, after freeing its connection; returns it otherwise. */
|
|
254
|
+
async function refuseRedirect(response, from) {
|
|
255
|
+
const next = redirectLocation(response, from);
|
|
256
|
+
if (next === undefined)
|
|
257
|
+
return response;
|
|
258
|
+
await discard(response);
|
|
259
|
+
throw new RedirectRefusedError(`The route answers ${response.status} with a redirect to ` +
|
|
260
|
+
`${sanitizeText(describeOrigin(next))}. Wasit does not follow a redirect on a request that ` +
|
|
261
|
+
`pays, so nothing was sent there and the payment was not sent again. Point the run at the ` +
|
|
262
|
+
`URL the route answers on.`);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* The unpaid request a payment check reads its fresh challenge from, and the
|
|
266
|
+
* URL the payment is then sent to (D-022c).
|
|
267
|
+
*
|
|
268
|
+
* One redirect inside the origin is followed, as the read-only tier does, but
|
|
269
|
+
* only when it keeps the request: a POST that a 301, 302 or 303 turns into a
|
|
270
|
+
* GET would have the payment signed for one request and sent with another. The
|
|
271
|
+
* payment goes to `url`, with redirects refused ({@link fetchTarget}'s default).
|
|
272
|
+
*/
|
|
273
|
+
export async function fetchPaymentChallenge(target, init) {
|
|
144
274
|
assertHttpUrl(target);
|
|
275
|
+
return followSameOrigin(target, init, true);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* A `fetch` that never follows a redirect, for a payment client that makes
|
|
279
|
+
* its own requests (mppx): every request it makes refuses a redirect answer.
|
|
280
|
+
* MPP pays at the URL it read the challenge from, so a route that redirects
|
|
281
|
+
* gets no verdict rather than a payment delivered to a second URL.
|
|
282
|
+
*/
|
|
283
|
+
export const fetchWithoutRedirects = async (input, init) => {
|
|
284
|
+
const target = input instanceof Request ? input.url : input instanceof URL ? input.href : String(input);
|
|
285
|
+
assertHttpUrl(target);
|
|
286
|
+
let response;
|
|
145
287
|
try {
|
|
146
|
-
|
|
288
|
+
response = await baseFetch()(input, { ...init, redirect: "manual" });
|
|
147
289
|
}
|
|
148
290
|
catch (error) {
|
|
149
291
|
throw new TargetUnreachableError(target, error);
|
|
150
292
|
}
|
|
293
|
+
return refuseRedirect(response, new URL(target));
|
|
294
|
+
};
|
|
295
|
+
export async function fetchTarget(target, init, options) {
|
|
296
|
+
assertHttpUrl(target);
|
|
297
|
+
if (options?.redirects === "same-origin")
|
|
298
|
+
return (await followSameOrigin(target, init, false)).response;
|
|
299
|
+
return refuseRedirect(await fetchOnce(target, init), new URL(target));
|
|
151
300
|
}
|
|
152
301
|
/** Maps a thrown value onto the taxonomy. */
|
|
153
302
|
export function classifyCheckError(error) {
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The caps of spec B4, in code: at most $1 a payment and $5 a run, the run's
|
|
3
|
+
* total charged BEFORE each signature and never credited back (B3, B-A08).
|
|
4
|
+
*
|
|
5
|
+
* A cap in dollars needs an asset whose dollar value is known. Wasit pays only
|
|
6
|
+
* in USDC (the SDK client's default assets, plus Circle's USDC on Ethereum
|
|
7
|
+
* Sepolia), so the table below lists USDC on each test network with its
|
|
8
|
+
* decimals, and a payment in any other asset is refused: Wasit cannot hold it
|
|
9
|
+
* to the cap. Nothing on mainnet is listed: this version pays on test networks
|
|
10
|
+
* only, and the mainnet USDC constant waits for the opt-in (B4, B12).
|
|
11
|
+
*
|
|
12
|
+
* Amounts in dollars are bigints at 10^-18 of a dollar, so 6 and 7 decimal
|
|
13
|
+
* tokens add up exactly.
|
|
14
|
+
*
|
|
15
|
+
* Pure: no network, no key.
|
|
16
|
+
*/
|
|
17
|
+
import { CheckSetupError } from "../errors.js";
|
|
18
|
+
/** At most this many dollars in one payment. Fixed; nothing raises it. */
|
|
19
|
+
export declare const PER_PAYMENT_CAP_USD = 1;
|
|
20
|
+
/** At most this many dollars signed in one run. A config may only lower it (`maxRunUsd`). */
|
|
21
|
+
export declare const PER_RUN_CAP_USD = 5;
|
|
22
|
+
export interface UsdAsset {
|
|
23
|
+
readonly network: string;
|
|
24
|
+
readonly asset: string;
|
|
25
|
+
readonly decimals: number;
|
|
26
|
+
readonly symbol: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* USDC on each test network. Sources (read 2026-11-16): `@x402/stellar` 2.28.0
|
|
30
|
+
* `USDC_TESTNET_ADDRESS`, `DEFAULT_TOKEN_DECIMALS = 7`; `@x402/evm` 2.28.0 default
|
|
31
|
+
* asset of `eip155:84532`, decimals 6; Circle's USDC on Ethereum Sepolia as
|
|
32
|
+
* `chains/evm.ts` (`ETHEREUM_SEPOLIA_USDC`, 6 decimals read from the contract
|
|
33
|
+
* 2026-10-05); `@x402/svm` 2.28.0 `USDC_DEVNET_ADDRESS`, decimals 6.
|
|
34
|
+
*/
|
|
35
|
+
export declare const USD_ASSETS: readonly UsdAsset[];
|
|
36
|
+
/** Whether two addresses on `network` are the same: an EVM address in any letter case, others exactly. */
|
|
37
|
+
export declare function sameAddress(network: string, a: string, b: string): boolean;
|
|
38
|
+
/** The dollar asset `asset` is on `network`, if Wasit knows it. */
|
|
39
|
+
export declare function usdAsset(network: string, asset: string): UsdAsset | undefined;
|
|
40
|
+
/** Whole and fractional dollars, rounded down to a millionth: a cap is never rounded up. */
|
|
41
|
+
export declare function usdFromDollars(dollars: number): bigint;
|
|
42
|
+
/** "$4.50", "$0.0000001": exact, at least two decimals. */
|
|
43
|
+
export declare function formatUsd(amount: bigint): string;
|
|
44
|
+
export type GuardRefusal = "price" | "asset" | "amount" | "pin" | "plan" | "run-cap" | "accepted";
|
|
45
|
+
/**
|
|
46
|
+
* A payment the guard will not sign, or, with `signed`, a signed payment it
|
|
47
|
+
* will not send. A setup error: it says nothing about the target, and the
|
|
48
|
+
* check that hit it has no verdict.
|
|
49
|
+
*/
|
|
50
|
+
export declare class PaymentGuardRefusedError extends CheckSetupError {
|
|
51
|
+
readonly reason: GuardRefusal;
|
|
52
|
+
constructor(reason: GuardRefusal, message: string, options?: {
|
|
53
|
+
readonly signed?: boolean;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/** A value the target chose, for a message: one line, no control characters, bounded. */
|
|
57
|
+
export declare function shown(value: unknown): string;
|
|
58
|
+
export declare function parseAmount(amount: unknown): bigint;
|
|
59
|
+
export interface PinTerms {
|
|
60
|
+
readonly payTo: string;
|
|
61
|
+
readonly asset: string;
|
|
62
|
+
}
|
|
63
|
+
interface OptionTerms {
|
|
64
|
+
readonly scheme: string;
|
|
65
|
+
readonly network: string;
|
|
66
|
+
readonly payTo: string;
|
|
67
|
+
readonly asset: string;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Refuses unless EVERY `exact` option on `network` pays the pinned `payTo` in
|
|
71
|
+
* the pinned asset (B-A02): the SDK picks among the options by its own rules,
|
|
72
|
+
* so a check of the first one alone would not cover what it signs. Options on
|
|
73
|
+
* other networks or schemes are never signed and are not judged.
|
|
74
|
+
*/
|
|
75
|
+
export declare function assertPinned(network: string, accepts: readonly OptionTerms[], pin: PinTerms | undefined): void;
|
|
76
|
+
/** One signature a run makes, and its amount in base units. */
|
|
77
|
+
export interface PlannedSignature {
|
|
78
|
+
readonly check: string;
|
|
79
|
+
readonly amount: bigint;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* What the payment checks sign for an endpoint priced `price` (B0 fact 8):
|
|
83
|
+
* X402-06 the price (settles), X402-07 the price (signature then corrupted),
|
|
84
|
+
* X402-09 half the price, rounded down (none when there is no smaller amount),
|
|
85
|
+
* X402-10 the price with a short lifetime. X402-08 resends X402-06 and signs
|
|
86
|
+
* nothing. 3.5 times the price. With `rejectedRequest`, X402-11 the price as
|
|
87
|
+
* well (a valid payment sent with a request the service must refuse): 4.5 times.
|
|
88
|
+
*/
|
|
89
|
+
export declare function plannedSignatures(price: bigint, options?: {
|
|
90
|
+
readonly rejectedRequest?: boolean;
|
|
91
|
+
}): PlannedSignature[];
|
|
92
|
+
export interface BudgetOptions {
|
|
93
|
+
/** A lower per-run cap in dollars; a value above {@link PER_RUN_CAP_USD} is read as it. */
|
|
94
|
+
readonly maxRunUsd?: number;
|
|
95
|
+
}
|
|
96
|
+
export interface Charge {
|
|
97
|
+
readonly check: string;
|
|
98
|
+
readonly network: string;
|
|
99
|
+
readonly asset: string;
|
|
100
|
+
/** Base units, as the payment states them. */
|
|
101
|
+
readonly amount: string;
|
|
102
|
+
/** What the plan allowed for this check, in base units, when the run has a plan. */
|
|
103
|
+
readonly planned?: bigint;
|
|
104
|
+
}
|
|
105
|
+
export interface ChargedEntry {
|
|
106
|
+
readonly check: string;
|
|
107
|
+
readonly network: string;
|
|
108
|
+
readonly asset: string;
|
|
109
|
+
readonly amount: bigint;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The dollars one run may sign. Every signature is charged here first; a
|
|
113
|
+
* charge that does not fit is refused and nothing is signed. Nothing is ever
|
|
114
|
+
* credited back: a payment that failed after it was signed may still settle.
|
|
115
|
+
* Once the cap refuses a payment, the budget is spent: every later charge is
|
|
116
|
+
* refused too, so a run never signs a smaller payment after a larger one did
|
|
117
|
+
* not fit.
|
|
118
|
+
*/
|
|
119
|
+
export declare class RunBudget {
|
|
120
|
+
#private;
|
|
121
|
+
constructor(options?: BudgetOptions);
|
|
122
|
+
/** The run's cap, in dollar units. */
|
|
123
|
+
get capUsd(): bigint;
|
|
124
|
+
/** What the run has charged so far, in dollar units. */
|
|
125
|
+
get spentUsd(): bigint;
|
|
126
|
+
get entries(): readonly ChargedEntry[];
|
|
127
|
+
/**
|
|
128
|
+
* Charges one payment, or refuses it. Called before the signature, with the
|
|
129
|
+
* amount that will be signed.
|
|
130
|
+
*/
|
|
131
|
+
charge(charge: Charge): void;
|
|
132
|
+
/** The dollar value of `amount` base units of `asset` on `network`, if Wasit can value it. */
|
|
133
|
+
static valueOf(network: string, asset: string, amount: bigint): bigint | undefined;
|
|
134
|
+
}
|
|
135
|
+
export {};
|