@charter-agreement-protocol/verifier 0.2.1 → 0.3.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/README.md CHANGED
@@ -61,6 +61,12 @@ cannot verify green.
61
61
  | `decodeJsonText(text)` | Strict I-JSON decode (duplicates, noncharacters, lone surrogates, number round-tripping rejected) |
62
62
  | `jsonProjection(value)` | The tagged projection used by reports |
63
63
  | `CERTIFIED_INDEX_SHA256_BASE64URL`, `CERTIFIED_REGISTRY_DIGEST` | The certified identity pins this build carries |
64
+ | `verifyDescriptor(compact, predecessor?)` / `verifyDescriptorChain(compacts)` | Artifact-level verification: one party descriptor (with optional predecessor facts), or a full descriptor chain |
65
+ | `verifyAcceptance(compact, revisionText, descriptorCompacts)` / `verifyTermination(compact, revisionText, descriptorCompacts)` | Artifact-level verification of an acceptance / termination notice against the named revision and the signing party's descriptor chain |
66
+ | `verifyChain(chainInput)` / `verifyReceipt(compact, chainInput)` | Full chain-view verification (facts include the accepted/superseded topology and the verified acceptances); receipt verification against the verified chain |
67
+ | `checkSigningClaims(kind, claims)` | The producer build gate's claims half (the reference `decode_for_signing`): the claims-only schema checks per kind — `descriptor \| acceptance \| termination \| receipt` — shared with the verify paths; holder-side signers run it before any key is used |
68
+ | `decodeArtifact(compact)` / `verifySignature(message, signature, publicKey, alg)` | Framing-level decode (the holder-side provisional check) and the strict wrong-key guard primitive |
69
+ | `acceptanceRefusal(claims, chainInput)` / `terminationRefusal(claims, chainInput)` | The honest-signer refusal checks (R1–R3: claims-truth, no-equivocation, ancestry/governing coverage) over the caller's own view — `{ok: true}`, or `signing_refused` / `chain_invalid` / `signing_input_invalid`. Holder-side signers run these before any key is used; the rule that fired is deliberately not surfaced |
64
70
 
65
71
  ## Evidence
66
72
 
