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.
- package/LICENSE +3 -2
- package/README.md +1001 -232
- package/WAProto/index.js +75379 -142631
- package/engine-requirements.js +11 -8
- package/lib/Defaults/index.js +132 -146
- package/lib/Signal/Group/ciphertext-message.js +2 -6
- package/lib/Signal/Group/group-session-builder.js +7 -42
- package/lib/Signal/Group/group_cipher.js +37 -52
- package/lib/Signal/Group/index.js +11 -57
- package/lib/Signal/Group/keyhelper.js +7 -45
- package/lib/Signal/Group/sender-chain-key.js +7 -16
- package/lib/Signal/Group/sender-key-distribution-message.js +8 -12
- package/lib/Signal/Group/sender-key-message.js +9 -13
- package/lib/Signal/Group/sender-key-name.js +2 -6
- package/lib/Signal/Group/sender-key-record.js +9 -22
- package/lib/Signal/Group/sender-key-state.js +27 -43
- package/lib/Signal/Group/sender-message-key.js +4 -8
- package/lib/Signal/libsignal.js +319 -94
- package/lib/Signal/lid-mapping.js +224 -139
- package/lib/Socket/Client/index.js +2 -19
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +53 -0
- package/lib/Socket/business.js +162 -44
- package/lib/Socket/chats.js +477 -418
- package/lib/Socket/communities.js +430 -0
- package/lib/Socket/groups.js +110 -99
- package/lib/Socket/index.js +10 -10
- package/lib/Socket/messages-recv.js +884 -561
- package/lib/Socket/messages-send.js +859 -428
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter.js +195 -390
- package/lib/Socket/socket.js +465 -315
- package/lib/Store/index.js +3 -10
- package/lib/Store/make-in-memory-store.js +73 -79
- package/lib/Store/make-ordered-dictionary.js +4 -7
- package/lib/Store/object-repository.js +2 -6
- package/lib/Types/Auth.js +1 -2
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.js +1 -2
- package/lib/Types/Chat.js +7 -4
- package/lib/Types/Contact.js +1 -2
- package/lib/Types/Events.js +1 -2
- package/lib/Types/GroupMetadata.js +1 -2
- package/lib/Types/Label.js +2 -5
- package/lib/Types/LabelAssociation.js +2 -5
- package/lib/Types/Message.js +17 -9
- package/lib/Types/Newsletter.js +33 -38
- package/lib/Types/Product.js +1 -2
- package/lib/Types/Signal.js +1 -2
- package/lib/Types/Socket.js +2 -2
- package/lib/Types/State.js +12 -2
- package/lib/Types/USync.js +1 -2
- package/lib/Types/index.js +14 -31
- package/lib/Utils/auth-utils.js +228 -152
- package/lib/Utils/browser-utils.js +28 -0
- package/lib/Utils/business.js +66 -70
- package/lib/Utils/chat-utils.js +331 -249
- package/lib/Utils/crypto.js +57 -91
- package/lib/Utils/decode-wa-message.js +168 -84
- package/lib/Utils/event-buffer.js +138 -80
- package/lib/Utils/generics.js +180 -297
- package/lib/Utils/history.js +83 -49
- package/lib/Utils/identity-change-handler.js +48 -0
- package/lib/Utils/index.js +19 -33
- package/lib/Utils/link-preview.js +14 -23
- package/lib/Utils/logger.js +2 -7
- package/lib/Utils/lt-hash.js +2 -46
- package/lib/Utils/make-mutex.js +24 -47
- package/lib/Utils/message-retry-manager.js +224 -0
- package/lib/Utils/messages-media.js +501 -496
- package/lib/Utils/messages.js +1428 -362
- package/lib/Utils/noise-handler.js +145 -100
- package/lib/Utils/pre-key-manager.js +105 -0
- package/lib/Utils/process-message.js +356 -150
- package/lib/Utils/reporting-utils.js +257 -0
- package/lib/Utils/signal.js +78 -73
- package/lib/Utils/sync-action-utils.js +47 -0
- package/lib/Utils/tc-token-utils.js +17 -0
- package/lib/Utils/use-multi-file-auth-state.js +35 -45
- package/lib/Utils/validate-connection.js +91 -107
- package/lib/WABinary/constants.js +1300 -1304
- package/lib/WABinary/decode.js +26 -48
- package/lib/WABinary/encode.js +109 -155
- package/lib/WABinary/generic-utils.js +161 -149
- package/lib/WABinary/index.js +5 -21
- package/lib/WABinary/jid-utils.js +73 -40
- package/lib/WABinary/types.js +1 -2
- package/lib/WAM/BinaryInfo.js +2 -6
- package/lib/WAM/constants.js +19070 -11568
- package/lib/WAM/encode.js +17 -23
- package/lib/WAM/index.js +3 -19
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +8 -12
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -15
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -13
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -14
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -23
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +13 -9
- package/lib/WAUSync/Protocols/index.js +4 -20
- package/lib/WAUSync/USyncQuery.js +40 -36
- package/lib/WAUSync/USyncUser.js +2 -6
- package/lib/WAUSync/index.js +3 -19
- package/lib/index.js +11 -44
- package/package.json +74 -107
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Signal/Group/queue-job.js +0 -57
- package/lib/Socket/Client/abstract-socket-client.js +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.js +0 -118
- package/lib/Socket/groupStatus.js +0 -637
- package/lib/Socket/registration.js +0 -166
- package/lib/Socket/usync.js +0 -70
- package/lib/Store/make-cache-manager-store.js +0 -83
- package/lib/Utils/baileys-event-stream.js +0 -63
package/lib/Signal/libsignal.js
CHANGED
|
@@ -1,138 +1,363 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
|
52
|
-
|
|
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
|
|
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
|
|
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
|
|
62
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
65
63
|
}
|
|
66
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
|
116
|
+
const builder = new GroupSessionBuilder(storage);
|
|
94
117
|
const senderNameStr = senderName.toString();
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
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
|
|
118
|
-
|
|
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
|
|
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
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
|
|
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
|
|
384
|
+
return SenderKeyRecord.deserialize(key);
|
|
160
385
|
}
|
|
161
|
-
return new
|
|
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: (
|
|
398
|
+
pubKey: Buffer.from(generateSignalPubKey(signedIdentityKey.public))
|
|
174
399
|
};
|
|
175
400
|
}
|
|
176
401
|
};
|
|
177
|
-
}
|
|
402
|
+
}
|