@langitdeveloper/baileys 2.2.4 → 2.2.6
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 +0 -8
- package/lib/Signal/lid-mapping.js +0 -3
- package/lib/Socket/chats.js +56 -0
- package/lib/Socket/messages-send.js +7 -17
- package/lib/Utils/generics.js +21 -0
- package/lib/Utils/tc-token-utils.js +114 -0
- package/lib/WABinary/generic-utils.js +19 -3
- package/package.json +1 -1
package/lib/Signal/libsignal.js
CHANGED
|
@@ -55,8 +55,6 @@ function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
|
55
55
|
updateAgeOnGet: true
|
|
56
56
|
});
|
|
57
57
|
const runInTransaction = async (work, label) => {
|
|
58
|
-
// mahiru's addTransactionCapability().transaction() only takes one arg,
|
|
59
|
-
// the label is accepted (and ignored) for compatibility with upstream.
|
|
60
58
|
if (typeof parsedKeys.transaction === 'function') {
|
|
61
59
|
return parsedKeys.transaction(work, label);
|
|
62
60
|
}
|
|
@@ -175,9 +173,6 @@ function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
|
175
173
|
}, `delete-${jids.length}-sessions`);
|
|
176
174
|
},
|
|
177
175
|
/**
|
|
178
|
-
* Bulk-migrates every known device session of `fromJid`'s user from a PN
|
|
179
|
-
* address to the corresponding LID address (`toJid`). Used when WhatsApp
|
|
180
|
-
* upgrades a contact from phone-number identity to LID identity.
|
|
181
176
|
*/
|
|
182
177
|
async migrateSession(fromJid, toJid) {
|
|
183
178
|
if (!fromJid || (!(0, WABinary_1.isLidUser)(toJid) && !(0, WABinary_1.isHostedLidUser)(toJid))) {
|
|
@@ -283,9 +278,6 @@ const jidToSignalSenderKeyName = (group, user) => {
|
|
|
283
278
|
};
|
|
284
279
|
function signalStorage({ creds, keys }, lidMapping) {
|
|
285
280
|
/**
|
|
286
|
-
* If we're storing/loading a session keyed by a PN address but that user
|
|
287
|
-
* has since been migrated to a LID, transparently redirect to the LID
|
|
288
|
-
* session instead (so we don't keep two divergent sessions for one user).
|
|
289
281
|
*/
|
|
290
282
|
const resolveLIDSignalAddress = async (id) => {
|
|
291
283
|
if (id.includes('.')) {
|
|
@@ -10,9 +10,6 @@ const {
|
|
|
10
10
|
WAJIDDomains,
|
|
11
11
|
} = require("../WABinary");
|
|
12
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
13
|
*/
|
|
17
14
|
class LIDMappingStore {
|
|
18
15
|
constructor(keys, logger, pnToLIDFunc) {
|
package/lib/Socket/chats.js
CHANGED
|
@@ -1036,9 +1036,65 @@ const makeChatsSocket = (config) => {
|
|
|
1036
1036
|
const clearMessage = (jid, key, timeStamp) => chatModify({ delete: true, lastMessages: [{ key, messageTimestamp: timeStamp }] }, jid);
|
|
1037
1037
|
const addOrEditQuickReply = (quickReply) => chatModify({ quickReply }, '');
|
|
1038
1038
|
const removeQuickReply = (timestamp) => chatModify({ quickReply: { timestamp, deleted: true } }, '');
|
|
1039
|
+
/** convert jid ke format phone number (pn), pake lid mapping store kalo jid-nya lid */
|
|
1040
|
+
const toPn = async (jid) => {
|
|
1041
|
+
if (!jid) {
|
|
1042
|
+
return jid;
|
|
1043
|
+
}
|
|
1044
|
+
if (!WABinary_1.isLidUser(jid)) {
|
|
1045
|
+
return jid;
|
|
1046
|
+
}
|
|
1047
|
+
try {
|
|
1048
|
+
const pn = await sock.signalRepository?.lidMapping?.getPNForLID(jid);
|
|
1049
|
+
return pn || jid;
|
|
1050
|
+
}
|
|
1051
|
+
catch (error) {
|
|
1052
|
+
logger.debug({ jid, error }, 'toPn failed, returning original jid');
|
|
1053
|
+
return jid;
|
|
1054
|
+
}
|
|
1055
|
+
};
|
|
1056
|
+
/** convert jid ke format lid, pake lid mapping store kalo jid-nya pn */
|
|
1057
|
+
const toLID = async (jid) => {
|
|
1058
|
+
if (!jid) {
|
|
1059
|
+
return jid;
|
|
1060
|
+
}
|
|
1061
|
+
if (WABinary_1.isLidUser(jid)) {
|
|
1062
|
+
return jid;
|
|
1063
|
+
}
|
|
1064
|
+
try {
|
|
1065
|
+
const lid = await sock.signalRepository?.lidMapping?.getLIDForPN(jid);
|
|
1066
|
+
return lid || jid;
|
|
1067
|
+
}
|
|
1068
|
+
catch (error) {
|
|
1069
|
+
logger.debug({ jid, error }, 'toLID failed, returning original jid');
|
|
1070
|
+
return jid;
|
|
1071
|
+
}
|
|
1072
|
+
};
|
|
1073
|
+
/**
|
|
1074
|
+
* speculative - resolve username WA jadi jid asli via usync query.
|
|
1075
|
+
* NOTE: server WA lo kemungkinan blm support field ini (fitur username blm rollout ID),
|
|
1076
|
+
* jadi wajar kalo return null terus sampe ada rollout beneran / server support.
|
|
1077
|
+
*/
|
|
1078
|
+
const fetchUsername = async (username) => {
|
|
1079
|
+
try {
|
|
1080
|
+
const usyncQuery = new WAUSync_1.USyncQuery()
|
|
1081
|
+
.withContactProtocol()
|
|
1082
|
+
.withUser(new WAUSync_1.USyncUser().withUsername(username));
|
|
1083
|
+
const result = await sock.executeUSyncQuery(usyncQuery);
|
|
1084
|
+
const found = result?.list?.find((a) => !!a.contact);
|
|
1085
|
+
return found ? found.id : null;
|
|
1086
|
+
}
|
|
1087
|
+
catch (error) {
|
|
1088
|
+
logger.debug({ username, error }, 'fetchUsername failed');
|
|
1089
|
+
return null;
|
|
1090
|
+
}
|
|
1091
|
+
};
|
|
1039
1092
|
return {
|
|
1040
1093
|
...sock,
|
|
1041
1094
|
getLidUser,
|
|
1095
|
+
toPn,
|
|
1096
|
+
toLID,
|
|
1097
|
+
fetchUsername,
|
|
1042
1098
|
fetchDisappearingDuration,
|
|
1043
1099
|
createCallLink,
|
|
1044
1100
|
getBotListV2,
|
|
@@ -158,7 +158,6 @@ const makeMessagesSocket = (config) => {
|
|
|
158
158
|
}
|
|
159
159
|
let result = await executeUSyncQuery(query)
|
|
160
160
|
if (!result) {
|
|
161
|
-
// stability fix: query device kadang gagal transient (timeout/network), coba sekali lagi sebelum nyerah
|
|
162
161
|
logger.debug({ toFetch }, 'usync device query empty, retrying once')
|
|
163
162
|
result = await executeUSyncQuery(query)
|
|
164
163
|
}
|
|
@@ -265,9 +264,6 @@ const makeMessagesSocket = (config) => {
|
|
|
265
264
|
return node;
|
|
266
265
|
}
|
|
267
266
|
catch (error) {
|
|
268
|
-
// fix: "SessionError: No sessions" (atau error encrypt lain) buat 1 device jangan
|
|
269
|
-
// sampe nge-reject Promise.all dan bikin proses crash total - skip device ini aja,
|
|
270
|
-
// device lain tetep kekirim normal.
|
|
271
267
|
logger.warn({ jid, trace: error?.stack, err: error?.toString?.() ?? error }, 'failed to encrypt message for device, skipping (no session?)');
|
|
272
268
|
return null;
|
|
273
269
|
}
|
|
@@ -348,8 +344,6 @@ const makeMessagesSocket = (config) => {
|
|
|
348
344
|
const additionalDevices = await getUSyncDevices(participantsList, !!useUserDevicesCache, false)
|
|
349
345
|
devices.push(...additionalDevices)
|
|
350
346
|
}
|
|
351
|
-
// fallback: kalo server ga ngasih addressingMode di group metadata (kadang kosong/null),
|
|
352
|
-
// tebak dari suffix jid participant sendiri drpd langsung default ke pn - lebih akurat pas grup lid-only
|
|
353
347
|
if (groupData && !groupData.addressingMode && groupData.participants?.length) {
|
|
354
348
|
const lidCount = groupData.participants.filter(p => WABinary_1.isLidUser(p.id)).length
|
|
355
349
|
if (lidCount > 0 && lidCount === groupData.participants.length) {
|
|
@@ -371,16 +365,12 @@ const makeMessagesSocket = (config) => {
|
|
|
371
365
|
const { user: selfLidUser } = meLid ? WABinary_1.jidDecode(meLid) : { user: null };
|
|
372
366
|
for (const { user, device } of devices) {
|
|
373
367
|
const targetJid = WABinary_1.jidEncode(user, (groupData === null || groupData === void 0 ? void 0 : groupData.addressingMode) === 'lid' ? 'lid' : 's.whatsapp.net', device);
|
|
374
|
-
// a device never needs its own sender-key re-sent to it - it already has it
|
|
375
368
|
const isSendingDeviceItself = targetJid === meId || (meLid && targetJid === meLid);
|
|
376
369
|
if (isSendingDeviceItself) {
|
|
377
370
|
logger.debug({ targetJid, meId, meLid }, 'skipping sender\'s own device for sender-key distribution');
|
|
378
371
|
continue;
|
|
379
372
|
}
|
|
380
373
|
const belongsToSelf = user === selfPnUser || user === selfLidUser;
|
|
381
|
-
// when this call targets one specific participant (a retry resend),
|
|
382
|
-
// our own other devices don't need it - they're already in sync via
|
|
383
|
-
// the normal multi-device session, only the actual peer does
|
|
384
374
|
const isSkippedAsOwnDeviceDuringTargetedResend = !!participant && !(0, WABinary_1.isJidGroup)(targetJid) && !isStatus && belongsToSelf;
|
|
385
375
|
if (isSkippedAsOwnDeviceDuringTargetedResend) {
|
|
386
376
|
continue;
|
|
@@ -542,9 +532,6 @@ const makeMessagesSocket = (config) => {
|
|
|
542
532
|
if (!didPushAdditional && additionalNodes && additionalNodes.length > 0) {
|
|
543
533
|
stanza.content.push(...additionalNodes);
|
|
544
534
|
}
|
|
545
|
-
// TC (Trusted Contact) token: attach our stored token for this peer (if any
|
|
546
|
-
// and not expired) onto the outgoing stanza - mirrors WA Web's reliability
|
|
547
|
-
// mechanism for 1:1 chats. Ported from official Baileys v7.
|
|
548
535
|
const is1on1Send = isPrivate && !isStatus;
|
|
549
536
|
const getLIDForPN = signalRepository.lidMapping.getLIDForPN.bind(signalRepository.lidMapping);
|
|
550
537
|
let tcTokenJid = jid;
|
|
@@ -568,9 +555,6 @@ const makeMessagesSocket = (config) => {
|
|
|
568
555
|
}
|
|
569
556
|
logger.debug({ msgId }, `sending message to ${participants.length} devices`);
|
|
570
557
|
await sendNode(stanza);
|
|
571
|
-
// Fire-and-forget: after sending, issue our own token to this contact so
|
|
572
|
-
// future messages from them get the reliability benefit too. Skipped for
|
|
573
|
-
// protocol messages, PSA, and bot/MetaAI contacts (matches WA Web behavior).
|
|
574
558
|
if (is1on1Send) {
|
|
575
559
|
try {
|
|
576
560
|
const isProtocolMsg = !!Types_1.WAProto.Message.fromObject(message)?.protocolMessage;
|
|
@@ -695,6 +679,12 @@ const makeMessagesSocket = (config) => {
|
|
|
695
679
|
else if (message.interactiveMessage?.nativeFlowMessage?.buttons?.[0]?.name === 'payment_method') {
|
|
696
680
|
return 'payment_method'
|
|
697
681
|
}
|
|
682
|
+
else if (message.interactiveMessage?.nativeFlowMessage?.buttons?.[0]?.name === 'payment_key_info') {
|
|
683
|
+
return 'payment_key_info'
|
|
684
|
+
}
|
|
685
|
+
else if (message.listMessage?.listType === Types_1.WAProto.Message.ListMessage.ListType.PRODUCT_LIST) {
|
|
686
|
+
return 'catalog_message'
|
|
687
|
+
}
|
|
698
688
|
else if (message.interactiveMessage && message.interactiveMessage?.nativeFlowMessage) {
|
|
699
689
|
return 'interactive'
|
|
700
690
|
}
|
|
@@ -728,7 +718,7 @@ const makeMessagesSocket = (config) => {
|
|
|
728
718
|
});
|
|
729
719
|
return result;
|
|
730
720
|
}
|
|
731
|
-
/**
|
|
721
|
+
/** */
|
|
732
722
|
const issuePrivacyTokens = async (jids, timestamp) => {
|
|
733
723
|
const t = (timestamp !== undefined ? timestamp : (0, Utils_1.unixTimestampSeconds)()).toString();
|
|
734
724
|
const result = await query({
|
package/lib/Utils/generics.js
CHANGED
|
@@ -255,6 +255,27 @@ const generateMessageIDV2 = (userId) => {
|
|
|
255
255
|
return '3EB0' + hash.toString('hex').toUpperCase().substring(0, 18);
|
|
256
256
|
};
|
|
257
257
|
exports.generateMessageIDV2 = generateMessageIDV2;
|
|
258
|
+
// alias biar jelas ini yg dipake device Android (format 3EB0 + hash)
|
|
259
|
+
const generateMessageIDAndroid = (userId) => generateMessageIDV2(userId);
|
|
260
|
+
exports.generateMessageIDAndroid = generateMessageIDAndroid;
|
|
261
|
+
// speculative - format id gaya iOS/Safari: hex uppercase polos tanpa prefix "3EB0"
|
|
262
|
+
// (dipake modded-baileys lain buat nyamain "keliatan kirim dari iPhone/Safari")
|
|
263
|
+
const generateMessageIDIos = (userId) => {
|
|
264
|
+
const data = Buffer.alloc(8 + 20 + 16);
|
|
265
|
+
data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)));
|
|
266
|
+
if (userId) {
|
|
267
|
+
const id = (0, WABinary_1.jidDecode)(userId);
|
|
268
|
+
if (id === null || id === void 0 ? void 0 : id.user) {
|
|
269
|
+
data.write(id.user, 8);
|
|
270
|
+
data.write('@c.us', 8 + id.user.length);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const random = (0, crypto_1.randomBytes)(16);
|
|
274
|
+
random.copy(data, 28);
|
|
275
|
+
const hash = (0, crypto_1.createHash)('sha256').update(data).digest();
|
|
276
|
+
return hash.toString('hex').toUpperCase().substring(0, 20);
|
|
277
|
+
};
|
|
278
|
+
exports.generateMessageIDIos = generateMessageIDIos;
|
|
258
279
|
// generate a random ID to attach to a message
|
|
259
280
|
const generateMessageID = () => 'Z4PH-' + (0, crypto_1.randomBytes)(6).toString('hex').toUpperCase();
|
|
260
281
|
exports.generateMessageID = generateMessageID;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* token helpers.
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
const token_second = 30 * 24 * 60 * 60;
|
|
8
|
+
const interval_second = 7 * 24 * 60 * 60;
|
|
9
|
+
function nowSeconds() {
|
|
10
|
+
return Math.floor(Date.now() / 1000);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isTcTokenExpired(timestamp) {
|
|
14
|
+
if (!timestamp) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
const ts = Number(timestamp);
|
|
18
|
+
if (!Number.isFinite(ts) || ts <= 0) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
return (nowSeconds() - ts) > token_second;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function shouldSendNewTcToken(senderTimestamp) {
|
|
25
|
+
if (!senderTimestamp) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
const ts = Number(senderTimestamp);
|
|
29
|
+
if (!Number.isFinite(ts) || ts <= 0) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
return (nowSeconds() - ts) > interval_second;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function resolveTcTokenJid(jid, getLIDForPN) {
|
|
36
|
+
try {
|
|
37
|
+
if (typeof getLIDForPN === 'function') {
|
|
38
|
+
const lid = await getLIDForPN(jid);
|
|
39
|
+
if (lid) {
|
|
40
|
+
return lid;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (_a) {
|
|
45
|
+
}
|
|
46
|
+
return jid;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function resolveIssuanceJid(jid, preferLid, getLIDForPN, getPNForLID) {
|
|
50
|
+
try {
|
|
51
|
+
if (preferLid && typeof getLIDForPN === 'function') {
|
|
52
|
+
const lid = await getLIDForPN(jid);
|
|
53
|
+
if (lid) {
|
|
54
|
+
return lid;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (!preferLid && typeof getPNForLID === 'function') {
|
|
58
|
+
const pn = await getPNForLID(jid);
|
|
59
|
+
if (pn) {
|
|
60
|
+
return pn;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch (_a) {
|
|
65
|
+
}
|
|
66
|
+
return jid;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function storeTcTokensFromIqResult({ result, fallbackJid, keys, getLIDForPN }) {
|
|
70
|
+
var _a, _b, _c;
|
|
71
|
+
try {
|
|
72
|
+
const token = (_c = (_b = (_a = result === null || result === void 0 ? void 0 : result.content) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.content) !== null && _c !== void 0 ? _c : null;
|
|
73
|
+
if (!token || !fallbackJid) {
|
|
74
|
+
return {};
|
|
75
|
+
}
|
|
76
|
+
const jidKey = await resolveTcTokenJid(fallbackJid, getLIDForPN);
|
|
77
|
+
const entry = {
|
|
78
|
+
[jidKey]: {
|
|
79
|
+
token: Buffer.isBuffer(token) ? token : Buffer.from(token),
|
|
80
|
+
timestamp: nowSeconds(),
|
|
81
|
+
senderTimestamp: nowSeconds()
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
if (keys === null || keys === void 0 ? void 0 : keys.set) {
|
|
85
|
+
await keys.set({ tctoken: entry });
|
|
86
|
+
}
|
|
87
|
+
return entry;
|
|
88
|
+
}
|
|
89
|
+
catch (_d) {
|
|
90
|
+
return {};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function buildMergedTcTokenIndexWrite(keys, jids) {
|
|
95
|
+
var _a;
|
|
96
|
+
const merged = {};
|
|
97
|
+
try {
|
|
98
|
+
const existing = await (keys === null || keys === void 0 ? void 0 : keys.get('tctoken', jids));
|
|
99
|
+
for (const jid of jids) {
|
|
100
|
+
if ((_a = existing === null || existing === void 0 ? void 0 : existing[jid]) === null || _a === void 0 ? void 0 : _a.token) {
|
|
101
|
+
merged[jid] = existing[jid];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch (_b) {
|
|
106
|
+
}
|
|
107
|
+
return merged;
|
|
108
|
+
}
|
|
109
|
+
exports.isTcTokenExpired = isTcTokenExpired;
|
|
110
|
+
exports.shouldSendNewTcToken = shouldSendNewTcToken;
|
|
111
|
+
exports.resolveTcTokenJid = resolveTcTokenJid;
|
|
112
|
+
exports.resolveIssuanceJid = resolveIssuanceJid;
|
|
113
|
+
exports.storeTcTokensFromIqResult = storeTcTokensFromIqResult;
|
|
114
|
+
exports.buildMergedTcTokenIndexWrite = buildMergedTcTokenIndexWrite;
|
|
@@ -155,7 +155,7 @@ const getAdditionalNode = (name) => {
|
|
|
155
155
|
payment_status: 'payment_status',
|
|
156
156
|
payment_method: 'payment_method'
|
|
157
157
|
}
|
|
158
|
-
|
|
158
|
+
|
|
159
159
|
const flow_name = {
|
|
160
160
|
cta_catalog: 'cta_catalog',
|
|
161
161
|
mpm: 'mpm',
|
|
@@ -163,9 +163,25 @@ const getAdditionalNode = (name) => {
|
|
|
163
163
|
view_catalog: 'automated_greeting_message_view_catalog',
|
|
164
164
|
wa_pay_detail: 'wa_payment_transaction_details',
|
|
165
165
|
send_location: 'send_location',
|
|
166
|
+
// speculative - blm ada konfirmasi resmi nama native_flow-nya, tebakan berdasarkan pola yg udah ada
|
|
167
|
+
payment_key_info: 'wa_payment_key_info',
|
|
166
168
|
}
|
|
167
|
-
|
|
168
|
-
if(
|
|
169
|
+
|
|
170
|
+
if (name === 'catalog_message') {
|
|
171
|
+
// list message isi produk katalog -> beda struktur dari native_flow, node <list> bukan <interactive>
|
|
172
|
+
return [{
|
|
173
|
+
tag: 'biz',
|
|
174
|
+
attrs: {},
|
|
175
|
+
content: [{
|
|
176
|
+
tag: 'list',
|
|
177
|
+
attrs: {
|
|
178
|
+
type: 'product_list',
|
|
179
|
+
v: '2'
|
|
180
|
+
},
|
|
181
|
+
content: []
|
|
182
|
+
}]
|
|
183
|
+
}]
|
|
184
|
+
} else if(order_response_name[name]) {
|
|
169
185
|
return [{
|
|
170
186
|
tag: 'biz',
|
|
171
187
|
attrs: {
|