@open-e2ee/signal-protocol-sdk 13.4.0 → 13.5.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 (171) hide show
  1. package/CHANGELOG.md +423 -0
  2. package/README.md +4 -8
  3. package/SECURITY.md +2 -2
  4. package/TROUBLESHOOTING.md +55 -27
  5. package/dist/client/binary-send.d.ts +28 -0
  6. package/dist/client/binary-send.d.ts.map +1 -0
  7. package/dist/client/binary-send.js +26 -0
  8. package/dist/client/binary-send.js.map +1 -0
  9. package/dist/client/client.d.ts +46 -6
  10. package/dist/client/client.d.ts.map +1 -1
  11. package/dist/client/client.js +181 -66
  12. package/dist/client/client.js.map +1 -1
  13. package/dist/client/content-adapter.d.ts +18 -0
  14. package/dist/client/content-adapter.d.ts.map +1 -1
  15. package/dist/client/content-adapter.js.map +1 -1
  16. package/dist/client/device-set-reconciliation.d.ts +23 -8
  17. package/dist/client/device-set-reconciliation.d.ts.map +1 -1
  18. package/dist/client/device-set-reconciliation.js +25 -11
  19. package/dist/client/device-set-reconciliation.js.map +1 -1
  20. package/dist/client/event-hooks.d.ts +88 -8
  21. package/dist/client/event-hooks.d.ts.map +1 -1
  22. package/dist/client/event-hooks.js +1 -0
  23. package/dist/client/event-hooks.js.map +1 -1
  24. package/dist/client/group-outbox.d.ts +45 -10
  25. package/dist/client/group-outbox.d.ts.map +1 -1
  26. package/dist/client/group-outbox.js +246 -154
  27. package/dist/client/group-outbox.js.map +1 -1
  28. package/dist/client/group-receive-authorization.d.ts +10 -1
  29. package/dist/client/group-receive-authorization.d.ts.map +1 -1
  30. package/dist/client/group-receive-authorization.js.map +1 -1
  31. package/dist/client/hosted-anonymous-delivery.d.ts +18 -1
  32. package/dist/client/hosted-anonymous-delivery.d.ts.map +1 -1
  33. package/dist/client/hosted-anonymous-delivery.js +84 -12
  34. package/dist/client/hosted-anonymous-delivery.js.map +1 -1
  35. package/dist/client/hosted-connection.js +1 -1
  36. package/dist/client/hosted-connection.js.map +1 -1
  37. package/dist/client/hosted-group-authority.d.ts +6 -2
  38. package/dist/client/hosted-group-authority.d.ts.map +1 -1
  39. package/dist/client/hosted-group-authority.js +11 -7
  40. package/dist/client/hosted-group-authority.js.map +1 -1
  41. package/dist/client/hosted-groups.d.ts +5 -1
  42. package/dist/client/hosted-groups.d.ts.map +1 -1
  43. package/dist/client/hosted-groups.js +8 -4
  44. package/dist/client/hosted-groups.js.map +1 -1
  45. package/dist/client/hosted-http-error.d.ts +5 -1
  46. package/dist/client/hosted-http-error.d.ts.map +1 -1
  47. package/dist/client/hosted-http-error.js +5 -1
  48. package/dist/client/hosted-http-error.js.map +1 -1
  49. package/dist/client/hosted-mailbox-subscription.d.ts +2 -0
  50. package/dist/client/hosted-mailbox-subscription.d.ts.map +1 -1
  51. package/dist/client/hosted-mailbox-subscription.js +5 -5
  52. package/dist/client/hosted-mailbox-subscription.js.map +1 -1
  53. package/dist/client/hosted-transport.d.ts +11 -1
  54. package/dist/client/hosted-transport.d.ts.map +1 -1
  55. package/dist/client/hosted-transport.js +75 -30
  56. package/dist/client/hosted-transport.js.map +1 -1
  57. package/dist/client/hosted.d.ts.map +1 -1
  58. package/dist/client/hosted.js +1 -1
  59. package/dist/client/hosted.js.map +1 -1
  60. package/dist/client/index.d.ts +1 -1
  61. package/dist/client/index.d.ts.map +1 -1
  62. package/dist/client/index.js.map +1 -1
  63. package/dist/client/receive-queue.d.ts +5 -2
  64. package/dist/client/receive-queue.d.ts.map +1 -1
  65. package/dist/client/receive-queue.js +55 -18
  66. package/dist/client/receive-queue.js.map +1 -1
  67. package/dist/client/relay-acceptance.d.ts +49 -6
  68. package/dist/client/relay-acceptance.d.ts.map +1 -1
  69. package/dist/client/relay-acceptance.js +49 -3
  70. package/dist/client/relay-acceptance.js.map +1 -1
  71. package/dist/client/relay-protocol.d.ts +37 -7
  72. package/dist/client/relay-protocol.d.ts.map +1 -1
  73. package/dist/client/relay-protocol.js +53 -10
  74. package/dist/client/relay-protocol.js.map +1 -1
  75. package/dist/client/retry-family.d.ts +3 -3
  76. package/dist/client/retry-family.js +1 -1
  77. package/dist/client/retry-family.js.map +1 -1
  78. package/dist/client/retry-request-envelope.d.ts +18 -8
  79. package/dist/client/retry-request-envelope.d.ts.map +1 -1
  80. package/dist/client/retry-request-envelope.js +57 -29
  81. package/dist/client/retry-request-envelope.js.map +1 -1
  82. package/dist/client/retry-request-lifecycle.d.ts +24 -11
  83. package/dist/client/retry-request-lifecycle.d.ts.map +1 -1
  84. package/dist/client/retry-request-lifecycle.js +46 -20
  85. package/dist/client/retry-request-lifecycle.js.map +1 -1
  86. package/dist/client/retry-terminal-recovery.d.ts +11 -8
  87. package/dist/client/retry-terminal-recovery.d.ts.map +1 -1
  88. package/dist/client/retry-terminal-recovery.js +18 -12
  89. package/dist/client/retry-terminal-recovery.js.map +1 -1
  90. package/dist/client/retry.d.ts +8 -7
  91. package/dist/client/retry.d.ts.map +1 -1
  92. package/dist/client/retry.js +30 -22
  93. package/dist/client/retry.js.map +1 -1
  94. package/dist/client/send-deadline.d.ts +308 -0
  95. package/dist/client/send-deadline.d.ts.map +1 -0
  96. package/dist/client/send-deadline.js +914 -0
  97. package/dist/client/send-deadline.js.map +1 -0
  98. package/dist/client/send-intent-driver.d.ts +116 -0
  99. package/dist/client/send-intent-driver.d.ts.map +1 -0
  100. package/dist/client/send-intent-driver.js +373 -0
  101. package/dist/client/send-intent-driver.js.map +1 -0
  102. package/dist/client/signal-service-cipher.d.ts +257 -27
  103. package/dist/client/signal-service-cipher.d.ts.map +1 -1
  104. package/dist/client/signal-service-cipher.js +1274 -283
  105. package/dist/client/signal-service-cipher.js.map +1 -1
  106. package/dist/client/types.d.ts +48 -5
  107. package/dist/client/types.d.ts.map +1 -1
  108. package/dist/client/types.js.map +1 -1
  109. package/dist/index.d.ts +1 -1
  110. package/dist/index.d.ts.map +1 -1
  111. package/dist/index.js.map +1 -1
  112. package/dist/local/store/conformance/cases/metadata.d.ts +3 -2
  113. package/dist/local/store/conformance/cases/metadata.d.ts.map +1 -1
  114. package/dist/local/store/conformance/cases/metadata.js +153 -2
  115. package/dist/local/store/conformance/cases/metadata.js.map +1 -1
  116. package/dist/local/store/key-value/adapter.d.ts +2 -1
  117. package/dist/local/store/key-value/adapter.d.ts.map +1 -1
  118. package/dist/local/store/key-value/adapter.js +9 -0
  119. package/dist/local/store/key-value/adapter.js.map +1 -1
  120. package/dist/local/store/memory/adapter.d.ts +2 -1
  121. package/dist/local/store/memory/adapter.d.ts.map +1 -1
  122. package/dist/local/store/memory/adapter.js +4 -0
  123. package/dist/local/store/memory/adapter.js.map +1 -1
  124. package/dist/local/store/metadata-keys.d.ts +13 -0
  125. package/dist/local/store/metadata-keys.d.ts.map +1 -0
  126. package/dist/local/store/metadata-keys.js +20 -0
  127. package/dist/local/store/metadata-keys.js.map +1 -0
  128. package/dist/local/store/receive-work.d.ts.map +1 -1
  129. package/dist/local/store/receive-work.js +12 -1
  130. package/dist/local/store/receive-work.js.map +1 -1
  131. package/dist/local/store/reliability.d.ts +482 -33
  132. package/dist/local/store/reliability.d.ts.map +1 -1
  133. package/dist/local/store/reliability.js +690 -48
  134. package/dist/local/store/reliability.js.map +1 -1
  135. package/dist/local/store/sqlite/key-storage/index.d.ts +2 -1
  136. package/dist/local/store/sqlite/key-storage/index.d.ts.map +1 -1
  137. package/dist/local/store/sqlite/key-storage/index.js +3 -0
  138. package/dist/local/store/sqlite/key-storage/index.js.map +1 -1
  139. package/dist/local/store/sqlite/key-storage/metadata.d.ts +9 -0
  140. package/dist/local/store/sqlite/key-storage/metadata.d.ts.map +1 -1
  141. package/dist/local/store/sqlite/key-storage/metadata.js +12 -0
  142. package/dist/local/store/sqlite/key-storage/metadata.js.map +1 -1
  143. package/dist/local/store/sqlite/store.d.ts +2 -1
  144. package/dist/local/store/sqlite/store.d.ts.map +1 -1
  145. package/dist/local/store/sqlite/store.js +3 -0
  146. package/dist/local/store/sqlite/store.js.map +1 -1
  147. package/dist/local/store/web/adapter.d.ts +8 -1
  148. package/dist/local/store/web/adapter.d.ts.map +1 -1
  149. package/dist/local/store/web/adapter.js +18 -0
  150. package/dist/local/store/web/adapter.js.map +1 -1
  151. package/dist/remote/relay/request-admission.d.ts +49 -0
  152. package/dist/remote/relay/request-admission.d.ts.map +1 -0
  153. package/dist/remote/relay/request-admission.js +74 -0
  154. package/dist/remote/relay/request-admission.js.map +1 -0
  155. package/dist/types/api.d.ts +77 -0
  156. package/dist/types/api.d.ts.map +1 -1
  157. package/dist/types/errors.d.ts +144 -1
  158. package/dist/types/errors.d.ts.map +1 -1
  159. package/dist/types/errors.js +146 -1
  160. package/dist/types/errors.js.map +1 -1
  161. package/docs/ASSURANCE.md +31 -15
  162. package/docs/DEVIATIONS.md +34 -4
  163. package/docs/ERROR_HANDLING.md +134 -7
  164. package/docs/GETTING_STARTED.md +30 -23
  165. package/docs/PROTOCOL_POLICY.md +31 -20
  166. package/groups/README.md +1 -1
  167. package/hooks/README.md +6 -3
  168. package/local/store/react-native/README.md +9 -10
  169. package/local/store/web/README.md +1 -1
  170. package/package.json +1 -1
  171. package/safety/README.md +48 -11
