@tearleads/client-sdk 0.1.11 → 0.1.13

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 (141) hide show
  1. package/README.md +18 -2
  2. package/client/Tearleads.d.ts +4 -0
  3. package/client/Tearleads.js +5 -0
  4. package/client/Tearleads.js.map +1 -1
  5. package/client/index.d.ts +1 -0
  6. package/client/index.js.map +1 -1
  7. package/client/principalHistoryProtection.d.ts +19 -0
  8. package/client/principalHistoryProtection.js +76 -0
  9. package/client/principalHistoryProtection.js.map +1 -0
  10. package/client/workflowRuntime.d.ts +5 -0
  11. package/client/workflowRuntime.js +34 -5
  12. package/client/workflowRuntime.js.map +1 -1
  13. package/data/containers/shared/authorAccess.d.ts +2 -2
  14. package/data/containers/shared/authorAccess.js.map +1 -1
  15. package/data/containers/shared/heldContainerHeads.js.map +1 -1
  16. package/data/containers/shared/principalPolicies.js.map +1 -1
  17. package/data/containers/shared/types.d.ts +3 -2
  18. package/data/containers/shared/types.js.map +1 -1
  19. package/data/keyingProjectionVerification/accessManifestCheckpointEnforcement.js.map +1 -1
  20. package/data/keyingProjectionVerification/checkpointContext.d.ts +4 -3
  21. package/data/keyingProjectionVerification/checkpointContext.js.map +1 -1
  22. package/data/keyingProjectionVerification/containerManifestVerification.js.map +1 -1
  23. package/data/keyingProjectionVerification/containerPathVerification.js.map +1 -1
  24. package/data/keyingProjectionVerification/containerProjectionVerification.d.ts +4 -3
  25. package/data/keyingProjectionVerification/containerProjectionVerification.js.map +1 -1
  26. package/data/keyingProjectionVerification/documentManifestPolicies.js.map +1 -1
  27. package/data/keyingProjectionVerification/documentManifestVerification.js.map +1 -1
  28. package/data/keyingProjectionVerification/documentProjectionVerification.d.ts +3 -2
  29. package/data/keyingProjectionVerification/documentProjectionVerification.js.map +1 -1
  30. package/data/keyingProjectionVerification/organizationFounderBinding.js +9 -4
  31. package/data/keyingProjectionVerification/organizationFounderBinding.js.map +1 -1
  32. package/data/keyingProjectionVerification/principalPolicyCache.js +3 -4
  33. package/data/keyingProjectionVerification/principalPolicyCache.js.map +1 -1
  34. package/data/keyingProjectionVerification/principalPolicyVerification.js +12 -2
  35. package/data/keyingProjectionVerification/principalPolicyVerification.js.map +1 -1
  36. package/data/keyingProjectionVerification/projectionAuthorizationEvidence.js.map +1 -1
  37. package/data/keyingProjectionVerification/resolvedPrincipalPolicy.js +70 -0
  38. package/data/keyingProjectionVerification/resolvedPrincipalPolicy.js.map +1 -0
  39. package/data/keyingProjectionVerification/types.d.ts +17 -2
  40. package/data/keyingProjectionVerification/types.js +12 -0
  41. package/data/keyingProjectionVerification/types.js.map +1 -1
  42. package/data/persistence/organizationFounderPersistence.js +3 -2
  43. package/data/persistence/organizationFounderPersistence.js.map +1 -1
  44. package/data/persistence/principalHistoryEvidencePersistence.js +91 -0
  45. package/data/persistence/principalHistoryEvidencePersistence.js.map +1 -0
  46. package/data/persistence/principalHistoryPrefixPersistence.js +52 -0
  47. package/data/persistence/principalHistoryPrefixPersistence.js.map +1 -0
  48. package/data/persistence/principalHistoryStagePersistence.js +9 -2
  49. package/data/persistence/principalHistoryStagePersistence.js.map +1 -1
  50. package/data/persistence/principalKeyEnvelopeCandidates.js +67 -0
  51. package/data/persistence/principalKeyEnvelopeCandidates.js.map +1 -0
  52. package/data/principals/principalHistoryPrefixProtection.js +29 -0
  53. package/data/principals/principalHistoryPrefixProtection.js.map +1 -0
  54. package/data/principals/principalHistoryProtection.d.ts +10 -0
  55. package/data/principals/principalHistoryProtection.js +14 -0
  56. package/data/principals/principalHistoryProtection.js.map +1 -0
  57. package/data/principals/principalHistoryStageProtection.js.map +1 -1
  58. package/data/principals/principalPolicyCrypto.js +11 -21
  59. package/data/principals/principalPolicyCrypto.js.map +1 -1
  60. package/data/principals/principalPolicyEvidence.d.ts +4 -0
  61. package/data/principals/principalPolicyEvidence.js +12 -0
  62. package/data/principals/principalPolicyEvidence.js.map +1 -0
  63. package/data/sqlite/principalHistoryEvidenceSchema.js +46 -0
  64. package/data/sqlite/principalHistoryEvidenceSchema.js.map +1 -0
  65. package/data/sqlite/principalKeyFingerprintJson.js +9 -0
  66. package/data/sqlite/principalKeyFingerprintJson.js.map +1 -0
  67. package/data/sqlite/principalPolicySchema.js +4 -0
  68. package/data/sqlite/principalPolicySchema.js.map +1 -1
  69. package/data/sqlite/schema.d.ts +304 -0
  70. package/data/sqlite/schema.js +5 -0
  71. package/data/sqlite/schema.js.map +1 -1
  72. package/data/sqlite/sqlTableSchema.js +1 -1
  73. package/data/sqlite/sqlTableSchema.js.map +1 -1
  74. package/index.d.ts +3 -3
  75. package/index.js +1 -1
  76. package/index.js.map +1 -1
  77. package/internal/api-client/principalPolicyPages.d.ts +2 -0
  78. package/internal/api-client/principalPolicyPages.js +11 -2
  79. package/internal/api-client/principalPolicyPages.js.map +1 -1
  80. package/internal/crypto/index.d.ts +1 -1
  81. package/internal/crypto/index.js +1 -1
  82. package/internal/crypto/index.js.map +1 -1
  83. package/internal/crypto/keying/index.d.ts +1 -1
  84. package/internal/crypto/keying/index.js +1 -1
  85. package/internal/crypto/keying/index.js.map +1 -1
  86. package/internal/crypto/keying/principalPolicyHistoryIndex.d.ts +6 -2
  87. package/internal/crypto/keying/principalPolicyHistoryIndex.js +11 -5
  88. package/internal/crypto/keying/principalPolicyHistoryIndex.js.map +1 -1
  89. package/internal/crypto/keying/principalPolicyHistoryReferences.d.ts +2 -0
  90. package/internal/crypto/keying/principalPolicyHistoryReferences.js +19 -2
  91. package/internal/crypto/keying/principalPolicyHistoryReferences.js.map +1 -1
  92. package/package.json +1 -1
  93. package/workflows/containers/child/create.js.map +1 -1
  94. package/workflows/containers/child/move.js.map +1 -1
  95. package/workflows/containers/child/moveWraps.js.map +1 -1
  96. package/workflows/containers/child/mutationRequestCore.js.map +1 -1
  97. package/workflows/containers/child/recitePlan.js.map +1 -1
  98. package/workflows/containers/child/rekey.d.ts +3 -2
  99. package/workflows/containers/child/rekey.js.map +1 -1
  100. package/workflows/containers/child/revoke.d.ts +2 -2
  101. package/workflows/containers/child/revoke.js.map +1 -1
  102. package/workflows/containers/child/rotationPrincipalPolicies.js.map +1 -1
  103. package/workflows/containers/child/rotationWraps.js.map +1 -1
  104. package/workflows/containers/child/sharePlanCore.d.ts +11 -10
  105. package/workflows/containers/child/sharePlanCore.js.map +1 -1
  106. package/workflows/documents/linkSetAuthority.js.map +1 -1
  107. package/workflows/principals/index.d.ts +1 -0
  108. package/workflows/principals/index.js +1 -0
  109. package/workflows/principals/index.js.map +1 -1
  110. package/workflows/principals/principalHistoryRecoveryPrefix.js +65 -0
  111. package/workflows/principals/principalHistoryRecoveryPrefix.js.map +1 -0
  112. package/workflows/principals/principalHistoryRecoveryReferences.js +60 -0
  113. package/workflows/principals/principalHistoryRecoveryReferences.js.map +1 -0
  114. package/workflows/principals/principalHistoryRecoveryStage.js +38 -2
  115. package/workflows/principals/principalHistoryRecoveryStage.js.map +1 -1
  116. package/workflows/principals/principalHistoryRecoveryTypes.d.ts +9 -1
  117. package/workflows/principals/principalHistoryRecoveryTypes.js +7 -0
  118. package/workflows/principals/principalHistoryRecoveryTypes.js.map +1 -1
  119. package/workflows/principals/principalHistoryRecoveryVerification.js +24 -0
  120. package/workflows/principals/principalHistoryRecoveryVerification.js.map +1 -0
  121. package/workflows/principals/principalRecoveryDirectory.d.ts +10 -0
  122. package/workflows/principals/principalRecoveryDirectory.js +107 -0
  123. package/workflows/principals/principalRecoveryDirectory.js.map +1 -0
  124. package/workflows/principals/principalRecoveryMemo.js +46 -0
  125. package/workflows/principals/principalRecoveryMemo.js.map +1 -0
  126. package/workflows/principals/principalRecoveryOutage.js +40 -0
  127. package/workflows/principals/principalRecoveryOutage.js.map +1 -0
  128. package/workflows/principals/principalRecoveryQueue.js +22 -0
  129. package/workflows/principals/principalRecoveryQueue.js.map +1 -0
  130. package/workflows/principals/recoverPrincipalPolicyHistory.d.ts +1 -0
  131. package/workflows/principals/recoverPrincipalPolicyHistory.js +108 -36
  132. package/workflows/principals/recoverPrincipalPolicyHistory.js.map +1 -1
  133. package/workflows/principals/recoverScopedPrincipalPolicyHistory.d.ts +13 -0
  134. package/workflows/principals/recoverScopedPrincipalPolicyHistory.js +150 -0
  135. package/workflows/principals/recoverScopedPrincipalPolicyHistory.js.map +1 -0
  136. package/workflows/principals/runtimePolicyRecovery.js +65 -0
  137. package/workflows/principals/runtimePolicyRecovery.js.map +1 -0
  138. package/workflows/principals/runtimePolicyWarmer.js +5 -1
  139. package/workflows/principals/runtimePolicyWarmer.js.map +1 -1
  140. package/workflows/sync/remoteResetPrincipalScope.js +10 -0
  141. package/workflows/sync/remoteResetPrincipalScope.js.map +1 -1
