violetics 7.0.1-alpha → 7.0.2-alpha

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 (114) hide show
  1. package/LICENSE +3 -2
  2. package/README.md +1001 -232
  3. package/WAProto/index.js +75379 -142631
  4. package/engine-requirements.js +11 -8
  5. package/lib/Defaults/index.js +132 -146
  6. package/lib/Signal/Group/ciphertext-message.js +2 -6
  7. package/lib/Signal/Group/group-session-builder.js +7 -42
  8. package/lib/Signal/Group/group_cipher.js +37 -52
  9. package/lib/Signal/Group/index.js +11 -57
  10. package/lib/Signal/Group/keyhelper.js +7 -45
  11. package/lib/Signal/Group/sender-chain-key.js +7 -16
  12. package/lib/Signal/Group/sender-key-distribution-message.js +8 -12
  13. package/lib/Signal/Group/sender-key-message.js +9 -13
  14. package/lib/Signal/Group/sender-key-name.js +2 -6
  15. package/lib/Signal/Group/sender-key-record.js +9 -22
  16. package/lib/Signal/Group/sender-key-state.js +27 -43
  17. package/lib/Signal/Group/sender-message-key.js +4 -8
  18. package/lib/Signal/libsignal.js +319 -94
  19. package/lib/Signal/lid-mapping.js +224 -139
  20. package/lib/Socket/Client/index.js +2 -19
  21. package/lib/Socket/Client/types.js +10 -0
  22. package/lib/Socket/Client/websocket.js +53 -0
  23. package/lib/Socket/business.js +162 -44
  24. package/lib/Socket/chats.js +477 -418
  25. package/lib/Socket/communities.js +430 -0
  26. package/lib/Socket/groups.js +110 -99
  27. package/lib/Socket/index.js +10 -10
  28. package/lib/Socket/messages-recv.js +884 -561
  29. package/lib/Socket/messages-send.js +859 -428
  30. package/lib/Socket/mex.js +41 -0
  31. package/lib/Socket/newsletter.js +195 -390
  32. package/lib/Socket/socket.js +465 -315
  33. package/lib/Store/index.js +3 -10
  34. package/lib/Store/make-in-memory-store.js +73 -79
  35. package/lib/Store/make-ordered-dictionary.js +4 -7
  36. package/lib/Store/object-repository.js +2 -6
  37. package/lib/Types/Auth.js +1 -2
  38. package/lib/Types/Bussines.js +1 -0
  39. package/lib/Types/Call.js +1 -2
  40. package/lib/Types/Chat.js +7 -4
  41. package/lib/Types/Contact.js +1 -2
  42. package/lib/Types/Events.js +1 -2
  43. package/lib/Types/GroupMetadata.js +1 -2
  44. package/lib/Types/Label.js +2 -5
  45. package/lib/Types/LabelAssociation.js +2 -5
  46. package/lib/Types/Message.js +17 -9
  47. package/lib/Types/Newsletter.js +33 -38
  48. package/lib/Types/Product.js +1 -2
  49. package/lib/Types/Signal.js +1 -2
  50. package/lib/Types/Socket.js +2 -2
  51. package/lib/Types/State.js +12 -2
  52. package/lib/Types/USync.js +1 -2
  53. package/lib/Types/index.js +14 -31
  54. package/lib/Utils/auth-utils.js +228 -152
  55. package/lib/Utils/browser-utils.js +28 -0
  56. package/lib/Utils/business.js +66 -70
  57. package/lib/Utils/chat-utils.js +331 -249
  58. package/lib/Utils/crypto.js +57 -91
  59. package/lib/Utils/decode-wa-message.js +168 -84
  60. package/lib/Utils/event-buffer.js +138 -80
  61. package/lib/Utils/generics.js +180 -297
  62. package/lib/Utils/history.js +83 -49
  63. package/lib/Utils/identity-change-handler.js +48 -0
  64. package/lib/Utils/index.js +19 -33
  65. package/lib/Utils/link-preview.js +14 -23
  66. package/lib/Utils/logger.js +2 -7
  67. package/lib/Utils/lt-hash.js +2 -46
  68. package/lib/Utils/make-mutex.js +24 -47
  69. package/lib/Utils/message-retry-manager.js +224 -0
  70. package/lib/Utils/messages-media.js +501 -496
  71. package/lib/Utils/messages.js +1428 -362
  72. package/lib/Utils/noise-handler.js +145 -100
  73. package/lib/Utils/pre-key-manager.js +105 -0
  74. package/lib/Utils/process-message.js +356 -150
  75. package/lib/Utils/reporting-utils.js +257 -0
  76. package/lib/Utils/signal.js +78 -73
  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 +35 -45
  80. package/lib/Utils/validate-connection.js +91 -107
  81. package/lib/WABinary/constants.js +1300 -1304
  82. package/lib/WABinary/decode.js +26 -48
  83. package/lib/WABinary/encode.js +109 -155
  84. package/lib/WABinary/generic-utils.js +161 -149
  85. package/lib/WABinary/index.js +5 -21
  86. package/lib/WABinary/jid-utils.js +73 -40
  87. package/lib/WABinary/types.js +1 -2
  88. package/lib/WAM/BinaryInfo.js +2 -6
  89. package/lib/WAM/constants.js +19070 -11568
  90. package/lib/WAM/encode.js +17 -23
  91. package/lib/WAM/index.js +3 -19
  92. package/lib/WAUSync/Protocols/USyncContactProtocol.js +8 -12
  93. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -15
  94. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -13
  95. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -14
  96. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -23
  97. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +13 -9
  98. package/lib/WAUSync/Protocols/index.js +4 -20
  99. package/lib/WAUSync/USyncQuery.js +40 -36
  100. package/lib/WAUSync/USyncUser.js +2 -6
  101. package/lib/WAUSync/index.js +3 -19
  102. package/lib/index.js +11 -44
  103. package/package.json +74 -107
  104. package/lib/Defaults/baileys-version.json +0 -3
  105. package/lib/Defaults/phonenumber-mcc.json +0 -223
  106. package/lib/Signal/Group/queue-job.js +0 -57
  107. package/lib/Socket/Client/abstract-socket-client.js +0 -13
  108. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  109. package/lib/Socket/Client/web-socket-client.js +0 -118
  110. package/lib/Socket/groupStatus.js +0 -637
  111. package/lib/Socket/registration.js +0 -166
  112. package/lib/Socket/usync.js +0 -70
  113. package/lib/Store/make-cache-manager-store.js +0 -83
  114. package/lib/Utils/baileys-event-stream.js +0 -63