@@ -2,13 +2,15 @@ import { EncryptionError, EncryptionErrorCode } from '../types/index.js';
2
2
  import { SealedSenderAuthError } from '../types/errors.js';
3
3
  import { ContentHint } from '../types/messages.js';
4
4
  import * as CryptoUtils from '../internal/crypto/index.js';
5
- import { completeOutgoingMessageIntent, completeOutgoingSyncPhase, confirmOutgoingPreMessages, getOutgoingMessageIntent, outgoingMessageIntentResult, reconcileOutgoingGroupDeviceSet, reconcileOutgoingSyncMessages, repairOutgoingDeviceSet, replaceOutgoingGroupPreMessages, restartOutgoingDeviceSet, storeOutgoingMessageIntent, withOutgoingMessageIntentLock, } from '../local/store/reliability.js';
5
+ import { completeOutgoingMessageIntent, completeOutgoingSyncPhase, confirmOutgoingPreMessages, getOutgoingMessageIntent, outgoingDestinationProgressKey, outgoingMessageDeadlineAt, outgoingMessageIntentResult, reconcileOutgoingGroupDeviceSet, reconcileOutgoingSyncMessages, repairOutgoingDeviceSet, replaceOutgoingGroupPreMessages, storeOutgoingMessageIntent, withOutgoingMessageIntentLock, } from '../local/store/reliability.js';
6
6
  import { deviceSetRefusal, markDeviceSetRefusal } from '../remote/relay/device-set.js';
