@wasit-dev/core 0.1.0
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/dist/check.d.ts +70 -0
- package/dist/check.js +96 -0
- package/dist/errors.d.ts +65 -0
- package/dist/errors.js +127 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +7 -0
- package/dist/mpp/channel-commitment.d.ts +70 -0
- package/dist/mpp/channel-commitment.js +195 -0
- package/dist/mpp/channel-credential.d.ts +74 -0
- package/dist/mpp/channel-credential.js +115 -0
- package/dist/mpp/channel.d.ts +75 -0
- package/dist/mpp/channel.js +335 -0
- package/dist/mpp/charge-suite.d.ts +12 -0
- package/dist/mpp/charge-suite.js +18 -0
- package/dist/mpp/charge.d.ts +35 -0
- package/dist/mpp/charge.js +237 -0
- package/dist/mpp/network.d.ts +24 -0
- package/dist/mpp/network.js +55 -0
- package/dist/mpp/suite.d.ts +38 -0
- package/dist/mpp/suite.js +130 -0
- package/dist/x402/simulator.d.ts +40 -0
- package/dist/x402/simulator.js +367 -0
- package/package.json +54 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MPP-01: Charge-mode settlement, verified on-chain.
|
|
3
|
+
*
|
|
4
|
+
* The target's own 402 challenge is the source of truth for what should be
|
|
5
|
+
* paid. Verifying against it rather than against this run's configuration is
|
|
6
|
+
* what makes the check meaningful against a third-party service: it asks
|
|
7
|
+
* whether the service settled what it advertised, not what we assumed.
|
|
8
|
+
*
|
|
9
|
+
* Settlement is verified from the CAP-46 `transfer` contract event rather than
|
|
10
|
+
* the transaction envelope, because those can disagree. The envelope records
|
|
11
|
+
* what was requested; the event records what the token contract actually did.
|
|
12
|
+
* A `currency` contract whose `transfer` moves a different amount than its
|
|
13
|
+
* arguments claim is caught here and nowhere else — the same threat model
|
|
14
|
+
* @stellar/mpp's own client guards against when it refuses to sign a
|
|
15
|
+
* non-transfer authorization.
|
|
16
|
+
*/
|
|
17
|
+
import { Address, rpc, scValToNative, xdr } from "@stellar/stellar-sdk";
|
|
18
|
+
import { Mppx, charge } from "@stellar/mpp/charge/client";
|
|
19
|
+
import { Challenge, Receipt } from "mppx";
|
|
20
|
+
import { Keypair } from "@stellar/stellar-sdk";
|
|
21
|
+
import { ConfigurationError, MalformedResponseError, assertHttpUrl, fetchTarget, } from "../errors.js";
|
|
22
|
+
import { assertMppNetwork, resolveRpcUrl } from "./network.js";
|
|
23
|
+
const CHECK_ID = "MPP-01";
|
|
24
|
+
const CHECK_NAME = "Charge Settlement On-Chain";
|
|
25
|
+
/** How long to wait for the settled transaction to become visible to RPC. */
|
|
26
|
+
const POLL_ATTEMPTS = 12;
|
|
27
|
+
const POLL_DELAY_MS = 1_000;
|
|
28
|
+
function asRecord(value) {
|
|
29
|
+
return typeof value === "object" && value !== null
|
|
30
|
+
? value
|
|
31
|
+
: undefined;
|
|
32
|
+
}
|
|
33
|
+
function readString(source, key) {
|
|
34
|
+
const value = source[key];
|
|
35
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Reads the charge challenge without paying.
|
|
39
|
+
*
|
|
40
|
+
* Done as a separate unpaid request so the advertised terms are captured
|
|
41
|
+
* before any money moves. `amount` is in base units: the server converts via
|
|
42
|
+
* `toBaseUnits()` before building the challenge, so it is directly comparable
|
|
43
|
+
* to the `i128` in the transfer event without any decimal handling.
|
|
44
|
+
*/
|
|
45
|
+
async function fetchChargeChallenge(target) {
|
|
46
|
+
const response = await fetchTarget(target);
|
|
47
|
+
if (response.status !== 402) {
|
|
48
|
+
throw new MalformedResponseError(`Expected HTTP 402 with a payment challenge, got ${response.status}.`);
|
|
49
|
+
}
|
|
50
|
+
let challenge;
|
|
51
|
+
try {
|
|
52
|
+
challenge = Challenge.fromResponse(response);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
throw new MalformedResponseError(`Challenge could not be parsed from the WWW-Authenticate header: ` +
|
|
56
|
+
`${error.message}`);
|
|
57
|
+
}
|
|
58
|
+
const request = asRecord(challenge.request);
|
|
59
|
+
if (!request) {
|
|
60
|
+
throw new MalformedResponseError("Challenge carries no request object.");
|
|
61
|
+
}
|
|
62
|
+
const amountRaw = readString(request, "amount");
|
|
63
|
+
const currency = readString(request, "currency");
|
|
64
|
+
const recipient = readString(request, "recipient");
|
|
65
|
+
const missing = [
|
|
66
|
+
amountRaw ? null : "amount",
|
|
67
|
+
currency ? null : "currency",
|
|
68
|
+
recipient ? null : "recipient",
|
|
69
|
+
].filter((entry) => entry !== null);
|
|
70
|
+
if (missing.length > 0 || !amountRaw || !currency || !recipient) {
|
|
71
|
+
throw new MalformedResponseError(`Charge challenge is missing ${missing.join(", ")}.`);
|
|
72
|
+
}
|
|
73
|
+
let amount;
|
|
74
|
+
try {
|
|
75
|
+
amount = BigInt(amountRaw);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
throw new MalformedResponseError(`Challenge advertises a non-numeric amount ("${amountRaw}"). The spec ` +
|
|
79
|
+
`requires base units, e.g. "10000" for 0.001 of a 7-decimal token.`);
|
|
80
|
+
}
|
|
81
|
+
return { amount, currency, recipient };
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Extracts CAP-46 `transfer` events from a settled transaction.
|
|
85
|
+
*
|
|
86
|
+
* Adapted from @stellar/mpp's own `validateSimulationEvents`, with one
|
|
87
|
+
* necessary difference: that function reads `xdr.DiagnosticEvent[]` from a
|
|
88
|
+
* simulation and unwraps each via `.event()`. RPC's `getTransaction` returns
|
|
89
|
+
* `contractEventsXdr` as `xdr.ContractEvent[][]` — already unwrapped, and
|
|
90
|
+
* nested per operation. Calling `.event()` on these would throw at runtime.
|
|
91
|
+
*/
|
|
92
|
+
function collectTransferEvents(contractEventsXdr) {
|
|
93
|
+
const transfers = [];
|
|
94
|
+
for (const perOperation of contractEventsXdr) {
|
|
95
|
+
for (const contractEvent of perOperation) {
|
|
96
|
+
if (contractEvent.type().name !== "contract")
|
|
97
|
+
continue;
|
|
98
|
+
const body = contractEvent.body().v0();
|
|
99
|
+
const topics = body.topics();
|
|
100
|
+
if (topics.length < 3)
|
|
101
|
+
continue;
|
|
102
|
+
// CAP-46: topic[0] = "transfer", topic[1] = from, topic[2] = to.
|
|
103
|
+
if (topics[0]?.sym?.()?.toString() !== "transfer")
|
|
104
|
+
continue;
|
|
105
|
+
const amount = scValToNative(body.data());
|
|
106
|
+
if (typeof amount !== "bigint")
|
|
107
|
+
continue;
|
|
108
|
+
const contractId = contractEvent.contractId();
|
|
109
|
+
if (!contractId)
|
|
110
|
+
continue;
|
|
111
|
+
transfers.push({
|
|
112
|
+
from: Address.fromScVal(topics[1]).toString(),
|
|
113
|
+
to: Address.fromScVal(topics[2]).toString(),
|
|
114
|
+
amount,
|
|
115
|
+
contract: Address.fromScAddress(xdr.ScAddress.scAddressTypeContract(contractId)).toString(),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return transfers;
|
|
120
|
+
}
|
|
121
|
+
const sleep = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
122
|
+
function fail(detail) {
|
|
123
|
+
return [{ id: CHECK_ID, name: CHECK_NAME, pass: false, detail }];
|
|
124
|
+
}
|
|
125
|
+
function pass(detail) {
|
|
126
|
+
return [{ id: CHECK_ID, name: CHECK_NAME, pass: true, detail }];
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* MPP-01: the charge must settle on-chain for exactly the advertised amount.
|
|
130
|
+
*
|
|
131
|
+
* Runs a real payment against the target every time it is called. See
|
|
132
|
+
* docs/CHECKS.md — this check spends funds by design.
|
|
133
|
+
*
|
|
134
|
+
* @throws {ConfigurationError} Invalid target URL, network, or missing key.
|
|
135
|
+
* @throws {TargetUnreachableError} The target never answered.
|
|
136
|
+
* @throws {MalformedResponseError} The target answered, non-conformantly.
|
|
137
|
+
* @throws {Error} RPC failure — reported as `harness`, never as a target defect.
|
|
138
|
+
*/
|
|
139
|
+
export async function runMppChargeChecks(options) {
|
|
140
|
+
const { target, network, payerSecretKey, rpcUrl } = options;
|
|
141
|
+
assertHttpUrl(target);
|
|
142
|
+
assertMppNetwork(network);
|
|
143
|
+
if (!payerSecretKey) {
|
|
144
|
+
throw new ConfigurationError("No payer secret key. MPP-01 settles a real payment and cannot run without one.");
|
|
145
|
+
}
|
|
146
|
+
let payer;
|
|
147
|
+
try {
|
|
148
|
+
payer = Keypair.fromSecret(payerSecretKey);
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
throw new ConfigurationError("Payer secret key is not a valid Stellar secret (expected an S... string).");
|
|
152
|
+
}
|
|
153
|
+
// Resolve the endpoint before spending anything: a bad RPC URL discovered
|
|
154
|
+
// after settlement would leave money moved and no verdict to show for it.
|
|
155
|
+
const endpoint = resolveRpcUrl(network, rpcUrl);
|
|
156
|
+
const advertised = await fetchChargeChallenge(target);
|
|
157
|
+
const mppx = Mppx.create({ methods: [charge({ secretKey: payerSecretKey })] });
|
|
158
|
+
let paid;
|
|
159
|
+
try {
|
|
160
|
+
paid = await mppx.fetch(target);
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
// The target was reachable moments ago, so this is the payment path
|
|
164
|
+
// failing, not the network. Report it as a conformance failure.
|
|
165
|
+
throw new MalformedResponseError(`Payment attempt failed: ${error.message}`);
|
|
166
|
+
}
|
|
167
|
+
if (!paid.ok) {
|
|
168
|
+
return fail(`Paid the advertised ${advertised.amount} base units, but the target ` +
|
|
169
|
+
`answered HTTP ${paid.status} instead of serving the resource.`);
|
|
170
|
+
}
|
|
171
|
+
let reference;
|
|
172
|
+
try {
|
|
173
|
+
reference = Receipt.fromResponse(paid).reference;
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
return fail(`Target returned ${paid.status} but no valid Payment-Receipt header: ` +
|
|
177
|
+
`${error.message}`);
|
|
178
|
+
}
|
|
179
|
+
const server = new rpc.Server(endpoint, {
|
|
180
|
+
allowHttp: endpoint.startsWith("http://"),
|
|
181
|
+
});
|
|
182
|
+
let settled;
|
|
183
|
+
for (let attempt = 0; attempt < POLL_ATTEMPTS; attempt += 1) {
|
|
184
|
+
// An RPC failure is a harness problem: it says nothing about the target.
|
|
185
|
+
const current = await server.getTransaction(reference);
|
|
186
|
+
if (current.status !== rpc.Api.GetTransactionStatus.NOT_FOUND) {
|
|
187
|
+
settled = current;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
await sleep(POLL_DELAY_MS);
|
|
191
|
+
}
|
|
192
|
+
if (!settled || settled.status === rpc.Api.GetTransactionStatus.NOT_FOUND) {
|
|
193
|
+
return fail(`Target reported settlement as tx ${reference}, but RPC still has no ` +
|
|
194
|
+
`such transaction after ${POLL_ATTEMPTS} seconds. Either it was never ` +
|
|
195
|
+
`broadcast, or the receipt references a transaction that does not exist.`);
|
|
196
|
+
}
|
|
197
|
+
if (settled.status !== rpc.Api.GetTransactionStatus.SUCCESS) {
|
|
198
|
+
return fail(`Transaction ${reference} exists on-chain but failed (status ` +
|
|
199
|
+
`${settled.status}). The target reported success for a settlement that ` +
|
|
200
|
+
`did not settle.`);
|
|
201
|
+
}
|
|
202
|
+
const transfers = collectTransferEvents(settled.events.contractEventsXdr);
|
|
203
|
+
if (transfers.length === 0) {
|
|
204
|
+
return fail(`Transaction ${reference} succeeded but emitted no CAP-46 transfer ` +
|
|
205
|
+
`event, so no token movement can be verified.`);
|
|
206
|
+
}
|
|
207
|
+
// Per the MPP spec, events must show only the expected balance change.
|
|
208
|
+
if (transfers.length !== 1) {
|
|
209
|
+
return fail(`Transaction ${reference} emitted ${transfers.length} transfer events; ` +
|
|
210
|
+
`exactly one was expected. Additional balance changes mean the ` +
|
|
211
|
+
`settlement moved more than what was advertised.`);
|
|
212
|
+
}
|
|
213
|
+
const actual = transfers[0];
|
|
214
|
+
const mismatches = [];
|
|
215
|
+
if (actual.amount !== advertised.amount) {
|
|
216
|
+
mismatches.push(`amount: advertised ${advertised.amount} base units, moved ${actual.amount}`);
|
|
217
|
+
}
|
|
218
|
+
if (actual.to !== advertised.recipient) {
|
|
219
|
+
mismatches.push(`recipient: advertised ${advertised.recipient}, paid ${actual.to}`);
|
|
220
|
+
}
|
|
221
|
+
if (actual.contract !== advertised.currency) {
|
|
222
|
+
mismatches.push(`token: advertised ${advertised.currency}, moved ${actual.contract}`);
|
|
223
|
+
}
|
|
224
|
+
// Without this, a target could reference any pre-existing transaction that
|
|
225
|
+
// happens to match the amount and recipient, and never take payment at all.
|
|
226
|
+
if (actual.from !== payer.publicKey()) {
|
|
227
|
+
mismatches.push(`payer: this run paid from ${payer.publicKey()}, but the referenced ` +
|
|
228
|
+
`transfer came from ${actual.from}`);
|
|
229
|
+
}
|
|
230
|
+
if (mismatches.length > 0) {
|
|
231
|
+
return fail(`Settlement does not match what the target advertised — ` +
|
|
232
|
+
`${mismatches.join("; ")} (tx ${reference}).`);
|
|
233
|
+
}
|
|
234
|
+
return pass(`Settled on-chain for exactly the advertised ${advertised.amount} base ` +
|
|
235
|
+
`units of ${advertised.currency} to ${advertised.recipient}, verified ` +
|
|
236
|
+
`from the transfer event (tx ${reference}).`);
|
|
237
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Network resolution for MPP checks.
|
|
3
|
+
*
|
|
4
|
+
* Passphrases come from the SDK's own `Networks` constants rather than being
|
|
5
|
+
* restated here, so they cannot drift. RPC endpoints are overridable because
|
|
6
|
+
* the caller may target a private or self-hosted node.
|
|
7
|
+
*/
|
|
8
|
+
import { ConfigurationError } from "../errors.js";
|
|
9
|
+
/** CAIP-2 network identifiers supported by the MPP checks. */
|
|
10
|
+
export type MppNetwork = "stellar:testnet" | "stellar:pubnet";
|
|
11
|
+
export declare class UnsupportedNetworkError extends ConfigurationError {
|
|
12
|
+
constructor(network: string);
|
|
13
|
+
}
|
|
14
|
+
export declare function isMppNetwork(value: string): value is MppNetwork;
|
|
15
|
+
export declare function assertMppNetwork(value: string): MppNetwork;
|
|
16
|
+
export declare function networkPassphrase(network: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Resolves the RPC endpoint for a network.
|
|
19
|
+
*
|
|
20
|
+
* @param override - Explicit endpoint; always wins when provided.
|
|
21
|
+
* @throws {UnsupportedNetworkError} If the network is unknown.
|
|
22
|
+
* @throws {Error} If no default exists for the network and no override is given.
|
|
23
|
+
*/
|
|
24
|
+
export declare function resolveRpcUrl(network: string, override?: string): string;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Network resolution for MPP checks.
|
|
3
|
+
*
|
|
4
|
+
* Passphrases come from the SDK's own `Networks` constants rather than being
|
|
5
|
+
* restated here, so they cannot drift. RPC endpoints are overridable because
|
|
6
|
+
* the caller may target a private or self-hosted node.
|
|
7
|
+
*/
|
|
8
|
+
import { Networks } from "@stellar/stellar-sdk";
|
|
9
|
+
import { ConfigurationError } from "../errors.js";
|
|
10
|
+
export class UnsupportedNetworkError extends ConfigurationError {
|
|
11
|
+
constructor(network) {
|
|
12
|
+
super(`Unsupported network "${network}". Expected "stellar:testnet" or "stellar:pubnet".`);
|
|
13
|
+
this.name = "UnsupportedNetworkError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const PASSPHRASES = {
|
|
17
|
+
"stellar:testnet": Networks.TESTNET,
|
|
18
|
+
"stellar:pubnet": Networks.PUBLIC,
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Default public RPC endpoints. Only testnet has a well-known free endpoint;
|
|
22
|
+
* pubnet deliberately has none, so mainnet runs must pass `rpcUrl` explicitly
|
|
23
|
+
* rather than silently pointing at a third-party node.
|
|
24
|
+
*/
|
|
25
|
+
const DEFAULT_RPC_URLS = {
|
|
26
|
+
"stellar:testnet": "https://soroban-testnet.stellar.org",
|
|
27
|
+
};
|
|
28
|
+
export function isMppNetwork(value) {
|
|
29
|
+
return value === "stellar:testnet" || value === "stellar:pubnet";
|
|
30
|
+
}
|
|
31
|
+
export function assertMppNetwork(value) {
|
|
32
|
+
if (!isMppNetwork(value))
|
|
33
|
+
throw new UnsupportedNetworkError(value);
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
export function networkPassphrase(network) {
|
|
37
|
+
return PASSPHRASES[assertMppNetwork(network)];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Resolves the RPC endpoint for a network.
|
|
41
|
+
*
|
|
42
|
+
* @param override - Explicit endpoint; always wins when provided.
|
|
43
|
+
* @throws {UnsupportedNetworkError} If the network is unknown.
|
|
44
|
+
* @throws {Error} If no default exists for the network and no override is given.
|
|
45
|
+
*/
|
|
46
|
+
export function resolveRpcUrl(network, override) {
|
|
47
|
+
const resolved = assertMppNetwork(network);
|
|
48
|
+
if (override)
|
|
49
|
+
return override;
|
|
50
|
+
const fallback = DEFAULT_RPC_URLS[resolved];
|
|
51
|
+
if (!fallback) {
|
|
52
|
+
throw new ConfigurationError(`No default RPC endpoint for ${resolved}. Pass an explicit rpcUrl.`);
|
|
53
|
+
}
|
|
54
|
+
return fallback;
|
|
55
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orchestration for the MPP channel-mode suite.
|
|
3
|
+
*
|
|
4
|
+
* This lives in core rather than in a front end so that the CLI and the MCP
|
|
5
|
+
* server run the same checks, in the same order, under the same guards. A
|
|
6
|
+
* second front end reimplementing this is how the two would silently drift.
|
|
7
|
+
*/
|
|
8
|
+
import { type CheckResult } from "../check.js";
|
|
9
|
+
/** On-chain parameters MPP-10 asserts. Only the channel's operator knows these. */
|
|
10
|
+
export interface ChannelDeployExpectations {
|
|
11
|
+
readonly token: string;
|
|
12
|
+
readonly from: string;
|
|
13
|
+
readonly to: string;
|
|
14
|
+
readonly refundWaitingPeriod: number;
|
|
15
|
+
}
|
|
16
|
+
export interface MppChannelSuiteOptions {
|
|
17
|
+
readonly target: string;
|
|
18
|
+
readonly commitmentSecretHex: string;
|
|
19
|
+
readonly network: string;
|
|
20
|
+
readonly rpcUrl?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Asserts which channel MPP-10 should inspect. The target is authoritative:
|
|
23
|
+
* when it advertises a different channel the run fails rather than reporting
|
|
24
|
+
* on two contracts at once.
|
|
25
|
+
*/
|
|
26
|
+
readonly channelOverride?: string;
|
|
27
|
+
readonly expected?: Partial<ChannelDeployExpectations>;
|
|
28
|
+
readonly allowDestructive?: boolean;
|
|
29
|
+
readonly destructiveChannel?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Runs MPP-10 through MPP-14 against a target.
|
|
33
|
+
*
|
|
34
|
+
* The channel under test is resolved once, from the target's own challenge,
|
|
35
|
+
* so every check in the run reports on the same contract. MPP-13 runs last
|
|
36
|
+
* because a close is terminal.
|
|
37
|
+
*/
|
|
38
|
+
export declare function runMppChannelSuite(options: MppChannelSuiteOptions): Promise<CheckResult[]>;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orchestration for the MPP channel-mode suite.
|
|
3
|
+
*
|
|
4
|
+
* This lives in core rather than in a front end so that the CLI and the MCP
|
|
5
|
+
* server run the same checks, in the same order, under the same guards. A
|
|
6
|
+
* second front end reimplementing this is how the two would silently drift.
|
|
7
|
+
*/
|
|
8
|
+
import { errored, skipped } from "../check.js";
|
|
9
|
+
import { assertHttpUrl, classifyCheckError } from "../errors.js";
|
|
10
|
+
import { runMppChannelCloseCheck, runMppChannelCommitmentReplayCheck, runMppChannelDeployChecks, runMppChannelOrderingCheck, runMppChannelReplayCheck, } from "./channel.js";
|
|
11
|
+
import { fetchChannelChallenge } from "./channel-credential.js";
|
|
12
|
+
import { assertMppNetwork } from "./network.js";
|
|
13
|
+
const DEPLOY_ID = "MPP-10";
|
|
14
|
+
const DEPLOY_NAME = "Channel Deploy";
|
|
15
|
+
function missingExpectations(expected) {
|
|
16
|
+
return [
|
|
17
|
+
typeof expected?.token === "string" && expected.token.length > 0 ? null : "token",
|
|
18
|
+
typeof expected?.from === "string" && expected.from.length > 0 ? null : "from",
|
|
19
|
+
typeof expected?.to === "string" && expected.to.length > 0 ? null : "to",
|
|
20
|
+
typeof expected?.refundWaitingPeriod === "number" &&
|
|
21
|
+
Number.isInteger(expected.refundWaitingPeriod)
|
|
22
|
+
? null
|
|
23
|
+
: "refundWaitingPeriod",
|
|
24
|
+
].filter((entry) => entry !== null);
|
|
25
|
+
}
|
|
26
|
+
function isComplete(expected) {
|
|
27
|
+
return missingExpectations(expected).length === 0;
|
|
28
|
+
}
|
|
29
|
+
async function runDeployCheck(params) {
|
|
30
|
+
const { network, rpcUrl, advertised, advertisedFailure, channelOverride, expected } = params;
|
|
31
|
+
if (advertised && channelOverride && advertised !== channelOverride) {
|
|
32
|
+
return [
|
|
33
|
+
{
|
|
34
|
+
id: DEPLOY_ID,
|
|
35
|
+
name: DEPLOY_NAME,
|
|
36
|
+
pass: false,
|
|
37
|
+
detail: `Channel mismatch: the target bills through ${advertised}, but this run ` +
|
|
38
|
+
`named ${channelOverride}. Inspecting the named channel would report on a ` +
|
|
39
|
+
`different contract than the rest of the suite, so nothing was inspected. ` +
|
|
40
|
+
`Drop the override to use the advertised channel, or point the target at ` +
|
|
41
|
+
`the intended one.`,
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
const channel = advertised ?? channelOverride;
|
|
46
|
+
if (!channel) {
|
|
47
|
+
return [
|
|
48
|
+
skipped(DEPLOY_ID, DEPLOY_NAME, `the channel the target advertises could not be read ` +
|
|
49
|
+
`(${advertisedFailure ?? "unknown error"}), and no channel was named to ` +
|
|
50
|
+
`inspect instead.`),
|
|
51
|
+
];
|
|
52
|
+
}
|
|
53
|
+
if (!isComplete(expected)) {
|
|
54
|
+
return [
|
|
55
|
+
skipped(DEPLOY_ID, DEPLOY_NAME, `expected on-chain parameters not supplied ` +
|
|
56
|
+
`(${missingExpectations(expected).join(", ")}).`),
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
const results = await runMppChannelDeployChecks({
|
|
60
|
+
channelContract: channel,
|
|
61
|
+
network,
|
|
62
|
+
...(rpcUrl ? { rpcUrl } : {}),
|
|
63
|
+
expected,
|
|
64
|
+
});
|
|
65
|
+
if (advertised)
|
|
66
|
+
return results;
|
|
67
|
+
// Inspected an operator-supplied address without being able to confirm it is
|
|
68
|
+
// the one the target bills through. Say so, rather than letting the result
|
|
69
|
+
// read as though the two were checked against each other.
|
|
70
|
+
return results.map((result) => ({
|
|
71
|
+
...result,
|
|
72
|
+
detail: `${result.detail} Note: the target's advertised channel could not be read ` +
|
|
73
|
+
`(${advertisedFailure ?? "unknown error"}), so ${channel} came from this run's ` +
|
|
74
|
+
`own configuration and is unverified against the target.`,
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Runs MPP-10 through MPP-14 against a target.
|
|
79
|
+
*
|
|
80
|
+
* The channel under test is resolved once, from the target's own challenge,
|
|
81
|
+
* so every check in the run reports on the same contract. MPP-13 runs last
|
|
82
|
+
* because a close is terminal.
|
|
83
|
+
*/
|
|
84
|
+
export async function runMppChannelSuite(options) {
|
|
85
|
+
const { target, commitmentSecretHex, network, rpcUrl, channelOverride, expected, allowDestructive = false, destructiveChannel, } = options;
|
|
86
|
+
// A bad URL or an unknown network is wrong for every check in the suite, so
|
|
87
|
+
// it is reported once here instead of repeated identically per check.
|
|
88
|
+
try {
|
|
89
|
+
assertHttpUrl(target);
|
|
90
|
+
assertMppNetwork(network);
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
return [errored("PREFLIGHT", "Run Preflight", error)];
|
|
94
|
+
}
|
|
95
|
+
const shared = {
|
|
96
|
+
target,
|
|
97
|
+
commitmentSecretHex,
|
|
98
|
+
network,
|
|
99
|
+
...(rpcUrl ? { rpcUrl } : {}),
|
|
100
|
+
};
|
|
101
|
+
// The target decides which channel it bills through, so its challenge — not
|
|
102
|
+
// the operator's environment — is the authority on what this run is about.
|
|
103
|
+
let advertised;
|
|
104
|
+
let advertisedFailure;
|
|
105
|
+
try {
|
|
106
|
+
advertised = (await fetchChannelChallenge(target)).channelContract;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
advertisedFailure = classifyCheckError(error).message;
|
|
110
|
+
}
|
|
111
|
+
const results = [];
|
|
112
|
+
results.push(...(await runDeployCheck({
|
|
113
|
+
network,
|
|
114
|
+
...(rpcUrl ? { rpcUrl } : {}),
|
|
115
|
+
...(advertised ? { advertised } : {}),
|
|
116
|
+
...(advertisedFailure ? { advertisedFailure } : {}),
|
|
117
|
+
...(channelOverride ? { channelOverride } : {}),
|
|
118
|
+
...(expected ? { expected } : {}),
|
|
119
|
+
})));
|
|
120
|
+
results.push(...(await runMppChannelOrderingCheck(shared)));
|
|
121
|
+
results.push(...(await runMppChannelReplayCheck(shared)));
|
|
122
|
+
results.push(...(await runMppChannelCommitmentReplayCheck(shared)));
|
|
123
|
+
// Last: a close is terminal, so nothing may run against the channel after it.
|
|
124
|
+
results.push(...(await runMppChannelCloseCheck({
|
|
125
|
+
...shared,
|
|
126
|
+
allowDestructive,
|
|
127
|
+
...(destructiveChannel ? { expectedChannel: destructiveChannel } : {}),
|
|
128
|
+
})));
|
|
129
|
+
return results;
|
|
130
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type CheckResult } from "../check.js";
|
|
2
|
+
export type { CheckResult };
|
|
3
|
+
/**
|
|
4
|
+
* How to address the target's paid endpoint.
|
|
5
|
+
*
|
|
6
|
+
* A paid x402 endpoint is not necessarily a GET. An endpoint that computes
|
|
7
|
+
* something for the caller naturally takes a POST with a body, and real
|
|
8
|
+
* services expose only that. Issuing the wrong verb draws a 404 and produces a
|
|
9
|
+
* report claiming the service never answers 402 — a false finding about a
|
|
10
|
+
* conformant service, which is the worst thing a conformance tester can emit.
|
|
11
|
+
*/
|
|
12
|
+
export interface RequestShape {
|
|
13
|
+
/** HTTP method. Defaults to GET. */
|
|
14
|
+
readonly method?: string;
|
|
15
|
+
/** Raw request body, sent verbatim. Not permitted with GET or HEAD. */
|
|
16
|
+
readonly body?: string;
|
|
17
|
+
/** Headers the endpoint needs before it will issue a challenge at all. */
|
|
18
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
19
|
+
}
|
|
20
|
+
export interface X402SimulatorOptions extends RequestShape {
|
|
21
|
+
target: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Runs X402-01 through X402-05 against a target.
|
|
25
|
+
*
|
|
26
|
+
* Stops at the first failure whose consequence is that nothing downstream can
|
|
27
|
+
* be evaluated, so one broken challenge produces one finding.
|
|
28
|
+
*/
|
|
29
|
+
export declare function runX402ReadChecks(options: X402SimulatorOptions): Promise<CheckResult[]>;
|
|
30
|
+
export interface X402PaymentCheckOptions extends RequestShape {
|
|
31
|
+
readonly target: string;
|
|
32
|
+
readonly network: string;
|
|
33
|
+
readonly payerSecretKey: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Runs X402-06 and X402-07 against a target.
|
|
37
|
+
*
|
|
38
|
+
* Both settle real payments when the target accepts them; see docs/CHECKS.md.
|
|
39
|
+
*/
|
|
40
|
+
export declare function runX402PaymentChecks(options: X402PaymentCheckOptions): Promise<CheckResult[]>;
|