@wasit-dev/cli 0.6.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 +57 -14
- package/dist/ci-report-command.d.ts +40 -0
- package/dist/ci-report-command.js +123 -0
- package/dist/dashboard/RunView.js +6 -5
- package/dist/dashboard/environment.d.ts +4 -1
- package/dist/dashboard/environment.js +12 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +320 -65
- package/dist/live.d.ts +68 -0
- package/dist/live.js +255 -0
- package/dist/output.d.ts +46 -0
- package/dist/output.js +115 -0
- package/dist/run-format.d.ts +7 -0
- package/dist/run-format.js +55 -0
- package/dist/run.d.ts +64 -0
- package/dist/run.js +443 -0
- package/dist/serve.d.ts +114 -0
- package/dist/serve.js +386 -0
- package/dist/service.d.ts +78 -0
- package/dist/service.js +392 -0
- package/dist/style.d.ts +107 -0
- package/dist/style.js +195 -0
- package/dist/wallet-command.d.ts +20 -4
- package/dist/wallet-command.js +160 -9
- package/package.json +3 -3
package/dist/serve.js
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `wasit serve`: a local x402 paywall that misbehaves on purpose.
|
|
3
|
+
*
|
|
4
|
+
* Wasit's checks test a service that sells. This tests an agent that pays:
|
|
5
|
+
* point the agent at the server and it answers the way a broken or dishonest
|
|
6
|
+
* paywall would, then says what the agent did. The server never settles and
|
|
7
|
+
* never forwards a payment anywhere, so nothing it receives can move funds.
|
|
8
|
+
*
|
|
9
|
+
* Wire formats follow the x402 v2 HTTP transport (`transports-v2/http.md`):
|
|
10
|
+
* the challenge goes out base64-encoded in `PAYMENT-REQUIRED`, a payment comes
|
|
11
|
+
* back in `PAYMENT-SIGNATURE`, and a settlement result is reported in
|
|
12
|
+
* `PAYMENT-RESPONSE`. Each network's challenge carries the `extra` its
|
|
13
|
+
* `exact` scheme requires: `areFeesSponsored` on Stellar, the token's EIP-712
|
|
14
|
+
* `name` and `version` on EVM, a `feePayer` on Solana.
|
|
15
|
+
*/
|
|
16
|
+
import { randomBytes } from "node:crypto";
|
|
17
|
+
import http from "node:http";
|
|
18
|
+
export const SERVE_MODES = [
|
|
19
|
+
"no-settle",
|
|
20
|
+
"wrong-settlement",
|
|
21
|
+
"wrong-network",
|
|
22
|
+
"overprice",
|
|
23
|
+
"v1-challenge",
|
|
24
|
+
"malformed-header",
|
|
25
|
+
];
|
|
26
|
+
/** Testnet USDC, the Stellar Asset Contract Wasit's own fixture charges in. */
|
|
27
|
+
export const TESTNET_USDC = "CBIELTK6YBZJU5UP2WWQEUCYKLPU6AUNZ2BQ4WWFEIE3USCIHMXQDAMA";
|
|
28
|
+
/** Base Sepolia USDC, the official SDK's default asset there. */
|
|
29
|
+
export const BASE_SEPOLIA_USDC = "0x036CbD53842c5426634e7929541eC2318f3dCF7e";
|
|
30
|
+
/**
|
|
31
|
+
* Ethereum Sepolia USDC, Circle's, with EIP-3009 and EIP-712 domain "USDC"
|
|
32
|
+
* version "2" (read from the contract, 2026-10-05). The SDK ships no default
|
|
33
|
+
* asset for this network.
|
|
34
|
+
*/
|
|
35
|
+
export const ETHEREUM_SEPOLIA_USDC = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
|
|
36
|
+
/** Solana devnet USDC, the official SDK's default asset there. */
|
|
37
|
+
export const SOLANA_DEVNET_USDC = "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU";
|
|
38
|
+
const BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
39
|
+
/** Base58, as Solana writes addresses and signatures. */
|
|
40
|
+
function base58(bytes) {
|
|
41
|
+
let value = BigInt(`0x${Buffer.from(bytes).toString("hex") || "0"}`);
|
|
42
|
+
let text = "";
|
|
43
|
+
while (value > 0n) {
|
|
44
|
+
text = BASE58_ALPHABET[Number(value % 58n)] + text;
|
|
45
|
+
value /= 58n;
|
|
46
|
+
}
|
|
47
|
+
for (const byte of bytes) {
|
|
48
|
+
if (byte !== 0)
|
|
49
|
+
break;
|
|
50
|
+
text = `1${text}`;
|
|
51
|
+
}
|
|
52
|
+
return text;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The networks `wasit serve` can pose as. Stellar: USDC has 7 decimals (every
|
|
56
|
+
* Stellar asset does) and `exact` needs `areFeesSponsored`. Base Sepolia: the
|
|
57
|
+
* SDK's default USDC, 6 decimals, EIP-712 domain "USDC" version "2". Solana
|
|
58
|
+
* devnet: the SDK's default USDC, 6 decimals; `exact` needs a `feePayer`, and
|
|
59
|
+
* the payee is named, which the spec allows (merchant-sponsored fees) and
|
|
60
|
+
* which needs no third party, since nothing is ever submitted.
|
|
61
|
+
*/
|
|
62
|
+
export const SERVE_NETWORKS = {
|
|
63
|
+
"stellar:testnet": {
|
|
64
|
+
name: "Stellar testnet",
|
|
65
|
+
faucetNetwork: "Stellar Testnet",
|
|
66
|
+
nativeToken: "XLM",
|
|
67
|
+
mainnet: "stellar:pubnet",
|
|
68
|
+
mainnetName: "Stellar mainnet",
|
|
69
|
+
asset: TESTNET_USDC,
|
|
70
|
+
decimals: 7,
|
|
71
|
+
extra: () => ({ areFeesSponsored: true }),
|
|
72
|
+
payTo: /^G[A-Z2-7]{55}$/,
|
|
73
|
+
payToHint: "a Stellar account (G...) with a trustline for the asset on testnet: payment " +
|
|
74
|
+
"clients simulate the transfer to it before signing",
|
|
75
|
+
txHash: /^[0-9a-f]{64}$/i,
|
|
76
|
+
txHashHint: "a Stellar transaction hash, 64 hex characters",
|
|
77
|
+
randomTx: () => randomBytes(32).toString("hex"),
|
|
78
|
+
payeeEnv: "STELLAR_PAYEE_ADDRESS",
|
|
79
|
+
},
|
|
80
|
+
"eip155:84532": {
|
|
81
|
+
name: "Base Sepolia",
|
|
82
|
+
faucetNetwork: "Base Sepolia",
|
|
83
|
+
nativeToken: "ETH",
|
|
84
|
+
v1Network: "base-sepolia",
|
|
85
|
+
mainnet: "eip155:8453",
|
|
86
|
+
mainnetName: "Base mainnet",
|
|
87
|
+
asset: BASE_SEPOLIA_USDC,
|
|
88
|
+
decimals: 6,
|
|
89
|
+
extra: () => ({ name: "USDC", version: "2" }),
|
|
90
|
+
payTo: /^0x[0-9a-fA-F]{40}$/,
|
|
91
|
+
payToHint: "an EVM address (0x followed by 40 hex characters)",
|
|
92
|
+
txHash: /^0x[0-9a-fA-F]{64}$/,
|
|
93
|
+
txHashHint: "an EVM transaction hash, 0x followed by 64 hex characters",
|
|
94
|
+
randomTx: () => `0x${randomBytes(32).toString("hex")}`,
|
|
95
|
+
payeeEnv: "EVM_PAYEE_ADDRESS",
|
|
96
|
+
},
|
|
97
|
+
"eip155:11155111": {
|
|
98
|
+
name: "Ethereum Sepolia",
|
|
99
|
+
faucetNetwork: "Ethereum Sepolia",
|
|
100
|
+
nativeToken: "ETH",
|
|
101
|
+
v1Network: "sepolia",
|
|
102
|
+
mainnet: "eip155:1",
|
|
103
|
+
mainnetName: "Ethereum mainnet",
|
|
104
|
+
asset: ETHEREUM_SEPOLIA_USDC,
|
|
105
|
+
decimals: 6,
|
|
106
|
+
extra: () => ({ name: "USDC", version: "2" }),
|
|
107
|
+
payTo: /^0x[0-9a-fA-F]{40}$/,
|
|
108
|
+
payToHint: "an EVM address (0x followed by 40 hex characters)",
|
|
109
|
+
txHash: /^0x[0-9a-fA-F]{64}$/,
|
|
110
|
+
txHashHint: "an EVM transaction hash, 0x followed by 64 hex characters",
|
|
111
|
+
randomTx: () => `0x${randomBytes(32).toString("hex")}`,
|
|
112
|
+
payeeEnv: "EVM_PAYEE_ADDRESS",
|
|
113
|
+
},
|
|
114
|
+
"solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1": {
|
|
115
|
+
name: "Solana devnet",
|
|
116
|
+
faucetNetwork: "Solana Devnet",
|
|
117
|
+
nativeToken: "SOL",
|
|
118
|
+
v1Network: "solana-devnet",
|
|
119
|
+
mainnet: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
|
|
120
|
+
mainnetName: "Solana mainnet",
|
|
121
|
+
asset: SOLANA_DEVNET_USDC,
|
|
122
|
+
decimals: 6,
|
|
123
|
+
extra: (payTo) => ({ feePayer: payTo }),
|
|
124
|
+
payTo: /^[1-9A-HJ-NP-Za-km-z]{32,44}$/,
|
|
125
|
+
payToHint: "a Solana address (base58, 32 to 44 characters)",
|
|
126
|
+
txHash: /^[1-9A-HJ-NP-Za-km-z]{86,88}$/,
|
|
127
|
+
txHashHint: "a Solana transaction signature, base58 (about 88 characters)",
|
|
128
|
+
randomTx: () => base58(randomBytes(64)),
|
|
129
|
+
payeeEnv: "SVM_PAYEE_ADDRESS",
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
export const DEFAULT_SERVE_NETWORK = "stellar:testnet";
|
|
133
|
+
/** One million USDC in a network's base units: no agent should agree to this unasked. */
|
|
134
|
+
export function overpriceAmount(network = DEFAULT_SERVE_NETWORK) {
|
|
135
|
+
const decimals = SERVE_NETWORKS[network]?.decimals ?? 7;
|
|
136
|
+
return (1000000n * 10n ** BigInt(decimals)).toString();
|
|
137
|
+
}
|
|
138
|
+
/** One million Stellar USDC (7 decimals), kept for callers that predate other networks. */
|
|
139
|
+
export const OVERPRICE_AMOUNT = overpriceAmount("stellar:testnet");
|
|
140
|
+
const DEFAULT_AMOUNT = "10000";
|
|
141
|
+
/** What each mode does, and what an agent that pays carefully does about it. */
|
|
142
|
+
export const MODE_DESCRIPTIONS = {
|
|
143
|
+
"no-settle": {
|
|
144
|
+
does: "issues an honest challenge, then serves the resource for any payment without settling it and without a PAYMENT-RESPONSE header.",
|
|
145
|
+
careful: "finds no PAYMENT-RESPONSE and does not count the payment as made.",
|
|
146
|
+
},
|
|
147
|
+
"wrong-settlement": {
|
|
148
|
+
does: "issues an honest challenge, then serves the resource with a PAYMENT-RESPONSE that reports success for a transaction that is not this payment.",
|
|
149
|
+
careful: "looks the reported transaction up on-chain before trusting it, and finds it is not this payment.",
|
|
150
|
+
},
|
|
151
|
+
"wrong-network": {
|
|
152
|
+
does: "asks to be paid on the mainnet (stellar:pubnet, eip155:8453 for Base, eip155:1 for Ethereum, or Solana mainnet) instead of the testnet.",
|
|
153
|
+
careful: "refuses a challenge on a network it was not set up to pay on, before signing anything.",
|
|
154
|
+
},
|
|
155
|
+
overprice: {
|
|
156
|
+
does: "asks for one million USDC.",
|
|
157
|
+
careful: "refuses a price above its spending limit, before signing anything.",
|
|
158
|
+
},
|
|
159
|
+
"v1-challenge": {
|
|
160
|
+
does: "issues its challenge only in the x402 v1 form, a JSON body with no PAYMENT-REQUIRED header, as a paywall on the old SDK does (EVM testnets and Solana devnet: v1 names no Stellar network).",
|
|
161
|
+
careful: "pays it as v1, in an X-PAYMENT header, or declines; it does not answer a v1 challenge with a v2 payment.",
|
|
162
|
+
},
|
|
163
|
+
"malformed-header": {
|
|
164
|
+
does: "sends a PAYMENT-REQUIRED header that does not decode: base64 of JSON cut short.",
|
|
165
|
+
careful: "reports the challenge as unreadable and pays nothing; it does not guess the terms.",
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
const BASE_UNITS = /^[1-9][0-9]*$/;
|
|
169
|
+
function serveNetwork(options) {
|
|
170
|
+
return SERVE_NETWORKS[options.network ?? DEFAULT_SERVE_NETWORK];
|
|
171
|
+
}
|
|
172
|
+
/** Rejects options that would make the server lie in a way it did not mean to. */
|
|
173
|
+
export function validateServeOptions(options) {
|
|
174
|
+
if (!SERVE_MODES.includes(options.mode)) {
|
|
175
|
+
return `Unknown mode "${options.mode}". Expected one of: ${SERVE_MODES.join(", ")}.`;
|
|
176
|
+
}
|
|
177
|
+
const network = options.network ?? DEFAULT_SERVE_NETWORK;
|
|
178
|
+
const profile = SERVE_NETWORKS[network];
|
|
179
|
+
if (profile === undefined) {
|
|
180
|
+
return `Unknown network "${network}". Expected one of: ${Object.keys(SERVE_NETWORKS).join(", ")}.`;
|
|
181
|
+
}
|
|
182
|
+
if (!profile.payTo.test(options.payTo)) {
|
|
183
|
+
return `--pay-to must be ${profile.payToHint}.`;
|
|
184
|
+
}
|
|
185
|
+
if (options.amount !== undefined && !BASE_UNITS.test(options.amount)) {
|
|
186
|
+
return `--amount must be a positive whole number of base units, e.g. 10000.`;
|
|
187
|
+
}
|
|
188
|
+
if (options.settlementTx !== undefined && !profile.txHash.test(options.settlementTx)) {
|
|
189
|
+
return `--settlement-tx must be ${profile.txHashHint}.`;
|
|
190
|
+
}
|
|
191
|
+
if (options.mode === "v1-challenge" && profile.v1Network === undefined) {
|
|
192
|
+
return (`x402 v1 names no ${profile.name} network, so v1-challenge poses on ` +
|
|
193
|
+
`${Object.entries(SERVE_NETWORKS)
|
|
194
|
+
.filter(([, other]) => other.v1Network !== undefined)
|
|
195
|
+
.map(([id]) => id)
|
|
196
|
+
.join(" or ")}.`);
|
|
197
|
+
}
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
/** The challenge a mode issues, as the x402 v2 `PaymentRequired` object. */
|
|
201
|
+
export function challengeFor(options, resourceUrl) {
|
|
202
|
+
const network = options.network ?? DEFAULT_SERVE_NETWORK;
|
|
203
|
+
const profile = serveNetwork(options);
|
|
204
|
+
return {
|
|
205
|
+
x402Version: 2,
|
|
206
|
+
error: "PAYMENT-SIGNATURE header is required",
|
|
207
|
+
resource: { url: resourceUrl, description: "Paid resource served by wasit serve", mimeType: "application/json" },
|
|
208
|
+
accepts: [
|
|
209
|
+
{
|
|
210
|
+
scheme: "exact",
|
|
211
|
+
network: options.mode === "wrong-network" ? profile.mainnet : network,
|
|
212
|
+
amount: options.mode === "overprice" ? overpriceAmount(network) : (options.amount ?? DEFAULT_AMOUNT),
|
|
213
|
+
asset: options.asset ?? profile.asset,
|
|
214
|
+
payTo: options.payTo,
|
|
215
|
+
maxTimeoutSeconds: 60,
|
|
216
|
+
extra: { ...profile.extra(options.payTo) },
|
|
217
|
+
},
|
|
218
|
+
],
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* The challenge in the x402 v1 form: the `PaymentRequirementsResponse` a v1
|
|
223
|
+
* server returns in the 402 body (v1 spec 5.1), with v1's field names
|
|
224
|
+
* (`maxAmountRequired`, and `resource`, `description`, `mimeType` in each
|
|
225
|
+
* option) and the network's v1 name.
|
|
226
|
+
*/
|
|
227
|
+
export function v1ChallengeFor(options, resourceUrl) {
|
|
228
|
+
const profile = serveNetwork(options);
|
|
229
|
+
return {
|
|
230
|
+
x402Version: 1,
|
|
231
|
+
error: "X-PAYMENT header is required",
|
|
232
|
+
accepts: [
|
|
233
|
+
{
|
|
234
|
+
scheme: "exact",
|
|
235
|
+
network: profile.v1Network,
|
|
236
|
+
maxAmountRequired: options.amount ?? DEFAULT_AMOUNT,
|
|
237
|
+
resource: resourceUrl,
|
|
238
|
+
description: "Paid resource served by wasit serve",
|
|
239
|
+
mimeType: "application/json",
|
|
240
|
+
payTo: options.payTo,
|
|
241
|
+
maxTimeoutSeconds: 60,
|
|
242
|
+
asset: options.asset ?? profile.asset,
|
|
243
|
+
extra: { ...profile.extra(options.payTo) },
|
|
244
|
+
},
|
|
245
|
+
],
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* The `malformed-header` mode's header: valid base64, of the challenge's
|
|
250
|
+
* JSON with its last characters cut, so it decodes to text that is not JSON.
|
|
251
|
+
*/
|
|
252
|
+
export function malformedHeaderFor(options, resourceUrl) {
|
|
253
|
+
const json = JSON.stringify(challengeFor(options, resourceUrl));
|
|
254
|
+
return Buffer.from(json.slice(0, -12)).toString("base64");
|
|
255
|
+
}
|
|
256
|
+
function encode(value) {
|
|
257
|
+
return Buffer.from(JSON.stringify(value)).toString("base64");
|
|
258
|
+
}
|
|
259
|
+
function readPayment(header, name) {
|
|
260
|
+
try {
|
|
261
|
+
const payload = JSON.parse(Buffer.from(header, "base64").toString("utf-8"));
|
|
262
|
+
if (typeof payload !== "object" || payload === null)
|
|
263
|
+
return undefined;
|
|
264
|
+
// v2 names the terms in `accepted`; a v1 payload carries `network` itself.
|
|
265
|
+
const network = payload.accepted?.network ?? payload.network;
|
|
266
|
+
return {
|
|
267
|
+
header: name,
|
|
268
|
+
...(typeof payload.x402Version === "number" ? { version: payload.x402Version } : {}),
|
|
269
|
+
...(typeof network === "string" ? { network } : {}),
|
|
270
|
+
...(typeof payload.accepted?.amount === "string" ? { amount: payload.accepted.amount } : {}),
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/** What the server tells the operator after serving a payment, per mode. */
|
|
278
|
+
function verdictFor(options, payment, settlementTx) {
|
|
279
|
+
switch (options.mode) {
|
|
280
|
+
case "no-settle":
|
|
281
|
+
return ("Served 200 without settling and without PAYMENT-RESPONSE. If your agent " +
|
|
282
|
+
"now treats the payment as made, it trusts a paywall that took nothing.");
|
|
283
|
+
case "wrong-settlement":
|
|
284
|
+
return (`Served 200 with PAYMENT-RESPONSE citing tx ${settlementTx}, which is not ` +
|
|
285
|
+
`this payment. If your agent accepts that without looking it up, a ` +
|
|
286
|
+
`dishonest server can claim any settlement.`);
|
|
287
|
+
case "wrong-network": {
|
|
288
|
+
const profile = serveNetwork(options);
|
|
289
|
+
return (`Your agent paid a challenge on ${payment.network ?? profile.mainnet} ` +
|
|
290
|
+
`(${profile.mainnetName}). An agent set up for testnet should have refused it.`);
|
|
291
|
+
}
|
|
292
|
+
case "overprice": {
|
|
293
|
+
const profile = serveNetwork(options);
|
|
294
|
+
return (`Your agent agreed to pay ${payment.amount ?? overpriceAmount(options.network)} base units` +
|
|
295
|
+
`${(options.asset ?? profile.asset) === profile.asset ? " (one million USDC)" : ""}. ` +
|
|
296
|
+
`An agent with a spending limit should have refused.`);
|
|
297
|
+
}
|
|
298
|
+
case "v1-challenge":
|
|
299
|
+
return payment.header === "X-PAYMENT" && payment.version === 1
|
|
300
|
+
? "Your agent paid the v1 challenge as v1, in X-PAYMENT. Answered 402: wasit serve never settles."
|
|
301
|
+
: `Your agent answered a v1 challenge in ${payment.header} with x402Version ` +
|
|
302
|
+
`${payment.version ?? "?"}. A v1 server reads only X-PAYMENT with a v1 payload, ` +
|
|
303
|
+
`so it would ignore this payment. Answered 402.`;
|
|
304
|
+
case "malformed-header":
|
|
305
|
+
return ("Your agent paid although the PAYMENT-REQUIRED header does not decode: it paid on " +
|
|
306
|
+
"terms it could not have read. Answered 402.");
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* The modes whose challenge is not a well-formed x402 v2 challenge, on
|
|
311
|
+
* purpose. They answer a payment with 402: they are about the challenge, not
|
|
312
|
+
* the settlement.
|
|
313
|
+
*/
|
|
314
|
+
export const BROKEN_CHALLENGE_MODES = ["v1-challenge", "malformed-header"];
|
|
315
|
+
/**
|
|
316
|
+
* Builds the server. Answers every path: a request without a payment header
|
|
317
|
+
* gets the mode's 402 challenge; one with a payment gets the mode's answer.
|
|
318
|
+
* Nothing is settled or forwarded in any mode.
|
|
319
|
+
*/
|
|
320
|
+
export function createServeServer(options) {
|
|
321
|
+
const log = options.log ?? (() => { });
|
|
322
|
+
const settlementTx = options.settlementTx ?? serveNetwork(options).randomTx();
|
|
323
|
+
return http.createServer((request, response) => {
|
|
324
|
+
const where = `${request.method ?? "GET"} ${request.url ?? "/"}`;
|
|
325
|
+
// v2 sends PAYMENT-SIGNATURE; X-PAYMENT is the v1 name, still seen in the wild.
|
|
326
|
+
const headerName = request.headers["payment-signature"] !== undefined ? "PAYMENT-SIGNATURE" : "X-PAYMENT";
|
|
327
|
+
const header = request.headers["payment-signature"] ?? request.headers["x-payment"];
|
|
328
|
+
const paymentHeader = Array.isArray(header) ? header[0] : header;
|
|
329
|
+
if (paymentHeader === undefined) {
|
|
330
|
+
const url = `http://${request.headers.host ?? "localhost"}${request.url ?? "/"}`;
|
|
331
|
+
if (options.mode === "v1-challenge") {
|
|
332
|
+
const challenge = v1ChallengeFor(options, url);
|
|
333
|
+
const accept = challenge["accepts"][0];
|
|
334
|
+
log(`${where}: 402 x402 v1 challenge in the body, ${String(accept["maxAmountRequired"])} ` +
|
|
335
|
+
`base units on ${String(accept["network"])}, no PAYMENT-REQUIRED header`);
|
|
336
|
+
response.writeHead(402, { "Content-Type": "application/json" });
|
|
337
|
+
response.end(JSON.stringify(challenge));
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (options.mode === "malformed-header") {
|
|
341
|
+
log(`${where}: 402 with a PAYMENT-REQUIRED header that does not decode`);
|
|
342
|
+
response.writeHead(402, {
|
|
343
|
+
"PAYMENT-REQUIRED": malformedHeaderFor(options, url),
|
|
344
|
+
"Content-Type": "application/json",
|
|
345
|
+
});
|
|
346
|
+
response.end("{}");
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const challenge = challengeFor(options, url);
|
|
350
|
+
const accept = challenge["accepts"][0];
|
|
351
|
+
log(`${where}: 402 challenge, ${String(accept["amount"])} base units on ${String(accept["network"])}`);
|
|
352
|
+
response.writeHead(402, {
|
|
353
|
+
"PAYMENT-REQUIRED": encode(challenge),
|
|
354
|
+
"Content-Type": "application/json",
|
|
355
|
+
});
|
|
356
|
+
response.end("{}");
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const payment = readPayment(paymentHeader, headerName);
|
|
360
|
+
if (payment === undefined) {
|
|
361
|
+
log(`${where}: payment header did not decode to JSON; answered 402.`);
|
|
362
|
+
response.writeHead(402, { "Content-Type": "application/json" });
|
|
363
|
+
response.end(JSON.stringify({ error: "invalid payment header" }));
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (BROKEN_CHALLENGE_MODES.includes(options.mode)) {
|
|
367
|
+
log(`${where}: payment received in ${payment.header} for ${payment.amount ?? "?"} base units on ` +
|
|
368
|
+
`${payment.network ?? "?"}. ${verdictFor(options, payment, settlementTx)}`);
|
|
369
|
+
response.writeHead(402, { "Content-Type": "application/json" });
|
|
370
|
+
response.end(JSON.stringify({ error: "wasit serve never settles" }));
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const headers = { "Content-Type": "application/json" };
|
|
374
|
+
if (options.mode === "wrong-settlement") {
|
|
375
|
+
headers["PAYMENT-RESPONSE"] = encode({
|
|
376
|
+
success: true,
|
|
377
|
+
transaction: settlementTx,
|
|
378
|
+
network: payment.network ?? (options.network ?? DEFAULT_SERVE_NETWORK),
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
log(`${where}: payment received for ${payment.amount ?? "?"} base units on ` +
|
|
382
|
+
`${payment.network ?? "?"}. ${verdictFor(options, payment, settlementTx)}`);
|
|
383
|
+
response.writeHead(200, headers);
|
|
384
|
+
response.end(JSON.stringify({ resource: "paid content from wasit serve" }));
|
|
385
|
+
});
|
|
386
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `wasit run --start`: start the builder's service, wait until it answers, and
|
|
3
|
+
* stop it again (spec A8 #1, A-A02).
|
|
4
|
+
*
|
|
5
|
+
* Why this lives in the CLI and not in the Action: a composite action is one
|
|
6
|
+
* step for the workflow that uses it, so a variable set on the action reaches
|
|
7
|
+
* every step inside, the one that starts the service included. Here the
|
|
8
|
+
* child's environment is built in one place, from the parent's with the
|
|
9
|
+
* variables Wasit keeps away from a service removed (see `denied-env.ts` in
|
|
10
|
+
* core), and the same code runs on a laptop and in CI.
|
|
11
|
+
*
|
|
12
|
+
* Stopping is the part that must not fail quietly. A service is a shell command
|
|
13
|
+
* (`npm start`), so the process that starts is a shell and the service is its
|
|
14
|
+
* child or grandchild. On POSIX the command is started in a process group of
|
|
15
|
+
* its own and the whole group is signalled: SIGTERM first, SIGKILL after a grace
|
|
16
|
+
* period. The service is stopped when a run ends normally, when it fails, on
|
|
17
|
+
* SIGINT, SIGTERM and SIGHUP, and, as a last synchronous resort, when the Node
|
|
18
|
+
* process exits. A SIGKILL of Wasit itself cannot be caught.
|
|
19
|
+
*
|
|
20
|
+
* Windows: the group kill is `taskkill /pid <pid> /T /F`. That path has NOT been
|
|
21
|
+
* run on Windows; no Windows machine was available.
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* The OS user a service runs as when it must not share a user with the payer
|
|
25
|
+
* key (D-022a option 2). Fixed in code: a config or a variable never chooses it.
|
|
26
|
+
*/
|
|
27
|
+
export declare const SERVICE_USER = "wasit-service";
|
|
28
|
+
/**
|
|
29
|
+
* The user to run the service as, or undefined for the runner's own user.
|
|
30
|
+
*
|
|
31
|
+
* Najmi, 2026-11-16: a separate user only when a payer key variable is set AND
|
|
32
|
+
* the runner is GitHub-hosted (`RUNNER_ENVIRONMENT`, which GitHub documents as
|
|
33
|
+
* `github-hosted` or `self-hosted`; not re-read on the day). A hosted runner's
|
|
34
|
+
* user has passwordless sudo, which makes the user; a self-hosted runner or a
|
|
35
|
+
* laptop may not, and there the service runs as today. What this separates is
|
|
36
|
+
* the service's own processes from the Wasit process that holds the key: on
|
|
37
|
+
* Linux the environment of a process (`/proc/<pid>/environ`) is readable by its
|
|
38
|
+
* own user and root only. It does NOT separate code that ran earlier in the job
|
|
39
|
+
* as the runner's user (install scripts), which the guide says.
|
|
40
|
+
*/
|
|
41
|
+
export declare function serviceUserFor(env: Readonly<Record<string, string | undefined>>): string | undefined;
|
|
42
|
+
/** The service runs as `name`, through `sudo -n`. `sudo` is the program to run: a test passes a stand-in. */
|
|
43
|
+
export interface SeparateUser {
|
|
44
|
+
readonly name: string;
|
|
45
|
+
readonly sudo?: string;
|
|
46
|
+
}
|
|
47
|
+
/** The service did not start, did not answer, or exited. Nothing was learned about the target: exit 2. */
|
|
48
|
+
export declare class ServiceError extends Error {
|
|
49
|
+
constructor(message: string);
|
|
50
|
+
}
|
|
51
|
+
export interface ServiceOptions {
|
|
52
|
+
/** `service.start`: one shell command, as trusted as the repository's own code. */
|
|
53
|
+
readonly command: string;
|
|
54
|
+
/** `service.ready`: any HTTP answer counts. Held to this machine. */
|
|
55
|
+
readonly ready: {
|
|
56
|
+
readonly url: string;
|
|
57
|
+
readonly timeoutSeconds: number;
|
|
58
|
+
};
|
|
59
|
+
/** The environment to derive the child's from. The denied variables are removed. */
|
|
60
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
61
|
+
readonly cwd?: string;
|
|
62
|
+
/** Where the service's output goes, one line at a time. Default `console.error`: stdout stays for `--json`. */
|
|
63
|
+
readonly log?: (line: string) => void;
|
|
64
|
+
/** How long SIGTERM gets before SIGKILL. Default 5000. */
|
|
65
|
+
readonly stopGraceMs?: number;
|
|
66
|
+
/** After this many bytes of output, the rest is not printed (the service is not blocked). Default 256 KiB. */
|
|
67
|
+
readonly maxLogBytes?: number;
|
|
68
|
+
/** Default 250. */
|
|
69
|
+
readonly pollIntervalMs?: number;
|
|
70
|
+
/** Run the service as this user (see {@link serviceUserFor}). POSIX only; no fallback when it cannot be had. */
|
|
71
|
+
readonly separateUser?: SeparateUser;
|
|
72
|
+
}
|
|
73
|
+
export interface StartedService {
|
|
74
|
+
readonly pid: number;
|
|
75
|
+
/** Stops the service and everything it started. Safe to call twice. */
|
|
76
|
+
readonly stop: () => Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
export declare function startService(options: ServiceOptions): Promise<StartedService>;
|