@wasit-dev/core 0.8.0-beta.1 → 0.8.0-beta.3
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 +3 -1
- package/dist/baseline.js +23 -9
- package/dist/check.d.ts +1 -1
- package/dist/ci-report.js +20 -8
- package/dist/config-writer.d.ts +96 -0
- package/dist/config-writer.js +281 -0
- package/dist/guard/budget.d.ts +7 -4
- package/dist/guard/budget.js +11 -4
- 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-optin.d.ts +3 -1
- package/dist/guard/mainnet-optin.js +8 -2
- 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 +135 -0
- package/dist/guard/signed-terms.js +252 -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.d.ts +39 -4
- package/dist/x402/chains/stellar.js +224 -19
- package/dist/x402/chains/svm.js +6 -2
- package/dist/x402/chains/types.d.ts +44 -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 +461 -50
- package/package.json +9 -9
package/dist/guard/networks.d.ts
CHANGED
|
@@ -8,11 +8,12 @@
|
|
|
8
8
|
* third party using @wasit-dev/core get the same refusals; a guard in each
|
|
9
9
|
* front end would be three copies, and one forgotten copy is a hole.
|
|
10
10
|
*
|
|
11
|
-
* `stellar:pubnet` has its own class
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* `stellar:pubnet` has its own class: an x402 run pays on it only with a live
|
|
12
|
+
* mainnet opt-in (spec B5), which only core makes (`openMainnetOptIn`). MPP
|
|
13
|
+
* never pays on it.
|
|
14
14
|
*/
|
|
15
15
|
import { ConfigurationError } from "../errors.js";
|
|
16
|
+
import { type MainnetOptIn } from "./optin.js";
|
|
16
17
|
/** How the guard treats a network. */
|
|
17
18
|
export type NetworkClass = "test" | "mainnet-optin" | "unknown";
|
|
18
19
|
/** The protocol that would sign. MPP has no mainnet path at all. */
|
|
@@ -41,10 +42,10 @@ export declare class PaymentNetworkRefusedError extends ConfigurationError {
|
|
|
41
42
|
* Throws unless a payment by `protocol` may be made on `network`.
|
|
42
43
|
*
|
|
43
44
|
* Call it before any request is sent and before any key or signer is used.
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
45
|
+
* Test networks pass. `stellar:pubnet` passes for x402 only with `mainnet`, a
|
|
46
|
+
* live opt-in for it (made by core, neither stopped nor closed); MPP stays on
|
|
47
|
+
* test networks for good. Anything else is refused.
|
|
47
48
|
*
|
|
48
|
-
* @throws {PaymentNetworkRefusedError} For any network that is not a test network.
|
|
49
|
+
* @throws {PaymentNetworkRefusedError} For any network that is not a test network, but x402 on a live opt-in's.
|
|
49
50
|
*/
|
|
50
|
-
export declare function assertPaymentNetworkAllowed(protocol: PaymentProtocol, network: string): void;
|
|
51
|
+
export declare function assertPaymentNetworkAllowed(protocol: PaymentProtocol, network: string, mainnet?: MainnetOptIn): void;
|
package/dist/guard/networks.js
CHANGED
|
@@ -8,11 +8,12 @@
|
|
|
8
8
|
* third party using @wasit-dev/core get the same refusals; a guard in each
|
|
9
9
|
* front end would be three copies, and one forgotten copy is a hole.
|
|
10
10
|
*
|
|
11
|
-
* `stellar:pubnet` has its own class
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* `stellar:pubnet` has its own class: an x402 run pays on it only with a live
|
|
12
|
+
* mainnet opt-in (spec B5), which only core makes (`openMainnetOptIn`). MPP
|
|
13
|
+
* never pays on it.
|
|
14
14
|
*/
|
|
15
15
|
import { ConfigurationError } from "../errors.js";
|
|
16
|
+
import { isLiveOptIn, whyNotLive } from "./optin.js";
|
|
16
17
|
/**
|
|
17
18
|
* Every network the guard knows. Test networks are the four the payment
|
|
18
19
|
* checks pay on; `stellar:pubnet` is the only mainnet listed. Anything absent
|
|
@@ -58,20 +59,30 @@ export class PaymentNetworkRefusedError extends ConfigurationError {
|
|
|
58
59
|
* Throws unless a payment by `protocol` may be made on `network`.
|
|
59
60
|
*
|
|
60
61
|
* Call it before any request is sent and before any key or signer is used.
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
62
|
+
* Test networks pass. `stellar:pubnet` passes for x402 only with `mainnet`, a
|
|
63
|
+
* live opt-in for it (made by core, neither stopped nor closed); MPP stays on
|
|
64
|
+
* test networks for good. Anything else is refused.
|
|
64
65
|
*
|
|
65
|
-
* @throws {PaymentNetworkRefusedError} For any network that is not a test network.
|
|
66
|
+
* @throws {PaymentNetworkRefusedError} For any network that is not a test network, but x402 on a live opt-in's.
|
|
66
67
|
*/
|
|
67
|
-
export function assertPaymentNetworkAllowed(protocol, network) {
|
|
68
|
+
export function assertPaymentNetworkAllowed(protocol, network, mainnet) {
|
|
68
69
|
const kind = networkClass(network);
|
|
69
70
|
if (kind === "test")
|
|
70
71
|
return;
|
|
72
|
+
if (kind === "mainnet-optin" && protocol === "x402") {
|
|
73
|
+
if (isLiveOptIn(mainnet, network))
|
|
74
|
+
return;
|
|
75
|
+
// An opt-in core made, that is no longer live: say why, not that there is none.
|
|
76
|
+
const why = whyNotLive(mainnet, network);
|
|
77
|
+
if (why !== undefined)
|
|
78
|
+
throw new PaymentNetworkRefusedError(protocol, network, kind, `${network} is Stellar mainnet, and ${why}.`);
|
|
79
|
+
}
|
|
71
80
|
if (kind === "mainnet-optin") {
|
|
72
81
|
throw new PaymentNetworkRefusedError(protocol, network, kind, protocol === "x402"
|
|
73
|
-
? `${network} is Stellar mainnet
|
|
74
|
-
`
|
|
82
|
+
? `${network} is Stellar mainnet, and this run has no mainnet opt-in: Wasit pays there only through ` +
|
|
83
|
+
`wasit run --mainnet, the Action's mainnet: true on workflow_dispatch, or the MCP server's ` +
|
|
84
|
+
`wasit_x402_mainnet_test, each with every rule of the opt-in. Without it Wasit pays on test ` +
|
|
85
|
+
`networks only (${testNetworks()}).`
|
|
75
86
|
: `${network} is Stellar mainnet, and MPP stays on test networks: the MPP ` +
|
|
76
87
|
`checks pay on stellar:testnet only.`);
|
|
77
88
|
}
|
|
@@ -0,0 +1,106 @@
|
|
|
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 { type PinTerms, type RunBudget } from "./budget.js";
|
|
22
|
+
import { RunLedger } from "./run-ledger.js";
|
|
23
|
+
import type { MainnetRunLock } from "./run-lock.js";
|
|
24
|
+
import { SigningAnchor } from "./signed-terms.js";
|
|
25
|
+
import type { RunPlan } from "../x402/plan.js";
|
|
26
|
+
/** The front end whose switch asked for this run: `--mainnet`, the Action's input, the MCP server's start-up setting. */
|
|
27
|
+
export type MainnetFrontEnd = "cli" | "action" | "mcp";
|
|
28
|
+
export interface MainnetOptInFields {
|
|
29
|
+
readonly frontEnd: MainnetFrontEnd;
|
|
30
|
+
readonly environment: string;
|
|
31
|
+
readonly payer: string;
|
|
32
|
+
readonly pin: PinTerms;
|
|
33
|
+
readonly rpcUrl: string;
|
|
34
|
+
readonly budget: RunBudget;
|
|
35
|
+
readonly ledgerDir: string;
|
|
36
|
+
readonly lock: MainnetRunLock;
|
|
37
|
+
}
|
|
38
|
+
/** Which rule refused or stopped the mainnet run. */
|
|
39
|
+
export type MainnetRunRefusal = "network" | "operated-by-me" | "key" | "rpc" | "cap" | "job" | "run";
|
|
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 declare class MainnetRunRefusedError extends ConfigurationError {
|
|
48
|
+
readonly reason: MainnetRunRefusal;
|
|
49
|
+
constructor(reason: MainnetRunRefusal, message: string, options?: {
|
|
50
|
+
readonly during?: boolean;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
export declare class MainnetOptIn {
|
|
54
|
+
#private;
|
|
55
|
+
readonly network = "stellar:pubnet";
|
|
56
|
+
readonly frontEnd: MainnetFrontEnd;
|
|
57
|
+
readonly environment: string;
|
|
58
|
+
/** The payer's public address, derived from the key when the opt-in was made. */
|
|
59
|
+
readonly payer: string;
|
|
60
|
+
readonly pin: PinTerms;
|
|
61
|
+
readonly rpcUrl: string;
|
|
62
|
+
/** The run's budget: its own $5, or the MCP server's for the whole process (D-018). */
|
|
63
|
+
readonly budget: RunBudget;
|
|
64
|
+
/** Where the lock is, and where the ledger is written. */
|
|
65
|
+
readonly ledgerDir: string;
|
|
66
|
+
/** Core only: `openMainnetOptIn`. */
|
|
67
|
+
constructor(key: symbol, fields: MainnetOptInFields);
|
|
68
|
+
/** The plan L1 made for this run, once it passed. */
|
|
69
|
+
get plan(): RunPlan | undefined;
|
|
70
|
+
get ledger(): RunLedger | undefined;
|
|
71
|
+
/** Why the run stopped (the first reason), if it did. Nothing is signed after it. */
|
|
72
|
+
get stopped(): string | undefined;
|
|
73
|
+
get closed(): boolean;
|
|
74
|
+
/** Stops the whole run (spec B3): nothing more is signed with this opt-in. The first reason is kept. */
|
|
75
|
+
stop(reason: string): void;
|
|
76
|
+
/**
|
|
77
|
+
* Opens the run ledger (spec B5) in the ledger folder: the header from this
|
|
78
|
+
* opt-in and its plan, the RPC by its origin only, and the balances before
|
|
79
|
+
* as L1 read them on public Horizon. Only after a plan that passed, once,
|
|
80
|
+
* and before the first signature: a payment on mainnet without it is refused.
|
|
81
|
+
*/
|
|
82
|
+
openLedger(versions: Readonly<Record<string, string>>, options?: {
|
|
83
|
+
readonly now?: () => Date;
|
|
84
|
+
}): RunLedger;
|
|
85
|
+
/** Ends the run: nothing more is signed with it, and its lock is released. */
|
|
86
|
+
close(): void;
|
|
87
|
+
}
|
|
88
|
+
/** Core only: L1 records the plan that passed, once. Not a method, so no caller reaches it through an opt-in. */
|
|
89
|
+
export declare function recordMainnetPlan(optIn: MainnetOptIn, plan: RunPlan): void;
|
|
90
|
+
/** Core only: the one way to make an opt-in, used by `openMainnetOptIn`. */
|
|
91
|
+
export declare function issueMainnetOptIn(fields: MainnetOptInFields): MainnetOptIn;
|
|
92
|
+
/**
|
|
93
|
+
* The run's anchor (spec B17.2): read in L1 beside public Horizon's latest
|
|
94
|
+
* ledger, never at the first signature. Core only: not on the opt-in, so no
|
|
95
|
+
* caller can set it first.
|
|
96
|
+
*/
|
|
97
|
+
export declare function anchorOfOptIn(optIn: MainnetOptIn): SigningAnchor;
|
|
98
|
+
/** True for an opt-in core made, live, stopped or closed alike. */
|
|
99
|
+
export declare function isMainnetOptIn(value: unknown): value is MainnetOptIn;
|
|
100
|
+
/** True only for an opt-in core made, for `network`, neither stopped nor closed. */
|
|
101
|
+
export declare function isLiveOptIn(value: unknown, network: string): value is MainnetOptIn;
|
|
102
|
+
/**
|
|
103
|
+
* Why `value`, an opt-in core made, is not live for `network`, as a clause;
|
|
104
|
+
* undefined when it is live or is not an opt-in at all.
|
|
105
|
+
*/
|
|
106
|
+
export declare function whyNotLive(value: unknown, network: string): string | undefined;
|
|
@@ -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,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The run ledger (spec B5, safety rule 6): a file a paying run keeps beside
|
|
3
|
+
* it, so that a person can compare what Wasit signed with what the chain
|
|
4
|
+
* shows, also after a crash (B10 T13).
|
|
5
|
+
*
|
|
6
|
+
* One file per run, `wasit-ledger-<UTC time>.json`, created new and never
|
|
7
|
+
* overwritten. Every change rewrites it whole: to a temporary file, flushed to
|
|
8
|
+
* disk, then renamed over the ledger, so the file is valid JSON at every
|
|
9
|
+
* moment a process can be killed.
|
|
10
|
+
*
|
|
11
|
+
* One entry per signature (B-A03). Its intent (check, endpoint, amount,
|
|
12
|
+
* payee, asset) is written and flushed after the run is charged and BEFORE
|
|
13
|
+
* the key is asked to sign: a ledger that cannot take it stops the payment,
|
|
14
|
+
* with nothing signed. Then what became of it: signed, with the ledger its
|
|
15
|
+
* authorization expires at (decoded from what the key signed); not signed;
|
|
16
|
+
* or unknown (the client failed after it may have signed). Then discarded
|
|
17
|
+
* unsent, or sent with the answer's status, and the settlement if any. An
|
|
18
|
+
* entry with no outcome reads as "assume signed and sent".
|
|
19
|
+
*
|
|
20
|
+
* Each entry also takes the verdict of the check it belongs to, once that
|
|
21
|
+
* check has one. Balances before and after the run (USDC and XLM) close it,
|
|
22
|
+
* with whether they are consistent with what the ledger says settled:
|
|
23
|
+
* consistent, explained (the payer paid more than the settlements the run
|
|
24
|
+
* verified, but no more than the payments of checks that FAILed, which a
|
|
25
|
+
* service that takes what it should refuse may have settled), or
|
|
26
|
+
* inconsistent.
|
|
27
|
+
*
|
|
28
|
+
* No secret: the payer is named by its public address, the RPC by its origin,
|
|
29
|
+
* and free text is cut, with anything shaped like a Stellar secret or a 64-hex
|
|
30
|
+
* value in it masked. Works on every network; on mainnet it is not optional
|
|
31
|
+
* (B5).
|
|
32
|
+
*/
|
|
33
|
+
import { CheckSetupError } from "../errors.js";
|
|
34
|
+
import type { PinTerms } from "./budget.js";
|
|
35
|
+
/** The `format` field of every run ledger. */
|
|
36
|
+
export declare const RUN_LEDGER_FORMAT = "wasit-run-ledger";
|
|
37
|
+
/** `wasit-ledger-<UTC time>.json`, colons replaced: some file systems refuse them. */
|
|
38
|
+
export declare function runLedgerFileName(at: Date): string;
|
|
39
|
+
/** The run ledger cannot be written, or was asked something out of order. A setup error: no verdict. */
|
|
40
|
+
export declare class RunLedgerError extends CheckSetupError {
|
|
41
|
+
constructor(message: string);
|
|
42
|
+
}
|
|
43
|
+
/** What a run states about itself when it opens its ledger. */
|
|
44
|
+
export interface RunLedgerHeader {
|
|
45
|
+
readonly versions: Readonly<Record<string, string>>;
|
|
46
|
+
readonly environment: string;
|
|
47
|
+
readonly network: string;
|
|
48
|
+
/** The payer's public address. */
|
|
49
|
+
readonly payer: string;
|
|
50
|
+
readonly pin?: PinTerms;
|
|
51
|
+
readonly caps: {
|
|
52
|
+
readonly perPaymentUsd: string;
|
|
53
|
+
readonly runUsd: string;
|
|
54
|
+
};
|
|
55
|
+
/** The run's plan; bigints are written as strings. */
|
|
56
|
+
readonly plan: unknown;
|
|
57
|
+
/** The RPC's origin, never its path or query (they may hold a key). */
|
|
58
|
+
readonly rpc: string;
|
|
59
|
+
}
|
|
60
|
+
/** A reading of the payer's balances, in base units. */
|
|
61
|
+
export interface LedgerBalances {
|
|
62
|
+
readonly usdc: bigint;
|
|
63
|
+
readonly native: bigint;
|
|
64
|
+
}
|
|
65
|
+
export type LedgerEntryState =
|
|
66
|
+
/** Written before the signature; no outcome yet. Read as signed. */
|
|
67
|
+
"intent" | "signed"
|
|
68
|
+
/** The key was not asked, or refused: nothing was signed. */
|
|
69
|
+
| "not-signed"
|
|
70
|
+
/** The client failed when it may already have signed. Read as signed. */
|
|
71
|
+
| "unknown";
|
|
72
|
+
/** What the check a signature belongs to found, as the ledger records it. */
|
|
73
|
+
export type LedgerVerdict = "pass" | "fail" | "no-verdict" | "skipped";
|
|
74
|
+
/** One signature of the run, as the file holds it. */
|
|
75
|
+
export interface LedgerEntry {
|
|
76
|
+
readonly id: number;
|
|
77
|
+
readonly endpoint: string;
|
|
78
|
+
readonly check: string;
|
|
79
|
+
readonly network: string;
|
|
80
|
+
readonly asset: string;
|
|
81
|
+
readonly payTo: string;
|
|
82
|
+
/** Base units. */
|
|
83
|
+
readonly amount: string;
|
|
84
|
+
readonly intentAt: string;
|
|
85
|
+
readonly state: LedgerEntryState;
|
|
86
|
+
readonly signedAt?: string;
|
|
87
|
+
/** The ledger (the chain's unit) the authorization expires at; null when it could not be read. */
|
|
88
|
+
readonly expiresAtLedger?: number | null;
|
|
89
|
+
/** Why it was not signed, or is unknown. */
|
|
90
|
+
readonly note?: string;
|
|
91
|
+
/** Why a signed payment was discarded, unsent. */
|
|
92
|
+
readonly discarded?: string;
|
|
93
|
+
readonly sent?: {
|
|
94
|
+
readonly at: string;
|
|
95
|
+
readonly status?: number;
|
|
96
|
+
readonly error?: string;
|
|
97
|
+
};
|
|
98
|
+
readonly settlement?: {
|
|
99
|
+
readonly reference: string;
|
|
100
|
+
readonly verified: boolean;
|
|
101
|
+
};
|
|
102
|
+
/** The verdict of the check this signature belongs to; absent until that check has one. */
|
|
103
|
+
readonly verdict?: LedgerVerdict;
|
|
104
|
+
}
|
|
105
|
+
/** What a payment check records about one signature. */
|
|
106
|
+
export interface LedgerEntryHandle {
|
|
107
|
+
/** The key signed; `expiresAtLedger` undefined when it could not be read. */
|
|
108
|
+
signed(expiresAtLedger: number | undefined): void;
|
|
109
|
+
notSigned(reason: string): void;
|
|
110
|
+
unknown(reason: string): void;
|
|
111
|
+
/** A signed payment was discarded, unsent. */
|
|
112
|
+
discarded(reason: string): void;
|
|
113
|
+
sent(outcome: {
|
|
114
|
+
readonly status: number;
|
|
115
|
+
} | {
|
|
116
|
+
readonly error: string;
|
|
117
|
+
}): void;
|
|
118
|
+
settled(reference: string, verified: boolean): void;
|
|
119
|
+
}
|
|
120
|
+
/** The ledger as the payment checks of one endpoint see it. */
|
|
121
|
+
export interface PaymentLedger {
|
|
122
|
+
/**
|
|
123
|
+
* Writes, and flushes, the intent of a signature about to be made. Throws
|
|
124
|
+
* when it cannot: the caller then signs nothing.
|
|
125
|
+
*/
|
|
126
|
+
intent(entry: {
|
|
127
|
+
readonly check: string;
|
|
128
|
+
readonly network: string;
|
|
129
|
+
readonly asset: string;
|
|
130
|
+
readonly payTo: string;
|
|
131
|
+
readonly amount: string;
|
|
132
|
+
}): LedgerEntryHandle;
|
|
133
|
+
/**
|
|
134
|
+
* Records `check`'s verdict on every entry of it that has none yet. Only a
|
|
135
|
+
* FAIL recorded here can explain USDC the run did not verify settled; a
|
|
136
|
+
* ledger without this method, or one that cannot write it, explains none.
|
|
137
|
+
*/
|
|
138
|
+
verdict?(check: string, verdict: LedgerVerdict): void;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* How the payer's balances compare with the ledger. `consistent`: its USDC
|
|
142
|
+
* went down by exactly the settlements the run verified, and its XLM did not
|
|
143
|
+
* move. `explained`: its XLM did not move, and its USDC went down by more
|
|
144
|
+
* than the verified settlements, but by no more than the payments, signed and
|
|
145
|
+
* sent, of checks that FAILed (each counted once): a service that settles
|
|
146
|
+
* what it should refuse. Anything else is `inconsistent`.
|
|
147
|
+
*/
|
|
148
|
+
export type LedgerResult = "consistent" | "explained" | "inconsistent";
|
|
149
|
+
/** Whether the payer's balances moved as the ledger says they should have. */
|
|
150
|
+
export interface LedgerConsistency {
|
|
151
|
+
readonly result: LedgerResult;
|
|
152
|
+
/** True only when `result` is "consistent". */
|
|
153
|
+
readonly consistent: boolean;
|
|
154
|
+
readonly reasons: readonly string[];
|
|
155
|
+
/** USDC that left the payer, in base units, when both readings exist. */
|
|
156
|
+
readonly spentUsdc?: bigint;
|
|
157
|
+
/** USDC of the settlements the run verified on chain, in base units. */
|
|
158
|
+
readonly settledUsdc: bigint;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The ledger of one paying run. Create it before the first signature; hand
|
|
162
|
+
* {@link RunLedger.forEndpoint} to each endpoint's payment checks; close it
|
|
163
|
+
* once every authorization has expired and the balances after are read.
|
|
164
|
+
*/
|
|
165
|
+
export declare class RunLedger {
|
|
166
|
+
#private;
|
|
167
|
+
readonly path: string;
|
|
168
|
+
private constructor();
|
|
169
|
+
/**
|
|
170
|
+
* Creates the ledger in `dir` (made if missing), named after now, holding the
|
|
171
|
+
* header. Refuses a name that exists: a ledger is never overwritten.
|
|
172
|
+
*/
|
|
173
|
+
static create(dir: string, header: RunLedgerHeader, options?: {
|
|
174
|
+
readonly now?: () => Date;
|
|
175
|
+
}): RunLedger;
|
|
176
|
+
/** The entries so far, as the file holds them. */
|
|
177
|
+
get entries(): readonly LedgerEntry[];
|
|
178
|
+
/** The payment ledger of one endpoint's checks. */
|
|
179
|
+
forEndpoint(endpoint: string): PaymentLedger;
|
|
180
|
+
/** The payer's balances before the first signature, and where they were read. */
|
|
181
|
+
recordBalancesBefore(balances: LedgerBalances, source: string): void;
|
|
182
|
+
/**
|
|
183
|
+
* Closes the ledger with the balances after the run (undefined when they
|
|
184
|
+
* could not be read): writes whether they are consistent with what the
|
|
185
|
+
* ledger says settled, and returns it.
|
|
186
|
+
*/
|
|
187
|
+
close(input?: {
|
|
188
|
+
readonly after?: LedgerBalances;
|
|
189
|
+
readonly source?: string;
|
|
190
|
+
}): LedgerConsistency;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Whether the payer's balances moved as the ledger says (see
|
|
194
|
+
* {@link LedgerResult}). On Stellar the facilitator pays the fees and Wasit
|
|
195
|
+
* sends no transaction from the payer, so its XLM must not move. The USDC
|
|
196
|
+
* the run verified settled is the sum of the entries whose settlement it read
|
|
197
|
+
* back from the chain. More than that is explained only by the entries, sent
|
|
198
|
+
* and not verified, of checks that FAILed: a check with no verdict, a SKIP or
|
|
199
|
+
* a PASS explains nothing, and neither does a payment discarded unsent. The
|
|
200
|
+
* reasons also name every entry with no recorded outcome, which is read as
|
|
201
|
+
* signed and sent.
|
|
202
|
+
*/
|
|
203
|
+
export declare function ledgerConsistency(before: LedgerBalances | undefined, after: LedgerBalances | undefined, entries: readonly LedgerEntry[]): LedgerConsistency;
|
|
204
|
+
/**
|
|
205
|
+
* Until when the run's authorizations may be open (L4): the latest expiry of
|
|
206
|
+
* every entry that holds a signature or may, and how many of those have no
|
|
207
|
+
* known expiry (the caller then waits the longest a signature may live).
|
|
208
|
+
*/
|
|
209
|
+
export declare function openAuthorizationsUntil(entries: readonly LedgerEntry[]): {
|
|
210
|
+
latest: number | undefined;
|
|
211
|
+
unknown: number;
|
|
212
|
+
};
|