@textrp/briij-js-sdk 41.0.1 → 43.0.0

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 (380) hide show
  1. package/CHANGELOG.md +14 -1
  2. package/LICENSE +177 -177
  3. package/README.md +85 -3
  4. package/lib/@types/AESEncryptedSecretStoragePayload.js.map +1 -1
  5. package/lib/@types/IIdentityServerProvider.js.map +1 -1
  6. package/lib/@types/PushRules.js +14 -14
  7. package/lib/@types/PushRules.js.map +1 -1
  8. package/lib/@types/another-json.d.js.map +1 -1
  9. package/lib/@types/auth.d.ts +72 -1
  10. package/lib/@types/auth.d.ts.map +1 -1
  11. package/lib/@types/auth.js +57 -54
  12. package/lib/@types/auth.js.map +1 -1
  13. package/lib/@types/beacon.js +100 -100
  14. package/lib/@types/beacon.js.map +1 -1
  15. package/lib/@types/common.js.map +1 -1
  16. package/lib/@types/crypto.js.map +1 -1
  17. package/lib/@types/event.d.ts +59 -0
  18. package/lib/@types/event.d.ts.map +1 -1
  19. package/lib/@types/event.js +105 -102
  20. package/lib/@types/event.js.map +1 -1
  21. package/lib/@types/events.js.map +1 -1
  22. package/lib/@types/extensible_events.js +53 -53
  23. package/lib/@types/extensible_events.js.map +1 -1
  24. package/lib/@types/local_notifications.js.map +1 -1
  25. package/lib/@types/location.js +41 -41
  26. package/lib/@types/location.js.map +1 -1
  27. package/lib/@types/matrix-sdk-crypto-wasm.d.js.map +1 -1
  28. package/lib/@types/media.js.map +1 -1
  29. package/lib/@types/membership.js +39 -39
  30. package/lib/@types/membership.js.map +1 -1
  31. package/lib/@types/partials.js +25 -25
  32. package/lib/@types/partials.js.map +1 -1
  33. package/lib/@types/polls.js +46 -46
  34. package/lib/@types/polls.js.map +1 -1
  35. package/lib/@types/read_receipts.js +14 -14
  36. package/lib/@types/read_receipts.js.map +1 -1
  37. package/lib/@types/registration.js.map +1 -1
  38. package/lib/@types/search.js +14 -14
  39. package/lib/@types/search.js.map +1 -1
  40. package/lib/@types/signed.js.map +1 -1
  41. package/lib/@types/spaces.js.map +1 -1
  42. package/lib/@types/state_events.js.map +1 -1
  43. package/lib/@types/synapse.js.map +1 -1
  44. package/lib/@types/sync.js +18 -18
  45. package/lib/@types/sync.js.map +1 -1
  46. package/lib/@types/threepids.js +14 -14
  47. package/lib/@types/threepids.js.map +1 -1
  48. package/lib/@types/topic.js +47 -47
  49. package/lib/@types/topic.js.map +1 -1
  50. package/lib/@types/uia.js.map +1 -1
  51. package/lib/NamespacedValue.js +20 -20
  52. package/lib/NamespacedValue.js.map +1 -1
  53. package/lib/ReEmitter.js +16 -16
  54. package/lib/ReEmitter.js.map +1 -1
  55. package/lib/base64.js +32 -32
  56. package/lib/base64.js.map +1 -1
  57. package/lib/briij.d.ts +4 -0
  58. package/lib/briij.d.ts.map +1 -1
  59. package/lib/briij.js +4 -0
  60. package/lib/briij.js.map +1 -1
  61. package/lib/client.d.ts +48 -1
  62. package/lib/client.d.ts.map +1 -1
  63. package/lib/client.js +262 -108
  64. package/lib/client.js.map +1 -1
  65. package/lib/common-crypto/key-passphrase.js +19 -19
  66. package/lib/common-crypto/key-passphrase.js.map +1 -1
  67. package/lib/content-helpers.js +57 -57
  68. package/lib/content-helpers.js.map +1 -1
  69. package/lib/content-repo.js +36 -36
  70. package/lib/content-repo.js.map +1 -1
  71. package/lib/crypto/store/base.js +69 -69
  72. package/lib/crypto/store/base.js.map +1 -1
  73. package/lib/crypto/store/indexeddb-crypto-store-backend.js +58 -58
  74. package/lib/crypto/store/indexeddb-crypto-store-backend.js.map +1 -1
  75. package/lib/crypto/store/indexeddb-crypto-store.js +193 -193
  76. package/lib/crypto/store/indexeddb-crypto-store.js.map +1 -1
  77. package/lib/crypto/store/localStorage-crypto-store.js +72 -72
  78. package/lib/crypto/store/localStorage-crypto-store.js.map +1 -1
  79. package/lib/crypto/store/memory-crypto-store.js +74 -74
  80. package/lib/crypto/store/memory-crypto-store.js.map +1 -1
  81. package/lib/crypto-api/CryptoEventHandlerMap.js.map +1 -1
  82. package/lib/crypto-api/key-passphrase.js +22 -22
  83. package/lib/crypto-api/key-passphrase.js.map +1 -1
  84. package/lib/crypto-api/keybackup.js.map +1 -1
  85. package/lib/crypto-api/recovery-key.js +20 -20
  86. package/lib/crypto-api/recovery-key.js.map +1 -1
  87. package/lib/digest.js +21 -21
  88. package/lib/digest.js.map +1 -1
  89. package/lib/extensible_events_v1/ExtensibleEvent.js +39 -39
  90. package/lib/extensible_events_v1/ExtensibleEvent.js.map +1 -1
  91. package/lib/extensible_events_v1/InvalidEventError.js +16 -16
  92. package/lib/extensible_events_v1/InvalidEventError.js.map +1 -1
  93. package/lib/extensible_events_v1/MessageEvent.js +39 -39
  94. package/lib/extensible_events_v1/MessageEvent.js.map +1 -1
  95. package/lib/extensible_events_v1/PollEndEvent.js +29 -29
  96. package/lib/extensible_events_v1/PollEndEvent.js.map +1 -1
  97. package/lib/extensible_events_v1/PollResponseEvent.js +39 -39
  98. package/lib/extensible_events_v1/PollResponseEvent.js.map +1 -1
  99. package/lib/extensible_events_v1/PollStartEvent.js +52 -52
  100. package/lib/extensible_events_v1/PollStartEvent.js.map +1 -1
  101. package/lib/extensible_events_v1/utilities.js +22 -22
  102. package/lib/extensible_events_v1/utilities.js.map +1 -1
  103. package/lib/feature.js +16 -16
  104. package/lib/feature.js.map +1 -1
  105. package/lib/http-api/method.js +14 -14
  106. package/lib/http-api/method.js.map +1 -1
  107. package/lib/http-api/prefix.js +26 -26
  108. package/lib/http-api/prefix.js.map +1 -1
  109. package/lib/indexeddb-helpers.js +21 -21
  110. package/lib/indexeddb-helpers.js.map +1 -1
  111. package/lib/indexeddb-worker.js +18 -18
  112. package/lib/indexeddb-worker.js.map +1 -1
  113. package/lib/matrixrtc/IKeyTransport.js +17 -17
  114. package/lib/matrixrtc/IKeyTransport.js.map +1 -1
  115. package/lib/matrixrtc/IMembershipManager.js +27 -27
  116. package/lib/matrixrtc/IMembershipManager.js.map +1 -1
  117. package/lib/matrixrtc/LivekitTransport.js +19 -19
  118. package/lib/matrixrtc/LivekitTransport.js.map +1 -1
  119. package/lib/matrixrtc/index.js +14 -14
  120. package/lib/matrixrtc/index.js.map +1 -1
  121. package/lib/matrixrtc/utils.js +27 -27
  122. package/lib/matrixrtc/utils.js.map +1 -1
  123. package/lib/models/ToDeviceMessage.js.map +1 -1
  124. package/lib/models/device.js +24 -24
  125. package/lib/models/device.js.map +1 -1
  126. package/lib/models/event-status.js +17 -17
  127. package/lib/models/event-status.js.map +1 -1
  128. package/lib/models/invites-ignorer-types.js +25 -25
  129. package/lib/models/invites-ignorer-types.js.map +1 -1
  130. package/lib/models/profile-keys.js +26 -26
  131. package/lib/models/profile-keys.js.map +1 -1
  132. package/lib/models/room-summary.js +26 -26
  133. package/lib/models/room-summary.js.map +1 -1
  134. package/lib/models/search-result.js +22 -22
  135. package/lib/models/search-result.js.map +1 -1
  136. package/lib/models/typed-event-emitter.js +122 -122
  137. package/lib/models/typed-event-emitter.js.map +1 -1
  138. package/lib/oidc/authorize.js +76 -76
  139. package/lib/oidc/authorize.js.map +1 -1
  140. package/lib/oidc/error.js +17 -17
  141. package/lib/oidc/error.js.map +1 -1
  142. package/lib/oidc/index.js +17 -17
  143. package/lib/oidc/index.js.map +1 -1
  144. package/lib/oidc/register.js +41 -41
  145. package/lib/oidc/register.js.map +1 -1
  146. package/lib/oidc/tokenRefresher.js +51 -51
  147. package/lib/oidc/tokenRefresher.js.map +1 -1
  148. package/lib/oidc/validate.js +59 -59
  149. package/lib/oidc/validate.js.map +1 -1
  150. package/lib/randomstring.js +35 -35
  151. package/lib/randomstring.js.map +1 -1
  152. package/lib/realtime-callbacks.js +39 -39
  153. package/lib/realtime-callbacks.js.map +1 -1
  154. package/lib/receipt-accumulator.js +44 -44
  155. package/lib/receipt-accumulator.js.map +1 -1
  156. package/lib/rendezvous/RendezvousChannel.js.map +1 -1
  157. package/lib/rendezvous/RendezvousCode.js.map +1 -1
  158. package/lib/rendezvous/RendezvousError.js +14 -14
  159. package/lib/rendezvous/RendezvousError.js.map +1 -1
  160. package/lib/rendezvous/RendezvousFailureReason.js +14 -14
  161. package/lib/rendezvous/RendezvousFailureReason.js.map +1 -1
  162. package/lib/rendezvous/RendezvousIntent.js +14 -14
  163. package/lib/rendezvous/RendezvousIntent.js.map +1 -1
  164. package/lib/rendezvous/RendezvousTransport.js.map +1 -1
  165. package/lib/rendezvous/channels/MSC4108SecureChannel.js +63 -63
  166. package/lib/rendezvous/channels/MSC4108SecureChannel.js.map +1 -1
  167. package/lib/rendezvous/channels/index.js +14 -14
  168. package/lib/rendezvous/channels/index.js.map +1 -1
  169. package/lib/rendezvous/index.js +14 -14
  170. package/lib/rendezvous/index.js.map +1 -1
  171. package/lib/rendezvous/transports/index.js +14 -14
  172. package/lib/rendezvous/transports/index.js.map +1 -1
  173. package/lib/rust-crypto/CrossSigningIdentity.js +29 -29
  174. package/lib/rust-crypto/CrossSigningIdentity.js.map +1 -1
  175. package/lib/rust-crypto/OutgoingRequestsManager.js +37 -37
  176. package/lib/rust-crypto/OutgoingRequestsManager.js.map +1 -1
  177. package/lib/rust-crypto/device-converter.js +30 -30
  178. package/lib/rust-crypto/device-converter.js.map +1 -1
  179. package/lib/rust-crypto/secret-storage.js +30 -30
  180. package/lib/rust-crypto/secret-storage.js.map +1 -1
  181. package/lib/service-types.js +14 -14
  182. package/lib/service-types.js.map +1 -1
  183. package/lib/store/local-storage-events-emitter.js +21 -21
  184. package/lib/store/local-storage-events-emitter.js.map +1 -1
  185. package/lib/sync-accumulator.js +50 -50
  186. package/lib/sync-accumulator.js.map +1 -1
  187. package/lib/thread-utils.js +20 -20
  188. package/lib/thread-utils.js.map +1 -1
  189. package/lib/types.js +34 -34
  190. package/lib/types.js.map +1 -1
  191. package/lib/utils/decryptAESSecretStorageItem.js +22 -22
  192. package/lib/utils/decryptAESSecretStorageItem.js.map +1 -1
  193. package/lib/utils/encryptAESSecretStorageItem.js +26 -26
  194. package/lib/utils/encryptAESSecretStorageItem.js.map +1 -1
  195. package/lib/utils/internal/deriveKeys.js +21 -21
  196. package/lib/utils/internal/deriveKeys.js.map +1 -1
  197. package/lib/utils/roomVersion.js +26 -26
  198. package/lib/utils/roomVersion.js.map +1 -1
  199. package/lib/version-support.js +26 -26
  200. package/lib/version-support.js.map +1 -1
  201. package/lib/wallet-recovery.d.ts +24 -0
  202. package/lib/wallet-recovery.d.ts.map +1 -0
  203. package/lib/wallet-recovery.js +232 -0
  204. package/lib/wallet-recovery.js.map +1 -0
  205. package/lib/webrtc/audioContext.js +24 -24
  206. package/lib/webrtc/audioContext.js.map +1 -1
  207. package/lib/webrtc/callEventTypes.js.map +1 -1
  208. package/lib/webrtc/stats/callFeedStatsReporter.js +14 -14
  209. package/lib/webrtc/stats/callFeedStatsReporter.js.map +1 -1
  210. package/lib/webrtc/stats/callStatsReportGatherer.js +14 -14
  211. package/lib/webrtc/stats/callStatsReportGatherer.js.map +1 -1
  212. package/lib/webrtc/stats/callStatsReportSummary.js.map +1 -1
  213. package/lib/webrtc/stats/connectionStats.js +14 -14
  214. package/lib/webrtc/stats/connectionStats.js.map +1 -1
  215. package/lib/webrtc/stats/connectionStatsBuilder.js +14 -14
  216. package/lib/webrtc/stats/connectionStatsBuilder.js.map +1 -1
  217. package/lib/webrtc/stats/connectionStatsReportBuilder.js +14 -14
  218. package/lib/webrtc/stats/connectionStatsReportBuilder.js.map +1 -1
  219. package/lib/webrtc/stats/groupCallStats.js +14 -14
  220. package/lib/webrtc/stats/groupCallStats.js.map +1 -1
  221. package/lib/webrtc/stats/media/mediaSsrcHandler.js +14 -14
  222. package/lib/webrtc/stats/media/mediaSsrcHandler.js.map +1 -1
  223. package/lib/webrtc/stats/media/mediaTrackHandler.js +14 -14
  224. package/lib/webrtc/stats/media/mediaTrackHandler.js.map +1 -1
  225. package/lib/webrtc/stats/media/mediaTrackStats.js +27 -27
  226. package/lib/webrtc/stats/media/mediaTrackStats.js.map +1 -1
  227. package/lib/webrtc/stats/media/mediaTrackStatsHandler.js +20 -20
  228. package/lib/webrtc/stats/media/mediaTrackStatsHandler.js.map +1 -1
  229. package/lib/webrtc/stats/statsReport.js +14 -14
  230. package/lib/webrtc/stats/statsReport.js.map +1 -1
  231. package/lib/webrtc/stats/statsReportEmitter.js +14 -14
  232. package/lib/webrtc/stats/statsReportEmitter.js.map +1 -1
  233. package/lib/webrtc/stats/trackStatsBuilder.js +4 -4
  234. package/lib/webrtc/stats/trackStatsBuilder.js.map +1 -1
  235. package/lib/webrtc/stats/transportStats.js.map +1 -1
  236. package/lib/webrtc/stats/transportStatsBuilder.js.map +1 -1
  237. package/lib/webrtc/stats/valueFormatter.js +11 -11
  238. package/lib/webrtc/stats/valueFormatter.js.map +1 -1
  239. package/lib/xrpl/identity.d.ts +28 -0
  240. package/lib/xrpl/identity.d.ts.map +1 -0
  241. package/lib/xrpl/identity.js +213 -0
  242. package/lib/xrpl/identity.js.map +1 -0
  243. package/lib/xrpl/trust.d.ts +8 -0
  244. package/lib/xrpl/trust.d.ts.map +1 -0
  245. package/lib/xrpl/trust.js +61 -0
  246. package/lib/xrpl/trust.js.map +1 -0
  247. package/lib/xrpl/verification.d.ts +26 -0
  248. package/lib/xrpl/verification.d.ts.map +1 -0
  249. package/lib/xrpl/verification.js +295 -0
  250. package/lib/xrpl/verification.js.map +1 -0
  251. package/package.json +130 -129
  252. package/src/@types/AESEncryptedSecretStoragePayload.ts +29 -29
  253. package/src/@types/IIdentityServerProvider.ts +24 -24
  254. package/src/@types/PushRules.ts +208 -208
  255. package/src/@types/another-json.d.ts +19 -19
  256. package/src/@types/auth.ts +340 -258
  257. package/src/@types/beacon.ts +140 -140
  258. package/src/@types/common.ts +24 -24
  259. package/src/@types/crypto.ts +71 -71
  260. package/src/@types/event.ts +508 -449
  261. package/src/@types/events.ts +119 -119
  262. package/src/@types/extensible_events.ts +147 -147
  263. package/src/@types/local_notifications.ts +19 -19
  264. package/src/@types/location.ts +92 -92
  265. package/src/@types/matrix-sdk-crypto-wasm.d.ts +39 -39
  266. package/src/@types/media.ts +245 -245
  267. package/src/@types/membership.ts +57 -57
  268. package/src/@types/partials.ts +103 -103
  269. package/src/@types/polls.ts +120 -120
  270. package/src/@types/read_receipts.ts +61 -61
  271. package/src/@types/registration.ts +102 -102
  272. package/src/@types/search.ts +119 -119
  273. package/src/@types/signed.ts +25 -25
  274. package/src/@types/spaces.ts +37 -37
  275. package/src/@types/state_events.ts +153 -153
  276. package/src/@types/synapse.ts +40 -40
  277. package/src/@types/sync.ts +27 -27
  278. package/src/@types/threepids.ts +29 -29
  279. package/src/@types/topic.ts +69 -69
  280. package/src/@types/uia.ts +24 -24
  281. package/src/NamespacedValue.ts +121 -121
  282. package/src/ReEmitter.ts +93 -93
  283. package/src/base64.ts +86 -86
  284. package/src/briij.ts +4 -0
  285. package/src/client.ts +183 -10
  286. package/src/common-crypto/README.md +4 -4
  287. package/src/common-crypto/key-passphrase.ts +43 -43
  288. package/src/content-helpers.ts +298 -298
  289. package/src/content-repo.ts +122 -122
  290. package/src/crypto/store/base.ts +388 -388
  291. package/src/crypto/store/indexeddb-crypto-store-backend.ts +655 -655
  292. package/src/crypto/store/indexeddb-crypto-store.ts +555 -555
  293. package/src/crypto/store/localStorage-crypto-store.ts +409 -409
  294. package/src/crypto/store/memory-crypto-store.ts +326 -326
  295. package/src/crypto-api/CryptoEventHandlerMap.ts +42 -42
  296. package/src/crypto-api/key-passphrase.ts +58 -58
  297. package/src/crypto-api/keybackup.ts +114 -114
  298. package/src/crypto-api/recovery-key.ts +69 -69
  299. package/src/digest.ts +34 -34
  300. package/src/extensible_events_v1/ExtensibleEvent.ts +58 -58
  301. package/src/extensible_events_v1/InvalidEventError.ts +24 -24
  302. package/src/extensible_events_v1/MessageEvent.ts +143 -143
  303. package/src/extensible_events_v1/PollEndEvent.ts +97 -97
  304. package/src/extensible_events_v1/PollResponseEvent.ts +148 -148
  305. package/src/extensible_events_v1/PollStartEvent.ts +207 -207
  306. package/src/extensible_events_v1/utilities.ts +35 -35
  307. package/src/feature.ts +88 -88
  308. package/src/http-api/method.ts +25 -25
  309. package/src/http-api/prefix.ts +48 -48
  310. package/src/indexeddb-helpers.ts +50 -50
  311. package/src/indexeddb-worker.ts +24 -24
  312. package/src/matrixrtc/IKeyTransport.ts +63 -63
  313. package/src/matrixrtc/IMembershipManager.ts +120 -120
  314. package/src/matrixrtc/LivekitTransport.ts +46 -46
  315. package/src/matrixrtc/index.ts +24 -24
  316. package/src/matrixrtc/utils.ts +71 -71
  317. package/src/models/ToDeviceMessage.ts +38 -38
  318. package/src/models/device.ts +85 -85
  319. package/src/models/event-status.ts +39 -39
  320. package/src/models/invites-ignorer-types.ts +58 -58
  321. package/src/models/profile-keys.ts +33 -33
  322. package/src/models/room-summary.ts +78 -78
  323. package/src/models/search-result.ts +57 -57
  324. package/src/models/typed-event-emitter.ts +246 -246
  325. package/src/oidc/authorize.ts +279 -279
  326. package/src/oidc/error.ts +33 -33
  327. package/src/oidc/index.ts +33 -33
  328. package/src/oidc/register.ts +163 -163
  329. package/src/oidc/tokenRefresher.ts +184 -184
  330. package/src/oidc/validate.ts +265 -265
  331. package/src/randomstring.ts +103 -103
  332. package/src/realtime-callbacks.ts +191 -191
  333. package/src/receipt-accumulator.ts +189 -189
  334. package/src/rendezvous/RendezvousChannel.ts +48 -48
  335. package/src/rendezvous/RendezvousCode.ts +25 -25
  336. package/src/rendezvous/RendezvousError.ts +26 -26
  337. package/src/rendezvous/RendezvousFailureReason.ts +49 -49
  338. package/src/rendezvous/RendezvousIntent.ts +20 -20
  339. package/src/rendezvous/RendezvousTransport.ts +58 -58
  340. package/src/rendezvous/channels/MSC4108SecureChannel.ts +270 -270
  341. package/src/rendezvous/channels/index.ts +17 -17
  342. package/src/rendezvous/index.ts +25 -25
  343. package/src/rendezvous/transports/index.ts +17 -17
  344. package/src/rust-crypto/CrossSigningIdentity.ts +195 -195
  345. package/src/rust-crypto/OutgoingRequestsManager.ts +170 -170
  346. package/src/rust-crypto/device-converter.ts +128 -128
  347. package/src/rust-crypto/secret-storage.ts +60 -60
  348. package/src/service-types.ts +20 -20
  349. package/src/store/local-storage-events-emitter.ts +46 -46
  350. package/src/sync-accumulator.ts +779 -779
  351. package/src/thread-utils.ts +31 -31
  352. package/src/types.ts +59 -59
  353. package/src/utils/decryptAESSecretStorageItem.ts +54 -54
  354. package/src/utils/encryptAESSecretStorageItem.ts +73 -73
  355. package/src/utils/internal/deriveKeys.ts +63 -63
  356. package/src/utils/roomVersion.ts +35 -35
  357. package/src/version-support.ts +50 -50
  358. package/src/wallet-recovery.ts +252 -0
  359. package/src/webrtc/audioContext.ts +44 -44
  360. package/src/webrtc/callEventTypes.ts +101 -101
  361. package/src/webrtc/stats/callFeedStatsReporter.ts +91 -91
  362. package/src/webrtc/stats/callStatsReportGatherer.ts +219 -219
  363. package/src/webrtc/stats/callStatsReportSummary.ts +30 -30
  364. package/src/webrtc/stats/connectionStats.ts +47 -47
  365. package/src/webrtc/stats/connectionStatsBuilder.ts +28 -28
  366. package/src/webrtc/stats/connectionStatsReportBuilder.ts +140 -140
  367. package/src/webrtc/stats/groupCallStats.ts +93 -93
  368. package/src/webrtc/stats/media/mediaSsrcHandler.ts +57 -57
  369. package/src/webrtc/stats/media/mediaTrackHandler.ts +70 -70
  370. package/src/webrtc/stats/media/mediaTrackStats.ts +176 -176
  371. package/src/webrtc/stats/media/mediaTrackStatsHandler.ts +90 -90
  372. package/src/webrtc/stats/statsReport.ts +133 -133
  373. package/src/webrtc/stats/statsReportEmitter.ts +49 -49
  374. package/src/webrtc/stats/trackStatsBuilder.ts +207 -207
  375. package/src/webrtc/stats/transportStats.ts +26 -26
  376. package/src/webrtc/stats/transportStatsBuilder.ts +48 -48
  377. package/src/webrtc/stats/valueFormatter.ts +27 -27
  378. package/src/xrpl/identity.ts +245 -0
  379. package/src/xrpl/trust.ts +64 -0
  380. package/src/xrpl/verification.ts +284 -0
