@open-e2ee/signal-protocol-sdk 11.0.0 → 12.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 (590) hide show
  1. package/ADAPTERS.md +4 -4
  2. package/ARCHITECTURE.md +6 -8
  3. package/CHANGELOG.md +1348 -0
  4. package/README.md +1 -1
  5. package/SECURITY.md +2 -2
  6. package/TROUBLESHOOTING.md +8 -8
  7. package/client/README.md +70 -37
  8. package/device/README.md +39 -85
  9. package/dist/client/client.d.ts +334 -63
  10. package/dist/client/client.d.ts.map +1 -1
  11. package/dist/client/client.js +1175 -186
  12. package/dist/client/client.js.map +1 -1
  13. package/dist/client/config.d.ts +57 -42
  14. package/dist/client/config.d.ts.map +1 -1
  15. package/dist/client/config.js +1 -1
  16. package/dist/client/config.js.map +1 -1
  17. package/dist/client/constants.d.ts +42 -8
  18. package/dist/client/constants.d.ts.map +1 -1
  19. package/dist/client/constants.js +48 -3
  20. package/dist/client/constants.js.map +1 -1
  21. package/dist/client/content-adapter.d.ts +80 -0
  22. package/dist/client/content-adapter.d.ts.map +1 -1
  23. package/dist/client/content-adapter.js +225 -12
  24. package/dist/client/content-adapter.js.map +1 -1
  25. package/dist/client/delivery-error.d.ts +150 -0
  26. package/dist/client/delivery-error.d.ts.map +1 -0
  27. package/dist/client/delivery-error.js +226 -0
  28. package/dist/client/delivery-error.js.map +1 -0
  29. package/dist/client/delivery-status.d.ts +32 -6
  30. package/dist/client/delivery-status.d.ts.map +1 -1
  31. package/dist/client/delivery-status.js +61 -13
  32. package/dist/client/delivery-status.js.map +1 -1
  33. package/dist/client/device-set-reconciliation.d.ts +196 -0
  34. package/dist/client/device-set-reconciliation.d.ts.map +1 -0
  35. package/dist/client/device-set-reconciliation.js +282 -0
  36. package/dist/client/device-set-reconciliation.js.map +1 -0
  37. package/dist/client/distribution-records.d.ts +29 -0
  38. package/dist/client/distribution-records.d.ts.map +1 -0
  39. package/dist/client/distribution-records.js +33 -0
  40. package/dist/client/distribution-records.js.map +1 -0
  41. package/dist/client/event-hooks.d.ts +280 -14
  42. package/dist/client/event-hooks.d.ts.map +1 -1
  43. package/dist/client/event-hooks.js +8 -0
  44. package/dist/client/event-hooks.js.map +1 -1
  45. package/dist/client/group-device-set.d.ts +47 -0
  46. package/dist/client/group-device-set.d.ts.map +1 -0
  47. package/dist/client/group-device-set.js +65 -0
  48. package/dist/client/group-device-set.js.map +1 -0
  49. package/dist/client/group-fetcher.d.ts +116 -0
  50. package/dist/client/group-fetcher.d.ts.map +1 -0
  51. package/dist/client/group-fetcher.js +598 -0
  52. package/dist/client/group-fetcher.js.map +1 -0
  53. package/dist/client/group-outbox.d.ts +172 -10
  54. package/dist/client/group-outbox.d.ts.map +1 -1
  55. package/dist/client/group-outbox.js +915 -74
  56. package/dist/client/group-outbox.js.map +1 -1
  57. package/dist/client/group-receive-authorization.d.ts +127 -4
  58. package/dist/client/group-receive-authorization.d.ts.map +1 -1
  59. package/dist/client/group-receive-authorization.js +270 -20
  60. package/dist/client/group-receive-authorization.js.map +1 -1
  61. package/dist/client/group-sealed-sender.d.ts +3 -0
  62. package/dist/client/group-sealed-sender.d.ts.map +1 -1
  63. package/dist/client/group-sealed-sender.js +1 -0
  64. package/dist/client/group-sealed-sender.js.map +1 -1
  65. package/dist/client/group-sender-key-operations.d.ts +14 -3
  66. package/dist/client/group-sender-key-operations.d.ts.map +1 -1
  67. package/dist/client/group-sender-key-operations.js +26 -5
  68. package/dist/client/group-sender-key-operations.js.map +1 -1
  69. package/dist/client/groups.d.ts +5 -5
  70. package/dist/client/groups.d.ts.map +1 -1
  71. package/dist/client/groups.js +5 -4
  72. package/dist/client/groups.js.map +1 -1
  73. package/dist/client/hosted-anonymous-delivery.d.ts +61 -9
  74. package/dist/client/hosted-anonymous-delivery.d.ts.map +1 -1
  75. package/dist/client/hosted-anonymous-delivery.js +242 -48
  76. package/dist/client/hosted-anonymous-delivery.js.map +1 -1
  77. package/dist/client/hosted-assertion-id.d.ts +7 -0
  78. package/dist/client/hosted-assertion-id.d.ts.map +1 -0
  79. package/dist/client/hosted-assertion-id.js +51 -0
  80. package/dist/client/hosted-assertion-id.js.map +1 -0
  81. package/dist/client/hosted-mailbox-subscription.d.ts +34 -2
  82. package/dist/client/hosted-mailbox-subscription.d.ts.map +1 -1
  83. package/dist/client/hosted-mailbox-subscription.js +87 -17
  84. package/dist/client/hosted-mailbox-subscription.js.map +1 -1
  85. package/dist/client/hosted-push.d.ts +22 -4
  86. package/dist/client/hosted-push.d.ts.map +1 -1
  87. package/dist/client/hosted-push.js +75 -22
  88. package/dist/client/hosted-push.js.map +1 -1
  89. package/dist/client/hosted-transport.d.ts +22 -28
  90. package/dist/client/hosted-transport.d.ts.map +1 -1
  91. package/dist/client/hosted-transport.js +115 -55
  92. package/dist/client/hosted-transport.js.map +1 -1
  93. package/dist/client/hosted.d.ts +10 -4
  94. package/dist/client/hosted.d.ts.map +1 -1
  95. package/dist/client/hosted.js +10 -4
  96. package/dist/client/hosted.js.map +1 -1
  97. package/dist/client/index.d.ts +5 -3
  98. package/dist/client/index.d.ts.map +1 -1
  99. package/dist/client/index.js +2 -0
  100. package/dist/client/index.js.map +1 -1
  101. package/dist/client/messages.d.ts +25 -3
  102. package/dist/client/messages.d.ts.map +1 -1
  103. package/dist/client/messages.js +111 -20
  104. package/dist/client/messages.js.map +1 -1
  105. package/dist/client/receive-admission.d.ts +58 -0
  106. package/dist/client/receive-admission.d.ts.map +1 -0
  107. package/dist/client/receive-admission.js +78 -0
  108. package/dist/client/receive-admission.js.map +1 -0
  109. package/dist/client/receive-expiry.d.ts +31 -0
  110. package/dist/client/receive-expiry.d.ts.map +1 -0
  111. package/dist/client/receive-expiry.js +35 -0
  112. package/dist/client/receive-expiry.js.map +1 -0
  113. package/dist/client/receive-hook-bound.d.ts +50 -0
  114. package/dist/client/receive-hook-bound.d.ts.map +1 -0
  115. package/dist/client/receive-hook-bound.js +115 -0
  116. package/dist/client/receive-hook-bound.js.map +1 -0
  117. package/dist/client/receive-queue.d.ts +299 -0
  118. package/dist/client/receive-queue.d.ts.map +1 -0
  119. package/dist/client/receive-queue.js +1420 -0
  120. package/dist/client/receive-queue.js.map +1 -0
  121. package/dist/client/receive-work-seed.d.ts +54 -0
  122. package/dist/client/receive-work-seed.d.ts.map +1 -0
  123. package/dist/client/receive-work-seed.js +116 -0
  124. package/dist/client/receive-work-seed.js.map +1 -0
  125. package/dist/client/recovery-driver.d.ts +25 -0
  126. package/dist/client/recovery-driver.d.ts.map +1 -0
  127. package/dist/client/recovery-driver.js +121 -0
  128. package/dist/client/recovery-driver.js.map +1 -0
  129. package/dist/client/relay-acceptance.d.ts +20 -7
  130. package/dist/client/relay-acceptance.d.ts.map +1 -1
  131. package/dist/client/relay-acceptance.js +30 -12
  132. package/dist/client/relay-acceptance.js.map +1 -1
  133. package/dist/client/relay-device-refusal.d.ts +18 -0
  134. package/dist/client/relay-device-refusal.d.ts.map +1 -1
  135. package/dist/client/relay-device-refusal.js +31 -0
  136. package/dist/client/relay-device-refusal.js.map +1 -1
  137. package/dist/client/relay-sealed-route.d.ts +36 -0
  138. package/dist/client/relay-sealed-route.d.ts.map +1 -0
  139. package/dist/client/relay-sealed-route.js +45 -0
  140. package/dist/client/relay-sealed-route.js.map +1 -0
  141. package/dist/client/relay-subscription.d.ts +56 -13
  142. package/dist/client/relay-subscription.d.ts.map +1 -1
  143. package/dist/client/relay-subscription.js +193 -123
  144. package/dist/client/relay-subscription.js.map +1 -1
  145. package/dist/client/resendable-content.d.ts +98 -0
  146. package/dist/client/resendable-content.d.ts.map +1 -0
  147. package/dist/client/resendable-content.js +136 -0
  148. package/dist/client/resendable-content.js.map +1 -0
  149. package/dist/client/retry-family.d.ts +57 -10
  150. package/dist/client/retry-family.d.ts.map +1 -1
  151. package/dist/client/retry-family.js +54 -12
  152. package/dist/client/retry-family.js.map +1 -1
  153. package/dist/client/retry-request-envelope.d.ts +107 -8
  154. package/dist/client/retry-request-envelope.d.ts.map +1 -1
  155. package/dist/client/retry-request-envelope.js +172 -23
  156. package/dist/client/retry-request-envelope.js.map +1 -1
  157. package/dist/client/retry-request-lifecycle.d.ts +70 -14
  158. package/dist/client/retry-request-lifecycle.d.ts.map +1 -1
  159. package/dist/client/retry-request-lifecycle.js +162 -32
  160. package/dist/client/retry-request-lifecycle.js.map +1 -1
  161. package/dist/client/retry-terminal-recovery.d.ts +100 -0
  162. package/dist/client/retry-terminal-recovery.d.ts.map +1 -0
  163. package/dist/client/retry-terminal-recovery.js +164 -0
  164. package/dist/client/retry-terminal-recovery.js.map +1 -0
  165. package/dist/client/retry-utils.d.ts +27 -0
  166. package/dist/client/retry-utils.d.ts.map +1 -1
  167. package/dist/client/retry-utils.js +46 -0
  168. package/dist/client/retry-utils.js.map +1 -1
  169. package/dist/client/retry.d.ts +100 -12
  170. package/dist/client/retry.d.ts.map +1 -1
  171. package/dist/client/retry.js +619 -116
  172. package/dist/client/retry.js.map +1 -1
  173. package/dist/client/sealed-sender.d.ts +23 -6
  174. package/dist/client/sealed-sender.d.ts.map +1 -1
  175. package/dist/client/sealed-sender.js +30 -6
  176. package/dist/client/sealed-sender.js.map +1 -1
  177. package/dist/client/sessions.d.ts +2 -2
  178. package/dist/client/sessions.js +2 -2
  179. package/dist/client/signal-service-cipher.d.ts +726 -19
  180. package/dist/client/signal-service-cipher.d.ts.map +1 -1
  181. package/dist/client/signal-service-cipher.js +2260 -342
  182. package/dist/client/signal-service-cipher.js.map +1 -1
  183. package/dist/client/state.d.ts +5 -0
  184. package/dist/client/state.d.ts.map +1 -1
  185. package/dist/client/state.js +11 -1
  186. package/dist/client/state.js.map +1 -1
  187. package/dist/client/storage-protection.d.ts +64 -0
  188. package/dist/client/storage-protection.d.ts.map +1 -0
  189. package/dist/client/storage-protection.js +150 -0
  190. package/dist/client/storage-protection.js.map +1 -0
  191. package/dist/client/types.d.ts +149 -15
  192. package/dist/client/types.d.ts.map +1 -1
  193. package/dist/client/types.js +1 -1
  194. package/dist/client/types.js.map +1 -1
  195. package/dist/device/constants.d.ts +0 -5
  196. package/dist/device/constants.d.ts.map +1 -1
  197. package/dist/device/constants.js +0 -5
  198. package/dist/device/constants.js.map +1 -1
  199. package/dist/device/expo/index.d.ts +4 -17
  200. package/dist/device/expo/index.d.ts.map +1 -1
  201. package/dist/device/expo/index.js +4 -23
  202. package/dist/device/expo/index.js.map +1 -1
  203. package/dist/device/index.d.ts +0 -2
  204. package/dist/device/index.d.ts.map +1 -1
  205. package/dist/device/index.js +0 -7
  206. package/dist/device/index.js.map +1 -1
  207. package/dist/device/lifecycle/manager.d.ts +5 -4
  208. package/dist/device/lifecycle/manager.d.ts.map +1 -1
  209. package/dist/device/lifecycle/manager.js +16 -16
  210. package/dist/device/lifecycle/manager.js.map +1 -1
  211. package/dist/device/lifecycle/types.d.ts +2 -2
  212. package/dist/device/lifecycle/types.d.ts.map +1 -1
  213. package/dist/device/react-native/index.d.ts +4 -15
  214. package/dist/device/react-native/index.d.ts.map +1 -1
  215. package/dist/device/react-native/index.js +4 -21
  216. package/dist/device/react-native/index.js.map +1 -1
  217. package/dist/device/types.d.ts +1 -1
  218. package/dist/groups/index.d.ts +1 -1
  219. package/dist/groups/index.d.ts.map +1 -1
  220. package/dist/index.d.ts +8 -6
  221. package/dist/index.d.ts.map +1 -1
  222. package/dist/index.js +2 -1
  223. package/dist/index.js.map +1 -1
  224. package/dist/internal/crypto/ec/x25519-validation.d.ts +0 -1
  225. package/dist/internal/crypto/ec/x25519-validation.d.ts.map +1 -1
  226. package/dist/internal/crypto/ec/x25519-validation.js +0 -1
  227. package/dist/internal/crypto/ec/x25519-validation.js.map +1 -1
  228. package/dist/internal/crypto/validation.d.ts +2 -2
  229. package/dist/internal/groups/index.d.ts +1 -0
  230. package/dist/internal/groups/index.d.ts.map +1 -1
  231. package/dist/internal/groups/index.js +1 -0
  232. package/dist/internal/groups/index.js.map +1 -1
  233. package/dist/internal/groups/manager.d.ts +37 -16
  234. package/dist/internal/groups/manager.d.ts.map +1 -1
  235. package/dist/internal/groups/manager.js +46 -1
  236. package/dist/internal/groups/manager.js.map +1 -1
  237. package/dist/internal/groups/receive-record.d.ts +78 -0
  238. package/dist/internal/groups/receive-record.d.ts.map +1 -0
  239. package/dist/internal/groups/receive-record.js +43 -0
  240. package/dist/internal/groups/receive-record.js.map +1 -0
  241. package/dist/internal/groups/sdk-store.d.ts +27 -3
  242. package/dist/internal/groups/sdk-store.d.ts.map +1 -1
  243. package/dist/internal/groups/sdk-store.js +151 -19
  244. package/dist/internal/groups/sdk-store.js.map +1 -1
  245. package/dist/internal/groups/wire.d.ts +12 -1
  246. package/dist/internal/groups/wire.d.ts.map +1 -1
  247. package/dist/internal/groups/wire.js +36 -0
  248. package/dist/internal/groups/wire.js.map +1 -1
  249. package/dist/internal/manager/index.d.ts +1 -1
  250. package/dist/internal/manager/manager.d.ts +26 -20
  251. package/dist/internal/manager/manager.d.ts.map +1 -1
  252. package/dist/internal/manager/manager.js +64 -41
  253. package/dist/internal/manager/manager.js.map +1 -1
  254. package/dist/internal/protocol/double-ratchet/chains.d.ts +4 -18
  255. package/dist/internal/protocol/double-ratchet/chains.d.ts.map +1 -1
  256. package/dist/internal/protocol/double-ratchet/chains.js +23 -74
  257. package/dist/internal/protocol/double-ratchet/chains.js.map +1 -1
  258. package/dist/internal/protocol/double-ratchet/index.d.ts +1 -1
  259. package/dist/internal/protocol/double-ratchet/index.d.ts.map +1 -1
  260. package/dist/internal/protocol/double-ratchet/index.js +1 -1
  261. package/dist/internal/protocol/double-ratchet/index.js.map +1 -1
  262. package/dist/internal/protocol/double-ratchet/ratchet.d.ts +5 -7
  263. package/dist/internal/protocol/double-ratchet/ratchet.d.ts.map +1 -1
  264. package/dist/internal/protocol/double-ratchet/ratchet.js +4 -6
  265. package/dist/internal/protocol/double-ratchet/ratchet.js.map +1 -1
  266. package/dist/internal/protocol/pqxdh/pqxdh.d.ts.map +1 -1
  267. package/dist/internal/protocol/pqxdh/pqxdh.js +2 -1
  268. package/dist/internal/protocol/pqxdh/pqxdh.js.map +1 -1
  269. package/dist/internal/protocol/sealed-sender/types.d.ts +6 -8
  270. package/dist/internal/protocol/sealed-sender/types.d.ts.map +1 -1
  271. package/dist/internal/protocol/sealed-sender/types.js +8 -9
  272. package/dist/internal/protocol/sealed-sender/types.js.map +1 -1
  273. package/dist/internal/protocol/sender-keys/handoff-ledger.d.ts +110 -0
  274. package/dist/internal/protocol/sender-keys/handoff-ledger.d.ts.map +1 -0
  275. package/dist/internal/protocol/sender-keys/handoff-ledger.js +124 -0
  276. package/dist/internal/protocol/sender-keys/handoff-ledger.js.map +1 -0
  277. package/dist/internal/protocol/sender-keys/manager.d.ts +71 -62
  278. package/dist/internal/protocol/sender-keys/manager.d.ts.map +1 -1
  279. package/dist/internal/protocol/sender-keys/manager.js +228 -175
  280. package/dist/internal/protocol/sender-keys/manager.js.map +1 -1
  281. package/dist/internal/protocol/spqr/index.d.ts +2 -2
  282. package/dist/internal/protocol/spqr/index.d.ts.map +1 -1
  283. package/dist/internal/protocol/spqr/index.js +2 -2
  284. package/dist/internal/protocol/spqr/index.js.map +1 -1
  285. package/dist/internal/protocol/spqr/serialize.d.ts +9 -1
  286. package/dist/internal/protocol/spqr/serialize.d.ts.map +1 -1
  287. package/dist/internal/protocol/spqr/serialize.js +56 -16
  288. package/dist/internal/protocol/spqr/serialize.js.map +1 -1
  289. package/dist/internal/protocol/spqr/spqr.d.ts +1 -14
  290. package/dist/internal/protocol/spqr/spqr.d.ts.map +1 -1
  291. package/dist/internal/protocol/spqr/spqr.js +54 -42
  292. package/dist/internal/protocol/spqr/spqr.js.map +1 -1
  293. package/dist/internal/protocol/spqr/validate.d.ts +26 -6
  294. package/dist/internal/protocol/spqr/validate.d.ts.map +1 -1
  295. package/dist/internal/protocol/spqr/validate.js +49 -21
  296. package/dist/internal/protocol/spqr/validate.js.map +1 -1
  297. package/dist/internal/sesame/index.d.ts +1 -1
  298. package/dist/internal/sesame/index.d.ts.map +1 -1
  299. package/dist/internal/sesame/index.js.map +1 -1
  300. package/dist/internal/sesame/manager.d.ts +66 -13
  301. package/dist/internal/sesame/manager.d.ts.map +1 -1
  302. package/dist/internal/sesame/manager.js +221 -87
  303. package/dist/internal/sesame/manager.js.map +1 -1
  304. package/dist/internal/sesame/types.d.ts +95 -17
  305. package/dist/internal/sesame/types.d.ts.map +1 -1
  306. package/dist/internal/sesame/types.js +17 -0
  307. package/dist/internal/sesame/types.js.map +1 -1
  308. package/dist/internal/session/builder.d.ts +5 -5
  309. package/dist/internal/session/builder.d.ts.map +1 -1
  310. package/dist/internal/session/builder.js +12 -17
  311. package/dist/internal/session/builder.js.map +1 -1
  312. package/dist/internal/session/cipher.d.ts +15 -13
  313. package/dist/internal/session/cipher.d.ts.map +1 -1
  314. package/dist/internal/session/cipher.js +41 -23
  315. package/dist/internal/session/cipher.js.map +1 -1
  316. package/dist/internal/session/factory.d.ts +5 -5
  317. package/dist/internal/session/factory.d.ts.map +1 -1
  318. package/dist/internal/session/factory.js +4 -4
  319. package/dist/internal/session/factory.js.map +1 -1
  320. package/dist/internal/session/handshake.d.ts +14 -16
  321. package/dist/internal/session/handshake.d.ts.map +1 -1
  322. package/dist/internal/session/handshake.js +18 -75
  323. package/dist/internal/session/handshake.js.map +1 -1
  324. package/dist/internal/session/index.d.ts +1 -1
  325. package/dist/internal/session/index.js +1 -1
  326. package/dist/internal/session/record-locks.d.ts +17 -6
  327. package/dist/internal/session/record-locks.d.ts.map +1 -1
  328. package/dist/internal/session/record-locks.js +43 -7
  329. package/dist/internal/session/record-locks.js.map +1 -1
  330. package/dist/internal/session/session-resolver.d.ts +1 -1
  331. package/dist/internal/session/session-resolver.js +1 -1
  332. package/dist/internal/session/state.d.ts +2 -2
  333. package/dist/internal/session/state.js +1 -1
  334. package/dist/internal/session/types.d.ts +5 -13
  335. package/dist/internal/session/types.d.ts.map +1 -1
  336. package/dist/internal/session/types.js +1 -1
  337. package/dist/keys/generation.d.ts +2 -2
  338. package/dist/keys/generation.js +2 -2
  339. package/dist/keys/types.d.ts +5 -5
  340. package/dist/keys/types.d.ts.map +1 -1
  341. package/dist/keys/types.js +1 -1
  342. package/dist/layers.d.ts +1 -1
  343. package/dist/layers.js +2 -2
  344. package/dist/layers.js.map +1 -1
  345. package/dist/local/store/expo/adapter.d.ts.map +1 -1
  346. package/dist/local/store/expo/adapter.js +12 -8
  347. package/dist/local/store/expo/adapter.js.map +1 -1
  348. package/dist/local/store/key-value/adapter.d.ts +76 -10
  349. package/dist/local/store/key-value/adapter.d.ts.map +1 -1
  350. package/dist/local/store/key-value/adapter.js +367 -61
  351. package/dist/local/store/key-value/adapter.js.map +1 -1
  352. package/dist/local/store/key-value/backend-conformance.d.ts.map +1 -1
  353. package/dist/local/store/key-value/backend-conformance.js +74 -0
  354. package/dist/local/store/key-value/backend-conformance.js.map +1 -1
  355. package/dist/local/store/key-value/batch.d.ts +2 -0
  356. package/dist/local/store/key-value/batch.d.ts.map +1 -1
  357. package/dist/local/store/key-value/batch.js +26 -0
  358. package/dist/local/store/key-value/batch.js.map +1 -1
  359. package/dist/local/store/key-value/receive-work.d.ts +55 -0
  360. package/dist/local/store/key-value/receive-work.d.ts.map +1 -0
  361. package/dist/local/store/key-value/receive-work.js +162 -0
  362. package/dist/local/store/key-value/receive-work.js.map +1 -0
  363. package/dist/local/store/key-value/send-log.d.ts +61 -0
  364. package/dist/local/store/key-value/send-log.d.ts.map +1 -0
  365. package/dist/local/store/key-value/send-log.js +430 -0
  366. package/dist/local/store/key-value/send-log.js.map +1 -0
  367. package/dist/local/store/key-value/sender-key-handoff.d.ts +68 -0
  368. package/dist/local/store/key-value/sender-key-handoff.d.ts.map +1 -0
  369. package/dist/local/store/key-value/sender-key-handoff.js +189 -0
  370. package/dist/local/store/key-value/sender-key-handoff.js.map +1 -0
  371. package/dist/local/store/key-value/storage.d.ts +15 -0
  372. package/dist/local/store/key-value/storage.d.ts.map +1 -1
  373. package/dist/local/store/keyed-index.d.ts +23 -0
  374. package/dist/local/store/keyed-index.d.ts.map +1 -0
  375. package/dist/local/store/keyed-index.js +41 -0
  376. package/dist/local/store/keyed-index.js.map +1 -0
  377. package/dist/local/store/memory/adapter.d.ts +44 -12
  378. package/dist/local/store/memory/adapter.d.ts.map +1 -1
  379. package/dist/local/store/memory/adapter.js +197 -46
  380. package/dist/local/store/memory/adapter.js.map +1 -1
  381. package/dist/local/store/memory/send-log.d.ts +42 -0
  382. package/dist/local/store/memory/send-log.d.ts.map +1 -0
  383. package/dist/local/store/memory/send-log.js +190 -0
  384. package/dist/local/store/memory/send-log.js.map +1 -0
  385. package/dist/local/store/node/adapter.d.ts.map +1 -1
  386. package/dist/local/store/node/adapter.js +8 -6
  387. package/dist/local/store/node/adapter.js.map +1 -1
  388. package/dist/local/store/react-native/adapter.d.ts.map +1 -1
  389. package/dist/local/store/react-native/adapter.js +12 -8
  390. package/dist/local/store/react-native/adapter.js.map +1 -1
  391. package/dist/local/store/receive-work.d.ts +87 -0
  392. package/dist/local/store/receive-work.d.ts.map +1 -0
  393. package/dist/local/store/receive-work.js +204 -0
  394. package/dist/local/store/receive-work.js.map +1 -0
  395. package/dist/local/store/received-content.d.ts +3 -3
  396. package/dist/local/store/received-content.d.ts.map +1 -1
  397. package/dist/local/store/received-content.js +2 -11
  398. package/dist/local/store/received-content.js.map +1 -1
  399. package/dist/local/store/reliability.d.ts +317 -18
  400. package/dist/local/store/reliability.d.ts.map +1 -1
  401. package/dist/local/store/reliability.js +371 -46
  402. package/dist/local/store/reliability.js.map +1 -1
  403. package/dist/local/store/send-log-order.d.ts +15 -0
  404. package/dist/local/store/send-log-order.d.ts.map +1 -0
  405. package/dist/local/store/send-log-order.js +24 -0
  406. package/dist/local/store/send-log-order.js.map +1 -0
  407. package/dist/local/store/sender-key-handoff-index.d.ts +38 -0
  408. package/dist/local/store/sender-key-handoff-index.d.ts.map +1 -0
  409. package/dist/local/store/sender-key-handoff-index.js +101 -0
  410. package/dist/local/store/sender-key-handoff-index.js.map +1 -0
  411. package/dist/local/store/sender-key-handoff-table.d.ts +29 -0
  412. package/dist/local/store/sender-key-handoff-table.d.ts.map +1 -0
  413. package/dist/local/store/sender-key-handoff-table.js +97 -0
  414. package/dist/local/store/sender-key-handoff-table.js.map +1 -0
  415. package/dist/local/store/sqlite/driver.d.ts +9 -0
  416. package/dist/local/store/sqlite/driver.d.ts.map +1 -1
  417. package/dist/local/store/sqlite/encryption.d.ts +8 -1
  418. package/dist/local/store/sqlite/encryption.d.ts.map +1 -1
  419. package/dist/local/store/sqlite/encryption.js +9 -0
  420. package/dist/local/store/sqlite/encryption.js.map +1 -1
  421. package/dist/local/store/sqlite/key-storage/index.d.ts +25 -4
  422. package/dist/local/store/sqlite/key-storage/index.d.ts.map +1 -1
  423. package/dist/local/store/sqlite/key-storage/index.js +68 -7
  424. package/dist/local/store/sqlite/key-storage/index.js.map +1 -1
  425. package/dist/local/store/sqlite/key-storage/message-records.d.ts +16 -4
  426. package/dist/local/store/sqlite/key-storage/message-records.d.ts.map +1 -1
  427. package/dist/local/store/sqlite/key-storage/message-records.js +49 -6
  428. package/dist/local/store/sqlite/key-storage/message-records.js.map +1 -1
  429. package/dist/local/store/sqlite/key-storage/metadata.d.ts +8 -7
  430. package/dist/local/store/sqlite/key-storage/metadata.d.ts.map +1 -1
  431. package/dist/local/store/sqlite/key-storage/metadata.js +17 -15
  432. package/dist/local/store/sqlite/key-storage/metadata.js.map +1 -1
  433. package/dist/local/store/sqlite/key-storage/receive-work.d.ts +27 -0
  434. package/dist/local/store/sqlite/key-storage/receive-work.d.ts.map +1 -0
  435. package/dist/local/store/sqlite/key-storage/receive-work.js +98 -0
  436. package/dist/local/store/sqlite/key-storage/receive-work.js.map +1 -0
  437. package/dist/local/store/sqlite/key-storage/send-log.d.ts +27 -0
  438. package/dist/local/store/sqlite/key-storage/send-log.d.ts.map +1 -0
  439. package/dist/local/store/sqlite/key-storage/send-log.js +178 -0
  440. package/dist/local/store/sqlite/key-storage/send-log.js.map +1 -0
  441. package/dist/local/store/sqlite/key-storage/sender-key-handoff.d.ts +27 -0
  442. package/dist/local/store/sqlite/key-storage/sender-key-handoff.d.ts.map +1 -0
  443. package/dist/local/store/sqlite/key-storage/sender-key-handoff.js +75 -0
  444. package/dist/local/store/sqlite/key-storage/sender-key-handoff.js.map +1 -0
  445. package/dist/local/store/sqlite/key-storage/session-trust.d.ts +2 -2
  446. package/dist/local/store/sqlite/key-storage/session-trust.js +5 -5
  447. package/dist/local/store/sqlite/key-storage/session-trust.js.map +1 -1
  448. package/dist/local/store/sqlite/migrations.d.ts.map +1 -1
  449. package/dist/local/store/sqlite/migrations.js +101 -0
  450. package/dist/local/store/sqlite/migrations.js.map +1 -1
  451. package/dist/local/store/sqlite/models/index.d.ts +2 -1
  452. package/dist/local/store/sqlite/models/index.d.ts.map +1 -1
  453. package/dist/local/store/sqlite/models/index.js +5 -1
  454. package/dist/local/store/sqlite/models/index.js.map +1 -1
  455. package/dist/local/store/sqlite/models/message-record.d.ts +15 -0
  456. package/dist/local/store/sqlite/models/message-record.d.ts.map +1 -1
  457. package/dist/local/store/sqlite/models/message-record.js +24 -0
  458. package/dist/local/store/sqlite/models/message-record.js.map +1 -1
  459. package/dist/local/store/sqlite/models/send-log.d.ts +51 -0
  460. package/dist/local/store/sqlite/models/send-log.d.ts.map +1 -0
  461. package/dist/local/store/sqlite/models/send-log.js +206 -0
  462. package/dist/local/store/sqlite/models/send-log.js.map +1 -0
  463. package/dist/local/store/sqlite/open-store.d.ts +9 -1
  464. package/dist/local/store/sqlite/open-store.d.ts.map +1 -1
  465. package/dist/local/store/sqlite/open-store.js +20 -1
  466. package/dist/local/store/sqlite/open-store.js.map +1 -1
  467. package/dist/local/store/sqlite/schema.d.ts +34 -1
  468. package/dist/local/store/sqlite/schema.d.ts.map +1 -1
  469. package/dist/local/store/sqlite/schema.js +14 -0
  470. package/dist/local/store/sqlite/schema.js.map +1 -1
  471. package/dist/local/store/sqlite/store.d.ts +61 -11
  472. package/dist/local/store/sqlite/store.d.ts.map +1 -1
  473. package/dist/local/store/sqlite/store.js +191 -27
  474. package/dist/local/store/sqlite/store.js.map +1 -1
  475. package/dist/local/store/sqlite/web/sqlite3mc/sqlite3.mjs +2 -1
  476. package/dist/local/store/sqlite/web/worker.d.ts +8 -0
  477. package/dist/local/store/sqlite/web/worker.d.ts.map +1 -1
  478. package/dist/local/store/sqlite/web/worker.js +64 -2
  479. package/dist/local/store/sqlite/web/worker.js.map +1 -1
  480. package/dist/local/store/web/adapter.d.ts +76 -8
  481. package/dist/local/store/web/adapter.d.ts.map +1 -1
  482. package/dist/local/store/web/adapter.js +579 -127
  483. package/dist/local/store/web/adapter.js.map +1 -1
  484. package/dist/local/store/web/receive-work.d.ts +57 -0
  485. package/dist/local/store/web/receive-work.d.ts.map +1 -0
  486. package/dist/local/store/web/receive-work.js +99 -0
  487. package/dist/local/store/web/receive-work.js.map +1 -0
  488. package/dist/local/store/web/send-log.d.ts +92 -0
  489. package/dist/local/store/web/send-log.d.ts.map +1 -0
  490. package/dist/local/store/web/send-log.js +267 -0
  491. package/dist/local/store/web/send-log.js.map +1 -0
  492. package/dist/local/store/web/sender-key-handoff.d.ts +81 -0
  493. package/dist/local/store/web/sender-key-handoff.d.ts.map +1 -0
  494. package/dist/local/store/web/sender-key-handoff.js +156 -0
  495. package/dist/local/store/web/sender-key-handoff.js.map +1 -0
  496. package/dist/local/store/web-sqlite/adapter.d.ts +2 -0
  497. package/dist/local/store/web-sqlite/adapter.d.ts.map +1 -1
  498. package/dist/local/store/web-sqlite/adapter.js +12 -6
  499. package/dist/local/store/web-sqlite/adapter.js.map +1 -1
  500. package/dist/local/vault/custody.d.ts +8 -0
  501. package/dist/local/vault/custody.d.ts.map +1 -0
  502. package/dist/local/vault/custody.js +9 -0
  503. package/dist/local/vault/custody.js.map +1 -0
  504. package/dist/local/vault/electron-safe-storage.d.ts +3 -1
  505. package/dist/local/vault/electron-safe-storage.d.ts.map +1 -1
  506. package/dist/local/vault/electron-safe-storage.js +2 -0
  507. package/dist/local/vault/electron-safe-storage.js.map +1 -1
  508. package/dist/local/vault/expo-secure-store.d.ts +3 -1
  509. package/dist/local/vault/expo-secure-store.d.ts.map +1 -1
  510. package/dist/local/vault/expo-secure-store.js +2 -0
  511. package/dist/local/vault/expo-secure-store.js.map +1 -1
  512. package/dist/local/vault/indexed-db.d.ts +3 -1
  513. package/dist/local/vault/indexed-db.d.ts.map +1 -1
  514. package/dist/local/vault/indexed-db.js +2 -0
  515. package/dist/local/vault/indexed-db.js.map +1 -1
  516. package/dist/local/vault/react-native-keychain.d.ts +3 -1
  517. package/dist/local/vault/react-native-keychain.d.ts.map +1 -1
  518. package/dist/local/vault/react-native-keychain.js +2 -0
  519. package/dist/local/vault/react-native-keychain.js.map +1 -1
  520. package/dist/remote/relay/device-set.d.ts +94 -0
  521. package/dist/remote/relay/device-set.d.ts.map +1 -0
  522. package/dist/remote/relay/device-set.js +156 -0
  523. package/dist/remote/relay/device-set.js.map +1 -0
  524. package/dist/remote/relay/types.d.ts +92 -107
  525. package/dist/remote/relay/types.d.ts.map +1 -1
  526. package/dist/safety/core.d.ts +15 -6
  527. package/dist/safety/core.d.ts.map +1 -1
  528. package/dist/safety/core.js +41 -27
  529. package/dist/safety/core.js.map +1 -1
  530. package/dist/safety/index.d.ts +2 -1
  531. package/dist/safety/index.d.ts.map +1 -1
  532. package/dist/safety/index.js +12 -18
  533. package/dist/safety/index.js.map +1 -1
  534. package/dist/types/api.d.ts +707 -62
  535. package/dist/types/api.d.ts.map +1 -1
  536. package/dist/types/errors.d.ts +14 -33
  537. package/dist/types/errors.d.ts.map +1 -1
  538. package/dist/types/errors.js +14 -24
  539. package/dist/types/errors.js.map +1 -1
  540. package/dist/types/messages.d.ts +2 -2
  541. package/dist/types/messages.js +1 -1
  542. package/dist/types/protocol-config.d.ts +175 -42
  543. package/dist/types/protocol-config.d.ts.map +1 -1
  544. package/dist/types/protocol-config.js +19 -38
  545. package/dist/types/protocol-config.js.map +1 -1
  546. package/dist/types/protocol-constants.d.ts +6 -6
  547. package/dist/types/protocol-constants.js +6 -6
  548. package/dist/types/session.d.ts +7 -7
  549. package/dist/types/session.d.ts.map +1 -1
  550. package/dist/types/session.js +39 -21
  551. package/dist/types/session.js.map +1 -1
  552. package/dist/username/link.d.ts +2 -2
  553. package/dist/username/link.js +2 -2
  554. package/dist/utils/origin-lock.d.ts +45 -0
  555. package/dist/utils/origin-lock.d.ts.map +1 -0
  556. package/dist/utils/origin-lock.js +80 -0
  557. package/dist/utils/origin-lock.js.map +1 -0
  558. package/docs/ASSURANCE.md +6 -6
  559. package/docs/CLIENT_COMPOSITION.md +2 -2
  560. package/docs/DEVIATIONS.md +677 -182
  561. package/docs/E2EE.md +1 -1
  562. package/docs/ERROR_HANDLING.md +165 -2
  563. package/docs/GETTING_STARTED.md +7 -4
  564. package/docs/INTERFACES.md +1 -1
  565. package/docs/PACKAGE_SURFACE.md +6 -5
  566. package/docs/PROTOCOL_POLICY.md +102 -22
  567. package/docs/RECIPES.md +3 -15
  568. package/docs/SECURITY.md +90 -5
  569. package/keys/README.md +3 -3
  570. package/local/store/README.md +78 -14
  571. package/local/store/key-value/README.md +14 -3
  572. package/local/store/node/README.md +8 -3
  573. package/local/store/web/README.md +9 -0
  574. package/local/store/web-sqlite/README.md +14 -2
  575. package/local/vault/README.md +21 -0
  576. package/package.json +1 -6
  577. package/remote/README.md +0 -1
  578. package/remote/relay/README.md +2 -2
  579. package/dist/device/provisioning.d.ts +0 -242
  580. package/dist/device/provisioning.d.ts.map +0 -1
  581. package/dist/device/provisioning.js +0 -421
  582. package/dist/device/provisioning.js.map +0 -1
  583. package/dist/internal/protocol/x3dh/index.d.ts +0 -15
  584. package/dist/internal/protocol/x3dh/index.d.ts.map +0 -1
  585. package/dist/internal/protocol/x3dh/index.js +0 -13
  586. package/dist/internal/protocol/x3dh/index.js.map +0 -1
  587. package/dist/internal/protocol/x3dh/x3dh.d.ts +0 -131
  588. package/dist/internal/protocol/x3dh/x3dh.d.ts.map +0 -1
  589. package/dist/internal/protocol/x3dh/x3dh.js +0 -261
  590. package/dist/internal/protocol/x3dh/x3dh.js.map +0 -1
