@enbox/dwn-sdk-js 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (810) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +352 -0
  3. package/dist/bundles/dwn.js +153 -0
  4. package/dist/cjs/index.js +36749 -0
  5. package/dist/cjs/package.json +1 -0
  6. package/dist/esm/generated/precompiled-validators.js +11973 -0
  7. package/dist/esm/generated/precompiled-validators.js.map +1 -0
  8. package/dist/esm/json-schemas/definitions.json +23 -0
  9. package/dist/esm/src/core/abstract-message.js +47 -0
  10. package/dist/esm/src/core/abstract-message.js.map +1 -0
  11. package/dist/esm/src/core/auth.js +39 -0
  12. package/dist/esm/src/core/auth.js.map +1 -0
  13. package/dist/esm/src/core/dwn-constant.js +10 -0
  14. package/dist/esm/src/core/dwn-constant.js.map +1 -0
  15. package/dist/esm/src/core/dwn-error.js +169 -0
  16. package/dist/esm/src/core/dwn-error.js.map +1 -0
  17. package/dist/esm/src/core/grant-authorization.js +97 -0
  18. package/dist/esm/src/core/grant-authorization.js.map +1 -0
  19. package/dist/esm/src/core/message-reply.js +5 -0
  20. package/dist/esm/src/core/message-reply.js.map +1 -0
  21. package/dist/esm/src/core/message.js +233 -0
  22. package/dist/esm/src/core/message.js.map +1 -0
  23. package/dist/esm/src/core/messages-grant-authorization.js +101 -0
  24. package/dist/esm/src/core/messages-grant-authorization.js.map +1 -0
  25. package/dist/esm/src/core/protocol-authorization.js +608 -0
  26. package/dist/esm/src/core/protocol-authorization.js.map +1 -0
  27. package/dist/esm/src/core/protocols-grant-authorization.js +66 -0
  28. package/dist/esm/src/core/protocols-grant-authorization.js.map +1 -0
  29. package/dist/esm/src/core/records-grant-authorization.js +132 -0
  30. package/dist/esm/src/core/records-grant-authorization.js.map +1 -0
  31. package/dist/esm/src/core/resumable-task-manager.js +109 -0
  32. package/dist/esm/src/core/resumable-task-manager.js.map +1 -0
  33. package/dist/esm/src/core/tenant-gate.js +20 -0
  34. package/dist/esm/src/core/tenant-gate.js.map +1 -0
  35. package/dist/esm/src/dwn.js +164 -0
  36. package/dist/esm/src/dwn.js.map +1 -0
  37. package/dist/esm/src/enums/dwn-interface-method.js +16 -0
  38. package/dist/esm/src/enums/dwn-interface-method.js.map +1 -0
  39. package/dist/esm/src/event-log/event-emitter-stream.js +60 -0
  40. package/dist/esm/src/event-log/event-emitter-stream.js.map +1 -0
  41. package/dist/esm/src/event-log/event-log-level.js +63 -0
  42. package/dist/esm/src/event-log/event-log-level.js.map +1 -0
  43. package/dist/esm/src/handlers/messages-query.js +71 -0
  44. package/dist/esm/src/handlers/messages-query.js.map +1 -0
  45. package/dist/esm/src/handlers/messages-read.js +102 -0
  46. package/dist/esm/src/handlers/messages-read.js.map +1 -0
  47. package/dist/esm/src/handlers/messages-subscribe.js +81 -0
  48. package/dist/esm/src/handlers/messages-subscribe.js.map +1 -0
  49. package/dist/esm/src/handlers/protocols-configure.js +133 -0
  50. package/dist/esm/src/handlers/protocols-configure.js.map +1 -0
  51. package/dist/esm/src/handlers/protocols-query.js +73 -0
  52. package/dist/esm/src/handlers/protocols-query.js.map +1 -0
  53. package/dist/esm/src/handlers/records-delete.js +107 -0
  54. package/dist/esm/src/handlers/records-delete.js.map +1 -0
  55. package/dist/esm/src/handlers/records-query.js +210 -0
  56. package/dist/esm/src/handlers/records-query.js.map +1 -0
  57. package/dist/esm/src/handlers/records-read.js +169 -0
  58. package/dist/esm/src/handlers/records-read.js.map +1 -0
  59. package/dist/esm/src/handlers/records-subscribe.js +172 -0
  60. package/dist/esm/src/handlers/records-subscribe.js.map +1 -0
  61. package/dist/esm/src/handlers/records-write.js +344 -0
  62. package/dist/esm/src/handlers/records-write.js.map +1 -0
  63. package/dist/esm/src/index.js +49 -0
  64. package/dist/esm/src/index.js.map +1 -0
  65. package/dist/esm/src/interfaces/messages-query.js +53 -0
  66. package/dist/esm/src/interfaces/messages-query.js.map +1 -0
  67. package/dist/esm/src/interfaces/messages-read.js +61 -0
  68. package/dist/esm/src/interfaces/messages-read.js.map +1 -0
  69. package/dist/esm/src/interfaces/messages-subscribe.js +58 -0
  70. package/dist/esm/src/interfaces/messages-subscribe.js.map +1 -0
  71. package/dist/esm/src/interfaces/protocols-configure.js +271 -0
  72. package/dist/esm/src/interfaces/protocols-configure.js.map +1 -0
  73. package/dist/esm/src/interfaces/protocols-query.js +84 -0
  74. package/dist/esm/src/interfaces/protocols-query.js.map +1 -0
  75. package/dist/esm/src/interfaces/records-delete.js +89 -0
  76. package/dist/esm/src/interfaces/records-delete.js.map +1 -0
  77. package/dist/esm/src/interfaces/records-query.js +104 -0
  78. package/dist/esm/src/interfaces/records-query.js.map +1 -0
  79. package/dist/esm/src/interfaces/records-read.js +84 -0
  80. package/dist/esm/src/interfaces/records-read.js.map +1 -0
  81. package/dist/esm/src/interfaces/records-subscribe.js +91 -0
  82. package/dist/esm/src/interfaces/records-subscribe.js.map +1 -0
  83. package/dist/esm/src/interfaces/records-write.js +795 -0
  84. package/dist/esm/src/interfaces/records-write.js.map +1 -0
  85. package/dist/esm/src/jose/algorithms/signing/ed25519.js +51 -0
  86. package/dist/esm/src/jose/algorithms/signing/ed25519.js.map +1 -0
  87. package/dist/esm/src/jose/algorithms/signing/signature-algorithms.js +20 -0
  88. package/dist/esm/src/jose/algorithms/signing/signature-algorithms.js.map +1 -0
  89. package/dist/esm/src/jose/jws/general/builder.js +47 -0
  90. package/dist/esm/src/jose/jws/general/builder.js.map +1 -0
  91. package/dist/esm/src/jose/jws/general/verifier.js +97 -0
  92. package/dist/esm/src/jose/jws/general/verifier.js.map +1 -0
  93. package/dist/esm/src/protocols/permission-grant.js +39 -0
  94. package/dist/esm/src/protocols/permission-grant.js.map +1 -0
  95. package/dist/esm/src/protocols/permission-request.js +35 -0
  96. package/dist/esm/src/protocols/permission-request.js.map +1 -0
  97. package/dist/esm/src/protocols/permissions.js +357 -0
  98. package/dist/esm/src/protocols/permissions.js.map +1 -0
  99. package/dist/esm/src/schema-validator.js +37 -0
  100. package/dist/esm/src/schema-validator.js.map +1 -0
  101. package/dist/esm/src/store/blockstore-level.js +187 -0
  102. package/dist/esm/src/store/blockstore-level.js.map +1 -0
  103. package/dist/esm/src/store/blockstore-mock.js +168 -0
  104. package/dist/esm/src/store/blockstore-mock.js.map +1 -0
  105. package/dist/esm/src/store/data-store-level.js +136 -0
  106. package/dist/esm/src/store/data-store-level.js.map +1 -0
  107. package/dist/esm/src/store/index-level.js +660 -0
  108. package/dist/esm/src/store/index-level.js.map +1 -0
  109. package/dist/esm/src/store/level-wrapper.js +305 -0
  110. package/dist/esm/src/store/level-wrapper.js.map +1 -0
  111. package/dist/esm/src/store/message-store-level.js +159 -0
  112. package/dist/esm/src/store/message-store-level.js.map +1 -0
  113. package/dist/esm/src/store/resumable-task-store-level.js +131 -0
  114. package/dist/esm/src/store/resumable-task-store-level.js.map +1 -0
  115. package/dist/esm/src/store/storage-controller.js +184 -0
  116. package/dist/esm/src/store/storage-controller.js.map +1 -0
  117. package/dist/esm/src/types/cache.js +2 -0
  118. package/dist/esm/src/types/cache.js.map +1 -0
  119. package/dist/esm/src/types/data-store.js +2 -0
  120. package/dist/esm/src/types/data-store.js.map +1 -0
  121. package/dist/esm/src/types/event-log.js +2 -0
  122. package/dist/esm/src/types/event-log.js.map +1 -0
  123. package/dist/esm/src/types/jose-types.js +2 -0
  124. package/dist/esm/src/types/jose-types.js.map +1 -0
  125. package/dist/esm/src/types/jws-types.js +2 -0
  126. package/dist/esm/src/types/jws-types.js.map +1 -0
  127. package/dist/esm/src/types/message-interface.js +2 -0
  128. package/dist/esm/src/types/message-interface.js.map +1 -0
  129. package/dist/esm/src/types/message-store.js +2 -0
  130. package/dist/esm/src/types/message-store.js.map +1 -0
  131. package/dist/esm/src/types/message-types.js +3 -0
  132. package/dist/esm/src/types/message-types.js.map +1 -0
  133. package/dist/esm/src/types/messages-types.js +2 -0
  134. package/dist/esm/src/types/messages-types.js.map +1 -0
  135. package/dist/esm/src/types/method-handler.js +2 -0
  136. package/dist/esm/src/types/method-handler.js.map +1 -0
  137. package/dist/esm/src/types/permission-types.js +6 -0
  138. package/dist/esm/src/types/permission-types.js.map +1 -0
  139. package/dist/esm/src/types/protocols-types.js +20 -0
  140. package/dist/esm/src/types/protocols-types.js.map +1 -0
  141. package/dist/esm/src/types/query-types.js +6 -0
  142. package/dist/esm/src/types/query-types.js.map +1 -0
  143. package/dist/esm/src/types/records-types.js +8 -0
  144. package/dist/esm/src/types/records-types.js.map +1 -0
  145. package/dist/esm/src/types/resumable-task-store.js +2 -0
  146. package/dist/esm/src/types/resumable-task-store.js.map +1 -0
  147. package/dist/esm/src/types/signer.js +2 -0
  148. package/dist/esm/src/types/signer.js.map +1 -0
  149. package/dist/esm/src/types/subscriptions.js +2 -0
  150. package/dist/esm/src/types/subscriptions.js.map +1 -0
  151. package/dist/esm/src/utils/abort.js +40 -0
  152. package/dist/esm/src/utils/abort.js.map +1 -0
  153. package/dist/esm/src/utils/array.js +72 -0
  154. package/dist/esm/src/utils/array.js.map +1 -0
  155. package/dist/esm/src/utils/cid.js +130 -0
  156. package/dist/esm/src/utils/cid.js.map +1 -0
  157. package/dist/esm/src/utils/data-stream.js +100 -0
  158. package/dist/esm/src/utils/data-stream.js.map +1 -0
  159. package/dist/esm/src/utils/encoder.js +45 -0
  160. package/dist/esm/src/utils/encoder.js.map +1 -0
  161. package/dist/esm/src/utils/encryption.js +128 -0
  162. package/dist/esm/src/utils/encryption.js.map +1 -0
  163. package/dist/esm/src/utils/filter.js +229 -0
  164. package/dist/esm/src/utils/filter.js.map +1 -0
  165. package/dist/esm/src/utils/hd-key.js +114 -0
  166. package/dist/esm/src/utils/hd-key.js.map +1 -0
  167. package/dist/esm/src/utils/jws.js +89 -0
  168. package/dist/esm/src/utils/jws.js.map +1 -0
  169. package/dist/esm/src/utils/memory-cache.js +41 -0
  170. package/dist/esm/src/utils/memory-cache.js.map +1 -0
  171. package/dist/esm/src/utils/messages.js +84 -0
  172. package/dist/esm/src/utils/messages.js.map +1 -0
  173. package/dist/esm/src/utils/object.js +40 -0
  174. package/dist/esm/src/utils/object.js.map +1 -0
  175. package/dist/esm/src/utils/private-key-signer.js +43 -0
  176. package/dist/esm/src/utils/private-key-signer.js.map +1 -0
  177. package/dist/esm/src/utils/protocols.js +51 -0
  178. package/dist/esm/src/utils/protocols.js.map +1 -0
  179. package/dist/esm/src/utils/records.js +454 -0
  180. package/dist/esm/src/utils/records.js.map +1 -0
  181. package/dist/esm/src/utils/secp256k1.js +166 -0
  182. package/dist/esm/src/utils/secp256k1.js.map +1 -0
  183. package/dist/esm/src/utils/secp256r1.js +120 -0
  184. package/dist/esm/src/utils/secp256r1.js.map +1 -0
  185. package/dist/esm/src/utils/string.js +16 -0
  186. package/dist/esm/src/utils/string.js.map +1 -0
  187. package/dist/esm/src/utils/time.js +85 -0
  188. package/dist/esm/src/utils/time.js.map +1 -0
  189. package/dist/esm/src/utils/url.js +62 -0
  190. package/dist/esm/src/utils/url.js.map +1 -0
  191. package/dist/esm/tests/core/auth.spec.js +22 -0
  192. package/dist/esm/tests/core/auth.spec.js.map +1 -0
  193. package/dist/esm/tests/core/message-reply.spec.js +16 -0
  194. package/dist/esm/tests/core/message-reply.spec.js.map +1 -0
  195. package/dist/esm/tests/core/message.spec.js +146 -0
  196. package/dist/esm/tests/core/message.spec.js.map +1 -0
  197. package/dist/esm/tests/core/protocol-authorization.spec.js +48 -0
  198. package/dist/esm/tests/core/protocol-authorization.spec.js.map +1 -0
  199. package/dist/esm/tests/dwn.spec.js +166 -0
  200. package/dist/esm/tests/dwn.spec.js.map +1 -0
  201. package/dist/esm/tests/event-log/event-emitter-stream.spec.js +78 -0
  202. package/dist/esm/tests/event-log/event-emitter-stream.spec.js.map +1 -0
  203. package/dist/esm/tests/event-log/event-log-level.spec.js +44 -0
  204. package/dist/esm/tests/event-log/event-log-level.spec.js.map +1 -0
  205. package/dist/esm/tests/event-log/event-log.spec.js +236 -0
  206. package/dist/esm/tests/event-log/event-log.spec.js.map +1 -0
  207. package/dist/esm/tests/event-log/event-stream.spec.js +125 -0
  208. package/dist/esm/tests/event-log/event-stream.spec.js.map +1 -0
  209. package/dist/esm/tests/features/author-delegated-grant.spec.js +1273 -0
  210. package/dist/esm/tests/features/author-delegated-grant.spec.js.map +1 -0
  211. package/dist/esm/tests/features/owner-delegated-grant.spec.js +584 -0
  212. package/dist/esm/tests/features/owner-delegated-grant.spec.js.map +1 -0
  213. package/dist/esm/tests/features/owner-signature.spec.js +192 -0
  214. package/dist/esm/tests/features/owner-signature.spec.js.map +1 -0
  215. package/dist/esm/tests/features/permissions.spec.js +815 -0
  216. package/dist/esm/tests/features/permissions.spec.js.map +1 -0
  217. package/dist/esm/tests/features/protocol-create-action.spec.js +248 -0
  218. package/dist/esm/tests/features/protocol-create-action.spec.js.map +1 -0
  219. package/dist/esm/tests/features/protocol-delete-action.spec.js +492 -0
  220. package/dist/esm/tests/features/protocol-delete-action.spec.js.map +1 -0
  221. package/dist/esm/tests/features/protocol-update-action.spec.js +572 -0
  222. package/dist/esm/tests/features/protocol-update-action.spec.js.map +1 -0
  223. package/dist/esm/tests/features/records-prune.spec.js +812 -0
  224. package/dist/esm/tests/features/records-prune.spec.js.map +1 -0
  225. package/dist/esm/tests/features/records-tags.spec.js +2516 -0
  226. package/dist/esm/tests/features/records-tags.spec.js.map +1 -0
  227. package/dist/esm/tests/features/resumable-tasks.spec.js +349 -0
  228. package/dist/esm/tests/features/resumable-tasks.spec.js.map +1 -0
  229. package/dist/esm/tests/handlers/messages-query.spec.js +349 -0
  230. package/dist/esm/tests/handlers/messages-query.spec.js.map +1 -0
  231. package/dist/esm/tests/handlers/messages-read.spec.js +647 -0
  232. package/dist/esm/tests/handlers/messages-read.spec.js.map +1 -0
  233. package/dist/esm/tests/handlers/messages-subscribe.spec.js +432 -0
  234. package/dist/esm/tests/handlers/messages-subscribe.spec.js.map +1 -0
  235. package/dist/esm/tests/handlers/protocols-configure.spec.js +608 -0
  236. package/dist/esm/tests/handlers/protocols-configure.spec.js.map +1 -0
  237. package/dist/esm/tests/handlers/protocols-query.spec.js +454 -0
  238. package/dist/esm/tests/handlers/protocols-query.spec.js.map +1 -0
  239. package/dist/esm/tests/handlers/records-delete.spec.js +662 -0
  240. package/dist/esm/tests/handlers/records-delete.spec.js.map +1 -0
  241. package/dist/esm/tests/handlers/records-query.spec.js +2695 -0
  242. package/dist/esm/tests/handlers/records-query.spec.js.map +1 -0
  243. package/dist/esm/tests/handlers/records-read.spec.js +1724 -0
  244. package/dist/esm/tests/handlers/records-read.spec.js.map +1 -0
  245. package/dist/esm/tests/handlers/records-subscribe.spec.js +684 -0
  246. package/dist/esm/tests/handlers/records-subscribe.spec.js.map +1 -0
  247. package/dist/esm/tests/handlers/records-write.spec.js +3637 -0
  248. package/dist/esm/tests/handlers/records-write.spec.js.map +1 -0
  249. package/dist/esm/tests/interfaces/messages-get.spec.js +78 -0
  250. package/dist/esm/tests/interfaces/messages-get.spec.js.map +1 -0
  251. package/dist/esm/tests/interfaces/messages-subscribe.spec.js +30 -0
  252. package/dist/esm/tests/interfaces/messages-subscribe.spec.js.map +1 -0
  253. package/dist/esm/tests/interfaces/messagess-query.spec.js +127 -0
  254. package/dist/esm/tests/interfaces/messagess-query.spec.js.map +1 -0
  255. package/dist/esm/tests/interfaces/protocols-configure.spec.js +489 -0
  256. package/dist/esm/tests/interfaces/protocols-configure.spec.js.map +1 -0
  257. package/dist/esm/tests/interfaces/protocols-query.spec.js +46 -0
  258. package/dist/esm/tests/interfaces/protocols-query.spec.js.map +1 -0
  259. package/dist/esm/tests/interfaces/records-delete.spec.js +39 -0
  260. package/dist/esm/tests/interfaces/records-delete.spec.js.map +1 -0
  261. package/dist/esm/tests/interfaces/records-query.spec.js +85 -0
  262. package/dist/esm/tests/interfaces/records-query.spec.js.map +1 -0
  263. package/dist/esm/tests/interfaces/records-read.spec.js +62 -0
  264. package/dist/esm/tests/interfaces/records-read.spec.js.map +1 -0
  265. package/dist/esm/tests/interfaces/records-subscribe.spec.js +72 -0
  266. package/dist/esm/tests/interfaces/records-subscribe.spec.js.map +1 -0
  267. package/dist/esm/tests/interfaces/records-write.spec.js +423 -0
  268. package/dist/esm/tests/interfaces/records-write.spec.js.map +1 -0
  269. package/dist/esm/tests/jose/jws/general.spec.js +243 -0
  270. package/dist/esm/tests/jose/jws/general.spec.js.map +1 -0
  271. package/dist/esm/tests/protocols/permission-request.spec.js +40 -0
  272. package/dist/esm/tests/protocols/permission-request.spec.js.map +1 -0
  273. package/dist/esm/tests/protocols/permissions.spec.js +123 -0
  274. package/dist/esm/tests/protocols/permissions.spec.js.map +1 -0
  275. package/dist/esm/tests/scenarios/aggregator.spec.js +670 -0
  276. package/dist/esm/tests/scenarios/aggregator.spec.js.map +1 -0
  277. package/dist/esm/tests/scenarios/deleted-record.spec.js +102 -0
  278. package/dist/esm/tests/scenarios/deleted-record.spec.js.map +1 -0
  279. package/dist/esm/tests/scenarios/end-to-end-tests.spec.js +220 -0
  280. package/dist/esm/tests/scenarios/end-to-end-tests.spec.js.map +1 -0
  281. package/dist/esm/tests/scenarios/messages-query.spec.js +395 -0
  282. package/dist/esm/tests/scenarios/messages-query.spec.js.map +1 -0
  283. package/dist/esm/tests/scenarios/nested-roles.spec.js +300 -0
  284. package/dist/esm/tests/scenarios/nested-roles.spec.js.map +1 -0
  285. package/dist/esm/tests/scenarios/subscriptions.spec.js +886 -0
  286. package/dist/esm/tests/scenarios/subscriptions.spec.js.map +1 -0
  287. package/dist/esm/tests/store/blockstore-mock.spec.js +192 -0
  288. package/dist/esm/tests/store/blockstore-mock.spec.js.map +1 -0
  289. package/dist/esm/tests/store/data-store-level.spec.js +146 -0
  290. package/dist/esm/tests/store/data-store-level.spec.js.map +1 -0
  291. package/dist/esm/tests/store/index-level.spec.js +1208 -0
  292. package/dist/esm/tests/store/index-level.spec.js.map +1 -0
  293. package/dist/esm/tests/store/message-store-level.spec.js +48 -0
  294. package/dist/esm/tests/store/message-store-level.spec.js.map +1 -0
  295. package/dist/esm/tests/store/message-store.spec.js +404 -0
  296. package/dist/esm/tests/store/message-store.spec.js.map +1 -0
  297. package/dist/esm/tests/store-dependent-tests.spec.js +5 -0
  298. package/dist/esm/tests/store-dependent-tests.spec.js.map +1 -0
  299. package/dist/esm/tests/test-event-stream.js +25 -0
  300. package/dist/esm/tests/test-event-stream.js.map +1 -0
  301. package/dist/esm/tests/test-stores.js +45 -0
  302. package/dist/esm/tests/test-stores.js.map +1 -0
  303. package/dist/esm/tests/test-suite.js +88 -0
  304. package/dist/esm/tests/test-suite.js.map +1 -0
  305. package/dist/esm/tests/utils/cid.spec.js +80 -0
  306. package/dist/esm/tests/utils/cid.spec.js.map +1 -0
  307. package/dist/esm/tests/utils/data-stream.spec.js +27 -0
  308. package/dist/esm/tests/utils/data-stream.spec.js.map +1 -0
  309. package/dist/esm/tests/utils/encryption.spec.js +148 -0
  310. package/dist/esm/tests/utils/encryption.spec.js.map +1 -0
  311. package/dist/esm/tests/utils/filters.spec.js +295 -0
  312. package/dist/esm/tests/utils/filters.spec.js.map +1 -0
  313. package/dist/esm/tests/utils/hd-key.spec.js +35 -0
  314. package/dist/esm/tests/utils/hd-key.spec.js.map +1 -0
  315. package/dist/esm/tests/utils/jws.spec.js +8 -0
  316. package/dist/esm/tests/utils/jws.spec.js.map +1 -0
  317. package/dist/esm/tests/utils/memory-cache.spec.js +35 -0
  318. package/dist/esm/tests/utils/memory-cache.spec.js.map +1 -0
  319. package/dist/esm/tests/utils/messages.spec.js +101 -0
  320. package/dist/esm/tests/utils/messages.spec.js.map +1 -0
  321. package/dist/esm/tests/utils/object.spec.js +36 -0
  322. package/dist/esm/tests/utils/object.spec.js.map +1 -0
  323. package/dist/esm/tests/utils/poller.js +49 -0
  324. package/dist/esm/tests/utils/poller.js.map +1 -0
  325. package/dist/esm/tests/utils/private-key-signer.spec.js +44 -0
  326. package/dist/esm/tests/utils/private-key-signer.spec.js.map +1 -0
  327. package/dist/esm/tests/utils/records.spec.js +53 -0
  328. package/dist/esm/tests/utils/records.spec.js.map +1 -0
  329. package/dist/esm/tests/utils/secp256k1.spec.js +50 -0
  330. package/dist/esm/tests/utils/secp256k1.spec.js.map +1 -0
  331. package/dist/esm/tests/utils/secp256r1.spec.js +56 -0
  332. package/dist/esm/tests/utils/secp256r1.spec.js.map +1 -0
  333. package/dist/esm/tests/utils/test-data-generator.js +643 -0
  334. package/dist/esm/tests/utils/test-data-generator.js.map +1 -0
  335. package/dist/esm/tests/utils/test-stub-generator.js +39 -0
  336. package/dist/esm/tests/utils/test-stub-generator.js.map +1 -0
  337. package/dist/esm/tests/utils/time.spec.js +64 -0
  338. package/dist/esm/tests/utils/time.spec.js.map +1 -0
  339. package/dist/esm/tests/utils/url.spec.js +43 -0
  340. package/dist/esm/tests/utils/url.spec.js.map +1 -0
  341. package/dist/esm/tests/validation/json-schemas/definitions.spec.js +33 -0
  342. package/dist/esm/tests/validation/json-schemas/definitions.spec.js.map +1 -0
  343. package/dist/esm/tests/validation/json-schemas/jwk/general-jwk.spec.js +50 -0
  344. package/dist/esm/tests/validation/json-schemas/jwk/general-jwk.spec.js.map +1 -0
  345. package/dist/esm/tests/validation/json-schemas/jwk/public-jwk.spec.js +36 -0
  346. package/dist/esm/tests/validation/json-schemas/jwk/public-jwk.spec.js.map +1 -0
  347. package/dist/esm/tests/validation/json-schemas/jwk-verification-method.spec.js +82 -0
  348. package/dist/esm/tests/validation/json-schemas/jwk-verification-method.spec.js.map +1 -0
  349. package/dist/esm/tests/validation/json-schemas/protocols/protocols-configure.spec.js +75 -0
  350. package/dist/esm/tests/validation/json-schemas/protocols/protocols-configure.spec.js.map +1 -0
  351. package/dist/esm/tests/validation/json-schemas/records/records-query.spec.js +148 -0
  352. package/dist/esm/tests/validation/json-schemas/records/records-query.spec.js.map +1 -0
  353. package/dist/esm/tests/validation/json-schemas/records/records-write.spec.js +421 -0
  354. package/dist/esm/tests/validation/json-schemas/records/records-write.spec.js.map +1 -0
  355. package/dist/esm/tests/vectors/protocol-definitions/anyone-collaborate.json +21 -0
  356. package/dist/esm/tests/vectors/protocol-definitions/author-can.json +33 -0
  357. package/dist/esm/tests/vectors/protocol-definitions/chat.json +70 -0
  358. package/dist/esm/tests/vectors/protocol-definitions/credential-issuance.json +41 -0
  359. package/dist/esm/tests/vectors/protocol-definitions/dex.json +58 -0
  360. package/dist/esm/tests/vectors/protocol-definitions/email.json +62 -0
  361. package/dist/esm/tests/vectors/protocol-definitions/free-for-all.json +45 -0
  362. package/dist/esm/tests/vectors/protocol-definitions/friend-role.json +48 -0
  363. package/dist/esm/tests/vectors/protocol-definitions/message.json +37 -0
  364. package/dist/esm/tests/vectors/protocol-definitions/minimal.json +10 -0
  365. package/dist/esm/tests/vectors/protocol-definitions/nested.json +31 -0
  366. package/dist/esm/tests/vectors/protocol-definitions/private-protocol.json +15 -0
  367. package/dist/esm/tests/vectors/protocol-definitions/recipient-can.json +33 -0
  368. package/dist/esm/tests/vectors/protocol-definitions/slack.json +242 -0
  369. package/dist/esm/tests/vectors/protocol-definitions/social-media.json +106 -0
  370. package/dist/esm/tests/vectors/protocol-definitions/thread-role.json +70 -0
  371. package/dist/types/generated/precompiled-validators.d.ts +597 -0
  372. package/dist/types/generated/precompiled-validators.d.ts.map +1 -0
  373. package/dist/types/src/core/abstract-message.d.ts +25 -0
  374. package/dist/types/src/core/abstract-message.d.ts.map +1 -0
  375. package/dist/types/src/core/auth.d.ts +9 -0
  376. package/dist/types/src/core/auth.d.ts.map +1 -0
  377. package/dist/types/src/core/dwn-constant.d.ts +10 -0
  378. package/dist/types/src/core/dwn-constant.d.ts.map +1 -0
  379. package/dist/types/src/core/dwn-error.d.ts +164 -0
  380. package/dist/types/src/core/dwn-error.d.ts.map +1 -0
  381. package/dist/types/src/core/grant-authorization.d.ts +43 -0
  382. package/dist/types/src/core/grant-authorization.d.ts.map +1 -0
  383. package/dist/types/src/core/message-reply.d.ts +32 -0
  384. package/dist/types/src/core/message-reply.d.ts.map +1 -0
  385. package/dist/types/src/core/message.d.ts +94 -0
  386. package/dist/types/src/core/message.d.ts.map +1 -0
  387. package/dist/types/src/core/messages-grant-authorization.d.ts +34 -0
  388. package/dist/types/src/core/messages-grant-authorization.d.ts.map +1 -0
  389. package/dist/types/src/core/protocol-authorization.d.ts +103 -0
  390. package/dist/types/src/core/protocol-authorization.d.ts.map +1 -0
  391. package/dist/types/src/core/protocols-grant-authorization.d.ts +31 -0
  392. package/dist/types/src/core/protocols-grant-authorization.d.ts.map +1 -0
  393. package/dist/types/src/core/records-grant-authorization.d.ts +60 -0
  394. package/dist/types/src/core/records-grant-authorization.d.ts.map +1 -0
  395. package/dist/types/src/core/resumable-task-manager.d.ts +43 -0
  396. package/dist/types/src/core/resumable-task-manager.d.ts.map +1 -0
  397. package/dist/types/src/core/tenant-gate.d.ts +29 -0
  398. package/dist/types/src/core/tenant-gate.d.ts.map +1 -0
  399. package/dist/types/src/dwn.d.ts +85 -0
  400. package/dist/types/src/dwn.d.ts.map +1 -0
  401. package/dist/types/src/enums/dwn-interface-method.d.ts +14 -0
  402. package/dist/types/src/enums/dwn-interface-method.d.ts.map +1 -0
  403. package/dist/types/src/event-log/event-emitter-stream.d.ts +23 -0
  404. package/dist/types/src/event-log/event-emitter-stream.d.ts.map +1 -0
  405. package/dist/types/src/event-log/event-log-level.d.ts +35 -0
  406. package/dist/types/src/event-log/event-log-level.d.ts.map +1 -0
  407. package/dist/types/src/handlers/messages-query.d.ts +17 -0
  408. package/dist/types/src/handlers/messages-query.d.ts.map +1 -0
  409. package/dist/types/src/handlers/messages-read.d.ts +22 -0
  410. package/dist/types/src/handlers/messages-read.d.ts.map +1 -0
  411. package/dist/types/src/handlers/messages-subscribe.d.ts +18 -0
  412. package/dist/types/src/handlers/messages-subscribe.d.ts.map +1 -0
  413. package/dist/types/src/handlers/protocols-configure.d.ts +24 -0
  414. package/dist/types/src/handlers/protocols-configure.d.ts.map +1 -0
  415. package/dist/types/src/handlers/protocols-query.d.ts +20 -0
  416. package/dist/types/src/handlers/protocols-query.d.ts.map +1 -0
  417. package/dist/types/src/handlers/records-delete.d.ts +23 -0
  418. package/dist/types/src/handlers/records-delete.d.ts.map +1 -0
  419. package/dist/types/src/handlers/records-query.d.ts +70 -0
  420. package/dist/types/src/handlers/records-query.d.ts.map +1 -0
  421. package/dist/types/src/handlers/records-read.d.ts +20 -0
  422. package/dist/types/src/handlers/records-read.d.ts.map +1 -0
  423. package/dist/types/src/handlers/records-subscribe.d.ts +62 -0
  424. package/dist/types/src/handlers/records-subscribe.d.ts.map +1 -0
  425. package/dist/types/src/handlers/records-write.d.ts +51 -0
  426. package/dist/types/src/handlers/records-write.d.ts.map +1 -0
  427. package/dist/types/src/index.d.ts +63 -0
  428. package/dist/types/src/index.d.ts.map +1 -0
  429. package/dist/types/src/interfaces/messages-query.d.ts +16 -0
  430. package/dist/types/src/interfaces/messages-query.d.ts.map +1 -0
  431. package/dist/types/src/interfaces/messages-read.d.ts +20 -0
  432. package/dist/types/src/interfaces/messages-read.d.ts.map +1 -0
  433. package/dist/types/src/interfaces/messages-subscribe.d.ts +20 -0
  434. package/dist/types/src/interfaces/messages-subscribe.d.ts.map +1 -0
  435. package/dist/types/src/interfaces/protocols-configure.d.ts +40 -0
  436. package/dist/types/src/interfaces/protocols-configure.d.ts.map +1 -0
  437. package/dist/types/src/interfaces/protocols-query.d.ts +17 -0
  438. package/dist/types/src/interfaces/protocols-query.d.ts.map +1 -0
  439. package/dist/types/src/interfaces/records-delete.d.ts +34 -0
  440. package/dist/types/src/interfaces/records-delete.d.ts.map +1 -0
  441. package/dist/types/src/interfaces/records-query.d.ts +31 -0
  442. package/dist/types/src/interfaces/records-query.d.ts.map +1 -0
  443. package/dist/types/src/interfaces/records-read.d.ts +36 -0
  444. package/dist/types/src/interfaces/records-read.d.ts.map +1 -0
  445. package/dist/types/src/interfaces/records-subscribe.d.ts +27 -0
  446. package/dist/types/src/interfaces/records-subscribe.d.ts.map +1 -0
  447. package/dist/types/src/interfaces/records-write.d.ts +309 -0
  448. package/dist/types/src/interfaces/records-write.d.ts.map +1 -0
  449. package/dist/types/src/jose/algorithms/signing/ed25519.d.ts +3 -0
  450. package/dist/types/src/jose/algorithms/signing/ed25519.d.ts.map +1 -0
  451. package/dist/types/src/jose/algorithms/signing/signature-algorithms.d.ts +3 -0
  452. package/dist/types/src/jose/algorithms/signing/signature-algorithms.d.ts.map +1 -0
  453. package/dist/types/src/jose/jws/general/builder.d.ts +10 -0
  454. package/dist/types/src/jose/jws/general/builder.d.ts.map +1 -0
  455. package/dist/types/src/jose/jws/general/verifier.d.ts +32 -0
  456. package/dist/types/src/jose/jws/general/verifier.d.ts.map +1 -0
  457. package/dist/types/src/protocols/permission-grant.d.ts +50 -0
  458. package/dist/types/src/protocols/permission-grant.d.ts.map +1 -0
  459. package/dist/types/src/protocols/permission-request.d.ts +35 -0
  460. package/dist/types/src/protocols/permission-request.d.ts.map +1 -0
  461. package/dist/types/src/protocols/permissions.d.ts +150 -0
  462. package/dist/types/src/protocols/permissions.d.ts.map +1 -0
  463. package/dist/types/src/schema-validator.d.ts +8 -0
  464. package/dist/types/src/schema-validator.d.ts.map +1 -0
  465. package/dist/types/src/store/blockstore-level.d.ts +34 -0
  466. package/dist/types/src/store/blockstore-level.d.ts.map +1 -0
  467. package/dist/types/src/store/blockstore-mock.d.ts +27 -0
  468. package/dist/types/src/store/blockstore-mock.d.ts.map +1 -0
  469. package/dist/types/src/store/data-store-level.d.ts +34 -0
  470. package/dist/types/src/store/data-store-level.d.ts.map +1 -0
  471. package/dist/types/src/store/index-level.d.ts +171 -0
  472. package/dist/types/src/store/index-level.d.ts.map +1 -0
  473. package/dist/types/src/store/level-wrapper.d.ts +48 -0
  474. package/dist/types/src/store/level-wrapper.d.ts.map +1 -0
  475. package/dist/types/src/store/message-store-level.d.ts +46 -0
  476. package/dist/types/src/store/message-store-level.d.ts.map +1 -0
  477. package/dist/types/src/store/resumable-task-store-level.d.ts +28 -0
  478. package/dist/types/src/store/resumable-task-store-level.d.ts.map +1 -0
  479. package/dist/types/src/store/storage-controller.d.ts +46 -0
  480. package/dist/types/src/store/storage-controller.d.ts.map +1 -0
  481. package/dist/types/src/types/cache.d.ts +16 -0
  482. package/dist/types/src/types/cache.d.ts.map +1 -0
  483. package/dist/types/src/types/data-store.d.ts +57 -0
  484. package/dist/types/src/types/data-store.d.ts.map +1 -0
  485. package/dist/types/src/types/event-log.d.ts +52 -0
  486. package/dist/types/src/types/event-log.d.ts.map +1 -0
  487. package/dist/types/src/types/jose-types.d.ts +75 -0
  488. package/dist/types/src/types/jose-types.d.ts.map +1 -0
  489. package/dist/types/src/types/jws-types.d.ts +27 -0
  490. package/dist/types/src/types/jws-types.d.ts.map +1 -0
  491. package/dist/types/src/types/message-interface.d.ts +27 -0
  492. package/dist/types/src/types/message-interface.d.ts.map +1 -0
  493. package/dist/types/src/types/message-store.d.ts +42 -0
  494. package/dist/types/src/types/message-store.d.ts.map +1 -0
  495. package/dist/types/src/types/message-types.d.ts +116 -0
  496. package/dist/types/src/types/message-types.d.ts.map +1 -0
  497. package/dist/types/src/types/messages-types.d.ts +65 -0
  498. package/dist/types/src/types/messages-types.d.ts.map +1 -0
  499. package/dist/types/src/types/method-handler.d.ts +19 -0
  500. package/dist/types/src/types/method-handler.d.ts.map +1 -0
  501. package/dist/types/src/types/permission-types.d.ts +93 -0
  502. package/dist/types/src/types/permission-types.d.ts.map +1 -0
  503. package/dist/types/src/types/protocols-types.d.ts +154 -0
  504. package/dist/types/src/types/protocols-types.d.ts.map +1 -0
  505. package/dist/types/src/types/query-types.d.ts +66 -0
  506. package/dist/types/src/types/query-types.d.ts.map +1 -0
  507. package/dist/types/src/types/records-types.d.ts +230 -0
  508. package/dist/types/src/types/records-types.d.ts.map +1 -0
  509. package/dist/types/src/types/resumable-task-store.d.ts +89 -0
  510. package/dist/types/src/types/resumable-task-store.d.ts.map +1 -0
  511. package/dist/types/src/types/signer.d.ts +26 -0
  512. package/dist/types/src/types/signer.d.ts.map +1 -0
  513. package/dist/types/src/types/subscriptions.d.ts +30 -0
  514. package/dist/types/src/types/subscriptions.d.ts.map +1 -0
  515. package/dist/types/src/utils/abort.d.ts +5 -0
  516. package/dist/types/src/utils/abort.d.ts.map +1 -0
  517. package/dist/types/src/utils/array.d.ts +18 -0
  518. package/dist/types/src/utils/array.d.ts.map +1 -0
  519. package/dist/types/src/utils/cid.d.ts +30 -0
  520. package/dist/types/src/utils/cid.d.ts.map +1 -0
  521. package/dist/types/src/utils/data-stream.d.ts +31 -0
  522. package/dist/types/src/utils/data-stream.d.ts.map +1 -0
  523. package/dist/types/src/utils/encoder.d.ts +14 -0
  524. package/dist/types/src/utils/encoder.d.ts.map +1 -0
  525. package/dist/types/src/utils/encryption.d.ts +44 -0
  526. package/dist/types/src/utils/encryption.d.ts.map +1 -0
  527. package/dist/types/src/utils/filter.d.ts +60 -0
  528. package/dist/types/src/utils/filter.d.ts.map +1 -0
  529. package/dist/types/src/utils/hd-key.d.ts +54 -0
  530. package/dist/types/src/utils/hd-key.d.ts.map +1 -0
  531. package/dist/types/src/utils/jws.d.ts +39 -0
  532. package/dist/types/src/utils/jws.d.ts.map +1 -0
  533. package/dist/types/src/utils/memory-cache.d.ts +15 -0
  534. package/dist/types/src/utils/memory-cache.d.ts.map +1 -0
  535. package/dist/types/src/utils/messages.d.ts +27 -0
  536. package/dist/types/src/utils/messages.d.ts.map +1 -0
  537. package/dist/types/src/utils/object.d.ts +13 -0
  538. package/dist/types/src/utils/object.d.ts.map +1 -0
  539. package/dist/types/src/utils/private-key-signer.d.ts +34 -0
  540. package/dist/types/src/utils/private-key-signer.d.ts.map +1 -0
  541. package/dist/types/src/utils/protocols.d.ts +14 -0
  542. package/dist/types/src/utils/protocols.d.ts.map +1 -0
  543. package/dist/types/src/utils/records.d.ts +122 -0
  544. package/dist/types/src/utils/records.d.ts.map +1 -0
  545. package/dist/types/src/utils/secp256k1.d.ts +59 -0
  546. package/dist/types/src/utils/secp256k1.d.ts.map +1 -0
  547. package/dist/types/src/utils/secp256r1.d.ts +39 -0
  548. package/dist/types/src/utils/secp256r1.d.ts.map +1 -0
  549. package/dist/types/src/utils/string.d.ts +6 -0
  550. package/dist/types/src/utils/string.d.ts.map +1 -0
  551. package/dist/types/src/utils/time.d.ts +50 -0
  552. package/dist/types/src/utils/time.d.ts.map +1 -0
  553. package/dist/types/src/utils/url.d.ts +5 -0
  554. package/dist/types/src/utils/url.d.ts.map +1 -0
  555. package/dist/types/tests/core/auth.spec.d.ts +2 -0
  556. package/dist/types/tests/core/auth.spec.d.ts.map +1 -0
  557. package/dist/types/tests/core/message-reply.spec.d.ts +2 -0
  558. package/dist/types/tests/core/message-reply.spec.d.ts.map +1 -0
  559. package/dist/types/tests/core/message.spec.d.ts +2 -0
  560. package/dist/types/tests/core/message.spec.d.ts.map +1 -0
  561. package/dist/types/tests/core/protocol-authorization.spec.d.ts +2 -0
  562. package/dist/types/tests/core/protocol-authorization.spec.d.ts.map +1 -0
  563. package/dist/types/tests/dwn.spec.d.ts +2 -0
  564. package/dist/types/tests/dwn.spec.d.ts.map +1 -0
  565. package/dist/types/tests/event-log/event-emitter-stream.spec.d.ts +2 -0
  566. package/dist/types/tests/event-log/event-emitter-stream.spec.d.ts.map +1 -0
  567. package/dist/types/tests/event-log/event-log-level.spec.d.ts +2 -0
  568. package/dist/types/tests/event-log/event-log-level.spec.d.ts.map +1 -0
  569. package/dist/types/tests/event-log/event-log.spec.d.ts +2 -0
  570. package/dist/types/tests/event-log/event-log.spec.d.ts.map +1 -0
  571. package/dist/types/tests/event-log/event-stream.spec.d.ts +2 -0
  572. package/dist/types/tests/event-log/event-stream.spec.d.ts.map +1 -0
  573. package/dist/types/tests/features/author-delegated-grant.spec.d.ts +2 -0
  574. package/dist/types/tests/features/author-delegated-grant.spec.d.ts.map +1 -0
  575. package/dist/types/tests/features/owner-delegated-grant.spec.d.ts +2 -0
  576. package/dist/types/tests/features/owner-delegated-grant.spec.d.ts.map +1 -0
  577. package/dist/types/tests/features/owner-signature.spec.d.ts +2 -0
  578. package/dist/types/tests/features/owner-signature.spec.d.ts.map +1 -0
  579. package/dist/types/tests/features/permissions.spec.d.ts +2 -0
  580. package/dist/types/tests/features/permissions.spec.d.ts.map +1 -0
  581. package/dist/types/tests/features/protocol-create-action.spec.d.ts +2 -0
  582. package/dist/types/tests/features/protocol-create-action.spec.d.ts.map +1 -0
  583. package/dist/types/tests/features/protocol-delete-action.spec.d.ts +2 -0
  584. package/dist/types/tests/features/protocol-delete-action.spec.d.ts.map +1 -0
  585. package/dist/types/tests/features/protocol-update-action.spec.d.ts +2 -0
  586. package/dist/types/tests/features/protocol-update-action.spec.d.ts.map +1 -0
  587. package/dist/types/tests/features/records-prune.spec.d.ts +2 -0
  588. package/dist/types/tests/features/records-prune.spec.d.ts.map +1 -0
  589. package/dist/types/tests/features/records-tags.spec.d.ts +2 -0
  590. package/dist/types/tests/features/records-tags.spec.d.ts.map +1 -0
  591. package/dist/types/tests/features/resumable-tasks.spec.d.ts +2 -0
  592. package/dist/types/tests/features/resumable-tasks.spec.d.ts.map +1 -0
  593. package/dist/types/tests/handlers/messages-query.spec.d.ts +2 -0
  594. package/dist/types/tests/handlers/messages-query.spec.d.ts.map +1 -0
  595. package/dist/types/tests/handlers/messages-read.spec.d.ts +2 -0
  596. package/dist/types/tests/handlers/messages-read.spec.d.ts.map +1 -0
  597. package/dist/types/tests/handlers/messages-subscribe.spec.d.ts +2 -0
  598. package/dist/types/tests/handlers/messages-subscribe.spec.d.ts.map +1 -0
  599. package/dist/types/tests/handlers/protocols-configure.spec.d.ts +2 -0
  600. package/dist/types/tests/handlers/protocols-configure.spec.d.ts.map +1 -0
  601. package/dist/types/tests/handlers/protocols-query.spec.d.ts +2 -0
  602. package/dist/types/tests/handlers/protocols-query.spec.d.ts.map +1 -0
  603. package/dist/types/tests/handlers/records-delete.spec.d.ts +2 -0
  604. package/dist/types/tests/handlers/records-delete.spec.d.ts.map +1 -0
  605. package/dist/types/tests/handlers/records-query.spec.d.ts +2 -0
  606. package/dist/types/tests/handlers/records-query.spec.d.ts.map +1 -0
  607. package/dist/types/tests/handlers/records-read.spec.d.ts +2 -0
  608. package/dist/types/tests/handlers/records-read.spec.d.ts.map +1 -0
  609. package/dist/types/tests/handlers/records-subscribe.spec.d.ts +2 -0
  610. package/dist/types/tests/handlers/records-subscribe.spec.d.ts.map +1 -0
  611. package/dist/types/tests/handlers/records-write.spec.d.ts +2 -0
  612. package/dist/types/tests/handlers/records-write.spec.d.ts.map +1 -0
  613. package/dist/types/tests/interfaces/messages-get.spec.d.ts +2 -0
  614. package/dist/types/tests/interfaces/messages-get.spec.d.ts.map +1 -0
  615. package/dist/types/tests/interfaces/messages-subscribe.spec.d.ts +2 -0
  616. package/dist/types/tests/interfaces/messages-subscribe.spec.d.ts.map +1 -0
  617. package/dist/types/tests/interfaces/messagess-query.spec.d.ts +2 -0
  618. package/dist/types/tests/interfaces/messagess-query.spec.d.ts.map +1 -0
  619. package/dist/types/tests/interfaces/protocols-configure.spec.d.ts +2 -0
  620. package/dist/types/tests/interfaces/protocols-configure.spec.d.ts.map +1 -0
  621. package/dist/types/tests/interfaces/protocols-query.spec.d.ts +2 -0
  622. package/dist/types/tests/interfaces/protocols-query.spec.d.ts.map +1 -0
  623. package/dist/types/tests/interfaces/records-delete.spec.d.ts +2 -0
  624. package/dist/types/tests/interfaces/records-delete.spec.d.ts.map +1 -0
  625. package/dist/types/tests/interfaces/records-query.spec.d.ts +2 -0
  626. package/dist/types/tests/interfaces/records-query.spec.d.ts.map +1 -0
  627. package/dist/types/tests/interfaces/records-read.spec.d.ts +2 -0
  628. package/dist/types/tests/interfaces/records-read.spec.d.ts.map +1 -0
  629. package/dist/types/tests/interfaces/records-subscribe.spec.d.ts +2 -0
  630. package/dist/types/tests/interfaces/records-subscribe.spec.d.ts.map +1 -0
  631. package/dist/types/tests/interfaces/records-write.spec.d.ts +2 -0
  632. package/dist/types/tests/interfaces/records-write.spec.d.ts.map +1 -0
  633. package/dist/types/tests/jose/jws/general.spec.d.ts +2 -0
  634. package/dist/types/tests/jose/jws/general.spec.d.ts.map +1 -0
  635. package/dist/types/tests/protocols/permission-request.spec.d.ts +2 -0
  636. package/dist/types/tests/protocols/permission-request.spec.d.ts.map +1 -0
  637. package/dist/types/tests/protocols/permissions.spec.d.ts +2 -0
  638. package/dist/types/tests/protocols/permissions.spec.d.ts.map +1 -0
  639. package/dist/types/tests/scenarios/aggregator.spec.d.ts +2 -0
  640. package/dist/types/tests/scenarios/aggregator.spec.d.ts.map +1 -0
  641. package/dist/types/tests/scenarios/deleted-record.spec.d.ts +2 -0
  642. package/dist/types/tests/scenarios/deleted-record.spec.d.ts.map +1 -0
  643. package/dist/types/tests/scenarios/end-to-end-tests.spec.d.ts +2 -0
  644. package/dist/types/tests/scenarios/end-to-end-tests.spec.d.ts.map +1 -0
  645. package/dist/types/tests/scenarios/messages-query.spec.d.ts +2 -0
  646. package/dist/types/tests/scenarios/messages-query.spec.d.ts.map +1 -0
  647. package/dist/types/tests/scenarios/nested-roles.spec.d.ts +2 -0
  648. package/dist/types/tests/scenarios/nested-roles.spec.d.ts.map +1 -0
  649. package/dist/types/tests/scenarios/subscriptions.spec.d.ts +2 -0
  650. package/dist/types/tests/scenarios/subscriptions.spec.d.ts.map +1 -0
  651. package/dist/types/tests/store/blockstore-mock.spec.d.ts +2 -0
  652. package/dist/types/tests/store/blockstore-mock.spec.d.ts.map +1 -0
  653. package/dist/types/tests/store/data-store-level.spec.d.ts +2 -0
  654. package/dist/types/tests/store/data-store-level.spec.d.ts.map +1 -0
  655. package/dist/types/tests/store/index-level.spec.d.ts +2 -0
  656. package/dist/types/tests/store/index-level.spec.d.ts.map +1 -0
  657. package/dist/types/tests/store/message-store-level.spec.d.ts +2 -0
  658. package/dist/types/tests/store/message-store-level.spec.d.ts.map +1 -0
  659. package/dist/types/tests/store/message-store.spec.d.ts +2 -0
  660. package/dist/types/tests/store/message-store.spec.d.ts.map +1 -0
  661. package/dist/types/tests/store-dependent-tests.spec.d.ts +2 -0
  662. package/dist/types/tests/store-dependent-tests.spec.d.ts.map +1 -0
  663. package/dist/types/tests/test-event-stream.d.ts +22 -0
  664. package/dist/types/tests/test-event-stream.d.ts.map +1 -0
  665. package/dist/types/tests/test-stores.d.ts +33 -0
  666. package/dist/types/tests/test-stores.d.ts.map +1 -0
  667. package/dist/types/tests/test-suite.d.ts +18 -0
  668. package/dist/types/tests/test-suite.d.ts.map +1 -0
  669. package/dist/types/tests/utils/cid.spec.d.ts +2 -0
  670. package/dist/types/tests/utils/cid.spec.d.ts.map +1 -0
  671. package/dist/types/tests/utils/data-stream.spec.d.ts +2 -0
  672. package/dist/types/tests/utils/data-stream.spec.d.ts.map +1 -0
  673. package/dist/types/tests/utils/encryption.spec.d.ts +2 -0
  674. package/dist/types/tests/utils/encryption.spec.d.ts.map +1 -0
  675. package/dist/types/tests/utils/filters.spec.d.ts +2 -0
  676. package/dist/types/tests/utils/filters.spec.d.ts.map +1 -0
  677. package/dist/types/tests/utils/hd-key.spec.d.ts +2 -0
  678. package/dist/types/tests/utils/hd-key.spec.d.ts.map +1 -0
  679. package/dist/types/tests/utils/jws.spec.d.ts +2 -0
  680. package/dist/types/tests/utils/jws.spec.d.ts.map +1 -0
  681. package/dist/types/tests/utils/memory-cache.spec.d.ts +2 -0
  682. package/dist/types/tests/utils/memory-cache.spec.d.ts.map +1 -0
  683. package/dist/types/tests/utils/messages.spec.d.ts +2 -0
  684. package/dist/types/tests/utils/messages.spec.d.ts.map +1 -0
  685. package/dist/types/tests/utils/object.spec.d.ts +2 -0
  686. package/dist/types/tests/utils/object.spec.d.ts.map +1 -0
  687. package/dist/types/tests/utils/poller.d.ts +21 -0
  688. package/dist/types/tests/utils/poller.d.ts.map +1 -0
  689. package/dist/types/tests/utils/private-key-signer.spec.d.ts +2 -0
  690. package/dist/types/tests/utils/private-key-signer.spec.d.ts.map +1 -0
  691. package/dist/types/tests/utils/records.spec.d.ts +2 -0
  692. package/dist/types/tests/utils/records.spec.d.ts.map +1 -0
  693. package/dist/types/tests/utils/secp256k1.spec.d.ts +2 -0
  694. package/dist/types/tests/utils/secp256k1.spec.d.ts.map +1 -0
  695. package/dist/types/tests/utils/secp256r1.spec.d.ts +2 -0
  696. package/dist/types/tests/utils/secp256r1.spec.d.ts.map +1 -0
  697. package/dist/types/tests/utils/test-data-generator.d.ts +342 -0
  698. package/dist/types/tests/utils/test-data-generator.d.ts.map +1 -0
  699. package/dist/types/tests/utils/test-stub-generator.d.ts +16 -0
  700. package/dist/types/tests/utils/test-stub-generator.d.ts.map +1 -0
  701. package/dist/types/tests/utils/time.spec.d.ts +2 -0
  702. package/dist/types/tests/utils/time.spec.d.ts.map +1 -0
  703. package/dist/types/tests/utils/url.spec.d.ts +2 -0
  704. package/dist/types/tests/utils/url.spec.d.ts.map +1 -0
  705. package/dist/types/tests/validation/json-schemas/definitions.spec.d.ts +2 -0
  706. package/dist/types/tests/validation/json-schemas/definitions.spec.d.ts.map +1 -0
  707. package/dist/types/tests/validation/json-schemas/jwk/general-jwk.spec.d.ts +2 -0
  708. package/dist/types/tests/validation/json-schemas/jwk/general-jwk.spec.d.ts.map +1 -0
  709. package/dist/types/tests/validation/json-schemas/jwk/public-jwk.spec.d.ts +2 -0
  710. package/dist/types/tests/validation/json-schemas/jwk/public-jwk.spec.d.ts.map +1 -0
  711. package/dist/types/tests/validation/json-schemas/jwk-verification-method.spec.d.ts +2 -0
  712. package/dist/types/tests/validation/json-schemas/jwk-verification-method.spec.d.ts.map +1 -0
  713. package/dist/types/tests/validation/json-schemas/protocols/protocols-configure.spec.d.ts +2 -0
  714. package/dist/types/tests/validation/json-schemas/protocols/protocols-configure.spec.d.ts.map +1 -0
  715. package/dist/types/tests/validation/json-schemas/records/records-query.spec.d.ts +2 -0
  716. package/dist/types/tests/validation/json-schemas/records/records-query.spec.d.ts.map +1 -0
  717. package/dist/types/tests/validation/json-schemas/records/records-write.spec.d.ts +2 -0
  718. package/dist/types/tests/validation/json-schemas/records/records-write.spec.d.ts.map +1 -0
  719. package/package.json +167 -0
  720. package/src/core/abstract-message.ts +62 -0
  721. package/src/core/auth.ts +36 -0
  722. package/src/core/dwn-constant.ts +9 -0
  723. package/src/core/dwn-error.ts +167 -0
  724. package/src/core/grant-authorization.ts +148 -0
  725. package/src/core/message-reply.ts +41 -0
  726. package/src/core/message.ts +259 -0
  727. package/src/core/messages-grant-authorization.ts +134 -0
  728. package/src/core/protocol-authorization.ts +903 -0
  729. package/src/core/protocols-grant-authorization.ts +88 -0
  730. package/src/core/records-grant-authorization.ts +197 -0
  731. package/src/core/resumable-task-manager.ts +114 -0
  732. package/src/core/tenant-gate.ts +33 -0
  733. package/src/dwn.ts +264 -0
  734. package/src/enums/dwn-interface-method.ts +14 -0
  735. package/src/event-log/event-emitter-stream.ts +69 -0
  736. package/src/event-log/event-log-level.ts +72 -0
  737. package/src/handlers/messages-query.ts +67 -0
  738. package/src/handlers/messages-read.ts +103 -0
  739. package/src/handlers/messages-subscribe.ts +89 -0
  740. package/src/handlers/protocols-configure.ts +137 -0
  741. package/src/handlers/protocols-query.ts +82 -0
  742. package/src/handlers/records-delete.ts +124 -0
  743. package/src/handlers/records-query.ts +262 -0
  744. package/src/handlers/records-read.ts +187 -0
  745. package/src/handlers/records-subscribe.ts +218 -0
  746. package/src/handlers/records-write.ts +404 -0
  747. package/src/index.ts +67 -0
  748. package/src/interfaces/messages-query.ts +60 -0
  749. package/src/interfaces/messages-read.ts +63 -0
  750. package/src/interfaces/messages-subscribe.ts +64 -0
  751. package/src/interfaces/protocols-configure.ts +340 -0
  752. package/src/interfaces/protocols-query.ts +96 -0
  753. package/src/interfaces/records-delete.ts +117 -0
  754. package/src/interfaces/records-query.ts +131 -0
  755. package/src/interfaces/records-read.ts +100 -0
  756. package/src/interfaces/records-subscribe.ts +104 -0
  757. package/src/interfaces/records-write.ts +1072 -0
  758. package/src/jose/algorithms/signing/ed25519.ts +58 -0
  759. package/src/jose/algorithms/signing/signature-algorithms.ts +22 -0
  760. package/src/jose/jws/general/builder.ts +48 -0
  761. package/src/jose/jws/general/verifier.ts +112 -0
  762. package/src/protocols/permission-grant.ts +86 -0
  763. package/src/protocols/permission-request.ts +63 -0
  764. package/src/protocols/permissions.ts +508 -0
  765. package/src/schema-validator.ts +46 -0
  766. package/src/store/blockstore-level.ts +113 -0
  767. package/src/store/blockstore-mock.ts +80 -0
  768. package/src/store/data-store-level.ts +120 -0
  769. package/src/store/index-level.ts +691 -0
  770. package/src/store/level-wrapper.ts +272 -0
  771. package/src/store/message-store-level.ts +195 -0
  772. package/src/store/resumable-task-store-level.ts +120 -0
  773. package/src/store/storage-controller.ts +240 -0
  774. package/src/types/cache.ts +16 -0
  775. package/src/types/data-store.ts +64 -0
  776. package/src/types/event-log.ts +52 -0
  777. package/src/types/jose-types.ts +76 -0
  778. package/src/types/jws-types.ts +28 -0
  779. package/src/types/message-interface.ts +30 -0
  780. package/src/types/message-store.ts +57 -0
  781. package/src/types/message-types.ts +132 -0
  782. package/src/types/messages-types.ts +77 -0
  783. package/src/types/method-handler.ts +19 -0
  784. package/src/types/permission-types.ts +110 -0
  785. package/src/types/protocols-types.ts +177 -0
  786. package/src/types/query-types.ts +61 -0
  787. package/src/types/records-types.ts +263 -0
  788. package/src/types/resumable-task-store.ts +96 -0
  789. package/src/types/signer.ts +27 -0
  790. package/src/types/subscriptions.ts +34 -0
  791. package/src/utils/abort.ts +31 -0
  792. package/src/utils/array.ts +39 -0
  793. package/src/utils/cid.ts +101 -0
  794. package/src/utils/data-stream.ts +99 -0
  795. package/src/utils/encoder.ts +54 -0
  796. package/src/utils/encryption.ts +145 -0
  797. package/src/utils/filter.ts +245 -0
  798. package/src/utils/hd-key.ts +126 -0
  799. package/src/utils/jws.ts +95 -0
  800. package/src/utils/memory-cache.ts +31 -0
  801. package/src/utils/messages.ts +109 -0
  802. package/src/utils/object.ts +43 -0
  803. package/src/utils/private-key-signer.ts +72 -0
  804. package/src/utils/protocols.ts +50 -0
  805. package/src/utils/records.ts +559 -0
  806. package/src/utils/secp256k1.ts +157 -0
  807. package/src/utils/secp256r1.ts +142 -0
  808. package/src/utils/string.ts +13 -0
  809. package/src/utils/time.ts +78 -0
  810. package/src/utils/url.ts +65 -0
