@tearleads/client-sdk 0.1.13 → 0.1.15

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 (204) hide show
  1. package/README.md +32 -7
  2. package/client/containerContents.js +2 -1
  3. package/client/containerContents.js.map +1 -1
  4. package/data/documents/blob/shared/types.d.ts +4 -0
  5. package/data/documents/blob/shared/types.js.map +1 -1
  6. package/data/keyingProjectionVerification/accessManifestCheckpointEnforcement.js +2 -0
  7. package/data/keyingProjectionVerification/accessManifestCheckpointEnforcement.js.map +1 -1
  8. package/data/keyingProjectionVerification/checkpointContext.d.ts +2 -1
  9. package/data/keyingProjectionVerification/checkpointContext.js +22 -0
  10. package/data/keyingProjectionVerification/checkpointContext.js.map +1 -1
  11. package/data/keyingProjectionVerification/containerDestinationVerification.js +11 -1
  12. package/data/keyingProjectionVerification/containerDestinationVerification.js.map +1 -1
  13. package/data/keyingProjectionVerification/containerManifestVerification.js.map +1 -1
  14. package/data/keyingProjectionVerification/containerPathVerification.js.map +1 -1
  15. package/data/keyingProjectionVerification/containerProjectionVerification.d.ts +3 -2
  16. package/data/keyingProjectionVerification/containerProjectionVerification.js +10 -11
  17. package/data/keyingProjectionVerification/containerProjectionVerification.js.map +1 -1
  18. package/data/keyingProjectionVerification/documentManifestPolicies.js.map +1 -1
  19. package/data/keyingProjectionVerification/documentManifestVerification.js.map +1 -1
  20. package/data/keyingProjectionVerification/documentProjectionVerification.d.ts +2 -3
  21. package/data/keyingProjectionVerification/documentProjectionVerification.js +4 -0
  22. package/data/keyingProjectionVerification/documentProjectionVerification.js.map +1 -1
  23. package/data/keyingProjectionVerification/mergeProjectionPolicyEvidence.js +14 -12
  24. package/data/keyingProjectionVerification/mergeProjectionPolicyEvidence.js.map +1 -1
  25. package/data/keyingProjectionVerification/organizationFounderBinding.js.map +1 -1
  26. package/data/keyingProjectionVerification/projectionAuthorizationEvidence.js +33 -6
  27. package/data/keyingProjectionVerification/projectionAuthorizationEvidence.js.map +1 -1
  28. package/data/keyingProjectionVerification/projectionLifetimes.js +13 -0
  29. package/data/keyingProjectionVerification/projectionLifetimes.js.map +1 -0
  30. package/data/keyingProjectionVerification/projectionPolicyEvidence.js +19 -66
  31. package/data/keyingProjectionVerification/projectionPolicyEvidence.js.map +1 -1
  32. package/data/keyingProjectionVerification/types.d.ts +13 -2
  33. package/data/keyingProjectionVerification/types.js +15 -0
  34. package/data/keyingProjectionVerification/types.js.map +1 -1
  35. package/data/persistence/keyingCheckpointAdvancePersistence.js +4 -1
  36. package/data/persistence/keyingCheckpointAdvancePersistence.js.map +1 -1
  37. package/data/persistence/organizationFounderPersistence.js +2 -2
  38. package/data/persistence/organizationFounderPersistence.js.map +1 -1
  39. package/data/persistence/principalAuthorizationCheckpoints.js +29 -0
  40. package/data/persistence/principalAuthorizationCheckpoints.js.map +1 -0
  41. package/data/persistence/principalHistoryEvidencePersistence.d.ts +30 -0
  42. package/data/persistence/principalHistoryPrefixPersistence.d.ts +24 -0
  43. package/data/persistence/principalHistoryPrefixPersistence.js +6 -2
  44. package/data/persistence/principalHistoryPrefixPersistence.js.map +1 -1
  45. package/data/persistence/principalHistoryStagePersistence.d.ts +21 -0
  46. package/data/principals/principalHistoryRuntime.js +12 -0
  47. package/data/principals/principalHistoryRuntime.js.map +1 -0
  48. package/data/principals/principalHistoryStageProtection.js +2 -12
  49. package/data/principals/principalHistoryStageProtection.js.map +1 -1
  50. package/data/principals/principalPolicyEvidence.d.ts +2 -2
  51. package/data/principals/principalPolicyEvidence.js.map +1 -1
  52. package/data/sqlite/principalHistoryEvidenceSchema.d.ts +308 -0
  53. package/data/sqlite/principalHistoryStageSchema.d.ts +119 -0
  54. package/index.d.ts +2 -2
  55. package/index.js +1 -1
  56. package/index.js.map +1 -1
  57. package/internal/api-client/ApiCache.d.ts +54 -0
  58. package/internal/api-client/ApiClient.d.ts +255 -309
  59. package/internal/api-client/ApiClient.js +15 -6
  60. package/internal/api-client/ApiClient.js.map +1 -1
  61. package/internal/api-client/index.d.ts +1 -0
  62. package/internal/api-client/index.js.map +1 -1
  63. package/internal/api-client/principalHistoryDeadline.js +20 -0
  64. package/internal/api-client/principalHistoryDeadline.js.map +1 -0
  65. package/internal/api-client/principalHistoryRequest.js +59 -22
  66. package/internal/api-client/principalHistoryRequest.js.map +1 -1
  67. package/internal/api-client/principalHistoryRequestContext.js +18 -5
  68. package/internal/api-client/principalHistoryRequestContext.js.map +1 -1
  69. package/internal/api-client/principalPolicyRequests.d.ts +799 -0
  70. package/internal/api-client/principalPolicyRequests.js +9 -4
  71. package/internal/api-client/principalPolicyRequests.js.map +1 -1
  72. package/internal/api-client/projectionPolicyHistoryPages.d.ts +9 -0
  73. package/internal/api-client/projectionPolicyHistoryPages.js +88 -0
  74. package/internal/api-client/projectionPolicyHistoryPages.js.map +1 -0
  75. package/internal/api-client/writerProjectionRequests.js +26 -0
  76. package/internal/api-client/writerProjectionRequests.js.map +1 -0
  77. package/internal/crypto/keying/principalPolicyHistory.js +4 -0
  78. package/internal/crypto/keying/principalPolicyHistory.js.map +1 -1
  79. package/internal/crypto/keying/principalPolicyHistoryTypes.d.ts +2 -0
  80. package/internal/crypto/keying/principalPolicyHistoryTypes.js.map +1 -1
  81. package/internal/crypto/projectionHistorySlots.js +0 -4
  82. package/internal/crypto/projectionHistorySlots.js.map +1 -1
  83. package/internal/validators/operation/index.d.ts +1 -0
  84. package/internal/validators/operation/index.js +1 -0
  85. package/internal/validators/operation/index.js.map +1 -1
  86. package/internal/validators/operation/principals.d.ts +38 -16
  87. package/internal/validators/operation/principals.js +11 -5
  88. package/internal/validators/operation/principals.js.map +1 -1
  89. package/internal/validators/operation/projectionPolicyHistory.d.ts +512 -0
  90. package/internal/validators/operation/projectionPolicyHistory.js +37 -0
  91. package/internal/validators/operation/projectionPolicyHistory.js.map +1 -0
  92. package/internal/validators/operation/registry.d.ts +2 -0
  93. package/internal/validators/operation/registry.js +2 -0
  94. package/internal/validators/operation/registry.js.map +1 -1
  95. package/internal/validators/operation/writerProjections.d.ts +178 -1224
  96. package/internal/validators/operation/writerProjections.js +9 -2
  97. package/internal/validators/operation/writerProjections.js.map +1 -1
  98. package/internal/validators/response/container.d.ts +84 -612
  99. package/internal/validators/response/documentMutation.d.ts +84 -612
  100. package/internal/validators/response/index.d.ts +1 -0
  101. package/internal/validators/response/index.js +1 -0
  102. package/internal/validators/response/index.js.map +1 -1
  103. package/internal/validators/response/organizationBilling.d.ts +1 -0
  104. package/internal/validators/response/organizationBilling.js +1 -1
  105. package/internal/validators/response/organizationBilling.js.map +1 -1
  106. package/internal/validators/response/principalHistoryPreparation.js +28 -1
  107. package/internal/validators/response/principalHistoryPreparation.js.map +1 -1
  108. package/internal/validators/response/principalPolicySnapshotPage.d.ts +388 -0
  109. package/internal/validators/response/principalPolicySnapshotPage.js +24 -0
  110. package/internal/validators/response/principalPolicySnapshotPage.js.map +1 -0
  111. package/internal/validators/response/projectionPolicyEvidence.d.ts +85 -613
  112. package/internal/validators/response/projectionPolicyEvidence.js +10 -5
  113. package/internal/validators/response/projectionPolicyEvidence.js.map +1 -1
  114. package/package.json +4 -1
  115. package/sqlite/sqlite3-licenses.md +38 -0
  116. package/sqlite/sqlite3.wasm +0 -0
  117. package/sqlite/worker.js +4 -0
  118. package/stores/documents/documentStore/attachmentHydration.js +3 -0
  119. package/stores/documents/documentStore/attachmentHydration.js.map +1 -1
  120. package/stores/documents/documentStore/syncDetachedAttachments.js +3 -0
  121. package/stores/documents/documentStore/syncDetachedAttachments.js.map +1 -1
  122. package/workflows/blobs/decrypt.d.ts +1 -1
  123. package/workflows/blobs/decrypt.js +3 -1
  124. package/workflows/blobs/decrypt.js.map +1 -1
  125. package/workflows/blobs/detach.js +6 -1
  126. package/workflows/blobs/detach.js.map +1 -1
  127. package/workflows/blobs/hydrate.d.ts +3 -1
  128. package/workflows/blobs/hydrate.js +2 -0
  129. package/workflows/blobs/hydrate.js.map +1 -1
  130. package/workflows/blobs/retainedAttachmentVerification.js +2 -0
  131. package/workflows/blobs/retainedAttachmentVerification.js.map +1 -1
  132. package/workflows/container-contents/container-state/createAdoption.js +2 -0
  133. package/workflows/container-contents/container-state/createAdoption.js.map +1 -1
  134. package/workflows/container-contents/container-state/createWithMetadata.js +7 -39
  135. package/workflows/container-contents/container-state/createWithMetadata.js.map +1 -1
  136. package/workflows/container-contents/container-state/createWithMetadataPlan.js +68 -0
  137. package/workflows/container-contents/container-state/createWithMetadataPlan.js.map +1 -0
  138. package/workflows/container-contents/remoteContainerState.js +3 -1
  139. package/workflows/container-contents/remoteContainerState.js.map +1 -1
  140. package/workflows/container-contents/remoteHydration/verifiedDestination.js +3 -0
  141. package/workflows/container-contents/remoteHydration/verifiedDestination.js.map +1 -1
  142. package/workflows/container-contents/runtime.d.ts +3 -0
  143. package/workflows/container-contents/runtime.js +7 -6
  144. package/workflows/container-contents/runtime.js.map +1 -1
  145. package/workflows/documents/create.d.ts +2 -15
  146. package/workflows/documents/create.js +9 -64
  147. package/workflows/documents/create.js.map +1 -1
  148. package/workflows/documents/createProjection.d.ts +14 -0
  149. package/workflows/documents/createProjection.js +26 -0
  150. package/workflows/documents/createProjection.js.map +1 -0
  151. package/workflows/documents/createResponse.js +49 -0
  152. package/workflows/documents/createResponse.js.map +1 -0
  153. package/workflows/documents/linkSetBlobRewraps.js +2 -0
  154. package/workflows/documents/linkSetBlobRewraps.js.map +1 -1
  155. package/workflows/documents/runtime.d.ts +3 -0
  156. package/workflows/documents/runtime.js +3 -2
  157. package/workflows/documents/runtime.js.map +1 -1
  158. package/workflows/documents/syncFailureClassification.js +3 -1
  159. package/workflows/documents/syncFailureClassification.js.map +1 -1
  160. package/workflows/principals/index.d.ts +2 -0
  161. package/workflows/principals/index.js +1 -0
  162. package/workflows/principals/index.js.map +1 -1
  163. package/workflows/principals/policyVerification.js.map +1 -1
  164. package/workflows/principals/principalHistoryRecoveryAuthority.js +20 -0
  165. package/workflows/principals/principalHistoryRecoveryAuthority.js.map +1 -0
  166. package/workflows/principals/principalHistoryRecoveryStage.js +1 -1
  167. package/workflows/principals/principalHistoryRecoveryStage.js.map +1 -1
  168. package/workflows/principals/principalHistoryRecoveryTypes.d.ts +3 -3
  169. package/workflows/principals/principalHistoryRecoveryTypes.js.map +1 -1
  170. package/workflows/principals/principalHistoryRecoveryVerification.js +6 -1
  171. package/workflows/principals/principalHistoryRecoveryVerification.js.map +1 -1
  172. package/workflows/principals/principalRecoveryOutage.js +1 -1
  173. package/workflows/principals/principalRecoveryOutage.js.map +1 -1
  174. package/workflows/principals/publicPrincipalHistoryAuthority.js +60 -0
  175. package/workflows/principals/publicPrincipalHistoryAuthority.js.map +1 -0
  176. package/workflows/principals/publicPrincipalHistoryPage.js +87 -0
  177. package/workflows/principals/publicPrincipalHistoryPage.js.map +1 -0
  178. package/workflows/principals/publicPrincipalHistoryProgress.js +178 -0
  179. package/workflows/principals/publicPrincipalHistoryProgress.js.map +1 -0
  180. package/workflows/principals/publicPrincipalHistoryTypes.d.ts +39 -0
  181. package/workflows/principals/publicPrincipalHistoryTypes.js +2 -0
  182. package/workflows/principals/publicPrincipalHistoryTypes.js.map +1 -0
  183. package/workflows/principals/publicProjectionDirectory.js +40 -0
  184. package/workflows/principals/publicProjectionDirectory.js.map +1 -0
  185. package/workflows/principals/publicProjectionPrincipal.js +53 -0
  186. package/workflows/principals/publicProjectionPrincipal.js.map +1 -0
  187. package/workflows/principals/recoverPrincipalPolicyHistory.js +4 -1
  188. package/workflows/principals/recoverPrincipalPolicyHistory.js.map +1 -1
  189. package/workflows/principals/recoverProjectionPolicyHistory.d.ts +9 -0
  190. package/workflows/principals/recoverProjectionPolicyHistory.js +98 -0
  191. package/workflows/principals/recoverProjectionPolicyHistory.js.map +1 -0
  192. package/workflows/principals/recoverPublicPrincipalHistory.js +176 -0
  193. package/workflows/principals/recoverPublicPrincipalHistory.js.map +1 -0
  194. package/workflows/principals/recoverScopedPrincipalPolicyHistory.js +2 -5
  195. package/workflows/principals/recoverScopedPrincipalPolicyHistory.js.map +1 -1
  196. package/workflows/principals/runtimePolicyRecovery.js +2 -1
  197. package/workflows/principals/runtimePolicyRecovery.js.map +1 -1
  198. package/workflows/principals/runtimePolicyWarmer.js +2 -0
  199. package/workflows/principals/runtimePolicyWarmer.js.map +1 -1
  200. package/workflows/principals/runtimeProjectionPolicyRecovery.js +56 -0
  201. package/workflows/principals/runtimeProjectionPolicyRecovery.js.map +1 -0
  202. package/workflows/principals/selectPublicPrincipalHistory.js +90 -0
  203. package/workflows/principals/selectPublicPrincipalHistory.js.map +1 -0
  204. package/workflows/registration/provisioningBootstrapBuilders.d.ts +21 -153