7
7
  import { runDeviceSetPhase, syncPostReceipts, } from './device-set-reconciliation.js';
8
8
  import { isStaleDeviceRefusal } from './relay-device-refusal.js';
9
9
  import { newSendClientTimestamp } from './send-timestamp.js';
10
+ import { intentBinaryAttachment } from './binary-send.js';
10
11
  import { groupDeviceKey, listedGroupDeviceChange, listedOwnDeviceChange, sharedBodyDevices, } from './group-device-set.js';
11
- import { aggregateRelayAcceptance } from './relay-acceptance.js';
12
+ import { aggregateRelayAcceptance, isConflictAcceptance, sendResultAcceptance, } from './relay-acceptance.js';
13
+ import { SEND_END_REPORT, sendDestinations, storedSendEndError, } from './send-deadline.js';
12
14
  /**
13
15
  * Post one stored device message. With a declaration, an identified post
14
16
  * and an access-key sealed post declare the devices of the account. A
@@ -42,7 +44,7 @@ async function sendStoredDeviceMessage(context, intent, message, forceIdentified
42
44
  }),
43
45
  ...(intent.contentHint !== undefined && { contentHint: intent.contentHint }),
44
46
  ...(declaration && { declaredDeviceIds: declaration.deviceIds }),
45
- }), false);
47
+ }), false, sendDestinations(intent.clientMessageId, [message])[0]);
46
48
  declaration?.routes.set(acceptance, 'identified-direct');
47
49
  return acceptance;
48
50
  }
@@ -94,19 +96,39 @@ async function sendGroupDeviceMessage(context, intent, message, forceIdentified,
94
96
  }
95
97
  }
96
98
  const repair = await context.repairStaleDevice(intent, message);
97
- await reconcileOutgoingGroupDeviceSet(context.storage, intent.clientMessageId, {
99
+ // The write marks the repaired device, so a RETRY_CONFLICT of its repost
100
+ // is not an acceptance.
101
+ const repaired = await reconcileOutgoingGroupDeviceSet(context.storage, intent.clientMessageId, {
98
102
  removed: [{ userId: message.recipientUserId, deviceId: message.recipientDeviceId }],
99
103
  preMessages: repair.preMessages,
100
104
  deviceMessages: [repair.deviceMessage],
101
105
  syncMessages: [],
102
106
  });
103
- const dispatched = await dispatchPreMessages(context, intent, repair.preMessages, required);
107
+ refreshAppendedEntries(context, repaired, repair.preMessages);
108
+ const dispatched = await dispatchPreMessages(context, repaired, repair.preMessages, required);
104
109
  const device = { userId: message.recipientUserId, deviceId: message.recipientDeviceId };
105
110
  if (dispatched.removed.some((removed) => groupDeviceKey(removed) === groupDeviceKey(device))) {
106
111
  return undefined;
107
112
  }
108
- const { renewed } = await renewRefreshedDeviceMessages(context, dispatched.intent, dispatched.refreshed, [repair.deviceMessage]);
109
- return sendStoredDeviceMessage(context, intent, renewed[0] ?? repair.deviceMessage, forceIdentified);
113
+ const renewal = await renewRefreshedDeviceMessages(context, dispatched.intent, dispatched.refreshed, [repair.deviceMessage]);
114
+ const repost = renewal.renewed[0] ?? repair.deviceMessage;
115
+ refreshAppendedEntries(context, renewal.intent, [repost]);
116
+ return sendStoredDeviceMessage(context, repaired, repost, forceIdentified);
117
+ }
118
+ /**
119
+ * Give the evidence of the call the stored entry of each message that a
120
+ * write of this call appended, before the message posts. A reconcile or a
121
+ * repair writes the repair mark of the device, and a replay reads the same
122
+ * entry from the intent. So a RETRY_CONFLICT of the post gets the same
123
+ * class in the call and in a replay. A message with no stored entry keeps
124
+ * the entry that the call has.
125
+ */
126
+ function refreshAppendedEntries(context, intent, messages) {
127
+ for (const destination of sendDestinations(intent.clientMessageId, messages)) {
128
+ const entry = intent.destinationProgress?.[outgoingDestinationProgressKey(destination.postMessageId, destination.device)];
129
+ if (entry)
130
+ context.refreshEntry(destination.device, entry);
131
+ }
110
132
  }
