@agent-blueprint-protocol/verifier 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +891 -0
- package/LICENSE +190 -0
- package/NOTICE +4 -0
- package/README.md +199 -0
- package/dist/corpus/cases/base64url-decode.json +1 -0
- package/dist/corpus/cases/blueprint-decode.json +1 -0
- package/dist/corpus/cases/bounds-new.json +1 -0
- package/dist/corpus/cases/bounds_algebra-intersect.json +1 -0
- package/dist/corpus/cases/canonicalization-encode.json +1 -0
- package/dist/corpus/cases/compatibility-verify.json +1 -0
- package/dist/corpus/cases/deployment-decode.json +1 -0
- package/dist/corpus/cases/digest-tagged.json +1 -0
- package/dist/corpus/cases/extension-resolve.json +1 -0
- package/dist/corpus/cases/federation-decode.json +1 -0
- package/dist/corpus/cases/federation-verify_commitment.json +1 -0
- package/dist/corpus/cases/json-decode.json +1 -0
- package/dist/corpus/cases/negotiation-negotiate.json +1 -0
- package/dist/corpus/cases/portability-scan.json +1 -0
- package/dist/corpus/cases/schema-validate_instance.json +1 -0
- package/dist/corpus/cases/signature-verify.json +1 -0
- package/dist/corpus/index.json +1 -0
- package/dist/corpus/schemas/estate-contract.schema.json +1 -0
- package/dist/corpus/schemas/object.schema.json +1 -0
- package/dist/corpus/vectors/blueprint-golden.json +1 -0
- package/dist/corpus/vectors/deployment-golden.json +1 -0
- package/dist/corpus/vectors/rfc8785-numbers.json +1 -0
- package/dist/kit/cli.mjs +24 -0
- package/dist/verifier/b64url.d.ts +16 -0
- package/dist/verifier/b64url.js +36 -0
- package/dist/verifier/blueprint.d.ts +56 -0
- package/dist/verifier/blueprint.js +795 -0
- package/dist/verifier/bounds.d.ts +19 -0
- package/dist/verifier/bounds.js +45 -0
- package/dist/verifier/bounds_algebra.d.ts +27 -0
- package/dist/verifier/bounds_algebra.js +282 -0
- package/dist/verifier/canonical.d.ts +17 -0
- package/dist/verifier/canonical.js +160 -0
- package/dist/verifier/cli.d.ts +1 -0
- package/dist/verifier/cli.js +149 -0
- package/dist/verifier/compatibility.d.ts +22 -0
- package/dist/verifier/compatibility.js +112 -0
- package/dist/verifier/corpus.d.ts +42 -0
- package/dist/verifier/corpus.js +672 -0
- package/dist/verifier/decode.d.ts +16 -0
- package/dist/verifier/decode.js +457 -0
- package/dist/verifier/deployment.d.ts +49 -0
- package/dist/verifier/deployment.js +830 -0
- package/dist/verifier/digest.d.ts +23 -0
- package/dist/verifier/digest.js +58 -0
- package/dist/verifier/extension.d.ts +11 -0
- package/dist/verifier/extension.js +75 -0
- package/dist/verifier/federation.d.ts +76 -0
- package/dist/verifier/federation.js +817 -0
- package/dist/verifier/negotiation.d.ts +29 -0
- package/dist/verifier/negotiation.js +324 -0
- package/dist/verifier/portability.d.ts +12 -0
- package/dist/verifier/portability.js +184 -0
- package/dist/verifier/predicate.d.ts +10 -0
- package/dist/verifier/predicate.js +172 -0
- package/dist/verifier/registry.d.ts +16 -0
- package/dist/verifier/registry.js +120 -0
- package/dist/verifier/registry_engine.d.ts +42 -0
- package/dist/verifier/registry_engine.js +220 -0
- package/dist/verifier/report.d.ts +13 -0
- package/dist/verifier/report.js +29 -0
- package/dist/verifier/run_corpus.d.ts +1 -0
- package/dist/verifier/run_corpus.js +81 -0
- package/dist/verifier/runner.d.ts +20 -0
- package/dist/verifier/runner.js +778 -0
- package/dist/verifier/schema.d.ts +20 -0
- package/dist/verifier/schema.js +623 -0
- package/dist/verifier/self_checks.d.ts +1 -0
- package/dist/verifier/self_checks.js +316 -0
- package/dist/verifier/signature.d.ts +23 -0
- package/dist/verifier/signature.js +226 -0
- package/dist/verifier/value.d.ts +44 -0
- package/dist/verifier/value.js +77 -0
- package/package.json +42 -0
|
@@ -0,0 +1,817 @@
|
|
|
1
|
+
// The federation profile — the mirror of AgentBlueprintProtocol.Federation:
|
|
2
|
+
// the 23-member TaskEnvelope decoded through the registry engine (carrier-key
|
|
3
|
+
// placement laws, R1-R4 root hooks), the lossy-aware A2A/MCP state codecs
|
|
4
|
+
// (federation_state_unmappable, never a silent degrade), the bounded
|
|
5
|
+
// from_value reconstruction path, and verify_commitment's numbered steps
|
|
6
|
+
// (terminal members → JWS verify → binding → commitment recompute → context
|
|
7
|
+
// compare → prior-receipt conflict → signed freshness).
|
|
8
|
+
import { MAXIMA as CEILINGS, coerce as coerceBounds, maximum as maxBounds } from "./bounds.js";
|
|
9
|
+
import * as blueprint from "./blueprint.js";
|
|
10
|
+
import * as canonical from "./canonical.js";
|
|
11
|
+
import * as digest from "./digest.js";
|
|
12
|
+
import { entry as compiledEntry } from "./registry.js";
|
|
13
|
+
import { field, keyfind, validate } from "./registry_engine.js";
|
|
14
|
+
import * as portability from "./portability.js";
|
|
15
|
+
import * as signature from "./signature.js";
|
|
16
|
+
const IDENTIFIER_BYTES = 512;
|
|
17
|
+
const SEGMENT = /^[a-z0-9][a-z0-9._-]*$/;
|
|
18
|
+
const Z_FORM = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/;
|
|
19
|
+
const CLASSIFICATION = ["public", "internal", "confidential", "restricted"];
|
|
20
|
+
const REGULATED_MARKERS = ["pci", "phi"];
|
|
21
|
+
const CLAIM_KINDS = ["correlation", "binding", "authority"];
|
|
22
|
+
const CHECKPOINT_KINDS = ["input_required", "auth_required"];
|
|
23
|
+
const CHECKPOINT_STATUSES = ["submitted", "working", "input_required", "auth_required"];
|
|
24
|
+
const TERMINAL_STATES = ["completed", "failed", "canceled", "rejected"];
|
|
25
|
+
const TERMINAL_ATOMS = ["completed", "failed", "canceled", "rejected"];
|
|
26
|
+
const NATIVE_MEMBERS = ["task_identity", "recovery_handle", "checkpoint_status", "terminal_state"];
|
|
27
|
+
const A2A_TO_LOGICAL = {
|
|
28
|
+
TASK_STATE_SUBMITTED: "submitted",
|
|
29
|
+
TASK_STATE_WORKING: "working",
|
|
30
|
+
TASK_STATE_COMPLETED: "completed",
|
|
31
|
+
TASK_STATE_FAILED: "failed",
|
|
32
|
+
TASK_STATE_CANCELED: "canceled",
|
|
33
|
+
TASK_STATE_INPUT_REQUIRED: "input_required",
|
|
34
|
+
TASK_STATE_REJECTED: "rejected",
|
|
35
|
+
TASK_STATE_AUTH_REQUIRED: "auth_required",
|
|
36
|
+
};
|
|
37
|
+
const LOGICAL_TO_A2A = Object.fromEntries(Object.entries(A2A_TO_LOGICAL).map(([k, v]) => [v, k]));
|
|
38
|
+
const MCP_TO_LOGICAL = {
|
|
39
|
+
working: "working",
|
|
40
|
+
input_required: "input_required",
|
|
41
|
+
completed: "completed",
|
|
42
|
+
failed: "failed",
|
|
43
|
+
cancelled: "canceled",
|
|
44
|
+
};
|
|
45
|
+
const LOGICAL_TO_MCP = {
|
|
46
|
+
working: "working",
|
|
47
|
+
input_required: "input_required",
|
|
48
|
+
completed: "completed",
|
|
49
|
+
failed: "failed",
|
|
50
|
+
canceled: "cancelled",
|
|
51
|
+
};
|
|
52
|
+
// The runner's @logical_states gate: to_* codecs receive a wire spelling of
|
|
53
|
+
// one of the eight logical states.
|
|
54
|
+
const WIRE_LOGICAL_STATES = [
|
|
55
|
+
"submitted",
|
|
56
|
+
"working",
|
|
57
|
+
"completed",
|
|
58
|
+
"failed",
|
|
59
|
+
"canceled",
|
|
60
|
+
"rejected",
|
|
61
|
+
"input_required",
|
|
62
|
+
"auth_required",
|
|
63
|
+
];
|
|
64
|
+
const enumSet = (values) => new Set(values);
|
|
65
|
+
// ---- the field table (23 members, bijective with the mapping rows) ---------------
|
|
66
|
+
function classificationElement() {
|
|
67
|
+
return {
|
|
68
|
+
object: {
|
|
69
|
+
members: [
|
|
70
|
+
field("level", { enum: enumSet(CLASSIFICATION) }),
|
|
71
|
+
field("markers", { array: { kind: { enum: enumSet(REGULATED_MARKERS) } } }, {
|
|
72
|
+
uniqueBy: ":value",
|
|
73
|
+
}),
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function identityEvidenceElement() {
|
|
79
|
+
return {
|
|
80
|
+
object: {
|
|
81
|
+
members: [
|
|
82
|
+
field("claims", {
|
|
83
|
+
array: {
|
|
84
|
+
kind: {
|
|
85
|
+
object: {
|
|
86
|
+
members: [
|
|
87
|
+
field("kind", { enum: enumSet(CLAIM_KINDS) }),
|
|
88
|
+
field("value", "string", { check: checkNonempty }),
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
}, { minItems: 1, uniqueBy: "value" }),
|
|
94
|
+
],
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function table() {
|
|
99
|
+
return [
|
|
100
|
+
field("task_identity", "string", { check: checkIdentifier }),
|
|
101
|
+
field("idempotency_identity", "string", { check: checkIdentifier }),
|
|
102
|
+
field("parent_execution_reference", "string", {
|
|
103
|
+
required: false,
|
|
104
|
+
check: checkIdentifier,
|
|
105
|
+
rootHook: hookParent,
|
|
106
|
+
}),
|
|
107
|
+
field("initiating_subject", "string", { required: false, check: checkIdentifier }),
|
|
108
|
+
field("blueprint_digest", "custom", { check: checkTaggedDigest }),
|
|
109
|
+
field("deployment_digest", "custom", { check: checkTaggedDigest }),
|
|
110
|
+
field("input_commitment", "custom", { check: checkTaggedDigest }),
|
|
111
|
+
field("result_schema", "custom", { check: checkTaggedDigest }),
|
|
112
|
+
field("result_classification_ceiling", classificationElement()),
|
|
113
|
+
field("time_policy", {
|
|
114
|
+
object: { members: [field("elapsed_ms", "integer", { check: checkPositive })] },
|
|
115
|
+
}),
|
|
116
|
+
field("resource_policy", {
|
|
117
|
+
object: {
|
|
118
|
+
members: [
|
|
119
|
+
field("attempts", "integer", { check: checkPositive }),
|
|
120
|
+
field("concurrency", "integer", { check: checkPositive }),
|
|
121
|
+
field("tokens", "integer", { check: checkPositive }),
|
|
122
|
+
field("cost", "integer", { check: checkPositive }),
|
|
123
|
+
],
|
|
124
|
+
},
|
|
125
|
+
}),
|
|
126
|
+
field("recovery_handle", "string", { check: checkIdentifier }),
|
|
127
|
+
field("issuer", "string", { check: checkIdentifier }),
|
|
128
|
+
field("subject", "string", { check: checkIdentifier }),
|
|
129
|
+
field("audience", "string", { check: checkIdentifier }),
|
|
130
|
+
field("identity_mapping_evidence", identityEvidenceElement()),
|
|
131
|
+
field("checkpoint_request", {
|
|
132
|
+
object: {
|
|
133
|
+
members: [
|
|
134
|
+
field("kind", { enum: enumSet(CHECKPOINT_KINDS) }),
|
|
135
|
+
field("request_digest", "custom", { check: checkTaggedDigest }),
|
|
136
|
+
],
|
|
137
|
+
},
|
|
138
|
+
}, { required: false }),
|
|
139
|
+
field("checkpoint_status", { enum: enumSet(CHECKPOINT_STATUSES) }, {
|
|
140
|
+
required: false,
|
|
141
|
+
rootHook: hookCheckpoint,
|
|
142
|
+
}),
|
|
143
|
+
field("checkpoint_commitment", "custom", {
|
|
144
|
+
required: false,
|
|
145
|
+
check: checkTaggedDigest,
|
|
146
|
+
rootHook: hookCheckpoint,
|
|
147
|
+
}),
|
|
148
|
+
field("terminal_state", { enum: enumSet(TERMINAL_STATES) }, {
|
|
149
|
+
required: false,
|
|
150
|
+
rootHook: hookTerminal,
|
|
151
|
+
}),
|
|
152
|
+
field("evidence_receipt", {
|
|
153
|
+
object: {
|
|
154
|
+
members: [
|
|
155
|
+
field("result_digest", "custom", { check: checkTaggedDigest }),
|
|
156
|
+
field("checkpoint_history_commitment", "custom", { check: checkTaggedDigest }),
|
|
157
|
+
field("terminal_commitment", "custom", { check: checkTaggedDigest }),
|
|
158
|
+
field("signature", "custom", { check: checkSignatureShape }),
|
|
159
|
+
],
|
|
160
|
+
},
|
|
161
|
+
}, { required: false, rootHook: hookTerminal }),
|
|
162
|
+
field("compatibility_reference", {
|
|
163
|
+
array: {
|
|
164
|
+
kind: {
|
|
165
|
+
object: {
|
|
166
|
+
members: [
|
|
167
|
+
field("name", "string", { check: checkIdentifier }),
|
|
168
|
+
field("identity", "string", { check: checkNonempty }),
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
}, { minItems: 1, uniqueBy: "name" }),
|
|
174
|
+
field("authority_proof_references", { array: { kind: "custom", check: checkTaggedDigest } }),
|
|
175
|
+
];
|
|
176
|
+
}
|
|
177
|
+
// ---- member checks -------------------------------------------------------------------
|
|
178
|
+
function checkIdentifier(value) {
|
|
179
|
+
if (value.t !== "str")
|
|
180
|
+
return { ok: false, e: "invalid_constraint" };
|
|
181
|
+
return Buffer.byteLength(value.v, "utf8") <= IDENTIFIER_BYTES && SEGMENT.test(value.v)
|
|
182
|
+
? { ok: true }
|
|
183
|
+
: { ok: false, e: "invalid_constraint" };
|
|
184
|
+
}
|
|
185
|
+
function checkNonempty(value) {
|
|
186
|
+
if (value.t === "str" && value.v !== "")
|
|
187
|
+
return { ok: true };
|
|
188
|
+
return { ok: false, e: "invalid_constraint" };
|
|
189
|
+
}
|
|
190
|
+
function checkPositive(value) {
|
|
191
|
+
if (value.t === "int" && value.v >= 1)
|
|
192
|
+
return { ok: true };
|
|
193
|
+
return { ok: false, e: "invalid_constraint" };
|
|
194
|
+
}
|
|
195
|
+
function checkTaggedDigest(value) {
|
|
196
|
+
if (value.t !== "str")
|
|
197
|
+
return { ok: false, e: "invalid_type" };
|
|
198
|
+
return digest.fromTagged(value.v).ok ? { ok: true } : { ok: false, e: "invalid_constraint" };
|
|
199
|
+
}
|
|
200
|
+
// The receipt's JWS entry must at least parse as the §8.3 envelope; its
|
|
201
|
+
// cryptographic facts are verify_commitment's to establish.
|
|
202
|
+
function checkSignatureShape(entry) {
|
|
203
|
+
const input = signature.signingInput(entry);
|
|
204
|
+
return input.ok ? { ok: true } : { ok: false, e: "invalid_constraint" };
|
|
205
|
+
}
|
|
206
|
+
// ---- root hooks (R1-R3 + terminal-supersedes) ---------------------------------------
|
|
207
|
+
function hookParent(members) {
|
|
208
|
+
if (members.has("parent_execution_reference") && !members.has("initiating_subject")) {
|
|
209
|
+
return { ok: false, e: "invalid_constraint" };
|
|
210
|
+
}
|
|
211
|
+
return { ok: true };
|
|
212
|
+
}
|
|
213
|
+
function hookCheckpoint(members) {
|
|
214
|
+
const status = members.has("checkpoint_status");
|
|
215
|
+
const commitment = members.has("checkpoint_commitment");
|
|
216
|
+
if (status !== commitment)
|
|
217
|
+
return { ok: false, e: "invalid_constraint" };
|
|
218
|
+
if (status && members.has("terminal_state"))
|
|
219
|
+
return { ok: false, e: "invalid_constraint" };
|
|
220
|
+
return { ok: true };
|
|
221
|
+
}
|
|
222
|
+
function hookTerminal(members) {
|
|
223
|
+
const terminal = members.has("terminal_state");
|
|
224
|
+
const receipt = members.has("evidence_receipt");
|
|
225
|
+
if (terminal !== receipt)
|
|
226
|
+
return { ok: false, e: "invalid_constraint" };
|
|
227
|
+
return { ok: true };
|
|
228
|
+
}
|
|
229
|
+
// ---- decode -------------------------------------------------------------------------
|
|
230
|
+
export function decode(bytes, bounds) {
|
|
231
|
+
if (typeof bytes !== "string" && !Buffer.isBuffer(bytes)) {
|
|
232
|
+
return { ok: false, e: "invalid_type" };
|
|
233
|
+
}
|
|
234
|
+
const profile = normalizeBounds(bounds);
|
|
235
|
+
if (!profile.ok)
|
|
236
|
+
return profile;
|
|
237
|
+
const verified = canonical.verify(bytes, profile.v);
|
|
238
|
+
if (!verified.ok)
|
|
239
|
+
return verified;
|
|
240
|
+
const value = verified.v;
|
|
241
|
+
const validated = validate(table(), value);
|
|
242
|
+
if (!validated.ok)
|
|
243
|
+
return validated;
|
|
244
|
+
const scanned = scan(value);
|
|
245
|
+
if (!scanned.ok)
|
|
246
|
+
return scanned;
|
|
247
|
+
return { ok: true, v: { value } };
|
|
248
|
+
}
|
|
249
|
+
// The codec-reconstruction path: registry walk + portability pass over an
|
|
250
|
+
// already-decoded value, with the decoder's ceilings re-imposed (a hostile
|
|
251
|
+
// carrier body cannot buy unbounded work the decode lane would have refused).
|
|
252
|
+
export function fromValue(value) {
|
|
253
|
+
if (value.t !== "obj")
|
|
254
|
+
return { ok: false, e: "invalid_type" };
|
|
255
|
+
const bounded = boundedShape(value);
|
|
256
|
+
if (!bounded.ok)
|
|
257
|
+
return bounded;
|
|
258
|
+
const validated = validate(table(), value);
|
|
259
|
+
if (!validated.ok)
|
|
260
|
+
return validated;
|
|
261
|
+
const scanned = scan(value);
|
|
262
|
+
if (!scanned.ok)
|
|
263
|
+
return scanned;
|
|
264
|
+
return { ok: true, v: { value } };
|
|
265
|
+
}
|
|
266
|
+
function normalizeBounds(bounds) {
|
|
267
|
+
if (bounds === undefined)
|
|
268
|
+
return { ok: true, v: maxBounds() };
|
|
269
|
+
return coerceBounds(bounds);
|
|
270
|
+
}
|
|
271
|
+
function boundedShape(value) {
|
|
272
|
+
return shapeWalk(value, { members: 0, items: 0, nodes: 0, string: 0, depth: 0 });
|
|
273
|
+
}
|
|
274
|
+
function shapeWalk(value, acc) {
|
|
275
|
+
const entry = ceiling(acc);
|
|
276
|
+
if (!entry.ok)
|
|
277
|
+
return entry;
|
|
278
|
+
switch (value.t) {
|
|
279
|
+
case "obj": {
|
|
280
|
+
const bumped = bump(acc, { members: value.v.length, nodes: 1, depth: 1 });
|
|
281
|
+
return walkShapeMembers(value.v, bumped);
|
|
282
|
+
}
|
|
283
|
+
case "arr": {
|
|
284
|
+
const bumped = bump(acc, { items: value.v.length, nodes: 1, depth: 1 });
|
|
285
|
+
return walkShapeItems(value.v, bumped);
|
|
286
|
+
}
|
|
287
|
+
case "str": {
|
|
288
|
+
const bumped = bump(acc, { nodes: 1, string: Buffer.byteLength(value.v, "utf8"), depth: 1 });
|
|
289
|
+
return ceiling(bumped);
|
|
290
|
+
}
|
|
291
|
+
default:
|
|
292
|
+
return ceiling(bump(acc, { nodes: 1 }));
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
function walkShapeMembers(members, acc) {
|
|
296
|
+
for (const [, value] of members) {
|
|
297
|
+
const result = shapeWalk(value, acc);
|
|
298
|
+
if (!result.ok)
|
|
299
|
+
return result;
|
|
300
|
+
}
|
|
301
|
+
return { ok: true };
|
|
302
|
+
}
|
|
303
|
+
function walkShapeItems(items, acc) {
|
|
304
|
+
for (const item of items) {
|
|
305
|
+
const result = shapeWalk(item, acc);
|
|
306
|
+
if (!result.ok)
|
|
307
|
+
return result;
|
|
308
|
+
}
|
|
309
|
+
return { ok: true };
|
|
310
|
+
}
|
|
311
|
+
function bump(acc, adds) {
|
|
312
|
+
const out = { ...acc };
|
|
313
|
+
for (const [key, add] of Object.entries(adds)) {
|
|
314
|
+
out[key] += add;
|
|
315
|
+
}
|
|
316
|
+
return out;
|
|
317
|
+
}
|
|
318
|
+
function ceiling(acc) {
|
|
319
|
+
const checks = [
|
|
320
|
+
["members", CEILINGS.members, "members"],
|
|
321
|
+
["items", CEILINGS.items, "items"],
|
|
322
|
+
["nodes", CEILINGS.nodes, "nodes"],
|
|
323
|
+
["string", CEILINGS.string, "string"],
|
|
324
|
+
["depth", CEILINGS.depth, "depth"],
|
|
325
|
+
];
|
|
326
|
+
for (const [key, max, name] of checks) {
|
|
327
|
+
if (acc[key] > max)
|
|
328
|
+
return { ok: false, e: `ceiling:${name}` };
|
|
329
|
+
}
|
|
330
|
+
return { ok: true };
|
|
331
|
+
}
|
|
332
|
+
// ---- canonical bytes ------------------------------------------------------------------
|
|
333
|
+
export function canonicalBytes(envelope) {
|
|
334
|
+
return canonical.encode(envelope.value);
|
|
335
|
+
}
|
|
336
|
+
// ---- the federation portability pass (mode map + R4) ----------------------------------
|
|
337
|
+
const IDENTIFIER_SCANNED = [
|
|
338
|
+
"task_identity",
|
|
339
|
+
"idempotency_identity",
|
|
340
|
+
"parent_execution_reference",
|
|
341
|
+
"initiating_subject",
|
|
342
|
+
"recovery_handle",
|
|
343
|
+
"issuer",
|
|
344
|
+
"subject",
|
|
345
|
+
"audience",
|
|
346
|
+
];
|
|
347
|
+
function scan(value) {
|
|
348
|
+
if (value.t !== "obj")
|
|
349
|
+
return { ok: true };
|
|
350
|
+
for (const [name, member] of value.v) {
|
|
351
|
+
if (name === "identity_mapping_evidence" && member.t === "obj") {
|
|
352
|
+
const claims = keyfind(member.v, "claims");
|
|
353
|
+
if (claims !== null && claims.t === "arr") {
|
|
354
|
+
for (const [index, claim] of claims.v.entries()) {
|
|
355
|
+
const result = claimScan(claim, index);
|
|
356
|
+
if (!result.ok)
|
|
357
|
+
return result;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
if (name === "compatibility_reference" && member.t === "arr") {
|
|
363
|
+
for (const entry of member.v) {
|
|
364
|
+
if (entry.t !== "obj")
|
|
365
|
+
continue;
|
|
366
|
+
const identity = keyfind(entry.v, "identity");
|
|
367
|
+
if (identity === null)
|
|
368
|
+
continue;
|
|
369
|
+
const result = portability.scanValue(identity);
|
|
370
|
+
if (!result.ok)
|
|
371
|
+
return result;
|
|
372
|
+
}
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
if (name === "evidence_receipt" && member.t === "obj") {
|
|
376
|
+
const entry = keyfind(member.v, "signature");
|
|
377
|
+
if (entry !== null && entry.t === "obj") {
|
|
378
|
+
for (const [position, value] of keyIdPositions(entry.v)) {
|
|
379
|
+
const result = portability.scanValue(value);
|
|
380
|
+
if (!result.ok)
|
|
381
|
+
return { ok: false, e: result.e };
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
if (IDENTIFIER_SCANNED.includes(name)) {
|
|
387
|
+
const result = portability.scanIdentifier(member);
|
|
388
|
+
if (!result.ok)
|
|
389
|
+
return result;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return { ok: true };
|
|
394
|
+
}
|
|
395
|
+
// R4: correlation grants nothing — an authority-shaped claim is nonportable
|
|
396
|
+
// content, denied with the claim's schema path.
|
|
397
|
+
function claimScan(claim, index) {
|
|
398
|
+
if (claim.t !== "obj")
|
|
399
|
+
return { ok: true };
|
|
400
|
+
const kind = keyfind(claim.v, "kind");
|
|
401
|
+
if (kind !== null && kind.t === "str" && kind.v === "authority") {
|
|
402
|
+
return { ok: false, e: "nonportable_content" };
|
|
403
|
+
}
|
|
404
|
+
const value = keyfind(claim.v, "value");
|
|
405
|
+
if (value === null)
|
|
406
|
+
return { ok: true };
|
|
407
|
+
const result = portability.scanValue(value);
|
|
408
|
+
return result.ok ? { ok: true } : { ok: false, e: result.e };
|
|
409
|
+
}
|
|
410
|
+
// The receipt's signature entry carries key ids in two positions (protected
|
|
411
|
+
// kid and signed key_id — equal by the JWS shape check), both scanned STRICT.
|
|
412
|
+
function keyIdPositions(entry) {
|
|
413
|
+
const header = keyfind(entry, "protected");
|
|
414
|
+
const attrs = keyfind(entry, "signed_attributes");
|
|
415
|
+
if (header === null || header.t !== "obj" || attrs === null || attrs.t !== "obj")
|
|
416
|
+
return [];
|
|
417
|
+
const kid = keyfind(header.v, "kid");
|
|
418
|
+
const keyId = keyfind(attrs.v, "key_id");
|
|
419
|
+
if (kid === null || keyId === null)
|
|
420
|
+
return [];
|
|
421
|
+
return [
|
|
422
|
+
["kid", kid],
|
|
423
|
+
["key_id", keyId],
|
|
424
|
+
];
|
|
425
|
+
}
|
|
426
|
+
// ---- state codecs: lossy-aware by construction -----------------------------------------
|
|
427
|
+
export function fromA2aState(spelling) {
|
|
428
|
+
if (typeof spelling !== "string")
|
|
429
|
+
return { ok: false, e: "invalid_type" };
|
|
430
|
+
if (spelling in A2A_TO_LOGICAL)
|
|
431
|
+
return { ok: true, v: A2A_TO_LOGICAL[spelling] };
|
|
432
|
+
if (spelling === "TASK_STATE_UNSPECIFIED") {
|
|
433
|
+
return { ok: false, e: "federation_state_unmappable" };
|
|
434
|
+
}
|
|
435
|
+
return { ok: false, e: "invalid_constraint" };
|
|
436
|
+
}
|
|
437
|
+
export function toA2aState(spelling) {
|
|
438
|
+
// The runner's to_logical_state gate: only the eight wire spellings of the
|
|
439
|
+
// logical states reach the codec; anything else denies invalid_type.
|
|
440
|
+
if (!WIRE_LOGICAL_STATES.includes(spelling))
|
|
441
|
+
return { ok: false, e: "invalid_type" };
|
|
442
|
+
return { ok: true, v: LOGICAL_TO_A2A[spelling] };
|
|
443
|
+
}
|
|
444
|
+
export function fromMcpState(spelling) {
|
|
445
|
+
if (typeof spelling !== "string")
|
|
446
|
+
return { ok: false, e: "invalid_type" };
|
|
447
|
+
if (spelling in MCP_TO_LOGICAL)
|
|
448
|
+
return { ok: true, v: MCP_TO_LOGICAL[spelling] };
|
|
449
|
+
return { ok: false, e: "invalid_constraint" };
|
|
450
|
+
}
|
|
451
|
+
export function toMcpState(spelling) {
|
|
452
|
+
if (!WIRE_LOGICAL_STATES.includes(spelling))
|
|
453
|
+
return { ok: false, e: "invalid_type" };
|
|
454
|
+
const mapped = LOGICAL_TO_MCP[spelling];
|
|
455
|
+
if (mapped === undefined)
|
|
456
|
+
return { ok: false, e: "federation_state_unmappable" };
|
|
457
|
+
return { ok: true, v: mapped };
|
|
458
|
+
}
|
|
459
|
+
// ---- carriers: the placement laws, per transport ----------------------------------------
|
|
460
|
+
// The A2A-side carrier key: the registry entry's a2a_uri.
|
|
461
|
+
export function a2aCarrierKey() {
|
|
462
|
+
return compiledEntry("com.example/federation").a2a_uri;
|
|
463
|
+
}
|
|
464
|
+
// The MCP-side carrier key: the registry namespace.
|
|
465
|
+
export function mcpCarrierKey() {
|
|
466
|
+
return "com.example/federation";
|
|
467
|
+
}
|
|
468
|
+
export function fromA2aCarrier(carrier) {
|
|
469
|
+
if (carrier.t !== "obj")
|
|
470
|
+
return { ok: false, e: "invalid_type" };
|
|
471
|
+
const id = carrierString(carrier.v, "id");
|
|
472
|
+
if (!id.ok)
|
|
473
|
+
return id;
|
|
474
|
+
const stateRaw = carrierState(carrier.v);
|
|
475
|
+
if (!stateRaw.ok)
|
|
476
|
+
return stateRaw;
|
|
477
|
+
const logical = fromA2aState(stateRaw.v);
|
|
478
|
+
if (!logical.ok)
|
|
479
|
+
return logical;
|
|
480
|
+
const body = carrierBody(carrier.v, "metadata", a2aCarrierKey());
|
|
481
|
+
if (!body.ok)
|
|
482
|
+
return body;
|
|
483
|
+
const placed = noDoublePlacement(body.v);
|
|
484
|
+
if (!placed.ok)
|
|
485
|
+
return placed;
|
|
486
|
+
const rebuilt = rebuild(id.v, logical.v, body.v);
|
|
487
|
+
return rebuilt.ok ? { ok: true, v: { decoded: true } } : rebuilt;
|
|
488
|
+
}
|
|
489
|
+
export function fromMcpCarrier(carrier) {
|
|
490
|
+
if (carrier.t !== "obj")
|
|
491
|
+
return { ok: false, e: "invalid_type" };
|
|
492
|
+
const id = carrierString(carrier.v, "taskId");
|
|
493
|
+
if (!id.ok)
|
|
494
|
+
return id;
|
|
495
|
+
const stateRaw = carrierString(carrier.v, "status");
|
|
496
|
+
if (!stateRaw.ok)
|
|
497
|
+
return stateRaw;
|
|
498
|
+
const logical = fromMcpState(stateRaw.v);
|
|
499
|
+
if (!logical.ok)
|
|
500
|
+
return logical;
|
|
501
|
+
const body = carrierBody(carrier.v, "_meta", mcpCarrierKey());
|
|
502
|
+
if (!body.ok)
|
|
503
|
+
return body;
|
|
504
|
+
const placed = noDoublePlacement(body.v);
|
|
505
|
+
if (!placed.ok)
|
|
506
|
+
return placed;
|
|
507
|
+
const rebuilt = rebuild(id.v, logical.v, body.v);
|
|
508
|
+
return rebuilt.ok ? { ok: true, v: { decoded: true } } : rebuilt;
|
|
509
|
+
}
|
|
510
|
+
function carrierString(members, name) {
|
|
511
|
+
const found = keyfind(members, name);
|
|
512
|
+
if (found === null)
|
|
513
|
+
return { ok: false, e: "missing_required_field" };
|
|
514
|
+
if (found.t !== "str")
|
|
515
|
+
return { ok: false, e: "invalid_type" };
|
|
516
|
+
return { ok: true, v: found.v };
|
|
517
|
+
}
|
|
518
|
+
function carrierState(members) {
|
|
519
|
+
const found = keyfind(members, "status");
|
|
520
|
+
if (found === null)
|
|
521
|
+
return { ok: false, e: "missing_required_field" };
|
|
522
|
+
if (found.t !== "obj")
|
|
523
|
+
return { ok: false, e: "invalid_type" };
|
|
524
|
+
return carrierString(found.v, "state");
|
|
525
|
+
}
|
|
526
|
+
function carrierBody(members, envelopeMember, key) {
|
|
527
|
+
const found = keyfind(members, envelopeMember);
|
|
528
|
+
if (found === null)
|
|
529
|
+
return { ok: false, e: "missing_required_field" };
|
|
530
|
+
if (found.t !== "obj")
|
|
531
|
+
return { ok: false, e: "invalid_type" };
|
|
532
|
+
const inner = keyfind(found.v, key);
|
|
533
|
+
if (inner === null)
|
|
534
|
+
return { ok: false, e: "missing_required_field" };
|
|
535
|
+
if (inner.t !== "obj")
|
|
536
|
+
return { ok: false, e: "invalid_type" };
|
|
537
|
+
return { ok: true, v: inner.v };
|
|
538
|
+
}
|
|
539
|
+
// A native member appearing in the body is a double placement.
|
|
540
|
+
function noDoublePlacement(body) {
|
|
541
|
+
for (const [name] of body) {
|
|
542
|
+
if (NATIVE_MEMBERS.includes(name))
|
|
543
|
+
return { ok: false, e: "federation_mapping_conflict" };
|
|
544
|
+
}
|
|
545
|
+
return { ok: true };
|
|
546
|
+
}
|
|
547
|
+
function rebuild(id, logical, body) {
|
|
548
|
+
const stateMember = TERMINAL_ATOMS.includes(logical) ? "terminal_state" : "checkpoint_status";
|
|
549
|
+
const members = [
|
|
550
|
+
["task_identity", { t: "str", v: id }],
|
|
551
|
+
["recovery_handle", { t: "str", v: id }],
|
|
552
|
+
[stateMember, { t: "str", v: logical }],
|
|
553
|
+
...body,
|
|
554
|
+
];
|
|
555
|
+
return fromValue({ t: "obj", v: members });
|
|
556
|
+
}
|
|
557
|
+
// ---- terminal commitment + verify_commitment ---------------------------------------------
|
|
558
|
+
// The Terminal Commitment: the domain-separated digest over exactly the
|
|
559
|
+
// seven named components.
|
|
560
|
+
export function terminalCommitment(envelope) {
|
|
561
|
+
if (envelope.value.t !== "obj")
|
|
562
|
+
return { ok: false, e: "invalid_type" };
|
|
563
|
+
const members = new Map(envelope.value.v);
|
|
564
|
+
const taskOk = stringMember(members, "task_identity");
|
|
565
|
+
if (!taskOk.ok)
|
|
566
|
+
return taskOk;
|
|
567
|
+
const terminalOk = stringMember(members, "terminal_state");
|
|
568
|
+
if (!terminalOk.ok)
|
|
569
|
+
return terminalOk;
|
|
570
|
+
const receipt = receiptOf(members);
|
|
571
|
+
if (!receipt.ok)
|
|
572
|
+
return receipt;
|
|
573
|
+
const resultDigest = receiptFetch(receipt.v, "result_digest");
|
|
574
|
+
if (!resultDigest.ok)
|
|
575
|
+
return resultDigest;
|
|
576
|
+
const history = receiptFetch(receipt.v, "checkpoint_history_commitment");
|
|
577
|
+
if (!history.ok)
|
|
578
|
+
return history;
|
|
579
|
+
const ordered = [
|
|
580
|
+
["task_identity", members.get("task_identity")],
|
|
581
|
+
["terminal_state", members.get("terminal_state")],
|
|
582
|
+
["result_digest", resultDigest.v],
|
|
583
|
+
["result_classification_ceiling", members.get("result_classification_ceiling")],
|
|
584
|
+
["compatibility_reference", members.get("compatibility_reference")],
|
|
585
|
+
["authority_proof_references", members.get("authority_proof_references")],
|
|
586
|
+
["checkpoint_history_commitment", history.v],
|
|
587
|
+
];
|
|
588
|
+
const jcs = canonical.encode({ t: "obj", v: ordered });
|
|
589
|
+
if (!jcs.ok)
|
|
590
|
+
return jcs;
|
|
591
|
+
return { ok: true, v: digest.hash("federation_envelope", Buffer.from(jcs.v, "utf8")) };
|
|
592
|
+
}
|
|
593
|
+
export function verifyCommitment(envelope, context) {
|
|
594
|
+
if (envelope.value.t !== "obj")
|
|
595
|
+
return { ok: false, e: "invalid_type" };
|
|
596
|
+
const members = new Map(envelope.value.v);
|
|
597
|
+
const taskOk = stringMember(members, "task_identity");
|
|
598
|
+
if (!taskOk.ok)
|
|
599
|
+
return taskOk;
|
|
600
|
+
const terminalOk = stringMember(members, "terminal_state");
|
|
601
|
+
if (!terminalOk.ok)
|
|
602
|
+
return terminalOk;
|
|
603
|
+
const receipt = receiptOf(members);
|
|
604
|
+
if (!receipt.ok)
|
|
605
|
+
return receipt;
|
|
606
|
+
const signatureEntry = receiptFetch(receipt.v, "signature");
|
|
607
|
+
if (!signatureEntry.ok)
|
|
608
|
+
return signatureEntry;
|
|
609
|
+
const pool = resolvePool(members, context);
|
|
610
|
+
if (!pool.ok)
|
|
611
|
+
return pool;
|
|
612
|
+
const sigOk = signatureCheck(signatureEntry.v, pool.v.pool, pool.v.attributed);
|
|
613
|
+
if (!sigOk.ok)
|
|
614
|
+
return sigOk;
|
|
615
|
+
const binding = bindingCheck(envelope.value, signatureEntry.v);
|
|
616
|
+
if (!binding.ok)
|
|
617
|
+
return binding;
|
|
618
|
+
const commitment = terminalCommitment(envelope);
|
|
619
|
+
if (!commitment.ok)
|
|
620
|
+
return commitment;
|
|
621
|
+
const commitmentOk = commitmentCheck(receipt.v, commitment.v);
|
|
622
|
+
if (!commitmentOk.ok)
|
|
623
|
+
return commitmentOk;
|
|
624
|
+
const contextOk = contextCompare(members, context);
|
|
625
|
+
if (!contextOk.ok)
|
|
626
|
+
return contextOk;
|
|
627
|
+
const conflictOk = conflictCheck(members, commitment.v, context);
|
|
628
|
+
if (!conflictOk.ok)
|
|
629
|
+
return conflictOk;
|
|
630
|
+
const fresh = freshnessCheck(signatureEntry.v, context);
|
|
631
|
+
if (!fresh.ok)
|
|
632
|
+
return fresh;
|
|
633
|
+
const taskIdentity = members.get("task_identity");
|
|
634
|
+
return { ok: true, v: { taskIdentity: taskIdentity.v } };
|
|
635
|
+
}
|
|
636
|
+
function stringMember(members, name) {
|
|
637
|
+
const found = members.get(name);
|
|
638
|
+
if (found === undefined)
|
|
639
|
+
return { ok: false, e: "missing_required_field" };
|
|
640
|
+
if (found.t !== "str")
|
|
641
|
+
return { ok: false, e: "invalid_type" };
|
|
642
|
+
return { ok: true };
|
|
643
|
+
}
|
|
644
|
+
function receiptOf(members) {
|
|
645
|
+
const found = members.get("evidence_receipt");
|
|
646
|
+
if (found === undefined)
|
|
647
|
+
return { ok: false, e: "missing_required_field" };
|
|
648
|
+
if (found.t !== "obj")
|
|
649
|
+
return { ok: false, e: "invalid_type" };
|
|
650
|
+
return { ok: true, v: found.v };
|
|
651
|
+
}
|
|
652
|
+
function receiptFetch(receipt, name) {
|
|
653
|
+
const found = keyfind(receipt, name);
|
|
654
|
+
if (found === null)
|
|
655
|
+
return { ok: false, e: "missing_required_field" };
|
|
656
|
+
return { ok: true, v: found };
|
|
657
|
+
}
|
|
658
|
+
function signatureCheck(entry, pool, attributed) {
|
|
659
|
+
const verified = signature.verify(entry, pool);
|
|
660
|
+
if (verified.ok)
|
|
661
|
+
return { ok: true };
|
|
662
|
+
// The key-miss class under attribution names the issuer; envelope and
|
|
663
|
+
// algorithm failures keep the signature subject.
|
|
664
|
+
return { ok: false, e: verified.e };
|
|
665
|
+
}
|
|
666
|
+
// Pool resolution: the flat pool is used only when issuer_key_sets is
|
|
667
|
+
// nil; the claimed issuer member selects the pool from validated sets, a
|
|
668
|
+
// forged absent/non-string issuer selecting the literal empty pool.
|
|
669
|
+
function resolvePool(members, context) {
|
|
670
|
+
if (context.issuerKeySets === null || context.issuerKeySets === undefined) {
|
|
671
|
+
if (!Array.isArray(context.keys))
|
|
672
|
+
return { ok: false, e: "invalid_type" };
|
|
673
|
+
return { ok: true, v: { pool: context.keys, attributed: false } };
|
|
674
|
+
}
|
|
675
|
+
const sets = context.issuerKeySets;
|
|
676
|
+
for (const [issuer, pool] of Object.entries(sets)) {
|
|
677
|
+
if (typeof issuer !== "string" || !Array.isArray(pool)) {
|
|
678
|
+
return { ok: false, e: "invalid_type" };
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
const claimed = members.get("issuer");
|
|
682
|
+
if (claimed !== undefined && claimed.t === "str") {
|
|
683
|
+
// Own-property lookup only: a prototype-chain key ("constructor") can
|
|
684
|
+
// never surface a non-pool value from a caller-supplied plain object.
|
|
685
|
+
const pool = Object.prototype.hasOwnProperty.call(sets, claimed.v)
|
|
686
|
+
? sets[claimed.v]
|
|
687
|
+
: [];
|
|
688
|
+
return { ok: true, v: { pool, attributed: true } };
|
|
689
|
+
}
|
|
690
|
+
return { ok: true, v: { pool: [], attributed: true } };
|
|
691
|
+
}
|
|
692
|
+
// The F1 binding: the JWS is verified over its own input only; THIS step
|
|
693
|
+
// pins it to the envelope. The purpose member prevents cross-surface
|
|
694
|
+
// signature lifting; the covered bytes are the envelope minus the signature
|
|
695
|
+
// entry itself.
|
|
696
|
+
function bindingCheck(value, signatureEntry) {
|
|
697
|
+
const attrs = blueprint.signatureAttributes(signatureEntry);
|
|
698
|
+
if (!attrs.ok)
|
|
699
|
+
return attrs;
|
|
700
|
+
if (attrs.v.purpose !== "federation-envelope") {
|
|
701
|
+
return { ok: false, e: "invalid_constraint" };
|
|
702
|
+
}
|
|
703
|
+
const jcs = canonical.encode(coveredBody(value));
|
|
704
|
+
if (!jcs.ok)
|
|
705
|
+
return jcs;
|
|
706
|
+
const expected = digest.hash("federation_envelope", Buffer.from(jcs.v, "utf8"));
|
|
707
|
+
return digest.equal(attrs.v.contentDigest, expected)
|
|
708
|
+
? { ok: true }
|
|
709
|
+
: { ok: false, e: "digest_mismatch" };
|
|
710
|
+
}
|
|
711
|
+
function coveredBody(value) {
|
|
712
|
+
if (value.t !== "obj")
|
|
713
|
+
return value;
|
|
714
|
+
return {
|
|
715
|
+
t: "obj",
|
|
716
|
+
v: value.v.map(([name, member]) => {
|
|
717
|
+
if (name === "evidence_receipt" && member.t === "obj") {
|
|
718
|
+
return [name, { t: "obj", v: member.v.filter(([n]) => n !== "signature") }];
|
|
719
|
+
}
|
|
720
|
+
return [name, member];
|
|
721
|
+
}),
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
function commitmentCheck(receipt, recomputed) {
|
|
725
|
+
const taggedEntry = keyfind(receipt, "terminal_commitment");
|
|
726
|
+
if (taggedEntry === null)
|
|
727
|
+
return { ok: false, e: "missing_required_field" };
|
|
728
|
+
if (taggedEntry.t !== "str")
|
|
729
|
+
return { ok: false, e: "invalid_type" };
|
|
730
|
+
const declared = digest.fromTagged(taggedEntry.v);
|
|
731
|
+
if (!declared.ok)
|
|
732
|
+
return declared;
|
|
733
|
+
return digest.equal(declared.v, recomputed)
|
|
734
|
+
? { ok: true }
|
|
735
|
+
: { ok: false, e: "digest_mismatch" };
|
|
736
|
+
}
|
|
737
|
+
// One code for the issuer/subject/audience triple; a nil pin skips that
|
|
738
|
+
// member entirely.
|
|
739
|
+
function contextCompare(members, context) {
|
|
740
|
+
const pins = [
|
|
741
|
+
["issuer", context.issuer],
|
|
742
|
+
["subject", context.subject],
|
|
743
|
+
["audience", context.audience],
|
|
744
|
+
];
|
|
745
|
+
for (const [member, pin] of pins) {
|
|
746
|
+
if (pin === undefined || pin === null)
|
|
747
|
+
continue;
|
|
748
|
+
// The twin's exact match: members[m] must equal {:string, pin}. A pin
|
|
749
|
+
// that is present but not a string can NEVER equal a string member, so
|
|
750
|
+
// it denies — the receiver's constraint is enforced, never dropped.
|
|
751
|
+
const found = members.get(member);
|
|
752
|
+
const pinValue = pin;
|
|
753
|
+
const matches = pinValue !== null &&
|
|
754
|
+
typeof pinValue === "object" &&
|
|
755
|
+
pinValue.t === "str" &&
|
|
756
|
+
found !== undefined &&
|
|
757
|
+
found.t === "str" &&
|
|
758
|
+
found.v === pinValue.v;
|
|
759
|
+
if (!matches)
|
|
760
|
+
return { ok: false, e: "audience_mismatch" };
|
|
761
|
+
}
|
|
762
|
+
return { ok: true };
|
|
763
|
+
}
|
|
764
|
+
function conflictCheck(members, commitment, context) {
|
|
765
|
+
const taskIdentity = members.get("task_identity");
|
|
766
|
+
const terminalState = members.get("terminal_state");
|
|
767
|
+
if (taskIdentity === undefined || taskIdentity.t !== "str")
|
|
768
|
+
return { ok: false, e: "invalid_type" };
|
|
769
|
+
if (terminalState === undefined || terminalState.t !== "str")
|
|
770
|
+
return { ok: false, e: "invalid_type" };
|
|
771
|
+
for (const prior of context.priorReceipts) {
|
|
772
|
+
const divergent = prior.taskIdentity === taskIdentity.v &&
|
|
773
|
+
(prior.terminalState !== terminalState.v ||
|
|
774
|
+
!digest.equal(prior.terminalCommitment, commitment));
|
|
775
|
+
if (divergent)
|
|
776
|
+
return { ok: false, e: "federation_terminal_conflict" };
|
|
777
|
+
}
|
|
778
|
+
return { ok: true };
|
|
779
|
+
}
|
|
780
|
+
// Freshness: both bounds receiver-supplied Z-form, INCLUSIVE, over the
|
|
781
|
+
// SIGNED created_at; malformed pins deny typed. Runs after conflict
|
|
782
|
+
// detection so a stale AND equivocating receipt still reports the conflict.
|
|
783
|
+
function freshnessCheck(signatureEntry, context) {
|
|
784
|
+
const after = pin(context.createdAfter);
|
|
785
|
+
if (!after.ok)
|
|
786
|
+
return after;
|
|
787
|
+
const before = pin(context.createdBefore);
|
|
788
|
+
if (!before.ok)
|
|
789
|
+
return before;
|
|
790
|
+
const attrs = blueprint.signatureAttributes(signatureEntry);
|
|
791
|
+
if (!attrs.ok)
|
|
792
|
+
return attrs;
|
|
793
|
+
const stamped = parseTimestampMs(attrs.v.createdAt);
|
|
794
|
+
if (stamped === null)
|
|
795
|
+
return { ok: false, e: "invalid_type" };
|
|
796
|
+
const inWindow = (after.v === null || stamped >= after.v) && (before.v === null || stamped <= before.v);
|
|
797
|
+
return inWindow ? { ok: true } : { ok: false, e: "invalid_constraint" };
|
|
798
|
+
}
|
|
799
|
+
// The SAME double gate as the signed member: the Z-form regex rejects
|
|
800
|
+
// offsets and fractions that a lenient parser alone accepts.
|
|
801
|
+
function pin(value) {
|
|
802
|
+
if (value === undefined || value === null)
|
|
803
|
+
return { ok: true, v: null };
|
|
804
|
+
if (typeof value !== "string" || !Z_FORM.test(value) || parseTimestampMs(value) === null) {
|
|
805
|
+
return { ok: false, e: "invalid_type" };
|
|
806
|
+
}
|
|
807
|
+
return { ok: true, v: parseTimestampMs(value) };
|
|
808
|
+
}
|
|
809
|
+
function parseTimestampMs(stamped) {
|
|
810
|
+
const matched = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/.exec(stamped);
|
|
811
|
+
if (matched === null)
|
|
812
|
+
return null;
|
|
813
|
+
const [, y, mo, d, h, mi, s] = matched;
|
|
814
|
+
if (!blueprint.validZFormDate(stamped))
|
|
815
|
+
return null;
|
|
816
|
+
return Date.UTC(Number(y), Number(mo) - 1, Number(d), Number(h), Number(mi), Number(s));
|
|
817
|
+
}
|