@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,43 +1,23 @@
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
1
  import { Cid } from '../utils/cid.js';
22
- import { Encoder } from '../utils/encoder.js';
23
- import { Encryption } from '../utils/encryption.js';
24
- import { GeneralJwsBuilder } from '../jose/jws/general/builder.js';
25
2
  import { Jws } from '../utils/jws.js';
26
- import { KeyDerivationScheme } from '../utils/hd-key.js';
27
3
  import { Message } from '../core/message.js';
28
4
  import { PermissionGrant } from '../protocols/permission-grant.js';
29
5
  import { Records } from '../utils/records.js';
30
6
  import { RecordsGrantAuthorization } from '../core/records-grant-authorization.js';
31
7
  import { removeUndefinedProperties } from '../utils/object.js';
32
8
  import { Time } from '../utils/time.js';
9
+ import { createAttestation, createEncryptionProperty, createSignerSignature, validateAttestationIntegrity, } from './records-write-signing.js';
33
10
  import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
34
11
  import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
12
+ import { fetchInitialRecordsWrite, fetchInitialRecordsWriteMessage, fetchNewestRecordsWrite, getAttesters, getInitialWrite, verifyEqualityOfImmutableProperties, } from './records-write-query.js';
35
13
  import { normalizeProtocolUrl, normalizeSchemaUrl, validateProtocolUrlNormalized, validateSchemaUrlNormalized } from '../utils/url.js';
36
14
  /**
37
15
  * A class representing a RecordsWrite DWN message.
38
16
  * NOTE: Unable to extend `AbstractMessage` directly because the incompatible `_message` type, which is not just a generic `<M>` type.
39
17
  */
