@nexustechpro/baileys 2.0.2 → 2.0.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.
Files changed (108) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +924 -1299
  3. package/WAProto/index.js +22 -18
  4. package/lib/Defaults/baileys-version.json +6 -2
  5. package/lib/Defaults/index.js +173 -172
  6. package/lib/Signal/libsignal.js +395 -292
  7. package/lib/Signal/lid-mapping.js +264 -171
  8. package/lib/Socket/Client/index.js +2 -2
  9. package/lib/Socket/Client/types.js +10 -10
  10. package/lib/Socket/Client/websocket.js +45 -310
  11. package/lib/Socket/business.js +375 -375
  12. package/lib/Socket/chats.js +916 -963
  13. package/lib/Socket/communities.js +430 -430
  14. package/lib/Socket/groups.js +342 -342
  15. package/lib/Socket/index.js +21 -22
  16. package/lib/Socket/messages-recv.js +963 -743
  17. package/lib/Socket/messages-send.js +273 -321
  18. package/lib/Socket/mex.js +50 -50
  19. package/lib/Socket/newsletter.js +148 -148
  20. package/lib/Socket/nexus-handler.js +296 -247
  21. package/lib/Socket/registration.js +50 -33
  22. package/lib/Socket/socket.js +872 -1201
  23. package/lib/Store/index.js +5 -5
  24. package/lib/Store/make-cache-manager-store.js +81 -81
  25. package/lib/Store/make-in-memory-store.js +416 -416
  26. package/lib/Store/make-ordered-dictionary.js +81 -81
  27. package/lib/Store/object-repository.js +30 -30
  28. package/lib/Types/Auth.js +1 -1
  29. package/lib/Types/Bussines.js +1 -1
  30. package/lib/Types/Call.js +1 -1
  31. package/lib/Types/Chat.js +7 -7
  32. package/lib/Types/Contact.js +1 -1
  33. package/lib/Types/Events.js +1 -1
  34. package/lib/Types/GroupMetadata.js +1 -1
  35. package/lib/Types/Label.js +24 -24
  36. package/lib/Types/LabelAssociation.js +6 -6
  37. package/lib/Types/Message.js +10 -10
  38. package/lib/Types/Newsletter.js +37 -29
  39. package/lib/Types/Product.js +1 -1
  40. package/lib/Types/Signal.js +1 -1
  41. package/lib/Types/Socket.js +2 -2
  42. package/lib/Types/State.js +55 -12
  43. package/lib/Types/USync.js +1 -1
  44. package/lib/Types/index.js +25 -25
  45. package/lib/Utils/auth-utils.js +264 -256
  46. package/lib/Utils/baileys-event-stream.js +55 -55
  47. package/lib/Utils/browser-utils.js +27 -27
  48. package/lib/Utils/business.js +228 -230
  49. package/lib/Utils/chat-utils.js +726 -764
  50. package/lib/Utils/companion-reg-client-utils.js +34 -0
  51. package/lib/Utils/crypto.js +109 -135
  52. package/lib/Utils/decode-wa-message.js +342 -314
  53. package/lib/Utils/event-buffer.js +547 -547
  54. package/lib/Utils/generics.js +295 -297
  55. package/lib/Utils/history.js +91 -83
  56. package/lib/Utils/index.js +25 -20
  57. package/lib/Utils/key-store.js +17 -0
  58. package/lib/Utils/link-preview.js +107 -98
  59. package/lib/Utils/logger.js +2 -2
  60. package/lib/Utils/lt-hash.js +47 -47
  61. package/lib/Utils/make-mutex.js +39 -39
  62. package/lib/Utils/message-retry-manager.js +148 -148
  63. package/lib/Utils/messages-media.js +579 -535
  64. package/lib/Utils/messages.js +821 -706
  65. package/lib/Utils/noise-handler.js +255 -255
  66. package/lib/Utils/pre-key-manager.js +105 -105
  67. package/lib/Utils/process-message.js +430 -412
  68. package/lib/Utils/reporting-utils.js +155 -0
  69. package/lib/Utils/signal.js +191 -159
  70. package/lib/Utils/sync-action-utils.js +33 -0
  71. package/lib/Utils/tc-token-utils.js +162 -0
  72. package/lib/Utils/use-multi-file-auth-state.js +120 -120
  73. package/lib/Utils/validate-connection.js +194 -194
  74. package/lib/WABinary/constants.js +1306 -1300
  75. package/lib/WABinary/decode.js +237 -237
  76. package/lib/WABinary/encode.js +232 -232
  77. package/lib/WABinary/generic-utils.js +252 -211
  78. package/lib/WABinary/index.js +6 -5
  79. package/lib/WABinary/jid-utils.js +279 -95
  80. package/lib/WABinary/types.js +1 -1
  81. package/lib/WAM/BinaryInfo.js +9 -9
  82. package/lib/WAM/constants.js +22852 -22852
  83. package/lib/WAM/encode.js +149 -149
  84. package/lib/WAM/index.js +3 -3
  85. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -28
  86. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -53
  87. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -26
  88. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -37
  89. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
  90. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -28
  91. package/lib/WAUSync/Protocols/index.js +4 -4
  92. package/lib/WAUSync/USyncQuery.js +93 -93
  93. package/lib/WAUSync/USyncUser.js +22 -22
  94. package/lib/WAUSync/index.js +3 -3
  95. package/lib/index.js +65 -66
  96. package/package.json +172 -143
  97. package/lib/Signal/Group/ciphertext-message.js +0 -12
  98. package/lib/Signal/Group/group-session-builder.js +0 -30
  99. package/lib/Signal/Group/group_cipher.js +0 -100
  100. package/lib/Signal/Group/index.js +0 -12
  101. package/lib/Signal/Group/keyhelper.js +0 -18
  102. package/lib/Signal/Group/sender-chain-key.js +0 -26
  103. package/lib/Signal/Group/sender-key-distribution-message.js +0 -63
  104. package/lib/Signal/Group/sender-key-message.js +0 -66
  105. package/lib/Signal/Group/sender-key-name.js +0 -48
  106. package/lib/Signal/Group/sender-key-record.js +0 -41
  107. package/lib/Signal/Group/sender-key-state.js +0 -84
  108. package/lib/Signal/Group/sender-message-key.js +0 -26
