@uuaid/pillar 2.6.0 → 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 (62) hide show
  1. package/HOSTING-LIMITS.md +12 -0
  2. package/README.md +201 -5
  3. package/bin/carrier.mjs +72 -2
  4. package/bin/pillar-ask.mjs +4 -1
  5. package/bin/pillar-blob.mjs +6 -0
  6. package/bin/pillar-directory.mjs +280 -16
  7. package/bin/pillar-escrow.mjs +4 -1
  8. package/bin/pillar-group-v2.mjs +6 -0
  9. package/bin/pillar-notify.mjs +13 -0
  10. package/bin/pillar-responder.mjs +266 -23
  11. package/bin/pillar.mjs +504 -255
  12. package/package.json +17 -25
  13. package/src/cli-help.mjs +23 -0
  14. package/src/group-v2-cli.mjs +1 -1
  15. package/src/identity/trust.mjs +72 -6
  16. package/src/mailbox/mailbox.mjs +308 -12
  17. package/src/net/carrier-server.mjs +34 -2
  18. package/src/net/refusals.mjs +2 -0
  19. package/src/net/seed-registry.mjs +2 -1
  20. package/src/net/transport.mjs +308 -92
  21. package/src/node-check.mjs +29 -8
  22. package/src/notify-events.mjs +148 -0
  23. package/src/notify-log.mjs +124 -0
  24. package/src/notify-runner.mjs +372 -0
  25. package/src/notify-store.mjs +725 -0
  26. package/src/notify.mjs +350 -0
  27. package/src/pillar.mjs +382 -27
  28. package/src/readers.mjs +2 -2
  29. package/src/registry/federation.mjs +3056 -0
  30. package/src/registry/gossip.mjs +294 -0
  31. package/src/registry/heartbeats.mjs +195 -0
  32. package/src/registry/lineage-verifier.mjs +972 -0
  33. package/src/registry/lineage.mjs +984 -0
  34. package/src/registry/listings.mjs +549 -0
  35. package/src/registry/resolve.mjs +376 -0
  36. package/src/registry/stats.mjs +508 -0
  37. package/src/sqlite-loader.mjs +40 -0
  38. package/src/vendor/pillar-core.mjs +22 -0
  39. package/src/vendor/uuaid-soul.mjs +2246 -0
  40. package/src/vnext/a6b-common.mjs +270 -0
  41. package/src/vnext/b1-cli.mjs +970 -0
  42. package/src/vnext/card-cli.mjs +370 -0
  43. package/src/vnext/greeter-operator.mjs +160 -0
  44. package/src/vnext/greeter-suggest.mjs +318 -0
  45. package/src/vnext/greeter.mjs +571 -0
  46. package/src/vnext/homes.mjs +48 -0
  47. package/src/vnext/init-cli.mjs +489 -0
  48. package/src/vnext/intro-cli.mjs +81 -0
  49. package/src/vnext/json-dir-mailbox.mjs +296 -0
  50. package/src/vnext/jsonl-mailbox.mjs +1066 -0
  51. package/src/vnext/knock-answer.mjs +162 -0
  52. package/src/vnext/knock-cli.mjs +520 -0
  53. package/src/vnext/knock-lane.mjs +312 -0
  54. package/src/vnext/knock-responder.mjs +344 -0
  55. package/src/vnext/lineage-cli.mjs +653 -0
  56. package/src/vnext/listing-cli.mjs +323 -0
  57. package/src/vnext/resolve-cli.mjs +86 -0
  58. package/src/vnext/soul-cli.mjs +666 -0
  59. package/src/vnext/verify-cli.mjs +251 -0
  60. package/src/vnext/w4-redirect.mjs +178 -0
  61. package/src/vnext/wave-a.mjs +193 -0
  62. package/src/vnext/witness-cli.mjs +547 -0
