@dvmkit/dvm-cli 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{_esm-M2HUAA3B.js → _esm-MN4BUYV6.js} +56 -54
- package/dist/{accounts-S747UW3A.js → accounts-2WZE4N5F.js} +5 -5
- package/dist/actions-LR65XILR.js +244 -0
- package/dist/{ccip-XZ5F5J6R.js → ccip-JXYJSXTW.js} +3 -3
- package/dist/{chains-PINFXYUE.js → chains-LJ7XK6H3.js} +8 -8
- package/dist/{chunk-YZUBKHHA.js → chunk-2VVVWBQA.js} +471 -471
- package/dist/chunk-3SWX5BQP.js +66 -0
- package/dist/chunk-3W2M46WM.js +61142 -0
- package/dist/chunk-4JXFXP4Y.js +757 -0
- package/dist/{chunk-GIDSTROG.js → chunk-4NWEJTAW.js} +160 -158
- package/dist/{chunk-UXQCP26E.js → chunk-65EY7CT4.js} +2 -2
- package/dist/{chunk-6UGZAFIR.js → chunk-6CC6JGEU.js} +348 -18
- package/dist/{chunk-PHZHPRQE.js → chunk-6O27TEXD.js} +173 -173
- package/dist/{chunk-HPRIQEJS.js → chunk-7OK7MBT6.js} +19 -19
- package/dist/{chunk-7UPZZGC5.js → chunk-BECPI5H5.js} +193 -193
- package/dist/chunk-CJTE5TS3.js +1298 -0
- package/dist/{chunk-RWBYCM47.js → chunk-ECEM3ZAS.js} +10 -10
- package/dist/{chunk-ACNNU5RY.js → chunk-ECK465AY.js} +694 -565
- package/dist/{chunk-XGABM4H5.js → chunk-EGY4KDBW.js} +3822 -2012
- package/dist/{chunk-SLDVIXCS.js → chunk-EVQERRCP.js} +12 -12
- package/dist/{chunk-4AQ4G7L5.js → chunk-EZHT2RLJ.js} +383 -383
- package/dist/{chunk-IC6OXFYW.js → chunk-F55NNH2F.js} +63 -63
- package/dist/chunk-G5C6E6PJ.js +9063 -0
- package/dist/{chunk-6AJ7H2VX.js → chunk-GZWKCIYN.js} +213 -1281
- package/dist/{chunk-ZGXCK43G.js → chunk-LH3QPVN6.js} +8499 -8498
- package/dist/{chunk-MMJR6N3J.js → chunk-MLDUKBU3.js} +1368 -1368
- package/dist/chunk-N3UJT2NJ.js +3 -0
- package/dist/{chunk-BZNZWRBB.js → chunk-OWJZYYMG.js} +11 -15
- package/dist/chunk-ZJYO7OLO.js +514 -0
- package/dist/{client-D3XFLWOK.js → client-OWIOFBKS.js} +18 -16
- package/dist/{dist-7MMV3EZA.js → dist-2G3QBGID.js} +11 -10
- package/dist/dvm.js +26781 -4423
- package/dist/esm-RQLHAH2K.js +5183 -0
- package/dist/internal-QFQ7MJ5V.js +1228 -0
- package/dist/payout-reporter-3UB5WRCV-XTVZFIAO.js +17 -0
- package/dist/{pocket-refill-GDYSTECR.js → pocket-refill-PQ5ZVSBY.js} +26 -30
- package/dist/revenue-reporter-JIKUPXOK-TYUBTF4P.js +11 -0
- package/dist/{secp256k1-4YWQEMGF.js → secp256k1-JLZEQ2H7.js} +2 -2
- package/dist/tempo-lifecycle-SQL3KLEZ-AATA2SKI.js +28 -0
- package/dist/{tempo-wallet-5FEOI4CF.js → tempo-wallet-QOLEIPCV-SWCXRBDS.js} +2 -4
- package/dist/{x402-VNFA3DBY.js → x402-FTG2GRAQ-UGAUMUL6.js} +13 -24
- package/package.json +2 -10
- package/dist/channel-store-GOV7TEDN.js +0 -62
- package/dist/chunk-3PPOU55E.js +0 -312
- package/dist/chunk-3TBULV3F.js +0 -551
- package/dist/chunk-5BUV73FO.js +0 -2351
- package/dist/chunk-EY5K4BM3.js +0 -1780
- package/dist/chunk-G3RLVJF5.js +0 -19
- package/dist/chunk-KC4UPK44.js +0 -113
- package/dist/chunk-MTBCEXJ6.js +0 -4602
- package/dist/chunk-ONQMRWY3.js +0 -83
- package/dist/chunk-TO47XRK7.js +0 -24890
- package/dist/chunk-U5TNSUYW.js +0 -1757
- package/dist/chunk-UU6QMSTQ.js +0 -171
- package/dist/chunk-ZELLQI7F.js +0 -248
- package/dist/lifecycle-5RLM5JMN.js +0 -29
- package/dist/tempo-lifecycle-DHZPQTSB.js +0 -31
- package/dist/wallet-G3VOUCL4.js +0 -30
package/dist/chunk-3TBULV3F.js
DELETED
|
@@ -1,551 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { createRequire as __dvmCreateRequire } from "node:module";
|
|
3
|
-
const require = __dvmCreateRequire(import.meta.url);
|
|
4
|
-
import {
|
|
5
|
-
fundingReachableAt,
|
|
6
|
-
listCredits
|
|
7
|
-
} from "./chunk-6UGZAFIR.js";
|
|
8
|
-
import {
|
|
9
|
-
DvmError,
|
|
10
|
-
X402_CHANNELS_FILE,
|
|
11
|
-
ensureConfigDir,
|
|
12
|
-
require_proper_lockfile
|
|
13
|
-
} from "./chunk-U5TNSUYW.js";
|
|
14
|
-
import {
|
|
15
|
-
__toESM
|
|
16
|
-
} from "./chunk-IYHTITNZ.js";
|
|
17
|
-
|
|
18
|
-
// src/lib/x402/channel-store.ts
|
|
19
|
-
var import_proper_lockfile = __toESM(require_proper_lockfile(), 1);
|
|
20
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
21
|
-
import { existsSync, readFileSync, renameSync, writeFileSync } from "fs";
|
|
22
|
-
|
|
23
|
-
// src/lib/payment-authorization-bounds.ts
|
|
24
|
-
var X402_EXACT_AUTHORIZATION_TTL_SECONDS = 600;
|
|
25
|
-
var X402_REFUSAL_TRANSACTION_DISCOVERY_WINDOW_MS = 15 * 6e4;
|
|
26
|
-
|
|
27
|
-
// src/lib/x402/channel-store.ts
|
|
28
|
-
function x402ExactFundingDead(pending, now = Date.now()) {
|
|
29
|
-
const seconds = pending.authorizationValidBefore ? Number(pending.authorizationValidBefore) : NaN;
|
|
30
|
-
return Number.isFinite(seconds) ? now >= seconds * 1e3 : now >= pending.requestedAt + X402_EXACT_AUTHORIZATION_TTL_SECONDS * 1e3;
|
|
31
|
-
}
|
|
32
|
-
function recordPendingX402ExactFunding(pending) {
|
|
33
|
-
const { file, credits } = loadFundingFile();
|
|
34
|
-
file.exact = [
|
|
35
|
-
...file.exact.filter((entry) => !sameFundingCredit(entry, pending, credits)),
|
|
36
|
-
pending
|
|
37
|
-
];
|
|
38
|
-
saveFile(file);
|
|
39
|
-
}
|
|
40
|
-
function loadResumableX402ExactFunding(args) {
|
|
41
|
-
const { file, credits } = loadFundingFile();
|
|
42
|
-
return file.exact.find(
|
|
43
|
-
(entry) => sameFundingCredit(entry, args, credits) && !x402ExactFundingDead(entry, args.now)
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
function listResumableX402ExactFundings(filter = {}) {
|
|
47
|
-
const endpoint = filter.endpoint ? normalizeEndpoint(filter.endpoint) : void 0;
|
|
48
|
-
const { file, credits } = loadFundingFile();
|
|
49
|
-
return file.exact.filter(
|
|
50
|
-
(entry) => (!endpoint || fundingReachableAt(entry, endpoint, credits)) && (!filter.callerPubkey || entry.callerPubkey === filter.callerPubkey) && !x402ExactFundingDead(entry, filter.now)
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
function clearPendingX402ExactFunding(pending) {
|
|
54
|
-
const { file } = loadFundingFile();
|
|
55
|
-
const key = pendingKey(pending);
|
|
56
|
-
file.exact = file.exact.filter(
|
|
57
|
-
(entry) => pendingKey(entry) !== key || entry.fundId !== pending.fundId
|
|
58
|
-
);
|
|
59
|
-
saveFile(file);
|
|
60
|
-
}
|
|
61
|
-
var X402_PENDING_STALE_MS = 24 * 60 * 60 * 1e3;
|
|
62
|
-
function x402PendingStale(pending, now = Date.now()) {
|
|
63
|
-
return now - pending.requestedAt >= X402_PENDING_STALE_MS;
|
|
64
|
-
}
|
|
65
|
-
function x402PendingRetired(pending) {
|
|
66
|
-
return pending.refusedAt !== void 0 || pending.supersededAt !== void 0;
|
|
67
|
-
}
|
|
68
|
-
function x402PendingBlockExpiresAt(pending) {
|
|
69
|
-
return x402PendingDepositExpiresAt(pending) ?? pending.requestedAt + X402_PENDING_STALE_MS;
|
|
70
|
-
}
|
|
71
|
-
function x402PendingDepositExpiresAt(pending) {
|
|
72
|
-
const seconds = pending.authorizationValidBefore ? Number(pending.authorizationValidBefore) : NaN;
|
|
73
|
-
return Number.isFinite(seconds) ? seconds * 1e3 : void 0;
|
|
74
|
-
}
|
|
75
|
-
function x402PendingUnsettled(pending, now = Date.now()) {
|
|
76
|
-
return !x402PendingRetired(pending) || now <= x402PendingBlockExpiresAt(pending);
|
|
77
|
-
}
|
|
78
|
-
function x402PendingFundingJson(entry) {
|
|
79
|
-
return {
|
|
80
|
-
credit_id: entry.creditId,
|
|
81
|
-
fund_id: entry.fundId,
|
|
82
|
-
endpoint: entry.endpoint,
|
|
83
|
-
channel_id: entry.channelId,
|
|
84
|
-
amount_micro: entry.amountMicro,
|
|
85
|
-
// What the ERC-3009 authorization can still pull, which is the figure a
|
|
86
|
-
// `--force` retirement is really deciding about: a deposit's authorized
|
|
87
|
-
// value can exceed the amount funded (an internal review), so `amount_micro` alone
|
|
88
|
-
// understates it. Null on a voucher-only payload, which authorizes no
|
|
89
|
-
// deposit at all.
|
|
90
|
-
deposit_micro: entry.depositMicro ?? null,
|
|
91
|
-
signed_max_claimable_micro: entry.maxClaimableMicro ?? null,
|
|
92
|
-
requested_at: new Date(entry.requestedAt).toISOString(),
|
|
93
|
-
stale: x402PendingStale(entry),
|
|
94
|
-
...entry.authorizationValidBefore ? {
|
|
95
|
-
authorization_valid_before: new Date(
|
|
96
|
-
Number(entry.authorizationValidBefore) * 1e3
|
|
97
|
-
).toISOString()
|
|
98
|
-
} : {},
|
|
99
|
-
...entry.refusedAt !== void 0 ? {
|
|
100
|
-
refused_at: new Date(entry.refusedAt).toISOString(),
|
|
101
|
-
funding_blocked_until: new Date(x402PendingBlockExpiresAt(entry)).toISOString()
|
|
102
|
-
} : {},
|
|
103
|
-
...entry.supersededAt !== void 0 ? {
|
|
104
|
-
superseded_at: new Date(entry.supersededAt).toISOString(),
|
|
105
|
-
funding_blocked_until: new Date(x402PendingBlockExpiresAt(entry)).toISOString()
|
|
106
|
-
} : {}
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
var EMPTY = { version: 1, channels: {}, pending: [], exact: [] };
|
|
110
|
-
var x402LockOwner = new AsyncLocalStorage();
|
|
111
|
-
async function withX402ChannelLock(operation) {
|
|
112
|
-
if (x402LockOwner.getStore()?.active) return operation();
|
|
113
|
-
ensureX402ChannelFile();
|
|
114
|
-
const release = await import_proper_lockfile.default.lock(X402_CHANNELS_FILE, {
|
|
115
|
-
realpath: false,
|
|
116
|
-
retries: { retries: 20, minTimeout: 25, maxTimeout: 250 }
|
|
117
|
-
});
|
|
118
|
-
const owner = { active: true };
|
|
119
|
-
try {
|
|
120
|
-
return await x402LockOwner.run(owner, operation);
|
|
121
|
-
} finally {
|
|
122
|
-
owner.active = false;
|
|
123
|
-
await release();
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
var DvmX402ChannelStorage = class {
|
|
127
|
-
nextBalanceProvenance;
|
|
128
|
-
get(key) {
|
|
129
|
-
return fromSynchronousStore(() => loadFile().channels[canonicalChannelId(key)]?.context);
|
|
130
|
-
}
|
|
131
|
-
/** Read the highest cumulative amount a DVM has acknowledged for this channel. */
|
|
132
|
-
getAcknowledgedCumulativeAmount(key) {
|
|
133
|
-
return fromSynchronousStore(
|
|
134
|
-
() => readAcknowledgedCumulativeAmount(loadFile().channels[canonicalChannelId(key)])
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Upstream's ordinary write. Its chain-recovery paths are authoritative and
|
|
139
|
-
* clear the unreconciled stamp. DVM-response paths scope this same write to
|
|
140
|
-
* persist their weaker provenance atomically with the balance (an internal review).
|
|
141
|
-
*/
|
|
142
|
-
set(key, context) {
|
|
143
|
-
return fromSynchronousStore(() => {
|
|
144
|
-
const provenance = this.nextBalanceProvenance?.provenance ?? "observed";
|
|
145
|
-
this.nextBalanceProvenance = void 0;
|
|
146
|
-
writeContext(key, context, provenance, "advance");
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Apply non-chain provenance to the next upstream `set` on this instance.
|
|
151
|
-
* Returns a cleanup for paths where upstream rejects or deletes instead, so a
|
|
152
|
-
* later chain recovery cannot inherit a stale provenance decision.
|
|
153
|
-
*/
|
|
154
|
-
stageNextUnobservedBalanceProvenance(provenance) {
|
|
155
|
-
const staged = { provenance };
|
|
156
|
-
this.nextBalanceProvenance = staged;
|
|
157
|
-
return () => {
|
|
158
|
-
if (this.nextBalanceProvenance === staged) this.nextBalanceProvenance = void 0;
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Write a context whose `balance` carries a deposit this machine added on its
|
|
163
|
-
* own authority, and stamp the record as unreconciled (an internal review).
|
|
164
|
-
*
|
|
165
|
-
* Separate from {@link set} because the two differ in exactly one thing that
|
|
166
|
-
* matters later: where the figure came from. Nothing else about the write
|
|
167
|
-
* differs.
|
|
168
|
-
*/
|
|
169
|
-
setLocallyAppliedDeposit(key, context) {
|
|
170
|
-
return fromSynchronousStore(() => {
|
|
171
|
-
writeContext(key, context, "unreconciled", "advance");
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Write a context whose `balance` is byte-for-byte the one already stored,
|
|
176
|
-
* leaving the unreconciled stamp exactly as it found it (an internal review).
|
|
177
|
-
*
|
|
178
|
-
* The replay acknowledgement's idempotent second pass is what needs this: it
|
|
179
|
-
* restates a cumulative base without adding to the balance, so it observes
|
|
180
|
-
* nothing and must not certify the local sum it left alone. The caller owes
|
|
181
|
-
* the precondition in the name — a write that does move `balance` belongs on
|
|
182
|
-
* one of the other two.
|
|
183
|
-
*/
|
|
184
|
-
setUnchangedBalance(key, context) {
|
|
185
|
-
return fromSynchronousStore(() => {
|
|
186
|
-
writeContext(key, context, "unchanged", "advance");
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Seed upstream's cooperative-refund probe without changing the acknowledged
|
|
191
|
-
* base. Records written before an internal review have only the upstream context, so
|
|
192
|
-
* `hold` adopts its acknowledged value rather than losing that base to the
|
|
193
|
-
* chain floor this seeds. The seed itself is a `channels()` read, so it is
|
|
194
|
-
* `observed` and settles the unreconciled question like any other.
|
|
195
|
-
*/
|
|
196
|
-
setRefundProbe(key, context) {
|
|
197
|
-
return fromSynchronousStore(() => {
|
|
198
|
-
writeContext(key, context, "observed", "hold");
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Replace the stored projection with a successful `channels()` read.
|
|
203
|
-
*
|
|
204
|
-
* Only `balance` and `totalClaimed` move. The highest DVM-acknowledged
|
|
205
|
-
* cumulative base stays fail-closed, and the chain observation clears the
|
|
206
|
-
* unreconciled stamp.
|
|
207
|
-
*/
|
|
208
|
-
reconcileChainState(key, state) {
|
|
209
|
-
return fromSynchronousStore(() => {
|
|
210
|
-
const file = loadFile();
|
|
211
|
-
const id = canonicalChannelId(key);
|
|
212
|
-
const record = file.channels[id] ?? {};
|
|
213
|
-
file.channels[id] = channelRecordWithContext(
|
|
214
|
-
record,
|
|
215
|
-
{ ...record.context ?? {}, ...state },
|
|
216
|
-
"observed",
|
|
217
|
-
"hold"
|
|
218
|
-
);
|
|
219
|
-
saveFile(file);
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
delete(key) {
|
|
223
|
-
return fromSynchronousStore(() => {
|
|
224
|
-
const file = loadFile();
|
|
225
|
-
const id = canonicalChannelId(key);
|
|
226
|
-
const record = file.channels[id];
|
|
227
|
-
if (!record) return;
|
|
228
|
-
if (record.metadata) file.channels[id] = { metadata: record.metadata };
|
|
229
|
-
else {
|
|
230
|
-
const { [id]: _removed, ...channels } = file.channels;
|
|
231
|
-
file.channels = channels;
|
|
232
|
-
}
|
|
233
|
-
saveFile(file);
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
};
|
|
237
|
-
function recordX402ChannelMetadata(metadata) {
|
|
238
|
-
const file = loadFile();
|
|
239
|
-
const id = canonicalChannelId(metadata.channelId);
|
|
240
|
-
const acknowledged = file.channels[id]?.metadata;
|
|
241
|
-
const sameOwner = acknowledged?.endpoint === metadata.endpoint && acknowledged.callerPubkey === metadata.callerPubkey && acknowledged.creditId === metadata.creditId;
|
|
242
|
-
file.channels[id] = {
|
|
243
|
-
...file.channels[id],
|
|
244
|
-
metadata: !acknowledged || sameOwner ? { ...metadata, channelId: id } : { ...acknowledged, channelId: id }
|
|
245
|
-
};
|
|
246
|
-
saveFile(file);
|
|
247
|
-
}
|
|
248
|
-
function acknowledgeX402ChannelOwner(owner) {
|
|
249
|
-
const file = loadFile();
|
|
250
|
-
const id = canonicalChannelId(owner.channelId);
|
|
251
|
-
const record = file.channels[id];
|
|
252
|
-
if (!record?.metadata) {
|
|
253
|
-
throw new Error(
|
|
254
|
-
`Cannot acknowledge credit ${owner.creditId}: x402 channel ${id} has no metadata`
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
|
-
file.channels[id] = {
|
|
258
|
-
...record,
|
|
259
|
-
metadata: {
|
|
260
|
-
...record.metadata,
|
|
261
|
-
endpoint: owner.endpoint,
|
|
262
|
-
callerPubkey: owner.callerPubkey,
|
|
263
|
-
creditId: owner.creditId,
|
|
264
|
-
updatedAt: Date.now()
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
saveFile(file);
|
|
268
|
-
}
|
|
269
|
-
function listX402Channels(filter = {}) {
|
|
270
|
-
const payer = filter.payer?.toLowerCase();
|
|
271
|
-
return allChannelMetadata().filter((entry) => !payer || entry.config.payer.toLowerCase() === payer).sort((a, b) => b.updatedAt - a.updatedAt);
|
|
272
|
-
}
|
|
273
|
-
function findX402Channel(args) {
|
|
274
|
-
const channels = allChannelMetadata();
|
|
275
|
-
if (args.channelId) {
|
|
276
|
-
const channelId = canonicalChannelId(args.channelId);
|
|
277
|
-
return channels.find((entry) => entry.channelId === channelId);
|
|
278
|
-
}
|
|
279
|
-
const endpoint = args.endpoint ? normalizeEndpoint(args.endpoint) : void 0;
|
|
280
|
-
return channels.filter(
|
|
281
|
-
(entry) => (!endpoint || entry.endpoint === endpoint) && (!args.callerPubkey || entry.callerPubkey === args.callerPubkey) && (!args.creditId || entry.creditId === args.creditId)
|
|
282
|
-
).sort((a, b) => b.updatedAt - a.updatedAt)[0];
|
|
283
|
-
}
|
|
284
|
-
function x402ChannelBalanceUnreconciledAt(channelId) {
|
|
285
|
-
return loadFile().channels[canonicalChannelId(channelId)]?.unreconciledBalanceAt;
|
|
286
|
-
}
|
|
287
|
-
function recordPendingX402Funding(pending) {
|
|
288
|
-
const { file, credits } = loadFundingFile();
|
|
289
|
-
const pendingOrigin = normalizeEndpoint(pending.endpoint);
|
|
290
|
-
file.pending = [
|
|
291
|
-
...file.pending.flatMap((entry) => {
|
|
292
|
-
if (!sameFundingCredit(entry, pending, credits) || x402PendingRetired(entry)) return [entry];
|
|
293
|
-
if (normalizeEndpoint(entry.endpoint) === pendingOrigin) return [];
|
|
294
|
-
return [{ ...entry, supersededAt: pending.requestedAt }];
|
|
295
|
-
}),
|
|
296
|
-
pending
|
|
297
|
-
];
|
|
298
|
-
saveFile(file);
|
|
299
|
-
}
|
|
300
|
-
function loadResumableX402Funding(args) {
|
|
301
|
-
const { file, credits } = loadFundingFile();
|
|
302
|
-
return file.pending.find(
|
|
303
|
-
(entry) => sameFundingCredit(entry, args, credits) && !x402PendingRetired(entry)
|
|
304
|
-
);
|
|
305
|
-
}
|
|
306
|
-
function listPendingX402Fundings(filter = {}) {
|
|
307
|
-
const endpoint = filter.endpoint ? normalizeEndpoint(filter.endpoint) : void 0;
|
|
308
|
-
const { file, credits } = loadFundingFile();
|
|
309
|
-
return file.pending.filter(
|
|
310
|
-
(entry) => (!endpoint || fundingReachableAt(entry, endpoint, credits)) && (!filter.callerPubkey || entry.callerPubkey === filter.callerPubkey)
|
|
311
|
-
);
|
|
312
|
-
}
|
|
313
|
-
function listResumableX402Fundings(filter = {}) {
|
|
314
|
-
return listPendingX402Fundings(filter).filter((entry) => !x402PendingRetired(entry));
|
|
315
|
-
}
|
|
316
|
-
function listPendingX402FundingsForChannel(channelId) {
|
|
317
|
-
const canonical = canonicalChannelId(channelId);
|
|
318
|
-
return loadFundingFile().file.pending.filter(
|
|
319
|
-
(entry) => canonicalChannelId(entry.channelId) === canonical
|
|
320
|
-
);
|
|
321
|
-
}
|
|
322
|
-
function retirePendingX402Funding(pending) {
|
|
323
|
-
const { file } = loadFundingFile();
|
|
324
|
-
const key = pendingKey(pending);
|
|
325
|
-
const target = file.pending.find(
|
|
326
|
-
(entry) => pendingKey(entry) === key && !x402PendingRetired(entry)
|
|
327
|
-
);
|
|
328
|
-
if (!target) return;
|
|
329
|
-
const refusedAt = Date.now();
|
|
330
|
-
file.pending = file.pending.map((entry) => entry === target ? { ...entry, refusedAt } : entry);
|
|
331
|
-
saveFile(file);
|
|
332
|
-
}
|
|
333
|
-
function clearPendingX402Funding(pending) {
|
|
334
|
-
const { file } = loadFundingFile();
|
|
335
|
-
const next = file.pending.filter((entry) => !sameJournalEntry(entry, pending));
|
|
336
|
-
if (next.length === file.pending.length) return;
|
|
337
|
-
file.pending = next;
|
|
338
|
-
saveFile(file);
|
|
339
|
-
}
|
|
340
|
-
function writeContext(key, context, provenance, base) {
|
|
341
|
-
const file = loadFile();
|
|
342
|
-
const id = canonicalChannelId(key);
|
|
343
|
-
const record = file.channels[id] ?? {};
|
|
344
|
-
file.channels[id] = channelRecordWithContext(record, context, provenance, base);
|
|
345
|
-
saveFile(file);
|
|
346
|
-
}
|
|
347
|
-
function channelRecordWithContext(record, context, provenance, base) {
|
|
348
|
-
const { unreconciledBalanceAt: previousStamp, ...kept } = record;
|
|
349
|
-
const stamp = provenance === "unreconciled" ? Date.now() : provenance === "unchanged" ? previousStamp : void 0;
|
|
350
|
-
const stored = readAcknowledgedCumulativeAmount(record);
|
|
351
|
-
const acknowledgedCumulativeAmount = base === "advance" ? highestCumulativeAmount(stored, context.chargedCumulativeAmount) : stored;
|
|
352
|
-
return {
|
|
353
|
-
...kept,
|
|
354
|
-
context,
|
|
355
|
-
...acknowledgedCumulativeAmount !== void 0 ? { acknowledgedCumulativeAmount } : {},
|
|
356
|
-
...stamp !== void 0 ? { unreconciledBalanceAt: stamp } : {}
|
|
357
|
-
};
|
|
358
|
-
}
|
|
359
|
-
function allChannelMetadata() {
|
|
360
|
-
return Object.values(loadFile().channels).filter((record) => record !== void 0).map((record) => record.metadata).filter((entry) => entry !== void 0);
|
|
361
|
-
}
|
|
362
|
-
function pendingKey(entry) {
|
|
363
|
-
return `${normalizeEndpoint(entry.endpoint)}\0${entry.callerPubkey}\0${entry.creditId}`;
|
|
364
|
-
}
|
|
365
|
-
function sameFundingCredit(entry, target, credits) {
|
|
366
|
-
return entry.callerPubkey === target.callerPubkey && entry.creditId === target.creditId && fundingReachableAt(entry, normalizeEndpoint(target.endpoint), credits);
|
|
367
|
-
}
|
|
368
|
-
function loadFundingFile() {
|
|
369
|
-
const snapshot = collapseFundingFile();
|
|
370
|
-
if (!snapshot.changed) return snapshot;
|
|
371
|
-
const persisted = tryWithX402ChannelLockSync(() => {
|
|
372
|
-
const current = collapseFundingFile();
|
|
373
|
-
if (current.changed) saveFile(current.file);
|
|
374
|
-
return current;
|
|
375
|
-
});
|
|
376
|
-
return persisted ?? snapshot;
|
|
377
|
-
}
|
|
378
|
-
function collapseFundingFile() {
|
|
379
|
-
const file = loadFile();
|
|
380
|
-
const credits = listCredits();
|
|
381
|
-
const pending = dedupeX402FundingCredits(file.pending, credits);
|
|
382
|
-
const exact = dedupeFundingCredits(file.exact, credits);
|
|
383
|
-
if (pending.changed) file.pending = pending.entries;
|
|
384
|
-
if (exact.changed) file.exact = exact.entries;
|
|
385
|
-
return { file, credits, changed: pending.changed || exact.changed };
|
|
386
|
-
}
|
|
387
|
-
function dedupeX402FundingCredits(entries, credits) {
|
|
388
|
-
const kept = [];
|
|
389
|
-
let changed = false;
|
|
390
|
-
for (const entry of entries) {
|
|
391
|
-
if (x402PendingRetired(entry)) {
|
|
392
|
-
kept.push(entry);
|
|
393
|
-
continue;
|
|
394
|
-
}
|
|
395
|
-
const index = kept.findIndex(
|
|
396
|
-
(candidate) => !x402PendingRetired(candidate) && sameFundingCredit(candidate, entry, credits)
|
|
397
|
-
);
|
|
398
|
-
if (index === -1) {
|
|
399
|
-
kept.push(entry);
|
|
400
|
-
continue;
|
|
401
|
-
}
|
|
402
|
-
const seen = kept[index];
|
|
403
|
-
const seenAt = Number.isFinite(seen.requestedAt) ? seen.requestedAt : 0;
|
|
404
|
-
const entryAt = Number.isFinite(entry.requestedAt) ? entry.requestedAt : 0;
|
|
405
|
-
const sameOrigin = normalizeEndpoint(seen.endpoint) === normalizeEndpoint(entry.endpoint);
|
|
406
|
-
if (entryAt > seenAt) {
|
|
407
|
-
kept[index] = entry;
|
|
408
|
-
if (!sameOrigin) kept.push({ ...seen, supersededAt: entry.requestedAt });
|
|
409
|
-
} else if (!sameOrigin) {
|
|
410
|
-
kept.push({ ...entry, supersededAt: seen.requestedAt });
|
|
411
|
-
}
|
|
412
|
-
changed = true;
|
|
413
|
-
}
|
|
414
|
-
return { entries: kept, changed };
|
|
415
|
-
}
|
|
416
|
-
function dedupeFundingCredits(entries, credits) {
|
|
417
|
-
const kept = [];
|
|
418
|
-
let changed = false;
|
|
419
|
-
for (const entry of entries) {
|
|
420
|
-
const index = kept.findIndex((candidate) => sameFundingCredit(candidate, entry, credits));
|
|
421
|
-
if (index === -1) {
|
|
422
|
-
kept.push(entry);
|
|
423
|
-
continue;
|
|
424
|
-
}
|
|
425
|
-
const seenAt = Number.isFinite(kept[index].requestedAt) ? kept[index].requestedAt : 0;
|
|
426
|
-
const entryAt = Number.isFinite(entry.requestedAt) ? entry.requestedAt : 0;
|
|
427
|
-
if (entryAt > seenAt) kept[index] = entry;
|
|
428
|
-
changed = true;
|
|
429
|
-
}
|
|
430
|
-
return { entries: kept, changed };
|
|
431
|
-
}
|
|
432
|
-
function sameJournalEntry(entry, target) {
|
|
433
|
-
return pendingKey(entry) === pendingKey(target) && entry.fundId === target.fundId && x402PendingRetired(entry) === x402PendingRetired(target);
|
|
434
|
-
}
|
|
435
|
-
function fromSynchronousStore(operation) {
|
|
436
|
-
try {
|
|
437
|
-
return Promise.resolve(operation());
|
|
438
|
-
} catch (error) {
|
|
439
|
-
return Promise.reject(error instanceof Error ? error : new Error(String(error)));
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
function highestCumulativeAmount(previous, next) {
|
|
443
|
-
if (previous === void 0) return next;
|
|
444
|
-
if (next === void 0) return previous;
|
|
445
|
-
return BigInt(next) > BigInt(previous) ? next : previous;
|
|
446
|
-
}
|
|
447
|
-
function readAcknowledgedCumulativeAmount(record) {
|
|
448
|
-
return record?.acknowledgedCumulativeAmount ?? record?.context?.chargedCumulativeAmount;
|
|
449
|
-
}
|
|
450
|
-
function canonicalChannelId(value) {
|
|
451
|
-
return value.toLowerCase();
|
|
452
|
-
}
|
|
453
|
-
function normalizeEndpoint(endpoint) {
|
|
454
|
-
try {
|
|
455
|
-
return new URL(endpoint).origin;
|
|
456
|
-
} catch {
|
|
457
|
-
return endpoint.replace(/\/+$/, "");
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
function tryWithX402ChannelLockSync(operation) {
|
|
461
|
-
if (x402LockOwner.getStore()?.active) return operation();
|
|
462
|
-
ensureX402ChannelFile();
|
|
463
|
-
let release;
|
|
464
|
-
try {
|
|
465
|
-
release = import_proper_lockfile.default.lockSync(X402_CHANNELS_FILE, { realpath: false });
|
|
466
|
-
} catch (error) {
|
|
467
|
-
if (error.code === "ELOCKED") return void 0;
|
|
468
|
-
throw error;
|
|
469
|
-
}
|
|
470
|
-
const owner = { active: true };
|
|
471
|
-
try {
|
|
472
|
-
return x402LockOwner.run(owner, operation);
|
|
473
|
-
} finally {
|
|
474
|
-
owner.active = false;
|
|
475
|
-
release();
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
function ensureX402ChannelFile() {
|
|
479
|
-
ensureConfigDir();
|
|
480
|
-
if (existsSync(X402_CHANNELS_FILE)) return;
|
|
481
|
-
try {
|
|
482
|
-
writeFileSync(X402_CHANNELS_FILE, `${JSON.stringify(EMPTY, null, 2)}
|
|
483
|
-
`, {
|
|
484
|
-
mode: 384,
|
|
485
|
-
flag: "wx"
|
|
486
|
-
});
|
|
487
|
-
} catch {
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
function loadFile() {
|
|
491
|
-
if (!existsSync(X402_CHANNELS_FILE)) return { ...EMPTY, channels: {}, pending: [], exact: [] };
|
|
492
|
-
try {
|
|
493
|
-
const raw = JSON.parse(readFileSync(X402_CHANNELS_FILE, "utf8"));
|
|
494
|
-
if (raw.version !== 1 || !raw.channels || typeof raw.channels !== "object" || Array.isArray(raw.channels) || !Array.isArray(raw.pending)) {
|
|
495
|
-
throw new Error("unsupported x402 channel store shape");
|
|
496
|
-
}
|
|
497
|
-
return {
|
|
498
|
-
version: 1,
|
|
499
|
-
channels: raw.channels,
|
|
500
|
-
pending: raw.pending,
|
|
501
|
-
// Tolerated rather than required, unlike its two siblings: every file
|
|
502
|
-
// written before an internal review lacks the key, and failing closed on it would
|
|
503
|
-
// read as a corrupt journal on a store that is perfectly intact.
|
|
504
|
-
exact: Array.isArray(raw.exact) ? raw.exact : []
|
|
505
|
-
};
|
|
506
|
-
} catch (cause) {
|
|
507
|
-
throw new DvmError(
|
|
508
|
-
"x402_channel_store_invalid",
|
|
509
|
-
"The saved x402 channel journal cannot be read safely.",
|
|
510
|
-
`Do not delete ${X402_CHANNELS_FILE}; restore or repair it before signing another channel voucher.`,
|
|
511
|
-
{ cause: cause instanceof Error ? cause.message : String(cause) }
|
|
512
|
-
);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
function saveFile(file) {
|
|
516
|
-
ensureConfigDir();
|
|
517
|
-
const tmp = `${X402_CHANNELS_FILE}.${String(process.pid)}.tmp`;
|
|
518
|
-
writeFileSync(tmp, `${JSON.stringify(file, null, 2)}
|
|
519
|
-
`, { mode: 384 });
|
|
520
|
-
renameSync(tmp, X402_CHANNELS_FILE);
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
export {
|
|
524
|
-
X402_EXACT_AUTHORIZATION_TTL_SECONDS,
|
|
525
|
-
x402ExactFundingDead,
|
|
526
|
-
recordPendingX402ExactFunding,
|
|
527
|
-
loadResumableX402ExactFunding,
|
|
528
|
-
listResumableX402ExactFundings,
|
|
529
|
-
clearPendingX402ExactFunding,
|
|
530
|
-
X402_PENDING_STALE_MS,
|
|
531
|
-
x402PendingStale,
|
|
532
|
-
x402PendingRetired,
|
|
533
|
-
x402PendingBlockExpiresAt,
|
|
534
|
-
x402PendingDepositExpiresAt,
|
|
535
|
-
x402PendingUnsettled,
|
|
536
|
-
x402PendingFundingJson,
|
|
537
|
-
withX402ChannelLock,
|
|
538
|
-
DvmX402ChannelStorage,
|
|
539
|
-
recordX402ChannelMetadata,
|
|
540
|
-
acknowledgeX402ChannelOwner,
|
|
541
|
-
listX402Channels,
|
|
542
|
-
findX402Channel,
|
|
543
|
-
x402ChannelBalanceUnreconciledAt,
|
|
544
|
-
recordPendingX402Funding,
|
|
545
|
-
loadResumableX402Funding,
|
|
546
|
-
listPendingX402Fundings,
|
|
547
|
-
listResumableX402Fundings,
|
|
548
|
-
listPendingX402FundingsForChannel,
|
|
549
|
-
retirePendingX402Funding,
|
|
550
|
-
clearPendingX402Funding
|
|
551
|
-
};
|