111
133
  /**
112
134
  * Post stored pre-messages. Each one is authorized before any post, and a
@@ -243,6 +265,7 @@ async function renewRefreshedDeviceMessages(context, intent, refreshed, messages
243
265
  deviceMessages: renewed,
244
266
  syncMessages: [],
245
267
  });
268
+ refreshAppendedEntries(context, written, [...preMessages, ...renewed]);
246
269
  return { intent: written, renewed };
247
270
  }
248
271
  async function sendStoredGroupIntent(context, initialIntent, plaintextBytes) {
@@ -280,131 +303,139 @@ async function sendStoredGroupIntent(context, initialIntent, plaintextBytes) {
280
303
  const required = context.deliveryMode === 'required' ||
281
304
  intent.transportMode === 'sealed_sender_required' ||
282
305
  preMessages.some((message) => message.sealedSenderDeliveryMode === 'required');
283
- if (preMessages.length > 0) {
284
- // One refresh covers the pre-messages and the group message.
285
- await context.refreshPreMessageEndorsements(intent.recipientId, [
286
- ...new Set([
287
- ...preMessages.map((message) => message.recipientUserId),
288
- ...(intent.groupSharedMessage?.recipientUserIds ?? []),
289
- ]),
290
- ]);
291
- }
292
- // Every pre-message is accepted or dropped before the group message.
293
- const dispatched = await dispatchPreMessages(context, intent, preMessages, required);
294
- intent = dispatched.intent;
295
- // A device that a pre-message repair refreshed, or whose session no longer
296
- // has the registration that the shared body names, leaves the shared body
297
- // of this attempt. It gets the key of the send when it has no handoff row,
298
- // and its own post under its current registration.
299
- const leaving = await leaveStaleSharedBody(context, intent, dispatched.refreshed, groupCiphertext, required);
300
- intent = leaving.intent;
301
- if (intent.groupSharedMessage?.deliveryMode === 'required' &&
302
- !relay.sendMultiRecipientUnidentified) {
303
- throw new EncryptionError('Required group sealed-sender transport is unavailable', EncryptionErrorCode.SEALED_SENDER_REQUIRED, { groupId: intent.recipientId });
304
- }
305
- if (intent.groupSharedMessage && relay.sendMultiRecipientUnidentified) {
306
- const auth = await context.resolveGroupAuthorization(intent.groupSharedMessage.groupId, intent.groupSharedMessage.recipientUserIds);
307
- if (!auth) {
308
- if (intent.groupSharedMessage.deliveryMode === 'required') {
309
- throw new Error('Required group sealed-sender authorization is unavailable');
310
- }
306
+ // The recipient phase ends here when it fails at or after the posting
307
+ // deadline with an open destination, or when every open destination got
308
+ // OPERATION_EXPIRED. Before that, the error is thrown with the deadline.
309
+ try {
310
+ if (preMessages.length > 0) {
311
+ // One refresh covers the pre-messages and the group message.
312
+ await context.refreshPreMessageEndorsements(intent.recipientId, [
313
+ ...new Set([
314
+ ...preMessages.map((message) => message.recipientUserId),
315
+ ...(intent.groupSharedMessage?.recipientUserIds ?? []),
316
+ ]),
317
+ ]);
311
318
  }
312
- else {
313
- const shared = intent.groupSharedMessage;
314
- try {
315
- const result = await context.postToRelay(() => relay.sendMultiRecipientUnidentified(shared.sentMessageBase64, auth, intent.clientTimestamp, 'user-visible', shared.recipientUserIds, intent.clientMessageId), true);
316
- messageId = result.messageId;
317
- timestamp = result.serverTimestamp;
318
- skippedUsers = result.uuids404;
319
- refusedDestinationCount = result.refusedDestinationCount;
320
- sharedRefusals = result.refusals ?? [];
321
- sharedSucceeded = true;
322
- acceptances.push(result);
319
+ // Every pre-message is accepted or dropped before the group message.
320
+ const dispatched = await dispatchPreMessages(context, intent, preMessages, required);
321
+ intent = dispatched.intent;
322
+ // A device that a pre-message repair refreshed, or whose session no longer
323
+ // has the registration that the shared body names, leaves the shared body
324
+ // of this attempt. It gets the key of the send when it has no handoff row,
325
+ // and its own post under its current registration.
326
+ const leaving = await leaveStaleSharedBody(context, intent, dispatched.refreshed, groupCiphertext, required);
327
+ intent = leaving.intent;
328
+ if (intent.groupSharedMessage?.deliveryMode === 'required' &&
329
+ !relay.sendMultiRecipientUnidentified) {
330
+ throw new EncryptionError('Required group sealed-sender transport is unavailable', EncryptionErrorCode.SEALED_SENDER_REQUIRED, { groupId: intent.recipientId });
331
+ }
332
+ if (intent.groupSharedMessage && relay.sendMultiRecipientUnidentified) {
333
+ const auth = await context.resolveGroupAuthorization(intent.groupSharedMessage.groupId, intent.groupSharedMessage.recipientUserIds);
334
+ if (!auth) {
335
+ if (intent.groupSharedMessage.deliveryMode === 'required') {
336
+ throw new Error('Required group sealed-sender authorization is unavailable');
337
+ }
323
338
  }
324
- catch (error) {
325
- if (!(error instanceof SealedSenderAuthError))
326
- throw error;
327
- if (intent.groupSharedMessage.deliveryMode === 'required')
328
- throw error;
329
- await context.prepareEach(intent.groupSharedMessage.recipientUserIds, (recipientUserId) => context.reportIdentifiedFallback(recipientUserId));
339
+ else {
340
+ const shared = intent.groupSharedMessage;
341
+ try {
342
+ const result = await context.postToRelay(() => relay.sendMultiRecipientUnidentified(shared.sentMessageBase64, auth, intent.clientTimestamp, 'user-visible', shared.recipientUserIds, intent.clientMessageId), true, sharedBodyDestinations(intent.clientMessageId, sharedBodyDevices(shared)));
343
+ skippedUsers = result.uuids404;
344
+ refusedDestinationCount = result.refusedDestinationCount;
345
+ sharedRefusals = result.refusals ?? [];
346
+ sharedSucceeded = true;
347
+ acceptances.push(result);
348
+ }
349
+ catch (error) {
350
+ if (!(error instanceof SealedSenderAuthError))
351
+ throw error;
352
+ if (intent.groupSharedMessage.deliveryMode === 'required')
353
+ throw error;
354
+ await context.prepareEach(intent.groupSharedMessage.recipientUserIds, (recipientUserId) => context.reportIdentifiedFallback(recipientUserId));
355
+ }
330
356
  }
331
357
  }
332
- }
333
- // A device that the shared body does not name gets its own post. A replay
334
- // keeps the stored body, so a device that the replay added is one. A
335
- // device that left the shared body of this attempt is one too.
336
- const sharedDevices = sharedSucceeded
337
- ? sharedBodyDevices(intent.groupSharedMessage)
338
- : new Map();
339
- for (const device of leaving.left)
340
- sharedDevices.delete(device);
341
- // The group-token receipt names each refused destination with its code. A
342
- // destination that the Relay refused as STALE_DEVICE leaves the shared set
343
- // and gets the device repair once. The refused count alone names no
344
- // device, so the send infers nothing from it.
345
- const refusedDevices = new Set(sharedRefusals
346
- .filter((refusal) => refusal.code === 'STALE_DEVICE')
347
- .map(groupDeviceKey)
348
- .filter((key) => sharedDevices.has(key)));
349
- for (const key of refusedDevices)
350
- sharedDevices.delete(key);
351
- const usersToRepair = new Set(skippedUsers);
352
- const forceIdentified = Boolean(intent.groupSharedMessage && !sharedSucceeded);
353
- const devicePosts = intent.deviceMessages.filter((message) => !sharedDevices.has(groupDeviceKey({ userId: message.recipientUserId, deviceId: message.recipientDeviceId })) || usersToRepair.has(message.recipientUserId));
354
- const posting = intent;
355
- const results = await context.sendPhase(devicePosts, (message) => sendGroupDeviceMessage(context, posting, message, forceIdentified, required, refusedDevices.has(groupDeviceKey({ userId: message.recipientUserId, deviceId: message.recipientDeviceId }))), 'device');
356
- const posted = results.filter((result) => result !== undefined);
357
- acceptances.push(...posted);
358
- if (messageId.startsWith('group-') && posted[0]) {
359
- messageId = posted[0].messageId;
360
- timestamp = posted[0].serverTimestamp;
361
- }
362
- if (skippedUsers.length > 0) {
363
- const refreshed = await context.resolveMemberDevices(intent.recipientId, {
364
- clientMessageId: intent.clientMessageId,
365
- groupMemberUserIds: skippedUsers,
366
- });
367
- const { added } = listedGroupDeviceChange(intent, refreshed, context.senderUserId);
368
- if (added.length > 0) {
369
- // The pre-messages come first. They give each added device a session
370
- // and the key of the send, so its device message names the
371
- // registration of that session, and the device can decrypt it.
372
- const handoffs = await context.prepareSenderKeyHandoffs(intent, refreshed);
373
- const additions = await context.prepareEach(added, (member) => context.prepareDeviceMessage(member, intent.recipientId, groupCiphertext, intent.clientTimestamp, intent.contentHint));
374
- // A device that the repair adds gets a send log row before its post.
375
- if (isLoggedSend(intent)) {
376
- await context.storage.appendSendLogRecipients(intent.clientMessageId, sendLogDevices(additions));
377
- }
378
- intent = await reconcileOutgoingGroupDeviceSet(context.storage, intent.clientMessageId, {
379
- removed: [],
380
- preMessages: handoffs,
381
- deviceMessages: additions,
382
- syncMessages: [],
358
+ // A device that the shared body does not name gets its own post. A replay
359
+ // keeps the stored body, so a device that the replay added is one. A
360
+ // device that left the shared body of this attempt is one too.
361
+ const sharedDevices = sharedSucceeded
362
+ ? sharedBodyDevices(intent.groupSharedMessage)
363
+ : new Map();
364
+ for (const device of leaving.left)
365
+ sharedDevices.delete(device);
366
+ // The group-token receipt names each refused destination with its code. A
367
+ // destination that the Relay refused as STALE_DEVICE leaves the shared set
368
+ // and gets the device repair once. The refused count alone names no
369
+ // device, so the send infers nothing from it.
370
+ const refusedDevices = new Set(sharedRefusals
371
+ .filter((refusal) => refusal.code === 'STALE_DEVICE')
372
+ .map(groupDeviceKey)
373
+ .filter((key) => sharedDevices.has(key)));
374
+ for (const key of refusedDevices)
375
+ sharedDevices.delete(key);
376
+ const usersToRepair = new Set(skippedUsers);
377
+ const forceIdentified = Boolean(intent.groupSharedMessage && !sharedSucceeded);
378
+ const devicePosts = intent.deviceMessages.filter((message) => !sharedDevices.has(groupDeviceKey({ userId: message.recipientUserId, deviceId: message.recipientDeviceId })) || usersToRepair.has(message.recipientUserId));
379
+ const posting = intent;
380
+ const results = await context.sendPhase(devicePosts, (message) => sendGroupDeviceMessage(context, posting, message, forceIdentified, required, refusedDevices.has(groupDeviceKey({ userId: message.recipientUserId, deviceId: message.recipientDeviceId }))), 'device');
381
+ const posted = results.filter((result) => result !== undefined);
382
+ acceptances.push(...posted);
383
+ // The shared body gives the message ID and time, else the first device
384
+ // post. An acceptance that a RETRY_CONFLICT or a version 2
385
+ // DELIVERY_ALREADY_ACCEPTED gave has no Relay receipt, so a post with a
386
+ // receipt comes first.
387
+ const first = sendResultAcceptance(acceptances);
388
+ if (first) {
389
+ messageId = first.messageId;
390
+ timestamp = first.serverTimestamp;
391
+ }
392
+ if (skippedUsers.length > 0) {
393
+ const refreshed = await context.resolveMemberDevices(intent.recipientId, {
394
+ clientMessageId: intent.clientMessageId,
395
+ groupMemberUserIds: skippedUsers,
383
396
  });
384
- const dispatchedHandoffs = await dispatchPreMessages(context, intent, handoffs, required);
385
- // A device that a repair of its pre-message refreshed gets a device
386
- // message under its new session before its post.
387
- const renewal = await renewRefreshedDeviceMessages(context, dispatchedHandoffs.intent, dispatchedHandoffs.refreshed, additions, groupCiphertext);
388
- intent = renewal.intent;
389
- // The confirmation of the pre-messages removed the device message of
390
- // each device that the device list no longer shows. Only the additions
391
- // that the intent still holds get a post.
392
- const removedKeys = new Set(dispatchedHandoffs.removed.map(groupDeviceKey));
393
- const renewedOf = new Map(renewal.renewed.map((message) => [
394
- groupDeviceKey({ userId: message.recipientUserId, deviceId: message.recipientDeviceId }),
395
- message,
396
- ]));
397
- const extended = intent;
398
- const additionResults = await context.sendPhase(additions.flatMap((message) => {
399
- const key = groupDeviceKey({
400
- userId: message.recipientUserId,
401
- deviceId: message.recipientDeviceId,
397
+ const { added } = listedGroupDeviceChange(intent, refreshed, context.senderUserId);
398
+ if (added.length > 0) {
399
+ // The pre-messages come first. They give each added device a session
400
+ // and the key of the send, so its device message names the
401
+ // registration of that session, and the device can decrypt it.
402
+ const handoffs = await context.prepareSenderKeyHandoffs(intent, refreshed);
403
+ const additions = await context.prepareEach(added, (member) => context.prepareDeviceMessage(member, intent.recipientId, groupCiphertext, intent.clientTimestamp, intent.contentHint));
404
+ // A device that the repair adds gets a send log row before its post.
405
+ if (isLoggedSend(intent)) {
406
+ await context.storage.appendSendLogRecipients(intent.clientMessageId, sendLogDevices(additions));
407
+ }
408
+ intent = await reconcileOutgoingGroupDeviceSet(context.storage, intent.clientMessageId, {
409
+ removed: [],
410
+ preMessages: handoffs,
411
+ deviceMessages: additions,
412
+ syncMessages: [],
402
413
  });
403
- return removedKeys.has(key) ? [] : [renewedOf.get(key) ?? message];
404
- }), (message) => sendGroupDeviceMessage(context, extended, message, false, required), 'device');
405
- acceptances.push(...additionResults.filter((result) => result !== undefined));
414
+ const dispatchedHandoffs = await dispatchPreMessages(context, intent, handoffs, required);
415
+ // A device that a repair of its pre-message refreshed gets a device
416
+ // message under its new session before its post.
417
+ const renewal = await renewRefreshedDeviceMessages(context, dispatchedHandoffs.intent, dispatchedHandoffs.refreshed, additions, groupCiphertext);
418
+ intent = renewal.intent;
419
+ // The confirmation of the pre-messages removed the device message of
420
+ // each device that the device list no longer shows. Only the additions
421
+ // that the intent still holds get a post.
422
+ const removedKeys = new Set(dispatchedHandoffs.removed.map(groupDeviceKey));
423
+ const renewedOf = new Map(renewal.renewed.map((message) => [groupDeviceKey(messageDevice(message)), message]));
424
+ const extended = intent;
425
+ const additionResults = await context.sendPhase(additions.flatMap((message) => {
426
+ const key = groupDeviceKey({
427
+ userId: message.recipientUserId,
428
+ deviceId: message.recipientDeviceId,
429
+ });
430
+ return removedKeys.has(key) ? [] : [renewedOf.get(key) ?? message];
431
+ }), (message) => sendGroupDeviceMessage(context, extended, message, false, required), 'device');
432
+ acceptances.push(...additionResults.filter((result) => result !== undefined));
433
+ }
406
434
  }
407
435
  }
436
+ catch (error) {
437
+ return context.endRecipientPhase(intent, error);
438
+ }
408
439
  // The sync copies start after every device post settles. They go to the
409
440
  // sender's own devices, so they are identified in every privacy mode: the
410
441
  // relay already knows the account that owns the destination mailbox.
@@ -413,7 +444,10 @@ async function sendStoredGroupIntent(context, initialIntent, plaintextBytes) {
413
444
  const [message] = await context.prepareSyncMessages(syncing.recipientId, await transcriptContent(context, syncing, plaintextBytes), syncing.clientTimestamp, syncing.clientMessageId, [{ userId: context.senderUserId, deviceId }]);
414
445
  return message;
415
446
  };
416
- await context.sendPhase(intent.syncMessages, (message) => context.postSyncMessage(syncing, message, rebuildSyncMessage), 'sync');
447
+ // A sync copy that the posting deadline stopped keeps the sync phase
448
+ // pending.
449
+ const stopped = await context.sendPhase(intent.syncMessages, (message) => context.postSyncMessage(syncing, message, rebuildSyncMessage), 'sync');
450
+ const syncPending = stopped.some(Boolean);
417
451
  const result = {
418
452
  clientMessageId: intent.clientMessageId,
419
453
  messageId,
@@ -423,9 +457,21 @@ async function sendStoredGroupIntent(context, initialIntent, plaintextBytes) {
423
457
  ...(refusedDestinationCount !== undefined && { refusedDestinationCount }),
424
458
  groupId: intent.recipientId,
425
459
  };
426
- await completeOutgoingMessageIntent(context.storage, intent.clientMessageId, result);
460
+ await completeOutgoingMessageIntent(context.storage, intent.clientMessageId, result, {
461
+ syncPending,
462
+ });
427
463
  await completeSendLog(context, intent);
428
- return result;
464
+ return syncPending ? { ...result, syncPending: true } : result;
465
+ }
466
+ /**
467
+ * The destinations of a shared body: each device that the body names, under
468
+ * the client message ID of the send, which the shared post sends.
469
+ */
470
+ function sharedBodyDestinations(clientMessageId, devices) {
471
+ return [...devices.keys()].map((key) => {
472
+ const [userId, deviceId] = key.split('\u0000');
473
+ return { device: { userId, deviceId: Number(deviceId) }, postMessageId: clientMessageId };
474
+ });
429
475
  }
