@uuaid/pillar 2.6.1 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/README.md +201 -5
  2. package/bin/carrier.mjs +21 -0
  3. package/bin/pillar-ask.mjs +4 -1
  4. package/bin/pillar-blob.mjs +6 -0
  5. package/bin/pillar-directory.mjs +280 -16
  6. package/bin/pillar-escrow.mjs +4 -1
  7. package/bin/pillar-group-v2.mjs +6 -0
  8. package/bin/pillar-notify.mjs +13 -0
  9. package/bin/pillar-responder.mjs +266 -23
  10. package/bin/pillar.mjs +504 -255
  11. package/package.json +17 -25
  12. package/src/cli-help.mjs +23 -0
  13. package/src/group-v2-cli.mjs +1 -1
  14. package/src/identity/trust.mjs +72 -6
  15. package/src/mailbox/mailbox.mjs +308 -12
  16. package/src/net/carrier-server.mjs +34 -2
  17. package/src/net/refusals.mjs +2 -0
  18. package/src/net/seed-registry.mjs +2 -1
  19. package/src/net/transport.mjs +308 -92
  20. package/src/node-check.mjs +29 -8
  21. package/src/notify-events.mjs +148 -0
  22. package/src/notify-log.mjs +124 -0
  23. package/src/notify-runner.mjs +372 -0
  24. package/src/notify-store.mjs +725 -0
  25. package/src/notify.mjs +350 -0
  26. package/src/pillar.mjs +382 -27
  27. package/src/readers.mjs +2 -2
  28. package/src/registry/federation.mjs +3056 -0
  29. package/src/registry/gossip.mjs +294 -0
  30. package/src/registry/heartbeats.mjs +195 -0
  31. package/src/registry/lineage-verifier.mjs +972 -0
  32. package/src/registry/lineage.mjs +984 -0
  33. package/src/registry/listings.mjs +549 -0
  34. package/src/registry/resolve.mjs +376 -0
  35. package/src/registry/stats.mjs +508 -0
  36. package/src/sqlite-loader.mjs +40 -0
  37. package/src/vendor/pillar-core.mjs +22 -0
  38. package/src/vendor/uuaid-soul.mjs +2246 -0
  39. package/src/vnext/a6b-common.mjs +270 -0
  40. package/src/vnext/b1-cli.mjs +970 -0
  41. package/src/vnext/card-cli.mjs +370 -0
  42. package/src/vnext/greeter-operator.mjs +160 -0
  43. package/src/vnext/greeter-suggest.mjs +318 -0
  44. package/src/vnext/greeter.mjs +571 -0
  45. package/src/vnext/homes.mjs +48 -0
  46. package/src/vnext/init-cli.mjs +489 -0
  47. package/src/vnext/intro-cli.mjs +81 -0
  48. package/src/vnext/json-dir-mailbox.mjs +296 -0
  49. package/src/vnext/jsonl-mailbox.mjs +1066 -0
  50. package/src/vnext/knock-answer.mjs +162 -0
  51. package/src/vnext/knock-cli.mjs +520 -0
  52. package/src/vnext/knock-lane.mjs +312 -0
  53. package/src/vnext/knock-responder.mjs +344 -0
  54. package/src/vnext/lineage-cli.mjs +653 -0
  55. package/src/vnext/listing-cli.mjs +323 -0
  56. package/src/vnext/resolve-cli.mjs +86 -0
  57. package/src/vnext/soul-cli.mjs +666 -0
  58. package/src/vnext/verify-cli.mjs +251 -0
  59. package/src/vnext/w4-redirect.mjs +178 -0
  60. package/src/vnext/wave-a.mjs +193 -0
  61. package/src/vnext/witness-cli.mjs +547 -0
