@langitdeveloper/baileys 2.1.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Signal/lid-mapping.js +169 -0
- package/lib/WABinary/jid-utils.js +44 -1
- package/package.json +1 -1
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const { LRUCache } = require("lru-cache");
|
|
4
|
+
const {
|
|
5
|
+
isHostedPnUser,
|
|
6
|
+
isLidUser,
|
|
7
|
+
isPnUser,
|
|
8
|
+
jidDecode,
|
|
9
|
+
jidNormalizedUser,
|
|
10
|
+
WAJIDDomains,
|
|
11
|
+
} = require("../WABinary");
|
|
12
|
+
/**
|
|
13
|
+
* Keeps a persistent + in-memory mapping between PN (phone number) jids and
|
|
14
|
+
* LID jids. Needed because newer WhatsApp clients can address the same user
|
|
15
|
+
* via either identifier, and signal sessions are tied to one or the other.
|
|
16
|
+
*/
|
|
17
|
+
class LIDMappingStore {
|
|
18
|
+
constructor(keys, logger, pnToLIDFunc) {
|
|
19
|
+
this.mappingCache = new LRUCache({
|
|
20
|
+
ttl: 3 * 24 * 60 * 60 * 1e3,
|
|
21
|
+
ttlAutopurge: true,
|
|
22
|
+
updateAgeOnGet: true,
|
|
23
|
+
});
|
|
24
|
+
this.keys = keys;
|
|
25
|
+
this.pnToLIDFunc = pnToLIDFunc;
|
|
26
|
+
this.logger = logger;
|
|
27
|
+
}
|
|
28
|
+
async storeLIDPNMappings(pairs) {
|
|
29
|
+
const pairMap = {};
|
|
30
|
+
for (const { lid, pn } of pairs) {
|
|
31
|
+
if (!((isLidUser(lid) && isPnUser(pn)) || (isPnUser(lid) && isLidUser(pn)))) {
|
|
32
|
+
this.logger.warn(`Invalid LID-PN mapping: ${lid}, ${pn}`);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const lidDecoded = jidDecode(lid);
|
|
36
|
+
const pnDecoded = jidDecode(pn);
|
|
37
|
+
if (!lidDecoded || !pnDecoded) return;
|
|
38
|
+
const pnUser = pnDecoded.user;
|
|
39
|
+
const lidUser = lidDecoded.user;
|
|
40
|
+
let existingLidUser = this.mappingCache.get(`pn:${pnUser}`);
|
|
41
|
+
if (!existingLidUser) {
|
|
42
|
+
this.logger.trace(`Cache miss for PN user ${pnUser}; checking database`);
|
|
43
|
+
const stored = await this.keys.get("lid-mapping", [pnUser]);
|
|
44
|
+
existingLidUser = stored[pnUser];
|
|
45
|
+
if (existingLidUser) {
|
|
46
|
+
this.mappingCache.set(`pn:${pnUser}`, existingLidUser);
|
|
47
|
+
this.mappingCache.set(`lid:${existingLidUser}`, pnUser);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (existingLidUser === lidUser) {
|
|
51
|
+
this.logger.debug(
|
|
52
|
+
{ pnUser, lidUser },
|
|
53
|
+
"LID mapping already exists, skipping"
|
|
54
|
+
);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
pairMap[pnUser] = lidUser;
|
|
58
|
+
}
|
|
59
|
+
this.logger.trace(
|
|
60
|
+
{ pairMap },
|
|
61
|
+
`Storing ${Object.keys(pairMap).length} pn mappings`
|
|
62
|
+
);
|
|
63
|
+
await this.keys.transaction(async () => {
|
|
64
|
+
for (const [pnUser, lidUser] of Object.entries(pairMap)) {
|
|
65
|
+
await this.keys.set({
|
|
66
|
+
"lid-mapping": { [pnUser]: lidUser, [`${lidUser}_reverse`]: pnUser },
|
|
67
|
+
});
|
|
68
|
+
this.mappingCache.set(`pn:${pnUser}`, lidUser);
|
|
69
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
70
|
+
}
|
|
71
|
+
}, "lid-mapping");
|
|
72
|
+
}
|
|
73
|
+
async getLIDForPN(pn) {
|
|
74
|
+
return (await this.getLIDsForPNs([pn]))?.[0]?.lid || null;
|
|
75
|
+
}
|
|
76
|
+
async getLIDsForPNs(pns) {
|
|
77
|
+
const usyncFetch = {};
|
|
78
|
+
const successfulPairs = {};
|
|
79
|
+
for (const pn of pns) {
|
|
80
|
+
if (!isPnUser(pn) && !isHostedPnUser(pn)) continue;
|
|
81
|
+
const decoded = jidDecode(pn);
|
|
82
|
+
if (!decoded) continue;
|
|
83
|
+
const pnUser = decoded.user;
|
|
84
|
+
let lidUser = this.mappingCache.get(`pn:${pnUser}`);
|
|
85
|
+
if (!lidUser) {
|
|
86
|
+
const stored = await this.keys.get("lid-mapping", [pnUser]);
|
|
87
|
+
lidUser = stored[pnUser];
|
|
88
|
+
if (lidUser) {
|
|
89
|
+
this.mappingCache.set(`pn:${pnUser}`, lidUser);
|
|
90
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
91
|
+
} else {
|
|
92
|
+
this.logger.trace(
|
|
93
|
+
`No LID mapping found for PN user ${pnUser}; batch getting from USync`
|
|
94
|
+
);
|
|
95
|
+
const device = decoded.device || 0;
|
|
96
|
+
let normalizedPn = jidNormalizedUser(pn);
|
|
97
|
+
if (isHostedPnUser(normalizedPn)) {
|
|
98
|
+
normalizedPn = `${pnUser}@s.whatsapp.net`;
|
|
99
|
+
}
|
|
100
|
+
if (!usyncFetch[normalizedPn]) {
|
|
101
|
+
usyncFetch[normalizedPn] = [device];
|
|
102
|
+
} else {
|
|
103
|
+
usyncFetch[normalizedPn]?.push(device);
|
|
104
|
+
}
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
lidUser = lidUser.toString();
|
|
109
|
+
if (!lidUser) {
|
|
110
|
+
this.logger.warn(`Invalid or empty LID user for PN ${pn}: lidUser = "${lidUser}"`);
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
const pnDevice = decoded.device !== undefined ? decoded.device : 0;
|
|
114
|
+
const deviceSpecificLid = `${lidUser}${!!pnDevice ? `:${pnDevice}` : ``}@${decoded.domainType === WAJIDDomains.HOSTED ? "hosted.lid" : "lid"}`;
|
|
115
|
+
this.logger.trace(
|
|
116
|
+
`getLIDForPN: ${pn} → ${deviceSpecificLid} (user mapping with device ${pnDevice})`
|
|
117
|
+
);
|
|
118
|
+
successfulPairs[pn] = { lid: deviceSpecificLid, pn };
|
|
119
|
+
}
|
|
120
|
+
if (Object.keys(usyncFetch).length > 0) {
|
|
121
|
+
const result = await this.pnToLIDFunc?.(Object.keys(usyncFetch));
|
|
122
|
+
if (result && result.length > 0) {
|
|
123
|
+
await this.storeLIDPNMappings(result);
|
|
124
|
+
for (const pair of result) {
|
|
125
|
+
const pnDecoded = jidDecode(pair.pn);
|
|
126
|
+
const pnUser = pnDecoded?.user;
|
|
127
|
+
if (!pnUser) continue;
|
|
128
|
+
const lidUser = jidDecode(pair.lid)?.user;
|
|
129
|
+
if (!lidUser) continue;
|
|
130
|
+
for (const device of usyncFetch[pair.pn]) {
|
|
131
|
+
const deviceSpecificLid = `${lidUser}${!!device ? `:${device}` : ``}@${device === 99 ? "hosted.lid" : "lid"}`;
|
|
132
|
+
this.logger.trace(
|
|
133
|
+
`getLIDForPN: USYNC success for ${pair.pn} → ${deviceSpecificLid} (user mapping with device ${device})`
|
|
134
|
+
);
|
|
135
|
+
const deviceSpecificPn = `${pnUser}${!!device ? `:${device}` : ``}@${device === 99 ? "hosted" : "s.whatsapp.net"}`;
|
|
136
|
+
successfulPairs[deviceSpecificPn] = {
|
|
137
|
+
lid: deviceSpecificLid,
|
|
138
|
+
pn: deviceSpecificPn,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
} else {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return Object.values(successfulPairs);
|
|
147
|
+
}
|
|
148
|
+
async getPNForLID(lid) {
|
|
149
|
+
if (!isLidUser(lid)) return null;
|
|
150
|
+
const decoded = jidDecode(lid);
|
|
151
|
+
if (!decoded) return null;
|
|
152
|
+
const lidUser = decoded.user;
|
|
153
|
+
let pnUser = this.mappingCache.get(`lid:${lidUser}`);
|
|
154
|
+
if (!pnUser || typeof pnUser !== "string") {
|
|
155
|
+
const stored = await this.keys.get("lid-mapping", [`${lidUser}_reverse`]);
|
|
156
|
+
pnUser = stored[`${lidUser}_reverse`];
|
|
157
|
+
if (!pnUser || typeof pnUser !== "string") {
|
|
158
|
+
this.logger.trace(`No reverse mapping found for LID user: ${lidUser}`);
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
162
|
+
}
|
|
163
|
+
const lidDevice = decoded.device !== undefined ? decoded.device : 0;
|
|
164
|
+
const pnJid = `${pnUser}:${lidDevice}@${decoded.domainType === WAJIDDomains.HOSTED_LID ? "hosted" : "s.whatsapp.net"}`;
|
|
165
|
+
this.logger.trace(`Found reverse mapping: ${lid} → ${pnJid}`);
|
|
166
|
+
return pnJid;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
module.exports = { LIDMappingStore };
|
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.jidNormalizedUser = exports.isJidNewsLetter = exports.isJidStatusBroadcast = exports.isJidGroup = exports.isJidBroadcast = exports.isLidUser = exports.isJidUser = exports.areJidsSameUser = exports.jidDecode = exports.jidEncode = exports.STORIES_JID = exports.PSA_WID = exports.SERVER_JID = exports.OFFICIAL_BIZ_JID = exports.S_WHATSAPP_NET = void 0;
|
|
4
|
+
exports.isPnUser = exports.isHostedPnUser = exports.isHostedLidUser = exports.WAJIDDomains = exports.getServerFromDomainType = exports.transferDevice = void 0;
|
|
4
5
|
exports.S_WHATSAPP_NET = '@s.whatsapp.net';
|
|
5
6
|
exports.OFFICIAL_BIZ_JID = '16505361212@c.us';
|
|
6
7
|
exports.SERVER_JID = 'server@c.us';
|
|
7
8
|
exports.PSA_WID = '0@c.us';
|
|
8
9
|
exports.STORIES_JID = 'status@broadcast';
|
|
10
|
+
/** numeric domain-type codes used by newer WA clients for lid/hosted accounts */
|
|
11
|
+
const WAJIDDomains = { WHATSAPP: 0, LID: 1, HOSTED: 128, HOSTED_LID: 129 };
|
|
12
|
+
exports.WAJIDDomains = WAJIDDomains;
|
|
13
|
+
const getServerFromDomainType = (initialServer, domainType) => {
|
|
14
|
+
switch (domainType) {
|
|
15
|
+
case WAJIDDomains.LID:
|
|
16
|
+
return 'lid';
|
|
17
|
+
case WAJIDDomains.HOSTED:
|
|
18
|
+
return 'hosted';
|
|
19
|
+
case WAJIDDomains.HOSTED_LID:
|
|
20
|
+
return 'hosted.lid';
|
|
21
|
+
case WAJIDDomains.WHATSAPP:
|
|
22
|
+
default:
|
|
23
|
+
return initialServer;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
exports.getServerFromDomainType = getServerFromDomainType;
|
|
9
27
|
const jidEncode = (user, server, device, agent) => {
|
|
10
28
|
return `${user || ''}${!!agent ? `_${agent}` : ''}${!!device ? `:${device}` : ''}@${server}`;
|
|
11
29
|
};
|
|
@@ -19,14 +37,30 @@ const jidDecode = (jid) => {
|
|
|
19
37
|
const userCombined = jid.slice(0, sepIdx);
|
|
20
38
|
const [userAgent, device] = userCombined.split(':');
|
|
21
39
|
const user = userAgent.split('_')[0];
|
|
40
|
+
let domainType = WAJIDDomains.WHATSAPP;
|
|
41
|
+
if (server === 'lid') {
|
|
42
|
+
domainType = WAJIDDomains.LID;
|
|
43
|
+
} else if (server === 'hosted') {
|
|
44
|
+
domainType = WAJIDDomains.HOSTED;
|
|
45
|
+
} else if (server === 'hosted.lid') {
|
|
46
|
+
domainType = WAJIDDomains.HOSTED_LID;
|
|
47
|
+
}
|
|
22
48
|
return {
|
|
23
49
|
server,
|
|
24
50
|
user,
|
|
25
|
-
domainType
|
|
51
|
+
domainType,
|
|
26
52
|
device: device ? +device : undefined
|
|
27
53
|
};
|
|
28
54
|
};
|
|
29
55
|
exports.jidDecode = jidDecode;
|
|
56
|
+
/** transfers the device id from one jid onto another (used during lid<->pn swaps) */
|
|
57
|
+
const transferDevice = (fromJid, toJid) => {
|
|
58
|
+
const fromDecoded = jidDecode(fromJid);
|
|
59
|
+
const deviceId = (fromDecoded && fromDecoded.device) || 0;
|
|
60
|
+
const toDecoded = jidDecode(toJid);
|
|
61
|
+
return jidEncode(toDecoded.user, toDecoded.server, deviceId);
|
|
62
|
+
};
|
|
63
|
+
exports.transferDevice = transferDevice;
|
|
30
64
|
/** is the jid a user */
|
|
31
65
|
const areJidsSameUser = (jid1, jid2) => {
|
|
32
66
|
var _a, _b;
|
|
@@ -39,6 +73,15 @@ exports.isJidUser = isJidUser;
|
|
|
39
73
|
/** is the jid a group */
|
|
40
74
|
const isLidUser = (jid) => (jid === null || jid === void 0 ? void 0 : jid.endsWith('@lid'));
|
|
41
75
|
exports.isLidUser = isLidUser;
|
|
76
|
+
/** is the jid a regular phone-number user (alias of isJidUser, used by lid-mapping) */
|
|
77
|
+
const isPnUser = (jid) => (jid === null || jid === void 0 ? void 0 : jid.endsWith('@s.whatsapp.net'));
|
|
78
|
+
exports.isPnUser = isPnUser;
|
|
79
|
+
/** is the jid a "hosted" phone-number account */
|
|
80
|
+
const isHostedPnUser = (jid) => (jid === null || jid === void 0 ? void 0 : jid.endsWith('@hosted'));
|
|
81
|
+
exports.isHostedPnUser = isHostedPnUser;
|
|
82
|
+
/** is the jid a "hosted" lid account */
|
|
83
|
+
const isHostedLidUser = (jid) => (jid === null || jid === void 0 ? void 0 : jid.endsWith('@hosted.lid'));
|
|
84
|
+
exports.isHostedLidUser = isHostedLidUser;
|
|
42
85
|
/** is the jid a broadcast */
|
|
43
86
|
const isJidBroadcast = (jid) => (jid === null || jid === void 0 ? void 0 : jid.endsWith('@broadcast'));
|
|
44
87
|
exports.isJidBroadcast = isJidBroadcast;
|