430
476
  /** The device of a stored device message. */
431
477
  function messageDevice(message) {
@@ -561,6 +607,7 @@ async function sendDeclaredGroupIntent(context, deviceSets, initialIntent, plain
561
607
  syncMessages: [],
562
608
  ...(groupSharedMessage !== undefined && { groupSharedMessage }),
563
609
  });
610
+ refreshAppendedEntries(context, intent, renewed);
564
611
  }
565
612
  }
566
613
  }
@@ -605,7 +652,7 @@ async function sendDeclaredGroupIntent(context, deviceSets, initialIntent, plain
605
652
  deviceIds: deviceIds.sort((a, b) => a - b),
606
653
  }));
607
654
  try {
608
- const receipt = await context.postToRelay(() => relay.sendMultiRecipientUnidentified(body.sentMessageBase64, auth, intent.clientTimestamp, 'user-visible', body.recipientUserIds, clientMessageId, undefined, undefined, deviceSetsOfBody), true);
655
+ const receipt = await context.postToRelay(() => relay.sendMultiRecipientUnidentified(body.sentMessageBase64, auth, intent.clientTimestamp, 'user-visible', body.recipientUserIds, clientMessageId, undefined, undefined, deviceSetsOfBody), true, sharedBodyDestinations(clientMessageId, bodyDevices));
609
656
  sharedReceipt = receipt;
610
657
  routes.set(receipt, 'anonymous-group');
611
658
  bodyPosts({ ok: true, value: receipt });
@@ -719,24 +766,43 @@ async function sendDeclaredGroupIntent(context, deviceSets, initialIntent, plain
719
766
  deviceMessages,
720
767
  ...(groupSharedMessage !== undefined && { groupSharedMessage }),
721
768
  });
