@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,199 @@
|
|
|
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, ConfigurationError } from "../errors.js";
|
|
18
|
+
import { sanitizeText } from "../sanitize.js";
|
|
19
|
+
/** At most this many dollars in one payment. Fixed; nothing raises it. */
|
|
20
|
+
export const PER_PAYMENT_CAP_USD = 1;
|
|
21
|
+
/** At most this many dollars signed in one run. A config may only lower it (`maxRunUsd`). */
|
|
22
|
+
export const PER_RUN_CAP_USD = 5;
|
|
23
|
+
/** A dollar, in the units this module counts in. */
|
|
24
|
+
const DOLLAR = 10n ** 18n;
|
|
25
|
+
/**
|
|
26
|
+
* USDC on each test network. Sources (read 2026-11-16): `@x402/stellar` 2.28.0
|
|
27
|
+
* `USDC_TESTNET_ADDRESS`, `DEFAULT_TOKEN_DECIMALS = 7`; `@x402/evm` 2.28.0 default
|
|
28
|
+
* asset of `eip155:84532`, decimals 6; Circle's USDC on Ethereum Sepolia as
|
|
29
|
+
* `chains/evm.ts` (`ETHEREUM_SEPOLIA_USDC`, 6 decimals read from the contract
|
|
30
|
+
* 2026-10-05); `@x402/svm` 2.28.0 `USDC_DEVNET_ADDRESS`, decimals 6.
|
|
31
|
+
*/
|
|
32
|
+
export const USD_ASSETS = [
|
|
33
|
+
{ network: "stellar:testnet", asset: "CBIELTK6YBZJU5UP2WWQEUCYKLPU6AUNZ2BQ4WWFEIE3USCIHMXQDAMA", decimals: 7, symbol: "USDC" },
|
|
34
|
+
{ network: "eip155:84532", asset: "0x036CbD53842c5426634e7929541eC2318f3dCF7e", decimals: 6, symbol: "USDC" },
|
|
35
|
+
{ network: "eip155:11155111", asset: "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238", decimals: 6, symbol: "USDC" },
|
|
36
|
+
{ network: "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1", asset: "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU", decimals: 6, symbol: "USDC" },
|
|
37
|
+
];
|
|
38
|
+
/** Whether two addresses on `network` are the same: an EVM address in any letter case, others exactly. */
|
|
39
|
+
export function sameAddress(network, a, b) {
|
|
40
|
+
if (network.startsWith("eip155:"))
|
|
41
|
+
return a.toLowerCase() === b.toLowerCase();
|
|
42
|
+
return a === b;
|
|
43
|
+
}
|
|
44
|
+
/** The dollar asset `asset` is on `network`, if Wasit knows it. */
|
|
45
|
+
export function usdAsset(network, asset) {
|
|
46
|
+
return USD_ASSETS.find((entry) => entry.network === network && sameAddress(network, entry.asset, asset));
|
|
47
|
+
}
|
|
48
|
+
/** `amount` base units of `asset`, in this module's dollar units. */
|
|
49
|
+
function toUsd(asset, amount) {
|
|
50
|
+
return (amount * DOLLAR) / 10n ** BigInt(asset.decimals);
|
|
51
|
+
}
|
|
52
|
+
/** Whole and fractional dollars, rounded down to a millionth: a cap is never rounded up. */
|
|
53
|
+
export function usdFromDollars(dollars) {
|
|
54
|
+
return (BigInt(Math.floor(dollars * 1_000_000)) * DOLLAR) / 1000000n;
|
|
55
|
+
}
|
|
56
|
+
/** "$4.50", "$0.0000001": exact, at least two decimals. */
|
|
57
|
+
export function formatUsd(amount) {
|
|
58
|
+
const sign = amount < 0n ? "-" : "";
|
|
59
|
+
const absolute = amount < 0n ? -amount : amount;
|
|
60
|
+
const whole = absolute / DOLLAR;
|
|
61
|
+
let fraction = (absolute % DOLLAR).toString().padStart(18, "0").replace(/0+$/, "");
|
|
62
|
+
if (fraction.length < 2)
|
|
63
|
+
fraction = fraction.padEnd(2, "0");
|
|
64
|
+
return `${sign}$${whole}.${fraction}`;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* A payment the guard will not sign, or, with `signed`, a signed payment it
|
|
68
|
+
* will not send. A setup error: it says nothing about the target, and the
|
|
69
|
+
* check that hit it has no verdict.
|
|
70
|
+
*/
|
|
71
|
+
export class PaymentGuardRefusedError extends CheckSetupError {
|
|
72
|
+
reason;
|
|
73
|
+
constructor(reason, message, options = {}) {
|
|
74
|
+
super(options.signed === true ? `${message} It was signed and discarded; it was not sent.` : `${message} Nothing was signed.`);
|
|
75
|
+
this.name = "PaymentGuardRefusedError";
|
|
76
|
+
this.reason = reason;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** A value the target chose, for a message: one line, no control characters, bounded. */
|
|
80
|
+
export function shown(value) {
|
|
81
|
+
const text = sanitizeText(String(value)).replace(/\s+/g, " ");
|
|
82
|
+
return JSON.stringify(text.length > 80 ? `${text.slice(0, 77)}...` : text);
|
|
83
|
+
}
|
|
84
|
+
/** A base-unit amount as the challenge gives it: digits only, at most 78 (a uint256). */
|
|
85
|
+
const AMOUNT = /^\d{1,78}$/;
|
|
86
|
+
export function parseAmount(amount) {
|
|
87
|
+
if (typeof amount !== "string" || !AMOUNT.test(amount)) {
|
|
88
|
+
throw new PaymentGuardRefusedError("amount", `The amount to sign, ${shown(amount)}, is not a whole number of base units.`);
|
|
89
|
+
}
|
|
90
|
+
return BigInt(amount);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Refuses unless EVERY `exact` option on `network` pays the pinned `payTo` in
|
|
94
|
+
* the pinned asset (B-A02): the SDK picks among the options by its own rules,
|
|
95
|
+
* so a check of the first one alone would not cover what it signs. Options on
|
|
96
|
+
* other networks or schemes are never signed and are not judged.
|
|
97
|
+
*/
|
|
98
|
+
export function assertPinned(network, accepts, pin) {
|
|
99
|
+
if (pin === undefined)
|
|
100
|
+
return;
|
|
101
|
+
for (const [index, requirement] of accepts.entries()) {
|
|
102
|
+
if (requirement.scheme !== "exact" || requirement.network !== network)
|
|
103
|
+
continue;
|
|
104
|
+
for (const field of ["payTo", "asset"]) {
|
|
105
|
+
if (!sameAddress(network, String(requirement[field]), pin[field])) {
|
|
106
|
+
throw new PaymentGuardRefusedError("pin", `Option ${index + 1} of the challenge (exact, ${network}) has ${field} ${shown(requirement[field])}, ` +
|
|
107
|
+
`not the pinned ${pin[field]}.`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* What the payment checks sign for an endpoint priced `price` (B0 fact 8):
|
|
114
|
+
* X402-06 the price (settles), X402-07 the price (signature then corrupted),
|
|
115
|
+
* X402-09 half the price, rounded down (none when there is no smaller amount),
|
|
116
|
+
* X402-10 the price with a short lifetime. X402-08 resends X402-06 and signs
|
|
117
|
+
* nothing. 3.5 times the price. With `rejectedRequest`, X402-11 the price as
|
|
118
|
+
* well (a valid payment sent with a request the service must refuse): 4.5 times.
|
|
119
|
+
*/
|
|
120
|
+
export function plannedSignatures(price, options = {}) {
|
|
121
|
+
return [
|
|
122
|
+
{ check: "X402-06", amount: price },
|
|
123
|
+
{ check: "X402-07", amount: price },
|
|
124
|
+
...(price >= 2n ? [{ check: "X402-09", amount: price / 2n }] : []),
|
|
125
|
+
{ check: "X402-10", amount: price },
|
|
126
|
+
...(options.rejectedRequest === true ? [{ check: "X402-11", amount: price }] : []),
|
|
127
|
+
];
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The dollars one run may sign. Every signature is charged here first; a
|
|
131
|
+
* charge that does not fit is refused and nothing is signed. Nothing is ever
|
|
132
|
+
* credited back: a payment that failed after it was signed may still settle.
|
|
133
|
+
* Once the cap refuses a payment, the budget is spent: every later charge is
|
|
134
|
+
* refused too, so a run never signs a smaller payment after a larger one did
|
|
135
|
+
* not fit.
|
|
136
|
+
*/
|
|
137
|
+
export class RunBudget {
|
|
138
|
+
#cap;
|
|
139
|
+
#spent = 0n;
|
|
140
|
+
#exhausted;
|
|
141
|
+
#entries = [];
|
|
142
|
+
constructor(options = {}) {
|
|
143
|
+
const lower = options.maxRunUsd;
|
|
144
|
+
if (lower !== undefined && (!Number.isFinite(lower) || lower <= 0)) {
|
|
145
|
+
throw new ConfigurationError("maxRunUsd must be a number above 0: it can only lower the per-run cap.");
|
|
146
|
+
}
|
|
147
|
+
this.#cap = usdFromDollars(Math.min(lower ?? PER_RUN_CAP_USD, PER_RUN_CAP_USD));
|
|
148
|
+
}
|
|
149
|
+
/** The run's cap, in dollar units. */
|
|
150
|
+
get capUsd() {
|
|
151
|
+
return this.#cap;
|
|
152
|
+
}
|
|
153
|
+
/** What the run has charged so far, in dollar units. */
|
|
154
|
+
get spentUsd() {
|
|
155
|
+
return this.#spent;
|
|
156
|
+
}
|
|
157
|
+
get entries() {
|
|
158
|
+
return [...this.#entries];
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Charges one payment, or refuses it. Called before the signature, with the
|
|
162
|
+
* amount that will be signed.
|
|
163
|
+
*/
|
|
164
|
+
charge(charge) {
|
|
165
|
+
const amount = parseAmount(charge.amount);
|
|
166
|
+
const asset = usdAsset(charge.network, charge.asset);
|
|
167
|
+
if (asset === undefined) {
|
|
168
|
+
throw new PaymentGuardRefusedError("asset", `The target asks to be paid in ${shown(charge.asset)} on ${charge.network}, an asset Wasit does not ` +
|
|
169
|
+
`value in US dollars, so it cannot hold the payment to the $${PER_PAYMENT_CAP_USD}.00 cap.`);
|
|
170
|
+
}
|
|
171
|
+
const usd = toUsd(asset, amount);
|
|
172
|
+
const perPayment = usdFromDollars(PER_PAYMENT_CAP_USD);
|
|
173
|
+
if (usd > perPayment) {
|
|
174
|
+
throw new PaymentGuardRefusedError("price", `${charge.check} would sign ${amount} base units of ${asset.symbol} (${formatUsd(usd)}), above the ` +
|
|
175
|
+
`${formatUsd(perPayment)} per payment fixed in Wasit. A route priced at ` +
|
|
176
|
+
`${formatUsd(perPayment)} or less is checked as usual.`);
|
|
177
|
+
}
|
|
178
|
+
if (charge.planned !== undefined && amount > charge.planned) {
|
|
179
|
+
throw new PaymentGuardRefusedError("plan", `${charge.check} would sign ${amount} base units, more than the ${charge.planned} the plan allowed ` +
|
|
180
|
+
`for it before the run started: the service changed its price after the plan.`);
|
|
181
|
+
}
|
|
182
|
+
if (this.#exhausted !== undefined) {
|
|
183
|
+
throw new PaymentGuardRefusedError("run-cap", this.#exhausted);
|
|
184
|
+
}
|
|
185
|
+
if (this.#spent + usd > this.#cap) {
|
|
186
|
+
this.#exhausted =
|
|
187
|
+
`The run reached its ${formatUsd(this.#cap)} cap: ${charge.check} would have taken it from ` +
|
|
188
|
+
`${formatUsd(this.#spent)} to ${formatUsd(this.#spent + usd)}, so the run signs nothing more.`;
|
|
189
|
+
throw new PaymentGuardRefusedError("run-cap", this.#exhausted);
|
|
190
|
+
}
|
|
191
|
+
this.#spent += usd;
|
|
192
|
+
this.#entries.push({ check: charge.check, network: charge.network, asset: asset.asset, amount });
|
|
193
|
+
}
|
|
194
|
+
/** The dollar value of `amount` base units of `asset` on `network`, if Wasit can value it. */
|
|
195
|
+
static valueOf(network, asset, amount) {
|
|
196
|
+
const known = usdAsset(network, asset);
|
|
197
|
+
return known === undefined ? undefined : toUsd(known, amount);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which GitHub events may run the payment tier (D-022a option 2, D-032).
|
|
3
|
+
*
|
|
4
|
+
* The payment tier runs only where the code that runs is code that was merged:
|
|
5
|
+
* a push to the default branch, a schedule (GitHub runs it on the default
|
|
6
|
+
* branch), and workflow_dispatch on the default branch. Every pull request event
|
|
7
|
+
* is refused, a same-repository one too: its branch's code and new dependencies
|
|
8
|
+
* would run in the same job as the payer key before anyone reviewed them. Every
|
|
9
|
+
* other event is refused as well (merge_group and workflow_run run code that is
|
|
10
|
+
* not on the default branch yet). Outside GitHub Actions the gate is open: a run
|
|
11
|
+
* on a laptop is the builder's own choice.
|
|
12
|
+
*
|
|
13
|
+
* What this stops is a mistake in a workflow, not its author (B-A10): a step
|
|
14
|
+
* can hide the runner's variables from the CLI. The control that holds is
|
|
15
|
+
* where the workflow gives the secret, which the guide explains.
|
|
16
|
+
*/
|
|
17
|
+
export interface PaymentEventGate {
|
|
18
|
+
readonly allowed: boolean;
|
|
19
|
+
/** Set when not allowed: the reason a skipped payment check gives, "payment checks not run: ...". */
|
|
20
|
+
readonly reason?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Not allowed because the run is in a CI that is not GitHub Actions (`CI`
|
|
23
|
+
* set): there the service of the run would share its job with the key, and
|
|
24
|
+
* nothing keeps them apart. `wasit run` refuses the payment tier there.
|
|
25
|
+
*/
|
|
26
|
+
readonly ciOutsideGithub?: true;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* True when the process runs as a step of GitHub Actions, or says it does.
|
|
30
|
+
* Fail closed: either variable counts, with any value that is not empty, as
|
|
31
|
+
* the CLI's own CI test reads `GITHUB_ACTIONS`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function inGithubActions(env: Readonly<Record<string, string | undefined>>): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* May this run pay? `event` is the parsed payload of `GITHUB_EVENT_PATH`
|
|
36
|
+
* ({@link readGithubEvent}), or undefined when there is none.
|
|
37
|
+
*/
|
|
38
|
+
export declare function paymentEventGate(env: Readonly<Record<string, string | undefined>>, event: unknown): PaymentEventGate;
|
|
39
|
+
/**
|
|
40
|
+
* The payload of the event that started the workflow, from the file
|
|
41
|
+
* `GITHUB_EVENT_PATH` names; undefined when there is none, it cannot be read,
|
|
42
|
+
* it is not JSON, or it is above 1 MiB.
|
|
43
|
+
*/
|
|
44
|
+
export declare function readGithubEvent(env: Readonly<Record<string, string | undefined>>): unknown;
|
|
45
|
+
/**
|
|
46
|
+
* The key variables that hold a value, by their fixed names in upper case. A
|
|
47
|
+
* blank value is not a key: a pull request from a fork gets every secret as an
|
|
48
|
+
* empty string.
|
|
49
|
+
*/
|
|
50
|
+
export declare function payerKeyVariables(env: Readonly<Record<string, string | undefined>>): string[];
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which GitHub events may run the payment tier (D-022a option 2, D-032).
|
|
3
|
+
*
|
|
4
|
+
* The payment tier runs only where the code that runs is code that was merged:
|
|
5
|
+
* a push to the default branch, a schedule (GitHub runs it on the default
|
|
6
|
+
* branch), and workflow_dispatch on the default branch. Every pull request event
|
|
7
|
+
* is refused, a same-repository one too: its branch's code and new dependencies
|
|
8
|
+
* would run in the same job as the payer key before anyone reviewed them. Every
|
|
9
|
+
* other event is refused as well (merge_group and workflow_run run code that is
|
|
10
|
+
* not on the default branch yet). Outside GitHub Actions the gate is open: a run
|
|
11
|
+
* on a laptop is the builder's own choice.
|
|
12
|
+
*
|
|
13
|
+
* What this stops is a mistake in a workflow, not its author (B-A10): a step
|
|
14
|
+
* can hide the runner's variables from the CLI. The control that holds is
|
|
15
|
+
* where the workflow gives the secret, which the guide explains.
|
|
16
|
+
*/
|
|
17
|
+
import { readFileSync, statSync } from "node:fs";
|
|
18
|
+
import { DENIED_ENV_NAMES } from "../denied-env.js";
|
|
19
|
+
const ALLOWED_EVENTS = "Wasit pays on push to the default branch, schedule and workflow_dispatch only";
|
|
20
|
+
/** An event name or a branch, as far as it is ever printed. Anything else is not echoed. */
|
|
21
|
+
const PRINTABLE_EVENT = /^[a-z_]{1,64}$/;
|
|
22
|
+
const PRINTABLE_REF = /^[A-Za-z0-9._/-]{1,200}$/;
|
|
23
|
+
const MAX_EVENT_BYTES = 1024 * 1024;
|
|
24
|
+
function nonEmpty(value) {
|
|
25
|
+
return typeof value === "string" && value !== "";
|
|
26
|
+
}
|
|
27
|
+
function shownEvent(event) {
|
|
28
|
+
return PRINTABLE_EVENT.test(event) ? event : "(an event name Wasit does not print)";
|
|
29
|
+
}
|
|
30
|
+
function shownRef(ref) {
|
|
31
|
+
if (ref === "")
|
|
32
|
+
return "no ref";
|
|
33
|
+
return PRINTABLE_REF.test(ref) ? ref : "(a ref Wasit does not print)";
|
|
34
|
+
}
|
|
35
|
+
/** The default branch the event payload names, or undefined. */
|
|
36
|
+
function defaultBranch(event) {
|
|
37
|
+
if (event === null || typeof event !== "object")
|
|
38
|
+
return undefined;
|
|
39
|
+
const repository = event.repository;
|
|
40
|
+
if (repository === null || typeof repository !== "object")
|
|
41
|
+
return undefined;
|
|
42
|
+
const branch = repository.default_branch;
|
|
43
|
+
return typeof branch === "string" && PRINTABLE_REF.test(branch) ? branch : undefined;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* True when the process runs as a step of GitHub Actions, or says it does.
|
|
47
|
+
* Fail closed: either variable counts, with any value that is not empty, as
|
|
48
|
+
* the CLI's own CI test reads `GITHUB_ACTIONS`.
|
|
49
|
+
*/
|
|
50
|
+
export function inGithubActions(env) {
|
|
51
|
+
return nonEmpty(env["GITHUB_ACTIONS"]) || nonEmpty(env["GITHUB_EVENT_NAME"]);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* May this run pay? `event` is the parsed payload of `GITHUB_EVENT_PATH`
|
|
55
|
+
* ({@link readGithubEvent}), or undefined when there is none.
|
|
56
|
+
*/
|
|
57
|
+
export function paymentEventGate(env, event) {
|
|
58
|
+
if (!inGithubActions(env)) {
|
|
59
|
+
// Najmi, 2026-11-17 (review R9 a): outside GitHub Actions only a run outside CI pays.
|
|
60
|
+
if (nonEmpty(env["CI"])) {
|
|
61
|
+
return { allowed: false, reason: "payment checks not run: CI is set, and this is not GitHub Actions", ciOutsideGithub: true };
|
|
62
|
+
}
|
|
63
|
+
return { allowed: true };
|
|
64
|
+
}
|
|
65
|
+
const name = env["GITHUB_EVENT_NAME"] ?? "";
|
|
66
|
+
if (name.startsWith("pull_request"))
|
|
67
|
+
return { allowed: false, reason: "payment checks not run: pull request" };
|
|
68
|
+
if (name !== "push" && name !== "schedule" && name !== "workflow_dispatch") {
|
|
69
|
+
return {
|
|
70
|
+
allowed: false,
|
|
71
|
+
reason: `payment checks not run: the ${shownEvent(name)} event (${ALLOWED_EVENTS})`,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const ref = env["GITHUB_REF"] ?? "";
|
|
75
|
+
const branch = defaultBranch(event);
|
|
76
|
+
if (branch === undefined) {
|
|
77
|
+
// GitHub runs a schedule on the default branch, and its payload may not name
|
|
78
|
+
// the repository; its ref must still be a branch.
|
|
79
|
+
if (name === "schedule" && ref.startsWith("refs/heads/"))
|
|
80
|
+
return { allowed: true };
|
|
81
|
+
return {
|
|
82
|
+
allowed: false,
|
|
83
|
+
reason: `payment checks not run: the ${name} event does not name the default branch, so Wasit cannot tell that ${shownRef(ref)} is it`,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (ref !== `refs/heads/${branch}`) {
|
|
87
|
+
return {
|
|
88
|
+
allowed: false,
|
|
89
|
+
reason: `payment checks not run: ${name} on ${shownRef(ref)}, not the default branch ${branch} (${ALLOWED_EVENTS})`,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return { allowed: true };
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The payload of the event that started the workflow, from the file
|
|
96
|
+
* `GITHUB_EVENT_PATH` names; undefined when there is none, it cannot be read,
|
|
97
|
+
* it is not JSON, or it is above 1 MiB.
|
|
98
|
+
*/
|
|
99
|
+
export function readGithubEvent(env) {
|
|
100
|
+
const file = env["GITHUB_EVENT_PATH"];
|
|
101
|
+
if (!nonEmpty(file))
|
|
102
|
+
return undefined;
|
|
103
|
+
try {
|
|
104
|
+
if (statSync(file).size > MAX_EVENT_BYTES)
|
|
105
|
+
return undefined;
|
|
106
|
+
return JSON.parse(readFileSync(file, "utf-8"));
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The key variables that hold a value, by their fixed names in upper case. A
|
|
114
|
+
* blank value is not a key: a pull request from a fork gets every secret as an
|
|
115
|
+
* empty string.
|
|
116
|
+
*/
|
|
117
|
+
export function payerKeyVariables(env) {
|
|
118
|
+
const names = new Set();
|
|
119
|
+
for (const [name, value] of Object.entries(env)) {
|
|
120
|
+
const upper = name.toUpperCase();
|
|
121
|
+
if (DENIED_ENV_NAMES.includes(upper) && typeof value === "string" && value.trim() !== "")
|
|
122
|
+
names.add(upper);
|
|
123
|
+
}
|
|
124
|
+
return [...names];
|
|
125
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mainnet opt-in switches (spec B5), as this version has them: switches
|
|
3
|
+
* that always refuse (spec B12).
|
|
4
|
+
*
|
|
5
|
+
* A later version may let an x402 run pay on `stellar:pubnet` behind an
|
|
6
|
+
* explicit opt-in. This one does not, and it has no code path that answers
|
|
7
|
+
* "allowed" for it. The switches that opt-in would read exist here only so
|
|
8
|
+
* that a run, a workflow or a server that asks for mainnet is stopped and told
|
|
9
|
+
* why, instead of being run on whatever else it was given:
|
|
10
|
+
*
|
|
11
|
+
* - `--mainnet` on `wasit run` (never read from the config or the environment);
|
|
12
|
+
* - `WASIT_MAINNET_OPT_IN` and `WASIT_MAINNET_CONFIG`, the MCP server's
|
|
13
|
+
* start-up variables, and `--mainnet` among its arguments: the server does
|
|
14
|
+
* not start.
|
|
15
|
+
*
|
|
16
|
+
* The Action's `mainnet` input is refused by the Action's own input check,
|
|
17
|
+
* which runs before Wasit is installed and so cannot import this module.
|
|
18
|
+
*
|
|
19
|
+
* Every function here throws, or only says which switches are set.
|
|
20
|
+
*/
|
|
21
|
+
import { ConfigurationError } from "../errors.js";
|
|
22
|
+
/** The sentence of spec B12. */
|
|
23
|
+
export declare const MAINNET_OPT_IN_ABSENT = "the mainnet opt-in is not part of this version";
|
|
24
|
+
/** A switch that asks for mainnet: the CLI's flag, or one of the MCP server's start-up settings. */
|
|
25
|
+
export type MainnetSwitch = "--mainnet" | "WASIT_MAINNET_OPT_IN" | "WASIT_MAINNET_CONFIG";
|
|
26
|
+
/**
|
|
27
|
+
* A switch asked for mainnet, and this version has no mainnet opt-in. A
|
|
28
|
+
* `ConfigurationError`: it says nothing about any service. Nothing was read,
|
|
29
|
+
* sent or signed when it is thrown.
|
|
30
|
+
*/
|
|
31
|
+
export declare class MainnetOptInRefusedError extends ConfigurationError {
|
|
32
|
+
readonly switchName: MainnetSwitch;
|
|
33
|
+
constructor(switchName: MainnetSwitch, message: string);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Refuses a switch that asks for mainnet. Always throws.
|
|
37
|
+
*
|
|
38
|
+
* For `--mainnet`, `context.event` is the GitHub event of the run when it runs
|
|
39
|
+
* in GitHub Actions: on any event but `workflow_dispatch` the message says that
|
|
40
|
+
* rule as well, so a push or a schedule with the switch set reads as refused for
|
|
41
|
+
* that reason too.
|
|
42
|
+
*
|
|
43
|
+
* @throws {MainnetOptInRefusedError} Always.
|
|
44
|
+
*/
|
|
45
|
+
export declare function refuseMainnetSwitch(switchName: MainnetSwitch, context?: {
|
|
46
|
+
readonly event?: string;
|
|
47
|
+
}): never;
|
|
48
|
+
/**
|
|
49
|
+
* The switches that ask the MCP server for mainnet at start-up: `--mainnet`
|
|
50
|
+
* among its arguments (with or without a value), and each start-up variable
|
|
51
|
+
* that holds a value that is not blank. Any value counts, "0" included: the
|
|
52
|
+
* variable is there to be removed. Names are compared in upper case, as a
|
|
53
|
+
* Windows environment does.
|
|
54
|
+
*
|
|
55
|
+
* @param env the server's environment
|
|
56
|
+
* @param args the server's arguments, after the script (`process.argv.slice(2)`)
|
|
57
|
+
*/
|
|
58
|
+
export declare function mainnetStartupSwitches(env: Readonly<Record<string, string | undefined>>, args: readonly string[]): MainnetSwitch[];
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mainnet opt-in switches (spec B5), as this version has them: switches
|
|
3
|
+
* that always refuse (spec B12).
|
|
4
|
+
*
|
|
5
|
+
* A later version may let an x402 run pay on `stellar:pubnet` behind an
|
|
6
|
+
* explicit opt-in. This one does not, and it has no code path that answers
|
|
7
|
+
* "allowed" for it. The switches that opt-in would read exist here only so
|
|
8
|
+
* that a run, a workflow or a server that asks for mainnet is stopped and told
|
|
9
|
+
* why, instead of being run on whatever else it was given:
|
|
10
|
+
*
|
|
11
|
+
* - `--mainnet` on `wasit run` (never read from the config or the environment);
|
|
12
|
+
* - `WASIT_MAINNET_OPT_IN` and `WASIT_MAINNET_CONFIG`, the MCP server's
|
|
13
|
+
* start-up variables, and `--mainnet` among its arguments: the server does
|
|
14
|
+
* not start.
|
|
15
|
+
*
|
|
16
|
+
* The Action's `mainnet` input is refused by the Action's own input check,
|
|
17
|
+
* which runs before Wasit is installed and so cannot import this module.
|
|
18
|
+
*
|
|
19
|
+
* Every function here throws, or only says which switches are set.
|
|
20
|
+
*/
|
|
21
|
+
import { ConfigurationError } from "../errors.js";
|
|
22
|
+
import { NETWORK_CLASSES } from "./networks.js";
|
|
23
|
+
/** The sentence of spec B12. */
|
|
24
|
+
export const MAINNET_OPT_IN_ABSENT = "the mainnet opt-in is not part of this version";
|
|
25
|
+
/** The MCP server's start-up variables that ask for mainnet. */
|
|
26
|
+
const STARTUP_VARIABLES = ["WASIT_MAINNET_OPT_IN", "WASIT_MAINNET_CONFIG"];
|
|
27
|
+
/** An event name as GitHub writes them; anything else is never echoed. */
|
|
28
|
+
const EVENT_NAME = /^[a-z_]{1,64}$/;
|
|
29
|
+
/**
|
|
30
|
+
* A switch asked for mainnet, and this version has no mainnet opt-in. A
|
|
31
|
+
* `ConfigurationError`: it says nothing about any service. Nothing was read,
|
|
32
|
+
* sent or signed when it is thrown.
|
|
33
|
+
*/
|
|
34
|
+
export class MainnetOptInRefusedError extends ConfigurationError {
|
|
35
|
+
switchName;
|
|
36
|
+
constructor(switchName, message) {
|
|
37
|
+
super(message);
|
|
38
|
+
this.name = "MainnetOptInRefusedError";
|
|
39
|
+
this.switchName = switchName;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function testNetworks() {
|
|
43
|
+
return [...NETWORK_CLASSES]
|
|
44
|
+
.filter(([, kind]) => kind === "test")
|
|
45
|
+
.map(([network]) => network)
|
|
46
|
+
.join(", ");
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Refuses a switch that asks for mainnet. Always throws.
|
|
50
|
+
*
|
|
51
|
+
* For `--mainnet`, `context.event` is the GitHub event of the run when it runs
|
|
52
|
+
* in GitHub Actions: on any event but `workflow_dispatch` the message says that
|
|
53
|
+
* rule as well, so a push or a schedule with the switch set reads as refused for
|
|
54
|
+
* that reason too.
|
|
55
|
+
*
|
|
56
|
+
* @throws {MainnetOptInRefusedError} Always.
|
|
57
|
+
*/
|
|
58
|
+
export function refuseMainnetSwitch(switchName, context = {}) {
|
|
59
|
+
const networks = `Wasit pays on test networks only (${testNetworks()}).`;
|
|
60
|
+
if (switchName === "--mainnet") {
|
|
61
|
+
const event = context.event;
|
|
62
|
+
const eventRule = event === undefined || event === "workflow_dispatch"
|
|
63
|
+
? ""
|
|
64
|
+
: ` --mainnet is also refused on every GitHub event but workflow_dispatch, and this run's event is ` +
|
|
65
|
+
`${EVENT_NAME.test(event) ? event : "another event"}.`;
|
|
66
|
+
throw new MainnetOptInRefusedError(switchName, `--mainnet is refused: ${MAINNET_OPT_IN_ABSENT}. ${networks}${eventRule} ` +
|
|
67
|
+
`The config was not read, and nothing was started, sent or signed.`);
|
|
68
|
+
}
|
|
69
|
+
throw new MainnetOptInRefusedError(switchName, `${switchName} is set: ${MAINNET_OPT_IN_ABSENT}, so this server does not start. ${networks} ` +
|
|
70
|
+
`Nothing was read, sent or signed. Remove ${switchName} from the server's environment to start it.`);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The switches that ask the MCP server for mainnet at start-up: `--mainnet`
|
|
74
|
+
* among its arguments (with or without a value), and each start-up variable
|
|
75
|
+
* that holds a value that is not blank. Any value counts, "0" included: the
|
|
76
|
+
* variable is there to be removed. Names are compared in upper case, as a
|
|
77
|
+
* Windows environment does.
|
|
78
|
+
*
|
|
79
|
+
* @param env the server's environment
|
|
80
|
+
* @param args the server's arguments, after the script (`process.argv.slice(2)`)
|
|
81
|
+
*/
|
|
82
|
+
export function mainnetStartupSwitches(env, args) {
|
|
83
|
+
const found = [];
|
|
84
|
+
if (args.some((arg) => arg === "--mainnet" || arg.startsWith("--mainnet=")))
|
|
85
|
+
found.push("--mainnet");
|
|
86
|
+
const set = new Set();
|
|
87
|
+
for (const [name, value] of Object.entries(env)) {
|
|
88
|
+
if (typeof value === "string" && value.trim() !== "")
|
|
89
|
+
set.add(name.toUpperCase());
|
|
90
|
+
}
|
|
91
|
+
for (const name of STARTUP_VARIABLES)
|
|
92
|
+
if (set.has(name))
|
|
93
|
+
found.push(name);
|
|
94
|
+
return found;
|
|
95
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L0, the network guard: which networks a payment may be made on.
|
|
3
|
+
*
|
|
4
|
+
* A fixed table, not a pattern. A network is paid on only after someone adds
|
|
5
|
+
* it here, so a new chain, a new mainnet or a typo is refused rather than
|
|
6
|
+
* paid on (fail closed). The guard is called from inside every core entry
|
|
7
|
+
* point that can sign, so the CLI, the MCP server, the dashboard and any
|
|
8
|
+
* third party using @wasit-dev/core get the same refusals; a guard in each
|
|
9
|
+
* front end would be three copies, and one forgotten copy is a hole.
|
|
10
|
+
*
|
|
11
|
+
* `stellar:pubnet` has its own class because a later version may let an x402
|
|
12
|
+
* run pay on it behind an explicit opt-in. This version has no such opt-in,
|
|
13
|
+
* so this module has no path that allows it.
|
|
14
|
+
*/
|
|
15
|
+
import { ConfigurationError } from "../errors.js";
|
|
16
|
+
/** How the guard treats a network. */
|
|
17
|
+
export type NetworkClass = "test" | "mainnet-optin" | "unknown";
|
|
18
|
+
/** The protocol that would sign. MPP has no mainnet path at all. */
|
|
19
|
+
export type PaymentProtocol = "x402" | "mpp-charge" | "mpp-channel";
|
|
20
|
+
/**
|
|
21
|
+
* Every network the guard knows. Test networks are the four the payment
|
|
22
|
+
* checks pay on; `stellar:pubnet` is the only mainnet listed. Anything absent
|
|
23
|
+
* is `unknown`, and refused.
|
|
24
|
+
*/
|
|
25
|
+
export declare const NETWORK_CLASSES: ReadonlyMap<string, Exclude<NetworkClass, "unknown">>;
|
|
26
|
+
/** The class of `network`; anything not in the table, or not a string, is `unknown`. */
|
|
27
|
+
export declare function networkClass(network: unknown): NetworkClass;
|
|
28
|
+
/**
|
|
29
|
+
* A payment was refused because of the network it would be made on.
|
|
30
|
+
*
|
|
31
|
+
* A `ConfigurationError`: it says nothing about the target, only that this run
|
|
32
|
+
* asked for something Wasit will not do. Nothing was sent when it is thrown.
|
|
33
|
+
*/
|
|
34
|
+
export declare class PaymentNetworkRefusedError extends ConfigurationError {
|
|
35
|
+
readonly network: string;
|
|
36
|
+
readonly networkClass: NetworkClass;
|
|
37
|
+
readonly protocol: PaymentProtocol;
|
|
38
|
+
constructor(protocol: PaymentProtocol, network: unknown, kind: NetworkClass, message: string);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Throws unless a payment by `protocol` may be made on `network`.
|
|
42
|
+
*
|
|
43
|
+
* Call it before any request is sent and before any key or signer is used.
|
|
44
|
+
* Only test networks pass. `stellar:pubnet` is refused for every protocol in
|
|
45
|
+
* this version: MPP stays on test networks for good, and x402 on mainnet
|
|
46
|
+
* needs an opt-in this version does not have.
|
|
47
|
+
*
|
|
48
|
+
* @throws {PaymentNetworkRefusedError} For any network that is not a test network.
|
|
49
|
+
*/
|
|
50
|
+
export declare function assertPaymentNetworkAllowed(protocol: PaymentProtocol, network: string): void;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L0, the network guard: which networks a payment may be made on.
|
|
3
|
+
*
|
|
4
|
+
* A fixed table, not a pattern. A network is paid on only after someone adds
|
|
5
|
+
* it here, so a new chain, a new mainnet or a typo is refused rather than
|
|
6
|
+
* paid on (fail closed). The guard is called from inside every core entry
|
|
7
|
+
* point that can sign, so the CLI, the MCP server, the dashboard and any
|
|
8
|
+
* third party using @wasit-dev/core get the same refusals; a guard in each
|
|
9
|
+
* front end would be three copies, and one forgotten copy is a hole.
|
|
10
|
+
*
|
|
11
|
+
* `stellar:pubnet` has its own class because a later version may let an x402
|
|
12
|
+
* run pay on it behind an explicit opt-in. This version has no such opt-in,
|
|
13
|
+
* so this module has no path that allows it.
|
|
14
|
+
*/
|
|
15
|
+
import { ConfigurationError } from "../errors.js";
|
|
16
|
+
/**
|
|
17
|
+
* Every network the guard knows. Test networks are the four the payment
|
|
18
|
+
* checks pay on; `stellar:pubnet` is the only mainnet listed. Anything absent
|
|
19
|
+
* is `unknown`, and refused.
|
|
20
|
+
*/
|
|
21
|
+
export const NETWORK_CLASSES = new Map([
|
|
22
|
+
["stellar:testnet", "test"],
|
|
23
|
+
["eip155:84532", "test"], // Base Sepolia
|
|
24
|
+
["eip155:11155111", "test"], // Ethereum Sepolia
|
|
25
|
+
["solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1", "test"], // Solana devnet
|
|
26
|
+
["stellar:pubnet", "mainnet-optin"],
|
|
27
|
+
]);
|
|
28
|
+
/** The class of `network`; anything not in the table, or not a string, is `unknown`. */
|
|
29
|
+
export function networkClass(network) {
|
|
30
|
+
return (typeof network === "string" && NETWORK_CLASSES.get(network)) || "unknown";
|
|
31
|
+
}
|
|
32
|
+
/** The test networks, in table order, for messages. */
|
|
33
|
+
function testNetworks() {
|
|
34
|
+
return [...NETWORK_CLASSES]
|
|
35
|
+
.filter(([, kind]) => kind === "test")
|
|
36
|
+
.map(([network]) => network)
|
|
37
|
+
.join(", ");
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A payment was refused because of the network it would be made on.
|
|
41
|
+
*
|
|
42
|
+
* A `ConfigurationError`: it says nothing about the target, only that this run
|
|
43
|
+
* asked for something Wasit will not do. Nothing was sent when it is thrown.
|
|
44
|
+
*/
|
|
45
|
+
export class PaymentNetworkRefusedError extends ConfigurationError {
|
|
46
|
+
network;
|
|
47
|
+
networkClass;
|
|
48
|
+
protocol;
|
|
49
|
+
constructor(protocol, network, kind, message) {
|
|
50
|
+
super(`${message} Nothing was sent and nothing was signed.`);
|
|
51
|
+
this.name = "PaymentNetworkRefusedError";
|
|
52
|
+
this.network = String(network);
|
|
53
|
+
this.networkClass = kind;
|
|
54
|
+
this.protocol = protocol;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Throws unless a payment by `protocol` may be made on `network`.
|
|
59
|
+
*
|
|
60
|
+
* Call it before any request is sent and before any key or signer is used.
|
|
61
|
+
* Only test networks pass. `stellar:pubnet` is refused for every protocol in
|
|
62
|
+
* this version: MPP stays on test networks for good, and x402 on mainnet
|
|
63
|
+
* needs an opt-in this version does not have.
|
|
64
|
+
*
|
|
65
|
+
* @throws {PaymentNetworkRefusedError} For any network that is not a test network.
|
|
66
|
+
*/
|
|
67
|
+
export function assertPaymentNetworkAllowed(protocol, network) {
|
|
68
|
+
const kind = networkClass(network);
|
|
69
|
+
if (kind === "test")
|
|
70
|
+
return;
|
|
71
|
+
if (kind === "mainnet-optin") {
|
|
72
|
+
throw new PaymentNetworkRefusedError(protocol, network, kind, protocol === "x402"
|
|
73
|
+
? `${network} is Stellar mainnet. This version of Wasit pays on test networks ` +
|
|
74
|
+
`only (${testNetworks()}); the mainnet opt-in is not part of this version.`
|
|
75
|
+
: `${network} is Stellar mainnet, and MPP stays on test networks: the MPP ` +
|
|
76
|
+
`checks pay on stellar:testnet only.`);
|
|
77
|
+
}
|
|
78
|
+
throw new PaymentNetworkRefusedError(protocol, network, kind, `${JSON.stringify(network) ?? String(network)} is not on Wasit's list of test networks ` +
|
|
79
|
+
`(${testNetworks()}), so no payment is made on it.`);
|
|
80
|
+
}
|