@@ -1,765 +1,727 @@
1
- import { Boom } from '@hapi/boom';
2
- import { proto } from '../../WAProto/index.js';
3
- import { LabelAssociationType } from '../Types/LabelAssociation.js';
4
- import { getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidNormalizedUser } from '../WABinary/index.js';
5
- import { aesDecrypt, aesEncrypt, hkdf, hmacSign } from './crypto.js';
6
- import { toNumber } from './generics.js';
7
- import { LT_HASH_ANTI_TAMPERING } from './lt-hash.js';
8
- import { downloadContentFromMessage } from './messages-media.js';
9
- const mutationKeys = async (keydata) => {
10
- const expanded = hkdf(keydata, 160, { info: 'WhatsApp Mutation Keys' });
11
- return {
12
- indexKey: expanded.slice(0, 32),
13
- valueEncryptionKey: expanded.slice(32, 64),
14
- valueMacKey: expanded.slice(64, 96),
15
- snapshotMacKey: expanded.slice(96, 128),
16
- patchMacKey: expanded.slice(128, 160)
17
- };
18
- };
19
- const generateMac = (operation, data, keyId, key) => {
20
- const getKeyData = () => {
21
- let r;
22
- switch (operation) {
23
- case proto.SyncdMutation.SyncdOperation.SET:
24
- r = 0x01;
25
- break;
26
- case proto.SyncdMutation.SyncdOperation.REMOVE:
27
- r = 0x02;
28
- break;
29
- }
30
- const buff = Buffer.from([r]);
31
- return Buffer.concat([buff, Buffer.from(keyId, 'base64')]);
32
- };
33
- const keyData = getKeyData();
34
- const last = Buffer.alloc(8); // 8 bytes
35
- last.set([keyData.length], last.length - 1);
36
- const total = Buffer.concat([keyData, data, last]);
37
- const hmac = hmacSign(total, key, 'sha512');
38
- return hmac.slice(0, 32);
39
- };
40
- const to64BitNetworkOrder = (e) => {
41
- const buff = Buffer.alloc(8);
42
- buff.writeUint32BE(e, 4);
43
- return buff;
44
- };
45
- const makeLtHashGenerator = ({ indexValueMap, hash }) => {
46
- indexValueMap = { ...indexValueMap };
47
- const addBuffs = [];
48
- const subBuffs = [];
49
- return {
50
- mix: ({ indexMac, valueMac, operation }) => {
51
- const indexMacBase64 = Buffer.from(indexMac).toString('base64');
52
- const prevOp = indexValueMap[indexMacBase64];
53
- if (operation === proto.SyncdMutation.SyncdOperation.REMOVE) {
54
- if (!prevOp) {
55
- // Skip remove if there's no previous operation - likely sync corruption
56
- // The state will be resync'd from scratch
57
- return;
58
- }
59
- // remove from index value mac, since this mutation is erased
60
- delete indexValueMap[indexMacBase64];
61
- }
62
- else {
63
- addBuffs.push(new Uint8Array(valueMac).buffer);
64
- // add this index into the history map
65
- indexValueMap[indexMacBase64] = { valueMac };
66
- }
67
- if (prevOp) {
68
- subBuffs.push(new Uint8Array(prevOp.valueMac).buffer);
69
- }
70
- },
71
- finish: async () => {
72
- const hashArrayBuffer = new Uint8Array(hash).buffer;
73
- const result = await LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs);
74
- const buffer = Buffer.from(result);
75
- return {
76
- hash: buffer,
77
- indexValueMap
78
- };
79
- }
80
- };
81
- };
82
- const generateSnapshotMac = (lthash, version, name, key) => {
83
- const total = Buffer.concat([lthash, to64BitNetworkOrder(version), Buffer.from(name, 'utf-8')]);
84
- return hmacSign(total, key, 'sha256');
85
- };
86
- const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
87
- const total = Buffer.concat([snapshotMac, ...valueMacs, to64BitNetworkOrder(version), Buffer.from(type, 'utf-8')]);
88
- return hmacSign(total, key);
89
- };
90
- export const newLTHashState = () => ({ version: 0, hash: Buffer.alloc(128), indexValueMap: {} });
91
- export const encodeSyncdPatch = async ({ type, index, syncAction, apiVersion, operation }, myAppStateKeyId, state, getAppStateSyncKey) => {
92
- const key = !!myAppStateKeyId ? await getAppStateSyncKey(myAppStateKeyId) : undefined;
93
- if (!key) {
94
- throw new Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 });
95
- }
96
- const encKeyId = Buffer.from(myAppStateKeyId, 'base64');
97
- state = { ...state, indexValueMap: { ...state.indexValueMap } };
98
- const indexBuffer = Buffer.from(JSON.stringify(index));
99
- const dataProto = proto.SyncActionData.fromObject({
100
- index: indexBuffer,
101
- value: syncAction,
102
- padding: new Uint8Array(0),
103
- version: apiVersion
104
- });
105
- const encoded = proto.SyncActionData.encode(dataProto).finish();
106
- const keyValue = await mutationKeys(key.keyData);
107
- const encValue = aesEncrypt(encoded, keyValue.valueEncryptionKey);
108
- const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey);
109
- const indexMac = hmacSign(indexBuffer, keyValue.indexKey);
110
- // update LT hash
111
- const generator = makeLtHashGenerator(state);
112
- generator.mix({ indexMac, valueMac, operation });
113
- Object.assign(state, await generator.finish());
114
- state.version += 1;
115
- const snapshotMac = generateSnapshotMac(state.hash, state.version, type, keyValue.snapshotMacKey);
116
- const patch = {
117
- patchMac: generatePatchMac(snapshotMac, [valueMac], state.version, type, keyValue.patchMacKey),
118
- snapshotMac: snapshotMac,
119
- keyId: { id: encKeyId },
120
- mutations: [
121
- {
122
- operation: operation,
123
- record: {
124
- index: {
125
- blob: indexMac
126
- },
127
- value: {
128
- blob: Buffer.concat([encValue, valueMac])
129
- },
130
- keyId: { id: encKeyId }
131
- }
132
- }
133
- ]
134
- };
135
- const base64Index = indexMac.toString('base64');
136
- state.indexValueMap[base64Index] = { valueMac };
137
- return { patch, state };
138
- };
139
- export const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
140
- const ltGenerator = makeLtHashGenerator(initialState);
141
- // indexKey used to HMAC sign record.index.blob
142
- // valueEncryptionKey used to AES-256-CBC encrypt record.value.blob[0:-32]
143
- // the remaining record.value.blob[0:-32] is the mac, it the HMAC sign of key.keyId + decoded proto data + length of bytes in keyId
144
- for (const msgMutation of msgMutations) {
145
- // if it's a syncdmutation, get the operation property
146
- // otherwise, if it's only a record -- it'll be a SET mutation
147
- const operation = 'operation' in msgMutation ? msgMutation.operation : proto.SyncdMutation.SyncdOperation.SET;
148
- const record = 'record' in msgMutation && !!msgMutation.record ? msgMutation.record : msgMutation;
149
- const key = await getKey(record.keyId.id);
150
- const content = Buffer.from(record.value.blob);
151
- const encContent = content.slice(0, -32);
152
- const ogValueMac = content.slice(-32);
153
- if (validateMacs) {
154
- const contentHmac = generateMac(operation, encContent, record.keyId.id, key.valueMacKey);
155
- if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
156
- throw new Boom('HMAC content verification failed');
157
- }
158
- }
159
- const result = aesDecrypt(encContent, key.valueEncryptionKey);
160
- const syncAction = proto.SyncActionData.decode(result);
161
- if (validateMacs) {
162
- const hmac = hmacSign(syncAction.index, key.indexKey);
163
- if (Buffer.compare(hmac, record.index.blob) !== 0) {
164
- throw new Boom('HMAC index verification failed');
165
- }
166
- }
167
- const indexStr = Buffer.from(syncAction.index).toString();
168
- onMutation({ syncAction, index: JSON.parse(indexStr) });
169
- ltGenerator.mix({
170
- indexMac: record.index.blob,
171
- valueMac: ogValueMac,
172
- operation: operation
173
- });
174
- }
175
- return await ltGenerator.finish();
176
- async function getKey(keyId) {
177
- const base64Key = Buffer.from(keyId).toString('base64');
178
- const keyEnc = await getAppStateSyncKey(base64Key);
179
- if (!keyEnc) {
180
- throw new Boom(`failed to find key "${base64Key}" to decode mutation`, {
181
- statusCode: 404,
182
- data: { msgMutations }
183
- });
184
- }
185
- return mutationKeys(keyEnc.keyData);
186
- }
187
- };
188
- export const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
189
- if (validateMacs) {
190
- const base64Key = Buffer.from(msg.keyId.id).toString('base64');
191
- const mainKeyObj = await getAppStateSyncKey(base64Key);
192
- if (!mainKeyObj) {
193
- throw new Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } });
194
- }
195
- const mainKey = await mutationKeys(mainKeyObj.keyData);
196
- const mutationmacs = msg.mutations.map(mutation => mutation.record.value.blob.slice(-32));
197
- const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs, toNumber(msg.version.version), name, mainKey.patchMacKey);
198
- if (Buffer.compare(patchMac, msg.patchMac) !== 0) {
199
- throw new Boom('Invalid patch mac');
200
- }
201
- }
202
- const result = await decodeSyncdMutations(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs);
203
- return result;
204
- };
205
- export const extractSyncdPatches = async (result, options) => {
206
- const syncNode = getBinaryNodeChild(result, 'sync');
207
- const collectionNodes = getBinaryNodeChildren(syncNode, 'collection');
208
- const final = {};
209
- await Promise.all(collectionNodes.map(async (collectionNode) => {
210
- const patchesNode = getBinaryNodeChild(collectionNode, 'patches');
211
- const patches = getBinaryNodeChildren(patchesNode || collectionNode, 'patch');
212
- const snapshotNode = getBinaryNodeChild(collectionNode, 'snapshot');
213
- const syncds = [];
214
- const name = collectionNode.attrs.name;
215
- const hasMorePatches = collectionNode.attrs.has_more_patches === 'true';
216
- let snapshot = undefined;
217
- if (snapshotNode && !!snapshotNode.content) {
218
- if (!Buffer.isBuffer(snapshotNode)) {
219
- snapshotNode.content = Buffer.from(Object.values(snapshotNode.content));
220
- }
221
- const blobRef = proto.ExternalBlobReference.decode(snapshotNode.content);
222
- const data = await downloadExternalBlob(blobRef, options);
223
- snapshot = proto.SyncdSnapshot.decode(data);
224
- }
225
- for (let { content } of patches) {
226
- if (content) {
227
- if (!Buffer.isBuffer(content)) {
228
- content = Buffer.from(Object.values(content));
229
- }
230
- const syncd = proto.SyncdPatch.decode(content);
231
- if (!syncd.version) {
232
- syncd.version = { version: +collectionNode.attrs.version + 1 };
233
- }
234
- syncds.push(syncd);
235
- }
236
- }
237
- final[name] = { patches: syncds, hasMorePatches, snapshot };
238
- }));
239
- return final;
240
- };
241
- export const downloadExternalBlob = async (blob, options) => {
242
- const stream = await downloadContentFromMessage(blob, 'md-app-state', { options });
243
- const bufferArray = [];
244
- for await (const chunk of stream) {
245
- bufferArray.push(chunk);
246
- }
247
- return Buffer.concat(bufferArray);
248
- };
249
- export const downloadExternalPatch = async (blob, options) => {
250
- const buffer = await downloadExternalBlob(blob, options);
251
- const syncData = proto.SyncdMutations.decode(buffer);
252
- return syncData;
253
- };
254
- export const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, minimumVersionNumber, validateMacs = true) => {
255
- const newState = newLTHashState();
256
- newState.version = toNumber(snapshot.version.version);
257
- const mutationMap = {};
258
- const areMutationsRequired = typeof minimumVersionNumber === 'undefined' || newState.version > minimumVersionNumber;
259
- const { hash, indexValueMap } = await decodeSyncdMutations(snapshot.records, newState, getAppStateSyncKey, areMutationsRequired
260
- ? mutation => {
261
- const index = mutation.syncAction.index?.toString();
262
- mutationMap[index] = mutation;
263
- }
264
- : () => { }, validateMacs);
265
- newState.hash = hash;
266
- newState.indexValueMap = indexValueMap;
267
- if (validateMacs) {
268
- const base64Key = Buffer.from(snapshot.keyId.id).toString('base64');
269
- const keyEnc = await getAppStateSyncKey(base64Key);
270
- if (!keyEnc) {
271
- throw new Boom(`failed to find key "${base64Key}" to decode mutation`);
272
- }
273
- const result = await mutationKeys(keyEnc.keyData);
274
- const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
275
- if (Buffer.compare(snapshot.mac, computedSnapshotMac) !== 0) {
276
- throw new Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`);
277
- }
278
- }
279
- return {
280
- state: newState,
281
- mutationMap
282
- };
283
- };
284
- export const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options, minimumVersionNumber, logger, validateMacs = true) => {
285
- const newState = {
286
- ...initial,
287
- indexValueMap: { ...initial.indexValueMap }
288
- };
289
- const mutationMap = {};
290
- for (const syncd of syncds) {
291
- const { version, keyId, snapshotMac } = syncd;
292
- if (syncd.externalMutations) {
293
- logger?.trace({ name, version }, 'downloading external patch');
294
- const ref = await downloadExternalPatch(syncd.externalMutations, options);
295
- logger?.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch');
296
- syncd.mutations?.push(...ref.mutations);
297
- }
298
- const patchVersion = toNumber(version.version);
299
- newState.version = patchVersion;
300
- const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber;
301
- const decodeResult = await decodeSyncdPatch(syncd, name, newState, getAppStateSyncKey, shouldMutate
302
- ? mutation => {
303
- const index = mutation.syncAction.index?.toString();
304
- mutationMap[index] = mutation;
305
- }
306
- : () => { }, true);
307
- newState.hash = decodeResult.hash;
308
- newState.indexValueMap = decodeResult.indexValueMap;
309
- if (validateMacs) {
310
- const base64Key = Buffer.from(keyId.id).toString('base64');
311
- const keyEnc = await getAppStateSyncKey(base64Key);
312
- if (!keyEnc) {
313
- throw new Boom(`failed to find key "${base64Key}" to decode mutation`);
314
- }
315
- const result = await mutationKeys(keyEnc.keyData);
316
- const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
317
- if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
318
- throw new Boom(`failed to verify LTHash at ${newState.version} of ${name}`);
319
- }
320
- }
321
- // clear memory used up by the mutations
322
- syncd.mutations = [];
323
- }
324
- return { state: newState, mutationMap };
325
- };
326
- export const chatModificationToAppPatch = (mod, jid) => {
327
- const OP = proto.SyncdMutation.SyncdOperation;
328
- const getMessageRange = (lastMessages) => {
329
- let messageRange;
330
- if (Array.isArray(lastMessages)) {
331
- const lastMsg = lastMessages[lastMessages.length - 1];
332
- messageRange = {
333
- lastMessageTimestamp: lastMsg?.messageTimestamp,
334
- messages: lastMessages?.length
335
- ? lastMessages.map(m => {
336
- if (!m.key?.id || !m.key?.remoteJid) {
337
- throw new Boom('Incomplete key', { statusCode: 400, data: m });
338
- }
339
- if (isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
340
- throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m });
341
- }
342
- if (!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
343
- throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m });
344
- }
345
- if (m.key.participant) {
346
- m.key.participant = jidNormalizedUser(m.key.participant);
347
- }
348
- return m;
349
- })
350
- : undefined
351
- };
352
- }
353
- else {
354
- messageRange = lastMessages;
355
- }
356
- return messageRange;
357
- };
358
- let patch;
359
- if ('mute' in mod) {
360
- patch = {
361
- syncAction: {
362
- muteAction: {
363
- muted: !!mod.mute,
364
- muteEndTimestamp: mod.mute || undefined
365
- }
366
- },
367
- index: ['mute', jid],
368
- type: 'regular_high',
369
- apiVersion: 2,
370
- operation: OP.SET
371
- };
372
- }
373
- else if ('archive' in mod) {
374
- patch = {
375
- syncAction: {
376
- archiveChatAction: {
377
- archived: !!mod.archive,
378
- messageRange: getMessageRange(mod.lastMessages)
379
- }
380
- },
381
- index: ['archive', jid],
382
- type: 'regular_low',
383
- apiVersion: 3,
384
- operation: OP.SET
385
- };
386
- }
387
- else if ('markRead' in mod) {
388
- patch = {
389
- syncAction: {
390
- markChatAsReadAction: {
391
- read: mod.markRead,
392
- messageRange: getMessageRange(mod.lastMessages)
393
- }
394
- },
395
- index: ['markChatAsRead', jid],
396
- type: 'regular_low',
397
- apiVersion: 3,
398
- operation: OP.SET
399
- };
400
- }
401
- else if ('deleteForMe' in mod) {
402
- const { timestamp, key, deleteMedia } = mod.deleteForMe;
403
- patch = {
404
- syncAction: {
405
- deleteMessageForMeAction: {
406
- deleteMedia,
407
- messageTimestamp: timestamp
408
- }
409
- },
410
- index: ['deleteMessageForMe', jid, key.id, key.fromMe ? '1' : '0', '0'],
411
- type: 'regular_high',
412
- apiVersion: 3,
413
- operation: OP.SET
414
- };
415
- }
416
- else if ('clear' in mod) {
417
- patch = {
418
- syncAction: {
419
- clearChatAction: {
420
- messageRange: getMessageRange(mod.lastMessages)
421
- }
422
- },
423
- index: ['clearChat', jid, '1' /*the option here is 0 when keep starred messages is enabled*/, '0'],
424
- type: 'regular_high',
425
- apiVersion: 6,
426
- operation: OP.SET
427
- };
428
- }
429
- else if ('pin' in mod) {
430
- patch = {
431
- syncAction: {
432
- pinAction: {
433
- pinned: !!mod.pin
434
- }
435
- },
436
- index: ['pin_v1', jid],
437
- type: 'regular_low',
438
- apiVersion: 5,
439
- operation: OP.SET
440
- };
441
- }
442
- else if ('contact' in mod) {
443
- patch = {
444
- syncAction: {
445
- contactAction: mod.contact || {}
446
- },
447
- index: ['contact', jid],
448
- type: 'critical_unblock_low',
449
- apiVersion: 2,
450
- operation: mod.contact ? OP.SET : OP.REMOVE
451
- };
452
- }
453
- else if ('disableLinkPreviews' in mod) {
454
- patch = {
455
- syncAction: {
456
- privacySettingDisableLinkPreviewsAction: mod.disableLinkPreviews || {}
457
- },
458
- index: ['setting_disableLinkPreviews'],
459
- type: 'regular',
460
- apiVersion: 8,
461
- operation: OP.SET
462
- };
463
- }
464
- else if ('star' in mod) {
465
- const key = mod.star.messages[0];
466
- patch = {
467
- syncAction: {
468
- starAction: {
469
- starred: !!mod.star.star
470
- }
471
- },
472
- index: ['star', jid, key.id, key.fromMe ? '1' : '0', '0'],
473
- type: 'regular_low',
474
- apiVersion: 2,
475
- operation: OP.SET
476
- };
477
- }
478
- else if ('delete' in mod) {
479
- patch = {
480
- syncAction: {
481
- deleteChatAction: {
482
- messageRange: getMessageRange(mod.lastMessages)
483
- }
484
- },
485
- index: ['deleteChat', jid, '1'],
486
- type: 'regular_high',
487
- apiVersion: 6,
488
- operation: OP.SET
489
- };
490
- }
491
- else if ('pushNameSetting' in mod) {
492
- patch = {
493
- syncAction: {
494
- pushNameSetting: {
495
- name: mod.pushNameSetting
496
- }
497
- },
498
- index: ['setting_pushName'],
499
- type: 'critical_block',
500
- apiVersion: 1,
501
- operation: OP.SET
502
- };
503
- }
504
- else if ('quickReply' in mod) {
505
- patch = {
506
- syncAction: {
507
- quickReplyAction: {
508
- count: 0,
509
- deleted: mod.quickReply.deleted || false,
510
- keywords: [],
511
- message: mod.quickReply.message || '',
512
- shortcut: mod.quickReply.shortcut || ''
513
- }
514
- },
515
- index: ['quick_reply', mod.quickReply.timestamp || String(Math.floor(Date.now() / 1000))],
516
- type: 'regular',
517
- apiVersion: 2,
518
- operation: OP.SET
519
- };
520
- }
521
- else if ('addLabel' in mod) {
522
- patch = {
523
- syncAction: {
524
- labelEditAction: {
525
- name: mod.addLabel.name,
526
- color: mod.addLabel.color,
527
- predefinedId: mod.addLabel.predefinedId,
528
- deleted: mod.addLabel.deleted
529
- }
530
- },
531
- index: ['label_edit', mod.addLabel.id],
532
- type: 'regular',
533
- apiVersion: 3,
534
- operation: OP.SET
535
- };
536
- }
537
- else if ('addChatLabel' in mod) {
538
- patch = {
539
- syncAction: {
540
- labelAssociationAction: {
541
- labeled: true
542
- }
543
- },
544
- index: [LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
545
- type: 'regular',
546
- apiVersion: 3,
547
- operation: OP.SET
548
- };
549
- }
550
- else if ('removeChatLabel' in mod) {
551
- patch = {
552
- syncAction: {
553
- labelAssociationAction: {
554
- labeled: false
555
- }
556
- },
557
- index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
558
- type: 'regular',
559
- apiVersion: 3,
560
- operation: OP.SET
561
- };
562
- }
563
- else if ('addMessageLabel' in mod) {
564
- patch = {
565
- syncAction: {
566
- labelAssociationAction: {
567
- labeled: true
568
- }
569
- },
570
- index: [LabelAssociationType.Message, mod.addMessageLabel.labelId, jid, mod.addMessageLabel.messageId, '0', '0'],
571
- type: 'regular',
572
- apiVersion: 3,
573
- operation: OP.SET
574
- };
575
- }
576
- else if ('removeMessageLabel' in mod) {
577
- patch = {
578
- syncAction: {
579
- labelAssociationAction: {
580
- labeled: false
581
- }
582
- },
583
- index: [
584
- LabelAssociationType.Message,
585
- mod.removeMessageLabel.labelId,
586
- jid,
587
- mod.removeMessageLabel.messageId,
588
- '0',
589
- '0'
590
- ],
591
- type: 'regular',
592
- apiVersion: 3,
593
- operation: OP.SET
594
- };
595
- }
596
- else {
597
- throw new Boom('not supported');
598
- }
599
- patch.syncAction.timestamp = Date.now();
600
- return patch;
601
- };
602
- export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
603
- const isInitialSync = !!initialSyncOpts;
604
- const accountSettings = initialSyncOpts?.accountSettings;
605
- logger?.trace({ syncAction, initialSync: !!initialSyncOpts }, 'processing sync action');
606
- const { syncAction: { value: action }, index: [type, id, msgId, fromMe] } = syncAction;
607
- if (action?.muteAction) {
608
- ev.emit('chats.update', [
609
- {
610
- id,
611
- muteEndTime: action.muteAction?.muted ? toNumber(action.muteAction.muteEndTimestamp) : null,
612
- conditional: getChatUpdateConditional(id, undefined)
613
- }
614
- ]);
615
- }
616
- else if (action?.archiveChatAction || type === 'archive' || type === 'unarchive') {
617
- // okay so we've to do some annoying computation here
618
- // when we're initially syncing the app state
619
- // there are a few cases we need to handle
620
- // 1. if the account unarchiveChats setting is true
621
- // a. if the chat is archived, and no further messages have been received -- simple, keep archived
622
- // b. if the chat was archived, and the user received messages from the other person afterwards
623
- // then the chat should be marked unarchved --
624
- // we compare the timestamp of latest message from the other person to determine this
625
- // 2. if the account unarchiveChats setting is false -- then it doesn't matter,
626
- // it'll always take an app state action to mark in unarchived -- which we'll get anyway
627
- const archiveAction = action?.archiveChatAction;
628
- const isArchived = archiveAction ? archiveAction.archived : type === 'archive';
629
- // // basically we don't need to fire an "archive" update if the chat is being marked unarchvied
630
- // // this only applies for the initial sync
631
- // if(isInitialSync && !isArchived) {
632
- // isArchived = false
633
- // }
634
- const msgRange = !accountSettings?.unarchiveChats ? undefined : archiveAction?.messageRange;
635
- // logger?.debug({ chat: id, syncAction }, 'message range archive')
636
- ev.emit('chats.update', [
637
- {
638
- id,
639
- archived: isArchived,
640
- conditional: getChatUpdateConditional(id, msgRange)
641
- }
642
- ]);
643
- }
644
- else if (action?.markChatAsReadAction) {
645
- const markReadAction = action.markChatAsReadAction;
646
- // basically we don't need to fire an "read" update if the chat is being marked as read
647
- // because the chat is read by default
648
- // this only applies for the initial sync
649
- const isNullUpdate = isInitialSync && markReadAction.read;
650
- ev.emit('chats.update', [
651
- {
652
- id,
653
- unreadCount: isNullUpdate ? null : !!markReadAction?.read ? 0 : -1,
654
- conditional: getChatUpdateConditional(id, markReadAction?.messageRange)
655
- }
656
- ]);
657
- }
658
- else if (action?.deleteMessageForMeAction || type === 'deleteMessageForMe') {
659
- ev.emit('messages.delete', {
660
- keys: [
661
- {
662
- remoteJid: id,
663
- id: msgId,
664
- fromMe: fromMe === '1'
665
- }
666
- ]
667
- });
668
- }
669
- else if (action?.contactAction) {
670
- ev.emit('contacts.upsert', [
671
- {
672
- id: id,
673
- name: action.contactAction.fullName,
674
- lid: action.contactAction.lidJid || undefined,
675
- phoneNumber: action.contactAction.pnJid || undefined
676
- }
677
- ]);
678
- }
679
- else if (action?.pushNameSetting) {
680
- const name = action?.pushNameSetting?.name;
681
- if (name && me?.name !== name) {
682
- ev.emit('creds.update', { me: { ...me, name } });
683
- }
684
- }
685
- else if (action?.pinAction) {
686
- ev.emit('chats.update', [
687
- {
688
- id,
689
- pinned: action.pinAction?.pinned ? toNumber(action.timestamp) : null,
690
- conditional: getChatUpdateConditional(id, undefined)
691
- }
692
- ]);
693
- }
694
- else if (action?.unarchiveChatsSetting) {
695
- const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats;
696
- ev.emit('creds.update', { accountSettings: { unarchiveChats } });
697
- logger?.info(`archive setting updated => '${action.unarchiveChatsSetting.unarchiveChats}'`);
698
- if (accountSettings) {
699
- accountSettings.unarchiveChats = unarchiveChats;
700
- }
701
- }
702
- else if (action?.starAction || type === 'star') {
703
- let starred = action?.starAction?.starred;
704
- if (typeof starred !== 'boolean') {
705
- starred = syncAction.index[syncAction.index.length - 1] === '1';
706
- }
707
- ev.emit('messages.update', [
708
- {
709
- key: { remoteJid: id, id: msgId, fromMe: fromMe === '1' },
710
- update: { starred }
711
- }
712
- ]);
713
- }
714
- else if (action?.deleteChatAction || type === 'deleteChat') {
715
- if (!isInitialSync) {
716
- ev.emit('chats.delete', [id]);
717
- }
718
- }
719
- else if (action?.labelEditAction) {
720
- const { name, color, deleted, predefinedId } = action.labelEditAction;
721
- ev.emit('labels.edit', {
722
- id: id,
723
- name: name,
724
- color: color,
725
- deleted: deleted,
726
- predefinedId: predefinedId ? String(predefinedId) : undefined
727
- });
728
- }
729
- else if (action?.labelAssociationAction) {
730
- ev.emit('labels.association', {
731
- type: action.labelAssociationAction.labeled ? 'add' : 'remove',
732
- association: type === LabelAssociationType.Chat
733
- ? {
734
- type: LabelAssociationType.Chat,
735
- chatId: syncAction.index[2],
736
- labelId: syncAction.index[1]
737
- }
738
- : {
739
- type: LabelAssociationType.Message,
740
- chatId: syncAction.index[2],
741
- messageId: syncAction.index[3],
742
- labelId: syncAction.index[1]
743
- }
744
- });
745
- }
746
- else {
747
- logger?.debug({ syncAction, id }, 'unprocessable update');
748
- }
749
- function getChatUpdateConditional(id, msgRange) {
750
- return isInitialSync
751
- ? data => {
752
- const chat = data.historySets.chats[id] || data.chatUpserts[id];
753
- if (chat) {
754
- return msgRange ? isValidPatchBasedOnMessageRange(chat, msgRange) : true;
755
- }
756
- }
757
- : undefined;
758
- }
759
- function isValidPatchBasedOnMessageRange(chat, msgRange) {
760
- const lastMsgTimestamp = Number(msgRange?.lastMessageTimestamp || msgRange?.lastSystemMessageTimestamp || 0);
761
- const chatLastMsgTimestamp = Number(chat?.lastMessageRecvTimestamp || 0);
762
- return lastMsgTimestamp >= chatLastMsgTimestamp;
763
- }
764
- };
1
+ import { Boom } from '@hapi/boom';
2
+ import { proto } from '../../WAProto/index.js';
3
+ import { LabelAssociationType } from '../Types/LabelAssociation.js';
4
+ import { processContactAction, emitSyncActionResults } from './sync-action-utils.js'
5
+ import { getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidNormalizedUser, isJidUser } from '../WABinary/index.js';
6
+ import { aesDecrypt, aesEncrypt, hkdf, hmacSign } from './crypto.js';
7
+ import { toNumber } from './generics.js';
8
+ import { LT_HASH_ANTI_TAMPERING } from './lt-hash.js';
9
+ import { downloadContentFromMessage } from './messages-media.js';
10
+
11
+ const mutationKeys = async (keydata) => {
12
+ const expanded = hkdf(keydata, 160, { info: 'WhatsApp Mutation Keys' });
13
+ return {
14
+ indexKey: expanded.slice(0, 32),
15
+ valueEncryptionKey: expanded.slice(32, 64),
16
+ valueMacKey: expanded.slice(64, 96),
17
+ snapshotMacKey: expanded.slice(96, 128),
18
+ patchMacKey: expanded.slice(128, 160)
19
+ };
20
+ };
21
+
22
+ const generateMac = (operation, data, keyId, key) => {
23
+ const getKeyData = () => {
24
+ let r;
25
+ switch (operation) {
26
+ case proto.SyncdMutation.SyncdOperation.SET:
27
+ r = 0x01;
28
+ break;
29
+ case proto.SyncdMutation.SyncdOperation.REMOVE:
30
+ r = 0x02;
31
+ break;
32
+ }
33
+ const buff = Buffer.from([r]);
34
+ return Buffer.concat([buff, Buffer.from(keyId, 'base64')]);
35
+ };
36
+ const keyData = getKeyData();
37
+ const last = Buffer.alloc(8);
38
+ last.set([keyData.length], last.length - 1);
39
+ const total = Buffer.concat([keyData, data, last]);
40
+ const hmac = hmacSign(total, key, 'sha512');
41
+ return hmac.slice(0, 32);
42
+ };
43
+
44
+ const to64BitNetworkOrder = (e) => {
45
+ const buff = Buffer.alloc(8);
46
+ buff.writeUint32BE(e, 4);
47
+ return buff;
48
+ };
49
+
50
+ const makeLtHashGenerator = ({ indexValueMap, hash }) => {
51
+ indexValueMap = { ...indexValueMap };
52
+ const addBuffs = [];
53
+ const subBuffs = [];
54
+ return {
55
+ mix: ({ indexMac, valueMac, operation }) => {
56
+ const indexMacBase64 = Buffer.from(indexMac).toString('base64');
57
+ const prevOp = indexValueMap[indexMacBase64];
58
+ if (operation === proto.SyncdMutation.SyncdOperation.REMOVE) {
59
+ if (!prevOp) {
60
+ // Skip remove if there's no previous operation - likely sync corruption
61
+ // The state will be resync'd from scratch
62
+ return;
63
+ }
64
+ delete indexValueMap[indexMacBase64];
65
+ }
66
+ else {
67
+ addBuffs.push(new Uint8Array(valueMac).buffer);
68
+ indexValueMap[indexMacBase64] = { valueMac };
69
+ }
70
+ if (prevOp) {
71
+ subBuffs.push(new Uint8Array(prevOp.valueMac).buffer);
72
+ }
73
+ },
74
+ finish: async () => {
75
+ const hashArrayBuffer = new Uint8Array(hash).buffer;
76
+ const result = await LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs);
77
+ const buffer = Buffer.from(result);
78
+ return {
79
+ hash: buffer,
80
+ indexValueMap
81
+ };
82
+ }
83
+ };
84
+ };
85
+
86
+ const generateSnapshotMac = (lthash, version, name, key) => {
87
+ const total = Buffer.concat([lthash, to64BitNetworkOrder(version), Buffer.from(name, 'utf-8')]);
88
+ return hmacSign(total, key, 'sha256');
89
+ };
90
+
91
+ const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
92
+ const total = Buffer.concat([snapshotMac, ...valueMacs, to64BitNetworkOrder(version), Buffer.from(type, 'utf-8')]);
93
+ return hmacSign(total, key);
94
+ };
95
+
96
+ export const newLTHashState = () => ({ version: 0, hash: Buffer.alloc(128), indexValueMap: {} });
97
+
98
+ // Added: was missing from compiled output, required by chats.js
99
+ export const ensureLTHashStateVersion = (state) => {
100
+ if (typeof state.version !== 'number' || isNaN(state.version)) {
101
+ state.version = 0;
102
+ }
103
+ return state;
104
+ };
105
+
106
+ // Added: used by chats.js to detect missing key errors across all throw sites
107
+ export const isMissingKeyError = (error) => {
108
+ return error?.data?.isMissingKey === true
109
+ || error?.output?.statusCode === 404
110
+ || error?.statusCode === 404;
111
+ };
112
+
113
+ export const encodeSyncdPatch = async ({ type, index, syncAction, apiVersion, operation }, myAppStateKeyId, state, getAppStateSyncKey) => {
114
+ const key = !!myAppStateKeyId ? await getAppStateSyncKey(myAppStateKeyId) : undefined;
115
+ if (!key) {
116
+ throw new Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404, data: { isMissingKey: true } });
117
+ }
118
+ const encKeyId = Buffer.from(myAppStateKeyId, 'base64');
119
+ state = { ...state, indexValueMap: { ...state.indexValueMap } };
120
+ const indexBuffer = Buffer.from(JSON.stringify(index));
121
+ const dataProto = proto.SyncActionData.fromObject({
122
+ index: indexBuffer,
123
+ value: syncAction,
124
+ padding: new Uint8Array(0),
125
+ version: apiVersion
126
+ });
127
+ const encoded = proto.SyncActionData.encode(dataProto).finish();
128
+ const keyValue = await mutationKeys(key.keyData);
129
+ const encValue = aesEncrypt(encoded, keyValue.valueEncryptionKey);
130
+ const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey);
131
+ const indexMac = hmacSign(indexBuffer, keyValue.indexKey);
132
+ const generator = makeLtHashGenerator(state);
133
+ generator.mix({ indexMac, valueMac, operation });
134
+ Object.assign(state, await generator.finish());
135
+ state.version += 1;
136
+ const snapshotMac = generateSnapshotMac(state.hash, state.version, type, keyValue.snapshotMacKey);
137
+ const patch = {
138
+ patchMac: generatePatchMac(snapshotMac, [valueMac], state.version, type, keyValue.patchMacKey),
139
+ snapshotMac: snapshotMac,
140
+ keyId: { id: encKeyId },
141
+ mutations: [
142
+ {
143
+ operation: operation,
144
+ record: {
145
+ index: { blob: indexMac },
146
+ value: { blob: Buffer.concat([encValue, valueMac]) },
147
+ keyId: { id: encKeyId }
148
+ }
149
+ }
150
+ ]
151
+ };
152
+ const base64Index = indexMac.toString('base64');
153
+ state.indexValueMap[base64Index] = { valueMac };
154
+ return { patch, state };
155
+ };
156
+
157
+ export const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncKey, onMutation, validateMacs, logger) => {
158
+ const ltGenerator = makeLtHashGenerator(initialState);
159
+ const derivedKeyCache = new Map()
160
+ let skippedMutations = 0;
161
+ for (const msgMutation of msgMutations) {
162
+ const operation = 'operation' in msgMutation ? msgMutation.operation : proto.SyncdMutation.SyncdOperation.SET;
163
+ const record = 'record' in msgMutation && !!msgMutation.record ? msgMutation.record : msgMutation;
164
+ let key;
165
+ try {
166
+ key = await getKey(record.keyId.id);
167
+ } catch (err) {
168
+ if (err?.data?.isMissingKey) {
169
+ skippedMutations++;
170
+ logger?.warn?.({ keyId: Buffer.from(record.keyId.id).toString('base64'), skippedMutations }, 'Skipping mutation with missing key — non-critical data may be stale');
171
+ continue;
172
+ }
173
+ throw err; // re-throw non-missing-key errors (e.g. HMAC failures)
174
+ }
175
+ const content = Buffer.from(record.value.blob);
176
+ const encContent = content.slice(0, -32);
177
+ const ogValueMac = content.slice(-32);
178
+ if (validateMacs) {
179
+ const contentHmac = generateMac(operation, encContent, record.keyId.id, key.valueMacKey);
180
+ if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
181
+ throw new Boom('HMAC content verification failed');
182
+ }
183
+ }
184
+ const result = aesDecrypt(encContent, key.valueEncryptionKey);
185
+ const syncAction = proto.SyncActionData.decode(result);
186
+ if (validateMacs) {
187
+ const hmac = hmacSign(syncAction.index, key.indexKey);
188
+ if (Buffer.compare(hmac, record.index.blob) !== 0) {
189
+ throw new Boom('HMAC index verification failed');
190
+ }
191
+ }
192
+ const indexStr = Buffer.from(syncAction.index).toString();
193
+ onMutation({ syncAction, index: JSON.parse(indexStr) });
194
+ ltGenerator.mix({
195
+ indexMac: record.index.blob,
196
+ valueMac: ogValueMac,
197
+ operation: operation
198
+ });
199
+ }
200
+ if (skippedMutations > 0) {
201
+ logger?.info?.({ skippedMutations }, 'App state sync completed with skipped mutations');
202
+ }
203
+ return await ltGenerator.finish();
204
+
205
+ async function getKey(keyId) {
206
+ const base64Key = Buffer.from(keyId).toString('base64')
207
+ const cached = derivedKeyCache.get(base64Key)
208
+ if (cached) return cached
209
+ const keyEnc = await getAppStateSyncKey(base64Key)
210
+ if (!keyEnc) throw new Boom(`failed to find key "${base64Key}" to decode mutation`, { statusCode: 404, data: { isMissingKey: true, msgMutations } })
211
+ const keys = await mutationKeys(keyEnc.keyData)
212
+ derivedKeyCache.set(base64Key, keys)
213
+ return keys
214
+ }
215
+ };
216
+
217
+ export const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
218
+ if (validateMacs) {
219
+ const base64Key = Buffer.from(msg.keyId.id).toString('base64');
220
+ const mainKeyObj = await getAppStateSyncKey(base64Key);
221
+ if (!mainKeyObj) {
222
+ throw new Boom(`failed to find key "${base64Key}" to decode patch`, {
223
+ statusCode: 404,
224
+ data: { isMissingKey: true, msg }
225
+ });
226
+ }
227
+ const mainKey = await mutationKeys(mainKeyObj.keyData);
228
+ const mutationmacs = msg.mutations.map(mutation => mutation.record.value.blob.slice(-32));
229
+ const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs, toNumber(msg.version.version), name, mainKey.patchMacKey);
230
+ if (Buffer.compare(patchMac, msg.patchMac) !== 0) {
231
+ throw new Boom('Invalid patch mac');
232
+ }
233
+ }
234
+ const result = await decodeSyncdMutations(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs);
235
+ return result;
236
+ };
237
+
238
+ export const extractSyncdPatches = async (result, options) => {
239
+ const syncNode = getBinaryNodeChild(result, 'sync');
240
+ const collectionNodes = getBinaryNodeChildren(syncNode, 'collection');
241
+ const final = {};
242
+ await Promise.all(collectionNodes.map(async (collectionNode) => {
243
+ const patchesNode = getBinaryNodeChild(collectionNode, 'patches');
244
+ const patches = getBinaryNodeChildren(patchesNode || collectionNode, 'patch');
245
+ const snapshotNode = getBinaryNodeChild(collectionNode, 'snapshot');
246
+ const syncds = [];
247
+ const name = collectionNode.attrs.name;
248
+ const hasMorePatches = collectionNode.attrs.has_more_patches === 'true';
249
+ let snapshot = undefined;
250
+ if (snapshotNode && !!snapshotNode.content) {
251
+ if (!Buffer.isBuffer(snapshotNode)) {
252
+ snapshotNode.content = Buffer.from(Object.values(snapshotNode.content));
253
+ }
254
+ const blobRef = proto.ExternalBlobReference.decode(snapshotNode.content);
255
+ const data = await downloadExternalBlob(blobRef, options);
256
+ snapshot = proto.SyncdSnapshot.decode(data);
257
+ }
258
+ for (let { content } of patches) {
259
+ if (content) {
260
+ if (!Buffer.isBuffer(content)) {
261
+ content = Buffer.from(Object.values(content));
262
+ }
263
+ const syncd = proto.SyncdPatch.decode(content);
264
+ if (!syncd.version) {
265
+ syncd.version = { version: +collectionNode.attrs.version + 1 };
266
+ }
267
+ syncds.push(syncd);
268
+ }
269
+ }
270
+ final[name] = { patches: syncds, hasMorePatches, snapshot };
271
+ }));
272
+ return final;
273
+ };
274
+
275
+ export const downloadExternalBlob = async (blob, options) => {
276
+ const stream = await downloadContentFromMessage(blob, 'md-app-state', { options });
277
+ const bufferArray = [];
278
+ for await (const chunk of stream) {
279
+ bufferArray.push(chunk);
280
+ }
281
+ return Buffer.concat(bufferArray);
282
+ };
283
+
284
+ export const downloadExternalPatch = async (blob, options) => {
285
+ const buffer = await downloadExternalBlob(blob, options);
286
+ const syncData = proto.SyncdMutations.decode(buffer);
287
+ return syncData;
288
+ };
289
+
290
+ export const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, minimumVersionNumber, validateMacs = true, logger) => {
291
+ const newState = newLTHashState();
292
+ newState.version = toNumber(snapshot.version.version);
293
+ const mutationMap = {};
294
+ const areMutationsRequired = typeof minimumVersionNumber === 'undefined' || newState.version > minimumVersionNumber;
295
+ const { hash, indexValueMap } = await decodeSyncdMutations(snapshot.records, newState, getAppStateSyncKey, areMutationsRequired
296
+ ? mutation => {
297
+ const index = mutation.syncAction.index?.toString();
298
+ mutationMap[index] = mutation;
299
+ }
300
+ : () => { }, validateMacs, logger);
301
+ newState.hash = hash;
302
+ newState.indexValueMap = indexValueMap;
303
+ if (validateMacs) {
304
+ const base64Key = Buffer.from(snapshot.keyId.id).toString('base64');
305
+ const keyEnc = await getAppStateSyncKey(base64Key);
306
+ if (!keyEnc) {
307
+ throw new Boom(`failed to find key "${base64Key}" to decode mutation`, {
308
+ statusCode: 404,
309
+ data: { isMissingKey: true }
310
+ });
311
+ }
312
+ const result = await mutationKeys(keyEnc.keyData);
313
+ const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
314
+ if (Buffer.compare(snapshot.mac, computedSnapshotMac) !== 0) {
315
+ throw new Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`);
316
+ }
317
+ }
318
+ return { state: newState, mutationMap };
319
+ };
320
+
321
+ export const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options, minimumVersionNumber, logger, validateMacs = true) => {
322
+ const newState = {
323
+ ...initial,
324
+ indexValueMap: { ...initial.indexValueMap }
325
+ };
326
+ const mutationMap = {};
327
+ for (const syncd of syncds) {
328
+ const { version, keyId, snapshotMac } = syncd;
329
+ if (syncd.externalMutations) {
330
+ logger?.trace({ name, version }, 'downloading external patch');
331
+ const ref = await downloadExternalPatch(syncd.externalMutations, options);
332
+ logger?.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch');
333
+ syncd.mutations?.push(...ref.mutations);
334
+ }
335
+ const patchVersion = toNumber(version.version);
336
+ newState.version = patchVersion;
337
+ const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber;
338
+ const decodeResult = await decodeSyncdPatch(syncd, name, newState, getAppStateSyncKey, shouldMutate
339
+ ? mutation => {
340
+ const index = mutation.syncAction.index?.toString();
341
+ mutationMap[index] = mutation;
342
+ }
343
+ : () => { }, true);
344
+ newState.hash = decodeResult.hash;
345
+ newState.indexValueMap = decodeResult.indexValueMap;
346
+ if (validateMacs) {
347
+ const base64Key = Buffer.from(keyId.id).toString('base64');
348
+ const keyEnc = await getAppStateSyncKey(base64Key);
349
+ if (!keyEnc) {
350
+ throw new Boom(`failed to find key "${base64Key}" to decode mutation`, {
351
+ statusCode: 404,
352
+ data: { isMissingKey: true }
353
+ });
354
+ }
355
+ const result = await mutationKeys(keyEnc.keyData);
356
+ const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
357
+ if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
358
+ throw new Boom(`failed to verify LTHash at ${newState.version} of ${name}`);
359
+ }
360
+ }
361
+ syncd.mutations = [];
362
+ }
363
+ return { state: newState, mutationMap };
364
+ };
365
+
366
+ export const chatModificationToAppPatch = (mod, jid) => {
367
+ const OP = proto.SyncdMutation.SyncdOperation;
368
+ const getMessageRange = (lastMessages) => {
369
+ let messageRange;
370
+ if (Array.isArray(lastMessages)) {
371
+ const lastMsg = lastMessages[lastMessages.length - 1];
372
+ messageRange = {
373
+ lastMessageTimestamp: lastMsg?.messageTimestamp,
374
+ messages: lastMessages?.length
375
+ ? lastMessages.map(m => {
376
+ if (!m.key?.id || !m.key?.remoteJid) {
377
+ throw new Boom('Incomplete key', { statusCode: 400, data: m });
378
+ }
379
+ if (isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
380
+ throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m });
381
+ }
382
+ if (!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
383
+ throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m });
384
+ }
385
+ if (m.key.participant) {
386
+ m.key.participant = jidNormalizedUser(m.key.participant);
387
+ }
388
+ return m;
389
+ })
390
+ : undefined
391
+ };
392
+ }
393
+ else {
394
+ messageRange = lastMessages;
395
+ }
396
+ return messageRange;
397
+ };
398
+ let patch;
399
+ if ('mute' in mod) {
400
+ patch = {
401
+ syncAction: { muteAction: { muted: !!mod.mute, muteEndTimestamp: mod.mute || undefined } },
402
+ index: ['mute', jid],
403
+ type: 'regular_high',
404
+ apiVersion: 2,
405
+ operation: OP.SET
406
+ };
407
+ }
408
+ else if ('archive' in mod) {
409
+ patch = {
410
+ syncAction: { archiveChatAction: { archived: !!mod.archive, messageRange: getMessageRange(mod.lastMessages) } },
411
+ index: ['archive', jid],
412
+ type: 'regular_low',
413
+ apiVersion: 3,
414
+ operation: OP.SET
415
+ };
416
+ }
417
+ else if ('markRead' in mod) {
418
+ patch = {
419
+ syncAction: { markChatAsReadAction: { read: mod.markRead, messageRange: getMessageRange(mod.lastMessages) } },
420
+ index: ['markChatAsRead', jid],
421
+ type: 'regular_low',
422
+ apiVersion: 3,
423
+ operation: OP.SET
424
+ };
425
+ }
426
+ else if ('deleteForMe' in mod) {
427
+ const { timestamp, key, deleteMedia } = mod.deleteForMe;
428
+ patch = {
429
+ syncAction: { deleteMessageForMeAction: { deleteMedia, messageTimestamp: timestamp } },
430
+ index: ['deleteMessageForMe', jid, key.id, key.fromMe ? '1' : '0', '0'],
431
+ type: 'regular_high',
432
+ apiVersion: 3,
433
+ operation: OP.SET
434
+ };
435
+ }
436
+ else if ('clear' in mod) {
437
+ patch = {
438
+ syncAction: { clearChatAction: { messageRange: getMessageRange(mod.lastMessages) } },
439
+ index: ['clearChat', jid, '1', '0'],
440
+ type: 'regular_high',
441
+ apiVersion: 6,
442
+ operation: OP.SET
443
+ };
444
+ }
445
+ else if ('pin' in mod) {
446
+ patch = {
447
+ syncAction: { pinAction: { pinned: !!mod.pin } },
448
+ index: ['pin_v1', jid],
449
+ type: 'regular_low',
450
+ apiVersion: 5,
451
+ operation: OP.SET
452
+ };
453
+ }
454
+ else if ('contact' in mod) {
455
+ patch = {
456
+ syncAction: { contactAction: mod.contact || {} },
457
+ index: ['contact', jid],
458
+ type: 'critical_unblock_low',
459
+ apiVersion: 2,
460
+ operation: mod.contact ? OP.SET : OP.REMOVE
461
+ };
462
+ }
463
+ else if ('disableLinkPreviews' in mod) {
464
+ patch = {
465
+ syncAction: { privacySettingDisableLinkPreviewsAction: mod.disableLinkPreviews || {} },
466
+ index: ['setting_disableLinkPreviews'],
467
+ type: 'regular',
468
+ apiVersion: 8,
469
+ operation: OP.SET
470
+ };
471
+ }
472
+ else if ('star' in mod) {
473
+ const key = mod.star.messages[0];
474
+ patch = {
475
+ syncAction: { starAction: { starred: !!mod.star.star } },
476
+ index: ['star', jid, key.id, key.fromMe ? '1' : '0', '0'],
477
+ type: 'regular_low',
478
+ apiVersion: 2,
479
+ operation: OP.SET
480
+ };
481
+ }
482
+ else if ('delete' in mod) {
483
+ patch = {
484
+ syncAction: { deleteChatAction: { messageRange: getMessageRange(mod.lastMessages) } },
485
+ index: ['deleteChat', jid, '1'],
486
+ type: 'regular_high',
487
+ apiVersion: 6,
488
+ operation: OP.SET
489
+ };
490
+ }
491
+ else if ('pushNameSetting' in mod) {
492
+ patch = {
493
+ syncAction: { pushNameSetting: { name: mod.pushNameSetting } },
494
+ index: ['setting_pushName'],
495
+ type: 'critical_block',
496
+ apiVersion: 1,
497
+ operation: OP.SET
498
+ };
499
+ }
500
+ else if ('quickReply' in mod) {
501
+ patch = {
502
+ syncAction: {
503
+ quickReplyAction: {
504
+ count: 0,
505
+ deleted: mod.quickReply.deleted || false,
506
+ keywords: [],
507
+ message: mod.quickReply.message || '',
508
+ shortcut: mod.quickReply.shortcut || ''
509
+ }
510
+ },
511
+ index: ['quick_reply', mod.quickReply.timestamp || String(Math.floor(Date.now() / 1000))],
512
+ type: 'regular',
513
+ apiVersion: 2,
514
+ operation: OP.SET
515
+ };
516
+ }
517
+ else if ('addLabel' in mod) {
518
+ patch = {
519
+ syncAction: {
520
+ labelEditAction: {
521
+ name: mod.addLabel.name,
522
+ color: mod.addLabel.color,
523
+ predefinedId: mod.addLabel.predefinedId,
524
+ deleted: mod.addLabel.deleted
525
+ }
526
+ },
527
+ index: ['label_edit', mod.addLabel.id],
528
+ type: 'regular',
529
+ apiVersion: 3,
530
+ operation: OP.SET
531
+ };
532
+ }
533
+ else if ('addChatLabel' in mod) {
534
+ patch = {
535
+ syncAction: { labelAssociationAction: { labeled: true } },
536
+ index: [LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
537
+ type: 'regular',
538
+ apiVersion: 3,
539
+ operation: OP.SET
540
+ };
541
+ }
542
+ else if ('removeChatLabel' in mod) {
543
+ patch = {
544
+ syncAction: { labelAssociationAction: { labeled: false } },
545
+ index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
546
+ type: 'regular',
547
+ apiVersion: 3,
548
+ operation: OP.SET
549
+ };
550
+ }
551
+ else if ('addMessageLabel' in mod) {
552
+ patch = {
553
+ syncAction: { labelAssociationAction: { labeled: true } },
554
+ index: [LabelAssociationType.Message, mod.addMessageLabel.labelId, jid, mod.addMessageLabel.messageId, '0', '0'],
555
+ type: 'regular',
556
+ apiVersion: 3,
557
+ operation: OP.SET
558
+ };
559
+ }
560
+ else if ('removeMessageLabel' in mod) {
561
+ patch = {
562
+ syncAction: { labelAssociationAction: { labeled: false } },
563
+ index: [LabelAssociationType.Message, mod.removeMessageLabel.labelId, jid, mod.removeMessageLabel.messageId, '0', '0'],
564
+ type: 'regular',
565
+ apiVersion: 3,
566
+ operation: OP.SET
567
+ };
568
+ }
569
+ else {
570
+ throw new Boom('not supported');
571
+ }
572
+ patch.syncAction.timestamp = Date.now();
573
+ return patch;
574
+ };
575
+
576
+ export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
577
+ const isInitialSync = !!initialSyncOpts;
578
+ const accountSettings = initialSyncOpts?.accountSettings;
579
+ logger?.trace({ syncAction, initialSync: !!initialSyncOpts }, 'processing sync action');
580
+ const { syncAction: { value: action }, index: [type, id, msgId, fromMe] } = syncAction;
581
+ if (action?.muteAction) {
582
+ ev.emit('chats.update', [{
583
+ id,
584
+ muteEndTime: action.muteAction?.muted ? toNumber(action.muteAction.muteEndTimestamp) : null,
585
+ conditional: getChatUpdateConditional(id, undefined)
586
+ }]);
587
+ }
588
+ else if (action?.archiveChatAction || type === 'archive' || type === 'unarchive') {
589
+ const archiveAction = action?.archiveChatAction;
590
+ const isArchived = archiveAction ? archiveAction.archived : type === 'archive';
591
+ const msgRange = !accountSettings?.unarchiveChats ? undefined : archiveAction?.messageRange;
592
+ ev.emit('chats.update', [{
593
+ id,
594
+ archived: isArchived,
595
+ conditional: getChatUpdateConditional(id, msgRange)
596
+ }]);
597
+ }
598
+ else if (action?.markChatAsReadAction) {
599
+ const markReadAction = action.markChatAsReadAction;
600
+ const isNullUpdate = isInitialSync && markReadAction.read;
601
+ ev.emit('chats.update', [{
602
+ id,
603
+ unreadCount: isNullUpdate ? null : !!markReadAction?.read ? 0 : -1,
604
+ conditional: getChatUpdateConditional(id, markReadAction?.messageRange)
605
+ }]);
606
+ }
607
+ else if (action?.deleteMessageForMeAction || type === 'deleteMessageForMe') {
608
+ ev.emit('messages.delete', {
609
+ keys: [{ remoteJid: id, id: msgId, fromMe: fromMe === '1' }]
610
+ });
611
+ }
612
+ else if (action?.contactAction) {
613
+ const results = processContactAction(action.contactAction, id, logger)
614
+ emitSyncActionResults(ev, results)
615
+ }
616
+ else if (action?.pushNameSetting) {
617
+ const name = action?.pushNameSetting?.name;
618
+ if (name && me?.name !== name) {
619
+ ev.emit('creds.update', { me: { ...me, name } });
620
+ }
621
+ }
622
+ else if (action?.pinAction) {
623
+ ev.emit('chats.update', [{
624
+ id,
625
+ pinned: action.pinAction?.pinned ? toNumber(action.timestamp) : null,
626
+ conditional: getChatUpdateConditional(id, undefined)
627
+ }]);
628
+ }
629
+ else if (action?.unarchiveChatsSetting) {
630
+ const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats;
631
+ ev.emit('creds.update', { accountSettings: { unarchiveChats } });
632
+ logger?.info(`archive setting updated => '${action.unarchiveChatsSetting.unarchiveChats}'`);
633
+ if (accountSettings) {
634
+ accountSettings.unarchiveChats = unarchiveChats;
635
+ }
636
+ }
637
+ else if (action?.starAction || type === 'star') {
638
+ let starred = action?.starAction?.starred;
639
+ if (typeof starred !== 'boolean') {
640
+ starred = syncAction.index[syncAction.index.length - 1] === '1';
641
+ }
642
+ ev.emit('messages.update', [{
643
+ key: { remoteJid: id, id: msgId, fromMe: fromMe === '1' },
644
+ update: { starred }
645
+ }]);
646
+ }
647
+ else if (action?.deleteChatAction || type === 'deleteChat') {
648
+ if (!isInitialSync) {
649
+ ev.emit('chats.delete', [id]);
650
+ }
651
+ }
652
+ else if (action?.labelEditAction) {
653
+ const { name, color, deleted, predefinedId } = action.labelEditAction;
654
+ ev.emit('labels.edit', {
655
+ id: id,
656
+ name: name,
657
+ color: color,
658
+ deleted: deleted,
659
+ predefinedId: predefinedId ? String(predefinedId) : undefined
660
+ });
661
+ }
662
+ else if (action?.labelAssociationAction) {
663
+ ev.emit('labels.association', {
664
+ type: action.labelAssociationAction.labeled ? 'add' : 'remove',
665
+ association: type === LabelAssociationType.Chat
666
+ ? { type: LabelAssociationType.Chat, chatId: syncAction.index[2], labelId: syncAction.index[1] }
667
+ : { type: LabelAssociationType.Message, chatId: syncAction.index[2], messageId: syncAction.index[3], labelId: syncAction.index[1] }
668
+ });
669
+ }
670
+ else if (action?.localeSetting?.locale) {
671
+ ev.emit('settings.update', { setting: 'locale', value: action.localeSetting.locale })
672
+ }
673
+ else if (action?.timeFormatAction) {
674
+ ev.emit('settings.update', { setting: 'timeFormat', value: action.timeFormatAction })
675
+ }
676
+ else if (action?.pnForLidChatAction) {
677
+ if (action.pnForLidChatAction.pnJid) ev.emit('lid-mapping.update', { lid: id, pn: action.pnForLidChatAction.pnJid })
678
+ }
679
+ else if (action?.privacySettingRelayAllCalls) {
680
+ ev.emit('settings.update', { setting: 'privacySettingRelayAllCalls', value: action.privacySettingRelayAllCalls })
681
+ }
682
+ else if (action?.statusPrivacy) {
683
+ ev.emit('settings.update', { setting: 'statusPrivacy', value: action.statusPrivacy })
684
+ }
685
+ else if (action?.lockChatAction) {
686
+ ev.emit('chats.lock', { id, locked: !!action.lockChatAction.locked })
687
+ }
688
+ else if (action?.privacySettingDisableLinkPreviewsAction) {
689
+ ev.emit('settings.update', { setting: 'disableLinkPreviews', value: action.privacySettingDisableLinkPreviewsAction })
690
+ }
691
+ else if (action?.notificationActivitySettingAction?.notificationActivitySetting) {
692
+ ev.emit('settings.update', { setting: 'notificationActivitySetting', value: action.notificationActivitySettingAction.notificationActivitySetting })
693
+ }
694
+ else if (action?.lidContactAction) {
695
+ ev.emit('contacts.upsert', [{
696
+ id,
697
+ name: action.lidContactAction.fullName || action.lidContactAction.firstName || action.lidContactAction.username || undefined,
698
+ username: action.lidContactAction.username || undefined,
699
+ lid: id,
700
+ phoneNumber: undefined
701
+ }])
702
+ }
703
+ else if (action?.privacySettingChannelsPersonalisedRecommendationAction) {
704
+ ev.emit('settings.update', { setting: 'channelsPersonalisedRecommendation', value: action.privacySettingChannelsPersonalisedRecommendationAction })
705
+ }
706
+ else {
707
+ logger?.debug({ syncAction, id }, 'unprocessable update');
708
+ }
709
+
710
+ function getChatUpdateConditional(id, msgRange) {
711
+ return isInitialSync
712
+ ? data => {
713
+ const chat = data.historySets.chats[id] || data.chatUpserts[id];
714
+ if (chat) {
715
+ return msgRange ? isValidPatchBasedOnMessageRange(chat, msgRange) : true;
716
+ }
717
+ }
718
+ : undefined;
719
+ }
720
+
721
+ function isValidPatchBasedOnMessageRange(chat, msgRange) {
722
+ const lastMsgTimestamp = Number(msgRange?.lastMessageTimestamp || msgRange?.lastSystemMessageTimestamp || 0);
723
+ const chatLastMsgTimestamp = Number(chat?.lastMessageRecvTimestamp || 0);
724
+ return lastMsgTimestamp >= chatLastMsgTimestamp;
725
+ }
726
+ };
765
727
  //# sourceMappingURL=chat-utils.js.map