@itsliaaa/baileys 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 (103) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +1078 -0
  3. package/WAProto/index.js +100441 -0
  4. package/engine-requirements.js +10 -0
  5. package/lib/Defaults/index.js +144 -0
  6. package/lib/Signal/Group/ciphertext-message.js +11 -0
  7. package/lib/Signal/Group/group-session-builder.js +29 -0
  8. package/lib/Signal/Group/group_cipher.js +81 -0
  9. package/lib/Signal/Group/index.js +11 -0
  10. package/lib/Signal/Group/keyhelper.js +17 -0
  11. package/lib/Signal/Group/sender-chain-key.js +25 -0
  12. package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
  13. package/lib/Signal/Group/sender-key-message.js +65 -0
  14. package/lib/Signal/Group/sender-key-name.js +47 -0
  15. package/lib/Signal/Group/sender-key-record.js +40 -0
  16. package/lib/Signal/Group/sender-key-state.js +83 -0
  17. package/lib/Signal/Group/sender-message-key.js +25 -0
  18. package/lib/Signal/libsignal.js +402 -0
  19. package/lib/Signal/lid-mapping.js +270 -0
  20. package/lib/Socket/Client/index.js +2 -0
  21. package/lib/Socket/Client/types.js +10 -0
  22. package/lib/Socket/Client/websocket.js +53 -0
  23. package/lib/Socket/business.js +378 -0
  24. package/lib/Socket/chats.js +1048 -0
  25. package/lib/Socket/communities.js +430 -0
  26. package/lib/Socket/groups.js +328 -0
  27. package/lib/Socket/index.js +11 -0
  28. package/lib/Socket/messages-recv.js +1442 -0
  29. package/lib/Socket/messages-send.js +1153 -0
  30. package/lib/Socket/mex.js +41 -0
  31. package/lib/Socket/newsletter.js +227 -0
  32. package/lib/Socket/socket.js +936 -0
  33. package/lib/Store/index.js +3 -0
  34. package/lib/Store/make-in-memory-store.js +421 -0
  35. package/lib/Store/make-ordered-dictionary.js +78 -0
  36. package/lib/Store/object-repository.js +23 -0
  37. package/lib/Types/Auth.js +1 -0
  38. package/lib/Types/Bussines.js +1 -0
  39. package/lib/Types/Call.js +1 -0
  40. package/lib/Types/Chat.js +7 -0
  41. package/lib/Types/Contact.js +1 -0
  42. package/lib/Types/Events.js +1 -0
  43. package/lib/Types/GroupMetadata.js +1 -0
  44. package/lib/Types/Label.js +24 -0
  45. package/lib/Types/LabelAssociation.js +6 -0
  46. package/lib/Types/Message.js +17 -0
  47. package/lib/Types/Newsletter.js +33 -0
  48. package/lib/Types/Product.js +1 -0
  49. package/lib/Types/Signal.js +1 -0
  50. package/lib/Types/Socket.js +2 -0
  51. package/lib/Types/State.js +12 -0
  52. package/lib/Types/USync.js +1 -0
  53. package/lib/Types/index.js +25 -0
  54. package/lib/Utils/auth-utils.js +289 -0
  55. package/lib/Utils/browser-utils.js +28 -0
  56. package/lib/Utils/business.js +230 -0
  57. package/lib/Utils/chat-utils.js +811 -0
  58. package/lib/Utils/crypto.js +117 -0
  59. package/lib/Utils/decode-wa-message.js +282 -0
  60. package/lib/Utils/event-buffer.js +573 -0
  61. package/lib/Utils/generics.js +385 -0
  62. package/lib/Utils/history.js +130 -0
  63. package/lib/Utils/identity-change-handler.js +48 -0
  64. package/lib/Utils/index.js +19 -0
  65. package/lib/Utils/link-preview.js +84 -0
  66. package/lib/Utils/logger.js +2 -0
  67. package/lib/Utils/lt-hash.js +7 -0
  68. package/lib/Utils/make-mutex.js +32 -0
  69. package/lib/Utils/message-retry-manager.js +224 -0
  70. package/lib/Utils/messages-media.js +789 -0
  71. package/lib/Utils/messages.js +1832 -0
  72. package/lib/Utils/noise-handler.js +200 -0
  73. package/lib/Utils/pre-key-manager.js +105 -0
  74. package/lib/Utils/process-message.js +527 -0
  75. package/lib/Utils/reporting-utils.js +257 -0
  76. package/lib/Utils/signal.js +158 -0
  77. package/lib/Utils/sync-action-utils.js +47 -0
  78. package/lib/Utils/tc-token-utils.js +17 -0
  79. package/lib/Utils/use-multi-file-auth-state.js +120 -0
  80. package/lib/Utils/validate-connection.js +206 -0
  81. package/lib/WABinary/constants.js +1300 -0
  82. package/lib/WABinary/decode.js +261 -0
  83. package/lib/WABinary/encode.js +219 -0
  84. package/lib/WABinary/generic-utils.js +197 -0
  85. package/lib/WABinary/index.js +5 -0
  86. package/lib/WABinary/jid-utils.js +95 -0
  87. package/lib/WABinary/types.js +1 -0
  88. package/lib/WAM/BinaryInfo.js +9 -0
  89. package/lib/WAM/constants.js +22852 -0
  90. package/lib/WAM/encode.js +149 -0
  91. package/lib/WAM/index.js +3 -0
  92. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -0
  93. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
  94. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
  95. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
  96. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
  97. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  98. package/lib/WAUSync/Protocols/index.js +4 -0
  99. package/lib/WAUSync/USyncQuery.js +93 -0
  100. package/lib/WAUSync/USyncUser.js +22 -0
  101. package/lib/WAUSync/index.js +3 -0
  102. package/lib/index.js +11 -0
  103. package/package.json +72 -0