40
18
  export class RecordsWrite {
19
+ parentContextId;
20
+ _message;
41
21
  /**
42
22
  * Valid JSON message representing this RecordsWrite.
43
23
  * @throws `DwnErrorCode.RecordsWriteMissingSigner` if the message is not signed yet.
@@ -48,12 +28,15 @@ export class RecordsWrite {
48
28
  }
49
29
  return this._message;
50
30
  }
31
+ _author;
51
32
  get author() {
52
33
  return this._author;
53
34
  }
35
+ _signaturePayload;
54
36
  get signaturePayload() {
55
37
  return this._signaturePayload;
56
38
  }
39
+ _owner;
57
40
  /**
58
41
  * The owner DID of the message if owner signature is present in the message; `undefined` otherwise.
59
42
  * This is the logical owner of the message, not to be confused with the actual signer of the owner signature,
@@ -62,6 +45,7 @@ export class RecordsWrite {
62
45
  get owner() {
63
46
  return this._owner;
64
47
  }
48
+ _ownerSignaturePayload;
65
49
  /**
66
50
  * Decoded owner signature payload.
67
51
  */
@@ -94,13 +78,13 @@ export class RecordsWrite {
94
78
  * In the case that the owner signature is signed by the actual DWN owner, this value will be the same as {@link #owner}.
95
79
  */
96
80
  get ownerSignatureSigner() {
97
- var _a;
98
- if (((_a = this._message.authorization) === null || _a === void 0 ? void 0 : _a.ownerSignature) === undefined) {
81
+ if (this._message.authorization?.ownerSignature === undefined) {
99
82
  return undefined;
100
83
  }
101
84
  const signer = Jws.getSignerDid(this._message.authorization.ownerSignature.signatures[0]);
102
85
  return signer;
103
86
  }
87
+ attesters;
104
88
  constructor(message, parentContextId) {
105
89
  this.parentContextId = parentContextId;
106
90
  this._message = message;
@@ -119,33 +103,31 @@ export class RecordsWrite {
119
103
  this._ownerSignaturePayload = Jws.decodePlainObjectPayload(message.authorization.ownerSignature);
120
104
  }
121
105
  }
122
- this.attesters = RecordsWrite.getAttesters(message);
106
+ this.attesters = getAttesters(message);
123
107
  // consider converting isInitialWrite() & getEntryId() into properties for performance and convenience
124
108
  }
125
109
  /**
126
110
  * Parses a RecordsWrite message and returns a {RecordsWrite} instance.
127
111
  */
128
- static parse(recordsWriteMessage) {
129
- return __awaiter(this, void 0, void 0, function* () {
130
- // Make a copy so that the stored copy is not subject to external, unexpected modification.
131
- const message = JSON.parse(JSON.stringify(recordsWriteMessage));
132
- // Validate the message against the JSON schema.
133
- // We strip internal properties that the MessageStore may attach to stored messages
134
- // but are not part of the RecordsWrite JSON schema:
135
- // - `encodedData`: base64url-encoded payload for small records
136
- // - `initialWrite`: the initial RecordsWrite when this message is an update
137
- const _a = message, { encodedData: _, initialWrite: __ } = _a, messageToValidate = __rest(_a, ["encodedData", "initialWrite"]);
138
- Message.validateJsonSchema(messageToValidate);
139
- // asynchronous checks that are required by the constructor to initialize members properly
140
- yield Message.validateSignatureStructure(message.authorization.signature, message.descriptor, 'RecordsWriteSignaturePayload');
141
- if (message.authorization.ownerSignature !== undefined) {
142
- yield Message.validateSignatureStructure(message.authorization.ownerSignature, message.descriptor);
143
- }
144
- yield RecordsWrite.validateAttestationIntegrity(message);
145
- const recordsWrite = new RecordsWrite(message);
146
- yield recordsWrite.validateIntegrity(); // RecordsWrite specific data integrity check
147
- return recordsWrite;
148
- });
112
+ static async parse(recordsWriteMessage) {
113
+ // Make a copy so that the stored copy is not subject to external, unexpected modification.
114
+ const message = JSON.parse(JSON.stringify(recordsWriteMessage));
115
+ // Validate the message against the JSON schema.
116
+ // We strip internal properties that the MessageStore may attach to stored messages
117
+ // but are not part of the RecordsWrite JSON schema:
118
+ // - `encodedData`: base64url-encoded payload for small records
119
+ // - `initialWrite`: the initial RecordsWrite when this message is an update
120
+ const { encodedData: _, initialWrite: __, ...messageToValidate } = message;
121
+ Message.validateJsonSchema(messageToValidate);
122
+ // asynchronous checks that are required by the constructor to initialize members properly
123
+ await Message.validateSignatureStructure(message.authorization.signature, message.descriptor, 'RecordsWriteSignaturePayload');
124
+ if (message.authorization.ownerSignature !== undefined) {
125
+ await Message.validateSignatureStructure(message.authorization.ownerSignature, message.descriptor);
126
+ }
127
+ await validateAttestationIntegrity(message);
128
+ const recordsWrite = new RecordsWrite(message);
129
+ await recordsWrite.validateIntegrity(); // RecordsWrite specific data integrity check
130
+ return recordsWrite;
149
131
  }
150
132
  /**
151
133
  * Creates a RecordsWrite message.
@@ -157,88 +139,84 @@ export class RecordsWrite {
157
139
  * @param options.dateCreated If `undefined`, it will be auto-filled with current time.
158
140
  * @param options.messageTimestamp If `undefined`, it will be auto-filled with current time.
159
141
  * @param options.parentContextId Must be given if this message is for a non-root protocol record.
160
- * If not given, it either means this write is for a root protocol record or a flat-space record.
142
+ * If not given, it means this write is for a root protocol record.
161
143
  */
162
- static create(options) {
163
- return __awaiter(this, void 0, void 0, function* () {
164
- var _a, _b, _c, _d;
165
- if ((options.protocol === undefined && options.protocolPath !== undefined) ||
166
- (options.protocol !== undefined && options.protocolPath === undefined)) {
167
- throw new DwnError(DwnErrorCode.RecordsWriteCreateProtocolAndProtocolPathMutuallyInclusive, '`protocol` and `protocolPath` must both be defined or undefined at the same time');
168
- }
169
- if ((options.data === undefined && options.dataCid === undefined) ||
170
- (options.data !== undefined && options.dataCid !== undefined)) {
171
- throw new DwnError(DwnErrorCode.RecordsWriteCreateDataAndDataCidMutuallyExclusive, 'one and only one parameter between `data` and `dataCid` is required');
172
- }
173
- if ((options.dataCid === undefined && options.dataSize !== undefined) ||
174
- (options.dataCid !== undefined && options.dataSize === undefined)) {
175
- throw new DwnError(DwnErrorCode.RecordsWriteCreateDataCidAndDataSizeMutuallyInclusive, '`dataCid` and `dataSize` must both be defined or undefined at the same time');
176
- }
177
- if (options.signer === undefined && options.delegatedGrant !== undefined) {
178
- throw new DwnError(DwnErrorCode.RecordsWriteCreateMissingSigner, '`signer` must be given when `delegatedGrant` is given');
179
- }
180
- const dataCid = (_a = options.dataCid) !== null && _a !== void 0 ? _a : yield Cid.computeDagPbCidFromBytes(options.data);
181
- const dataSize = (_b = options.dataSize) !== null && _b !== void 0 ? _b : options.data.length;
182
- const currentTime = Time.getCurrentTimestamp();
183
- const descriptor = {
184
- interface: DwnInterfaceName.Records,
185
- method: DwnMethodName.Write,
186
- protocol: options.protocol !== undefined ? normalizeProtocolUrl(options.protocol) : undefined,
187
- protocolPath: options.protocolPath,
188
- recipient: options.recipient,
189
- schema: options.schema !== undefined ? normalizeSchemaUrl(options.schema) : undefined,
190
- tags: options.tags,
191
- parentId: RecordsWrite.getRecordIdFromContextId(options.parentContextId),
192
- dataCid,
193
- dataSize,
194
- dateCreated: (_c = options.dateCreated) !== null && _c !== void 0 ? _c : currentTime,
195
- messageTimestamp: (_d = options.messageTimestamp) !== null && _d !== void 0 ? _d : currentTime,
196
- published: options.published,
197
- datePublished: options.datePublished,
198
- dataFormat: options.dataFormat,
144
+ static async create(options) {
145
+ if (options.protocol === undefined || options.protocolPath === undefined) {
146
+ throw new DwnError(DwnErrorCode.RecordsWriteCreateMissingProtocol, '`protocol` and `protocolPath` are required');
147
+ }
148
+ if ((options.data === undefined && options.dataCid === undefined) ||
149
+ (options.data !== undefined && options.dataCid !== undefined)) {
150
+ throw new DwnError(DwnErrorCode.RecordsWriteCreateDataAndDataCidMutuallyExclusive, 'one and only one parameter between `data` and `dataCid` is required');
151
+ }
152
+ if ((options.dataCid === undefined && options.dataSize !== undefined) ||
153
+ (options.dataCid !== undefined && options.dataSize === undefined)) {
154
+ throw new DwnError(DwnErrorCode.RecordsWriteCreateDataCidAndDataSizeMutuallyInclusive, '`dataCid` and `dataSize` must both be defined or undefined at the same time');
155
+ }
156
+ if (options.signer === undefined && options.delegatedGrant !== undefined) {
157
+ throw new DwnError(DwnErrorCode.RecordsWriteCreateMissingSigner, '`signer` must be given when `delegatedGrant` is given');
158
+ }
159
+ const dataCid = options.dataCid ?? await Cid.computeDagPbCidFromBytes(options.data);
160
+ const dataSize = options.dataSize ?? options.data.length;
161
+ const currentTime = Time.getCurrentTimestamp();
162
+ const descriptor = {
163
+ interface: DwnInterfaceName.Records,
164
+ method: DwnMethodName.Write,
165
+ protocol: normalizeProtocolUrl(options.protocol),
166
+ protocolPath: options.protocolPath,
167
+ recipient: options.recipient,
168
+ schema: options.schema !== undefined ? normalizeSchemaUrl(options.schema) : undefined,
169
+ tags: options.tags,
170
+ parentId: RecordsWrite.getRecordIdFromContextId(options.parentContextId),
171
+ dataCid,
172
+ dataSize,
173
+ dateCreated: options.dateCreated ?? currentTime,
174
+ messageTimestamp: options.messageTimestamp ?? currentTime,
175
+ published: options.published,
176
+ datePublished: options.datePublished,
177
+ dataFormat: options.dataFormat,
178
+ permissionGrantId: options.permissionGrantId,
179
+ };
180
+ // generate `datePublished` if the message is to be published but `datePublished` is not given
181
+ if (options.published === true &&
182
+ options.datePublished === undefined) {
183
+ descriptor.datePublished = currentTime;
184
+ }
185
+ // delete all descriptor properties that are `undefined` else the code will encounter the following IPLD issue when attempting to generate CID:
186
+ // Error: `undefined` is not supported by the IPLD Data Model and cannot be encoded
187
+ removeUndefinedProperties(descriptor);
188
+ // `recordId` computation
189
+ const recordId = options.recordId;
190
+ // `attestation` generation
191
+ const descriptorCid = await Cid.computeCid(descriptor);
192
+ const attestation = await createAttestation(descriptorCid, options.attestationSigners);
193
+ // `encryption` generation
194
+ const encryption = await createEncryptionProperty(options.encryptionInput);
195
+ const message = {
196
+ recordId,
197
+ descriptor
198
+ };
199
+ // assign optional properties only if they exist
200
+ if (attestation !== undefined) {
201
+ message.attestation = attestation;
202
+ }
203
+ if (encryption !== undefined) {
204
+ message.encryption = encryption;
205
+ }
206
+ const recordsWrite = new RecordsWrite(message, options.parentContextId);
207
+ if (options.signer !== undefined) {
208
+ await recordsWrite.sign({
209
+ signer: options.signer,
210
+ delegatedGrant: options.delegatedGrant,
199
211
  permissionGrantId: options.permissionGrantId,
200
- };
201
- // generate `datePublished` if the message is to be published but `datePublished` is not given
202
- if (options.published === true &&
203
- options.datePublished === undefined) {
204
- descriptor.datePublished = currentTime;
205
- }
206
- // delete all descriptor properties that are `undefined` else the code will encounter the following IPLD issue when attempting to generate CID:
207
- // Error: `undefined` is not supported by the IPLD Data Model and cannot be encoded
208
- removeUndefinedProperties(descriptor);
209
- // `recordId` computation
210
- const recordId = options.recordId;
211
- // `attestation` generation
212
- const descriptorCid = yield Cid.computeCid(descriptor);
213
- const attestation = yield RecordsWrite.createAttestation(descriptorCid, options.attestationSigners);
214
- // `encryption` generation
215
- const encryption = yield RecordsWrite.createEncryptionProperty(descriptor, options.encryptionInput);
216
- const message = {
217
- recordId,
218
- descriptor
219
- };
220
- // assign optional properties only if they exist
221
- if (attestation !== undefined) {
222
- message.attestation = attestation;
223
- }
224
- if (encryption !== undefined) {
225
- message.encryption = encryption;
226
- }
227
- const recordsWrite = new RecordsWrite(message, options.parentContextId);
228
- if (options.signer !== undefined) {
229
- yield recordsWrite.sign({
230
- signer: options.signer,
231
- delegatedGrant: options.delegatedGrant,
232
- permissionGrantId: options.permissionGrantId,
233
- protocolRole: options.protocolRole,
234
- authorKeyDeliveryPublicKey: options.authorKeyDeliveryPublicKey,
235
- });
236
- }
237
- return recordsWrite;
238
- });
212
+ protocolRole: options.protocolRole,
213
+ authorKeyDeliveryPublicKey: options.authorKeyDeliveryPublicKey,
214
+ });
215
+ }
216
+ return recordsWrite;
239
217
  }
240
218
  static getRecordIdFromContextId(contextId) {
241
- return contextId === null || contextId === void 0 ? void 0 : contextId.split('/').filter(segment => segment !== '').pop();
219
+ return contextId?.split('/').filter(segment => segment !== '').pop();
242
220
  }
243
221
  /**
244
222
  * Convenience method that creates a message by:
@@ -255,60 +233,57 @@ export class RecordsWrite {
255
233
  * - will be set to the same published date as the given message if it wss already published; else
256
234
  * - will be set to current time (because this is a toggle from unpublished to published)
257
235
  */
258
- static createFrom(options) {
259
- return __awaiter(this, void 0, void 0, function* () {
260
- var _a, _b, _c, _d;
261
- const sourceMessage = options.recordsWriteMessage;
262
- const sourceRecordsWrite = yield RecordsWrite.parse(sourceMessage);
263
- const currentTime = Time.getCurrentTimestamp();
264
- // inherit published value from parent if neither published nor datePublished is specified
265
- const published = (_a = options.published) !== null && _a !== void 0 ? _a : (options.datePublished ? true : sourceMessage.descriptor.published);
266
- // use current time if published but no explicit time given
267
- let datePublished = undefined;
268
- // if given explicitly published dated
269
- if (options.datePublished) {
270
- datePublished = options.datePublished;
271
- }
272
- else {
273
- // if this RecordsWrite will publish the record
274
- if (published) {
275
- // the parent was already published, inherit the same published date
276
- if (sourceMessage.descriptor.published) {
277
- datePublished = sourceMessage.descriptor.datePublished;
278
- }
279
- else {
280
- // this is a toggle from unpublished to published, use current time
281
- datePublished = currentTime;
282
- }
236
+ static async createFrom(options) {
237
+ const sourceMessage = options.recordsWriteMessage;
238
+ const sourceRecordsWrite = await RecordsWrite.parse(sourceMessage);
239
+ const currentTime = Time.getCurrentTimestamp();
240
+ // inherit published value from parent if neither published nor datePublished is specified
241
+ const published = options.published ?? (options.datePublished ? true : sourceMessage.descriptor.published);
242
+ // use current time if published but no explicit time given
243
+ let datePublished = undefined;
244
+ // if given explicitly published dated
245
+ if (options.datePublished) {
246
+ datePublished = options.datePublished;
247
+ }
248
+ else {
249
+ // if this RecordsWrite will publish the record
250
+ if (published) {
251
+ // the parent was already published, inherit the same published date
252
+ if (sourceMessage.descriptor.published) {
253
+ datePublished = sourceMessage.descriptor.datePublished;
254
+ }
255
+ else {
256
+ // this is a toggle from unpublished to published, use current time
257
+ datePublished = currentTime;
283
258
  }
284
259
  }
285
- const createOptions = {
286
- // immutable properties below, just copy from the source message
287
- recipient: sourceMessage.descriptor.recipient,
288
- recordId: sourceMessage.recordId,
289
- dateCreated: sourceMessage.descriptor.dateCreated,
290
- protocol: sourceMessage.descriptor.protocol,
291
- protocolPath: sourceMessage.descriptor.protocolPath,
292
- schema: sourceMessage.descriptor.schema,
293
- parentContextId: Records.getParentContextFromOfContextId(sourceMessage.contextId),
294
- // mutable properties below
295
- messageTimestamp: (_b = options.messageTimestamp) !== null && _b !== void 0 ? _b : currentTime,
296
- published,
297
- datePublished,
298
- tags: options.tags,
299
- data: options.data,
300
- dataCid: options.data ? undefined : sourceMessage.descriptor.dataCid, // if new `data` not given, use value from source message
301
- dataSize: options.data ? undefined : sourceMessage.descriptor.dataSize, // if new `data` not given, use value from source message
302
- dataFormat: (_c = options.dataFormat) !== null && _c !== void 0 ? _c : sourceMessage.descriptor.dataFormat,
303
- protocolRole: (_d = options.protocolRole) !== null && _d !== void 0 ? _d : sourceRecordsWrite.signaturePayload.protocolRole, // if not given, use value from source message
304
- delegatedGrant: options.delegatedGrant,
305
- // finally still need signers
306
- signer: options.signer,
307
- attestationSigners: options.attestationSigners
308
- };
309
- const recordsWrite = yield RecordsWrite.create(createOptions);
310
- return recordsWrite;
311
- });
260
+ }
261
+ const createOptions = {
262
+ // immutable properties below, just copy from the source message
263
+ recipient: sourceMessage.descriptor.recipient,
264
+ recordId: sourceMessage.recordId,
265
+ dateCreated: sourceMessage.descriptor.dateCreated,
266
+ protocol: sourceMessage.descriptor.protocol,
267
+ protocolPath: sourceMessage.descriptor.protocolPath,
268
+ schema: sourceMessage.descriptor.schema,
269
+ parentContextId: Records.getParentContextFromOfContextId(sourceMessage.contextId),
270
+ // mutable properties below
271
+ messageTimestamp: options.messageTimestamp ?? currentTime,
272
+ published,
273
+ datePublished,
274
+ tags: options.tags,
275
+ data: options.data,
276
+ dataCid: options.data ? undefined : sourceMessage.descriptor.dataCid, // if new `data` not given, use value from source message
277
+ dataSize: options.data ? undefined : sourceMessage.descriptor.dataSize, // if new `data` not given, use value from source message
278
+ dataFormat: options.dataFormat ?? sourceMessage.descriptor.dataFormat,
279
+ protocolRole: options.protocolRole ?? sourceRecordsWrite.signaturePayload.protocolRole, // if not given, use value from source message
280
+ delegatedGrant: options.delegatedGrant,
281
+ // finally still need signers
282
+ signer: options.signer,
283
+ attestationSigners: options.attestationSigners
284
+ };
285
+ const recordsWrite = await RecordsWrite.create(createOptions);
286
+ return recordsWrite;
312
287
  }
313
288
  /**
314
289
  * Called by `JSON.stringify(...)` automatically.
@@ -325,503 +300,314 @@ export class RecordsWrite {
325
300
  * root-record upgrade: adding a ProtocolContext recipient entry alongside an existing
326
301
  * ProtocolPath entry so both the owner and context key holders can decrypt.
327
302
  */
328
- encryptSymmetricEncryptionKey(encryptionInput, options) {
329
- return __awaiter(this, void 0, void 0, function* () {
330
- if (options === null || options === void 0 ? void 0 : options.append) {
331
- if (!this._message.encryption) {
332
- throw new DwnError(DwnErrorCode.RecordsWriteMissingEncryption, 'Cannot append recipients: record does not have an existing `encryption` property.');
333
- }
334
- // Build only the new recipients (reuses createEncryptionProperty for ECDH-ES+A256KW logic)
335
- const newEncryption = yield RecordsWrite.createEncryptionProperty(this._message.descriptor, encryptionInput);
336
- if (newEncryption) {
337
- this._message.encryption.recipients.push(...newEncryption.recipients);
338
- }
339
- // In append mode, preserve the author's identity and authorization so
340
- // that signAsOwner() can be called afterwards. The author's signature
341
- // payload will have a stale encryptionCid (since we just appended new
342
- // recipients), but the owner's signature vouches for the
343
- // updated state. validateIntegrity() skips the encryptionCid check on
344
- // the author's signature when an ownerSignature is present.
345
- //
346
- // NOTE: An alternative design would deliver the DEK out-of-band via the
347
- // key-delivery protocol (as a field on the contextKey record) instead of
348
- // mutating the record's encryption property. That avoids the stale
349
- // encryptionCid issue entirely but adds complexity to the read path and
350
- // the contextKey schema. We chose the in-record approach because it keeps
351
- // records self-contained and the read/decrypt path unchanged.
352
- }
353
- else {
354
- this._message.encryption = yield RecordsWrite.createEncryptionProperty(this._message.descriptor, encryptionInput);
355
- // Full replacement invalidates the authorization — caller must re-sign.
356
- delete this._message.authorization;
357
- this._signaturePayload = undefined;
358
- this._author = undefined;
359
- }
360
- });
303
+ async encryptSymmetricEncryptionKey(encryptionInput, options) {
304
+ if (options?.append) {
305
+ if (!this._message.encryption) {
306
+ throw new DwnError(DwnErrorCode.RecordsWriteMissingEncryption, 'Cannot append recipients: record does not have an existing `encryption` property.');
307
+ }
308
+ // Build only the new recipients (reuses createEncryptionProperty for ECDH-ES+A256KW logic)
309
+ const newEncryption = await createEncryptionProperty(encryptionInput);
310
+ if (newEncryption) {
311
+ this._message.encryption.recipients.push(...newEncryption.recipients);
312
+ }
313
+ // In append mode, preserve the author's identity and authorization so
314
+ // that signAsOwner() can be called afterwards. The author's signature
315
+ // payload will have a stale encryptionCid (since we just appended new
316
+ // recipients), but the owner's signature vouches for the
317
+ // updated state. validateIntegrity() skips the encryptionCid check on
318
+ // the author's signature when an ownerSignature is present.
319
+ //
320
+ // NOTE: An alternative design would deliver the DEK out-of-band via the
321
+ // key-delivery protocol (as a field on the contextKey record) instead of
322
+ // mutating the record's encryption property. That avoids the stale
323
+ // encryptionCid issue entirely but adds complexity to the read path and
324
+ // the contextKey schema. We chose the in-record approach because it keeps
325
+ // records self-contained and the read/decrypt path unchanged.
326
+ }
327
+ else {
328
+ this._message.encryption = await createEncryptionProperty(encryptionInput);
329
+ // Full replacement invalidates the authorization — caller must re-sign.
330
+ delete this._message.authorization;
331
+ this._signaturePayload = undefined;
332
+ this._author = undefined;
333
+ }
361
334
  }
362
335
  /**
363
336
  * Signs the RecordsWrite, the signer is commonly the author, but can also be a delegate.
364
337
  */
365
- sign(options) {
366
- return __awaiter(this, void 0, void 0, function* () {
367
- var _a;
368
- const { signer, delegatedGrant, permissionGrantId, protocolRole, authorKeyDeliveryPublicKey } = options;
369
- // compute delegated grant ID and author if delegated grant is given
370
- let delegatedGrantId;
371
- let authorDid;
372
- if (delegatedGrant !== undefined) {
373
- delegatedGrantId = yield Message.getCid(delegatedGrant);
374
- authorDid = Jws.getSignerDid(delegatedGrant.authorization.signature.signatures[0]);
375
- }
376
- else {
377
- authorDid = Jws.extractDid(signer.keyId);
378
- }
379
- const descriptor = this._message.descriptor;
380
- const descriptorCid = yield Cid.computeCid(descriptor);
381
- // compute `recordId` if not given at construction time
382
- this._message.recordId = (_a = this._message.recordId) !== null && _a !== void 0 ? _a : yield RecordsWrite.getEntryId(authorDid, descriptor);
383
- // compute `contextId` if this is a protocol-space record
384
- if (this._message.descriptor.protocol !== undefined) {
385
- // if `parentContextId` is not given, this is a root protocol record
386
- if (this.parentContextId === undefined || this.parentContextId === '') {
387
- this._message.contextId = this._message.recordId;
388
- }
389
- else {
390
- // else this is a non-root protocol record
391
- this._message.contextId = this.parentContextId + '/' + this._message.recordId;
392
- }
393
- }
394
- // `signature` generation
395
- const signature = yield RecordsWrite.createSignerSignature({
396
- recordId: this._message.recordId,
397
- contextId: this._message.contextId,
398
- descriptorCid,
399
- attestation: this._message.attestation,
400
- encryption: this._message.encryption,
401
- signer,
402
- delegatedGrantId,
403
- permissionGrantId,
404
- protocolRole
405
- });
406
- this._message.authorization = { signature };
407
- if (delegatedGrant !== undefined) {
408
- this._message.authorization.authorDelegatedGrant = delegatedGrant;
409
- }
410
- if (authorKeyDeliveryPublicKey !== undefined) {
411
- this._message.authorization.authorKeyDeliveryPublicKey = authorKeyDeliveryPublicKey;
412
- }
413
- // there is opportunity to optimize here as the payload is constructed within `createAuthorization(...)`
414
- this._signaturePayload = Jws.decodePlainObjectPayload(signature);
415
- this._author = authorDid;
338
+ async sign(options) {
339
+ const { signer, delegatedGrant, permissionGrantId, protocolRole, authorKeyDeliveryPublicKey } = options;
340
+ // compute delegated grant ID and author if delegated grant is given
341
+ let delegatedGrantId;
342
+ let authorDid;
343
+ if (delegatedGrant !== undefined) {
344
+ delegatedGrantId = await Message.getCid(delegatedGrant);
345
+ authorDid = Jws.getSignerDid(delegatedGrant.authorization.signature.signatures[0]);
346
+ }
347
+ else {
348
+ authorDid = Jws.extractDid(signer.keyId);
349
+ }
350
+ const descriptor = this._message.descriptor;
351
+ const descriptorCid = await Cid.computeCid(descriptor);
352
+ // compute `recordId` if not given at construction time
353
+ this._message.recordId = this._message.recordId ?? await RecordsWrite.getEntryId(authorDid, descriptor);
354
+ // compute `contextId` all records belong to a protocol
355
+ if (this.parentContextId === undefined || this.parentContextId === '') {
356
+ // root protocol record
357
+ this._message.contextId = this._message.recordId;
358
+ }
359
+ else {
360
+ // non-root protocol record
361
+ this._message.contextId = this.parentContextId + '/' + this._message.recordId;
362
+ }
363
+ // `signature` generation
364
+ const signature = await createSignerSignature({
365
+ recordId: this._message.recordId,
366
+ contextId: this._message.contextId, // contextId is computed just above, always defined here
367
+ descriptorCid,
368
+ attestation: this._message.attestation,
369
+ encryption: this._message.encryption,
370
+ signer,
371
+ delegatedGrantId,
372
+ permissionGrantId,
373
+ protocolRole
416
374
  });
375
+ this._message.authorization = { signature };
376
+ if (delegatedGrant !== undefined) {
377
+ this._message.authorization.authorDelegatedGrant = delegatedGrant;
378
+ }
379
+ if (authorKeyDeliveryPublicKey !== undefined) {
380
+ this._message.authorization.authorKeyDeliveryPublicKey = authorKeyDeliveryPublicKey;
381
+ }
382
+ // there is opportunity to optimize here as the payload is constructed within `createAuthorization(...)`
383
+ this._signaturePayload = Jws.decodePlainObjectPayload(signature);
384
+ this._author = authorDid;
417
385
  }
418
386
  /**
419
387
  * Signs the `RecordsWrite` as the DWN owner.
420
388
  * This is used when the DWN owner wants to retain a copy of a message that the owner did not author.
421
389
  * NOTE: requires the `RecordsWrite` to already have the author's signature.
422
390
  */
423
- signAsOwner(signer) {
424
- return __awaiter(this, void 0, void 0, function* () {
425
- if (this._author === undefined) {
426
- throw new DwnError(DwnErrorCode.RecordsWriteSignAsOwnerUnknownAuthor, 'Unable to sign as owner without message signature because owner needs to sign over `recordId` which depends on author DID.');
427
- }
428
- const descriptor = this._message.descriptor;
429
- const ownerSignature = yield Message.createSignature(descriptor, signer);
430
- this._message.authorization.ownerSignature = ownerSignature;
431
- this._ownerSignaturePayload = Jws.decodePlainObjectPayload(ownerSignature);
432
- this._owner = Jws.extractDid(signer.keyId);
433
- ;
434
- });
391
+ async signAsOwner(signer) {
392
+ if (this._author === undefined) {
393
+ throw new DwnError(DwnErrorCode.RecordsWriteSignAsOwnerUnknownAuthor, 'Unable to sign as owner without message signature because owner needs to sign over `recordId` which depends on author DID.');
394
+ }
395
+ const descriptor = this._message.descriptor;
396
+ const ownerSignature = await Message.createSignature(descriptor, signer);
397
+ this._message.authorization.ownerSignature = ownerSignature;
398
+ this._ownerSignaturePayload = Jws.decodePlainObjectPayload(ownerSignature);
399
+ this._owner = Jws.extractDid(signer.keyId);
435
400
  }
436
401
  /**
437
402
  * Signs the `RecordsWrite` as the DWN owner-delegate.
438
403
  * This is used when a DWN owner-delegate wants to retain a copy of a message that the owner did not author.
439
404
  * NOTE: requires the `RecordsWrite` to already have the author's signature.
440
405
  */
441
- signAsOwnerDelegate(signer, delegatedGrant) {
442
- return __awaiter(this, void 0, void 0, function* () {
443
- if (this._author === undefined) {
444
- throw new DwnError(DwnErrorCode.RecordsWriteSignAsOwnerDelegateUnknownAuthor, 'Unable to sign as owner delegate without message signature because owner delegate needs to sign over `recordId` which depends on author DID.');
445
- }
446
- const delegatedGrantId = yield Message.getCid(delegatedGrant);
447
- const descriptor = this._message.descriptor;
448
- const ownerSignature = yield Message.createSignature(descriptor, signer, { delegatedGrantId });
449
- this._message.authorization.ownerSignature = ownerSignature;
450
- this._message.authorization.ownerDelegatedGrant = delegatedGrant;
451
- this._ownerSignaturePayload = Jws.decodePlainObjectPayload(ownerSignature);
452
- this._owner = Jws.getSignerDid(delegatedGrant.authorization.signature.signatures[0]);
453
- });
406
+ async signAsOwnerDelegate(signer, delegatedGrant) {
407
+ if (this._author === undefined) {
408
+ throw new DwnError(DwnErrorCode.RecordsWriteSignAsOwnerDelegateUnknownAuthor, 'Unable to sign as owner delegate without message signature because owner delegate needs to sign over `recordId` which depends on author DID.');
409
+ }
410
+ const delegatedGrantId = await Message.getCid(delegatedGrant);
411
+ const descriptor = this._message.descriptor;
412
+ const ownerSignature = await Message.createSignature(descriptor, signer, { delegatedGrantId });
413
+ this._message.authorization.ownerSignature = ownerSignature;
414
+ this._message.authorization.ownerDelegatedGrant = delegatedGrant;
415
+ this._ownerSignaturePayload = Jws.decodePlainObjectPayload(ownerSignature);
416
+ this._owner = Jws.getSignerDid(delegatedGrant.authorization.signature.signatures[0]);
454
417
  }
455
418
  /**
456
419
  * Validates the integrity of the RecordsWrite message assuming the message passed basic schema validation.
457
420
  * There is opportunity to integrate better with `validateSchema(...)`
458
421
  */
459
- validateIntegrity() {
460
- return __awaiter(this, void 0, void 0, function* () {
461
- var _a;
462
- // if the new message is the initial write
463
- const isInitialWrite = yield this.isInitialWrite();
464
- if (isInitialWrite) {
465
- // `messageTimestamp` and `dateCreated` equality check
466
- const dateRecordCreated = this.message.descriptor.dateCreated;
467
- const messageTimestamp = this.message.descriptor.messageTimestamp;
468
- if (messageTimestamp !== dateRecordCreated) {
469
- throw new DwnError(DwnErrorCode.RecordsWriteValidateIntegrityDateCreatedMismatch, `messageTimestamp ${messageTimestamp} must match dateCreated ${dateRecordCreated} for the initial write`);
470
- }
471
- // if the message is also a protocol context root, the `contextId` must match the expected deterministic value
472
- if (this.message.descriptor.protocol !== undefined &&
473
- this.message.descriptor.parentId === undefined) {
474
- const expectedContextId = yield this.getEntryId();
475
- if (this.message.contextId !== expectedContextId) {
476
- throw new DwnError(DwnErrorCode.RecordsWriteValidateIntegrityContextIdMismatch, `contextId in message: ${this.message.contextId} does not match deterministic contextId: ${expectedContextId}`);
477
- }
478
- }
479
- }
480
- // NOTE: validateSignatureStructure() call earlier enforces the presence of `authorization` and thus `signature` in RecordsWrite
481
- const signaturePayload = this.signaturePayload;
482
- // make sure the `recordId` in message is the same as the `recordId` in the payload of the message signature
483
- if (this.message.recordId !== signaturePayload.recordId) {
484
- throw new DwnError(DwnErrorCode.RecordsWriteValidateIntegrityRecordIdUnauthorized, `recordId in message ${this.message.recordId} does not match recordId in authorization: ${signaturePayload.recordId}`);
485
- }
486
- // if `contextId` is given in message, make sure the same `contextId` is in the payload of the message signature
487
- if (this.message.contextId !== signaturePayload.contextId) {
488
- throw new DwnError(DwnErrorCode.RecordsWriteValidateIntegrityContextIdNotInSignerSignaturePayload, `contextId in message ${this.message.contextId} does not match contextId in authorization: ${signaturePayload.contextId}`);
489
- }
490
- yield Records.validateDelegatedGrantReferentialIntegrity(this.message, signaturePayload, this.ownerSignaturePayload);
491
- // if `attestation` is given in message, make sure the correct `attestationCid` is in the payload of the message signature
492
- if (signaturePayload.attestationCid !== undefined) {
493
- const expectedAttestationCid = yield Cid.computeCid(this.message.attestation);
494
- const actualAttestationCid = signaturePayload.attestationCid;
495
- if (actualAttestationCid !== expectedAttestationCid) {
496
- throw new DwnError(DwnErrorCode.RecordsWriteValidateIntegrityAttestationMismatch, `CID ${expectedAttestationCid} of attestation property in message does not match attestationCid in authorization: ${actualAttestationCid}`);
422
+ async validateIntegrity() {
423
+ // if the new message is the initial write
424
+ const isInitialWrite = await this.isInitialWrite();
425
+ if (isInitialWrite) {
426
+ // `messageTimestamp` and `dateCreated` equality check
427
+ const dateRecordCreated = this.message.descriptor.dateCreated;
428
+ const messageTimestamp = this.message.descriptor.messageTimestamp;
429
+ if (messageTimestamp !== dateRecordCreated) {
430
+ throw new DwnError(DwnErrorCode.RecordsWriteValidateIntegrityDateCreatedMismatch, `messageTimestamp ${messageTimestamp} must match dateCreated ${dateRecordCreated} for the initial write`);
431
+ }
432
+ // if the message is a protocol context root, the `contextId` must match the expected deterministic value
433
+ if (this.message.descriptor.parentId === undefined) {
434
+ const expectedContextId = await this.getEntryId();
435
+ if (this.message.contextId !== expectedContextId) {
436
+ throw new DwnError(DwnErrorCode.RecordsWriteValidateIntegrityContextIdMismatch, `contextId in message: ${this.message.contextId} does not match deterministic contextId: ${expectedContextId}`);
497
437
  }
498
438
  }
499
- // If `encryption` is given in message, make sure the correct `encryptionCid`
500
- // is in the payload of the message signature UNLESS the message has an
501
- // ownerSignature. When the DWN owner appends recipients to an
502
- // externally-authored record (reactive root-record upgrade), the author's
503
- // encryptionCid becomes stale. The owner's signature vouches for the
504
- // updated encryption property, so the mismatch is expected and safe.
505
- const hasOwnerSignature = ((_a = this.message.authorization) === null || _a === void 0 ? void 0 : _a.ownerSignature) !== undefined;
506
- if (signaturePayload.encryptionCid !== undefined && !hasOwnerSignature) {
507
- const expectedEncryptionCid = yield Cid.computeCid(this.message.encryption);
508
- const actualEncryptionCid = signaturePayload.encryptionCid;
509
- if (actualEncryptionCid !== expectedEncryptionCid) {
510
- throw new DwnError(DwnErrorCode.RecordsWriteValidateIntegrityEncryptionCidMismatch, `CID ${expectedEncryptionCid} of encryption property in message does not match encryptionCid in authorization: ${actualEncryptionCid}`);
511
- }
512
- }
513
- if (this.message.descriptor.protocol !== undefined) {
514
- validateProtocolUrlNormalized(this.message.descriptor.protocol);
515
- }
516
- if (this.message.descriptor.schema !== undefined) {
517
- validateSchemaUrlNormalized(this.message.descriptor.schema);
518
- }
519
- Time.validateTimestamp(this.message.descriptor.messageTimestamp);
520
- Time.validateTimestamp(this.message.descriptor.dateCreated);
521
- if (this.message.descriptor.datePublished) {
522
- Time.validateTimestamp(this.message.descriptor.datePublished);
523
- }
524
- });
525
- }
526
- /**
527
- * Validates the structural integrity of the `attestation` property.
528
- * NOTE: Cryptographic verification of attestation signatures is performed in `authenticate()`.
529
- */
530
- static validateAttestationIntegrity(message) {
531
- return __awaiter(this, void 0, void 0, function* () {
532
- if (message.attestation === undefined) {
533
- return;
534
- }
535
- // TODO: multi-attesters to be unblocked by #205 - Revisit database interfaces (https://github.com/enboxorg/enbox/issues/205)
536
- if (message.attestation.signatures.length !== 1) {
537
- throw new DwnError(DwnErrorCode.RecordsWriteAttestationIntegrityMoreThanOneSignature, `Currently implementation only supports 1 attester, but got ${message.attestation.signatures.length}`);
538
- }
539
- const payloadJson = Jws.decodePlainObjectPayload(message.attestation);
540
- const { descriptorCid } = payloadJson;
541
- // `descriptorCid` validation - ensure that the provided descriptorCid matches the CID of the actual message
542
- const expectedDescriptorCid = yield Cid.computeCid(message.descriptor);
543
- if (descriptorCid !== expectedDescriptorCid) {
544
- throw new DwnError(DwnErrorCode.RecordsWriteAttestationIntegrityDescriptorCidMismatch, `descriptorCid ${descriptorCid} does not match expected descriptorCid ${expectedDescriptorCid}`);
439
+ }
440
+ // NOTE: validateSignatureStructure() call earlier enforces the presence of `authorization` and thus `signature` in RecordsWrite
441
+ const signaturePayload = this.signaturePayload;
442
+ // make sure the `recordId` in message is the same as the `recordId` in the payload of the message signature
443
+ if (this.message.recordId !== signaturePayload.recordId) {
444
+ throw new DwnError(DwnErrorCode.RecordsWriteValidateIntegrityRecordIdUnauthorized, `recordId in message ${this.message.recordId} does not match recordId in authorization: ${signaturePayload.recordId}`);
445
+ }
446
+ // if `contextId` is given in message, make sure the same `contextId` is in the payload of the message signature
447
+ if (this.message.contextId !== signaturePayload.contextId) {
448
+ throw new DwnError(DwnErrorCode.RecordsWriteValidateIntegrityContextIdNotInSignerSignaturePayload, `contextId in message ${this.message.contextId} does not match contextId in authorization: ${signaturePayload.contextId}`);
449
+ }
450
+ await Records.validateDelegatedGrantReferentialIntegrity(this.message, signaturePayload, this.ownerSignaturePayload);
451
+ // if `attestation` is given in message, make sure the correct `attestationCid` is in the payload of the message signature
452
+ if (signaturePayload.attestationCid !== undefined) {
453
+ const expectedAttestationCid = await Cid.computeCid(this.message.attestation);
454
+ const actualAttestationCid = signaturePayload.attestationCid;
455
+ if (actualAttestationCid !== expectedAttestationCid) {
456
+ throw new DwnError(DwnErrorCode.RecordsWriteValidateIntegrityAttestationMismatch, `CID ${expectedAttestationCid} of attestation property in message does not match attestationCid in authorization: ${actualAttestationCid}`);
545
457
  }
546
- // check to ensure that no other unexpected properties exist in payload.
547
- const propertyCount = Object.keys(payloadJson).length;
548
- if (propertyCount > 1) {
549
- throw new DwnError(DwnErrorCode.RecordsWriteAttestationIntegrityInvalidPayloadProperty, `Only 'descriptorCid' is allowed in attestation payload, but got ${propertyCount} properties.`);
458
+ }
459
+ // If `encryption` is given in message, make sure the correct `encryptionCid`
460
+ // is in the payload of the message signature — UNLESS the message has an
461
+ // ownerSignature. When the DWN owner appends recipients to an
462
+ // externally-authored record (reactive root-record upgrade), the author's
463
+ // encryptionCid becomes stale. The owner's signature vouches for the
464
+ // updated encryption property, so the mismatch is expected and safe.
465
+ const hasOwnerSignature = this.message.authorization?.ownerSignature !== undefined;
466
+ if (signaturePayload.encryptionCid !== undefined && !hasOwnerSignature) {
467
+ const expectedEncryptionCid = await Cid.computeCid(this.message.encryption);
468
+ const actualEncryptionCid = signaturePayload.encryptionCid;
469
+ if (actualEncryptionCid !== expectedEncryptionCid) {
470
+ throw new DwnError(DwnErrorCode.RecordsWriteValidateIntegrityEncryptionCidMismatch, `CID ${expectedEncryptionCid} of encryption property in message does not match encryptionCid in authorization: ${actualEncryptionCid}`);
550
471
  }
551
- });
472
+ }
473
+ validateProtocolUrlNormalized(this.message.descriptor.protocol);
474
+ if (this.message.descriptor.schema !== undefined) {
475
+ validateSchemaUrlNormalized(this.message.descriptor.schema);
476
+ }
477
+ Time.validateTimestamp(this.message.descriptor.messageTimestamp);
478
+ Time.validateTimestamp(this.message.descriptor.dateCreated);
479
+ if (this.message.descriptor.datePublished) {
480
+ Time.validateTimestamp(this.message.descriptor.datePublished);
481
+ }
552
482
  }
553
- ;
554
483
  /**
555
484
  * Computes the deterministic Entry ID of this message.
556
485
  */
557
- getEntryId() {
558
- return __awaiter(this, void 0, void 0, function* () {
559
- const entryId = yield RecordsWrite.getEntryId(this.author, this.message.descriptor);
560
- return entryId;
561
- });
486
+ async getEntryId() {
487
+ const entryId = await RecordsWrite.getEntryId(this.author, this.message.descriptor);
488
+ return entryId;
562
489
  }
563
490
  ;
564
491
  /**
565
492
  * Computes the deterministic Entry ID of this message.
566
493
  */
567
- static getEntryId(author, descriptor) {
568
- return __awaiter(this, void 0, void 0, function* () {
569
- if (author === undefined) {
570
- throw new DwnError(DwnErrorCode.RecordsWriteGetEntryIdUndefinedAuthor, 'Property `author` is needed to compute entry ID.');
571
- }
572
- const entryIdInput = Object.assign(Object.assign({}, descriptor), { author });
573
- const cid = yield Cid.computeCid(entryIdInput);
574
- return cid;
575
- });
494
+ static async getEntryId(author, descriptor) {
495
+ if (author === undefined) {
496
+ throw new DwnError(DwnErrorCode.RecordsWriteGetEntryIdUndefinedAuthor, 'Property `author` is needed to compute entry ID.');
497
+ }
498
+ const entryIdInput = { ...descriptor, author };
499
+ const cid = await Cid.computeCid(entryIdInput);
500
+ return cid;
576
501
  }
577
502
  ;
578
503
  /**
579
504
  * Checks if the given message is the initial entry of a record.
580
505
  */
581
- isInitialWrite() {
582
- return __awaiter(this, void 0, void 0, function* () {
583
- const entryId = yield this.getEntryId();
584
- return (entryId === this.message.recordId);
585
- });
586
- }
587
- constructIndexes(isLatestBaseState) {
588
- return __awaiter(this, void 0, void 0, function* () {
589
- const message = this.message;
590
- // we want to process tags separately from the rest of descriptors as it is an object and not a primitive KeyValue type.
591
- const _a = message.descriptor, { tags } = _a, descriptor = __rest(_a, ["tags"]);
592
- delete descriptor.published; // handle `published` specifically further down
593
- let indexes = Object.assign(Object.assign({}, descriptor), { isLatestBaseState, published: !!message.descriptor.published, author: this.author, recordId: message.recordId, entryId: yield RecordsWrite.getEntryId(this.author, this.message.descriptor) });
594
- // in order to avoid name clashes with first-class index keys
595
- // we build the indexes with `tag.property_name` for each tag property.
596
- // we only index tags if the message is the latest base state, as that's the only time filtering for tags is relevant.
597
- if (tags !== undefined && isLatestBaseState === true) {
598
- const flattenedTags = Records.buildTagIndexes(Object.assign({}, tags));
599
- indexes = Object.assign(Object.assign({}, indexes), flattenedTags);
600
- }
601
- // add additional indexes to optional values if given
602
- // TODO: index multi-attesters to be unblocked by #205 - Revisit database interfaces (https://github.com/enboxorg/enbox/issues/205)
603
- if (this.attesters.length > 0) {
604
- indexes.attester = this.attesters[0];
605
- }
606
- if (message.contextId !== undefined) {
607
- indexes.contextId = message.contextId;
608
- }
609
- return indexes;
610
- });
506
+ async isInitialWrite() {
507
+ const entryId = await this.getEntryId();
508
+ return (entryId === this.message.recordId);
509
+ }
510
+ async constructIndexes(isLatestBaseState) {
511
+ const message = this.message;
512
+ // we want to process tags separately from the rest of descriptors as it is an object and not a primitive KeyValue type.
513
+ const { tags, ...descriptor } = message.descriptor;
514
+ delete descriptor.published; // handle `published` specifically further down
515
+ let indexes = {
516
+ ...descriptor,
517
+ isLatestBaseState,
518
+ published: !!message.descriptor.published,
519
+ author: this.author, //author will not be undefined when indexes are constructed as it's been authorized
520
+ recordId: message.recordId,
521
+ entryId: await RecordsWrite.getEntryId(this.author, this.message.descriptor)
522
+ };
523
+ // in order to avoid name clashes with first-class index keys
524
+ // we build the indexes with `tag.property_name` for each tag property.
525
+ // we only index tags if the message is the latest base state, as that's the only time filtering for tags is relevant.
526
+ if (tags !== undefined && isLatestBaseState === true) {
527
+ const flattenedTags = Records.buildTagIndexes({ ...tags });
528
+ indexes = { ...indexes, ...flattenedTags };
529
+ }
530
+ // add additional indexes to optional values if given
531
+ // TODO: index multiple attesters (https://github.com/enboxorg/enbox/issues/223)
532
+ if (this.attesters.length > 0) {
533
+ indexes.attester = this.attesters[0];
534
+ }
535
+ indexes.contextId = message.contextId;
536
+ return indexes;
611
537
  }
612
538
  /**
613
539
  * Authorizes the author-delegate who signed this message.
614
540
  * @param messageStore Used to check if the grant has been revoked.
615
541
  */
616
- authorizeAuthorDelegate(messageStore) {
617
- return __awaiter(this, void 0, void 0, function* () {
618
- const delegatedGrant = yield PermissionGrant.parse(this.message.authorization.authorDelegatedGrant);
619
- yield RecordsGrantAuthorization.authorizeWrite({
620
- recordsWriteMessage: this.message,
621
- expectedGrantor: this.author,
622
- expectedGrantee: this.signer,
623
- permissionGrant: delegatedGrant,
624
- messageStore
625
- });
542
+ async authorizeAuthorDelegate(messageStore) {
543
+ const delegatedGrant = PermissionGrant.parse(this.message.authorization.authorDelegatedGrant);
544
+ await RecordsGrantAuthorization.authorizeWrite({
545
+ recordsWriteMessage: this.message,
546
+ expectedGrantor: this.author,
547
+ expectedGrantee: this.signer,
548
+ permissionGrant: delegatedGrant,
549
+ messageStore
626
550
  });
627
551
  }
628
552
  /**
629
553
  * Authorizes the owner-delegate who signed this message.
630
554
  * @param messageStore Used to check if the grant has been revoked.
631
555
  */
632
- authorizeOwnerDelegate(messageStore) {
633
- return __awaiter(this, void 0, void 0, function* () {
634
- const delegatedGrant = yield PermissionGrant.parse(this.message.authorization.ownerDelegatedGrant);
635
- yield RecordsGrantAuthorization.authorizeWrite({
636
- recordsWriteMessage: this.message,
637
- expectedGrantor: this.owner,
638
- expectedGrantee: this.ownerSignatureSigner,
639
- permissionGrant: delegatedGrant,
640
- messageStore
641
- });
556
+ async authorizeOwnerDelegate(messageStore) {
557
+ const delegatedGrant = PermissionGrant.parse(this.message.authorization.ownerDelegatedGrant);
558
+ await RecordsGrantAuthorization.authorizeWrite({
559
+ recordsWriteMessage: this.message,
560
+ expectedGrantor: this.owner,
561
+ expectedGrantee: this.ownerSignatureSigner,
562
+ permissionGrant: delegatedGrant,
563
+ messageStore
642
564
  });
643
565
  }
644
566
  /**
645
567
  * Checks if the given message is the initial entry of a record.
646
568
  */
647
- static isInitialWrite(message) {
648
- return __awaiter(this, void 0, void 0, function* () {
649
- // can't be the initial write if the message is not a Records Write
650
- if (message.descriptor.interface !== DwnInterfaceName.Records ||
651
- message.descriptor.method !== DwnMethodName.Write) {
652
- return false;
653
- }
654
- const recordsWriteMessage = message;
655
- const author = Message.getAuthor(recordsWriteMessage);
656
- const entryId = yield RecordsWrite.getEntryId(author, recordsWriteMessage.descriptor);
657
- return (entryId === recordsWriteMessage.recordId);
658
- });
659
- }
660
- /**
661
- * Creates the JWE `encryption` property if encryption input is given. Else `undefined` is returned.
662
- * Uses ECDH-ES+A256KW key agreement with X25519 and AEAD content encryption (A256GCM or XC20P).
663
- * @param descriptor Descriptor of the `RecordsWrite` message which contains the information needed by key path derivation schemes.
664
- */
665
- /**
666
- * Creates the JWE `encryption` property if encryption input is given. Else `undefined` is returned.
667
- * Uses ECDH-ES+A256KW key agreement with X25519 and AEAD content encryption (A256GCM or XC20P).
668
- * @param descriptor Descriptor of the `RecordsWrite` message which contains the information needed by key path derivation schemes.
669
- * @param encryptionInput The encryption input containing CEK, IV, and recipient key encryption inputs.
670
- * @param tag The authentication tag from the AEAD content encryption (stored in the JWE, separate from the ciphertext).
671
- */
672
- /**
673
- * Creates the JWE `encryption` property if encryption input is given. Else `undefined` is returned.
674
- * Uses ECDH-ES+A256KW key agreement with X25519 and AEAD content encryption (A256GCM or XC20P).
675
- * @param descriptor Descriptor of the `RecordsWrite` message which contains the information needed by key path derivation schemes.
676
- * @param encryptionInput The encryption input containing CEK, IV, authentication tag, and recipient key encryption inputs.
677
- */
678
- static createEncryptionProperty(descriptor, encryptionInput) {
679
- return __awaiter(this, void 0, void 0, function* () {
680
- if (encryptionInput === undefined) {
681
- return undefined;
682
- }
683
- // Validate derivation scheme prerequisites
684
- for (const keyEncryptionInput of encryptionInput.keyEncryptionInputs) {
685
- if (keyEncryptionInput.derivationScheme === KeyDerivationScheme.ProtocolPath && descriptor.protocol === undefined) {
686
- throw new DwnError(DwnErrorCode.RecordsWriteMissingProtocol, '`protocols` encryption scheme cannot be applied to record without the `protocol` property.');
687
- }
688
- if (keyEncryptionInput.derivationScheme === KeyDerivationScheme.Schemas && descriptor.schema === undefined) {
689
- throw new DwnError(DwnErrorCode.RecordsWriteMissingSchema, '`schemas` encryption scheme cannot be applied to record without the `schema` property.');
690
- }
691
- }
692
- // Build the JWE structure. The authentication tag comes from the AEAD encryption of record data.
693
- const jwe = yield Encryption.buildJwe(encryptionInput, encryptionInput.authenticationTag);
694
- return jwe;
695
- });
569
+ static async isInitialWrite(message) {
570
+ // can't be the initial write if the message is not a Records Write
571
+ if (message.descriptor.interface !== DwnInterfaceName.Records ||
572
+ message.descriptor.method !== DwnMethodName.Write) {
573
+ return false;
574
+ }
575
+ const recordsWriteMessage = message;
576
+ const author = Message.getAuthor(recordsWriteMessage);
577
+ const entryId = await RecordsWrite.getEntryId(author, recordsWriteMessage.descriptor);
578
+ return (entryId === recordsWriteMessage.recordId);
696
579
  }
697
- /**
698
- * Creates the `attestation` property of a RecordsWrite message if given signature inputs; returns `undefined` otherwise.
699
- */
700
- static createAttestation(descriptorCid, signers) {
701
- return __awaiter(this, void 0, void 0, function* () {
702
- if (signers === undefined || signers.length === 0) {
703
- return undefined;
704
- }
705
- const attestationPayload = { descriptorCid };
706
- const attestationPayloadBytes = Encoder.objectToBytes(attestationPayload);
707
- const builder = yield GeneralJwsBuilder.create(attestationPayloadBytes, signers);
708
- return builder.getJws();
709
- });
580
+ /** Delegate to `createAttestation` in `records-write-signing.ts`. */
581
+ static async createAttestation(descriptorCid, signers) {
582
+ return createAttestation(descriptorCid, signers);
710
583
  }
711
- /**
712
- * Creates the `signature` property in the `authorization` of a `RecordsWrite` message.
713
- */
714
- static createSignerSignature(input) {
715
- return __awaiter(this, void 0, void 0, function* () {
716
- const { recordId, contextId, descriptorCid, attestation, encryption, signer, delegatedGrantId, permissionGrantId, protocolRole } = input;
717
- const attestationCid = attestation ? yield Cid.computeCid(attestation) : undefined;
718
- const encryptionCid = encryption ? yield Cid.computeCid(encryption) : undefined;
719
- const signaturePayload = {
720
- recordId,
721
- descriptorCid,
722
- contextId,
723
- attestationCid,
724
- encryptionCid,
725
- delegatedGrantId,
726
- permissionGrantId,
727
- protocolRole
728
- };
729
- removeUndefinedProperties(signaturePayload);
730
- const signaturePayloadBytes = Encoder.objectToBytes(signaturePayload);
731
- const builder = yield GeneralJwsBuilder.create(signaturePayloadBytes, [signer]);
732
- const signature = builder.getJws();
733
- return signature;
734
- });
584
+ /** Delegate to `createSignerSignature` in `records-write-signing.ts`. */
585
+ static async createSignerSignature(input) {
586
+ return createSignerSignature(input);
735
587
  }
736
- /**
737
- * Gets the initial write from the given list of `RecordsWrite`.
738
- */
739
- static getInitialWrite(messages) {
740
- return __awaiter(this, void 0, void 0, function* () {
741
- for (const message of messages) {
742
- if (yield RecordsWrite.isInitialWrite(message)) {
743
- return message;
744
- }
745
- }
746
- throw new DwnError(DwnErrorCode.RecordsWriteGetInitialWriteNotFound, `Initial write is not found.`);
747
- });
588
+ /** Delegate to `getInitialWrite` in `records-write-query.ts`. */
589
+ static async getInitialWrite(messages) {
590
+ return getInitialWrite(messages);
748
591
  }
749
- /**
750
- * Verifies that immutable properties of the two given messages are identical.
751
- * @throws {Error} if immutable properties between two RecordsWrite message
752
- */
592
+ /** Delegate to `verifyEqualityOfImmutableProperties` in `records-write-query.ts`. */
753
593
  static verifyEqualityOfImmutableProperties(existingWriteMessage, newMessage) {
754
- const mutableDescriptorProperties = ['dataCid', 'dataSize', 'dataFormat', 'datePublished', 'published', 'messageTimestamp', 'tags'];
755
- // get distinct property names that exist in either the existing message given or new message
756
- let descriptorPropertyNames = [];
757
- descriptorPropertyNames.push(...Object.keys(existingWriteMessage.descriptor));
758
- descriptorPropertyNames.push(...Object.keys(newMessage.descriptor));
759
- descriptorPropertyNames = [...new Set(descriptorPropertyNames)]; // step to remove duplicates
760
- // ensure all immutable properties are not modified
761
- for (const descriptorPropertyName of descriptorPropertyNames) {
762
- // if property is supposed to be immutable
763
- if (mutableDescriptorProperties.indexOf(descriptorPropertyName) === -1) {
764
- const valueInExistingWrite = existingWriteMessage.descriptor[descriptorPropertyName];
765
- const valueInNewMessage = newMessage.descriptor[descriptorPropertyName];
766
- if (valueInNewMessage !== valueInExistingWrite) {
767
- throw new DwnError(DwnErrorCode.RecordsWriteImmutablePropertyChanged, `${descriptorPropertyName} is an immutable property: cannot change '${valueInExistingWrite}' to '${valueInNewMessage}'`);
768
- }
769
- }
770
- }
771
- return true;
594
+ return verifyEqualityOfImmutableProperties(existingWriteMessage, newMessage);
772
595
  }
773
- /**
774
- * Gets the DID of the attesters of the given message.
775
- */
596
+ /** Delegate to `getAttesters` in `records-write-query.ts`. */
776
597
  static getAttesters(message) {
777
- var _a, _b;
778
- const attestationSignatures = (_b = (_a = message.attestation) === null || _a === void 0 ? void 0 : _a.signatures) !== null && _b !== void 0 ? _b : [];
779
- const attesters = attestationSignatures.map((signature) => Jws.getSignerDid(signature));
780
- return attesters;
781
- }
782
- static fetchNewestRecordsWrite(messageStore, tenant, recordId) {
783
- return __awaiter(this, void 0, void 0, function* () {
784
- // get existing RecordsWrite messages matching the `recordId`
785
- const query = {
786
- interface: DwnInterfaceName.Records,
787
- method: DwnMethodName.Write,
788
- recordId: recordId
789
- };
790
- const { messages: existingMessages } = yield messageStore.query(tenant, [query]);
791
- const newestWrite = yield Message.getNewestMessage(existingMessages);
792
- if (newestWrite !== undefined) {
793
- return newestWrite;
794
- }
795
- throw new DwnError(DwnErrorCode.RecordsWriteGetNewestWriteRecordNotFound, 'record not found');
796
- });
598
+ return getAttesters(message);
797
599
  }
798
- /**
799
- * Fetches the initial RecordsWrite of a record.
800
- * @returns The initial RecordsWrite if found; `undefined` otherwise.
801
- */
802
- static fetchInitialRecordsWrite(messageStore, tenant, recordId) {
803
- return __awaiter(this, void 0, void 0, function* () {
804
- const initialRecordsWriteMessage = yield RecordsWrite.fetchInitialRecordsWriteMessage(messageStore, tenant, recordId);
805
- if (initialRecordsWriteMessage === undefined) {
806
- return undefined;
807
- }
808
- const initialRecordsWrite = yield RecordsWrite.parse(initialRecordsWriteMessage);
809
- return initialRecordsWrite;
810
- });
600
+ /** Delegate to `fetchNewestRecordsWrite` in `records-write-query.ts`. */
601
+ static async fetchNewestRecordsWrite(messageStore, tenant, recordId) {
602
+ return fetchNewestRecordsWrite(messageStore, tenant, recordId);
811
603
  }
812
- /**
813
- * Fetches the initial RecordsWrite message of a record.
814
- * @returns The initial RecordsWriteMessage if found; `undefined` otherwise.
815
- */
816
- static fetchInitialRecordsWriteMessage(messageStore, tenant, recordId) {
817
- return __awaiter(this, void 0, void 0, function* () {
818
- const query = { entryId: recordId };
819
- const { messages } = yield messageStore.query(tenant, [query]);
820
- if (messages.length === 0) {
821
- return undefined;
822
- }
823
- return messages[0];
824
- });
604
+ /** Delegate to `fetchInitialRecordsWrite` in `records-write-query.ts`. */
605
+ static async fetchInitialRecordsWrite(messageStore, tenant, recordId) {
606
+ return fetchInitialRecordsWrite(messageStore, tenant, recordId);
607
+ }
608
+ /** Delegate to `fetchInitialRecordsWriteMessage` in `records-write-query.ts`. */
609
+ static async fetchInitialRecordsWriteMessage(messageStore, tenant, recordId) {
610
+ return fetchInitialRecordsWriteMessage(messageStore, tenant, recordId);
825
611
  }
826
612
  }
827
613
  //# sourceMappingURL=records-write.js.map