@mikeargento/bitgraph-verify 1.4.0 → 1.5.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/src/fuse.ts CHANGED
@@ -325,7 +325,7 @@ function parseTar(bytes: Uint8Array): TarEntry[] | null {
325
325
  // Placement registry
326
326
  // ---------------------------------------------------------------------------
327
327
 
328
- export type PlacementId = "trailer/1" | "container/1" | "produced/1";
328
+ export type PlacementId = "trailer/1" | "container/1" | "produced/1" | "set/1";
329
329
 
330
330
  export interface Located {
331
331
  /** The commitment found in the fused bytes. */
@@ -409,17 +409,210 @@ const produced1: Placement = {
409
409
  /** Registered placements in the fixed order a verifier tries them when none is declared. */
410
410
  export const PLACEMENTS: readonly Placement[] = Object.freeze([trailer1, container1, produced1]);
411
411
 
412
+ // ---------------------------------------------------------------------------
413
+ // Set manifest (placement set/1): N files fused under ONE slot
414
+ // ---------------------------------------------------------------------------
415
+ //
416
+ // A set is N files fused under one slot. The commitment c is computed once
417
+ // from the one slot record; every member's fused bytes carry c via that
418
+ // member's own placement (trailer/1 or container/1, chosen per file as
419
+ // today); and the COMMITTED ARTIFACT is a canonical manifest listing the
420
+ // members' fused digests, origin digests and placement ids, plus c itself.
421
+ // The manifest is a Form C artifact under the placement id "set/1".
422
+ //
423
+ // Its canonical encoding is load-bearing: one committed hash must stand for
424
+ // exactly one member list. buildSetManifest is the single source of the byte
425
+ // layout (rows strictly ascending by artifact digest, lowercase hex, sorted
426
+ // keys, no whitespace) and parseSetManifest accepts nothing that is not byte
427
+ // for byte equal to its own rebuild. Anything outside that domain is refused,
428
+ // never normalized.
429
+
430
+ export const SET_PLACEMENT_ID = "set/1" as const;
431
+
432
+ /**
433
+ * The proof.metadata key under which a set proof carries its manifest as a
434
+ * parsed plain object: the profile id itself, namespaced so it cannot collide
435
+ * with a site's own metadata keys. metadata is UNSIGNED and advisory. The
436
+ * manifest is protected only because its canonical bytes must hash to the
437
+ * signed artifact digest, which verifyFuseMember checks before reading a row.
438
+ */
439
+ export const SET_METADATA_KEY = FUSE_PROFILE;
440
+
441
+ /** A placement id is a lowercase name, a slash, and a positive version: "trailer/1". */
442
+ const PLACEMENT_ID_PATTERN = /^[a-z0-9][a-z0-9-]*\/[1-9][0-9]*$/;
443
+
444
+ /** One member of a set, bytes view: the build input and the parse output. */
445
+ export interface SetMember {
446
+ /** SHA-256 of the member's FUSED bytes. */
447
+ artifact: Uint8Array;
448
+ /** SHA-256 of the member's ORIGINAL bytes; selects the rebuild path and feeds lookup by original. */
449
+ origin: Uint8Array;
450
+ /** The placement that carries the commitment inside this member's fused bytes. */
451
+ placement: string;
452
+ }
453
+
454
+ /** The manifest as JSON: the type of the value under proof.metadata[SET_METADATA_KEY]. */
455
+ export interface SetManifest {
456
+ members: Array<{
457
+ artifact: { algorithm: "sha256"; digest: string };
458
+ origin: { algorithm: "sha256"; digest: string };
459
+ placement: string;
460
+ }>;
461
+ placement: typeof SET_PLACEMENT_ID;
462
+ slotCommitment: { algorithm: "sha256"; digest: string };
463
+ type: typeof FUSE_PROFILE;
464
+ }
465
+
466
+ /**
467
+ * Build the canonical set manifest bytes. Rows are sorted strictly ascending
468
+ * by artifact digest (byte order, which is the lexicographic order of the
469
+ * lowercase hex), so the same members in any input order give the same bytes.
470
+ * Throws on a commitment or digest that is not 32 bytes, an empty list, a
471
+ * malformed placement id, a member placement of "set/1" (no nesting in v1),
472
+ * or a duplicate artifact digest. Duplicate ORIGIN digests are permitted: one
473
+ * original fused two ways is two members with two artifact digests.
474
+ */
475
+ export function buildSetManifest(commitment: Uint8Array, members: readonly SetMember[]): Uint8Array {
476
+ if (commitment.length !== 32) throw new TypeError("commitment must be 32 bytes");
477
+ if (members.length === 0) throw new TypeError("a set lists at least one member");
478
+ const rows: SetManifest["members"] = [];
479
+ const seen = new Set<string>();
480
+ for (const m of members) {
481
+ if (m.artifact.length !== 32) throw new TypeError("member artifact digest must be 32 bytes");
482
+ if (m.origin.length !== 32) throw new TypeError("member origin digest must be 32 bytes");
483
+ if (!PLACEMENT_ID_PATTERN.test(m.placement)) throw new TypeError(`member placement "${m.placement}" is not a placement id`);
484
+ if (m.placement === SET_PLACEMENT_ID) throw new TypeError("a set cannot list a set as a member");
485
+ const artifact = bytesToHex(m.artifact);
486
+ if (seen.has(artifact)) throw new TypeError(`duplicate member artifact digest ${artifact}`);
487
+ seen.add(artifact);
488
+ rows.push({
489
+ artifact: { algorithm: "sha256", digest: artifact },
490
+ origin: { algorithm: "sha256", digest: bytesToHex(m.origin) },
491
+ placement: m.placement,
492
+ });
493
+ }
494
+ rows.sort((a, b) => (a.artifact.digest < b.artifact.digest ? -1 : 1));
495
+ const manifest: SetManifest = {
496
+ members: rows,
497
+ placement: SET_PLACEMENT_ID,
498
+ slotCommitment: { algorithm: "sha256", digest: bytesToHex(commitment) },
499
+ type: FUSE_PROFILE,
500
+ };
501
+ return canonicalize(manifest);
502
+ }
503
+
504
+ /**
505
+ * Strict parse of set manifest bytes, in the style of parseFusePayload. The
506
+ * bytes must be valid UTF-8 JSON, a plain object with exactly the keys
507
+ * {members, placement, slotCommitment, type}, the profile type, placement
508
+ * "set/1", a lowercase-hex 32-byte commitment, at least one row, every row
509
+ * exactly {artifact, origin, placement} with 32-byte digests and a
510
+ * well-formed placement id other than "set/1", and finally must equal
511
+ * buildSetManifest over what was read byte for byte. That one comparison
512
+ * rejects whitespace, key reordering, duplicate JSON keys (a duplicate cannot
513
+ * survive a round trip), unsorted or duplicated rows, non-canonical escapes
514
+ * and trailing bytes. Registration of a row's placement is NOT checked here:
515
+ * a v2 placement must not poison v1 readers, and it surfaces per row as
516
+ * UNDETERMINED_PLACEMENT at verify time. Returns null on any deviation.
517
+ */
518
+ export function parseSetManifest(bytes: Uint8Array): { commitment: Uint8Array; members: SetMember[] } | null {
519
+ let text: string;
520
+ try {
521
+ // ignoreBOM keeps a leading BOM in the text, where JSON.parse refuses it,
522
+ // rather than stripping it as though it were whitespace.
523
+ text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
524
+ } catch {
525
+ return null;
526
+ }
527
+ let parsed: unknown;
528
+ try {
529
+ parsed = JSON.parse(text);
530
+ } catch {
531
+ return null;
532
+ }
533
+ if (!isPlainObject(parsed)) return null;
534
+ if (Object.keys(parsed).sort().join(",") !== "members,placement,slotCommitment,type") return null;
535
+ if (parsed["type"] !== FUSE_PROFILE || parsed["placement"] !== SET_PLACEMENT_ID) return null;
536
+ const commitment = readDigestField(parsed["slotCommitment"]);
537
+ if (commitment === null) return null;
538
+ const list = parsed["members"];
539
+ if (!Array.isArray(list) || list.length === 0) return null;
540
+ const members: SetMember[] = [];
541
+ for (const row of list as unknown[]) {
542
+ if (!isPlainObject(row)) return null;
543
+ if (Object.keys(row).sort().join(",") !== "artifact,origin,placement") return null;
544
+ const artifact = readDigestField(row["artifact"]);
545
+ const origin = readDigestField(row["origin"]);
546
+ const placement = row["placement"];
547
+ if (artifact === null || origin === null || typeof placement !== "string") return null;
548
+ if (!PLACEMENT_ID_PATTERN.test(placement) || placement === SET_PLACEMENT_ID) return null;
549
+ members.push({ artifact, origin, placement });
550
+ }
551
+ let rebuilt: Uint8Array;
552
+ try {
553
+ rebuilt = buildSetManifest(commitment, members);
554
+ } catch {
555
+ return null;
556
+ }
557
+ if (!bytesEqual(rebuilt, bytes)) return null;
558
+ return { commitment, members };
559
+ }
560
+
561
+ /**
562
+ * The manifest bytes a proof carries under proof.metadata[SET_METADATA_KEY],
563
+ * re-canonicalized from the parsed object, or null when there is none or it
564
+ * is not a plain object. UNBOUND and UNVALIDATED: metadata is unsigned, so
565
+ * this returns bytes only, never rows. Nothing reads a member from it except
566
+ * through verifyFuseMember, which first requires these bytes to parse
567
+ * strictly and to hash to the signed artifact digest.
568
+ */
569
+ export function readSetMetadata(proof: BitGraphProof): Uint8Array | null {
570
+ const value = proof.metadata?.[SET_METADATA_KEY];
571
+ if (!isPlainObject(value)) return null;
572
+ try {
573
+ return canonicalize(value);
574
+ } catch {
575
+ return null;
576
+ }
577
+ }
578
+
579
+ const set1: Placement = {
580
+ id: SET_PLACEMENT_ID,
581
+ form: "C",
582
+ byteExact: false,
583
+ build() {
584
+ throw new TypeError("set/1 is built with buildSetManifest(commitment, members)");
585
+ },
586
+ locate(fused) {
587
+ const manifest = parseSetManifest(fused);
588
+ return manifest === null ? null : { commitment: manifest.commitment };
589
+ },
590
+ };
591
+
592
+ /**
593
+ * Resolve a placement by id. set/1 resolves here but is NOT in PLACEMENTS:
594
+ * the undeclared scan is for bytes whose placement was not declared, whereas
595
+ * a set manifest is identified by hashing to the signed artifact digest and
596
+ * by its signed title, so the scan order of every existing fixture is
597
+ * literally unchanged.
598
+ */
412
599
  export function getPlacement(id: string): Placement | undefined {
413
- return PLACEMENTS.find((p) => p.id === id);
600
+ return [...PLACEMENTS, set1].find((p) => p.id === id);
414
601
  }
415
602
 
416
603
  // ---------------------------------------------------------------------------
417
604
  // Attribution (the signed carrier of placement and origin, spec 6.5)
418
605
  // ---------------------------------------------------------------------------
419
606
 
420
- /** attribution.name = "bitgraph-fuse/1" (the profile id), title = placement id, message = origin digest in standard base64. */
607
+ /**
608
+ * attribution.name = "bitgraph-fuse/1" (the profile id), title = placement id,
609
+ * message = origin digest in standard base64. A set has no single origin, so
610
+ * set/1 refuses an origin digest: a set/1 marker carrying one is out of
611
+ * profile and verifyFuseMember refuses it.
612
+ */
421
613
  export function fuseAttribution(placement: PlacementId, originDigest?: Uint8Array): Attribution {
422
614
  if (originDigest !== undefined && originDigest.length !== 32) throw new TypeError("originDigest must be 32 bytes");
615
+ if (placement === SET_PLACEMENT_ID && originDigest !== undefined) throw new TypeError("set/1 has no single origin; a set marker carries no origin digest");
423
616
  return {
424
617
  name: FUSE_ATTRIBUTION_NAME,
425
618
  title: placement,
package/src/index.ts CHANGED
@@ -41,6 +41,11 @@ export {
41
41
  CONTAINER_ORIGINAL_PATH,
42
42
  PLACEMENTS,
43
43
  getPlacement,
44
+ SET_PLACEMENT_ID,
45
+ SET_METADATA_KEY,
46
+ buildSetManifest,
47
+ parseSetManifest,
48
+ readSetMetadata,
44
49
  canonicalSlotBody,
45
50
  computeSlotRecordHash,
46
51
  slotCommitmentPreimage,
@@ -59,6 +64,8 @@ export {
59
64
  hexToBytes,
60
65
  bytesEqual,
61
66
  } from "./fuse.js";
62
- export type { PlacementId, Placement, Located, FusePayload, FuseFrame, FuseMarker, MarkerSource } from "./fuse.js";
67
+ export type { PlacementId, Placement, Located, FusePayload, FuseFrame, FuseMarker, MarkerSource, SetMember, SetManifest } from "./fuse.js";
63
68
  export { verifyFuse, assembledAfterCommit } from "./fuse-verify.js";
64
69
  export type { FuseCategory, FuseSpan, FuseVerifyResult, FuseVerifyOptions } from "./fuse-verify.js";
70
+ export { verifyFuseMember } from "./fuse-member.js";
71
+ export type { FuseMemberCategory, FuseSetEvidence, FuseMemberOptions, FuseMemberResult } from "./fuse-member.js";