769
+ // The store wrote `repairedFor` on each appended device, and `repaired`
770
+ // on a device that the repair removed and appended again, as for a stale
771
+ // device: the Relay can hold its replaced message. The evidence of the
772
+ // call takes the entry of each appended device before the next pass
773
+ // posts it, so the call and a replay give a RETRY_CONFLICT of that post
774
+ // the same class.
775
+ refreshAppendedEntries(context, intent, [...preMessages, ...deviceMessages]);
722
776
  unposted = bundleMissing;
723
777
  return { bundleMissing };
724
778
  };
725
- const posts = await runDeviceSetPhase({
726
- phase: 'recipient',
727
- clientMessageId,
728
- restarts: intent.deviceSetRestarts?.recipient ?? 0,
729
- pass,
730
- repair,
731
- unresolve: (unresolved) => deviceSets.unresolve(clientMessageId, unresolved),
732
- wait: (milliseconds) => deviceSets.wait(milliseconds),
733
- logFailure: (device, error) => deviceSets.logFailure(device, error),
734
- });
779
+ // The recipient phase ends here when it fails at or after the posting
780
+ // deadline with an open destination, or when every open destination got
781
+ // OPERATION_EXPIRED. Before that, the error is thrown with the deadline.
782
+ let posts;
783
+ try {
784
+ posts = await runDeviceSetPhase({
785
+ phase: 'recipient',
786
+ clientMessageId,
787
+ restarts: intent.deviceSetRestarts?.recipient ?? 0,
788
+ pass,
789
+ repair,
790
+ unresolve: (unresolved) => deviceSets.unresolve(clientMessageId, unresolved),
791
+ wait: (milliseconds) => deviceSets.wait(milliseconds),
792
+ logFailure: (device, error) => deviceSets.logFailure(device, error),
793
+ });
794
+ }
795
+ catch (error) {
796
+ return context.endRecipientPhase(intent, error);
797
+ }
735
798
  // The body receipt answers each device of the body, so it counts once.
