@badzz88/baileys 8.5.6 → 8.5.8
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 +337 -1055
- package/WAProto/WAProto.proto +28 -1189
- package/WAProto/fix-imports.js +79 -69
- package/WAProto/index.d.ts +71491 -15254
- package/WAProto/index.js +126436 -19814
- package/engine-requirements.js +8 -15
- package/lib/Defaults/index.js +130 -0
- package/{src → lib}/Signal/Group/ciphertext-message.js +2 -5
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/{src → lib}/Signal/Group/group_cipher.js +11 -11
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/keyhelper.js +18 -0
- package/{src → lib}/Signal/Group/sender-chain-key.js +6 -9
- package/{src → lib}/Signal/Group/sender-key-distribution-message.js +6 -9
- package/{src → lib}/Signal/Group/sender-key-message.js +9 -12
- package/{src → lib}/Signal/Group/sender-key-name.js +2 -5
- package/{src → lib}/Signal/Group/sender-key-record.js +8 -11
- package/{src → lib}/Signal/Group/sender-key-state.js +6 -9
- package/lib/Signal/Group/sender-message-key.js +26 -0
- package/{src → lib}/Signal/libsignal.js +131 -155
- package/{src → lib}/Signal/lid-mapping.js +27 -23
- package/lib/Socket/Client/index.js +3 -0
- package/lib/Socket/Client/types.js +11 -0
- package/{src → lib}/Socket/Client/websocket.js +11 -18
- package/{src → lib}/Socket/business.js +35 -67
- package/{src → lib}/Socket/chats.js +294 -550
- package/{src → lib}/Socket/communities.js +82 -114
- package/lib/Socket/graphql.js +716 -0
- package/lib/Socket/groups.js +374 -0
- package/lib/Socket/index.js +18 -0
- package/lib/Socket/interop.js +463 -0
- package/{src → lib}/Socket/luxu.js +120 -82
- package/lib/Socket/messages-recv.js +1916 -0
- package/lib/Socket/messages-send.js +1436 -0
- package/lib/Socket/mex.js +54 -0
- package/lib/Socket/newsletter.js +757 -0
- package/lib/Socket/privacy.js +318 -0
- package/{src → lib}/Socket/socket.js +356 -244
- package/lib/Socket/username.js +236 -0
- package/lib/Store/index.js +10 -0
- package/{src → lib}/Store/keyed-db.js +11 -21
- package/lib/Store/make-cache-manager-store.js +85 -0
- package/lib/Store/make-in-memory-store.js +244 -0
- package/{src → lib}/Store/make-ordered-dictionary.js +19 -25
- package/{src → lib}/Store/object-repository.js +11 -5
- 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/Mex.js +111 -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 +56 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +26 -0
- package/{src → lib}/Utils/auth-utils.js +91 -43
- package/lib/Utils/browser-utils.js +48 -0
- package/lib/Utils/business.js +231 -0
- package/lib/Utils/chat-utils.js +872 -0
- package/lib/Utils/companion-reg-client-utils.js +35 -0
- package/lib/Utils/crypto.js +118 -0
- package/lib/Utils/decode-wa-message.js +350 -0
- package/{src → lib}/Utils/event-buffer.js +81 -39
- package/lib/Utils/generics.js +403 -0
- package/lib/Utils/history.js +134 -0
- package/{src → lib}/Utils/identity-change-handler.js +9 -11
- package/lib/Utils/index.js +23 -0
- package/lib/Utils/link-preview.js +85 -0
- package/lib/Utils/logger.js +3 -0
- package/lib/Utils/lt-hash.js +8 -0
- package/{src → lib}/Utils/make-mutex.js +7 -10
- package/lib/Utils/message-composer.js +273 -0
- package/{src → lib}/Utils/message-retry-manager.js +76 -28
- package/lib/Utils/messages-media.js +870 -0
- package/lib/Utils/messages.js +1337 -0
- package/lib/Utils/noise-handler.js +201 -0
- package/{src → lib}/Utils/offline-node-processor.js +10 -4
- package/{src → lib}/Utils/pre-key-manager.js +27 -11
- package/lib/Utils/process-message.js +630 -0
- package/{src → lib}/Utils/reporting-utils.js +12 -15
- package/lib/Utils/signal.js +201 -0
- package/{src → lib}/Utils/stanza-ack.js +12 -4
- package/lib/Utils/sticker.js +110 -0
- package/{src → lib}/Utils/sync-action-utils.js +14 -10
- package/{src → lib}/Utils/tc-token-utils.js +48 -43
- package/{src → lib}/Utils/use-multi-file-auth-state.js +35 -25
- package/lib/Utils/validate-connection.js +203 -0
- package/{src → lib}/WABinary/constants.js +9 -12
- package/{src → lib}/WABinary/decode.js +38 -118
- package/{src → lib}/WABinary/encode.js +6 -11
- package/lib/WABinary/generic-utils.js +204 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/jid-utils.js +98 -0
- package/lib/WABinary/types.js +2 -0
- package/{src → lib}/WAM/BinaryInfo.js +2 -5
- package/{src → lib}/WAM/constants.js +4 -16635
- package/{src → lib}/WAM/encode.js +18 -17
- package/lib/WAM/index.js +4 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +43 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +54 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +58 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +39 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +27 -0
- package/lib/WAUSync/Protocols/index.js +12 -0
- package/lib/WAUSync/USyncQuery.js +151 -0
- package/lib/WAUSync/USyncUser.js +56 -0
- package/lib/WAUSync/index.js +3 -0
- package/{src → lib}/index.js +16 -51
- package/package.json +129 -99
- package/LICENSE +0 -21
- package/src/Defaults/index.js +0 -186
- package/src/Defaults/phonenumber-mcc.json +0 -223
- package/src/Signal/Group/group-session-builder.js +0 -86
- package/src/Signal/Group/index.js +0 -140
- package/src/Signal/Group/keyhelper.js +0 -77
- package/src/Signal/Group/sender-message-key.js +0 -29
- package/src/Socket/Client/index.js +0 -31
- package/src/Socket/Client/types.js +0 -13
- package/src/Socket/aigroups.js +0 -221
- package/src/Socket/graphql.js +0 -523
- package/src/Socket/groups.js +0 -516
- package/src/Socket/index.js +0 -31
- package/src/Socket/interactive-handler.js +0 -527
- package/src/Socket/interop.js +0 -339
- package/src/Socket/managed-account.js +0 -98
- package/src/Socket/messages-recv.js +0 -2685
- package/src/Socket/messages-send.js +0 -2047
- package/src/Socket/mex.js +0 -57
- package/src/Socket/newsletter.js +0 -455
- package/src/Socket/privacy.js +0 -125
- package/src/Socket/registration.js +0 -236
- package/src/Socket/text-router.js +0 -65
- package/src/Socket/username.js +0 -130
- package/src/Store/index.js +0 -33
- package/src/Store/make-cache-manager-store.js +0 -84
- package/src/Store/make-in-memory-store.js +0 -551
- package/src/Types/Auth.js +0 -31
- package/src/Types/Bussines.js +0 -2
- package/src/Types/Call.js +0 -2
- package/src/Types/Chat.js +0 -4
- package/src/Types/Contact.js +0 -2
- package/src/Types/Events.js +0 -2
- package/src/Types/GroupMetadata.js +0 -2
- package/src/Types/Label.js +0 -26
- package/src/Types/LabelAssociation.js +0 -8
- package/src/Types/Message.js +0 -93
- package/src/Types/Mex.js +0 -112
- package/src/Types/Newsletter.js +0 -109
- package/src/Types/Product.js +0 -2
- package/src/Types/Signal.js +0 -2
- package/src/Types/Socket.js +0 -2
- package/src/Types/State.js +0 -62
- package/src/Types/USync.js +0 -2
- package/src/Types/index.js +0 -56
- package/src/Utils/browser-utils.js +0 -112
- package/src/Utils/business.js +0 -240
- package/src/Utils/chat-utils.js +0 -1230
- package/src/Utils/command-loader.d.ts +0 -27
- package/src/Utils/command-loader.js +0 -89
- package/src/Utils/companion-reg-client-utils.js +0 -40
- package/src/Utils/consumer-application.js +0 -105
- package/src/Utils/crypto.js +0 -118
- package/src/Utils/curve25519-js.js +0 -242
- package/src/Utils/decode-wa-message.js +0 -529
- package/src/Utils/generics.js +0 -483
- package/src/Utils/group-history.js +0 -44
- package/src/Utils/history.js +0 -232
- package/src/Utils/index.js +0 -58
- package/src/Utils/jid-display-normalization.js +0 -195
- package/src/Utils/link-preview.js +0 -135
- package/src/Utils/logger.js +0 -8
- package/src/Utils/lt-hash.js +0 -25
- package/src/Utils/message-composer.js +0 -471
- package/src/Utils/messages-media.js +0 -843
- package/src/Utils/messages.js +0 -2817
- package/src/Utils/meta-ai-msmsg.js +0 -222
- package/src/Utils/native-bridge.js +0 -68
- package/src/Utils/noise-handler.js +0 -169
- package/src/Utils/process-message.js +0 -950
- package/src/Utils/session-pool.d.ts +0 -14
- package/src/Utils/session-pool.js +0 -70
- package/src/Utils/signal.js +0 -217
- package/src/Utils/sticker.d.ts +0 -13
- package/src/Utils/sticker.js +0 -123
- package/src/Utils/validate-connection.js +0 -209
- package/src/Utils/view-once-cache.d.ts +0 -12
- package/src/Utils/view-once-cache.js +0 -63
- package/src/Utils/voip-rekey.js +0 -22
- package/src/WABinary/generic-utils.js +0 -238
- package/src/WABinary/index.js +0 -34
- package/src/WABinary/jid-utils.js +0 -351
- package/src/WABinary/types.js +0 -2
- package/src/WAM/index.js +0 -32
- package/src/WAUSync/Protocols/USyncBotProfileProtocol.js +0 -53
- package/src/WAUSync/Protocols/USyncBusinessProtocol.js +0 -44
- package/src/WAUSync/Protocols/USyncContactProtocol.js +0 -55
- package/src/WAUSync/Protocols/USyncDeviceProtocol.js +0 -55
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -31
- package/src/WAUSync/Protocols/USyncFeatureProtocol.js +0 -54
- package/src/WAUSync/Protocols/USyncLIDProtocol.js +0 -32
- package/src/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -473
- package/src/WAUSync/Protocols/USyncPictureProtocol.js +0 -34
- package/src/WAUSync/Protocols/USyncSidelistProtocol.js +0 -29
- package/src/WAUSync/Protocols/USyncStatusProtocol.js +0 -42
- package/src/WAUSync/Protocols/USyncTextStatusProtocol.js +0 -38
- package/src/WAUSync/Protocols/USyncUsernameProtocol.js +0 -28
- package/src/WAUSync/Protocols/index.js +0 -40
- package/src/WAUSync/USyncQuery.js +0 -126
- package/src/WAUSync/USyncUser.js +0 -50
- package/src/WAUSync/index.js +0 -32
- package/src/antiban.js +0 -4152
|
@@ -1,81 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
Object.defineProperty(o, k2, desc);
|
|
17
|
-
}
|
|
18
|
-
: function (o, m, k, k2) {
|
|
19
|
-
if (k2 === undefined)
|
|
20
|
-
k2 = k;
|
|
21
|
-
o[k2] = m[k];
|
|
22
|
-
});
|
|
23
|
-
var __setModuleDefault = (this && this.__setModuleDefault) ||
|
|
24
|
-
(Object.create
|
|
25
|
-
? function (o, v) {
|
|
26
|
-
Object.defineProperty(o, 'default', { enumerable: true, value: v });
|
|
27
|
-
}
|
|
28
|
-
: function (o, v) {
|
|
29
|
-
o['default'] = v;
|
|
30
|
-
});
|
|
31
|
-
var __importStar = (this && this.__importStar) ||
|
|
32
|
-
(function () {
|
|
33
|
-
var ownKeys = function (o) {
|
|
34
|
-
ownKeys =
|
|
35
|
-
Object.getOwnPropertyNames ||
|
|
36
|
-
function (o) {
|
|
37
|
-
var ar = [];
|
|
38
|
-
for (var k in o)
|
|
39
|
-
if (Object.prototype.hasOwnProperty.call(o, k))
|
|
40
|
-
ar[ar.length] = k;
|
|
41
|
-
return ar;
|
|
42
|
-
};
|
|
43
|
-
return ownKeys(o);
|
|
44
|
-
};
|
|
45
|
-
return function (mod) {
|
|
46
|
-
if (mod && mod.__esModule)
|
|
47
|
-
return mod;
|
|
48
|
-
var result = {};
|
|
49
|
-
if (mod != null)
|
|
50
|
-
for (var k = ownKeys(mod), i = 0; i < k.length; i++)
|
|
51
|
-
if (k[i] !== 'default')
|
|
52
|
-
__createBinding(result, mod, k[i]);
|
|
53
|
-
__setModuleDefault(result, mod);
|
|
54
|
-
return result;
|
|
55
|
-
};
|
|
56
|
-
})();
|
|
57
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
58
|
-
exports.makeLibSignalRepository = makeLibSignalRepository;
|
|
59
|
-
const rb = require('../Utils/native-bridge').loadRustBridge();
|
|
60
|
-
const lru_cache_1 = require('lru-cache');
|
|
61
|
-
const Utils_1 = require('../Utils');
|
|
62
|
-
const WABinary_1 = require('../WABinary');
|
|
63
|
-
const sender_key_name_1 = require('./Group/sender-key-name');
|
|
64
|
-
const sender_key_record_1 = require('./Group/sender-key-record');
|
|
65
|
-
const Group_1 = require('./Group');
|
|
66
|
-
const lid_mapping_1 = require('./lid-mapping');
|
|
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 */
|
|
67
13
|
function extractIdentityFromPkmsg(ciphertext) {
|
|
68
14
|
try {
|
|
69
15
|
if (!ciphertext || ciphertext.length < 2) {
|
|
70
16
|
return undefined;
|
|
71
17
|
}
|
|
18
|
+
// Version byte check (version 3)
|
|
72
19
|
const version = ciphertext[0];
|
|
73
20
|
if ((version & 0xf) !== 3) {
|
|
74
21
|
return undefined;
|
|
75
22
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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);
|
|
79
27
|
}
|
|
80
28
|
return undefined;
|
|
81
29
|
}
|
|
@@ -83,50 +31,58 @@ function extractIdentityFromPkmsg(ciphertext) {
|
|
|
83
31
|
return undefined;
|
|
84
32
|
}
|
|
85
33
|
}
|
|
86
|
-
function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
87
|
-
|
|
88
|
-
throw new Error('The Signal session (Double Ratchet encryption) requires the "whatsapp-rust-bridge" native ' +
|
|
89
|
-
'module, which failed to load. Make sure it is installed: npm install whatsapp-rust-bridge@0.5.4');
|
|
90
|
-
}
|
|
91
|
-
const lidMapping = new lid_mapping_1.LIDMappingStore(auth.keys, logger, pnToLIDFunc);
|
|
34
|
+
export function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
35
|
+
const lidMapping = new LIDMappingStore(auth.keys, logger, pnToLIDFunc);
|
|
92
36
|
const storage = signalStorage(auth, lidMapping);
|
|
93
37
|
const parsedKeys = auth.keys;
|
|
94
|
-
const migratedSessionCache = new
|
|
95
|
-
ttl: 3 * 24 * 60 * 60 * 1000,
|
|
38
|
+
const migratedSessionCache = new LRUCache({
|
|
39
|
+
ttl: 3 * 24 * 60 * 60 * 1000, // 7 days
|
|
96
40
|
ttlAutopurge: true,
|
|
97
41
|
updateAgeOnGet: true
|
|
98
42
|
});
|
|
43
|
+
const ensureSenderKeyAndCreateSkdm = async (group, meId) => {
|
|
44
|
+
const senderName = jidToSignalSenderKeyName(group, meId);
|
|
45
|
+
const senderNameStr = senderName.toString();
|
|
46
|
+
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
47
|
+
if (!senderKey) {
|
|
48
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
49
|
+
}
|
|
50
|
+
const skdm = await new GroupSessionBuilder(storage).create(senderName);
|
|
51
|
+
return { senderName, skdm };
|
|
52
|
+
};
|
|
99
53
|
const repository = {
|
|
100
54
|
decryptGroupMessage({ group, authorJid, msg }) {
|
|
101
55
|
const senderName = jidToSignalSenderKeyName(group, authorJid);
|
|
102
|
-
const cipher = new
|
|
56
|
+
const cipher = new GroupCipher(storage, senderName);
|
|
57
|
+
// Use transaction to ensure atomicity
|
|
103
58
|
return parsedKeys.transaction(async () => {
|
|
104
59
|
return cipher.decrypt(msg);
|
|
105
60
|
}, group);
|
|
106
61
|
},
|
|
107
62
|
async processSenderKeyDistributionMessage({ item, authorJid }) {
|
|
108
|
-
const builder = new
|
|
63
|
+
const builder = new GroupSessionBuilder(storage);
|
|
109
64
|
if (!item.groupId) {
|
|
110
65
|
throw new Error('Group ID is required for sender key distribution message');
|
|
111
66
|
}
|
|
112
67
|
const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
|
|
113
|
-
const senderMsg = new
|
|
68
|
+
const senderMsg = new SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
|
|
114
69
|
const senderNameStr = senderName.toString();
|
|
115
70
|
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
116
71
|
if (!senderKey) {
|
|
117
|
-
await storage.storeSenderKey(senderName, new
|
|
72
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
118
73
|
}
|
|
119
74
|
return parsedKeys.transaction(async () => {
|
|
120
75
|
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
121
76
|
if (!senderKey) {
|
|
122
|
-
await storage.storeSenderKey(senderName, new
|
|
77
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
123
78
|
}
|
|
124
79
|
await builder.process(senderName, senderMsg);
|
|
125
80
|
}, item.groupId);
|
|
126
81
|
},
|
|
127
82
|
async decryptMessage({ jid, type, ciphertext }) {
|
|
128
83
|
const addr = jidToSignalProtocolAddress(jid);
|
|
129
|
-
const session = new
|
|
84
|
+
const session = new libsignal.SessionCipher(storage, addr);
|
|
85
|
+
// Extract and save sender's identity key before decryption for identity change detection
|
|
130
86
|
if (type === 'pkmsg') {
|
|
131
87
|
const identityKey = extractIdentityFromPkmsg(ciphertext);
|
|
132
88
|
if (identityKey) {
|
|
@@ -149,13 +105,16 @@ function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
|
149
105
|
}
|
|
150
106
|
return result;
|
|
151
107
|
}
|
|
108
|
+
// If it's not a sync message, we need to ensure atomicity
|
|
109
|
+
// For regular messages, we use a transaction to ensure atomicity
|
|
152
110
|
return parsedKeys.transaction(async () => {
|
|
153
111
|
return await doDecrypt();
|
|
154
112
|
}, jid);
|
|
155
113
|
},
|
|
156
114
|
async encryptMessage({ jid, data }) {
|
|
157
115
|
const addr = jidToSignalProtocolAddress(jid);
|
|
158
|
-
const cipher = new
|
|
116
|
+
const cipher = new libsignal.SessionCipher(storage, addr);
|
|
117
|
+
// Use transaction to ensure atomicity
|
|
159
118
|
return parsedKeys.transaction(async () => {
|
|
160
119
|
const { type: sigType, body } = await cipher.encrypt(data);
|
|
161
120
|
const type = sigType === 3 ? 'pkmsg' : 'msg';
|
|
@@ -163,61 +122,61 @@ function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
|
163
122
|
}, jid);
|
|
164
123
|
},
|
|
165
124
|
async encryptGroupMessage({ group, meId, data }) {
|
|
166
|
-
const senderName = jidToSignalSenderKeyName(group, meId);
|
|
167
|
-
const builder = new Group_1.GroupSessionBuilder(storage);
|
|
168
|
-
const senderNameStr = senderName.toString();
|
|
169
125
|
return parsedKeys.transaction(async () => {
|
|
170
|
-
const {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
const senderKeyDistributionMessage = await builder.create(senderName);
|
|
175
|
-
const session = new Group_1.GroupCipher(storage, senderName);
|
|
176
|
-
const ciphertext = await session.encrypt(data);
|
|
177
|
-
return {
|
|
178
|
-
ciphertext,
|
|
179
|
-
senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
|
|
180
|
-
};
|
|
126
|
+
const { senderName, skdm } = await ensureSenderKeyAndCreateSkdm(group, meId);
|
|
127
|
+
const ciphertext = await new GroupCipher(storage, senderName).encrypt(data);
|
|
128
|
+
return { ciphertext, senderKeyDistributionMessage: skdm.serialize() };
|
|
181
129
|
}, group);
|
|
182
130
|
},
|
|
183
|
-
async
|
|
184
|
-
logger.trace({ jid }, 'injecting E2EE session');
|
|
185
|
-
const cipher = new rb.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
|
|
131
|
+
async getSenderKeyDistributionMessage({ group, meId }) {
|
|
186
132
|
return parsedKeys.transaction(async () => {
|
|
187
|
-
await
|
|
188
|
-
|
|
133
|
+
const { skdm } = await ensureSenderKeyAndCreateSkdm(group, meId);
|
|
134
|
+
return skdm.serialize();
|
|
135
|
+
}, group);
|
|
136
|
+
},
|
|
137
|
+
async hasSenderKey({ group, meId }) {
|
|
138
|
+
const senderName = jidToSignalSenderKeyName(group, meId).toString();
|
|
139
|
+
const { [senderName]: key } = await auth.keys.get('sender-key', [senderName]);
|
|
140
|
+
return !!key;
|
|
189
141
|
},
|
|
190
142
|
async getSessionInfo(jid) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
return null;
|
|
196
|
-
const session = rb.SessionRecord.deserialize(sessBytes);
|
|
197
|
-
const open = session.getOpenSession?.();
|
|
198
|
-
const baseKey = open?.indexInfo?.baseKey;
|
|
199
|
-
const registrationId = open?.registrationId;
|
|
200
|
-
if (!baseKey || typeof registrationId !== 'number')
|
|
201
|
-
return null;
|
|
202
|
-
return { baseKey: new Uint8Array(baseKey), registrationId };
|
|
143
|
+
const addr = jidToSignalProtocolAddress(jid).toString();
|
|
144
|
+
const session = (await storage.loadSession(addr));
|
|
145
|
+
if (!session) {
|
|
146
|
+
return null;
|
|
203
147
|
}
|
|
204
|
-
|
|
148
|
+
const open = session.getOpenSession?.();
|
|
149
|
+
const baseKey = open?.indexInfo?.baseKey;
|
|
150
|
+
const registrationId = open?.registrationId;
|
|
151
|
+
if (!baseKey || typeof registrationId !== 'number') {
|
|
205
152
|
return null;
|
|
206
153
|
}
|
|
154
|
+
return { baseKey: new Uint8Array(baseKey), registrationId };
|
|
155
|
+
},
|
|
156
|
+
async injectE2ESession({ jid, session }) {
|
|
157
|
+
logger.trace({ jid }, 'injecting E2EE session');
|
|
158
|
+
const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
|
|
159
|
+
return parsedKeys.transaction(async () => {
|
|
160
|
+
// libsignal runtime accepts an absent prekey (initOutgoing checks `device.preKey && ...`)
|
|
161
|
+
// but the bundled .d.ts marks it required.
|
|
162
|
+
await cipher.initOutgoing(session);
|
|
163
|
+
}, jid);
|
|
207
164
|
},
|
|
208
165
|
jidToSignalProtocolAddress(jid) {
|
|
209
166
|
return jidToSignalProtocolAddress(jid).toString();
|
|
210
167
|
},
|
|
168
|
+
// Optimized direct access to LID mapping store
|
|
211
169
|
lidMapping,
|
|
212
170
|
async validateSession(jid) {
|
|
213
171
|
try {
|
|
214
172
|
const addr = jidToSignalProtocolAddress(jid);
|
|
215
|
-
const
|
|
216
|
-
if (!
|
|
173
|
+
const session = await storage.loadSession(addr.toString());
|
|
174
|
+
if (!session) {
|
|
217
175
|
return { exists: false, reason: 'no session' };
|
|
218
|
-
|
|
219
|
-
if (!session.haveOpenSession())
|
|
176
|
+
}
|
|
177
|
+
if (!session.haveOpenSession()) {
|
|
220
178
|
return { exists: false, reason: 'no open session' };
|
|
179
|
+
}
|
|
221
180
|
return { exists: true };
|
|
222
181
|
}
|
|
223
182
|
catch (error) {
|
|
@@ -227,41 +186,54 @@ function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
|
227
186
|
async deleteSession(jids) {
|
|
228
187
|
if (!jids.length)
|
|
229
188
|
return;
|
|
189
|
+
// Convert JIDs to signal addresses and prepare for bulk deletion
|
|
230
190
|
const sessionUpdates = {};
|
|
231
191
|
jids.forEach(jid => {
|
|
232
192
|
const addr = jidToSignalProtocolAddress(jid);
|
|
233
193
|
sessionUpdates[addr.toString()] = null;
|
|
234
194
|
});
|
|
195
|
+
// Single transaction for all deletions
|
|
235
196
|
return parsedKeys.transaction(async () => {
|
|
236
197
|
await auth.keys.set({ session: sessionUpdates });
|
|
237
198
|
}, `delete-${jids.length}-sessions`);
|
|
238
199
|
},
|
|
200
|
+
close() {
|
|
201
|
+
migratedSessionCache.clear();
|
|
202
|
+
lidMapping.close();
|
|
203
|
+
},
|
|
239
204
|
async migrateSession(fromJid, toJid) {
|
|
240
|
-
|
|
205
|
+
// TODO: use usync to handle this entire mess
|
|
206
|
+
if (!fromJid || (!isLidUser(toJid) && !isHostedLidUser(toJid)))
|
|
241
207
|
return { migrated: 0, skipped: 0, total: 0 };
|
|
242
|
-
|
|
208
|
+
// Only support PN to LID migration
|
|
209
|
+
if (!isPnUser(fromJid) && !isHostedPnUser(fromJid)) {
|
|
243
210
|
return { migrated: 0, skipped: 0, total: 1 };
|
|
244
211
|
}
|
|
245
|
-
const { user } =
|
|
212
|
+
const { user } = jidDecode(fromJid);
|
|
246
213
|
logger.debug({ fromJid }, 'bulk device migration - loading all user devices');
|
|
214
|
+
// Get user's device list from storage
|
|
247
215
|
const { [user]: userDevices } = await parsedKeys.get('device-list', [user]);
|
|
248
216
|
if (!userDevices) {
|
|
249
217
|
return { migrated: 0, skipped: 0, total: 0 };
|
|
250
218
|
}
|
|
251
|
-
const { device: fromDevice } =
|
|
219
|
+
const { device: fromDevice } = jidDecode(fromJid);
|
|
252
220
|
const fromDeviceStr = fromDevice?.toString() || '0';
|
|
253
221
|
if (!userDevices.includes(fromDeviceStr)) {
|
|
254
222
|
userDevices.push(fromDeviceStr);
|
|
255
223
|
}
|
|
224
|
+
// Filter out cached devices before database fetch
|
|
256
225
|
const uncachedDevices = userDevices.filter(device => {
|
|
257
226
|
const deviceKey = `${user}.${device}`;
|
|
258
227
|
return !migratedSessionCache.has(deviceKey);
|
|
259
228
|
});
|
|
229
|
+
// Bulk check session existence only for uncached devices
|
|
260
230
|
const deviceSessionKeys = uncachedDevices.map(device => `${user}.${device}`);
|
|
261
231
|
const existingSessions = await parsedKeys.get('session', deviceSessionKeys);
|
|
232
|
+
// Step 3: Convert existing sessions to JIDs (only migrate sessions that exist)
|
|
262
233
|
const deviceJids = [];
|
|
263
234
|
for (const [sessionKey, sessionData] of Object.entries(existingSessions)) {
|
|
264
235
|
if (sessionData) {
|
|
236
|
+
// Session exists in storage
|
|
265
237
|
const deviceStr = sessionKey.split('.')[1];
|
|
266
238
|
if (!deviceStr)
|
|
267
239
|
continue;
|
|
@@ -279,11 +251,12 @@ function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
|
279
251
|
devicesWithSessions: deviceJids.length,
|
|
280
252
|
devices: deviceJids
|
|
281
253
|
}, 'bulk device migration complete - all user devices processed');
|
|
254
|
+
// Single transaction for all migrations
|
|
282
255
|
return parsedKeys.transaction(async () => {
|
|
283
256
|
const migrationOps = deviceJids.map(jid => {
|
|
284
|
-
const lidWithDevice =
|
|
285
|
-
const fromDecoded =
|
|
286
|
-
const toDecoded =
|
|
257
|
+
const lidWithDevice = transferDevice(jid, toJid);
|
|
258
|
+
const fromDecoded = jidDecode(jid);
|
|
259
|
+
const toDecoded = jidDecode(lidWithDevice);
|
|
287
260
|
return {
|
|
288
261
|
fromJid: jid,
|
|
289
262
|
toJid: lidWithDevice,
|
|
@@ -296,25 +269,31 @@ function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
|
296
269
|
});
|
|
297
270
|
const totalOps = migrationOps.length;
|
|
298
271
|
let migratedCount = 0;
|
|
272
|
+
// Bulk fetch PN sessions - already exist (verified during device discovery)
|
|
299
273
|
const pnAddrStrings = Array.from(new Set(migrationOps.map(op => op.fromAddr.toString())));
|
|
300
274
|
const pnSessions = await parsedKeys.get('session', pnAddrStrings);
|
|
275
|
+
// Prepare bulk session updates (PN → LID migration + deletion)
|
|
301
276
|
const sessionUpdates = {};
|
|
302
277
|
for (const op of migrationOps) {
|
|
303
278
|
const pnAddrStr = op.fromAddr.toString();
|
|
304
279
|
const lidAddrStr = op.toAddr.toString();
|
|
305
280
|
const pnSession = pnSessions[pnAddrStr];
|
|
306
281
|
if (pnSession) {
|
|
307
|
-
|
|
282
|
+
// Session exists (guaranteed from device discovery)
|
|
283
|
+
const fromSession = libsignal.SessionRecord.deserialize(pnSession);
|
|
308
284
|
if (fromSession.haveOpenSession()) {
|
|
285
|
+
// Queue for bulk update: copy to LID, delete from PN
|
|
309
286
|
sessionUpdates[lidAddrStr] = fromSession.serialize();
|
|
310
287
|
sessionUpdates[pnAddrStr] = null;
|
|
311
288
|
migratedCount++;
|
|
312
289
|
}
|
|
313
290
|
}
|
|
314
291
|
}
|
|
292
|
+
// Single bulk session update for all migrations
|
|
315
293
|
if (Object.keys(sessionUpdates).length > 0) {
|
|
316
294
|
await parsedKeys.set({ session: sessionUpdates });
|
|
317
295
|
logger.debug({ migratedSessions: migratedCount }, 'bulk session migration complete');
|
|
296
|
+
// Cache device-level migrations
|
|
318
297
|
for (const op of migrationOps) {
|
|
319
298
|
if (sessionUpdates[op.toAddr.toString()]) {
|
|
320
299
|
const deviceKey = `${op.pnUser}.${op.deviceId}`;
|
|
@@ -324,38 +303,37 @@ function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
|
324
303
|
}
|
|
325
304
|
const skippedCount = totalOps - migratedCount;
|
|
326
305
|
return { migrated: migratedCount, skipped: skippedCount, total: totalOps };
|
|
327
|
-
}, `migrate-${deviceJids.length}-sessions-${
|
|
306
|
+
}, `migrate-${deviceJids.length}-sessions-${jidDecode(toJid)?.user}`);
|
|
328
307
|
}
|
|
329
308
|
};
|
|
330
309
|
return repository;
|
|
331
310
|
}
|
|
332
|
-
const jidToSignalProtocolAddress = jid => {
|
|
333
|
-
const decoded =
|
|
311
|
+
const jidToSignalProtocolAddress = (jid) => {
|
|
312
|
+
const decoded = jidDecode(jid);
|
|
334
313
|
const { user, device, server, domainType } = decoded;
|
|
335
314
|
if (!user) {
|
|
336
315
|
throw new Error(`JID decoded but user is empty: "${jid}" -> user: "${user}", server: "${server}", device: ${device}`);
|
|
337
316
|
}
|
|
338
|
-
const signalUser = domainType !==
|
|
317
|
+
const signalUser = domainType !== WAJIDDomains.WHATSAPP ? `${user}_${domainType}` : user;
|
|
339
318
|
const finalDevice = device || 0;
|
|
340
319
|
if (device === 99 && decoded.server !== 'hosted' && decoded.server !== 'hosted.lid') {
|
|
341
320
|
throw new Error('Unexpected non-hosted device JID with device 99. This ID seems invalid. ID:' + jid);
|
|
342
321
|
}
|
|
343
|
-
return new
|
|
322
|
+
return new libsignal.ProtocolAddress(signalUser, finalDevice);
|
|
344
323
|
};
|
|
345
324
|
const jidToSignalSenderKeyName = (group, user) => {
|
|
346
|
-
return new
|
|
325
|
+
return new SenderKeyName(group, jidToSignalProtocolAddress(user));
|
|
347
326
|
};
|
|
348
327
|
function signalStorage({ creds, keys }, lidMapping) {
|
|
328
|
+
// Shared function to resolve PN signal address to LID if mapping exists
|
|
349
329
|
const resolveLIDSignalAddress = async (id) => {
|
|
350
330
|
if (id.includes('.')) {
|
|
351
331
|
const [deviceId, device] = id.split('.');
|
|
352
332
|
const [user, domainType_] = deviceId.split('_');
|
|
353
333
|
const domainType = parseInt(domainType_ || '0');
|
|
354
|
-
if (domainType ===
|
|
355
|
-
domainType === WABinary_1.WAJIDDomains.HOSTED_LID ||
|
|
356
|
-
user.includes('-'))
|
|
334
|
+
if (domainType === WAJIDDomains.LID || domainType === WAJIDDomains.HOSTED_LID)
|
|
357
335
|
return id;
|
|
358
|
-
const pnJid = `${user}${device !== '0' ? `:${device}` : ''}@${domainType ===
|
|
336
|
+
const pnJid = `${user}${device !== '0' ? `:${device}` : ''}@${domainType === WAJIDDomains.HOSTED ? 'hosted' : 's.whatsapp.net'}`;
|
|
359
337
|
const lidForPN = await lidMapping.getLIDForPN(pnJid);
|
|
360
338
|
if (lidForPN) {
|
|
361
339
|
const lidAddr = jidToSignalProtocolAddress(lidForPN);
|
|
@@ -368,25 +346,22 @@ function signalStorage({ creds, keys }, lidMapping) {
|
|
|
368
346
|
loadSession: async (id) => {
|
|
369
347
|
try {
|
|
370
348
|
const wireJid = await resolveLIDSignalAddress(id);
|
|
371
|
-
const
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
if (!sess)
|
|
375
|
-
return null;
|
|
376
|
-
if (typeof sess === 'object' && !Buffer.isBuffer(sess) && !ArrayBuffer.isView(sess) && sess._sessions) {
|
|
377
|
-
return null;
|
|
349
|
+
const { [wireJid]: sess } = await keys.get('session', [wireJid]);
|
|
350
|
+
if (sess) {
|
|
351
|
+
return libsignal.SessionRecord.deserialize(sess);
|
|
378
352
|
}
|
|
379
|
-
return Buffer.isBuffer(sess) ? sess : Buffer.from(sess);
|
|
380
353
|
}
|
|
381
354
|
catch (e) {
|
|
382
355
|
return null;
|
|
383
356
|
}
|
|
357
|
+
return null;
|
|
384
358
|
},
|
|
385
359
|
storeSession: async (id, session) => {
|
|
386
|
-
|
|
360
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
361
|
+
await keys.set({ session: { [wireJid]: session.serialize() } });
|
|
387
362
|
},
|
|
388
363
|
isTrustedIdentity: () => {
|
|
389
|
-
return true;
|
|
364
|
+
return true; // TOFU - Trust on First Use (same as WhatsApp Web)
|
|
390
365
|
},
|
|
391
366
|
loadIdentityKey: async (id) => {
|
|
392
367
|
const wireJid = await resolveLIDSignalAddress(id);
|
|
@@ -396,10 +371,9 @@ function signalStorage({ creds, keys }, lidMapping) {
|
|
|
396
371
|
saveIdentity: async (id, identityKey) => {
|
|
397
372
|
const wireJid = await resolveLIDSignalAddress(id);
|
|
398
373
|
const { [wireJid]: existingKey } = await keys.get('identity-key', [wireJid]);
|
|
399
|
-
const keysMatch = existingKey &&
|
|
400
|
-
existingKey.length === identityKey.length &&
|
|
401
|
-
existingKey.every((byte, i) => byte === identityKey[i]);
|
|
374
|
+
const keysMatch = existingKey?.length === identityKey.length && existingKey.every((byte, i) => byte === identityKey[i]);
|
|
402
375
|
if (existingKey && !keysMatch) {
|
|
376
|
+
// Identity changed - clear session and update key
|
|
403
377
|
await keys.set({
|
|
404
378
|
session: { [wireJid]: null },
|
|
405
379
|
'identity-key': { [wireJid]: identityKey }
|
|
@@ -407,6 +381,7 @@ function signalStorage({ creds, keys }, lidMapping) {
|
|
|
407
381
|
return true;
|
|
408
382
|
}
|
|
409
383
|
if (!existingKey) {
|
|
384
|
+
// New contact - Trust on First Use (TOFU)
|
|
410
385
|
await keys.set({ 'identity-key': { [wireJid]: identityKey } });
|
|
411
386
|
return true;
|
|
412
387
|
}
|
|
@@ -422,7 +397,7 @@ function signalStorage({ creds, keys }, lidMapping) {
|
|
|
422
397
|
};
|
|
423
398
|
}
|
|
424
399
|
},
|
|
425
|
-
removePreKey: id => keys.set({ 'pre-key': { [id]: null } }),
|
|
400
|
+
removePreKey: (id) => keys.set({ 'pre-key': { [id]: null } }),
|
|
426
401
|
loadSignedPreKey: () => {
|
|
427
402
|
const key = creds.signedPreKey;
|
|
428
403
|
return {
|
|
@@ -434,9 +409,9 @@ function signalStorage({ creds, keys }, lidMapping) {
|
|
|
434
409
|
const keyId = senderKeyName.toString();
|
|
435
410
|
const { [keyId]: key } = await keys.get('sender-key', [keyId]);
|
|
436
411
|
if (key) {
|
|
437
|
-
return
|
|
412
|
+
return SenderKeyRecord.deserialize(key);
|
|
438
413
|
}
|
|
439
|
-
return new
|
|
414
|
+
return new SenderKeyRecord();
|
|
440
415
|
},
|
|
441
416
|
storeSenderKey: async (senderKeyName, key) => {
|
|
442
417
|
const keyId = senderKeyName.toString();
|
|
@@ -448,8 +423,9 @@ function signalStorage({ creds, keys }, lidMapping) {
|
|
|
448
423
|
const { signedIdentityKey } = creds;
|
|
449
424
|
return {
|
|
450
425
|
privKey: Buffer.from(signedIdentityKey.private),
|
|
451
|
-
pubKey: Buffer.from(
|
|
426
|
+
pubKey: Buffer.from(generateSignalPubKey(signedIdentityKey.public))
|
|
452
427
|
};
|
|
453
428
|
}
|
|
454
429
|
};
|
|
455
430
|
}
|
|
431
|
+
//# sourceMappingURL=libsignal.js.map
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const lru_cache_1 = require('lru-cache');
|
|
5
|
-
const WABinary_1 = require('../WABinary');
|
|
6
|
-
class LIDMappingStore {
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
|
+
import { isHostedPnUser, isLidUser, isPnUser, jidDecode, jidNormalizedUser, WAJIDDomains } from '../WABinary/index.js';
|
|
3
|
+
export class LIDMappingStore {
|
|
7
4
|
constructor(keys, logger, pnToLIDFunc) {
|
|
8
|
-
this.mappingCache = new
|
|
9
|
-
ttl: 3 * 24 * 60 * 60 * 1000,
|
|
5
|
+
this.mappingCache = new LRUCache({
|
|
6
|
+
ttl: 3 * 24 * 60 * 60 * 1000, // 7 days
|
|
10
7
|
ttlAutopurge: true,
|
|
11
8
|
updateAgeOnGet: true
|
|
12
9
|
});
|
|
@@ -21,13 +18,12 @@ class LIDMappingStore {
|
|
|
21
18
|
return;
|
|
22
19
|
const validatedPairs = [];
|
|
23
20
|
for (const { lid, pn } of pairs) {
|
|
24
|
-
if (!((
|
|
25
|
-
((0, WABinary_1.isPnUser)(lid) && (0, WABinary_1.isLidUser)(pn)))) {
|
|
21
|
+
if (!((isLidUser(lid) && isPnUser(pn)) || (isPnUser(lid) && isLidUser(pn)))) {
|
|
26
22
|
this.logger.warn(`Invalid LID-PN mapping: ${lid}, ${pn}`);
|
|
27
23
|
continue;
|
|
28
24
|
}
|
|
29
|
-
const lidDecoded =
|
|
30
|
-
const pnDecoded =
|
|
25
|
+
const lidDecoded = jidDecode(lid);
|
|
26
|
+
const pnDecoded = jidDecode(pn);
|
|
31
27
|
if (!lidDecoded || !pnDecoded)
|
|
32
28
|
continue;
|
|
33
29
|
validatedPairs.push({ pnUser: pnDecoded.user, lidUser: lidDecoded.user });
|
|
@@ -78,6 +74,7 @@ class LIDMappingStore {
|
|
|
78
74
|
await this.keys.transaction(async () => {
|
|
79
75
|
await this.keys.set({ 'lid-mapping': batchData });
|
|
80
76
|
}, 'lid-mapping');
|
|
77
|
+
// Update cache after successful DB write
|
|
81
78
|
for (const [pnUser, lidUser] of Object.entries(pairMap)) {
|
|
82
79
|
this.mappingCache.set(`pn:${pnUser}`, lidUser);
|
|
83
80
|
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
@@ -115,6 +112,7 @@ class LIDMappingStore {
|
|
|
115
112
|
this.logger.warn(`Invalid or empty LID user for PN ${pn}: lidUser = "${lidUser}"`);
|
|
116
113
|
return false;
|
|
117
114
|
}
|
|
115
|
+
// Push the PN device ID to the LID to maintain device separation
|
|
118
116
|
const pnDevice = decoded.device !== undefined ? decoded.device : 0;
|
|
119
117
|
const deviceSpecificLid = `${normalizedLidUser}${!!pnDevice ? `:${pnDevice}` : ``}@${decoded.server === 'hosted' ? 'hosted.lid' : 'lid'}`;
|
|
120
118
|
this.logger.trace(`getLIDForPN: ${pn} → ${deviceSpecificLid} (user mapping with device ${pnDevice})`);
|
|
@@ -122,9 +120,9 @@ class LIDMappingStore {
|
|
|
122
120
|
return true;
|
|
123
121
|
};
|
|
124
122
|
for (const pn of pns) {
|
|
125
|
-
if (!
|
|
123
|
+
if (!isPnUser(pn) && !isHostedPnUser(pn))
|
|
126
124
|
continue;
|
|
127
|
-
const decoded =
|
|
125
|
+
const decoded = jidDecode(pn);
|
|
128
126
|
if (!decoded)
|
|
129
127
|
continue;
|
|
130
128
|
const pnUser = decoded.user;
|
|
@@ -159,8 +157,8 @@ class LIDMappingStore {
|
|
|
159
157
|
else {
|
|
160
158
|
this.logger.trace(`No LID mapping found for PN user ${pnUser}; batch getting from USync`);
|
|
161
159
|
const device = decoded.device || 0;
|
|
162
|
-
let normalizedPn =
|
|
163
|
-
if (
|
|
160
|
+
let normalizedPn = jidNormalizedUser(pn);
|
|
161
|
+
if (isHostedPnUser(normalizedPn)) {
|
|
164
162
|
normalizedPn = `${pnUser}@s.whatsapp.net`;
|
|
165
163
|
}
|
|
166
164
|
if (!usyncFetch[normalizedPn]) {
|
|
@@ -173,15 +171,15 @@ class LIDMappingStore {
|
|
|
173
171
|
}
|
|
174
172
|
}
|
|
175
173
|
if (Object.keys(usyncFetch).length > 0) {
|
|
176
|
-
const result = await this.pnToLIDFunc?.(Object.keys(usyncFetch));
|
|
174
|
+
const result = await this.pnToLIDFunc?.(Object.keys(usyncFetch)); // this function already adds LIDs to mapping
|
|
177
175
|
if (result && result.length > 0) {
|
|
178
176
|
await this.storeLIDPNMappings(result);
|
|
179
177
|
for (const pair of result) {
|
|
180
|
-
const pnDecoded =
|
|
178
|
+
const pnDecoded = jidDecode(pair.pn);
|
|
181
179
|
const pnUser = pnDecoded?.user;
|
|
182
180
|
if (!pnUser)
|
|
183
181
|
continue;
|
|
184
|
-
const lidUser =
|
|
182
|
+
const lidUser = jidDecode(pair.lid)?.user;
|
|
185
183
|
if (!lidUser)
|
|
186
184
|
continue;
|
|
187
185
|
for (const device of usyncFetch[pair.pn]) {
|
|
@@ -228,15 +226,15 @@ class LIDMappingStore {
|
|
|
228
226
|
return false;
|
|
229
227
|
}
|
|
230
228
|
const lidDevice = decoded.device !== undefined ? decoded.device : 0;
|
|
231
|
-
const pnJid = `${pnUser}:${lidDevice}@${decoded.domainType ===
|
|
229
|
+
const pnJid = `${pnUser}:${lidDevice}@${decoded.domainType === WAJIDDomains.HOSTED_LID ? 'hosted' : 's.whatsapp.net'}`;
|
|
232
230
|
this.logger.trace(`Found reverse mapping: ${lid} → ${pnJid}`);
|
|
233
231
|
successfulPairs[lid] = { lid, pn: pnJid };
|
|
234
232
|
return true;
|
|
235
233
|
};
|
|
236
234
|
for (const lid of lids) {
|
|
237
|
-
if (!
|
|
235
|
+
if (!isLidUser(lid))
|
|
238
236
|
continue;
|
|
239
|
-
const decoded =
|
|
237
|
+
const decoded = jidDecode(lid);
|
|
240
238
|
if (!decoded)
|
|
241
239
|
continue;
|
|
242
240
|
const lidUser = decoded.user;
|
|
@@ -269,5 +267,11 @@ class LIDMappingStore {
|
|
|
269
267
|
}
|
|
270
268
|
return Object.values(successfulPairs).length ? Object.values(successfulPairs) : null;
|
|
271
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Close the cache and release resources
|
|
272
|
+
*/
|
|
273
|
+
close() {
|
|
274
|
+
this.mappingCache.clear();
|
|
275
|
+
}
|
|
272
276
|
}
|
|
273
|
-
|
|
277
|
+
//# sourceMappingURL=lid-mapping.js.map
|