@@ -1,138 +1,363 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
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;
7
29
  }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.makeLibSignalRepository = makeLibSignalRepository;
37
- const libsignal = __importStar(require("libsignal"));
38
- const Utils_1 = require("../Utils");
39
- const WABinary_1 = require("../WABinary");
40
- const sender_key_name_1 = require("./Group/sender-key-name");
41
- const sender_key_record_1 = require("./Group/sender-key-record");
42
- const Group_1 = require("./Group");
43
- const lid_mapping_1 = require("./lid-mapping");
44
- function makeLibSignalRepository(auth) {
45
- const storage = signalStorage(auth);
46
- const lidMapping = new lid_mapping_1.LIDMappingStore(auth.keys, undefined, undefined);
47
- return {
48
- lidMapping,
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 = {
49
44
  decryptGroupMessage({ group, authorJid, msg }) {
50
45
  const senderName = jidToSignalSenderKeyName(group, authorJid);
51
- const cipher = new Group_1.GroupCipher(storage, senderName);
52
- return cipher.decrypt(msg);
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);
53
51
  },
54
52
  async processSenderKeyDistributionMessage({ item, authorJid }) {
55
- const builder = new Group_1.GroupSessionBuilder(storage);
53
+ const builder = new GroupSessionBuilder(storage);
56
54
  if (!item.groupId) {
57
55
  throw new Error('Group ID is required for sender key distribution message');
58
56
  }
59
57
  const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
60
- const senderMsg = new Group_1.SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
58
+ const senderMsg = new SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
61
59
  const senderNameStr = senderName.toString();
62
60
  const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
63
61
  if (!senderKey) {
64
- await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
62
+ await storage.storeSenderKey(senderName, new SenderKeyRecord());
65
63
  }
66
- await builder.process(senderName, senderMsg);
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);
67
71
  },
68
72
  async decryptMessage({ jid, type, ciphertext }) {
69
73
  const addr = jidToSignalProtocolAddress(jid);
70
74
  const session = new libsignal.SessionCipher(storage, addr);
71
- let result;
72
- switch (type) {
73
- case 'pkmsg':
74
- result = await session.decryptPreKeyWhisperMessage(ciphertext);
75
- break;
76
- case 'msg':
77
- result = await session.decryptWhisperMessage(ciphertext);
78
- break;
79
- default:
80
- throw new Error(`Unknown message type: ${type}`);
81
- }
82
- return result;
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);
83
103
  },
