@vinzsocket/baileys 2.8.10 → 2.8.30

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 (83) hide show
  1. package/lib/Defaults/index.js +75 -73
  2. package/lib/Signal/Group/ciphertext-message.js +9 -9
  3. package/lib/Signal/Group/group-session-builder.js +21 -21
  4. package/lib/Signal/Group/group_cipher.js +79 -81
  5. package/lib/Signal/Group/index.js +2 -2
  6. package/lib/Signal/Group/keyhelper.js +9 -9
  7. package/lib/Signal/Group/queue-job.js +43 -46
  8. package/lib/Signal/Group/sender-chain-key.js +24 -25
  9. package/lib/Signal/Group/sender-key-distribution-message.js +50 -58
  10. package/lib/Signal/Group/sender-key-message.js +55 -60
  11. package/lib/Signal/Group/sender-key-name.js +38 -39
  12. package/lib/Signal/Group/sender-key-record.js +39 -41
  13. package/lib/Signal/Group/sender-key-state.js +85 -89
  14. package/lib/Signal/Group/sender-message-key.js +22 -22
  15. package/lib/Signal/libsignal.js +387 -292
  16. package/lib/Signal/lid-mapping.js +98 -93
  17. package/lib/Socket/Client/types.js +6 -6
  18. package/lib/Socket/Client/websocket.js +44 -44
  19. package/lib/Socket/business.js +350 -365
  20. package/lib/Socket/chats.js +1019 -914
  21. package/lib/Socket/communities.js +454 -376
  22. package/lib/Socket/groups.js +378 -310
  23. package/lib/Socket/index.js +11 -11
  24. package/lib/Socket/messages-recv.js +1355 -1182
  25. package/lib/Socket/messages-send.js +1224 -1081
  26. package/lib/Socket/mex.js +40 -34
  27. package/lib/Socket/newsletter.js +212 -169
  28. package/lib/Socket/socket.js +896 -757
  29. package/lib/Store/index.js +1 -1
  30. package/lib/Store/make-cache-manager-store.js +75 -79
  31. package/lib/Store/make-in-memory-store.js +450 -389
  32. package/lib/Store/make-ordered-dictionary.js +73 -76
  33. package/lib/Store/object-repository.js +24 -22
  34. package/lib/Types/Chat.js +1 -7
  35. package/lib/Types/Label.js +20 -20
  36. package/lib/Types/LabelAssociation.js +2 -2
  37. package/lib/Types/Message.js +2 -2
  38. package/lib/Types/Newsletter.js +24 -24
  39. package/lib/Types/Socket.js +1 -1
  40. package/lib/Types/State.js +8 -8
  41. package/lib/Types/index.js +10 -10
  42. package/lib/Utils/auth-utils.js +408 -401
  43. package/lib/Utils/baileys-event-stream.js +44 -35
  44. package/lib/Utils/business.js +200 -197
  45. package/lib/Utils/chat-utils.js +775 -708
  46. package/lib/Utils/crypto.js +97 -85
  47. package/lib/Utils/decode-wa-message.js +240 -227
  48. package/lib/Utils/event-buffer.js +547 -518
  49. package/lib/Utils/extended-messages.js +250 -203
  50. package/lib/Utils/generics.js +300 -289
  51. package/lib/Utils/history.js +76 -69
  52. package/lib/Utils/lid-resolver.js +47 -42
  53. package/lib/Utils/link-preview.js +75 -65
  54. package/lib/Utils/logger.js +3 -1
  55. package/lib/Utils/lt-hash.js +38 -34
  56. package/lib/Utils/make-mutex.js +32 -34
  57. package/lib/Utils/mention.js +79 -86
  58. package/lib/Utils/message-retry-manager.js +148 -136
  59. package/lib/Utils/messages-media.js +652 -562
  60. package/lib/Utils/messages.js +753 -707
  61. package/lib/Utils/noise-handler.js +156 -135
  62. package/lib/Utils/process-message.js +404 -341
  63. package/lib/Utils/signal.js +180 -118
  64. package/lib/Utils/use-multi-file-auth-state.js +87 -91
  65. package/lib/Utils/validate-connection.js +167 -142
  66. package/lib/WABinary/constants.js +28 -1293
  67. package/lib/WABinary/decode.js +220 -218
  68. package/lib/WABinary/encode.js +199 -201
  69. package/lib/WABinary/generic-utils.js +80 -77
  70. package/lib/WABinary/jid-utils.js +41 -35
  71. package/lib/WAM/BinaryInfo.js +7 -7
  72. package/lib/WAM/constants.js +1 -15241
  73. package/lib/WAM/encode.js +124 -131
  74. package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
  75. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +51 -47
  76. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -22
  77. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -32
  78. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -19
  79. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -43
  80. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +17 -17
  81. package/lib/WAUSync/USyncQuery.js +82 -84
  82. package/lib/WAUSync/USyncUser.js +20 -20
  83. package/package.json +4 -2