package/dist/core.d.ts CHANGED
@@ -130,4 +130,18 @@ export declare function verifyAcceptance(compact: string, revisionText: string,
130
130
  export declare function verifyTermination(compact: string, revisionText: string, descriptorCompacts: string[]): VerifyResult;
131
131
  export declare function verifyChain(chainInput: AnyRecord): VerifyResult;
132
132
  export declare function verifyReceipt(compact: string, chainInput: AnyRecord): VerifyResult;
133
+ export declare function checkSigningClaims(kind: "descriptor" | "acceptance" | "termination" | "receipt", claims: AnyRecord): {
134
+ ok: true;
135
+ } | {
136
+ ok: false;
137
+ code: string;
138
+ };
139
+ export type RefusalResult = {
140
+ ok: true;
141
+ } | {
142
+ ok: false;
143
+ code: string;
144
+ };
145
+ export declare function acceptanceRefusal(claims: AnyRecord, chainInput: AnyRecord): RefusalResult;
146
+ export declare function terminationRefusal(claims: AnyRecord, chainInput: AnyRecord): RefusalResult;
133
147
  export {};
package/dist/core.js CHANGED
@@ -562,6 +562,11 @@ function keyGrammarError(payload) {
562
562
  return "nested_invalid";
563
563
  }
564
564
  for (const one of payload.verification_keys) {
565
+ // Malformed entries carry the typed code, never a dereference throw -
566
+ // the pre-sign claims gate and the verify path share this walk.
567
+ if (!one || typeof one !== "object" || typeof one.algorithm !== "string" || typeof one.public_key !== "string") {
568
+ return "nested_invalid";
569
+ }
565
570
  const row = ALG_ROWS.find((candidate) => candidate.keyAlgorithm === one.algorithm);
566
571
  if (!row)
567
572
  return "nested_invalid";
@@ -650,6 +655,8 @@ function revisionFromText(text) {
650
655
  catch (_error) {
651
656
  return fail("invalid_syntax");
652
657
  }
658
+ if (!value || typeof value !== "object" || Array.isArray(value))
659
+ return fail("revision_invalid");
653
660
  const unknown = Object.keys(value).find((key) => !REVISION_FIELDS.includes(key));
654
661
  if (unknown)
655
662
  return fail("unknown_member");
@@ -704,10 +711,9 @@ function acceptanceFromCompact(compact, revision, chain) {
704
711
  if (!decoded.ok)
705
712
  return decoded;
706
713
  const claims = decoded.value.payload;
707
- if ((claims.revision_number === 1) !== (claims.prev_revision_digest === undefined))
708
- return fail("acceptance_invalid");
709
- if (!parseTimestamp(claims.accepted_at))
710
- return fail("timestamp_invalid");
714
+ const acceptanceShape = acceptanceClaimsError(claims);
715
+ if (acceptanceShape)
716
+ return fail(acceptanceShape);
711
717
  const descriptor = chain.descriptors.find((one) => one.digest === claims.party_descriptor_digest);
712
718
  if (!descriptor || !verifyDecodedJws(decoded.value, "cap+acceptance", descriptor.payload.verification_keys))
713
719
  return fail("signature_invalid");
@@ -730,10 +736,9 @@ function terminationFromCompact(compact, revision, chain) {
730
736
  const party = revision.value.parties.find((one) => one.party_descriptor_digest === claims.party_descriptor_digest);
731
737
  if (claims.charter_id !== expectedCharter || claims.governing_revision_digest !== revision.digest || claims.party_role !== party?.role || !revision.value.termination_rules.reason_codes.includes(claims.reason_code))
732
738
  return fail("termination_claims_mismatch");
733
- const issuedAt = parseTimestamp(claims.issued_at);
734
- const effectiveAt = parseTimestamp(claims.effective_at);
735
- if (!issuedAt || !effectiveAt || compareTimestamps(issuedAt, effectiveAt) > 0)
736
- return fail("termination_invalid");
739
+ const terminationShape = terminationClaimsError(claims);
740
+ if (terminationShape)
741
+ return fail(terminationShape);
737
742
  return ok({ claims, digest: taggedHash("termination_content", decoded.value.payloadBytes), descriptorPosition: chain.positions[claims.party_descriptor_digest] });
738
743
  }
739
744
  // Chain-level structural checks mirroring Chain.verify: unique revision
@@ -782,7 +787,7 @@ function chainFromInput(input) {
782
787
  }
783
788
  const acceptances = [];
784
789
  const coordinates = new Set();
785
- for (const compact of input.acceptances) {
790
+ for (const compact of (input.acceptances || [])) {
786
791
  const decoded = decodeJws(compact);
787
792
  if (!decoded.ok)
788
793
  return fail("chain_invalid");
@@ -844,7 +849,7 @@ function chainFromInput(input) {
844
849
  };
845
850
  const linear = heads.length === 1 && ancestryCovers(heads[0].digest);
846
851
  const topology = active.length > 0 && !linear ? "forked" : "linear";
847
- return ok({ descriptors: descriptors.value, revisions, accepted, acceptedDigests: accepted.map((one) => one.digest).sort(), supersededDigests: [...superseded].sort(), topology, charterId });
852
+ return ok({ descriptors: descriptors.value, revisions, acceptances, accepted, acceptedDigests: accepted.map((one) => one.digest).sort(), supersededDigests: [...superseded].sort(), topology, charterId });
848
853
  }
849
854
  // Governing mirrors the reference semantics exactly: candidates are accepted,
850
855
  // non-superseded, and effective at the instant (start-inclusive, end-exclusive
@@ -920,35 +925,19 @@ function receiptFromCompact(compact, chain) {
920
925
  if (!decoded.ok)
921
926
  return decoded;
922
927
  const claims = decoded.value.payload;
923
- if (claims.grant?.scheme === "bap" && claims.grant?.grant_digest === undefined)
924
- return fail("receipt_invalid");
925
- const optional = claims.extensions?.optional || {};
926
- for (const namespace of Object.keys(optional)) {
927
- const profile = EXTENSION_PROFILES.find((entry) => entry.namespace === namespace);
928
- if (!profile || profile.state === "reserved" || profile.state === "retired")
929
- continue;
930
- if (profile.surface !== "receipt")
931
- return fail("extension_scope_invalid");
932
- if (profile.schema_digest === null)
933
- return fail("extension_schema_unavailable");
934
- }
928
+ const receiptShape = receiptGrantError(claims);
929
+ if (receiptShape)
930
+ return fail(receiptShape);
935
931
  const claimedRevision = chain.accepted.find((one) => one.digest === claims.revision_digest);
936
932
  const verifiedPublicKeys = new Set(receiptSigningKeys(chain, claimedRevision, claims.issuing_party_role)
937
933
  .filter((key) => verifyDecodedJws(decoded.value, "cap+receipt", [key]))
938
934
  .map((key) => key.public_key));
939
935
  if (verifiedPublicKeys.size !== 1)
940
936
  return fail("signature_invalid");
941
- const occurredAt = parseTimestamp(claims.occurred_at);
942
- const recordedAt = parseTimestamp(claims.recorded_at);
943
- if (!occurredAt || !recordedAt || compareTimestamps(recordedAt, occurredAt) < 0)
944
- return fail("receipt_invalid");
945
- // The closed matrix mirrors the reference decoder: rejected requires
946
- // no_effect; accepted admits effect_committed, no_effect, or indeterminate.
947
- const outcomeAllowed = claims.decision === "rejected" ? claims.outcome === "no_effect" :
948
- (claims.outcome === "effect_committed" || claims.outcome === "no_effect" || claims.outcome === "indeterminate");
949
- if (!outcomeAllowed)
950
- return fail("cross_field_invalid");
951
- const governingDigest = governing(chain, occurredAt);
937
+ const receiptTiming = receiptTimingError(claims);
938
+ if (receiptTiming)
939
+ return fail(receiptTiming);
940
+ const governingDigest = governing(chain, parseTimestamp(claims.occurred_at));
952
941
  const projection = projectReceipt(claims, chain, governingDigest);
953
942
  if (!projection.ok)
954
943
  return projection;
@@ -1397,3 +1386,171 @@ export function verifyReceipt(compact, chainInput) {
1397
1386
  },
1398
1387
  };
1399
1388
  }
1389
+ // ---------------------------------------------------------------------------
1390
+ // The producer build gate's claims half (the reference decode_for_signing):
1391
+ // the schema checks that depend only on the claims, shared with the verify
1392
+ // paths below so the pre-sign gate and the post-sign discipline are ONE
1393
+ // implementation. Holder-side signers run checkSigningClaims before any key
1394
+ // is used; malformed claims are a typed producer rejection, never a burned
1395
+ // key operation.
1396
+ // ---------------------------------------------------------------------------
1397
+ export function checkSigningClaims(kind, claims) {
1398
+ if (!claims || typeof claims !== "object" || Array.isArray(claims))
1399
+ return { ok: false, code: "invalid_type" };
1400
+ const error = kind === "descriptor" ? descriptorClaimsError(claims) :
1401
+ kind === "acceptance" ? acceptanceClaimsError(claims) :
1402
+ kind === "termination" ? terminationClaimsError(claims) :
1403
+ kind === "receipt" ? (receiptGrantError(claims) ?? receiptTimingError(claims)) :
1404
+ "invalid_type";
1405
+ return error ? { ok: false, code: error } : { ok: true };
1406
+ }
1407
+ function acceptanceClaimsError(claims) {
1408
+ if ((claims.revision_number === 1) !== (claims.prev_revision_digest === undefined))
1409
+ return "acceptance_invalid";
1410
+ if (!parseTimestamp(claims.accepted_at))
1411
+ return "timestamp_invalid";
1412
+ return null;
1413
+ }
1414
+ function terminationClaimsError(claims) {
1415
+ const issuedAt = parseTimestamp(claims.issued_at);
1416
+ const effectiveAt = parseTimestamp(claims.effective_at);
1417
+ if (!issuedAt || !effectiveAt || compareTimestamps(issuedAt, effectiveAt) > 0)
1418
+ return "termination_invalid";
1419
+ return null;
1420
+ }
1421
+ function receiptGrantError(claims) {
1422
+ if (claims.grant?.scheme === "bap" && claims.grant?.grant_digest === undefined)
1423
+ return "receipt_invalid";
1424
+ const optional = claims.extensions?.optional || {};
1425
+ for (const namespace of Object.keys(optional)) {
1426
+ const profile = EXTENSION_PROFILES.find((entry) => entry.namespace === namespace);
1427
+ if (!profile || profile.state === "reserved" || profile.state === "retired")
1428
+ continue;
1429
+ if (profile.surface !== "receipt")
1430
+ return "extension_scope_invalid";
1431
+ if (profile.schema_digest === null)
1432
+ return "extension_schema_unavailable";
1433
+ }
1434
+ return null;
1435
+ }
1436
+ function receiptTimingError(claims) {
1437
+ const occurredAt = parseTimestamp(claims.occurred_at);
1438
+ const recordedAt = parseTimestamp(claims.recorded_at);
1439
+ if (!occurredAt || !recordedAt || compareTimestamps(recordedAt, occurredAt) < 0)
1440
+ return "receipt_invalid";
1441
+ // The closed matrix mirrors the reference decoder: rejected requires
1442
+ // no_effect; accepted admits effect_committed, no_effect, or indeterminate.
1443
+ const outcomeAllowed = claims.decision === "rejected" ? claims.outcome === "no_effect" :
1444
+ (claims.outcome === "effect_committed" || claims.outcome === "no_effect" || claims.outcome === "indeterminate");
1445
+ if (!outcomeAllowed)
1446
+ return "cross_field_invalid";
1447
+ return null;
1448
+ }
1449
+ function descriptorClaimsError(payload) {
1450
+ const grammar = keyGrammarError(payload);
1451
+ if (grammar)
1452
+ return grammar;
1453
+ if (!parseTimestamp(payload.effective_from))
1454
+ return "timestamp_invalid";
1455
+ if (payload.descriptor_number === 1 && payload.prev_descriptor_digest !== undefined)
1456
+ return "descriptor_invalid";
1457
+ return null;
1458
+ }
1459
+ function refused() {
1460
+ return { ok: false, code: "signing_refused" };
1461
+ }
1462
+ // The producer build gate these checks assume already ran upstream in the
1463
+ // reference: fields the refusal rules consume must be well-typed, or the
1464
+ // caller gets the typed producer code instead of a refusal.
1465
+ function refusalClaimsShape(claims, stringFields, integerFields) {
1466
+ if (!claims || typeof claims !== "object" || Array.isArray(claims))
1467
+ return false;
1468
+ return stringFields.every((field) => typeof claims[field] === "string") &&
1469
+ integerFields.every((field) => typeof claims[field] === "number" && Number.isInteger(claims[field]));
1470
+ }
1471
+ // A non-object view mirrors the reference producer's non-ArtifactSet arm
1472
+ // (the typed producer code); an object that fails the chain discipline is
1473
+ // the caller's invalid set. Neither may throw out of the refusal boundary.
1474
+ function refusalChain(chainInput) {
1475
+ if (!chainInput || typeof chainInput !== "object" || Array.isArray(chainInput))
1476
+ return { invalidSet: "signing_input_invalid" };
1477
+ const chain = chainFromInput(chainInput);
1478
+ return chain.ok ? { chain: chain.value } : { invalidSet: "chain_invalid" };
1479
+ }
1480
+ function refusalPartyMatches(revision, claims) {
1481
+ return revision.value.parties.some((party) => party.role === claims.party_role && party.party_descriptor_digest === claims.party_descriptor_digest);
1482
+ }
1483
+ // Walk up the candidate's prev links through the whole verified revision
1484
+ // index; the verified chain guarantees strictly decreasing numbers, so the
1485
+ // walk terminates. Identity is ancestry (re-accepting a head covers itself).
1486
+ function refusalAncestorOf(candidate, target, byDigest) {
1487
+ if (candidate === target)
1488
+ return true;
1489
+ const previous = byDigest.get(candidate)?.value.prev_revision_digest;
1490
+ return typeof previous === "string" ? refusalAncestorOf(previous, target, byDigest) : false;
1491
+ }
1492
+ export function acceptanceRefusal(claims, chainInput) {
1493
+ if (!refusalClaimsShape(claims, ["revision_digest", "charter_id", "party_role", "party_descriptor_digest"], ["revision_number"])) {
1494
+ return { ok: false, code: "signing_input_invalid" };
1495
+ }
1496
+ const resolved = refusalChain(chainInput);
1497
+ if ("invalidSet" in resolved)
1498
+ return { ok: false, code: resolved.invalidSet };
1499
+ const chain = resolved.chain;
1500
+ // R1 claims-truth: the named revision exists and every coordinate binds.
1501
+ const revision = chain.revisions.find((one) => one.digest === claims.revision_digest);
1502
+ if (!revision)
1503
+ return refused();
1504
+ const charterId = revision.value.charter_id || revision.digest;
1505
+ if (claims.charter_id !== charterId ||
1506
+ claims.revision_number !== revision.value.revision_number ||
1507
+ (revision.value.prev_revision_digest || undefined) !== (claims.prev_revision_digest || undefined) ||
1508
+ !refusalPartyMatches(revision, claims)) {
1509
+ return refused();
1510
+ }
1511
+ // R2 no-equivocation: no existing acceptance holds this charter/number
1512
+ // coordinate for a different revision.
1513
+ const equivocation = chain.acceptances.some((one) => one.claims.charter_id === claims.charter_id &&
1514
+ one.claims.revision_number === claims.revision_number &&
1515
+ one.claims.revision_digest !== claims.revision_digest);
1516
+ if (equivocation)
1517
+ return refused();
1518
+ // R3 ancestry coverage: every maximum accepted head is superseded by the
1519
+ // candidate or an ancestor of it (an empty accepted set covers trivially).
1520
+ const maximum = Math.max(...chain.accepted.map((one) => one.value.revision_number));
1521
+ const heads = chain.accepted.filter((one) => one.value.revision_number === maximum).map((one) => one.digest);
1522
+ const byDigest = new Map(chain.revisions.map((one) => [one.digest, one]));
1523
+ const covered = heads.every((head) => (revision.value.supersedes || []).includes(head) || refusalAncestorOf(revision.digest, head, byDigest));
1524
+ if (!covered)
1525
+ return refused();
1526
+ return { ok: true };
1527
+ }
1528
+ export function terminationRefusal(claims, chainInput) {
1529
+ if (!refusalClaimsShape(claims, ["governing_revision_digest", "charter_id", "party_role", "party_descriptor_digest", "reason_code"], [])) {
1530
+ return { ok: false, code: "signing_input_invalid" };
1531
+ }
1532
+ const effectiveAt = parseTimestamp(claims.effective_at);
1533
+ if (!effectiveAt)
1534
+ return { ok: false, code: "signing_input_invalid" };
1535
+ const resolved = refusalChain(chainInput);
1536
+ if ("invalidSet" in resolved)
1537
+ return { ok: false, code: resolved.invalidSet };
1538
+ const chain = resolved.chain;
1539
+ // R1 claims-truth: the named revision exists, the reason is listed in its
1540
+ // termination rules, and the signing party is one of its parties.
1541
+ const revision = chain.revisions.find((one) => one.digest === claims.governing_revision_digest);
1542
+ if (!revision)
1543
+ return refused();
1544
+ const charterId = revision.value.charter_id || revision.digest;
1545
+ if (claims.charter_id !== charterId ||
1546
+ !revision.value.termination_rules.reason_codes.includes(claims.reason_code) ||
1547
+ !refusalPartyMatches(revision, claims)) {
1548
+ return refused();
1549
+ }
1550
+ // R3 governing coverage: the named revision must be THE unique governing
1551
+ // revision at the notice's own effective instant — a stale or contested
1552
+ // view refuses.
1553
+ if (governing(chain, effectiveAt) !== revision.digest)
1554
+ return refused();
1555
+ return { ok: true };
1556
+ }
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { canonical, loadCorpus, reportFor, selfChecks, decodeJsonText, jsonProjection, type CanonicalValue, type Projection, type DecodeResult, type CaseResult, type CorpusFile, type CorpusIndex, type ConformanceCase, type LoadedCorpus, type CaseOutcome, type ConformanceReport, type ReportOutput, } from "./core.ts";
2
- export { CERTIFIED_INDEX_SHA256_BASE64URL, CERTIFIED_REGISTRY_DIGEST, algorithmRegistry, emissions, defaultEmissionName, encodeBase64url, taggedDigest, decodeArtifact, verifySignature, verifyDescriptor, verifyDescriptorChain, verifyAcceptance, verifyTermination, verifyChain, verifyReceipt, type AlgRow, type VerifyResult, } from "./core.ts";
2
+ export { CERTIFIED_INDEX_SHA256_BASE64URL, CERTIFIED_REGISTRY_DIGEST, algorithmRegistry, emissions, defaultEmissionName, encodeBase64url, taggedDigest, decodeArtifact, verifySignature, verifyDescriptor, verifyDescriptorChain, verifyAcceptance, verifyTermination, verifyChain, verifyReceipt, acceptanceRefusal, checkSigningClaims, terminationRefusal, type AlgRow, type VerifyResult, type RefusalResult, } from "./core.ts";
package/dist/index.js CHANGED
@@ -5,4 +5,4 @@
5
5
  // and the self-check battery. Every verdict traces to corpus agreement with
6
6
  // the Elixir reference implementation, recomputed from raw bytes.
7
7
  export { canonical, loadCorpus, reportFor, selfChecks, decodeJsonText, jsonProjection, } from "./core.js";
8
- export { CERTIFIED_INDEX_SHA256_BASE64URL, CERTIFIED_REGISTRY_DIGEST, algorithmRegistry, emissions, defaultEmissionName, encodeBase64url, taggedDigest, decodeArtifact, verifySignature, verifyDescriptor, verifyDescriptorChain, verifyAcceptance, verifyTermination, verifyChain, verifyReceipt, } from "./core.js";
8
+ export { CERTIFIED_INDEX_SHA256_BASE64URL, CERTIFIED_REGISTRY_DIGEST, algorithmRegistry, emissions, defaultEmissionName, encodeBase64url, taggedDigest, decodeArtifact, verifySignature, verifyDescriptor, verifyDescriptorChain, verifyAcceptance, verifyTermination, verifyChain, verifyReceipt, acceptanceRefusal, checkSigningClaims, terminationRefusal, } from "./core.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charter-agreement-protocol/verifier",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Independent TypeScript verifier for the Charter Agreement Protocol (CAP) — byte-exact, offline re-verification of signed charter evidence. CAP verifies; it never authorizes.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",