@@ -1,6 +1,6 @@
1
1
  import { unwrapDek } from "../../internal/crypto/index.js";
2
2
  import { base64ToBytes } from "../../internal/encoding/index.js";
3
- import { loadAllPrincipalPolicyBundles } from "../persistence/principalPolicyPersistence.js";
3
+ import { loadPrincipalKeyEnvelopeCandidates, } from "../persistence/principalKeyEnvelopeCandidates.js";
4
4
  function toKeyEnvelopeEntries(envelopes) {
5
5
  return envelopes.map((envelope) => ({
6
6
  keyFingerprint: envelope.keyFingerprint,
@@ -18,8 +18,8 @@ function toMemberEnvelopeEntries(envelopes) {
18
18
  function principalBundleKey(bundle) {
19
19
  return `${bundle.principalType}:${bundle.principalId}`;
20
20
  }
21
- async function createPrincipalPolicyResolutionContext(execSql) {
22
- const bundles = await loadAllPrincipalPolicyBundles(execSql);
21
+ async function createPrincipalPolicyResolutionContext(execSql, fingerprints) {
22
+ const bundles = await loadPrincipalKeyEnvelopeCandidates(execSql, fingerprints);
23
23
  const bundlesByKeyFingerprint = new Map();
24
24
  for (const bundle of bundles) {
25
25
  const matchingBundles = bundlesByKeyFingerprint.get(bundle.currentState.keyFingerprint) ?? [];
@@ -28,21 +28,13 @@ async function createPrincipalPolicyResolutionContext(execSql) {
28
28
  }
29
29
  return {
30
30
  bundlesByKeyFingerprint,
31
- resolvedPrincipalSecretKeys: new Map(),
32
31
  };
33
32
  }
34
- async function unwrapPrincipalSecretKey(bundle, secretKey, context) {
33
+ async function unwrapPrincipalSecretKey(bundle, secretKey) {
35
34
  const principalKey = principalBundleKey(bundle.currentState);
36
- const policyEpochKey = `${principalKey}:${bundle.currentState.stateHash}`;
37
- const cachedSecretKey = context.resolvedPrincipalSecretKeys.get(policyEpochKey) ?? null;
38
- if (cachedSecretKey) {
39
- return cachedSecretKey;
40
- }
41
35
  const memberEnvelopeEntries = toMemberEnvelopeEntries(bundle.currentMemberEnvelopes.envelopes);
42
36
  try {
43
- const resolvedSecretKey = await unwrapDek(memberEnvelopeEntries, secretKey);
44
- context.resolvedPrincipalSecretKeys.set(policyEpochKey, resolvedSecretKey);
45
- return resolvedSecretKey;
37
+ return await unwrapDek(memberEnvelopeEntries, secretKey);
46
38
  }
47
39
  catch {
48
40
  // No transitive fallback: a principal's envelopes are all addressed to
@@ -61,18 +53,16 @@ export async function unwrapKeyEnvelopesWithPrincipalPolicies(input) {
61
53
  throw new Error("No matching key envelope entry found for this secret key and no principal policy cache is available");
62
54
  }
63
55
  }
64
- const context = await createPrincipalPolicyResolutionContext(input.execSql);
65
- const attemptedPrincipalKeys = new Set();
56
+ const context = await createPrincipalPolicyResolutionContext(input.execSql, input.envelopes.map((envelope) => envelope.keyFingerprint));
57
+ const attemptedFingerprints = new Set();
66
58
  for (const envelope of input.envelopes) {
59
+ if (attemptedFingerprints.has(envelope.keyFingerprint))
60
+ continue;
61
+ attemptedFingerprints.add(envelope.keyFingerprint);
67
62
  const candidateBundles = context.bundlesByKeyFingerprint.get(envelope.keyFingerprint) ?? [];
68
63
  for (const bundle of candidateBundles) {
69
- const principalKey = principalBundleKey(bundle.currentState);
70
- if (attemptedPrincipalKeys.has(principalKey)) {
71
- continue;
72
- }
73
- attemptedPrincipalKeys.add(principalKey);
74
64
  try {
75
- const principalSecretKey = await unwrapPrincipalSecretKey(bundle, input.secretKey, context);
65
+ const principalSecretKey = await unwrapPrincipalSecretKey(bundle, input.secretKey);
76
66
  return await unwrapDek(keyEntries, principalSecretKey);
77
67
  }
78
68
  catch { }
@@ -1 +1 @@
1
- {"version":3,"file":"principalPolicyCrypto.js","sourceRoot":"","sources":["principalPolicyCrypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAMpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,2CAA2C,CAAC;AAW1F,SAAS,oBAAoB,CAC3B,SAA+C;IAE/C,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAClC,cAAc,EAAE,QAAQ,CAAC,cAAc;QACvC,aAAa,EAAE,aAAa,CAAC,QAAQ,CAAC,aAAa,CAAC;QACpD,UAAU,EAAE,aAAa,CAAC,QAAQ,CAAC,UAAU,CAAC;KAC/C,CAAC,CAAC,CAAC;AACN,CAAC;AAED,SAAS,uBAAuB,CAC9B,SAAyD;IAEzD,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAClC,cAAc,EAAE,QAAQ,CAAC,oBAAoB;QAC7C,aAAa,EAAE,aAAa,CAAC,QAAQ,CAAC,aAAa,CAAC;QACpD,UAAU,EAAE,aAAa,CAAC,QAAQ,CAAC,UAAU,CAAC;KAC/C,CAAC,CAAC,CAAC;AACN,CAAC;AAED,SAAS,kBAAkB,CACzB,MAGC;IAED,OAAO,GAAG,MAAM,CAAC,aAAa,IAAI,MAAM,CAAC,WAAW,EAAE,CAAC;AACzD,CAAC;AAED,KAAK,UAAU,sCAAsC,CACnD,OAAgB;IAEhB,MAAM,OAAO,GAAG,MAAM,6BAA6B,CAAC,OAAO,CAAC,CAAC;IAC7D,MAAM,uBAAuB,GAAG,IAAI,GAAG,EAGpC,CAAC;IAEJ,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,MAAM,eAAe,GAC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KAAK,CAAC,SAAS,CAAC,CAAC;IAEzD,IAAI,CAAC;QACH,OAAO,MAAM,SAAS,CAAC,UAAU,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;IACtD,CAAC;IAAC,MAAM,CAAC;QACP,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CACb,qGAAqG,CACtG,CAAC;QACJ,CAAC;IACH,CAAC;IAED,MAAM,OAAO,GAAG,MAAM,sCAAsC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC5E,MAAM,sBAAsB,GAAG,IAAI,GAAG,EAAU,CAAC;IAEjD,KAAK,MAAM,QAAQ,IAAI,KAAK,CAAC,SAAS,EAAE,CAAC;QACvC,MAAM,gBAAgB,GACpB,OAAO,CAAC,uBAAuB,CAAC,GAAG,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC;QAErE,KAAK,MAAM,MAAM,IAAI,gBAAgB,EAAE,CAAC;YACtC,MAAM,YAAY,GAAG,kBAAkB,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;YAE7D,IAAI,sBAAsB,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC;gBAC7C,SAAS;YACX,CAAC;YAED,sBAAsB,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;YAEzC,IAAI,CAAC;gBACH,MAAM,kBAAkB,GAAG,MAAM,wBAAwB,CACvD,MAAM,EACN,KAAK,CAAC,SAAS,EACf,OAAO,CACR,CAAC;gBAEF,OAAO,MAAM,SAAS,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC;YACzD,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACZ,CAAC;IACH,CAAC;IAED,MAAM,IAAI,KAAK,CACb,uFAAuF,CACxF,CAAC;AACJ,CAAC","sourcesContent":["import { type RecipientEntry, unwrapDek } from \"@tearleads/crypto\";\nimport { base64ToBytes } from \"@tearleads/encoding\";\nimport type {\n PrincipalMemberEnvelopeResponse,\n PrincipalPolicyBundleResponse,\n} from \"@tearleads/validators/response\";\nimport type { SerializedKeyEnvelope } from \"@tearleads/validators/util\";\nimport { loadAllPrincipalPolicyBundles } from \"../persistence/principalPolicyPersistence\";\nimport type { ExecSql } from \"../sqlite/sqlSchema\";\n\ninterface PrincipalPolicyResolutionContext {\n bundlesByKeyFingerprint: ReadonlyMap<\n string,\n ReadonlyArray<PrincipalPolicyBundleResponse>\n >;\n resolvedPrincipalSecretKeys: Map<string, Uint8Array>;\n}\n\nfunction toKeyEnvelopeEntries(\n envelopes: ReadonlyArray<SerializedKeyEnvelope>,\n): RecipientEntry[] {\n return envelopes.map((envelope) => ({\n keyFingerprint: envelope.keyFingerprint,\n kemCipherText: base64ToBytes(envelope.kemCipherText),\n wrappedKey: base64ToBytes(envelope.wrappedKey),\n }));\n}\n\nfunction toMemberEnvelopeEntries(\n envelopes: ReadonlyArray<PrincipalMemberEnvelopeResponse>,\n): RecipientEntry[] {\n return envelopes.map((envelope) => ({\n keyFingerprint: envelope.memberKeyFingerprint,\n kemCipherText: base64ToBytes(envelope.kemCipherText),\n wrappedKey: base64ToBytes(envelope.wrappedKey),\n }));\n}\n\nfunction principalBundleKey(\n bundle: Pick<\n PrincipalPolicyBundleResponse[\"currentState\"],\n \"principalType\" | \"principalId\"\n >,\n): string {\n return `${bundle.principalType}:${bundle.principalId}`;\n}\n\nasync function createPrincipalPolicyResolutionContext(\n execSql: ExecSql,\n): Promise<PrincipalPolicyResolutionContext> {\n const bundles = await loadAllPrincipalPolicyBundles(execSql);\n const bundlesByKeyFingerprint = new Map<\n string,\n PrincipalPolicyBundleResponse[]\n >();\n\n for (const bundle of bundles) {\n const matchingBundles =\n bundlesByKeyFingerprint.get(bundle.currentState.keyFingerprint) ?? [];\n matchingBundles.push(bundle);\n bundlesByKeyFingerprint.set(\n bundle.currentState.keyFingerprint,\n matchingBundles,\n );\n }\n\n return {\n bundlesByKeyFingerprint,\n resolvedPrincipalSecretKeys: new Map(),\n };\n}\n\nasync function unwrapPrincipalSecretKey(\n bundle: PrincipalPolicyBundleResponse,\n secretKey: Uint8Array,\n context: PrincipalPolicyResolutionContext,\n): Promise<Uint8Array> {\n const principalKey = principalBundleKey(bundle.currentState);\n const policyEpochKey = `${principalKey}:${bundle.currentState.stateHash}`;\n const cachedSecretKey =\n context.resolvedPrincipalSecretKeys.get(policyEpochKey) ?? null;\n\n if (cachedSecretKey) {\n return cachedSecretKey;\n }\n\n const memberEnvelopeEntries = toMemberEnvelopeEntries(\n bundle.currentMemberEnvelopes.envelopes,\n );\n\n try {\n const resolvedSecretKey = await unwrapDek(memberEnvelopeEntries, secretKey);\n context.resolvedPrincipalSecretKeys.set(policyEpochKey, resolvedSecretKey);\n return resolvedSecretKey;\n } catch {\n // No transitive fallback: a principal's envelopes are all addressed to\n // users directly, so if this identity key opens none of them the requester\n // is not a member.\n }\n\n throw new Error(`No matching principal member envelope for ${principalKey}`);\n}\n\nexport async function unwrapKeyEnvelopesWithPrincipalPolicies(input: {\n envelopes: ReadonlyArray<SerializedKeyEnvelope>;\n execSql?: ExecSql | undefined;\n secretKey: Uint8Array;\n}): Promise<Uint8Array> {\n const keyEntries = toKeyEnvelopeEntries(input.envelopes);\n\n try {\n return await unwrapDek(keyEntries, input.secretKey);\n } catch {\n if (!input.execSql) {\n throw new Error(\n \"No matching key envelope entry found for this secret key and no principal policy cache is available\",\n );\n }\n }\n\n const context = await createPrincipalPolicyResolutionContext(input.execSql);\n const attemptedPrincipalKeys = new Set<string>();\n\n for (const envelope of input.envelopes) {\n const candidateBundles =\n context.bundlesByKeyFingerprint.get(envelope.keyFingerprint) ?? [];\n\n for (const bundle of candidateBundles) {\n const principalKey = principalBundleKey(bundle.currentState);\n\n if (attemptedPrincipalKeys.has(principalKey)) {\n continue;\n }\n\n attemptedPrincipalKeys.add(principalKey);\n\n try {\n const principalSecretKey = await unwrapPrincipalSecretKey(\n bundle,\n input.secretKey,\n context,\n );\n\n return await unwrapDek(keyEntries, principalSecretKey);\n } catch {}\n }\n }\n\n throw new Error(\n \"No matching key envelope entry found for this secret key or cached principal policies\",\n );\n}\n"]}
1
+ {"version":3,"file":"principalPolicyCrypto.js","sourceRoot":"","sources":["principalPolicyCrypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,EACL,kCAAkC,GAEnC,MAAM,+CAA+C,CAAC;AAUvD,SAAS,oBAAoB,CAC3B,SAA+C;IAE/C,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAClC,cAAc,EAAE,QAAQ,CAAC,cAAc;QACvC,aAAa,EAAE,aAAa,CAAC,QAAQ,CAAC,aAAa,CAAC;QACpD,UAAU,EAAE,aAAa,CAAC,QAAQ,CAAC,UAAU,CAAC;KAC/C,CAAC,CAAC,CAAC;AACN,CAAC;AAED,SAAS,uBAAuB,CAC9B,SAAyD;IAEzD,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAClC,cAAc,EAAE,QAAQ,CAAC,oBAAoB;QAC7C,aAAa,EAAE,aAAa,CAAC,QAAQ,CAAC,aAAa,CAAC;QACpD,UAAU,EAAE,aAAa,CAAC,QAAQ,CAAC,UAAU,CAAC;KAC/C,CAAC,CAAC,CAAC;AACN,CAAC;AAED,SAAS,kBAAkB,CACzB,MAGC;IAED,OAAO,GAAG,MAAM,CAAC,aAAa,IAAI,MAAM,CAAC,WAAW,EAAE,CAAC;AACzD,CAAC;AAED,KAAK,UAAU,sCAAsC,CACnD,OAAgB,EAChB,YAA+B;IAE/B,MAAM,OAAO,GAAG,MAAM,kCAAkC,CACtD,OAAO,EACP,YAAY,CACb,CAAC;IACF,MAAM,uBAAuB,GAAG,IAAI,GAAG,EAGpC,CAAC;IAEJ,KAAK,MAAM,MAAM,IAAI,OA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QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,CAC3D,CAAC;IACF,MAAM,qBAAqB,GAAG,IAAI,GAAG,EAAU,CAAC;IAEhD,KAAK,MAAM,QAAQ,IAAI,KAAK,CAAC,SAAS,EAAE,CAAC;QACvC,IAAI,qBAAqB,CAAC,GAAG,CAAC,QAAQ,CAAC,cAAc,CAAC;YAAE,SAAS;QACjE,qBAAqB,CAAC,GAAG,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;QACnD,MAAM,gBAAgB,GACpB,OAAO,CAAC,uBAAuB,CAAC,GAAG,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC;QAErE,KAAK,MAAM,MAAM,IAAI,gBAAgB,EAAE,CAAC;YACtC,IAAI,CAAC;gBACH,MAAM,kBAAkB,GAAG,MAAM,wBAAwB,CACvD,MAAM,EACN,KAAK,CAAC,SAAS,CAChB,CAAC;gBAEF,OAAO,MAAM,SAAS,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC;YACzD,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACZ,CAAC;IACH,CAAC;IAED,MAAM,IAAI,KAAK,CACb,uFAAuF,CACxF,CAAC;AACJ,CAAC","sourcesContent":["import { type RecipientEntry, unwrapDek } from \"@tearleads/crypto\";\nimport { base64ToBytes } from \"@tearleads/encoding\";\nimport type { PrincipalMemberEnvelopeResponse } from \"@tearleads/validators/response\";\nimport type { SerializedKeyEnvelope } from \"@tearleads/validators/util\";\nimport {\n loadPrincipalKeyEnvelopeCandidates,\n type PrincipalKeyEnvelopeCandidate,\n} from \"../persistence/principalKeyEnvelopeCandidates\";\nimport type { ExecSql } from \"../sqlite/sqlSchema\";\n\ninterface PrincipalPolicyResolutionContext {\n bundlesByKeyFingerprint: ReadonlyMap<\n string,\n ReadonlyArray<PrincipalKeyEnvelopeCandidate>\n >;\n}\n\nfunction toKeyEnvelopeEntries(\n envelopes: ReadonlyArray<SerializedKeyEnvelope>,\n): RecipientEntry[] {\n return envelopes.map((envelope) => ({\n keyFingerprint: envelope.keyFingerprint,\n kemCipherText: base64ToBytes(envelope.kemCipherText),\n wrappedKey: base64ToBytes(envelope.wrappedKey),\n }));\n}\n\nfunction toMemberEnvelopeEntries(\n envelopes: ReadonlyArray<PrincipalMemberEnvelopeResponse>,\n): RecipientEntry[] {\n return envelopes.map((envelope) => ({\n keyFingerprint: envelope.memberKeyFingerprint,\n kemCipherText: base64ToBytes(envelope.kemCipherText),\n wrappedKey: base64ToBytes(envelope.wrappedKey),\n }));\n}\n\nfunction principalBundleKey(\n bundle: Pick<\n PrincipalKeyEnvelopeCandidate[\"currentState\"],\n \"principalType\" | \"principalId\"\n >,\n): string {\n return `${bundle.principalType}:${bundle.principalId}`;\n}\n\nasync function createPrincipalPolicyResolutionContext(\n execSql: ExecSql,\n fingerprints: readonly string[],\n): Promise<PrincipalPolicyResolutionContext> {\n const bundles = await loadPrincipalKeyEnvelopeCandidates(\n execSql,\n fingerprints,\n );\n const bundlesByKeyFingerprint = new Map<\n string,\n PrincipalKeyEnvelopeCandidate[]\n >();\n\n for (const bundle of bundles) {\n const matchingBundles =\n bundlesByKeyFingerprint.get(bundle.currentState.keyFingerprint) ?? [];\n matchingBundles.push(bundle);\n bundlesByKeyFingerprint.set(\n bundle.currentState.keyFingerprint,\n matchingBundles,\n );\n }\n\n return {\n bundlesByKeyFingerprint,\n };\n}\n\nasync function unwrapPrincipalSecretKey(\n bundle: PrincipalKeyEnvelopeCandidate,\n secretKey: Uint8Array,\n): Promise<Uint8Array> {\n const principalKey = principalBundleKey(bundle.currentState);\n const memberEnvelopeEntries = toMemberEnvelopeEntries(\n bundle.currentMemberEnvelopes.envelopes,\n );\n\n try {\n return await unwrapDek(memberEnvelopeEntries, secretKey);\n } catch {\n // No transitive fallback: a principal's envelopes are all addressed to\n // users directly, so if this identity key opens none of them the requester\n // is not a member.\n }\n\n throw new Error(`No matching principal member envelope for ${principalKey}`);\n}\n\nexport async function unwrapKeyEnvelopesWithPrincipalPolicies(input: {\n envelopes: ReadonlyArray<SerializedKeyEnvelope>;\n execSql?: ExecSql | undefined;\n secretKey: Uint8Array;\n}): Promise<Uint8Array> {\n const keyEntries = toKeyEnvelopeEntries(input.envelopes);\n\n try {\n return await unwrapDek(keyEntries, input.secretKey);\n } catch {\n if (!input.execSql) {\n throw new Error(\n \"No matching key envelope entry found for this secret key and no principal policy cache is available\",\n );\n }\n }\n\n const context = await createPrincipalPolicyResolutionContext(\n input.execSql,\n input.envelopes.map((envelope) => envelope.keyFingerprint),\n );\n const attemptedFingerprints = new Set<string>();\n\n for (const envelope of input.envelopes) {\n if (attemptedFingerprints.has(envelope.keyFingerprint)) continue;\n attemptedFingerprints.add(envelope.keyFingerprint);\n const candidateBundles =\n context.bundlesByKeyFingerprint.get(envelope.keyFingerprint) ?? [];\n\n for (const bundle of candidateBundles) {\n try {\n const principalSecretKey = await unwrapPrincipalSecretKey(\n bundle,\n input.secretKey,\n );\n\n return await unwrapDek(keyEntries, principalSecretKey);\n } catch {}\n }\n }\n\n throw new Error(\n \"No matching key envelope entry found for this secret key or cached principal policies\",\n );\n}\n"]}
@@ -0,0 +1,4 @@
1
+ import type { AnyVerifiedPrincipalPolicy, VerifiedPrincipalPolicy, VerifiedPrincipalPolicyCurrent } from "../../internal/crypto/index.js";
2
+ export type PrincipalPolicyCurrentEvidence = VerifiedPrincipalPolicy | VerifiedPrincipalPolicyCurrent;
3
+ export type PrincipalPolicyCheckpointEvidence = AnyVerifiedPrincipalPolicy | VerifiedPrincipalPolicyCurrent;
4
+ export declare function principalPolicyEvidenceEntries(policy: PrincipalPolicyCheckpointEvidence): NonNullable<VerifiedPrincipalPolicy["history"]>;
@@ -0,0 +1,12 @@
1
+ export function principalPolicyEvidenceEntries(policy) {
2
+ return "retainedHistory" in policy
3
+ ? policy.retainedHistory
4
+ : (policy.history ?? [
5
+ {
6
+ state: policy.state,
7
+ projection: policy.projection,
8
+ grants: policy.grants,
9
+ },
10
+ ]);
11
+ }
12
+ //# sourceMappingURL=principalPolicyEvidence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"principalPolicyEvidence.js","sourceRoot":"","sources":["principalPolicyEvidence.ts"],"names":[],"mappings":"AAaA,MAAM,UAAU,8BAA8B,CAC5C,MAAyC;IAEzC,OAAO,iBAAiB,IAAI,MAAM;QAChC,CAAC,CAAC,MAAM,CAAC,eAAe;QACxB,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,IAAI;YACjB;gBACE,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,UAAU,EAAE,MAAM,CAAC,UAAU;gBAC7B,MAAM,EAAE,MAAM,CAAC,MAAM;aACtB;SACF,CAAC,CAAC;AACT,CAAC","sourcesContent":["import type {\n AnyVerifiedPrincipalPolicy,\n VerifiedPrincipalPolicy,\n VerifiedPrincipalPolicyCurrent,\n} from \"@tearleads/crypto\";\n\nexport type PrincipalPolicyCurrentEvidence =\n | VerifiedPrincipalPolicy\n | VerifiedPrincipalPolicyCurrent;\nexport type PrincipalPolicyCheckpointEvidence =\n | AnyVerifiedPrincipalPolicy\n | VerifiedPrincipalPolicyCurrent;\n\nexport function principalPolicyEvidenceEntries(\n policy: PrincipalPolicyCheckpointEvidence,\n): NonNullable<VerifiedPrincipalPolicy[\"history\"]> {\n return \"retainedHistory\" in policy\n ? policy.retainedHistory\n : (policy.history ?? [\n {\n state: policy.state,\n projection: policy.projection,\n grants: policy.grants,\n },\n ]);\n}\n"]}
@@ -0,0 +1,46 @@
1
+ import { desc } from "drizzle-orm";
2
+ import { index, integer, primaryKey, sqliteTable, text, } from "drizzle-orm/sqlite-core";
3
+ import { principalCurrentFingerprintJson } from "./principalKeyFingerprintJson.js";
4
+ import { defineSqlTableSchema } from "./sqlTableSchema.js";
5
+ /** A completed, locally authenticated prefix; never an application trust pin. */
6
+ export const principalHistoryPrefixes = sqliteTable("principal_history_prefixes", {
7
+ scopeId: text("scope_id").primaryKey(),
8
+ organizationId: text("organization_id").notNull(),
9
+ version: integer("version").notNull(),
10
+ headJson: text("head_json").notNull(),
11
+ currentJson: text("current_json").notNull(),
12
+ progress: text("progress").notNull(),
13
+ }, (table) => [
14
+ index("principal_history_prefixes_key_fingerprint_idx").on(principalCurrentFingerprintJson(table.currentJson), desc(table.version)),
15
+ index("principal_history_prefixes_organization_idx").on(table.organizationId),
16
+ ]);
17
+ /** Entries are selected by their exact signed leaf, including signature bytes. */
18
+ export const principalHistoryEntries = sqliteTable("principal_history_entries", {
19
+ scopeId: text("scope_id").notNull(),
20
+ organizationId: text("organization_id").notNull(),
21
+ leafHash: text("leaf_hash").notNull(),
22
+ entryJson: text("entry_json").notNull(),
23
+ }, (table) => [
24
+ primaryKey({ columns: [table.scopeId, table.leafHash] }),
25
+ index("principal_history_entries_organization_idx").on(table.organizationId),
26
+ ]);
27
+ /** Untrusted proof material, checked against a locally restored private root. */
28
+ export const principalHistoryNodes = sqliteTable("principal_history_nodes", {
29
+ scopeId: text("scope_id").notNull(),
30
+ organizationId: text("organization_id").notNull(),
31
+ hash: text("hash").notNull(),
32
+ leftHash: text("left_hash").notNull(),
33
+ rightHash: text("right_hash").notNull(),
34
+ }, (table) => [
35
+ primaryKey({ columns: [table.scopeId, table.hash] }),
36
+ index("principal_history_nodes_organization_idx").on(table.organizationId),
37
+ ]);
38
+ export const principalHistoryEvidenceTables = [
39
+ {
40
+ ...defineSqlTableSchema(principalHistoryPrefixes),
41
+ requiredColumns: ["current_json"],
42
+ },
43
+ defineSqlTableSchema(principalHistoryEntries),
44
+ defineSqlTableSchema(principalHistoryNodes),
45
+ ];
46
+ //# sourceMappingURL=principalHistoryEvidenceSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"principalHistoryEvidenceSchema.js","sourceRoot":"","sources":["principalHistoryEvidenceSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EACL,KAAK,EACL,OAAO,EACP,UAAU,EACV,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AU,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC;IACxD,KAAK,CAAC,4CAA4C,CAAC,CAAC,EAAE,CACpD,KAAK,CAAC,cAAc,CACrB;CACF,CACF,CAAC;AAEF,iFAAiF;AACjF,MAAM,CAAC,MAAM,qBAAqB,GAAG,WAAW,CAC9C,yBAAyB,EACzB;IACE,OAAO,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC,OAAO,EAAE;IACnC,cAAc,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC,OAAO,EAAE;IACjD,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE;IAC5B,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC,OAAO,EAAE;IACrC,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,OAAO,EAAE;CACxC,EACD,CAAC,KAAK,EAAE,EAAE,CAAC;IACT,UAAU,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;IACpD,KAAK,CAAC,0CAA0C,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC;CAC3E,CACF,CAAC;AAEF,MAAM,CAAC,MAAM,8BAA8B,GAAG;IAC5C;QACE,GAAG,oBAAoB,CAAC,wBAAwB,CAAC;QACjD,eAAe,EAAE,CAAC,cAAc,CAAC;KAClC;IACD,oBAAoB,CAAC,uBAAuB,CAAC;IAC7C,oBAAoB,CAAC,qBAAqB,CAAC;CAC5C,CAAC","sourcesContent":["import { desc } from \"drizzle-orm\";\nimport {\n index,\n integer,\n primaryKey,\n sqliteTable,\n text,\n} from \"drizzle-orm/sqlite-core\";\nimport { principalCurrentFingerprintJson } from \"./principalKeyFingerprintJson\";\nimport { defineSqlTableSchema } from \"./sqlTableSchema\";\n\n/** A completed, locally authenticated prefix; never an application trust pin. */\nexport const principalHistoryPrefixes = sqliteTable(\n \"principal_history_prefixes\",\n {\n scopeId: text(\"scope_id\").primaryKey(),\n organizationId: text(\"organization_id\").notNull(),\n version: integer(\"version\").notNull(),\n headJson: text(\"head_json\").notNull(),\n currentJson: text(\"current_json\").notNull(),\n progress: text(\"progress\").notNull(),\n },\n (table) => [\n index(\"principal_history_prefixes_key_fingerprint_idx\").on(\n principalCurrentFingerprintJson(table.currentJson),\n desc(table.version),\n ),\n index(\"principal_history_prefixes_organization_idx\").on(\n table.organizationId,\n ),\n ],\n);\n\n/** Entries are selected by their exact signed leaf, including signature bytes. */\nexport const principalHistoryEntries = sqliteTable(\n \"principal_history_entries\",\n {\n scopeId: text(\"scope_id\").notNull(),\n organizationId: text(\"organization_id\").notNull(),\n leafHash: text(\"leaf_hash\").notNull(),\n entryJson: text(\"entry_json\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.scopeId, table.leafHash] }),\n index(\"principal_history_entries_organization_idx\").on(\n table.organizationId,\n ),\n ],\n);\n\n/** Untrusted proof material, checked against a locally restored private root. */\nexport const principalHistoryNodes = sqliteTable(\n \"principal_history_nodes\",\n {\n scopeId: text(\"scope_id\").notNull(),\n organizationId: text(\"organization_id\").notNull(),\n hash: text(\"hash\").notNull(),\n leftHash: text(\"left_hash\").notNull(),\n rightHash: text(\"right_hash\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.scopeId, table.hash] }),\n index(\"principal_history_nodes_organization_idx\").on(table.organizationId),\n ],\n);\n\nexport const principalHistoryEvidenceTables = [\n {\n ...defineSqlTableSchema(principalHistoryPrefixes),\n requiredColumns: [\"current_json\"],\n },\n defineSqlTableSchema(principalHistoryEntries),\n defineSqlTableSchema(principalHistoryNodes),\n];\n"]}
@@ -0,0 +1,9 @@
1
+ import { sql } from "drizzle-orm";
2
+ /** Match the candidate query to its expression index, including corrupt caches. */
3
+ export function principalStateFingerprintJson(column) {
4
+ return sql `json_extract(CASE WHEN json_valid(${column}) THEN ${column} END, '$.keyFingerprint')`;
5
+ }
6
+ export function principalCurrentFingerprintJson(column) {
7
+ return sql `json_extract(CASE WHEN json_valid(${column}) THEN ${column} END, '$.currentState.keyFingerprint')`;
8
+ }
9
+ //# sourceMappingURL=principalKeyFingerprintJson.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"principalKeyFingerprintJson.js","sourceRoot":"","sources":["principalKeyFingerprintJson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,GAAG,EAAE,MAAM,aAAa,CAAC;AAEnD,mFAAmF;AACnF,MAAM,UAAU,6BAA6B,CAAC,MAAkB;IAC9D,OAAO,GAAG,CAAQ,qCAAqC,MAAM,UAAU,MAAM,2BAA2B,CAAC;AAC3G,CAAC;AAED,MAAM,UAAU,+BAA+B,CAAC,MAAkB;IAChE,OAAO,GAAG,CAAQ,qCAAqC,MAAM,UAAU,MAAM,wCAAwC,CAAC;AACxH,CAAC","sourcesContent":["import { type SQLWrapper, sql } from \"drizzle-orm\";\n\n/** Match the candidate query to its expression index, including corrupt caches. */\nexport function principalStateFingerprintJson(column: SQLWrapper) {\n return sql<string>`json_extract(CASE WHEN json_valid(${column}) THEN ${column} END, '$.keyFingerprint')`;\n}\n\nexport function principalCurrentFingerprintJson(column: SQLWrapper) {\n return sql<string>`json_extract(CASE WHEN json_valid(${column}) THEN ${column} END, '$.currentState.keyFingerprint')`;\n}\n"]}
@@ -1,4 +1,6 @@
1
+ import { desc } from "drizzle-orm";
1
2
  import { index, integer, primaryKey, sqliteTable, text, } from "drizzle-orm/sqlite-core";
3
+ import { principalStateFingerprintJson } from "./principalKeyFingerprintJson.js";
2
4
  /** Durable organization ownership for every managed-principal cache. */
3
5
  export const principalPolicyOrganizations = sqliteTable("principal_policy_organizations", {
4
6
  principalType: text("principal_type").notNull(),
@@ -25,12 +27,14 @@ function principalPolicyBundleColumns() {
25
27
  /** Latest full policy bundle per managed principal. */
26
28
  export const principalPolicies = sqliteTable("principal_policies", principalPolicyBundleColumns(), (table) => [
27
29
  primaryKey({ columns: [table.principalType, table.principalId] }),
30
+ index("principal_policies_key_fingerprint_idx").on(principalStateFingerprintJson(table.currentStateJson), desc(table.updatedAt)),
28
31
  ]);
29
32
  /** Older verified bundles retained for exact-reference verification and epoch key unwrapping. */
30
33
  export const principalPolicyBundleHistory = sqliteTable("principal_policy_bundle_history", principalPolicyBundleColumns(), (table) => [
31
34
  primaryKey({
32
35
  columns: [table.principalType, table.principalId, table.stateHash],
33
36
  }),
37
+ index("principal_policy_history_key_fingerprint_idx").on(principalStateFingerprintJson(table.currentStateJson), desc(table.updatedAt)),
34
38
  ]);
35
39
  /** Exact policy states mapped to every retained bundle chain that contains them. */
36
40
  export const principalPolicyBundleReferences = sqliteTable("principal_policy_bundle_references", {
@@ -1 +1 @@
1
- {"version":3,"file":"principalPolicySchema.js","sourceRoot":"","sources":["principalPolicySchema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,OAAO,EACP,UAAU,EACV,WAAW,EACX,IAAI,GACL,MAAM,yBAAyB,CAAC;AAEjC,wEAAwE;AACxE,MAAM,CAAC,MAAM,4BAA4B,GAAG,WAAW,CACrD,gCAAgC,EAChC;IACE,aAAa,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC,OAAO,EAAE;IAC/C,WAAW,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC,OAAO,EAAE;IAC3C,cAAc,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC,OAAO,EAAE;CAClD,EACD,CAAC,KAAK,EAAE,EAAE,CAAC;IACT,UAAU,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,CAAC,WAAW,CAAC,EAAE,CAAC;IACjE,KAAK,CAAC,iDAAiD,CAAC,CAAC,EAAE,CACzD,KAAK,CAAC,cAAc,CACrB;CACF,CACF,CAAC;AAEF,SAAS,4BAA4B;IACnC,OAAO;QACL,aAAa,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC,OAAO,E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K,CAAC,WAAW,CAAC,EAAE,CAAC;CAClE,CACF,CAAC","sourcesContent":["import {\n index,\n integer,\n primaryKey,\n sqliteTable,\n text,\n} from \"drizzle-orm/sqlite-core\";\n\n/** Durable organization ownership for every managed-principal cache. */\nexport const principalPolicyOrganizations = sqliteTable(\n \"principal_policy_organizations\",\n {\n principalType: text(\"principal_type\").notNull(),\n principalId: text(\"principal_id\").notNull(),\n organizationId: text(\"organization_id\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.principalType, table.principalId] }),\n index(\"principal_policy_organizations_organization_idx\").on(\n table.organizationId,\n ),\n ],\n);\n\nfunction principalPolicyBundleColumns() {\n return {\n principalType: text(\"principal_type\").notNull(),\n principalId: text(\"principal_id\").notNull(),\n stateHash: text(\"state_hash\").notNull(),\n currentStateJson: text(\"current_state_json\").notNull(),\n currentPayloadJson: text(\"current_payload_json\").notNull(),\n currentProjectionJson: text(\"current_projection_json\").notNull(),\n currentGrantsJson: text(\"current_grants_json\").notNull(),\n currentMemberEnvelopesJson: text(\"current_member_envelopes_json\").notNull(),\n previousStatesJson: text(\"previous_states_json\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n };\n}\n\n/** Latest full policy bundle per managed principal. */\nexport const principalPolicies = sqliteTable(\n \"principal_policies\",\n principalPolicyBundleColumns(),\n (table) => [\n primaryKey({ columns: [table.principalType, table.principalId] }),\n ],\n);\n\n/** Older verified bundles retained for exact-reference verification and epoch key unwrapping. */\nexport const principalPolicyBundleHistory = sqliteTable(\n \"principal_policy_bundle_history\",\n principalPolicyBundleColumns(),\n (table) => [\n primaryKey({\n columns: [table.principalType, table.principalId, table.stateHash],\n }),\n ],\n);\n\n/** Exact policy states mapped to every retained bundle chain that contains them. */\nexport const principalPolicyBundleReferences = sqliteTable(\n \"principal_policy_bundle_references\",\n {\n principalType: text(\"principal_type\").notNull(),\n principalId: text(\"principal_id\").notNull(),\n version: integer(\"version\").notNull(),\n stateHash: text(\"state_hash\").notNull(),\n keyEpoch: integer(\"key_epoch\").notNull(),\n keyFingerprint: text(\"key_fingerprint\").notNull(),\n bundleVersion: integer(\"bundle_version\").notNull(),\n bundleStateHash: text(\"bundle_state_hash\").notNull(),\n },\n (table) => [\n primaryKey({\n columns: [\n table.principalType,\n table.principalId,\n table.version,\n table.stateHash,\n table.keyEpoch,\n table.keyFingerprint,\n table.bundleStateHash,\n ],\n }),\n ],\n);\n\n/** Monotonic anti-rollback pin for each verified managed principal. */\nexport const principalPolicyCheckpoints = sqliteTable(\n \"principal_policy_checkpoints\",\n {\n principalType: text(\"principal_type\").notNull(),\n principalId: text(\"principal_id\").notNull(),\n version: integer(\"version\").notNull(),\n stateHash: text(\"state_hash\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.principalType, table.principalId] }),\n ],\n);\n"]}
1
+ {"version":3,"file":"principalPolicySchema.js","sourceRoot":"","sources":["principalPolicySchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EACL,KAAK,EACL,OAAO,EACP,UAAU,EACV,WAAW,EACX,IAAI,GACL,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,6BAA6B,EAAE,MAAM,+BAA+B,CAAC;AAE9E,wEAAwE;AACxE,MAAM,CAAC,MAAM,4BAA4B,GAAG,WAAW,CACrD,gCAAgC,EAChC;IACE,aAAa,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC,OAAO,EAAE;IAC/C,WAAW,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC,OAAO,EAAE;IAC3C,cAAc,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC,OAAO,EAAE;CAClD,EACD,CAAC,KAAK,EAAE,EAAE,CAAC;IACT,UAAU,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,CAAC,WAAW,CAAC,EAAE,CAAC;IACjE,KAAK,CAAC,iDAAiD,CAAC,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B,EAC9B;IACE,aAAa,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC,OAAO,EAAE;IAC/C,WAAW,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC,OAAO,EAAE;IAC3C,OAAO,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE;IACrC,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,OAAO,EAAE;IACvC,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,OAAO,EAAE;CACxC,EACD,CAAC,KAAK,EAAE,EAAE,CAAC;IACT,UAAU,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,CAAC,WAAW,CAAC,EAAE,CAAC;CAClE,CACF,CAAC","sourcesContent":["import { desc } from \"drizzle-orm\";\nimport {\n index,\n integer,\n primaryKey,\n sqliteTable,\n text,\n} from \"drizzle-orm/sqlite-core\";\n\nimport { principalStateFingerprintJson } from \"./principalKeyFingerprintJson\";\n\n/** Durable organization ownership for every managed-principal cache. */\nexport const principalPolicyOrganizations = sqliteTable(\n \"principal_policy_organizations\",\n {\n principalType: text(\"principal_type\").notNull(),\n principalId: text(\"principal_id\").notNull(),\n organizationId: text(\"organization_id\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.principalType, table.principalId] }),\n index(\"principal_policy_organizations_organization_idx\").on(\n table.organizationId,\n ),\n ],\n);\n\nfunction principalPolicyBundleColumns() {\n return {\n principalType: text(\"principal_type\").notNull(),\n principalId: text(\"principal_id\").notNull(),\n stateHash: text(\"state_hash\").notNull(),\n currentStateJson: text(\"current_state_json\").notNull(),\n currentPayloadJson: text(\"current_payload_json\").notNull(),\n currentProjectionJson: text(\"current_projection_json\").notNull(),\n currentGrantsJson: text(\"current_grants_json\").notNull(),\n currentMemberEnvelopesJson: text(\"current_member_envelopes_json\").notNull(),\n previousStatesJson: text(\"previous_states_json\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n };\n}\n\n/** Latest full policy bundle per managed principal. */\nexport const principalPolicies = sqliteTable(\n \"principal_policies\",\n principalPolicyBundleColumns(),\n (table) => [\n primaryKey({ columns: [table.principalType, table.principalId] }),\n index(\"principal_policies_key_fingerprint_idx\").on(\n principalStateFingerprintJson(table.currentStateJson),\n desc(table.updatedAt),\n ),\n ],\n);\n\n/** Older verified bundles retained for exact-reference verification and epoch key unwrapping. */\nexport const principalPolicyBundleHistory = sqliteTable(\n \"principal_policy_bundle_history\",\n principalPolicyBundleColumns(),\n (table) => [\n primaryKey({\n columns: [table.principalType, table.principalId, table.stateHash],\n }),\n index(\"principal_policy_history_key_fingerprint_idx\").on(\n principalStateFingerprintJson(table.currentStateJson),\n desc(table.updatedAt),\n ),\n ],\n);\n\n/** Exact policy states mapped to every retained bundle chain that contains them. */\nexport const principalPolicyBundleReferences = sqliteTable(\n \"principal_policy_bundle_references\",\n {\n principalType: text(\"principal_type\").notNull(),\n principalId: text(\"principal_id\").notNull(),\n version: integer(\"version\").notNull(),\n stateHash: text(\"state_hash\").notNull(),\n keyEpoch: integer(\"key_epoch\").notNull(),\n keyFingerprint: text(\"key_fingerprint\").notNull(),\n bundleVersion: integer(\"bundle_version\").notNull(),\n bundleStateHash: text(\"bundle_state_hash\").notNull(),\n },\n (table) => [\n primaryKey({\n columns: [\n table.principalType,\n table.principalId,\n table.version,\n table.stateHash,\n table.keyEpoch,\n table.keyFingerprint,\n table.bundleStateHash,\n ],\n }),\n ],\n);\n\n/** Monotonic anti-rollback pin for each verified managed principal. */\nexport const principalPolicyCheckpoints = sqliteTable(\n \"principal_policy_checkpoints\",\n {\n principalType: text(\"principal_type\").notNull(),\n principalId: text(\"principal_id\").notNull(),\n version: integer(\"version\").notNull(),\n stateHash: text(\"state_hash\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.principalType, table.principalId] }),\n ],\n);\n"]}
@@ -6510,6 +6510,310 @@ export declare const clientSQLiteSchema: {
6510
6510
  };
6511
6511
  dialect: "sqlite";
6512
6512
  }>;
6513
+ principalHistoryEntries: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
6514
+ name: "principal_history_entries";
6515
+ schema: undefined;
6516
+ columns: {
6517
+ scopeId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6518
+ name: "scope_id";
6519
+ tableName: "principal_history_entries";
6520
+ dataType: "string";
6521
+ columnType: "SQLiteText";
6522
+ data: string;
6523
+ driverParam: string;
6524
+ notNull: true;
6525
+ hasDefault: false;
6526
+ isPrimaryKey: false;
6527
+ isAutoincrement: false;
6528
+ hasRuntimeDefault: false;
6529
+ enumValues: [string, ...string[]];
6530
+ baseColumn: never;
6531
+ identity: undefined;
6532
+ generated: undefined;
6533
+ }, {}, {
6534
+ length: number | undefined;
6535
+ }>;
6536
+ organizationId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6537
+ name: "organization_id";
6538
+ tableName: "principal_history_entries";
6539
+ dataType: "string";
6540
+ columnType: "SQLiteText";
6541
+ data: string;
6542
+ driverParam: string;
6543
+ notNull: true;
6544
+ hasDefault: false;
6545
+ isPrimaryKey: false;
6546
+ isAutoincrement: false;
6547
+ hasRuntimeDefault: false;
6548
+ enumValues: [string, ...string[]];
6549
+ baseColumn: never;
6550
+ identity: undefined;
6551
+ generated: undefined;
6552
+ }, {}, {
6553
+ length: number | undefined;
6554
+ }>;
6555
+ leafHash: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6556
+ name: "leaf_hash";
6557
+ tableName: "principal_history_entries";
6558
+ dataType: "string";
6559
+ columnType: "SQLiteText";
6560
+ data: string;
6561
+ driverParam: string;
6562
+ notNull: true;
6563
+ hasDefault: false;
6564
+ isPrimaryKey: false;
6565
+ isAutoincrement: false;
6566
+ hasRuntimeDefault: false;
6567
+ enumValues: [string, ...string[]];
6568
+ baseColumn: never;
6569
+ identity: undefined;
6570
+ generated: undefined;
6571
+ }, {}, {
6572
+ length: number | undefined;
6573
+ }>;
6574
+ entryJson: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6575
+ name: "entry_json";
6576
+ tableName: "principal_history_entries";
6577
+ dataType: "string";
6578
+ columnType: "SQLiteText";
6579
+ data: string;
6580
+ driverParam: string;
6581
+ notNull: true;
6582
+ hasDefault: false;
6583
+ isPrimaryKey: false;
6584
+ isAutoincrement: false;
6585
+ hasRuntimeDefault: false;
6586
+ enumValues: [string, ...string[]];
6587
+ baseColumn: never;
6588
+ identity: undefined;
6589
+ generated: undefined;
6590
+ }, {}, {
6591
+ length: number | undefined;
6592
+ }>;
6593
+ };
6594
+ dialect: "sqlite";
6595
+ }>;
6596
+ principalHistoryNodes: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
6597
+ name: "principal_history_nodes";
6598
+ schema: undefined;
6599
+ columns: {
6600
+ scopeId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6601
+ name: "scope_id";
6602
+ tableName: "principal_history_nodes";
6603
+ dataType: "string";
6604
+ columnType: "SQLiteText";
6605
+ data: string;
6606
+ driverParam: string;
6607
+ notNull: true;
6608
+ hasDefault: false;
6609
+ isPrimaryKey: false;
6610
+ isAutoincrement: false;
6611
+ hasRuntimeDefault: false;
6612
+ enumValues: [string, ...string[]];
6613
+ baseColumn: never;
6614
+ identity: undefined;
6615
+ generated: undefined;
6616
+ }, {}, {
6617
+ length: number | undefined;
6618
+ }>;
6619
+ organizationId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6620
+ name: "organization_id";
6621
+ tableName: "principal_history_nodes";
6622
+ dataType: "string";
6623
+ columnType: "SQLiteText";
6624
+ data: string;
6625
+ driverParam: string;
6626
+ notNull: true;
6627
+ hasDefault: false;
6628
+ isPrimaryKey: false;
6629
+ isAutoincrement: false;
6630
+ hasRuntimeDefault: false;
6631
+ enumValues: [string, ...string[]];
6632
+ baseColumn: never;
6633
+ identity: undefined;
6634
+ generated: undefined;
6635
+ }, {}, {
6636
+ length: number | undefined;
6637
+ }>;
6638
+ hash: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6639
+ name: "hash";
6640
+ tableName: "principal_history_nodes";
6641
+ dataType: "string";
6642
+ columnType: "SQLiteText";
6643
+ data: string;
6644
+ driverParam: string;
6645
+ notNull: true;
6646
+ hasDefault: false;
6647
+ isPrimaryKey: false;
6648
+ isAutoincrement: false;
6649
+ hasRuntimeDefault: false;
6650
+ enumValues: [string, ...string[]];
6651
+ baseColumn: never;
6652
+ identity: undefined;
6653
+ generated: undefined;
6654
+ }, {}, {
6655
+ length: number | undefined;
6656
+ }>;
6657
+ leftHash: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6658
+ name: "left_hash";
6659
+ tableName: "principal_history_nodes";
6660
+ dataType: "string";
6661
+ columnType: "SQLiteText";
6662
+ data: string;
6663
+ driverParam: string;
6664
+ notNull: true;
6665
+ hasDefault: false;
6666
+ isPrimaryKey: false;
6667
+ isAutoincrement: false;
6668
+ hasRuntimeDefault: false;
6669
+ enumValues: [string, ...string[]];
6670
+ baseColumn: never;
6671
+ identity: undefined;
6672
+ generated: undefined;
6673
+ }, {}, {
6674
+ length: number | undefined;
6675
+ }>;
6676
+ rightHash: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6677
+ name: "right_hash";
6678
+ tableName: "principal_history_nodes";
6679
+ dataType: "string";
6680
+ columnType: "SQLiteText";
6681
+ data: string;
6682
+ driverParam: string;
6683
+ notNull: true;
6684
+ hasDefault: false;
6685
+ isPrimaryKey: false;
6686
+ isAutoincrement: false;
6687
+ hasRuntimeDefault: false;
6688
+ enumValues: [string, ...string[]];
6689
+ baseColumn: never;
6690
+ identity: undefined;
6691
+ generated: undefined;
6692
+ }, {}, {
6693
+ length: number | undefined;
6694
+ }>;
6695
+ };
6696
+ dialect: "sqlite";
6697
+ }>;
6698
+ principalHistoryPrefixes: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
6699
+ name: "principal_history_prefixes";
6700
+ schema: undefined;
6701
+ columns: {
6702
+ scopeId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6703
+ name: "scope_id";
6704
+ tableName: "principal_history_prefixes";
6705
+ dataType: "string";
6706
+ columnType: "SQLiteText";
6707
+ data: string;
6708
+ driverParam: string;
6709
+ notNull: true;
6710
+ hasDefault: false;
6711
+ isPrimaryKey: true;
6712
+ isAutoincrement: false;
6713
+ hasRuntimeDefault: false;
6714
+ enumValues: [string, ...string[]];
6715
+ baseColumn: never;
6716
+ identity: undefined;
6717
+ generated: undefined;
6718
+ }, {}, {
6719
+ length: number | undefined;
6720
+ }>;
6721
+ organizationId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6722
+ name: "organization_id";
6723
+ tableName: "principal_history_prefixes";
6724
+ dataType: "string";
6725
+ columnType: "SQLiteText";
6726
+ data: string;
6727
+ driverParam: string;
6728
+ notNull: true;
6729
+ hasDefault: false;
6730
+ isPrimaryKey: false;
6731
+ isAutoincrement: false;
6732
+ hasRuntimeDefault: false;
6733
+ enumValues: [string, ...string[]];
6734
+ baseColumn: never;
6735
+ identity: undefined;
6736
+ generated: undefined;
6737
+ }, {}, {
6738
+ length: number | undefined;
6739
+ }>;
6740
+ version: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6741
+ name: "version";
6742
+ tableName: "principal_history_prefixes";
6743
+ dataType: "number";
6744
+ columnType: "SQLiteInteger";
6745
+ data: number;
6746
+ driverParam: number;
6747
+ notNull: true;
6748
+ hasDefault: false;
6749
+ isPrimaryKey: false;
6750
+ isAutoincrement: false;
6751
+ hasRuntimeDefault: false;
6752
+ enumValues: undefined;
6753
+ baseColumn: never;
6754
+ identity: undefined;
6755
+ generated: undefined;
6756
+ }, {}, {}>;
6757
+ headJson: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6758
+ name: "head_json";
6759
+ tableName: "principal_history_prefixes";
6760
+ dataType: "string";
6761
+ columnType: "SQLiteText";
6762
+ data: string;
6763
+ driverParam: string;
6764
+ notNull: true;
6765
+ hasDefault: false;
6766
+ isPrimaryKey: false;
6767
+ isAutoincrement: false;
6768
+ hasRuntimeDefault: false;
6769
+ enumValues: [string, ...string[]];
6770
+ baseColumn: never;
6771
+ identity: undefined;
6772
+ generated: undefined;
6773
+ }, {}, {
6774
+ length: number | undefined;
6775
+ }>;
6776
+ currentJson: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6777
+ name: "current_json";
6778
+ tableName: "principal_history_prefixes";
6779
+ dataType: "string";
6780
+ columnType: "SQLiteText";
6781
+ data: string;
6782
+ driverParam: string;
6783
+ notNull: true;
6784
+ hasDefault: false;
6785
+ isPrimaryKey: false;
6786
+ isAutoincrement: false;
6787
+ hasRuntimeDefault: false;
6788
+ enumValues: [string, ...string[]];
6789
+ baseColumn: never;
6790
+ identity: undefined;
6791
+ generated: undefined;
6792
+ }, {}, {
6793
+ length: number | undefined;
6794
+ }>;
6795
+ progress: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6796
+ name: "progress";
6797
+ tableName: "principal_history_prefixes";
6798
+ dataType: "string";
6799
+ columnType: "SQLiteText";
6800
+ data: string;
6801
+ driverParam: string;
6802
+ notNull: true;
6803
+ hasDefault: false;
6804
+ isPrimaryKey: false;
6805
+ isAutoincrement: false;
6806
+ hasRuntimeDefault: false;
6807
+ enumValues: [string, ...string[]];
6808
+ baseColumn: never;
6809
+ identity: undefined;
6810
+ generated: undefined;
6811
+ }, {}, {
6812
+ length: number | undefined;
6813
+ }>;
6814
+ };
6815
+ dialect: "sqlite";
6816
+ }>;
6513
6817
  principalHistoryStages: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
6514
6818
  name: "principal_history_stages";
6515
6819
  schema: undefined;
@@ -11,6 +11,7 @@ import { organizationFounders } from "./organizationFounderSchema.js";
11
11
  import * as provisioningAttempt from "./organizationProvisioningAttemptSchema.js";
12
12
  import { organizationReadModelSQLiteSchema, organizationReadModelTables, } from "./organizationReadModelTableRegistry.js";
13
13
  import { principalGrantRetirements } from "./principalGrantRetirementSchema.js";
14
+ import { principalHistoryEntries, principalHistoryEvidenceTables, principalHistoryNodes, principalHistoryPrefixes, } from "./principalHistoryEvidenceSchema.js";
14
15
  import { principalHistoryStages, principalHistoryStageTables, } from "./principalHistoryStageSchema.js";
15
16
  import { principalPolicies, principalPolicyBundleHistory, principalPolicyBundleReferences, principalPolicyCheckpoints, principalPolicyOrganizations, } from "./principalPolicySchema.js";
16
17
  import { securityIncidents, securityIncidentTable, } from "./securityIncidentSchema.js";
@@ -523,6 +524,7 @@ export const principalPolicyTables = [
523
524
  defineSqlTableSchema(principalPolicyBundleReferences),
524
525
  defineSqlTableSchema(principalPolicyOrganizations),
525
526
  ...principalHistoryStageTables,
527
+ ...principalHistoryEvidenceTables,
526
528
  ];
527
529
  export const keyingCheckpointTables = [
528
530
  defineSqlTableSchema(organizationFounders),
@@ -596,6 +598,9 @@ export const clientSqlTables = [
596
598
  ...provisioningAttempt.organizationProvisioningAttemptTables,
597
599
  ];
598
600
  export const clientSQLiteSchema = {
601
+ principalHistoryEntries,
602
+ principalHistoryNodes,
603
+ principalHistoryPrefixes,
599
604
  principalHistoryStages,
600
605
  organizationFounders,
601
606
  principalGrantRetirements,