@@ -9,306 +9,401 @@ import { SenderKeyRecord } from './Group/sender-key-record.js';
9
9
  import { GroupCipher, GroupSessionBuilder, SenderKeyDistributionMessage } from './Group/index.js';
10
10
  import { LIDMappingStore } from './lid-mapping.js';
11
11
  export function makeLibSignalRepository(auth, onWhatsAppFunc) {
12
- const lidMapping = new LIDMappingStore(auth.keys, onWhatsAppFunc);
13
- const storage = signalStorage(auth, lidMapping);
14
- const parsedKeys = auth.keys;
15
- function isLikelySyncMessage(addr) {
16
- const key = addr.toString();
17
- // Only bypass for WhatsApp system addresses, not regular user contacts
18
- // Be very specific about sync service patterns
19
- return (key.includes('@lid.whatsapp.net') || // WhatsApp system messages
20
- key.includes('@broadcast') || // Broadcast messages
21
- key.includes('@newsletter'));
22
- }
23
- // Simple operation-level deduplication (5 minutes)
24
- const recentMigrations = new LRUCache({
25
- max: 500,
26
- ttl: 5 * 60 * 1000
27
- });
28
- const repository = {
29
- decryptGroupMessage({ group, authorJid, msg }) {
30
- const senderName = jidToSignalSenderKeyName(group, authorJid);
31
- const cipher = new GroupCipher(storage, senderName);
32
- // Use transaction to ensure atomicity
33
- return parsedKeys.transaction(async () => {
34
- return cipher.decrypt(msg);
35
- }, group);
36
- },
37
- async processSenderKeyDistributionMessage({ item, authorJid }) {
38
- const builder = new GroupSessionBuilder(storage);
39
- if (!item.groupId) {
40
- throw new Error('Group ID is required for sender key distribution message');
41
- }
42
- const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
43
- const senderMsg = new SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
44
- const senderNameStr = senderName.toString();
45
- const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
46
- if (!senderKey) {
47
- await storage.storeSenderKey(senderName, new SenderKeyRecord());
48
- }
49
- return parsedKeys.transaction(async () => {
50
- const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
51
- if (!senderKey) {
52
- await storage.storeSenderKey(senderName, new SenderKeyRecord());
53
- }
54
- await builder.process(senderName, senderMsg);
55
- }, item.groupId);
56
- },
57
- async decryptMessage({ jid, type, ciphertext }) {
58
- const addr = jidToSignalProtocolAddress(jid);
59
- const session = new libsignal.SessionCipher(storage, addr);
60
- async function doDecrypt() {
61
- let result;
62
- switch (type) {
63
- case 'pkmsg':
64
- result = await session.decryptPreKeyWhisperMessage(ciphertext);
65
- break;
66
- case 'msg':
67
- result = await session.decryptWhisperMessage(ciphertext);
68
- break;
69
- }
70
- return result;
71
- }
72
- if (isLikelySyncMessage(addr)) {
73
- // If it's a sync message, we can skip the transaction
74
- // as it is likely to be a system message that doesn't require strict atomicity
75
- return await doDecrypt();
76
- }
77
- // If it's not a sync message, we need to ensure atomicity
78
- // For regular messages, we use a transaction to ensure atomicity
79
- return parsedKeys.transaction(async () => {
80
- return await doDecrypt();
81
- }, jid);
82
- },
83
- async encryptMessage({ jid, data }) {
84
- // LID SINGLE SOURCE OF TRUTH: Always prefer LID when available
85
- let encryptionJid = jid;
86
- // Check for LID mapping and use it if session exists
87
- if (jid.includes('@s.whatsapp.net')) {
88
- const lidForPN = await lidMapping.getLIDForPN(jid);
89
- if (lidForPN?.includes('@lid')) {
90
- const lidAddr = jidToSignalProtocolAddress(lidForPN);
91
- const { [lidAddr.toString()]: lidSession } = await auth.keys.get('session', [lidAddr.toString()]);
92
- if (lidSession) {
93
- // LID session exists, use it
94
- encryptionJid = lidForPN;
95
- }
96
- else {
97
- // Try to migrate if PN session exists
98
- const pnAddr = jidToSignalProtocolAddress(jid);
99
- const { [pnAddr.toString()]: pnSession } = await auth.keys.get('session', [pnAddr.toString()]);
100
- if (pnSession) {
101
- // Migrate PN to LID
102
- await repository.migrateSession(jid, lidForPN);
103
- encryptionJid = lidForPN;
104
- }
105
- }
106
- }
107
- }
108
- const addr = jidToSignalProtocolAddress(encryptionJid);
109
- const cipher = new libsignal.SessionCipher(storage, addr);
110
- // Use transaction to ensure atomicity
111
- return parsedKeys.transaction(async () => {
112
- const { type: sigType, body } = await cipher.encrypt(data);
113
- const type = sigType === 3 ? 'pkmsg' : 'msg';
114
- return { type, ciphertext: Buffer.from(body, 'binary') };
115
- }, jid);
116
- },
117
- async encryptGroupMessage({ group, meId, data }) {
118
- const senderName = jidToSignalSenderKeyName(group, meId);
119
- const builder = new GroupSessionBuilder(storage);
120
- const senderNameStr = senderName.toString();
121
- return parsedKeys.transaction(async () => {
122
- const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
123
- if (!senderKey) {
124
- await storage.storeSenderKey(senderName, new SenderKeyRecord());
125
- }
126
- const senderKeyDistributionMessage = await builder.create(senderName);
127
- const session = new GroupCipher(storage, senderName);
128
- const ciphertext = await session.encrypt(data);
129
- return {
130
- ciphertext,
131
- senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
132
- };
133
- }, group);
134
- },
135
- async injectE2ESession({ jid, session }) {
136
- const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
137
- return parsedKeys.transaction(async () => {
138
- await cipher.initOutgoing(session);
139
- }, jid);
140
- },
141
- jidToSignalProtocolAddress(jid) {
142
- return jidToSignalProtocolAddress(jid).toString();
143
- },
144
- async storeLIDPNMapping(lid, pn) {
145
- await lidMapping.storeLIDPNMapping(lid, pn);
146
- },
147
- getLIDMappingStore() {
148
- return lidMapping;
149
- },
150
- async validateSession(jid) {
151
- try {
152
- const addr = jidToSignalProtocolAddress(jid);
153
- const session = await storage.loadSession(addr.toString());
154
- if (!session) {
155
- return { exists: false, reason: 'no session' };
156
- }
157
- if (!session.haveOpenSession()) {
158
- return { exists: false, reason: 'no open session' };
159
- }
160
- return { exists: true };
161
- }
162
- catch (error) {
163
- return { exists: false, reason: 'validation error' };
164
- }
165
- },
166
- async deleteSession(jid) {
167
- const addr = jidToSignalProtocolAddress(jid);
168
- return parsedKeys.transaction(async () => {
169
- await auth.keys.set({ session: { [addr.toString()]: null } });
170
- }, jid);
171
- },
172
- async migrateSession(fromJid, toJid) {
173
- // Only migrate PN → LID
174
- if (!fromJid.includes('@s.whatsapp.net') || !toJid.includes('@lid')) {
175
- return;
176
- }
177
- const fromDecoded = jidDecode(fromJid);
178
- const toDecoded = jidDecode(toJid);
179
- if (!fromDecoded || !toDecoded)
180
- return;
181
- const deviceId = fromDecoded.device || 0;
182
- const migrationKey = `${fromDecoded.user}.${deviceId}→${toDecoded.user}.${deviceId}`;
183
- // Check if recently migrated (5 min window)
184
- if (recentMigrations.has(migrationKey)) {
185
- return;
12
+ const lidMapping = new LIDMappingStore(auth.keys, onWhatsAppFunc);
13
+ const storage = signalStorage(auth, lidMapping);
14
+ const parsedKeys = auth.keys;
15
+ function isLikelySyncMessage(addr) {
16
+ const key = addr.toString();
17
+ // Only bypass for WhatsApp system addresses, not regular user contacts
18
+ // Be very specific about sync service patterns
19
+ return key.includes('@lid.whatsapp.net') ||
20
+ // WhatsApp system messages
21
+ key.includes('@broadcast') ||
22
+ // Broadcast messages
23
+ key.includes('@newsletter');
24
+ }
25
+ // Simple operation-level deduplication (5 minutes)
26
+ const recentMigrations = new LRUCache({
27
+ max: 500,
28
+ ttl: 5 * 60 * 1000
29
+ });
30
+ const repository = {
31
+ decryptGroupMessage({
32
+ group,
33
+ authorJid,
34
+ msg
35
+ }) {
36
+ const senderName = jidToSignalSenderKeyName(group, authorJid);
37
+ const cipher = new GroupCipher(storage, senderName);
38
+ // Use transaction to ensure atomicity
39
+ return parsedKeys.transaction(async () => {
40
+ return cipher.decrypt(msg);
41
+ }, group);
42
+ },
43
+ async processSenderKeyDistributionMessage({
44
+ item,
45
+ authorJid
46
+ }) {
47
+ const builder = new GroupSessionBuilder(storage);
48
+ if (!item.groupId) {
49
+ throw new Error('Group ID is required for sender key distribution message');
50
+ }
51
+ const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
52
+ const senderMsg = new SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
53
+ const senderNameStr = senderName.toString();
54
+ const {
55
+ [senderNameStr]: senderKey
56
+ } = await auth.keys.get('sender-key', [senderNameStr]);
57
+ if (!senderKey) {
58
+ await storage.storeSenderKey(senderName, new SenderKeyRecord());
59
+ }
60
+ return parsedKeys.transaction(async () => {
61
+ const {
62
+ [senderNameStr]: senderKey
63
+ } = await auth.keys.get('sender-key', [senderNameStr]);
64
+ if (!senderKey) {
65
+ await storage.storeSenderKey(senderName, new SenderKeyRecord());
66
+ }
67
+ await builder.process(senderName, senderMsg);
68
+ }, item.groupId);
69
+ },
70
+ async decryptMessage({
71
+ jid,
72
+ type,
73
+ ciphertext
74
+ }) {
75
+ const addr = jidToSignalProtocolAddress(jid);
76
+ const session = new libsignal.SessionCipher(storage, addr);
77
+ async function doDecrypt() {
78
+ let result;
79
+ switch (type) {
80
+ case 'pkmsg':
81
+ result = await session.decryptPreKeyWhisperMessage(ciphertext);
82
+ break;
83
+ case 'msg':
84
+ result = await session.decryptWhisperMessage(ciphertext);
85
+ break;
86
+ }
87
+ return result;
88
+ }
89
+ if (isLikelySyncMessage(addr)) {
90
+ // If it's a sync message, we can skip the transaction
91
+ // as it is likely to be a system message that doesn't require strict atomicity
92
+ return await doDecrypt();
93
+ }
94
+ // If it's not a sync message, we need to ensure atomicity
95
+ // For regular messages, we use a transaction to ensure atomicity
96
+ return parsedKeys.transaction(async () => {
97
+ return await doDecrypt();
98
+ }, jid);
99
+ },
100
+ async encryptMessage({
101
+ jid,
102
+ data
103
+ }) {
104
+ // LID SINGLE SOURCE OF TRUTH: Always prefer LID when available
105
+ let encryptionJid = jid;
106
+ // Check for LID mapping and use it if session exists
107
+ if (jid.includes('@s.whatsapp.net')) {
108
+ const lidForPN = await lidMapping.getLIDForPN(jid);
109
+ if (lidForPN?.includes('@lid')) {
110
+ const lidAddr = jidToSignalProtocolAddress(lidForPN);
111
+ const {
112
+ [lidAddr.toString()]: lidSession
113
+ } = await auth.keys.get('session', [lidAddr.toString()]);
114
+ if (lidSession) {
115
+ // LID session exists, use it
116
+ encryptionJid = lidForPN;
117
+ } else {
118
+ // Try to migrate if PN session exists
119
+ const pnAddr = jidToSignalProtocolAddress(jid);
120
+ const {
121
+ [pnAddr.toString()]: pnSession
122
+ } = await auth.keys.get('session', [pnAddr.toString()]);
123
+ if (pnSession) {
124
+ // Migrate PN to LID
125
+ await repository.migrateSession(jid, lidForPN);
126
+ encryptionJid = lidForPN;
186
127
  }
187
- // Check if LID session already exists
188
- const lidAddr = jidToSignalProtocolAddress(toJid);
189
- const { [lidAddr.toString()]: lidExists } = await auth.keys.get('session', [lidAddr.toString()]);
190
- if (lidExists) {
191
- recentMigrations.set(migrationKey, true);
192
- return;
128
+ }
129
+ }
130
+ }
131
+ const addr = jidToSignalProtocolAddress(encryptionJid);
132
+ const cipher = new libsignal.SessionCipher(storage, addr);
133
+ // Use transaction to ensure atomicity
134
+ return parsedKeys.transaction(async () => {
135
+ const {
136
+ type: sigType,
137
+ body
138
+ } = await cipher.encrypt(data);
139
+ const type = sigType === 3 ? 'pkmsg' : 'msg';
140
+ return {
141
+ type,
142
+ ciphertext: Buffer.from(body, 'binary')
143
+ };
144
+ }, jid);
145
+ },
146
+ async encryptGroupMessage({
147
+ group,
148
+ meId,
149
+ data
150
+ }) {
151
+ const senderName = jidToSignalSenderKeyName(group, meId);
152
+ const builder = new GroupSessionBuilder(storage);
153
+ const senderNameStr = senderName.toString();
154
+ return parsedKeys.transaction(async () => {
155
+ const {
156
+ [senderNameStr]: senderKey
157
+ } = await auth.keys.get('sender-key', [senderNameStr]);
158
+ if (!senderKey) {
159
+ await storage.storeSenderKey(senderName, new SenderKeyRecord());
160
+ }
161
+ const senderKeyDistributionMessage = await builder.create(senderName);
162
+ const session = new GroupCipher(storage, senderName);
163
+ const ciphertext = await session.encrypt(data);
164
+ return {
165
+ ciphertext,
166
+ senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
167
+ };
168
+ }, group);
169
+ },
170
+ async injectE2ESession({
171
+ jid,
172
+ session
173
+ }) {
174
+ const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
175
+ return parsedKeys.transaction(async () => {
176
+ await cipher.initOutgoing(session);
177
+ }, jid);
178
+ },
179
+ jidToSignalProtocolAddress(jid) {
180
+ return jidToSignalProtocolAddress(jid).toString();
181
+ },
182
+ async storeLIDPNMapping(lid, pn) {
183
+ await lidMapping.storeLIDPNMapping(lid, pn);
184
+ },
185
+ getLIDMappingStore() {
186
+ return lidMapping;
187
+ },
188
+ async validateSession(jid) {
189
+ try {
190
+ const addr = jidToSignalProtocolAddress(jid);
191
+ const session = await storage.loadSession(addr.toString());
192
+ if (!session) {
193
+ return {
194
+ exists: false,
195
+ reason: 'no session'
196
+ };
197
+ }
198
+ if (!session.haveOpenSession()) {
199
+ return {
200
+ exists: false,
201
+ reason: 'no open session'
202
+ };
203
+ }
204
+ return {
205
+ exists: true
206
+ };
207
+ } catch (error) {
208
+ return {
209
+ exists: false,
210
+ reason: 'validation error'
211
+ };
212
+ }
213
+ },
214
+ async deleteSession(jid) {
215
+ const addr = jidToSignalProtocolAddress(jid);
216
+ return parsedKeys.transaction(async () => {
217
+ await auth.keys.set({
218
+ session: {
219
+ [addr.toString()]: null
220
+ }
221
+ });
222
+ }, jid);
223
+ },
224
+ async migrateSession(fromJid, toJid) {
225
+ // Only migrate PN → LID
226
+ if (!fromJid.includes('@s.whatsapp.net') || !toJid.includes('@lid')) {
227
+ return;
228
+ }
229
+ const fromDecoded = jidDecode(fromJid);
230
+ const toDecoded = jidDecode(toJid);
231
+ if (!fromDecoded || !toDecoded) return;
232
+ const deviceId = fromDecoded.device || 0;
233
+ const migrationKey = `${fromDecoded.user}.${deviceId}→${toDecoded.user}.${deviceId}`;
234
+ // Check if recently migrated (5 min window)
235
+ if (recentMigrations.has(migrationKey)) {
236
+ return;
237
+ }
238
+ // Check if LID session already exists
239
+ const lidAddr = jidToSignalProtocolAddress(toJid);
240
+ const {
241
+ [lidAddr.toString()]: lidExists
242
+ } = await auth.keys.get('session', [lidAddr.toString()]);
243
+ if (lidExists) {
244
+ recentMigrations.set(migrationKey, true);
245
+ return;
246
+ }
247
+ return parsedKeys.transaction(async () => {
248
+ // Store mapping
249
+ await lidMapping.storeLIDPNMapping(toJid, fromJid);
250
+ // Load and copy session
251
+ const fromAddr = jidToSignalProtocolAddress(fromJid);
252
+ const fromSession = await storage.loadSession(fromAddr.toString());
253
+ if (fromSession?.haveOpenSession()) {
254
+ // Deep copy session to prevent reference issues
255
+ const sessionBytes = fromSession.serialize();
256
+ const copiedSession = libsignal.SessionRecord.deserialize(sessionBytes);
257
+ // Store at LID address
258
+ await storage.storeSession(lidAddr.toString(), copiedSession);
259
+ // Delete PN session - maintain single encryption layer
260
+ await auth.keys.set({
261
+ session: {
262
+ [fromAddr.toString()]: null
193
263
  }
194
- return parsedKeys.transaction(async () => {
195
- // Store mapping
196
- await lidMapping.storeLIDPNMapping(toJid, fromJid);
197
- // Load and copy session
198
- const fromAddr = jidToSignalProtocolAddress(fromJid);
199
- const fromSession = await storage.loadSession(fromAddr.toString());
200
- if (fromSession?.haveOpenSession()) {
201
- // Deep copy session to prevent reference issues
202
- const sessionBytes = fromSession.serialize();
203
- const copiedSession = libsignal.SessionRecord.deserialize(sessionBytes);
204
- // Store at LID address
205
- await storage.storeSession(lidAddr.toString(), copiedSession);
206
- // Delete PN session - maintain single encryption layer
207
- await auth.keys.set({ session: { [fromAddr.toString()]: null } });
208
- }
209
- recentMigrations.set(migrationKey, true);
210
- }, fromJid);
211
- },
212
- async encryptMessageWithWire({ encryptionJid, wireJid, data }) {
213
- const result = await repository.encryptMessage({ jid: encryptionJid, data });
214
- return { ...result, wireJid };
215
- },
216
- destroy() {
217
- recentMigrations.clear();
264
+ });
218
265
  }
219
- };
220
- return repository;
266
+ recentMigrations.set(migrationKey, true);
267
+ }, fromJid);
268
+ },
269
+ async encryptMessageWithWire({
270
+ encryptionJid,
271
+ wireJid,
272
+ data
273
+ }) {
274
+ const result = await repository.encryptMessage({
275
+ jid: encryptionJid,
276
+ data
277
+ });
278
+ return {
279
+ ...result,
280
+ wireJid
281
+ };
282
+ },
283
+ destroy() {
284
+ recentMigrations.clear();
285
+ }
286
+ };
287
+ return repository;
221
288
  }
222
- const jidToSignalProtocolAddress = (jid) => {
223
- const decoded = jidDecode(jid);
224
- const { user, device, server } = decoded;
225
- // LID addresses get _1 suffix for Signal protocol
226
- const signalUser = server === 'lid' ? `${user}_1` : user;
227
- const finalDevice = device || 0;
228
- return new libsignal.ProtocolAddress(signalUser, finalDevice);
289
+ const jidToSignalProtocolAddress = jid => {
290
+ const decoded = jidDecode(jid);
291
+ const {
292
+ user,
293
+ device,
294
+ server
295
+ } = decoded;
296
+ // LID addresses get _1 suffix for Signal protocol
297
+ const signalUser = server === 'lid' ? `${user}_1` : user;
298
+ const finalDevice = device || 0;
299
+ return new libsignal.ProtocolAddress(signalUser, finalDevice);
229
300
  };
230
301
  const jidToSignalSenderKeyName = (group, user) => {
231
- return new SenderKeyName(group, jidToSignalProtocolAddress(user));
302
+ return new SenderKeyName(group, jidToSignalProtocolAddress(user));
232
303
  };
233
- function signalStorage({ creds, keys }, lidMapping) {
234
- return {
235
- loadSession: async (id) => {
236
- try {
237
- // LID SINGLE SOURCE OF TRUTH: Auto-redirect PN to LID if mapping exists
238
- let actualId = id;
239
- if (id.includes('.') && !id.includes('_1')) {
240
- // This is a PN signal address format (e.g., "1234567890.0")
241
- // Convert back to JID to check for LID mapping
242
- const parts = id.split('.');
243
- const device = parts[1] || '0';
244
- const pnJid = device === '0' ? `${parts[0]}@s.whatsapp.net` : `${parts[0]}:${device}@s.whatsapp.net`;
245
- const lidForPN = await lidMapping.getLIDForPN(pnJid);
246
- if (lidForPN?.includes('@lid')) {
247
- const lidAddr = jidToSignalProtocolAddress(lidForPN);
248
- const lidId = lidAddr.toString();
249
- // Check if LID session exists
250
- const { [lidId]: lidSession } = await keys.get('session', [lidId]);
251
- if (lidSession) {
252
- actualId = lidId;
253
- }
254
- }
255
- }
256
- const { [actualId]: sess } = await keys.get('session', [actualId]);
257
- if (sess) {
258
- return libsignal.SessionRecord.deserialize(sess);
259
- }
260
- }
261
- catch (e) {
262
- return null;
263
- }
264
- return null;
265
- },
266
- // TODO: Replace with libsignal.SessionRecord when type exports are added to libsignal
267
- storeSession: async (id, session) => {
268
- await keys.set({ session: { [id]: session.serialize() } });
269
- },
270
- isTrustedIdentity: () => {
271
- return true;
272
- },
273
- loadPreKey: async (id) => {
274
- const keyId = id.toString();
275
- const { [keyId]: key } = await keys.get('pre-key', [keyId]);
276
- if (key) {
277
- return {
278
- privKey: Buffer.from(key.private),
279
- pubKey: Buffer.from(key.public)
280
- };
304
+ function signalStorage({
305
+ creds,
306
+ keys
307
+ }, lidMapping) {
308
+ return {
309
+ loadSession: async id => {
310
+ try {
311
+ // LID SINGLE SOURCE OF TRUTH: Auto-redirect PN to LID if mapping exists
312
+ let actualId = id;
313
+ if (id.includes('.') && !id.includes('_1')) {
314
+ // This is a PN signal address format (e.g., "1234567890.0")
315
+ // Convert back to JID to check for LID mapping
316
+ const parts = id.split('.');
317
+ const device = parts[1] || '0';
318
+ const pnJid = device === '0' ? `${parts[0]}@s.whatsapp.net` : `${parts[0]}:${device}@s.whatsapp.net`;
319
+ const lidForPN = await lidMapping.getLIDForPN(pnJid);
320
+ if (lidForPN?.includes('@lid')) {
321
+ const lidAddr = jidToSignalProtocolAddress(lidForPN);
322
+ const lidId = lidAddr.toString();
323
+ // Check if LID session exists
324
+ const {
325
+ [lidId]: lidSession
326
+ } = await keys.get('session', [lidId]);
327
+ if (lidSession) {
328
+ actualId = lidId;
281
329
  }
282
- },
283
- removePreKey: (id) => keys.set({ 'pre-key': { [id]: null } }),
284
- loadSignedPreKey: () => {
285
- const key = creds.signedPreKey;
286
- return {
287
- privKey: Buffer.from(key.keyPair.private),
288
- pubKey: Buffer.from(key.keyPair.public)
289
- };
290
- },
291
- loadSenderKey: async (senderKeyName) => {
292
- const keyId = senderKeyName.toString();
293
- const { [keyId]: key } = await keys.get('sender-key', [keyId]);
294
- if (key) {
295
- return SenderKeyRecord.deserialize(key);
296
- }
297
- return new SenderKeyRecord();
298
- },
299
- storeSenderKey: async (senderKeyName, key) => {
300
- const keyId = senderKeyName.toString();
301
- const serialized = JSON.stringify(key.serialize());
302
- await keys.set({ 'sender-key': { [keyId]: Buffer.from(serialized, 'utf-8') } });
303
- },
304
- getOurRegistrationId: () => creds.registrationId,
305
- getOurIdentity: () => {
306
- const { signedIdentityKey } = creds;
307
- return {
308
- privKey: Buffer.from(signedIdentityKey.private),
309
- pubKey: generateSignalPubKey(signedIdentityKey.public)
310
- };
330
+ }
331
+ }
332
+ const {
333
+ [actualId]: sess
334
+ } = await keys.get('session', [actualId]);
335
+ if (sess) {
336
+ return libsignal.SessionRecord.deserialize(sess);
337
+ }
338
+ } catch (e) {
339
+ return null;
340
+ }
341
+ return null;
342
+ },
343
+ // TODO: Replace with libsignal.SessionRecord when type exports are added to libsignal
344
+ storeSession: async (id, session) => {
345
+ await keys.set({
346
+ session: {
347
+ [id]: session.serialize()
311
348
  }
312
- };
349
+ });
350
+ },
351
+ isTrustedIdentity: () => {
352
+ return true;
353
+ },
354
+ loadPreKey: async id => {
355
+ const keyId = id.toString();
356
+ const {
357
+ [keyId]: key
358
+ } = await keys.get('pre-key', [keyId]);
359
+ if (key) {
360
+ return {
361
+ privKey: Buffer.from(key.private),
362
+ pubKey: Buffer.from(key.public)
363
+ };
364
+ }
365
+ },
366
+ removePreKey: id => keys.set({
367
+ 'pre-key': {
368
+ [id]: null
369
+ }
370
+ }),
371
+ loadSignedPreKey: () => {
372
+ const key = creds.signedPreKey;
373
+ return {
374
+ privKey: Buffer.from(key.keyPair.private),
375
+ pubKey: Buffer.from(key.keyPair.public)
376
+ };
377
+ },
378
+ loadSenderKey: async senderKeyName => {
379
+ const keyId = senderKeyName.toString();
380
+ const {
381
+ [keyId]: key
382
+ } = await keys.get('sender-key', [keyId]);
383
+ if (key) {
384
+ return SenderKeyRecord.deserialize(key);
385
+ }
386
+ return new SenderKeyRecord();
387
+ },
388
+ storeSenderKey: async (senderKeyName, key) => {
389
+ const keyId = senderKeyName.toString();
390
+ const serialized = JSON.stringify(key.serialize());
391
+ await keys.set({
392
+ 'sender-key': {
393
+ [keyId]: Buffer.from(serialized, 'utf-8')
394
+ }
395
+ });
396
+ },
397
+ getOurRegistrationId: () => creds.registrationId,
398
+ getOurIdentity: () => {
399
+ const {
400
+ signedIdentityKey
401
+ } = creds;
402
+ return {
403
+ privKey: Buffer.from(signedIdentityKey.private),
404
+ pubKey: generateSignalPubKey(signedIdentityKey.public)
405
+ };
406
+ }
407
+ };
313
408
  }
314
409
  //# sourceMappingURL=libsignal.js.map