@@ -18,7 +18,7 @@
18
18
  import { utf8Decode } from '../platform/index.js';
19
19
  import { defaultSignalProtocolLogger } from '../../logger.js';
20
20
  import { getErrorMessage } from '../../utils/errors.js';
21
- import { RetryReason, DEFAULT_SESAME_CONFIG, SesameError, SesameDeviceNotFoundError, NoActiveSessionError, DecryptionFailedError, StaleDeviceListError, MIN_DEVICE_ID, MAX_DEVICE_ID,
21
+ import { RetryReason, DEFAULT_SESAME_CONFIG, SesameError, SesameDeviceNotFoundError, NoActiveSessionError, DecryptionFailedError, StaleDeviceListError, MIN_DEVICE_ID, MAX_DEVICE_ID, isRemovedDevice, isExpiredRemovedDevice,
22
22
  // Session record helpers
23
23
  updateSessionRecordAfterSend, updateSessionRecordAfterReceive, } from './types.js';
24
24
  import { validateSesameConfig } from './validation.js';
@@ -93,6 +93,7 @@ export class DefaultSesameManager {
93
93
  * Per SESAME Specification Section 6.2.
94
94
  */
95
95
  messageRecordStore = null;
96
+ messageRecordPlaintext = (plaintext) => plaintext;
96
97
  /**
97
98
  * O(1) session lookup index: sessionId -> { userId, deviceId }
98
99
  *
@@ -145,8 +146,9 @@ export class DefaultSesameManager {
145
146
  *
146
147
  * When set, sent messages are stored so they can be resent on retry request.
147
148
  */
148
- setMessageRecordStore(store) {
149
+ setMessageRecordStore(store, recordPlaintext = (plaintext) => plaintext) {
149
150
  this.messageRecordStore = store;
151
+ this.messageRecordPlaintext = recordPlaintext;
150
152
  }
151
153
  /**
152
154
  * Set event callbacks for session lifecycle events.
@@ -246,13 +248,19 @@ export class DefaultSesameManager {
246
248
  this.events.onDeviceAdded?.(userId, deviceId);
247
249
  }
248
250
  /**
249
- * Remove a device from a user. The removal deletes the device's session, so
250
- * it waits for a session write that is in progress for the device.
251
+ * Remove a device from a user. The removal archives the current session of
252
+ * the device, marks the record removed, and keeps the record, so a message
253
+ * that the device sent earlier still decrypts. The removal writes the
254
+ * session, so it waits for a session write that is in progress for the
255
+ * device. A device that is already removed stays as it is.
251
256
  */
252
257
  async removeDevice(userId, deviceId) {
253
258
  await this.withDeviceLocks(userId, deviceId, () => this.removeDeviceLocked(userId, deviceId));
254
259
  }
255
260
  async removeDeviceLocked(userId, deviceId) {
261
+ const deviceRecord = await this.storage.getDeviceRecord(userId, deviceId);
262
+ if (!deviceRecord || isRemovedDevice(deviceRecord))
263
+ return;
256
264
  this.logger.debug('SESAME: Removing device', {
257
265
  category: 'E2EE',
258
266
  data: {
@@ -261,23 +269,54 @@ export class DefaultSesameManager {
261
269
  deviceId,
262
270
  },
263
271
  });
264
- const userRecord = await this.storage.getUserRecord(userId);
265
- if (!userRecord)
266
- return;
267
- // Remove session index entries for this device
268
- this.removeDeviceFromSessionIndex(userId, deviceId);
269
- // Remove device
270
- userRecord.devices.delete(deviceId);
271
- userRecord.updatedAt = Date.now();
272
- // Delete device record from storage
273
- await this.storage.deleteDeviceRecord(userId, deviceId);
274
- // Update user record (or clean up if empty)
275
- if (userRecord.devices.size > 0) {
276
- await this.storage.setUserRecord(userId, userRecord);
277
- }
278
- // Emit device removed event
272
+ const now = Date.now();
273
+ // One Signal client archives the session of a device that the
274
+ // server no longer lists, and keeps the session for receive. The marker
275
+ // write carries the handoff invalidation with or without a session.
276
+ await this.storage.setDeviceRecord(userId, deviceId, {
277
+ ...deviceRecord,
278
+ session: this.archiveCurrentIndexed(userId, deviceId, deviceRecord.session),
279
+ removedAt: now,
280
+ updatedAt: now,
281
+ }, { senderKeyHandoffInvalidation: { userId, deviceId } });
279
282
  this.events.onDeviceRemoved?.(userId, deviceId);
280
283
  }
284
+ /**
285
+ * Return a removed device to the fan-out set after its device list names
286
+ * it again. The marker clears, and the device has no current session until
287
+ * a send starts one. The archived sessions stay. A device that is not
288
+ * removed stays as it is.
289
+ */
290
+ async relistDevice(userId, deviceId) {
291
+ await this.withUserLock(userId, async () => {
292
+ const deviceRecord = await this.storage.getDeviceRecord(userId, deviceId);
293
+ if (deviceRecord)
294
+ await this.relistDeviceLocked(deviceRecord);
295
+ });
296
+ }
297
+ /**
298
+ * Clear the removed marker of a record under the user lock. The write
299
+ * carries no session, so the stored session stays.
300
+ *
301
+ * @returns false when the record is not removed
302
+ */
303
+ async relistDeviceLocked(deviceRecord) {
304
+ if (!isRemovedDevice(deviceRecord))
305
+ return false;
306
+ const { userId, deviceId } = deviceRecord;
307
+ const { removedAt: _removedAt, ...listed } = deviceRecord;
308
+ await this.storage.setDeviceRecord(userId, deviceId, {
309
+ ...listed,
310
+ session: null,
311
+ updatedAt: Date.now(),
312
+ });
313
+ this.logger.debug('SESAME: Relisted device', {
314
+ category: 'E2EE',
315
+ data: { operation: 'sesame-relist-device', userId, deviceId },
316
+ });
317
+ this.events.onDeviceAdded?.(userId, deviceId);
318
+ return true;
319
+ }
281
320
  /**
282
321
  * Clear pending verification flag for a device after user verifies safety numbers.
283
322
  * This unblocks messaging to the device.
@@ -327,6 +366,14 @@ export class DefaultSesameManager {
327
366
  const sessionRecord = await this.getSession(userId, deviceId);
328
367
  return sessionRecord?.currentSession ?? null;
329
368
  }
369
+ /**
370
+ * Get the registration ID of the remote device in the current session.
371
+ * Returns undefined if no current session exists.
372
+ */
373
+ async getRemoteRegistrationId(userId, deviceId) {
374
+ const session = await this.getActiveSession(userId, deviceId);
375
+ return session?.remoteRegistrationId;
376
+ }
330
377
  /**
331
378
  * Set the session record for a device (internal helper).
332
379
  * Uses SessionRecord.archiveCurrent() to move the previous active session to archived.
@@ -493,15 +540,21 @@ export class DefaultSesameManager {
493
540
  const clientTimestamp = options?.clientTimestamp ?? Date.now();
494
541
  // Store MessageRecord for retry request support (SESAME spec §6.2)
495
542
  // "Each MessageRecord stores the plaintext of the encrypted message"
496
- if (this.messageRecordStore) {
543
+ // The policy can keep less than the sent plaintext, or no record.
544
+ const recordPlaintext = this.messageRecordStore
545
+ ? this.messageRecordPlaintext(plaintextString)
546
+ : null;
547
+ if (this.messageRecordStore && recordPlaintext !== null) {
497
548
  try {
498
549
  await this.messageRecordStore.storeMessageRecord({
499
550
  sessionId,
500
551
  timestamp: clientTimestamp,
501
552
  recipientUserId,
502
553
  recipientDeviceId,
503
- plaintext: plaintextString,
504
- createdAt: clientTimestamp,
554
+ plaintext: recordPlaintext,
555
+ // The store sets createdAt from its clock at the first write.
556
+ // Retention reads it, so the client timestamp never moves it.
557
+ createdAt: Date.now(),
505
558
  // store sender's
506
559
  // current ratchet key (DHs) for retry session matching. Detects intra-session
507
560
  // ratchet advancement, not just cross-session changes.
@@ -687,10 +740,14 @@ export class DefaultSesameManager {
687
740
  if (!localUserRecord) {
688
741
  return [];
689
742
  }
743
+ const { listedDeviceIds, ...sendOptions } = options;
690
744
  const syncMessages = [];
691
- for (const [deviceId, deviceRecord] of localUserRecord.devices.entries()) {
745
+ for (const deviceId of new Set(listedDeviceIds)) {
692
746
  if (deviceId === this.localDeviceId)
693
747
  continue;
748
+ const deviceRecord = localUserRecord.devices.get(deviceId);
749
+ if (!deviceRecord || isRemovedDevice(deviceRecord))
750
+ continue;
694
751
  try {
695
752
  const activeSession = deviceRecord.session?.currentSession;
696
753
  if (!activeSession)
@@ -699,7 +756,7 @@ export class DefaultSesameManager {
699
756
  const canSend = await this.canSendOnSession(sessionId);
700
757
  if (!canSend)
701
758
  continue;
702
- const syncMessage = await this.sendMessage(this.localUserId, deviceId, plaintext, options);
759
+ const syncMessage = await this.sendMessage(this.localUserId, deviceId, plaintext, sendOptions);
703
760
  syncMessages.push(syncMessage);
704
761
  }
705
762
  catch {
@@ -741,7 +798,10 @@ export class DefaultSesameManager {
741
798
  if (!userRecord) {
742
799
  throw new SesameError(`No user record found for ${recipientUserId}`, 'USER_NOT_FOUND');
743
800
  }
744
- if (userRecord.devices.size === 0) {
801
+ // A removed device gets no fan-out. A listed device with no current
802
+ // session is still attempted, and fails with NoActiveSessionError.
803
+ const devices = Array.from(userRecord.devices.entries()).filter(([, deviceRecord]) => !isRemovedDevice(deviceRecord));
804
+ if (devices.length === 0) {
745
805
  throw new SesameError(`User ${recipientUserId} has no devices`, 'NO_DEVICES');
746
806
  }
747
807
  const timestamp = Date.now();
@@ -754,12 +814,12 @@ export class DefaultSesameManager {
754
814
  data: {
755
815
  operation: 'sesame-send-user',
756
816
  recipientUserId,
757
- deviceCount: userRecord.devices.size,
817
+ deviceCount: devices.length,
758
818
  includeSyncMessages,
759
819
  },
760
820
  });
761
821
  // Phase 1 & 2: Encrypt message for each recipient device
762
- for (const [deviceId, deviceRecord] of userRecord.devices.entries()) {
822
+ for (const [deviceId, deviceRecord] of devices) {
763
823
  attemptedDevices++;
764
824
  try {
765
825
  // Check if device has an active session
@@ -807,16 +867,26 @@ export class DefaultSesameManager {
807
867
  if (deviceMessages.length === 0 && failures.length > 0) {
808
868
  throw new SesameError(`Failed to send message to all ${failures.length} devices: ${failures.map((f) => `${f.deviceId}: ${f.error.message}`).join(', ')}`, 'ALL_DEVICES_FAILED');
809
869
  }
810
- // Encrypt sync messages for our own other devices (multi-device sync)
870
+ // Encrypt sync messages for our own other devices (multi-device sync).
871
+ // This batch reads no device list, so the copies go to the known devices.
811
872
  if (includeSyncMessages) {
873
+ const knownDeviceIds = [];
812
874
  if (this.localUserId) {
813
875
  const localUserRecord = await this.getUserRecord(this.localUserId);
814
876
  if (localUserRecord) {
815
- attemptedDevices += Array.from(localUserRecord.devices.keys()).filter((deviceId) => deviceId !== this.localDeviceId).length;
877
+ for (const [deviceId, deviceRecord] of localUserRecord.devices) {
878
+ if (deviceId !== this.localDeviceId && !isRemovedDevice(deviceRecord)) {
879
+ knownDeviceIds.push(deviceId);
880
+ }
881
+ }
882
+ attemptedDevices += knownDeviceIds.length;
816
883
  }
817
884
  }
818
885
  const syncPayload = options?.syncPlaintext ?? plaintext;
819
- const localSyncMessages = await this.sendToLocalOtherDevices(syncPayload, options);
886
+ const localSyncMessages = await this.sendToLocalOtherDevices(syncPayload, {
887
+ ...options,
888
+ listedDeviceIds: knownDeviceIds,
889
+ });
820
890
  syncMessages.push(...localSyncMessages);
821
891
  }
822
892
  // Log partial failures
@@ -853,7 +923,7 @@ export class DefaultSesameManager {
853
923
  *
854
924
  * @see https://signal.org/docs/specifications/sesame/
855
925
  */
856
- async receive(message, receiveId) {
926
+ async receive(message, receiveWork) {
857
927
  if (!this.localUserId || !this.localDeviceId) {
858
928
  throw new SesameError('DefaultSesameManager not initialized', 'NOT_INITIALIZED');
859
929
  }
@@ -885,7 +955,7 @@ export class DefaultSesameManager {
885
955
  // session establishment for first messages from new senders
886
956
  if (candidates.length === 0) {
887
957
  try {
888
- const result = await this.handlePreKeyMessage(message, senderAddress, ciphertext, receiveId);
958
+ const result = await this.handlePreKeyMessage(message, senderAddress, ciphertext, receiveWork);
889
959
  // TRANSACTIONAL: Only persist after successful decrypt
890
960
  // This prevents session state corruption if decryption fails
891
961
  await this.persistPreKeyResult(senderAddress, result);
@@ -922,7 +992,7 @@ export class DefaultSesameManager {
922
992
  let successfulCandidate = null;
923
993
  for (const candidate of candidates) {
924
994
  try {
925
- const plaintextString = await this.protocol.decrypt(senderAddress, ciphertext, receiveId);
995
+ const plaintextString = await this.protocol.decrypt(senderAddress, ciphertext, receiveWork);
926
996
  decryptedPlaintext = new TextEncoder().encode(plaintextString);
927
997
  successfulCandidate = candidate;
928
998
  break;
@@ -981,7 +1051,7 @@ export class DefaultSesameManager {
981
1051
  },
982
1052
  });
983
1053
  try {
984
- const result = await this.handlePreKeyMessage(message, senderAddress, ciphertext, receiveId);
1054
+ const result = await this.handlePreKeyMessage(message, senderAddress, ciphertext, receiveWork);
985
1055
  // TRANSACTIONAL: Only persist after successful decrypt
986
1056
  // This prevents session state corruption if decryption fails
987
1057
  await this.persistPreKeyResult(senderAddress, result);
@@ -1016,7 +1086,7 @@ export class DefaultSesameManager {
1016
1086
  /**
1017
1087
  * Create a retry request for a failed message
1018
1088
  */
1019
- async createRetryRequest(failedMessage, reason) {
1089
+ async createRetryRequest(failedMessage, reason, options) {
1020
1090
  if (!this.config.enableRetryRequests) {
1021
1091
  throw new SesameError('Retry requests are disabled in configuration', 'RETRY_DISABLED');
1022
1092
  }
@@ -1028,7 +1098,11 @@ export class DefaultSesameManager {
1028
1098
  if (failedMessage.timestamp === undefined) {
1029
1099
  throw new SesameError('Cannot create retry request: message has no timestamp', 'MISSING_TIMESTAMP');
1030
1100
  }
1031
- const ratchetKey = this.extractRetryRatchetKey(failedMessage.ciphertext);
1101
+ // A sender-key ciphertext has no ratchet key, and its bytes can parse as
1102
+ // one. A request without a ratchet key names a sender-key failure.
1103
+ const ratchetKey = options?.senderKey
1104
+ ? undefined
1105
+ : this.extractRetryRatchetKey(failedMessage.ciphertext);
1032
1106
  const retryRequest = {
1033
1107
  requesterUserId: this.localUserId,
1034
1108
  requesterDeviceId: this.localDeviceId,
@@ -1123,7 +1197,7 @@ export class DefaultSesameManager {
1123
1197
  if (retryRequest.reason === RetryReason.DECRYPTION_FAILED) {
1124
1198
  const activeSession = await this.getActiveSession(requesterUserId, requesterDeviceId);
1125
1199
  const currentRatchetKey = activeSession?.DHs?.publicKey;
1126
- // Reference implementation behavior (Android/iOS): only reset session on retry if the ratchet
1200
+ // Two Signal clients reset a session on retry only if the ratchet
1127
1201
  // key from DecryptionErrorMessage matches the current active session.
1128
1202
  if (!retryRequest.ratchetKey ||
1129
1203
  !currentRatchetKey ||
@@ -1280,7 +1354,7 @@ export class DefaultSesameManager {
1280
1354
  * @returns Decrypted plaintext as Uint8Array
1281
1355
  * @throws DecryptionFailedError if decryption fails
1282
1356
  */
1283
- async handlePreKeyMessage(message, senderAddress, ciphertext, receiveId) {
1357
+ async handlePreKeyMessage(message, senderAddress, ciphertext, receiveWork) {
1284
1358
  this.logger.debug('SESAME: No existing sessions, trying PreKeyMessage handling', {
1285
1359
  category: 'E2EE',
1286
1360
  data: {
@@ -1295,7 +1369,7 @@ export class DefaultSesameManager {
1295
1369
  // brand-new session appear pre-existing and archive it into itself.
1296
1370
  const previousDeviceRecord = await this.getDeviceRecord(message.senderUserId, message.senderDeviceId);
1297
1371
  // Note: this.protocol is guaranteed non-null because receive() checks it before calling this method
1298
- const plaintextString = await this.protocol.decrypt(senderAddress, ciphertext, receiveId);
1372
+ const plaintextString = await this.protocol.decrypt(senderAddress, ciphertext, receiveWork);
1299
1373
  const decryptedPlaintext = new TextEncoder().encode(plaintextString);
1300
1374
  // Parse PreKeyMessage to get sender's identity key (binary protobuf format)
1301
1375
  // This is needed for both new senders AND existing senders (to detect identity key changes)
@@ -1402,7 +1476,12 @@ export class DefaultSesameManager {
1402
1476
  // Update session index for the newly established session
1403
1477
  this.removeDeviceFromSessionIndex(userId, deviceId);
1404
1478
  this.indexSessionRecord(userId, deviceId, deviceRecord.session);
1405
- await this.storage.setDeviceRecord(userId, deviceId, deviceRecord);
1479
+ // The PreKey session can come from a new install of the device, which
1480
+ // holds no own sender key. The same write deletes the handoff rows of
1481
+ // the device in every group.
1482
+ await this.storage.setDeviceRecord(userId, deviceId, deviceRecord, {
1483
+ senderKeyHandoffInvalidation: { userId, deviceId },
1484
+ });
1406
1485
  // A store joins a device that has no pin with the identity of its
1407
1486
  // session, and the decrypt committed that session. A device that had no
1408
1487
  // pin before the decrypt is a first-contact pin.
@@ -1509,6 +1588,19 @@ export class DefaultSesameManager {
1509
1588
  }
1510
1589
  });
1511
1590
  }
1591
+ /**
1592
+ * Archive the session of one device in the protocol store and in its
1593
+ * device record, so the next send to the device starts a new session.
1594
+ * Both writes run under one hold of the session lock of the device and
1595
+ * the user lock: `archiveProtocolSession` first, and then the device
1596
+ * record. A device record with no current session stays as it is.
1597
+ */
1598
+ async archiveSession(userId, deviceId, archiveProtocolSession) {
1599
+ await this.withDeviceLocks(userId, deviceId, async () => {
1600
+ await archiveProtocolSession();
1601
+ await this.archiveSessionForDeviceLocked(userId, deviceId);
1602
+ });
1603
+ }
1512
1604
  /**
1513
1605
  * Archive the active session for a specific device
1514
1606
  *
@@ -1518,6 +1610,20 @@ export class DefaultSesameManager {
1518
1610
  async archiveSessionForDevice(userId, deviceId) {
1519
1611
  await this.withDeviceLocks(userId, deviceId, () => this.archiveSessionForDeviceLocked(userId, deviceId));
1520
1612
  }
1613
+ /**
1614
+ * Move the current session of a device record to its archived sessions,
1615
+ * and index the result. The caller holds the device locks and writes the
1616
+ * returned session. A record with no current session is returned as it is.
1617
+ */
1618
+ archiveCurrentIndexed(userId, deviceId, session) {
1619
+ if (!session?.currentSession)
1620
+ return session;
1621
+ const archived = SessionResolver.archiveCurrentSession(session, this.config.maxInactiveSessions);
1622
+ // The session moved from current to archived.
1623
+ this.removeDeviceFromSessionIndex(userId, deviceId);
1624
+ this.indexSessionRecord(userId, deviceId, archived);
1625
+ return archived;
1626
+ }
1521
1627
  async archiveSessionForDeviceLocked(userId, deviceId) {
1522
1628
  const deviceRecord = await this.storage.getDeviceRecord(userId, deviceId);
1523
1629
  if (!deviceRecord?.session?.currentSession) {
@@ -1525,12 +1631,13 @@ export class DefaultSesameManager {
1525
1631
  return;
1526
1632
  }
1527
1633
  const sessionId = ProtocolAddress.toString(deviceRecord.session.currentSession.remoteAddress);
1528
- // Archive the current session and clear it using SessionResolver
1529
- deviceRecord.session = SessionResolver.archiveCurrentSession(deviceRecord.session, this.config.maxInactiveSessions);
1530
- // Rebuild session index for this device (session moved from current to archived)
1531
- this.removeDeviceFromSessionIndex(userId, deviceId);
1532
- this.indexSessionRecord(userId, deviceId, deviceRecord.session);
1533
- await this.storage.setDeviceRecord(userId, deviceId, deviceRecord);
1634
+ deviceRecord.session = this.archiveCurrentIndexed(userId, deviceId, deviceRecord.session);
1635
+ // An archive follows a sign that the device lost its session, so it can
1636
+ // also have lost the own sender keys. The same write deletes the handoff
1637
+ // rows of the device in every group.
1638
+ await this.storage.setDeviceRecord(userId, deviceId, deviceRecord, {
1639
+ senderKeyHandoffInvalidation: { userId, deviceId },
1640
+ });
1534
1641
  this.logger.debug('SESAME: Session archived', {
1535
1642
  category: 'E2EE',
1536
1643
  data: {
@@ -1547,12 +1654,17 @@ export class DefaultSesameManager {
1547
1654
  * Retry metadata includes the sender ratchet key for one-to-one
1548
1655
  * SignalProtocolMessage/PreKeySignalProtocolMessage failures and omits it for
1549
1656
  * sender-key messages.
1657
+ *
1658
+ * The failed message holds the stored wire form of the ciphertext: the
1659
+ * UTF-8 bytes of the base64 text of the framed message. A sealed or
1660
+ * malformed ciphertext gives no key.
1550
1661
  */
1551
- extractRetryRatchetKey(ciphertext) {
1552
- if (ciphertext.length < 2) {
1662
+ extractRetryRatchetKey(wire) {
1663
+ if (wire.length < 2) {
1553
1664
  return undefined;
1554
1665
  }
1555
1666
  try {
1667
+ const ciphertext = base64ToBytes(utf8Decode(wire));
1556
1668
  let signalMessageProtobuf;
1557
1669
  const firstTag = ciphertext[1];
1558
1670
  const isPreKey = firstTag === 0x08 || firstTag === 0x12;
@@ -1688,7 +1800,9 @@ export class DefaultSesameManager {
1688
1800
  return count;
1689
1801
  }
1690
1802
  /**
1691
- * Clean up stale device records with no sessions and age > maxLatency
1803
+ * Delete the stale device records: a record with no session that is older
1804
+ * than maxLatency, and a removed record whose removal is older than
1805
+ * maxLatency. The client runs it at start, after the receive queue resume.
1692
1806
  */
1693
1807
  async cleanupStaleRecords() {
1694
1808
  if (!this.config.enableStaleRecordCleanup) {
@@ -1746,32 +1860,45 @@ export class DefaultSesameManager {
1746
1860
  }
1747
1861
  return null;
1748
1862
  }
1749
- // Devices that no longer exist on server
1750
- const replaced = Array.from(userRecord.devices.keys()).filter((deviceId) => !devices.has(deviceId));
1863
+ // Devices that no longer exist on server. A device that is already
1864
+ // removed stays as it is.
1865
+ const replaced = Array.from(userRecord.devices.entries())
1866
+ .filter(([deviceId, device]) => !devices.has(deviceId) && !isRemovedDevice(device))
1867
+ .map(([deviceId]) => deviceId);
1751
1868
  // Add or update devices from server
1752
1869
  for (const [deviceId, identityKey] of devices) {
1753
1870
  const existingDevice = await this.getDeviceRecord(userId, deviceId);
1754
1871
  if (!existingDevice) {
1755
1872
  await this.addDeviceLocked(userId, deviceId, identityKey);
1873
+ continue;
1756
1874
  }
1757
- else if (identityKey.length > 0) {
1758
- // Only a device list that actually carries an identity key can tell us
1759
- // the identity changed. A response that omits it carries no
1760
- // information about identity, and must not be read as a change.
1761
- // Reading it as a change would drop every live session and raise a
1762
- // security event on each sync.
1763
- const comparison = compareDeviceIdentityKeys(existingDevice.identityKey, identityKey);
1764
- if (comparison === 'unpinned') {
1765
- // Device was tracked before its identity was known - TOFU-pin it.
1766
- existingDevice.identityKey = identityKey;
1767
- await this.storage.setDeviceRecord(userId, deviceId, {
1768
- ...existingDevice,
1769
- session: null,
1770
- });
1771
- }
1772
- if (comparison === 'changed') {
1773
- replaced.push(deviceId);
1774
- }
1875
+ // Only a device list that actually carries an identity key can tell us
1876
+ // the identity changed. A response that omits it carries no
1877
+ // information about identity, and must not be read as a change.
1878
+ // Reading it as a change would drop every live session and raise a
1879
+ // security event on each sync.
1880
+ const comparison = identityKey.length > 0
1881
+ ? compareDeviceIdentityKeys(existingDevice.identityKey, identityKey)
1882
+ : undefined;
1883
+ if (comparison === 'changed') {
1884
+ // The replacement also clears a removed marker.
1885
+ replaced.push(deviceId);
1886
+ }
1887
+ else if (comparison === 'unpinned') {
1888
+ // Device was tracked before its identity was known - TOFU-pin it.
1889
+ // The same write clears a removed marker.
1890
+ const { removedAt: _removedAt, ...listed } = existingDevice;
1891
+ await this.storage.setDeviceRecord(userId, deviceId, {
1892
+ ...listed,
1893
+ identityKey,
1894
+ session: null,
1895
+ ...(isRemovedDevice(existingDevice) && { updatedAt: Date.now() }),
1896
+ });
1897
+ if (isRemovedDevice(existingDevice))
1898
+ this.events.onDeviceAdded?.(userId, deviceId);
1899
+ }
1900
+ else {
1901
+ await this.relistDeviceLocked(existingDevice);
1775
1902
  }
1776
1903
  }
1777
1904
  return replaced;
@@ -1802,10 +1929,15 @@ export class DefaultSesameManager {
1802
1929
  }
1803
1930
  }
1804
1931
  /**
1805
- * Remove a device that the device list no longer lists, or replace a
1806
- * device whose listed identity key changed. Another write can have changed
1807
- * the device since the list was compared under the user lock, so the
1808
- * decision is made again here.
1932
+ * Remove a device that the device list no longer lists, or replace the
1933
+ * identity of a device whose listed identity key changed. Another write can
1934
+ * have changed the device since the list was compared under the user lock,
1935
+ * so the decision is made again here.
1936
+ *
1937
+ * The identity change archives the current session, so a message under the
1938
+ * old session still decrypts. The record takes the new identity key and
1939
+ * pending verification, and it is not marked removed. A removed device is
1940
+ * relisted.
1809
1941
  *
1810
1942
  * @param identityKey - The listed identity key, or undefined when the list
1811
1943
  * does not list the device
@@ -1822,19 +1954,20 @@ export class DefaultSesameManager {
1822
1954
  if (compareDeviceIdentityKeys(existingDevice.identityKey, identityKey) !== 'changed') {
1823
1955
  return false;
1824
1956
  }
1825
- // Identity key changed - security event
1826
- // Remove old device and add new one (clears sessions)
1827
- await this.removeDeviceLocked(userId, deviceId);
1828
- await this.addDeviceLocked(userId, deviceId, identityKey);
1829
- // Mark device as pending verification to block messaging until user verifies
1830
- const newRecord = await this.getDeviceRecord(userId, deviceId);
1831
- if (newRecord) {
1832
- newRecord.pendingVerification = true;
1833
- await this.storage.setDeviceRecord(userId, deviceId, {
1834
- ...newRecord,
1835
- session: null,
1836
- });
1837
- }
1957
+ // Identity key changed - security event. Pending verification blocks
1958
+ // messaging until the user verifies. The handoff invalidation goes with
1959
+ // the archive, as for every other archive.
1960
+ const { removedAt: _removedAt, ...listed } = existingDevice;
1961
+ await this.storage.setDeviceRecord(userId, deviceId, {
1962
+ ...listed,
1963
+ identityKey,
1964
+ session: this.archiveCurrentIndexed(userId, deviceId, existingDevice.session),
1965
+ pendingVerification: true,
1966
+ updatedAt: Date.now(),
1967
+ }, { senderKeyHandoffInvalidation: { userId, deviceId } });
1968
+ // The replacement relists a removed device, as the other relist paths do.
1969
+ if (isRemovedDevice(existingDevice))
1970
+ this.events.onDeviceAdded?.(userId, deviceId);
1838
1971
  return true;
1839
1972
  }
1840
1973
  /**
@@ -1863,7 +1996,8 @@ export class DefaultSesameManager {
1863
1996
  : 0;
1864
1997
  const hasNoSessions = !hasCurrentSession && archivedSessionCount === 0;
1865
1998
  const isOld = now - deviceRecord.createdAt > this.config.maxLatency;
1866
- if (hasNoSessions && isOld) {
1999
+ if ((hasNoSessions && isOld) ||
2000
+ isExpiredRemovedDevice(deviceRecord, now, this.config.maxLatency)) {
1867
2001
  staleRecords++;
1868
2002
  }
1869
2003
  // Count active session and check if expired