@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,972 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pillar vNext §2 — Full 9-event `uuaid-lineage/1` closed-schema validator and
|
|
3
|
+
* `r2.5b'''` tree/epoch verifier for `pillar-directory` home registries (Wave B B9).
|
|
4
|
+
*
|
|
5
|
+
* Matches frozen `vnext-ref.mjs` (`a434835c`) and `uuaid-lineage-event.schema.json`
|
|
6
|
+
* while enforcing strict RFC 8032 Ed25519 verification (`verifyLineageDoc` /
|
|
7
|
+
* `isStrictEd25519PublicKey`) and strict calendar date-time parsing.
|
|
8
|
+
*/
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import {
|
|
11
|
+
LINEAGE_SKEW_MS,
|
|
12
|
+
LINEAGE_VERDICT_VERSION,
|
|
13
|
+
LINEAGE_VERSION,
|
|
14
|
+
MIN_CONTEST_MS,
|
|
15
|
+
durMs,
|
|
16
|
+
eligibleHomes,
|
|
17
|
+
eventDigest,
|
|
18
|
+
isKeyDerivedAgentUuaid,
|
|
19
|
+
isStrictEd25519PublicKey,
|
|
20
|
+
jcs,
|
|
21
|
+
signLineageDoc,
|
|
22
|
+
signingBytes,
|
|
23
|
+
verifyLineageDoc,
|
|
24
|
+
} from "../vendor/pillar-core.mjs";
|
|
25
|
+
|
|
26
|
+
export const LINEAGE_SEEN_VERSION = "uuaid-lineage-seen/1";
|
|
27
|
+
export const LINEAGE_LOG_VERSION = "uuaid-lineage-log/1";
|
|
28
|
+
export const MAX_EVENT_SIGS = 16;
|
|
29
|
+
export const MAX_LOG_RECEIPTS = 1024;
|
|
30
|
+
export const REGISTRY_VERIFIER_TOOL = "pillar-directory/2.7.0";
|
|
31
|
+
|
|
32
|
+
export const ALL_EVENT_TYPES = Object.freeze(
|
|
33
|
+
new Set([
|
|
34
|
+
"inception",
|
|
35
|
+
"rotation",
|
|
36
|
+
"door.add",
|
|
37
|
+
"door.retire",
|
|
38
|
+
"recovery.claim",
|
|
39
|
+
"recovery.veto",
|
|
40
|
+
"recovery.final",
|
|
41
|
+
"bind",
|
|
42
|
+
"tombstone",
|
|
43
|
+
]),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
export const RECOVERY_EVENT_TYPES = Object.freeze(
|
|
47
|
+
new Set(["recovery.claim", "recovery.veto", "recovery.final"]),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
export const UUAID_RE = /^uuaid:[a-z0-9-]+:[a-z0-9-]+:[a-z0-9][a-z0-9-]*$/;
|
|
51
|
+
export const HEX64_RE = /^[0-9a-f]{64}$/;
|
|
52
|
+
export const HEX128_RE = /^[0-9a-f]{128}$/;
|
|
53
|
+
|
|
54
|
+
const STRICT_DATETIME_RE =
|
|
55
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(Z|[+-](\d{2}):(\d{2}))$/;
|
|
56
|
+
const DAYS_IN_MONTH = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
57
|
+
const STRICT_HTTPS_URI_RE =
|
|
58
|
+
/^https:\/\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'()*+,;=]|%[0-9a-f]{2})+)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*(?:\?(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i;
|
|
59
|
+
|
|
60
|
+
const BASE_KEYS = ["v", "lineage", "seq", "prev", "type", "ts", "sigs"];
|
|
61
|
+
const TYPE_ALLOWED_KEYS = {
|
|
62
|
+
inception: new Set([...BASE_KEYS, "key", "next", "witnesses", "witnessThreshold", "contestWindow", "doors", "homes"]),
|
|
63
|
+
rotation: new Set([...BASE_KEYS, "reveal", "key", "next", "witnesses", "witnessThreshold", "contestWindow", "homes", "reason"]),
|
|
64
|
+
"door.add": new Set([...BASE_KEYS, "door", "reason"]),
|
|
65
|
+
"door.retire": new Set([...BASE_KEYS, "door", "reason"]),
|
|
66
|
+
"recovery.claim": new Set([...BASE_KEYS, "key", "next", "claimedAt", "reason"]),
|
|
67
|
+
"recovery.veto": new Set([...BASE_KEYS, "claim", "nextKey", "reason"]),
|
|
68
|
+
"recovery.final": new Set([...BASE_KEYS, "claim", "reason"]),
|
|
69
|
+
bind: new Set([...BASE_KEYS, "legacy", "reason"]),
|
|
70
|
+
tombstone: new Set([...BASE_KEYS, "reason"]),
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const KEY_REC_KEYS = new Set(["uuaid", "publicKey"]);
|
|
74
|
+
const HOME_REC_KEYS = new Set(["uuaid", "publicKey", "url"]);
|
|
75
|
+
const DOOR_REC_KEYS = new Set(["uuaid", "publicKey", "purpose", "notAfter"]);
|
|
76
|
+
const SIG_KEYS = new Set(["by", "sig"]);
|
|
77
|
+
const SEEN_KEYS = new Set(["v", "lineage", "event", "registry", "seenAt", "sig"]);
|
|
78
|
+
|
|
79
|
+
export function sha256Hex(hexStr) {
|
|
80
|
+
return createHash("sha256").update(Buffer.from(hexStr, "hex")).digest("hex");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function isPlainObject(v) {
|
|
84
|
+
if (typeof v !== "object" || v === null || Array.isArray(v)) return false;
|
|
85
|
+
const proto = Object.getPrototypeOf(v);
|
|
86
|
+
return proto === Object.prototype || proto === null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function isDenseArray(a) {
|
|
90
|
+
if (!Array.isArray(a)) return false;
|
|
91
|
+
for (let i = 0; i < a.length; i++) {
|
|
92
|
+
if (!Object.hasOwn(a, i)) return false;
|
|
93
|
+
}
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function isStrictDateTime(s) {
|
|
98
|
+
if (typeof s !== "string") return false;
|
|
99
|
+
const m = STRICT_DATETIME_RE.exec(s);
|
|
100
|
+
if (!m) return false;
|
|
101
|
+
const year = +m[1];
|
|
102
|
+
const month = +m[2];
|
|
103
|
+
const day = +m[3];
|
|
104
|
+
const hr = +m[4];
|
|
105
|
+
const min = +m[5];
|
|
106
|
+
const sec = +m[6];
|
|
107
|
+
const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
108
|
+
const maxDay = month === 2 && leap ? 29 : (DAYS_IN_MONTH[month] ?? 0);
|
|
109
|
+
if (month < 1 || month > 12 || day < 1 || day > maxDay) return false;
|
|
110
|
+
if (hr > 23 || min > 59 || sec > 59 || (m[7] !== "Z" && (+m[8] > 23 || +m[9] > 59))) return false;
|
|
111
|
+
return Number.isFinite(Date.parse(s));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function isStrictHttps(u) {
|
|
115
|
+
if (typeof u !== "string" || !u.startsWith("https://")) return false;
|
|
116
|
+
if (!STRICT_HTTPS_URI_RE.test(u)) return false;
|
|
117
|
+
try {
|
|
118
|
+
return new URL(u).protocol === "https:";
|
|
119
|
+
} catch {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function httpsOrigin(url) {
|
|
125
|
+
if (!isStrictHttps(url)) return null; // @rule ver.home-https
|
|
126
|
+
const u = new URL(url);
|
|
127
|
+
let host = u.hostname.toLowerCase();
|
|
128
|
+
if (host.endsWith(".")) host = host.slice(0, -1); // @rule ver.home-root-dot
|
|
129
|
+
if (!host || host.endsWith(".")) return null; // @rule ver.home-host
|
|
130
|
+
return `https://${host}${u.port ? `:${u.port}` : ""}`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function validKeyRecShape(k) {
|
|
134
|
+
if (!isPlainObject(k)) return false;
|
|
135
|
+
const keys = Object.keys(k);
|
|
136
|
+
if (keys.length !== 2 || !keys.every((x) => KEY_REC_KEYS.has(x))) return false;
|
|
137
|
+
return typeof k.uuaid === "string" && UUAID_RE.test(k.uuaid) && typeof k.publicKey === "string" && HEX64_RE.test(k.publicKey);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function validHomeRecShape(h) {
|
|
141
|
+
if (!isPlainObject(h)) return false;
|
|
142
|
+
const keys = Object.keys(h);
|
|
143
|
+
if (keys.length !== 3 || !keys.every((x) => HOME_REC_KEYS.has(x))) return false;
|
|
144
|
+
return typeof h.uuaid === "string" && UUAID_RE.test(h.uuaid) && typeof h.publicKey === "string" && HEX64_RE.test(h.publicKey) && isStrictHttps(h.url);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function validDoorRecShape(d) {
|
|
148
|
+
if (!isPlainObject(d)) return false;
|
|
149
|
+
const keys = Object.keys(d);
|
|
150
|
+
if (!keys.every((x) => DOOR_REC_KEYS.has(x))) return false;
|
|
151
|
+
if (typeof d.uuaid !== "string" || !UUAID_RE.test(d.uuaid) || typeof d.publicKey !== "string" || !HEX64_RE.test(d.publicKey)) return false;
|
|
152
|
+
if (Object.hasOwn(d, "purpose") && (typeof d.purpose !== "string" || [...d.purpose].length > 32)) return false;
|
|
153
|
+
if (Object.hasOwn(d, "notAfter") && !isStrictDateTime(d.notAfter)) return false;
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function validInertString(s) {
|
|
158
|
+
return typeof s === "string" && [...s].length <= 280;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function sigsShapeOk(sigs) {
|
|
162
|
+
if (!isDenseArray(sigs) || sigs.length === 0) return false; // @rule reg.lineage-sigs-array
|
|
163
|
+
if (sigs.length > MAX_EVENT_SIGS) return false; // @rule reg.lineage-sigs-max
|
|
164
|
+
const seenBy = new Set();
|
|
165
|
+
for (const s of sigs) {
|
|
166
|
+
if (!isPlainObject(s)) return false; // @rule reg.lineage-sig-object
|
|
167
|
+
const keys = Object.keys(s);
|
|
168
|
+
if (keys.length !== 2 || !keys.every((k) => SIG_KEYS.has(k))) return false; // @rule reg.lineage-sig-closed
|
|
169
|
+
if (typeof s.by !== "string" || !UUAID_RE.test(s.by)) return false; // @rule reg.lineage-sig-by
|
|
170
|
+
if (seenBy.has(s.by)) return false; // @rule reg.lineage-sig-dup-by
|
|
171
|
+
seenBy.add(s.by);
|
|
172
|
+
if (typeof s.sig !== "string" || !HEX128_RE.test(s.sig)) return false; // @rule reg.lineage-sig-hex
|
|
173
|
+
}
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Validate closed `uuaid-lineage-event.schema.json` structural rules before cryptographic checks.
|
|
179
|
+
* Returns `null` when valid, or a refusal reason string.
|
|
180
|
+
*/
|
|
181
|
+
export function checkEventTypeFields(ev) {
|
|
182
|
+
const allowed = TYPE_ALLOWED_KEYS[ev.type];
|
|
183
|
+
if (!allowed || !Object.keys(ev).every((k) => allowed.has(k))) return "malformed-artifact"; // @rule ver.event-closed-keys
|
|
184
|
+
if (Object.hasOwn(ev, "reason") && !validInertString(ev.reason)) return "malformed-artifact"; // @rule ver.event-reason-inert
|
|
185
|
+
|
|
186
|
+
if (ev.type === "inception" || ev.type === "rotation") {
|
|
187
|
+
if (!validKeyRecShape(ev.key)) return "malformed-artifact"; // @rule ver.event-key-shape
|
|
188
|
+
if (typeof ev.next !== "string" || !HEX64_RE.test(ev.next)) return "bad-next"; // @rule ver.event-next-shape
|
|
189
|
+
if (Object.hasOwn(ev, "witnesses")) {
|
|
190
|
+
if (!isDenseArray(ev.witnesses) || ev.witnesses.length > 16 || !ev.witnesses.every(validKeyRecShape)) return "malformed-artifact"; // @rule ver.event-witnesses-shape
|
|
191
|
+
}
|
|
192
|
+
if (Object.hasOwn(ev, "witnessThreshold")) {
|
|
193
|
+
if (!Number.isInteger(ev.witnessThreshold) || ev.witnessThreshold < 1) return "bad-threshold"; // @rule ver.event-threshold-int
|
|
194
|
+
}
|
|
195
|
+
if (Object.hasOwn(ev, "contestWindow") && durMs(ev.contestWindow) === null) return "bad-duration"; // @rule ver.event-cw-shape
|
|
196
|
+
if (Object.hasOwn(ev, "homes")) {
|
|
197
|
+
if (!isDenseArray(ev.homes) || ev.homes.length > 4) return "home-duplicate"; // @rule ver.event-homes-array
|
|
198
|
+
for (const h of ev.homes) {
|
|
199
|
+
if (!isPlainObject(h) || !Object.keys(h).every((x) => HOME_REC_KEYS.has(x)) || typeof h.uuaid !== "string" || typeof h.publicKey !== "string" || !HEX64_RE.test(h.publicKey)) {
|
|
200
|
+
return "malformed-artifact";
|
|
201
|
+
}
|
|
202
|
+
if (!httpsOrigin(h.url)) return "home-url-required"; // @rule ver.event-home-url
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (ev.type === "inception" && Object.hasOwn(ev, "doors")) {
|
|
206
|
+
if (!isDenseArray(ev.doors) || ev.doors.length > 8 || !ev.doors.every(validDoorRecShape)) return "malformed-artifact"; // @rule ver.event-doors-shape
|
|
207
|
+
}
|
|
208
|
+
if (ev.type === "rotation") {
|
|
209
|
+
if (!validKeyRecShape(ev.reveal)) return "malformed-artifact"; // @rule ver.event-reveal-shape
|
|
210
|
+
}
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (ev.type === "door.add" || ev.type === "door.retire") {
|
|
215
|
+
if (!validDoorRecShape(ev.door)) return "malformed-artifact"; // @rule ver.event-door-shape
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (ev.type === "recovery.claim") {
|
|
220
|
+
if (!validKeyRecShape(ev.key)) return "malformed-artifact"; // @rule ver.claim-key-shape
|
|
221
|
+
if (typeof ev.next !== "string" || !HEX64_RE.test(ev.next)) return "bad-next"; // @rule ver.claim-next-shape
|
|
222
|
+
if (!isStrictDateTime(ev.claimedAt)) return "malformed-artifact"; // @rule ver.claim-claimed-at
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (ev.type === "recovery.veto") {
|
|
227
|
+
if (typeof ev.claim !== "string" || !HEX64_RE.test(ev.claim)) return "malformed-artifact"; // @rule ver.veto-claim-hex
|
|
228
|
+
if (Object.hasOwn(ev, "nextKey") && (typeof ev.nextKey !== "string" || !HEX64_RE.test(ev.nextKey))) return "malformed-artifact"; // @rule ver.veto-nextkey-hex
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (ev.type === "recovery.final") {
|
|
233
|
+
if (typeof ev.claim !== "string" || !HEX64_RE.test(ev.claim)) return "malformed-artifact"; // @rule ver.final-claim-hex
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (ev.type === "bind") {
|
|
238
|
+
if (!isPlainObject(ev.legacy)) return "malformed-artifact"; // @rule ver.bind-legacy-obj
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (ev.type === "tombstone") {
|
|
243
|
+
if (ev.sigs.length !== 1) return "bad-signature"; // @rule ver.tombstone-single-sig
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return "unsupported-event";
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function derivesStrict(pubHex, uuaid) {
|
|
251
|
+
return (
|
|
252
|
+
typeof pubHex === "string" &&
|
|
253
|
+
HEX64_RE.test(pubHex) &&
|
|
254
|
+
isStrictEd25519PublicKey(pubHex) &&
|
|
255
|
+
typeof uuaid === "string" &&
|
|
256
|
+
isKeyDerivedAgentUuaid(uuaid, pubHex)
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Create a signed `uuaid-lineage-seen/1` receipt over `"uuaid-lineage-seen/1\n" + jcs(without sig)`.
|
|
262
|
+
*/
|
|
263
|
+
export function buildSeenReceipt(registryKey, lineage, eventOrDigest, seenAt) {
|
|
264
|
+
const digest = typeof eventOrDigest === "string" ? eventOrDigest : eventDigest(eventOrDigest);
|
|
265
|
+
const unsigned = {
|
|
266
|
+
v: LINEAGE_SEEN_VERSION,
|
|
267
|
+
lineage,
|
|
268
|
+
event: digest,
|
|
269
|
+
registry: registryKey.uuaid,
|
|
270
|
+
seenAt,
|
|
271
|
+
};
|
|
272
|
+
const sig =
|
|
273
|
+
typeof registryKey.sign === "function"
|
|
274
|
+
? Buffer.from(registryKey.sign(Buffer.from(signingBytes(unsigned)))).toString("hex")
|
|
275
|
+
: signLineageDoc(unsigned, { seedHex: registryKey.seedHex, publicKeyHex: registryKey.publicKey, uuaid: registryKey.uuaid });
|
|
276
|
+
return { ...unsigned, sig };
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Verify a `uuaid-lineage-seen/1` receipt against a declared home record and clock `nowMs`.
|
|
281
|
+
*/
|
|
282
|
+
export function verifySeenReceiptStrict(receipt, { lineage, home, nowMs }) {
|
|
283
|
+
if (!isPlainObject(receipt) || !Object.keys(receipt).every((k) => SEEN_KEYS.has(k))) return false; // @rule ver.seen-closed
|
|
284
|
+
if (receipt.v !== LINEAGE_SEEN_VERSION) return false; // @rule ver.seen-version
|
|
285
|
+
if (receipt.lineage !== lineage) return false; // @rule ver.seen-lineage
|
|
286
|
+
if (typeof receipt.event !== "string" || !HEX64_RE.test(receipt.event)) return false; // @rule ver.seen-event-hex
|
|
287
|
+
if (receipt.registry !== home.uuaid) return false; // @rule ver.seen-registry
|
|
288
|
+
if (!isStrictDateTime(receipt.seenAt)) return false; // @rule ver.seen-ts-format
|
|
289
|
+
const seenMs = Date.parse(receipt.seenAt);
|
|
290
|
+
if (seenMs > nowMs + LINEAGE_SKEW_MS) return false; // @rule ver.seen-ts-skew
|
|
291
|
+
if (typeof receipt.sig !== "string" || !HEX128_RE.test(receipt.sig)) return false; // @rule ver.seen-sig-hex
|
|
292
|
+
if (!derivesStrict(home.publicKey, home.uuaid)) return false; // @rule ver.seen-home-key
|
|
293
|
+
if (!verifyLineageDoc(receipt, home.publicKey, receipt.sig)) return false; // @rule ver.seen-sig-verify
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function mergeSigs(a, b) {
|
|
298
|
+
const seen = new Set(a.map((s) => `${s.by}:${s.sig}`));
|
|
299
|
+
return [...a, ...b.filter((s) => !seen.has(`${s.by}:${s.sig}`))];
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function dupIn(list) {
|
|
303
|
+
const u = new Set();
|
|
304
|
+
const p = new Set();
|
|
305
|
+
for (const x of list) {
|
|
306
|
+
if (u.has(x.uuaid) || p.has(x.publicKey)) return true;
|
|
307
|
+
u.add(x.uuaid);
|
|
308
|
+
p.add(x.publicKey);
|
|
309
|
+
}
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Full 9-event `r2.5b'''` tree + epoch verifier matching `vnext-ref.mjs` (`a434835c`).
|
|
315
|
+
*
|
|
316
|
+
* Options:
|
|
317
|
+
* - `now` (required ISO string)
|
|
318
|
+
* - `receipts` (optional extra `uuaid-lineage-seen/1` array)
|
|
319
|
+
* - `unreceiptedOwnerEvent` (`"ignored"` default | `"contested"`)
|
|
320
|
+
* - `allowBind` (`false` default; `true` allows Guard C storage/eval of owner-signed `bind`)
|
|
321
|
+
* - `ignoreRecoveryFutureTs` (`false` default; `true` for Guard C `C2`/`R25BP-4` storage check)
|
|
322
|
+
*/
|
|
323
|
+
export function verifyLineageFull(input, opts = {}) {
|
|
324
|
+
if (!opts || !isStrictDateTime(opts.now)) throw new TypeError("verifyLineageFull: opts.now (ISO time) is required");
|
|
325
|
+
if (opts.firstSeen !== undefined || opts.localFirstSeen !== undefined) {
|
|
326
|
+
throw new TypeError("verifyLineageFull: local first-sight options are forbidden");
|
|
327
|
+
}
|
|
328
|
+
const nowMs = Date.parse(opts.now);
|
|
329
|
+
const log = Array.isArray(input) ? input : (input?.events ?? []);
|
|
330
|
+
const receipts = [...(Array.isArray(input) ? [] : (input?.receipts ?? [])), ...(opts.receipts ?? [])];
|
|
331
|
+
const unreceipted = opts.unreceiptedOwnerEvent ?? "ignored";
|
|
332
|
+
if (unreceipted !== "ignored" && unreceipted !== "contested") {
|
|
333
|
+
throw new TypeError("verifyLineageFull: bad unreceiptedOwnerEvent");
|
|
334
|
+
}
|
|
335
|
+
const allowBind = opts.allowBind === true;
|
|
336
|
+
const ignoreRecFuture = opts.ignoreRecoveryFutureTs === true;
|
|
337
|
+
const discarded = [];
|
|
338
|
+
const validNodes = new Map();
|
|
339
|
+
|
|
340
|
+
const out = (verdict, reasons = [], facts = {}) => ({
|
|
341
|
+
v: LINEAGE_VERDICT_VERSION,
|
|
342
|
+
type: "lineage",
|
|
343
|
+
...(typeof log[0]?.lineage === "string" ? { subject: log[0].lineage } : {}),
|
|
344
|
+
verdict,
|
|
345
|
+
reasons,
|
|
346
|
+
facts: { ...facts, ...(discarded.length ? { discarded } : {}) },
|
|
347
|
+
checkedAt: opts.now,
|
|
348
|
+
tool: REGISTRY_VERIFIER_TOOL,
|
|
349
|
+
_nodes: validNodes,
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
if (!log.length) return out("invalid", ["no-inception"], { failedAtSeq: 0 });
|
|
353
|
+
const odd = log.find((e) => e?.v !== LINEAGE_VERSION);
|
|
354
|
+
if (odd) {
|
|
355
|
+
return out("indeterminate", ["unsupported-version"], {
|
|
356
|
+
version: typeof odd?.v === "string" ? odd.v : null,
|
|
357
|
+
...(Number.isInteger(odd?.seq) ? { failedAtSeq: odd.seq } : {}),
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const bySeq = new Map();
|
|
362
|
+
for (const e of log) {
|
|
363
|
+
if (!isPlainObject(e) || !Number.isInteger(e.seq) || e.seq < 0) return out("invalid", ["bad-seq"], { failedAtSeq: 0 });
|
|
364
|
+
let d;
|
|
365
|
+
try {
|
|
366
|
+
jcs(e);
|
|
367
|
+
d = eventDigest(e);
|
|
368
|
+
} catch {
|
|
369
|
+
return out("invalid", ["malformed-artifact"], { failedAtSeq: e.seq });
|
|
370
|
+
}
|
|
371
|
+
const m = bySeq.get(e.seq) ?? new Map();
|
|
372
|
+
bySeq.set(e.seq, m);
|
|
373
|
+
m.set(d, m.has(d) ? { ...m.get(d), sigs: mergeSigs(m.get(d).sigs ?? [], e.sigs ?? []) } : { ...e, sigs: [...(e.sigs ?? [])] });
|
|
374
|
+
}
|
|
375
|
+
const maxSeq = Math.max(...bySeq.keys());
|
|
376
|
+
for (let s = 0; s <= maxSeq; s++) {
|
|
377
|
+
if (!bySeq.has(s)) return out("invalid", ["seq-gap"], { failedAtSeq: s });
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const homeTimes = (lineage, digest, homes, witnesses) => {
|
|
381
|
+
const elig = eligibleHomes(homes ?? [], witnesses ?? []);
|
|
382
|
+
const best = new Map();
|
|
383
|
+
for (const r of receipts) {
|
|
384
|
+
if (!isPlainObject(r) || r.event !== digest) continue;
|
|
385
|
+
const h = elig.find((x) => x.uuaid === r.registry);
|
|
386
|
+
if (!h || !verifySeenReceiptStrict(r, { lineage, home: h, nowMs })) continue;
|
|
387
|
+
const t = Date.parse(r.seenAt);
|
|
388
|
+
if (!best.has(h.uuaid) || t < best.get(h.uuaid)) best.set(h.uuaid, t);
|
|
389
|
+
}
|
|
390
|
+
return [...best.values()].sort((a, b) => a - b);
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
const corroboratedAt = (lineage, digest, S) => {
|
|
394
|
+
const ts = homeTimes(lineage, digest, S.st.homes, S.st.witnesses);
|
|
395
|
+
return ts.length >= 2 ? ts[1] : undefined;
|
|
396
|
+
};
|
|
397
|
+
const observedAt = (lineage, digest, homes, witnesses) => {
|
|
398
|
+
const ts = homeTimes(lineage, digest, homes, witnesses);
|
|
399
|
+
return ts.length >= 2 ? { at: ts[1], source: "receipts" } : null;
|
|
400
|
+
};
|
|
401
|
+
const classify = (lineage, digest, E, homes, witnesses) => {
|
|
402
|
+
if (E === undefined) return { cls: "timely", why: "claim-unobserved" };
|
|
403
|
+
const ts = homeTimes(lineage, digest, homes, witnesses);
|
|
404
|
+
const early = ts.filter((t) => t < E).length;
|
|
405
|
+
const late = ts.length - early;
|
|
406
|
+
if (early >= 2) return { cls: "timely", why: "receipts" };
|
|
407
|
+
if (late >= 2) return { cls: "late", why: "receipts" };
|
|
408
|
+
if (unreceipted === "ignored" && early === 0) return { cls: "ignored", why: "no-timely-receipt" };
|
|
409
|
+
return { cls: "unknown", why: ts.length === 0 ? "owner-event-unobserved" : "owner-event-uncorroborated" };
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
const sigBy = (e, uuaid, pub) =>
|
|
413
|
+
derivesStrict(pub, uuaid) &&
|
|
414
|
+
(e.sigs ?? []).some((s) => isPlainObject(s) && s.by === uuaid && typeof s.sig === "string" && HEX128_RE.test(s.sig) && verifyLineageDoc(e, pub, s.sig));
|
|
415
|
+
|
|
416
|
+
const fail = (reason, verdict = "invalid", extra = {}) => ({ ok: false, verdict, reason, extra });
|
|
417
|
+
|
|
418
|
+
function apply(b, e, d) {
|
|
419
|
+
if (!isStrictDateTime(e.ts)) return fail("bad-ts");
|
|
420
|
+
const futureTs = Date.parse(e.ts) > nowMs + LINEAGE_SKEW_MS;
|
|
421
|
+
if (futureTs && !(b && RECOVERY_EVENT_TYPES.has(e.type))) return fail("future-ts"); // @rule ver.non-recovery-future-ts
|
|
422
|
+
if (!b && e.type !== "inception") return fail("no-inception");
|
|
423
|
+
if (e.key && !derivesStrict(e.key.publicKey, e.key.uuaid)) return fail("key-uuaid-mismatch");
|
|
424
|
+
if (b && e.lineage !== b.lineage) return fail("lineage-mismatch");
|
|
425
|
+
if (b?.tombstoned) return fail("after-tombstone"); // @rule ver.after-tombstone
|
|
426
|
+
|
|
427
|
+
const n = b
|
|
428
|
+
? {
|
|
429
|
+
...b,
|
|
430
|
+
st: { ...b.st, doors: new Map(b.st.doors) },
|
|
431
|
+
path: [...b.path],
|
|
432
|
+
used: [...b.used],
|
|
433
|
+
usedDoors: [...b.usedDoors],
|
|
434
|
+
usedNext: [...b.usedNext],
|
|
435
|
+
finalizedList: [...b.finalizedList],
|
|
436
|
+
proofOf: null,
|
|
437
|
+
}
|
|
438
|
+
: null;
|
|
439
|
+
|
|
440
|
+
const homeClash = (homes, pubs, nextHashes) => homes.some((h) => pubs.includes(h.publicKey) || nextHashes.includes(sha256Hex(h.publicKey)));
|
|
441
|
+
const freshKey = (k, next, used, usedNext = []) => {
|
|
442
|
+
if (typeof next !== "string" || !HEX64_RE.test(next)) return "bad-next";
|
|
443
|
+
const kh = sha256Hex(k.publicKey);
|
|
444
|
+
if (used.includes(k.publicKey) || usedNext.includes(kh)) return "key-reused";
|
|
445
|
+
if (next === kh) return "next-is-key";
|
|
446
|
+
if (used.some((u) => sha256Hex(u) === next) || usedNext.includes(next)) return "next-reused";
|
|
447
|
+
return null;
|
|
448
|
+
};
|
|
449
|
+
const ownerEvent = (extra = {}) => {
|
|
450
|
+
Object.assign(n, { tip: d, last: e, proofOf: b.st.key.uuaid, cancelled: n.openClaim?.d ?? null, openClaim: null, ...extra });
|
|
451
|
+
return { ok: true, branch: n };
|
|
452
|
+
};
|
|
453
|
+
const setLists = (st, ev) => {
|
|
454
|
+
if (ev.witnesses) {
|
|
455
|
+
if (!ev.witnesses.every((w) => w && derivesStrict(w.publicKey, w.uuaid))) return "key-uuaid-mismatch";
|
|
456
|
+
if (dupIn(ev.witnesses)) return "witness-duplicate";
|
|
457
|
+
st.witnesses = ev.witnesses;
|
|
458
|
+
}
|
|
459
|
+
if (ev.witnessThreshold !== undefined) st.m = ev.witnessThreshold;
|
|
460
|
+
if (ev.contestWindow !== undefined) st.cw = ev.contestWindow;
|
|
461
|
+
const cwMs = durMs(st.cw);
|
|
462
|
+
if (cwMs === null) return "bad-duration";
|
|
463
|
+
if (cwMs < MIN_CONTEST_MS) return "contest-window-short";
|
|
464
|
+
if (st.witnesses.length && !(Number.isInteger(st.m) && st.m >= 1 && st.m <= st.witnesses.length)) return "bad-threshold";
|
|
465
|
+
if (ev.homes) {
|
|
466
|
+
if (!ev.homes.every((h) => h && derivesStrict(h.publicKey, h.uuaid))) return "key-uuaid-mismatch";
|
|
467
|
+
if (dupIn(ev.homes) || ev.homes.length > 4) return "home-duplicate";
|
|
468
|
+
const origins = [];
|
|
469
|
+
for (const h of ev.homes) {
|
|
470
|
+
const o = httpsOrigin(h.url);
|
|
471
|
+
if (!o) return "home-url-required";
|
|
472
|
+
if (origins.includes(o)) return "home-origin-duplicate";
|
|
473
|
+
origins.push(o);
|
|
474
|
+
}
|
|
475
|
+
st.homes = ev.homes;
|
|
476
|
+
}
|
|
477
|
+
if (st.witnesses.length && eligibleHomes(st.homes, st.witnesses).length < 2) return "homes-required";
|
|
478
|
+
return null;
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
switch (e.type) {
|
|
482
|
+
case "inception": {
|
|
483
|
+
if (b) return fail("inception-not-first");
|
|
484
|
+
if (e.lineage !== e.key?.uuaid) return fail("lineage-not-genesis");
|
|
485
|
+
if (!sigBy(e, e.key.uuaid, e.key.publicKey)) return fail("bad-signature");
|
|
486
|
+
const st = { key: e.key, next: e.next, witnesses: [], m: 1, cw: "P7D", homes: [], doors: new Map() };
|
|
487
|
+
const err = setLists(st, e) ?? freshKey(e.key, e.next, [], []);
|
|
488
|
+
if (err) return fail(err);
|
|
489
|
+
for (const dr of e.doors ?? []) {
|
|
490
|
+
if (!dr || !derivesStrict(dr.publicKey, dr.uuaid)) return fail("key-uuaid-mismatch");
|
|
491
|
+
if (st.doors.has(dr.uuaid)) return fail("door-duplicate");
|
|
492
|
+
if (!sigBy(e, dr.uuaid, dr.publicKey)) return fail("door-signature");
|
|
493
|
+
st.doors.set(dr.uuaid, dr);
|
|
494
|
+
}
|
|
495
|
+
const initDoors = [...st.doors.values()].map((x) => x.publicKey);
|
|
496
|
+
if (homeClash(st.homes, [e.key.publicKey, ...initDoors], [e.next])) return fail("home-is-lineage-key");
|
|
497
|
+
return {
|
|
498
|
+
ok: true,
|
|
499
|
+
branch: {
|
|
500
|
+
lineage: e.lineage,
|
|
501
|
+
tip: d,
|
|
502
|
+
st,
|
|
503
|
+
path: [e.key.uuaid],
|
|
504
|
+
used: [e.key.publicKey],
|
|
505
|
+
usedDoors: initDoors,
|
|
506
|
+
usedNext: [e.next],
|
|
507
|
+
recovered: false,
|
|
508
|
+
finalizedList: [],
|
|
509
|
+
openClaim: null,
|
|
510
|
+
tombstoned: false,
|
|
511
|
+
last: e,
|
|
512
|
+
proofOf: null,
|
|
513
|
+
},
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
case "rotation": {
|
|
517
|
+
if (!e.reveal || !derivesStrict(e.reveal.publicKey, e.reveal.uuaid)) return fail("key-uuaid-mismatch");
|
|
518
|
+
if (sha256Hex(e.reveal.publicKey) !== n.st.next) return fail("next-commitment"); // @rule ver.rot-next-commitment
|
|
519
|
+
if (!sigBy(e, e.reveal.uuaid, e.reveal.publicKey) || !sigBy(e, e.key.uuaid, e.key.publicKey)) return fail("bad-signature"); // @rule ver.rot-sigs
|
|
520
|
+
if (e.key.publicKey === e.reveal.publicKey) return fail("key-is-reveal");
|
|
521
|
+
const err = setLists(n.st, e) ?? freshKey(e.key, e.next, [...n.used, e.reveal.publicKey], n.usedNext);
|
|
522
|
+
if (err) return fail(err);
|
|
523
|
+
n.used.push(e.reveal.publicKey, e.key.publicKey);
|
|
524
|
+
n.usedNext.push(e.next);
|
|
525
|
+
if (homeClash(n.st.homes, [...n.used, ...n.usedDoors], n.usedNext)) return fail("home-is-lineage-key");
|
|
526
|
+
n.st.key = e.key;
|
|
527
|
+
n.st.next = e.next;
|
|
528
|
+
n.path.push(e.key.uuaid);
|
|
529
|
+
return ownerEvent();
|
|
530
|
+
}
|
|
531
|
+
case "door.add":
|
|
532
|
+
case "door.retire": {
|
|
533
|
+
const dr = e.door;
|
|
534
|
+
if (!dr || !derivesStrict(dr.publicKey, dr.uuaid)) return fail("key-uuaid-mismatch");
|
|
535
|
+
if (!sigBy(e, n.st.key.uuaid, n.st.key.publicKey)) return fail("bad-signature"); // @rule ver.door-owner-sig
|
|
536
|
+
if (e.type === "door.add") {
|
|
537
|
+
if (n.st.doors.has(dr.uuaid)) return fail("door-duplicate"); // @rule ver.door-duplicate
|
|
538
|
+
if (!sigBy(e, dr.uuaid, dr.publicKey)) return fail("door-signature"); // @rule ver.door-self-sig
|
|
539
|
+
if (homeClash(n.st.homes, [dr.publicKey], [])) return fail("home-is-lineage-key");
|
|
540
|
+
n.st.doors.set(dr.uuaid, dr);
|
|
541
|
+
n.usedDoors.push(dr.publicKey);
|
|
542
|
+
} else {
|
|
543
|
+
if (!n.st.doors.has(dr.uuaid)) return fail("door-unknown"); // @rule ver.door-retire-known
|
|
544
|
+
n.st.doors.delete(dr.uuaid);
|
|
545
|
+
}
|
|
546
|
+
return ownerEvent();
|
|
547
|
+
}
|
|
548
|
+
case "recovery.claim": {
|
|
549
|
+
if (n.openClaim) return fail("claim-already-open"); // @rule ver.claim-not-already-open
|
|
550
|
+
const counted = n.st.witnesses.filter((w) => sigBy(e, w.uuaid, w.publicKey)).length;
|
|
551
|
+
if (n.st.witnesses.length === 0 || counted < n.st.m) return fail("witness-threshold"); // @rule ver.claim-threshold
|
|
552
|
+
if (!sigBy(e, e.key.uuaid, e.key.publicKey)) return fail("bad-signature"); // @rule ver.claim-newkey-sig
|
|
553
|
+
const err = freshKey(e.key, e.next, n.used, n.usedNext);
|
|
554
|
+
if (err) return fail(err);
|
|
555
|
+
if (homeClash(n.st.homes, [e.key.publicKey], [e.next])) return fail("home-is-lineage-key");
|
|
556
|
+
if (!ignoreRecFuture && futureTs && !observedAt(n.lineage, d, n.st.homes, n.st.witnesses)) {
|
|
557
|
+
return fail("claim-future-ts", "valid", { claimTs: e.ts });
|
|
558
|
+
}
|
|
559
|
+
Object.assign(n, { tip: d, last: e, cancelled: null, openClaim: { d, ev: e, witnesses: n.st.witnesses, homes: n.st.homes, cw: n.st.cw } });
|
|
560
|
+
return { ok: true, branch: n };
|
|
561
|
+
}
|
|
562
|
+
case "recovery.veto": {
|
|
563
|
+
if (!n.openClaim || e.claim !== n.openClaim.d) return fail("veto-without-claim"); // @rule ver.veto-matches-claim
|
|
564
|
+
const curOk = sigBy(e, n.st.key.uuaid, n.st.key.publicKey);
|
|
565
|
+
const nextOk =
|
|
566
|
+
typeof e.nextKey === "string" &&
|
|
567
|
+
HEX64_RE.test(e.nextKey) &&
|
|
568
|
+
sha256Hex(e.nextKey) === n.st.next &&
|
|
569
|
+
(e.sigs ?? []).some((x) => isPlainObject(x) && derivesStrict(e.nextKey, x.by) && typeof x.sig === "string" && HEX128_RE.test(x.sig) && verifyLineageDoc(e, e.nextKey, x.sig));
|
|
570
|
+
if (!curOk && !nextOk) return fail("veto-not-owner"); // @rule ver.veto-owner-or-next
|
|
571
|
+
return ownerEvent();
|
|
572
|
+
}
|
|
573
|
+
case "recovery.final": {
|
|
574
|
+
if (!n.openClaim || e.claim !== n.openClaim.d) return fail("final-without-open-claim"); // @rule ver.final-matches-claim
|
|
575
|
+
const nk = n.openClaim.ev.key;
|
|
576
|
+
if (!sigBy(e, nk.uuaid, nk.publicKey)) return fail("bad-signature"); // @rule ver.final-newkey-sig
|
|
577
|
+
const obs = observedAt(n.lineage, n.openClaim.d, n.openClaim.homes, n.openClaim.witnesses);
|
|
578
|
+
if (!obs) return fail("claim-unobserved", "indeterminate"); // @rule ver.final-requires-corroboration
|
|
579
|
+
const effectiveAt = obs.at + durMs(n.openClaim.cw);
|
|
580
|
+
if (nowMs < effectiveAt || (!ignoreRecFuture && futureTs)) {
|
|
581
|
+
const finExtra = { claimEffectiveAt: new Date(effectiveAt).toISOString(), observedBy: obs.source };
|
|
582
|
+
return fail("final-not-yet-effective", "pending-recovery", finExtra); // @rule ver.final-contest-window
|
|
583
|
+
}
|
|
584
|
+
const oc = n.openClaim;
|
|
585
|
+
const finRec = { d: oc.d, effectiveAt, claim: oc };
|
|
586
|
+
Object.assign(n, { tip: d, last: e, cancelled: null, openClaim: null, recovered: true, finalized: finRec, finalizedList: [...n.finalizedList, finRec] });
|
|
587
|
+
n.st.key = nk;
|
|
588
|
+
n.st.next = oc.ev.next;
|
|
589
|
+
n.path.push(nk.uuaid);
|
|
590
|
+
n.used.push(nk.publicKey);
|
|
591
|
+
n.usedNext.push(oc.ev.next);
|
|
592
|
+
return { ok: true, branch: n };
|
|
593
|
+
}
|
|
594
|
+
case "bind": {
|
|
595
|
+
if (!allowBind) return fail("unsupported-event:bind", "indeterminate");
|
|
596
|
+
if (!isPlainObject(e.legacy)) return fail("malformed-artifact");
|
|
597
|
+
if (!sigBy(e, n.st.key.uuaid, n.st.key.publicKey)) return fail("bad-signature"); // @rule ver.bind-owner-sig
|
|
598
|
+
return ownerEvent();
|
|
599
|
+
}
|
|
600
|
+
case "tombstone": {
|
|
601
|
+
if (!sigBy(e, n.st.key.uuaid, n.st.key.publicKey)) return fail("tombstone-not-current-key"); // @rule ver.tombstone-owner-sig
|
|
602
|
+
return ownerEvent({ tombstoned: true });
|
|
603
|
+
}
|
|
604
|
+
default:
|
|
605
|
+
return fail(`unsupported-event:${String(e.type)}`, "indeterminate");
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
const TIMING = new Set(["claim-unobserved", "final-not-yet-effective", "claim-future-ts"]);
|
|
610
|
+
const roots = [];
|
|
611
|
+
const deferred = new Map();
|
|
612
|
+
for (let s = 0; s <= maxSeq; s++) {
|
|
613
|
+
const failures = [];
|
|
614
|
+
let any = false;
|
|
615
|
+
const entries = [...bySeq.get(s).entries()].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
616
|
+
for (const [d, e] of entries) {
|
|
617
|
+
if (s > 0 && deferred.has(e.prev)) {
|
|
618
|
+
const f0 = deferred.get(e.prev);
|
|
619
|
+
const f = { ...f0, seq: s, event: d, reason: "parent-deferred", parent: null, timing: true };
|
|
620
|
+
deferred.set(d, f);
|
|
621
|
+
failures.push(f);
|
|
622
|
+
continue;
|
|
623
|
+
}
|
|
624
|
+
const parent = s === 0 ? null : validNodes.get(e.prev);
|
|
625
|
+
if (s === 0 ? e.prev !== null : !parent || parent.seq !== s - 1) {
|
|
626
|
+
failures.push({ seq: s, event: d, verdict: "invalid", reason: s === 0 ? "prev-not-null" : "prev-mismatch", extra: {}, parent: null });
|
|
627
|
+
continue;
|
|
628
|
+
}
|
|
629
|
+
const r = apply(parent?.branch ?? null, e, d);
|
|
630
|
+
if (!r.ok) {
|
|
631
|
+
const f = { seq: s, event: d, verdict: r.verdict, reason: r.reason, extra: r.extra, parent, timing: TIMING.has(r.reason) };
|
|
632
|
+
if (f.timing) deferred.set(d, f);
|
|
633
|
+
failures.push(f);
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
const node = { d, e, seq: s, parent, branch: r.branch, kids: [] };
|
|
637
|
+
validNodes.set(d, node);
|
|
638
|
+
any = true;
|
|
639
|
+
if (parent) parent.kids.push(node);
|
|
640
|
+
else roots.push(node);
|
|
641
|
+
}
|
|
642
|
+
const hard = failures.filter((f) => !f.timing);
|
|
643
|
+
if (!any && hard.length) {
|
|
644
|
+
const f = hard[0];
|
|
645
|
+
const p = f.parent?.branch;
|
|
646
|
+
return out(f.verdict, [f.reason], { failedAtSeq: s, ...(p ? { current: p.st.key.uuaid, keyPath: p.path } : {}), ...f.extra });
|
|
647
|
+
}
|
|
648
|
+
for (const f of failures) discarded.push({ seq: f.seq, event: f.event, reason: f.reason });
|
|
649
|
+
}
|
|
650
|
+
if (!roots.length) return out("invalid", ["no-inception"], { failedAtSeq: 0 });
|
|
651
|
+
if (new Set(roots.map((r) => r.branch.lineage)).size > 1) return out("invalid", ["lineage-mismatch"], { failedAtSeq: 0 });
|
|
652
|
+
|
|
653
|
+
// ---- Epoch resolution (`r2.5b'''`) ----
|
|
654
|
+
const isExit = (x) => x.e.type === "rotation" || x.e.type === "recovery.final";
|
|
655
|
+
const STATEFUL = new Set(["door.add", "door.retire", "tombstone"]);
|
|
656
|
+
const ancestorsOf = (x) => {
|
|
657
|
+
const a = new Set();
|
|
658
|
+
for (let p = x.parent; p; p = p.parent) a.add(p);
|
|
659
|
+
return a;
|
|
660
|
+
};
|
|
661
|
+
const lca = (a, b) => {
|
|
662
|
+
const aa = new Set([a, ...ancestorsOf(a)]);
|
|
663
|
+
for (let y = b; y; y = y.parent) if (aa.has(y)) return y;
|
|
664
|
+
return null;
|
|
665
|
+
};
|
|
666
|
+
const pathFrom = (E, x) => {
|
|
667
|
+
const p = [];
|
|
668
|
+
for (let y = x; y && y !== E; y = y.parent) p.unshift(y);
|
|
669
|
+
return p;
|
|
670
|
+
};
|
|
671
|
+
const dropped = new Set();
|
|
672
|
+
const drop = (xs, reason) => {
|
|
673
|
+
for (const x of xs) {
|
|
674
|
+
if (!dropped.has(x.d)) {
|
|
675
|
+
dropped.add(x.d);
|
|
676
|
+
discarded.push({ seq: x.seq, event: x.d, reason });
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
const byUuaid = (a, b) => (a.uuaid < b.uuaid ? -1 : a.uuaid > b.uuaid ? 1 : 0);
|
|
681
|
+
const fp = (b) =>
|
|
682
|
+
jcs({
|
|
683
|
+
k: b.st.key,
|
|
684
|
+
n: b.st.next,
|
|
685
|
+
w: [...b.st.witnesses].sort(byUuaid),
|
|
686
|
+
m: b.st.m,
|
|
687
|
+
cw: b.st.cw,
|
|
688
|
+
h: [...b.st.homes].sort(byUuaid),
|
|
689
|
+
d: [...b.st.doors.values()].sort(byUuaid),
|
|
690
|
+
t: !!b.tombstoned,
|
|
691
|
+
r: !!b.recovered,
|
|
692
|
+
p: b.path,
|
|
693
|
+
});
|
|
694
|
+
const deepest = (xs) => [...xs].sort((a, b) => b.seq - a.seq || (a.d < b.d ? -1 : 1));
|
|
695
|
+
const forkSeqOf = (xs) => {
|
|
696
|
+
const anc = xs.map((x) => new Set([x, ...ancestorsOf(x)]));
|
|
697
|
+
let common = null;
|
|
698
|
+
for (let p = xs[0]; p; p = p.parent) {
|
|
699
|
+
if (anc.every((a) => a.has(p))) {
|
|
700
|
+
common = p;
|
|
701
|
+
break;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
return common ? common.seq + 1 : 0;
|
|
705
|
+
};
|
|
706
|
+
const dropOffPath = (E, path, keepKid = () => false, reasonFor) => {
|
|
707
|
+
const on = new Set([E, ...path]);
|
|
708
|
+
for (const x of [E, ...path]) {
|
|
709
|
+
for (const k of x.kids) {
|
|
710
|
+
if (!on.has(k) && !keepKid(x, k)) drop([k], reasonFor(k));
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
const branchesFact = (xs) => xs.map((x) => ({ event: x.d, type: x.e.type, current: x.branch.st.key.uuaid }));
|
|
715
|
+
|
|
716
|
+
function combine(results, seq) {
|
|
717
|
+
const bad = results.find((r) => r.contested);
|
|
718
|
+
if (bad) return bad;
|
|
719
|
+
let active = results;
|
|
720
|
+
if (results.length > 1 && new Set(results.map((r) => fp(r.entry.branch))).size === 1) {
|
|
721
|
+
const advanced = results.filter((r) => r.tip !== r.entry || r.pending.length > 0);
|
|
722
|
+
if (advanced.length) {
|
|
723
|
+
drop(results.filter((r) => !advanced.includes(r)).map((r) => r.tip), "equivalent-branch");
|
|
724
|
+
active = advanced;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
if (new Set(active.map((r) => fp(r.tip.branch))).size > 1) {
|
|
728
|
+
return { contested: true, reasons: [`fork-at-seq:${seq}`], branches: branchesFact(active.map((r) => r.tip)) };
|
|
729
|
+
}
|
|
730
|
+
const w = [...active].sort((a, b) => b.tip.seq - a.tip.seq || (a.tip.d < b.tip.d ? -1 : 1))[0];
|
|
731
|
+
drop(active.filter((r) => r !== w).map((r) => r.tip), "equivalent-branch");
|
|
732
|
+
return { entry: w.entry, tip: w.tip, pending: active.flatMap((r) => r.pending) };
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
const hasChangeBelow = (x) =>
|
|
736
|
+
x.kids.some((k) => k.e.type !== "recovery.final" && (k.e.type === "rotation" || STATEFUL.has(k.e.type) || hasChangeBelow(k)));
|
|
737
|
+
|
|
738
|
+
const yieldedToParent = (p, c, oc, corrAt) => {
|
|
739
|
+
const P = c.parent;
|
|
740
|
+
const L = lca(p, c);
|
|
741
|
+
if (!P || !L || L === c || L === P) return false;
|
|
742
|
+
const pStart = Math.min(
|
|
743
|
+
corrAt(p, oc) ?? Infinity,
|
|
744
|
+
p.parent?.e.type === "recovery.claim" ? (corrAt(p.parent, p.parent.branch.openClaim) ?? Infinity) : Infinity,
|
|
745
|
+
);
|
|
746
|
+
if (pathFrom(L, P).some((y) => STATEFUL.has(y.e.type)) && !pathFrom(L, p).some((y) => y.e.type === "rotation" || STATEFUL.has(y.e.type)) && !hasChangeBelow(p)) {
|
|
747
|
+
const pStateTimes = pathFrom(L, P)
|
|
748
|
+
.filter((y) => STATEFUL.has(y.e.type))
|
|
749
|
+
.map((y) => corrAt(y, oc))
|
|
750
|
+
.filter((t) => t !== undefined);
|
|
751
|
+
return pStateTimes.length > 0 && pStart < Math.max(...pStateTimes);
|
|
752
|
+
}
|
|
753
|
+
if (!p.kids.some((k) => k.e.type !== "recovery.final") && fp(p.branch) === fp(P.branch) && (P.seq > p.seq || (P.seq === p.seq && P.d < p.d))) {
|
|
754
|
+
return true;
|
|
755
|
+
}
|
|
756
|
+
return false;
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
function resolveEpoch(E) {
|
|
760
|
+
const S = E.branch;
|
|
761
|
+
const K = S.st.key.uuaid;
|
|
762
|
+
const nodes = [];
|
|
763
|
+
const walk = (x) => {
|
|
764
|
+
for (const k of x.kids) {
|
|
765
|
+
nodes.push(k);
|
|
766
|
+
if (!isExit(k)) walk(k);
|
|
767
|
+
}
|
|
768
|
+
};
|
|
769
|
+
walk(E);
|
|
770
|
+
const claims = nodes.filter((x) => x.e.type === "recovery.claim");
|
|
771
|
+
const proofs = nodes.filter((x) => x.branch.proofOf === K);
|
|
772
|
+
const rotations = nodes.filter((x) => x.e.type === "rotation");
|
|
773
|
+
|
|
774
|
+
const corrAt = (x, oc) => {
|
|
775
|
+
const ts = homeTimes(S.lineage, x.d, oc.homes, oc.witnesses);
|
|
776
|
+
return ts.length >= 2 ? ts[1] : undefined;
|
|
777
|
+
};
|
|
778
|
+
const subtreeOf = (x) => {
|
|
779
|
+
const s = new Set([x]);
|
|
780
|
+
const w = (y) => {
|
|
781
|
+
for (const k of y.kids) {
|
|
782
|
+
s.add(k);
|
|
783
|
+
w(k);
|
|
784
|
+
}
|
|
785
|
+
};
|
|
786
|
+
w(x);
|
|
787
|
+
return s;
|
|
788
|
+
};
|
|
789
|
+
|
|
790
|
+
const judged = [];
|
|
791
|
+
const order = claims
|
|
792
|
+
.map((c) => ({ c, obs: observedAt(S.lineage, c.d, c.branch.openClaim.homes, c.branch.openClaim.witnesses) }))
|
|
793
|
+
.sort((a, b) => (a.obs?.at ?? Infinity) - (b.obs?.at ?? Infinity) || (a.c.d < b.c.d ? -1 : 1));
|
|
794
|
+
|
|
795
|
+
for (const { c, obs } of order) {
|
|
796
|
+
const oc = c.branch.openClaim;
|
|
797
|
+
const anc = ancestorsOf(c);
|
|
798
|
+
const sub = subtreeOf(c);
|
|
799
|
+
const O = obs?.at;
|
|
800
|
+
const Ec = obs ? O + durMs(oc.cw) : undefined;
|
|
801
|
+
let stale = null;
|
|
802
|
+
if (O !== undefined) {
|
|
803
|
+
if (rotations.some((r) => !sub.has(r) && (corrAt(r, oc) ?? Infinity) < O)) stale = "epoch-rotated";
|
|
804
|
+
else if (judged.some((j) => j.status === "effective" && j.Ec <= O)) stale = "epoch-recovered";
|
|
805
|
+
else {
|
|
806
|
+
const Rcorr = new Set();
|
|
807
|
+
for (const x of nodes) {
|
|
808
|
+
if (!sub.has(x) && x.branch.proofOf === K && x.e.type !== "rotation" && (corrAt(x, oc) ?? Infinity) < O) {
|
|
809
|
+
for (let y = x; y && y !== E; y = y.parent) Rcorr.add(y);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
let leaves = [...Rcorr].filter((x) => x.e.type !== "recovery.claim" && !x.kids.some((k) => Rcorr.has(k)));
|
|
813
|
+
if (!leaves.length) leaves = [E];
|
|
814
|
+
const chg = leaves.filter((x) => pathFrom(E, x).some((y) => STATEFUL.has(y.e.type)));
|
|
815
|
+
if (chg.length) {
|
|
816
|
+
const fps = new Set(chg.map((x) => fp(x.branch)));
|
|
817
|
+
const cChain = new Set([E, ...pathFrom(E, c).slice(0, -1)]);
|
|
818
|
+
if (fps.size === 1 && [...fps][0] !== fp(c.parent.branch) && !chg.some((h) => cChain.has(h))) stale = "parent-not-head";
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
const cts = homeTimes(S.lineage, c.d, oc.homes, oc.witnesses);
|
|
823
|
+
const F = cts[0];
|
|
824
|
+
const oRobust = cts.length >= 3;
|
|
825
|
+
const history = (p) => {
|
|
826
|
+
if (sub.has(p)) return false;
|
|
827
|
+
if (O === undefined) return false;
|
|
828
|
+
const cp = corrAt(p, oc);
|
|
829
|
+
const beforeF = F !== undefined && cp !== undefined && cp < F - LINEAGE_SKEW_MS;
|
|
830
|
+
if (cp !== undefined && cp < O - LINEAGE_SKEW_MS) {
|
|
831
|
+
let old = yieldedToParent(p, c, oc, corrAt);
|
|
832
|
+
if (!old && p.e.type === "recovery.veto" && p.parent?.e.type === "recovery.claim") {
|
|
833
|
+
const oc0 = p.parent.branch.openClaim;
|
|
834
|
+
const closedAt = Math.min(corrAt(p.parent, oc0) ?? Infinity, cp) + durMs(oc0.cw);
|
|
835
|
+
old = closedAt <= O;
|
|
836
|
+
}
|
|
837
|
+
if (old) return oRobust || beforeF ? true : "ambiguous";
|
|
838
|
+
}
|
|
839
|
+
if (F === undefined) return false;
|
|
840
|
+
const pts = homeTimes(S.lineage, p.d, oc.homes, oc.witnesses);
|
|
841
|
+
return beforeF || (pts.length > 0 && Math.max(...pts) < F - LINEAGE_SKEW_MS);
|
|
842
|
+
};
|
|
843
|
+
const ts = stale
|
|
844
|
+
? []
|
|
845
|
+
: proofs
|
|
846
|
+
.filter((p) => !anc.has(p))
|
|
847
|
+
.map((p) => [p, history(p)])
|
|
848
|
+
.filter(([, h]) => h !== true)
|
|
849
|
+
.map(([p, h]) => (h === "ambiguous" ? { cls: "unknown", why: "claim-receipts-ambiguous" } : classify(S.lineage, p.d, Ec, oc.homes, oc.witnesses)));
|
|
850
|
+
let status;
|
|
851
|
+
if (stale) status = "stale";
|
|
852
|
+
else if (ts.some((t) => t.cls === "timely")) status = "defeated";
|
|
853
|
+
else if (Ec !== undefined && nowMs >= Ec) status = ts.some((t) => t.cls === "unknown") ? "disputed" : "effective";
|
|
854
|
+
else status = "pending";
|
|
855
|
+
c.judgment = { status, O, Ec, stale, why: ts.find((t) => t.cls === "unknown")?.why };
|
|
856
|
+
judged.push(c.judgment);
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
for (const c of claims) {
|
|
860
|
+
if (c.judgment.status === "stale") drop(c.kids.filter((x) => x.e.type === "recovery.final"), `claim-stale:${c.judgment.stale}`);
|
|
861
|
+
}
|
|
862
|
+
const disputed = claims.find((c) => c.judgment.status === "disputed");
|
|
863
|
+
if (disputed) return { contested: true, reasons: [`fork-at-seq:${disputed.seq}`, disputed.judgment.why], branches: branchesFact([disputed]) };
|
|
864
|
+
const eff = claims.filter((c) => c.judgment.status === "effective");
|
|
865
|
+
if (eff.length >= 2) return { contested: true, reasons: [`fork-at-seq:${forkSeqOf(eff)}`, "multiple-effective-claims"], branches: branchesFact(eff) };
|
|
866
|
+
const pendNodes = claims.filter((c) => c.judgment.status === "pending");
|
|
867
|
+
const overlapPend = pendNodes.filter((p) => p.judgment.O !== undefined);
|
|
868
|
+
if (eff.length === 1 && overlapPend.length) return { entry: E, tip: E, pending: [eff[0], ...overlapPend] };
|
|
869
|
+
if (eff.length === 1) {
|
|
870
|
+
const c = eff[0];
|
|
871
|
+
const finals = c.kids.filter((x) => x.e.type === "recovery.final");
|
|
872
|
+
dropOffPath(E, pathFrom(E, c), (x, k) => x === c && finals.includes(k), (k) => (k.branch.proofOf === K ? "owner-event-too-late" : "superseded-by-recovery"));
|
|
873
|
+
if (!finals.length) return { entry: E, tip: c, pending: [c] };
|
|
874
|
+
return combine(finals.map((f) => ({ ...resolveEpoch(f), entry: f })), c.seq + 1);
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
for (const c of claims) {
|
|
878
|
+
if (c.judgment.status === "defeated") drop(c.kids.filter((x) => x.e.type === "recovery.final"), "final-overridden-by-owner");
|
|
879
|
+
}
|
|
880
|
+
const liveKids = (x) => x.kids.filter((k) => k.e.type !== "recovery.final");
|
|
881
|
+
const isPend = (k) => pendNodes.includes(k);
|
|
882
|
+
const onPendPath = (k) => pendNodes.some((p) => p === k || ancestorsOf(p).has(k));
|
|
883
|
+
|
|
884
|
+
if (rotations.length) {
|
|
885
|
+
let w = rotations[0];
|
|
886
|
+
if (rotations.length > 1) {
|
|
887
|
+
const ranked = rotations
|
|
888
|
+
.map((r) => ({ r, t: corroboratedAt(S.lineage, r.d, S) }))
|
|
889
|
+
.filter((x) => x.t !== undefined)
|
|
890
|
+
.sort((a, b) => a.t - b.t);
|
|
891
|
+
if (!ranked.length || (ranked.length > 1 && ranked[0].t === ranked[1].t)) {
|
|
892
|
+
return { contested: true, reasons: [`fork-at-seq:${forkSeqOf(rotations)}`, "rotation-equivocation"], branches: branchesFact(rotations) };
|
|
893
|
+
}
|
|
894
|
+
w = ranked[0].r;
|
|
895
|
+
}
|
|
896
|
+
dropOffPath(E, pathFrom(E, w), (x, k) => onPendPath(k) || x === w, (k) => (rotations.includes(k) ? "rotation-not-first-seen" : "superseded-by-rotation")); // @rule ver.rotation-supersedes-k-only
|
|
897
|
+
const r = resolveEpoch(w);
|
|
898
|
+
return r.contested ? r : { ...r, entry: E, pending: [...pendNodes, ...r.pending] };
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
const tips = [];
|
|
902
|
+
const visit = (x) => {
|
|
903
|
+
const ks = liveKids(x).filter((k) => {
|
|
904
|
+
if (isPend(k)) return false;
|
|
905
|
+
if (k.e.type === "recovery.claim" && !liveKids(k).length) {
|
|
906
|
+
drop([k], k.judgment?.status === "stale" ? `claim-stale:${k.judgment.stale}` : "claim-defeated");
|
|
907
|
+
return false;
|
|
908
|
+
}
|
|
909
|
+
return true;
|
|
910
|
+
});
|
|
911
|
+
if (!ks.length) tips.push(x);
|
|
912
|
+
else for (const k of ks) visit(k);
|
|
913
|
+
};
|
|
914
|
+
visit(E);
|
|
915
|
+
let cand = tips;
|
|
916
|
+
const changing = (x) => pathFrom(E, x).some((y) => STATEFUL.has(y.e.type));
|
|
917
|
+
if (cand.length > 1 && cand.some(changing)) {
|
|
918
|
+
drop(cand.filter((x) => !changing(x)), "cancellation-branch");
|
|
919
|
+
cand = cand.filter(changing);
|
|
920
|
+
}
|
|
921
|
+
if (new Set(cand.map((x) => fp(x.branch))).size > 1) {
|
|
922
|
+
return { contested: true, reasons: [`fork-at-seq:${forkSeqOf(cand)}`], branches: branchesFact(cand) };
|
|
923
|
+
}
|
|
924
|
+
const [w, ...rest] = deepest(cand);
|
|
925
|
+
drop(rest, "equivalent-branch");
|
|
926
|
+
return { entry: E, tip: w, pending: pendNodes };
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
const top = roots.length === 1 ? resolveEpoch(roots[0]) : combine(roots.map((r) => ({ ...resolveEpoch(r), entry: r })), 0);
|
|
930
|
+
if (top.contested) {
|
|
931
|
+
const rootElig = eligibleHomes(roots[0].branch.st.homes, roots[0].branch.st.witnesses).map((h) => h.uuaid);
|
|
932
|
+
return out("contested", top.reasons.filter(Boolean), { branches: top.branches, eligibleHomes: rootElig });
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
const b = top.tip.branch;
|
|
936
|
+
const tipElig = eligibleHomes(b.st.homes, b.st.witnesses).map((h) => h.uuaid);
|
|
937
|
+
const recoveries =
|
|
938
|
+
b.recovered && b.finalizedList?.length
|
|
939
|
+
? b.finalizedList.map((f) => ({ claim: `sha256:${f.d}`, eligibleHomes: eligibleHomes(f.claim.homes, f.claim.witnesses).map((h) => h.uuaid) }))
|
|
940
|
+
: [];
|
|
941
|
+
const facts = {
|
|
942
|
+
current: b.st.key.uuaid,
|
|
943
|
+
keyPath: b.path,
|
|
944
|
+
head: `sha256:${top.tip.d}`,
|
|
945
|
+
doors: [...b.st.doors.values()].filter((x) => !x.notAfter || Date.parse(x.notAfter) > nowMs).map((x) => x.uuaid),
|
|
946
|
+
eligibleHomes: tipElig,
|
|
947
|
+
...(recoveries.length ? { recoveries, recoveredBy: recoveries[recoveries.length - 1] } : {}),
|
|
948
|
+
...(b.tombstoned ? { tombstoned: true } : {}),
|
|
949
|
+
};
|
|
950
|
+
|
|
951
|
+
const open = top.pending;
|
|
952
|
+
if (open.length) {
|
|
953
|
+
const timing = {};
|
|
954
|
+
let anyObserved = false;
|
|
955
|
+
for (const c of open) {
|
|
956
|
+
const oc = c.branch.openClaim;
|
|
957
|
+
const obs = observedAt(b.lineage, c.d, oc.homes, oc.witnesses);
|
|
958
|
+
const Ec = obs ? obs.at + durMs(oc.cw) : undefined;
|
|
959
|
+
if (obs) anyObserved = true;
|
|
960
|
+
timing[c.d] = obs ? { claimEffectiveAt: new Date(Ec).toISOString(), observedBy: obs.source, effective: nowMs >= Ec } : { claimObserved: false };
|
|
961
|
+
}
|
|
962
|
+
const deferredOf = [...deferred.values()]
|
|
963
|
+
.filter((f) => f.reason !== "parent-deferred" && open.includes(f.parent))
|
|
964
|
+
.sort((a, b) => (a.reason === b.reason ? (a.event < b.event ? -1 : 1) : a.reason === "final-not-yet-effective" ? -1 : 1));
|
|
965
|
+
const deferredFinal = deferredOf.find((f) => f.reason === "final-not-yet-effective") ?? (anyObserved ? null : deferredOf[0] ?? null);
|
|
966
|
+
const first = open[0].d;
|
|
967
|
+
const more = { claim: first, ...(open.length > 1 ? { claims: open.map((c) => c.d) } : {}), ...timing[first], claimTiming: timing };
|
|
968
|
+
if (deferredFinal) return out(deferredFinal.verdict, [deferredFinal.reason], { ...facts, ...more, ...deferredFinal.extra });
|
|
969
|
+
return out("pending-recovery", [], { ...facts, ...more });
|
|
970
|
+
}
|
|
971
|
+
return out(b.recovered ? "recovered" : "valid", [], facts);
|
|
972
|
+
}
|