736
799
  const accepted = [
737
800
  ...new Set(posts.flatMap((post) => (post.outcome.ok ? [post.outcome.value] : []))),
738
801
  ];
739
- const first = sharedReceipt ?? accepted[0];
802
+ // An acceptance that a RETRY_CONFLICT or a version 2
803
+ // DELIVERY_ALREADY_ACCEPTED gave has no Relay receipt, so a post with a
804
+ // receipt gives the message ID and time.
805
+ const first = sendResultAcceptance(sharedReceipt ? [sharedReceipt, ...accepted] : accepted);
740
806
  const result = {
741
807
  clientMessageId,
742
808
  messageId: first?.messageId ?? `group-${groupId}-${String(intent.clientTimestamp)}`,
@@ -749,13 +815,23 @@ async function sendDeclaredGroupIntent(context, deviceSets, initialIntent, plain
749
815
  groupId,
750
816
  };
751
817
  // The sync phase runs when the intent holds a sync copy, or when a
752
- // receipt sets needsSync. With needsSync and no own record, the send
753
- // reads the own list once. When that read fails, the sync phase stays
754
- // pending with no sync messages, and a replay reads it.
818
+ // receipt sets needsSync. An acceptance that a RETRY_CONFLICT, a version 2
819
+ // DELIVERY_ALREADY_ACCEPTED or the stored progress gave has no receipt,
820
+ // and the lost receipt can have set needsSync, so it counts as needsSync
821
+ // too. With needsSync and no own record, the send reads the own list
822
+ // once. When that read fails, the sync phase stays pending with no sync
823
+ // messages, and a replay reads it.
755
824
  let current = (await getOutgoingMessageIntent(context.storage, clientMessageId)) ?? intent;
825
+ // Another process can complete the send while this one posts. The Relay
826
+ // then answers the body of this process with RETRY_CONFLICT, which counts
827
+ // as acceptance, and the stored result holds the Relay receipt.
828
+ if (current.result) {
829
+ await completeSendLog(context, current);
830
+ return outgoingMessageIntentResult(current);
831
+ }
756
832
  let syncUnread = false;
757
833
  if (current.syncMessages.length === 0 &&
758
- accepted.some((acceptance) => acceptance.needsSync === true)) {
834
+ accepted.some((acceptance) => acceptance.needsSync === true || isConflictAcceptance(acceptance))) {
759
835
  const added = await addDeclaredGroupSync(context, deviceSets, current, plaintextBytes);
760
836
  current = added.intent;
761
837
  syncUnread = added.unread;
@@ -823,8 +899,9 @@ function runDeclaredGroupSync(context, deviceSets, intent, plaintextBytes) {
823
899
  });
824
900
  }
