@peac/schema 0.11.2 → 0.12.0-preview.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.
Files changed (49) hide show
  1. package/README.md +64 -3
  2. package/dist/actor-binding.d.ts +148 -0
  3. package/dist/actor-binding.d.ts.map +1 -0
  4. package/dist/dispute.d.ts +4 -4
  5. package/dist/errors.d.ts +2 -1
  6. package/dist/errors.d.ts.map +1 -1
  7. package/dist/extensions/control-action.d.ts +68 -0
  8. package/dist/extensions/control-action.d.ts.map +1 -0
  9. package/dist/extensions/credential-event.d.ts +53 -0
  10. package/dist/extensions/credential-event.d.ts.map +1 -0
  11. package/dist/extensions/fingerprint-ref.d.ts +50 -0
  12. package/dist/extensions/fingerprint-ref.d.ts.map +1 -0
  13. package/dist/extensions/index.d.ts +16 -0
  14. package/dist/extensions/index.d.ts.map +1 -0
  15. package/dist/extensions/tool-registry.d.ts +32 -0
  16. package/dist/extensions/tool-registry.d.ts.map +1 -0
  17. package/dist/extensions/treaty.d.ts +55 -0
  18. package/dist/extensions/treaty.d.ts.map +1 -0
  19. package/dist/index.cjs +883 -5
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.ts +24 -3
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.mjs +803 -7
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/issuer-config.d.ts +61 -0
  26. package/dist/issuer-config.d.ts.map +1 -0
  27. package/dist/policy-binding.d.ts +24 -0
  28. package/dist/policy-binding.d.ts.map +1 -0
  29. package/dist/receipt-parser.cjs +492 -3
  30. package/dist/receipt-parser.cjs.map +1 -1
  31. package/dist/receipt-parser.d.ts +36 -14
  32. package/dist/receipt-parser.d.ts.map +1 -1
  33. package/dist/receipt-parser.mjs +493 -5
  34. package/dist/receipt-parser.mjs.map +1 -1
  35. package/dist/types.d.ts +17 -0
  36. package/dist/types.d.ts.map +1 -1
  37. package/dist/validators.d.ts +16 -0
  38. package/dist/validators.d.ts.map +1 -1
  39. package/dist/wire-02-envelope.d.ts +152 -0
  40. package/dist/wire-02-envelope.d.ts.map +1 -0
  41. package/dist/wire-02-extensions.d.ts +216 -0
  42. package/dist/wire-02-extensions.d.ts.map +1 -0
  43. package/dist/wire-02-registries.d.ts +21 -0
  44. package/dist/wire-02-registries.d.ts.map +1 -0
  45. package/dist/wire-02-representation.d.ts +49 -0
  46. package/dist/wire-02-representation.d.ts.map +1 -0
  47. package/dist/wire-02-warnings.d.ts +29 -0
  48. package/dist/wire-02-warnings.d.ts.map +1 -0
  49. package/package.json +2 -2
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Issuer Configuration Schema Extensions (v0.11.3+, DD-148)
3
+ *
4
+ * Zod schemas for revoked_keys field in peac-issuer.json.
5
+ * Reason values aligned with RFC 5280 CRLReason subset
6
+ * (only values meaningful for receipt signing keys).
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ import { z } from 'zod';
11
+ /**
12
+ * Revocation reasons: RFC 5280 CRLReason subset relevant to receipt signing keys.
13
+ */
14
+ export declare const REVOCATION_REASONS: readonly ["key_compromise", "superseded", "cessation_of_operation", "privilege_withdrawn"];
15
+ export type RevocationReason = (typeof REVOCATION_REASONS)[number];
16
+ /**
17
+ * Schema for a single revoked key entry.
18
+ */
19
+ export declare const RevokedKeyEntrySchema: z.ZodObject<{
20
+ kid: z.ZodString;
21
+ revoked_at: z.ZodString;
22
+ reason: z.ZodOptional<z.ZodEnum<{
23
+ key_compromise: "key_compromise";
24
+ superseded: "superseded";
25
+ cessation_of_operation: "cessation_of_operation";
26
+ privilege_withdrawn: "privilege_withdrawn";
27
+ }>>;
28
+ }, z.core.$strict>;
29
+ export type RevokedKeyEntryInput = z.input<typeof RevokedKeyEntrySchema>;
30
+ export type RevokedKeyEntryOutput = z.output<typeof RevokedKeyEntrySchema>;
31
+ /**
32
+ * Schema for the revoked_keys array in issuer configuration.
33
+ * Maximum 100 entries to prevent unbounded growth.
34
+ */
35
+ export declare const RevokedKeysArraySchema: z.ZodArray<z.ZodObject<{
36
+ kid: z.ZodString;
37
+ revoked_at: z.ZodString;
38
+ reason: z.ZodOptional<z.ZodEnum<{
39
+ key_compromise: "key_compromise";
40
+ superseded: "superseded";
41
+ cessation_of_operation: "cessation_of_operation";
42
+ privilege_withdrawn: "privilege_withdrawn";
43
+ }>>;
44
+ }, z.core.$strict>>;
45
+ /**
46
+ * Validate a revoked_keys array.
47
+ * Returns a discriminated result (no exceptions).
48
+ */
49
+ export declare function validateRevokedKeys(data: unknown): {
50
+ ok: true;
51
+ value: RevokedKeyEntryOutput[];
52
+ } | {
53
+ ok: false;
54
+ error: string;
55
+ };
56
+ /**
57
+ * Check if a kid is present in a revoked_keys array.
58
+ * Returns the revocation entry if found, null otherwise.
59
+ */
60
+ export declare function findRevokedKey(revokedKeys: RevokedKeyEntryOutput[], kid: string): RevokedKeyEntryOutput | null;
61
+ //# sourceMappingURL=issuer-config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"issuer-config.d.ts","sourceRoot":"","sources":["../src/issuer-config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;GAEG;AACH,eAAO,MAAM,kBAAkB,4FAKrB,CAAC;AAEX,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEnE;;GAEG;AACH,eAAO,MAAM,qBAAqB;;;;;;;;;kBASvB,CAAC;AAEZ,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AACzE,MAAM,MAAM,qBAAqB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAE3E;;;GAGG;AACH,eAAO,MAAM,sBAAsB;;;;;;;;;mBAA0C,CAAC;AAE9E;;;GAGG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,OAAO,GACZ;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,qBAAqB,EAAE,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAM7E;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAC5B,WAAW,EAAE,qBAAqB,EAAE,EACpC,GAAG,EAAE,MAAM,GACV,qBAAqB,GAAG,IAAI,CAE9B"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Policy binding comparison (Layer 1, DD-49, DD-151)
3
+ *
4
+ * Pure string comparison with no I/O and no crypto imports (DD-141).
5
+ * The digest format is 'sha256:<64 lowercase hex>'.
6
+ *
7
+ * This function handles only the binary match/mismatch decision. The full
8
+ * 3-state result ('verified' | 'failed' | 'unavailable') is computed by
9
+ * checkPolicyBinding() in @peac/protocol (Layer 3), which handles the
10
+ * absent-digest case and invokes computePolicyDigestJcs() for hashing.
11
+ */
12
+ /**
13
+ * Compare a receipt policy digest against a locally-computed digest.
14
+ *
15
+ * Returns 'verified' if the two digests match exactly, 'failed' otherwise.
16
+ * Both arguments must be present; callers must handle the absent-digest case
17
+ * (producing 'unavailable') before calling this function.
18
+ *
19
+ * @param receiptDigest - policy.digest from the receipt claims
20
+ * @param localDigest - digest computed from the caller's local policy bytes
21
+ * @returns 'verified' on exact match, 'failed' on mismatch
22
+ */
23
+ export declare function verifyPolicyBinding(receiptDigest: string, localDigest: string): 'verified' | 'failed';
24
+ //# sourceMappingURL=policy-binding.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"policy-binding.d.ts","sourceRoot":"","sources":["../src/policy-binding.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CACjC,aAAa,EAAE,MAAM,EACrB,WAAW,EAAE,MAAM,GAClB,UAAU,GAAG,QAAQ,CAEvB"}
@@ -45,6 +45,10 @@ var JsonObjectSchema = PlainObjectSchema.transform(
45
45
  (obj) => obj
46
46
  ).pipe(zod.z.record(zod.z.string(), JsonValueSchema));
