@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,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,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `pillar resolve <uuaid> [--registry URL] [--now ISO] [--home DIR] [--json]`
|
|
3
|
+
* (and the live form of `pillar verify <uuaid>`, through resolveLive)
|
|
4
|
+
*
|
|
5
|
+
* Calls core `resolve()` (fail-closed cryptographic verification of listing,
|
|
6
|
+
* card, and lineage). Exits:
|
|
7
|
+
* 0 on `ok: true`
|
|
8
|
+
* 1 on resolution or verification failure
|
|
9
|
+
* 64 on usage error
|
|
10
|
+
*/
|
|
11
|
+
import { DEFAULT_RESOLVE_REGISTRY, resolve } from "../vendor/pillar-core.mjs";
|
|
12
|
+
import {
|
|
13
|
+
failCli,
|
|
14
|
+
isRegistryUrl,
|
|
15
|
+
isValidUuaid,
|
|
16
|
+
loadHomeConfig,
|
|
17
|
+
parseStrictFlags,
|
|
18
|
+
resolveHome,
|
|
19
|
+
} from "./a6b-common.mjs";
|
|
20
|
+
import { isStrictIsoDateTime, nowIsoSeconds } from "./wave-a.mjs";
|
|
21
|
+
|
|
22
|
+
function formatHumanResolve(res) {
|
|
23
|
+
const lines = [
|
|
24
|
+
`query: ${res.answer?.query ?? res.lineage}`,
|
|
25
|
+
`lineage: ${res.lineage}`,
|
|
26
|
+
`current: ${res.current}`,
|
|
27
|
+
`status: ${res.status}`,
|
|
28
|
+
];
|
|
29
|
+
if (res.card?.publicKey) lines.push(`publicKey: ${res.card.publicKey}`);
|
|
30
|
+
if (res.card?.name) lines.push(`name: ${res.card.name}`);
|
|
31
|
+
if (Array.isArray(res.card?.carriers)) lines.push(`carriers: ${res.card.carriers.join(", ")}`);
|
|
32
|
+
if (res.answer?.answeredBy) lines.push(`answeredBy: ${res.answer.answeredBy}`);
|
|
33
|
+
return `${lines.join("\n")}\n`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Parse `<uuaid> [--registry URL] [--now ISO] [--home DIR] [--json]` and run
|
|
38
|
+
* core `resolve()` (fail-closed verification of listing, card and lineage).
|
|
39
|
+
* Shared by `pillar resolve` and `pillar verify <uuaid>`. Returns
|
|
40
|
+
* `{ usage: { exitCode } }` after printing a usage error, or
|
|
41
|
+
* `{ json, registryUrl, uuaid, result, error }` (error: a thrown message).
|
|
42
|
+
*/
|
|
43
|
+
export async function resolveLive(rawArgs, { cmdName = "resolve", io = {} } = {}) {
|
|
44
|
+
const errOut = io.stderr ?? ((s) => process.stderr.write(s));
|
|
45
|
+
const parsed = parseStrictFlags(rawArgs, {
|
|
46
|
+
boolFlags: new Set(["json"]),
|
|
47
|
+
valueFlags: new Set(["home", "registry", "now"]),
|
|
48
|
+
cmdName,
|
|
49
|
+
});
|
|
50
|
+
if (!parsed.ok) return { usage: failCli(errOut, parsed.error, parsed.exitCode) };
|
|
51
|
+
|
|
52
|
+
const { positionals, opts } = parsed;
|
|
53
|
+
if (positionals.length !== 1) return { usage: failCli(errOut, `usage: pillar ${cmdName} <uuaid> [--registry URL] [--now ISO] [--json]`, 64) }; // @rule resolve-cli.arg-count
|
|
54
|
+
|
|
55
|
+
const uuaid = positionals[0];
|
|
56
|
+
if (!isValidUuaid(uuaid)) return { usage: failCli(errOut, `${cmdName}: invalid UUAID "${uuaid}"`, 64) }; // @rule resolve-cli.uuaid-valid
|
|
57
|
+
|
|
58
|
+
const ts = opts.now !== undefined ? opts.now : nowIsoSeconds();
|
|
59
|
+
if (!isStrictIsoDateTime(ts)) return { usage: failCli(errOut, `${cmdName}: --now must be a valid ISO-8601 timestamp`, 64) }; // @rule resolve-cli.now-valid
|
|
60
|
+
|
|
61
|
+
let cfg = {};
|
|
62
|
+
try { cfg = loadHomeConfig(resolveHome(opts)); } catch { cfg = {}; }
|
|
63
|
+
const registryUrl = opts.registry ?? cfg.registry ?? DEFAULT_RESOLVE_REGISTRY;
|
|
64
|
+
if (!isRegistryUrl(registryUrl)) return { usage: failCli(errOut, `${cmdName}: --registry must be an https:// (or loopback http://) URL, got "${registryUrl}"`, 64) }; // @rule resolve-cli.registry-url
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const result = await resolve(uuaid, {
|
|
68
|
+
registryUrl,
|
|
69
|
+
now: ts,
|
|
70
|
+
...(io.fetch ? { fetch: io.fetch } : {}),
|
|
71
|
+
});
|
|
72
|
+
return { json: opts.json, registryUrl, uuaid, result, error: null };
|
|
73
|
+
} catch (e) {
|
|
74
|
+
return { json: opts.json, registryUrl, uuaid, result: null, error: e instanceof Error ? e.message : String(e) };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Execute `pillar resolve` with injectable streams and fetch.
|
|
80
|
+
* Returns `{ exitCode, result? }`.
|
|
81
|
+
*/
|
|
82
|
+
export async function runResolveCli(rawArgs, io = {}) {
|
|
83
|
+
const out = io.stdout ?? ((s) => process.stdout.write(s));
|
|
84
|
+
const errOut = io.stderr ?? ((s) => process.stderr.write(s));
|
|
85
|
+
|
|
86
|
+
const live = await resolveLive(rawArgs, { cmdName: "resolve", io });
|
|
87
|
+
if (live.usage) return live.usage;
|
|
88
|
+
const { json, result, error } = live;
|
|
89
|
+
|
|
90
|
+
if (error !== null) {
|
|
91
|
+
if (json) out(`${JSON.stringify({ ok: false, reason: "resolve-error", message: error })}\n`);
|
|
92
|
+
else errOut(`resolve: ${error}\n`);
|
|
93
|
+
return { exitCode: 1, result: { ok: false, reason: "resolve-error" } };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!result || result.ok !== true) {
|
|
97
|
+
if (json) out(`${JSON.stringify(result)}\n`);
|
|
98
|
+
else errOut(`resolve: ${result?.reason ?? "failed"}${result?.statusCode ? ` (HTTP ${result.statusCode})` : ""}\n`);
|
|
99
|
+
return { exitCode: 1, result }; // @rule resolve-cli.fail-closed-exit-1
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
out(json ? `${JSON.stringify(result)}\n` : formatHumanResolve(result));
|
|
103
|
+
return { exitCode: 0, result };
|
|
104
|
+
}
|