825
901
  /**
826
- * Replay a stored group send on a relay that compares device sets. A
827
- * terminal send starts a new run at 0 restarts. A completed send with a
902
+ * Replay a stored group send on a relay that compares device sets. A later
903
+ * `send()` after a recipient-phase end throws the stored-end class of its
904
+ * outcome and posts nothing. A completed send with a
828
905
  * pending sync phase posts only its sync messages. A send that is not
829
906
  * terminal resumes with its stored restart count. The replay reads no device
830
907
  * list: the Relay comparison finds each change. The one exception is a
@@ -834,7 +911,7 @@ function runDeclaredGroupSync(context, deviceSets, intent, plaintextBytes) {
834
911
  * refuses it as missing and retries it as the live run does.
835
912
  */
836
913
  async function replayDeclaredGroupIntent(context, deviceSets, existing, plaintextBytes) {
837
- const intent = await restartOutgoingDeviceSet(context.storage, existing.clientMessageId);
914
+ const intent = existing;
838
915
  const stored = outgoingMessageIntentResult(intent);
839
916
  if (!stored)
840
917
  return sendDeclaredGroupIntent(context, deviceSets, intent, plaintextBytes);
@@ -892,11 +969,14 @@ async function rereadStoredGroupDevices(context, intent, plaintextBytes) {
892
969
  *
893
970
  * Signal encrypts the message again for the devices of each attempt. The
894
971
  * replay here keeps the stored ciphertext. The Relay keys each delivery
895
- * operation by its message ID and refuses another operation under the same
896
- * ID with RETRY_CONFLICT, so a new group ciphertext under the clientMessageId
897
- * of the send would conflict with the posts that the Relay already holds. A
898
- * device that the replay adds therefore gets the content only when the send
899
- * is `Resendable`.
972
+ * operation by message ID, class, destination device, generation and epoch.
973
+ * It refuses a changed request under the same operation with RETRY_CONFLICT.
974
+ * It also refuses a new operation whose message ID a row or journal entry of
975
+ * that mailbox holds. The message ID alone is not the ring key or the
976
+ * operation key. So a new group ciphertext under the clientMessageId of the
977
+ * send would conflict with the posts that the Relay already holds. A device
978
+ * that the replay adds therefore gets the content only when the send is
979
+ * `Resendable`.
900
980
  */
901
981
  async function rereadMemberDevices(context, intent) {
902
982
  const groupCiphertext = intent.groupCiphertext;
@@ -1007,6 +1087,7 @@ async function completeSendLog(context, intent) {
1007
1087
  await context.storage.markSendLogComplete(intent.clientMessageId);
1008
1088
  }
1009
1089
  export async function sendGroupWithExactOutbox(context, groupId, plaintextBytes, options) {
1090
+ const report = options[SEND_END_REPORT];
1010
1091
  return withOutgoingMessageIntentLock(context.storage, options.clientMessageId, async () => {
1011
1092
  const plaintextDigest = CryptoUtils.bytesToBase64(await CryptoUtils.sha256(plaintextBytes));
1012
1093
  const groupMemberUserIds = [...new Set(options.groupMemberUserIds ?? [])].sort();
@@ -1019,12 +1100,18 @@ export async function sendGroupWithExactOutbox(context, groupId, plaintextBytes,
1019
1100
  JSON.stringify(existing.groupMemberUserIds) !== JSON.stringify(groupMemberUserIds)) {
1020
1101
  throw new Error('A clientMessageId cannot identify two different logical sends');
1021
1102
  }
1103
+ // A send that ended at its posting deadline posts nothing again.
1104
+ if (!existing.result && existing.outcome)
1105
+ throw storedSendEndError(existing);
1106
+ context.bindEvidence(existing);
1022
1107
  if (context.deviceSets && context.relay) {
1023
1108
  return replayDeclaredGroupIntent(context, context.deviceSets, existing, plaintextBytes);
1024
1109
  }
1110
+ // An undeclared intent completes with syncPending only when the posting
1111
+ // deadline stopped its sync posts, so a replay posts nothing.
1025
1112
  if (existing.result) {
1026
1113
  await completeSendLog(context, existing);
1027
- return existing.result;
1114
+ return outgoingMessageIntentResult(existing);
1028
1115
  }
1029
1116
  return sendStoredGroupIntent(context, await rereadStoredGroupDevices(context, existing, plaintextBytes), plaintextBytes);
1030
1117
  }
@@ -1046,13 +1133,16 @@ export async function sendGroupWithExactOutbox(context, groupId, plaintextBytes,
1046
1133
  const syncMessages = context.relay
1047
1134
  ? await context.prepareSyncMessages(groupId, contentBytes, clientTimestamp, options.clientMessageId, await context.resolveOwnDevices())
1048
1135
  : [];
1136
+ const createdAt = Date.now();
1049
1137
  const intent = {
1050
1138
  kind: 'group',
1051
1139
  clientMessageId: options.clientMessageId,
1052
1140
  recipientId: groupId,
1053
1141
  plaintextDigest,
1054
1142
  clientTimestamp,
1055
- createdAt: Date.now(),
1143
+ createdAt,
1144
+ deadlineAt: outgoingMessageDeadlineAt({ createdAt, clientTimestamp }),
1145
+ progressFromStart: true,
1056
1146
  transportMode: groupSharedMessage
1057
1147
  ? context.deliveryMode === 'required'
1058
1148
  ? 'sealed_sender_required'
@@ -1071,13 +1161,15 @@ export async function sendGroupWithExactOutbox(context, groupId, plaintextBytes,
1071
1161
  }),
1072
1162
  ...(groupSharedMessage && { groupSharedMessage }),
1073
1163
  ...(options.contentHint !== undefined && { contentHint: options.contentHint }),
1164
+ ...intentBinaryAttachment(options),
1074
1165
  };
1075
1166
  await storeSendLog(context, intent, contentBytes);
1076
1167
  await storeOutgoingMessageIntent(context.storage, intent);
1168
+ context.bindEvidence(intent);
1077
1169
  if (context.deviceSets && context.relay) {
1078
1170
  return sendDeclaredGroupIntent(context, context.deviceSets, intent, plaintextBytes);
1079
1171
  }
1080
1172
  return sendStoredGroupIntent(context, intent, plaintextBytes);
1081
- });
1173
+ }, report);
1082
1174
  }
1083
1175
  //# sourceMappingURL=group-outbox.js.map