@@ -1 +1 @@
1
- {"version":3,"file":"organizationFounderPersistence.js","sourceRoot":"","sources":["organizationFounderPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACjC,OAAO,EAEL,8BAA8B,GAC/B,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,CAAC;AACvF,OAAO,EAAgB,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEpE,SAAS,iBAAiB,CACxB,MAAgD,EAChD,OAAiD;IAEjD,IACE,MAAM,CAAC,MAAM,KAAK,OAAO,CAAC,MAAM;QAChC,MAAM,CAAC,qBAAqB,KAAK,OAAO,CAAC,qBAAqB;QAC9D,MAAM,CAAC,gBAAgB,KAAK,OAAO,CAAC,gBAAgB;QAEpD,MAAM,IAAI,uBAAuB,CAC/B,cAAc,EACd,iEAAiE,CAClE,CAAC;AACN,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,OAAgB,EAChB,cAAsB;IAEtB,MAAM,eAAe,CAAC,OAAO,EAAE,sBA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E,EAAE,EAAE,EAAE;QACX,MAAM,CAAC,MAAM,CAAC,GAAG,MAAM,EAAE;aACtB,MAAM,EAAE;aACR,IAAI,CAAC,oBAAoB,CAAC;aAC1B,KAAK,CAAC,EAAE,CAAC,oBAAoB,CAAC,cAAc,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC;aACtE,KAAK,CAAC,CAAC,CAAC,CAAC;QACZ,IAAI,MAAM,EAAE,CAAC;YACX,iBAAiB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;YACnC,OAAO;QACT,CAAC;QACD,MAAM,EAAE;aACL,MAAM,CAAC,oBAAoB,CAAC;aAC5B,MAAM,CAAC,OAAO,CAAC;aACf,mBAAmB,EAAE;aACrB,GAAG,EAAE,CAAC;IACX,CAAC,EACD,EAAE,QAAQ,EAAE,WAAW,EAAE,CAC1B,CAAC;AACJ,CAAC","sourcesContent":["import { KeyingVerificationError } from \"@tearleads/crypto\";\nimport { eq } from \"drizzle-orm\";\nimport {\n type PrincipalPolicyCheckpointEvidence,\n principalPolicyEvidenceEntries,\n} from \"../principals/principalPolicyEvidence\";\nimport { organizationFounders } from \"../sqlite/organizationFounderSchema\";\nimport { keyingCheckpointTables } from \"../sqlite/schema\";\nimport { getClientSQLitePersistenceRuntime } from \"../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../sqlite/sqlSchema\";\n\nfunction assertSameFounder(\n stored: typeof organizationFounders.$inferSelect,\n founder: typeof organizationFounders.$inferInsert,\n): void {\n if (\n stored.userId !== founder.userId ||\n stored.signingKeyFingerprint !== founder.signingKeyFingerprint ||\n stored.genesisStateHash !== founder.genesisStateHash\n )\n throw new KeyingVerificationError(\n \"equivocation\",\n \"Organization founder conflicts with its durable genesis binding\",\n );\n}\n\nexport async function loadOrganizationFounder(\n execSql: ExecSql,\n organizationId: string,\n): Promise<typeof organizationFounders.$inferSelect | null> {\n await ensureSqlTables(execSql, keyingCheckpointTables);\n const runtime = getClientSQLitePersistenceRuntime(execSql);\n const [stored] = await runtime.db\n .select()\n .from(organizationFounders)\n .where(eq(organizationFounders.organizationId, organizationId))\n .limit(1);\n return stored ?? null;\n}\n\n/** Pin verified genesis, never a later administrator or a folder's creator. */\nexport async function rememberOrganizationFounder(input: {\n readonly execSql: ExecSql;\n readonly organization: PrincipalPolicyCheckpointEvidence;\n}): Promise<void> {\n const genesis = principalPolicyEvidenceEntries(input.organization).find(\n ({ state }) => state.version === 1,\n )?.state;\n if (\n genesis?.principalType !== \"organization\" ||\n genesis.prevStateHash !== null\n )\n throw new KeyingVerificationError(\n \"missing_dependency\",\n \"Organization founder requires verified genesis\",\n );\n const founder = {\n organizationId: genesis.principalId,\n userId: genesis.signerUserId,\n signingKeyFingerprint: genesis.signerUserKeyFingerprint,\n genesisStateHash: genesis.stateHash,\n };\n const held = await loadOrganizationFounder(\n input.execSql,\n founder.organizationId,\n );\n if (held) {\n assertSameFounder(held, founder);\n return;\n }\n const runtime = getClientSQLitePersistenceRuntime(input.execSql);\n await runtime.transaction(\n async (tx) => {\n const [stored] = await tx\n .select()\n .from(organizationFounders)\n .where(eq(organizationFounders.organizationId, founder.organizationId))\n .limit(1);\n if (stored) {\n assertSameFounder(stored, founder);\n return;\n }\n await tx\n .insert(organizationFounders)\n .values(founder)\n .onConflictDoNothing()\n .run();\n },\n { behavior: \"immediate\" },\n );\n}\n"]}
1
+ {"version":3,"file":"organizationFounderPersistence.js","sourceRoot":"","sources":["organizationFounderPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,GAExB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,8BAA8B,EAAE,MAAM,uCAAuC,CAAC;AACvF,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,iCAAiC,EAAE,MAAM,oC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AAO,CAAC,WAAW;QACnC,MAAM,EAAE,OAAO,CAAC,YAAY;QAC5B,qBAAqB,EAAE,OAAO,CAAC,wBAAwB;QACvD,gBAAgB,EAAE,OAAO,CAAC,SAAS;KACpC,CAAC;IACF,MAAM,IAAI,GAAG,MAAM,uBAAuB,CACxC,KAAK,CAAC,OAAO,EACb,OAAO,CAAC,cAAc,CACvB,CAAC;IACF,IAAI,IAAI,EAAE,CAAC;QACT,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACjC,OAAO;IACT,CAAC;IACD,MAAM,OAAO,GAAG,iCAAiC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACjE,MAAM,OAAO,CAAC,WAAW,CACvB,KAAK,EAAE,EAAE,EAAE,EAAE;QACX,MAAM,CAAC,MAAM,CAAC,GAAG,MAAM,EAAE;aACtB,MAAM,EAAE;aACR,IAAI,CAAC,oBAAoB,CAAC;aAC1B,KAAK,CAAC,EAAE,CAAC,oBAAoB,CAAC,cAAc,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC;aACtE,KAAK,CAAC,CAAC,CAAC,CAAC;QACZ,IAAI,MAAM,EAAE,CAAC;YACX,iBAAiB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;YACnC,OAAO;QACT,CAAC;QACD,MAAM,EAAE;aACL,MAAM,CAAC,oBAAoB,CAAC;aAC5B,MAAM,CAAC,OAAO,CAAC;aACf,mBAAmB,EAAE;aACrB,GAAG,EAAE,CAAC;IACX,CAAC,EACD,EAAE,QAAQ,EAAE,WAAW,EAAE,CAC1B,CAAC;AACJ,CAAC","sourcesContent":["import {\n KeyingVerificationError,\n type PrincipalPolicyAuthorization,\n} from \"@tearleads/crypto\";\nimport { eq } from \"drizzle-orm\";\nimport { principalPolicyEvidenceEntries } from \"../principals/principalPolicyEvidence\";\nimport { organizationFounders } from \"../sqlite/organizationFounderSchema\";\nimport { keyingCheckpointTables } from \"../sqlite/schema\";\nimport { getClientSQLitePersistenceRuntime } from \"../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../sqlite/sqlSchema\";\n\nfunction assertSameFounder(\n stored: typeof organizationFounders.$inferSelect,\n founder: typeof organizationFounders.$inferInsert,\n): void {\n if (\n stored.userId !== founder.userId ||\n stored.signingKeyFingerprint !== founder.signingKeyFingerprint ||\n stored.genesisStateHash !== founder.genesisStateHash\n )\n throw new KeyingVerificationError(\n \"equivocation\",\n \"Organization founder conflicts with its durable genesis binding\",\n );\n}\n\nexport async function loadOrganizationFounder(\n execSql: ExecSql,\n organizationId: string,\n): Promise<typeof organizationFounders.$inferSelect | null> {\n await ensureSqlTables(execSql, keyingCheckpointTables);\n const runtime = getClientSQLitePersistenceRuntime(execSql);\n const [stored] = await runtime.db\n .select()\n .from(organizationFounders)\n .where(eq(organizationFounders.organizationId, organizationId))\n .limit(1);\n return stored ?? null;\n}\n\n/** Pin verified genesis, never a later administrator or a folder's creator. */\nexport async function rememberOrganizationFounder(input: {\n readonly execSql: ExecSql;\n readonly organization: PrincipalPolicyAuthorization;\n}): Promise<void> {\n const genesis = principalPolicyEvidenceEntries(input.organization).find(\n ({ state }) => state.version === 1,\n )?.state;\n if (\n genesis?.principalType !== \"organization\" ||\n genesis.prevStateHash !== null\n )\n throw new KeyingVerificationError(\n \"missing_dependency\",\n \"Organization founder requires verified genesis\",\n );\n const founder = {\n organizationId: genesis.principalId,\n userId: genesis.signerUserId,\n signingKeyFingerprint: genesis.signerUserKeyFingerprint,\n genesisStateHash: genesis.stateHash,\n };\n const held = await loadOrganizationFounder(\n input.execSql,\n founder.organizationId,\n );\n if (held) {\n assertSameFounder(held, founder);\n return;\n }\n const runtime = getClientSQLitePersistenceRuntime(input.execSql);\n await runtime.transaction(\n async (tx) => {\n const [stored] = await tx\n .select()\n .from(organizationFounders)\n .where(eq(organizationFounders.organizationId, founder.organizationId))\n .limit(1);\n if (stored) {\n assertSameFounder(stored, founder);\n return;\n }\n await tx\n .insert(organizationFounders)\n .values(founder)\n .onConflictDoNothing()\n .run();\n },\n { behavior: \"immediate\" },\n );\n}\n"]}
@@ -0,0 +1,29 @@
1
+ import { KeyingVerificationError, verifyPrincipalPolicyCheckpoint, } from "../../internal/crypto/index.js";
2
+ import { loadStoredPrincipalPolicyCheckpoint } from "./keyingCheckpointPersistence.js";
3
+ export class PrincipalAuthorizationCheckpointUnavailableError extends KeyingVerificationError {
4
+ constructor() {
5
+ super("missing_dependency", "Public authorization cannot connect to the latest durable checkpoint");
6
+ }
7
+ }
8
+ /** Historical capabilities validate trust but never advance current-policy pins. */
9
+ export async function validatePrincipalAuthorizationCheckpoints(tx, policies) {
10
+ for (const policy of policies) {
11
+ const checkpoint = await loadStoredPrincipalPolicyCheckpoint(tx, policy);
12
+ if (!checkpoint)
13
+ continue;
14
+ const matchingHead = policies.filter((other) => other.principalType === policy.principalType &&
15
+ other.principalId === policy.principalId &&
16
+ other.stateHash === policy.stateHash);
17
+ const chain = matchingHead.flatMap((other) => other.retainedHistory);
18
+ if (checkpoint.version > policy.version ||
19
+ (checkpoint.version < policy.version &&
20
+ !chain.some(({ state }) => state.version === checkpoint.version)))
21
+ throw new PrincipalAuthorizationCheckpointUnavailableError();
22
+ verifyPrincipalPolicyCheckpoint({
23
+ chain,
24
+ currentState: policy.state,
25
+ localCheckpoint: checkpoint,
26
+ });
27
+ }
28
+ }
29
+ //# sourceMappingURL=principalAuthorizationCheckpoints.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"principalAuthorizationCheckpoints.js","sourceRoot":"","sources":["principalAuthorizationCheckpoints.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EAEvB,+BAA+B,GAChC,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,mCAAmC,EAAE,MAAM,+BAA+B,CAAC;AAEpF,MAAM,OAAO,gDAAiD,SAAQ,uBAAuB;IAC3F;QACE,KAAK,CACH,oBAAoB,EACpB,sEAAsE,CACvE,CAAC;IACJ,CAAC;CACF;AAED,oFAAoF;AACpF,MAAM,CAAC,KAAK,UAAU,yCAAyC,CAC7D,EAAgC,EAChC,QAAqD;IAErD,KAAK,MAAM,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC9B,MAAM,UAAU,GAAG,MAAM,mCAAmC,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;QACzE,IAAI,CAAC,UAAU;YAAE,SAAS;QAC1B,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAClC,CAAC,KAAK,EAAE,EAAE,CACR,KAAK,CAAC,aAAa,KAAK,MAAM,CAAC,aAAa;YAC5C,KAAK,CAAC,WAAW,KAAK,MAAM,CAAC,WAAW;YACxC,KAAK,CAAC,SAAS,KAAK,MAAM,CAAC,SAAS,CACvC,CAAC;QACF,MAAM,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;QACrE,IACE,UAAU,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO;YACnC,CAAC,UAAU,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO;gBAClC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,KAAK,UAAU,CAAC,OAAO,CAAC,CAAC;YAEnE,MAAM,IAAI,gDAAgD,EAAE,CAAC;QAC/D,+BAA+B,CAAC;YAC9B,KAAK;YACL,YAAY,EAAE,MAAM,CAAC,KAAK;YAC1B,eAAe,EAAE,UAAU;SAC5B,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import {\n KeyingVerificationError,\n type VerifiedPrincipalPolicySelection,\n verifyPrincipalPolicyCheckpoint,\n} from \"@tearleads/crypto\";\nimport type { ClientSQLiteTransactionScope } from \"../sqlite/sqlitePersistenceRuntime\";\nimport { loadStoredPrincipalPolicyCheckpoint } from \"./keyingCheckpointPersistence\";\n\nexport class PrincipalAuthorizationCheckpointUnavailableError extends KeyingVerificationError {\n constructor() {\n super(\n \"missing_dependency\",\n \"Public authorization cannot connect to the latest durable checkpoint\",\n );\n }\n}\n\n/** Historical capabilities validate trust but never advance current-policy pins. */\nexport async function validatePrincipalAuthorizationCheckpoints(\n tx: ClientSQLiteTransactionScope,\n policies: readonly VerifiedPrincipalPolicySelection[],\n): Promise<void> {\n for (const policy of policies) {\n const checkpoint = await loadStoredPrincipalPolicyCheckpoint(tx, policy);\n if (!checkpoint) continue;\n const matchingHead = policies.filter(\n (other) =>\n other.principalType === policy.principalType &&\n other.principalId === policy.principalId &&\n other.stateHash === policy.stateHash,\n );\n const chain = matchingHead.flatMap((other) => other.retainedHistory);\n if (\n checkpoint.version > policy.version ||\n (checkpoint.version < policy.version &&\n !chain.some(({ state }) => state.version === checkpoint.version))\n )\n throw new PrincipalAuthorizationCheckpointUnavailableError();\n verifyPrincipalPolicyCheckpoint({\n chain,\n currentState: policy.state,\n localCheckpoint: checkpoint,\n });\n }\n}\n"]}
@@ -0,0 +1,30 @@
1
+ import { type PrincipalHistoryIndexNode, type PrincipalPolicyHistoryReferenceProof, type ReferencedPrincipalHead, type VerifiedPrincipalPolicyHistory } from "../../internal/crypto/index.js";
2
+ import { type PrincipalPolicyStateChainEntryResponse } from "../../internal/validators/response/index.js";
3
+ import { type ClientSQLiteTransactionScope } from "../sqlite/sqlitePersistenceRuntime.js";
4
+ import { type ExecSql } from "../sqlite/sqlSchema.js";
5
+ export interface PrincipalHistoryEvidencePage {
6
+ readonly scopeId: string;
7
+ readonly organizationId: string;
8
+ readonly entries: readonly {
9
+ readonly leafHash: string;
10
+ readonly entryJson: string;
11
+ }[];
12
+ readonly nodes: readonly PrincipalHistoryIndexNode[];
13
+ }
14
+ /** Prepare only a page accepted by the signature/authorization verifier. */
15
+ export declare function preparePrincipalHistoryEvidencePage(input: {
16
+ readonly scopeId: string;
17
+ readonly organizationId: string;
18
+ readonly entries: readonly PrincipalPolicyStateChainEntryResponse[];
19
+ readonly nodes: readonly PrincipalHistoryIndexNode[];
20
+ }): Promise<PrincipalHistoryEvidencePage>;
21
+ /** Publish in the same guarded transaction as the accepted page's progress. */
22
+ export declare function writePrincipalHistoryEvidencePage(tx: ClientSQLiteTransactionScope, page: PrincipalHistoryEvidencePage): Promise<void>;
23
+ /** Returns untrusted material. The caller must verify it against the issued root. */
24
+ export declare function loadPrincipalHistoryReference(input: {
25
+ readonly execSql: ExecSql;
26
+ readonly scopeId: string;
27
+ readonly history: VerifiedPrincipalPolicyHistory;
28
+ readonly version: number;
29
+ readonly expectedReference?: ReferencedPrincipalHead | undefined;
30
+ }): Promise<PrincipalPolicyHistoryReferenceProof>;
@@ -0,0 +1,24 @@
1
+ import { principalHistoryPrefixes } from "../sqlite/principalHistoryEvidenceSchema.js";
2
+ import { type ExecSql } from "../sqlite/sqlSchema.js";
3
+ export type PrincipalHistoryPrefix = typeof principalHistoryPrefixes.$inferSelect;
4
+ export declare function loadPrincipalHistoryPrefix(execSql: ExecSql, scopeId: string): Promise<{
5
+ scopeId: string;
6
+ organizationId: string;
7
+ version: number;
8
+ headJson: string;
9
+ currentJson: string;
10
+ progress: string;
11
+ } | null>;
12
+ /** Keep the newest prefix unless replay replaces the exact rejected candidate. */
13
+ export declare function savePrincipalHistoryPrefix(input: {
14
+ readonly execSql: ExecSql;
15
+ readonly prefix: PrincipalHistoryPrefix;
16
+ readonly stillCurrent: () => boolean;
17
+ readonly rejectedPrefix?: PrincipalHistoryPrefix | null;
18
+ }): Promise<void>;
19
+ /** Invalid hints are disposable, but a losing reader must preserve a newer hint. */
20
+ export declare function discardPrincipalHistoryPrefix(input: {
21
+ readonly execSql: ExecSql;
22
+ readonly prefix: PrincipalHistoryPrefix;
23
+ readonly stillCurrent: () => boolean;
24
+ }): Promise<void>;
@@ -13,9 +13,10 @@ export async function loadPrincipalHistoryPrefix(execSql, scopeId) {
13
13
  .limit(1);
14
14
  return prefix ?? null;
15
15
  }
16
- /** Keep one completed prefix per principal/trust scope; older readers cannot replace it. */
16
+ /** Keep the newest prefix unless replay replaces the exact rejected candidate. */
17
17
  export async function savePrincipalHistoryPrefix(input) {
18
18
  const prefix = { ...input.prefix };
19
+ const rejected = input.rejectedPrefix ? { ...input.rejectedPrefix } : null;
19
20
  await ensureSqlTables(input.execSql, principalHistoryEvidenceTables);
20
21
  const runtime = getClientSQLitePersistenceRuntime(input.execSql);
21
22
  const result = await runtime.guardedTransaction(async (tx) => {
@@ -24,7 +25,10 @@ export async function savePrincipalHistoryPrefix(input) {
24
25
  .from(principalHistoryPrefixes)
25
26
  .where(eq(principalHistoryPrefixes.scopeId, prefix.scopeId))
26
27
  .limit(1);
27
- if (previous && previous.version > prefix.version)
28
+ if (previous &&
29
+ previous.version > prefix.version &&
30
+ !(rejected?.scopeId === previous.scopeId &&
31
+ rejected.progress === previous.progress))
28
32
  return;
29
33
  await tx
30
34
  .insert(principalHistoryPrefixes)
@@ -1 +1 @@
1
- {"version":3,"file":"principalHistoryPrefixPersistence.js","sourceRoot":"","sources":["principalHistoryPrefixPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,mCAAmC,EAAE,MAAM,uCAAuC,CAAC;AAC5F,OAAO,EACL,8BAA8B,EAC9B,wBAAwB,GACzB,MAAM,0CAA0C,CAAC;AAClD,OAAO,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,CAAC;AACvF,OAAO,EAAgB,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAKpE,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC9C,OAAgB,EAChB,OAAe;IAEf,MAAM,eAAe,CAAC,OAAO,EAAE,8BAA8B,CAAC,CAAC;IAC/D,MAAM,EAAE,EAAE,EAAE,GAAG,iCAAiC,CAAC,OAAO,CAAC,CAAC;IAC1D,MAAM,CAAC,MAAM,CAAC,GAAG,MAAM,EAAE;SACtB,MAAM,EAAE;SACR,IAAI,CAAC,wBAAwB,CAAC;SA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AAC,KAInD;IACC,MAAM,MAAM,GAAG,EAAE,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;IACnC,MAAM,OAAO,GAAG,iCAAiC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACjE,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,kBAAkB,CAC7C,KAAK,EAAE,EAAE,EAAE,EAAE;QACX,MAAM,EAAE;aACL,MAAM,CAAC,wBAAwB,CAAC;aAChC,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,wBAAwB,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,EACpD,EAAE,CAAC,wBAAwB,CAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,CACvD,CACF;aACA,GAAG,EAAE,CAAC;IACX,CAAC,EACD,KAAK,CAAC,YAAY,EAClB,EAAE,QAAQ,EAAE,WAAW,EAAE,CAC1B,CAAC;IACF,mCAAmC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import { and, eq } from \"drizzle-orm\";\nimport { assertProjectionVerificationCurrent } from \"../keyingProjectionVerification/types\";\nimport {\n principalHistoryEvidenceTables,\n principalHistoryPrefixes,\n} from \"../sqlite/principalHistoryEvidenceSchema\";\nimport { getClientSQLitePersistenceRuntime } from \"../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../sqlite/sqlSchema\";\n\nexport type PrincipalHistoryPrefix =\n typeof principalHistoryPrefixes.$inferSelect;\n\nexport async function loadPrincipalHistoryPrefix(\n execSql: ExecSql,\n scopeId: string,\n) {\n await ensureSqlTables(execSql, principalHistoryEvidenceTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const [prefix] = await db\n .select()\n .from(principalHistoryPrefixes)\n .where(eq(principalHistoryPrefixes.scopeId, scopeId))\n .limit(1);\n return prefix ?? null;\n}\n\n/** Keep one completed prefix per principal/trust scope; older readers cannot replace it. */\nexport async function savePrincipalHistoryPrefix(input: {\n readonly execSql: ExecSql;\n readonly prefix: PrincipalHistoryPrefix;\n readonly stillCurrent: () => boolean;\n}): Promise<void> {\n const prefix = { ...input.prefix };\n await ensureSqlTables(input.execSql, principalHistoryEvidenceTables);\n const runtime = getClientSQLitePersistenceRuntime(input.execSql);\n const result = await runtime.guardedTransaction(\n async (tx) => {\n const [previous] = await tx\n .select()\n .from(principalHistoryPrefixes)\n .where(eq(principalHistoryPrefixes.scopeId, prefix.scopeId))\n .limit(1);\n if (previous && previous.version > prefix.version) return;\n await tx\n .insert(principalHistoryPrefixes)\n .values(prefix)\n .onConflictDoUpdate({\n target: principalHistoryPrefixes.scopeId,\n set: prefix,\n })\n .run();\n },\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => result.committed);\n}\n\n/** Invalid hints are disposable, but a losing reader must preserve a newer hint. */\nexport async function discardPrincipalHistoryPrefix(input: {\n readonly execSql: ExecSql;\n readonly prefix: PrincipalHistoryPrefix;\n readonly stillCurrent: () => boolean;\n}): Promise<void> {\n const prefix = { ...input.prefix };\n const runtime = getClientSQLitePersistenceRuntime(input.execSql);\n const result = await runtime.guardedTransaction(\n async (tx) => {\n await tx\n .delete(principalHistoryPrefixes)\n .where(\n and(\n eq(principalHistoryPrefixes.scopeId, prefix.scopeId),\n eq(principalHistoryPrefixes.progress, prefix.progress),\n ),\n )\n .run();\n },\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => result.committed);\n}\n"]}
1
+ {"version":3,"file":"principalHistoryPrefixPersistence.js","sourceRoot":"","sources":["principalHistoryPrefixPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,mCAAmC,EAAE,MAAM,uCAAuC,CAAC;AAC5F,OAAO,EACL,8BAA8B,EAC9B,wBAAwB,GACzB,MAAM,0CAA0C,CAAC;AAClD,OA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AAM,CAAC,SAAS,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import { and, eq } from \"drizzle-orm\";\nimport { assertProjectionVerificationCurrent } from \"../keyingProjectionVerification/types\";\nimport {\n principalHistoryEvidenceTables,\n principalHistoryPrefixes,\n} from \"../sqlite/principalHistoryEvidenceSchema\";\nimport { getClientSQLitePersistenceRuntime } from \"../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../sqlite/sqlSchema\";\n\nexport type PrincipalHistoryPrefix =\n typeof principalHistoryPrefixes.$inferSelect;\n\nexport async function loadPrincipalHistoryPrefix(\n execSql: ExecSql,\n scopeId: string,\n) {\n await ensureSqlTables(execSql, principalHistoryEvidenceTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const [prefix] = await db\n .select()\n .from(principalHistoryPrefixes)\n .where(eq(principalHistoryPrefixes.scopeId, scopeId))\n .limit(1);\n return prefix ?? null;\n}\n\n/** Keep the newest prefix unless replay replaces the exact rejected candidate. */\nexport async function savePrincipalHistoryPrefix(input: {\n readonly execSql: ExecSql;\n readonly prefix: PrincipalHistoryPrefix;\n readonly stillCurrent: () => boolean;\n readonly rejectedPrefix?: PrincipalHistoryPrefix | null;\n}): Promise<void> {\n const prefix = { ...input.prefix };\n const rejected = input.rejectedPrefix ? { ...input.rejectedPrefix } : null;\n await ensureSqlTables(input.execSql, principalHistoryEvidenceTables);\n const runtime = getClientSQLitePersistenceRuntime(input.execSql);\n const result = await runtime.guardedTransaction(\n async (tx) => {\n const [previous] = await tx\n .select()\n .from(principalHistoryPrefixes)\n .where(eq(principalHistoryPrefixes.scopeId, prefix.scopeId))\n .limit(1);\n if (\n previous &&\n previous.version > prefix.version &&\n !(\n rejected?.scopeId === previous.scopeId &&\n rejected.progress === previous.progress\n )\n )\n return;\n await tx\n .insert(principalHistoryPrefixes)\n .values(prefix)\n .onConflictDoUpdate({\n target: principalHistoryPrefixes.scopeId,\n set: prefix,\n })\n .run();\n },\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => result.committed);\n}\n\n/** Invalid hints are disposable, but a losing reader must preserve a newer hint. */\nexport async function discardPrincipalHistoryPrefix(input: {\n readonly execSql: ExecSql;\n readonly prefix: PrincipalHistoryPrefix;\n readonly stillCurrent: () => boolean;\n}): Promise<void> {\n const prefix = { ...input.prefix };\n const runtime = getClientSQLitePersistenceRuntime(input.execSql);\n const result = await runtime.guardedTransaction(\n async (tx) => {\n await tx\n .delete(principalHistoryPrefixes)\n .where(\n and(\n eq(principalHistoryPrefixes.scopeId, prefix.scopeId),\n eq(principalHistoryPrefixes.progress, prefix.progress),\n ),\n )\n .run();\n },\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => result.committed);\n}\n"]}
@@ -0,0 +1,21 @@
1
+ import { principalHistoryStages } from "../sqlite/principalHistoryStageSchema.js";
2
+ import { type ExecSql } from "../sqlite/sqlSchema.js";
3
+ import { type PrincipalHistoryEvidencePage } from "./principalHistoryEvidencePersistence.js";
4
+ export type PrincipalHistoryStage = typeof principalHistoryStages.$inferSelect;
5
+ export declare function loadPrincipalHistoryStage(execSql: ExecSql, id: string): Promise<{
6
+ id: string;
7
+ organizationId: string;
8
+ currentJson: string;
9
+ afterVersion: number;
10
+ complete: boolean;
11
+ progress: string;
12
+ } | null>;
13
+ /** Publish accepted entries and index nodes atomically with authenticated progress. */
14
+ export declare function savePrincipalHistoryStage(input: {
15
+ readonly execSql: ExecSql;
16
+ readonly stage: PrincipalHistoryStage;
17
+ readonly evidence: PrincipalHistoryEvidencePage;
18
+ readonly previousProgress: string | null;
19
+ readonly stillCurrent: () => boolean;
20
+ }): Promise<void>;
21
+ export declare function discardPrincipalHistoryStage(execSql: ExecSql, stage: PrincipalHistoryStage, stillCurrent: () => boolean): Promise<void>;
@@ -0,0 +1,12 @@
1
+ const leases = new WeakMap();
2
+ /** Internal workflow adapters carry custody without exposing it on public views. */
3
+ export function readPrincipalHistoryProtection(runtime) {
4
+ return leases.get(runtime) ?? runtime.withPrincipalHistoryProtection;
5
+ }
6
+ export function inheritPrincipalHistoryProtection(source, target) {
7
+ const lease = readPrincipalHistoryProtection(source);
8
+ if (lease)
9
+ leases.set(target, lease);
10
+ return target;
11
+ }
12
+ //# sourceMappingURL=principalHistoryRuntime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"principalHistoryRuntime.js","sourceRoot":"","sources":["principalHistoryRuntime.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,GAAG,IAAI,OAAO,EAA2C,CAAC;AAEtE,oFAAoF;AACpF,MAAM,UAAU,8BAA8B,CAC5C,OAIC;IAED,OAAO,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,8BAA8B,CAAC;AACvE,CAAC;AAED,MAAM,UAAU,iCAAiC,CAC/C,MAAc,EACd,MAAS;IAET,MAAM,KAAK,GAAG,8BAA8B,CAAC,MAAM,CAAC,CAAC;IACrD,IAAI,KAAK;QAAE,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACrC,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { PrincipalHistoryProtectionLease } from \"./principalHistoryProtection\";\n\nconst leases = new WeakMap<object, PrincipalHistoryProtectionLease>();\n\n/** Internal workflow adapters carry custody without exposing it on public views. */\nexport function readPrincipalHistoryProtection(\n runtime: object & {\n readonly withPrincipalHistoryProtection?:\n | PrincipalHistoryProtectionLease\n | undefined;\n },\n) {\n return leases.get(runtime) ?? runtime.withPrincipalHistoryProtection;\n}\n\nexport function inheritPrincipalHistoryProtection<T extends object>(\n source: object,\n target: T,\n): T {\n const lease = readPrincipalHistoryProtection(source);\n if (lease) leases.set(target, lease);\n return target;\n}\n"]}
@@ -1,8 +1,8 @@
1
1
  import { serializeKeyingCanonicalJson, toFingerprint, } from "../../internal/crypto/index.js";
2
2
  import { isPrincipalPolicyPageResponse } from "../../internal/validators/response/index.js";
3
- export function principalHistoryStageId(organizationId, head, context, retainedReferences) {
3
+ export function principalHistoryStageId(organizationId, head, context) {
4
4
  return toFingerprint(new TextEncoder().encode(serializeKeyingCanonicalJson([
5
- "tearleads.sdk.principal-history-stage.v1",
5
+ "tearleads.sdk.principal-history-stage.v2",
6
6
  organizationId,
7
7
  head.principalType,
8
8
  head.principalId,
@@ -11,16 +11,6 @@ export function principalHistoryStageId(organizationId, head, context, retainedR
11
11
  head.stateHash,
12
12
  head.keyFingerprint,
13
13
  context,
14
- [...retainedReferences]
15
- .sort((a, b) => a.version - b.version)
16
- .map((reference) => [
17
- reference.principalType,
18
- reference.principalId,
19
- reference.version,
20
- reference.keyEpoch,
21
- reference.stateHash,
22
- reference.keyFingerprint,
23
- ]),
24
14
  ])));
25
15
  }
26
16
  export async function principalHistoryStageProtection(protection, expectedHead, stage) {
@@ -1 +1 @@
1
- {"version":3,"file":"principalHistoryStageProtection.js","sourceRoot":"","sources":["principalHistoryStageProtection.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,4BAA4B,EAC5B,aAAa,GACd,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAG/E,MAAM,UAAU,uBAAuB,CACrC,cAAsB,EACtB,IAA6B,EAC7B,OAAe,EACf,kBAAsD;IAEtD,OAAO,aAAa,CAClB,IAAI,WAAW,EAAE,CAAC,MAAM,CACtB,4BAA4B,CAAC;QAC3B,0CAA0C;QAC1C,cAAc;QACd,IAAI,CAAC,aAAa;QAClB,IAAI,CAAC,WAAW;QAChB,IAAI,CAAC,OAAO;QACZ,IAAI,CAAC,QAAQ;QACb,IAAI,CAAC,SAAS;QACd,IAAI,CAAC,cAAc;QACnB,OAAO;QACP,CAAC,GAAG,kBAAkB,CAAC;aACpB,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC;aACrC,GAAG,C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YAAY,EAAE,gBAAgB,EAAE,IAAI,EAAE;KAC1E,CAAC;IACF,IACE,CAAC,6BAA6B,CAAC,IAAI,CAAC;QACpC,KAAK,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,CAAC,OAAO;QAE/C,OAAO,IAAI,CAAC;IACd,OAAO;QACL,YAAY,EAAE,IAAI,CAAC,YAAY;QAC/B,cAAc,EAAE,IAAI,CAAC,cAAc;QACnC,iBAAiB,EAAE,IAAI,CAAC,iBAAiB;QACzC,aAAa,EAAE,IAAI,CAAC,aAAa;QACjC,sBAAsB,EAAE,IAAI,CAAC,sBAAsB;KACpD,CAAC;AACJ,CAAC","sourcesContent":["import type { PrincipalPolicyPageCurrent } from \"@tearleads/api-client\";\nimport {\n type PrincipalPolicyHistoryProgressOptions,\n type ReferencedPrincipalHead,\n serializeKeyingCanonicalJson,\n toFingerprint,\n} from \"@tearleads/crypto\";\nimport { isPrincipalPolicyPageResponse } from \"@tearleads/validators/response\";\nimport type { PrincipalHistoryStage } from \"../persistence/principalHistoryStagePersistence\";\n\nexport function principalHistoryStageId(\n organizationId: string,\n head: ReferencedPrincipalHead,\n context: string,\n retainedReferences: readonly ReferencedPrincipalHead[],\n) {\n return toFingerprint(\n new TextEncoder().encode(\n serializeKeyingCanonicalJson([\n \"tearleads.sdk.principal-history-stage.v1\",\n organizationId,\n head.principalType,\n head.principalId,\n head.version,\n head.keyEpoch,\n head.stateHash,\n head.keyFingerprint,\n context,\n [...retainedReferences]\n .sort((a, b) => a.version - b.version)\n .map((reference) => [\n reference.principalType,\n reference.principalId,\n reference.version,\n reference.keyEpoch,\n reference.stateHash,\n reference.keyFingerprint,\n ]),\n ]),\n ),\n );\n}\n\nexport async function principalHistoryStageProtection(\n protection: PrincipalPolicyHistoryProgressOptions,\n expectedHead: ReferencedPrincipalHead,\n stage: Omit<PrincipalHistoryStage, \"progress\">,\n): Promise<PrincipalPolicyHistoryProgressOptions> {\n return {\n localKey: protection.localKey,\n context: serializeKeyingCanonicalJson({\n domain: \"tearleads.sdk.principal-history-stage.v1\",\n context: protection.context,\n id: stage.id,\n organizationId: stage.organizationId,\n expectedHead: { ...expectedHead },\n afterVersion: stage.afterVersion,\n complete: stage.complete,\n currentDigest: await toFingerprint(\n new TextEncoder().encode(stage.currentJson),\n ),\n }),\n };\n}\n\nexport function parsePrincipalHistoryStageCurrent(\n stage: Pick<PrincipalHistoryStage, \"currentJson\" | \"afterVersion\">,\n): PrincipalPolicyPageCurrent | null {\n let current: unknown;\n try {\n current = JSON.parse(stage.currentJson);\n } catch {\n return null;\n }\n if (!current || typeof current !== \"object\" || Array.isArray(current))\n return null;\n const page = {\n ...current,\n previousStates: [],\n historyPage: { afterVersion: stage.afterVersion, nextAfterVersion: null },\n };\n if (\n !isPrincipalPolicyPageResponse(page) ||\n stage.afterVersion >= page.currentState.version\n )\n return null;\n return {\n currentState: page.currentState,\n currentPayload: page.currentPayload,\n currentProjection: page.currentProjection,\n currentGrants: page.currentGrants,\n currentMemberEnvelopes: page.currentMemberEnvelopes,\n };\n}\n"]}
1
+ {"version":3,"file":"principalHistoryStageProtection.js","sourceRoot":"","sources":["principalHistoryStageProtection.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,4BAA4B,EAC5B,aAAa,GACd,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAG/E,MAAM,UAAU,uBAAuB,CACrC,cAAsB,EACtB,IAA6B,EAC7B,OAAe;IAEf,OAAO,aAAa,CAClB,IAAI,WAAW,EAAE,CAAC,MAAM,CACtB,4BAA4B,CAAC;QAC3B,0CAA0C;QAC1C,cAAc;QACd,IAAI,CAAC,aAAa;QAClB,IAAI,CAAC,WAAW;QAChB,IAAI,CAAC,OAAO;QACZ,IAAI,CAAC,QAAQ;QACb,IAAI,CAAC,SAAS;QACd,IAAI,CAAC,cAAc;QACnB,OAAO;KACR,CAAC,CACH,CACF,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,K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,CAAC,cAAc;QACnC,iBAAiB,EAAE,IAAI,CAAC,iBAAiB;QACzC,aAAa,EAAE,IAAI,CAAC,aAAa;QACjC,sBAAsB,EAAE,IAAI,CAAC,sBAAsB;KACpD,CAAC;AACJ,CAAC","sourcesContent":["import type { PrincipalPolicyPageCurrent } from \"@tearleads/api-client\";\nimport {\n type PrincipalPolicyHistoryProgressOptions,\n type ReferencedPrincipalHead,\n serializeKeyingCanonicalJson,\n toFingerprint,\n} from \"@tearleads/crypto\";\nimport { isPrincipalPolicyPageResponse } from \"@tearleads/validators/response\";\nimport type { PrincipalHistoryStage } from \"../persistence/principalHistoryStagePersistence\";\n\nexport function principalHistoryStageId(\n organizationId: string,\n head: ReferencedPrincipalHead,\n context: string,\n) {\n return toFingerprint(\n new TextEncoder().encode(\n serializeKeyingCanonicalJson([\n \"tearleads.sdk.principal-history-stage.v2\",\n organizationId,\n head.principalType,\n head.principalId,\n head.version,\n head.keyEpoch,\n head.stateHash,\n head.keyFingerprint,\n context,\n ]),\n ),\n );\n}\n\nexport async function principalHistoryStageProtection(\n protection: PrincipalPolicyHistoryProgressOptions,\n expectedHead: ReferencedPrincipalHead,\n stage: Omit<PrincipalHistoryStage, \"progress\">,\n): Promise<PrincipalPolicyHistoryProgressOptions> {\n return {\n localKey: protection.localKey,\n context: serializeKeyingCanonicalJson({\n domain: \"tearleads.sdk.principal-history-stage.v1\",\n context: protection.context,\n id: stage.id,\n organizationId: stage.organizationId,\n expectedHead: { ...expectedHead },\n afterVersion: stage.afterVersion,\n complete: stage.complete,\n currentDigest: await toFingerprint(\n new TextEncoder().encode(stage.currentJson),\n ),\n }),\n };\n}\n\nexport function parsePrincipalHistoryStageCurrent(\n stage: Pick<PrincipalHistoryStage, \"currentJson\" | \"afterVersion\">,\n): PrincipalPolicyPageCurrent | null {\n let current: unknown;\n try {\n current = JSON.parse(stage.currentJson);\n } catch {\n return null;\n }\n if (!current || typeof current !== \"object\" || Array.isArray(current))\n return null;\n const page = {\n ...current,\n previousStates: [],\n historyPage: { afterVersion: stage.afterVersion, nextAfterVersion: null },\n };\n if (\n !isPrincipalPolicyPageResponse(page) ||\n stage.afterVersion >= page.currentState.version\n )\n return null;\n return {\n currentState: page.currentState,\n currentPayload: page.currentPayload,\n currentProjection: page.currentProjection,\n currentGrants: page.currentGrants,\n currentMemberEnvelopes: page.currentMemberEnvelopes,\n };\n}\n"]}
@@ -1,4 +1,4 @@
1
- import type { AnyVerifiedPrincipalPolicy, VerifiedPrincipalPolicy, VerifiedPrincipalPolicyCurrent } from "../../internal/crypto/index.js";
1
+ import type { AnyVerifiedPrincipalPolicy, PrincipalPolicyAuthorization, VerifiedPrincipalPolicy, VerifiedPrincipalPolicyCurrent } from "../../internal/crypto/index.js";
2
2
  export type PrincipalPolicyCurrentEvidence = VerifiedPrincipalPolicy | VerifiedPrincipalPolicyCurrent;
3
3
  export type PrincipalPolicyCheckpointEvidence = AnyVerifiedPrincipalPolicy | VerifiedPrincipalPolicyCurrent;
4
- export declare function principalPolicyEvidenceEntries(policy: PrincipalPolicyCheckpointEvidence): NonNullable<VerifiedPrincipalPolicy["history"]>;
4
+ export declare function principalPolicyEvidenceEntries(policy: PrincipalPolicyAuthorization): NonNullable<VerifiedPrincipalPolicy["history"]>;
@@ -1 +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"]}
1
+ {"version":3,"file":"principalPolicyEvidence.js","sourceRoot":"","sources":["principalPolicyEvidence.ts"],"names":[],"mappings":"AAcA,MAAM,UAAU,8BAA8B,CAC5C,MAAoC;IAEpC,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 PrincipalPolicyAuthorization,\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: PrincipalPolicyAuthorization,\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,308 @@
1
+ /** A completed, locally authenticated prefix; never an application trust pin. */
2
+ export declare const principalHistoryPrefixes: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
3
+ name: "principal_history_prefixes";
4
+ schema: undefined;
5
+ columns: {
6
+ scopeId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
7
+ name: "scope_id";
8
+ tableName: "principal_history_prefixes";
9
+ dataType: "string";
10
+ columnType: "SQLiteText";
11
+ data: string;
12
+ driverParam: string;
13
+ notNull: true;
14
+ hasDefault: false;
15
+ isPrimaryKey: true;
16
+ isAutoincrement: false;
17
+ hasRuntimeDefault: false;
18
+ enumValues: [string, ...string[]];
19
+ baseColumn: never;
20
+ identity: undefined;
21
+ generated: undefined;
22
+ }, {}, {
23
+ length: number | undefined;
24
+ }>;
25
+ organizationId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
26
+ name: "organization_id";
27
+ tableName: "principal_history_prefixes";
28
+ dataType: "string";
29
+ columnType: "SQLiteText";
30
+ data: string;
31
+ driverParam: string;
32
+ notNull: true;
33
+ hasDefault: false;
34
+ isPrimaryKey: false;
35
+ isAutoincrement: false;
36
+ hasRuntimeDefault: false;
37
+ enumValues: [string, ...string[]];
38
+ baseColumn: never;
39
+ identity: undefined;
40
+ generated: undefined;
41
+ }, {}, {
42
+ length: number | undefined;
43
+ }>;
44
+ version: import("drizzle-orm/sqlite-core").SQLiteColumn<{
45
+ name: "version";
46
+ tableName: "principal_history_prefixes";
47
+ dataType: "number";
48
+ columnType: "SQLiteInteger";
49
+ data: number;
50
+ driverParam: number;
51
+ notNull: true;
52
+ hasDefault: false;
53
+ isPrimaryKey: false;
54
+ isAutoincrement: false;
55
+ hasRuntimeDefault: false;
56
+ enumValues: undefined;
57
+ baseColumn: never;
58
+ identity: undefined;
59
+ generated: undefined;
60
+ }, {}, {}>;
61
+ headJson: import("drizzle-orm/sqlite-core").SQLiteColumn<{
62
+ name: "head_json";
63
+ tableName: "principal_history_prefixes";
64
+ dataType: "string";
65
+ columnType: "SQLiteText";
66
+ data: string;
67
+ driverParam: string;
68
+ notNull: true;
69
+ hasDefault: false;
70
+ isPrimaryKey: false;
71
+ isAutoincrement: false;
72
+ hasRuntimeDefault: false;
73
+ enumValues: [string, ...string[]];
74
+ baseColumn: never;
75
+ identity: undefined;
76
+ generated: undefined;
77
+ }, {}, {
78
+ length: number | undefined;
79
+ }>;
80
+ currentJson: import("drizzle-orm/sqlite-core").SQLiteColumn<{
81
+ name: "current_json";
82
+ tableName: "principal_history_prefixes";
83
+ dataType: "string";
84
+ columnType: "SQLiteText";
85
+ data: string;
86
+ driverParam: string;
87
+ notNull: true;
88
+ hasDefault: false;
89
+ isPrimaryKey: false;
90
+ isAutoincrement: false;
91
+ hasRuntimeDefault: false;
92
+ enumValues: [string, ...string[]];
93
+ baseColumn: never;
94
+ identity: undefined;
95
+ generated: undefined;
96
+ }, {}, {
97
+ length: number | undefined;
98
+ }>;
99
+ progress: import("drizzle-orm/sqlite-core").SQLiteColumn<{
100
+ name: "progress";
101
+ tableName: "principal_history_prefixes";
102
+ dataType: "string";
103
+ columnType: "SQLiteText";
104
+ data: string;
105
+ driverParam: string;
106
+ notNull: true;
107
+ hasDefault: false;
108
+ isPrimaryKey: false;
109
+ isAutoincrement: false;
110
+ hasRuntimeDefault: false;
111
+ enumValues: [string, ...string[]];
112
+ baseColumn: never;
113
+ identity: undefined;
114
+ generated: undefined;
115
+ }, {}, {
116
+ length: number | undefined;
117
+ }>;
118
+ };
119
+ dialect: "sqlite";
120
+ }>;
121
+ /** Entries are selected by their exact signed leaf, including signature bytes. */
122
+ export declare const principalHistoryEntries: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
123
+ name: "principal_history_entries";
124
+ schema: undefined;
125
+ columns: {
126
+ scopeId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
127
+ name: "scope_id";
128
+ tableName: "principal_history_entries";
129
+ dataType: "string";
130
+ columnType: "SQLiteText";
131
+ data: string;
132
+ driverParam: string;
133
+ notNull: true;
134
+ hasDefault: false;
135
+ isPrimaryKey: false;
136
+ isAutoincrement: false;
137
+ hasRuntimeDefault: false;
138
+ enumValues: [string, ...string[]];
139
+ baseColumn: never;
140
+ identity: undefined;
141
+ generated: undefined;
142
+ }, {}, {
143
+ length: number | undefined;
144
+ }>;
145
+ organizationId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
146
+ name: "organization_id";
147
+ tableName: "principal_history_entries";
148
+ dataType: "string";
149
+ columnType: "SQLiteText";
150
+ data: string;
151
+ driverParam: string;
152
+ notNull: true;
153
+ hasDefault: false;
154
+ isPrimaryKey: false;
155
+ isAutoincrement: false;
156
+ hasRuntimeDefault: false;
157
+ enumValues: [string, ...string[]];
158
+ baseColumn: never;
159
+ identity: undefined;
160
+ generated: undefined;
161
+ }, {}, {
162
+ length: number | undefined;
163
+ }>;
164
+ leafHash: import("drizzle-orm/sqlite-core").SQLiteColumn<{
165
+ name: "leaf_hash";
166
+ tableName: "principal_history_entries";
167
+ dataType: "string";
168
+ columnType: "SQLiteText";
169
+ data: string;
170
+ driverParam: string;
171
+ notNull: true;
172
+ hasDefault: false;
173
+ isPrimaryKey: false;
174
+ isAutoincrement: false;
175
+ hasRuntimeDefault: false;
176
+ enumValues: [string, ...string[]];
177
+ baseColumn: never;
178
+ identity: undefined;
179
+ generated: undefined;
180
+ }, {}, {
181
+ length: number | undefined;
182
+ }>;
183
+ entryJson: import("drizzle-orm/sqlite-core").SQLiteColumn<{
184
+ name: "entry_json";
185
+ tableName: "principal_history_entries";
186
+ dataType: "string";
187
+ columnType: "SQLiteText";
188
+ data: string;
189
+ driverParam: string;
190
+ notNull: true;
191
+ hasDefault: false;
192
+ isPrimaryKey: false;
193
+ isAutoincrement: false;
194
+ hasRuntimeDefault: false;
195
+ enumValues: [string, ...string[]];
196
+ baseColumn: never;
197
+ identity: undefined;
198
+ generated: undefined;
199
+ }, {}, {
200
+ length: number | undefined;
201
+ }>;
202
+ };
203
+ dialect: "sqlite";
204
+ }>;
205
+ /** Untrusted proof material, checked against a locally restored private root. */
206
+ export declare const principalHistoryNodes: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
207
+ name: "principal_history_nodes";
208
+ schema: undefined;
209
+ columns: {
210
+ scopeId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
211
+ name: "scope_id";
212
+ tableName: "principal_history_nodes";
213
+ dataType: "string";
214
+ columnType: "SQLiteText";
215
+ data: string;
216
+ driverParam: string;
217
+ notNull: true;
218
+ hasDefault: false;
219
+ isPrimaryKey: false;
220
+ isAutoincrement: false;
221
+ hasRuntimeDefault: false;
222
+ enumValues: [string, ...string[]];
223
+ baseColumn: never;
224
+ identity: undefined;
225
+ generated: undefined;
226
+ }, {}, {
227
+ length: number | undefined;
228
+ }>;
229
+ organizationId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
230
+ name: "organization_id";
231
+ tableName: "principal_history_nodes";
232
+ dataType: "string";
233
+ columnType: "SQLiteText";
234
+ data: string;
235
+ driverParam: string;
236
+ notNull: true;
237
+ hasDefault: false;
238
+ isPrimaryKey: false;
239
+ isAutoincrement: false;
240
+ hasRuntimeDefault: false;
241
+ enumValues: [string, ...string[]];
242
+ baseColumn: never;
243
+ identity: undefined;
244
+ generated: undefined;
245
+ }, {}, {
246
+ length: number | undefined;
247
+ }>;
248
+ hash: import("drizzle-orm/sqlite-core").SQLiteColumn<{
249
+ name: "hash";
250
+ tableName: "principal_history_nodes";
251
+ dataType: "string";
252
+ columnType: "SQLiteText";
253
+ data: string;
254
+ driverParam: string;
255
+ notNull: true;
256
+ hasDefault: false;
257
+ isPrimaryKey: false;
258
+ isAutoincrement: false;
259
+ hasRuntimeDefault: false;
260
+ enumValues: [string, ...string[]];
261
+ baseColumn: never;
262
+ identity: undefined;
263
+ generated: undefined;
264
+ }, {}, {
265
+ length: number | undefined;
266
+ }>;
267
+ leftHash: import("drizzle-orm/sqlite-core").SQLiteColumn<{
268
+ name: "left_hash";
269
+ tableName: "principal_history_nodes";
270
+ dataType: "string";
271
+ columnType: "SQLiteText";
272
+ data: string;
273
+ driverParam: string;
274
+ notNull: true;
275
+ hasDefault: false;
276
+ isPrimaryKey: false;
277
+ isAutoincrement: false;
278
+ hasRuntimeDefault: false;
279
+ enumValues: [string, ...string[]];
280
+ baseColumn: never;
281
+ identity: undefined;
282
+ generated: undefined;
283
+ }, {}, {
284
+ length: number | undefined;
285
+ }>;
286
+ rightHash: import("drizzle-orm/sqlite-core").SQLiteColumn<{
287
+ name: "right_hash";
288
+ tableName: "principal_history_nodes";
289
+ dataType: "string";
290
+ columnType: "SQLiteText";
291
+ data: string;
292
+ driverParam: string;
293
+ notNull: true;
294
+ hasDefault: false;
295
+ isPrimaryKey: false;
296
+ isAutoincrement: false;
297
+ hasRuntimeDefault: false;
298
+ enumValues: [string, ...string[]];
299
+ baseColumn: never;
300
+ identity: undefined;
301
+ generated: undefined;
302
+ }, {}, {
303
+ length: number | undefined;
304
+ }>;
305
+ };
306
+ dialect: "sqlite";
307
+ }>;
308
+ export declare const principalHistoryEvidenceTables: import("./sqlTableSchema.js").SqlTableSchema[];