@open-e2ee/signal-protocol-sdk 0.1.0-alpha.8 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/ADAPTERS.md +12 -12
  2. package/ARCHITECTURE.md +3 -3
  3. package/CHANGELOG.md +461 -0
  4. package/README.md +25 -15
  5. package/SECURITY.md +1 -1
  6. package/client/README.md +2 -2
  7. package/device/README.md +3 -1
  8. package/dist/client/client.d.ts +6 -6
  9. package/dist/client/client.js +6 -6
  10. package/dist/client/compose.d.ts +2 -2
  11. package/dist/client/index.d.ts +2 -2
  12. package/dist/device/expo-metadata.d.ts +26 -0
  13. package/dist/device/expo-metadata.d.ts.map +1 -0
  14. package/dist/device/expo-metadata.js +33 -0
  15. package/dist/device/expo-metadata.js.map +1 -0
  16. package/dist/device/index.d.ts +1 -1
  17. package/dist/device/index.d.ts.map +1 -1
  18. package/dist/device/index.js +5 -1
  19. package/dist/device/index.js.map +1 -1
  20. package/dist/device/provisioning.d.ts +4 -12
  21. package/dist/device/provisioning.d.ts.map +1 -1
  22. package/dist/device/provisioning.js +4 -21
  23. package/dist/device/provisioning.js.map +1 -1
  24. package/dist/index.d.ts +5 -5
  25. package/dist/index.js +5 -5
  26. package/dist/internal/crypto/symmetric/aes.d.ts.map +1 -1
  27. package/dist/internal/crypto/symmetric/aes.js +27 -30
  28. package/dist/internal/crypto/symmetric/aes.js.map +1 -1
  29. package/dist/internal/encoding/proto/primitives.d.ts +261 -6
  30. package/dist/internal/encoding/proto/primitives.d.ts.map +1 -1
  31. package/dist/internal/encoding/proto/primitives.js +468 -6
  32. package/dist/internal/encoding/proto/primitives.js.map +1 -1
  33. package/dist/internal/encoding/proto/sender-key-message.d.ts +4 -3
  34. package/dist/internal/encoding/proto/sender-key-message.d.ts.map +1 -1
  35. package/dist/internal/encoding/proto/sender-key-message.js +97 -76
  36. package/dist/internal/encoding/proto/sender-key-message.js.map +1 -1
  37. package/dist/internal/encoding/proto/signal-message.d.ts +7 -2
  38. package/dist/internal/encoding/proto/signal-message.d.ts.map +1 -1
  39. package/dist/internal/encoding/proto/signal-message.js +194 -110
  40. package/dist/internal/encoding/proto/signal-message.js.map +1 -1
  41. package/dist/internal/groups/manager.d.ts +24 -5
  42. package/dist/internal/groups/manager.d.ts.map +1 -1
  43. package/dist/internal/groups/manager.js +33 -11
  44. package/dist/internal/groups/manager.js.map +1 -1
  45. package/dist/internal/groups/server-engine.d.ts +29 -14
  46. package/dist/internal/groups/server-engine.d.ts.map +1 -1
  47. package/dist/internal/groups/server-engine.js +63 -14
  48. package/dist/internal/groups/server-engine.js.map +1 -1
  49. package/dist/internal/manager/manager.d.ts +2 -2
  50. package/dist/internal/protocol/sealed-sender/proto/index.d.ts +50 -4
  51. package/dist/internal/protocol/sealed-sender/proto/index.d.ts.map +1 -1
  52. package/dist/internal/protocol/sealed-sender/proto/index.js +273 -151
  53. package/dist/internal/protocol/sealed-sender/proto/index.js.map +1 -1
  54. package/dist/internal/protocol/spqr/ml-kem-braid/serialize.d.ts +7 -1
  55. package/dist/internal/protocol/spqr/ml-kem-braid/serialize.d.ts.map +1 -1
  56. package/dist/internal/protocol/spqr/ml-kem-braid/serialize.js +334 -151
  57. package/dist/internal/protocol/spqr/ml-kem-braid/serialize.js.map +1 -1
  58. package/dist/layers.d.ts +2 -2
  59. package/dist/layers.d.ts.map +1 -1
  60. package/dist/layers.js +2 -2
  61. package/dist/layers.js.map +1 -1
  62. package/dist/local/store/index.d.ts +3 -3
  63. package/dist/local/store/index.d.ts.map +1 -1
  64. package/dist/local/store/index.js +4 -4
  65. package/dist/local/store/index.js.map +1 -1
  66. package/dist/local/store/{mock → memory}/adapter.d.ts +10 -10
  67. package/dist/local/store/memory/adapter.d.ts.map +1 -0
  68. package/dist/local/store/{mock → memory}/adapter.js +5 -5
  69. package/dist/local/store/memory/adapter.js.map +1 -0
  70. package/dist/local/store/{mock → memory}/failures.d.ts +6 -6
  71. package/dist/local/store/memory/failures.d.ts.map +1 -0
  72. package/dist/local/store/{mock → memory}/failures.js +6 -6
  73. package/dist/local/store/memory/failures.js.map +1 -0
  74. package/dist/local/store/memory/index.d.ts +11 -0
  75. package/dist/local/store/memory/index.d.ts.map +1 -0
  76. package/dist/local/store/memory/index.js +7 -0
  77. package/dist/local/store/memory/index.js.map +1 -0
  78. package/dist/local/store/node/adapter.d.ts +53 -4
  79. package/dist/local/store/node/adapter.d.ts.map +1 -1
  80. package/dist/local/store/node/adapter.js +456 -9
  81. package/dist/local/store/node/adapter.js.map +1 -1
  82. package/dist/local/store/node/database.d.ts +68 -0
  83. package/dist/local/store/node/database.d.ts.map +1 -1
  84. package/dist/local/store/node/database.js +201 -18
  85. package/dist/local/store/node/database.js.map +1 -1
  86. package/dist/local/store/react-native/adapter.d.ts.map +1 -1
  87. package/dist/local/store/react-native/adapter.js +69 -1
  88. package/dist/local/store/react-native/adapter.js.map +1 -1
  89. package/dist/local/store/react-native/backend-conformance.d.ts +66 -0
  90. package/dist/local/store/react-native/backend-conformance.d.ts.map +1 -0
  91. package/dist/local/store/react-native/backend-conformance.js +326 -0
  92. package/dist/local/store/react-native/backend-conformance.js.map +1 -0
  93. package/dist/local/store/react-native/index.d.ts +9 -4
  94. package/dist/local/store/react-native/index.d.ts.map +1 -1
  95. package/dist/local/store/react-native/index.js +2 -0
  96. package/dist/local/store/react-native/index.js.map +1 -1
  97. package/dist/local/store/react-native/reference-backend.d.ts +42 -0
  98. package/dist/local/store/react-native/reference-backend.d.ts.map +1 -0
  99. package/dist/local/store/react-native/reference-backend.js +177 -0
  100. package/dist/local/store/react-native/reference-backend.js.map +1 -0
  101. package/dist/local/store/react-native/storage.d.ts +9 -0
  102. package/dist/local/store/react-native/storage.d.ts.map +1 -1
  103. package/dist/local/store/web/adapter.d.ts +11 -1
  104. package/dist/local/store/web/adapter.d.ts.map +1 -1
  105. package/dist/local/store/web/adapter.js +196 -33
  106. package/dist/local/store/web/adapter.js.map +1 -1
  107. package/dist/local/store/web/index.d.ts +3 -2
  108. package/dist/local/store/web/index.d.ts.map +1 -1
  109. package/dist/local/store/web/index.js.map +1 -1
  110. package/dist/local/vault/index.d.ts +5 -1
  111. package/dist/local/vault/index.d.ts.map +1 -1
  112. package/dist/local/vault/index.js +1 -1
  113. package/dist/local/vault/index.js.map +1 -1
  114. package/dist/remote/relay/convex/backend.d.ts +9 -6
  115. package/dist/remote/relay/convex/backend.d.ts.map +1 -1
  116. package/dist/remote/relay/convex/backend.js +4 -1
  117. package/dist/remote/relay/convex/backend.js.map +1 -1
  118. package/dist/remote/relay/convex/component/_generated/component.d.ts +10 -7
  119. package/dist/remote/relay/convex/component/_generated/component.d.ts.map +1 -1
  120. package/dist/remote/relay/convex/component/certificates.d.ts.map +1 -1
  121. package/dist/remote/relay/convex/component/certificates.js +26 -31
  122. package/dist/remote/relay/convex/component/certificates.js.map +1 -1
  123. package/dist/remote/relay/convex/component/cleanup.d.ts +6 -0
  124. package/dist/remote/relay/convex/component/cleanup.d.ts.map +1 -1
  125. package/dist/remote/relay/convex/component/cleanup.js +19 -0
  126. package/dist/remote/relay/convex/component/cleanup.js.map +1 -1
  127. package/dist/remote/relay/convex/component/crons.d.ts.map +1 -1
  128. package/dist/remote/relay/convex/component/crons.js +1 -0
  129. package/dist/remote/relay/convex/component/crons.js.map +1 -1
  130. package/dist/remote/relay/convex/component/groups.d.ts +9 -6
  131. package/dist/remote/relay/convex/component/groups.d.ts.map +1 -1
  132. package/dist/remote/relay/convex/component/groups.js +21 -11
  133. package/dist/remote/relay/convex/component/groups.js.map +1 -1
  134. package/dist/remote/relay/convex/component/messages.d.ts.map +1 -1
  135. package/dist/remote/relay/convex/component/messages.js +90 -16
  136. package/dist/remote/relay/convex/component/messages.js.map +1 -1
  137. package/dist/remote/relay/convex/component/schema.d.ts +15 -1
  138. package/dist/remote/relay/convex/component/schema.js +21 -0
  139. package/dist/remote/relay/convex/component/schema.js.map +1 -1
  140. package/dist/remote/relay/convex/group-server.d.ts +6 -5
  141. package/dist/remote/relay/convex/group-server.d.ts.map +1 -1
  142. package/dist/remote/relay/convex/group-server.js +14 -10
  143. package/dist/remote/relay/convex/group-server.js.map +1 -1
  144. package/dist/remote/relay/convex/relay.d.ts +2 -2
  145. package/dist/remote/relay/convex/relay.d.ts.map +1 -1
  146. package/dist/remote/relay/convex/relay.js +10 -7
  147. package/dist/remote/relay/convex/relay.js.map +1 -1
  148. package/dist/remote/relay/index.d.ts +1 -1
  149. package/dist/remote/relay/index.d.ts.map +1 -1
  150. package/dist/remote/relay/index.js +2 -2
  151. package/dist/remote/relay/index.js.map +1 -1
  152. package/dist/remote/relay/{mock → memory}/adapter.d.ts +19 -19
  153. package/dist/remote/relay/memory/adapter.d.ts.map +1 -0
  154. package/dist/remote/relay/{mock → memory}/adapter.js +24 -19
  155. package/dist/remote/relay/memory/adapter.js.map +1 -0
  156. package/dist/remote/relay/{mock → memory}/failures.d.ts +5 -5
  157. package/dist/remote/relay/memory/failures.d.ts.map +1 -0
  158. package/dist/remote/relay/{mock → memory}/failures.js +1 -1
  159. package/dist/remote/relay/memory/failures.js.map +1 -0
  160. package/dist/remote/relay/{mock → memory}/group-server.d.ts +1 -1
  161. package/dist/remote/relay/memory/group-server.d.ts.map +1 -0
  162. package/dist/remote/relay/{mock → memory}/group-server.js +1 -1
  163. package/dist/remote/relay/memory/group-server.js.map +1 -0
  164. package/dist/remote/relay/memory/index.d.ts +11 -0
  165. package/dist/remote/relay/memory/index.d.ts.map +1 -0
  166. package/dist/remote/relay/memory/index.js +7 -0
  167. package/dist/remote/relay/memory/index.js.map +1 -0
  168. package/dist/remote/relay/types.d.ts +25 -6
  169. package/dist/remote/relay/types.d.ts.map +1 -1
  170. package/dist/types/errors.d.ts +33 -0
  171. package/dist/types/errors.d.ts.map +1 -1
  172. package/dist/types/errors.js +36 -0
  173. package/dist/types/errors.js.map +1 -1
  174. package/docs/ASSURANCE.md +50 -15
  175. package/docs/CLIENT_COMPOSITION.md +3 -3
  176. package/docs/GETTING_STARTED.md +11 -11
  177. package/docs/PACKAGE_SURFACE.md +10 -8
  178. package/docs/RECIPES.md +12 -12
  179. package/local/store/README.md +98 -9
  180. package/local/store/{mock → memory}/README.md +8 -8
  181. package/local/store/node/README.md +6 -8
  182. package/local/store/react-native/README.md +34 -2
  183. package/local/store/web/README.md +30 -3
  184. package/package.json +13 -10
  185. package/remote/README.md +7 -7
  186. package/remote/relay/README.md +5 -5
  187. package/remote/relay/{mock → memory}/README.md +8 -8
  188. package/dist/local/store/mock/adapter.d.ts.map +0 -1
  189. package/dist/local/store/mock/adapter.js.map +0 -1
  190. package/dist/local/store/mock/failures.d.ts.map +0 -1
  191. package/dist/local/store/mock/failures.js.map +0 -1
  192. package/dist/local/store/mock/index.d.ts +0 -11
  193. package/dist/local/store/mock/index.d.ts.map +0 -1
  194. package/dist/local/store/mock/index.js +0 -7
  195. package/dist/local/store/mock/index.js.map +0 -1
  196. package/dist/remote/relay/mock/adapter.d.ts.map +0 -1
  197. package/dist/remote/relay/mock/adapter.js.map +0 -1
  198. package/dist/remote/relay/mock/failures.d.ts.map +0 -1
  199. package/dist/remote/relay/mock/failures.js.map +0 -1
  200. package/dist/remote/relay/mock/group-server.d.ts.map +0 -1
  201. package/dist/remote/relay/mock/group-server.js.map +0 -1
  202. package/dist/remote/relay/mock/index.d.ts +0 -11
  203. package/dist/remote/relay/mock/index.d.ts.map +0 -1
  204. package/dist/remote/relay/mock/index.js +0 -7
  205. package/dist/remote/relay/mock/index.js.map +0 -1