84
104
  async encryptMessage({ jid, data }) {
85
105
  const addr = jidToSignalProtocolAddress(jid);
86
106
  const cipher = new libsignal.SessionCipher(storage, addr);
87
- const { type: sigType, body } = await cipher.encrypt(data);
88
- const type = sigType === 3 ? 'pkmsg' : 'msg';
89
- return { type, ciphertext: Buffer.from(body, 'binary') };
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);
90
113
  },
91
114
  async encryptGroupMessage({ group, meId, data }) {
92
115
  const senderName = jidToSignalSenderKeyName(group, meId);
93
- const builder = new Group_1.GroupSessionBuilder(storage);
116
+ const builder = new GroupSessionBuilder(storage);
94
117
  const senderNameStr = senderName.toString();
95
- const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
96
- if (!senderKey) {
97
- await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
98
- }
99
- const senderKeyDistributionMessage = await builder.create(senderName);
100
- const session = new Group_1.GroupCipher(storage, senderName);
101
- const ciphertext = await session.encrypt(data);
102
- return {
103
- ciphertext,
104
- senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
105
- };
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);
106
131
  },
107
132
  async injectE2ESession({ jid, session }) {
133
+ logger.trace({ jid }, 'injecting E2EE session');
108
134
  const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
109
- await cipher.initOutgoing(session);
135
+ return parsedKeys.transaction(async () => {
136
+ await cipher.initOutgoing(session);
137
+ }, jid);
110
138
  },
111
139
  jidToSignalProtocolAddress(jid) {
112
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}`);
113
277
  }
114
278
  };
279
+ return repository;
115
280
  }
116
281
  const jidToSignalProtocolAddress = (jid) => {
117
- const { user, device } = (0, WABinary_1.jidDecode)(jid);
118
- return new libsignal.ProtocolAddress(user, device || 0);
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);
119
293
  };
120
294
  const jidToSignalSenderKeyName = (group, user) => {
121
- return new sender_key_name_1.SenderKeyName(group, jidToSignalProtocolAddress(user));
295
+ return new SenderKeyName(group, jidToSignalProtocolAddress(user));
122
296
  };
123
- function signalStorage({ creds, keys }) {
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
+ };
124
315
  return {
125
316
  loadSession: async (id) => {
126
- const { [id]: sess } = await keys.get('session', [id]);
127
- if (sess) {
128
- return libsignal.SessionRecord.deserialize(sess);
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
+ }
129
323
  }
324
+ catch (e) {
325
+ return null;
326
+ }
327
+ return null;
130
328
  },
131
329
  storeSession: async (id, session) => {
132
- await keys.set({ session: { [id]: session.serialize() } });
330
+ const wireJid = await resolveLIDSignalAddress(id);
331
+ await keys.set({ session: { [wireJid]: session.serialize() } });
133
332
  },
134
333
  isTrustedIdentity: () => {
135
- return true;
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;
136
361
  },
137
362
  loadPreKey: async (id) => {
138
363
  const keyId = id.toString();
@@ -156,9 +381,9 @@ function signalStorage({ creds, keys }) {
156
381
  const keyId = senderKeyName.toString();
157
382
  const { [keyId]: key } = await keys.get('sender-key', [keyId]);
158
383
  if (key) {
159
- return sender_key_record_1.SenderKeyRecord.deserialize(key);
384
+ return SenderKeyRecord.deserialize(key);
160
385
  }
161
- return new sender_key_record_1.SenderKeyRecord();
386
+ return new SenderKeyRecord();
162
387
  },
163
388
  storeSenderKey: async (senderKeyName, key) => {
164
389
  const keyId = senderKeyName.toString();
@@ -170,8 +395,8 @@ function signalStorage({ creds, keys }) {
170
395
  const { signedIdentityKey } = creds;
171
396
  return {
172
397
  privKey: Buffer.from(signedIdentityKey.private),
173
- pubKey: (0, Utils_1.generateSignalPubKey)(signedIdentityKey.public)
398
+ pubKey: Buffer.from(generateSignalPubKey(signedIdentityKey.public))
174
399
  };
175
400
  }
176
401
  };
177
- }
402
+ }