@@ -0,0 +1,323 @@
1
+ /**
2
+ * `pillar listing publish [--home DIR] [--registry URL] [--bits BITS] [--withdraw]
3
+ * [--carrier https://...] [--registry-home https://...] [--now ISO] [--json]`
4
+ *
5
+ * Wave A single-registry publishing:
6
+ * 1. Posts stored `<home>/lineage/log.json` events to `POST <registry>/v1/lineage`.
7
+ * 2. Checks whether a listing already exists on `<registry>`; when a first-listing
8
+ * W-10 PoW stamp is needed, fetches `GET <registry>/v1/health` for `registryUuaid`
9
+ * and mints `mintListingStamp(lineage, registryUuaid, day, bits)`.
10
+ * 3. Signs `pillar-listing/1` via `buildListing()` (`does: []` + `withdrawn: true` when `--withdraw`).
11
+ * 4. Posts `POST <registry>/v1/listings`.
12
+ */
13
+ import { existsSync, readFileSync } from "node:fs";
14
+ import { join } from "node:path";
15
+ import {
16
+ DEFAULT_CARD_CARRIER,
17
+ DEFAULT_CARD_HOME,
18
+ DEFAULT_RESOLVE_REGISTRY,
19
+ DEFAULT_STAMP_BITS,
20
+ MAX_STAMP_BITS,
21
+ MIN_STAMP_BITS,
22
+ buildCard,
23
+ buildListing,
24
+ mintListingStamp,
25
+ verifyLineage,
26
+ } from "../vendor/pillar-core.mjs";
27
+ import {
28
+ failCli,
29
+ isHttpsUrl,
30
+ isRegistryUrl,
31
+ isValidUuaid,
32
+ loadHomeConfig,
33
+ loadHomeIdentity,
34
+ loadHomeLineage,
35
+ parseDecimalInt,
36
+ parseStrictFlags,
37
+ resolveCarriers,
38
+ resolveHome,
39
+ } from "./a6b-common.mjs";
40
+ import { CARD_KEY, isStrictIsoDateTime, nowIsoSeconds } from "./wave-a.mjs";
41
+
42
+ function trimSlash(url) {
43
+ return url.replace(/\/+$/, "");
44
+ }
45
+
46
+ function nextMonotonicIso(baseIso, prevIso) {
47
+ const baseMs = Date.parse(baseIso);
48
+ const prevMs = Date.parse(prevIso);
49
+ if (!Number.isFinite(prevMs) || baseMs > prevMs) return baseIso;
50
+ return new Date(prevMs + 1000).toISOString().replace(/\.\d{3}Z$/, "Z");
51
+ }
52
+
53
+ function buildListingCard({
54
+ home,
55
+ homeId,
56
+ cfg,
57
+ lin,
58
+ optCarriers,
59
+ optHomes,
60
+ withdraw,
61
+ issuedAt,
62
+ }) {
63
+ const cardPath = join(home, CARD_KEY);
64
+ let stored = null;
65
+ if (existsSync(cardPath)) {
66
+ const parsed = JSON.parse(readFileSync(cardPath, "utf8"));
67
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed) && parsed.v === "pillar-card/1" && parsed.uuaid === homeId.uuaid) {
68
+ stored = parsed;
69
+ }
70
+ }
71
+
72
+ let lineage = homeId.uuaid;
73
+ let lineageHead;
74
+ if (lin.exists && lin.log) {
75
+ const v = verifyLineage(lin.log, { now: issuedAt });
76
+ if (v.verdict === "valid" && v.facts?.current === homeId.uuaid) {
77
+ lineage = lin.lineage ?? homeId.uuaid;
78
+ lineageHead = v.facts?.head ?? lin.head ?? undefined;
79
+ }
80
+ }
81
+
82
+ const cfgHttpsCarriers = resolveCarriers([], cfg).filter((c) => isHttpsUrl(c));
83
+ const carriers = optCarriers.length > 0
84
+ ? optCarriers
85
+ : (Array.isArray(stored?.carriers) && stored.carriers.length > 0
86
+ ? stored.carriers
87
+ : (cfgHttpsCarriers.length > 0 ? cfgHttpsCarriers : [DEFAULT_CARD_CARRIER]));
88
+
89
+ const homes = optHomes.length > 0
90
+ ? optHomes
91
+ : (Array.isArray(stored?.homes) && stored.homes.length > 0 ? stored.homes : [DEFAULT_CARD_HOME]);
92
+
93
+ const needsRebuild =
94
+ !stored ||
95
+ withdraw ||
96
+ optCarriers.length > 0 ||
97
+ optHomes.length > 0 ||
98
+ stored.lineage !== lineage ||
99
+ (lineageHead !== undefined && stored.lineageHead !== lineageHead);
100
+
101
+ if (!needsRebuild) {
102
+ return { card: stored, homes };
103
+ }
104
+
105
+ const card = buildCard(homeId.coreId, {
106
+ name: typeof stored?.name === "string" && stored.name.length > 0 ? stored.name : `pillar-${homeId.uuaid.slice(-12)}`,
107
+ ...(typeof stored?.about === "string" ? { about: stored.about } : {}),
108
+ does: withdraw ? [] : (Array.isArray(stored?.does) ? stored.does : []),
109
+ wants: Array.isArray(stored?.wants) ? stored.wants : [],
110
+ carriers,
111
+ homes,
112
+ ...(stored?.knock && typeof stored.knock === "object" ? { knock: stored.knock } : {}),
113
+ ...(Array.isArray(stored?.languages) ? { languages: stored.languages } : {}),
114
+ ...(Array.isArray(stored?.doors) ? { doors: stored.doors } : {}),
115
+ ...(Array.isArray(stored?.interfaces) ? { interfaces: stored.interfaces } : {}),
116
+ lineage,
117
+ ...(lineageHead ? { lineageHead } : {}),
118
+ updatedAt: issuedAt,
119
+ namespace: homeId.namespace,
120
+ });
121
+ return { card, homes };
122
+ }
123
+
124
+ /**
125
+ * Execute `pillar listing publish` with injectable streams and fetch.
126
+ * Returns `{ exitCode, listing?, response? }`.
127
+ */
128
+ export async function runListingCli(rawArgs, io = {}) {
129
+ const out = io.stdout ?? ((s) => process.stdout.write(s));
130
+ const errOut = io.stderr ?? ((s) => process.stderr.write(s));
131
+ const passphrase = io.passphrase ?? process.env.PILLAR_PASSPHRASE ?? "";
132
+ const fetchFn = io.fetch ?? globalThis.fetch;
133
+
134
+ const parsed = parseStrictFlags(rawArgs, {
135
+ boolFlags: new Set(["withdraw", "json"]),
136
+ valueFlags: new Set(["home", "registry", "bits", "now"]),
137
+ repeatValueFlags: new Set(["carrier", "registry-home"]),
138
+ cmdName: "listing",
139
+ });
140
+ if (!parsed.ok) return failCli(errOut, parsed.error, parsed.exitCode);
141
+
142
+ const { positionals, opts } = parsed;
143
+ const sub = positionals[0];
144
+ if (sub !== "publish") return failCli(errOut, "usage: pillar listing publish [--home DIR] [--registry URL] [--bits BITS] [--withdraw] [--carrier https://...] [--registry-home https://...] [--now ISO] [--json]", 64); // @rule listing-cli.subcommand-publish
145
+ if (positionals.length > 1) return failCli(errOut, `listing publish: unexpected argument "${positionals[1]}"`, 64); // @rule listing-cli.no-extra-args
146
+
147
+ const explicitNow = opts.now !== undefined;
148
+ const rawNow = explicitNow ? opts.now : nowIsoSeconds();
149
+ if (!isStrictIsoDateTime(rawNow)) return failCli(errOut, "listing publish: --now must be a valid ISO-8601 timestamp", 64); // @rule listing-cli.now-valid
150
+
151
+ let bits = DEFAULT_STAMP_BITS;
152
+ if (opts.bits !== undefined) {
153
+ const b = parseDecimalInt(opts.bits, { min: MIN_STAMP_BITS, max: MAX_STAMP_BITS, label: "listing publish: --bits" });
154
+ if (!b.ok) return failCli(errOut, b.error, 64);
155
+ bits = b.value;
156
+ }
157
+
158
+ for (const c of opts.carrier) {
159
+ if (!isHttpsUrl(c)) return failCli(errOut, `listing publish: --carrier must be a valid https:// URI, got "${c}"`, 64); // @rule listing-cli.carrier-https
160
+ }
161
+ for (const h of opts["registry-home"]) {
162
+ if (!isHttpsUrl(h)) return failCli(errOut, `listing publish: --registry-home must be a valid https:// URI, got "${h}"`, 64); // @rule listing-cli.registry-home-https
163
+ }
164
+ if (opts["registry-home"].length > 4) return failCli(errOut, "listing publish: at most 4 --registry-home URIs are allowed", 64); // @rule listing-cli.registry-home-max
165
+
166
+ const home = resolveHome(opts);
167
+ let cfg;
168
+ try { cfg = loadHomeConfig(home); } catch (e) {
169
+ return failCli(errOut, `listing publish: config.json invalid (${e.message})`, 1);
170
+ }
171
+
172
+ const registryUrl = trimSlash(opts.registry ?? cfg.registry ?? DEFAULT_RESOLVE_REGISTRY);
173
+ if (!isRegistryUrl(registryUrl)) return failCli(errOut, `listing publish: --registry must be an https:// (or loopback http://) URL, got "${registryUrl}"`, 64); // @rule listing-cli.registry-url
174
+
175
+ let homeId;
176
+ try { homeId = loadHomeIdentity(home, passphrase); } catch (e) {
177
+ return failCli(errOut, `listing publish: ${e.message}`, 1);
178
+ }
179
+ let lin;
180
+ try { lin = loadHomeLineage(home); } catch (e) {
181
+ return failCli(errOut, `listing publish: ${e.message}`, 1);
182
+ }
183
+
184
+ // 1. Post stored lineage events (if present) so registry has current key proof.
185
+ let lineagePosted = 0;
186
+ if (lin.exists && Array.isArray(lin.log)) {
187
+ for (const ev of lin.log) {
188
+ let res;
189
+ try {
190
+ res = await fetchFn(`${registryUrl}/v1/lineage`, {
191
+ method: "POST",
192
+ headers: { "content-type": "application/json", accept: "application/json" },
193
+ body: JSON.stringify(ev),
194
+ });
195
+ } catch (e) {
196
+ return failCli(errOut, `listing publish: POST /v1/lineage network error (${e.message})`, 1);
197
+ }
198
+ if (!res.ok) {
199
+ const body = await res.json().catch(() => ({}));
200
+ return failCli(errOut, `listing publish: POST /v1/lineage failed (${res.status}: ${body?.reason ?? "error"})`, 1); // @rule listing-cli.lineage-post-ok
201
+ }
202
+ lineagePosted++;
203
+ }
204
+ }
205
+
206
+ const lineageId = lin.lineage ?? homeId.uuaid;
207
+
208
+ // 2. Check whether this lineage already has a listing on the registry.
209
+ let existingListing = null;
210
+ try {
211
+ const probeRes = await fetchFn(`${registryUrl}/v1/resolve/${encodeURIComponent(lineageId)}`, {
212
+ method: "GET",
213
+ headers: { accept: "application/json" },
214
+ });
215
+ if (probeRes.ok) {
216
+ const probeBody = await probeRes.json().catch(() => null);
217
+ if (probeBody?.evidence?.listing && typeof probeBody.evidence.listing === "object") {
218
+ existingListing = probeBody.evidence.listing;
219
+ }
220
+ }
221
+ } catch { /* fall through to first-listing stamp path */ }
222
+
223
+ const issuedAt = !explicitNow && existingListing?.issuedAt
224
+ ? nextMonotonicIso(rawNow, existingListing.issuedAt)
225
+ : rawNow;
226
+
227
+ const optHomes = opts["registry-home"].length > 0
228
+ ? opts["registry-home"]
229
+ : (isHttpsUrl(registryUrl) ? [registryUrl] : []);
230
+
231
+ let card;
232
+ let listingHomes;
233
+ try {
234
+ const built = buildListingCard({
235
+ home,
236
+ homeId,
237
+ cfg,
238
+ lin,
239
+ optCarriers: opts.carrier,
240
+ optHomes,
241
+ withdraw: opts.withdraw,
242
+ issuedAt,
243
+ });
244
+ card = built.card;
245
+ listingHomes = built.homes;
246
+ } catch (e) {
247
+ return failCli(errOut, `listing publish: ${e.message}`, 1);
248
+ }
249
+
250
+ // 3. Mint W-10 stamp via GET /v1/health when this is a first listing.
251
+ let stamp;
252
+ let registryUuaid = null;
253
+ if (!existingListing) {
254
+ let healthRes;
255
+ try {
256
+ healthRes = await fetchFn(`${registryUrl}/v1/health`, {
257
+ method: "GET",
258
+ headers: { accept: "application/json" },
259
+ });
260
+ } catch (e) {
261
+ return failCli(errOut, `listing publish: GET /v1/health network error (${e.message})`, 1);
262
+ }
263
+ if (!healthRes.ok) return failCli(errOut, `listing publish: GET /v1/health failed (${healthRes.status})`, 1); // @rule listing-cli.health-ok
264
+ const health = await healthRes.json().catch(() => null);
265
+ if (!isValidUuaid(health?.uuaid)) return failCli(errOut, "listing publish: /v1/health did not return a valid registry uuaid", 1); // @rule listing-cli.health-uuaid
266
+ registryUuaid = health.uuaid;
267
+ stamp = mintListingStamp(card.lineage, registryUuaid, issuedAt.slice(0, 10), bits);
268
+ }
269
+
270
+ const listing = buildListing(homeId.coreId, {
271
+ card,
272
+ homes: listingHomes,
273
+ issuedAt,
274
+ ...(stamp !== undefined ? { stamp } : {}),
275
+ ...(opts.withdraw ? { withdrawn: true } : {}),
276
+ namespace: homeId.namespace,
277
+ });
278
+
279
+ // 4. POST /v1/listings
280
+ let postRes;
281
+ try {
282
+ postRes = await fetchFn(`${registryUrl}/v1/listings`, {
283
+ method: "POST",
284
+ headers: { "content-type": "application/json", accept: "application/json" },
285
+ body: JSON.stringify(listing),
286
+ });
287
+ } catch (e) {
288
+ return failCli(errOut, `listing publish: POST /v1/listings network error (${e.message})`, 1);
289
+ }
290
+ const postBody = await postRes.json().catch(() => ({}));
291
+ if (!postRes.ok || postBody?.ok === false) {
292
+ return failCli(errOut, `listing publish: POST /v1/listings failed (${postRes.status}: ${postBody?.reason ?? "error"})`, 1); // @rule listing-cli.post-ok
293
+ }
294
+
295
+ const result = {
296
+ ok: true,
297
+ registry: registryUrl,
298
+ lineage: listing.lineage,
299
+ uuaid: listing.uuaid,
300
+ issuedAt: listing.issuedAt,
301
+ lineagePosted,
302
+ stamped: stamp !== undefined,
303
+ withdrawn: opts.withdraw,
304
+ ...(registryUuaid ? { registryUuaid } : {}),
305
+ listing,
306
+ response: postBody,
307
+ };
308
+
309
+ if (opts.json) {
310
+ out(`${JSON.stringify(result)}\n`);
311
+ } else {
312
+ out([
313
+ `${opts.withdraw ? "withdrew" : "published"} listing for ${listing.lineage}`,
314
+ ` uuaid: ${listing.uuaid}`,
315
+ ` registry: ${registryUrl}`,
316
+ ` issuedAt: ${listing.issuedAt}`,
317
+ ` lineage: ${lineagePosted} event(s) synced`,
318
+ ` stamp: ${stamp ? `pow (${stamp.bits} bits)` : "update (none required)"}`,
319
+ "",
320
+ ].join("\n"));
321
+ }
322
+ return { exitCode: 0, result, listing, response: postBody };
323
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * `pillar resolve <uuaid> [--registry URL] [--now ISO] [--home DIR] [--json]`
3
+ *
4
+ * Calls core `resolve()` (fail-closed cryptographic verification of listing,
5
+ * card, and lineage). Exits:
6
+ * 0 on `ok: true`
7
+ * 1 on resolution or verification failure
8
+ * 64 on usage error
9
+ */
10
+ import { DEFAULT_RESOLVE_REGISTRY, resolve } from "../vendor/pillar-core.mjs";
11
+ import {
12
+ failCli,
13
+ isRegistryUrl,
14
+ isValidUuaid,
15
+ loadHomeConfig,
16
+ parseStrictFlags,
17
+ resolveHome,
18
+ } from "./a6b-common.mjs";
19
+ import { isStrictIsoDateTime, nowIsoSeconds } from "./wave-a.mjs";
20
+
21
+ function formatHumanResolve(res) {
22
+ const lines = [
23
+ `query: ${res.answer?.query ?? res.lineage}`,
24
+ `lineage: ${res.lineage}`,
25
+ `current: ${res.current}`,
26
+ `status: ${res.status}`,
27
+ ];
28
+ if (res.card?.publicKey) lines.push(`publicKey: ${res.card.publicKey}`);
29
+ if (res.card?.name) lines.push(`name: ${res.card.name}`);
30
+ if (Array.isArray(res.card?.carriers)) lines.push(`carriers: ${res.card.carriers.join(", ")}`);
31
+ if (res.answer?.answeredBy) lines.push(`answeredBy: ${res.answer.answeredBy}`);
32
+ return `${lines.join("\n")}\n`;
33
+ }
34
+
35
+ /**
36
+ * Execute `pillar resolve` with injectable streams and fetch.
37
+ * Returns `{ exitCode, result? }`.
38
+ */
39
+ export async function runResolveCli(rawArgs, io = {}) {
40
+ const out = io.stdout ?? ((s) => process.stdout.write(s));
41
+ const errOut = io.stderr ?? ((s) => process.stderr.write(s));
42
+
43
+ const parsed = parseStrictFlags(rawArgs, {
44
+ boolFlags: new Set(["json"]),
45
+ valueFlags: new Set(["home", "registry", "now"]),
46
+ cmdName: "resolve",
47
+ });
48
+ if (!parsed.ok) return failCli(errOut, parsed.error, parsed.exitCode);
49
+
50
+ const { positionals, opts } = parsed;
51
+ if (positionals.length !== 1) return failCli(errOut, "usage: pillar resolve <uuaid> [--registry URL] [--now ISO] [--json]", 64); // @rule resolve-cli.arg-count
52
+
53
+ const uuaid = positionals[0];
54
+ if (!isValidUuaid(uuaid)) return failCli(errOut, `resolve: invalid UUAID "${uuaid}"`, 64); // @rule resolve-cli.uuaid-valid
55
+
56
+ const ts = opts.now !== undefined ? opts.now : nowIsoSeconds();
57
+ if (!isStrictIsoDateTime(ts)) return failCli(errOut, "resolve: --now must be a valid ISO-8601 timestamp", 64); // @rule resolve-cli.now-valid
58
+
59
+ let cfg = {};
60
+ try { cfg = loadHomeConfig(resolveHome(opts)); } catch { cfg = {}; }
61
+ const registryUrl = opts.registry ?? cfg.registry ?? DEFAULT_RESOLVE_REGISTRY;
62
+ if (!isRegistryUrl(registryUrl)) return failCli(errOut, `resolve: --registry must be an https:// (or loopback http://) URL, got "${registryUrl}"`, 64); // @rule resolve-cli.registry-url
63
+
64
+ let result;
65
+ try {
66
+ result = await resolve(uuaid, {
67
+ registryUrl,
68
+ now: ts,
69
+ ...(io.fetch ? { fetch: io.fetch } : {}),
70
+ });
71
+ } catch (e) {
72
+ const msg = e instanceof Error ? e.message : String(e);
73
+ if (opts.json) out(`${JSON.stringify({ ok: false, reason: "resolve-error", message: msg })}\n`);
74
+ else errOut(`resolve: ${msg}\n`);
75
+ return { exitCode: 1, result: { ok: false, reason: "resolve-error" } };
76
+ }
77
+
78
+ if (!result || result.ok !== true) {
79
+ if (opts.json) out(`${JSON.stringify(result)}\n`);
80
+ else errOut(`resolve: ${result?.reason ?? "failed"}${result?.statusCode ? ` (HTTP ${result.statusCode})` : ""}\n`);
81
+ return { exitCode: 1, result }; // @rule resolve-cli.fail-closed-exit-1
82
+ }
83
+
84
+ out(opts.json ? `${JSON.stringify(result)}\n` : formatHumanResolve(result));
85
+ return { exitCode: 0, result };
86
+ }