@uuaid/pillar 2.6.1 → 2.7.1
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 +325 -7
- package/bin/carrier.mjs +21 -0
- package/bin/pillar-ask.mjs +4 -1
- package/bin/pillar-blob.mjs +6 -0
- package/bin/pillar-directory.mjs +282 -16
- package/bin/pillar-escrow.mjs +4 -1
- package/bin/pillar-group-v2.mjs +6 -0
- package/bin/pillar-notify.mjs +13 -0
- package/bin/pillar-responder.mjs +266 -23
- package/bin/pillar.mjs +512 -257
- package/package.json +17 -25
- package/src/cli-help.mjs +23 -0
- package/src/group-v2-cli.mjs +1 -1
- package/src/identity/trust.mjs +72 -6
- package/src/mailbox/mailbox.mjs +313 -12
- package/src/net/carrier-server.mjs +34 -2
- package/src/net/refusals.mjs +2 -0
- package/src/net/seed-registry.mjs +2 -1
- package/src/net/transport.mjs +320 -91
- package/src/node-check.mjs +29 -8
- package/src/notify-events.mjs +148 -0
- package/src/notify-log.mjs +124 -0
- package/src/notify-runner.mjs +372 -0
- package/src/notify-store.mjs +736 -0
- package/src/notify.mjs +350 -0
- package/src/pillar.mjs +382 -27
- package/src/readers.mjs +2 -2
- package/src/registry/federation.mjs +3056 -0
- package/src/registry/gossip.mjs +294 -0
- package/src/registry/heartbeats.mjs +205 -0
- package/src/registry/lineage-verifier.mjs +972 -0
- package/src/registry/lineage.mjs +984 -0
- package/src/registry/listings.mjs +612 -0
- package/src/registry/resolve.mjs +376 -0
- package/src/registry/stats.mjs +508 -0
- package/src/sqlite-loader.mjs +40 -0
- package/src/vendor/pillar-core.mjs +22 -0
- package/src/vendor/uuaid-soul.mjs +2246 -0
- package/src/vnext/a6b-common.mjs +270 -0
- package/src/vnext/b1-cli.mjs +970 -0
- package/src/vnext/card-cli.mjs +370 -0
- package/src/vnext/greeter-operator.mjs +160 -0
- package/src/vnext/greeter-suggest.mjs +318 -0
- package/src/vnext/greeter.mjs +571 -0
- package/src/vnext/homes.mjs +48 -0
- package/src/vnext/init-cli.mjs +489 -0
- package/src/vnext/intro-cli.mjs +81 -0
- package/src/vnext/json-dir-mailbox.mjs +304 -0
- package/src/vnext/jsonl-mailbox.mjs +1132 -0
- package/src/vnext/knock-answer.mjs +162 -0
- package/src/vnext/knock-cli.mjs +520 -0
- package/src/vnext/knock-lane.mjs +312 -0
- package/src/vnext/knock-responder.mjs +344 -0
- package/src/vnext/lineage-cli.mjs +653 -0
- package/src/vnext/listing-cli.mjs +323 -0
- package/src/vnext/resolve-cli.mjs +104 -0
- package/src/vnext/soul-cli.mjs +666 -0
- package/src/vnext/verify-cli.mjs +333 -0
- package/src/vnext/w4-redirect.mjs +178 -0
- package/src/vnext/wave-a.mjs +193 -0
- package/src/vnext/witness-cli.mjs +547 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `pillar card [show|build|serve]` — Pillar vNext §3 card management and R4 serving.
|
|
3
|
+
*
|
|
4
|
+
* Decision R4:
|
|
5
|
+
* `/.well-known/agent-card.json` serves `toA2AAgentCard(card)` filtered so
|
|
6
|
+
* `supportedInterfaces` retains ONLY `https://` URLs. `toA2AAgentCard` itself
|
|
7
|
+
* in the vendored core is left untouched.
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
10
|
+
import { createServer } from "node:http";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import {
|
|
13
|
+
DEFAULT_CARD_CARRIER,
|
|
14
|
+
DEFAULT_CARD_HOME,
|
|
15
|
+
DEFAULT_STAMP_BITS,
|
|
16
|
+
MAX_STAMP_BITS,
|
|
17
|
+
MIN_STAMP_BITS,
|
|
18
|
+
buildCard,
|
|
19
|
+
toA2AAgentCard,
|
|
20
|
+
verifyCard,
|
|
21
|
+
verifyLineage,
|
|
22
|
+
} from "../vendor/pillar-core.mjs";
|
|
23
|
+
import {
|
|
24
|
+
INTENT_TAG_RE,
|
|
25
|
+
failCli,
|
|
26
|
+
isHttpsUrl,
|
|
27
|
+
loadHomeConfig,
|
|
28
|
+
loadHomeIdentity,
|
|
29
|
+
loadHomeLineage,
|
|
30
|
+
parseDecimalInt,
|
|
31
|
+
parseStrictFlags,
|
|
32
|
+
resolveCarriers,
|
|
33
|
+
resolveHome,
|
|
34
|
+
} from "./a6b-common.mjs";
|
|
35
|
+
import { MAX_KNOCK_MAX_BYTES, MIN_KNOCK_MAX_BYTES } from "./knock-lane.mjs";
|
|
36
|
+
import { loadHomeSoulCard } from "./soul-cli.mjs";
|
|
37
|
+
import { CARD_KEY, isStrictIsoDateTime, nowIsoSeconds, writeFileAtomic } from "./wave-a.mjs";
|
|
38
|
+
|
|
39
|
+
const CARD_SUBCOMMANDS = new Set(["show", "build", "serve"]);
|
|
40
|
+
const DEFAULT_SERVE_HOST = "127.0.0.1";
|
|
41
|
+
const DEFAULT_SERVE_PORT = 8788;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Project a `pillar-card/1` document to an A2A AgentCard for
|
|
45
|
+
* `/.well-known/agent-card.json` per Decision R4: keep ONLY `https://`
|
|
46
|
+
* interfaces in `supportedInterfaces` while leaving `toA2AAgentCard` unchanged.
|
|
47
|
+
*/
|
|
48
|
+
export function filterA2ACardForWellKnown(card) {
|
|
49
|
+
const a2a = toA2AAgentCard(card);
|
|
50
|
+
if (!a2a) return null;
|
|
51
|
+
const filtered = [];
|
|
52
|
+
for (const iface of a2a.supportedInterfaces) {
|
|
53
|
+
if (!isHttpsUrl(iface?.url)) continue; // @rule card-cli.r4-https-interfaces-only
|
|
54
|
+
filtered.push(iface);
|
|
55
|
+
}
|
|
56
|
+
return { ...a2a, supportedInterfaces: filtered };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function sendJson(res, statusCode, bodyObj, cacheControl = "public, max-age=60") {
|
|
60
|
+
const body = `${JSON.stringify(bodyObj, null, 2)}\n`;
|
|
61
|
+
res.writeHead(statusCode, {
|
|
62
|
+
"content-type": "application/json; charset=utf-8",
|
|
63
|
+
"content-length": Buffer.byteLength(body),
|
|
64
|
+
"access-control-allow-origin": "*",
|
|
65
|
+
"cache-control": cacheControl,
|
|
66
|
+
});
|
|
67
|
+
res.end(body);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Create the HTTP server for `pillar card serve`.
|
|
72
|
+
* Serves `/.well-known/pillar-card.json`, `/.well-known/agent-card.json` (R4),
|
|
73
|
+
* and `/.well-known/soul-card.json` when a SOUL-1 card is present.
|
|
74
|
+
*/
|
|
75
|
+
export function createCardServer(card, { soulCard = null } = {}) {
|
|
76
|
+
return createServer((req, res) => {
|
|
77
|
+
if (req.method !== "GET") { sendJson(res, 405, { error: "method-not-allowed" }, "no-store"); return; } // @rule card-cli.serve-get-only
|
|
78
|
+
const url = new URL(req.url ?? "/", `http://${req.headers.host ?? "127.0.0.1"}`);
|
|
79
|
+
if (url.pathname === "/.well-known/pillar-card.json") {
|
|
80
|
+
sendJson(res, 200, card);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (url.pathname === "/.well-known/agent-card.json") {
|
|
84
|
+
const a2a = filterA2ACardForWellKnown(card);
|
|
85
|
+
if (!a2a) { sendJson(res, 404, { error: "a2a-card-unavailable" }, "no-store"); return; } // @rule card-cli.serve-a2a-404
|
|
86
|
+
sendJson(res, 200, a2a);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (url.pathname === "/.well-known/soul-card.json" && soulCard) {
|
|
90
|
+
sendJson(res, 200, soulCard);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
sendJson(res, 404, { error: "not-found" }, "no-store");
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function parseCapabilityEntry(raw, flagName) {
|
|
98
|
+
const colon = raw.indexOf(":");
|
|
99
|
+
const tag = colon === -1 ? raw : raw.slice(0, colon);
|
|
100
|
+
const summary = colon === -1 ? undefined : raw.slice(colon + 1);
|
|
101
|
+
if (!INTENT_TAG_RE.test(tag)) { return { ok: false, error: `card build: ${flagName} tag "${tag}" must match ${INTENT_TAG_RE}` }; } // @rule card-cli.cap-tag-valid
|
|
102
|
+
if (summary !== undefined && [...summary].length > 280) { return { ok: false, error: `card build: ${flagName} summary exceeds 280 code points` }; } // @rule card-cli.cap-summary-len
|
|
103
|
+
return { ok: true, value: summary !== undefined ? { tag, summary } : { tag } };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function formatHumanCard(card) {
|
|
107
|
+
const lines = [
|
|
108
|
+
`uuaid: ${card.uuaid}`,
|
|
109
|
+
`lineage: ${card.lineage}`,
|
|
110
|
+
`name: ${card.name}`,
|
|
111
|
+
];
|
|
112
|
+
if (card.about) lines.push(`about: ${card.about}`);
|
|
113
|
+
lines.push(`does: ${card.does.length ? card.does.map((d) => (d.summary ? `${d.tag ?? d.action}:${d.summary}` : (d.tag ?? d.action))).join(", ") : "(none)"}`);
|
|
114
|
+
lines.push(`wants: ${card.wants.length ? card.wants.map((w) => (w.summary ? `${w.tag ?? w.action}:${w.summary}` : (w.tag ?? w.action))).join(", ") : "(none)"}`);
|
|
115
|
+
lines.push(`carriers: ${card.carriers.join(", ")}`);
|
|
116
|
+
lines.push(`knock: pow=${card.knock.pow}${card.knock.introductions !== undefined ? ` introductions=${card.knock.introductions}` : ""}${card.knock.maxBytes !== undefined ? ` maxBytes=${card.knock.maxBytes}` : ""}`);
|
|
117
|
+
lines.push(`updatedAt: ${card.updatedAt}`);
|
|
118
|
+
return `${lines.join("\n")}\n`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function formatHostForUrl(host) {
|
|
122
|
+
return host.includes(":") && !host.startsWith("[") ? `[${host}]` : host;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Execute `pillar card [show|build|serve]` with injectable streams.
|
|
127
|
+
* Returns `{ exitCode, card?, server? }`.
|
|
128
|
+
*/
|
|
129
|
+
export async function runCardCli(rawArgs, io = {}) {
|
|
130
|
+
const out = io.stdout ?? ((s) => process.stdout.write(s));
|
|
131
|
+
const errOut = io.stderr ?? ((s) => process.stderr.write(s));
|
|
132
|
+
const passphrase = io.passphrase ?? process.env.PILLAR_PASSPHRASE ?? "";
|
|
133
|
+
|
|
134
|
+
const parsed = parseStrictFlags(rawArgs, {
|
|
135
|
+
boolFlags: new Set(["a2a", "json"]),
|
|
136
|
+
valueFlags: new Set(["home", "name", "summary", "pow", "introductions", "max-bytes", "host", "port", "now"]),
|
|
137
|
+
repeatValueFlags: new Set(["does", "wants", "carrier"]),
|
|
138
|
+
cmdName: "card",
|
|
139
|
+
});
|
|
140
|
+
if (!parsed.ok) return failCli(errOut, parsed.error, parsed.exitCode);
|
|
141
|
+
|
|
142
|
+
const { positionals, opts } = parsed;
|
|
143
|
+
const sub = positionals[0] ?? "show";
|
|
144
|
+
if (!CARD_SUBCOMMANDS.has(sub)) return failCli(errOut, `card: unknown subcommand "${sub}" (expected show, build, or serve)`, 64); // @rule card-cli.subcommand-known
|
|
145
|
+
if (positionals.length > 1) return failCli(errOut, `card ${sub}: unexpected argument "${positionals[1]}"`, 64); // @rule card-cli.no-extra-args
|
|
146
|
+
|
|
147
|
+
const home = resolveHome(opts);
|
|
148
|
+
const cardPath = join(home, CARD_KEY);
|
|
149
|
+
|
|
150
|
+
if (sub === "show") {
|
|
151
|
+
if (!existsSync(cardPath)) return failCli(errOut, `card show: no card found at ${cardPath}; run: pillar card build --name <name>`, 1); // @rule card-cli.show-exists
|
|
152
|
+
let card;
|
|
153
|
+
try { card = JSON.parse(readFileSync(cardPath, "utf8")); } catch { card = null; }
|
|
154
|
+
if (!card || typeof card !== "object" || Array.isArray(card) || card.v !== "pillar-card/1") return failCli(errOut, `card show: ${cardPath} is not a valid pillar-card/1 JSON document`, 1); // @rule card-cli.show-valid-card
|
|
155
|
+
if (opts.a2a) {
|
|
156
|
+
const a2a = toA2AAgentCard(card);
|
|
157
|
+
if (!a2a) return failCli(errOut, "card show --a2a: card must list at least one --does entry for A2A projection", 1); // @rule card-cli.show-a2a-available
|
|
158
|
+
out(`${JSON.stringify(a2a, null, opts.json ? 0 : 2)}\n`);
|
|
159
|
+
return { exitCode: 0, card: a2a };
|
|
160
|
+
}
|
|
161
|
+
out(opts.json ? `${JSON.stringify(card)}\n` : formatHumanCard(card));
|
|
162
|
+
return { exitCode: 0, card };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (sub === "build") {
|
|
166
|
+
const ts = opts.now !== undefined ? opts.now : nowIsoSeconds();
|
|
167
|
+
if (!isStrictIsoDateTime(ts)) return failCli(errOut, "card build: --now must be a valid ISO-8601 timestamp", 64); // @rule card-cli.build-now-valid
|
|
168
|
+
|
|
169
|
+
let powBits;
|
|
170
|
+
if (opts.pow !== undefined) {
|
|
171
|
+
const p = parseDecimalInt(opts.pow, { min: MIN_STAMP_BITS, max: MAX_STAMP_BITS, label: "card build: --pow" });
|
|
172
|
+
if (!p.ok) return failCli(errOut, p.error, 64);
|
|
173
|
+
powBits = p.value;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
let introductions;
|
|
177
|
+
if (opts.introductions !== undefined) {
|
|
178
|
+
if (opts.introductions !== "true" && opts.introductions !== "false") return failCli(errOut, 'card build: --introductions must be "true" or "false"', 64); // @rule card-cli.build-introductions-bool
|
|
179
|
+
introductions = opts.introductions === "true";
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
let maxBytes;
|
|
183
|
+
if (opts["max-bytes"] !== undefined) {
|
|
184
|
+
const mb = parseDecimalInt(opts["max-bytes"], { min: MIN_KNOCK_MAX_BYTES, max: MAX_KNOCK_MAX_BYTES, label: "card build: --max-bytes" });
|
|
185
|
+
if (!mb.ok) return failCli(errOut, mb.error, 64);
|
|
186
|
+
maxBytes = mb.value;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
for (const c of opts.carrier) {
|
|
190
|
+
if (!isHttpsUrl(c)) return failCli(errOut, `card build: --carrier must be a valid https:// URI, got "${c}"`, 64); // @rule card-cli.build-carrier-https
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const doesParsed = [];
|
|
194
|
+
for (const raw of opts.does) {
|
|
195
|
+
const r = parseCapabilityEntry(raw, "--does");
|
|
196
|
+
if (!r.ok) return failCli(errOut, r.error, 64);
|
|
197
|
+
doesParsed.push(r.value);
|
|
198
|
+
}
|
|
199
|
+
const wantsParsed = [];
|
|
200
|
+
for (const raw of opts.wants) {
|
|
201
|
+
const r = parseCapabilityEntry(raw, "--wants");
|
|
202
|
+
if (!r.ok) return failCli(errOut, r.error, 64);
|
|
203
|
+
wantsParsed.push(r.value);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
let homeId;
|
|
207
|
+
try { homeId = loadHomeIdentity(home, passphrase); } catch (e) {
|
|
208
|
+
return failCli(errOut, `card build: ${e.message}`, 1);
|
|
209
|
+
}
|
|
210
|
+
let cfg;
|
|
211
|
+
try { cfg = loadHomeConfig(home); } catch (e) {
|
|
212
|
+
return failCli(errOut, `card build: config.json invalid (${e.message})`, 1);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
let existing = null;
|
|
216
|
+
if (existsSync(cardPath)) {
|
|
217
|
+
try {
|
|
218
|
+
const prev = JSON.parse(readFileSync(cardPath, "utf8"));
|
|
219
|
+
if (prev && typeof prev === "object" && !Array.isArray(prev) && prev.v === "pillar-card/1") existing = prev;
|
|
220
|
+
} catch { /* rebuild cleanly */ }
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const name = opts.name !== undefined ? opts.name : existing?.name;
|
|
224
|
+
if (typeof name !== "string" || [...name].length < 1 || [...name].length > 64) return failCli(errOut, "card build: --name is required (1..64 code points)", 64); // @rule card-cli.build-name-required
|
|
225
|
+
|
|
226
|
+
const about = opts.summary !== undefined ? opts.summary : existing?.about;
|
|
227
|
+
if (about !== undefined && [...about].length > 500) return failCli(errOut, "card build: --summary must be <= 500 code points", 64); // @rule card-cli.build-summary-len
|
|
228
|
+
|
|
229
|
+
const does = opts.does.length > 0 ? doesParsed : (Array.isArray(existing?.does) ? existing.does : []);
|
|
230
|
+
const wants = opts.wants.length > 0 ? wantsParsed : (Array.isArray(existing?.wants) ? existing.wants : []);
|
|
231
|
+
|
|
232
|
+
const cfgHttpsCarriers = resolveCarriers([], cfg).filter((c) => isHttpsUrl(c));
|
|
233
|
+
const carriers = opts.carrier.length > 0
|
|
234
|
+
? opts.carrier
|
|
235
|
+
: (Array.isArray(existing?.carriers) && existing.carriers.length > 0
|
|
236
|
+
? existing.carriers
|
|
237
|
+
: (cfgHttpsCarriers.length > 0 ? cfgHttpsCarriers : [DEFAULT_CARD_CARRIER]));
|
|
238
|
+
|
|
239
|
+
const homes = Array.isArray(existing?.homes) && existing.homes.length > 0 ? existing.homes : [DEFAULT_CARD_HOME];
|
|
240
|
+
|
|
241
|
+
const prevKnock = existing?.knock && typeof existing.knock === "object" ? existing.knock : {};
|
|
242
|
+
const knock = {
|
|
243
|
+
pow: powBits ?? (Number.isInteger(prevKnock.pow) ? prevKnock.pow : DEFAULT_STAMP_BITS),
|
|
244
|
+
...(introductions !== undefined
|
|
245
|
+
? { introductions }
|
|
246
|
+
: (typeof prevKnock.introductions === "boolean" ? { introductions: prevKnock.introductions } : {})),
|
|
247
|
+
...(maxBytes !== undefined
|
|
248
|
+
? { maxBytes }
|
|
249
|
+
: (Number.isInteger(prevKnock.maxBytes) ? { maxBytes: prevKnock.maxBytes } : {})),
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
let lineage = homeId.uuaid;
|
|
253
|
+
let lineageHead;
|
|
254
|
+
let linLog = null;
|
|
255
|
+
try {
|
|
256
|
+
const lin = loadHomeLineage(home);
|
|
257
|
+
if (lin.exists && lin.log) {
|
|
258
|
+
const v = verifyLineage(lin.log, { now: ts });
|
|
259
|
+
if (v.verdict === "valid" && v.facts?.current === homeId.uuaid) {
|
|
260
|
+
lineage = lin.lineage ?? homeId.uuaid;
|
|
261
|
+
lineageHead = v.facts?.head ?? lin.head ?? undefined;
|
|
262
|
+
linLog = lin.log;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
} catch (e) {
|
|
266
|
+
return failCli(errOut, `card build: ${e.message}`, 1);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
let card;
|
|
270
|
+
try {
|
|
271
|
+
card = buildCard(homeId.coreId, {
|
|
272
|
+
name,
|
|
273
|
+
...(about !== undefined ? { about } : {}),
|
|
274
|
+
does,
|
|
275
|
+
wants,
|
|
276
|
+
carriers,
|
|
277
|
+
homes,
|
|
278
|
+
knock,
|
|
279
|
+
...(Array.isArray(existing?.languages) ? { languages: existing.languages } : {}),
|
|
280
|
+
...(Array.isArray(existing?.doors) ? { doors: existing.doors } : {}),
|
|
281
|
+
...(Array.isArray(existing?.interfaces) ? { interfaces: existing.interfaces } : {}),
|
|
282
|
+
lineage,
|
|
283
|
+
...(lineageHead ? { lineageHead } : {}),
|
|
284
|
+
updatedAt: ts,
|
|
285
|
+
namespace: homeId.namespace,
|
|
286
|
+
});
|
|
287
|
+
} catch (e) {
|
|
288
|
+
return failCli(errOut, `card build: ${e.message}`, 64);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const v = verifyCard(card, { now: ts, ...(linLog ? { lineage: linLog } : {}) });
|
|
292
|
+
if (v.verdict === "invalid") {
|
|
293
|
+
return failCli(errOut, `card build: verification failed (${v.reasons.join(", ")})`, 1);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
writeFileAtomic(cardPath, `${JSON.stringify(card, null, 2)}\n`, 0o600);
|
|
297
|
+
if (opts.a2a) {
|
|
298
|
+
const a2a = toA2AAgentCard(card);
|
|
299
|
+
out(`${JSON.stringify(a2a)}\n`);
|
|
300
|
+
return { exitCode: 0, card };
|
|
301
|
+
}
|
|
302
|
+
out(opts.json ? `${JSON.stringify(card)}\n` : formatHumanCard(card));
|
|
303
|
+
return { exitCode: 0, card };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// sub === "serve"
|
|
307
|
+
const host = opts.host ?? DEFAULT_SERVE_HOST;
|
|
308
|
+
if (typeof host !== "string" || host.length === 0 || /\s/.test(host)) return failCli(errOut, `card serve: invalid --host "${host}"`, 64); // @rule card-cli.serve-host-valid
|
|
309
|
+
|
|
310
|
+
let port = DEFAULT_SERVE_PORT;
|
|
311
|
+
if (opts.port !== undefined) {
|
|
312
|
+
const p = parseDecimalInt(opts.port, { min: 0, max: 65535, label: "card serve: --port" });
|
|
313
|
+
if (!p.ok) return failCli(errOut, p.error, 64);
|
|
314
|
+
port = p.value;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (!existsSync(cardPath)) return failCli(errOut, `card serve: no card found at ${cardPath}; run: pillar card build --name <name>`, 1); // @rule card-cli.serve-card-exists
|
|
318
|
+
let card;
|
|
319
|
+
try { card = JSON.parse(readFileSync(cardPath, "utf8")); } catch { card = null; }
|
|
320
|
+
if (!card || typeof card !== "object" || Array.isArray(card) || card.v !== "pillar-card/1") return failCli(errOut, `card serve: ${cardPath} is not a valid pillar-card/1 JSON document`, 1); // @rule card-cli.serve-card-valid
|
|
321
|
+
|
|
322
|
+
const soulCard = loadHomeSoulCard(home);
|
|
323
|
+
const server = createCardServer(card, { soulCard });
|
|
324
|
+
await new Promise((resolveListen, rejectListen) => {
|
|
325
|
+
server.once("error", rejectListen);
|
|
326
|
+
server.listen({ host, port }, () => {
|
|
327
|
+
server.removeListener("error", rejectListen);
|
|
328
|
+
resolveListen();
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
const addr = server.address();
|
|
333
|
+
const actualHost = typeof addr === "object" && addr ? addr.address : host;
|
|
334
|
+
const actualPort = typeof addr === "object" && addr ? addr.port : port;
|
|
335
|
+
const baseUrl = `http://${formatHostForUrl(actualHost)}:${actualPort}`;
|
|
336
|
+
const info = {
|
|
337
|
+
ok: true,
|
|
338
|
+
host: actualHost,
|
|
339
|
+
port: actualPort,
|
|
340
|
+
url: baseUrl,
|
|
341
|
+
pillarCardUrl: `${baseUrl}/.well-known/pillar-card.json`,
|
|
342
|
+
agentCardUrl: `${baseUrl}/.well-known/agent-card.json`,
|
|
343
|
+
...(soulCard ? { soulCardUrl: `${baseUrl}/.well-known/soul-card.json` } : {}),
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
if (opts.json) {
|
|
347
|
+
out(`${JSON.stringify(info)}\n`);
|
|
348
|
+
} else {
|
|
349
|
+
out([
|
|
350
|
+
`pillar card server listening on ${baseUrl}`,
|
|
351
|
+
` pillar-card: ${info.pillarCardUrl}`,
|
|
352
|
+
` agent-card: ${info.agentCardUrl}`,
|
|
353
|
+
"",
|
|
354
|
+
].join("\n"));
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (io.stayAlive === false) {
|
|
358
|
+
return { exitCode: 0, server, info, card };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
await new Promise((resolveDone) => {
|
|
362
|
+
const shutdown = () => {
|
|
363
|
+
server.closeAllConnections?.();
|
|
364
|
+
server.close(() => resolveDone());
|
|
365
|
+
};
|
|
366
|
+
process.once("SIGINT", shutdown);
|
|
367
|
+
process.once("SIGTERM", shutdown);
|
|
368
|
+
});
|
|
369
|
+
return { exitCode: 0, server, info, card };
|
|
370
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pillar vNext Plan Row B7 — Greeter operator binding (`pillar-greeter-list/1`) and operator count.
|
|
3
|
+
*
|
|
4
|
+
* `pillar-card/1` is closed and has no operator field, so operators are bound outside the card:
|
|
5
|
+
* each operator signs a greeter list
|
|
6
|
+
*
|
|
7
|
+
* { v: "pillar-greeter-list/1", operator: <operator uuaid>, publicKey: <operator key hex>,
|
|
8
|
+
* greeters: [<greeter uuaid>, ...], issuedAt, sig }
|
|
9
|
+
*
|
|
10
|
+
* with `sig` = Ed25519 over `"pillar-greeter-list/1\n" + jcs(doc without sig)` (the
|
|
11
|
+
* `signLineageDoc` canonical form). The trust anchor mirrors the B9 registry-list check
|
|
12
|
+
* (`homes.ts` `TrustedOperator { operator, uuaid, publicKey }`): a list counts only when its
|
|
13
|
+
* signer is a trusted operator key, and operators are counted by trusted `operator` label.
|
|
14
|
+
*
|
|
15
|
+
* A greeter counts only if its card verifies, carries `does: [{ tag: "greeter" }]`, and
|
|
16
|
+
* appears in exactly one verified list (each operator key's newest list supersedes its older ones).
|
|
17
|
+
*/
|
|
18
|
+
import {
|
|
19
|
+
LINEAGE_SKEW_MS,
|
|
20
|
+
isKeyDerivedAgentUuaid,
|
|
21
|
+
isStrictEd25519PublicKey,
|
|
22
|
+
keyRecord,
|
|
23
|
+
resolve as coreResolve,
|
|
24
|
+
signLineageDoc,
|
|
25
|
+
verifyLineageDoc,
|
|
26
|
+
} from "../vendor/pillar-core.mjs";
|
|
27
|
+
import { isPlainObject, isStrictDateTime } from "../registry/lineage.mjs";
|
|
28
|
+
import { isDenseArray } from "../registry/listings.mjs";
|
|
29
|
+
import { hasGreeterTag, hasValidGreeterCard } from "./greeter.mjs";
|
|
30
|
+
import { isValidUuaid } from "./knock-lane.mjs";
|
|
31
|
+
import { nowIsoSeconds } from "./wave-a.mjs";
|
|
32
|
+
|
|
33
|
+
export const GREETER_LIST_VERSION = "pillar-greeter-list/1";
|
|
34
|
+
export const MAX_GREETER_LIST_ENTRIES = 64;
|
|
35
|
+
|
|
36
|
+
const LIST_KEYS = new Set(["v", "operator", "publicKey", "greeters", "issuedAt", "sig"]);
|
|
37
|
+
const HEX128_RE = /^[0-9a-f]{128}$/;
|
|
38
|
+
const fail = (reason) => ({ ok: false, reason });
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Sign a `pillar-greeter-list/1` with the operator's key. Throws on an invalid list.
|
|
42
|
+
*/
|
|
43
|
+
export function signGreeterList(signer, { greeters, issuedAt = nowIsoSeconds(), namespace = "pillar" } = {}) {
|
|
44
|
+
const rec = keyRecord(signer, namespace);
|
|
45
|
+
const unsigned = { v: GREETER_LIST_VERSION, operator: rec.uuaid, publicKey: rec.publicKey, greeters: Array.isArray(greeters) ? [...greeters] : greeters, issuedAt };
|
|
46
|
+
const doc = { ...unsigned, sig: signLineageDoc(unsigned, signer) };
|
|
47
|
+
const check = verifyGreeterList(doc, { now: issuedAt });
|
|
48
|
+
if (!check.ok) throw new TypeError(`signGreeterList: ${check.reason}`);
|
|
49
|
+
return doc;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Verify one `pillar-greeter-list/1`. With `trustedOperators`, the signer must match a trusted
|
|
54
|
+
* `{ operator, uuaid, publicKey }` entry and the returned `operator` is that entry's label;
|
|
55
|
+
* otherwise the returned `operator` is the signer UUAID.
|
|
56
|
+
*/
|
|
57
|
+
export function verifyGreeterList(doc, { now, trustedOperators } = {}) {
|
|
58
|
+
if (!isStrictDateTime(now)) throw new TypeError("verifyGreeterList: now must be a strict date-time"); // @rule greeter-list.now
|
|
59
|
+
if (!isPlainObject(doc) || !Object.keys(doc).every((k) => LIST_KEYS.has(k))) return fail("malformed-list"); // @rule greeter-list.closed
|
|
60
|
+
if (doc.v !== GREETER_LIST_VERSION) return fail("bad-version"); // @rule greeter-list.version
|
|
61
|
+
if (typeof doc.publicKey !== "string" || !isStrictEd25519PublicKey(doc.publicKey) || !isKeyDerivedAgentUuaid(doc.operator, doc.publicKey)) return fail("bad-operator-key"); // @rule greeter-list.operator-key
|
|
62
|
+
if (!isDenseArray(doc.greeters) || doc.greeters.length < 1 || doc.greeters.length > MAX_GREETER_LIST_ENTRIES) return fail("bad-greeters"); // @rule greeter-list.greeters-array
|
|
63
|
+
if (!doc.greeters.every((g) => isValidUuaid(g))) return fail("bad-greeters"); // @rule greeter-list.greeters-uuaid
|
|
64
|
+
if (new Set(doc.greeters).size !== doc.greeters.length) return fail("duplicate-greeter"); // @rule greeter-list.greeters-distinct
|
|
65
|
+
if (!isStrictDateTime(doc.issuedAt)) return fail("bad-issued-at"); // @rule greeter-list.issued-at
|
|
66
|
+
if (Date.parse(doc.issuedAt) > Date.parse(now) + LINEAGE_SKEW_MS) return fail("future-ts"); // @rule greeter-list.future-ts
|
|
67
|
+
if (typeof doc.sig !== "string" || !HEX128_RE.test(doc.sig)) return fail("bad-signature"); // @rule greeter-list.sig-format
|
|
68
|
+
if (!verifyLineageDoc(doc, doc.publicKey, doc.sig)) return fail("bad-signature"); // @rule greeter-list.sig-verify
|
|
69
|
+
let operator = doc.operator;
|
|
70
|
+
if (trustedOperators !== undefined) {
|
|
71
|
+
const m = Array.isArray(trustedOperators) ? trustedOperators.find((t) => t?.uuaid === doc.operator && t?.publicKey === doc.publicKey) : undefined;
|
|
72
|
+
if (!m || typeof m.operator !== "string" || !m.operator.trim()) return fail("untrusted-operator"); // @rule greeter-list.trusted-operator
|
|
73
|
+
operator = m.operator.trim();
|
|
74
|
+
}
|
|
75
|
+
return { ok: true, operator, list: doc };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Count greeter operators.
|
|
80
|
+
*
|
|
81
|
+
* @param {object} input
|
|
82
|
+
* @param {unknown[]} input.lists candidate `pillar-greeter-list/1` documents
|
|
83
|
+
* @param {Array<{uuaid: string, card: object|null, lineage?: object[]|null, unverified?: string}>} input.greeters
|
|
84
|
+
* presented greeter cards (`unverified` names why a card could not be verified upstream)
|
|
85
|
+
* @param {string} input.now
|
|
86
|
+
* @param {Array<{operator: string, uuaid: string, publicKey: string}>} input.trustedOperators
|
|
87
|
+
*/
|
|
88
|
+
export function countGreeterOperators({ lists, greeters, now, trustedOperators } = {}) {
|
|
89
|
+
if (!isStrictDateTime(now)) throw new TypeError("countGreeterOperators: now must be a strict date-time"); // @rule greeter-count.now
|
|
90
|
+
if (!Array.isArray(lists) || !Array.isArray(greeters)) throw new TypeError("countGreeterOperators: lists and greeters must be arrays"); // @rule greeter-count.inputs
|
|
91
|
+
if (!isDenseArray(trustedOperators)) throw new TypeError("countGreeterOperators: trustedOperators is required (the Sybil anchor)"); // @rule greeter-count.trust-anchor
|
|
92
|
+
|
|
93
|
+
const rejectedLists = [];
|
|
94
|
+
const current = new Map();
|
|
95
|
+
const conflicted = new Set();
|
|
96
|
+
lists.forEach((doc, index) => {
|
|
97
|
+
const v = verifyGreeterList(doc, { now, trustedOperators });
|
|
98
|
+
if (!v.ok) { rejectedLists.push({ index, reason: v.reason }); return; } // @rule greeter-count.list-verified
|
|
99
|
+
const k = v.list.publicKey;
|
|
100
|
+
const prev = current.get(k);
|
|
101
|
+
if (!prev) { current.set(k, { ...v, index }); return; }
|
|
102
|
+
const d = Date.parse(v.list.issuedAt) - Date.parse(prev.list.issuedAt);
|
|
103
|
+
if (d === 0 && v.list.sig !== prev.list.sig) conflicted.add(k); // @rule greeter-count.equivocation
|
|
104
|
+
if (d > 0) { current.set(k, { ...v, index }); conflicted.delete(k); } // @rule greeter-count.newest-per-operator
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const membership = new Map();
|
|
108
|
+
for (const [k, v] of current) {
|
|
109
|
+
if (conflicted.has(k)) { rejectedLists.push({ index: v.index, reason: "operator-equivocation" }); continue; } // @rule greeter-count.drop-equivocating
|
|
110
|
+
for (const g of v.list.greeters) membership.set(g, [...(membership.get(g) ?? []), v.operator]);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const presented = new Map();
|
|
114
|
+
for (const p of greeters) if (isPlainObject(p) && typeof p.uuaid === "string" && !presented.has(p.uuaid)) presented.set(p.uuaid, p);
|
|
115
|
+
|
|
116
|
+
const counted = [];
|
|
117
|
+
const rejectedGreeters = [];
|
|
118
|
+
const operators = new Set();
|
|
119
|
+
for (const g of [...membership.keys()].sort()) {
|
|
120
|
+
const ops = membership.get(g);
|
|
121
|
+
if (ops.length !== 1) { rejectedGreeters.push({ uuaid: g, reason: "multi-list", lists: ops.length }); continue; } // @rule greeter-count.exactly-one-list
|
|
122
|
+
const p = presented.get(g);
|
|
123
|
+
if (!p || !isPlainObject(p.card)) { rejectedGreeters.push({ uuaid: g, reason: p?.unverified ? "card-invalid" : "card-missing", ...(p?.unverified ? { detail: p.unverified } : {}) }); continue; } // @rule greeter-count.card-present
|
|
124
|
+
if (!hasGreeterTag(p.card)) { rejectedGreeters.push({ uuaid: g, reason: "untagged" }); continue; } // @rule greeter-count.greeter-tag
|
|
125
|
+
if (!hasValidGreeterCard(p.card, g, { now, lineage: Array.isArray(p.lineage) ? p.lineage : null })) { rejectedGreeters.push({ uuaid: g, reason: "card-invalid" }); continue; } // @rule greeter-count.card-verify
|
|
126
|
+
counted.push({ uuaid: g, operator: ops[0] });
|
|
127
|
+
operators.add(ops[0]);
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
operators: operators.size,
|
|
131
|
+
operatorIds: [...operators].sort(),
|
|
132
|
+
greeters: counted,
|
|
133
|
+
rejectedGreeters,
|
|
134
|
+
rejectedLists: rejectedLists.sort((a, b) => a.index - b.index),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Loopback/registry census: resolve and verify every greeter named by a verified list with the
|
|
140
|
+
* core `resolve()`, then run `countGreeterOperators`.
|
|
141
|
+
*/
|
|
142
|
+
export async function censusGreeters({ registryUrl, lists, trustedOperators, now = nowIsoSeconds(), fetchImpl = globalThis.fetch, resolveImpl = coreResolve } = {}) {
|
|
143
|
+
if (!Array.isArray(lists)) throw new TypeError("censusGreeters: lists must be an array");
|
|
144
|
+
const uuaids = new Set();
|
|
145
|
+
for (const doc of lists) {
|
|
146
|
+
const v = verifyGreeterList(doc, { now, trustedOperators });
|
|
147
|
+
if (v.ok) for (const g of doc.greeters) uuaids.add(g);
|
|
148
|
+
}
|
|
149
|
+
const presented = [];
|
|
150
|
+
for (const u of [...uuaids].sort()) {
|
|
151
|
+
let r = null;
|
|
152
|
+
try { r = await resolveImpl(u, { registryUrl, now, fetch: fetchImpl }); } catch { r = null; }
|
|
153
|
+
if (r?.ok && isPlainObject(r.card) && r.current === u) {
|
|
154
|
+
presented.push({ uuaid: u, card: r.card, lineage: Array.isArray(r.lineageLog?.events) ? r.lineageLog.events : null });
|
|
155
|
+
} else {
|
|
156
|
+
presented.push({ uuaid: u, card: null, ...(r?.reason !== "not-found" ? { unverified: r?.ok ? "not-current" : (r?.reason ?? "resolve-failed") } : {}) });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return countGreeterOperators({ lists, greeters: presented, now, trustedOperators });
|
|
160
|
+
}
|