@@ -0,0 +1,402 @@
1
+ // @ts-ignore
2
+ import * as libsignal from 'libsignal';
3
+ // @ts-ignore
4
+ import { PreKeyWhisperMessage } from 'libsignal/src/protobufs.js';
5
+ import { LRUCache } from 'lru-cache';
6
+ import { generateSignalPubKey } from '../Utils/index.js';
7
+ import { isHostedLidUser, isHostedPnUser, isLidUser, isPnUser, jidDecode, transferDevice, WAJIDDomains } from '../WABinary/index.js';
8
+ import { SenderKeyName } from './Group/sender-key-name.js';
9
+ import { SenderKeyRecord } from './Group/sender-key-record.js';
10
+ import { GroupCipher, GroupSessionBuilder, SenderKeyDistributionMessage } from './Group/index.js';
11
+ import { LIDMappingStore } from './lid-mapping.js';
12
+ /** Extract identity key from PreKeyWhisperMessage for identity change detection */
13
+ function extractIdentityFromPkmsg(ciphertext) {
14
+ try {
15
+ if (!ciphertext || ciphertext.length < 2) {
16
+ return undefined;
17
+ }
18
+ // Version byte check (version 3)
19
+ const version = ciphertext[0];
20
+ if ((version & 0xf) !== 3) {
21
+ return undefined;
22
+ }
23
+ // Parse protobuf (skip version byte)
24
+ const preKeyProto = PreKeyWhisperMessage.decode(ciphertext.slice(1));
25
+ if (preKeyProto.identityKey?.length === 33) {
26
+ return new Uint8Array(preKeyProto.identityKey);
27
+ }
28
+ return undefined;
29
+ }
30
+ catch {
31
+ return undefined;
32
+ }
33
+ }
34
+ export function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
35
+ const lidMapping = new LIDMappingStore(auth.keys, logger, pnToLIDFunc);
36
+ const storage = signalStorage(auth, lidMapping);
37
+ const parsedKeys = auth.keys;
38
+ const migratedSessionCache = new LRUCache({
39
+ ttl: 3 * 24 * 60 * 60 * 1000, // 7 days
40
+ ttlAutopurge: true,
41
+ updateAgeOnGet: true
42
+ });
43
+ const repository = {
44
+ decryptGroupMessage({ group, authorJid, msg }) {
45
+ const senderName = jidToSignalSenderKeyName(group, authorJid);
46
+ const cipher = new GroupCipher(storage, senderName);
47
+ // Use transaction to ensure atomicity
48
+ return parsedKeys.transaction(async () => {
49
+ return cipher.decrypt(msg);
50
+ }, group);
51
+ },
52
+ async processSenderKeyDistributionMessage({ item, authorJid }) {
53
+ const builder = new GroupSessionBuilder(storage);
54
+ if (!item.groupId) {
55
+ throw new Error('Group ID is required for sender key distribution message');
56
+ }
57
+ const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
58
+ const senderMsg = new SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
59
+ const senderNameStr = senderName.toString();
60
+ const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
61
+ if (!senderKey) {
62
+ await storage.storeSenderKey(senderName, new SenderKeyRecord());
63
+ }
64
+ return parsedKeys.transaction(async () => {
65
+ const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
66
+ if (!senderKey) {
67
+ await storage.storeSenderKey(senderName, new SenderKeyRecord());
68
+ }
69
+ await builder.process(senderName, senderMsg);
70
+ }, item.groupId);
71
+ },
72
+ async decryptMessage({ jid, type, ciphertext }) {
73
+ const addr = jidToSignalProtocolAddress(jid);
74
+ const session = new libsignal.SessionCipher(storage, addr);
75
+ // Extract and save sender's identity key before decryption for identity change detection
76
+ if (type === 'pkmsg') {
77
+ const identityKey = extractIdentityFromPkmsg(ciphertext);
78
+ if (identityKey) {
79
+ const addrStr = addr.toString();
80
+ const identityChanged = await storage.saveIdentity(addrStr, identityKey);
81
+ if (identityChanged) {
82
+ logger.info({ jid, addr: addrStr }, 'identity key changed or new contact, session will be re-established');
83
+ }
84
+ }
85
+ }
86
+ async function doDecrypt() {
87
+ let result;
88
+ switch (type) {
89
+ case 'pkmsg':
90
+ result = await session.decryptPreKeyWhisperMessage(ciphertext);
91
+ break;
92
+ case 'msg':
93
+ result = await session.decryptWhisperMessage(ciphertext);
94
+ break;
95
+ }
96
+ return result;
97
+ }
98
+ // If it's not a sync message, we need to ensure atomicity
99
+ // For regular messages, we use a transaction to ensure atomicity
100
+ return parsedKeys.transaction(async () => {
101
+ return await doDecrypt();
102
+ }, jid);
103
+ },
104
+ async encryptMessage({ jid, data }) {
105
+ const addr = jidToSignalProtocolAddress(jid);
106
+ const cipher = new libsignal.SessionCipher(storage, addr);
107
+ // Use transaction to ensure atomicity
108
+ return parsedKeys.transaction(async () => {
109
+ const { type: sigType, body } = await cipher.encrypt(data);
110
+ const type = sigType === 3 ? 'pkmsg' : 'msg';
111
+ return { type, ciphertext: Buffer.from(body, 'binary') };
112
+ }, jid);
113
+ },
114
+ async encryptGroupMessage({ group, meId, data }) {
115
+ const senderName = jidToSignalSenderKeyName(group, meId);
116
+ const builder = new GroupSessionBuilder(storage);
117
+ const senderNameStr = senderName.toString();
118
+ return parsedKeys.transaction(async () => {
119
+ const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
120
+ if (!senderKey) {
121
+ await storage.storeSenderKey(senderName, new SenderKeyRecord());
122
+ }
123
+ const senderKeyDistributionMessage = await builder.create(senderName);
124
+ const session = new GroupCipher(storage, senderName);
125
+ const ciphertext = await session.encrypt(data);
126
+ return {
127
+ ciphertext,
128
+ senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
129
+ };
130
+ }, group);
131
+ },
132
+ async injectE2ESession({ jid, session }) {
133
+ logger.trace({ jid }, 'injecting E2EE session');
134
+ const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
135
+ return parsedKeys.transaction(async () => {
136
+ await cipher.initOutgoing(session);
137
+ }, jid);
138
+ },
139
+ jidToSignalProtocolAddress(jid) {
140
+ return jidToSignalProtocolAddress(jid).toString();
141
+ },
142
+ // Optimized direct access to LID mapping store
143
+ lidMapping,
144
+ async validateSession(jid) {
145
+ try {
146
+ const addr = jidToSignalProtocolAddress(jid);
147
+ const session = await storage.loadSession(addr.toString());
148
+ if (!session) {
149
+ return { exists: false, reason: 'no session' };
150
+ }
151
+ if (!session.haveOpenSession()) {
152
+ return { exists: false, reason: 'no open session' };
153
+ }
154
+ return { exists: true };
155
+ }
156
+ catch (error) {
157
+ return { exists: false, reason: 'validation error' };
158
+ }
159
+ },
160
+ async deleteSession(jids) {
161
+ if (!jids.length)
162
+ return;
163
+ // Convert JIDs to signal addresses and prepare for bulk deletion
164
+ const sessionUpdates = {};
165
+ jids.forEach(jid => {
166
+ const addr = jidToSignalProtocolAddress(jid);
167
+ sessionUpdates[addr.toString()] = null;
168
+ });
169
+ // Single transaction for all deletions
170
+ return parsedKeys.transaction(async () => {
171
+ await auth.keys.set({ session: sessionUpdates });
172
+ }, `delete-${jids.length}-sessions`);
173
+ },
174
+ async migrateSession(fromJid, toJid) {
175
+ // TODO: use usync to handle this entire mess
176
+ if (!fromJid || (!isLidUser(toJid) && !isHostedLidUser(toJid)))
177
+ return { migrated: 0, skipped: 0, total: 0 };
178
+ // Only support PN to LID migration
179
+ if (!isPnUser(fromJid) && !isHostedPnUser(fromJid)) {
180
+ return { migrated: 0, skipped: 0, total: 1 };
181
+ }
182
+ const { user } = jidDecode(fromJid);
183
+ logger.debug({ fromJid }, 'bulk device migration - loading all user devices');
184
+ // Get user's device list from storage
185
+ const { [user]: userDevices } = await parsedKeys.get('device-list', [user]);
186
+ if (!userDevices) {
187
+ return { migrated: 0, skipped: 0, total: 0 };
188
+ }
189
+ const { device: fromDevice } = jidDecode(fromJid);
190
+ const fromDeviceStr = fromDevice?.toString() || '0';
191
+ if (!userDevices.includes(fromDeviceStr)) {
192
+ userDevices.push(fromDeviceStr);
193
+ }
194
+ // Filter out cached devices before database fetch
195
+ const uncachedDevices = userDevices.filter(device => {
196
+ const deviceKey = `${user}.${device}`;
197
+ return !migratedSessionCache.has(deviceKey);
198
+ });
199
+ // Bulk check session existence only for uncached devices
200
+ const deviceSessionKeys = uncachedDevices.map(device => `${user}.${device}`);
201
+ const existingSessions = await parsedKeys.get('session', deviceSessionKeys);
202
+ // Step 3: Convert existing sessions to JIDs (only migrate sessions that exist)
203
+ const deviceJids = [];
204
+ for (const [sessionKey, sessionData] of Object.entries(existingSessions)) {
205
+ if (sessionData) {
206
+ // Session exists in storage
207
+ const deviceStr = sessionKey.split('.')[1];
208
+ if (!deviceStr)
209
+ continue;
210
+ const deviceNum = parseInt(deviceStr);
211
+ let jid = deviceNum === 0 ? `${user}@s.whatsapp.net` : `${user}:${deviceNum}@s.whatsapp.net`;
212
+ if (deviceNum === 99) {
213
+ jid = `${user}:99@hosted`;
214
+ }
215
+ deviceJids.push(jid);
216
+ }
217
+ }
218
+ logger.debug({
219
+ fromJid,
220
+ totalDevices: userDevices.length,
221
+ devicesWithSessions: deviceJids.length,
222
+ devices: deviceJids
223
+ }, 'bulk device migration complete - all user devices processed');
224
+ // Single transaction for all migrations
225
+ return parsedKeys.transaction(async () => {
226
+ const migrationOps = deviceJids.map(jid => {
227
+ const lidWithDevice = transferDevice(jid, toJid);
228
+ const fromDecoded = jidDecode(jid);
229
+ const toDecoded = jidDecode(lidWithDevice);
230
+ return {
231
+ fromJid: jid,
232
+ toJid: lidWithDevice,
233
+ pnUser: fromDecoded.user,
234
+ lidUser: toDecoded.user,
235
+ deviceId: fromDecoded.device || 0,
236
+ fromAddr: jidToSignalProtocolAddress(jid),
237
+ toAddr: jidToSignalProtocolAddress(lidWithDevice)
238
+ };
239
+ });
240
+ const totalOps = migrationOps.length;
241
+ let migratedCount = 0;
242
+ // Bulk fetch PN sessions - already exist (verified during device discovery)
243
+ const pnAddrStrings = Array.from(new Set(migrationOps.map(op => op.fromAddr.toString())));
244
+ const pnSessions = await parsedKeys.get('session', pnAddrStrings);
245
+ // Prepare bulk session updates (PN → LID migration + deletion)
246
+ const sessionUpdates = {};
247
+ for (const op of migrationOps) {
248
+ const pnAddrStr = op.fromAddr.toString();
249
+ const lidAddrStr = op.toAddr.toString();
250
+ const pnSession = pnSessions[pnAddrStr];
251
+ if (pnSession) {
252
+ // Session exists (guaranteed from device discovery)
253
+ const fromSession = libsignal.SessionRecord.deserialize(pnSession);
254
+ if (fromSession.haveOpenSession()) {
255
+ // Queue for bulk update: copy to LID, delete from PN
256
+ sessionUpdates[lidAddrStr] = fromSession.serialize();
257
+ sessionUpdates[pnAddrStr] = null;
258
+ migratedCount++;
259
+ }
260
+ }
261
+ }
262
+ // Single bulk session update for all migrations
263
+ if (Object.keys(sessionUpdates).length > 0) {
264
+ await parsedKeys.set({ session: sessionUpdates });
265
+ logger.debug({ migratedSessions: migratedCount }, 'bulk session migration complete');
266
+ // Cache device-level migrations
267
+ for (const op of migrationOps) {
268
+ if (sessionUpdates[op.toAddr.toString()]) {
269
+ const deviceKey = `${op.pnUser}.${op.deviceId}`;
270
+ migratedSessionCache.set(deviceKey, true);
271
+ }
272
+ }
273
+ }
274
+ const skippedCount = totalOps - migratedCount;
275
+ return { migrated: migratedCount, skipped: skippedCount, total: totalOps };
276
+ }, `migrate-${deviceJids.length}-sessions-${jidDecode(toJid)?.user}`);
277
+ }
278
+ };
279
+ return repository;
280
+ }
281
+ const jidToSignalProtocolAddress = (jid) => {
282
+ const decoded = jidDecode(jid);
283
+ const { user, device, server, domainType } = decoded;
284
+ if (!user) {
285
+ throw new Error(`JID decoded but user is empty: "${jid}" -> user: "${user}", server: "${server}", device: ${device}`);
286
+ }
287
+ const signalUser = domainType !== WAJIDDomains.WHATSAPP ? `${user}_${domainType}` : user;
288
+ const finalDevice = device || 0;
289
+ if (device === 99 && decoded.server !== 'hosted' && decoded.server !== 'hosted.lid') {
290
+ throw new Error('Unexpected non-hosted device JID with device 99. This ID seems invalid. ID:' + jid);
291
+ }
292
+ return new libsignal.ProtocolAddress(signalUser, finalDevice);
293
+ };
294
+ const jidToSignalSenderKeyName = (group, user) => {
295
+ return new SenderKeyName(group, jidToSignalProtocolAddress(user));
296
+ };
297
+ function signalStorage({ creds, keys }, lidMapping) {
298
+ // Shared function to resolve PN signal address to LID if mapping exists
299
+ const resolveLIDSignalAddress = async (id) => {
300
+ if (id.includes('.')) {
301
+ const [deviceId, device] = id.split('.');
302
+ const [user, domainType_] = deviceId.split('_');
303
+ const domainType = parseInt(domainType_ || '0');
304
+ if (domainType === WAJIDDomains.LID || domainType === WAJIDDomains.HOSTED_LID)
305
+ return id;
306
+ const pnJid = `${user}${device !== '0' ? `:${device}` : ''}@${domainType === WAJIDDomains.HOSTED ? 'hosted' : 's.whatsapp.net'}`;
307
+ const lidForPN = await lidMapping.getLIDForPN(pnJid);
308
+ if (lidForPN) {
309
+ const lidAddr = jidToSignalProtocolAddress(lidForPN);
310
+ return lidAddr.toString();
311
+ }
312
+ }
313
+ return id;
314
+ };
315
+ return {
316
+ loadSession: async (id) => {
317
+ try {
318
+ const wireJid = await resolveLIDSignalAddress(id);
319
+ const { [wireJid]: sess } = await keys.get('session', [wireJid]);
320
+ if (sess) {
321
+ return libsignal.SessionRecord.deserialize(sess);
322
+ }
323
+ }
324
+ catch (e) {
325
+ return null;
326
+ }
327
+ return null;
328
+ },
329
+ storeSession: async (id, session) => {
330
+ const wireJid = await resolveLIDSignalAddress(id);
331
+ await keys.set({ session: { [wireJid]: session.serialize() } });
332
+ },
333
+ isTrustedIdentity: () => {
334
+ return true; // TOFU - Trust on First Use (same as WhatsApp Web)
335
+ },
336
+ loadIdentityKey: async (id) => {
337
+ const wireJid = await resolveLIDSignalAddress(id);
338
+ const { [wireJid]: key } = await keys.get('identity-key', [wireJid]);
339
+ return key || undefined;
340
+ },
341
+ saveIdentity: async (id, identityKey) => {
342
+ const wireJid = await resolveLIDSignalAddress(id);
343
+ const { [wireJid]: existingKey } = await keys.get('identity-key', [wireJid]);
344
+ const keysMatch = existingKey &&
345
+ existingKey.length === identityKey.length &&
346
+ existingKey.every((byte, i) => byte === identityKey[i]);
347
+ if (existingKey && !keysMatch) {
348
+ // Identity changed - clear session and update key
349
+ await keys.set({
350
+ session: { [wireJid]: null },
351
+ 'identity-key': { [wireJid]: identityKey }
352
+ });
353
+ return true;
354
+ }
355
+ if (!existingKey) {
356
+ // New contact - Trust on First Use (TOFU)
357
+ await keys.set({ 'identity-key': { [wireJid]: identityKey } });
358
+ return true;
359
+ }
360
+ return false;
361
+ },
362
+ loadPreKey: async (id) => {
363
+ const keyId = id.toString();
364
+ const { [keyId]: key } = await keys.get('pre-key', [keyId]);
365
+ if (key) {
366
+ return {
367
+ privKey: Buffer.from(key.private),
368
+ pubKey: Buffer.from(key.public)
369
+ };
370
+ }
371
+ },
372
+ removePreKey: (id) => keys.set({ 'pre-key': { [id]: null } }),
373
+ loadSignedPreKey: () => {
374
+ const key = creds.signedPreKey;
375
+ return {
376
+ privKey: Buffer.from(key.keyPair.private),
377
+ pubKey: Buffer.from(key.keyPair.public)
378
+ };
379
+ },
380
+ loadSenderKey: async (senderKeyName) => {
381
+ const keyId = senderKeyName.toString();
382
+ const { [keyId]: key } = 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({ 'sender-key': { [keyId]: Buffer.from(serialized, 'utf-8') } });
392
+ },
393
+ getOurRegistrationId: () => creds.registrationId,
394
+ getOurIdentity: () => {
395
+ const { signedIdentityKey } = creds;
396
+ return {
397
+ privKey: Buffer.from(signedIdentityKey.private),
398
+ pubKey: Buffer.from(generateSignalPubKey(signedIdentityKey.public))
399
+ };
400
+ }
401
+ };
402
+ }
@@ -0,0 +1,270 @@
1
+ import { LRUCache } from 'lru-cache';
2
+ import { isHostedPnUser, isLidUser, isPnUser, jidDecode, jidNormalizedUser, WAJIDDomains } from '../WABinary/index.js';
3
+ export class LIDMappingStore {
4
+ constructor(keys, logger, pnToLIDFunc) {
5
+ this.mappingCache = new LRUCache({
6
+ ttl: 3 * 24 * 60 * 60 * 1000, // 7 days
7
+ ttlAutopurge: true,
8
+ updateAgeOnGet: true
9
+ });
10
+ this.inflightLIDLookups = new Map();
11
+ this.inflightPNLookups = new Map();
12
+ this.keys = keys;
13
+ this.pnToLIDFunc = pnToLIDFunc;
14
+ this.logger = logger;
15
+ }
16
+ async storeLIDPNMappings(pairs) {
17
+ if (pairs.length === 0)
18
+ return;
19
+ const validatedPairs = [];
20
+ for (const { lid, pn } of pairs) {
21
+ if (!((isLidUser(lid) && isPnUser(pn)) || (isPnUser(lid) && isLidUser(pn)))) {
22
+ this.logger.warn(`Invalid LID-PN mapping: ${lid}, ${pn}`);
23
+ continue;
24
+ }
25
+ const lidDecoded = jidDecode(lid);
26
+ const pnDecoded = jidDecode(pn);
27
+ if (!lidDecoded || !pnDecoded)
28
+ continue;
29
+ validatedPairs.push({ pnUser: pnDecoded.user, lidUser: lidDecoded.user });
30
+ }
31
+ if (validatedPairs.length === 0)
32
+ return;
33
+ const cacheMissSet = new Set();
34
+ const existingMappings = new Map();
35
+ for (const { pnUser } of validatedPairs) {
36
+ const cached = this.mappingCache.get(`pn:${pnUser}`);
37
+ if (cached) {
38
+ existingMappings.set(pnUser, cached);
39
+ }
40
+ else {
41
+ cacheMissSet.add(pnUser);
42
+ }
43
+ }
44
+ if (cacheMissSet.size > 0) {
45
+ const cacheMisses = [...cacheMissSet];
46
+ this.logger.trace(`Batch fetching ${cacheMisses.length} LID mappings from database`);
47
+ const stored = await this.keys.get('lid-mapping', cacheMisses);
48
+ for (const pnUser of cacheMisses) {
49
+ const existingLidUser = stored[pnUser];
50
+ if (existingLidUser) {
51
+ existingMappings.set(pnUser, existingLidUser);
52
+ this.mappingCache.set(`pn:${pnUser}`, existingLidUser);
53
+ this.mappingCache.set(`lid:${existingLidUser}`, pnUser);
54
+ }
55
+ }
56
+ }
57
+ const pairMap = {};
58
+ for (const { pnUser, lidUser } of validatedPairs) {
59
+ const existingLidUser = existingMappings.get(pnUser);
60
+ if (existingLidUser === lidUser) {
61
+ this.logger.debug({ pnUser, lidUser }, 'LID mapping already exists, skipping');
62
+ continue;
63
+ }
64
+ pairMap[pnUser] = lidUser;
65
+ }
66
+ if (Object.keys(pairMap).length === 0)
67
+ return;
68
+ this.logger.trace({ pairMap }, `Storing ${Object.keys(pairMap).length} pn mappings`);
69
+ const batchData = {};
70
+ for (const [pnUser, lidUser] of Object.entries(pairMap)) {
71
+ batchData[pnUser] = lidUser;
72
+ batchData[`${lidUser}_reverse`] = pnUser;
73
+ }
74
+ await this.keys.transaction(async () => {
75
+ await this.keys.set({ 'lid-mapping': batchData });
76
+ }, 'lid-mapping');
77
+ // Update cache after successful DB write
78
+ for (const [pnUser, lidUser] of Object.entries(pairMap)) {
79
+ this.mappingCache.set(`pn:${pnUser}`, lidUser);
80
+ this.mappingCache.set(`lid:${lidUser}`, pnUser);
81
+ }
82
+ }
83
+ async getLIDForPN(pn) {
84
+ return (await this.getLIDsForPNs([pn]))?.[0]?.lid || null;
85
+ }
86
+ async getLIDsForPNs(pns) {
87
+ if (pns.length === 0)
88
+ return null;
89
+ const sortedPns = [...new Set(pns)].sort();
90
+ const cacheKey = sortedPns.join(',');
91
+ const inflight = this.inflightLIDLookups.get(cacheKey);
92
+ if (inflight) {
93
+ this.logger.trace(`Coalescing getLIDsForPNs request for ${sortedPns.length} PNs`);
94
+ return inflight;
95
+ }
96
+ const promise = this._getLIDsForPNsImpl(pns);
97
+ this.inflightLIDLookups.set(cacheKey, promise);
98
+ try {
99
+ return await promise;
100
+ }
101
+ finally {
102
+ this.inflightLIDLookups.delete(cacheKey);
103
+ }
104
+ }
105
+ async _getLIDsForPNsImpl(pns) {
106
+ const usyncFetch = {};
107
+ const successfulPairs = {};
108
+ const pending = [];
109
+ const addResolvedPair = (pn, decoded, lidUser) => {
110
+ const normalizedLidUser = lidUser.toString();
111
+ if (!normalizedLidUser) {
112
+ this.logger.warn(`Invalid or empty LID user for PN ${pn}: lidUser = "${lidUser}"`);
113
+ return false;
114
+ }
115
+ // Push the PN device ID to the LID to maintain device separation
116
+ const pnDevice = decoded.device !== undefined ? decoded.device : 0;
117
+ const deviceSpecificLid = `${normalizedLidUser}${!!pnDevice ? `:${pnDevice}` : ``}@${decoded.server === 'hosted' ? 'hosted.lid' : 'lid'}`;
118
+ this.logger.trace(`getLIDForPN: ${pn} → ${deviceSpecificLid} (user mapping with device ${pnDevice})`);
119
+ successfulPairs[pn] = { lid: deviceSpecificLid, pn };
120
+ return true;
121
+ };
122
+ for (const pn of pns) {
123
+ if (!isPnUser(pn) && !isHostedPnUser(pn))
124
+ continue;
125
+ const decoded = jidDecode(pn);
126
+ if (!decoded)
127
+ continue;
128
+ const pnUser = decoded.user;
129
+ const cached = this.mappingCache.get(`pn:${pnUser}`);
130
+ if (cached && typeof cached === 'string') {
131
+ if (!addResolvedPair(pn, decoded, cached)) {
132
+ this.logger.warn(`Invalid entry for ${pn} (pair not resolved)`);
133
+ continue;
134
+ }
135
+ continue;
136
+ }
137
+ pending.push({ pn, pnUser, decoded });
138
+ }
139
+ if (pending.length) {
140
+ const pnUsers = [...new Set(pending.map(item => item.pnUser))];
141
+ const stored = await this.keys.get('lid-mapping', pnUsers);
142
+ for (const pnUser of pnUsers) {
143
+ const lidUser = stored[pnUser];
144
+ if (lidUser && typeof lidUser === 'string') {
145
+ this.mappingCache.set(`pn:${pnUser}`, lidUser);
146
+ this.mappingCache.set(`lid:${lidUser}`, pnUser);
147
+ }
148
+ }
149
+ for (const { pn, pnUser, decoded } of pending) {
150
+ const cached = this.mappingCache.get(`pn:${pnUser}`);
151
+ if (cached && typeof cached === 'string') {
152
+ if (!addResolvedPair(pn, decoded, cached)) {
153
+ this.logger.warn(`Invalid entry for ${pn} (pair not resolved)`);
154
+ continue;
155
+ }
156
+ }
157
+ else {
158
+ this.logger.trace(`No LID mapping found for PN user ${pnUser}; batch getting from USync`);
159
+ const device = decoded.device || 0;
160
+ let normalizedPn = jidNormalizedUser(pn);
161
+ if (isHostedPnUser(normalizedPn)) {
162
+ normalizedPn = `${pnUser}@s.whatsapp.net`;
163
+ }
164
+ if (!usyncFetch[normalizedPn]) {
165
+ usyncFetch[normalizedPn] = [device];
166
+ }
167
+ else {
168
+ usyncFetch[normalizedPn]?.push(device);
169
+ }
170
+ }
171
+ }
172
+ }
173
+ if (Object.keys(usyncFetch).length > 0) {
174
+ const result = await this.pnToLIDFunc?.(Object.keys(usyncFetch)); // this function already adds LIDs to mapping
175
+ if (result && result.length > 0) {
176
+ await this.storeLIDPNMappings(result);
177
+ for (const pair of result) {
178
+ const pnDecoded = jidDecode(pair.pn);
179
+ const pnUser = pnDecoded?.user;
180
+ if (!pnUser)
181
+ continue;
182
+ const lidUser = jidDecode(pair.lid)?.user;
183
+ if (!lidUser)
184
+ continue;
185
+ for (const device of usyncFetch[pair.pn]) {
186
+ const deviceSpecificLid = `${lidUser}${!!device ? `:${device}` : ``}@${device === 99 ? 'hosted.lid' : 'lid'}`;
187
+ this.logger.trace(`getLIDForPN: USYNC success for ${pair.pn} → ${deviceSpecificLid} (user mapping with device ${device})`);
188
+ const deviceSpecificPn = `${pnUser}${!!device ? `:${device}` : ``}@${device === 99 ? 'hosted' : 's.whatsapp.net'}`;
189
+ successfulPairs[deviceSpecificPn] = { lid: deviceSpecificLid, pn: deviceSpecificPn };
190
+ }
191
+ }
192
+ }
193
+ else {
194
+ this.logger.warn('USync fetch yielded no results for pending PNs');
195
+ }
196
+ }
197
+ return Object.values(successfulPairs).length > 0 ? Object.values(successfulPairs) : null;
198
+ }
199
+ async getPNForLID(lid) {
200
+ return (await this.getPNsForLIDs([lid]))?.[0]?.pn || null;
201
+ }
202
+ async getPNsForLIDs(lids) {
203
+ if (lids.length === 0)
204
+ return null;
205
+ const sortedLids = [...new Set(lids)].sort();
206
+ const cacheKey = sortedLids.join(',');
207
+ const inflight = this.inflightPNLookups.get(cacheKey);
208
+ if (inflight) {
209
+ this.logger.trace(`Coalescing getPNsForLIDs request for ${sortedLids.length} LIDs`);
210
+ return inflight;
211
+ }
212
+ const promise = this._getPNsForLIDsImpl(lids);
213
+ this.inflightPNLookups.set(cacheKey, promise);
214
+ try {
215
+ return await promise;
216
+ }
217
+ finally {
218
+ this.inflightPNLookups.delete(cacheKey);
219
+ }
220
+ }
221
+ async _getPNsForLIDsImpl(lids) {
222
+ const successfulPairs = {};
223
+ const pending = [];
224
+ const addResolvedPair = (lid, decoded, pnUser) => {
225
+ if (!pnUser || typeof pnUser !== 'string') {
226
+ return false;
227
+ }
228
+ const lidDevice = decoded.device !== undefined ? decoded.device : 0;
229
+ const pnJid = `${pnUser}:${lidDevice}@${decoded.domainType === WAJIDDomains.HOSTED_LID ? 'hosted' : 's.whatsapp.net'}`;
230
+ this.logger.trace(`Found reverse mapping: ${lid} → ${pnJid}`);
231
+ successfulPairs[lid] = { lid, pn: pnJid };
232
+ return true;
233
+ };
234
+ for (const lid of lids) {
235
+ if (!isLidUser(lid))
236
+ continue;
237
+ const decoded = jidDecode(lid);
238
+ if (!decoded)
239
+ continue;
240
+ const lidUser = decoded.user;
241
+ const cached = this.mappingCache.get(`lid:${lidUser}`);
242
+ if (cached && typeof cached === 'string') {
243
+ addResolvedPair(lid, decoded, cached);
244
+ continue;
245
+ }
246
+ pending.push({ lid, lidUser, decoded });
247
+ }
248
+ if (pending.length) {
249
+ const reverseKeys = [...new Set(pending.map(item => `${item.lidUser}_reverse`))];
250
+ const stored = await this.keys.get('lid-mapping', reverseKeys);
251
+ for (const { lid, lidUser, decoded } of pending) {
252
+ let pnUser = this.mappingCache.get(`lid:${lidUser}`);
253
+ if (!pnUser || typeof pnUser !== 'string') {
254
+ pnUser = stored[`${lidUser}_reverse`];
255
+ if (pnUser && typeof pnUser === 'string') {
256
+ this.mappingCache.set(`lid:${lidUser}`, pnUser);
257
+ this.mappingCache.set(`pn:${pnUser}`, lidUser);
258
+ }
259
+ }
260
+ if (pnUser && typeof pnUser === 'string') {
261
+ addResolvedPair(lid, decoded, pnUser);
262
+ }
263
+ else {
264
+ this.logger.trace(`No reverse mapping found for LID user: ${lidUser}`);
265
+ }
266
+ }
267
+ }
268
+ return Object.values(successfulPairs).length ? Object.values(successfulPairs) : null;
269
+ }
270
+ }
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export * from './websocket.js';
@@ -0,0 +1,10 @@
1
+ import { EventEmitter } from 'events';
2
+ import { URL } from 'url';
3
+ export class AbstractSocketClient extends EventEmitter {
4
+ constructor(url, config) {
5
+ super();
6
+ this.url = url;
7
+ this.config = config;
8
+ this.setMaxListeners(0);
9
+ }
10
+ }