@enbox/dwn-sdk-js 0.0.6 → 0.0.8

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 (527) hide show
  1. package/dist/browser.mjs +8 -8
  2. package/dist/browser.mjs.map +4 -4
  3. package/dist/esm/generated/precompiled-validators.js +762 -911
  4. package/dist/esm/generated/precompiled-validators.js.map +1 -1
  5. package/dist/esm/src/core/abstract-message.js +4 -0
  6. package/dist/esm/src/core/abstract-message.js.map +1 -1
  7. package/dist/esm/src/core/auth.js +22 -33
  8. package/dist/esm/src/core/auth.js.map +1 -1
  9. package/dist/esm/src/core/constants.js +11 -0
  10. package/dist/esm/src/core/constants.js.map +1 -0
  11. package/dist/esm/src/core/core-protocol.js +44 -0
  12. package/dist/esm/src/core/core-protocol.js.map +1 -0
  13. package/dist/esm/src/core/dwn-constant.js +7 -7
  14. package/dist/esm/src/core/dwn-constant.js.map +1 -1
  15. package/dist/esm/src/core/dwn-error.js +10 -12
  16. package/dist/esm/src/core/dwn-error.js.map +1 -1
  17. package/dist/esm/src/core/grant-authorization.js +50 -52
  18. package/dist/esm/src/core/grant-authorization.js.map +1 -1
  19. package/dist/esm/src/core/message.js +85 -116
  20. package/dist/esm/src/core/message.js.map +1 -1
  21. package/dist/esm/src/core/messages-grant-authorization.js +63 -78
  22. package/dist/esm/src/core/messages-grant-authorization.js.map +1 -1
  23. package/dist/esm/src/core/protocol-authorization-action.js +266 -0
  24. package/dist/esm/src/core/protocol-authorization-action.js.map +1 -0
  25. package/dist/esm/src/core/protocol-authorization-validation.js +321 -0
  26. package/dist/esm/src/core/protocol-authorization-validation.js.map +1 -0
  27. package/dist/esm/src/core/protocol-authorization.js +144 -741
  28. package/dist/esm/src/core/protocol-authorization.js.map +1 -1
  29. package/dist/esm/src/core/protocols-grant-authorization.js +24 -38
  30. package/dist/esm/src/core/protocols-grant-authorization.js.map +1 -1
  31. package/dist/esm/src/core/record-chain.js +64 -0
  32. package/dist/esm/src/core/record-chain.js.map +1 -0
  33. package/dist/esm/src/core/records-grant-authorization.js +53 -72
  34. package/dist/esm/src/core/records-grant-authorization.js.map +1 -1
  35. package/dist/esm/src/core/resumable-task-manager.js +50 -65
  36. package/dist/esm/src/core/resumable-task-manager.js.map +1 -1
  37. package/dist/esm/src/core/tenant-gate.js +2 -13
  38. package/dist/esm/src/core/tenant-gate.js.map +1 -1
  39. package/dist/esm/src/dwn.js +108 -101
  40. package/dist/esm/src/dwn.js.map +1 -1
  41. package/dist/esm/src/event-stream/event-emitter-event-log.js +204 -0
  42. package/dist/esm/src/event-stream/event-emitter-event-log.js.map +1 -0
  43. package/dist/esm/src/handlers/messages-read.js +67 -81
  44. package/dist/esm/src/handlers/messages-read.js.map +1 -1
  45. package/dist/esm/src/handlers/messages-subscribe.js +51 -63
  46. package/dist/esm/src/handlers/messages-subscribe.js.map +1 -1
  47. package/dist/esm/src/handlers/messages-sync.js +75 -89
  48. package/dist/esm/src/handlers/messages-sync.js.map +1 -1
  49. package/dist/esm/src/handlers/protocols-configure.js +153 -163
  50. package/dist/esm/src/handlers/protocols-configure.js.map +1 -1
  51. package/dist/esm/src/handlers/protocols-query.js +52 -55
  52. package/dist/esm/src/handlers/protocols-query.js.map +1 -1
  53. package/dist/esm/src/handlers/records-count.js +97 -85
  54. package/dist/esm/src/handlers/records-count.js.map +1 -1
  55. package/dist/esm/src/handlers/records-delete.js +75 -93
  56. package/dist/esm/src/handlers/records-delete.js.map +1 -1
  57. package/dist/esm/src/handlers/records-query.js +116 -105
  58. package/dist/esm/src/handlers/records-query.js.map +1 -1
  59. package/dist/esm/src/handlers/records-read.js +130 -132
  60. package/dist/esm/src/handlers/records-read.js.map +1 -1
  61. package/dist/esm/src/handlers/records-subscribe.js +164 -104
  62. package/dist/esm/src/handlers/records-subscribe.js.map +1 -1
  63. package/dist/esm/src/handlers/records-write.js +213 -280
  64. package/dist/esm/src/handlers/records-write.js.map +1 -1
  65. package/dist/esm/src/index.js +5 -2
  66. package/dist/esm/src/index.js.map +1 -1
  67. package/dist/esm/src/interfaces/messages-read.js +24 -32
  68. package/dist/esm/src/interfaces/messages-read.js.map +1 -1
  69. package/dist/esm/src/interfaces/messages-subscribe.js +28 -41
  70. package/dist/esm/src/interfaces/messages-subscribe.js.map +1 -1
  71. package/dist/esm/src/interfaces/messages-sync.js +26 -40
  72. package/dist/esm/src/interfaces/messages-sync.js.map +1 -1
  73. package/dist/esm/src/interfaces/protocols-configure.js +87 -65
  74. package/dist/esm/src/interfaces/protocols-configure.js.map +1 -1
  75. package/dist/esm/src/interfaces/protocols-query.js +55 -68
  76. package/dist/esm/src/interfaces/protocols-query.js.map +1 -1
  77. package/dist/esm/src/interfaces/records-count.js +50 -66
  78. package/dist/esm/src/interfaces/records-count.js.map +1 -1
  79. package/dist/esm/src/interfaces/records-delete.js +45 -55
  80. package/dist/esm/src/interfaces/records-delete.js.map +1 -1
  81. package/dist/esm/src/interfaces/records-query.js +60 -76
  82. package/dist/esm/src/interfaces/records-query.js.map +1 -1
  83. package/dist/esm/src/interfaces/records-read.js +51 -67
  84. package/dist/esm/src/interfaces/records-read.js.map +1 -1
  85. package/dist/esm/src/interfaces/records-subscribe.js +53 -68
  86. package/dist/esm/src/interfaces/records-subscribe.js.map +1 -1
  87. package/dist/esm/src/interfaces/records-write-query.js +102 -0
  88. package/dist/esm/src/interfaces/records-write-query.js.map +1 -0
  89. package/dist/esm/src/interfaces/records-write-signing.js +81 -0
  90. package/dist/esm/src/interfaces/records-write-signing.js.map +1 -0
  91. package/dist/esm/src/interfaces/records-write.js +396 -610
  92. package/dist/esm/src/interfaces/records-write.js.map +1 -1
  93. package/dist/esm/src/jose/algorithms/signing/ed25519.js +10 -19
  94. package/dist/esm/src/jose/algorithms/signing/ed25519.js.map +1 -1
  95. package/dist/esm/src/jose/jws/general/builder.js +23 -35
  96. package/dist/esm/src/jose/jws/general/builder.js.map +1 -1
  97. package/dist/esm/src/jose/jws/general/verifier.js +56 -69
  98. package/dist/esm/src/jose/jws/general/verifier.js.map +1 -1
  99. package/dist/esm/src/protocols/permission-grant.js +43 -14
  100. package/dist/esm/src/protocols/permission-grant.js.map +1 -1
  101. package/dist/esm/src/protocols/permission-request.js +28 -14
  102. package/dist/esm/src/protocols/permission-request.js.map +1 -1
  103. package/dist/esm/src/protocols/permissions.js +325 -227
  104. package/dist/esm/src/protocols/permissions.js.map +1 -1
  105. package/dist/esm/src/smt/smt-store-level.js +42 -64
  106. package/dist/esm/src/smt/smt-store-level.js.map +1 -1
  107. package/dist/esm/src/smt/smt-store-memory.js +19 -45
  108. package/dist/esm/src/smt/smt-store-memory.js.map +1 -1
  109. package/dist/esm/src/smt/smt-utils.js +28 -45
  110. package/dist/esm/src/smt/smt-utils.js.map +1 -1
  111. package/dist/esm/src/smt/sparse-merkle-tree.js +426 -471
  112. package/dist/esm/src/smt/sparse-merkle-tree.js.map +1 -1
  113. package/dist/esm/src/state-index/state-index-level.js +113 -150
  114. package/dist/esm/src/state-index/state-index-level.js.map +1 -1
  115. package/dist/esm/src/store/blockstore-level.js +54 -156
  116. package/dist/esm/src/store/blockstore-level.js.map +1 -1
  117. package/dist/esm/src/store/blockstore-mock.js +48 -153
  118. package/dist/esm/src/store/blockstore-mock.js.map +1 -1
  119. package/dist/esm/src/store/data-store-level.js +137 -100
  120. package/dist/esm/src/store/data-store-level.js.map +1 -1
  121. package/dist/esm/src/store/index-level-compound.js +246 -0
  122. package/dist/esm/src/store/index-level-compound.js.map +1 -0
  123. package/dist/esm/src/store/index-level.js +307 -715
  124. package/dist/esm/src/store/index-level.js.map +1 -1
  125. package/dist/esm/src/store/level-wrapper.js +143 -244
  126. package/dist/esm/src/store/level-wrapper.js.map +1 -1
  127. package/dist/esm/src/store/message-store-level.js +71 -94
  128. package/dist/esm/src/store/message-store-level.js.map +1 -1
  129. package/dist/esm/src/store/resumable-task-store-level.js +62 -101
  130. package/dist/esm/src/store/resumable-task-store-level.js.map +1 -1
  131. package/dist/esm/src/store/storage-controller.js +131 -146
  132. package/dist/esm/src/store/storage-controller.js.map +1 -1
  133. package/dist/esm/src/types/permission-types.js.map +1 -1
  134. package/dist/esm/src/types/protocols-types.js +10 -0
  135. package/dist/esm/src/types/protocols-types.js.map +1 -1
  136. package/dist/esm/src/types/records-types.js.map +1 -1
  137. package/dist/esm/src/utils/abort.js +8 -19
  138. package/dist/esm/src/utils/abort.js.map +1 -1
  139. package/dist/esm/src/utils/array.js +15 -49
  140. package/dist/esm/src/utils/array.js.map +1 -1
  141. package/dist/esm/src/utils/cid.js +29 -77
  142. package/dist/esm/src/utils/cid.js.map +1 -1
  143. package/dist/esm/src/utils/data-stream.js +37 -65
  144. package/dist/esm/src/utils/data-stream.js.map +1 -1
  145. package/dist/esm/src/utils/encryption.js +136 -162
  146. package/dist/esm/src/utils/encryption.js.map +1 -1
  147. package/dist/esm/src/utils/filter.js +1 -12
  148. package/dist/esm/src/utils/filter.js.map +1 -1
  149. package/dist/esm/src/utils/hd-key.js +45 -71
  150. package/dist/esm/src/utils/hd-key.js.map +1 -1
  151. package/dist/esm/src/utils/jws.js +9 -20
  152. package/dist/esm/src/utils/jws.js.map +1 -1
  153. package/dist/esm/src/utils/memory-cache.js +12 -23
  154. package/dist/esm/src/utils/memory-cache.js.map +1 -1
  155. package/dist/esm/src/utils/messages.js +21 -33
  156. package/dist/esm/src/utils/messages.js.map +1 -1
  157. package/dist/esm/src/utils/private-key-signer.js +9 -17
  158. package/dist/esm/src/utils/private-key-signer.js.map +1 -1
  159. package/dist/esm/src/utils/protocols.js +62 -70
  160. package/dist/esm/src/utils/protocols.js.map +1 -1
  161. package/dist/esm/src/utils/records.js +103 -166
  162. package/dist/esm/src/utils/records.js.map +1 -1
  163. package/dist/esm/src/utils/secp256k1.js +60 -96
  164. package/dist/esm/src/utils/secp256k1.js.map +1 -1
  165. package/dist/esm/src/utils/secp256r1.js +54 -71
  166. package/dist/esm/src/utils/secp256r1.js.map +1 -1
  167. package/dist/esm/src/utils/time.js +5 -18
  168. package/dist/esm/src/utils/time.js.map +1 -1
  169. package/dist/esm/src/utils/url.js +3 -3
  170. package/dist/esm/src/utils/url.js.map +1 -1
  171. package/dist/esm/tests/core/auth.spec.js +3 -12
  172. package/dist/esm/tests/core/auth.spec.js.map +1 -1
  173. package/dist/esm/tests/core/message.spec.js +50 -59
  174. package/dist/esm/tests/core/message.spec.js.map +1 -1
  175. package/dist/esm/tests/core/protocol-authorization.spec.js +10 -18
  176. package/dist/esm/tests/core/protocol-authorization.spec.js.map +1 -1
  177. package/dist/esm/tests/dwn.spec.js +65 -89
  178. package/dist/esm/tests/dwn.spec.js.map +1 -1
  179. package/dist/esm/tests/event-emitter-event-log.spec.js +305 -0
  180. package/dist/esm/tests/event-emitter-event-log.spec.js.map +1 -0
  181. package/dist/esm/tests/features/author-delegated-grant.spec.js +337 -347
  182. package/dist/esm/tests/features/author-delegated-grant.spec.js.map +1 -1
  183. package/dist/esm/tests/features/owner-delegated-grant.spec.js +160 -172
  184. package/dist/esm/tests/features/owner-delegated-grant.spec.js.map +1 -1
  185. package/dist/esm/tests/features/owner-signature.spec.js +78 -82
  186. package/dist/esm/tests/features/owner-signature.spec.js.map +1 -1
  187. package/dist/esm/tests/features/permissions.spec.js +449 -184
  188. package/dist/esm/tests/features/permissions.spec.js.map +1 -1
  189. package/dist/esm/tests/features/protocol-composition.spec.js +981 -360
  190. package/dist/esm/tests/features/protocol-composition.spec.js.map +1 -1
  191. package/dist/esm/tests/features/protocol-create-action.spec.js +45 -54
  192. package/dist/esm/tests/features/protocol-create-action.spec.js.map +1 -1
  193. package/dist/esm/tests/features/protocol-delete-action.spec.js +99 -108
  194. package/dist/esm/tests/features/protocol-delete-action.spec.js.map +1 -1
  195. package/dist/esm/tests/features/protocol-update-action.spec.js +108 -117
  196. package/dist/esm/tests/features/protocol-update-action.spec.js.map +1 -1
  197. package/dist/esm/tests/features/records-immutable.spec.js +315 -0
  198. package/dist/esm/tests/features/records-immutable.spec.js.map +1 -0
  199. package/dist/esm/tests/features/records-prune.spec.js +178 -194
  200. package/dist/esm/tests/features/records-prune.spec.js.map +1 -1
  201. package/dist/esm/tests/features/records-record-limit.spec.js +542 -0
  202. package/dist/esm/tests/features/records-record-limit.spec.js.map +1 -0
  203. package/dist/esm/tests/features/records-tags.spec.js +456 -463
  204. package/dist/esm/tests/features/records-tags.spec.js.map +1 -1
  205. package/dist/esm/tests/features/resumable-tasks.spec.js +88 -98
  206. package/dist/esm/tests/features/resumable-tasks.spec.js.map +1 -1
  207. package/dist/esm/tests/handlers/messages-read.spec.js +215 -210
  208. package/dist/esm/tests/handlers/messages-read.spec.js.map +1 -1
  209. package/dist/esm/tests/handlers/messages-subscribe.spec.js +309 -171
  210. package/dist/esm/tests/handlers/messages-subscribe.spec.js.map +1 -1
  211. package/dist/esm/tests/handlers/messages-sync.spec.js +272 -199
  212. package/dist/esm/tests/handlers/messages-sync.spec.js.map +1 -1
  213. package/dist/esm/tests/handlers/protocols-configure.spec.js +247 -241
  214. package/dist/esm/tests/handlers/protocols-configure.spec.js.map +1 -1
  215. package/dist/esm/tests/handlers/protocols-query.spec.js +159 -172
  216. package/dist/esm/tests/handlers/protocols-query.spec.js.map +1 -1
  217. package/dist/esm/tests/handlers/records-count.spec.js +101 -105
  218. package/dist/esm/tests/handlers/records-count.spec.js.map +1 -1
  219. package/dist/esm/tests/handlers/records-delete.spec.js +266 -279
  220. package/dist/esm/tests/handlers/records-delete.spec.js.map +1 -1
  221. package/dist/esm/tests/handlers/records-query.spec.js +984 -996
  222. package/dist/esm/tests/handlers/records-query.spec.js.map +1 -1
  223. package/dist/esm/tests/handlers/records-read.spec.js +542 -671
  224. package/dist/esm/tests/handlers/records-read.spec.js.map +1 -1
  225. package/dist/esm/tests/handlers/records-subscribe.spec.js +433 -302
  226. package/dist/esm/tests/handlers/records-subscribe.spec.js.map +1 -1
  227. package/dist/esm/tests/handlers/records-write.spec.js +1216 -1140
  228. package/dist/esm/tests/handlers/records-write.spec.js.map +1 -1
  229. package/dist/esm/tests/interfaces/messages-get.spec.js +39 -48
  230. package/dist/esm/tests/interfaces/messages-get.spec.js.map +1 -1
  231. package/dist/esm/tests/interfaces/messages-subscribe.spec.js +4 -13
  232. package/dist/esm/tests/interfaces/messages-subscribe.spec.js.map +1 -1
  233. package/dist/esm/tests/interfaces/protocols-configure.spec.js +212 -88
  234. package/dist/esm/tests/interfaces/protocols-configure.spec.js.map +1 -1
  235. package/dist/esm/tests/interfaces/protocols-query.spec.js +8 -17
  236. package/dist/esm/tests/interfaces/protocols-query.spec.js.map +1 -1
  237. package/dist/esm/tests/interfaces/records-delete.spec.js +8 -17
  238. package/dist/esm/tests/interfaces/records-delete.spec.js.map +1 -1
  239. package/dist/esm/tests/interfaces/records-query.spec.js +20 -29
  240. package/dist/esm/tests/interfaces/records-query.spec.js.map +1 -1
  241. package/dist/esm/tests/interfaces/records-read.spec.js +42 -51
  242. package/dist/esm/tests/interfaces/records-read.spec.js.map +1 -1
  243. package/dist/esm/tests/interfaces/records-subscribe.spec.js +16 -25
  244. package/dist/esm/tests/interfaces/records-subscribe.spec.js.map +1 -1
  245. package/dist/esm/tests/interfaces/records-write.spec.js +190 -219
  246. package/dist/esm/tests/interfaces/records-write.spec.js.map +1 -1
  247. package/dist/esm/tests/jose/jws/general.spec.js +36 -45
  248. package/dist/esm/tests/jose/jws/general.spec.js.map +1 -1
  249. package/dist/esm/tests/protocols/permission-grant.spec.js +44 -50
  250. package/dist/esm/tests/protocols/permission-grant.spec.js.map +1 -1
  251. package/dist/esm/tests/protocols/permission-request.spec.js +23 -32
  252. package/dist/esm/tests/protocols/permission-request.spec.js.map +1 -1
  253. package/dist/esm/tests/protocols/permissions.spec.js +49 -55
  254. package/dist/esm/tests/protocols/permissions.spec.js.map +1 -1
  255. package/dist/esm/tests/scenarios/aggregator.spec.js +127 -138
  256. package/dist/esm/tests/scenarios/aggregator.spec.js.map +1 -1
  257. package/dist/esm/tests/scenarios/deleted-record.spec.js +372 -36
  258. package/dist/esm/tests/scenarios/deleted-record.spec.js.map +1 -1
  259. package/dist/esm/tests/scenarios/end-to-end-tests.spec.js +55 -64
  260. package/dist/esm/tests/scenarios/end-to-end-tests.spec.js.map +1 -1
  261. package/dist/esm/tests/scenarios/nested-roles.spec.js +66 -76
  262. package/dist/esm/tests/scenarios/nested-roles.spec.js.map +1 -1
  263. package/dist/esm/tests/scenarios/subscriptions.spec.js +451 -354
  264. package/dist/esm/tests/scenarios/subscriptions.spec.js.map +1 -1
  265. package/dist/esm/tests/smt/smt-store-level.spec.js +76 -87
  266. package/dist/esm/tests/smt/smt-store-level.spec.js.map +1 -1
  267. package/dist/esm/tests/smt/sparse-merkle-tree.spec.js +344 -353
  268. package/dist/esm/tests/smt/sparse-merkle-tree.spec.js.map +1 -1
  269. package/dist/esm/tests/state-index/state-index-level.spec.js +117 -126
  270. package/dist/esm/tests/state-index/state-index-level.spec.js.map +1 -1
  271. package/dist/esm/tests/store/blockstore-level.spec.js +44 -99
  272. package/dist/esm/tests/store/blockstore-level.spec.js.map +1 -1
  273. package/dist/esm/tests/store/blockstore-mock.spec.js +40 -120
  274. package/dist/esm/tests/store/blockstore-mock.spec.js.map +1 -1
  275. package/dist/esm/tests/store/data-store-level.spec.js +160 -108
  276. package/dist/esm/tests/store/data-store-level.spec.js.map +1 -1
  277. package/dist/esm/tests/store/index-level.spec.js +404 -414
  278. package/dist/esm/tests/store/index-level.spec.js.map +1 -1
  279. package/dist/esm/tests/store/message-store-level.spec.js +13 -22
  280. package/dist/esm/tests/store/message-store-level.spec.js.map +1 -1
  281. package/dist/esm/tests/store/message-store.spec.js +229 -238
  282. package/dist/esm/tests/store/message-store.spec.js.map +1 -1
  283. package/dist/esm/tests/test-event-stream.js +12 -13
  284. package/dist/esm/tests/test-event-stream.js.map +1 -1
  285. package/dist/esm/tests/test-stores.js +16 -13
  286. package/dist/esm/tests/test-stores.js.map +1 -1
  287. package/dist/esm/tests/test-suite.js +8 -15
  288. package/dist/esm/tests/test-suite.js.map +1 -1
  289. package/dist/esm/tests/utils/cid.spec.js +24 -33
  290. package/dist/esm/tests/utils/cid.spec.js.map +1 -1
  291. package/dist/esm/tests/utils/data-stream.spec.js +48 -57
  292. package/dist/esm/tests/utils/data-stream.spec.js.map +1 -1
  293. package/dist/esm/tests/utils/encryption-callbacks.spec.js +45 -54
  294. package/dist/esm/tests/utils/encryption-callbacks.spec.js.map +1 -1
  295. package/dist/esm/tests/utils/encryption.spec.js +229 -82
  296. package/dist/esm/tests/utils/encryption.spec.js.map +1 -1
  297. package/dist/esm/tests/utils/filters.spec.js +46 -55
  298. package/dist/esm/tests/utils/filters.spec.js.map +1 -1
  299. package/dist/esm/tests/utils/hd-key.spec.js +10 -19
  300. package/dist/esm/tests/utils/hd-key.spec.js.map +1 -1
  301. package/dist/esm/tests/utils/jws.spec.js +3 -12
  302. package/dist/esm/tests/utils/jws.spec.js.map +1 -1
  303. package/dist/esm/tests/utils/memory-cache.spec.js +9 -18
  304. package/dist/esm/tests/utils/memory-cache.spec.js.map +1 -1
  305. package/dist/esm/tests/utils/messages.spec.js +18 -20
  306. package/dist/esm/tests/utils/messages.spec.js.map +1 -1
  307. package/dist/esm/tests/utils/poller.js +22 -33
  308. package/dist/esm/tests/utils/poller.js.map +1 -1
  309. package/dist/esm/tests/utils/private-key-signer.spec.js +15 -24
  310. package/dist/esm/tests/utils/private-key-signer.spec.js.map +1 -1
  311. package/dist/esm/tests/utils/records.spec.js +14 -27
  312. package/dist/esm/tests/utils/records.spec.js.map +1 -1
  313. package/dist/esm/tests/utils/secp256k1.spec.js +16 -25
  314. package/dist/esm/tests/utils/secp256k1.spec.js.map +1 -1
  315. package/dist/esm/tests/utils/secp256r1.spec.js +18 -27
  316. package/dist/esm/tests/utils/secp256r1.spec.js.map +1 -1
  317. package/dist/esm/tests/utils/test-data-generator.js +446 -467
  318. package/dist/esm/tests/utils/test-data-generator.js.map +1 -1
  319. package/dist/esm/tests/validation/json-schemas/definitions.spec.js +2 -11
  320. package/dist/esm/tests/validation/json-schemas/definitions.spec.js.map +1 -1
  321. package/dist/esm/tests/validation/json-schemas/jwk/general-jwk.spec.js +4 -13
  322. package/dist/esm/tests/validation/json-schemas/jwk/general-jwk.spec.js.map +1 -1
  323. package/dist/esm/tests/validation/json-schemas/jwk/public-jwk.spec.js +8 -17
  324. package/dist/esm/tests/validation/json-schemas/jwk/public-jwk.spec.js.map +1 -1
  325. package/dist/esm/tests/validation/json-schemas/jwk-verification-method.spec.js +3 -12
  326. package/dist/esm/tests/validation/json-schemas/jwk-verification-method.spec.js.map +1 -1
  327. package/dist/esm/tests/validation/json-schemas/protocols/protocols-configure.spec.js +4 -13
  328. package/dist/esm/tests/validation/json-schemas/protocols/protocols-configure.spec.js.map +1 -1
  329. package/dist/esm/tests/validation/json-schemas/records/records-query.spec.js +2 -11
  330. package/dist/esm/tests/validation/json-schemas/records/records-query.spec.js.map +1 -1
  331. package/dist/esm/tests/validation/json-schemas/records/records-read.spec.js +2 -11
  332. package/dist/esm/tests/validation/json-schemas/records/records-read.spec.js.map +1 -1
  333. package/dist/esm/tests/validation/json-schemas/records/records-write.spec.js +44 -24
  334. package/dist/esm/tests/validation/json-schemas/records/records-write.spec.js.map +1 -1
  335. package/dist/types/generated/precompiled-validators.d.ts +49 -40
  336. package/dist/types/generated/precompiled-validators.d.ts.map +1 -1
  337. package/dist/types/src/core/constants.d.ts +11 -0
  338. package/dist/types/src/core/constants.d.ts.map +1 -0
  339. package/dist/types/src/core/core-protocol.d.ts +89 -0
  340. package/dist/types/src/core/core-protocol.d.ts.map +1 -0
  341. package/dist/types/src/core/dwn-error.d.ts +9 -12
  342. package/dist/types/src/core/dwn-error.d.ts.map +1 -1
  343. package/dist/types/src/core/grant-authorization.d.ts +6 -2
  344. package/dist/types/src/core/grant-authorization.d.ts.map +1 -1
  345. package/dist/types/src/core/protocol-authorization-action.d.ts +42 -0
  346. package/dist/types/src/core/protocol-authorization-action.d.ts.map +1 -0
  347. package/dist/types/src/core/protocol-authorization-validation.d.ts +81 -0
  348. package/dist/types/src/core/protocol-authorization-validation.d.ts.map +1 -0
  349. package/dist/types/src/core/protocol-authorization.d.ts +24 -106
  350. package/dist/types/src/core/protocol-authorization.d.ts.map +1 -1
  351. package/dist/types/src/core/record-chain.d.ts +24 -0
  352. package/dist/types/src/core/record-chain.d.ts.map +1 -0
  353. package/dist/types/src/core/records-grant-authorization.d.ts.map +1 -1
  354. package/dist/types/src/dwn.d.ts +19 -7
  355. package/dist/types/src/dwn.d.ts.map +1 -1
  356. package/dist/types/src/event-stream/event-emitter-event-log.d.ts +50 -0
  357. package/dist/types/src/event-stream/event-emitter-event-log.d.ts.map +1 -0
  358. package/dist/types/src/handlers/messages-read.d.ts +3 -8
  359. package/dist/types/src/handlers/messages-read.d.ts.map +1 -1
  360. package/dist/types/src/handlers/messages-subscribe.d.ts +6 -10
  361. package/dist/types/src/handlers/messages-subscribe.d.ts.map +1 -1
  362. package/dist/types/src/handlers/messages-sync.d.ts +3 -8
  363. package/dist/types/src/handlers/messages-sync.d.ts.map +1 -1
  364. package/dist/types/src/handlers/protocols-configure.d.ts +3 -10
  365. package/dist/types/src/handlers/protocols-configure.d.ts.map +1 -1
  366. package/dist/types/src/handlers/protocols-query.d.ts +3 -8
  367. package/dist/types/src/handlers/protocols-query.d.ts.map +1 -1
  368. package/dist/types/src/handlers/records-count.d.ts +3 -6
  369. package/dist/types/src/handlers/records-count.d.ts.map +1 -1
  370. package/dist/types/src/handlers/records-delete.d.ts +3 -8
  371. package/dist/types/src/handlers/records-delete.d.ts.map +1 -1
  372. package/dist/types/src/handlers/records-query.d.ts +3 -8
  373. package/dist/types/src/handlers/records-query.d.ts.map +1 -1
  374. package/dist/types/src/handlers/records-read.d.ts +3 -8
  375. package/dist/types/src/handlers/records-read.d.ts.map +1 -1
  376. package/dist/types/src/handlers/records-subscribe.d.ts +8 -10
  377. package/dist/types/src/handlers/records-subscribe.d.ts.map +1 -1
  378. package/dist/types/src/handlers/records-write.d.ts +4 -24
  379. package/dist/types/src/handlers/records-write.d.ts.map +1 -1
  380. package/dist/types/src/index.d.ts +8 -4
  381. package/dist/types/src/index.d.ts.map +1 -1
  382. package/dist/types/src/interfaces/messages-subscribe.d.ts +5 -0
  383. package/dist/types/src/interfaces/messages-subscribe.d.ts.map +1 -1
  384. package/dist/types/src/interfaces/protocols-configure.d.ts.map +1 -1
  385. package/dist/types/src/interfaces/records-subscribe.d.ts +5 -0
  386. package/dist/types/src/interfaces/records-subscribe.d.ts.map +1 -1
  387. package/dist/types/src/interfaces/records-write-query.d.ts +33 -0
  388. package/dist/types/src/interfaces/records-write-query.d.ts.map +1 -0
  389. package/dist/types/src/interfaces/records-write-signing.d.ts +34 -0
  390. package/dist/types/src/interfaces/records-write-signing.d.ts.map +1 -0
  391. package/dist/types/src/interfaces/records-write.d.ts +13 -53
  392. package/dist/types/src/interfaces/records-write.d.ts.map +1 -1
  393. package/dist/types/src/protocols/permission-grant.d.ts +1 -1
  394. package/dist/types/src/protocols/permission-grant.d.ts.map +1 -1
  395. package/dist/types/src/protocols/permission-request.d.ts +1 -1
  396. package/dist/types/src/protocols/permission-request.d.ts.map +1 -1
  397. package/dist/types/src/protocols/permissions.d.ts +40 -3
  398. package/dist/types/src/protocols/permissions.d.ts.map +1 -1
  399. package/dist/types/src/state-index/state-index-level.d.ts.map +1 -1
  400. package/dist/types/src/store/data-store-level.d.ts +20 -4
  401. package/dist/types/src/store/data-store-level.d.ts.map +1 -1
  402. package/dist/types/src/store/index-level-compound.d.ts +70 -0
  403. package/dist/types/src/store/index-level-compound.d.ts.map +1 -0
  404. package/dist/types/src/store/index-level.d.ts +4 -58
  405. package/dist/types/src/store/index-level.d.ts.map +1 -1
  406. package/dist/types/src/store/storage-controller.d.ts +4 -4
  407. package/dist/types/src/store/storage-controller.d.ts.map +1 -1
  408. package/dist/types/src/types/message-types.d.ts +3 -3
  409. package/dist/types/src/types/message-types.d.ts.map +1 -1
  410. package/dist/types/src/types/messages-types.d.ts +12 -3
  411. package/dist/types/src/types/messages-types.d.ts.map +1 -1
  412. package/dist/types/src/types/method-handler.d.ts +24 -3
  413. package/dist/types/src/types/method-handler.d.ts.map +1 -1
  414. package/dist/types/src/types/permission-types.d.ts +7 -0
  415. package/dist/types/src/types/permission-types.d.ts.map +1 -1
  416. package/dist/types/src/types/protocols-types.d.ts +41 -1
  417. package/dist/types/src/types/protocols-types.d.ts.map +1 -1
  418. package/dist/types/src/types/records-types.d.ts +16 -6
  419. package/dist/types/src/types/records-types.d.ts.map +1 -1
  420. package/dist/types/src/types/subscriptions.d.ts +151 -13
  421. package/dist/types/src/types/subscriptions.d.ts.map +1 -1
  422. package/dist/types/src/utils/hd-key.d.ts +1 -9
  423. package/dist/types/src/utils/hd-key.d.ts.map +1 -1
  424. package/dist/types/src/utils/messages.d.ts +7 -5
  425. package/dist/types/src/utils/messages.d.ts.map +1 -1
  426. package/dist/types/src/utils/protocols.d.ts +5 -0
  427. package/dist/types/src/utils/protocols.d.ts.map +1 -1
  428. package/dist/types/src/utils/records.d.ts +1 -11
  429. package/dist/types/src/utils/records.d.ts.map +1 -1
  430. package/dist/types/tests/dwn.spec.d.ts.map +1 -1
  431. package/dist/types/tests/event-emitter-event-log.spec.d.ts +2 -0
  432. package/dist/types/tests/event-emitter-event-log.spec.d.ts.map +1 -0
  433. package/dist/types/tests/features/author-delegated-grant.spec.d.ts.map +1 -1
  434. package/dist/types/tests/features/owner-delegated-grant.spec.d.ts.map +1 -1
  435. package/dist/types/tests/features/owner-signature.spec.d.ts.map +1 -1
  436. package/dist/types/tests/features/permissions.spec.d.ts.map +1 -1
  437. package/dist/types/tests/features/protocol-composition.spec.d.ts.map +1 -1
  438. package/dist/types/tests/features/records-immutable.spec.d.ts +2 -0
  439. package/dist/types/tests/features/records-immutable.spec.d.ts.map +1 -0
  440. package/dist/types/tests/features/records-record-limit.spec.d.ts +2 -0
  441. package/dist/types/tests/features/records-record-limit.spec.d.ts.map +1 -0
  442. package/dist/types/tests/features/records-tags.spec.d.ts.map +1 -1
  443. package/dist/types/tests/features/resumable-tasks.spec.d.ts.map +1 -1
  444. package/dist/types/tests/handlers/messages-read.spec.d.ts.map +1 -1
  445. package/dist/types/tests/handlers/messages-subscribe.spec.d.ts.map +1 -1
  446. package/dist/types/tests/handlers/messages-sync.spec.d.ts.map +1 -1
  447. package/dist/types/tests/handlers/records-count.spec.d.ts.map +1 -1
  448. package/dist/types/tests/handlers/records-delete.spec.d.ts.map +1 -1
  449. package/dist/types/tests/handlers/records-query.spec.d.ts.map +1 -1
  450. package/dist/types/tests/handlers/records-read.spec.d.ts.map +1 -1
  451. package/dist/types/tests/handlers/records-subscribe.spec.d.ts.map +1 -1
  452. package/dist/types/tests/handlers/records-write.spec.d.ts.map +1 -1
  453. package/dist/types/tests/scenarios/deleted-record.spec.d.ts.map +1 -1
  454. package/dist/types/tests/scenarios/subscriptions.spec.d.ts.map +1 -1
  455. package/dist/types/tests/test-event-stream.d.ts +11 -12
  456. package/dist/types/tests/test-event-stream.d.ts.map +1 -1
  457. package/dist/types/tests/test-suite.d.ts +2 -2
  458. package/dist/types/tests/test-suite.d.ts.map +1 -1
  459. package/dist/types/tests/utils/test-data-generator.d.ts +18 -0
  460. package/dist/types/tests/utils/test-data-generator.d.ts.map +1 -1
  461. package/package.json +5 -4
  462. package/src/core/constants.ts +11 -0
  463. package/src/core/core-protocol.ts +129 -0
  464. package/src/core/dwn-error.ts +15 -12
  465. package/src/core/grant-authorization.ts +20 -3
  466. package/src/core/protocol-authorization-action.ts +377 -0
  467. package/src/core/protocol-authorization-validation.ts +487 -0
  468. package/src/core/protocol-authorization.ts +111 -856
  469. package/src/core/record-chain.ts +99 -0
  470. package/src/core/records-grant-authorization.ts +6 -8
  471. package/src/dwn.ts +58 -73
  472. package/src/event-stream/event-emitter-event-log.ts +283 -0
  473. package/src/handlers/messages-read.ts +8 -9
  474. package/src/handlers/messages-subscribe.ts +24 -28
  475. package/src/handlers/messages-sync.ts +10 -16
  476. package/src/handlers/protocols-configure.ts +47 -32
  477. package/src/handlers/protocols-query.ts +6 -9
  478. package/src/handlers/records-count.ts +11 -10
  479. package/src/handlers/records-delete.ts +12 -21
  480. package/src/handlers/records-query.ts +12 -12
  481. package/src/handlers/records-read.ts +34 -22
  482. package/src/handlers/records-subscribe.ts +47 -26
  483. package/src/handlers/records-write.ts +47 -104
  484. package/src/index.ts +9 -5
  485. package/src/interfaces/messages-subscribe.ts +7 -1
  486. package/src/interfaces/protocols-configure.ts +73 -8
  487. package/src/interfaces/records-count.ts +1 -1
  488. package/src/interfaces/records-delete.ts +1 -1
  489. package/src/interfaces/records-query.ts +1 -1
  490. package/src/interfaces/records-read.ts +1 -1
  491. package/src/interfaces/records-subscribe.ts +8 -1
  492. package/src/interfaces/records-write-query.ts +139 -0
  493. package/src/interfaces/records-write-signing.ts +123 -0
  494. package/src/interfaces/records-write.ts +66 -261
  495. package/src/protocols/permission-grant.ts +1 -1
  496. package/src/protocols/permission-request.ts +1 -1
  497. package/src/protocols/permissions.ts +148 -6
  498. package/src/state-index/state-index-level.ts +5 -7
  499. package/src/store/data-store-level.ts +124 -34
  500. package/src/store/index-level-compound.ts +324 -0
  501. package/src/store/index-level.ts +68 -341
  502. package/src/store/storage-controller.ts +11 -11
  503. package/src/types/message-types.ts +3 -3
  504. package/src/types/messages-types.ts +12 -3
  505. package/src/types/method-handler.ts +26 -4
  506. package/src/types/mitt.d.ts +28 -0
  507. package/src/types/permission-types.ts +7 -0
  508. package/src/types/protocols-types.ts +46 -0
  509. package/src/types/records-types.ts +16 -6
  510. package/src/types/subscriptions.ts +178 -14
  511. package/src/utils/hd-key.ts +0 -9
  512. package/src/utils/messages.ts +17 -37
  513. package/src/utils/protocols.ts +8 -0
  514. package/src/utils/records.ts +8 -59
  515. package/dist/esm/src/event-stream/event-emitter-stream.js +0 -60
  516. package/dist/esm/src/event-stream/event-emitter-stream.js.map +0 -1
  517. package/dist/esm/tests/event-stream/event-emitter-stream.spec.js +0 -77
  518. package/dist/esm/tests/event-stream/event-emitter-stream.spec.js.map +0 -1
  519. package/dist/esm/tests/event-stream/event-stream.spec.js +0 -123
  520. package/dist/esm/tests/event-stream/event-stream.spec.js.map +0 -1
  521. package/dist/types/src/event-stream/event-emitter-stream.d.ts +0 -23
  522. package/dist/types/src/event-stream/event-emitter-stream.d.ts.map +0 -1
  523. package/dist/types/tests/event-stream/event-emitter-stream.spec.d.ts +0 -2
  524. package/dist/types/tests/event-stream/event-emitter-stream.spec.d.ts.map +0 -1
  525. package/dist/types/tests/event-stream/event-stream.spec.d.ts +0 -2
  526. package/dist/types/tests/event-stream/event-stream.spec.d.ts.map +0 -1
  527. package/src/event-stream/event-emitter-stream.ts +0 -69
