@wasit-dev/core 0.8.0-beta.1 → 0.8.0-beta.2
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 +13 -2
- package/dist/baseline.js +3 -1
- package/dist/check.d.ts +1 -1
- package/dist/ci-report.js +15 -7
- package/dist/config-writer.d.ts +96 -0
- package/dist/config-writer.js +281 -0
- package/dist/guard/budget.d.ts +6 -3
- package/dist/guard/budget.js +10 -3
- package/dist/guard/ci-event.d.ts +6 -0
- package/dist/guard/ci-event.js +7 -3
- package/dist/guard/ci-mainnet.d.ts +59 -0
- package/dist/guard/ci-mainnet.js +149 -0
- package/dist/guard/horizon.d.ts +30 -0
- package/dist/guard/horizon.js +107 -0
- package/dist/guard/mainnet-payer.d.ts +66 -0
- package/dist/guard/mainnet-payer.js +92 -0
- package/dist/guard/mainnet-plan.d.ts +133 -0
- package/dist/guard/mainnet-plan.js +208 -0
- package/dist/guard/mainnet-run.d.ts +87 -0
- package/dist/guard/mainnet-run.js +225 -0
- package/dist/guard/networks.d.ts +9 -8
- package/dist/guard/networks.js +21 -10
- package/dist/guard/optin.d.ts +106 -0
- package/dist/guard/optin.js +202 -0
- package/dist/guard/run-ledger.d.ts +212 -0
- package/dist/guard/run-ledger.js +426 -0
- package/dist/guard/run-lock.d.ts +62 -0
- package/dist/guard/run-lock.js +172 -0
- package/dist/guard/signed-terms.d.ts +96 -0
- package/dist/guard/signed-terms.js +207 -0
- package/dist/index.d.ts +18 -7
- package/dist/index.js +19 -5
- package/dist/mpp/channel-credential.d.ts +13 -1
- package/dist/mpp/channel-credential.js +18 -2
- package/dist/mpp/channel.d.ts +6 -0
- package/dist/mpp/channel.js +7 -3
- package/dist/mpp/charge.d.ts +31 -1
- package/dist/mpp/charge.js +74 -20
- package/dist/mpp/plan.d.ts +77 -0
- package/dist/mpp/plan.js +188 -0
- package/dist/mpp/suite.d.ts +6 -0
- package/dist/mpp/suite.js +2 -1
- package/dist/route-reader.d.ts +124 -0
- package/dist/route-reader.js +349 -0
- package/dist/wallet.d.ts +58 -1
- package/dist/wallet.js +128 -0
- package/dist/x402/chains/stellar.js +134 -3
- package/dist/x402/chains/types.d.ts +36 -2
- package/dist/x402/plan.d.ts +102 -0
- package/dist/x402/plan.js +380 -6
- package/dist/x402/rejected-request.d.ts +10 -0
- package/dist/x402/rejected-request.js +18 -0
- package/dist/x402/simulator.d.ts +34 -0
- package/dist/x402/simulator.js +452 -46
- package/package.json +1 -1
- package/dist/guard/mainnet-optin.d.ts +0 -58
- package/dist/guard/mainnet-optin.js +0 -95
package/dist/mpp/charge.js
CHANGED
|
@@ -20,13 +20,16 @@
|
|
|
20
20
|
import { Mppx, charge } from "@stellar/mpp/charge/client";
|
|
21
21
|
import { Challenge, Receipt } from "mppx";
|
|
22
22
|
import { Keypair } from "@stellar/stellar-sdk";
|
|
23
|
-
import { ConfigurationError, MalformedResponseError, assertHttpUrl, fetchTarget, fetchWithoutRedirects, } from "../errors.js";
|
|
23
|
+
import { CheckSetupError, ConfigurationError, MalformedResponseError, assertHttpUrl, fetchTarget, fetchWithoutRedirects, } from "../errors.js";
|
|
24
24
|
import { assertMppNetwork, isMppNetwork, resolveRpcUrl } from "./network.js";
|
|
25
|
+
import { PaymentGuardRefusedError, shown } from "../guard/budget.js";
|
|
25
26
|
import { assertPaymentNetworkAllowed } from "../guard/networks.js";
|
|
26
27
|
import { assertRpcServesNetwork } from "../guard/rpc-identity.js";
|
|
27
28
|
import { verifySettlement } from "../settlement.js";
|
|
28
29
|
const CHECK_ID = "MPP-01";
|
|
29
30
|
const CHECK_NAME = "Charge Settlement On-Chain";
|
|
31
|
+
/** An amount as a charge challenge must give it: base units, digits only, at most 78 (a uint256). */
|
|
32
|
+
const WHOLE_BASE_UNITS = /^\d{1,78}$/;
|
|
30
33
|
function asRecord(value) {
|
|
31
34
|
return typeof value === "object" && value !== null
|
|
32
35
|
? value
|
|
@@ -44,7 +47,7 @@ function readString(source, key) {
|
|
|
44
47
|
* `toBaseUnits()` before building the challenge, so it is directly comparable
|
|
45
48
|
* to the `i128` in the transfer event without any decimal handling.
|
|
46
49
|
*/
|
|
47
|
-
async function
|
|
50
|
+
export async function readChargeChallenge(target) {
|
|
48
51
|
const response = await fetchTarget(target);
|
|
49
52
|
if (response.status !== 402) {
|
|
50
53
|
throw new MalformedResponseError(`Expected HTTP 402 with a payment challenge, got ${response.status}.`);
|
|
@@ -84,14 +87,14 @@ export function parseChargeChallenge(challenge) {
|
|
|
84
87
|
if (missing.length > 0 || !amountRaw || !currency || !recipient) {
|
|
85
88
|
throw new MalformedResponseError(`Charge challenge is missing ${missing.join(", ")}.`);
|
|
86
89
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
throw new MalformedResponseError(`Challenge advertises a non-numeric amount ("${amountRaw}"). The spec ` +
|
|
90
|
+
// Digits only, the rule the run's guard holds a signed amount to (`parseAmount`
|
|
91
|
+
// in guard/budget.ts). `BigInt` alone also takes "-5", "0x10" and " 10 ": a
|
|
92
|
+
// negative price would lower the worst case a run's plan adds up.
|
|
93
|
+
if (!WHOLE_BASE_UNITS.test(amountRaw)) {
|
|
94
|
+
throw new MalformedResponseError(`Challenge advertises an amount that is not a whole number of base units (${shown(amountRaw)}). The spec ` +
|
|
93
95
|
`requires base units, e.g. "10000" for 0.001 of a 7-decimal token.`);
|
|
94
96
|
}
|
|
97
|
+
const amount = BigInt(amountRaw);
|
|
95
98
|
return { amount, currency, recipient, network: chargeNetwork(request) };
|
|
96
99
|
}
|
|
97
100
|
/**
|
|
@@ -119,7 +122,34 @@ function assertRunNetwork(asked, network) {
|
|
|
119
122
|
}
|
|
120
123
|
}
|
|
121
124
|
/**
|
|
122
|
-
*
|
|
125
|
+
* Refuses a charge that is not the one the plan read: another amount, currency
|
|
126
|
+
* or recipient. The service may change its challenge between the plan and the
|
|
127
|
+
* payment; L2 then refuses, rather than signing what the plan never saw.
|
|
128
|
+
*/
|
|
129
|
+
function assertPlanned(asked, planned) {
|
|
130
|
+
const differs = [];
|
|
131
|
+
if (typeof asked.amount !== "string" || !WHOLE_BASE_UNITS.test(asked.amount) || BigInt(asked.amount) !== planned.amount) {
|
|
132
|
+
differs.push(`amount ${shown(asked.amount)} (planned ${planned.amount})`);
|
|
133
|
+
}
|
|
134
|
+
if (asked.currency !== planned.currency)
|
|
135
|
+
differs.push(`currency ${shown(asked.currency)} (planned ${planned.currency})`);
|
|
136
|
+
if (asked.recipient !== planned.recipient)
|
|
137
|
+
differs.push(`recipient ${shown(asked.recipient)} (planned ${planned.recipient})`);
|
|
138
|
+
if (differs.length > 0) {
|
|
139
|
+
throw new PaymentGuardRefusedError("plan", `MPP-01 would pay a charge the plan did not read before the run started: ${differs.join(", ")}. ` +
|
|
140
|
+
`The service changed its challenge after the plan.`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/** Why a guarded MPP-01 pays nothing: the plan of the run did not plan a payment for the endpoint. */
|
|
144
|
+
function notPlannedRefusal(guard) {
|
|
145
|
+
const says = guard.notPlanned?.replace(/\s*Nothing was signed\.?\s*$/, "").trim();
|
|
146
|
+
return new PaymentGuardRefusedError("plan", `The plan of this run did not plan a payment for this endpoint, so MPP-01 does not pay it.` +
|
|
147
|
+
(says === undefined || says === "" ? "" : ` The plan says: ${says.replace(/\.?$/, ".")}`));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* `method`, refusing to sign a challenge for any network but `network` and,
|
|
151
|
+
* with a guard, any charge but the planned one, and charging the run's budget
|
|
152
|
+
* before the SDK signs.
|
|
123
153
|
*
|
|
124
154
|
* Checking the unpaid challenge is not enough on its own: mppx requests the
|
|
125
155
|
* target again and pays the challenge in that answer, which the target is
|
|
@@ -127,11 +157,25 @@ function assertRunNetwork(asked, network) {
|
|
|
127
157
|
* be signed. Without it a target asking for `stellar:pubnet` gets a mainnet
|
|
128
158
|
* transfer signed, sent through the SDK's own default pubnet RPC endpoint.
|
|
129
159
|
*/
|
|
130
|
-
function onNetwork(method, network) {
|
|
160
|
+
function onNetwork(method, network, guard) {
|
|
131
161
|
return {
|
|
132
162
|
...method,
|
|
133
163
|
createCredential: async (parameters) => {
|
|
134
|
-
|
|
164
|
+
const request = asRecord(parameters.challenge.request) ?? {};
|
|
165
|
+
assertRunNetwork(chargeNetwork(request), network);
|
|
166
|
+
if (guard !== undefined) {
|
|
167
|
+
if (guard.planned === undefined)
|
|
168
|
+
throw notPlannedRefusal(guard);
|
|
169
|
+
assertPlanned({ amount: request["amount"], currency: request["currency"], recipient: request["recipient"] }, guard.planned);
|
|
170
|
+
// Charged before the signature and never credited back: a payment that fails after it was signed may still settle.
|
|
171
|
+
guard.budget.charge({
|
|
172
|
+
check: CHECK_ID,
|
|
173
|
+
network,
|
|
174
|
+
asset: guard.planned.currency,
|
|
175
|
+
amount: String(guard.planned.amount),
|
|
176
|
+
planned: guard.planned.amount,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
135
179
|
return method.createCredential(parameters);
|
|
136
180
|
},
|
|
137
181
|
};
|
|
@@ -145,12 +189,12 @@ function onNetwork(method, network) {
|
|
|
145
189
|
* 0.8.14). MPP-01 judges one payment: a target that answers 402 after being
|
|
146
190
|
* paid has that 402 reported, rather than being paid twice more.
|
|
147
191
|
*/
|
|
148
|
-
export async function payCharge(target, network, method) {
|
|
192
|
+
export async function payCharge(target, network, method, guard) {
|
|
149
193
|
// L0 again at the signing call itself: it is exported, so a caller can
|
|
150
194
|
// reach it without going through runMppChargeChecks.
|
|
151
195
|
assertPaymentNetworkAllowed("mpp-charge", network);
|
|
152
196
|
const mppx = Mppx.create({
|
|
153
|
-
methods: [onNetwork(method, network)],
|
|
197
|
+
methods: [onNetwork(method, network, guard)],
|
|
154
198
|
polyfill: false,
|
|
155
199
|
maxPaymentRetries: 1,
|
|
156
200
|
// Every request mppx makes here refuses a redirect (D-022c): it pays at the
|
|
@@ -162,8 +206,9 @@ export async function payCharge(target, network, method) {
|
|
|
162
206
|
function fail(detail) {
|
|
163
207
|
return [{ id: CHECK_ID, name: CHECK_NAME, pass: false, detail }];
|
|
164
208
|
}
|
|
165
|
-
function pass(detail) {
|
|
166
|
-
|
|
209
|
+
function pass(detail, reference) {
|
|
210
|
+
// Read back from the chain and found to be this payment: a report may link it.
|
|
211
|
+
return [{ id: CHECK_ID, name: CHECK_NAME, pass: true, detail, reference }];
|
|
167
212
|
}
|
|
168
213
|
/**
|
|
169
214
|
* MPP-01: the charge must settle on-chain for exactly the advertised amount.
|
|
@@ -180,7 +225,7 @@ function pass(detail) {
|
|
|
180
225
|
* @throws {Error} RPC failure — reported as `harness`, never as a target defect.
|
|
181
226
|
*/
|
|
182
227
|
export async function runMppChargeChecks(options) {
|
|
183
|
-
const { target, network, payerSecretKey, rpcUrl } = options;
|
|
228
|
+
const { target, network, payerSecretKey, rpcUrl, guard } = options;
|
|
184
229
|
assertHttpUrl(target);
|
|
185
230
|
const runNetwork = assertMppNetwork(network);
|
|
186
231
|
// L0: before the key is read, any request is sent or the SDK's charge
|
|
@@ -210,17 +255,26 @@ export async function runMppChargeChecks(options) {
|
|
|
210
255
|
// The payment is built and its settlement read through this endpoint: it
|
|
211
256
|
// must serve the run's network before the target is asked anything (D-023).
|
|
212
257
|
await assertRpcServesNetwork(runNetwork, endpoint);
|
|
213
|
-
const advertised = await
|
|
258
|
+
const advertised = await readChargeChallenge(target);
|
|
214
259
|
assertRunNetwork(advertised.network, runNetwork);
|
|
260
|
+
if (guard !== undefined) {
|
|
261
|
+
// A guarded run pays only what its plan read. An endpoint it did not plan still has its unpaid challenge read
|
|
262
|
+
// above, so a service that does not answer with one gets its verdict; one that does is not paid.
|
|
263
|
+
if (guard.planned === undefined)
|
|
264
|
+
throw notPlannedRefusal(guard);
|
|
265
|
+
// The challenge the check judges against is the planned one, or nothing is paid.
|
|
266
|
+
assertPlanned({ ...advertised, amount: String(advertised.amount) }, guard.planned);
|
|
267
|
+
}
|
|
215
268
|
let paid;
|
|
216
269
|
try {
|
|
217
270
|
// The payment goes through the run's endpoint too. Left to itself the SDK
|
|
218
271
|
// picks its own per network, so `--rpc-url` would cover only the
|
|
219
272
|
// settlement read, and pubnet would get a default endpoint after all.
|
|
220
|
-
paid = await payCharge(target, runNetwork, charge({ secretKey: payerSecretKey, rpcUrl: endpoint }));
|
|
273
|
+
paid = await payCharge(target, runNetwork, charge({ secretKey: payerSecretKey, rpcUrl: endpoint }), guard);
|
|
221
274
|
}
|
|
222
275
|
catch (error) {
|
|
223
|
-
|
|
276
|
+
// A refusal of the run's own rules (network, plan, budget) is not the target's failure: no verdict.
|
|
277
|
+
if (error instanceof ConfigurationError || error instanceof CheckSetupError)
|
|
224
278
|
throw error;
|
|
225
279
|
// The target was reachable moments ago, so this is the payment path
|
|
226
280
|
// failing, not the network. Report it as a conformance failure.
|
|
@@ -248,6 +302,6 @@ export async function runMppChargeChecks(options) {
|
|
|
248
302
|
});
|
|
249
303
|
// Same sentence MPP-01 has always printed, capitalised and closed.
|
|
250
304
|
return verdict.pass
|
|
251
|
-
? pass(`${verdict.detail.charAt(0).toUpperCase()}${verdict.detail.slice(1)}
|
|
305
|
+
? pass(`${verdict.detail.charAt(0).toUpperCase()}${verdict.detail.slice(1)}.`, reference)
|
|
252
306
|
: fail(verdict.detail);
|
|
253
307
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L1 for MPP-01 in `wasit run` (D-036): the charge an MPP charge endpoint asks
|
|
3
|
+
* for, read from its unpaid challenge before anything in the run is signed,
|
|
4
|
+
* held to the same caps as x402 ($1 a payment, one budget for the run), and
|
|
5
|
+
* planned together with the run's x402 payment checks.
|
|
6
|
+
*
|
|
7
|
+
* MPP pays on test networks only (L0): a run on any other network is refused
|
|
8
|
+
* here before any request. MPP-01 signs once per endpoint (mppx is built with
|
|
9
|
+
* `maxPaymentRetries: 1`, `charge.ts`), so its plan is one signature of the
|
|
10
|
+
* advertised amount. The channel checks sign commitments off-chain and Wasit
|
|
11
|
+
* sends no transaction for them, so they are not planned.
|
|
12
|
+
*
|
|
13
|
+
* It sends no payment, reads no key and signs nothing. `x402/plan.ts` is not
|
|
14
|
+
* changed by it: with no MPP charge endpoint the run's plan is the x402 plan
|
|
15
|
+
* as it was.
|
|
16
|
+
*/
|
|
17
|
+
import { type PinTerms } from "../guard/budget.js";
|
|
18
|
+
import { type EndpointPlan, type PlanEndpointInput, type RunPlan } from "../x402/plan.js";
|
|
19
|
+
import { type PlannedCharge } from "./charge.js";
|
|
20
|
+
/** The scheme an MPP-01 entry of a plan carries, so the plan's other readers can tell it from x402. */
|
|
21
|
+
export declare const MPP_CHARGE_SCHEME = "mpp-charge";
|
|
22
|
+
export interface MppChargePlanInput {
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly target: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* One MPP charge endpoint's plan: what MPP-01 would sign, or why it would not
|
|
28
|
+
* pay ("not-paid": its challenge could not be read; "refused": the caps, the
|
|
29
|
+
* pin or the network do not allow it).
|
|
30
|
+
*/
|
|
31
|
+
export declare function planMppChargeEndpoint(network: string, endpoint: MppChargePlanInput, pin?: PinTerms): Promise<EndpointPlan>;
|
|
32
|
+
/** What MPP-01 may pay for an endpoint of the plan, or undefined when the plan does not let it pay. */
|
|
33
|
+
export declare function plannedChargeOf(entry: EndpointPlan | undefined): PlannedCharge | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* The x402 plan and the MPP-01 entries as one plan, against one cap: the
|
|
36
|
+
* run's worst case is the sum, and a sum above the cap refuses the run before
|
|
37
|
+
* anything is signed. With no MPP-01 entry it is the x402 plan, unchanged.
|
|
38
|
+
* Pure.
|
|
39
|
+
*/
|
|
40
|
+
export declare function combinePaymentPlan(x402: RunPlan, mppCharge: readonly EndpointPlan[]): RunPlan;
|
|
41
|
+
export interface PaymentRunPlanOptions {
|
|
42
|
+
readonly network: string;
|
|
43
|
+
readonly pin?: PinTerms;
|
|
44
|
+
/** A lower per-run cap in dollars. */
|
|
45
|
+
readonly maxRunUsd?: number;
|
|
46
|
+
/** The x402 endpoints this run pays. */
|
|
47
|
+
readonly x402: readonly PlanEndpointInput[];
|
|
48
|
+
/** The MPP charge endpoints this run pays. */
|
|
49
|
+
readonly mppCharge: readonly MppChargePlanInput[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The plan of a paying run on a test network: its x402 payment checks
|
|
53
|
+
* (`planX402Run`) and its MPP-01 payments, one cap for both. Throws the
|
|
54
|
+
* guard's refusal for a network that is not a test network (L0), before any
|
|
55
|
+
* request. Not for a mainnet run: that one pays x402 only, by its own plan.
|
|
56
|
+
*/
|
|
57
|
+
export declare function planPaymentRun(options: PaymentRunPlanOptions): Promise<RunPlan>;
|
|
58
|
+
/**
|
|
59
|
+
* The RPC of a run that pays MPP, checked before its service starts or any
|
|
60
|
+
* MPP endpoint is asked anything (D-023): it must serve the run's network,
|
|
61
|
+
* and for MPP-01 it must be https, because the SDK's charge client builds the
|
|
62
|
+
* payment through it and accepts no other. The suites check it again.
|
|
63
|
+
*
|
|
64
|
+
* @throws {RpcIdentityError} The RPC is not https for MPP-01, or does not serve the network.
|
|
65
|
+
*/
|
|
66
|
+
export declare function assertMppPaymentRpc(network: string, rpcUrl: string | undefined, uses: {
|
|
67
|
+
readonly charge: boolean;
|
|
68
|
+
}): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* The plan for a terminal or a log, as `describePlan` prints it; when the run
|
|
71
|
+
* pays MPP-01 too, its first line says so. `x402` and `mppCharge`: whether the
|
|
72
|
+
* run has endpoints of each.
|
|
73
|
+
*/
|
|
74
|
+
export declare function describePaymentPlan(plan: RunPlan, has: {
|
|
75
|
+
readonly x402: boolean;
|
|
76
|
+
readonly mppCharge: boolean;
|
|
77
|
+
}): string[];
|
package/dist/mpp/plan.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L1 for MPP-01 in `wasit run` (D-036): the charge an MPP charge endpoint asks
|
|
3
|
+
* for, read from its unpaid challenge before anything in the run is signed,
|
|
4
|
+
* held to the same caps as x402 ($1 a payment, one budget for the run), and
|
|
5
|
+
* planned together with the run's x402 payment checks.
|
|
6
|
+
*
|
|
7
|
+
* MPP pays on test networks only (L0): a run on any other network is refused
|
|
8
|
+
* here before any request. MPP-01 signs once per endpoint (mppx is built with
|
|
9
|
+
* `maxPaymentRetries: 1`, `charge.ts`), so its plan is one signature of the
|
|
10
|
+
* advertised amount. The channel checks sign commitments off-chain and Wasit
|
|
11
|
+
* sends no transaction for them, so they are not planned.
|
|
12
|
+
*
|
|
13
|
+
* It sends no payment, reads no key and signs nothing. `x402/plan.ts` is not
|
|
14
|
+
* changed by it: with no MPP charge endpoint the run's plan is the x402 plan
|
|
15
|
+
* as it was.
|
|
16
|
+
*/
|
|
17
|
+
import { classifyCheckError } from "../errors.js";
|
|
18
|
+
import { sanitizeText } from "../sanitize.js";
|
|
19
|
+
import { PER_PAYMENT_CAP_USD, RunBudget, formatUsd, sameAddress, shown, usdAsset, usdFromDollars, } from "../guard/budget.js";
|
|
20
|
+
import { assertPaymentNetworkAllowed } from "../guard/networks.js";
|
|
21
|
+
import { RpcIdentityError, assertRpcServesNetwork } from "../guard/rpc-identity.js";
|
|
22
|
+
import { describePlan, planX402Run } from "../x402/plan.js";
|
|
23
|
+
import { readChargeChallenge } from "./charge.js";
|
|
24
|
+
import { assertMppNetwork, resolveRpcUrl } from "./network.js";
|
|
25
|
+
/** The scheme an MPP-01 entry of a plan carries, so the plan's other readers can tell it from x402. */
|
|
26
|
+
export const MPP_CHARGE_SCHEME = "mpp-charge";
|
|
27
|
+
const CHECK_ID = "MPP-01";
|
|
28
|
+
const nothing = (name, status, reason) => ({
|
|
29
|
+
name,
|
|
30
|
+
status,
|
|
31
|
+
reason,
|
|
32
|
+
signatures: [],
|
|
33
|
+
worstCase: 0n,
|
|
34
|
+
worstCaseUsd: 0n,
|
|
35
|
+
});
|
|
36
|
+
function message(error) {
|
|
37
|
+
return sanitizeText(classifyCheckError(error).message).replace(/\s+/g, " ");
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* One MPP charge endpoint's plan: what MPP-01 would sign, or why it would not
|
|
41
|
+
* pay ("not-paid": its challenge could not be read; "refused": the caps, the
|
|
42
|
+
* pin or the network do not allow it).
|
|
43
|
+
*/
|
|
44
|
+
export async function planMppChargeEndpoint(network, endpoint, pin) {
|
|
45
|
+
// L0 before any request: MPP pays on test networks only, and on Stellar.
|
|
46
|
+
assertPaymentNetworkAllowed("mpp-charge", network);
|
|
47
|
+
assertMppNetwork(network);
|
|
48
|
+
let challenge;
|
|
49
|
+
try {
|
|
50
|
+
challenge = await readChargeChallenge(endpoint.target);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
// The same reason MPP-01 gives when it reads the challenge itself.
|
|
54
|
+
return nothing(endpoint.name, "not-paid", message(error));
|
|
55
|
+
}
|
|
56
|
+
const { amount, currency, recipient } = challenge;
|
|
57
|
+
if (challenge.network !== network) {
|
|
58
|
+
return nothing(endpoint.name, "refused", `it asks to be paid on ${challenge.network}, and this run pays on ${network} only. Nothing was signed.`);
|
|
59
|
+
}
|
|
60
|
+
if (pin !== undefined) {
|
|
61
|
+
for (const [field, value, pinned] of [
|
|
62
|
+
["recipient", recipient, pin.payTo],
|
|
63
|
+
["currency", currency, pin.asset],
|
|
64
|
+
]) {
|
|
65
|
+
if (!sameAddress(network, value, pinned)) {
|
|
66
|
+
return nothing(endpoint.name, "refused", `its charge has ${field} ${shown(value)}, not the pinned ${pinned}. Nothing was signed.`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const asset = usdAsset(network, currency);
|
|
71
|
+
const price = RunBudget.valueOf(network, currency, amount);
|
|
72
|
+
if (asset === undefined || price === undefined) {
|
|
73
|
+
return nothing(endpoint.name, "refused", `it asks to be paid in ${shown(currency)}, an asset Wasit does not value in US dollars, so it cannot be held ` +
|
|
74
|
+
`to the $${PER_PAYMENT_CAP_USD}.00 cap. Nothing was signed.`);
|
|
75
|
+
}
|
|
76
|
+
const perPayment = usdFromDollars(PER_PAYMENT_CAP_USD);
|
|
77
|
+
if (price > perPayment) {
|
|
78
|
+
return nothing(endpoint.name, "refused", `it is priced at ${amount} base units of ${asset.symbol} (${formatUsd(price)}), above the ` +
|
|
79
|
+
`${formatUsd(perPayment)} per payment fixed in Wasit. Nothing was signed. Test a route priced at ` +
|
|
80
|
+
`${formatUsd(perPayment)} or less.`);
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
name: endpoint.name,
|
|
84
|
+
status: "planned",
|
|
85
|
+
option: { scheme: MPP_CHARGE_SCHEME, network, payTo: recipient, asset: currency, amount, maxTimeoutSeconds: 0 },
|
|
86
|
+
signatures: [{ check: CHECK_ID, amount }],
|
|
87
|
+
worstCase: amount,
|
|
88
|
+
worstCaseUsd: price,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** What MPP-01 may pay for an endpoint of the plan, or undefined when the plan does not let it pay. */
|
|
92
|
+
export function plannedChargeOf(entry) {
|
|
93
|
+
if (entry?.status !== "planned" || entry.option?.scheme !== MPP_CHARGE_SCHEME)
|
|
94
|
+
return undefined;
|
|
95
|
+
return { amount: entry.option.amount, currency: entry.option.asset, recipient: entry.option.payTo };
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The x402 plan and the MPP-01 entries as one plan, against one cap: the
|
|
99
|
+
* run's worst case is the sum, and a sum above the cap refuses the run before
|
|
100
|
+
* anything is signed. With no MPP-01 entry it is the x402 plan, unchanged.
|
|
101
|
+
* Pure.
|
|
102
|
+
*/
|
|
103
|
+
export function combinePaymentPlan(x402, mppCharge) {
|
|
104
|
+
if (mppCharge.length === 0)
|
|
105
|
+
return x402;
|
|
106
|
+
const endpoints = [...x402.endpoints, ...mppCharge];
|
|
107
|
+
const planned = endpoints.filter((endpoint) => endpoint.status === "planned");
|
|
108
|
+
const worstCaseUsd = planned.reduce((sum, endpoint) => sum + endpoint.worstCaseUsd, 0n);
|
|
109
|
+
const { refusal: _x402Refusal, ...rest } = x402;
|
|
110
|
+
const plan = { ...rest, endpoints, worstCaseUsd };
|
|
111
|
+
// No entry may lower the sum: a worst case below zero is not a price, and the
|
|
112
|
+
// challenge reader refuses one. Were one here, the run's sum would hide the rest.
|
|
113
|
+
const negative = planned.filter((endpoint) => endpoint.worstCaseUsd < 0n);
|
|
114
|
+
if (negative.length > 0) {
|
|
115
|
+
return {
|
|
116
|
+
...plan,
|
|
117
|
+
refusal: `The plan has a worst case below zero for ${negative.map((endpoint) => sanitizeText(endpoint.name)).join(", ")}, ` +
|
|
118
|
+
`which is not a price. Nothing was signed.`,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (worstCaseUsd <= x402.capUsd)
|
|
122
|
+
return plan;
|
|
123
|
+
const parts = planned.map((endpoint) => `${sanitizeText(endpoint.name)} ${formatUsd(endpoint.worstCaseUsd)}`);
|
|
124
|
+
return {
|
|
125
|
+
...plan,
|
|
126
|
+
refusal: `This run could spend up to ${formatUsd(worstCaseUsd)} (${parts.join(", ")}), above the ` +
|
|
127
|
+
`${formatUsd(x402.capUsd)} per run${x402.capLowered ? " set by maxRunUsd" : ""}. Nothing was signed. ` +
|
|
128
|
+
`Select fewer endpoints with --endpoint, or test a cheaper route.`,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The plan of a paying run on a test network: its x402 payment checks
|
|
133
|
+
* (`planX402Run`) and its MPP-01 payments, one cap for both. Throws the
|
|
134
|
+
* guard's refusal for a network that is not a test network (L0), before any
|
|
135
|
+
* request. Not for a mainnet run: that one pays x402 only, by its own plan.
|
|
136
|
+
*/
|
|
137
|
+
export async function planPaymentRun(options) {
|
|
138
|
+
if (options.mppCharge.length > 0)
|
|
139
|
+
assertPaymentNetworkAllowed("mpp-charge", options.network);
|
|
140
|
+
const x402 = await planX402Run({
|
|
141
|
+
network: options.network,
|
|
142
|
+
...(options.pin === undefined ? {} : { pin: options.pin }),
|
|
143
|
+
...(options.maxRunUsd === undefined ? {} : { maxRunUsd: options.maxRunUsd }),
|
|
144
|
+
endpoints: options.x402,
|
|
145
|
+
});
|
|
146
|
+
const mppCharge = [];
|
|
147
|
+
for (const endpoint of options.mppCharge) {
|
|
148
|
+
mppCharge.push(await planMppChargeEndpoint(options.network, endpoint, options.pin));
|
|
149
|
+
}
|
|
150
|
+
return combinePaymentPlan(x402, mppCharge);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The RPC of a run that pays MPP, checked before its service starts or any
|
|
154
|
+
* MPP endpoint is asked anything (D-023): it must serve the run's network,
|
|
155
|
+
* and for MPP-01 it must be https, because the SDK's charge client builds the
|
|
156
|
+
* payment through it and accepts no other. The suites check it again.
|
|
157
|
+
*
|
|
158
|
+
* @throws {RpcIdentityError} The RPC is not https for MPP-01, or does not serve the network.
|
|
159
|
+
*/
|
|
160
|
+
export async function assertMppPaymentRpc(network, rpcUrl, uses) {
|
|
161
|
+
const endpoint = resolveRpcUrl(network, rpcUrl);
|
|
162
|
+
if (uses.charge) {
|
|
163
|
+
let protocol = "";
|
|
164
|
+
try {
|
|
165
|
+
protocol = new URL(endpoint).protocol;
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
protocol = "";
|
|
169
|
+
}
|
|
170
|
+
if (protocol !== "https:") {
|
|
171
|
+
throw new RpcIdentityError(`MPP-01 pays through @stellar/mpp's charge client, which builds the payment through the run's RPC and ` +
|
|
172
|
+
`accepts only an https RPC, and this run's RPC is not https. No payment was sent and nothing was signed.`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
await assertRpcServesNetwork(network, endpoint);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* The plan for a terminal or a log, as `describePlan` prints it; when the run
|
|
179
|
+
* pays MPP-01 too, its first line says so. `x402` and `mppCharge`: whether the
|
|
180
|
+
* run has endpoints of each.
|
|
181
|
+
*/
|
|
182
|
+
export function describePaymentPlan(plan, has) {
|
|
183
|
+
const lines = describePlan(plan);
|
|
184
|
+
if (has.x402 && !has.mppCharge)
|
|
185
|
+
return lines;
|
|
186
|
+
const what = has.x402 ? "x402 payment checks and MPP-01" : has.mppCharge ? "MPP-01" : "no payment to plan (no x402 or MPP charge endpoint)";
|
|
187
|
+
return [sanitizeText(`Plan (dry run): ${what} on ${plan.network}. Nothing is signed or sent.`), ...lines.slice(1)];
|
|
188
|
+
}
|
package/dist/mpp/suite.d.ts
CHANGED
|
@@ -24,6 +24,12 @@ export interface MppChannelSuiteOptions {
|
|
|
24
24
|
* on two contracts at once.
|
|
25
25
|
*/
|
|
26
26
|
readonly channelOverride?: string;
|
|
27
|
+
/**
|
|
28
|
+
* The only channel the checks sign commitments for (`wasit run` passes the
|
|
29
|
+
* config's `channel`): a challenge that names another gets no signature.
|
|
30
|
+
* Without it they sign for the channel each challenge names.
|
|
31
|
+
*/
|
|
32
|
+
readonly onlyChannel?: string;
|
|
27
33
|
readonly expected?: Partial<ChannelDeployExpectations>;
|
|
28
34
|
readonly allowDestructive?: boolean;
|
|
29
35
|
readonly destructiveChannel?: string;
|
package/dist/mpp/suite.js
CHANGED
|
@@ -84,7 +84,7 @@ async function runDeployCheck(params) {
|
|
|
84
84
|
* because a close is terminal.
|
|
85
85
|
*/
|
|
86
86
|
export async function runMppChannelSuite(options) {
|
|
87
|
-
const { target, commitmentSecretHex, network, rpcUrl, channelOverride, expected, allowDestructive = false, destructiveChannel, } = options;
|
|
87
|
+
const { target, commitmentSecretHex, network, rpcUrl, channelOverride, onlyChannel, expected, allowDestructive = false, destructiveChannel, } = options;
|
|
88
88
|
// A bad URL or an unknown network is wrong for every check in the suite, so
|
|
89
89
|
// it is reported once here instead of repeated identically per check. L0
|
|
90
90
|
// runs here too: the suite signs commitments and may close a channel, so a
|
|
@@ -107,6 +107,7 @@ export async function runMppChannelSuite(options) {
|
|
|
107
107
|
commitmentSecretHex,
|
|
108
108
|
network,
|
|
109
109
|
...(rpcUrl ? { rpcUrl } : {}),
|
|
110
|
+
...(onlyChannel ? { onlyChannel } : {}),
|
|
110
111
|
};
|
|
111
112
|
// The target decides which channel it bills through, so its challenge — not
|
|
112
113
|
// the operator's environment — is the authority on what this run is about.
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What `wasit init` reads from a route (spec C2, C7 row 4; W3-2 slice 3).
|
|
3
|
+
*
|
|
4
|
+
* One unpaid request with the method and body the route takes: no payment,
|
|
5
|
+
* no signature, no key. A redirect is followed once inside the origin, and
|
|
6
|
+
* only when it keeps the request, the rule of the unpaid request a payment
|
|
7
|
+
* check reads (`fetchPaymentChallenge`, spec A8 row 5); any other redirect is
|
|
8
|
+
* reported and nothing is sent to its Location. The answer is data: read,
|
|
9
|
+
* never followed or run (rule 3). What a message takes from it passes through
|
|
10
|
+
* the sanitiser (A8 row 6), on one line and bounded; what the config takes
|
|
11
|
+
* from it is held to the config's own rules before it is written.
|
|
12
|
+
*
|
|
13
|
+
* Reading. A `PAYMENT-REQUIRED` header (or `X-Payment`, which the read checks
|
|
14
|
+
* also accept) is x402, one offer per payment option. A `WWW-Authenticate`
|
|
15
|
+
* header with `Payment` challenges is MPP: the intent tells charge from
|
|
16
|
+
* channel, and a channel challenge names its channel. MPP's network is read
|
|
17
|
+
* the way the Stellar MPP client reads it, so a challenge that names none is
|
|
18
|
+
* stellar:testnet (`@stellar/mpp` 0.7.1 `resolveNetworkId`,
|
|
19
|
+
* dist/shared/validation.js; the charge and the channel client both call it).
|
|
20
|
+
* An x402 v1 challenge in the body is reported as v1.
|
|
21
|
+
*
|
|
22
|
+
* Choosing. init writes one protocol on one network per route, and only what
|
|
23
|
+
* is certain (Najmi, 2026-11-15): with several ways to pay on test networks
|
|
24
|
+
* and no flag to choose, it does not choose; a network that is mainnet or
|
|
25
|
+
* that Wasit does not know is not written in this version.
|
|
26
|
+
*/
|
|
27
|
+
import type { PinConfig } from "./config.js";
|
|
28
|
+
import { type RequestShape } from "./x402/simulator.js";
|
|
29
|
+
/** How long init waits for the route's answer. */
|
|
30
|
+
export declare const ROUTE_READ_TIMEOUT_MS = 15000;
|
|
31
|
+
export type RouteProtocol = "x402" | "mpp-charge" | "mpp-channel";
|
|
32
|
+
/** One way the route asks to be paid, as its challenge writes it. Every value is the service's: not trusted. */
|
|
33
|
+
export interface RouteOffer {
|
|
34
|
+
readonly protocol: RouteProtocol;
|
|
35
|
+
/** A CAIP-2 id, as the challenge names it; MPP: as the Stellar MPP client reads it. */
|
|
36
|
+
readonly network: string;
|
|
37
|
+
/** False for an MPP challenge that names no network (read as stellar:testnet). */
|
|
38
|
+
readonly networkNamed: boolean;
|
|
39
|
+
/** The price in base units. */
|
|
40
|
+
readonly amount?: string;
|
|
41
|
+
/** x402 `asset`; MPP charge `currency`. */
|
|
42
|
+
readonly asset?: string;
|
|
43
|
+
/** x402 `payTo`; MPP charge `recipient`. */
|
|
44
|
+
readonly payTo?: string;
|
|
45
|
+
/** MPP channel: the channel contract it bills through. */
|
|
46
|
+
readonly channel?: string;
|
|
47
|
+
}
|
|
48
|
+
/** What the route answered. `url` is where the answer came from: the route's own URL, or the one redirect inside its origin. */
|
|
49
|
+
export type RouteReading = {
|
|
50
|
+
readonly kind: "challenge";
|
|
51
|
+
readonly url: string;
|
|
52
|
+
readonly offers: readonly RouteOffer[];
|
|
53
|
+
readonly notes: readonly string[];
|
|
54
|
+
} | {
|
|
55
|
+
readonly kind: "x402-v1";
|
|
56
|
+
readonly url: string;
|
|
57
|
+
} | {
|
|
58
|
+
readonly kind: "unreadable";
|
|
59
|
+
readonly url: string;
|
|
60
|
+
readonly notes: readonly string[];
|
|
61
|
+
} | {
|
|
62
|
+
readonly kind: "status";
|
|
63
|
+
readonly url: string;
|
|
64
|
+
readonly status: number;
|
|
65
|
+
} | {
|
|
66
|
+
readonly kind: "unreachable";
|
|
67
|
+
readonly message: string;
|
|
68
|
+
} | {
|
|
69
|
+
readonly kind: "redirect";
|
|
70
|
+
readonly message: string;
|
|
71
|
+
};
|
|
72
|
+
/** The answer as received, for {@link interpretRouteAnswer}. */
|
|
73
|
+
export interface RouteAnswer {
|
|
74
|
+
readonly status: number;
|
|
75
|
+
readonly headers: Headers;
|
|
76
|
+
/** The body, read only for a 402 without a payment header; otherwise "". */
|
|
77
|
+
readonly body: string;
|
|
78
|
+
readonly url: string;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* What an answer says, without a network: the recorded answers of the tests
|
|
82
|
+
* go through the same reading as a live one.
|
|
83
|
+
*/
|
|
84
|
+
export declare function interpretRouteAnswer(answer: RouteAnswer): RouteReading;
|
|
85
|
+
export interface ReadRouteOptions {
|
|
86
|
+
/** Default {@link ROUTE_READ_TIMEOUT_MS}. */
|
|
87
|
+
readonly timeoutMs?: number;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Sends the route one unpaid request and reads its answer.
|
|
91
|
+
*
|
|
92
|
+
* @throws {ConfigurationError} For a URL or a request shape that cannot be sent (checked before anything is sent).
|
|
93
|
+
*/
|
|
94
|
+
export declare function readRoute(url: string, shape: RequestShape, options?: ReadRouteOptions): Promise<RouteReading>;
|
|
95
|
+
/** One offer for a line of output. Sanitised. */
|
|
96
|
+
export declare function describeOffer(offer: RouteOffer): string;
|
|
97
|
+
/** What init may choose: a protocol, a network, or both (`--protocol`, `--network`). */
|
|
98
|
+
export interface OfferWanted {
|
|
99
|
+
readonly protocol?: RouteProtocol;
|
|
100
|
+
readonly network?: string;
|
|
101
|
+
}
|
|
102
|
+
export type OfferChoice = {
|
|
103
|
+
readonly ok: true;
|
|
104
|
+
readonly protocol: RouteProtocol;
|
|
105
|
+
readonly network: string;
|
|
106
|
+
/** The offers of that protocol on that network. */
|
|
107
|
+
readonly offers: readonly RouteOffer[];
|
|
108
|
+
/** The other offers, not written. */
|
|
109
|
+
readonly left: readonly RouteOffer[];
|
|
110
|
+
/** x402 and MPP charge: the receiving address and asset, when the offers agree on one of each. */
|
|
111
|
+
readonly pin?: PinConfig;
|
|
112
|
+
/** Why there is no pin, when there could have been one. */
|
|
113
|
+
readonly noPin?: string;
|
|
114
|
+
/** MPP channel: the channel contract. */
|
|
115
|
+
readonly channel?: string;
|
|
116
|
+
} | {
|
|
117
|
+
readonly ok: false;
|
|
118
|
+
readonly reason: string;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* The one way to pay init writes for this route, or why it writes none.
|
|
122
|
+
* Pure: the same offers and flags always give the same answer.
|
|
123
|
+
*/
|
|
124
|
+
export declare function chooseOffer(offers: readonly RouteOffer[], wanted?: OfferWanted): OfferChoice;
|