@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,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one sanitiser. Everything Wasit prints, writes into a report, or puts in
|
|
3
|
+
* a pull request comment or job summary passes through here when it can carry
|
|
4
|
+
* text a service chose.
|
|
5
|
+
*
|
|
6
|
+
* A service answers with whatever it likes, and a pull request can change what
|
|
7
|
+
* that is. Two sinks are dangerous:
|
|
8
|
+
*
|
|
9
|
+
* - a terminal or a CI job log: control characters move the cursor or hide
|
|
10
|
+
* text, and a line that starts with `::` is a workflow command to the
|
|
11
|
+
* GitHub runner (set an output, add a mask, stop all commands);
|
|
12
|
+
* - a Markdown comment: HTML, links, mentions and table syntax change what the
|
|
13
|
+
* reader sees and who gets notified.
|
|
14
|
+
*
|
|
15
|
+
* `sanitizeText` is the plain level for the first, `sanitizeForMarkdown` adds
|
|
16
|
+
* the second. Neither is a security boundary on its own against a renderer
|
|
17
|
+
* Wasit does not control; they make the common attacks inert and are tested
|
|
18
|
+
* against the CommonMark rules, not against GitHub's renderer.
|
|
19
|
+
*/
|
|
20
|
+
/** The longest a service-supplied value may be in a comment or summary. */
|
|
21
|
+
export declare const MARKDOWN_VALUE_MAX = 300;
|
|
22
|
+
export interface SanitizeOptions {
|
|
23
|
+
/**
|
|
24
|
+
* Values that must never appear in the output, such as the value of a
|
|
25
|
+
* `WASIT_HEADER_*` variable or a payer key. Matched as literals, after
|
|
26
|
+
* control characters are stripped, so a control character cannot hide one.
|
|
27
|
+
*/
|
|
28
|
+
readonly redact?: readonly string[];
|
|
29
|
+
}
|
|
30
|
+
export interface MarkdownOptions extends SanitizeOptions {
|
|
31
|
+
/** Longest value, in characters, ellipsis included. Default {@link MARKDOWN_VALUE_MAX}. */
|
|
32
|
+
readonly maxLength?: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Plain level, for the terminal, the job log and report text.
|
|
36
|
+
*
|
|
37
|
+
* Strips control characters (keeping newline and tab), masks secrets, and
|
|
38
|
+
* breaks a `::` or `##[` at the start of a line by putting a space inside it.
|
|
39
|
+
* Idempotent. Newlines stay: this level does not reflow text.
|
|
40
|
+
*/
|
|
41
|
+
export declare function sanitizeText(input: string, options?: SanitizeOptions): string;
|
|
42
|
+
/**
|
|
43
|
+
* Markdown level, for a value placed in a pull request comment or job summary.
|
|
44
|
+
*
|
|
45
|
+
* Returns one line: the plain level first, then whitespace (newlines too)
|
|
46
|
+
* collapsed so the value cannot open a heading, list or table row; links,
|
|
47
|
+
* issue references and mentions removed; the value cut to `maxLength`
|
|
48
|
+
* characters (300 by default); then Markdown and HTML escaped. The cut comes
|
|
49
|
+
* before the escaping, so an escape or an entity is never split. Not
|
|
50
|
+
* idempotent: apply it once, to text that has only had the plain level.
|
|
51
|
+
*/
|
|
52
|
+
export declare function sanitizeForMarkdown(input: string, options?: MarkdownOptions): string;
|
package/dist/sanitize.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one sanitiser. Everything Wasit prints, writes into a report, or puts in
|
|
3
|
+
* a pull request comment or job summary passes through here when it can carry
|
|
4
|
+
* text a service chose.
|
|
5
|
+
*
|
|
6
|
+
* A service answers with whatever it likes, and a pull request can change what
|
|
7
|
+
* that is. Two sinks are dangerous:
|
|
8
|
+
*
|
|
9
|
+
* - a terminal or a CI job log: control characters move the cursor or hide
|
|
10
|
+
* text, and a line that starts with `::` is a workflow command to the
|
|
11
|
+
* GitHub runner (set an output, add a mask, stop all commands);
|
|
12
|
+
* - a Markdown comment: HTML, links, mentions and table syntax change what the
|
|
13
|
+
* reader sees and who gets notified.
|
|
14
|
+
*
|
|
15
|
+
* `sanitizeText` is the plain level for the first, `sanitizeForMarkdown` adds
|
|
16
|
+
* the second. Neither is a security boundary on its own against a renderer
|
|
17
|
+
* Wasit does not control; they make the common attacks inert and are tested
|
|
18
|
+
* against the CommonMark rules, not against GitHub's renderer.
|
|
19
|
+
*/
|
|
20
|
+
/** The longest a service-supplied value may be in a comment or summary. */
|
|
21
|
+
export const MARKDOWN_VALUE_MAX = 300;
|
|
22
|
+
/** What replaces a secret, or a value that looks like one. */
|
|
23
|
+
const REDACTED = "[redacted]";
|
|
24
|
+
/** A redact entry shorter than this would blank ordinary words, and is not a secret. */
|
|
25
|
+
const MIN_REDACT_LENGTH = 4;
|
|
26
|
+
/**
|
|
27
|
+
* C0 controls except tab (U+0009) and newline (U+000A), DEL, C1 controls, and
|
|
28
|
+
* the bidirectional overrides that make text read in another order than it is
|
|
29
|
+
* stored. A carriage return is stripped too: .NET readers, the GitHub runner
|
|
30
|
+
* among them, split lines on it, so it could start a line of its own.
|
|
31
|
+
*/
|
|
32
|
+
const CONTROL_CHARACTERS = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f--]/g;
|
|
33
|
+
/** The shape of a Stellar secret key. Public keys, contract ids and hashes do not match. */
|
|
34
|
+
const STELLAR_SECRET = /S[A-Z2-7]{55}/g;
|
|
35
|
+
/**
|
|
36
|
+
* A line the GitHub runner reads as a command: `::name::`, or the older
|
|
37
|
+
* `##[name]`. The runner trims leading whitespace first, and .NET counts more
|
|
38
|
+
* characters as whitespace than a space and a tab, so any whitespace but the
|
|
39
|
+
* line break is allowed before it.
|
|
40
|
+
*/
|
|
41
|
+
const LINE_START_COMMAND = /^([^\S\n]*)(::|##\[)/gm;
|
|
42
|
+
/**
|
|
43
|
+
* Plain level, for the terminal, the job log and report text.
|
|
44
|
+
*
|
|
45
|
+
* Strips control characters (keeping newline and tab), masks secrets, and
|
|
46
|
+
* breaks a `::` or `##[` at the start of a line by putting a space inside it.
|
|
47
|
+
* Idempotent. Newlines stay: this level does not reflow text.
|
|
48
|
+
*/
|
|
49
|
+
export function sanitizeText(input, options = {}) {
|
|
50
|
+
let text = String(input).replace(CONTROL_CHARACTERS, "");
|
|
51
|
+
const secrets = [...(options.redact ?? [])]
|
|
52
|
+
.filter((secret) => secret.length >= MIN_REDACT_LENGTH)
|
|
53
|
+
.sort((a, b) => b.length - a.length);
|
|
54
|
+
for (const secret of secrets)
|
|
55
|
+
text = text.split(secret).join(REDACTED);
|
|
56
|
+
return text
|
|
57
|
+
.replace(STELLAR_SECRET, REDACTED)
|
|
58
|
+
.replace(LINE_START_COMMAND, (_match, space, command) => space + (command === "::" ? ": :" : "# #["));
|
|
59
|
+
}
|
|
60
|
+
/** A link a Markdown renderer would make clickable: a scheme with an authority, `www.`, mailto and xmpp. */
|
|
61
|
+
const LINK = /\b[a-z][a-z0-9+.-]*:\/\/\S*|\b(?:www\.|mailto:|xmpp:)\S*/gi;
|
|
62
|
+
/** `#123` and `owner/repo#123`: GitHub turns both into links, the second one on another repository. */
|
|
63
|
+
const ISSUE_REFERENCE = /(?:[\w.-]+\/[\w.-]+)?#\d+/g;
|
|
64
|
+
/** An `@` that GitHub would read as the start of a mention. */
|
|
65
|
+
const MENTION = /@(?=[A-Za-z0-9_])/g;
|
|
66
|
+
const LINK_REMOVED = "[link removed]";
|
|
67
|
+
/** Cuts to `max` characters, counting code points, with an ellipsis in the last place. */
|
|
68
|
+
function truncate(text, max) {
|
|
69
|
+
const characters = Array.from(text);
|
|
70
|
+
if (characters.length <= max)
|
|
71
|
+
return text;
|
|
72
|
+
return `${characters.slice(0, Math.max(0, max - 1)).join("")}…`;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Backslash-escapes what could still format text inside a cell or a line, and
|
|
76
|
+
* HTML-escapes the rest. Newlines are already gone, so only the start of the
|
|
77
|
+
* string can open a heading, a list or a block quote.
|
|
78
|
+
*/
|
|
79
|
+
function escapeMarkdown(text) {
|
|
80
|
+
return text
|
|
81
|
+
.replace(/[\\`*_~[\]()|]/g, "\\$&")
|
|
82
|
+
.replace(/^([-+=#])/, "\\$1")
|
|
83
|
+
.replace(/^(\d+)\./, "$1\\.")
|
|
84
|
+
.replace(/&/g, "&")
|
|
85
|
+
.replace(/</g, "<")
|
|
86
|
+
.replace(/>/g, ">");
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Markdown level, for a value placed in a pull request comment or job summary.
|
|
90
|
+
*
|
|
91
|
+
* Returns one line: the plain level first, then whitespace (newlines too)
|
|
92
|
+
* collapsed so the value cannot open a heading, list or table row; links,
|
|
93
|
+
* issue references and mentions removed; the value cut to `maxLength`
|
|
94
|
+
* characters (300 by default); then Markdown and HTML escaped. The cut comes
|
|
95
|
+
* before the escaping, so an escape or an entity is never split. Not
|
|
96
|
+
* idempotent: apply it once, to text that has only had the plain level.
|
|
97
|
+
*/
|
|
98
|
+
export function sanitizeForMarkdown(input, options = {}) {
|
|
99
|
+
const max = Math.max(1, Math.floor(options.maxLength ?? MARKDOWN_VALUE_MAX));
|
|
100
|
+
const plain = sanitizeText(input, options).replace(/\s+/g, " ").trim();
|
|
101
|
+
const stripped = plain
|
|
102
|
+
.replace(LINK, LINK_REMOVED)
|
|
103
|
+
.replace(ISSUE_REFERENCE, LINK_REMOVED)
|
|
104
|
+
.replace(MENTION, "[at]");
|
|
105
|
+
return escapeMarkdown(truncate(stripped, max));
|
|
106
|
+
}
|
package/dist/settlement.d.ts
CHANGED
|
@@ -22,6 +22,20 @@ export interface TransferEvent {
|
|
|
22
22
|
readonly amount: bigint;
|
|
23
23
|
readonly contract: string;
|
|
24
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Reads the amount, and the destination's muxed id if any, from a `transfer`
|
|
27
|
+
* event's data.
|
|
28
|
+
*
|
|
29
|
+
* CAP-46 made the data a bare `i128` amount. CAP-67 keeps that when no muxed
|
|
30
|
+
* information is emitted, and otherwise makes it a map
|
|
31
|
+
* `{ amount: i128, to_muxed_id: u64 | bytes | string }`. Reading only the bare
|
|
32
|
+
* form would drop every payment to a muxed address, and report a settlement
|
|
33
|
+
* that happened as one that did not.
|
|
34
|
+
*/
|
|
35
|
+
export declare function readTransferData(data: xdr.ScVal): {
|
|
36
|
+
amount: bigint;
|
|
37
|
+
toMuxedId?: string;
|
|
38
|
+
} | undefined;
|
|
25
39
|
/**
|
|
26
40
|
* Extracts CAP-46 `transfer` events from a settled transaction.
|
|
27
41
|
*
|
package/dist/settlement.js
CHANGED
|
@@ -36,7 +36,7 @@ function asRecord(value) {
|
|
|
36
36
|
* form would drop every payment to a muxed address, and report a settlement
|
|
37
37
|
* that happened as one that did not.
|
|
38
38
|
*/
|
|
39
|
-
function readTransferData(data) {
|
|
39
|
+
export function readTransferData(data) {
|
|
40
40
|
const native = scValToNative(data);
|
|
41
41
|
if (typeof native === "bigint")
|
|
42
42
|
return { amount: native };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The exit code of a run of several endpoints (spec A2 with A8 #3, D-021).
|
|
3
|
+
*
|
|
4
|
+
* - 1: an endpoint is non-conformant, or an endpoint that was graded on the
|
|
5
|
+
* default branch is not graded now (the baseline says so). A service that
|
|
6
|
+
* drops below the standard of its chain must not be green.
|
|
7
|
+
* - 2: no verdict. Nothing could be graded, a check could not run, or payment
|
|
8
|
+
* checks were asked for and none could be graded on an endpoint's network. A
|
|
9
|
+
* service that was never graded is here: not green, and not labelled FAIL.
|
|
10
|
+
* - 0: at least one endpoint was graded, none failed, no check errored.
|
|
11
|
+
*
|
|
12
|
+
* The reason carries the counts ("0 graded, 1 unsupported"), so a green run
|
|
13
|
+
* cannot be read as a pass of something that was not graded.
|
|
14
|
+
*/
|
|
15
|
+
import { type Regression } from "./baseline.js";
|
|
16
|
+
import { type RunReport } from "./report.js";
|
|
17
|
+
export interface RunVerdict {
|
|
18
|
+
readonly code: 0 | 1 | 2;
|
|
19
|
+
/** One line, for the terminal, the job summary and the report notes. */
|
|
20
|
+
readonly reason: string;
|
|
21
|
+
/** Endpoints that were graded on the baseline and are not now. Empty without a baseline. */
|
|
22
|
+
readonly regressions: readonly Regression[];
|
|
23
|
+
}
|
|
24
|
+
export declare function runExitCode(report: RunReport, baseline?: RunReport): RunVerdict;
|
package/dist/verdict.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The exit code of a run of several endpoints (spec A2 with A8 #3, D-021).
|
|
3
|
+
*
|
|
4
|
+
* - 1: an endpoint is non-conformant, or an endpoint that was graded on the
|
|
5
|
+
* default branch is not graded now (the baseline says so). A service that
|
|
6
|
+
* drops below the standard of its chain must not be green.
|
|
7
|
+
* - 2: no verdict. Nothing could be graded, a check could not run, or payment
|
|
8
|
+
* checks were asked for and none could be graded on an endpoint's network. A
|
|
9
|
+
* service that was never graded is here: not green, and not labelled FAIL.
|
|
10
|
+
* - 0: at least one endpoint was graded, none failed, no check errored.
|
|
11
|
+
*
|
|
12
|
+
* The reason carries the counts ("0 graded, 1 unsupported"), so a green run
|
|
13
|
+
* cannot be read as a pass of something that was not graded.
|
|
14
|
+
*/
|
|
15
|
+
import { findRegressions } from "./baseline.js";
|
|
16
|
+
import { describeTotals, paymentGroupUnsupported } from "./report.js";
|
|
17
|
+
export function runExitCode(report, baseline) {
|
|
18
|
+
const regressions = baseline === undefined ? [] : findRegressions(report, baseline);
|
|
19
|
+
const counts = describeTotals(report.totals);
|
|
20
|
+
const errored = report.endpoints.reduce((total, endpoint) => total + endpoint.results.filter((result) => result.status === "ERROR").length, 0);
|
|
21
|
+
if (report.totals.nonConformant > 0) {
|
|
22
|
+
const n = report.totals.nonConformant;
|
|
23
|
+
return { code: 1, reason: `${n} endpoint${n === 1 ? " is" : "s are"} non-conformant (${counts})`, regressions };
|
|
24
|
+
}
|
|
25
|
+
if (regressions.length > 0) {
|
|
26
|
+
const list = regressions.map((item) => `${item.endpoint} (${item.was} on main, now ${item.now})`).join(", ");
|
|
27
|
+
return { code: 1, reason: `regression against main: ${list} (${counts})`, regressions };
|
|
28
|
+
}
|
|
29
|
+
if (report.totals.graded === 0) {
|
|
30
|
+
return { code: 2, reason: `nothing could be graded: ${counts}`, regressions };
|
|
31
|
+
}
|
|
32
|
+
if (errored > 0) {
|
|
33
|
+
return { code: 2, reason: `${errored} check${errored === 1 ? "" : "s"} could not run (${counts})`, regressions };
|
|
34
|
+
}
|
|
35
|
+
// Payment checks were asked for and none could be graded on an endpoint's network (Najmi, 2026-11-15).
|
|
36
|
+
const unsupportedPayment = report.endpoints.filter((endpoint) => paymentGroupUnsupported(endpoint.results, endpoint.protocol)).length;
|
|
37
|
+
if (unsupportedPayment > 0) {
|
|
38
|
+
return {
|
|
39
|
+
code: 2,
|
|
40
|
+
reason: `payment checks were asked for and could not be graded on ${unsupportedPayment} ` +
|
|
41
|
+
`endpoint${unsupportedPayment === 1 ? "" : "s"} (${counts})`,
|
|
42
|
+
regressions,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return { code: 0, reason: counts, regressions };
|
|
46
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The versions a result names: Wasit core and the CLI, the x402 version Wasit
|
|
3
|
+
* grades, and the SDK packages the checks run through.
|
|
4
|
+
*
|
|
5
|
+
* Read at run time from the installed packages' own `package.json`, by path,
|
|
6
|
+
* never typed by hand: a constant drifts from what is installed, and the point
|
|
7
|
+
* of printing a version is that it is the one that ran. They are read by path
|
|
8
|
+
* and not through `require.resolve("<pkg>/package.json")` because the SDK
|
|
9
|
+
* packages' export maps do not list it (checked on 2026-11-15 for all five:
|
|
10
|
+
* ERR_PACKAGE_PATH_NOT_EXPORTED).
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* The installed version of `name`, found the way Node finds a dependency: in
|
|
14
|
+
* `node_modules` of `from` or of any directory above it.
|
|
15
|
+
*
|
|
16
|
+
* Returns "unknown" when the package is not there, its name does not match, or
|
|
17
|
+
* its version is not a version.
|
|
18
|
+
*/
|
|
19
|
+
export declare function readPackageVersion(name: string, from: string): string;
|
|
20
|
+
/** True for exactly seven lower-case hex characters: the only build text that reaches a comment. */
|
|
21
|
+
export declare function isShortCommit(value: unknown): value is string;
|
|
22
|
+
/**
|
|
23
|
+
* The commit this build of Wasit was made from, when the Action says so: the
|
|
24
|
+
* action ref (`WASIT_BUILD_COMMIT`) if it is a full commit id. A tag or a branch
|
|
25
|
+
* name is not a commit, and says nothing here: a pinned tag already names the
|
|
26
|
+
* version, and a result must not name a build it cannot show.
|
|
27
|
+
*/
|
|
28
|
+
export declare function readBuildCommit(env: Readonly<Record<string, string | undefined>>): string | undefined;
|
|
29
|
+
export interface ToolVersions {
|
|
30
|
+
readonly wasit: {
|
|
31
|
+
readonly cli: string;
|
|
32
|
+
readonly core: string;
|
|
33
|
+
/** The first seven characters of the commit an Action build came from. Absent for an installed package. */
|
|
34
|
+
readonly build?: string;
|
|
35
|
+
};
|
|
36
|
+
readonly sdk: {
|
|
37
|
+
/** The x402 version Wasit grades. */
|
|
38
|
+
readonly x402Version: 2;
|
|
39
|
+
readonly "@x402/core": string;
|
|
40
|
+
readonly "@x402/stellar": string;
|
|
41
|
+
readonly "@stellar/mpp": string;
|
|
42
|
+
readonly mppx: string;
|
|
43
|
+
readonly "@stellar/stellar-sdk": string;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The versions of this run. The CLI knows its own version (core cannot read the
|
|
48
|
+
* CLI's `package.json`), so it passes it in.
|
|
49
|
+
*/
|
|
50
|
+
export declare function readToolVersions(cliVersion?: string, build?: string): ToolVersions;
|
package/dist/versions.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The versions a result names: Wasit core and the CLI, the x402 version Wasit
|
|
3
|
+
* grades, and the SDK packages the checks run through.
|
|
4
|
+
*
|
|
5
|
+
* Read at run time from the installed packages' own `package.json`, by path,
|
|
6
|
+
* never typed by hand: a constant drifts from what is installed, and the point
|
|
7
|
+
* of printing a version is that it is the one that ran. They are read by path
|
|
8
|
+
* and not through `require.resolve("<pkg>/package.json")` because the SDK
|
|
9
|
+
* packages' export maps do not list it (checked on 2026-11-15 for all five:
|
|
10
|
+
* ERR_PACKAGE_PATH_NOT_EXPORTED).
|
|
11
|
+
*/
|
|
12
|
+
import { readFileSync } from "node:fs";
|
|
13
|
+
import { dirname, join, resolve } from "node:path";
|
|
14
|
+
import { fileURLToPath } from "node:url";
|
|
15
|
+
/** What is printed when a version cannot be established. Never a guess. */
|
|
16
|
+
const UNKNOWN = "unknown";
|
|
17
|
+
/** A version as npm publishes it. Anything else is not printed: these strings reach a pull request comment. */
|
|
18
|
+
const VERSION = /^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.+-]*)?$/;
|
|
19
|
+
/** An npm package name, scoped or not: no path segment can climb out of node_modules. */
|
|
20
|
+
const PACKAGE_NAME = /^(?:@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*$/;
|
|
21
|
+
function versionIn(manifestPath, expectedName) {
|
|
22
|
+
try {
|
|
23
|
+
const manifest = JSON.parse(readFileSync(manifestPath, "utf-8"));
|
|
24
|
+
return manifest.name === expectedName && typeof manifest.version === "string" && VERSION.test(manifest.version)
|
|
25
|
+
? manifest.version
|
|
26
|
+
: undefined;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The installed version of `name`, found the way Node finds a dependency: in
|
|
34
|
+
* `node_modules` of `from` or of any directory above it.
|
|
35
|
+
*
|
|
36
|
+
* Returns "unknown" when the package is not there, its name does not match, or
|
|
37
|
+
* its version is not a version.
|
|
38
|
+
*/
|
|
39
|
+
export function readPackageVersion(name, from) {
|
|
40
|
+
if (!PACKAGE_NAME.test(name))
|
|
41
|
+
return UNKNOWN;
|
|
42
|
+
let directory = resolve(from);
|
|
43
|
+
for (;;) {
|
|
44
|
+
const version = versionIn(join(directory, "node_modules", name, "package.json"), name);
|
|
45
|
+
if (version !== undefined)
|
|
46
|
+
return version;
|
|
47
|
+
const parent = dirname(directory);
|
|
48
|
+
if (parent === directory)
|
|
49
|
+
return UNKNOWN;
|
|
50
|
+
directory = parent;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** This package's own version: the `package.json` above this file, in `src/` and in `dist/` alike. */
|
|
54
|
+
function ownVersion() {
|
|
55
|
+
let directory = dirname(fileURLToPath(import.meta.url));
|
|
56
|
+
for (;;) {
|
|
57
|
+
const version = versionIn(join(directory, "package.json"), "@wasit-dev/core");
|
|
58
|
+
if (version !== undefined)
|
|
59
|
+
return version;
|
|
60
|
+
const parent = dirname(directory);
|
|
61
|
+
if (parent === directory)
|
|
62
|
+
return UNKNOWN;
|
|
63
|
+
directory = parent;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** A commit id as an Action ref gives it: all 40 characters, lower case. Anything else (a tag, a branch) is not a commit. */
|
|
67
|
+
const FULL_COMMIT = /^[0-9a-f]{40}$/;
|
|
68
|
+
/** What a result names of a commit: its first seven characters. */
|
|
69
|
+
const SHORT_COMMIT = /^[0-9a-f]{7}$/;
|
|
70
|
+
/** True for exactly seven lower-case hex characters: the only build text that reaches a comment. */
|
|
71
|
+
export function isShortCommit(value) {
|
|
72
|
+
return typeof value === "string" && SHORT_COMMIT.test(value);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The commit this build of Wasit was made from, when the Action says so: the
|
|
76
|
+
* action ref (`WASIT_BUILD_COMMIT`) if it is a full commit id. A tag or a branch
|
|
77
|
+
* name is not a commit, and says nothing here: a pinned tag already names the
|
|
78
|
+
* version, and a result must not name a build it cannot show.
|
|
79
|
+
*/
|
|
80
|
+
export function readBuildCommit(env) {
|
|
81
|
+
const value = env["WASIT_BUILD_COMMIT"];
|
|
82
|
+
return value !== undefined && FULL_COMMIT.test(value) ? value.slice(0, 7) : undefined;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The versions of this run. The CLI knows its own version (core cannot read the
|
|
86
|
+
* CLI's `package.json`), so it passes it in.
|
|
87
|
+
*/
|
|
88
|
+
export function readToolVersions(cliVersion, build) {
|
|
89
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
90
|
+
// `@x402/core` is a dependency of `@x402/fetch`; when npm does not hoist it, it sits under that package.
|
|
91
|
+
const fetchDir = (() => {
|
|
92
|
+
for (let directory = here;; directory = dirname(directory)) {
|
|
93
|
+
const candidate = join(directory, "node_modules", "@x402", "fetch");
|
|
94
|
+
if (versionIn(join(candidate, "package.json"), "@x402/fetch") !== undefined)
|
|
95
|
+
return candidate;
|
|
96
|
+
if (dirname(directory) === directory)
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
})();
|
|
100
|
+
const read = (name) => {
|
|
101
|
+
for (const start of fetchDir === undefined ? [here] : [here, fetchDir]) {
|
|
102
|
+
const version = readPackageVersion(name, start);
|
|
103
|
+
if (version !== UNKNOWN)
|
|
104
|
+
return version;
|
|
105
|
+
}
|
|
106
|
+
return UNKNOWN;
|
|
107
|
+
};
|
|
108
|
+
return {
|
|
109
|
+
wasit: {
|
|
110
|
+
cli: cliVersion !== undefined && VERSION.test(cliVersion) ? cliVersion : UNKNOWN,
|
|
111
|
+
core: ownVersion(),
|
|
112
|
+
...(isShortCommit(build) ? { build } : {}),
|
|
113
|
+
},
|
|
114
|
+
sdk: {
|
|
115
|
+
x402Version: 2,
|
|
116
|
+
"@x402/core": read("@x402/core"),
|
|
117
|
+
"@x402/stellar": read("@x402/stellar"),
|
|
118
|
+
"@stellar/mpp": read("@stellar/mpp"),
|
|
119
|
+
mppx: read("mppx"),
|
|
120
|
+
"@stellar/stellar-sdk": read("@stellar/stellar-sdk"),
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
package/dist/x402/chains/evm.js
CHANGED
|
@@ -207,7 +207,10 @@ export const evmChain = {
|
|
|
207
207
|
client.register("eip155:*", new ExactEvmScheme(signer, { rpcUrl }));
|
|
208
208
|
const allowed = NETWORKS[network]?.allowedAssets;
|
|
209
209
|
if (allowed !== undefined) {
|
|
210
|
+
// Replaces the controls Wasit set before this call, so the $1 cap on the
|
|
211
|
+
// default assets is stated again rather than left to the SDK's default.
|
|
210
212
|
client.setSpendControls({
|
|
213
|
+
maxAmountPerPayment: "$1",
|
|
211
214
|
allowedAssets: allowed.map((entry) => ({ network: network, ...entry })),
|
|
212
215
|
});
|
|
213
216
|
}
|
|
@@ -4,6 +4,14 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import type { PaymentChain } from "./types.js";
|
|
6
6
|
export type { PaymentChain } from "./types.js";
|
|
7
|
+
/**
|
|
8
|
+
* Tests only: replaces the adapters, so a recording chain can stand in for a
|
|
9
|
+
* real one (spec B11, the worst-case property test). `undefined` restores the
|
|
10
|
+
* real ones. Not exported from the package index, and reachable from no flag,
|
|
11
|
+
* setting or tool argument. The network guard still applies: a replacement
|
|
12
|
+
* pays only on a test network.
|
|
13
|
+
*/
|
|
14
|
+
export declare function setPaymentChainsForTesting(replacement: readonly PaymentChain[] | undefined): void;
|
|
7
15
|
/** The adapter that pays on `network`, if Wasit has one. */
|
|
8
16
|
export declare function paymentChainFor(network: string): PaymentChain | undefined;
|
|
9
17
|
/** Every network the payment checks can pay on, in adapter order. */
|
|
@@ -7,11 +7,22 @@ import { stellarChain } from "./stellar.js";
|
|
|
7
7
|
import { svmChain } from "./svm.js";
|
|
8
8
|
// Stellar first: it is the default network, and the only chain with MPP.
|
|
9
9
|
const CHAINS = [stellarChain, evmChain, svmChain];
|
|
10
|
+
let chains = CHAINS;
|
|
11
|
+
/**
|
|
12
|
+
* Tests only: replaces the adapters, so a recording chain can stand in for a
|
|
13
|
+
* real one (spec B11, the worst-case property test). `undefined` restores the
|
|
14
|
+
* real ones. Not exported from the package index, and reachable from no flag,
|
|
15
|
+
* setting or tool argument. The network guard still applies: a replacement
|
|
16
|
+
* pays only on a test network.
|
|
17
|
+
*/
|
|
18
|
+
export function setPaymentChainsForTesting(replacement) {
|
|
19
|
+
chains = replacement ?? CHAINS;
|
|
20
|
+
}
|
|
10
21
|
/** The adapter that pays on `network`, if Wasit has one. */
|
|
11
22
|
export function paymentChainFor(network) {
|
|
12
|
-
return
|
|
23
|
+
return chains.find((chain) => chain.networks.includes(network));
|
|
13
24
|
}
|
|
14
25
|
/** Every network the payment checks can pay on, in adapter order. */
|
|
15
26
|
export function paymentNetworks() {
|
|
16
|
-
return
|
|
27
|
+
return chains.flatMap((chain) => chain.networks);
|
|
17
28
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* only one with MPP as well.
|
|
6
6
|
*/
|
|
7
7
|
import { Asset } from "@stellar/stellar-sdk";
|
|
8
|
-
import type { PaymentChain } from "./types.js";
|
|
8
|
+
import type { PaymentChain, SignedAuthorization } from "./types.js";
|
|
9
9
|
/**
|
|
10
10
|
* Corrupts the client's authorization signature, and nothing else.
|
|
11
11
|
*
|
|
@@ -28,4 +28,11 @@ export declare function corruptAuthSignature(transaction: string): string;
|
|
|
28
28
|
* the id derived from the asset.
|
|
29
29
|
*/
|
|
30
30
|
export declare function sacAssetFromName(name: unknown, contractId: string, passphrase: string): Asset | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* X402-11 reads (D-032, spec B16.4): the authorization a payment carries, as
|
|
33
|
+
* the payer signed it. Only an address credential is read; another form (the
|
|
34
|
+
* newer `AddressV2`, one with delegates) is not, and gives no reading rather
|
|
35
|
+
* than a guess, since its nonce handling was not read.
|
|
36
|
+
*/
|
|
37
|
+
export declare function decodeStellarAuthorization(payload: Record<string, unknown>): SignedAuthorization;
|
|
31
38
|
export declare const stellarChain: PaymentChain;
|
|
@@ -9,7 +9,7 @@ import { ExactStellarScheme as ExactStellarClientScheme } from "@x402/stellar/ex
|
|
|
9
9
|
import { Account, Address, Asset, Contract, Keypair, StrKey, TransactionBuilder, rpc, scValToNative, xdr, } from "@stellar/stellar-sdk";
|
|
10
10
|
import { CheckSetupError } from "../../errors.js";
|
|
11
11
|
import { networkPassphrase, resolveRpcUrl } from "../../mpp/network.js";
|
|
12
|
-
import { verifySettlement } from "../../settlement.js";
|
|
12
|
+
import { readTransferData, verifySettlement } from "../../settlement.js";
|
|
13
13
|
/** A Stellar transaction hash: 64 hex characters. */
|
|
14
14
|
const TRANSACTION_HASH = /^[0-9a-f]{64}$/i;
|
|
15
15
|
/**
|
|
@@ -128,6 +128,129 @@ async function stellarTokenBalance(network, rpcUrl, payer, asset) {
|
|
|
128
128
|
})));
|
|
129
129
|
return entries.length === 0 ? 0n : undefined;
|
|
130
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* X402-11 reads (D-032, spec B16.4): the authorization a payment carries, as
|
|
133
|
+
* the payer signed it. Only an address credential is read; another form (the
|
|
134
|
+
* newer `AddressV2`, one with delegates) is not, and gives no reading rather
|
|
135
|
+
* than a guess, since its nonce handling was not read.
|
|
136
|
+
*/
|
|
137
|
+
export function decodeStellarAuthorization(payload) {
|
|
138
|
+
const transaction = payload["transaction"];
|
|
139
|
+
if (typeof transaction !== "string") {
|
|
140
|
+
throw new CheckSetupError("The payment payload carries no transaction to read its authorization from.");
|
|
141
|
+
}
|
|
142
|
+
let envelope;
|
|
143
|
+
try {
|
|
144
|
+
envelope = xdr.TransactionEnvelope.fromXDR(transaction, "base64");
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
throw new CheckSetupError("The payment payload's transaction does not decode, so its authorization cannot be read.");
|
|
148
|
+
}
|
|
149
|
+
const operations = envelope.switch().name === "envelopeTypeTx" ? envelope.v1().tx().operations() : [];
|
|
150
|
+
const found = [];
|
|
151
|
+
let otherForms = 0;
|
|
152
|
+
for (const operation of operations) {
|
|
153
|
+
if (operation.body().switch().name !== "invokeHostFunction")
|
|
154
|
+
continue;
|
|
155
|
+
for (const entry of operation.body().invokeHostFunctionOp().auth()) {
|
|
156
|
+
const credentials = entry.credentials();
|
|
157
|
+
const kind = credentials.switch().name;
|
|
158
|
+
if (kind === "sorobanCredentialsSourceAccount")
|
|
159
|
+
continue;
|
|
160
|
+
if (kind !== "sorobanCredentialsAddress") {
|
|
161
|
+
otherForms += 1;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const address = credentials.address();
|
|
165
|
+
found.push({
|
|
166
|
+
payer: Address.fromScAddress(address.address()).toString(),
|
|
167
|
+
nonce: address.nonce().toString(),
|
|
168
|
+
expiresAt: address.signatureExpirationLedger(),
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (otherForms > 0) {
|
|
173
|
+
throw new CheckSetupError("The payment's authorization uses a credential form this check does not read (it reads address credentials), " +
|
|
174
|
+
"so whether it was used cannot be read.");
|
|
175
|
+
}
|
|
176
|
+
if (found.length !== 1) {
|
|
177
|
+
throw new CheckSetupError(`The payment carries ${found.length} address authorizations; this check reads exactly one.`);
|
|
178
|
+
}
|
|
179
|
+
return found[0];
|
|
180
|
+
}
|
|
181
|
+
/** The ledger entry the host writes when it consumes the nonce: TEMPORARY contract data of the signer. */
|
|
182
|
+
function nonceKey(authorization) {
|
|
183
|
+
return xdr.LedgerKey.contractData(new xdr.LedgerKeyContractData({
|
|
184
|
+
contract: new Address(authorization.payer).toScAddress(),
|
|
185
|
+
key: xdr.ScVal.scvLedgerKeyNonce(new xdr.ScNonceKey({ nonce: xdr.Int64.fromString(authorization.nonce) })),
|
|
186
|
+
durability: xdr.ContractDataDurability.temporary(),
|
|
187
|
+
}));
|
|
188
|
+
}
|
|
189
|
+
function rpcServer(rpcUrl) {
|
|
190
|
+
return new rpc.Server(rpcUrl, { allowHttp: rpcUrl.startsWith("http://") });
|
|
191
|
+
}
|
|
192
|
+
/** The most events read for one X402-11 window: far more than one run makes. */
|
|
193
|
+
const EVENTS_PAGE = 100;
|
|
194
|
+
const EVENTS_MAX_PAGES = 20;
|
|
195
|
+
const stellarAuthorizations = {
|
|
196
|
+
unit: "ledger",
|
|
197
|
+
unitMs: 5_000,
|
|
198
|
+
// The x402 Stellar client's own default (DEFAULT_ESTIMATED_LEDGER_SECONDS in
|
|
199
|
+
// @x402/stellar 2.28.0); it estimates from recent ledgers when it can.
|
|
200
|
+
unitSeconds: 5,
|
|
201
|
+
decode: decodeStellarAuthorization,
|
|
202
|
+
async read(_network, rpcUrl, authorizations) {
|
|
203
|
+
const keys = authorizations.map(nonceKey);
|
|
204
|
+
const response = await rpcServer(rpcUrl).getLedgerEntries(...keys);
|
|
205
|
+
const byKey = new Map(response.entries.map((entry) => [entry.key.toXDR("base64"), entry]));
|
|
206
|
+
return {
|
|
207
|
+
latest: response.latestLedger,
|
|
208
|
+
used: keys.map((key) => {
|
|
209
|
+
const entry = byKey.get(key.toXDR("base64"));
|
|
210
|
+
return entry === undefined ? undefined : (entry.lastModifiedLedgerSeq ?? response.latestLedger);
|
|
211
|
+
}),
|
|
212
|
+
};
|
|
213
|
+
},
|
|
214
|
+
async transfers(_network, rpcUrl, query) {
|
|
215
|
+
const server = rpcServer(rpcUrl);
|
|
216
|
+
const name = xdr.ScVal.scvSymbol("transfer").toXDR("base64");
|
|
217
|
+
const from = new Address(query.from).toScVal().toXDR("base64");
|
|
218
|
+
const to = new Address(query.to).toScVal().toXDR("base64");
|
|
219
|
+
// A Stellar Asset Contract adds the asset as a fourth topic (CAP-67); a SEP-41 token may not.
|
|
220
|
+
const filters = [
|
|
221
|
+
{ type: "contract", contractIds: [query.asset], topics: [[name, from, to, "*"], [name, from, to]] },
|
|
222
|
+
];
|
|
223
|
+
const transfers = [];
|
|
224
|
+
let response = await server.getEvents({ startLedger: query.fromLedger, filters, limit: EVENTS_PAGE });
|
|
225
|
+
for (let page = 1;; page += 1) {
|
|
226
|
+
for (const event of response.events) {
|
|
227
|
+
// Only an event the RPC marks as from a failed call is left out. The testnet RPC no
|
|
228
|
+
// longer sends the field at all (observed 2026-11-16, run of slice 4's live check):
|
|
229
|
+
// reading its absence as "failed" dropped every transfer.
|
|
230
|
+
if (event.inSuccessfulContractCall === false)
|
|
231
|
+
continue;
|
|
232
|
+
const [topic, sender, receiver] = event.topic;
|
|
233
|
+
if (topic?.switch().name !== "scvSymbol" || topic.sym().toString() !== "transfer")
|
|
234
|
+
continue;
|
|
235
|
+
if (sender === undefined || receiver === undefined)
|
|
236
|
+
continue;
|
|
237
|
+
if (Address.fromScVal(sender).toString() !== query.from || Address.fromScVal(receiver).toString() !== query.to)
|
|
238
|
+
continue;
|
|
239
|
+
const data = readTransferData(event.value);
|
|
240
|
+
if (data === undefined)
|
|
241
|
+
continue;
|
|
242
|
+
transfers.push({ amount: data.amount, reference: event.txHash, ledger: event.ledger });
|
|
243
|
+
}
|
|
244
|
+
if (response.events.length < EVENTS_PAGE)
|
|
245
|
+
break;
|
|
246
|
+
// Never a partial answer read as complete.
|
|
247
|
+
if (page >= EVENTS_MAX_PAGES)
|
|
248
|
+
throw new Error("more transfer events than can be read in full");
|
|
249
|
+
response = await server.getEvents({ cursor: response.cursor, filters, limit: EVENTS_PAGE });
|
|
250
|
+
}
|
|
251
|
+
return { latest: response.latestLedger, transfers };
|
|
252
|
+
},
|
|
253
|
+
};
|
|
131
254
|
export const stellarChain = {
|
|
132
255
|
name: "Stellar",
|
|
133
256
|
networks: ["stellar:testnet", "stellar:pubnet"],
|
|
@@ -150,4 +273,5 @@ export const stellarChain = {
|
|
|
150
273
|
...payload,
|
|
151
274
|
transaction: corruptAuthSignature(payload["transaction"]),
|
|
152
275
|
}),
|
|
276
|
+
authorizations: stellarAuthorizations,
|
|
153
277
|
};
|