davexbaileys 2.5.23 → 2.5.24
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/libsignal.js +79 -35
- package/lib/Signal/lid-mapping.js +156 -0
- package/lib/Socket/socket.js +98 -1
- package/lib/Types/Bussines.js +2 -0
- package/lib/Utils/generics.js +19 -1
- package/lib/WABinary/jid-utils.js +33 -3
- package/package.json +1 -1
package/lib/Signal/libsignal.js
CHANGED
|
@@ -35,18 +35,30 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.makeLibSignalRepository = makeLibSignalRepository;
|
|
37
37
|
const libsignal = __importStar(require("libsignal"));
|
|
38
|
+
const lru_cache_1 = require("lru-cache");
|
|
38
39
|
const Utils_1 = require("../Utils");
|
|
39
40
|
const WABinary_1 = require("../WABinary");
|
|
40
41
|
const sender_key_name_1 = require("./Group/sender-key-name");
|
|
41
42
|
const sender_key_record_1 = require("./Group/sender-key-record");
|
|
42
43
|
const Group_1 = require("./Group");
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
const lid_mapping_1 = require("./lid-mapping");
|
|
45
|
+
|
|
46
|
+
function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
47
|
+
const lidMapping = new lid_mapping_1.LIDMappingStore(auth.keys, logger, pnToLIDFunc);
|
|
48
|
+
const storage = signalStorage(auth, lidMapping);
|
|
49
|
+
const parsedKeys = auth.keys;
|
|
50
|
+
const migratedSessionCache = new lru_cache_1.LRUCache({
|
|
51
|
+
ttl: 3 * 24 * 60 * 60 * 1000,
|
|
52
|
+
ttlAutopurge: true,
|
|
53
|
+
updateAgeOnGet: true
|
|
54
|
+
});
|
|
45
55
|
return {
|
|
46
56
|
decryptGroupMessage({ group, authorJid, msg }) {
|
|
47
57
|
const senderName = jidToSignalSenderKeyName(group, authorJid);
|
|
48
58
|
const cipher = new Group_1.GroupCipher(storage, senderName);
|
|
49
|
-
return
|
|
59
|
+
return parsedKeys.transaction(async () => {
|
|
60
|
+
return cipher.decrypt(msg);
|
|
61
|
+
}, group);
|
|
50
62
|
},
|
|
51
63
|
async processSenderKeyDistributionMessage({ item, authorJid }) {
|
|
52
64
|
const builder = new Group_1.GroupSessionBuilder(storage);
|
|
@@ -56,60 +68,91 @@ function makeLibSignalRepository(auth) {
|
|
|
56
68
|
const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
|
|
57
69
|
const senderMsg = new Group_1.SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
|
|
58
70
|
const senderNameStr = senderName.toString();
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
71
|
+
return parsedKeys.transaction(async () => {
|
|
72
|
+
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
73
|
+
if (!senderKey) {
|
|
74
|
+
await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
|
|
75
|
+
}
|
|
76
|
+
await builder.process(senderName, senderMsg);
|
|
77
|
+
}, item.groupId);
|
|
64
78
|
},
|
|
65
79
|
async decryptMessage({ jid, type, ciphertext }) {
|
|
66
80
|
const addr = jidToSignalProtocolAddress(jid);
|
|
67
81
|
const session = new libsignal.SessionCipher(storage, addr);
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
82
|
+
async function doDecrypt() {
|
|
83
|
+
let result;
|
|
84
|
+
switch (type) {
|
|
85
|
+
case 'pkmsg':
|
|
86
|
+
result = await session.decryptPreKeyWhisperMessage(ciphertext);
|
|
87
|
+
break;
|
|
88
|
+
case 'msg':
|
|
89
|
+
result = await session.decryptWhisperMessage(ciphertext);
|
|
90
|
+
break;
|
|
91
|
+
default:
|
|
92
|
+
throw new Error(`Unknown message type: ${type}`);
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
78
95
|
}
|
|
79
|
-
return
|
|
96
|
+
return parsedKeys.transaction(async () => {
|
|
97
|
+
return await doDecrypt();
|
|
98
|
+
}, jid);
|
|
80
99
|
},
|
|
81
100
|
async encryptMessage({ jid, data }) {
|
|
82
101
|
const addr = jidToSignalProtocolAddress(jid);
|
|
83
102
|
const cipher = new libsignal.SessionCipher(storage, addr);
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
103
|
+
return parsedKeys.transaction(async () => {
|
|
104
|
+
const { type: sigType, body } = await cipher.encrypt(data);
|
|
105
|
+
const type = sigType === 3 ? 'pkmsg' : 'msg';
|
|
106
|
+
return { type, ciphertext: Buffer.from(body, 'binary') };
|
|
107
|
+
}, jid);
|
|
87
108
|
},
|
|
88
109
|
async encryptGroupMessage({ group, meId, data }) {
|
|
89
110
|
const senderName = jidToSignalSenderKeyName(group, meId);
|
|
90
111
|
const builder = new Group_1.GroupSessionBuilder(storage);
|
|
91
112
|
const senderNameStr = senderName.toString();
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
113
|
+
return parsedKeys.transaction(async () => {
|
|
114
|
+
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
115
|
+
if (!senderKey) {
|
|
116
|
+
await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
|
|
117
|
+
}
|
|
118
|
+
const senderKeyDistributionMessage = await builder.create(senderName);
|
|
119
|
+
const session = new Group_1.GroupCipher(storage, senderName);
|
|
120
|
+
const ciphertext = await session.encrypt(data);
|
|
121
|
+
return {
|
|
122
|
+
ciphertext,
|
|
123
|
+
senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
|
|
124
|
+
};
|
|
125
|
+
}, group);
|
|
103
126
|
},
|
|
104
127
|
async injectE2ESession({ jid, session }) {
|
|
105
128
|
const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
|
|
106
|
-
|
|
129
|
+
return parsedKeys.transaction(async () => {
|
|
130
|
+
await cipher.initOutgoing(session);
|
|
131
|
+
}, jid);
|
|
107
132
|
},
|
|
108
133
|
jidToSignalProtocolAddress(jid) {
|
|
109
134
|
return jidToSignalProtocolAddress(jid).toString();
|
|
135
|
+
},
|
|
136
|
+
lidMapping,
|
|
137
|
+
async validateSession(jid) {
|
|
138
|
+
try {
|
|
139
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
140
|
+
const session = await storage.loadSession(addr.toString());
|
|
141
|
+
if (!session) {
|
|
142
|
+
return { exists: false, reason: 'no session' };
|
|
143
|
+
}
|
|
144
|
+
if (!session.haveOpenSession()) {
|
|
145
|
+
return { exists: false, reason: 'no open session' };
|
|
146
|
+
}
|
|
147
|
+
return { exists: true };
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
return { exists: false, reason: 'validation error' };
|
|
151
|
+
}
|
|
110
152
|
}
|
|
111
153
|
};
|
|
112
154
|
}
|
|
155
|
+
|
|
113
156
|
const jidToSignalProtocolAddress = (jid) => {
|
|
114
157
|
const { user, device } = (0, WABinary_1.jidDecode)(jid);
|
|
115
158
|
return new libsignal.ProtocolAddress(user, device || 0);
|
|
@@ -117,7 +160,8 @@ const jidToSignalProtocolAddress = (jid) => {
|
|
|
117
160
|
const jidToSignalSenderKeyName = (group, user) => {
|
|
118
161
|
return new sender_key_name_1.SenderKeyName(group, jidToSignalProtocolAddress(user));
|
|
119
162
|
};
|
|
120
|
-
|
|
163
|
+
|
|
164
|
+
function signalStorage({ creds, keys }, lidMapping) {
|
|
121
165
|
return {
|
|
122
166
|
loadSession: async (id) => {
|
|
123
167
|
const { [id]: sess } = await keys.get('session', [id]);
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LIDMappingStore = void 0;
|
|
4
|
+
const lru_cache_1 = require("lru-cache");
|
|
5
|
+
const WABinary_1 = require("../WABinary");
|
|
6
|
+
|
|
7
|
+
class LIDMappingStore {
|
|
8
|
+
constructor(keys, logger, pnToLIDFunc) {
|
|
9
|
+
this.mappingCache = new lru_cache_1.LRUCache({
|
|
10
|
+
ttl: 3 * 24 * 60 * 60 * 1000,
|
|
11
|
+
ttlAutopurge: true,
|
|
12
|
+
updateAgeOnGet: true
|
|
13
|
+
});
|
|
14
|
+
this.keys = keys;
|
|
15
|
+
this.pnToLIDFunc = pnToLIDFunc;
|
|
16
|
+
this.logger = logger;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async storeLIDPNMappings(pairs) {
|
|
20
|
+
const pairMap = {};
|
|
21
|
+
for (const { lid, pn } of pairs) {
|
|
22
|
+
if (!((0, WABinary_1.isLidUser)(lid) && (0, WABinary_1.isPnUser)(pn)) &&
|
|
23
|
+
!((0, WABinary_1.isPnUser)(lid) && (0, WABinary_1.isLidUser)(pn))) {
|
|
24
|
+
this.logger.warn(`Invalid LID-PN mapping: ${lid}, ${pn}`);
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const lidDecoded = (0, WABinary_1.jidDecode)(lid);
|
|
28
|
+
const pnDecoded = (0, WABinary_1.jidDecode)(pn);
|
|
29
|
+
if (!lidDecoded || !pnDecoded) return;
|
|
30
|
+
const pnUser = pnDecoded.user;
|
|
31
|
+
const lidUser = lidDecoded.user;
|
|
32
|
+
let existingLidUser = this.mappingCache.get(`pn:${pnUser}`);
|
|
33
|
+
if (!existingLidUser) {
|
|
34
|
+
this.logger.trace(`Cache miss for PN user ${pnUser}; checking database`);
|
|
35
|
+
const stored = await this.keys.get('lid-mapping', [pnUser]);
|
|
36
|
+
existingLidUser = stored[pnUser];
|
|
37
|
+
if (existingLidUser) {
|
|
38
|
+
this.mappingCache.set(`pn:${pnUser}`, existingLidUser);
|
|
39
|
+
this.mappingCache.set(`lid:${existingLidUser}`, pnUser);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (existingLidUser === lidUser) {
|
|
43
|
+
this.logger.debug({ pnUser, lidUser }, 'LID mapping already exists, skipping');
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
pairMap[pnUser] = lidUser;
|
|
47
|
+
}
|
|
48
|
+
this.logger.trace({ pairMap }, `Storing ${Object.keys(pairMap).length} pn mappings`);
|
|
49
|
+
await this.keys.transaction(async () => {
|
|
50
|
+
for (const [pnUser, lidUser] of Object.entries(pairMap)) {
|
|
51
|
+
await this.keys.set({
|
|
52
|
+
'lid-mapping': {
|
|
53
|
+
[pnUser]: lidUser,
|
|
54
|
+
[`${lidUser}_reverse`]: pnUser
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
this.mappingCache.set(`pn:${pnUser}`, lidUser);
|
|
58
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
59
|
+
}
|
|
60
|
+
}, 'lid-mapping');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async getLIDForPN(pn) {
|
|
64
|
+
return (await this.getLIDsForPNs([pn]))?.[0]?.lid || null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async getLIDsForPNs(pns) {
|
|
68
|
+
const usyncFetch = {};
|
|
69
|
+
const successfulPairs = {};
|
|
70
|
+
for (const pn of pns) {
|
|
71
|
+
if (!(0, WABinary_1.isPnUser)(pn) && !(0, WABinary_1.isHostedPnUser)(pn)) continue;
|
|
72
|
+
const decoded = (0, WABinary_1.jidDecode)(pn);
|
|
73
|
+
if (!decoded) continue;
|
|
74
|
+
const pnUser = decoded.user;
|
|
75
|
+
let lidUser = this.mappingCache.get(`pn:${pnUser}`);
|
|
76
|
+
if (!lidUser) {
|
|
77
|
+
const stored = await this.keys.get('lid-mapping', [pnUser]);
|
|
78
|
+
lidUser = stored[pnUser];
|
|
79
|
+
if (lidUser) {
|
|
80
|
+
this.mappingCache.set(`pn:${pnUser}`, lidUser);
|
|
81
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
this.logger.trace(`No LID mapping found for PN user ${pnUser}; batch getting from USync`);
|
|
85
|
+
const device = decoded.device || 0;
|
|
86
|
+
let normalizedPn = (0, WABinary_1.jidNormalizedUser)(pn);
|
|
87
|
+
if ((0, WABinary_1.isHostedPnUser)(normalizedPn)) {
|
|
88
|
+
normalizedPn = `${pnUser}@s.whatsapp.net`;
|
|
89
|
+
}
|
|
90
|
+
if (!usyncFetch[normalizedPn]) {
|
|
91
|
+
usyncFetch[normalizedPn] = [device];
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
usyncFetch[normalizedPn]?.push(device);
|
|
95
|
+
}
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
lidUser = lidUser.toString();
|
|
100
|
+
if (!lidUser) {
|
|
101
|
+
this.logger.warn(`Invalid or empty LID user for PN ${pn}: lidUser = "${lidUser}"`);
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
const pnDevice = decoded.device !== undefined ? decoded.device : 0;
|
|
105
|
+
const domainType = decoded.domainType;
|
|
106
|
+
const deviceSpecificLid = `${lidUser}${pnDevice ? `:${pnDevice}` : ''}@${domainType === 128 ? 'hosted.lid' : 'lid'}`;
|
|
107
|
+
this.logger.trace(`getLIDForPN: ${pn} → ${deviceSpecificLid} (user mapping with device ${pnDevice})`);
|
|
108
|
+
successfulPairs[pn] = { lid: deviceSpecificLid, pn };
|
|
109
|
+
}
|
|
110
|
+
if (Object.keys(usyncFetch).length > 0) {
|
|
111
|
+
const result = await this.pnToLIDFunc?.(Object.keys(usyncFetch));
|
|
112
|
+
if (result && result.length > 0) {
|
|
113
|
+
await this.storeLIDPNMappings(result);
|
|
114
|
+
for (const pair of result) {
|
|
115
|
+
const pnDecoded = (0, WABinary_1.jidDecode)(pair.pn);
|
|
116
|
+
const pnUser = pnDecoded?.user;
|
|
117
|
+
if (!pnUser) continue;
|
|
118
|
+
const lidUser = (0, WABinary_1.jidDecode)(pair.lid)?.user;
|
|
119
|
+
if (!lidUser) continue;
|
|
120
|
+
for (const device of (usyncFetch[pair.pn] || [])) {
|
|
121
|
+
const deviceSpecificLid = `${lidUser}${device ? `:${device}` : ''}@${device === 99 ? 'hosted.lid' : 'lid'}`;
|
|
122
|
+
this.logger.trace(`getLIDForPN: USYNC success for ${pair.pn} → ${deviceSpecificLid}`);
|
|
123
|
+
const deviceSpecificPn = `${pnUser}${device ? `:${device}` : ''}@${device === 99 ? 'hosted' : 's.whatsapp.net'}`;
|
|
124
|
+
successfulPairs[deviceSpecificPn] = { lid: deviceSpecificLid, pn: deviceSpecificPn };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return Object.values(successfulPairs);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async getPNForLID(lid) {
|
|
136
|
+
if (!(0, WABinary_1.isLidUser)(lid)) return null;
|
|
137
|
+
const decoded = (0, WABinary_1.jidDecode)(lid);
|
|
138
|
+
if (!decoded) return null;
|
|
139
|
+
const lidUser = decoded.user;
|
|
140
|
+
let pnUser = this.mappingCache.get(`lid:${lidUser}`);
|
|
141
|
+
if (!pnUser || typeof pnUser !== 'string') {
|
|
142
|
+
const stored = await this.keys.get('lid-mapping', [`${lidUser}_reverse`]);
|
|
143
|
+
pnUser = stored[`${lidUser}_reverse`];
|
|
144
|
+
if (!pnUser || typeof pnUser !== 'string') {
|
|
145
|
+
this.logger.trace(`No reverse mapping found for LID user: ${lidUser}`);
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
149
|
+
}
|
|
150
|
+
const lidDevice = decoded.device !== undefined ? decoded.device : 0;
|
|
151
|
+
const pnJid = `${pnUser}:${lidDevice}@${decoded.domainType === 129 ? 'hosted' : 's.whatsapp.net'}`;
|
|
152
|
+
this.logger.trace(`Found reverse mapping: ${lid} → ${pnJid}`);
|
|
153
|
+
return pnJid;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
exports.LIDMappingStore = LIDMappingStore;
|
package/lib/Socket/socket.js
CHANGED
|
@@ -45,7 +45,101 @@ const makeSocket = (config) => {
|
|
|
45
45
|
const { creds } = authState;
|
|
46
46
|
// add transaction capability
|
|
47
47
|
const keys = (0, Utils_1.addTransactionCapability)(authState.keys, logger, transactionOpts);
|
|
48
|
-
|
|
48
|
+
// Validate current key-bundle on server; on failure, trigger pre-key upload and rethrow
|
|
49
|
+
const digestKeyBundle = async () => {
|
|
50
|
+
const res = await query({
|
|
51
|
+
tag: 'iq',
|
|
52
|
+
attrs: { to: WABinary_1.S_WHATSAPP_NET, type: 'get', xmlns: 'encrypt' },
|
|
53
|
+
content: [{ tag: 'digest', attrs: {} }]
|
|
54
|
+
});
|
|
55
|
+
const digestNode = (0, WABinary_1.getBinaryNodeChild)(res, 'digest');
|
|
56
|
+
if (!digestNode) {
|
|
57
|
+
await uploadPreKeys();
|
|
58
|
+
throw new Error('encrypt/get digest returned no digest node');
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
// Rotate our signed pre-key on server
|
|
62
|
+
const rotateSignedPreKey = async () => {
|
|
63
|
+
const newId = (creds.signedPreKey.keyId || 0) + 1;
|
|
64
|
+
const skey = await (0, Utils_1.signedKeyPair)(creds.signedIdentityKey, newId);
|
|
65
|
+
await query({
|
|
66
|
+
tag: 'iq',
|
|
67
|
+
attrs: { to: WABinary_1.S_WHATSAPP_NET, type: 'set', xmlns: 'encrypt' },
|
|
68
|
+
content: [
|
|
69
|
+
{
|
|
70
|
+
tag: 'rotate',
|
|
71
|
+
attrs: {},
|
|
72
|
+
content: [(0, Utils_1.xmppSignedPreKey)(skey)]
|
|
73
|
+
}
|
|
74
|
+
]
|
|
75
|
+
});
|
|
76
|
+
ev.emit('creds.update', { signedPreKey: skey });
|
|
77
|
+
};
|
|
78
|
+
const executeUSyncQuery = async (usyncQuery) => {
|
|
79
|
+
if (!usyncQuery.protocols || usyncQuery.protocols.length === 0) {
|
|
80
|
+
throw new boom_1.Boom('USyncQuery must have at least one protocol');
|
|
81
|
+
}
|
|
82
|
+
const validUsers = usyncQuery.users;
|
|
83
|
+
const userNodes = validUsers.map(user => {
|
|
84
|
+
return {
|
|
85
|
+
tag: 'user',
|
|
86
|
+
attrs: {
|
|
87
|
+
jid: !user.phone ? user.id : undefined
|
|
88
|
+
},
|
|
89
|
+
content: usyncQuery.protocols.map(a => a.getUserElement(user)).filter(a => a !== null)
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
const listNode = { tag: 'list', attrs: {}, content: userNodes };
|
|
93
|
+
const queryNode = {
|
|
94
|
+
tag: 'query',
|
|
95
|
+
attrs: {},
|
|
96
|
+
content: usyncQuery.protocols.map(a => a.getQueryElement())
|
|
97
|
+
};
|
|
98
|
+
const iq = {
|
|
99
|
+
tag: 'iq',
|
|
100
|
+
attrs: { to: WABinary_1.S_WHATSAPP_NET, type: 'get', xmlns: 'usync' },
|
|
101
|
+
content: [
|
|
102
|
+
{
|
|
103
|
+
tag: 'usync',
|
|
104
|
+
attrs: {
|
|
105
|
+
context: usyncQuery.context,
|
|
106
|
+
mode: usyncQuery.mode,
|
|
107
|
+
sid: generateMessageTag(),
|
|
108
|
+
last: 'true',
|
|
109
|
+
index: '0'
|
|
110
|
+
},
|
|
111
|
+
content: [queryNode, listNode]
|
|
112
|
+
}
|
|
113
|
+
]
|
|
114
|
+
};
|
|
115
|
+
const result = await query(iq);
|
|
116
|
+
return usyncQuery.parseUSyncQueryResult(result);
|
|
117
|
+
};
|
|
118
|
+
const pnFromLIDUSync = async (jids) => {
|
|
119
|
+
try {
|
|
120
|
+
const WAUSync_1 = require('../WAUSync');
|
|
121
|
+
const usyncQuery = new WAUSync_1.USyncQuery().withLIDProtocol().withContext('background');
|
|
122
|
+
for (const jid of jids) {
|
|
123
|
+
if ((0, WABinary_1.isLidUser)(jid)) {
|
|
124
|
+
logger?.warn('LID user found in LID fetch call');
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
usyncQuery.withUser(new WAUSync_1.USyncUser().withId(jid));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (usyncQuery.users.length === 0) return [];
|
|
132
|
+
const results = await executeUSyncQuery(usyncQuery);
|
|
133
|
+
if (results) {
|
|
134
|
+
return results.list.filter(a => !!a.lid).map(({ lid, id }) => ({ pn: id, lid }));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
logger?.debug({ err: e?.message }, 'pnFromLIDUSync failed');
|
|
139
|
+
}
|
|
140
|
+
return [];
|
|
141
|
+
};
|
|
142
|
+
const signalRepository = makeSignalRepository({ creds, keys }, logger, pnFromLIDUSync);
|
|
49
143
|
let lastDateRecv;
|
|
50
144
|
let epoch = 1;
|
|
51
145
|
let keepAliveReq;
|
|
@@ -600,6 +694,9 @@ const makeSocket = (config) => {
|
|
|
600
694
|
uploadPreKeys,
|
|
601
695
|
uploadPreKeysToServerIfRequired,
|
|
602
696
|
requestPairingCode,
|
|
697
|
+
digestKeyBundle,
|
|
698
|
+
rotateSignedPreKey,
|
|
699
|
+
executeUSyncQuery,
|
|
603
700
|
/** Waits for the connection to WA to reach a state */
|
|
604
701
|
waitForConnectionUpdate: (0, Utils_1.bindWaitForConnectionUpdate)(ev),
|
|
605
702
|
sendWAMBuffer
|
package/lib/Utils/generics.js
CHANGED
|
@@ -9,7 +9,7 @@ exports.isWABusinessPlatform = exports.getCodeFromWSError = exports.getCallStatu
|
|
|
9
9
|
isLatest: true
|
|
10
10
|
};
|
|
11
11
|
};
|
|
12
|
-
exports.fetchLatestWaWebVersion = exports.fetchLatestBaileysVersion = exports.bindWaitForConnectionUpdate = exports.generateMessageID = exports.generateMessageIDV2 = exports.delayCancellable = exports.delay = exports.debouncedTimeout = exports.unixTimestampSeconds = exports.toNumber = exports.encodeBigEndian = exports.generateRegistrationId = exports.encodeWAMessage = exports.unpadRandomMax16 = exports.writeRandomPadMax16 = exports.getKeyAuthor = exports.BufferJSON = exports.getPlatformId = exports.Browsers = void 0;
|
|
12
|
+
exports.fetchLatestWaWebVersion = exports.fetchLatestBaileysVersion = exports.bindWaitForConnectionUpdate = exports.generateMessageID = exports.generateMessageIDV2 = exports.delayCancellable = exports.delay = exports.debouncedTimeout = exports.unixTimestampSeconds = exports.toNumber = exports.encodeBigEndian = exports.generateRegistrationId = exports.generateParticipantHashV2 = exports.encodeWAMessage = exports.unpadRandomMax16 = exports.writeRandomPadMax16 = exports.getKeyAuthor = exports.BufferJSON = exports.getPlatformId = exports.Browsers = void 0;
|
|
13
13
|
exports.promiseTimeout = promiseTimeout;
|
|
14
14
|
exports.bindWaitForEvent = bindWaitForEvent;
|
|
15
15
|
exports.trimUndefined = trimUndefined;
|
|
@@ -55,10 +55,22 @@ exports.BufferJSON = {
|
|
|
55
55
|
},
|
|
56
56
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
57
57
|
reviver: (_, value) => {
|
|
58
|
+
if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
|
|
59
|
+
return Buffer.from(value.data, 'base64');
|
|
60
|
+
}
|
|
58
61
|
if (typeof value === 'object' && !!value && (value.buffer === true || value.type === 'Buffer')) {
|
|
59
62
|
const val = value.data || value.value;
|
|
60
63
|
return typeof val === 'string' ? Buffer.from(val, 'base64') : Buffer.from(val || []);
|
|
61
64
|
}
|
|
65
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
66
|
+
const keys = Object.keys(value);
|
|
67
|
+
if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
|
|
68
|
+
const values = Object.values(value);
|
|
69
|
+
if (values.every(v => typeof v === 'number')) {
|
|
70
|
+
return Buffer.from(values);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
62
74
|
return value;
|
|
63
75
|
}
|
|
64
76
|
};
|
|
@@ -85,6 +97,12 @@ const unpadRandomMax16 = (e) => {
|
|
|
85
97
|
return new Uint8Array(t.buffer, t.byteOffset, t.length - r);
|
|
86
98
|
};
|
|
87
99
|
exports.unpadRandomMax16 = unpadRandomMax16;
|
|
100
|
+
const generateParticipantHashV2 = (participants) => {
|
|
101
|
+
const sorted = [...participants].sort();
|
|
102
|
+
const hash = (0, crypto_1.createHash)('sha256').update(Buffer.from(sorted.join(''))).digest('base64');
|
|
103
|
+
return '2:' + hash.slice(0, 6);
|
|
104
|
+
};
|
|
105
|
+
exports.generateParticipantHashV2 = generateParticipantHashV2;
|
|
88
106
|
const encodeWAMessage = (message) => (0, exports.writeRandomPadMax16)(WAProto_1.proto.Message.encode(message).finish());
|
|
89
107
|
exports.encodeWAMessage = encodeWAMessage;
|
|
90
108
|
const generateRegistrationId = () => {
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.jidNormalizedUser = exports.isJidBot = exports.isJidNewsletter = exports.isJidStatusBroadcast = exports.isJidGroup = exports.isJidBroadcast = exports.isLidUser = exports.isJidUser = exports.isJidMetaIa = exports.areJidsSameUser = exports.jidDecode = exports.jidEncode = exports.META_AI_JID = exports.STORIES_JID = exports.PSA_WID = exports.SERVER_JID = exports.OFFICIAL_BIZ_JID = exports.S_WHATSAPP_NET = void 0;
|
|
3
|
+
exports.jidNormalizedUser = exports.isJidBot = exports.isJidNewsletter = exports.isJidStatusBroadcast = exports.isJidGroup = exports.isJidBroadcast = exports.isHostedLidUser = exports.isHostedPnUser = exports.isPnUser = exports.isLidUser = exports.isJidUser = exports.isJidMetaIa = exports.areJidsSameUser = exports.jidDecode = exports.jidEncode = exports.WAJIDDomains = exports.META_AI_JID = exports.STORIES_JID = exports.PSA_WID = exports.SERVER_JID = exports.OFFICIAL_BIZ_JID = exports.S_WHATSAPP_NET = void 0;
|
|
4
|
+
var WAJIDDomains;
|
|
5
|
+
(function (WAJIDDomains) {
|
|
6
|
+
WAJIDDomains[WAJIDDomains["WHATSAPP"] = 0] = "WHATSAPP";
|
|
7
|
+
WAJIDDomains[WAJIDDomains["LID"] = 1] = "LID";
|
|
8
|
+
WAJIDDomains[WAJIDDomains["HOSTED"] = 128] = "HOSTED";
|
|
9
|
+
WAJIDDomains[WAJIDDomains["HOSTED_LID"] = 129] = "HOSTED_LID";
|
|
10
|
+
})(WAJIDDomains = exports.WAJIDDomains || (exports.WAJIDDomains = {}));
|
|
4
11
|
exports.S_WHATSAPP_NET = '@s.whatsapp.net';
|
|
5
12
|
exports.OFFICIAL_BIZ_JID = '16505361212@c.us';
|
|
6
13
|
exports.SERVER_JID = 'server@c.us';
|
|
@@ -20,10 +27,24 @@ const jidDecode = (jid) => {
|
|
|
20
27
|
const userCombined = jid.slice(0, sepIdx);
|
|
21
28
|
const [userAgent, device] = userCombined.split(':');
|
|
22
29
|
const user = userAgent.split('_')[0];
|
|
30
|
+
let domainType;
|
|
31
|
+
switch (server) {
|
|
32
|
+
case 'lid':
|
|
33
|
+
domainType = WAJIDDomains.LID;
|
|
34
|
+
break;
|
|
35
|
+
case 'hosted':
|
|
36
|
+
domainType = WAJIDDomains.HOSTED;
|
|
37
|
+
break;
|
|
38
|
+
case 'hosted.lid':
|
|
39
|
+
domainType = WAJIDDomains.HOSTED_LID;
|
|
40
|
+
break;
|
|
41
|
+
default:
|
|
42
|
+
domainType = WAJIDDomains.WHATSAPP;
|
|
43
|
+
}
|
|
23
44
|
return {
|
|
24
45
|
server: server,
|
|
25
46
|
user,
|
|
26
|
-
domainType
|
|
47
|
+
domainType,
|
|
27
48
|
device: device ? +device : undefined
|
|
28
49
|
};
|
|
29
50
|
};
|
|
@@ -37,7 +58,16 @@ exports.isJidMetaIa = isJidMetaIa;
|
|
|
37
58
|
/** is the jid a user */
|
|
38
59
|
const isJidUser = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith('@s.whatsapp.net');
|
|
39
60
|
exports.isJidUser = isJidUser;
|
|
40
|
-
/** is the jid a
|
|
61
|
+
/** is the jid a PN (phone number) user */
|
|
62
|
+
const isPnUser = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith('@s.whatsapp.net');
|
|
63
|
+
exports.isPnUser = isPnUser;
|
|
64
|
+
/** is the jid a hosted PN user */
|
|
65
|
+
const isHostedPnUser = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith('@hosted');
|
|
66
|
+
exports.isHostedPnUser = isHostedPnUser;
|
|
67
|
+
/** is the jid a hosted LID user */
|
|
68
|
+
const isHostedLidUser = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith('@hosted.lid');
|
|
69
|
+
exports.isHostedLidUser = isHostedLidUser;
|
|
70
|
+
/** is the jid a LID user */
|
|
41
71
|
const isLidUser = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith('@lid');
|
|
42
72
|
exports.isLidUser = isLidUser;
|
|
43
73
|
/** is the jid a broadcast */
|
package/package.json
CHANGED