@nexustechpro/baileys 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1573 -0
- package/WAProto/fix-imports.js +29 -0
- package/WAProto/index.js +169659 -0
- package/lib/Defaults/index.js +115 -0
- package/lib/Signal/Group/ciphertext-message.js +12 -0
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/lib/Signal/Group/group_cipher.js +82 -0
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/keyhelper.js +18 -0
- package/lib/Signal/Group/sender-chain-key.js +26 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
- package/lib/Signal/Group/sender-key-message.js +66 -0
- package/lib/Signal/Group/sender-key-name.js +48 -0
- package/lib/Signal/Group/sender-key-record.js +41 -0
- package/lib/Signal/Group/sender-key-state.js +84 -0
- package/lib/Signal/Group/sender-message-key.js +26 -0
- package/lib/Signal/libsignal.js +342 -0
- package/lib/Signal/lid-mapping.js +171 -0
- package/lib/Socket/Client/index.js +3 -0
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/websocket.js +91 -0
- package/lib/Socket/business.js +376 -0
- package/lib/Socket/chats.js +963 -0
- package/lib/Socket/communities.js +431 -0
- package/lib/Socket/groups.js +328 -0
- package/lib/Socket/index.js +19 -0
- package/lib/Socket/messages-recv.js +1240 -0
- package/lib/Socket/messages-send.js +1370 -0
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/newsletter.js +202 -0
- package/lib/Socket/nexus-handler.js +667 -0
- package/lib/Socket/socket.js +871 -0
- package/lib/Store/index.js +4 -0
- package/lib/Store/make-cache-manager-store.js +81 -0
- package/lib/Store/make-in-memory-store.js +416 -0
- package/lib/Store/make-ordered-dictionary.js +82 -0
- package/lib/Store/object-repository.js +31 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +8 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.js +11 -0
- package/lib/Types/Newsletter.js +31 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +3 -0
- package/lib/Types/State.js +13 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +26 -0
- package/lib/Utils/auth-utils.js +257 -0
- package/lib/Utils/baileys-event-stream.js +56 -0
- package/lib/Utils/browser-utils.js +28 -0
- package/lib/Utils/business.js +231 -0
- package/lib/Utils/chat-utils.js +763 -0
- package/lib/Utils/crypto.js +142 -0
- package/lib/Utils/decode-wa-message.js +279 -0
- package/lib/Utils/event-buffer.js +548 -0
- package/lib/Utils/generics.js +381 -0
- package/lib/Utils/history.js +84 -0
- package/lib/Utils/index.js +20 -0
- package/lib/Utils/link-preview.js +85 -0
- package/lib/Utils/logger.js +3 -0
- package/lib/Utils/lt-hash.js +48 -0
- package/lib/Utils/make-mutex.js +40 -0
- package/lib/Utils/message-retry-manager.js +149 -0
- package/lib/Utils/messages-media.js +685 -0
- package/lib/Utils/messages.js +820 -0
- package/lib/Utils/noise-handler.js +147 -0
- package/lib/Utils/pre-key-manager.js +106 -0
- package/lib/Utils/process-message.js +413 -0
- package/lib/Utils/signal.js +159 -0
- package/lib/Utils/use-multi-file-auth-state.js +121 -0
- package/lib/Utils/validate-connection.js +195 -0
- package/lib/WABinary/constants.js +1301 -0
- package/lib/WABinary/decode.js +238 -0
- package/lib/WABinary/encode.js +216 -0
- package/lib/WABinary/generic-utils.js +111 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/jid-utils.js +96 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +10 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.js +150 -0
- package/lib/WAM/index.js +4 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +29 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
- package/lib/WAUSync/Protocols/index.js +5 -0
- package/lib/WAUSync/USyncQuery.js +94 -0
- package/lib/WAUSync/USyncUser.js +23 -0
- package/lib/WAUSync/index.js +4 -0
- package/lib/index.js +24 -0
- package/package.json +113 -0
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/* @ts-ignore */
|
|
2
|
+
import * as libsignal from 'libsignal';
|
|
3
|
+
import { LRUCache } from 'lru-cache';
|
|
4
|
+
import { generateSignalPubKey } from '../Utils/index.js';
|
|
5
|
+
import { isHostedLidUser, isHostedPnUser, isLidUser, isPnUser, jidDecode, transferDevice, WAJIDDomains } from '../WABinary/index.js';
|
|
6
|
+
import { SenderKeyName } from './Group/sender-key-name.js';
|
|
7
|
+
import { SenderKeyRecord } from './Group/sender-key-record.js';
|
|
8
|
+
import { GroupCipher, GroupSessionBuilder, SenderKeyDistributionMessage } from './Group/index.js';
|
|
9
|
+
import { LIDMappingStore } from './lid-mapping.js';
|
|
10
|
+
export function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
11
|
+
const lidMapping = new LIDMappingStore(auth.keys, logger, pnToLIDFunc);
|
|
12
|
+
const storage = signalStorage(auth, lidMapping);
|
|
13
|
+
const parsedKeys = auth.keys;
|
|
14
|
+
const migratedSessionCache = new LRUCache({
|
|
15
|
+
ttl: 7 * 24 * 60 * 60 * 1000, // 7 days
|
|
16
|
+
ttlAutopurge: true,
|
|
17
|
+
updateAgeOnGet: true
|
|
18
|
+
});
|
|
19
|
+
const repository = {
|
|
20
|
+
decryptGroupMessage({ group, authorJid, msg }) {
|
|
21
|
+
const senderName = jidToSignalSenderKeyName(group, authorJid);
|
|
22
|
+
const cipher = new GroupCipher(storage, senderName);
|
|
23
|
+
// Use transaction to ensure atomicity
|
|
24
|
+
return parsedKeys.transaction(async () => {
|
|
25
|
+
return cipher.decrypt(msg);
|
|
26
|
+
}, group);
|
|
27
|
+
},
|
|
28
|
+
async processSenderKeyDistributionMessage({ item, authorJid }) {
|
|
29
|
+
const builder = new GroupSessionBuilder(storage);
|
|
30
|
+
if (!item.groupId) {
|
|
31
|
+
throw new Error('Group ID is required for sender key distribution message');
|
|
32
|
+
}
|
|
33
|
+
const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
|
|
34
|
+
const senderMsg = new SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
|
|
35
|
+
const senderNameStr = senderName.toString();
|
|
36
|
+
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
37
|
+
if (!senderKey) {
|
|
38
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
39
|
+
}
|
|
40
|
+
return parsedKeys.transaction(async () => {
|
|
41
|
+
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
42
|
+
if (!senderKey) {
|
|
43
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
44
|
+
}
|
|
45
|
+
await builder.process(senderName, senderMsg);
|
|
46
|
+
}, item.groupId);
|
|
47
|
+
},
|
|
48
|
+
async decryptMessage({ jid, type, ciphertext }) {
|
|
49
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
50
|
+
const session = new libsignal.SessionCipher(storage, addr);
|
|
51
|
+
async function doDecrypt() {
|
|
52
|
+
let result;
|
|
53
|
+
switch (type) {
|
|
54
|
+
case 'pkmsg':
|
|
55
|
+
result = await session.decryptPreKeyWhisperMessage(ciphertext);
|
|
56
|
+
break;
|
|
57
|
+
case 'msg':
|
|
58
|
+
result = await session.decryptWhisperMessage(ciphertext);
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
// If it's not a sync message, we need to ensure atomicity
|
|
64
|
+
// For regular messages, we use a transaction to ensure atomicity
|
|
65
|
+
return parsedKeys.transaction(async () => {
|
|
66
|
+
return await doDecrypt();
|
|
67
|
+
}, jid);
|
|
68
|
+
},
|
|
69
|
+
async encryptMessage({ jid, data }) {
|
|
70
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
71
|
+
const cipher = new libsignal.SessionCipher(storage, addr);
|
|
72
|
+
// Use transaction to ensure atomicity
|
|
73
|
+
return parsedKeys.transaction(async () => {
|
|
74
|
+
const { type: sigType, body } = await cipher.encrypt(data);
|
|
75
|
+
const type = sigType === 3 ? 'pkmsg' : 'msg';
|
|
76
|
+
return { type, ciphertext: Buffer.from(body, 'binary') };
|
|
77
|
+
}, jid);
|
|
78
|
+
},
|
|
79
|
+
async encryptGroupMessage({ group, meId, data }) {
|
|
80
|
+
const senderName = jidToSignalSenderKeyName(group, meId);
|
|
81
|
+
const builder = new GroupSessionBuilder(storage);
|
|
82
|
+
const senderNameStr = senderName.toString();
|
|
83
|
+
return parsedKeys.transaction(async () => {
|
|
84
|
+
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
85
|
+
if (!senderKey) {
|
|
86
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
87
|
+
}
|
|
88
|
+
const senderKeyDistributionMessage = await builder.create(senderName);
|
|
89
|
+
const session = new GroupCipher(storage, senderName);
|
|
90
|
+
const ciphertext = await session.encrypt(data);
|
|
91
|
+
return {
|
|
92
|
+
ciphertext,
|
|
93
|
+
senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
|
|
94
|
+
};
|
|
95
|
+
}, group);
|
|
96
|
+
},
|
|
97
|
+
async injectE2ESession({ jid, session }) {
|
|
98
|
+
logger.trace({ jid }, 'injecting E2EE session');
|
|
99
|
+
const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
|
|
100
|
+
return parsedKeys.transaction(async () => {
|
|
101
|
+
await cipher.initOutgoing(session);
|
|
102
|
+
}, jid);
|
|
103
|
+
},
|
|
104
|
+
jidToSignalProtocolAddress(jid) {
|
|
105
|
+
return jidToSignalProtocolAddress(jid).toString();
|
|
106
|
+
},
|
|
107
|
+
// Optimized direct access to LID mapping store
|
|
108
|
+
lidMapping,
|
|
109
|
+
async validateSession(jid) {
|
|
110
|
+
try {
|
|
111
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
112
|
+
const session = await storage.loadSession(addr.toString());
|
|
113
|
+
if (!session) {
|
|
114
|
+
return { exists: false, reason: 'no session' };
|
|
115
|
+
}
|
|
116
|
+
if (!session.haveOpenSession()) {
|
|
117
|
+
return { exists: false, reason: 'no open session' };
|
|
118
|
+
}
|
|
119
|
+
return { exists: true };
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
return { exists: false, reason: 'validation error' };
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
async deleteSession(jids) {
|
|
126
|
+
if (!jids.length)
|
|
127
|
+
return;
|
|
128
|
+
// Convert JIDs to signal addresses and prepare for bulk deletion
|
|
129
|
+
const sessionUpdates = {};
|
|
130
|
+
jids.forEach(jid => {
|
|
131
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
132
|
+
sessionUpdates[addr.toString()] = null;
|
|
133
|
+
});
|
|
134
|
+
// Single transaction for all deletions
|
|
135
|
+
return parsedKeys.transaction(async () => {
|
|
136
|
+
await auth.keys.set({ session: sessionUpdates });
|
|
137
|
+
}, `delete-${jids.length}-sessions`);
|
|
138
|
+
},
|
|
139
|
+
async migrateSession(fromJid, toJid) {
|
|
140
|
+
// TODO: use usync to handle this entire mess
|
|
141
|
+
if (!fromJid || (!isLidUser(toJid) && !isHostedLidUser(toJid)))
|
|
142
|
+
return { migrated: 0, skipped: 0, total: 0 };
|
|
143
|
+
// Only support PN to LID migration
|
|
144
|
+
if (!isPnUser(fromJid) && !isHostedPnUser(fromJid)) {
|
|
145
|
+
return { migrated: 0, skipped: 0, total: 1 };
|
|
146
|
+
}
|
|
147
|
+
const { user } = jidDecode(fromJid);
|
|
148
|
+
logger.debug({ fromJid }, 'bulk device migration - loading all user devices');
|
|
149
|
+
// Get user's device list from storage
|
|
150
|
+
const { [user]: userDevices } = await parsedKeys.get('device-list', [user]);
|
|
151
|
+
if (!userDevices) {
|
|
152
|
+
return { migrated: 0, skipped: 0, total: 0 };
|
|
153
|
+
}
|
|
154
|
+
const { device: fromDevice } = jidDecode(fromJid);
|
|
155
|
+
const fromDeviceStr = fromDevice?.toString() || '0';
|
|
156
|
+
if (!userDevices.includes(fromDeviceStr)) {
|
|
157
|
+
userDevices.push(fromDeviceStr);
|
|
158
|
+
}
|
|
159
|
+
// Filter out cached devices before database fetch
|
|
160
|
+
const uncachedDevices = userDevices.filter(device => {
|
|
161
|
+
const deviceKey = `${user}.${device}`;
|
|
162
|
+
return !migratedSessionCache.has(deviceKey);
|
|
163
|
+
});
|
|
164
|
+
// Bulk check session existence only for uncached devices
|
|
165
|
+
const deviceSessionKeys = uncachedDevices.map(device => `${user}.${device}`);
|
|
166
|
+
const existingSessions = await parsedKeys.get('session', deviceSessionKeys);
|
|
167
|
+
// Step 3: Convert existing sessions to JIDs (only migrate sessions that exist)
|
|
168
|
+
const deviceJids = [];
|
|
169
|
+
for (const [sessionKey, sessionData] of Object.entries(existingSessions)) {
|
|
170
|
+
if (sessionData) {
|
|
171
|
+
// Session exists in storage
|
|
172
|
+
const deviceStr = sessionKey.split('.')[1];
|
|
173
|
+
if (!deviceStr)
|
|
174
|
+
continue;
|
|
175
|
+
const deviceNum = parseInt(deviceStr);
|
|
176
|
+
let jid = deviceNum === 0 ? `${user}@s.whatsapp.net` : `${user}:${deviceNum}@s.whatsapp.net`;
|
|
177
|
+
if (deviceNum === 99) {
|
|
178
|
+
jid = `${user}:99@hosted`;
|
|
179
|
+
}
|
|
180
|
+
deviceJids.push(jid);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
logger.debug({
|
|
184
|
+
fromJid,
|
|
185
|
+
totalDevices: userDevices.length,
|
|
186
|
+
devicesWithSessions: deviceJids.length,
|
|
187
|
+
devices: deviceJids
|
|
188
|
+
}, 'bulk device migration complete - all user devices processed');
|
|
189
|
+
// Single transaction for all migrations
|
|
190
|
+
return parsedKeys.transaction(async () => {
|
|
191
|
+
const migrationOps = deviceJids.map(jid => {
|
|
192
|
+
const lidWithDevice = transferDevice(jid, toJid);
|
|
193
|
+
const fromDecoded = jidDecode(jid);
|
|
194
|
+
const toDecoded = jidDecode(lidWithDevice);
|
|
195
|
+
return {
|
|
196
|
+
fromJid: jid,
|
|
197
|
+
toJid: lidWithDevice,
|
|
198
|
+
pnUser: fromDecoded.user,
|
|
199
|
+
lidUser: toDecoded.user,
|
|
200
|
+
deviceId: fromDecoded.device || 0,
|
|
201
|
+
fromAddr: jidToSignalProtocolAddress(jid),
|
|
202
|
+
toAddr: jidToSignalProtocolAddress(lidWithDevice)
|
|
203
|
+
};
|
|
204
|
+
});
|
|
205
|
+
const totalOps = migrationOps.length;
|
|
206
|
+
let migratedCount = 0;
|
|
207
|
+
// Bulk fetch PN sessions - already exist (verified during device discovery)
|
|
208
|
+
const pnAddrStrings = Array.from(new Set(migrationOps.map(op => op.fromAddr.toString())));
|
|
209
|
+
const pnSessions = await parsedKeys.get('session', pnAddrStrings);
|
|
210
|
+
// Prepare bulk session updates (PN → LID migration + deletion)
|
|
211
|
+
const sessionUpdates = {};
|
|
212
|
+
for (const op of migrationOps) {
|
|
213
|
+
const pnAddrStr = op.fromAddr.toString();
|
|
214
|
+
const lidAddrStr = op.toAddr.toString();
|
|
215
|
+
const pnSession = pnSessions[pnAddrStr];
|
|
216
|
+
if (pnSession) {
|
|
217
|
+
// Session exists (guaranteed from device discovery)
|
|
218
|
+
const fromSession = libsignal.SessionRecord.deserialize(pnSession);
|
|
219
|
+
if (fromSession.haveOpenSession()) {
|
|
220
|
+
// Queue for bulk update: copy to LID, delete from PN
|
|
221
|
+
sessionUpdates[lidAddrStr] = fromSession.serialize();
|
|
222
|
+
sessionUpdates[pnAddrStr] = null;
|
|
223
|
+
migratedCount++;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
// Single bulk session update for all migrations
|
|
228
|
+
if (Object.keys(sessionUpdates).length > 0) {
|
|
229
|
+
await parsedKeys.set({ session: sessionUpdates });
|
|
230
|
+
logger.debug({ migratedSessions: migratedCount }, 'bulk session migration complete');
|
|
231
|
+
// Cache device-level migrations
|
|
232
|
+
for (const op of migrationOps) {
|
|
233
|
+
if (sessionUpdates[op.toAddr.toString()]) {
|
|
234
|
+
const deviceKey = `${op.pnUser}.${op.deviceId}`;
|
|
235
|
+
migratedSessionCache.set(deviceKey, true);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
const skippedCount = totalOps - migratedCount;
|
|
240
|
+
return { migrated: migratedCount, skipped: skippedCount, total: totalOps };
|
|
241
|
+
}, `migrate-${deviceJids.length}-sessions-${jidDecode(toJid)?.user}`);
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
return repository;
|
|
245
|
+
}
|
|
246
|
+
const jidToSignalProtocolAddress = (jid) => {
|
|
247
|
+
const decoded = jidDecode(jid);
|
|
248
|
+
const { user, device, server, domainType } = decoded;
|
|
249
|
+
if (!user) {
|
|
250
|
+
throw new Error(`JID decoded but user is empty: "${jid}" -> user: "${user}", server: "${server}", device: ${device}`);
|
|
251
|
+
}
|
|
252
|
+
const signalUser = domainType !== WAJIDDomains.WHATSAPP ? `${user}_${domainType}` : user;
|
|
253
|
+
const finalDevice = device || 0;
|
|
254
|
+
if (device === 99 && decoded.server !== 'hosted' && decoded.server !== 'hosted.lid') {
|
|
255
|
+
throw new Error('Unexpected non-hosted device JID with device 99. This ID seems invalid. ID:' + jid);
|
|
256
|
+
}
|
|
257
|
+
return new libsignal.ProtocolAddress(signalUser, finalDevice);
|
|
258
|
+
};
|
|
259
|
+
const jidToSignalSenderKeyName = (group, user) => {
|
|
260
|
+
return new SenderKeyName(group, jidToSignalProtocolAddress(user));
|
|
261
|
+
};
|
|
262
|
+
function signalStorage({ creds, keys }, lidMapping) {
|
|
263
|
+
// Shared function to resolve PN signal address to LID if mapping exists
|
|
264
|
+
const resolveLIDSignalAddress = async (id) => {
|
|
265
|
+
if (id.includes('.')) {
|
|
266
|
+
const [deviceId, device] = id.split('.');
|
|
267
|
+
const [user, domainType_] = deviceId.split('_');
|
|
268
|
+
const domainType = parseInt(domainType_ || '0');
|
|
269
|
+
if (domainType === WAJIDDomains.LID || domainType === WAJIDDomains.HOSTED_LID)
|
|
270
|
+
return id;
|
|
271
|
+
const pnJid = `${user}${device !== '0' ? `:${device}` : ''}@${domainType === WAJIDDomains.HOSTED ? 'hosted' : 's.whatsapp.net'}`;
|
|
272
|
+
const lidForPN = await lidMapping.getLIDForPN(pnJid);
|
|
273
|
+
if (lidForPN) {
|
|
274
|
+
const lidAddr = jidToSignalProtocolAddress(lidForPN);
|
|
275
|
+
return lidAddr.toString();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return id;
|
|
279
|
+
};
|
|
280
|
+
return {
|
|
281
|
+
loadSession: async (id) => {
|
|
282
|
+
try {
|
|
283
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
284
|
+
const { [wireJid]: sess } = await keys.get('session', [wireJid]);
|
|
285
|
+
if (sess) {
|
|
286
|
+
return libsignal.SessionRecord.deserialize(sess);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
catch (e) {
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
return null;
|
|
293
|
+
},
|
|
294
|
+
storeSession: async (id, session) => {
|
|
295
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
296
|
+
await keys.set({ session: { [wireJid]: session.serialize() } });
|
|
297
|
+
},
|
|
298
|
+
isTrustedIdentity: () => {
|
|
299
|
+
return true; // todo: implement
|
|
300
|
+
},
|
|
301
|
+
loadPreKey: async (id) => {
|
|
302
|
+
const keyId = id.toString();
|
|
303
|
+
const { [keyId]: key } = await keys.get('pre-key', [keyId]);
|
|
304
|
+
if (key) {
|
|
305
|
+
return {
|
|
306
|
+
privKey: Buffer.from(key.private),
|
|
307
|
+
pubKey: Buffer.from(key.public)
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
},
|
|
311
|
+
removePreKey: (id) => keys.set({ 'pre-key': { [id]: null } }),
|
|
312
|
+
loadSignedPreKey: () => {
|
|
313
|
+
const key = creds.signedPreKey;
|
|
314
|
+
return {
|
|
315
|
+
privKey: Buffer.from(key.keyPair.private),
|
|
316
|
+
pubKey: Buffer.from(key.keyPair.public)
|
|
317
|
+
};
|
|
318
|
+
},
|
|
319
|
+
loadSenderKey: async (senderKeyName) => {
|
|
320
|
+
const keyId = senderKeyName.toString();
|
|
321
|
+
const { [keyId]: key } = await keys.get('sender-key', [keyId]);
|
|
322
|
+
if (key) {
|
|
323
|
+
return SenderKeyRecord.deserialize(key);
|
|
324
|
+
}
|
|
325
|
+
return new SenderKeyRecord();
|
|
326
|
+
},
|
|
327
|
+
storeSenderKey: async (senderKeyName, key) => {
|
|
328
|
+
const keyId = senderKeyName.toString();
|
|
329
|
+
const serialized = JSON.stringify(key.serialize());
|
|
330
|
+
await keys.set({ 'sender-key': { [keyId]: Buffer.from(serialized, 'utf-8') } });
|
|
331
|
+
},
|
|
332
|
+
getOurRegistrationId: () => creds.registrationId,
|
|
333
|
+
getOurIdentity: () => {
|
|
334
|
+
const { signedIdentityKey } = creds;
|
|
335
|
+
return {
|
|
336
|
+
privKey: Buffer.from(signedIdentityKey.private),
|
|
337
|
+
pubKey: Buffer.from(generateSignalPubKey(signedIdentityKey.public))
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
//# sourceMappingURL=libsignal.js.map
|
|
@@ -0,0 +1,171 @@
|
|
|
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: 7 * 24 * 60 * 60 * 1000, // 7 days
|
|
7
|
+
ttlAutopurge: true,
|
|
8
|
+
updateAgeOnGet: true
|
|
9
|
+
});
|
|
10
|
+
this.keys = keys;
|
|
11
|
+
this.pnToLIDFunc = pnToLIDFunc;
|
|
12
|
+
this.logger = logger;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Store LID-PN mapping - USER LEVEL
|
|
16
|
+
*/
|
|
17
|
+
async storeLIDPNMappings(pairs) {
|
|
18
|
+
// Validate inputs
|
|
19
|
+
const pairMap = {};
|
|
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
|
+
return;
|
|
29
|
+
const pnUser = pnDecoded.user;
|
|
30
|
+
const lidUser = lidDecoded.user;
|
|
31
|
+
let existingLidUser = this.mappingCache.get(`pn:${pnUser}`);
|
|
32
|
+
if (!existingLidUser) {
|
|
33
|
+
this.logger.trace(`Cache miss for PN user ${pnUser}; checking database`);
|
|
34
|
+
const stored = await this.keys.get('lid-mapping', [pnUser]);
|
|
35
|
+
existingLidUser = stored[pnUser];
|
|
36
|
+
if (existingLidUser) {
|
|
37
|
+
// Update cache with database value
|
|
38
|
+
this.mappingCache.set(`pn:${pnUser}`, existingLidUser);
|
|
39
|
+
this.mappingCache.set(`lid:${existingLidUser}`, pnUser);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (existingLidUser === lidUser) {
|
|
43
|
+
this.logger.debug({ pnUser, lidUser }, 'LID mapping already exists, skipping');
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
pairMap[pnUser] = lidUser;
|
|
47
|
+
}
|
|
48
|
+
this.logger.trace({ pairMap }, `Storing ${Object.keys(pairMap).length} pn mappings`);
|
|
49
|
+
await this.keys.transaction(async () => {
|
|
50
|
+
for (const [pnUser, lidUser] of Object.entries(pairMap)) {
|
|
51
|
+
await this.keys.set({
|
|
52
|
+
'lid-mapping': {
|
|
53
|
+
[pnUser]: lidUser,
|
|
54
|
+
[`${lidUser}_reverse`]: pnUser
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
this.mappingCache.set(`pn:${pnUser}`, lidUser);
|
|
58
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
59
|
+
}
|
|
60
|
+
}, 'lid-mapping');
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get LID for PN - Returns device-specific LID based on user mapping
|
|
64
|
+
*/
|
|
65
|
+
async getLIDForPN(pn) {
|
|
66
|
+
return (await this.getLIDsForPNs([pn]))?.[0]?.lid || null;
|
|
67
|
+
}
|
|
68
|
+
async getLIDsForPNs(pns) {
|
|
69
|
+
const usyncFetch = {};
|
|
70
|
+
// mapped from pn to lid mapping to prevent duplication in results later
|
|
71
|
+
const successfulPairs = {};
|
|
72
|
+
for (const pn of pns) {
|
|
73
|
+
if (!isPnUser(pn) && !isHostedPnUser(pn))
|
|
74
|
+
continue;
|
|
75
|
+
const decoded = jidDecode(pn);
|
|
76
|
+
if (!decoded)
|
|
77
|
+
continue;
|
|
78
|
+
// Check cache first for PN → LID mapping
|
|
79
|
+
const pnUser = decoded.user;
|
|
80
|
+
let lidUser = this.mappingCache.get(`pn:${pnUser}`);
|
|
81
|
+
if (!lidUser) {
|
|
82
|
+
// Cache miss - check database
|
|
83
|
+
const stored = await this.keys.get('lid-mapping', [pnUser]);
|
|
84
|
+
lidUser = stored[pnUser];
|
|
85
|
+
if (lidUser) {
|
|
86
|
+
this.mappingCache.set(`pn:${pnUser}`, lidUser);
|
|
87
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
this.logger.trace(`No LID mapping found for PN user ${pnUser}; batch getting from USync`);
|
|
91
|
+
const device = decoded.device || 0;
|
|
92
|
+
let normalizedPn = jidNormalizedUser(pn);
|
|
93
|
+
if (isHostedPnUser(normalizedPn)) {
|
|
94
|
+
normalizedPn = `${pnUser}@s.whatsapp.net`;
|
|
95
|
+
}
|
|
96
|
+
if (!usyncFetch[normalizedPn]) {
|
|
97
|
+
usyncFetch[normalizedPn] = [device];
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
usyncFetch[normalizedPn]?.push(device);
|
|
101
|
+
}
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
lidUser = lidUser.toString();
|
|
106
|
+
if (!lidUser) {
|
|
107
|
+
this.logger.warn(`Invalid or empty LID user for PN ${pn}: lidUser = "${lidUser}"`);
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
// Push the PN device ID to the LID to maintain device separation
|
|
111
|
+
const pnDevice = decoded.device !== undefined ? decoded.device : 0;
|
|
112
|
+
const deviceSpecificLid = `${lidUser}${!!pnDevice ? `:${pnDevice}` : ``}@${decoded.server === 'hosted' ? 'hosted.lid' : 'lid'}`;
|
|
113
|
+
this.logger.trace(`getLIDForPN: ${pn} → ${deviceSpecificLid} (user mapping with device ${pnDevice})`);
|
|
114
|
+
successfulPairs[pn] = { lid: deviceSpecificLid, pn };
|
|
115
|
+
}
|
|
116
|
+
if (Object.keys(usyncFetch).length > 0) {
|
|
117
|
+
const result = await this.pnToLIDFunc?.(Object.keys(usyncFetch)); // this function already adds LIDs to mapping
|
|
118
|
+
if (result && result.length > 0) {
|
|
119
|
+
this.storeLIDPNMappings(result);
|
|
120
|
+
for (const pair of result) {
|
|
121
|
+
const pnDecoded = jidDecode(pair.pn);
|
|
122
|
+
const pnUser = pnDecoded?.user;
|
|
123
|
+
if (!pnUser)
|
|
124
|
+
continue;
|
|
125
|
+
const lidUser = jidDecode(pair.lid)?.user;
|
|
126
|
+
if (!lidUser)
|
|
127
|
+
continue;
|
|
128
|
+
for (const device of usyncFetch[pair.pn]) {
|
|
129
|
+
const deviceSpecificLid = `${lidUser}${!!device ? `:${device}` : ``}@${device === 99 ? 'hosted.lid' : 'lid'}`;
|
|
130
|
+
this.logger.trace(`getLIDForPN: USYNC success for ${pair.pn} → ${deviceSpecificLid} (user mapping with device ${device})`);
|
|
131
|
+
const deviceSpecificPn = `${pnUser}${!!device ? `:${device}` : ``}@${device === 99 ? 'hosted' : 's.whatsapp.net'}`;
|
|
132
|
+
successfulPairs[deviceSpecificPn] = { lid: deviceSpecificLid, pn: deviceSpecificPn };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return Object.values(successfulPairs);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Get PN for LID - USER LEVEL with device construction
|
|
144
|
+
*/
|
|
145
|
+
async getPNForLID(lid) {
|
|
146
|
+
if (!isLidUser(lid))
|
|
147
|
+
return null;
|
|
148
|
+
const decoded = jidDecode(lid);
|
|
149
|
+
if (!decoded)
|
|
150
|
+
return null;
|
|
151
|
+
// Check cache first for LID → PN mapping
|
|
152
|
+
const lidUser = decoded.user;
|
|
153
|
+
let pnUser = this.mappingCache.get(`lid:${lidUser}`);
|
|
154
|
+
if (!pnUser || typeof pnUser !== 'string') {
|
|
155
|
+
// Cache miss - check database
|
|
156
|
+
const stored = await this.keys.get('lid-mapping', [`${lidUser}_reverse`]);
|
|
157
|
+
pnUser = stored[`${lidUser}_reverse`];
|
|
158
|
+
if (!pnUser || typeof pnUser !== 'string') {
|
|
159
|
+
this.logger.trace(`No reverse mapping found for LID user: ${lidUser}`);
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
163
|
+
}
|
|
164
|
+
// Construct device-specific PN JID
|
|
165
|
+
const lidDevice = decoded.device !== undefined ? decoded.device : 0;
|
|
166
|
+
const pnJid = `${pnUser}:${lidDevice}@${decoded.domainType === WAJIDDomains.HOSTED_LID ? 'hosted' : 's.whatsapp.net'}`;
|
|
167
|
+
this.logger.trace(`Found reverse mapping: ${lid} → ${pnJid}`);
|
|
168
|
+
return pnJid;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=lid-mapping.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
|
11
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import WebSocket from 'ws';
|
|
2
|
+
import { DEFAULT_ORIGIN } from '../../Defaults/index.js';
|
|
3
|
+
import { AbstractSocketClient } from './types.js';
|
|
4
|
+
export class WebSocketClient extends AbstractSocketClient {
|
|
5
|
+
constructor() {
|
|
6
|
+
super(...arguments);
|
|
7
|
+
this.socket = null;
|
|
8
|
+
// queue & dispatch variables for throttling outgoing messages to avoid rate limits
|
|
9
|
+
this._queue = [];
|
|
10
|
+
this._isDispatching = false;
|
|
11
|
+
this._lastDispatch = 0;
|
|
12
|
+
this._minSendIntervalMs = 50; // 50ms minimum interval between sends
|
|
13
|
+
}
|
|
14
|
+
get isOpen() {
|
|
15
|
+
return this.socket?.readyState === WebSocket.OPEN;
|
|
16
|
+
}
|
|
17
|
+
get isClosed() {
|
|
18
|
+
return this.socket === null || this.socket?.readyState === WebSocket.CLOSED;
|
|
19
|
+
}
|
|
20
|
+
get isClosing() {
|
|
21
|
+
return this.socket === null || this.socket?.readyState === WebSocket.CLOSING;
|
|
22
|
+
}
|
|
23
|
+
get isConnecting() {
|
|
24
|
+
return this.socket?.readyState === WebSocket.CONNECTING;
|
|
25
|
+
}
|
|
26
|
+
async connect() {
|
|
27
|
+
if (this.socket) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
this.socket = new WebSocket(this.url, {
|
|
31
|
+
origin: DEFAULT_ORIGIN,
|
|
32
|
+
headers: this.config.options?.headers,
|
|
33
|
+
handshakeTimeout: this.config.connectTimeoutMs,
|
|
34
|
+
timeout: this.config.connectTimeoutMs,
|
|
35
|
+
agent: this.config.agent
|
|
36
|
+
});
|
|
37
|
+
this.socket.setMaxListeners(0);
|
|
38
|
+
const events = ['close', 'error', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response'];
|
|
39
|
+
for (const event of events) {
|
|
40
|
+
this.socket?.on(event, (...args) => this.emit(event, ...args));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async close() {
|
|
44
|
+
if (!this.socket) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
this.socket.close();
|
|
48
|
+
this.socket = null;
|
|
49
|
+
}
|
|
50
|
+
async restart() {
|
|
51
|
+
if (this.socket) {
|
|
52
|
+
await new Promise(resolve => {
|
|
53
|
+
this.socket.once('close', resolve);
|
|
54
|
+
this.socket.terminate();
|
|
55
|
+
});
|
|
56
|
+
this.socket = null;
|
|
57
|
+
}
|
|
58
|
+
await this.connect();
|
|
59
|
+
}
|
|
60
|
+
send(str, cb) {
|
|
61
|
+
// throttle sends to reduce rate-limit likelihood
|
|
62
|
+
const doSend = () => {
|
|
63
|
+
this.socket?.send(str, cb);
|
|
64
|
+
return Boolean(this.socket);
|
|
65
|
+
};
|
|
66
|
+
this._queue.push(doSend);
|
|
67
|
+
this._dispatch();
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
_dispatch() {
|
|
71
|
+
if (this._isDispatching) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const now = Date.now();
|
|
75
|
+
if (this._queue.length > 0 && (now - this._lastDispatch) >= this._minSendIntervalMs) {
|
|
76
|
+
this._isDispatching = true;
|
|
77
|
+
const fn = this._queue.shift();
|
|
78
|
+
fn();
|
|
79
|
+
this._lastDispatch = Date.now();
|
|
80
|
+
this._isDispatching = false;
|
|
81
|
+
// continue dispatching if queue not empty
|
|
82
|
+
if (this._queue.length > 0) {
|
|
83
|
+
setTimeout(() => this._dispatch(), this._minSendIntervalMs - (Date.now() - this._lastDispatch));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else if (this._queue.length > 0 && !this._isDispatching) {
|
|
87
|
+
setTimeout(() => this._dispatch(), this._minSendIntervalMs - (now - this._lastDispatch));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=websocket.js.map
|