@@ -0,0 +1,376 @@
1
+ /**
2
+ * Pillar vNext §3 — Single-registry `GET /v1/resolve/<uuaid>` builder and HTTP route handler.
3
+ *
4
+ * Wave A boundaries:
5
+ * - Serves `/v1/resolve/<uuaid>`, `/v1/lineage/<uuaid>`, `POST /v1/lineage`, `POST /v1/listings`.
6
+ * - Answers strictly from local SQLite (no federation fan-out; `via` omitted).
7
+ * - Zero `uuaid-lineage-seen/1` receipts (Option 1).
8
+ * - H-2: malformed `<uuaid>` returns `400` `{ ok: false, reason: "bad-uuaid" }`.
9
+ */
10
+ import { MAX_HEARTBEAT_BODY } from "./heartbeats.mjs";
11
+ import { LINEAGE_LOG_VERSION, UUAID_RE, isPlainObject, isStrictDateTime } from "./lineage.mjs";
12
+ import { computeListingStatus, isStrictHttpsUri } from "./listings.mjs";
13
+
14
+ export const RESOLVE_VERSION = "pillar-resolve/1";
15
+ export const MAX_LISTING_BODY = 64 * 1024;
16
+ export const MAX_LINEAGE_BODY = 600 * 1024;
17
+ export { MAX_HEARTBEAT_BODY };
18
+ const RESOLVED_LINEAGE_VERDICTS = new Set(["valid", "recovered", "pending-recovery"]);
19
+
20
+ function nowIso(now) {
21
+ if (typeof now === "string") return now;
22
+ const ms = typeof now === "number" ? now : Date.now();
23
+ return new Date(ms).toISOString().replace(/\.\d{3}Z$/, "Z");
24
+ }
25
+
26
+ /**
27
+ * Resolve any genesis, rotated, or current UUAID from local SQLite stores into a
28
+ * schema-valid `pillar-resolve/1` response (or `bad-uuaid` / `not-found`).
29
+ */
30
+ export function resolveUuaid(uuaid, { listingStore, lineageStore, heartbeatStore, answeredBy, now } = {}) {
31
+ if (typeof uuaid !== "string" || !UUAID_RE.test(uuaid)) return { ok: false, status: 400, reason: "bad-uuaid" }; // @rule reg.resolve-uuaid
32
+ if (!isStrictHttpsUri(answeredBy)) throw new TypeError("resolveUuaid: answeredBy must be a valid https:// URI"); // @rule reg.resolve-answered-by
33
+
34
+ // No pruning here (V1 F2): it re-verified every listing on every request.
35
+ // Pruning runs off the request path (startTombstonePruning, listings.mjs).
36
+ const nowStr = nowIso(now);
37
+
38
+ let linLineage = null;
39
+ if (lineageStore) {
40
+ const linLookup = lineageStore.lookupLineageByUuaid(uuaid);
41
+ if (!linLookup.ok && linLookup.reason === "ambiguous-uuaid") return { ok: false, status: 409, reason: "ambiguous-uuaid" }; // @rule reg.resolve-lineage-ambiguous
42
+ if (linLookup.ok) linLineage = linLookup.lineage;
43
+ }
44
+ let listRow = null;
45
+ if (listingStore) {
46
+ const listLookup = listingStore.lookupByUuaidOrLineage(uuaid);
47
+ if (!listLookup.ok && listLookup.reason === "ambiguous-uuaid") return { ok: false, status: 409, reason: "ambiguous-uuaid" }; // @rule reg.resolve-listing-ambiguous
48
+ if (listLookup.ok) listRow = listLookup.row;
49
+ }
50
+ if (linLineage && listRow && linLineage !== listRow.lineage) return { ok: false, status: 409, reason: "ambiguous-uuaid" }; // @rule reg.resolve-cross-store-ambiguous
51
+ const lineage = linLineage ?? listRow?.lineage ?? null;
52
+ if (!lineage) return { ok: false, status: 404, reason: "not-found" }; // @rule reg.resolve-not-found
53
+ const listingRow = listRow ?? (listingStore ? listingStore.getByLineage(lineage) : null);
54
+
55
+ const lineageEval = lineageStore ? lineageStore.evaluateLineage(lineage, { now: nowStr }) : null;
56
+ const listing = listingRow?.listing ?? null;
57
+
58
+ let current;
59
+ let lineageVerdict;
60
+ let lineageHead;
61
+ let includeListing = Boolean(listing);
62
+
63
+ if (lineageEval) {
64
+ if (RESOLVED_LINEAGE_VERDICTS.has(lineageEval.verdict)) {
65
+ current = lineageEval.current;
66
+ lineageVerdict = lineageEval.verdict;
67
+ lineageHead = lineageEval.head;
68
+ if (includeListing && listing.uuaid !== current) includeListing = false; // @rule reg.resolve-listing-matches-current
69
+ } else if (lineageEval.verdict === "contested") {
70
+ current = lineage;
71
+ lineageVerdict = "contested";
72
+ includeListing = false; // @rule reg.resolve-contested-no-listing
73
+ } else {
74
+ return { ok: false, status: 404, reason: "not-found" }; // @rule reg.resolve-lineage-invalid
75
+ }
76
+ } else {
77
+ current = listing.uuaid;
78
+ }
79
+
80
+ const hbStore = heartbeatStore ?? listingStore?.heartbeatStore ?? null;
81
+ const hbRow = hbStore && lineageVerdict !== "contested" ? hbStore.getLatest(lineage) : null;
82
+ let includeHeartbeat = Boolean(hbRow);
83
+ if (includeHeartbeat && hbRow.uuaid !== current) includeHeartbeat = false; // @rule reg.resolve-heartbeat-matches-current
84
+
85
+ const baseLastSeen = includeListing ? listing.issuedAt : lineageEval.latestTs;
86
+ let lastSeen = baseLastSeen;
87
+ if (includeHeartbeat && (!lastSeen || Date.parse(hbRow.ts) > Date.parse(lastSeen))) lastSeen = hbRow.ts; // @rule reg.resolve-heartbeat-last-seen
88
+
89
+ let status = computeListingStatus(lastSeen, {
90
+ withdrawn: includeListing && listing.withdrawn === true,
91
+ now: nowStr,
92
+ });
93
+ if (lineageEval?.tombstoned) status = "dead"; // @rule reg.resolve-tombstone-dead
94
+
95
+ const evidence = {};
96
+ if (includeHeartbeat) evidence.heartbeat = hbRow.heartbeat;
97
+ if (includeListing) evidence.listing = listing;
98
+ if (lineageHead) evidence.lineageHead = lineageHead;
99
+
100
+ const answer = {
101
+ v: RESOLVE_VERSION,
102
+ query: uuaid,
103
+ lineage,
104
+ current,
105
+ status,
106
+ ...(lastSeen ? { lastSeen } : {}),
107
+ ...(includeListing ? { card: listing.card } : {}),
108
+ ...(includeListing && Array.isArray(listing.card.doors) ? { doors: listing.card.doors } : {}),
109
+ ...(includeListing ? { carriers: listing.card.carriers } : {}),
110
+ ...(lineageVerdict ? { lineageVerdict } : {}),
111
+ evidence,
112
+ answeredBy,
113
+ };
114
+
115
+ return { ok: true, status: 200, answer };
116
+ }
117
+
118
+ function readLimitedBody(req, maxBytes) {
119
+ return new Promise((resolve, reject) => {
120
+ let size = 0;
121
+ let settled = false;
122
+ const chunks = [];
123
+ req.on("data", (c) => {
124
+ if (settled) return;
125
+ size += c.length;
126
+ if (size > maxBytes) { settled = true; const err = new Error("body too large"); err.code = "BODY_TOO_LARGE"; if (typeof req.pause === "function") req.pause(); reject(err); return; } // @rule reg.http-body-limit
127
+ chunks.push(c);
128
+ });
129
+ req.on("end", () => {
130
+ if (!settled) {
131
+ settled = true;
132
+ resolve(Buffer.concat(chunks).toString("utf-8"));
133
+ }
134
+ });
135
+ req.on("error", (err) => {
136
+ if (!settled) {
137
+ settled = true;
138
+ reject(err);
139
+ }
140
+ });
141
+ });
142
+ }
143
+
144
+ function sendNoStore(res, code, obj, { close = false, req = null, extraHeaders = null } = {}) {
145
+ const body = JSON.stringify(obj, null, 2);
146
+ res.writeHead(code, {
147
+ "content-type": "application/json",
148
+ "content-length": Buffer.byteLength(body),
149
+ "access-control-allow-origin": "*",
150
+ "cache-control": "no-store",
151
+ ...(extraHeaders ?? {}),
152
+ ...(close ? { connection: "close" } : {}),
153
+ });
154
+ if (close && req && typeof res.once === "function") {
155
+ res.end(body, () => {
156
+ if (typeof req.destroy === "function" && !req.destroyed) {
157
+ const timer = setTimeout(() => { if (!req.destroyed) req.destroy(); }, 50);
158
+ timer.unref?.();
159
+ res.once("close", () => clearTimeout(timer));
160
+ }
161
+ });
162
+ } else {
163
+ res.end(body);
164
+ if (close && req && typeof req.destroy === "function") req.destroy();
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Handle Wave A registry routes on `pillar-directory`:
170
+ * POST /v1/lineage
171
+ * GET /v1/lineage/<uuaid>
172
+ * POST /v1/listings
173
+ * GET /v1/resolve/<uuaid>
174
+ *
175
+ * Returns `true` if the request matched and was handled, `false` otherwise.
176
+ */
177
+ export async function handleRegistryRoute(
178
+ req,
179
+ res,
180
+ url,
181
+ {
182
+ lineageStore,
183
+ listingStore,
184
+ heartbeatStore,
185
+ gossipCoordinator = null,
186
+ federationCoordinator = null,
187
+ getAnsweredBy,
188
+ getNow = () => undefined,
189
+ checkListingRateLimit = () => true,
190
+ checkLineageRateLimit = () => true,
191
+ checkHeartbeatRateLimit = () => true,
192
+ checkResolveMissRateLimit = () => true,
193
+ },
194
+ ) {
195
+ const hbStore = heartbeatStore ?? listingStore?.heartbeatStore ?? null;
196
+
197
+ if (federationCoordinator && req.method === "GET" && url.pathname === "/v1/federation/peers") {
198
+ sendNoStore(res, 200, federationCoordinator.getSignedPeers({ now: getNow() }));
199
+ return true;
200
+ }
201
+
202
+ if (federationCoordinator && req.method === "GET" && url.pathname === "/v1/federation/changes") {
203
+ const since = url.searchParams.get("since") ?? "0";
204
+ const rawLimit = url.searchParams.get("limit");
205
+ const ch = federationCoordinator.getChanges({
206
+ since,
207
+ ...(rawLimit !== null ? { limit: rawLimit } : {}),
208
+ });
209
+ if (!ch.ok) {
210
+ sendNoStore(res, ch.status ?? 400, { ok: false, reason: ch.reason });
211
+ return true;
212
+ }
213
+ sendNoStore(res, 200, ch.body);
214
+ return true;
215
+ }
216
+
217
+ if (req.method === "POST" && url.pathname === "/v1/heartbeats") {
218
+ if (!checkHeartbeatRateLimit(req)) { sendNoStore(res, 429, { ok: false, reason: "rate-limited" }); return true; } // @rule reg.http-hb-ip-rate-limit
219
+ let payload;
220
+ try {
221
+ payload = JSON.parse(await readLimitedBody(req, MAX_HEARTBEAT_BODY));
222
+ } catch (e) {
223
+ if (e?.code === "BODY_TOO_LARGE") { sendNoStore(res, 413, { ok: false, reason: "body-too-large" }, { close: true, req }); return true; } // @rule reg.http-hb-413
224
+ sendNoStore(res, 400, { ok: false, reason: "bad-json" });
225
+ return true;
226
+ }
227
+ const r = hbStore.putHeartbeat(payload, { now: getNow() });
228
+ if (!r.ok) {
229
+ sendNoStore(res, r.status ?? 400, { ok: false, reason: r.reason }); // @rule reg.http-hb-status
230
+ return true;
231
+ }
232
+ sendNoStore(res, 200, r);
233
+ return true;
234
+ }
235
+ if (req.method === "POST" && (url.pathname === "/v1/lineage" || url.pathname === "/v1/lineage/gossip")) {
236
+ if (!checkLineageRateLimit(req)) { sendNoStore(res, 429, { ok: false, reason: "rate-limited" }); return true; } // @rule reg.http-lineage-ip-rate-limit
237
+ let payload;
238
+ try {
239
+ payload = JSON.parse(await readLimitedBody(req, MAX_LINEAGE_BODY));
240
+ } catch (e) {
241
+ if (e?.code === "BODY_TOO_LARGE") { sendNoStore(res, 400, { ok: false, reason: "lineage-cap-exceeded" }, { close: true, req }); return true; } // @rule reg.http-lineage-body-cap
242
+ sendNoStore(res, 400, { ok: false, reason: "bad-json" });
243
+ return true;
244
+ }
245
+ const nowVal = getNow();
246
+ const isPresentation = isPlainObject(payload) && payload.v === LINEAGE_LOG_VERSION;
247
+ const r = isPresentation ? lineageStore.ingestPresentation(payload, { now: nowVal }) : lineageStore.storeEvent(payload, { now: nowVal });
248
+ if (r.ok && gossipCoordinator) {
249
+ if (r.changed) {
250
+ setImmediate(() => { void gossipCoordinator.syncLineage(r.lineage, { getNow }).catch(() => {}); });
251
+ } else {
252
+ setImmediate(() => { void gossipCoordinator.broadcastCorroboratedClaims(r.lineage, { getNow }).catch(() => {}); });
253
+ }
254
+ }
255
+ sendNoStore(res, r.ok ? 200 : 400, r);
256
+ return true;
257
+ }
258
+
259
+ const lineageMatch = /^\/v1\/lineage\/(.*)$/.exec(url.pathname);
260
+ if (req.method === "GET" && lineageMatch) {
261
+ let uuaid;
262
+ try {
263
+ uuaid = decodeURIComponent(lineageMatch[1]);
264
+ } catch {
265
+ sendNoStore(res, 400, { ok: false, reason: "bad-uuaid" }); // @rule reg.http-lineage-decode-400
266
+ return true;
267
+ }
268
+ const r = lineageStore.getPresentation(uuaid);
269
+ if (!r.ok) {
270
+ if (r.status === 409) { sendNoStore(res, 409, { ok: false, reason: r.reason }); return true; } // @rule reg.http-lineage-409
271
+ sendNoStore(res, r.reason === "bad-uuaid" ? 400 : 404, { ok: false, reason: r.reason });
272
+ return true;
273
+ }
274
+ sendNoStore(res, 200, r.presentation);
275
+ return true;
276
+ }
277
+
278
+ if (req.method === "POST" && url.pathname === "/v1/listings") {
279
+ if (!checkListingRateLimit(req)) { sendNoStore(res, 429, { ok: false, reason: "rate-limited" }); return true; } // @rule reg.http-listing-ip-rate-limit
280
+ let payload;
281
+ try {
282
+ payload = JSON.parse(await readLimitedBody(req, MAX_LISTING_BODY));
283
+ } catch (e) {
284
+ if (e?.code === "BODY_TOO_LARGE") { sendNoStore(res, 413, { ok: false, reason: "body-too-large" }, { close: true, req }); return true; } // @rule reg.http-listing-413
285
+ sendNoStore(res, 400, { ok: false, reason: "bad-json" });
286
+ return true;
287
+ }
288
+ const r = listingStore.putListing(payload, { now: getNow() });
289
+ if (!r.ok && r.status === 409) { sendNoStore(res, 409, { ok: false, reason: r.reason }); return true; } // @rule reg.http-listing-409
290
+ if (!r.ok && r.status === 413) { sendNoStore(res, 413, { ok: false, reason: r.reason }); return true; }
291
+ if (!r.ok && r.status === 429) { sendNoStore(res, 429, { ok: false, reason: r.reason }); return true; } // @rule reg.http-listing-429
292
+ sendNoStore(res, r.ok ? 200 : 400, r.ok ? r : { ok: false, reason: r.reason });
293
+ return true;
294
+ }
295
+
296
+ const resolveMatch = /^\/v1\/resolve\/(.*)$/.exec(url.pathname);
297
+ if (req.method === "GET" && resolveMatch) {
298
+ let uuaid;
299
+ try {
300
+ uuaid = decodeURIComponent(resolveMatch[1]);
301
+ } catch {
302
+ sendNoStore(res, 400, { ok: false, reason: "bad-uuaid" }); // @rule reg.http-resolve-decode-400
303
+ return true;
304
+ }
305
+ const rawDepth = url.searchParams.get("depth");
306
+ if (rawDepth !== null && !/^\d+$/.test(rawDepth)) {
307
+ sendNoStore(res, 400, { ok: false, reason: "bad-depth" });
308
+ return true;
309
+ }
310
+ const depth = rawDepth !== null ? Number(rawDepth) : 0;
311
+ const nowVal = getNow();
312
+ const answeredBy = getAnsweredBy();
313
+ const r = resolveUuaid(uuaid, {
314
+ listingStore,
315
+ lineageStore,
316
+ heartbeatStore: hbStore,
317
+ answeredBy,
318
+ now: nowVal,
319
+ });
320
+ if (!r.ok) {
321
+ if (r.status === 409) { sendNoStore(res, 409, { ok: false, reason: r.reason }); return true; } // @rule reg.http-resolve-409
322
+ if (r.reason === "bad-uuaid") { sendNoStore(res, 400, { ok: false, reason: r.reason }); return true; }
323
+ if (!checkResolveMissRateLimit(req)) {
324
+ sendNoStore(res, 429, { ok: false, reason: "rate-limited" });
325
+ return true;
326
+ }
327
+ if (federationCoordinator) {
328
+ const rawVisited = req.headers?.["x-pillar-visited"];
329
+ const visited = typeof rawVisited === "string" ? rawVisited.split(",").map((s) => s.trim()).filter(Boolean) : [];
330
+ const up = await federationCoordinator.resolveUpstream(uuaid, {
331
+ depth,
332
+ visited,
333
+ answeredBy,
334
+ now: nowVal,
335
+ localResolve: (q, ts) =>
336
+ resolveUuaid(q, {
337
+ listingStore,
338
+ lineageStore,
339
+ heartbeatStore: hbStore,
340
+ answeredBy,
341
+ now: ts,
342
+ }),
343
+ });
344
+ if (up.ok) {
345
+ const upOrig =
346
+ up.answer.evidence?.listing?.stamp &&
347
+ (up.stampRegistry ?? up.listingOrigin ?? federationCoordinator.store?.getListingOrigin?.(up.answer.lineage));
348
+ sendNoStore(res, 200, up.answer, {
349
+ extraHeaders: upOrig
350
+ ? { "x-pillar-stamp-registry": upOrig, "x-pillar-listing-origin": upOrig }
351
+ : null,
352
+ });
353
+ return true;
354
+ }
355
+ if (up.status === 400) {
356
+ sendNoStore(res, 400, { ok: false, reason: up.reason });
357
+ return true;
358
+ }
359
+ }
360
+ sendNoStore(res, 404, { ok: false, reason: r.reason });
361
+ return true;
362
+ }
363
+ const localOrig =
364
+ federationCoordinator && r.answer.evidence?.listing?.stamp
365
+ ? federationCoordinator.store?.getListingOrigin?.(r.answer.lineage) ?? null
366
+ : null;
367
+ sendNoStore(res, 200, r.answer, {
368
+ extraHeaders: localOrig
369
+ ? { "x-pillar-stamp-registry": localOrig, "x-pillar-listing-origin": localOrig }
370
+ : null,
371
+ });
372
+ return true;
373
+ }
374
+
375
+ return false;
376
+ }