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