@@ -1,236 +1,181 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var __rest = (this && this.__rest) || function (s, e) {
11
- var t = {};
12
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
13
- t[p] = s[p];
14
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
15
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
16
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
17
- t[p[i]] = s[p[i]];
18
- }
19
- return t;
20
- };
21
- import Ajv from 'ajv/dist/2020.js';
22
- import { FilterUtility } from '../utils/filter.js';
23
- import { PermissionsProtocol } from '../protocols/permissions.js';
24
- import { Records } from '../utils/records.js';
25
- import { RecordsWrite } from '../interfaces/records-write.js';
1
+ import { getRuleSetAtPath } from '../utils/protocols.js';
26
2
  import { SortDirection } from '../types/query-types.js';
27
3
  import { DwnError, DwnErrorCode } from './dwn-error.js';
28
4
  import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
29
- import { getRuleSetAtPath, isCrossProtocolRef, parseCrossProtocolRef } from '../utils/protocols.js';
30
- import { ProtocolAction, ProtocolActor } from '../types/protocols-types.js';
5
+ import { authorizeAgainstAllowedActions, verifyInvokedRole } from './protocol-authorization-action.js';
6
+ import { constructRecordChain, fetchInitialWrite, getGoverningTimestamp } from './record-chain.js';
7
+ import { verifyAsRoleRecordIfNeeded, verifyImmutability, verifyProtocolPathAndContextId, verifyRecordLimit, verifySizeLimit, verifyTagsIfNeeded, verifyTypeWithComposition, } from './protocol-authorization-validation.js';
31
8
  export class ProtocolAuthorization {
32
9
  /**
33
10
  * Performs validation on the structure of RecordsWrite messages that use a protocol.
34
11
  * @throws {Error} if validation fails.
35
12
  */
36
- static validateReferentialIntegrity(tenant, incomingMessage, messageStore) {
37
- return __awaiter(this, void 0, void 0, function* () {
38
- // Determine the governing timestamp for protocol definition lookup.
39
- // For an initial write, this is the message's own timestamp.
40
- // For an update, this is the initial write's timestamp (the protocol version is locked at creation time).
41
- const governingTimestamp = yield ProtocolAuthorization.getGoverningTimestamp(tenant, incomingMessage, messageStore);
42
- // fetch the protocol definition that was active at the governing timestamp
43
- const protocolDefinition = yield ProtocolAuthorization.fetchProtocolDefinition(tenant, incomingMessage.message.descriptor.protocol, messageStore, governingTimestamp);
44
- // verify declared protocol type exists in protocol and that it conforms to type specification.
45
- // For cross-protocol composition, the type may be defined in a referenced protocol.
46
- yield ProtocolAuthorization.verifyTypeWithComposition(tenant, incomingMessage.message, protocolDefinition, messageStore, governingTimestamp);
47
- // validate `protocolPath`
48
- yield ProtocolAuthorization.verifyProtocolPathAndContextId(tenant, incomingMessage, messageStore, governingTimestamp);
49
- // get the rule set for the inbound message
50
- const ruleSet = ProtocolAuthorization.getRuleSet(incomingMessage.message.descriptor.protocolPath, protocolDefinition);
51
- // Validate as a role record if the incoming message is writing a role record
52
- yield ProtocolAuthorization.verifyAsRoleRecordIfNeeded(tenant, incomingMessage, ruleSet, messageStore);
53
- // Verify size limit
54
- ProtocolAuthorization.verifySizeLimit(incomingMessage, ruleSet);
55
- // Verify protocol tags
56
- ProtocolAuthorization.verifyTagsIfNeeded(incomingMessage, ruleSet);
57
- });
13
+ static async validateReferentialIntegrity(tenant, incomingMessage, messageStore, coreProtocols) {
14
+ // Determine the governing timestamp for protocol definition lookup.
15
+ // For an initial write, this is the message's own timestamp.
16
+ // For an update, this is the initial write's timestamp (the protocol version is locked at creation time).
17
+ const governingTimestamp = await getGoverningTimestamp(tenant, incomingMessage, messageStore);
18
+ // fetch the protocol definition that was active at the governing timestamp
19
+ const protocolDefinition = await ProtocolAuthorization.fetchProtocolDefinition(tenant, incomingMessage.message.descriptor.protocol, messageStore, governingTimestamp, coreProtocols);
20
+ // Create a bound fetch function that captures the registry for downstream callbacks.
21
+ const boundFetchDefinition = ProtocolAuthorization.createBoundFetchDefinition(coreProtocols);
22
+ // verify declared protocol type exists in protocol and that it conforms to type specification.
23
+ // For cross-protocol composition, the type may be defined in a referenced protocol.
24
+ await verifyTypeWithComposition(tenant, incomingMessage.message, protocolDefinition, messageStore, boundFetchDefinition, governingTimestamp);
25
+ // validate `protocolPath`
26
+ await verifyProtocolPathAndContextId(tenant, incomingMessage, messageStore, boundFetchDefinition, governingTimestamp);
27
+ // get the rule set for the inbound message
28
+ const ruleSet = ProtocolAuthorization.getRuleSet(incomingMessage.message.descriptor.protocolPath, protocolDefinition);
29
+ // Validate as a role record if the incoming message is writing a role record
30
+ await verifyAsRoleRecordIfNeeded(tenant, incomingMessage, ruleSet, messageStore);
31
+ // Verify size limit
32
+ verifySizeLimit(incomingMessage, ruleSet);
33
+ // Verify protocol tags
34
+ verifyTagsIfNeeded(incomingMessage, ruleSet);
35
+ // Verify immutability — reject updates to write-once records
36
+ await verifyImmutability(incomingMessage, ruleSet);
37
+ // Verify record count limit
38
+ await verifyRecordLimit(tenant, incomingMessage, ruleSet, messageStore);
58
39
  }
59
40
  /**
60
41
  * Performs protocol-based authorization against the incoming RecordsWrite message.
61
42
  * @throws {Error} if authorization fails.
62
43
  */
63
- static authorizeWrite(tenant, incomingMessage, messageStore) {
64
- return __awaiter(this, void 0, void 0, function* () {
65
- const existingInitialWrite = yield ProtocolAuthorization.fetchInitialWrite(tenant, incomingMessage.message.recordId, messageStore);
66
- let recordChain;
67
- if (existingInitialWrite === undefined) {
68
- // NOTE: we can assume this message is an initial write because an existing initial write does not exist.
69
- // Additionally, we check further down in the `RecordsWriteHandler` if the incoming message is an initialWrite,
70
- // so we don't check explicitly here to avoid an unnecessary duplicate check.
71
- recordChain = yield ProtocolAuthorization.constructRecordChain(tenant, incomingMessage.message.descriptor.parentId, messageStore);
72
- }
73
- else {
74
- recordChain = yield ProtocolAuthorization.constructRecordChain(tenant, incomingMessage.message.recordId, messageStore);
75
- }
76
- // Determine the governing timestamp for protocol definition lookup.
77
- const governingTimestamp = yield ProtocolAuthorization.getGoverningTimestamp(tenant, incomingMessage, messageStore);
78
- // fetch the protocol definition that was active at the governing timestamp
79
- const protocolDefinition = yield ProtocolAuthorization.fetchProtocolDefinition(tenant, incomingMessage.message.descriptor.protocol, messageStore, governingTimestamp);
80
- // get the rule set for the inbound message
81
- const ruleSet = ProtocolAuthorization.getRuleSet(incomingMessage.message.descriptor.protocolPath, protocolDefinition);
82
- // If the incoming message has `protocolRole` in the descriptor, validate the invoked role
83
- yield ProtocolAuthorization.verifyInvokedRole(tenant, incomingMessage, incomingMessage.message.descriptor.protocol, incomingMessage.message.contextId, protocolDefinition, messageStore, governingTimestamp);
84
- // verify method invoked against the allowed actions in the rule set
85
- yield ProtocolAuthorization.authorizeAgainstAllowedActions(tenant, incomingMessage, ruleSet, recordChain, messageStore, protocolDefinition);
86
- });
44
+ static async authorizeWrite(tenant, incomingMessage, messageStore, coreProtocols) {
45
+ const existingInitialWrite = await fetchInitialWrite(tenant, incomingMessage.message.recordId, messageStore);
46
+ let recordChain;
47
+ if (existingInitialWrite === undefined) {
48
+ // NOTE: we can assume this message is an initial write because an existing initial write does not exist.
49
+ // Additionally, we check further down in the `RecordsWriteHandler` if the incoming message is an initialWrite,
50
+ // so we don't check explicitly here to avoid an unnecessary duplicate check.
51
+ recordChain = await constructRecordChain(tenant, incomingMessage.message.descriptor.parentId, messageStore);
52
+ }
53
+ else {
54
+ recordChain = await constructRecordChain(tenant, incomingMessage.message.recordId, messageStore);
55
+ }
56
+ // Determine the governing timestamp for protocol definition lookup.
57
+ const governingTimestamp = await getGoverningTimestamp(tenant, incomingMessage, messageStore);
58
+ // fetch the protocol definition that was active at the governing timestamp
59
+ const protocolDefinition = await ProtocolAuthorization.fetchProtocolDefinition(tenant, incomingMessage.message.descriptor.protocol, messageStore, governingTimestamp, coreProtocols);
60
+ // get the rule set for the inbound message
61
+ const ruleSet = ProtocolAuthorization.getRuleSet(incomingMessage.message.descriptor.protocolPath, protocolDefinition);
62
+ const boundFetchDefinition = ProtocolAuthorization.createBoundFetchDefinition(coreProtocols);
63
+ // If the incoming message has `protocolRole` in the descriptor, validate the invoked role
64
+ await verifyInvokedRole(tenant, incomingMessage, incomingMessage.message.descriptor.protocol, incomingMessage.message.contextId, protocolDefinition, messageStore, boundFetchDefinition, governingTimestamp);
65
+ // verify method invoked against the allowed actions in the rule set
66
+ await authorizeAgainstAllowedActions(tenant, incomingMessage, ruleSet, recordChain, messageStore, protocolDefinition);
87
67
  }
88
68
  /**
89
69
  * Performs protocol-based authorization against the incoming `RecordsRead` message.
90
70
  * @param newestRecordsWrite The latest RecordsWrite associated with the recordId being read.
91
71
  * @throws {Error} if authorization fails.
92
72
  */
93
- static authorizeRead(tenant, incomingMessage, newestRecordsWrite, messageStore) {
94
- return __awaiter(this, void 0, void 0, function* () {
95
- // fetch record chain
96
- const recordChain = yield ProtocolAuthorization.constructRecordChain(tenant, newestRecordsWrite.message.recordId, messageStore);
97
- // Use the initial write's timestamp to determine the governing protocol definition.
98
- // The protocol version is locked at the time the record was first created.
99
- const initialWrite = yield ProtocolAuthorization.fetchInitialWrite(tenant, newestRecordsWrite.message.recordId, messageStore);
100
- const governingTimestamp = initialWrite !== undefined
101
- ? initialWrite.descriptor.messageTimestamp
102
- : newestRecordsWrite.message.descriptor.messageTimestamp;
103
- // fetch the protocol definition that was active when the record was created
104
- const protocolDefinition = yield ProtocolAuthorization.fetchProtocolDefinition(tenant, newestRecordsWrite.message.descriptor.protocol, messageStore, governingTimestamp);
105
- // get the rule set for the inbound message
106
- const ruleSet = ProtocolAuthorization.getRuleSet(newestRecordsWrite.message.descriptor.protocolPath, protocolDefinition);
107
- // If the incoming message has `protocolRole` in the descriptor, validate the invoked role
108
- yield ProtocolAuthorization.verifyInvokedRole(tenant, incomingMessage, newestRecordsWrite.message.descriptor.protocol, newestRecordsWrite.message.contextId, protocolDefinition, messageStore, governingTimestamp);
109
- // verify method invoked against the allowed actions in the rule set
110
- yield ProtocolAuthorization.authorizeAgainstAllowedActions(tenant, incomingMessage, ruleSet, recordChain, messageStore, protocolDefinition);
111
- });
112
- }
113
- static authorizeQueryOrSubscribe(tenant, incomingMessage, messageStore) {
114
- return __awaiter(this, void 0, void 0, function* () {
115
- const { protocol, protocolPath, contextId } = incomingMessage.message.descriptor.filter;
116
- // fetch the protocol definition
117
- const protocolDefinition = yield ProtocolAuthorization.fetchProtocolDefinition(tenant, protocol, // `authorizeQueryOrSubscribe` is only called if `protocol` is present
118
- messageStore);
119
- // get the rule set for the inbound message
120
- const ruleSet = ProtocolAuthorization.getRuleSet(protocolPath, // presence of `protocolPath` is verified in `parse()`
121
- protocolDefinition);
122
- // If the incoming message has `protocolRole` in the descriptor, validate the invoked role
123
- yield ProtocolAuthorization.verifyInvokedRole(tenant, incomingMessage, protocol, contextId, protocolDefinition, messageStore);
124
- // verify method invoked against the allowed actions in the rule set
125
- yield ProtocolAuthorization.authorizeAgainstAllowedActions(tenant, incomingMessage, ruleSet, [], // record chain is not relevant to queries or subscriptions
126
- messageStore, protocolDefinition);
127
- });
73
+ static async authorizeRead(tenant, incomingMessage, newestRecordsWrite, messageStore, coreProtocols) {
74
+ // fetch record chain
75
+ const recordChain = await constructRecordChain(tenant, newestRecordsWrite.message.recordId, messageStore);
76
+ // Use the initial write's timestamp to determine the governing protocol definition.
77
+ // The protocol version is locked at the time the record was first created.
78
+ const initialWrite = await fetchInitialWrite(tenant, newestRecordsWrite.message.recordId, messageStore);
79
+ const governingTimestamp = initialWrite !== undefined
80
+ ? initialWrite.descriptor.messageTimestamp
81
+ : newestRecordsWrite.message.descriptor.messageTimestamp;
82
+ // fetch the protocol definition that was active when the record was created
83
+ const protocolDefinition = await ProtocolAuthorization.fetchProtocolDefinition(tenant, newestRecordsWrite.message.descriptor.protocol, messageStore, governingTimestamp, coreProtocols);
84
+ // get the rule set for the inbound message
85
+ const ruleSet = ProtocolAuthorization.getRuleSet(newestRecordsWrite.message.descriptor.protocolPath, protocolDefinition);
86
+ const boundFetchDefinition = ProtocolAuthorization.createBoundFetchDefinition(coreProtocols);
87
+ // If the incoming message has `protocolRole` in the descriptor, validate the invoked role
88
+ await verifyInvokedRole(tenant, incomingMessage, newestRecordsWrite.message.descriptor.protocol, newestRecordsWrite.message.contextId, protocolDefinition, messageStore, boundFetchDefinition, governingTimestamp);
89
+ // verify method invoked against the allowed actions in the rule set
90
+ await authorizeAgainstAllowedActions(tenant, incomingMessage, ruleSet, recordChain, messageStore, protocolDefinition);
91
+ }
92
+ static async authorizeQueryOrSubscribe(tenant, incomingMessage, messageStore, coreProtocols) {
93
+ const { protocol, protocolPath, contextId } = incomingMessage.message.descriptor.filter;
94
+ // fetch the protocol definition
95
+ const protocolDefinition = await ProtocolAuthorization.fetchProtocolDefinition(tenant, protocol, // `authorizeQueryOrSubscribe` is only called if `protocol` is present
96
+ messageStore, undefined, coreProtocols);
97
+ // get the rule set for the inbound message
98
+ const ruleSet = ProtocolAuthorization.getRuleSet(protocolPath, // presence of `protocolPath` is verified in `parse()`
99
+ protocolDefinition);
100
+ const boundFetchDefinition = ProtocolAuthorization.createBoundFetchDefinition(coreProtocols);
101
+ // If the incoming message has `protocolRole` in the descriptor, validate the invoked role
102
+ await verifyInvokedRole(tenant, incomingMessage, protocol, contextId, protocolDefinition, messageStore, boundFetchDefinition);
103
+ // verify method invoked against the allowed actions in the rule set
104
+ await authorizeAgainstAllowedActions(tenant, incomingMessage, ruleSet, [], // record chain is not relevant to queries or subscriptions
105
+ messageStore, protocolDefinition);
128
106
  }
129
107
  /**
130
108
  * Performs protocol-based authorization against the incoming `RecordsDelete` message.
131
109
  * @param recordsWrite A `RecordsWrite` of the record being deleted.
132
110
  */
133
- static authorizeDelete(tenant, incomingMessage, recordsWrite, messageStore) {
134
- return __awaiter(this, void 0, void 0, function* () {
135
- // fetch record chain
136
- const recordChain = yield ProtocolAuthorization.constructRecordChain(tenant, incomingMessage.message.descriptor.recordId, messageStore);
137
- // Use the initial write's timestamp to determine the governing protocol definition.
138
- const initialWrite = yield ProtocolAuthorization.fetchInitialWrite(tenant, incomingMessage.message.descriptor.recordId, messageStore);
139
- const governingTimestamp = initialWrite !== undefined
140
- ? initialWrite.descriptor.messageTimestamp
141
- : recordsWrite.message.descriptor.messageTimestamp;
142
- // fetch the protocol definition that was active when the record was created
143
- const protocolDefinition = yield ProtocolAuthorization.fetchProtocolDefinition(tenant, recordsWrite.message.descriptor.protocol, messageStore, governingTimestamp);
144
- // get the rule set for the inbound message
145
- const ruleSet = ProtocolAuthorization.getRuleSet(recordsWrite.message.descriptor.protocolPath, protocolDefinition);
146
- // If the incoming message has `protocolRole` in the descriptor, validate the invoked role
147
- yield ProtocolAuthorization.verifyInvokedRole(tenant, incomingMessage, recordsWrite.message.descriptor.protocol, recordsWrite.message.contextId, protocolDefinition, messageStore, governingTimestamp);
148
- // verify method invoked against the allowed actions in the rule set
149
- yield ProtocolAuthorization.authorizeAgainstAllowedActions(tenant, incomingMessage, ruleSet, recordChain, messageStore, protocolDefinition);
150
- });
111
+ static async authorizeDelete(tenant, incomingMessage, recordsWrite, messageStore, coreProtocols) {
112
+ // fetch record chain
113
+ const recordChain = await constructRecordChain(tenant, incomingMessage.message.descriptor.recordId, messageStore);
114
+ // Use the initial write's timestamp to determine the governing protocol definition.
115
+ const initialWrite = await fetchInitialWrite(tenant, incomingMessage.message.descriptor.recordId, messageStore);
116
+ const governingTimestamp = initialWrite !== undefined
117
+ ? initialWrite.descriptor.messageTimestamp
118
+ : recordsWrite.message.descriptor.messageTimestamp;
119
+ // fetch the protocol definition that was active when the record was created
120
+ const protocolDefinition = await ProtocolAuthorization.fetchProtocolDefinition(tenant, recordsWrite.message.descriptor.protocol, messageStore, governingTimestamp, coreProtocols);
121
+ // get the rule set for the inbound message
122
+ const ruleSet = ProtocolAuthorization.getRuleSet(recordsWrite.message.descriptor.protocolPath, protocolDefinition);
123
+ const boundFetchDefinition = ProtocolAuthorization.createBoundFetchDefinition(coreProtocols);
124
+ // If the incoming message has `protocolRole` in the descriptor, validate the invoked role
125
+ await verifyInvokedRole(tenant, incomingMessage, recordsWrite.message.descriptor.protocol, recordsWrite.message.contextId, protocolDefinition, messageStore, boundFetchDefinition, governingTimestamp);
126
+ // verify method invoked against the allowed actions in the rule set
127
+ await authorizeAgainstAllowedActions(tenant, incomingMessage, ruleSet, recordChain, messageStore, protocolDefinition);
151
128
  }
152
129
  /**
153
130
  * Fetches the protocol definition based on the protocol specified in the given message.
154
131
  * When `messageTimestamp` is provided, returns the protocol definition that was active at that
155
132
  * point in time — i.e. the ProtocolsConfigure with the greatest `messageTimestamp` that is <= the
156
133
  * given timestamp. When not provided, returns the latest (current) protocol definition.
157
- */
158
- static fetchProtocolDefinition(tenant, protocolUri, messageStore, messageTimestamp) {
159
- return __awaiter(this, void 0, void 0, function* () {
160
- // if first-class protocol, return the definition from const object directly without going to data store
161
- if (protocolUri === PermissionsProtocol.uri) {
162
- return PermissionsProtocol.definition;
163
- }
164
- // fetch the corresponding protocol definition
165
- const query = {
166
- interface: DwnInterfaceName.Protocols,
167
- method: DwnMethodName.Configure,
168
- protocol: protocolUri,
169
- };
170
- if (messageTimestamp !== undefined) {
171
- // temporal lookup: find the protocol definition active at the given timestamp
172
- query.messageTimestamp = { lte: messageTimestamp };
173
- }
174
- else {
175
- // default: return only the latest protocol definition
176
- query.isLatestBaseState = true;
177
- }
178
- const { messages: protocols } = yield messageStore.query(tenant, [query], { messageTimestamp: SortDirection.Descending }, { limit: 1 });
179
- if (protocols.length === 0) {
180
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationProtocolNotFound, `unable to find protocol definition for ${protocolUri}`);
181
- }
182
- const protocolMessage = protocols[0];
183
- return protocolMessage.descriptor.definition;
184
- });
185
- }
186
- /**
187
- * Constructs the chain of EXISTING records in the datastore where the first record is the root initial `RecordsWrite` of the record chain
188
- * and last record is the initial `RecordsWrite` of the descendant record specified.
189
- * @param descendantRecordId The ID of the descendent record to start constructing the record chain from by repeatedly looking up the parent.
190
- * @returns the record chain where each record is represented by its initial `RecordsWrite`;
191
- * returns empty array if `descendantRecordId` is `undefined`.
192
- * @throws {DwnError} if `descendantRecordId` is defined but any initial `RecordsWrite` is not found in the chain of records.
193
- */
194
- static constructRecordChain(tenant, descendantRecordId, messageStore) {
195
- return __awaiter(this, void 0, void 0, function* () {
196
- if (descendantRecordId === undefined) {
197
- return [];
198
- }
199
- const recordChain = [];
200
- // keep walking up the chain from the inbound message's parent, until there is no more parent
201
- let currentRecordId = descendantRecordId;
202
- while (currentRecordId !== undefined) {
203
- const initialWrite = yield ProtocolAuthorization.fetchInitialWrite(tenant, currentRecordId, messageStore);
204
- // RecordsWrite needed should be available since we perform necessary checks at the time of writes,
205
- // eg. check the immediate parent in `verifyProtocolPathAndContextId` at the time of writing,
206
- // so if this condition is triggered, it means there is an unexpected bug that caused an incomplete chain.
207
- // We add additional defensive check here because returning an unexpected/incorrect record chain could lead to security vulnerabilities.
208
- if (initialWrite === undefined) {
209
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationParentNotFoundConstructingRecordChain, `Unexpected error that should never trigger: no parent found with ID ${currentRecordId} when constructing record chain.`);
210
- }
211
- recordChain.push(initialWrite);
212
- currentRecordId = initialWrite.descriptor.parentId;
134
+ *
135
+ * When `coreProtocols` is provided, core protocol definitions are returned directly from the
136
+ * registry without a message store query. The extra parameter does not affect the
137
+ * `FetchProtocolDefinitionFn` callback type callers that pass this function as a callback
138
+ * should bind the registry via a closure (see `createBoundFetchDefinition`).
139
+ */
140
+ static async fetchProtocolDefinition(tenant, protocolUri, messageStore, messageTimestamp, coreProtocols) {
141
+ // if the protocol is a registered core protocol, return the definition directly without a store query
142
+ if (coreProtocols !== undefined) {
143
+ const coreDefinition = coreProtocols.getDefinition(protocolUri);
144
+ if (coreDefinition !== undefined) {
145
+ return coreDefinition;
213
146
  }
214
- return recordChain.reverse(); // root record first
215
- });
147
+ }
148
+ // fetch the corresponding protocol definition
149
+ const query = {
150
+ interface: DwnInterfaceName.Protocols,
151
+ method: DwnMethodName.Configure,
152
+ protocol: protocolUri,
153
+ };
154
+ if (messageTimestamp !== undefined) {
155
+ // temporal lookup: find the protocol definition active at the given timestamp
156
+ query.messageTimestamp = { lte: messageTimestamp };
157
+ }
158
+ else {
159
+ // default: return only the latest protocol definition
160
+ query.isLatestBaseState = true;
161
+ }
162
+ const { messages: protocols } = await messageStore.query(tenant, [query], { messageTimestamp: SortDirection.Descending }, { limit: 1 });
163
+ if (protocols.length === 0) {
164
+ throw new DwnError(DwnErrorCode.ProtocolAuthorizationProtocolNotFound, `unable to find protocol definition for ${protocolUri}`);
165
+ }
166
+ const protocolMessage = protocols[0];
167
+ return protocolMessage.descriptor.definition;
216
168
  }
217
169
  /**
218
- * Fetches the initial RecordsWrite message associated with the given (tenant + recordId).
170
+ * Creates a `FetchProtocolDefinitionFn` closure that binds the given `CoreProtocolRegistry`.
171
+ * This allows core protocol definitions to be resolved from the registry without changing
172
+ * the `FetchProtocolDefinitionFn` type signature — zero ripple to downstream consumers
173
+ * like `protocol-authorization-action.ts` and `protocol-authorization-validation.ts`.
219
174
  */
220
- static fetchInitialWrite(tenant, recordId, messageStore) {
221
- return __awaiter(this, void 0, void 0, function* () {
222
- const query = {
223
- interface: DwnInterfaceName.Records,
224
- method: DwnMethodName.Write,
225
- recordId: recordId
226
- };
227
- const { messages } = yield messageStore.query(tenant, [query]);
228
- if (messages.length === 0) {
229
- return undefined;
230
- }
231
- const initialWrite = yield RecordsWrite.getInitialWrite(messages);
232
- return initialWrite;
233
- });
175
+ static createBoundFetchDefinition(coreProtocols) {
176
+ return (tenant, protocolUri, messageStore, messageTimestamp) => {
177
+ return ProtocolAuthorization.fetchProtocolDefinition(tenant, protocolUri, messageStore, messageTimestamp, coreProtocols);
178
+ };
234
179
  }
235
180
  /**
236
181
  * Gets the rule set corresponding to the given protocolPath.
@@ -242,547 +187,5 @@ export class ProtocolAuthorization {
242
187
  }
243
188
  return ruleSet;
244
189
  }
245
- /**
246
- * Verifies the `protocolPath` declared in the given message (if it is a RecordsWrite) matches the path of actual record chain.
247
- * For cross-protocol composition, the parent record may belong to a different protocol (resolved via `$ref` in the composing protocol).
248
- * @throws {DwnError} if fails verification.
249
- */
250
- static verifyProtocolPathAndContextId(tenant, inboundMessage, messageStore, governingTimestamp) {
251
- return __awaiter(this, void 0, void 0, function* () {
252
- const declaredProtocolPath = inboundMessage.message.descriptor.protocolPath;
253
- const declaredTypeName = ProtocolAuthorization.getTypeName(declaredProtocolPath);
254
- const parentId = inboundMessage.message.descriptor.parentId;
255
- if (parentId === undefined) {
256
- if (declaredProtocolPath !== declaredTypeName) {
257
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationParentlessIncorrectProtocolPath, `Declared protocol path '${declaredProtocolPath}' is not valid for records with no parent'.`);
258
- }
259
- return;
260
- }
261
- // Else `parentId` is defined, so we need to verify both protocolPath and contextId
262
- // Determine the protocol URI for the parent query.
263
- // If the parent path segment has a `$ref` in the composing protocol, the parent lives in a different protocol.
264
- const childProtocol = inboundMessage.message.descriptor.protocol;
265
- const parentProtocolUri = yield ProtocolAuthorization.resolveParentProtocolUri(tenant, childProtocol, declaredProtocolPath, messageStore, governingTimestamp);
266
- // fetch the parent message
267
- const query = {
268
- isLatestBaseState: true, // NOTE: this filter is critical, to ensure are are not returning a deleted parent
269
- interface: DwnInterfaceName.Records,
270
- method: DwnMethodName.Write,
271
- protocol: parentProtocolUri,
272
- recordId: parentId
273
- };
274
- const { messages: parentMessages } = yield messageStore.query(tenant, [query]);
275
- const parentMessage = parentMessages[0];
276
- if (parentMessage === undefined) {
277
- // if this is a cross-protocol composition lookup, use a more descriptive error
278
- if (parentProtocolUri !== childProtocol) {
279
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationCrossProtocolParentNotFound, `Could not find parent record '${parentId}' in protocol '${parentProtocolUri}' ` +
280
- `for cross-protocol child at path '${declaredProtocolPath}'.`);
281
- }
282
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationIncorrectProtocolPath, `Could not find matching parent record to verify declared protocol path '${declaredProtocolPath}'.`);
283
- }
284
- // verifying protocolPath of incoming message is a child of the parent message's protocolPath
285
- const parentProtocolPath = parentMessage.descriptor.protocolPath;
286
- const expectedProtocolPath = `${parentProtocolPath}/${declaredTypeName}`;
287
- if (expectedProtocolPath !== declaredProtocolPath) {
288
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationIncorrectProtocolPath, `Could not find matching parent record to verify declared protocol path '${declaredProtocolPath}'.`);
289
- }
290
- // verifying contextId of incoming message is a child of the parent message's contextId
291
- const expectedContextId = `${parentMessage.contextId}/${inboundMessage.message.recordId}`;
292
- const actualContextId = inboundMessage.message.contextId;
293
- if (actualContextId !== expectedContextId) {
294
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationIncorrectContextId, `Declared contextId '${actualContextId}' is not the same as expected: '${expectedContextId}'.`);
295
- }
296
- });
297
- }
298
- /**
299
- * Resolves the protocol URI that should be used when querying for the parent record.
300
- * For standard (non-composed) records, this is the same as the child's protocol.
301
- * For cross-protocol composition, the parent may live in a different protocol
302
- * (resolved via `$ref` in the composing protocol's definition).
303
- *
304
- * Logic: Given a child at protocolPath `a/b/c`, the parent is at `a/b`.
305
- * Walk up the composing protocol's structure from root to `a/b`.
306
- * If any segment along the way has a `$ref`, the parent (and its ancestors up to the `$ref` boundary)
307
- * live in the referenced protocol. Specifically, the `$ref` at the topmost ancestor tells us
308
- * the parent's protocol URI.
309
- */
310
- static resolveParentProtocolUri(tenant, childProtocolUri, childProtocolPath, messageStore, governingTimestamp) {
311
- return __awaiter(this, void 0, void 0, function* () {
312
- const segments = childProtocolPath.split('/');
313
- // A root-level record (no `/` in path) has no parent or uses the same protocol
314
- if (segments.length <= 1) {
315
- return childProtocolUri;
316
- }
317
- // Fetch the composing protocol's definition at the governing timestamp
318
- const composingDefinition = yield ProtocolAuthorization.fetchProtocolDefinition(tenant, childProtocolUri, messageStore, governingTimestamp);
319
- // Walk the structure to find the parent's path segment
320
- // The parent's position in the structure is at segments[0..n-2]
321
- // We check if the first segment has a `$ref`, which means the parent is in a different protocol
322
- const firstSegmentRuleSet = composingDefinition.structure[segments[0]];
323
- if ((firstSegmentRuleSet === null || firstSegmentRuleSet === void 0 ? void 0 : firstSegmentRuleSet.$ref) !== undefined) {
324
- const parsed = parseCrossProtocolRef(firstSegmentRuleSet.$ref);
325
- if (parsed !== undefined && composingDefinition.uses !== undefined) {
326
- const resolvedUri = composingDefinition.uses[parsed.alias];
327
- if (resolvedUri !== undefined) {
328
- // The parent path is within the `$ref` boundary — check if the parent IS the `$ref` node
329
- // or is a descendant of it (which would still be in the composing protocol).
330
- // If segments.length === 2, parent is at segments[0] which IS the $ref node → parent's protocol is the referenced one.
331
- // If segments.length > 2, parent is at segments[0..n-2]. If segments[0] is $ref, the parent could be:
332
- // - Still the $ref node itself (segments.length === 2) → referenced protocol
333
- // - A child of the $ref node defined in the composing protocol (segments.length > 2) → composing protocol
334
- if (segments.length === 2) {
335
- // Parent is the $ref node itself (e.g., child is "thread/comment", parent is "thread")
336
- return resolvedUri;
337
- }
338
- // else: parent is a deeper child defined in the composing protocol
339
- return childProtocolUri;
340
- }
341
- }
342
- }
343
- return childProtocolUri;
344
- });
345
- }
346
- /**
347
- * Verifies the `dataFormat` and `schema` declared in the given message matches the type in the protocol.
348
- * For cross-protocol composition, if the type is at a `$ref` position in the structure,
349
- * the type definition is looked up in the referenced protocol's `types` map instead.
350
- */
351
- static verifyTypeWithComposition(tenant, inboundMessage, protocolDefinition, messageStore, governingTimestamp) {
352
- return __awaiter(this, void 0, void 0, function* () {
353
- const declaredProtocolPath = inboundMessage.descriptor.protocolPath;
354
- const declaredTypeName = ProtocolAuthorization.getTypeName(declaredProtocolPath);
355
- // Resolve which protocol types map to use.
356
- // If the first path segment has `$ref`, this record's type might be defined in a referenced protocol.
357
- const protocolTypes = yield ProtocolAuthorization.resolveProtocolTypesForPath(tenant, declaredProtocolPath, protocolDefinition, messageStore, governingTimestamp);
358
- ProtocolAuthorization.verifyType(inboundMessage, protocolTypes, declaredTypeName);
359
- });
360
- }
361
- /**
362
- * Resolves the `ProtocolTypes` map that contains the type definition for the given protocol path.
363
- * For non-composed records, this is the protocol definition's own `types` map.
364
- * For records at a `$ref` position, this is the referenced protocol's `types` map.
365
- */
366
- static resolveProtocolTypesForPath(tenant, protocolPath, protocolDefinition, messageStore, governingTimestamp) {
367
- return __awaiter(this, void 0, void 0, function* () {
368
- const segments = protocolPath.split('/');
369
- // Check if the first segment has a `$ref`
370
- const firstSegmentRuleSet = protocolDefinition.structure[segments[0]];
371
- if ((firstSegmentRuleSet === null || firstSegmentRuleSet === void 0 ? void 0 : firstSegmentRuleSet.$ref) !== undefined && segments.length === 1) {
372
- // This record IS the $ref node itself — its type is defined in the referenced protocol
373
- const parsed = parseCrossProtocolRef(firstSegmentRuleSet.$ref);
374
- if (parsed !== undefined && protocolDefinition.uses !== undefined) {
375
- const refProtocolUri = protocolDefinition.uses[parsed.alias];
376
- if (refProtocolUri !== undefined) {
377
- const refDefinition = yield ProtocolAuthorization.fetchProtocolDefinition(tenant, refProtocolUri, messageStore, governingTimestamp);
378
- return refDefinition.types;
379
- }
380
- }
381
- }
382
- // Default: use the composing protocol's own types
383
- return protocolDefinition.types;
384
- });
385
- }
386
- /**
387
- * Verifies the `dataFormat` and `schema` declared in the given message (if it is a RecordsWrite) matches dataFormat
388
- * and schema of the type in the given protocol.
389
- * @throws {DwnError} if fails verification.
390
- */
391
- static verifyType(inboundMessage, protocolTypes, typeName) {
392
- const declaredTypeName = typeName !== null && typeName !== void 0 ? typeName : ProtocolAuthorization.getTypeName(inboundMessage.descriptor.protocolPath);
393
- const typeNames = Object.keys(protocolTypes);
394
- if (!typeNames.includes(declaredTypeName)) {
395
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationInvalidType, `record with type ${declaredTypeName} not allowed in protocol`);
396
- }
397
- const protocolType = protocolTypes[declaredTypeName];
398
- // no `schema` specified in protocol definition means that any schema is allowed
399
- const { schema } = inboundMessage.descriptor;
400
- if (protocolType.schema !== undefined && protocolType.schema !== schema) {
401
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationInvalidSchema, `type '${declaredTypeName}' must have schema '${protocolType.schema}', \
402
- instead has '${schema}'`);
403
- }
404
- // no `dataFormats` specified in protocol definition means that all dataFormats are allowed
405
- const { dataFormat } = inboundMessage.descriptor;
406
- if (protocolType.dataFormats !== undefined && !protocolType.dataFormats.includes(dataFormat)) {
407
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationIncorrectDataFormat, `type '${declaredTypeName}' must have data format in (${protocolType.dataFormats}), \
408
- instead has '${dataFormat}'`);
409
- }
410
- // enforce encryption when the protocol type requires it
411
- if (protocolType.encryptionRequired === true && inboundMessage.encryption === undefined) {
412
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationEncryptionRequired, `type '${declaredTypeName}' requires encryption but message has no encryption metadata`);
413
- }
414
- }
415
- /**
416
- * Check if the incoming message is invoking a role. If so, validate the invoked role.
417
- * For cross-protocol role invocation, the role record may live in a different protocol
418
- * (resolved via the composing protocol's `uses` map).
419
- */
420
- static verifyInvokedRole(tenant, incomingMessage, protocolUri, contextId, protocolDefinition, messageStore, governingTimestamp) {
421
- return __awaiter(this, void 0, void 0, function* () {
422
- var _a;
423
- const protocolRole = (_a = incomingMessage.signaturePayload) === null || _a === void 0 ? void 0 : _a.protocolRole;
424
- // Only verify role if there is a role being invoked
425
- if (protocolRole === undefined) {
426
- return;
427
- }
428
- // Determine the protocol URI and protocol path for the role record.
429
- // For cross-protocol roles (e.g., "threads:thread/participant"), resolve the alias.
430
- let roleProtocolUri = protocolUri;
431
- let roleProtocolPath = protocolRole;
432
- if (isCrossProtocolRef(protocolRole)) {
433
- const parsed = parseCrossProtocolRef(protocolRole);
434
- if (parsed === undefined) {
435
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationNotARole, `Cross-protocol role '${protocolRole}' could not be parsed as a valid 'alias:path' format.`);
436
- }
437
- if (protocolDefinition.uses === undefined || protocolDefinition.uses[parsed.alias] === undefined) {
438
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationNotARole, `Cross-protocol role alias '${parsed.alias}' in '${protocolRole}' does not exist in the protocol's 'uses' map.`);
439
- }
440
- roleProtocolUri = protocolDefinition.uses[parsed.alias];
441
- roleProtocolPath = parsed.protocolPath;
442
- // Fetch the referenced protocol's definition to validate the role exists
443
- const refDefinition = yield ProtocolAuthorization.fetchProtocolDefinition(tenant, roleProtocolUri, messageStore, governingTimestamp);
444
- const roleRuleSet = getRuleSetAtPath(roleProtocolPath, refDefinition.structure);
445
- if (roleRuleSet === undefined || !roleRuleSet.$role) {
446
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationNotARole, `Cross-protocol role path ${protocolRole} does not match role record type.`);
447
- }
448
- }
449
- else {
450
- // Local role: validate in the composing protocol's definition
451
- const roleRuleSet = getRuleSetAtPath(protocolRole, protocolDefinition.structure);
452
- if (roleRuleSet === undefined || !roleRuleSet.$role) {
453
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationNotARole, `Protocol path ${protocolRole} does not match role record type.`);
454
- }
455
- }
456
- // Construct a filter to fetch the invoked role record
457
- const roleRecordFilter = {
458
- interface: DwnInterfaceName.Records,
459
- method: DwnMethodName.Write,
460
- protocol: roleProtocolUri,
461
- protocolPath: roleProtocolPath,
462
- recipient: incomingMessage.author,
463
- isLatestBaseState: true,
464
- };
465
- const ancestorSegmentCountOfRolePath = roleProtocolPath.split('/').length - 1;
466
- if (contextId === undefined && ancestorSegmentCountOfRolePath > 0) {
467
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationMissingContextId, 'Could not verify role because contextId is missing.');
468
- }
469
- // Compute `contextId` prefix filter for fetching the invoked role record if the role path is not at the root level.
470
- // e.g. if invoked role path is `Thread/Participant`, and the `contextId` of the message is `threadX/messageY/attachmentZ`,
471
- // then we need to add a prefix filter as `threadX` for the `contextId`
472
- // because the `contextId` of the Participant record would be in the form of be `threadX/participantA`
473
- if (ancestorSegmentCountOfRolePath > 0) {
474
- const contextIdSegments = contextId.split('/'); // NOTE: currently contextId segment count is never shorter than the role path count.
475
- const contextIdPrefix = contextIdSegments.slice(0, ancestorSegmentCountOfRolePath).join('/');
476
- const contextIdPrefixFilter = FilterUtility.constructPrefixFilterAsRangeFilter(contextIdPrefix);
477
- roleRecordFilter.contextId = contextIdPrefixFilter;
478
- }
479
- const { messages: matchingMessages } = yield messageStore.query(tenant, [roleRecordFilter]);
480
- if (matchingMessages.length === 0) {
481
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationMatchingRoleRecordNotFound, `No matching role record found for protocol path ${roleProtocolPath}`);
482
- }
483
- });
484
- }
485
- /**
486
- * Returns all the ProtocolActions that would authorized the incoming message
487
- * (but we still need to later verify if there is a rule defined that matches one of the actions).
488
- * NOTE: the reason why there could be multiple actions is because:
489
- * - In case of an initial RecordsWrite, the RecordsWrite can be authorized by an allow `create` or `write` rule.
490
- * - In case of a non-initial RecordsWrite by the original record author, the RecordsWrite can be authorized by a `write` or `co-update` rule.
491
- *
492
- * It is important to recognize that the `write` access that allowed the original record author to create the record maybe revoked
493
- * (e.g. by role revocation) by the time a "non-initial" write by the same author is attempted.
494
- */
495
- static getActionsSeekingARuleMatch(tenant, incomingMessage, messageStore) {
496
- return __awaiter(this, void 0, void 0, function* () {
497
- switch (incomingMessage.message.descriptor.method) {
498
- case DwnMethodName.Delete:
499
- const recordsDelete = incomingMessage;
500
- const recordId = recordsDelete.message.descriptor.recordId;
501
- const initialWrite = yield RecordsWrite.fetchInitialRecordsWrite(messageStore, tenant, recordId);
502
- // if there is no initial write, then no action rule can authorize the incoming message, because we won't know who the original author is
503
- // NOTE: purely defensive programming: currently not reachable
504
- // because RecordsDelete handler already have an existence check prior to this method being called.
505
- if (initialWrite === undefined) {
506
- return [];
507
- }
508
- const actionsThatWouldAuthorizeDelete = [];
509
- const prune = recordsDelete.message.descriptor.prune;
510
- if (prune) {
511
- actionsThatWouldAuthorizeDelete.push(ProtocolAction.CoPrune);
512
- // A prune by the original record author can also be authorized by a 'prune' rule.
513
- if (incomingMessage.author === initialWrite.author) {
514
- actionsThatWouldAuthorizeDelete.push(ProtocolAction.Prune);
515
- }
516
- }
517
- else {
518
- actionsThatWouldAuthorizeDelete.push(ProtocolAction.CoDelete);
519
- // A delete by the original record author can also be authorized by a 'delete' rule.
520
- if (incomingMessage.author === initialWrite.author) {
521
- actionsThatWouldAuthorizeDelete.push(ProtocolAction.Delete);
522
- }
523
- }
524
- return actionsThatWouldAuthorizeDelete;
525
- case DwnMethodName.Count:
526
- return [ProtocolAction.Read];
527
- case DwnMethodName.Query:
528
- return [ProtocolAction.Read];
529
- case DwnMethodName.Read:
530
- return [ProtocolAction.Read];
531
- case DwnMethodName.Subscribe:
532
- return [ProtocolAction.Read];
533
- case DwnMethodName.Write:
534
- const incomingRecordsWrite = incomingMessage;
535
- if (yield incomingRecordsWrite.isInitialWrite()) {
536
- return [ProtocolAction.Create];
537
- }
538
- else {
539
- // else incoming RecordsWrite not an initial write
540
- const recordId = incomingMessage.message.recordId;
541
- const initialWrite = yield RecordsWrite.fetchInitialRecordsWrite(messageStore, tenant, recordId);
542
- // if there is no initial write to update from, then no action rule can authorize the incoming message
543
- if (initialWrite === undefined) {
544
- return [];
545
- }
546
- if (incomingMessage.author === initialWrite.author) {
547
- // 'update' or 'co-update' action authorizes the incoming message
548
- return [ProtocolAction.CoUpdate, ProtocolAction.Update];
549
- }
550
- else {
551
- // An update by someone who is not the record author can only be authorized by a 'co-update' rule.
552
- return [ProtocolAction.CoUpdate];
553
- }
554
- }
555
- }
556
- // purely defensive programming: should not be reachable
557
- // setting to empty array will prevent any message from being authorized
558
- return [];
559
- });
560
- }
561
- /**
562
- * Verifies the given message is authorized by one of the action rules in the given protocol rule set.
563
- * @param protocolDefinition Optional protocol definition for resolving cross-protocol `of` and `role` references.
564
- * @throws {Error} if action not allowed.
565
- */
566
- static authorizeAgainstAllowedActions(tenant, incomingMessage, ruleSet, recordChain, messageStore, protocolDefinition) {
567
- return __awaiter(this, void 0, void 0, function* () {
568
- var _a;
569
- const incomingMessageMethod = incomingMessage.message.descriptor.method;
570
- const actionsSeekingARuleMatch = yield ProtocolAuthorization.getActionsSeekingARuleMatch(tenant, incomingMessage, messageStore);
571
- const author = incomingMessage.author;
572
- const actionRules = ruleSet.$actions;
573
- // NOTE: We have already checked that the message is not from tenant, owner, or permission grant authorized prior to this method being called.
574
- if (actionRules === undefined) {
575
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationActionRulesNotFound, `no action rule defined for Records${incomingMessageMethod}, ${author} is unauthorized`);
576
- }
577
- const invokedRole = (_a = incomingMessage.signaturePayload) === null || _a === void 0 ? void 0 : _a.protocolRole;
578
- // Iterate through the action rules to find a rule that authorizes the incoming message.
579
- for (const actionRule of actionRules) {
580
- // If the action rule does not have an allowed action that matches an action that can authorize the message, skip to evaluate next action rule.
581
- const ruleHasAMatchingAllowedAction = actionRule.can.some(allowedAction => actionsSeekingARuleMatch.includes(allowedAction));
582
- if (!ruleHasAMatchingAllowedAction) {
583
- continue;
584
- }
585
- // Code reaches here means this action rule has an allowed action that matches the action of the message.
586
- // The remaining code checks the actor/author of the incoming message.
587
- // If the action rule allows `anyone`, then no further checks are needed.
588
- if (actionRule.who === ProtocolActor.Anyone) {
589
- return;
590
- }
591
- // Since not `anyone` is allowed in this action rule, we will need to check the author of the incoming message,
592
- // if the author of incoming message is not defined, this action rule cannot authorize the incoming message.
593
- if (author === undefined) {
594
- continue;
595
- }
596
- // go through role validation path if a role is invoked by the incoming message
597
- if (invokedRole !== undefined) {
598
- // When a protocol role is being invoked, we require that there is a matching `role` rule.
599
- if (actionRule.role === invokedRole) {
600
- // role is successfully invoked
601
- return;
602
- }
603
- else {
604
- continue;
605
- }
606
- }
607
- // else we go through the actor (`who`) validation
608
- // If `of` is not set, handle it as a special case
609
- // NOTE: `of` is always set if `who` is set to `author` (we do this check in `validateRuleSetRecursively()`)
610
- if (actionRule.who === ProtocolActor.Recipient && actionRule.of === undefined) {
611
- // If the action rule specifies a recipient without `of` and the incoming message is authenticated:
612
- // Author must be recipient of the record being accessed
613
- let recordsWriteMessage;
614
- if (incomingMessage.message.descriptor.method === DwnMethodName.Write) {
615
- recordsWriteMessage = incomingMessage.message;
616
- }
617
- else {
618
- // else the incoming message must be a `RecordsDelete` because only `co-update`, `co-delete`, `co-prune` are allowed recipient actions,
619
- // (we do this check in `validateRuleSetRecursively()`)
620
- // and we have already checked that the incoming message is not a `RecordsWrite` above which covers `co-update` path.
621
- recordsWriteMessage = recordChain[recordChain.length - 1];
622
- }
623
- if (recordsWriteMessage.descriptor.recipient === author) {
624
- return;
625
- }
626
- else {
627
- continue;
628
- }
629
- }
630
- // validate the actor is allowed by the current action rule
631
- const ancestorRuleSuccess = yield ProtocolAuthorization.checkActor(author, actionRule, recordChain, protocolDefinition);
632
- if (ancestorRuleSuccess) {
633
- return;
634
- }
635
- }
636
- // No action rules were satisfied, message is not authorized
637
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationActionNotAllowed, `Inbound message action Records${incomingMessageMethod} by author ${incomingMessage.author} not allowed.`);
638
- });
639
- }
640
- /**
641
- * Verifies that writes adhere to the $size constraints if provided
642
- * @throws {Error} if size is exceeded.
643
- */
644
- static verifySizeLimit(incomingMessage, ruleSet) {
645
- const { min = 0, max } = ruleSet.$size || {};
646
- const dataSize = incomingMessage.message.descriptor.dataSize;
647
- if (dataSize < min) {
648
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationMinSizeInvalid, `data size ${dataSize} is less than allowed ${min}`);
649
- }
650
- if (max === undefined) {
651
- return;
652
- }
653
- if (dataSize > max) {
654
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationMaxSizeInvalid, `data size ${dataSize} is more than allowed ${max}`);
655
- }
656
- }
657
- static verifyTagsIfNeeded(incomingMessage, ruleSet) {
658
- if (ruleSet.$tags !== undefined) {
659
- const { tags = {}, protocol, protocolPath } = incomingMessage.message.descriptor;
660
- const _a = ruleSet.$tags, { $allowUndefinedTags, $requiredTags } = _a, properties = __rest(_a, ["$allowUndefinedTags", "$requiredTags"]);
661
- // if $allowUndefinedTags is set to false and there are properties not defined in the schema, an error is thrown
662
- const additionalProperties = $allowUndefinedTags || false;
663
- // if $requiredTags is set, all required tags must be present
664
- const required = $requiredTags || [];
665
- const ajv = new Ajv.default();
666
- const compiledTags = ajv.compile({
667
- type: 'object',
668
- properties,
669
- required,
670
- additionalProperties,
671
- });
672
- const validSchema = compiledTags(tags);
673
- if (!validSchema) {
674
- // the `dataVar` is used to add a qualifier to the error message.
675
- // For example. If the error is related to a tag `status` in a protocol `https://example.protocol` with the protocolPath `example/path`
676
- // the error would be described as `https://example.protocol/example/path/$tags/status'
677
- // without this decorator it would show up as `data/status` which may be confusing.
678
- const schemaError = ajv.errorsText(compiledTags.errors, { dataVar: `${protocol}/${protocolPath}/$tags` });
679
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationTagsInvalidSchema, `tags schema validation error: ${schemaError}`);
680
- }
681
- }
682
- }
683
- /**
684
- * If the given RecordsWrite is not a role record, this method does nothing and succeeds immediately.
685
- *
686
- * Else it verifies the validity of the given `RecordsWrite` as a role record, including:
687
- * 1. The same role has not been assigned to the same entity/recipient.
688
- */
689
- static verifyAsRoleRecordIfNeeded(tenant, incomingMessage, ruleSet, messageStore) {
690
- return __awaiter(this, void 0, void 0, function* () {
691
- if (!ruleSet.$role) {
692
- return;
693
- }
694
- // else this is a role record
695
- const incomingRecordsWrite = incomingMessage;
696
- const recipient = incomingRecordsWrite.message.descriptor.recipient;
697
- if (recipient === undefined) {
698
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationRoleMissingRecipient, 'Role records must have a recipient');
699
- }
700
- const protocolPath = incomingRecordsWrite.message.descriptor.protocolPath;
701
- const filter = {
702
- interface: DwnInterfaceName.Records,
703
- method: DwnMethodName.Write,
704
- isLatestBaseState: true,
705
- protocol: incomingRecordsWrite.message.descriptor.protocol,
706
- protocolPath,
707
- recipient,
708
- };
709
- const parentContextId = Records.getParentContextFromOfContextId(incomingRecordsWrite.message.contextId);
710
- // if this is not the root record, add a prefix filter to the query
711
- if (parentContextId !== '') {
712
- const prefixFilter = FilterUtility.constructPrefixFilterAsRangeFilter(parentContextId);
713
- filter.contextId = prefixFilter;
714
- }
715
- const { messages: matchingMessages } = yield messageStore.query(tenant, [filter]);
716
- const matchingRecords = matchingMessages;
717
- const matchingRecordsExceptIncomingRecordId = matchingRecords.filter((recordsWriteMessage) => recordsWriteMessage.recordId !== incomingRecordsWrite.message.recordId);
718
- if (matchingRecordsExceptIncomingRecordId.length > 0) {
719
- throw new DwnError(DwnErrorCode.ProtocolAuthorizationDuplicateRoleRecipient, `DID '${recipient}' is already recipient of a role record at protocol path '${protocolPath} under the parent context ${parentContextId}.`);
720
- }
721
- });
722
- }
723
- /**
724
- * Checks if the `who: 'author' | 'recipient'` action rule has a matching record in the record chain.
725
- * For cross-protocol `of` references (e.g., `"threads:thread"`), matches against both the protocol URI
726
- * and the protocol path of the ancestor record.
727
- * @returns `true` if the action rule is satisfied; `false` otherwise.
728
- */
729
- static checkActor(author, actionRule, recordChain, composingDefinition) {
730
- return __awaiter(this, void 0, void 0, function* () {
731
- const ofValue = actionRule.of;
732
- // `of` should always be defined when `checkActor` is called, but guard defensively
733
- if (ofValue === undefined) {
734
- return false;
735
- }
736
- let ancestorRecordsWrite;
737
- if (isCrossProtocolRef(ofValue) && (composingDefinition === null || composingDefinition === void 0 ? void 0 : composingDefinition.uses) !== undefined) {
738
- // Cross-protocol `of`: resolve alias to protocol URI and match by both protocol + protocolPath
739
- const parsed = parseCrossProtocolRef(ofValue);
740
- if (parsed !== undefined) {
741
- const refProtocolUri = composingDefinition.uses[parsed.alias];
742
- if (refProtocolUri !== undefined) {
743
- ancestorRecordsWrite = recordChain.find((msg) => msg.descriptor.protocol === refProtocolUri && msg.descriptor.protocolPath === parsed.protocolPath);
744
- }
745
- }
746
- }
747
- else {
748
- // Local `of`: match by protocolPath only (same protocol assumed)
749
- ancestorRecordsWrite = recordChain.find((msg) => msg.descriptor.protocolPath === ofValue);
750
- }
751
- if (ancestorRecordsWrite === undefined) {
752
- // No matching ancestor found in the record chain. Return false to allow the caller
753
- // to continue evaluating other action rules that might authorize the request.
754
- return false;
755
- }
756
- if (actionRule.who === ProtocolActor.Recipient) {
757
- // author of the incoming message must be the recipient of the ancestor message
758
- return author === ancestorRecordsWrite.descriptor.recipient;
759
- }
760
- else { // actionRule.who === ProtocolActor.Author
761
- // author of the incoming message must be the author of the ancestor message
762
- const ancestorAuthor = (yield RecordsWrite.parse(ancestorRecordsWrite)).author;
763
- return author === ancestorAuthor;
764
- }
765
- });
766
- }
767
- /**
768
- * Determines the timestamp that governs which protocol definition version applies to the given RecordsWrite.
769
- * For an update, this is the initial write's `messageTimestamp` (the protocol version is locked at creation time).
770
- * For a new initial write, returns `undefined` — the latest protocol definition should be used because the
771
- * record is being created now and must conform to the current protocol rules.
772
- */
773
- static getGoverningTimestamp(tenant, incomingMessage, messageStore) {
774
- return __awaiter(this, void 0, void 0, function* () {
775
- const existingInitialWrite = yield ProtocolAuthorization.fetchInitialWrite(tenant, incomingMessage.message.recordId, messageStore);
776
- if (existingInitialWrite !== undefined) {
777
- // update case: use the initial write's timestamp
778
- return existingInitialWrite.descriptor.messageTimestamp;
779
- }
780
- // initial write case: validate against the latest protocol definition
781
- return undefined;
782
- });
783
- }
784
- static getTypeName(protocolPath) {
785
- return protocolPath.split('/').slice(-1)[0];
786
- }
787
190
  }
788
191
  //# sourceMappingURL=protocol-authorization.js.map