@@ -0,0 +1,1072 @@
1
+ import type { GeneralJws } from '../types/jws-types.js';
2
+ import type { KeyValues } from '../types/query-types.js';
3
+ import type { MessageInterface } from '../types/message-interface.js';
4
+ import type { MessageStore } from '../types/message-store.js';
5
+ import type { PublicJwk } from '../types/jose-types.js';
6
+ import type { Signer } from '../types/signer.js';
7
+ import type {
8
+ DataEncodedRecordsWriteMessage,
9
+ EncryptedKey,
10
+ EncryptionProperty,
11
+ InternalRecordsWriteMessage,
12
+ RecordsWriteAttestationPayload,
13
+ RecordsWriteDescriptor,
14
+ RecordsWriteMessage,
15
+ RecordsWriteSignaturePayload,
16
+ RecordsWriteTags
17
+ } from '../types/records-types.js';
18
+ import type { GenericMessage, GenericSignaturePayload } from '../types/message-types.js';
19
+
20
+ import { Cid } from '../utils/cid.js';
21
+ import { Encoder } from '../utils/encoder.js';
22
+ import { Encryption } from '../utils/encryption.js';
23
+ import { EncryptionAlgorithm } from '../utils/encryption.js';
24
+ import { GeneralJwsBuilder } from '../jose/jws/general/builder.js';
25
+ import { Jws } from '../utils/jws.js';
26
+ import { KeyDerivationScheme } from '../utils/hd-key.js';
27
+ import { Message } from '../core/message.js';
28
+ import { PermissionGrant } from '../protocols/permission-grant.js';
29
+ import { Records } from '../utils/records.js';
30
+ import { RecordsGrantAuthorization } from '../core/records-grant-authorization.js';
31
+ import { removeUndefinedProperties } from '../utils/object.js';
32
+ import { Secp256k1 } from '../utils/secp256k1.js';
33
+ import { Time } from '../utils/time.js';
34
+ import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
35
+ import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
36
+ import { normalizeProtocolUrl, normalizeSchemaUrl, validateProtocolUrlNormalized, validateSchemaUrlNormalized } from '../utils/url.js';
37
+
38
+ export type RecordsWriteOptions = {
39
+ recipient?: string;
40
+ protocol?: string;
41
+ protocolPath?: string;
42
+ protocolRole?: string;
43
+ schema?: string;
44
+ tags?: RecordsWriteTags;
45
+ recordId?: string;
46
+
47
+ /**
48
+ * Must be given if this message is for a non-root protocol record.
49
+ * If not given, it either means this write is for a root protocol record or a flat-space record.
50
+ */
51
+ parentContextId?: string;
52
+
53
+ data?: Uint8Array;
54
+ dataCid?: string;
55
+ dataSize?: number;
56
+ dateCreated?: string;
57
+ messageTimestamp?: string;
58
+ published?: boolean;
59
+ datePublished?: string;
60
+ dataFormat: string;
61
+
62
+ /**
63
+ * The signer of the message, which is commonly the author, but can also be a delegate.
64
+ */
65
+ signer?: Signer;
66
+
67
+ /**
68
+ * The delegated grant invoked to sign on behalf of the logical author, which is the grantor of the delegated grant.
69
+ */
70
+ delegatedGrant?: DataEncodedRecordsWriteMessage;
71
+
72
+ attestationSigners?: Signer[];
73
+ encryptionInput?: EncryptionInput;
74
+ permissionGrantId?: string;
75
+ };
76
+
77
+ /**
78
+ * Input that describes how data is encrypted as spec-ed in TP18 (https://github.com/TBD54566975/technical-proposals/pull/6).
79
+ */
80
+ export type EncryptionInput = {
81
+ /**
82
+ * Algorithm used for encrypting the Data. Uses {EncryptionAlgorithm.Aes256Ctr} if not given.
83
+ */
84
+ algorithm?: EncryptionAlgorithm;
85
+
86
+ /**
87
+ * Initialization vector used for encrypting the data.
88
+ */
89
+ initializationVector: Uint8Array;
90
+
91
+ /**
92
+ * Symmetric key used to encrypt the data.
93
+ */
94
+ key: Uint8Array;
95
+
96
+ /**
97
+ * Array of input that specifies how the symmetric key is encrypted.
98
+ * Each entry in the array will result in a unique ciphertext of the symmetric key.
99
+ */
100
+ keyEncryptionInputs: KeyEncryptionInput[];
101
+ };
102
+
103
+ /**
104
+ * Input that specifies how a symmetric key is encrypted.
105
+ */
106
+ export type KeyEncryptionInput = {
107
+ /**
108
+ * Key derivation scheme used to derive the public key to encrypt the symmetric key.
109
+ */
110
+ derivationScheme: KeyDerivationScheme;
111
+
112
+ /**
113
+ * Fully qualified ID of root public key used derive the public key to be used to to encrypt the symmetric key.
114
+ * (e.g. did:example:abc#encryption-key-id)
115
+ */
116
+ publicKeyId: string;
117
+
118
+ /**
119
+ * Public key to be used to encrypt the symmetric key.
120
+ */
121
+ publicKey: PublicJwk;
122
+
123
+ /**
124
+ * Algorithm used for encrypting the symmetric key. Uses {EncryptionAlgorithm.EciesSecp256k1} if not given.
125
+ */
126
+ algorithm?: EncryptionAlgorithm;
127
+ };
128
+
129
+ export type CreateFromOptions = {
130
+ recordsWriteMessage: RecordsWriteMessage,
131
+ data?: Uint8Array;
132
+
133
+ /**
134
+ * The data format of the new data. If not given, the data format from the existing message will be used.
135
+ */
136
+ dataFormat?: string;
137
+
138
+ published?: boolean;
139
+ tags?: RecordsWriteTags;
140
+ messageTimestamp?: string;
141
+ datePublished?: string;
142
+
143
+
144
+ /**
145
+ * The signer of the message, which is commonly the author, but can also be a delegate.
146
+ */
147
+ signer?: Signer;
148
+
149
+ /**
150
+ * The delegated grant to sign on behalf of the logical author, which is the grantor (`grantedBy`) of the delegated grant.
151
+ */
152
+ delegatedGrant?: DataEncodedRecordsWriteMessage;
153
+
154
+ attestationSigners?: Signer[];
155
+ encryptionInput?: EncryptionInput;
156
+ protocolRole?: string;
157
+ };
158
+
159
+ /**
160
+ * A class representing a RecordsWrite DWN message.
161
+ * NOTE: Unable to extend `AbstractMessage` directly because the incompatible `_message` type, which is not just a generic `<M>` type.
162
+ */
163
+ export class RecordsWrite implements MessageInterface<RecordsWriteMessage> {
164
+ private parentContextId: string | undefined;
165
+
166
+ private _message: InternalRecordsWriteMessage;
167
+ /**
168
+ * Valid JSON message representing this RecordsWrite.
169
+ * @throws `DwnErrorCode.RecordsWriteMissingSigner` if the message is not signed yet.
170
+ */
171
+ public get message(): RecordsWriteMessage {
172
+ if (this._message.authorization === undefined) {
173
+ throw new DwnError(
174
+ DwnErrorCode.RecordsWriteMissingSigner,
175
+ 'This RecordsWrite is not yet signed, JSON message cannot be generated from an incomplete state.'
176
+ );
177
+ }
178
+
179
+ return this._message as RecordsWriteMessage;
180
+ }
181
+
182
+ private _author: string | undefined;
183
+ public get author(): string | undefined {
184
+ return this._author;
185
+ }
186
+
187
+ private _signaturePayload: RecordsWriteSignaturePayload | undefined;
188
+ public get signaturePayload(): RecordsWriteSignaturePayload | undefined {
189
+ return this._signaturePayload;
190
+ }
191
+
192
+ private _owner: string | undefined;
193
+ /**
194
+ * The owner DID of the message if owner signature is present in the message; `undefined` otherwise.
195
+ * This is the logical owner of the message, not to be confused with the actual signer of the owner signature,
196
+ * this is because the signer of the owner signature may not be the actual DWN owner, but a delegate authorized by the owner.
197
+ */
198
+ public get owner(): string | undefined {
199
+ return this._owner;
200
+ }
201
+
202
+ private _ownerSignaturePayload: GenericSignaturePayload | undefined;
203
+ /**
204
+ * Decoded owner signature payload.
205
+ */
206
+ public get ownerSignaturePayload(): GenericSignaturePayload | undefined {
207
+ return this._ownerSignaturePayload;
208
+ }
209
+
210
+ /**
211
+ * If this message is signed by an author-delegate.
212
+ */
213
+ public get isSignedByAuthorDelegate(): boolean {
214
+ return Message.isSignedByAuthorDelegate(this._message);
215
+ }
216
+
217
+ /**
218
+ * If this message is signed by an owner-delegate.
219
+ */
220
+ public get isSignedByOwnerDelegate(): boolean {
221
+ return Message.isSignedByOwnerDelegate(this._message);
222
+ }
223
+
224
+ /**
225
+ * Gets the signer of this message.
226
+ * This is not to be confused with the logical author of the message.
227
+ */
228
+ public get signer(): string | undefined {
229
+ return Message.getSigner(this._message);
230
+ }
231
+
232
+ /**
233
+ * Gets the signer of owner signature; `undefined` if owner signature is not present in the message.
234
+ * This is not to be confused with the logical owner {@link #owner} of the message,
235
+ * this is because the signer of the owner signature may not be the actual DWN owner, but a delegate authorized by the owner.
236
+ * In the case that the owner signature is signed by the actual DWN owner, this value will be the same as {@link #owner}.
237
+ */
238
+ public get ownerSignatureSigner(): string | undefined {
239
+ if (this._message.authorization?.ownerSignature === undefined) {
240
+ return undefined;
241
+ }
242
+
243
+ const signer = Jws.getSignerDid(this._message.authorization.ownerSignature.signatures[0]);
244
+ return signer;
245
+ }
246
+
247
+ readonly attesters: string[];
248
+
249
+ private constructor(message: InternalRecordsWriteMessage, parentContextId?: string) {
250
+ this.parentContextId = parentContextId;
251
+ this._message = message;
252
+
253
+ if (message.authorization !== undefined) {
254
+ this._author = Message.getAuthor(message as RecordsWriteMessage);
255
+
256
+ this._signaturePayload = Jws.decodePlainObjectPayload(message.authorization.signature);
257
+
258
+ if (message.authorization.ownerSignature !== undefined) {
259
+ // if the message authorization contains owner delegated grant, the owner would be the grantor of the grant
260
+ // else the owner would be the signer of the owner signature
261
+ if (message.authorization.ownerDelegatedGrant !== undefined) {
262
+ this._owner = Message.getSigner(message.authorization.ownerDelegatedGrant);
263
+ } else {
264
+ this._owner = Jws.getSignerDid(message.authorization.ownerSignature.signatures[0]);
265
+ }
266
+
267
+ this._ownerSignaturePayload = Jws.decodePlainObjectPayload(message.authorization.ownerSignature);
268
+ }
269
+ }
270
+
271
+ this.attesters = RecordsWrite.getAttesters(message);
272
+
273
+ // consider converting isInitialWrite() & getEntryId() into properties for performance and convenience
274
+ }
275
+
276
+ /**
277
+ * Parses a RecordsWrite message and returns a {RecordsWrite} instance.
278
+ */
279
+ public static async parse(recordsWriteMessage: RecordsWriteMessage): Promise<RecordsWrite> {
280
+ // Make a copy so that the stored copy is not subject to external, unexpected modification.
281
+ const message = JSON.parse(JSON.stringify(recordsWriteMessage)) as RecordsWriteMessage;
282
+
283
+ // asynchronous checks that are required by the constructor to initialize members properly
284
+
285
+ await Message.validateSignatureStructure(message.authorization.signature, message.descriptor, 'RecordsWriteSignaturePayload');
286
+
287
+ if (message.authorization.ownerSignature !== undefined) {
288
+ await Message.validateSignatureStructure(message.authorization.ownerSignature, message.descriptor);
289
+ }
290
+
291
+ await RecordsWrite.validateAttestationIntegrity(message);
292
+
293
+ const recordsWrite = new RecordsWrite(message);
294
+
295
+ await recordsWrite.validateIntegrity(); // RecordsWrite specific data integrity check
296
+
297
+ return recordsWrite;
298
+ }
299
+
300
+ /**
301
+ * Creates a RecordsWrite message.
302
+ * @param options.recordId If `undefined`, will be auto-filled as the initial message as convenience for developer.
303
+ * @param options.data Data used to compute the `dataCid`, must be the encrypted data bytes if `options.encryptionInput` is given.
304
+ * Must specify `options.dataCid` if `undefined`.
305
+ * @param options.dataCid CID of the data that is already stored in the DWN. Must specify `options.data` if `undefined`.
306
+ * @param options.dataSize Size of data in number of bytes. Must be defined if `options.dataCid` is defined; must be `undefined` otherwise.
307
+ * @param options.dateCreated If `undefined`, it will be auto-filled with current time.
308
+ * @param options.messageTimestamp If `undefined`, it will be auto-filled with current time.
309
+ * @param options.parentContextId Must be given if this message is for a non-root protocol record.
310
+ * If not given, it either means this write is for a root protocol record or a flat-space record.
311
+ */
312
+ public static async create(options: RecordsWriteOptions): Promise<RecordsWrite> {
313
+ if ((options.protocol === undefined && options.protocolPath !== undefined) ||
314
+ (options.protocol !== undefined && options.protocolPath === undefined)) {
315
+ throw new DwnError(DwnErrorCode.RecordsWriteCreateProtocolAndProtocolPathMutuallyInclusive, '`protocol` and `protocolPath` must both be defined or undefined at the same time');
316
+ }
317
+
318
+ if ((options.data === undefined && options.dataCid === undefined) ||
319
+ (options.data !== undefined && options.dataCid !== undefined)) {
320
+ throw new DwnError(DwnErrorCode.RecordsWriteCreateDataAndDataCidMutuallyExclusive, 'one and only one parameter between `data` and `dataCid` is required');
321
+ }
322
+
323
+ if ((options.dataCid === undefined && options.dataSize !== undefined) ||
324
+ (options.dataCid !== undefined && options.dataSize === undefined)) {
325
+ throw new DwnError(DwnErrorCode.RecordsWriteCreateDataCidAndDataSizeMutuallyInclusive, '`dataCid` and `dataSize` must both be defined or undefined at the same time');
326
+ }
327
+
328
+ if (options.signer === undefined && options.delegatedGrant !== undefined) {
329
+ throw new DwnError(DwnErrorCode.RecordsWriteCreateMissingSigner, '`signer` must be given when `delegatedGrant` is given');
330
+ }
331
+
332
+ const dataCid = options.dataCid ?? await Cid.computeDagPbCidFromBytes(options.data!);
333
+ const dataSize = options.dataSize ?? options.data!.length;
334
+
335
+ const currentTime = Time.getCurrentTimestamp();
336
+
337
+ const descriptor: RecordsWriteDescriptor = {
338
+ interface : DwnInterfaceName.Records,
339
+ method : DwnMethodName.Write,
340
+ protocol : options.protocol !== undefined ? normalizeProtocolUrl(options.protocol) : undefined,
341
+ protocolPath : options.protocolPath,
342
+ recipient : options.recipient,
343
+ schema : options.schema !== undefined ? normalizeSchemaUrl(options.schema) : undefined,
344
+ tags : options.tags,
345
+ parentId : RecordsWrite.getRecordIdFromContextId(options.parentContextId),
346
+ dataCid,
347
+ dataSize,
348
+ dateCreated : options.dateCreated ?? currentTime,
349
+ messageTimestamp : options.messageTimestamp ?? currentTime,
350
+ published : options.published,
351
+ datePublished : options.datePublished,
352
+ dataFormat : options.dataFormat
353
+ };
354
+
355
+ // generate `datePublished` if the message is to be published but `datePublished` is not given
356
+ if (options.published === true &&
357
+ options.datePublished === undefined) {
358
+ descriptor.datePublished = currentTime;
359
+ }
360
+
361
+ // delete all descriptor properties that are `undefined` else the code will encounter the following IPLD issue when attempting to generate CID:
362
+ // Error: `undefined` is not supported by the IPLD Data Model and cannot be encoded
363
+ removeUndefinedProperties(descriptor);
364
+
365
+ // `recordId` computation
366
+ const recordId = options.recordId;
367
+
368
+ // `attestation` generation
369
+ const descriptorCid = await Cid.computeCid(descriptor);
370
+ const attestation = await RecordsWrite.createAttestation(descriptorCid, options.attestationSigners);
371
+
372
+ // `encryption` generation
373
+ const encryption = await RecordsWrite.createEncryptionProperty(descriptor, options.encryptionInput);
374
+
375
+ const message: InternalRecordsWriteMessage = {
376
+ recordId,
377
+ descriptor
378
+ };
379
+
380
+ // assign optional properties only if they exist
381
+ if (attestation !== undefined) { message.attestation = attestation; }
382
+ if (encryption !== undefined) { message.encryption = encryption; }
383
+
384
+ const recordsWrite = new RecordsWrite(message, options.parentContextId);
385
+
386
+ if (options.signer !== undefined) {
387
+ await recordsWrite.sign({
388
+ signer : options.signer,
389
+ delegatedGrant : options.delegatedGrant,
390
+ permissionGrantId : options.permissionGrantId,
391
+ protocolRole : options.protocolRole
392
+ });
393
+ }
394
+
395
+ return recordsWrite;
396
+ }
397
+
398
+ private static getRecordIdFromContextId(contextId: string | undefined): string | undefined {
399
+ return contextId?.split('/').filter(segment => segment !== '').pop();
400
+ }
401
+
402
+ /**
403
+ * Convenience method that creates a message by:
404
+ * 1. Copying over immutable properties from the given source message
405
+ * 2. Copying over mutable properties that are not overwritten from the given source message
406
+ * 3. Replace the mutable properties that are given new value
407
+ * @param options.recordsWriteMessage Message that the new RecordsWrite will be based from.
408
+ * @param options.messageTimestamp The new date the record is modified. If not given, current time will be used .
409
+ * @param options.data The new data or the record. If not given, data from given message will be used.
410
+ * @param options.published The new published state. If not given, then will be set to `true` if {options.messageTimestamp} is given;
411
+ * else the state from given message will be used.
412
+ * @param options.publishedDate The new date the record is modified. If not given, then:
413
+ * - will not be set if the record will be unpublished as the result of this RecordsWrite; else
414
+ * - will be set to the same published date as the given message if it wss already published; else
415
+ * - will be set to current time (because this is a toggle from unpublished to published)
416
+ */
417
+ public static async createFrom(options: CreateFromOptions): Promise<RecordsWrite> {
418
+ const sourceMessage = options.recordsWriteMessage;
419
+ const sourceRecordsWrite = await RecordsWrite.parse(sourceMessage);
420
+ const currentTime = Time.getCurrentTimestamp();
421
+
422
+ // inherit published value from parent if neither published nor datePublished is specified
423
+ const published = options.published ?? (options.datePublished ? true : sourceMessage.descriptor.published);
424
+ // use current time if published but no explicit time given
425
+ let datePublished: string | undefined = undefined;
426
+ // if given explicitly published dated
427
+ if (options.datePublished) {
428
+ datePublished = options.datePublished;
429
+ } else {
430
+ // if this RecordsWrite will publish the record
431
+ if (published) {
432
+ // the parent was already published, inherit the same published date
433
+ if (sourceMessage.descriptor.published) {
434
+ datePublished = sourceMessage.descriptor.datePublished;
435
+ } else {
436
+ // this is a toggle from unpublished to published, use current time
437
+ datePublished = currentTime;
438
+ }
439
+ }
440
+ }
441
+
442
+ const createOptions: RecordsWriteOptions = {
443
+ // immutable properties below, just copy from the source message
444
+ recipient : sourceMessage.descriptor.recipient,
445
+ recordId : sourceMessage.recordId,
446
+ dateCreated : sourceMessage.descriptor.dateCreated,
447
+ protocol : sourceMessage.descriptor.protocol,
448
+ protocolPath : sourceMessage.descriptor.protocolPath,
449
+ schema : sourceMessage.descriptor.schema,
450
+ parentContextId : Records.getParentContextFromOfContextId(sourceMessage.contextId),
451
+ // mutable properties below
452
+ messageTimestamp : options.messageTimestamp ?? currentTime,
453
+ published,
454
+ datePublished,
455
+ tags : options.tags,
456
+ data : options.data,
457
+ dataCid : options.data ? undefined : sourceMessage.descriptor.dataCid, // if new `data` not given, use value from source message
458
+ dataSize : options.data ? undefined : sourceMessage.descriptor.dataSize, // if new `data` not given, use value from source message
459
+ dataFormat : options.dataFormat ?? sourceMessage.descriptor.dataFormat,
460
+ protocolRole : options.protocolRole ?? sourceRecordsWrite.signaturePayload!.protocolRole, // if not given, use value from source message
461
+ delegatedGrant : options.delegatedGrant,
462
+ // finally still need signers
463
+ signer : options.signer,
464
+ attestationSigners : options.attestationSigners
465
+ };
466
+
467
+ const recordsWrite = await RecordsWrite.create(createOptions);
468
+ return recordsWrite;
469
+ }
470
+
471
+ /**
472
+ * Called by `JSON.stringify(...)` automatically.
473
+ */
474
+ toJSON(): RecordsWriteMessage {
475
+ return this.message;
476
+ }
477
+
478
+ /**
479
+ * Encrypts the symmetric encryption key using the public keys given and attach the resulting `encryption` property to the RecordsWrite.
480
+ */
481
+ public async encryptSymmetricEncryptionKey(encryptionInput: EncryptionInput): Promise<void> {
482
+ this._message.encryption = await RecordsWrite.createEncryptionProperty(this._message.descriptor, encryptionInput);
483
+
484
+ // opportunity here to re-sign instead of remove
485
+ delete this._message.authorization;
486
+ this._signaturePayload = undefined;
487
+ this._author = undefined;
488
+ }
489
+
490
+ /**
491
+ * Signs the RecordsWrite, the signer is commonly the author, but can also be a delegate.
492
+ */
493
+ public async sign(options: {
494
+ signer: Signer,
495
+ delegatedGrant?: DataEncodedRecordsWriteMessage,
496
+ permissionGrantId?: string,
497
+ protocolRole?: string
498
+ }): Promise<void> {
499
+ const { signer, delegatedGrant, permissionGrantId, protocolRole } = options;
500
+
501
+ // compute delegated grant ID and author if delegated grant is given
502
+ let delegatedGrantId;
503
+ let authorDid;
504
+ if (delegatedGrant !== undefined) {
505
+ delegatedGrantId = await Message.getCid(delegatedGrant);
506
+ authorDid = Jws.getSignerDid(delegatedGrant.authorization.signature.signatures[0]);
507
+ } else {
508
+ authorDid = Jws.extractDid(signer.keyId);
509
+ }
510
+
511
+ const descriptor = this._message.descriptor;
512
+ const descriptorCid = await Cid.computeCid(descriptor);
513
+
514
+ // compute `recordId` if not given at construction time
515
+ this._message.recordId = this._message.recordId ?? await RecordsWrite.getEntryId(authorDid, descriptor);
516
+
517
+ // compute `contextId` if this is a protocol-space record
518
+ if (this._message.descriptor.protocol !== undefined) {
519
+ // if `parentContextId` is not given, this is a root protocol record
520
+ if (this.parentContextId === undefined || this.parentContextId === '') {
521
+ this._message.contextId = this._message.recordId;
522
+ } else {
523
+ // else this is a non-root protocol record
524
+
525
+ this._message.contextId = this.parentContextId + '/' + this._message.recordId;
526
+ }
527
+ }
528
+
529
+ // `signature` generation
530
+ const signature = await RecordsWrite.createSignerSignature({
531
+ recordId : this._message.recordId,
532
+ contextId : this._message.contextId,
533
+ descriptorCid,
534
+ attestation : this._message.attestation,
535
+ encryption : this._message.encryption,
536
+ signer,
537
+ delegatedGrantId,
538
+ permissionGrantId,
539
+ protocolRole
540
+ });
541
+
542
+ this._message.authorization = { signature };
543
+
544
+ if (delegatedGrant !== undefined) {
545
+ this._message.authorization.authorDelegatedGrant = delegatedGrant;
546
+ }
547
+
548
+ // there is opportunity to optimize here as the payload is constructed within `createAuthorization(...)`
549
+ this._signaturePayload = Jws.decodePlainObjectPayload(signature);
550
+ this._author = authorDid;
551
+ }
552
+
553
+ /**
554
+ * Signs the `RecordsWrite` as the DWN owner.
555
+ * This is used when the DWN owner wants to retain a copy of a message that the owner did not author.
556
+ * NOTE: requires the `RecordsWrite` to already have the author's signature.
557
+ */
558
+ public async signAsOwner(signer: Signer): Promise<void> {
559
+ if (this._author === undefined) {
560
+ throw new DwnError(
561
+ DwnErrorCode.RecordsWriteSignAsOwnerUnknownAuthor,
562
+ 'Unable to sign as owner without message signature because owner needs to sign over `recordId` which depends on author DID.');
563
+ }
564
+
565
+ const descriptor = this._message.descriptor;
566
+ const ownerSignature = await Message.createSignature(descriptor, signer);
567
+
568
+ this._message.authorization!.ownerSignature = ownerSignature;
569
+
570
+ this._ownerSignaturePayload = Jws.decodePlainObjectPayload(ownerSignature);
571
+ this._owner = Jws.extractDid(signer.keyId);
572
+ ;
573
+ }
574
+
575
+ /**
576
+ * Signs the `RecordsWrite` as the DWN owner-delegate.
577
+ * This is used when a DWN owner-delegate wants to retain a copy of a message that the owner did not author.
578
+ * NOTE: requires the `RecordsWrite` to already have the author's signature.
579
+ */
580
+ public async signAsOwnerDelegate(signer: Signer, delegatedGrant: DataEncodedRecordsWriteMessage): Promise<void> {
581
+ if (this._author === undefined) {
582
+ throw new DwnError(
583
+ DwnErrorCode.RecordsWriteSignAsOwnerDelegateUnknownAuthor,
584
+ 'Unable to sign as owner delegate without message signature because owner delegate needs to sign over `recordId` which depends on author DID.');
585
+ }
586
+
587
+ const delegatedGrantId = await Message.getCid(delegatedGrant);
588
+
589
+ const descriptor = this._message.descriptor;
590
+ const ownerSignature = await Message.createSignature(descriptor, signer, { delegatedGrantId });
591
+
592
+ this._message.authorization!.ownerSignature = ownerSignature;
593
+ this._message.authorization!.ownerDelegatedGrant = delegatedGrant;
594
+
595
+ this._ownerSignaturePayload = Jws.decodePlainObjectPayload(ownerSignature);
596
+ this._owner = Jws.getSignerDid(delegatedGrant.authorization.signature.signatures[0]);
597
+ }
598
+
599
+ /**
600
+ * Validates the integrity of the RecordsWrite message assuming the message passed basic schema validation.
601
+ * There is opportunity to integrate better with `validateSchema(...)`
602
+ */
603
+ private async validateIntegrity(): Promise<void> {
604
+ // if the new message is the initial write
605
+ const isInitialWrite = await this.isInitialWrite();
606
+ if (isInitialWrite) {
607
+ // `messageTimestamp` and `dateCreated` equality check
608
+ const dateRecordCreated = this.message.descriptor.dateCreated;
609
+ const messageTimestamp = this.message.descriptor.messageTimestamp;
610
+ if (messageTimestamp !== dateRecordCreated) {
611
+ throw new DwnError(
612
+ DwnErrorCode.RecordsWriteValidateIntegrityDateCreatedMismatch,
613
+ `messageTimestamp ${messageTimestamp} must match dateCreated ${dateRecordCreated} for the initial write`
614
+ );
615
+ }
616
+
617
+ // if the message is also a protocol context root, the `contextId` must match the expected deterministic value
618
+ if (this.message.descriptor.protocol !== undefined &&
619
+ this.message.descriptor.parentId === undefined) {
620
+ const expectedContextId = await this.getEntryId();
621
+
622
+ if (this.message.contextId !== expectedContextId) {
623
+ throw new DwnError(
624
+ DwnErrorCode.RecordsWriteValidateIntegrityContextIdMismatch,
625
+ `contextId in message: ${this.message.contextId} does not match deterministic contextId: ${expectedContextId}`
626
+ );
627
+ }
628
+ }
629
+ }
630
+
631
+ // NOTE: validateSignatureStructure() call earlier enforces the presence of `authorization` and thus `signature` in RecordsWrite
632
+ const signaturePayload = this.signaturePayload!;
633
+
634
+ // make sure the `recordId` in message is the same as the `recordId` in the payload of the message signature
635
+ if (this.message.recordId !== signaturePayload.recordId) {
636
+ throw new DwnError(
637
+ DwnErrorCode.RecordsWriteValidateIntegrityRecordIdUnauthorized,
638
+ `recordId in message ${this.message.recordId} does not match recordId in authorization: ${signaturePayload.recordId}`
639
+ );
640
+ }
641
+
642
+ // if `contextId` is given in message, make sure the same `contextId` is in the payload of the message signature
643
+ if (this.message.contextId !== signaturePayload.contextId) {
644
+ throw new DwnError(
645
+ DwnErrorCode.RecordsWriteValidateIntegrityContextIdNotInSignerSignaturePayload,
646
+ `contextId in message ${this.message.contextId} does not match contextId in authorization: ${signaturePayload.contextId}`
647
+ );
648
+ }
649
+
650
+ await Records.validateDelegatedGrantReferentialIntegrity(this.message, signaturePayload, this.ownerSignaturePayload);
651
+
652
+ // if `attestation` is given in message, make sure the correct `attestationCid` is in the payload of the message signature
653
+ if (signaturePayload.attestationCid !== undefined) {
654
+ const expectedAttestationCid = await Cid.computeCid(this.message.attestation);
655
+ const actualAttestationCid = signaturePayload.attestationCid;
656
+ if (actualAttestationCid !== expectedAttestationCid) {
657
+ throw new DwnError(
658
+ DwnErrorCode.RecordsWriteValidateIntegrityAttestationMismatch,
659
+ `CID ${expectedAttestationCid} of attestation property in message does not match attestationCid in authorization: ${actualAttestationCid}`
660
+ );
661
+ }
662
+ }
663
+
664
+ // if `encryption` is given in message, make sure the correct `encryptionCid` is in the payload of the message signature
665
+ if (signaturePayload.encryptionCid !== undefined) {
666
+ const expectedEncryptionCid = await Cid.computeCid(this.message.encryption);
667
+ const actualEncryptionCid = signaturePayload.encryptionCid;
668
+ if (actualEncryptionCid !== expectedEncryptionCid) {
669
+ throw new DwnError(
670
+ DwnErrorCode.RecordsWriteValidateIntegrityEncryptionCidMismatch,
671
+ `CID ${expectedEncryptionCid} of encryption property in message does not match encryptionCid in authorization: ${actualEncryptionCid}`
672
+ );
673
+ }
674
+ }
675
+
676
+ if (this.message.descriptor.protocol !== undefined) {
677
+ validateProtocolUrlNormalized(this.message.descriptor.protocol);
678
+ }
679
+ if (this.message.descriptor.schema !== undefined) {
680
+ validateSchemaUrlNormalized(this.message.descriptor.schema);
681
+ }
682
+
683
+ Time.validateTimestamp(this.message.descriptor.messageTimestamp);
684
+ Time.validateTimestamp(this.message.descriptor.dateCreated);
685
+ if (this.message.descriptor.datePublished) {
686
+ Time.validateTimestamp(this.message.descriptor.datePublished);
687
+ }
688
+ }
689
+
690
+ /**
691
+ * Validates the structural integrity of the `attestation` property.
692
+ * NOTE: signature is not verified.
693
+ */
694
+ private static async validateAttestationIntegrity(message: RecordsWriteMessage): Promise<void> {
695
+ if (message.attestation === undefined) {
696
+ return;
697
+ }
698
+
699
+ // TODO: multi-attesters to be unblocked by #205 - Revisit database interfaces (https://github.com/TBD54566975/dwn-sdk-js/issues/205)
700
+ if (message.attestation.signatures.length !== 1) {
701
+ throw new DwnError(
702
+ DwnErrorCode.RecordsWriteAttestationIntegrityMoreThanOneSignature,
703
+ `Currently implementation only supports 1 attester, but got ${message.attestation.signatures.length}`
704
+ );
705
+ }
706
+
707
+ const payloadJson = Jws.decodePlainObjectPayload(message.attestation);
708
+ const { descriptorCid } = payloadJson;
709
+
710
+ // `descriptorCid` validation - ensure that the provided descriptorCid matches the CID of the actual message
711
+ const expectedDescriptorCid = await Cid.computeCid(message.descriptor);
712
+ if (descriptorCid !== expectedDescriptorCid) {
713
+ throw new DwnError(
714
+ DwnErrorCode.RecordsWriteAttestationIntegrityDescriptorCidMismatch,
715
+ `descriptorCid ${descriptorCid} does not match expected descriptorCid ${expectedDescriptorCid}`
716
+ );
717
+ }
718
+
719
+ // check to ensure that no other unexpected properties exist in payload.
720
+ const propertyCount = Object.keys(payloadJson).length;
721
+ if (propertyCount > 1) {
722
+ throw new DwnError(
723
+ DwnErrorCode.RecordsWriteAttestationIntegrityInvalidPayloadProperty,
724
+ `Only 'descriptorCid' is allowed in attestation payload, but got ${propertyCount} properties.`
725
+ );
726
+ }
727
+ };
728
+
729
+ /**
730
+ * Computes the deterministic Entry ID of this message.
731
+ */
732
+ public async getEntryId(): Promise<string> {
733
+ const entryId = await RecordsWrite.getEntryId(this.author, this.message.descriptor);
734
+ return entryId;
735
+ };
736
+
737
+ /**
738
+ * Computes the deterministic Entry ID of this message.
739
+ */
740
+ public static async getEntryId(author: string | undefined, descriptor: RecordsWriteDescriptor): Promise<string> {
741
+ if (author === undefined) {
742
+ throw new DwnError(DwnErrorCode.RecordsWriteGetEntryIdUndefinedAuthor, 'Property `author` is needed to compute entry ID.');
743
+ }
744
+
745
+ const entryIdInput = { ...descriptor };
746
+ (entryIdInput as any).author = author;
747
+
748
+ const cid = await Cid.computeCid(entryIdInput);
749
+ return cid;
750
+ };
751
+
752
+ /**
753
+ * Checks if the given message is the initial entry of a record.
754
+ */
755
+ public async isInitialWrite(): Promise<boolean> {
756
+ const entryId = await this.getEntryId();
757
+ return (entryId === this.message.recordId);
758
+ }
759
+
760
+ public async constructIndexes(
761
+ isLatestBaseState: boolean
762
+ ): Promise<KeyValues> {
763
+ const message = this.message;
764
+ // we want to process tags separately from the rest of descriptors as it is an object and not a primitive KeyValue type.
765
+ const { tags, ...descriptor } = message.descriptor;
766
+ delete descriptor.published; // handle `published` specifically further down
767
+
768
+ let indexes: KeyValues = {
769
+ ...descriptor,
770
+ isLatestBaseState,
771
+ published : !!message.descriptor.published,
772
+ author : this.author!, //author will not be undefined when indexes are constructed as it's been authorized
773
+ recordId : message.recordId,
774
+ entryId : await RecordsWrite.getEntryId(this.author, this.message.descriptor)
775
+ };
776
+
777
+ // in order to avoid name clashes with first-class index keys
778
+ // we build the indexes with `tag.property_name` for each tag property.
779
+ // we only index tags if the message is the latest base state, as that's the only time filtering for tags is relevant.
780
+ if (tags !== undefined && isLatestBaseState === true) {
781
+ const flattenedTags = Records.buildTagIndexes({ ...tags });
782
+ indexes = { ...indexes, ...flattenedTags };
783
+ }
784
+
785
+ // add additional indexes to optional values if given
786
+ // TODO: index multi-attesters to be unblocked by #205 - Revisit database interfaces (https://github.com/TBD54566975/dwn-sdk-js/issues/205)
787
+ if (this.attesters.length > 0) { indexes.attester = this.attesters[0]; }
788
+ if (message.contextId !== undefined) { indexes.contextId = message.contextId; }
789
+
790
+ return indexes;
791
+ }
792
+
793
+ /**
794
+ * Authorizes the author-delegate who signed this message.
795
+ * @param messageStore Used to check if the grant has been revoked.
796
+ */
797
+ public async authorizeAuthorDelegate(messageStore: MessageStore): Promise<void> {
798
+ const delegatedGrant = await PermissionGrant.parse(this.message.authorization.authorDelegatedGrant!);
799
+ await RecordsGrantAuthorization.authorizeWrite({
800
+ recordsWriteMessage : this.message,
801
+ expectedGrantor : this.author!,
802
+ expectedGrantee : this.signer!,
803
+ permissionGrant : delegatedGrant,
804
+ messageStore
805
+ });
806
+ }
807
+
808
+ /**
809
+ * Authorizes the owner-delegate who signed this message.
810
+ * @param messageStore Used to check if the grant has been revoked.
811
+ */
812
+ public async authorizeOwnerDelegate(messageStore: MessageStore): Promise<void> {
813
+ const delegatedGrant = await PermissionGrant.parse(this.message.authorization.ownerDelegatedGrant!);
814
+ await RecordsGrantAuthorization.authorizeWrite({
815
+ recordsWriteMessage : this.message,
816
+ expectedGrantor : this.owner!,
817
+ expectedGrantee : this.ownerSignatureSigner!,
818
+ permissionGrant : delegatedGrant,
819
+ messageStore
820
+ });
821
+ }
822
+
823
+ /**
824
+ * Checks if the given message is the initial entry of a record.
825
+ */
826
+ public static async isInitialWrite(message: GenericMessage): Promise<boolean> {
827
+ // can't be the initial write if the message is not a Records Write
828
+ if (message.descriptor.interface !== DwnInterfaceName.Records ||
829
+ message.descriptor.method !== DwnMethodName.Write) {
830
+ return false;
831
+ }
832
+
833
+ const recordsWriteMessage = message as RecordsWriteMessage;
834
+ const author = Message.getAuthor(recordsWriteMessage);
835
+ const entryId = await RecordsWrite.getEntryId(author, recordsWriteMessage.descriptor);
836
+ return (entryId === recordsWriteMessage.recordId);
837
+ }
838
+
839
+ /**
840
+ * Creates the `encryption` property if encryption input is given. Else `undefined` is returned.
841
+ * @param descriptor Descriptor of the `RecordsWrite` message which contains the information need by key path derivation schemes.
842
+ */
843
+ private static async createEncryptionProperty(
844
+ descriptor: RecordsWriteDescriptor,
845
+ encryptionInput: EncryptionInput | undefined
846
+ ): Promise<EncryptionProperty | undefined> {
847
+ if (encryptionInput === undefined) {
848
+ return undefined;
849
+ }
850
+
851
+ // encrypt the data encryption key once per encryption input
852
+ const keyEncryption: EncryptedKey[] = [];
853
+ for (const keyEncryptionInput of encryptionInput.keyEncryptionInputs) {
854
+
855
+ if (keyEncryptionInput.derivationScheme === KeyDerivationScheme.ProtocolPath && descriptor.protocol === undefined) {
856
+ throw new DwnError(
857
+ DwnErrorCode.RecordsWriteMissingProtocol,
858
+ '`protocols` encryption scheme cannot be applied to record without the `protocol` property.'
859
+ );
860
+ }
861
+
862
+ if (keyEncryptionInput.derivationScheme === KeyDerivationScheme.Schemas && descriptor.schema === undefined) {
863
+ throw new DwnError(
864
+ DwnErrorCode.RecordsWriteMissingSchema,
865
+ '`schemas` encryption scheme cannot be applied to record without the `schema` property.'
866
+ );
867
+ }
868
+
869
+ // NOTE: right now only `ECIES-ES256K` algorithm is supported for asymmetric encryption,
870
+ // so we will assume that's the algorithm without additional switch/if statements
871
+ const publicKeyBytes = Secp256k1.publicJwkToBytes(keyEncryptionInput.publicKey);
872
+ const keyEncryptionOutput = await Encryption.eciesSecp256k1Encrypt(publicKeyBytes, encryptionInput.key);
873
+
874
+ const encryptedKey = Encoder.bytesToBase64Url(keyEncryptionOutput.ciphertext);
875
+ const ephemeralPublicKey = await Secp256k1.publicKeyToJwk(keyEncryptionOutput.ephemeralPublicKey);
876
+ const keyEncryptionInitializationVector = Encoder.bytesToBase64Url(keyEncryptionOutput.initializationVector);
877
+ const messageAuthenticationCode = Encoder.bytesToBase64Url(keyEncryptionOutput.messageAuthenticationCode);
878
+ const encryptedKeyData: EncryptedKey = {
879
+ rootKeyId : keyEncryptionInput.publicKeyId,
880
+ algorithm : keyEncryptionInput.algorithm ?? EncryptionAlgorithm.EciesSecp256k1,
881
+ derivationScheme : keyEncryptionInput.derivationScheme,
882
+ ephemeralPublicKey,
883
+ initializationVector : keyEncryptionInitializationVector,
884
+ messageAuthenticationCode,
885
+ encryptedKey
886
+ };
887
+
888
+ // we need to attach the actual public key if derivation scheme is protocol-context,
889
+ // so that the responder to this message is able to encrypt the message/symmetric key using the same protocol-context derived public key,
890
+ // without needing the knowledge of the corresponding private key
891
+ if (keyEncryptionInput.derivationScheme === KeyDerivationScheme.ProtocolContext) {
892
+ encryptedKeyData.derivedPublicKey = keyEncryptionInput.publicKey;
893
+ }
894
+
895
+ keyEncryption.push(encryptedKeyData);
896
+ }
897
+
898
+ const encryption: EncryptionProperty = {
899
+ algorithm : encryptionInput.algorithm ?? EncryptionAlgorithm.Aes256Ctr,
900
+ initializationVector : Encoder.bytesToBase64Url(encryptionInput.initializationVector),
901
+ keyEncryption
902
+ };
903
+
904
+ return encryption;
905
+ }
906
+
907
+ /**
908
+ * Creates the `attestation` property of a RecordsWrite message if given signature inputs; returns `undefined` otherwise.
909
+ */
910
+ public static async createAttestation(descriptorCid: string, signers?: Signer[]): Promise<GeneralJws | undefined> {
911
+ if (signers === undefined || signers.length === 0) {
912
+ return undefined;
913
+ }
914
+
915
+ const attestationPayload: RecordsWriteAttestationPayload = { descriptorCid };
916
+ const attestationPayloadBytes = Encoder.objectToBytes(attestationPayload);
917
+
918
+ const builder = await GeneralJwsBuilder.create(attestationPayloadBytes, signers);
919
+ return builder.getJws();
920
+ }
921
+
922
+ /**
923
+ * Creates the `signature` property in the `authorization` of a `RecordsWrite` message.
924
+ */
925
+ public static async createSignerSignature(input: {
926
+ recordId: string,
927
+ contextId: string | undefined,
928
+ descriptorCid: string,
929
+ attestation: GeneralJws | undefined,
930
+ encryption: EncryptionProperty | undefined,
931
+ signer: Signer,
932
+ delegatedGrantId?: string,
933
+ permissionGrantId?: string,
934
+ protocolRole?: string
935
+ }): Promise<GeneralJws> {
936
+ const { recordId, contextId, descriptorCid, attestation, encryption, signer, delegatedGrantId, permissionGrantId, protocolRole } = input;
937
+
938
+ const attestationCid = attestation ? await Cid.computeCid(attestation) : undefined;
939
+ const encryptionCid = encryption ? await Cid.computeCid(encryption) : undefined;
940
+
941
+ const signaturePayload: RecordsWriteSignaturePayload = {
942
+ recordId,
943
+ descriptorCid,
944
+ contextId,
945
+ attestationCid,
946
+ encryptionCid,
947
+ delegatedGrantId,
948
+ permissionGrantId,
949
+ protocolRole
950
+ };
951
+ removeUndefinedProperties(signaturePayload);
952
+
953
+ const signaturePayloadBytes = Encoder.objectToBytes(signaturePayload);
954
+
955
+ const builder = await GeneralJwsBuilder.create(signaturePayloadBytes, [signer]);
956
+ const signature = builder.getJws();
957
+
958
+ return signature;
959
+ }
960
+
961
+ /**
962
+ * Gets the initial write from the given list of `RecordsWrite`.
963
+ */
964
+ public static async getInitialWrite(messages: GenericMessage[]): Promise<RecordsWriteMessage> {
965
+ for (const message of messages) {
966
+ if (await RecordsWrite.isInitialWrite(message)) {
967
+ return message as RecordsWriteMessage;
968
+ }
969
+ }
970
+
971
+ throw new DwnError(DwnErrorCode.RecordsWriteGetInitialWriteNotFound, `Initial write is not found.`);
972
+ }
973
+
974
+ /**
975
+ * Verifies that immutable properties of the two given messages are identical.
976
+ * @throws {Error} if immutable properties between two RecordsWrite message
977
+ */
978
+ public static verifyEqualityOfImmutableProperties(existingWriteMessage: RecordsWriteMessage, newMessage: RecordsWriteMessage): boolean {
979
+ const mutableDescriptorProperties = ['dataCid', 'dataSize', 'dataFormat', 'datePublished', 'published', 'messageTimestamp', 'tags'];
980
+
981
+ // get distinct property names that exist in either the existing message given or new message
982
+ let descriptorPropertyNames: string[] = [];
983
+ descriptorPropertyNames.push(...Object.keys(existingWriteMessage.descriptor));
984
+ descriptorPropertyNames.push(...Object.keys(newMessage.descriptor));
985
+ descriptorPropertyNames = [...new Set(descriptorPropertyNames)]; // step to remove duplicates
986
+
987
+ // ensure all immutable properties are not modified
988
+ for (const descriptorPropertyName of descriptorPropertyNames) {
989
+ // if property is supposed to be immutable
990
+ if (mutableDescriptorProperties.indexOf(descriptorPropertyName) === -1) {
991
+ const valueInExistingWrite = (existingWriteMessage.descriptor as any)[descriptorPropertyName];
992
+ const valueInNewMessage = (newMessage.descriptor as any)[descriptorPropertyName];
993
+ if (valueInNewMessage !== valueInExistingWrite) {
994
+ throw new DwnError(
995
+ DwnErrorCode.RecordsWriteImmutablePropertyChanged,
996
+ `${descriptorPropertyName} is an immutable property: cannot change '${valueInExistingWrite}' to '${valueInNewMessage}'`
997
+ );
998
+ }
999
+ }
1000
+ }
1001
+
1002
+ return true;
1003
+ }
1004
+
1005
+ /**
1006
+ * Gets the DID of the attesters of the given message.
1007
+ */
1008
+ public static getAttesters(message: InternalRecordsWriteMessage): string[] {
1009
+ const attestationSignatures = message.attestation?.signatures ?? [];
1010
+ const attesters = attestationSignatures.map((signature) => Jws.getSignerDid(signature));
1011
+ return attesters;
1012
+ }
1013
+
1014
+ public static async fetchNewestRecordsWrite(
1015
+ messageStore: MessageStore,
1016
+ tenant: string,
1017
+ recordId: string,
1018
+ ): Promise<RecordsWriteMessage> {
1019
+ // get existing RecordsWrite messages matching the `recordId`
1020
+ const query = {
1021
+ interface : DwnInterfaceName.Records,
1022
+ method : DwnMethodName.Write,
1023
+ recordId : recordId
1024
+ };
1025
+
1026
+ const { messages: existingMessages } = await messageStore.query(tenant, [ query ]);
1027
+ const newestWrite = await Message.getNewestMessage(existingMessages);
1028
+ if (newestWrite !== undefined) {
1029
+ return newestWrite as RecordsWriteMessage;
1030
+ }
1031
+
1032
+ throw new DwnError(DwnErrorCode.RecordsWriteGetNewestWriteRecordNotFound, 'record not found');
1033
+ }
1034
+
1035
+ /**
1036
+ * Fetches the initial RecordsWrite of a record.
1037
+ * @returns The initial RecordsWrite if found; `undefined` otherwise.
1038
+ */
1039
+ public static async fetchInitialRecordsWrite(
1040
+ messageStore: MessageStore,
1041
+ tenant: string,
1042
+ recordId: string
1043
+ ): Promise<RecordsWrite | undefined> {
1044
+
1045
+ const initialRecordsWriteMessage = await RecordsWrite.fetchInitialRecordsWriteMessage(messageStore, tenant, recordId);
1046
+ if (initialRecordsWriteMessage === undefined) {
1047
+ return undefined;
1048
+ }
1049
+
1050
+ const initialRecordsWrite = await RecordsWrite.parse(initialRecordsWriteMessage);
1051
+ return initialRecordsWrite;
1052
+ }
1053
+
1054
+ /**
1055
+ * Fetches the initial RecordsWrite message of a record.
1056
+ * @returns The initial RecordsWriteMessage if found; `undefined` otherwise.
1057
+ */
1058
+ public static async fetchInitialRecordsWriteMessage(
1059
+ messageStore: MessageStore,
1060
+ tenant: string,
1061
+ recordId: string
1062
+ ): Promise<RecordsWriteMessage | undefined> {
1063
+ const query = { entryId: recordId };
1064
+ const { messages } = await messageStore.query(tenant, [query]);
1065
+
1066
+ if (messages.length === 0) {
1067
+ return undefined;
1068
+ }
1069
+
1070
+ return messages[0] as RecordsWriteMessage;
1071
+ }
1072
+ }