47
47
  zod.z.array(JsonValueSchema);
48
+ var ERROR_CODES = {
49
+ // Wire 0.2 extension errors (400, DD-153/DD-156)
50
+ E_INVALID_EXTENSION_KEY: "E_INVALID_EXTENSION_KEY"
51
+ };
48
52
 
49
53
  // src/purpose.ts
50
54
  var PURPOSE_TOKEN_REGEX = /^[a-z](?:[a-z0-9_-]*[a-z0-9])?(?::[a-z](?:[a-z0-9_-]*[a-z0-9])?)?$/;
@@ -274,15 +278,465 @@ var AttestationReceiptClaimsSchema = zod.z.object({
274
278
  /** Extensions (optional) */
275
279
  ext: AttestationExtensionsSchema.optional()
276
280
  }).strict();
281
+ var PROOF_TYPES = [
282
+ "ed25519-cert-chain",
283
+ "eat-passport",
284
+ "eat-background-check",
285
+ "sigstore-oidc",
286
+ "did",
287
+ "spiffe",
288
+ "x509-pki",
289
+ "custom"
290
+ ];
291
+ var ProofTypeSchema = zod.z.enum(PROOF_TYPES);
292
+ function isOriginOnly(value) {
293
+ try {
294
+ const url = new URL(value);
295
+ if (url.protocol !== "https:" && url.protocol !== "http:") {
296
+ return false;
297
+ }
298
+ if (url.pathname !== "/") {
299
+ return false;
300
+ }
301
+ if (url.search !== "") {
302
+ return false;
303
+ }
304
+ if (url.hash !== "" || value.includes("#")) {
305
+ return false;
306
+ }
307
+ if (url.username !== "" || url.password !== "") {
308
+ return false;
309
+ }
310
+ if (url.hostname.endsWith(".")) {
311
+ return false;
312
+ }
313
+ const hostPart = value.replace(/^https?:\/\//, "").split(/[/:]/)[0];
314
+ if (hostPart.endsWith(".")) {
315
+ return false;
316
+ }
317
+ if (url.hostname.includes("%")) {
318
+ return false;
319
+ }
320
+ return true;
321
+ } catch {
322
+ return false;
323
+ }
324
+ }
325
+ var ActorBindingSchema = zod.z.object({
326
+ /** Stable actor identifier (opaque, no PII) */
327
+ id: zod.z.string().min(1).max(256),
328
+ /** Proof type from DD-143 multi-root vocabulary */
329
+ proof_type: ProofTypeSchema,
330
+ /** URI or hash of external proof artifact */
331
+ proof_ref: zod.z.string().max(2048).optional(),
332
+ /** Origin-only URL: scheme + host + optional port; NO path, query, or fragment */
333
+ origin: zod.z.string().max(2048).refine(isOriginOnly, {
334
+ message: "origin must be an origin-only URL (scheme + host + optional port; no path, query, or fragment)"
335
+ }),
336
+ /** SHA-256 hash of the intent (hash-first per DD-138) */
337
+ intent_hash: zod.z.string().regex(/^sha256:[a-f0-9]{64}$/, {
338
+ message: "intent_hash must match sha256:<64 hex chars>"
339
+ }).optional()
340
+ }).strict();
341
+ var MVISTimeBoundsSchema = zod.z.object({
342
+ /** Earliest valid time (RFC 3339) */
343
+ not_before: zod.z.string().datetime(),
344
+ /** Latest valid time (RFC 3339) */
345
+ not_after: zod.z.string().datetime()
346
+ }).strict();
347
+ var MVISReplayProtectionSchema = zod.z.object({
348
+ /** Unique token identifier (jti from JWT or equivalent) */
349
+ jti: zod.z.string().min(1).max(256),
350
+ /** Optional nonce for additional replay protection */
351
+ nonce: zod.z.string().max(256).optional()
352
+ }).strict();
353
+ zod.z.object({
354
+ /** Who issued the identity assertion */
355
+ issuer: zod.z.string().min(1).max(2048),
356
+ /** Who the identity is about (opaque identifier, no PII) */
357
+ subject: zod.z.string().min(1).max(256),
358
+ /** Cryptographic binding: kid or JWK thumbprint */
359
+ key_binding: zod.z.string().min(1).max(256),
360
+ /** Validity period */
361
+ time_bounds: MVISTimeBoundsSchema,
362
+ /** Replay protection */
363
+ replay_protection: MVISReplayProtectionSchema
364
+ }).strict();
365
+
366
+ // src/extensions/fingerprint-ref.ts
367
+ function hexToBase64url(hex) {
368
+ const bytes = new Uint8Array(hex.length / 2);
369
+ for (let i = 0; i < hex.length; i += 2) {
370
+ bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
371
+ }
372
+ let base64;
373
+ if (typeof Buffer !== "undefined") {
374
+ base64 = Buffer.from(bytes).toString("base64");
375
+ } else {
376
+ base64 = btoa(String.fromCharCode(...bytes));
377
+ }
378
+ return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
379
+ }
380
+ var STRING_FORM_PATTERN = /^(sha256|hmac-sha256):([a-f0-9]{64})$/;
381
+ var MAX_FINGERPRINT_REF_LENGTH = 76;
382
+ function stringToFingerprintRef(s) {
383
+ if (s.length > MAX_FINGERPRINT_REF_LENGTH) {
384
+ return null;
385
+ }
386
+ const match = STRING_FORM_PATTERN.exec(s);
387
+ if (!match) {
388
+ return null;
389
+ }
390
+ const alg = match[1];
391
+ const hex = match[2];
392
+ return {
393
+ alg,
394
+ value: hexToBase64url(hex)
395
+ };
396
+ }
397
+
398
+ // src/wire-02-representation.ts
399
+ function isValidContentHash(s) {
400
+ const ref = stringToFingerprintRef(s);
401
+ if (ref === null) return false;
402
+ return ref.alg === "sha256";
403
+ }
404
+ var MIME_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9!#$&\-^_.+]*\/[a-zA-Z0-9][a-zA-Z0-9!#$&\-^_.+]*(;\s*[a-zA-Z0-9][a-zA-Z0-9!#$&\-^_.+]*=[^\s;]+)*$/;
405
+ function isValidMimeType(s) {
406
+ return MIME_PATTERN.test(s);
407
+ }
408
+ var REPRESENTATION_LIMITS = {
409
+ /** Max content_hash string length (sha256:<64 hex> = 71 chars, capped at FingerprintRef max) */
410
+ maxContentHashLength: MAX_FINGERPRINT_REF_LENGTH,
411
+ /** Max content_type string length */
412
+ maxContentTypeLength: 256
413
+ };
414
+ var Wire02RepresentationFieldsSchema = zod.z.object({
415
+ /**
416
+ * FingerprintRef of the served content body.
417
+ * Format: sha256:<64 lowercase hex>
418
+ * hmac-sha256 is NOT permitted for representation hashes.
419
+ */
420
+ content_hash: zod.z.string().max(REPRESENTATION_LIMITS.maxContentHashLength).refine(isValidContentHash, {
421
+ message: "content_hash must be a valid sha256 FingerprintRef (sha256:<64 lowercase hex>)"
422
+ }).optional(),
423
+ /**
424
+ * MIME type of the served content (e.g., 'text/plain', 'application/json').
425
+ * Conservative pattern validation: type/subtype with optional parameters.
426
+ */
427
+ content_type: zod.z.string().max(REPRESENTATION_LIMITS.maxContentTypeLength).refine(isValidMimeType, {
428
+ message: "content_type must be a valid MIME type (type/subtype with optional parameters)"
429
+ }).optional(),
430
+ /**
431
+ * Size of the served content in bytes.
432
+ * Non-negative integer, bounded by Number.MAX_SAFE_INTEGER.
433
+ */
434
+ content_length: zod.z.number().int().finite().nonnegative().max(Number.MAX_SAFE_INTEGER).optional()
435
+ }).strict();
436
+ var EXTENSION_LIMITS = {
437
+ // Extension key grammar
438
+ maxExtensionKeyLength: 512,
439
+ maxDnsLabelLength: 63,
440
+ maxDnsDomainLength: 253,
441
+ // Commerce
442
+ maxPaymentRailLength: 128,
443
+ maxCurrencyLength: 16,
444
+ maxAmountMinorLength: 64,
445
+ maxReferenceLength: 256,
446
+ maxAssetLength: 256,
447
+ // Access
448
+ maxResourceLength: 2048,
449
+ maxActionLength: 256,
450
+ // Challenge
451
+ maxProblemTypeLength: 2048,
452
+ maxProblemTitleLength: 256,
453
+ maxProblemDetailLength: 4096,
454
+ maxProblemInstanceLength: 2048,
455
+ // Identity
456
+ maxProofRefLength: 256,
457
+ // Correlation
458
+ maxTraceIdLength: 32,
459
+ maxSpanIdLength: 16,
460
+ maxWorkflowIdLength: 256,
461
+ maxParentJtiLength: 256,
462
+ maxDependsOnLength: 64
463
+ };
464
+ var DNS_LABEL = /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
465
+ var SEGMENT_PATTERN = /^[a-z0-9][a-z0-9_-]*$/;
466
+ function isValidExtensionKey(key) {
467
+ if (key.length === 0 || key.length > EXTENSION_LIMITS.maxExtensionKeyLength) return false;
468
+ const slashIdx = key.indexOf("/");
469
+ if (slashIdx <= 0) return false;
470
+ const domain = key.slice(0, slashIdx);
471
+ const segment = key.slice(slashIdx + 1);
472
+ if (!domain.includes(".")) return false;
473
+ if (domain.length > EXTENSION_LIMITS.maxDnsDomainLength) return false;
474
+ if (segment.length === 0) return false;
475
+ if (!SEGMENT_PATTERN.test(segment)) return false;
476
+ const labels = domain.split(".");
477
+ for (const label of labels) {
478
+ if (label.length === 0 || label.length > EXTENSION_LIMITS.maxDnsLabelLength) return false;
479
+ if (!DNS_LABEL.test(label)) return false;
480
+ }
481
+ return true;
482
+ }
483
+ var COMMERCE_EXTENSION_KEY = "org.peacprotocol/commerce";
484
+ var ACCESS_EXTENSION_KEY = "org.peacprotocol/access";
485
+ var CHALLENGE_EXTENSION_KEY = "org.peacprotocol/challenge";
486
+ var IDENTITY_EXTENSION_KEY = "org.peacprotocol/identity";
487
+ var CORRELATION_EXTENSION_KEY = "org.peacprotocol/correlation";
488
+ var AMOUNT_MINOR_PATTERN = /^-?[0-9]+$/;
489
+ var CommerceExtensionSchema = zod.z.object({
490
+ /** Payment rail identifier (e.g., 'stripe', 'x402', 'lightning') */
491
+ payment_rail: zod.z.string().min(1).max(EXTENSION_LIMITS.maxPaymentRailLength),
492
+ /**
493
+ * Amount in smallest currency unit as a string for arbitrary precision.
494
+ * Base-10 integer: optional leading minus, one or more digits.
495
+ * Decimals and empty strings are rejected.
496
+ */
497
+ amount_minor: zod.z.string().min(1).max(EXTENSION_LIMITS.maxAmountMinorLength).regex(
498
+ AMOUNT_MINOR_PATTERN,
499
+ 'amount_minor must be a base-10 integer string (e.g., "1000", "-50")'
500
+ ),
501
+ /** ISO 4217 currency code or asset identifier */
502
+ currency: zod.z.string().min(1).max(EXTENSION_LIMITS.maxCurrencyLength),
503
+ /** Caller-assigned payment reference */
504
+ reference: zod.z.string().max(EXTENSION_LIMITS.maxReferenceLength).optional(),
505
+ /** Asset identifier for non-fiat (e.g., token address) */
506
+ asset: zod.z.string().max(EXTENSION_LIMITS.maxAssetLength).optional(),
507
+ /** Environment discriminant */
508
+ env: zod.z.enum(["live", "test"]).optional()
509
+ }).strict();
510
+ var AccessExtensionSchema = zod.z.object({
511
+ /** Resource being accessed (URI or identifier) */
512
+ resource: zod.z.string().min(1).max(EXTENSION_LIMITS.maxResourceLength),
513
+ /** Action performed on the resource */
514
+ action: zod.z.string().min(1).max(EXTENSION_LIMITS.maxActionLength),
515
+ /** Access decision */
516
+ decision: zod.z.enum(["allow", "deny", "review"])
517
+ }).strict();
518
+ var CHALLENGE_TYPES = [
519
+ "payment_required",
520
+ "identity_required",
521
+ "consent_required",
522
+ "attestation_required",
523
+ "rate_limited",
524
+ "purpose_disallowed",
525
+ "custom"
526
+ ];
527
+ var ChallengeTypeSchema = zod.z.enum(CHALLENGE_TYPES);
528
+ var ProblemDetailsSchema = zod.z.object({
529
+ /** HTTP status code (100-599) */
530
+ status: zod.z.number().int().min(100).max(599),
531
+ /** Problem type URI */
532
+ type: zod.z.string().min(1).max(EXTENSION_LIMITS.maxProblemTypeLength).url(),
533
+ /** Short human-readable summary */
534
+ title: zod.z.string().max(EXTENSION_LIMITS.maxProblemTitleLength).optional(),
535
+ /** Human-readable explanation specific to this occurrence */
536
+ detail: zod.z.string().max(EXTENSION_LIMITS.maxProblemDetailLength).optional(),
537
+ /** URI reference identifying the specific occurrence */
538
+ instance: zod.z.string().max(EXTENSION_LIMITS.maxProblemInstanceLength).optional()
539
+ }).passthrough();
540
+ var ChallengeExtensionSchema = zod.z.object({
541
+ /** Challenge type (7 values) */
542
+ challenge_type: ChallengeTypeSchema,
543
+ /** RFC 9457 Problem Details */
544
+ problem: ProblemDetailsSchema,
545
+ /** Resource that triggered the challenge */
546
+ resource: zod.z.string().max(EXTENSION_LIMITS.maxResourceLength).optional(),
547
+ /** Action that triggered the challenge */
548
+ action: zod.z.string().max(EXTENSION_LIMITS.maxActionLength).optional(),
549
+ /** Caller-defined requirements for resolving the challenge */
550
+ requirements: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
551
+ }).strict();
552
+ var IdentityExtensionSchema = zod.z.object({
553
+ /** Proof reference (opaque string; no actor_binding: top-level actor is sole location) */
554
+ proof_ref: zod.z.string().max(EXTENSION_LIMITS.maxProofRefLength).optional()
555
+ }).strict();
556
+ var TRACE_ID_PATTERN = /^[0-9a-f]{32}$/;
557
+ var SPAN_ID_PATTERN = /^[0-9a-f]{16}$/;
558
+ var CorrelationExtensionSchema = zod.z.object({
559
+ /** OpenTelemetry-compatible trace ID (32 lowercase hex chars) */
560
+ trace_id: zod.z.string().length(EXTENSION_LIMITS.maxTraceIdLength).regex(TRACE_ID_PATTERN, "trace_id must be 32 lowercase hex characters").optional(),
561
+ /** OpenTelemetry-compatible span ID (16 lowercase hex chars) */
562
+ span_id: zod.z.string().length(EXTENSION_LIMITS.maxSpanIdLength).regex(SPAN_ID_PATTERN, "span_id must be 16 lowercase hex characters").optional(),
563
+ /** Workflow identifier */
564
+ workflow_id: zod.z.string().min(1).max(EXTENSION_LIMITS.maxWorkflowIdLength).optional(),
565
+ /** Parent receipt JTI for causal chains */
566
+ parent_jti: zod.z.string().min(1).max(EXTENSION_LIMITS.maxParentJtiLength).optional(),
567
+ /** JTIs this receipt depends on */
568
+ depends_on: zod.z.array(zod.z.string().min(1).max(EXTENSION_LIMITS.maxParentJtiLength)).max(EXTENSION_LIMITS.maxDependsOnLength).optional()
569
+ }).strict();
570
+ var EXTENSION_SCHEMA_MAP = /* @__PURE__ */ new Map();
571
+ EXTENSION_SCHEMA_MAP.set(COMMERCE_EXTENSION_KEY, CommerceExtensionSchema);
572
+ EXTENSION_SCHEMA_MAP.set(ACCESS_EXTENSION_KEY, AccessExtensionSchema);
573
+ EXTENSION_SCHEMA_MAP.set(CHALLENGE_EXTENSION_KEY, ChallengeExtensionSchema);
574
+ EXTENSION_SCHEMA_MAP.set(IDENTITY_EXTENSION_KEY, IdentityExtensionSchema);
575
+ EXTENSION_SCHEMA_MAP.set(CORRELATION_EXTENSION_KEY, CorrelationExtensionSchema);
576
+ function validateKnownExtensions(extensions, ctx) {
577
+ if (extensions === void 0) return;
578
+ for (const key of Object.keys(extensions)) {
579
+ if (!isValidExtensionKey(key)) {
580
+ ctx.addIssue({
581
+ code: "custom",
582
+ message: ERROR_CODES.E_INVALID_EXTENSION_KEY,
583
+ path: ["extensions", key]
584
+ });
585
+ continue;
586
+ }
587
+ const schema = EXTENSION_SCHEMA_MAP.get(key);
588
+ if (schema !== void 0) {
589
+ const result = schema.safeParse(extensions[key]);
590
+ if (!result.success) {
591
+ const firstIssue = result.error.issues[0];
592
+ const issuePath = firstIssue?.path ?? [];
593
+ ctx.addIssue({
594
+ code: "custom",
595
+ message: firstIssue?.message ?? "Invalid extension value",
596
+ path: ["extensions", key, ...issuePath]
597
+ });
598
+ }
599
+ }
600
+ }
601
+ }
602
+
603
+ // src/wire-02-envelope.ts
604
+ function isSortedAndUnique(arr) {
605
+ for (let i = 1; i < arr.length; i++) {
606
+ if (arr[i] <= arr[i - 1]) return false;
607
+ }
608
+ return true;
609
+ }
610
+ function isCanonicalIss(iss) {
611
+ if (typeof iss !== "string" || iss.length === 0 || iss.length > kernel.ISS_CANONICAL.maxLength) {
612
+ return false;
613
+ }
614
+ if (iss.startsWith("did:")) {
615
+ return /^did:[a-z0-9]+:[^#?/]+$/.test(iss);
616
+ }
617
+ try {
618
+ const url = new URL(iss);
619
+ if (url.protocol !== "https:") return false;
620
+ if (!url.hostname) return false;
621
+ if (url.username !== "" || url.password !== "") return false;
622
+ const origin = `${url.protocol}//${url.host}`;
623
+ return iss === origin;
624
+ } catch {
625
+ return false;
626
+ }
627
+ }
628
+ var ABS_URI_PATTERN = /^[a-z][a-z0-9+.-]*:\/\//;
629
+ function isValidReceiptType(value) {
630
+ if (value.length === 0 || value.length > kernel.TYPE_GRAMMAR.maxLength) return false;
631
+ if (ABS_URI_PATTERN.test(value)) return true;
632
+ const slashIdx = value.indexOf("/");
633
+ if (slashIdx <= 0) return false;
634
+ const domain = value.slice(0, slashIdx);
635
+ const segment = value.slice(slashIdx + 1);
636
+ if (!domain.includes(".")) return false;
637
+ if (segment.length === 0) return false;
638
+ if (!/^[a-zA-Z0-9][a-zA-Z0-9.-]*$/.test(domain)) return false;
639
+ if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(segment)) return false;
640
+ return true;
641
+ }
642
+ var EVIDENCE_PILLARS = [
643
+ "access",
644
+ "attribution",
645
+ "commerce",
646
+ "compliance",
647
+ "consent",
648
+ "identity",
649
+ "privacy",
650
+ "provenance",
651
+ "purpose",
652
+ "safety"
653
+ ];
654
+ var EvidencePillarSchema = zod.z.enum(
655
+ EVIDENCE_PILLARS
656
+ );
657
+ var PillarsSchema = zod.z.array(EvidencePillarSchema).min(1).superRefine((arr, ctx) => {
658
+ if (!isSortedAndUnique(arr)) {
659
+ ctx.addIssue({
660
+ code: "custom",
661
+ message: "E_PILLARS_NOT_SORTED"
662
+ });
663
+ }
664
+ });
665
+ var Wire02KindSchema = zod.z.enum(["evidence", "challenge"]);
666
+ var ReceiptTypeSchema = zod.z.string().max(kernel.TYPE_GRAMMAR.maxLength).refine(isValidReceiptType, {
667
+ message: "type must be reverse-DNS notation (e.g., org.example/flow) or an absolute URI"
668
+ });
669
+ var CanonicalIssSchema = zod.z.string().max(kernel.ISS_CANONICAL.maxLength).refine(isCanonicalIss, {
670
+ message: "E_ISS_NOT_CANONICAL"
671
+ });
672
+ var PolicyBlockSchema = zod.z.object({
673
+ /** JCS+SHA-256 digest: 'sha256:<64 lowercase hex>' */
674
+ digest: zod.z.string().regex(kernel.HASH.pattern, "digest must be sha256:<64 lowercase hex>"),
675
+ /**
676
+ * HTTPS locator hint for the policy document.
677
+ * MUST be an https:// URL (max 2048 chars).
678
+ * MUST NOT trigger auto-fetch; callers use this as a hint only (DD-55).
679
+ */
680
+ uri: zod.z.string().max(kernel.POLICY_BLOCK.uriMaxLength).url().refine((u) => u.startsWith("https://"), "policy.uri must be an https:// URL").optional(),
681
+ /** Caller-assigned version label (max 256 chars) */
682
+ version: zod.z.string().max(kernel.POLICY_BLOCK.versionMaxLength).optional()
683
+ });
684
+ var Wire02ClaimsSchema = zod.z.object({
685
+ /** Wire format version discriminant; always '0.2' for Wire 0.2 */
686
+ peac_version: zod.z.literal("0.2"),
687
+ /** Structural kind: 'evidence' or 'challenge' */
688
+ kind: Wire02KindSchema,
689
+ /** Open semantic type (reverse-DNS or absolute URI) */
690
+ type: ReceiptTypeSchema,
691
+ /** Canonical issuer (https:// ASCII origin or did: identifier) */
692
+ iss: CanonicalIssSchema,
693
+ /** Issued-at time (Unix seconds). REQUIRED. */
694
+ iat: zod.z.number().int(),
695
+ /** Unique receipt identifier; 1 to 256 chars */
696
+ jti: zod.z.string().min(1).max(256),
697
+ /** Subject identifier; max 2048 chars */
698
+ sub: zod.z.string().max(2048).optional(),
699
+ /** Evidence pillars (closed 10-value taxonomy); sorted ascending, unique */
700
+ pillars: PillarsSchema.optional(),
701
+ /** Top-level actor binding (sole location for ActorBinding in Wire 0.2) */
702
+ actor: ActorBindingSchema.optional(),
703
+ /** Policy binding block (DD-151) */
704
+ policy: PolicyBlockSchema.optional(),
705
+ /** Representation fields (DD-152): FingerprintRef validation, sha256-only, strict */
706
+ representation: Wire02RepresentationFieldsSchema.optional(),
707
+ /** ISO 8601 / RFC 3339 timestamp when the interaction occurred; evidence kind only */
708
+ occurred_at: zod.z.string().datetime({ offset: true }).optional(),
709
+ /** Declared purpose string; max 256 chars */
710
+ purpose_declared: zod.z.string().max(256).optional(),
711
+ /** Extension groups (open; known group keys validated by group schema) */
712
+ extensions: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
713
+ }).superRefine((data, ctx) => {
714
+ if (data.kind === "challenge" && data.occurred_at !== void 0) {
715
+ ctx.addIssue({
716
+ code: "custom",
717
+ message: "E_OCCURRED_AT_ON_CHALLENGE"
718
+ });
719
+ }
720
+ validateKnownExtensions(data.extensions, ctx);
721
+ }).strict();
277
722
 
278
723
  // src/receipt-parser.ts
279
- function classifyReceipt(obj) {
724
+ function detectWireVersion(obj) {
725
+ if (obj === null || obj === void 0 || typeof obj !== "object" || Array.isArray(obj)) {
726
+ return null;
727
+ }
728
+ const record = obj;
729
+ if (record.peac_version === "0.2") return "0.2";
730
+ if ("peac_version" in record) return null;
731
+ return "0.1";
732
+ }
733
+ function classifyWire01Receipt(obj) {
280
734
  if ("amt" in obj || "cur" in obj || "payment" in obj) {
281
735
  return "commerce";
282
736
  }
283
737
  return "attestation";
284
738
  }
285
- function parseReceiptClaims(input, _opts) {
739
+ function parseReceiptClaims(input, opts) {
286
740
  if (input === null || input === void 0 || typeof input !== "object" || Array.isArray(input)) {
287
741
  return {
288
742
  ok: false,
@@ -293,7 +747,37 @@ function parseReceiptClaims(input, _opts) {
293
747
  };
294
748
  }
295
749
  const obj = input;
296
- const variant = classifyReceipt(obj);
750
+ const wireVersion = opts?.wireVersion === "0.2" || opts?.wireVersion === "0.1" ? opts.wireVersion : detectWireVersion(obj);
751
+ if (wireVersion === null) {
752
+ return {
753
+ ok: false,
754
+ error: {
755
+ code: "E_UNSUPPORTED_WIRE_VERSION",
756
+ message: `Unsupported or unrecognized peac_version: ${JSON.stringify(obj["peac_version"])}`
757
+ }
758
+ };
759
+ }
760
+ if (wireVersion === "0.2") {
761
+ const result2 = Wire02ClaimsSchema.safeParse(obj);
762
+ if (!result2.success) {
763
+ return {
764
+ ok: false,
765
+ error: {
766
+ code: "E_INVALID_FORMAT",
767
+ message: `Wire 0.2 receipt validation failed: ${result2.error.issues.map((i) => i.message).join("; ")}`,
768
+ issues: result2.error.issues
769
+ }
770
+ };
771
+ }
772
+ return {
773
+ ok: true,
774
+ variant: "wire-02",
775
+ wireVersion: "0.2",
776
+ warnings: [],
777
+ claims: result2.data
778
+ };
779
+ }
780
+ const variant = classifyWire01Receipt(obj);
297
781
  if (variant === "commerce") {
298
782
  const result2 = ReceiptClaimsSchema.safeParse(obj);
299
783
  if (!result2.success) {
@@ -309,6 +793,8 @@ function parseReceiptClaims(input, _opts) {
309
793
  return {
310
794
  ok: true,
311
795
  variant: "commerce",
796
+ wireVersion: "0.1",
797
+ warnings: [],
312
798
  claims: result2.data
313
799
  };
314
800
  }
@@ -326,10 +812,13 @@ function parseReceiptClaims(input, _opts) {
326
812
  return {
327
813
  ok: true,
328
814
  variant: "attestation",
815
+ wireVersion: "0.1",
816
+ warnings: [],
329
817
  claims: result.data
330
818
  };
331
819
  }
332
820
 
821
+ exports.detectWireVersion = detectWireVersion;
333
822
  exports.parseReceiptClaims = parseReceiptClaims;
334
823
  //# sourceMappingURL=receipt-parser.cjs.map
335
824
  //# sourceMappingURL=receipt-parser.cjs.map