@wasit-dev/core 0.7.0 → 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.d.ts +109 -0
- package/dist/baseline.js +353 -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 +399 -0
- package/dist/config-writer.d.ts +96 -0
- package/dist/config-writer.js +281 -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 +138 -0
- package/dist/guard/budget.js +206 -0
- package/dist/guard/ci-event.d.ts +56 -0
- package/dist/guard/ci-event.js +129 -0
- 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 +51 -0
- package/dist/guard/networks.js +91 -0
- package/dist/guard/optin.d.ts +106 -0
- package/dist/guard/optin.js +202 -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/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 +44 -4
- package/dist/index.js +42 -2
- package/dist/mpp/channel-commitment.d.ts +48 -2
- package/dist/mpp/channel-commitment.js +80 -4
- package/dist/mpp/channel-credential.d.ts +13 -1
- package/dist/mpp/channel-credential.js +21 -2
- package/dist/mpp/channel.d.ts +6 -0
- package/dist/mpp/channel.js +36 -3
- package/dist/mpp/charge.d.ts +33 -1
- package/dist/mpp/charge.js +90 -20
- package/dist/mpp/network.js +4 -1
- 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 +12 -3
- package/dist/report.d.ts +163 -0
- package/dist/report.js +282 -0
- package/dist/route-reader.d.ts +124 -0
- package/dist/route-reader.js +349 -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/wallet.d.ts +58 -1
- package/dist/wallet.js +128 -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 +259 -4
- package/dist/x402/chains/types.d.ts +104 -2
- package/dist/x402/plan.d.ts +184 -0
- package/dist/x402/plan.js +520 -0
- package/dist/x402/rejected-request.d.ts +156 -0
- package/dist/x402/rejected-request.js +287 -0
- package/dist/x402/requirements.d.ts +1 -0
- package/dist/x402/requirements.js +37 -4
- package/dist/x402/simulator.d.ts +86 -1
- package/dist/x402/simulator.js +863 -87
- package/package.json +1 -1
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mainnet opt-in (spec B5), as one object: made by core only
|
|
3
|
+
* (`openMainnetOptIn`, guard/mainnet-run.ts), for one run.
|
|
4
|
+
*
|
|
5
|
+
* An x402 payment on `stellar:pubnet` is made only with a live opt-in: L0
|
|
6
|
+
* passes that network with one and with nothing else. It holds what the run
|
|
7
|
+
* pays by (the payer's public address, the pin, the RPC, the budget) and what
|
|
8
|
+
* the run has done so far (its plan, its ledger, whether it was stopped or
|
|
9
|
+
* closed). It never holds the key.
|
|
10
|
+
*
|
|
11
|
+
* Its constructor takes a symbol only this module has, and every opt-in it
|
|
12
|
+
* makes is kept in a set only this module reads, so an object that only looks
|
|
13
|
+
* like one (a literal, a copy, `Object.create` of its prototype) is never taken
|
|
14
|
+
* for one. The object and its prototype are frozen, and whether it is live is
|
|
15
|
+
* read from its private state, never from a property: no caller can reassign
|
|
16
|
+
* its RPC, payer, budget or methods, or make a stopped one read as live. That
|
|
17
|
+
* stops a mistake or a forged object; code that reaches into the package's
|
|
18
|
+
* files holds the key itself and is outside every rule of Wasit.
|
|
19
|
+
*/
|
|
20
|
+
import { ConfigurationError } from "../errors.js";
|
|
21
|
+
import { PER_PAYMENT_CAP_USD, formatUsd, usdFromDollars } from "./budget.js";
|
|
22
|
+
import { RunLedger } from "./run-ledger.js";
|
|
23
|
+
import { SigningAnchor } from "./signed-terms.js";
|
|
24
|
+
const ISSUE = Symbol("mainnet opt-in");
|
|
25
|
+
const issued = new WeakSet();
|
|
26
|
+
const anchors = new WeakMap();
|
|
27
|
+
const plans = new WeakMap();
|
|
28
|
+
/** The opt-in's private state, read by the module's own functions (set in the class's static block). */
|
|
29
|
+
let stoppedOf;
|
|
30
|
+
let closedOf;
|
|
31
|
+
/** A plan, a pin or anything else the opt-in keeps: frozen all the way down, so no caller changes it later. */
|
|
32
|
+
function deepFreeze(value) {
|
|
33
|
+
if (typeof value === "object" && value !== null && !Object.isFrozen(value)) {
|
|
34
|
+
Object.freeze(value);
|
|
35
|
+
for (const item of Object.values(value))
|
|
36
|
+
deepFreeze(item);
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A mainnet run does not open, or a step of it is refused. A
|
|
42
|
+
* `ConfigurationError`: it says nothing about the target. Nothing was started,
|
|
43
|
+
* sent or signed by the step that threw it. A stop of a run that is open
|
|
44
|
+
* (`during`) says that nothing more is signed in it: an earlier endpoint of
|
|
45
|
+
* the run may have paid.
|
|
46
|
+
*/
|
|
47
|
+
export class MainnetRunRefusedError extends ConfigurationError {
|
|
48
|
+
reason;
|
|
49
|
+
constructor(reason, message, options = {}) {
|
|
50
|
+
super(`${message} ${options.during === true ? "Nothing more is signed in this run." : "Nothing was started, sent or signed."}`);
|
|
51
|
+
this.name = "MainnetRunRefusedError";
|
|
52
|
+
this.reason = reason;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function refuse(message) {
|
|
56
|
+
return new MainnetRunRefusedError("run", message);
|
|
57
|
+
}
|
|
58
|
+
export class MainnetOptIn {
|
|
59
|
+
network = "stellar:pubnet";
|
|
60
|
+
frontEnd;
|
|
61
|
+
environment;
|
|
62
|
+
/** The payer's public address, derived from the key when the opt-in was made. */
|
|
63
|
+
payer;
|
|
64
|
+
pin;
|
|
65
|
+
rpcUrl;
|
|
66
|
+
/** The run's budget: its own $5, or the MCP server's for the whole process (D-018). */
|
|
67
|
+
budget;
|
|
68
|
+
/** Where the lock is, and where the ledger is written. */
|
|
69
|
+
ledgerDir;
|
|
70
|
+
#lock;
|
|
71
|
+
#ledger;
|
|
72
|
+
#stopped;
|
|
73
|
+
#closed = false;
|
|
74
|
+
static {
|
|
75
|
+
stoppedOf = (optIn) => optIn.#stopped;
|
|
76
|
+
closedOf = (optIn) => optIn.#closed;
|
|
77
|
+
}
|
|
78
|
+
/** Core only: `openMainnetOptIn`. */
|
|
79
|
+
constructor(key, fields) {
|
|
80
|
+
if (key !== ISSUE)
|
|
81
|
+
throw new TypeError("A mainnet opt-in is made by openMainnetOptIn only.");
|
|
82
|
+
this.frontEnd = fields.frontEnd;
|
|
83
|
+
this.environment = fields.environment;
|
|
84
|
+
this.payer = fields.payer;
|
|
85
|
+
this.pin = deepFreeze({ payTo: fields.pin.payTo, asset: fields.pin.asset });
|
|
86
|
+
this.rpcUrl = fields.rpcUrl;
|
|
87
|
+
this.budget = fields.budget;
|
|
88
|
+
this.ledgerDir = fields.ledgerDir;
|
|
89
|
+
this.#lock = fields.lock;
|
|
90
|
+
issued.add(this);
|
|
91
|
+
anchors.set(this, new SigningAnchor());
|
|
92
|
+
// Its fields cannot be reassigned, nor a property added over its getters; its private state still moves.
|
|
93
|
+
Object.freeze(this);
|
|
94
|
+
}
|
|
95
|
+
/** The plan L1 made for this run, once it passed. */
|
|
96
|
+
get plan() {
|
|
97
|
+
return plans.get(this);
|
|
98
|
+
}
|
|
99
|
+
get ledger() {
|
|
100
|
+
return this.#ledger;
|
|
101
|
+
}
|
|
102
|
+
/** Why the run stopped (the first reason), if it did. Nothing is signed after it. */
|
|
103
|
+
get stopped() {
|
|
104
|
+
return this.#stopped;
|
|
105
|
+
}
|
|
106
|
+
get closed() {
|
|
107
|
+
return this.#closed;
|
|
108
|
+
}
|
|
109
|
+
/** Stops the whole run (spec B3): nothing more is signed with this opt-in. The first reason is kept. */
|
|
110
|
+
stop(reason) {
|
|
111
|
+
this.#stopped ??= reason;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Opens the run ledger (spec B5) in the ledger folder: the header from this
|
|
115
|
+
* opt-in and its plan, the RPC by its origin only, and the balances before
|
|
116
|
+
* as L1 read them on public Horizon. Only after a plan that passed, once,
|
|
117
|
+
* and before the first signature: a payment on mainnet without it is refused.
|
|
118
|
+
*/
|
|
119
|
+
openLedger(versions, options = {}) {
|
|
120
|
+
if (!isLiveOptIn(this, this.network))
|
|
121
|
+
throw refuse("The mainnet run is stopped or closed, so its ledger is not opened.");
|
|
122
|
+
const plan = plans.get(this);
|
|
123
|
+
if (plan?.mainnet === undefined)
|
|
124
|
+
throw refuse("The mainnet run has no plan that passed yet, so its ledger is not opened.");
|
|
125
|
+
if (this.#ledger !== undefined)
|
|
126
|
+
throw refuse("The mainnet run's ledger is already open.");
|
|
127
|
+
const ledger = RunLedger.create(this.ledgerDir, {
|
|
128
|
+
versions,
|
|
129
|
+
environment: this.environment,
|
|
130
|
+
network: this.network,
|
|
131
|
+
payer: this.payer,
|
|
132
|
+
pin: this.pin,
|
|
133
|
+
caps: { perPaymentUsd: formatUsd(usdFromDollars(PER_PAYMENT_CAP_USD)), runUsd: formatUsd(this.budget.capUsd) },
|
|
134
|
+
plan,
|
|
135
|
+
rpc: new URL(this.rpcUrl).origin,
|
|
136
|
+
}, options);
|
|
137
|
+
ledger.recordBalancesBefore({ usdc: plan.mainnet.usdc, native: plan.mainnet.native }, `public Horizon (${plan.mainnet.horizon})`);
|
|
138
|
+
this.#ledger = ledger;
|
|
139
|
+
return ledger;
|
|
140
|
+
}
|
|
141
|
+
/** Ends the run: nothing more is signed with it, and its lock is released. */
|
|
142
|
+
close() {
|
|
143
|
+
if (this.#closed)
|
|
144
|
+
return;
|
|
145
|
+
this.#closed = true;
|
|
146
|
+
this.#lock.release();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// Its methods and getters cannot be replaced for every opt-in at once.
|
|
150
|
+
Object.freeze(MainnetOptIn.prototype);
|
|
151
|
+
Object.freeze(MainnetOptIn);
|
|
152
|
+
/** Core only: L1 records the plan that passed, once. Not a method, so no caller reaches it through an opt-in. */
|
|
153
|
+
export function recordMainnetPlan(optIn, plan) {
|
|
154
|
+
if (!issued.has(optIn) || plans.has(optIn))
|
|
155
|
+
throw refuse("The mainnet run already has a plan.");
|
|
156
|
+
plans.set(optIn, deepFreeze(plan));
|
|
157
|
+
}
|
|
158
|
+
/** Core only: the one way to make an opt-in, used by `openMainnetOptIn`. */
|
|
159
|
+
export function issueMainnetOptIn(fields) {
|
|
160
|
+
return new MainnetOptIn(ISSUE, fields);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The run's anchor (spec B17.2): read in L1 beside public Horizon's latest
|
|
164
|
+
* ledger, never at the first signature. Core only: not on the opt-in, so no
|
|
165
|
+
* caller can set it first.
|
|
166
|
+
*/
|
|
167
|
+
export function anchorOfOptIn(optIn) {
|
|
168
|
+
const anchor = anchors.get(optIn);
|
|
169
|
+
if (anchor === undefined)
|
|
170
|
+
throw new TypeError("Not a mainnet opt-in.");
|
|
171
|
+
return anchor;
|
|
172
|
+
}
|
|
173
|
+
/** True for an opt-in core made, live, stopped or closed alike. */
|
|
174
|
+
export function isMainnetOptIn(value) {
|
|
175
|
+
return typeof value === "object" && value !== null && issued.has(value);
|
|
176
|
+
}
|
|
177
|
+
/** True only for an opt-in core made, for `network`, neither stopped nor closed. */
|
|
178
|
+
export function isLiveOptIn(value, network) {
|
|
179
|
+
return (typeof value === "object" &&
|
|
180
|
+
value !== null &&
|
|
181
|
+
issued.has(value) &&
|
|
182
|
+
value.network === network &&
|
|
183
|
+
stoppedOf(value) === undefined &&
|
|
184
|
+
!closedOf(value));
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Why `value`, an opt-in core made, is not live for `network`, as a clause;
|
|
188
|
+
* undefined when it is live or is not an opt-in at all.
|
|
189
|
+
*/
|
|
190
|
+
export function whyNotLive(value, network) {
|
|
191
|
+
if (typeof value !== "object" || value === null || !issued.has(value))
|
|
192
|
+
return undefined;
|
|
193
|
+
const optIn = value;
|
|
194
|
+
const stopped = stoppedOf(optIn);
|
|
195
|
+
if (stopped !== undefined)
|
|
196
|
+
return `the mainnet run stopped, so nothing more is signed in it: ${stopped.replace(/\.\s*$/, "")}`;
|
|
197
|
+
if (closedOf(optIn))
|
|
198
|
+
return "the mainnet run is closed, so nothing more is signed in it";
|
|
199
|
+
if (optIn.network !== network)
|
|
200
|
+
return `the mainnet opt-in is for ${optIn.network}, not ${network}`;
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The RPC identity check (D-032, D-023a+b): before a run signs anything or
|
|
3
|
+
* reads a settlement, the RPC it was given must say which network it serves,
|
|
4
|
+
* and it must be the run's.
|
|
5
|
+
*
|
|
6
|
+
* L0 judges the network the run is labelled with. The RPC is a separate input
|
|
7
|
+
* (`--rpc-url`, the MCP tool argument `rpcUrl`, the config), and nothing tied
|
|
8
|
+
* the two together: a `stellar:testnet` run pointed at a mainnet RPC signed a
|
|
9
|
+
* mainnet-valid channel commitment in 0.7.0, and every settlement read of such
|
|
10
|
+
* a run comes from another chain. So each entry point that signs or reads a
|
|
11
|
+
* settlement asks the RPC first:
|
|
12
|
+
*
|
|
13
|
+
* - Stellar: `getNetwork`, whose `passphrase` must be the run's network's;
|
|
14
|
+
* - EVM: `eth_chainId`, which must equal the CAIP-2 reference;
|
|
15
|
+
* - Solana: `getGenesisHash`, whose first characters are the CAIP-2 reference.
|
|
16
|
+
*
|
|
17
|
+
* A mismatch, no answer, or an answer that cannot be read refuses the run
|
|
18
|
+
* before any request reaches the target (fail closed). This guards against a
|
|
19
|
+
* mistake or a wrong default, not against an RPC that lies: an RPC that answers
|
|
20
|
+
* the right identity and then serves another chain is outside what any client
|
|
21
|
+
* check can see.
|
|
22
|
+
*/
|
|
23
|
+
import { ConfigurationError } from "../errors.js";
|
|
24
|
+
/** The RPC does not serve the run's network, or would not say which one it serves. Nothing was sent or signed. */
|
|
25
|
+
export declare class RpcIdentityError extends ConfigurationError {
|
|
26
|
+
constructor(message: string);
|
|
27
|
+
}
|
|
28
|
+
/** One JSON-RPC call: the `result`, or a throw. */
|
|
29
|
+
export type RpcIdentityTransport = (rpcUrl: string, method: string) => Promise<unknown>;
|
|
30
|
+
/**
|
|
31
|
+
* Tests only: replaces the JSON-RPC call (an SDK that accepts only https RPCs
|
|
32
|
+
* cannot be given a loopback stand-in). `undefined` restores the real one. Not
|
|
33
|
+
* exported from the package index, and reachable from no flag, setting or tool
|
|
34
|
+
* argument.
|
|
35
|
+
*/
|
|
36
|
+
export declare function setRpcIdentityTransportForTesting(replacement: RpcIdentityTransport | undefined): void;
|
|
37
|
+
/**
|
|
38
|
+
* Refuses unless the RPC at `rpcUrl` says it serves `network`.
|
|
39
|
+
*
|
|
40
|
+
* @throws {RpcIdentityError} The RPC serves another network, did not answer, or
|
|
41
|
+
* answered something that does not name a network; or Wasit has no rule to
|
|
42
|
+
* tell which network an RPC for `network` serves. Nothing was sent to the
|
|
43
|
+
* target and nothing was signed.
|
|
44
|
+
*/
|
|
45
|
+
export declare function assertRpcServesNetwork(network: string, rpcUrl: string): Promise<void>;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The RPC identity check (D-032, D-023a+b): before a run signs anything or
|
|
3
|
+
* reads a settlement, the RPC it was given must say which network it serves,
|
|
4
|
+
* and it must be the run's.
|
|
5
|
+
*
|
|
6
|
+
* L0 judges the network the run is labelled with. The RPC is a separate input
|
|
7
|
+
* (`--rpc-url`, the MCP tool argument `rpcUrl`, the config), and nothing tied
|
|
8
|
+
* the two together: a `stellar:testnet` run pointed at a mainnet RPC signed a
|
|
9
|
+
* mainnet-valid channel commitment in 0.7.0, and every settlement read of such
|
|
10
|
+
* a run comes from another chain. So each entry point that signs or reads a
|
|
11
|
+
* settlement asks the RPC first:
|
|
12
|
+
*
|
|
13
|
+
* - Stellar: `getNetwork`, whose `passphrase` must be the run's network's;
|
|
14
|
+
* - EVM: `eth_chainId`, which must equal the CAIP-2 reference;
|
|
15
|
+
* - Solana: `getGenesisHash`, whose first characters are the CAIP-2 reference.
|
|
16
|
+
*
|
|
17
|
+
* A mismatch, no answer, or an answer that cannot be read refuses the run
|
|
18
|
+
* before any request reaches the target (fail closed). This guards against a
|
|
19
|
+
* mistake or a wrong default, not against an RPC that lies: an RPC that answers
|
|
20
|
+
* the right identity and then serves another chain is outside what any client
|
|
21
|
+
* check can see.
|
|
22
|
+
*/
|
|
23
|
+
import { Networks } from "@stellar/stellar-sdk";
|
|
24
|
+
import { ConfigurationError } from "../errors.js";
|
|
25
|
+
import { sanitizeText } from "../sanitize.js";
|
|
26
|
+
/** The RPC does not serve the run's network, or would not say which one it serves. Nothing was sent or signed. */
|
|
27
|
+
export class RpcIdentityError extends ConfigurationError {
|
|
28
|
+
constructor(message) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = "RpcIdentityError";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const TIMEOUT_MS = 10_000;
|
|
34
|
+
/** The origin of the RPC for a message: its path or query may hold an API key. */
|
|
35
|
+
function origin(rpcUrl) {
|
|
36
|
+
try {
|
|
37
|
+
const parsed = new URL(rpcUrl);
|
|
38
|
+
return parsed.protocol === "http:" || parsed.protocol === "https:" ? parsed.origin : "an RPC that is not an http URL";
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return "an RPC that is not a URL";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const jsonRpc = async (rpcUrl, method) => {
|
|
45
|
+
const response = await fetch(rpcUrl, {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers: { "content-type": "application/json" },
|
|
48
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method }),
|
|
49
|
+
// An RPC that redirects is not followed: the identity must come from the
|
|
50
|
+
// URL the run will use.
|
|
51
|
+
redirect: "manual",
|
|
52
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
53
|
+
});
|
|
54
|
+
if (response.status !== 200) {
|
|
55
|
+
await response.body?.cancel().catch(() => undefined);
|
|
56
|
+
throw new Error(`HTTP ${response.status}`);
|
|
57
|
+
}
|
|
58
|
+
const body = (await response.json());
|
|
59
|
+
if (body.error !== undefined) {
|
|
60
|
+
throw new Error(typeof body.error.message === "string" ? body.error.message : "a JSON-RPC error");
|
|
61
|
+
}
|
|
62
|
+
return body.result;
|
|
63
|
+
};
|
|
64
|
+
let transport = jsonRpc;
|
|
65
|
+
/**
|
|
66
|
+
* Tests only: replaces the JSON-RPC call (an SDK that accepts only https RPCs
|
|
67
|
+
* cannot be given a loopback stand-in). `undefined` restores the real one. Not
|
|
68
|
+
* exported from the package index, and reachable from no flag, setting or tool
|
|
69
|
+
* argument.
|
|
70
|
+
*/
|
|
71
|
+
export function setRpcIdentityTransportForTesting(replacement) {
|
|
72
|
+
transport = replacement ?? jsonRpc;
|
|
73
|
+
}
|
|
74
|
+
const STELLAR_PASSPHRASES = new Map([
|
|
75
|
+
["stellar:testnet", Networks.TESTNET],
|
|
76
|
+
["stellar:pubnet", Networks.PUBLIC],
|
|
77
|
+
]);
|
|
78
|
+
/** The network a Stellar passphrase belongs to, for the message. */
|
|
79
|
+
function stellarNetworkOf(passphrase) {
|
|
80
|
+
for (const [network, known] of STELLAR_PASSPHRASES)
|
|
81
|
+
if (known === passphrase)
|
|
82
|
+
return network;
|
|
83
|
+
return `a network with the passphrase "${sanitizeText(passphrase).slice(0, 80)}"`;
|
|
84
|
+
}
|
|
85
|
+
/** The method to ask, and how to read the answer into the CAIP-2 id it shows (or `undefined` when unreadable). */
|
|
86
|
+
function ruleFor(network) {
|
|
87
|
+
const [namespace, reference] = network.split(":", 2);
|
|
88
|
+
if (reference === undefined || reference === "")
|
|
89
|
+
return undefined;
|
|
90
|
+
if (namespace === "stellar" && STELLAR_PASSPHRASES.has(network)) {
|
|
91
|
+
return {
|
|
92
|
+
method: "getNetwork",
|
|
93
|
+
served: (result) => {
|
|
94
|
+
const passphrase = result?.passphrase;
|
|
95
|
+
return typeof passphrase === "string" ? stellarNetworkOf(passphrase) : undefined;
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
if (namespace === "eip155" && /^[1-9][0-9]*$/.test(reference)) {
|
|
100
|
+
return {
|
|
101
|
+
method: "eth_chainId",
|
|
102
|
+
served: (result) => {
|
|
103
|
+
if (typeof result !== "string" || !/^0x[0-9a-fA-F]{1,16}$/.test(result))
|
|
104
|
+
return undefined;
|
|
105
|
+
return `eip155:${BigInt(result).toString()}`;
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
if (namespace === "solana" && /^[1-9A-HJ-NP-Za-km-z]{32}$/.test(reference)) {
|
|
110
|
+
return {
|
|
111
|
+
method: "getGenesisHash",
|
|
112
|
+
served: (result) => {
|
|
113
|
+
if (typeof result !== "string" || !/^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(result))
|
|
114
|
+
return undefined;
|
|
115
|
+
return `solana:${result.slice(0, 32)}`;
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Refuses unless the RPC at `rpcUrl` says it serves `network`.
|
|
123
|
+
*
|
|
124
|
+
* @throws {RpcIdentityError} The RPC serves another network, did not answer, or
|
|
125
|
+
* answered something that does not name a network; or Wasit has no rule to
|
|
126
|
+
* tell which network an RPC for `network` serves. Nothing was sent to the
|
|
127
|
+
* target and nothing was signed.
|
|
128
|
+
*/
|
|
129
|
+
export async function assertRpcServesNetwork(network, rpcUrl) {
|
|
130
|
+
const label = sanitizeText(network);
|
|
131
|
+
const at = sanitizeText(origin(rpcUrl));
|
|
132
|
+
const rule = ruleFor(network);
|
|
133
|
+
if (rule === undefined) {
|
|
134
|
+
throw new RpcIdentityError(`Wasit cannot tell which network an RPC for ${label} serves, so it does not pay or read ` +
|
|
135
|
+
`settlements through ${at}; nothing was signed.`);
|
|
136
|
+
}
|
|
137
|
+
let result;
|
|
138
|
+
try {
|
|
139
|
+
result = await transport(rpcUrl, rule.method);
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
const reason = sanitizeText(error instanceof Error ? error.message : String(error)).slice(0, 160);
|
|
143
|
+
throw new RpcIdentityError(`The RPC at ${at} did not say which network it serves (${rule.method}: ${reason}), so Wasit ` +
|
|
144
|
+
`could not confirm it is ${label}. No payment was sent and nothing was signed.`);
|
|
145
|
+
}
|
|
146
|
+
const served = rule.served(result);
|
|
147
|
+
if (served === undefined) {
|
|
148
|
+
throw new RpcIdentityError(`The RPC at ${at} answered ${rule.method} with something that does not name a network, so ` +
|
|
149
|
+
`Wasit could not confirm it is ${label}. No payment was sent and nothing was signed.`);
|
|
150
|
+
}
|
|
151
|
+
if (served !== network) {
|
|
152
|
+
throw new RpcIdentityError(`The RPC at ${at} serves ${served}, and this run is on ${label}. Wasit pays and reads ` +
|
|
153
|
+
`settlements only through an RPC of the run's own network, so no payment was sent and ` +
|
|
154
|
+
`nothing was signed. Point the run at an RPC for ${label}.`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which RPC a run reads the chain through, as a result names it (D-023c,
|
|
3
|
+
* option C), and the start-up setting that fixes it for the MCP server.
|
|
4
|
+
*
|
|
5
|
+
* The RPC identity check (rpc-identity.ts) refuses an RPC of another network.
|
|
6
|
+
* What it cannot see is an RPC of the right network that lies about a
|
|
7
|
+
* settlement: that is a false PASS, not a payment on another chain. So a result
|
|
8
|
+
* says which RPC it read through, by origin only (a key often rides in the path
|
|
9
|
+
* or the query), where that RPC came from, and when it is not Wasit's default
|
|
10
|
+
* for the network: then every chain read of the run is that RPC's word.
|
|
11
|
+
*
|
|
12
|
+
* `WASIT_RPC_URL`, read once when the MCP server starts, fixes the RPC of every
|
|
13
|
+
* tool call; the server then refuses the `rpcUrl` argument, so an agent cannot
|
|
14
|
+
* point a run at another RPC.
|
|
15
|
+
*/
|
|
16
|
+
/** Where the RPC of a run came from. */
|
|
17
|
+
export type RpcSource = "default" | "argument" | "start-up";
|
|
18
|
+
export interface RpcInUse {
|
|
19
|
+
/** The origin only: scheme, host and port. */
|
|
20
|
+
readonly origin: string;
|
|
21
|
+
readonly source: RpcSource;
|
|
22
|
+
/** True when it is Wasit's default RPC for the network. */
|
|
23
|
+
readonly isDefault: boolean;
|
|
24
|
+
}
|
|
25
|
+
/** The variable that fixes the MCP server's RPC at start-up. */
|
|
26
|
+
export declare const RPC_LOCK_VARIABLE = "WASIT_RPC_URL";
|
|
27
|
+
/**
|
|
28
|
+
* The RPC a run on `network` reads through: `given` when there is one, else
|
|
29
|
+
* Wasit's default for the network. Undefined on a network that is not a test
|
|
30
|
+
* network (nothing is read there: the guard refuses first), and when there is
|
|
31
|
+
* neither.
|
|
32
|
+
*/
|
|
33
|
+
export declare function rpcInUse(protocol: "x402" | "mpp", network: string, given?: {
|
|
34
|
+
readonly url: string;
|
|
35
|
+
readonly source: "argument" | "start-up";
|
|
36
|
+
}): RpcInUse | undefined;
|
|
37
|
+
/** One line for a result: which RPC, from where, and a flag when it is not the default. */
|
|
38
|
+
export declare function describeRpcInUse(network: string, rpc: RpcInUse): string;
|
|
39
|
+
/**
|
|
40
|
+
* The MCP server's RPC lock: `WASIT_RPC_URL` when it holds a value. Off when it
|
|
41
|
+
* is unset or blank. A value that is not an http(s) URL is a problem the server
|
|
42
|
+
* refuses to start on; the message never quotes the value.
|
|
43
|
+
*/
|
|
44
|
+
export declare function readRpcLock(env: Readonly<Record<string, string | undefined>>): {
|
|
45
|
+
readonly url: string;
|
|
46
|
+
} | {
|
|
47
|
+
readonly problem: string;
|
|
48
|
+
} | undefined;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which RPC a run reads the chain through, as a result names it (D-023c,
|
|
3
|
+
* option C), and the start-up setting that fixes it for the MCP server.
|
|
4
|
+
*
|
|
5
|
+
* The RPC identity check (rpc-identity.ts) refuses an RPC of another network.
|
|
6
|
+
* What it cannot see is an RPC of the right network that lies about a
|
|
7
|
+
* settlement: that is a false PASS, not a payment on another chain. So a result
|
|
8
|
+
* says which RPC it read through, by origin only (a key often rides in the path
|
|
9
|
+
* or the query), where that RPC came from, and when it is not Wasit's default
|
|
10
|
+
* for the network: then every chain read of the run is that RPC's word.
|
|
11
|
+
*
|
|
12
|
+
* `WASIT_RPC_URL`, read once when the MCP server starts, fixes the RPC of every
|
|
13
|
+
* tool call; the server then refuses the `rpcUrl` argument, so an agent cannot
|
|
14
|
+
* point a run at another RPC.
|
|
15
|
+
*/
|
|
16
|
+
import { urlOrigin } from "../config.js";
|
|
17
|
+
import { resolveRpcUrl as resolveMppRpcUrl } from "../mpp/network.js";
|
|
18
|
+
import { paymentChainFor } from "../x402/chains/index.js";
|
|
19
|
+
import { networkClass } from "./networks.js";
|
|
20
|
+
/** The variable that fixes the MCP server's RPC at start-up. */
|
|
21
|
+
export const RPC_LOCK_VARIABLE = "WASIT_RPC_URL";
|
|
22
|
+
function defaultRpc(protocol, network) {
|
|
23
|
+
try {
|
|
24
|
+
return protocol === "x402" ? paymentChainFor(network)?.resolveRpcUrl(network) : resolveMppRpcUrl(network);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function sameUrl(a, b) {
|
|
31
|
+
try {
|
|
32
|
+
return new URL(a).href === new URL(b).href;
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The RPC a run on `network` reads through: `given` when there is one, else
|
|
40
|
+
* Wasit's default for the network. Undefined on a network that is not a test
|
|
41
|
+
* network (nothing is read there: the guard refuses first), and when there is
|
|
42
|
+
* neither.
|
|
43
|
+
*/
|
|
44
|
+
export function rpcInUse(protocol, network, given) {
|
|
45
|
+
if (networkClass(network) !== "test")
|
|
46
|
+
return undefined;
|
|
47
|
+
const fallback = defaultRpc(protocol, network);
|
|
48
|
+
if (fallback === undefined)
|
|
49
|
+
return undefined;
|
|
50
|
+
if (given === undefined)
|
|
51
|
+
return { origin: urlOrigin(fallback), source: "default", isDefault: true };
|
|
52
|
+
return { origin: urlOrigin(given.url), source: given.source, isDefault: sameUrl(given.url, fallback) };
|
|
53
|
+
}
|
|
54
|
+
/** One line for a result: which RPC, from where, and a flag when it is not the default. */
|
|
55
|
+
export function describeRpcInUse(network, rpc) {
|
|
56
|
+
if (rpc.source === "default")
|
|
57
|
+
return `RPC: ${rpc.origin}, Wasit's default for ${network}.`;
|
|
58
|
+
const from = rpc.source === "argument" ? "from the rpcUrl argument" : `fixed when this server started (${RPC_LOCK_VARIABLE})`;
|
|
59
|
+
return rpc.isDefault
|
|
60
|
+
? `RPC: ${rpc.origin}, ${from} (Wasit's default for ${network}).`
|
|
61
|
+
: `RPC: ${rpc.origin}, ${from}: not Wasit's default for ${network}, so every chain read of this run is this RPC's word.`;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The MCP server's RPC lock: `WASIT_RPC_URL` when it holds a value. Off when it
|
|
65
|
+
* is unset or blank. A value that is not an http(s) URL is a problem the server
|
|
66
|
+
* refuses to start on; the message never quotes the value.
|
|
67
|
+
*/
|
|
68
|
+
export function readRpcLock(env) {
|
|
69
|
+
const value = env[RPC_LOCK_VARIABLE];
|
|
70
|
+
if (value === undefined || value.trim() === "")
|
|
71
|
+
return undefined;
|
|
72
|
+
let url;
|
|
73
|
+
try {
|
|
74
|
+
url = new URL(value.trim());
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return { problem: `${RPC_LOCK_VARIABLE} is not a valid absolute URL; include the scheme, e.g. https://rpc.example.com` };
|
|
78
|
+
}
|
|
79
|
+
if (url.protocol !== "http:" && url.protocol !== "https:")
|
|
80
|
+
return { problem: `${RPC_LOCK_VARIABLE} must be an http or https URL` };
|
|
81
|
+
if (url.hostname === "")
|
|
82
|
+
return { problem: `${RPC_LOCK_VARIABLE} has no host` };
|
|
83
|
+
return { url: value.trim() };
|
|
84
|
+
}
|