@@ -1,655 +1,655 @@
1
- /*
2
- Copyright 2017 - 2021 The Matrix.org Foundation C.I.C.
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- */
16
-
17
- import { type Logger, logger } from "../../logger.ts";
18
- import {
19
- type CryptoStore,
20
- type IDeviceData,
21
- type ISession,
22
- type SessionExtended,
23
- type ISessionInfo,
24
- type IWithheld,
25
- MigrationState,
26
- type Mode,
27
- type SecretStorePrivateKeys,
28
- SESSION_BATCH_SIZE,
29
- ACCOUNT_OBJECT_KEY_MIGRATION_STATE,
30
- type InboundGroupSessionData,
31
- type IRoomEncryption,
32
- } from "./base.ts";
33
- import { IndexedDBCryptoStore } from "./indexeddb-crypto-store.ts";
34
- import { type CrossSigningKeyInfo } from "../../crypto-api/index.ts";
35
-
36
- const PROFILE_TRANSACTIONS = false;
37
-
38
- /**
39
- * Implementation of a CryptoStore which is backed by an existing
40
- * IndexedDB connection. Generally you want IndexedDBCryptoStore
41
- * which connects to the database and defers to one of these.
42
- *
43
- * @internal
44
- */
45
- export class Backend implements CryptoStore {
46
- private nextTxnId = 0;
47
-
48
- /**
49
- */
50
- public constructor(private db: IDBDatabase) {
51
- // make sure we close the db on `onversionchange` - otherwise
52
- // attempts to delete the database will block (and subsequent
53
- // attempts to re-create it will also block).
54
- db.onversionchange = (): void => {
55
- logger.log(`versionchange for indexeddb ${this.db.name}: closing`);
56
- db.close();
57
- };
58
- }
59
-
60
- public async containsData(): Promise<boolean> {
61
- throw Error("Not implemented for Backend");
62
- }
63
-
64
- public async startup(): Promise<CryptoStore> {
65
- // No work to do, as the startup is done by the caller (e.g IndexedDBCryptoStore)
66
- // by passing us a ready IDBDatabase instance
67
- return this;
68
- }
69
-
70
- public async deleteAllData(): Promise<void> {
71
- throw Error("This is not implemented, call IDBFactory::deleteDatabase(dbName) instead.");
72
- }
73
-
74
- /**
75
- * Get data on how much of the libolm to Rust Crypto migration has been done.
76
- *
77
- * Implementation of {@link CryptoStore.getMigrationState}.
78
- */
79
- public async getMigrationState(): Promise<MigrationState> {
80
- let migrationState = MigrationState.NOT_STARTED;
81
- await this.doTxn("readonly", [IndexedDBCryptoStore.STORE_ACCOUNT], (txn) => {
82
- const objectStore = txn.objectStore(IndexedDBCryptoStore.STORE_ACCOUNT);
83
- const getReq = objectStore.get(ACCOUNT_OBJECT_KEY_MIGRATION_STATE);
84
- getReq.onsuccess = (): void => {
85
- migrationState = getReq.result ?? MigrationState.NOT_STARTED;
86
- };
87
- });
88
- return migrationState;
89
- }
90
-
91
- /**
92
- * Set data on how much of the libolm to Rust Crypto migration has been done.
93
- *
94
- * Implementation of {@link CryptoStore.setMigrationState}.
95
- */
96
- public async setMigrationState(migrationState: MigrationState): Promise<void> {
97
- await this.doTxn("readwrite", [IndexedDBCryptoStore.STORE_ACCOUNT], (txn) => {
98
- const objectStore = txn.objectStore(IndexedDBCryptoStore.STORE_ACCOUNT);
99
- objectStore.put(migrationState, ACCOUNT_OBJECT_KEY_MIGRATION_STATE);
100
- });
101
- }
102
-
103
- // Olm Account
104
-
105
- public getAccount(txn: IDBTransaction, func: (accountPickle: string | null) => void): void {
106
- const objectStore = txn.objectStore("account");
107
- const getReq = objectStore.get("-");
108
- getReq.onsuccess = function (): void {
109
- try {
110
- func(getReq.result || null);
111
- } catch (e) {
112
- abortWithException(txn, <Error>e);
113
- }
114
- };
115
- }
116
-
117
- public storeAccount(txn: IDBTransaction, accountPickle: string): void {
118
- const objectStore = txn.objectStore("account");
119
- objectStore.put(accountPickle, "-");
120
- }
121
-
122
- public getCrossSigningKeys(
123
- txn: IDBTransaction,
124
- func: (keys: Record<string, CrossSigningKeyInfo> | null) => void,
125
- ): void {
126
- const objectStore = txn.objectStore("account");
127
- const getReq = objectStore.get("crossSigningKeys");
128
- getReq.onsuccess = function (): void {
129
- try {
130
- func(getReq.result || null);
131
- } catch (e) {
132
- abortWithException(txn, <Error>e);
133
- }
134
- };
135
- }
136
-
137
- public getSecretStorePrivateKey<K extends keyof SecretStorePrivateKeys>(
138
- txn: IDBTransaction,
139
- func: (key: SecretStorePrivateKeys[K] | null) => void,
140
- type: K,
141
- ): void {
142
- const objectStore = txn.objectStore("account");
143
- const getReq = objectStore.get(`ssss_cache:${type}`);
144
- getReq.onsuccess = function (): void {
145
- try {
146
- func(getReq.result || null);
147
- } catch (e) {
148
- abortWithException(txn, <Error>e);
149
- }
150
- };
151
- }
152
-
153
- public storeSecretStorePrivateKey<K extends keyof SecretStorePrivateKeys>(
154
- txn: IDBTransaction,
155
- type: K,
156
- key: SecretStorePrivateKeys[K],
157
- ): void {
158
- const objectStore = txn.objectStore("account");
159
- objectStore.put(key, `ssss_cache:${type}`);
160
- }
161
-
162
- // Olm Sessions
163
-
164
- public countEndToEndSessions(txn: IDBTransaction, func: (count: number) => void): void {
165
- const objectStore = txn.objectStore("sessions");
166
- const countReq = objectStore.count();
167
- countReq.onsuccess = function (): void {
168
- try {
169
- func(countReq.result);
170
- } catch (e) {
171
- abortWithException(txn, <Error>e);
172
- }
173
- };
174
- }
175
-
176
- public getEndToEndSessions(
177
- deviceKey: string,
178
- txn: IDBTransaction,
179
- func: (sessions: { [sessionId: string]: ISessionInfo }) => void,
180
- ): void {
181
- const objectStore = txn.objectStore("sessions");
182
- const idx = objectStore.index("deviceKey");
183
- const getReq = idx.openCursor(deviceKey);
184
- const results: Parameters<Parameters<Backend["getEndToEndSessions"]>[2]>[0] = {};
185
- getReq.onsuccess = function (): void {
186
- const cursor = getReq.result;
187
- if (cursor) {
188
- results[cursor.value.sessionId] = {
189
- session: cursor.value.session,
190
- lastReceivedMessageTs: cursor.value.lastReceivedMessageTs,
191
- };
192
- cursor.continue();
193
- } else {
194
- try {
195
- func(results);
196
- } catch (e) {
197
- abortWithException(txn, <Error>e);
198
- }
199
- }
200
- };
201
- }
202
-
203
- public getEndToEndSession(
204
- deviceKey: string,
205
- sessionId: string,
206
- txn: IDBTransaction,
207
- func: (session: ISessionInfo | null) => void,
208
- ): void {
209
- const objectStore = txn.objectStore("sessions");
210
- const getReq = objectStore.get([deviceKey, sessionId]);
211
- getReq.onsuccess = function (): void {
212
- try {
213
- if (getReq.result) {
214
- func({
215
- session: getReq.result.session,
216
- lastReceivedMessageTs: getReq.result.lastReceivedMessageTs,
217
- });
218
- } else {
219
- func(null);
220
- }
221
- } catch (e) {
222
- abortWithException(txn, <Error>e);
223
- }
224
- };
225
- }
226
-
227
- public storeEndToEndSession(
228
- deviceKey: string,
229
- sessionId: string,
230
- sessionInfo: ISessionInfo,
231
- txn: IDBTransaction,
232
- ): void {
233
- const objectStore = txn.objectStore("sessions");
234
- objectStore.put({
235
- deviceKey,
236
- sessionId,
237
- session: sessionInfo.session,
238
- lastReceivedMessageTs: sessionInfo.lastReceivedMessageTs,
239
- });
240
- }
241
-
242
- /**
243
- * Fetch a batch of Olm sessions from the database.
244
- *
245
- * Implementation of {@link CryptoStore.getEndToEndSessionsBatch}.
246
- */
247
- public async getEndToEndSessionsBatch(): Promise<null | ISessionInfo[]> {
248
- const result: ISessionInfo[] = [];
249
- await this.doTxn("readonly", [IndexedDBCryptoStore.STORE_SESSIONS], (txn) => {
250
- const objectStore = txn.objectStore(IndexedDBCryptoStore.STORE_SESSIONS);
251
- const getReq = objectStore.openCursor();
252
- getReq.onsuccess = function (): void {
253
- try {
254
- const cursor = getReq.result;
255
- if (cursor) {
256
- result.push(cursor.value);
257
- if (result.length < SESSION_BATCH_SIZE) {
258
- cursor.continue();
259
- }
260
- }
261
- } catch (e) {
262
- abortWithException(txn, <Error>e);
263
- }
264
- };
265
- });
266
-
267
- if (result.length === 0) {
268
- // No sessions left.
269
- return null;
270
- }
271
-
272
- return result;
273
- }
274
-
275
- /**
276
- * Delete a batch of Olm sessions from the database.
277
- *
278
- * Implementation of {@link CryptoStore.deleteEndToEndSessionsBatch}.
279
- *
280
- * @internal
281
- */
282
- public async deleteEndToEndSessionsBatch(sessions: { deviceKey: string; sessionId: string }[]): Promise<void> {
283
- await this.doTxn("readwrite", [IndexedDBCryptoStore.STORE_SESSIONS], async (txn) => {
284
- try {
285
- const objectStore = txn.objectStore(IndexedDBCryptoStore.STORE_SESSIONS);
286
- for (const { deviceKey, sessionId } of sessions) {
287
- const req = objectStore.delete([deviceKey, sessionId]);
288
- await new Promise((resolve) => {
289
- req.onsuccess = resolve;
290
- });
291
- }
292
- } catch (e) {
293
- abortWithException(txn, <Error>e);
294
- }
295
- });
296
- }
297
-
298
- // Inbound group sessions
299
-
300
- public getEndToEndInboundGroupSession(
301
- senderCurve25519Key: string,
302
- sessionId: string,
303
- txn: IDBTransaction,
304
- func: (groupSession: InboundGroupSessionData | null, groupSessionWithheld: IWithheld | null) => void,
305
- ): void {
306
- let session: InboundGroupSessionData | null | boolean = false;
307
- let withheld: IWithheld | null | boolean = false;
308
- const objectStore = txn.objectStore("inbound_group_sessions");
309
- const getReq = objectStore.get([senderCurve25519Key, sessionId]);
310
- getReq.onsuccess = function (): void {
311
- try {
312
- if (getReq.result) {
313
- session = getReq.result.session;
314
- } else {
315
- session = null;
316
- }
317
- if (withheld !== false) {
318
- func(session as InboundGroupSessionData, withheld as IWithheld);
319
- }
320
- } catch (e) {
321
- abortWithException(txn, <Error>e);
322
- }
323
- };
324
-
325
- const withheldObjectStore = txn.objectStore("inbound_group_sessions_withheld");
326
- const withheldGetReq = withheldObjectStore.get([senderCurve25519Key, sessionId]);
327
- withheldGetReq.onsuccess = function (): void {
328
- try {
329
- if (withheldGetReq.result) {
330
- withheld = withheldGetReq.result.session;
331
- } else {
332
- withheld = null;
333
- }
334
- if (session !== false) {
335
- func(session as InboundGroupSessionData, withheld as IWithheld);
336
- }
337
- } catch (e) {
338
- abortWithException(txn, <Error>e);
339
- }
340
- };
341
- }
342
-
343
- public storeEndToEndInboundGroupSession(
344
- senderCurve25519Key: string,
345
- sessionId: string,
346
- sessionData: InboundGroupSessionData,
347
- txn: IDBTransaction,
348
- ): void {
349
- const objectStore = txn.objectStore("inbound_group_sessions");
350
- objectStore.put({
351
- senderCurve25519Key,
352
- sessionId,
353
- session: sessionData,
354
- });
355
- }
356
-
357
- /**
358
- * Count the number of Megolm sessions in the database.
359
- *
360
- * Implementation of {@link CryptoStore.countEndToEndInboundGroupSessions}.
361
- *
362
- * @internal
363
- */
364
- public async countEndToEndInboundGroupSessions(): Promise<number> {
365
- let result = 0;
366
- await this.doTxn("readonly", [IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS], (txn) => {
367
- const sessionStore = txn.objectStore(IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS);
368
- const countReq = sessionStore.count();
369
- countReq.onsuccess = (): void => {
370
- result = countReq.result;
371
- };
372
- });
373
- return result;
374
- }
375
-
376
- /**
377
- * Fetch a batch of Megolm sessions from the database.
378
- *
379
- * Implementation of {@link CryptoStore.getEndToEndInboundGroupSessionsBatch}.
380
- */
381
- public async getEndToEndInboundGroupSessionsBatch(): Promise<null | SessionExtended[]> {
382
- const result: SessionExtended[] = [];
383
- await this.doTxn(
384
- "readonly",
385
- [IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS, IndexedDBCryptoStore.STORE_BACKUP],
386
- (txn) => {
387
- const sessionStore = txn.objectStore(IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS);
388
- const backupStore = txn.objectStore(IndexedDBCryptoStore.STORE_BACKUP);
389
-
390
- const getReq = sessionStore.openCursor();
391
- getReq.onsuccess = function (): void {
392
- try {
393
- const cursor = getReq.result;
394
- if (cursor) {
395
- const backupGetReq = backupStore.get(cursor.key);
396
- backupGetReq.onsuccess = (): void => {
397
- result.push({
398
- senderKey: cursor.value.senderCurve25519Key,
399
- sessionId: cursor.value.sessionId,
400
- sessionData: cursor.value.session,
401
- needsBackup: backupGetReq.result !== undefined,
402
- });
403
- if (result.length < SESSION_BATCH_SIZE) {
404
- cursor.continue();
405
- }
406
- };
407
- }
408
- } catch (e) {
409
- abortWithException(txn, <Error>e);
410
- }
411
- };
412
- },
413
- );
414
-
415
- if (result.length === 0) {
416
- // No sessions left.
417
- return null;
418
- }
419
-
420
- return result;
421
- }
422
-
423
- /**
424
- * Delete a batch of Megolm sessions from the database.
425
- *
426
- * Implementation of {@link CryptoStore.deleteEndToEndInboundGroupSessionsBatch}.
427
- *
428
- * @internal
429
- */
430
- public async deleteEndToEndInboundGroupSessionsBatch(
431
- sessions: { senderKey: string; sessionId: string }[],
432
- ): Promise<void> {
433
- await this.doTxn("readwrite", [IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS], async (txn) => {
434
- try {
435
- const objectStore = txn.objectStore(IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS);
436
- for (const { senderKey, sessionId } of sessions) {
437
- const req = objectStore.delete([senderKey, sessionId]);
438
- await new Promise((resolve) => {
439
- req.onsuccess = resolve;
440
- });
441
- }
442
- } catch (e) {
443
- abortWithException(txn, <Error>e);
444
- }
445
- });
446
- }
447
-
448
- public getEndToEndDeviceData(txn: IDBTransaction, func: (deviceData: IDeviceData | null) => void): void {
449
- const objectStore = txn.objectStore("device_data");
450
- const getReq = objectStore.get("-");
451
- getReq.onsuccess = function (): void {
452
- try {
453
- func(getReq.result || null);
454
- } catch (e) {
455
- abortWithException(txn, <Error>e);
456
- }
457
- };
458
- }
459
-
460
- public getEndToEndRooms(txn: IDBTransaction, func: (rooms: Record<string, IRoomEncryption>) => void): void {
461
- const rooms: Parameters<Parameters<Backend["getEndToEndRooms"]>[1]>[0] = {};
462
- const objectStore = txn.objectStore("rooms");
463
- const getReq = objectStore.openCursor();
464
- getReq.onsuccess = function (): void {
465
- const cursor = getReq.result;
466
- if (cursor) {
467
- rooms[cursor.key as string] = cursor.value;
468
- cursor.continue();
469
- } else {
470
- try {
471
- func(rooms);
472
- } catch (e) {
473
- abortWithException(txn, <Error>e);
474
- }
475
- }
476
- };
477
- }
478
-
479
- public async markSessionsNeedingBackup(sessions: ISession[], txn?: IDBTransaction): Promise<void> {
480
- if (!txn) {
481
- txn = this.db.transaction("sessions_needing_backup", "readwrite");
482
- }
483
- const objectStore = txn.objectStore("sessions_needing_backup");
484
- await Promise.all(
485
- sessions.map((session) => {
486
- return new Promise((resolve, reject) => {
487
- const req = objectStore.put({
488
- senderCurve25519Key: session.senderKey,
489
- sessionId: session.sessionId,
490
- });
491
- req.onsuccess = resolve;
492
- req.onerror = reject;
493
- });
494
- }),
495
- );
496
- }
497
-
498
- public doTxn<T>(
499
- mode: Mode,
500
- stores: string | string[],
501
- func: (txn: IDBTransaction) => T,
502
- log: Logger = logger,
503
- ): Promise<T> {
504
- let startTime: number;
505
- let description: string;
506
- if (PROFILE_TRANSACTIONS) {
507
- const txnId = this.nextTxnId++;
508
- startTime = Date.now();
509
- description = `${mode} crypto store transaction ${txnId} in ${stores}`;
510
- log.debug(`Starting ${description}`);
511
- }
512
- const txn = this.db.transaction(stores, mode);
513
- const promise = promiseifyTxn(txn);
514
- const result = func(txn);
515
- if (PROFILE_TRANSACTIONS) {
516
- promise.then(
517
- () => {
518
- const elapsedTime = Date.now() - startTime;
519
- log.debug(`Finished ${description}, took ${elapsedTime} ms`);
520
- },
521
- () => {
522
- const elapsedTime = Date.now() - startTime;
523
- log.error(`Failed ${description}, took ${elapsedTime} ms`);
524
- },
525
- );
526
- }
527
- return promise.then(() => {
528
- return result;
529
- });
530
- }
531
- }
532
-
533
- type DbMigration = (db: IDBDatabase) => void;
534
- const DB_MIGRATIONS: DbMigration[] = [
535
- (db): void => {
536
- createDatabase(db);
537
- },
538
- (db): void => {
539
- db.createObjectStore("account");
540
- },
541
- (db): void => {
542
- const sessionsStore = db.createObjectStore("sessions", {
543
- keyPath: ["deviceKey", "sessionId"],
544
- });
545
- sessionsStore.createIndex("deviceKey", "deviceKey");
546
- },
547
- (db): void => {
548
- db.createObjectStore("inbound_group_sessions", {
549
- keyPath: ["senderCurve25519Key", "sessionId"],
550
- });
551
- },
552
- (db): void => {
553
- db.createObjectStore("device_data");
554
- },
555
- (db): void => {
556
- db.createObjectStore("rooms");
557
- },
558
- (db): void => {
559
- db.createObjectStore("sessions_needing_backup", {
560
- keyPath: ["senderCurve25519Key", "sessionId"],
561
- });
562
- },
563
- (db): void => {
564
- db.createObjectStore("inbound_group_sessions_withheld", {
565
- keyPath: ["senderCurve25519Key", "sessionId"],
566
- });
567
- },
568
- (db): void => {
569
- const problemsStore = db.createObjectStore("session_problems", {
570
- keyPath: ["deviceKey", "time"],
571
- });
572
- problemsStore.createIndex("deviceKey", "deviceKey");
573
-
574
- db.createObjectStore("notified_error_devices", {
575
- keyPath: ["userId", "deviceId"],
576
- });
577
- },
578
- (db): void => {
579
- db.createObjectStore("shared_history_inbound_group_sessions", {
580
- keyPath: ["roomId"],
581
- });
582
- },
583
- (db): void => {
584
- db.createObjectStore("parked_shared_history", {
585
- keyPath: ["roomId"],
586
- });
587
- },
588
- // Expand as needed.
589
- ];
590
- export const VERSION = DB_MIGRATIONS.length;
591
-
592
- export function upgradeDatabase(db: IDBDatabase, oldVersion: number): void {
593
- logger.log(`Upgrading IndexedDBCryptoStore from version ${oldVersion}` + ` to ${VERSION}`);
594
- DB_MIGRATIONS.forEach((migration, index) => {
595
- if (oldVersion <= index) migration(db);
596
- });
597
- }
598
-
599
- function createDatabase(db: IDBDatabase): void {
600
- const outgoingRoomKeyRequestsStore = db.createObjectStore("outgoingRoomKeyRequests", { keyPath: "requestId" });
601
-
602
- // we assume that the RoomKeyRequestBody will have room_id and session_id
603
- // properties, to make the index efficient.
604
- outgoingRoomKeyRequestsStore.createIndex("session", ["requestBody.room_id", "requestBody.session_id"]);
605
-
606
- outgoingRoomKeyRequestsStore.createIndex("state", "state");
607
- }
608
-
609
- interface IWrappedIDBTransaction extends IDBTransaction {
610
- _mx_abortexception: Error; // eslint-disable-line camelcase
611
- }
612
-
613
- /*
614
- * Aborts a transaction with a given exception
615
- * The transaction promise will be rejected with this exception.
616
- */
617
- function abortWithException(txn: IDBTransaction, e: Error): void {
618
- // We cheekily stick our exception onto the transaction object here
619
- // We could alternatively make the thing we pass back to the app
620
- // an object containing the transaction and exception.
621
- (txn as IWrappedIDBTransaction)._mx_abortexception = e;
622
- try {
623
- txn.abort();
624
- } catch {
625
- // sometimes we won't be able to abort the transaction
626
- // (ie. if it's aborted or completed)
627
- }
628
- }
629
-
630
- function promiseifyTxn<T>(txn: IDBTransaction): Promise<T | null> {
631
- return new Promise((resolve, reject) => {
632
- txn.oncomplete = (): void => {
633
- if ((txn as IWrappedIDBTransaction)._mx_abortexception !== undefined) {
634
- reject((txn as IWrappedIDBTransaction)._mx_abortexception);
635
- }
636
- resolve(null);
637
- };
638
- txn.onerror = (event): void => {
639
- if ((txn as IWrappedIDBTransaction)._mx_abortexception !== undefined) {
640
- reject((txn as IWrappedIDBTransaction)._mx_abortexception);
641
- } else {
642
- logger.log("Error performing indexeddb txn", event);
643
- reject(txn.error);
644
- }
645
- };
646
- txn.onabort = (event): void => {
647
- if ((txn as IWrappedIDBTransaction)._mx_abortexception !== undefined) {
648
- reject((txn as IWrappedIDBTransaction)._mx_abortexception);
649
- } else {
650
- logger.log("Error performing indexeddb txn", event);
651
- reject(txn.error);
652
- }
653
- };
654
- });
655
- }
1
+ /*
2
+ Copyright 2017 - 2021 The Matrix.org Foundation C.I.C.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import { type Logger, logger } from "../../logger.ts";
18
+ import {
19
+ type CryptoStore,
20
+ type IDeviceData,
21
+ type ISession,
22
+ type SessionExtended,
23
+ type ISessionInfo,
24
+ type IWithheld,
25
+ MigrationState,
26
+ type Mode,
27
+ type SecretStorePrivateKeys,
28
+ SESSION_BATCH_SIZE,
29
+ ACCOUNT_OBJECT_KEY_MIGRATION_STATE,
30
+ type InboundGroupSessionData,
31
+ type IRoomEncryption,
32
+ } from "./base.ts";
33
+ import { IndexedDBCryptoStore } from "./indexeddb-crypto-store.ts";
34
+ import { type CrossSigningKeyInfo } from "../../crypto-api/index.ts";
35
+
36
+ const PROFILE_TRANSACTIONS = false;
37
+
38
+ /**
39
+ * Implementation of a CryptoStore which is backed by an existing
40
+ * IndexedDB connection. Generally you want IndexedDBCryptoStore
41
+ * which connects to the database and defers to one of these.
42
+ *
43
+ * @internal
44
+ */
45
+ export class Backend implements CryptoStore {
46
+ private nextTxnId = 0;
47
+
48
+ /**
49
+ */
50
+ public constructor(private db: IDBDatabase) {
51
+ // make sure we close the db on `onversionchange` - otherwise
52
+ // attempts to delete the database will block (and subsequent
53
+ // attempts to re-create it will also block).
54
+ db.onversionchange = (): void => {
55
+ logger.log(`versionchange for indexeddb ${this.db.name}: closing`);
56
+ db.close();
57
+ };
58
+ }
59
+
60
+ public async containsData(): Promise<boolean> {
61
+ throw Error("Not implemented for Backend");
62
+ }
63
+
64
+ public async startup(): Promise<CryptoStore> {
65
+ // No work to do, as the startup is done by the caller (e.g IndexedDBCryptoStore)
66
+ // by passing us a ready IDBDatabase instance
67
+ return this;
68
+ }
69
+
70
+ public async deleteAllData(): Promise<void> {
71
+ throw Error("This is not implemented, call IDBFactory::deleteDatabase(dbName) instead.");
72
+ }
73
+
74
+ /**
75
+ * Get data on how much of the libolm to Rust Crypto migration has been done.
76
+ *
77
+ * Implementation of {@link CryptoStore.getMigrationState}.
78
+ */
79
+ public async getMigrationState(): Promise<MigrationState> {
80
+ let migrationState = MigrationState.NOT_STARTED;
81
+ await this.doTxn("readonly", [IndexedDBCryptoStore.STORE_ACCOUNT], (txn) => {
82
+ const objectStore = txn.objectStore(IndexedDBCryptoStore.STORE_ACCOUNT);
83
+ const getReq = objectStore.get(ACCOUNT_OBJECT_KEY_MIGRATION_STATE);
84
+ getReq.onsuccess = (): void => {
85
+ migrationState = getReq.result ?? MigrationState.NOT_STARTED;
86
+ };
87
+ });
88
+ return migrationState;
89
+ }
90
+
91
+ /**
92
+ * Set data on how much of the libolm to Rust Crypto migration has been done.
93
+ *
94
+ * Implementation of {@link CryptoStore.setMigrationState}.
95
+ */
96
+ public async setMigrationState(migrationState: MigrationState): Promise<void> {
97
+ await this.doTxn("readwrite", [IndexedDBCryptoStore.STORE_ACCOUNT], (txn) => {
98
+ const objectStore = txn.objectStore(IndexedDBCryptoStore.STORE_ACCOUNT);
99
+ objectStore.put(migrationState, ACCOUNT_OBJECT_KEY_MIGRATION_STATE);
100
+ });
101
+ }
102
+
103
+ // Olm Account
104
+
105
+ public getAccount(txn: IDBTransaction, func: (accountPickle: string | null) => void): void {
106
+ const objectStore = txn.objectStore("account");
107
+ const getReq = objectStore.get("-");
108
+ getReq.onsuccess = function (): void {
109
+ try {
110
+ func(getReq.result || null);
111
+ } catch (e) {
112
+ abortWithException(txn, <Error>e);
113
+ }
114
+ };
115
+ }
116
+
117
+ public storeAccount(txn: IDBTransaction, accountPickle: string): void {
118
+ const objectStore = txn.objectStore("account");
119
+ objectStore.put(accountPickle, "-");
120
+ }
121
+
122
+ public getCrossSigningKeys(
123
+ txn: IDBTransaction,
124
+ func: (keys: Record<string, CrossSigningKeyInfo> | null) => void,
125
+ ): void {
126
+ const objectStore = txn.objectStore("account");
127
+ const getReq = objectStore.get("crossSigningKeys");
128
+ getReq.onsuccess = function (): void {
129
+ try {
130
+ func(getReq.result || null);
131
+ } catch (e) {
132
+ abortWithException(txn, <Error>e);
133
+ }
134
+ };
135
+ }
136
+
137
+ public getSecretStorePrivateKey<K extends keyof SecretStorePrivateKeys>(
138
+ txn: IDBTransaction,
139
+ func: (key: SecretStorePrivateKeys[K] | null) => void,
140
+ type: K,
141
+ ): void {
142
+ const objectStore = txn.objectStore("account");
143
+ const getReq = objectStore.get(`ssss_cache:${type}`);
144
+ getReq.onsuccess = function (): void {
145
+ try {
146
+ func(getReq.result || null);
147
+ } catch (e) {
148
+ abortWithException(txn, <Error>e);
149
+ }
150
+ };
151
+ }
152
+
153
+ public storeSecretStorePrivateKey<K extends keyof SecretStorePrivateKeys>(
154
+ txn: IDBTransaction,
155
+ type: K,
156
+ key: SecretStorePrivateKeys[K],
157
+ ): void {
158
+ const objectStore = txn.objectStore("account");
159
+ objectStore.put(key, `ssss_cache:${type}`);
160
+ }
161
+
162
+ // Olm Sessions
163
+
164
+ public countEndToEndSessions(txn: IDBTransaction, func: (count: number) => void): void {
165
+ const objectStore = txn.objectStore("sessions");
166
+ const countReq = objectStore.count();
167
+ countReq.onsuccess = function (): void {
168
+ try {
169
+ func(countReq.result);
170
+ } catch (e) {
171
+ abortWithException(txn, <Error>e);
172
+ }
173
+ };
174
+ }
175
+
176
+ public getEndToEndSessions(
177
+ deviceKey: string,
178
+ txn: IDBTransaction,
179
+ func: (sessions: { [sessionId: string]: ISessionInfo }) => void,
180
+ ): void {
181
+ const objectStore = txn.objectStore("sessions");
182
+ const idx = objectStore.index("deviceKey");
183
+ const getReq = idx.openCursor(deviceKey);
184
+ const results: Parameters<Parameters<Backend["getEndToEndSessions"]>[2]>[0] = {};
185
+ getReq.onsuccess = function (): void {
186
+ const cursor = getReq.result;
187
+ if (cursor) {
188
+ results[cursor.value.sessionId] = {
189
+ session: cursor.value.session,
190
+ lastReceivedMessageTs: cursor.value.lastReceivedMessageTs,
191
+ };
192
+ cursor.continue();
193
+ } else {
194
+ try {
195
+ func(results);
196
+ } catch (e) {
197
+ abortWithException(txn, <Error>e);
198
+ }
199
+ }
200
+ };
201
+ }
202
+
203
+ public getEndToEndSession(
204
+ deviceKey: string,
205
+ sessionId: string,
206
+ txn: IDBTransaction,
207
+ func: (session: ISessionInfo | null) => void,
208
+ ): void {
209
+ const objectStore = txn.objectStore("sessions");
210
+ const getReq = objectStore.get([deviceKey, sessionId]);
211
+ getReq.onsuccess = function (): void {
212
+ try {
213
+ if (getReq.result) {
214
+ func({
215
+ session: getReq.result.session,
216
+ lastReceivedMessageTs: getReq.result.lastReceivedMessageTs,
217
+ });
218
+ } else {
219
+ func(null);
220
+ }
221
+ } catch (e) {
222
+ abortWithException(txn, <Error>e);
223
+ }
224
+ };
225
+ }
226
+
227
+ public storeEndToEndSession(
228
+ deviceKey: string,
229
+ sessionId: string,
230
+ sessionInfo: ISessionInfo,
231
+ txn: IDBTransaction,
232
+ ): void {
233
+ const objectStore = txn.objectStore("sessions");
234
+ objectStore.put({
235
+ deviceKey,
236
+ sessionId,
237
+ session: sessionInfo.session,
238
+ lastReceivedMessageTs: sessionInfo.lastReceivedMessageTs,
239
+ });
240
+ }
241
+
242
+ /**
243
+ * Fetch a batch of Olm sessions from the database.
244
+ *
245
+ * Implementation of {@link CryptoStore.getEndToEndSessionsBatch}.
246
+ */
247
+ public async getEndToEndSessionsBatch(): Promise<null | ISessionInfo[]> {
248
+ const result: ISessionInfo[] = [];
249
+ await this.doTxn("readonly", [IndexedDBCryptoStore.STORE_SESSIONS], (txn) => {
250
+ const objectStore = txn.objectStore(IndexedDBCryptoStore.STORE_SESSIONS);
251
+ const getReq = objectStore.openCursor();
252
+ getReq.onsuccess = function (): void {
253
+ try {
254
+ const cursor = getReq.result;
255
+ if (cursor) {
256
+ result.push(cursor.value);
257
+ if (result.length < SESSION_BATCH_SIZE) {
258
+ cursor.continue();
259
+ }
260
+ }
261
+ } catch (e) {
262
+ abortWithException(txn, <Error>e);
263
+ }
264
+ };
265
+ });
266
+
267
+ if (result.length === 0) {
268
+ // No sessions left.
269
+ return null;
270
+ }
271
+
272
+ return result;
273
+ }
274
+
275
+ /**
276
+ * Delete a batch of Olm sessions from the database.
277
+ *
278
+ * Implementation of {@link CryptoStore.deleteEndToEndSessionsBatch}.
279
+ *
280
+ * @internal
281
+ */
282
+ public async deleteEndToEndSessionsBatch(sessions: { deviceKey: string; sessionId: string }[]): Promise<void> {
283
+ await this.doTxn("readwrite", [IndexedDBCryptoStore.STORE_SESSIONS], async (txn) => {
284
+ try {
285
+ const objectStore = txn.objectStore(IndexedDBCryptoStore.STORE_SESSIONS);
286
+ for (const { deviceKey, sessionId } of sessions) {
287
+ const req = objectStore.delete([deviceKey, sessionId]);
288
+ await new Promise((resolve) => {
289
+ req.onsuccess = resolve;
290
+ });
291
+ }
292
+ } catch (e) {
293
+ abortWithException(txn, <Error>e);
294
+ }
295
+ });
296
+ }
297
+
298
+ // Inbound group sessions
299
+
300
+ public getEndToEndInboundGroupSession(
301
+ senderCurve25519Key: string,
302
+ sessionId: string,
303
+ txn: IDBTransaction,
304
+ func: (groupSession: InboundGroupSessionData | null, groupSessionWithheld: IWithheld | null) => void,
305
+ ): void {
306
+ let session: InboundGroupSessionData | null | boolean = false;
307
+ let withheld: IWithheld | null | boolean = false;
308
+ const objectStore = txn.objectStore("inbound_group_sessions");
309
+ const getReq = objectStore.get([senderCurve25519Key, sessionId]);
310
+ getReq.onsuccess = function (): void {
311
+ try {
312
+ if (getReq.result) {
313
+ session = getReq.result.session;
314
+ } else {
315
+ session = null;
316
+ }
317
+ if (withheld !== false) {
318
+ func(session as InboundGroupSessionData, withheld as IWithheld);
319
+ }
320
+ } catch (e) {
321
+ abortWithException(txn, <Error>e);
322
+ }
323
+ };
324
+
325
+ const withheldObjectStore = txn.objectStore("inbound_group_sessions_withheld");
326
+ const withheldGetReq = withheldObjectStore.get([senderCurve25519Key, sessionId]);
327
+ withheldGetReq.onsuccess = function (): void {
328
+ try {
329
+ if (withheldGetReq.result) {
330
+ withheld = withheldGetReq.result.session;
331
+ } else {
332
+ withheld = null;
333
+ }
334
+ if (session !== false) {
335
+ func(session as InboundGroupSessionData, withheld as IWithheld);
336
+ }
337
+ } catch (e) {
338
+ abortWithException(txn, <Error>e);
339
+ }
340
+ };
341
+ }
342
+
343
+ public storeEndToEndInboundGroupSession(
344
+ senderCurve25519Key: string,
345
+ sessionId: string,
346
+ sessionData: InboundGroupSessionData,
347
+ txn: IDBTransaction,
348
+ ): void {
349
+ const objectStore = txn.objectStore("inbound_group_sessions");
350
+ objectStore.put({
351
+ senderCurve25519Key,
352
+ sessionId,
353
+ session: sessionData,
354
+ });
355
+ }
356
+
357
+ /**
358
+ * Count the number of Megolm sessions in the database.
359
+ *
360
+ * Implementation of {@link CryptoStore.countEndToEndInboundGroupSessions}.
361
+ *
362
+ * @internal
363
+ */
364
+ public async countEndToEndInboundGroupSessions(): Promise<number> {
365
+ let result = 0;
366
+ await this.doTxn("readonly", [IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS], (txn) => {
367
+ const sessionStore = txn.objectStore(IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS);
368
+ const countReq = sessionStore.count();
369
+ countReq.onsuccess = (): void => {
370
+ result = countReq.result;
371
+ };
372
+ });
373
+ return result;
374
+ }
375
+
376
+ /**
377
+ * Fetch a batch of Megolm sessions from the database.
378
+ *
379
+ * Implementation of {@link CryptoStore.getEndToEndInboundGroupSessionsBatch}.
380
+ */
381
+ public async getEndToEndInboundGroupSessionsBatch(): Promise<null | SessionExtended[]> {
382
+ const result: SessionExtended[] = [];
383
+ await this.doTxn(
384
+ "readonly",
385
+ [IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS, IndexedDBCryptoStore.STORE_BACKUP],
386
+ (txn) => {
387
+ const sessionStore = txn.objectStore(IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS);
388
+ const backupStore = txn.objectStore(IndexedDBCryptoStore.STORE_BACKUP);
389
+
390
+ const getReq = sessionStore.openCursor();
391
+ getReq.onsuccess = function (): void {
392
+ try {
393
+ const cursor = getReq.result;
394
+ if (cursor) {
395
+ const backupGetReq = backupStore.get(cursor.key);
396
+ backupGetReq.onsuccess = (): void => {
397
+ result.push({
398
+ senderKey: cursor.value.senderCurve25519Key,
399
+ sessionId: cursor.value.sessionId,
400
+ sessionData: cursor.value.session,
401
+ needsBackup: backupGetReq.result !== undefined,
402
+ });
403
+ if (result.length < SESSION_BATCH_SIZE) {
404
+ cursor.continue();
405
+ }
406
+ };
407
+ }
408
+ } catch (e) {
409
+ abortWithException(txn, <Error>e);
410
+ }
411
+ };
412
+ },
413
+ );
414
+
415
+ if (result.length === 0) {
416
+ // No sessions left.
417
+ return null;
418
+ }
419
+
420
+ return result;
421
+ }
422
+
423
+ /**
424
+ * Delete a batch of Megolm sessions from the database.
425
+ *
426
+ * Implementation of {@link CryptoStore.deleteEndToEndInboundGroupSessionsBatch}.
427
+ *
428
+ * @internal
429
+ */
430
+ public async deleteEndToEndInboundGroupSessionsBatch(
431
+ sessions: { senderKey: string; sessionId: string }[],
432
+ ): Promise<void> {
433
+ await this.doTxn("readwrite", [IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS], async (txn) => {
434
+ try {
435
+ const objectStore = txn.objectStore(IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS);
436
+ for (const { senderKey, sessionId } of sessions) {
437
+ const req = objectStore.delete([senderKey, sessionId]);
438
+ await new Promise((resolve) => {
439
+ req.onsuccess = resolve;
440
+ });
441
+ }
442
+ } catch (e) {
443
+ abortWithException(txn, <Error>e);
444
+ }
445
+ });
446
+ }
447
+
448
+ public getEndToEndDeviceData(txn: IDBTransaction, func: (deviceData: IDeviceData | null) => void): void {
449
+ const objectStore = txn.objectStore("device_data");
450
+ const getReq = objectStore.get("-");
451
+ getReq.onsuccess = function (): void {
452
+ try {
453
+ func(getReq.result || null);
454
+ } catch (e) {
455
+ abortWithException(txn, <Error>e);
456
+ }
457
+ };
458
+ }
459
+
460
+ public getEndToEndRooms(txn: IDBTransaction, func: (rooms: Record<string, IRoomEncryption>) => void): void {
461
+ const rooms: Parameters<Parameters<Backend["getEndToEndRooms"]>[1]>[0] = {};
462
+ const objectStore = txn.objectStore("rooms");
463
+ const getReq = objectStore.openCursor();
464
+ getReq.onsuccess = function (): void {
465
+ const cursor = getReq.result;
466
+ if (cursor) {
467
+ rooms[cursor.key as string] = cursor.value;
468
+ cursor.continue();
469
+ } else {
470
+ try {
471
+ func(rooms);
472
+ } catch (e) {
473
+ abortWithException(txn, <Error>e);
474
+ }
475
+ }
476
+ };
477
+ }
478
+
479
+ public async markSessionsNeedingBackup(sessions: ISession[], txn?: IDBTransaction): Promise<void> {
480
+ if (!txn) {
481
+ txn = this.db.transaction("sessions_needing_backup", "readwrite");
482
+ }
483
+ const objectStore = txn.objectStore("sessions_needing_backup");
484
+ await Promise.all(
485
+ sessions.map((session) => {
486
+ return new Promise((resolve, reject) => {
487
+ const req = objectStore.put({
488
+ senderCurve25519Key: session.senderKey,
489
+ sessionId: session.sessionId,
490
+ });
491
+ req.onsuccess = resolve;
492
+ req.onerror = reject;
493
+ });
494
+ }),
495
+ );
496
+ }
497
+
498
+ public doTxn<T>(
499
+ mode: Mode,
500
+ stores: string | string[],
501
+ func: (txn: IDBTransaction) => T,
502
+ log: Logger = logger,
503
+ ): Promise<T> {
504
+ let startTime: number;
505
+ let description: string;
506
+ if (PROFILE_TRANSACTIONS) {
507
+ const txnId = this.nextTxnId++;
508
+ startTime = Date.now();
509
+ description = `${mode} crypto store transaction ${txnId} in ${stores}`;
510
+ log.debug(`Starting ${description}`);
511
+ }
512
+ const txn = this.db.transaction(stores, mode);
513
+ const promise = promiseifyTxn(txn);
514
+ const result = func(txn);
515
+ if (PROFILE_TRANSACTIONS) {
516
+ promise.then(
517
+ () => {
518
+ const elapsedTime = Date.now() - startTime;
519
+ log.debug(`Finished ${description}, took ${elapsedTime} ms`);
520
+ },
521
+ () => {
522
+ const elapsedTime = Date.now() - startTime;
523
+ log.error(`Failed ${description}, took ${elapsedTime} ms`);
524
+ },
525
+ );
526
+ }
527
+ return promise.then(() => {
528
+ return result;
529
+ });
530
+ }
531
+ }
532
+
533
+ type DbMigration = (db: IDBDatabase) => void;
534
+ const DB_MIGRATIONS: DbMigration[] = [
535
+ (db): void => {
536
+ createDatabase(db);
537
+ },
538
+ (db): void => {
539
+ db.createObjectStore("account");
540
+ },
541
+ (db): void => {
542
+ const sessionsStore = db.createObjectStore("sessions", {
543
+ keyPath: ["deviceKey", "sessionId"],
544
+ });
545
+ sessionsStore.createIndex("deviceKey", "deviceKey");
546
+ },
547
+ (db): void => {
548
+ db.createObjectStore("inbound_group_sessions", {
549
+ keyPath: ["senderCurve25519Key", "sessionId"],
550
+ });
551
+ },
552
+ (db): void => {
553
+ db.createObjectStore("device_data");
554
+ },
555
+ (db): void => {
556
+ db.createObjectStore("rooms");
557
+ },
558
+ (db): void => {
559
+ db.createObjectStore("sessions_needing_backup", {
560
+ keyPath: ["senderCurve25519Key", "sessionId"],
561
+ });
562
+ },
563
+ (db): void => {
564
+ db.createObjectStore("inbound_group_sessions_withheld", {
565
+ keyPath: ["senderCurve25519Key", "sessionId"],
566
+ });
567
+ },
568
+ (db): void => {
569
+ const problemsStore = db.createObjectStore("session_problems", {
570
+ keyPath: ["deviceKey", "time"],
571
+ });
572
+ problemsStore.createIndex("deviceKey", "deviceKey");
573
+
574
+ db.createObjectStore("notified_error_devices", {
575
+ keyPath: ["userId", "deviceId"],
576
+ });
577
+ },
578
+ (db): void => {
579
+ db.createObjectStore("shared_history_inbound_group_sessions", {
580
+ keyPath: ["roomId"],
581
+ });
582
+ },
583
+ (db): void => {
584
+ db.createObjectStore("parked_shared_history", {
585
+ keyPath: ["roomId"],
586
+ });
587
+ },
588
+ // Expand as needed.
589
+ ];
590
+ export const VERSION = DB_MIGRATIONS.length;
591
+
592
+ export function upgradeDatabase(db: IDBDatabase, oldVersion: number): void {
593
+ logger.log(`Upgrading IndexedDBCryptoStore from version ${oldVersion}` + ` to ${VERSION}`);
594
+ DB_MIGRATIONS.forEach((migration, index) => {
595
+ if (oldVersion <= index) migration(db);
596
+ });
597
+ }
598
+
599
+ function createDatabase(db: IDBDatabase): void {
600
+ const outgoingRoomKeyRequestsStore = db.createObjectStore("outgoingRoomKeyRequests", { keyPath: "requestId" });
601
+
602
+ // we assume that the RoomKeyRequestBody will have room_id and session_id
603
+ // properties, to make the index efficient.
604
+ outgoingRoomKeyRequestsStore.createIndex("session", ["requestBody.room_id", "requestBody.session_id"]);
605
+
606
+ outgoingRoomKeyRequestsStore.createIndex("state", "state");
607
+ }
608
+
609
+ interface IWrappedIDBTransaction extends IDBTransaction {
610
+ _mx_abortexception: Error; // eslint-disable-line camelcase
611
+ }
612
+
613
+ /*
614
+ * Aborts a transaction with a given exception
615
+ * The transaction promise will be rejected with this exception.
616
+ */
617
+ function abortWithException(txn: IDBTransaction, e: Error): void {
618
+ // We cheekily stick our exception onto the transaction object here
619
+ // We could alternatively make the thing we pass back to the app
620
+ // an object containing the transaction and exception.
621
+ (txn as IWrappedIDBTransaction)._mx_abortexception = e;
622
+ try {
623
+ txn.abort();
624
+ } catch {
625
+ // sometimes we won't be able to abort the transaction
626
+ // (ie. if it's aborted or completed)
627
+ }
628
+ }
629
+
630
+ function promiseifyTxn<T>(txn: IDBTransaction): Promise<T | null> {
631
+ return new Promise((resolve, reject) => {
632
+ txn.oncomplete = (): void => {
633
+ if ((txn as IWrappedIDBTransaction)._mx_abortexception !== undefined) {
634
+ reject((txn as IWrappedIDBTransaction)._mx_abortexception);
635
+ }
636
+ resolve(null);
637
+ };
638
+ txn.onerror = (event): void => {
639
+ if ((txn as IWrappedIDBTransaction)._mx_abortexception !== undefined) {
640
+ reject((txn as IWrappedIDBTransaction)._mx_abortexception);
641
+ } else {
642
+ logger.log("Error performing indexeddb txn", event);
643
+ reject(txn.error);
644
+ }
645
+ };
646
+ txn.onabort = (event): void => {
647
+ if ((txn as IWrappedIDBTransaction)._mx_abortexception !== undefined) {
648
+ reject((txn as IWrappedIDBTransaction)._mx_abortexception);
649
+ } else {
650
+ logger.log("Error performing indexeddb txn", event);
651
+ reject(txn.error);
652
+ }
653
+ };
654
+ });
655
+ }