@@ -24,7 +24,9 @@ import type { SessionState } from '../../../types/index.js';
24
24
  import { type SessionRecord } from '../../../types/session.js';
25
25
  import { ProtocolAddress } from '../../../types/address.js';
26
26
  import { IdentityKeyChange, TrustDirection } from '../../../types/trust.js';
27
- import type { SessionTrustCommit } from '../../../types/index.js';
27
+ import type { MessageRecord, SessionTrustCommit, UserRecord, DeviceRecord } from '../../../types/index.js';
28
+ import type { ISignalProtocolLocalStore, SkippedSenderMessageKey } from '../../../types/api.js';
29
+ import type { SenderKeyState } from '../../../internal/protocol/sender-keys/manager.js';
28
30
  /**
29
31
  * Node.js storage adapter configuration
30
32
  */
@@ -37,10 +39,8 @@ export interface NodeSignalProtocolStoreConfig {
37
39
  *
38
40
  * Provides encrypted filesystem storage for Signal Protocol keys.
39
41
  */
40
- export declare class NodeSignalProtocolStore {
42
+ export declare class NodeSignalProtocolStore implements ISignalProtocolLocalStore {
41
43
  private db;
42
- private registrationIds;
43
- private kyberUsage;
44
44
  constructor(config?: NodeSignalProtocolStoreConfig);
45
45
  initialize(): Promise<void>;
46
46
  storeIdentityKey(keyPair: IdentityKeyPair, identityType?: IdentityType): Promise<void>;
@@ -78,6 +78,55 @@ export declare class NodeSignalProtocolStore {
78
78
  hasSession(address: ProtocolAddress): Promise<boolean>;
79
79
  getSessionCount(): Promise<number>;
80
80
  getDatabaseKey(): Promise<Uint8Array>;
81
+ getUserRecord(userId: string): Promise<UserRecord | null>;
82
+ setUserRecord(userId: string, record: UserRecord): Promise<void>;
83
+ getDeviceRecord(userId: string, deviceId: number): Promise<DeviceRecord | null>;
84
+ setDeviceRecord(userId: string, deviceId: number, record: DeviceRecord): Promise<void>;
85
+ deleteDeviceRecord(userId: string, deviceId: number): Promise<void>;
86
+ getDeviceSession(userId: string, deviceId: number): Promise<SessionRecord | null>;
87
+ setDeviceSession(userId: string, deviceId: number, session: SessionRecord): Promise<void>;
88
+ deleteStaleRecords(maxLatency: number): Promise<number>;
89
+ cleanupExpiredSessions(maxRecv: number): Promise<number>;
90
+ getAllUserIds(): Promise<string[]>;
91
+ getSesameDeviceIds(userId: string): Promise<number[]>;
92
+ storeSenderKey(groupId: string, userId: string, deviceId: number, state: SenderKeyState): Promise<void>;
93
+ getSenderKey(groupId: string, userId: string, deviceId: number): Promise<SenderKeyState | null>;
94
+ deleteSenderKey(groupId: string, userId: string, deviceId: number): Promise<void>;
95
+ resolveGroupForSenderKeyId(senderKeyId: string, userId: string, deviceId: number): Promise<string | null>;
96
+ getAllSenderKeysForGroup(groupId: string): Promise<SenderKeyState[]>;
97
+ deleteAllSenderKeysForGroup(groupId: string): Promise<number>;
98
+ storeSenderKeyRecord(groupId: string, userId: string, deviceId: number, states: SenderKeyState[]): Promise<void>;
99
+ getSenderKeyRecord(groupId: string, userId: string, deviceId: number): Promise<SenderKeyState[] | null>;
100
+ storeSkippedSenderKey(groupId: string, senderId: string, senderDeviceId: number, chainIndex: number, messageKey: SkippedSenderMessageKey): Promise<void>;
101
+ getSkippedSenderKey(groupId: string, senderId: string, senderDeviceId: number, chainIndex: number): Promise<SkippedSenderMessageKey | null>;
102
+ deleteSkippedSenderKey(groupId: string, senderId: string, senderDeviceId: number, chainIndex: number): Promise<void>;
103
+ countSkippedSenderKeys(groupId: string, senderId: string, senderDeviceId: number): Promise<number>;
104
+ deleteOldestSkippedSenderKeys(groupId: string, senderId: string, senderDeviceId: number, count: number): Promise<number>;
105
+ storeMessageRecord(record: MessageRecord): Promise<void>;
106
+ getMessageRecord(sessionId: string, timestamp: number): Promise<MessageRecord | null>;
107
+ deleteMessageRecord(sessionId: string, timestamp: number): Promise<void>;
108
+ deleteExpiredMessageRecords(maxAgeMs: number): Promise<number>;
109
+ clearAllMessageRecords(): Promise<number>;
110
+ deleteMessageRecordsForSession(sessionId: string): Promise<number>;
111
+ getMetadata(key: string): Promise<string | null>;
112
+ setMetadata(key: string, value: string): Promise<void>;
113
+ getEcSignedPreKeyMaxId(identityType?: IdentityType): Promise<number>;
114
+ getKyberPreKeyMaxId(identityType?: IdentityType): Promise<number>;
115
+ deleteAllPreKeys(identityType?: IdentityType): Promise<{
116
+ ecSignedPreKeys: number;
117
+ ecOneTimePreKeys: number;
118
+ kyberPreKeys: number;
119
+ kemOneTimePreKeys: number;
120
+ }>;
121
+ clearAllSessions(): Promise<void>;
122
+ getDetailedStats(): Promise<{
123
+ sessions: number;
124
+ ecSignedPreKeys: number;
125
+ ecOneTimePreKeys: number;
126
+ kyberPreKeys: number;
127
+ kemOneTimePreKeys: number;
128
+ users: number;
129
+ }>;
81
130
  clearAllKeys(): Promise<void>;
82
131
  private getAddressKey;
83
132
  private getContactIdentityKey;
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../../../local/store/node/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACd,eAAe,EACf,WAAW,EACX,gBAAgB,EACjB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAQpG,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAEL,KAAK,aAAa,EACnB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAIzE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAGzD;;GAEG;AACH,OAAO,EAAE,CAAC;AACV,MAAM,WAAW,6BAA6B;IAC5C,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;GAIG;AACH,qBAAa,uBAAuB;IAClC,OAAO,CAAC,EAAE,CAAwB;IAClC,OAAO,CAAC,eAAe,CAAmC;IAC1D,OAAO,CAAC,UAAU,CAA2E;IAE7F,YAAY,MAAM,CAAC,EAAE,6BAA6B,EAEjD;IAMK,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAEhC;IAMK,gBAAgB,CAAC,OAAO,EAAE,eAAe,EAAE,YAAY,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAG3F;IAEK,cAAc,CAAC,YAAY,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAGjF;IAEK,cAA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K,gBAAgB,IAAI,OAAO,CAAC;QAChC,QAAQ,EAAE,MAAM,CAAC;QACjB,eAAe,EAAE,MAAM,CAAC;QACxB,gBAAgB,EAAE,MAAM,CAAC;QACzB,YAAY,EAAE,MAAM,CAAC;QACrB,iBAAiB,EAAE,MAAM,CAAC;QAC1B,KAAK,EAAE,MAAM,CAAC;KACf,CAAC,CAED;IAMK,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC,CAElC;IAMD,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,qBAAqB;CAG9B"}
@@ -24,7 +24,79 @@ import { ProtocolAddress } from '../../../types/address.js';
24
24
  import { IdentityKeyChange } from '../../../types/trust.js';
25
25
  import { EncryptionError, EncryptionErrorCode } from '../../../types/index.js';
26
26
  import { getNodeEncryptedDatabase } from './database.js';
27
+ import { encodeCompositeIdentityV1, UNPINNED_DEVICE_IDENTITY_KEY } from '../../../keys/identity.js';
27
28
  import { deserializeSessionRecord, serializeSessionRecord } from '../session-codec.js';
29
+ function serializeDeviceRecord(record) {
30
+ return {
31
+ ...record,
32
+ identityKey: Array.from(record.identityKey),
33
+ session: record.session ? serializeSessionRecord(record.session) : null,
34
+ };
35
+ }
36
+ function deserializeDeviceRecord(data) {
37
+ return {
38
+ ...data,
39
+ identityKey: Uint8Array.from(data.identityKey),
40
+ session: data.session ? deserializeSessionRecord(data.session) : null,
41
+ };
42
+ }
43
+ function serializeUserRecord(record) {
44
+ return {
45
+ ...record,
46
+ devices: Array.from(record.devices.entries(), ([deviceId, device]) => [
47
+ deviceId,
48
+ serializeDeviceRecord(device),
49
+ ]),
50
+ };
51
+ }
52
+ function deserializeUserRecord(data) {
53
+ return {
54
+ ...data,
55
+ devices: new Map(data.devices.map(([deviceId, device]) => [deviceId, deserializeDeviceRecord(device)])),
56
+ };
57
+ }
58
+ /**
59
+ * Decode a persisted device session, treating an unreadable one as absent.
60
+ *
61
+ * `getSessionRecord` already drops a session it cannot decode instead of
62
+ * failing the caller. A maintenance sweep that threw on the same record would
63
+ * abort the whole pass, leaving a store that can never expire anything again.
64
+ */
65
+ function readStoredSession(serialized) {
66
+ if (!serialized)
67
+ return null;
68
+ try {
69
+ return deserializeSessionRecord(serialized);
70
+ }
71
+ catch {
72
+ return null;
73
+ }
74
+ }
75
+ /** Read a nested dictionary level, creating it on demand for writes. */
76
+ function branch(parent, key, create) {
77
+ const existing = parent[key];
78
+ if (existing !== undefined)
79
+ return existing;
80
+ const created = create();
81
+ parent[key] = created;
82
+ return created;
83
+ }
84
+ /** Same, for a level whose value is itself a dictionary. */
85
+ function subtree(parent, key) {
86
+ return branch(parent, key, () => Object.create(null));
87
+ }
88
+ /** Remove one device from a group/user/device tree, pruning levels left empty. */
89
+ function pruneSenderKeyDevice(tree, groupId, userId, deviceId) {
90
+ const users = tree[groupId];
91
+ const devices = users?.[userId];
92
+ if (!users || !devices)
93
+ return;
94
+ delete devices[String(deviceId)];
95
+ if (Object.keys(devices).length === 0)
96
+ delete users[userId];
97
+ if (Object.keys(users).length === 0)
98
+ delete tree[groupId];
99
+ }
28
100
  /**
29
101
  * Node.js Storage Adapter
30
102
  *
@@ -32,8 +104,6 @@ import { deserializeSessionRecord, serializeSessionRecord } from '../session-cod
32
104
  */
33
105
  export class NodeSignalProtocolStore {
34
106
  db;
35
- registrationIds = new Map();
36
- kyberUsage = new Map();
37
107
  constructor(config) {
38
108
  this.db = getNodeEncryptedDatabase(config?.dataDir);
39
109
  }
@@ -59,12 +129,13 @@ export class NodeSignalProtocolStore {
59
129
  return key !== null;
60
130
  }
61
131
  async getLocalRegistrationId(identityType) {
62
- const it = identityType ?? 'aci';
63
- return this.registrationIds.get(it) ?? 0;
132
+ return await this.db.getRegistrationId(identityType ?? 'aci');
64
133
  }
65
134
  async setLocalRegistrationId(id, identityType) {
66
- const it = identityType ?? 'aci';
67
- this.registrationIds.set(it, id);
135
+ // A registration ID identifies this install for the lifetime of the
136
+ // install. Holding it in memory would hand every restart a fresh one and
137
+ // make every peer read the process as a reinstall.
138
+ await this.db.setRegistrationId(identityType ?? 'aci', id);
68
139
  }
69
140
  // ============================================================================
70
141
  // Identity Verification (Contact Identity Keys)
@@ -170,7 +241,10 @@ export class NodeSignalProtocolStore {
170
241
  }
171
242
  async markKyberPreKeyUsed(kyberPreKeyId, signedPreKeyId, baseKeyBytes, identityType) {
172
243
  const it = identityType ?? 'aci';
173
- this.kyberUsage.set(`${it}:${kyberPreKeyId}`, { signedPreKeyId, baseKeyBytes });
244
+ await this.db.markKyberPreKeyUsed(`${it}:${kyberPreKeyId}`, {
245
+ signedPreKeyId,
246
+ baseKeyBytes: Array.from(baseKeyBytes),
247
+ });
174
248
  }
175
249
  // ============================================================================
176
250
  // Session Management
@@ -254,12 +328,385 @@ export class NodeSignalProtocolStore {
254
328
  return key;
255
329
  }
256
330
  // ============================================================================
331
+ // SESAME Multi-Device Session Management
332
+ //
333
+ // A device's session is the same session the protocol store holds for that
334
+ // address. Writes go to both so `getSessionRecord` and `getDeviceSession`
335
+ // never disagree about which ratchet is current.
336
+ // ============================================================================
337
+ async getUserRecord(userId) {
338
+ const { users } = await this.db.readSesameState();
339
+ const stored = users[userId];
340
+ return stored ? deserializeUserRecord(stored) : null;
341
+ }
342
+ async setUserRecord(userId, record) {
343
+ const serialized = serializeUserRecord(record);
344
+ await this.db.mutateSesameState(({ users, sessions }) => {
345
+ users[userId] = serialized;
346
+ for (const [deviceId, device] of serialized.devices) {
347
+ if (!device.session)
348
+ continue;
349
+ sessions[this.getAddressKey({ userId, deviceId })] = {
350
+ userId,
351
+ deviceId,
352
+ serializedRecord: device.session,
353
+ };
354
+ }
355
+ });
356
+ }
357
+ async getDeviceRecord(userId, deviceId) {
358
+ // The live session is read through `getSessionRecord` rather than out of the
359
+ // device record, so a session this store would refuse to hand the protocol
360
+ // layer is not handed to the SESAME layer either.
361
+ const session = await this.getSessionRecord(ProtocolAddress.create(userId, deviceId));
362
+ const { users } = await this.db.readSesameState();
363
+ const stored = users[userId]?.devices.find(([id]) => id === deviceId)?.[1] ?? null;
364
+ if (!session)
365
+ return stored ? deserializeDeviceRecord(stored) : null;
366
+ const now = Date.now();
367
+ return {
368
+ ...(stored ? deserializeDeviceRecord(stored) : {}),
369
+ userId,
370
+ deviceId,
371
+ // The identity pin is a trust decision, so it comes from what was
372
+ // persisted rather than from whichever session happens to be current.
373
+ // Re-deriving it would unpin a device the moment its session is archived.
374
+ identityKey: stored?.identityKey.length
375
+ ? Uint8Array.from(stored.identityKey)
376
+ : session.currentSession?.remoteIdentity
377
+ ? encodeCompositeIdentityV1(session.currentSession.remoteIdentity)
378
+ : UNPINNED_DEVICE_IDENTITY_KEY,
379
+ session,
380
+ createdAt: session.metadata?.createdAt ?? stored?.createdAt ?? now,
381
+ updatedAt: now,
382
+ };
383
+ }
384
+ async setDeviceRecord(userId, deviceId, record) {
385
+ const serialized = serializeDeviceRecord(record);
386
+ await this.db.mutateSesameState(({ users, sessions }) => {
387
+ const now = Date.now();
388
+ const user = branch(users, userId, () => ({
389
+ userId,
390
+ devices: [],
391
+ createdAt: now,
392
+ updatedAt: now,
393
+ }));
394
+ const index = user.devices.findIndex(([id]) => id === deviceId);
395
+ if (index >= 0)
396
+ user.devices[index] = [deviceId, serialized];
397
+ else
398
+ user.devices.push([deviceId, serialized]);
399
+ user.updatedAt = now;
400
+ if (serialized.session) {
401
+ sessions[this.getAddressKey({ userId, deviceId })] = {
402
+ userId,
403
+ deviceId,
404
+ serializedRecord: serialized.session,
405
+ };
406
+ }
407
+ });
408
+ }
409
+ async deleteDeviceRecord(userId, deviceId) {
410
+ await this.db.mutateSesameState(({ users, sessions }) => {
411
+ delete sessions[this.getAddressKey({ userId, deviceId })];
412
+ const user = users[userId];
413
+ if (!user)
414
+ return;
415
+ user.devices = user.devices.filter(([id]) => id !== deviceId);
416
+ user.updatedAt = Date.now();
417
+ if (user.devices.length === 0)
418
+ delete users[userId];
419
+ });
420
+ }
421
+ async getDeviceSession(userId, deviceId) {
422
+ return (await this.getDeviceRecord(userId, deviceId))?.session ?? null;
423
+ }
424
+ async setDeviceSession(userId, deviceId, session) {
425
+ const record = await this.getDeviceRecord(userId, deviceId);
426
+ if (!record)
427
+ throw new Error(`Device record not found: ${userId}:${deviceId}`);
428
+ record.session = session;
429
+ record.updatedAt = Date.now();
430
+ await this.setDeviceRecord(userId, deviceId, record);
431
+ }
432
+ async deleteStaleRecords(maxLatency) {
433
+ return await this.db.mutateSesameState(({ users }) => {
434
+ const now = Date.now();
435
+ let deleted = 0;
436
+ for (const userId of Object.keys(users)) {
437
+ const user = users[userId];
438
+ const kept = user.devices.filter(([, device]) => {
439
+ const record = readStoredSession(device.session);
440
+ const hasNoSessions = !record ||
441
+ (!record.currentSession && Object.keys(record.archivedSessions).length === 0);
442
+ return !(hasNoSessions && now - device.createdAt > maxLatency);
443
+ });
444
+ deleted += user.devices.length - kept.length;
445
+ user.devices = kept;
446
+ user.updatedAt = now;
447
+ if (user.devices.length === 0)
448
+ delete users[userId];
449
+ }
450
+ return deleted;
451
+ });
452
+ }
453
+ async cleanupExpiredSessions(maxRecv) {
454
+ return await this.db.mutateSesameState(({ users, sessions }) => {
455
+ const now = Date.now();
456
+ let deleted = 0;
457
+ for (const user of Object.values(users)) {
458
+ for (const [deviceId, device] of user.devices) {
459
+ if (!device.session)
460
+ continue;
461
+ const record = readStoredSession(device.session);
462
+ if (!record) {
463
+ device.session = null;
464
+ delete sessions[this.getAddressKey({ userId: user.userId, deviceId })];
465
+ continue;
466
+ }
467
+ const createdAt = record.metadata?.createdAt ?? 0;
468
+ if (now - createdAt > maxRecv) {
469
+ if (record.currentSession)
470
+ deleted++;
471
+ deleted += Object.keys(record.archivedSessions).length;
472
+ device.session = null;
473
+ delete sessions[this.getAddressKey({ userId: user.userId, deviceId })];
474
+ }
475
+ else {
476
+ for (const [baseKey, state] of Object.entries(record.archivedSessions)) {
477
+ if (now - (state?.lastUsedAt ?? createdAt) <= maxRecv)
478
+ continue;
479
+ delete record.archivedSessions[baseKey];
480
+ deleted++;
481
+ }
482
+ device.session = serializeSessionRecord(record);
483
+ sessions[this.getAddressKey({ userId: user.userId, deviceId })] = {
484
+ userId: user.userId,
485
+ deviceId,
486
+ serializedRecord: device.session,
487
+ };
488
+ }
489
+ device.updatedAt = now;
490
+ }
491
+ user.updatedAt = now;
492
+ }
493
+ return deleted;
494
+ });
495
+ }
496
+ async getAllUserIds() {
497
+ const { users } = await this.db.readSesameState();
498
+ return Object.keys(users);
499
+ }
500
+ async getSesameDeviceIds(userId) {
501
+ const { users } = await this.db.readSesameState();
502
+ return users[userId]?.devices.map(([deviceId]) => deviceId) ?? [];
503
+ }
504
+ // ============================================================================
505
+ // Sender Keys (Group Messaging)
506
+ // ============================================================================
507
+ async storeSenderKey(groupId, userId, deviceId, state) {
508
+ await this.db.mutateSenderKeyState(({ current }) => {
509
+ const users = subtree(current, groupId);
510
+ subtree(users, userId)[String(deviceId)] = state;
511
+ });
512
+ }
513
+ async getSenderKey(groupId, userId, deviceId) {
514
+ const { current } = await this.db.readSenderKeyState();
515
+ return current[groupId]?.[userId]?.[String(deviceId)] ?? null;
516
+ }
517
+ async deleteSenderKey(groupId, userId, deviceId) {
518
+ await this.db.mutateSenderKeyState(({ current, records }) => {
519
+ pruneSenderKeyDevice(current, groupId, userId, deviceId);
520
+ pruneSenderKeyDevice(records, groupId, userId, deviceId);
521
+ });
522
+ }
523
+ async resolveGroupForSenderKeyId(senderKeyId, userId, deviceId) {
524
+ if (!senderKeyId)
525
+ return null;
526
+ const { current, records } = await this.db.readSenderKeyState();
527
+ const device = String(deviceId);
528
+ for (const groupId of new Set([...Object.keys(current), ...Object.keys(records)])) {
529
+ if (current[groupId]?.[userId]?.[device]?.senderKeyId === senderKeyId)
530
+ return groupId;
531
+ // A message encrypted just before a rotation still names the superseded
532
+ // key, so the retained previous states have to be searched too.
533
+ const retained = records[groupId]?.[userId]?.[device];
534
+ if (retained?.some((state) => state.senderKeyId === senderKeyId))
535
+ return groupId;
536
+ }
537
+ return null;
538
+ }
539
+ async getAllSenderKeysForGroup(groupId) {
540
+ const { current } = await this.db.readSenderKeyState();
541
+ const users = current[groupId];
542
+ if (!users)
543
+ return [];
544
+ return Object.values(users).flatMap((devices) => Object.values(devices));
545
+ }
546
+ async deleteAllSenderKeysForGroup(groupId) {
547
+ return await this.db.mutateSenderKeyState(({ current, records, skipped }) => {
548
+ const users = current[groupId];
549
+ const count = users
550
+ ? Object.values(users).reduce((total, devices) => total + Object.keys(devices).length, 0)
551
+ : 0;
552
+ // Each identifier is its own key, so deleting one group cannot reach a
553
+ // group whose name merely shares a prefix with it.
554
+ delete current[groupId];
555
+ delete records[groupId];
556
+ delete skipped[groupId];
557
+ return count;
558
+ });
559
+ }
560
+ async storeSenderKeyRecord(groupId, userId, deviceId, states) {
561
+ if (states.length === 0)
562
+ return;
563
+ await this.db.mutateSenderKeyState(({ current, records }) => {
564
+ const device = String(deviceId);
565
+ subtree(subtree(current, groupId), userId)[device] = states[0];
566
+ subtree(subtree(records, groupId), userId)[device] = states;
567
+ });
568
+ }
569
+ async getSenderKeyRecord(groupId, userId, deviceId) {
570
+ const { current, records } = await this.db.readSenderKeyState();
571
+ const device = String(deviceId);
572
+ const record = records[groupId]?.[userId]?.[device];
573
+ if (record)
574
+ return record;
575
+ const state = current[groupId]?.[userId]?.[device];
576
+ return state ? [state] : null;
577
+ }
578
+ // ============================================================================
579
+ // Skipped Sender Keys (Out-of-Order Group Messages)
580
+ // ============================================================================
581
+ async storeSkippedSenderKey(groupId, senderId, senderDeviceId, chainIndex, messageKey) {
582
+ await this.db.mutateSenderKeyState(({ skipped }) => {
583
+ const senders = subtree(skipped, groupId);
584
+ const devices = subtree(senders, senderId);
585
+ subtree(devices, String(senderDeviceId))[String(chainIndex)] = messageKey;
586
+ });
587
+ }
588
+ async getSkippedSenderKey(groupId, senderId, senderDeviceId, chainIndex) {
589
+ const { skipped } = await this.db.readSenderKeyState();
590
+ const stored = skipped[groupId]?.[senderId]?.[String(senderDeviceId)]?.[String(chainIndex)];
591
+ return stored ?? null;
592
+ }
593
+ async deleteSkippedSenderKey(groupId, senderId, senderDeviceId, chainIndex) {
594
+ await this.db.mutateSenderKeyState(({ skipped }) => {
595
+ const keys = skipped[groupId]?.[senderId]?.[String(senderDeviceId)];
596
+ if (keys)
597
+ delete keys[String(chainIndex)];
598
+ });
599
+ }
600
+ async countSkippedSenderKeys(groupId, senderId, senderDeviceId) {
601
+ const { skipped } = await this.db.readSenderKeyState();
602
+ const keys = skipped[groupId]?.[senderId]?.[String(senderDeviceId)];
603
+ return keys ? Object.keys(keys).length : 0;
604
+ }
605
+ async deleteOldestSkippedSenderKeys(groupId, senderId, senderDeviceId, count) {
606
+ return await this.db.mutateSenderKeyState(({ skipped }) => {
607
+ const keys = skipped[groupId]?.[senderId]?.[String(senderDeviceId)];
608
+ if (!keys)
609
+ return 0;
610
+ // Chain index is the message order, so the lowest indices are the oldest.
611
+ const oldest = Object.keys(keys)
612
+ .sort((a, b) => Number(a) - Number(b))
613
+ .slice(0, count);
614
+ for (const chainIndex of oldest)
615
+ delete keys[chainIndex];
616
+ return oldest.length;
617
+ });
618
+ }
619
+ // ============================================================================
620
+ // Message Records (SESAME Retry Support)
621
+ // ============================================================================
622
+ async storeMessageRecord(record) {
623
+ await this.db.mutateMessageRecords((records) => {
624
+ subtree(records, record.sessionId)[String(record.timestamp)] = record;
625
+ });
626
+ }
627
+ async getMessageRecord(sessionId, timestamp) {
628
+ const records = await this.db.readMessageRecords();
629
+ return records[sessionId]?.[String(timestamp)] ?? null;
630
+ }
631
+ async deleteMessageRecord(sessionId, timestamp) {
632
+ await this.db.mutateMessageRecords((records) => {
633
+ const session = records[sessionId];
634
+ if (!session)
635
+ return;
636
+ delete session[String(timestamp)];
637
+ if (Object.keys(session).length === 0)
638
+ delete records[sessionId];
639
+ });
640
+ }
641
+ async deleteExpiredMessageRecords(maxAgeMs) {
642
+ const cutoff = Date.now() - maxAgeMs;
643
+ return await this.db.mutateMessageRecords((records) => {
644
+ let deleted = 0;
645
+ for (const sessionId of Object.keys(records)) {
646
+ const session = records[sessionId];
647
+ for (const key of Object.keys(session)) {
648
+ if (session[key].createdAt >= cutoff)
649
+ continue;
650
+ delete session[key];
651
+ deleted++;
652
+ }
653
+ if (Object.keys(session).length === 0)
654
+ delete records[sessionId];
655
+ }
656
+ return deleted;
657
+ });
658
+ }
659
+ async clearAllMessageRecords() {
660
+ return await this.db.mutateMessageRecords((records) => {
661
+ let deleted = 0;
662
+ for (const sessionId of Object.keys(records)) {
663
+ deleted += Object.keys(records[sessionId]).length;
664
+ delete records[sessionId];
665
+ }
666
+ return deleted;
667
+ });
668
+ }
669
+ async deleteMessageRecordsForSession(sessionId) {
670
+ return await this.db.mutateMessageRecords((records) => {
671
+ const deleted = Object.keys(records[sessionId] ?? {}).length;
672
+ delete records[sessionId];
673
+ return deleted;
674
+ });
675
+ }
676
+ // ============================================================================
677
+ // Metadata
678
+ // ============================================================================
679
+ async getMetadata(key) {
680
+ return await this.db.getMetadataValue(key);
681
+ }
682
+ async setMetadata(key, value) {
683
+ await this.db.setMetadataValue(key, value);
684
+ }
685
+ // ============================================================================
686
+ // Key Recovery (PQXDH §4.13 identifier-collision recovery)
687
+ // ============================================================================
688
+ async getEcSignedPreKeyMaxId(identityType) {
689
+ return await this.db.getEcSignedPreKeyMaxId(identityType ?? 'aci');
690
+ }
691
+ async getKyberPreKeyMaxId(identityType) {
692
+ return await this.db.getKyberPreKeyMaxId(identityType ?? 'aci');
693
+ }
694
+ async deleteAllPreKeys(identityType) {
695
+ return await this.db.deleteAllPreKeys(identityType ?? 'aci');
696
+ }
697
+ async clearAllSessions() {
698
+ // A device record whose session is gone is not a device this store can
699
+ // reach, so the SESAME records go with the sessions in the same commit.
700
+ await this.db.clearSesameState();
701
+ }
702
+ async getDetailedStats() {
703
+ return await this.db.getDetailedStats();
704
+ }
705
+ // ============================================================================
257
706
  // Utility
258
707
  // ============================================================================
259
708
  async clearAllKeys() {
260
709
  await this.db.deleteDatabase();
261
- this.registrationIds.clear();
262
- this.kyberUsage.clear();
263
710
  }
264
711
  // ============================================================================
265
712
  // Helper Methods