vialeys 0.0.1 → 0.0.2

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 (102) hide show
  1. package/README.md +11 -5
  2. package/WAProto/AICommon/AICommon.js +13991 -9169
  3. package/WAProto/AICommon/AICommon.proto +110 -3
  4. package/WAProto/CompanionReg/CompanionReg.js +114 -0
  5. package/WAProto/CompanionReg/CompanionReg.proto +3 -0
  6. package/WAProto/DeviceCapabilities/DeviceCapabilities.js +652 -0
  7. package/WAProto/DeviceCapabilities/DeviceCapabilities.proto +19 -0
  8. package/WAProto/E2E/E2E.js +77193 -51248
  9. package/WAProto/E2E/E2E.proto +68 -12
  10. package/WAProto/HistorySync/HistorySync.js +3375 -178
  11. package/WAProto/HistorySync/HistorySync.proto +3 -3
  12. package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.js +98 -49
  13. package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.proto +2 -2
  14. package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.js +75226 -48422
  15. package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.proto +6 -0
  16. package/WAProto/StatusAttributions/StatusAttributions.js +21 -0
  17. package/WAProto/StatusAttributions/StatusAttributions.proto +3 -0
  18. package/WAProto/SyncAction/SyncAction.js +9423 -2023
  19. package/WAProto/SyncAction/SyncAction.proto +208 -15
  20. package/WAProto/Wa6/Wa6.js +83 -0
  21. package/WAProto/Wa6/Wa6.proto +3 -0
  22. package/WAProto/Web/Web.js +92664 -63403
  23. package/WAProto/Web/Web.proto +31 -13
  24. package/engine-requirements.js +9 -7
  25. package/lib/Defaults/baileys-version.json +2 -2
  26. package/lib/Defaults/connection.js +51 -0
  27. package/lib/Defaults/constants.js +62 -0
  28. package/lib/Defaults/history.js +17 -0
  29. package/lib/Defaults/index.js +36 -142
  30. package/lib/Defaults/media.js +48 -0
  31. package/lib/Defaults/prefix.js +18 -0
  32. package/lib/Signal/Group/group-session-builder.js +10 -42
  33. package/lib/Signal/Group/group_cipher.js +9 -6
  34. package/lib/Signal/Group/index.js +39 -53
  35. package/lib/Signal/Group/keyhelper.js +8 -41
  36. package/lib/Signal/Group/sender-chain-key.js +4 -4
  37. package/lib/Signal/Group/sender-key-distribution-message.js +5 -5
  38. package/lib/Signal/Group/sender-key-message.js +12 -8
  39. package/lib/Signal/Group/sender-key-state.js +4 -4
  40. package/lib/Signal/Group/sender-message-key.js +2 -2
  41. package/lib/Signal/libsignal.js +45 -69
  42. package/lib/Signal/lid-mapping.js +15 -11
  43. package/lib/Socket/Client/types.js +2 -2
  44. package/lib/Socket/Client/websocket.js +16 -14
  45. package/lib/Socket/business.js +41 -32
  46. package/lib/Socket/chats.js +123 -98
  47. package/lib/Socket/community.js +50 -40
  48. package/lib/Socket/groups.js +59 -47
  49. package/lib/Socket/index.js +4 -4
  50. package/lib/Socket/messages-recv.js +226 -171
  51. package/lib/Socket/messages-send.js +187 -143
  52. package/lib/Socket/newsletter.js +61 -47
  53. package/lib/Socket/socket.js +133 -90
  54. package/lib/Socket/usync.js +6 -6
  55. package/lib/Store/index.js +27 -11
  56. package/lib/Store/make-cache-manager-store.js +14 -15
  57. package/lib/Store/make-in-memory-store.js +28 -24
  58. package/lib/Types/LabelAssociation.js +2 -2
  59. package/lib/Types/Message.js +6 -6
  60. package/lib/Types/MexUpdates.js +5 -4
  61. package/lib/Types/State.js +4 -4
  62. package/lib/Types/index.js +28 -12
  63. package/lib/Utils/auth-utils.js +28 -26
  64. package/lib/Utils/baileys-event-stream.js +68 -69
  65. package/lib/Utils/business.js +63 -53
  66. package/lib/Utils/chat-utils.js +81 -71
  67. package/lib/Utils/crypto.js +25 -45
  68. package/lib/Utils/decode-wa-message.js +319 -311
  69. package/lib/Utils/event-buffer.js +21 -22
  70. package/lib/Utils/generics.js +103 -73
  71. package/lib/Utils/history.js +21 -21
  72. package/lib/Utils/index.js +27 -13
  73. package/lib/Utils/link-preview.js +7 -30
  74. package/lib/Utils/logger.js +5 -5
  75. package/lib/Utils/lt-hash.js +3 -3
  76. package/lib/Utils/message-retry-manager.js +4 -4
  77. package/lib/Utils/messages-media.js +104 -109
  78. package/lib/Utils/messages.js +203 -171
  79. package/lib/Utils/noise-handler.js +28 -19
  80. package/lib/Utils/process-message.js +370 -136
  81. package/lib/Utils/signal.js +36 -25
  82. package/lib/Utils/use-multi-file-auth-state.js +18 -22
  83. package/lib/Utils/validate-connection.js +52 -45
  84. package/lib/WABinary/decode.js +6 -32
  85. package/lib/WABinary/encode.js +3 -29
  86. package/lib/WABinary/generic-utils.js +4 -4
  87. package/lib/WABinary/index.js +27 -11
  88. package/lib/WAM/encode.js +16 -8
  89. package/lib/WAM/index.js +27 -11
  90. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +20 -16
  91. package/lib/WAUSync/Protocols/USyncContactProtocol.js +2 -2
  92. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +7 -4
  93. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +2 -2
  94. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -2
  95. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +2 -2
  96. package/lib/WAUSync/Protocols/index.js +27 -11
  97. package/lib/WAUSync/USyncQuery.js +17 -10
  98. package/lib/WAUSync/index.js +27 -11
  99. package/lib/index.js +62 -37
  100. package/package.json +1 -1
  101. package/WAProto/index.d.ts +0 -55
  102. package/lib/index.d.ts +0 -13
@@ -2,31 +2,46 @@
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true })
4
4
 
5
- const WAProto_1 = require("../../WAProto")
6
- const Types_1 = require("../Types")
7
- const messages_1 = require("../Utils/messages")
8
- const WABinary_1 = require("../WABinary")
9
- const crypto_1 = require("./crypto")
10
- const generics_1 = require("./generics")
11
- const history_1 = require("./history")
5
+ const { proto } = require("../../WAProto")
6
+ const { WAMessageStubType } = require("../Types")
7
+ const {
8
+ getDevice,
9
+ getContentType,
10
+ normalizeMessageContent
11
+ } = require("../Utils")
12
+ const {
13
+ areJidsSameUser,
14
+ isJidBroadcast,
15
+ isJidStatusBroadcast,
16
+ jidNormalizedUser
17
+ } = require("../WABinary")
18
+ const {
19
+ aesDecryptGCM,
20
+ hmacSign
21
+ } = require("./crypto")
22
+ const {
23
+ getKeyAuthor,
24
+ toNumber
25
+ } = require("./generics")
26
+ const { downloadAndProcessHistorySyncNotification } = require("./history")
12
27
 
13
28
  const REAL_MSG_STUB_TYPES = new Set([
14
- Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
15
- Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE,
16
- Types_1.WAMessageStubType.CALL_MISSED_VIDEO,
17
- Types_1.WAMessageStubType.CALL_MISSED_VOICE
29
+ WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
30
+ WAMessageStubType.CALL_MISSED_GROUP_VOICE,
31
+ WAMessageStubType.CALL_MISSED_VIDEO,
32
+ WAMessageStubType.CALL_MISSED_VOICE
18
33
  ])
19
34
 
20
35
  const REAL_MSG_REQ_ME_STUB_TYPES = new Set([
21
- Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD
36
+ WAMessageStubType.GROUP_PARTICIPANT_ADD
22
37
  ])
23
38
 
24
39
  /** Cleans a received message to further processing */
25
40
  const cleanMessage = (message, meId) => {
26
41
  // ensure remoteJid and participant doesn't have device or agent in it
27
- message.key.remoteJid = WABinary_1.jidNormalizedUser(message.key.remoteJid)
28
- message.key.participant = message.key.participant ? WABinary_1.jidNormalizedUser(message.key.participant) : undefined
29
- const content = messages_1.normalizeMessageContent(message.message)
42
+ message.key.remoteJid = jidNormalizedUser(message.key.remoteJid)
43
+ message.key.participant = message.key.participant ? jidNormalizedUser(message.key.participant) : undefined
44
+ const content = normalizeMessageContent(message.message)
30
45
  // if the message has a reaction, ensure fromMe & remoteJid are from our perspective
31
46
  if (content?.reactionMessage) {
32
47
  normaliseKey(content.reactionMessage.key)
@@ -41,7 +56,7 @@ const cleanMessage = (message, meId) => {
41
56
  // if the sender believed the message being reacted to is not from them
42
57
  // we've to correct the key to be from them, or some other participant
43
58
  msgKey.fromMe = !msgKey.fromMe
44
- ? WABinary_1.areJidsSameUser(msgKey.participant || msgKey.remoteJid, meId)
59
+ ? areJidsSameUser(msgKey.participant || msgKey.remoteJid, meId)
45
60
  // if the message being reacted to, was from them
46
61
  // fromMe automatically becomes false
47
62
  : false
@@ -54,14 +69,14 @@ const cleanMessage = (message, meId) => {
54
69
  }
55
70
 
56
71
  const isRealMessage = (message, meId) => {
57
- const normalizedContent = messages_1.normalizeMessageContent(message.message)
58
- const hasSomeContent = !!messages_1.getContentType(normalizedContent)
72
+ const normalizedContent = normalizeMessageContent(message.message)
73
+ const hasSomeContent = !!getContentType(normalizedContent)
59
74
  return (
60
75
  !!normalizedContent ||
61
76
  REAL_MSG_STUB_TYPES.has(message.messageStubType) ||
62
77
  (
63
78
  REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType) &&
64
- message.messageStubParameters?.some(p => WABinary_1.areJidsSameUser(meId, p))
79
+ message.messageStubParameters?.some(p => areJidsSameUser(meId, p))
65
80
  )
66
81
  ) &&
67
82
  hasSomeContent &&
@@ -77,8 +92,8 @@ const shouldIncrementChatUnread = (message) => (!message.key.fromMe && !message.
77
92
  * Typically -- that'll be the remoteJid, but for broadcasts, it'll be the participant
78
93
  */
79
94
  const getChatId = ({ remoteJid, participant, fromMe }) => {
80
- if (WABinary_1.isJidBroadcast(remoteJid)
81
- && !WABinary_1.isJidStatusBroadcast(remoteJid)
95
+ if (isJidBroadcast(remoteJid)
96
+ && !isJidStatusBroadcast(remoteJid)
82
97
  && !fromMe) {
83
98
  return participant
84
99
  }
@@ -99,12 +114,37 @@ function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pol
99
114
  toBinary('Poll Vote'),
100
115
  new Uint8Array([1])
101
116
  ])
102
- const key0 = crypto_1.hmacSign(pollEncKey, new Uint8Array(32), 'sha256')
103
- const decKey = crypto_1.hmacSign(sign, key0, 'sha256')
117
+ const key0 = hmacSign(pollEncKey, new Uint8Array(32), 'sha256')
118
+ const decKey = hmacSign(sign, key0, 'sha256')
104
119
  const aad = toBinary(`${pollMsgId}\u0000${voterJid}`)
105
- const decrypted = crypto_1.aesDecryptGCM(encPayload, decKey, encIv, aad)
120
+ const decrypted = aesDecryptGCM(encPayload, decKey, encIv, aad)
106
121
 
107
- return WAProto_1.proto.Message.PollVoteMessage.decode(decrypted)
122
+ return proto.Message.PollVoteMessage.decode(decrypted)
123
+
124
+ function toBinary(txt) {
125
+ return Buffer.from(txt)
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Decrypt an event edit
131
+ * @param edit encrypted event edit
132
+ * @returns message
133
+ */
134
+ function decryptEventEdit({ encPayload, encIv }, { eventCreatorJid, eventMsgId, eventEncKey, responderJid }) {
135
+ const sign = Buffer.concat([
136
+ toBinary(eventMsgId),
137
+ toBinary(eventCreatorJid),
138
+ toBinary(responderJid),
139
+ toBinary('Event Edit'),
140
+ new Uint8Array([1])
141
+ ])
142
+
143
+ const key0 = hmacSign(eventEncKey, new Uint8Array(32), 'sha256')
144
+ const decKey = hmacSign(sign, key0, 'sha256')
145
+ const decrypted = aesDecryptGCM(encPayload, decKey, encIv, null)
146
+
147
+ return proto.Message.decode(decrypted)
108
148
 
109
149
  function toBinary(txt) {
110
150
  return Buffer.from(txt)
@@ -126,12 +166,62 @@ function decryptEventResponse({ encPayload, encIv }, { eventCreatorJid, eventMsg
126
166
  new Uint8Array([1])
127
167
  ])
128
168
 
129
- const key0 = crypto_1.hmacSign(eventEncKey, new Uint8Array(32), 'sha256')
130
- const decKey = crypto_1.hmacSign(sign, key0, 'sha256')
169
+ const key0 = hmacSign(eventEncKey, new Uint8Array(32), 'sha256')
170
+ const decKey = hmacSign(sign, key0, 'sha256')
131
171
  const aad = toBinary(`${eventMsgId}\u0000${responderJid}`)
132
- const decrypted = crypto_1.aesDecryptGCM(encPayload, decKey, encIv, aad)
172
+ const decrypted = aesDecryptGCM(encPayload, decKey, encIv, aad)
173
+
174
+ return proto.Message.EventResponseMessage.decode(decrypted)
175
+
176
+ function toBinary(txt) {
177
+ return Buffer.from(txt)
178
+ }
179
+ }
180
+
181
+ /**
182
+ * Decrypt an comment message
183
+ * @param comment encrypted comment message
184
+ * @returns message
185
+ */
186
+ function decryptComment({ encPayload, encIv }, { commentCreatorJid, commentMsgId, commentEncKey, commentJid }) {
187
+ const sign = Buffer.concat([
188
+ toBinary(commentMsgId),
189
+ toBinary(commentCreatorJid),
190
+ toBinary(commentJid),
191
+ toBinary('Enc Comment'),
192
+ new Uint8Array([1])
193
+ ])
194
+
195
+ const key0 = hmacSign(commentEncKey, new Uint8Array(32), 'sha256')
196
+ const decKey = hmacSign(sign, key0, 'sha256')
197
+ const decrypted = aesDecryptGCM(encPayload, decKey, encIv, null)
133
198
 
134
- return WAProto_1.proto.Message.EventResponseMessage.decode(decrypted)
199
+ return proto.Message.decode(decrypted)
200
+
201
+ function toBinary(txt) {
202
+ return Buffer.from(txt)
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Decrypt an reaction
208
+ * @param reaction encrypted reaction
209
+ * @returns reaction message
210
+ */
211
+ function decryptReaction({ encPayload, encIv }, { reactionCreatorJid, reactionMsgId, reactionEncKey, reactionJid }) {
212
+ const sign = Buffer.concat([
213
+ toBinary(reactionMsgId),
214
+ toBinary(reactionCreatorJid),
215
+ toBinary(reactionJid),
216
+ toBinary('Enc Reaction'),
217
+ new Uint8Array([1])
218
+ ])
219
+
220
+ const key0 = hmacSign(reactionEncKey, new Uint8Array(32), 'sha256')
221
+ const decKey = hmacSign(sign, key0, 'sha256')
222
+ const decrypted = aesDecryptGCM(encPayload, decKey, encIv, null)
223
+
224
+ return proto.Message.ReactionMessage.decode(decrypted)
135
225
 
136
226
  function toBinary(txt) {
137
227
  return Buffer.from(txt)
@@ -142,17 +232,17 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
142
232
  const meId = creds.me.id
143
233
  const meLid = creds.me.lid
144
234
  const { accountSettings } = creds
145
- const chat = { id: WABinary_1.jidNormalizedUser(getChatId(message.key)) }
235
+ const chat = { id: jidNormalizedUser(getChatId(message.key)) }
146
236
  const isRealMsg = isRealMessage(message, meId)
147
237
  if (isRealMsg) {
148
238
  chat.messages = [{ message }]
149
- chat.conversationTimestamp = generics_1.toNumber(message.messageTimestamp)
239
+ chat.conversationTimestamp = toNumber(message.messageTimestamp)
150
240
  // only increment unread count if not CIPHERTEXT and from another person
151
241
  if (shouldIncrementChatUnread(message)) {
152
242
  chat.unreadCount = (chat.unreadCount || 0) + 1
153
243
  }
154
244
  }
155
- const content = messages_1.normalizeMessageContent(message.message)
245
+ const content = normalizeMessageContent(message.message)
156
246
  // unarchive chat if it's a real message, or someone reacted to our message
157
247
  // and we've the unarchive chats setting on
158
248
  if (isRealMsg || content?.reactionMessage?.key?.fromMe
@@ -163,7 +253,7 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
163
253
  const protocolMsg = content?.protocolMessage
164
254
  if (protocolMsg) {
165
255
  switch (protocolMsg.type) {
166
- case WAProto_1.proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
256
+ case proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
167
257
  const histNotification = protocolMsg.historySyncNotification
168
258
  const process = shouldProcessHistoryMsg
169
259
  const isLatest = !creds.processedHistoryMessages?.length
@@ -174,7 +264,7 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
174
264
  isLatest,
175
265
  }, 'got history notification')
176
266
  if (process) {
177
- if (histNotification.syncType !== WAProto_1.proto.HistorySync.HistorySyncType.ON_DEMAND) {
267
+ if (histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND) {
178
268
  ev.emit('creds.update', {
179
269
  processedHistoryMessages: [
180
270
  ...(creds.processedHistoryMessages || []),
@@ -182,17 +272,17 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
182
272
  ]
183
273
  })
184
274
  }
185
- const data = await history_1.downloadAndProcessHistorySyncNotification(histNotification, options)
275
+ const data = await downloadAndProcessHistorySyncNotification(histNotification, options)
186
276
  ev.emit('messaging-history.set', {
187
277
  ...data,
188
- isLatest: histNotification.syncType !== WAProto_1.proto.HistorySync.HistorySyncType.ON_DEMAND
278
+ isLatest: histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND
189
279
  ? isLatest
190
280
  : undefined,
191
281
  peerDataRequestSessionId: histNotification.peerDataRequestSessionId
192
282
  })
193
283
  }
194
284
  break
195
- case WAProto_1.proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
285
+ case proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
196
286
  const keys = protocolMsg.appStateSyncKeyShare.keys
197
287
  if (keys?.length) {
198
288
  let newAppStateSyncKeyId = ''
@@ -212,24 +302,24 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
212
302
  logger?.info({ protocolMsg }, 'recv app state sync with 0 keys')
213
303
  }
214
304
  break
215
- case WAProto_1.proto.Message.ProtocolMessage.Type.REVOKE:
305
+ case proto.Message.ProtocolMessage.Type.REVOKE:
216
306
  ev.emit('messages.update', [
217
307
  {
218
308
  key: {
219
309
  ...message.key,
220
310
  id: protocolMsg.key.id
221
311
  },
222
- update: { message: null, messageStubType: Types_1.WAMessageStubType.REVOKE, key: message.key }
312
+ update: { message: null, messageStubType: WAMessageStubType.REVOKE, key: message.key }
223
313
  }
224
314
  ])
225
315
  break
226
- case WAProto_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
316
+ case proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
227
317
  Object.assign(chat, {
228
- ephemeralSettingTimestamp: generics_1.toNumber(message.messageTimestamp),
318
+ ephemeralSettingTimestamp: toNumber(message.messageTimestamp),
229
319
  ephemeralExpiration: protocolMsg.ephemeralExpiration || null
230
320
  })
231
321
  break
232
- case WAProto_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
322
+ case proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
233
323
  const response = protocolMsg.peerDataOperationRequestResponseMessage
234
324
  if (response) {
235
325
  await laceholderResendCache?.del(response.stanzaId)
@@ -239,7 +329,7 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
239
329
  const { placeholderMessageResendResponse: retryResponse } = result
240
330
  //eslint-disable-next-line max-depth
241
331
  if (retryResponse) {
242
- const webMessageInfo = WAProto_1.proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes)
332
+ const webMessageInfo = proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes)
243
333
  // wait till another upsert event is available, don't want it to be part of the PDO response message
244
334
  setTimeout(() => {
245
335
  ev.emit('messages.upsert', {
@@ -252,7 +342,7 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
252
342
  }
253
343
  }
254
344
  break
255
- case WAProto_1.proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
345
+ case proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
256
346
  ev.emit('messages.update', [
257
347
  {
258
348
  // flip the sender / fromMe properties because they're in the perspective of the sender
@@ -264,15 +354,15 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
264
354
  }
265
355
  },
266
356
  messageTimestamp: protocolMsg.timestampMs
267
- ? Math.floor(generics_1.toNumber(protocolMsg.timestampMs) / 1000)
357
+ ? Math.floor(toNumber(protocolMsg.timestampMs) / 1000)
268
358
  : message.messageTimestamp
269
359
  }
270
360
  }
271
361
  ])
272
362
  break
273
- case WAProto_1.proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC:
363
+ case proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC:
274
364
  const encodedPayload = protocolMsg.lidMigrationMappingSyncMessage?.encodedMappingPayload
275
- const { pnToLidMappings, chatDbMigrationTimestamp } = WAProto_1.proto.LIDMigrationMappingSyncPayload.decode(encodedPayload)
365
+ const { pnToLidMappings, chatDbMigrationTimestamp } = proto.LIDMigrationMappingSyncPayload.decode(encodedPayload)
276
366
 
277
367
  logger?.debug({ pnToLidMappings, chatDbMigrationTimestamp }, 'got lid mappings and chat db migration timestamp')
278
368
 
@@ -285,10 +375,10 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
285
375
 
286
376
  await signalRepository.lidMapping.storeLIDPNMappings(pairs)
287
377
  break
288
- case WAProto_1.proto.Message.ProtocolMessage.Type.LIMIT_SHARING:
378
+ case proto.Message.ProtocolMessage.Type.LIMIT_SHARING:
289
379
  ev.emit('limit-sharing.update', {
290
380
  id: message.key.remoteJid,
291
- author: WABinary_1.areJidsSameUser(message.key.remoteJid, protocolMsg.key.remoteJid) ? WABinary_1.jidNormalizedUser(meId) : message.key.remoteJid,
381
+ author: areJidsSameUser(message.key.remoteJid, protocolMsg.key.remoteJid) ? jidNormalizedUser(meId) : message.key.remoteJid,
292
382
  action: `${protocolMsg.limitSharing.sharingLimited ? 'on' : 'off'}`,
293
383
  trigger: protocolMsg.limitSharing.trigger,
294
384
  update_time: protocolMsg.limitSharing.limitSharingSettingTimestamp
@@ -317,13 +407,13 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
317
407
  const emitGroupRequestJoin = (participant, action, method) => {
318
408
  ev.emit('group.join-request', { id: jid, author: message.participant, participant, action, method: method })
319
409
  }
320
- const participantsIncludesMe = () => participants.find(jid => WABinary_1.areJidsSameUser(meId, jid))
410
+ const participantsIncludesMe = () => participants.find(jid => areJidsSameUser(meId, jid))
321
411
  switch (message.messageStubType) {
322
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
412
+ case WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
323
413
  participants = message.messageStubParameters || []
324
414
  emitParticipantsUpdate('modify')
325
415
  break
326
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
416
+ case WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
327
417
  participants = message.messageStubParameters || [];
328
418
  emitParticipantsUpdate('leave');
329
419
  // mark the chat read only if you left the group
@@ -331,7 +421,7 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
331
421
  chat.readOnly = true;
332
422
  }
333
423
  break;
334
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
424
+ case WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
335
425
  participants = message.messageStubParameters || []
336
426
  emitParticipantsUpdate('remove')
337
427
  // mark the chat read only if you left the group
@@ -339,62 +429,62 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
339
429
  chat.readOnly = true
340
430
  }
341
431
  break
342
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD:
343
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE:
432
+ case WAMessageStubType.GROUP_PARTICIPANT_ADD:
433
+ case WAMessageStubType.GROUP_PARTICIPANT_INVITE:
344
434
  let actionGp = 'invite'
345
435
  participants = message.messageStubParameters || []
346
436
  if (participantsIncludesMe()) chat.readOnly = false;
347
437
  if (message?.key?.participant && !participants.includes(message?.key?.participant)) {
348
438
  actionGp = 'approval-invite'
349
439
  }
350
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
440
+ case WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
351
441
  participants = message.messageStubParameters || []
352
442
  if (participantsIncludesMe()) {
353
443
  chat.readOnly = false
354
444
  }
355
445
  emitParticipantsUpdate('add')
356
446
  break
357
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
358
- case Types_1.WAMessageStubType.COMMUNITY_PARTICIPANT_DEMOTE:
447
+ case WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
448
+ case WAMessageStubType.COMMUNITY_PARTICIPANT_DEMOTE:
359
449
  participants = message.messageStubParameters || []
360
450
  emitParticipantsUpdate('demote')
361
451
  break
362
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
363
- case Types_1.WAMessageStubType.COMMUNITY_PARTICIPANT_PROMOTE:
452
+ case WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
453
+ case WAMessageStubType.COMMUNITY_PARTICIPANT_PROMOTE:
364
454
  participants = message.messageStubParameters || []
365
455
  emitParticipantsUpdate('promote')
366
456
  break
367
- case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
457
+ case WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
368
458
  const announceValue = message.messageStubParameters?.[0]
369
459
  emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' })
370
460
  break
371
- case Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT:
461
+ case WAMessageStubType.GROUP_CHANGE_RESTRICT:
372
462
  const restrictValue = message.messageStubParameters?.[0]
373
463
  emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' })
374
464
  break
375
- case Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT:
465
+ case WAMessageStubType.GROUP_CHANGE_SUBJECT:
376
466
  const name = message.messageStubParameters?.[0]
377
467
  chat.name = name
378
468
  emitGroupUpdate({ subject: name })
379
469
  break
380
- case Types_1.WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
470
+ case WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
381
471
  const description = message.messageStubParameters?.[0]
382
472
  chat.description = description
383
473
  emitGroupUpdate({ desc: description })
384
474
  break
385
- case Types_1.WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
475
+ case WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
386
476
  const code = message.messageStubParameters?.[0]
387
477
  emitGroupUpdate({ inviteCode: code })
388
478
  break
389
- case Types_1.WAMessageStubType.GROUP_MEMBER_ADD_MODE:
479
+ case WAMessageStubType.GROUP_MEMBER_ADD_MODE:
390
480
  const memberAddValue = message.messageStubParameters?.[0]
391
481
  emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' })
392
482
  break
393
- case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
483
+ case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
394
484
  const approvalMode = message.messageStubParameters?.[0]
395
485
  emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' })
396
486
  break
397
- case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
487
+ case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
398
488
  const participant = message.messageStubParameters?.[0]
399
489
  const action = message.messageStubParameters?.[1]
400
490
  const method = message.messageStubParameters?.[2]
@@ -403,50 +493,50 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
403
493
  }
404
494
  }
405
495
  else if (content?.pollUpdateMessage) {
406
- const pollUpdate = content.pollUpdateMessage
496
+ const pollUpdate = content.pollUpdateMessage
407
497
  const creationMsgKey = pollUpdate.pollCreationMessageKey
408
-
498
+
409
499
  // we need to fetch the poll creation message to get the poll enc key
410
500
  const pollMsg = await getMessage(creationMsgKey)
411
501
  if (pollMsg) {
412
- try {
413
- const meLidNormalised = WABinary_1.jidNormalizedUser(meLid)
414
- const getDevice = messages_1.getDevice(creationMsgKey.id)
415
- const pollCreationFromMe = getDevice === 'baileys' ? true : false
416
- const pollEncKey = pollMsg.messageContextInfo?.messageSecret
417
- const voterJid = generics_1.getKeyAuthor(message.key, meLidNormalised)
502
+ try {
503
+ const meLidNormalised = jidNormalizedUser(meLid)
504
+ const deviceType = getDevice(creationMsgKey.id)
505
+ const pollCreationFromMe = deviceType === 'baileys' ? true : false
506
+ const pollEncKey = pollMsg.messageContextInfo?.messageSecret
507
+ const voterJid = getKeyAuthor(message.key, meLidNormalised)
418
508
 
419
- let pollCreatorJid = generics_1.getKeyAuthor(creationMsgKey, meLidNormalised)
509
+ let pollCreatorJid = getKeyAuthor(creationMsgKey, meLidNormalised)
420
510
 
421
- if (pollCreationFromMe) {
422
- pollCreatorJid = meLidNormalised
423
- }
511
+ if (pollCreationFromMe) {
512
+ pollCreatorJid = meLidNormalised
513
+ }
424
514
 
425
- if (!pollEncKey) {
426
- logger?.warn({ vote: pollUpdate.vote, creationMsgKey }, 'poll creation: missing messageSecret for decryption')
427
- } else {
428
- const voteMsg = decryptPollVote(pollUpdate.vote, {
429
- pollEncKey,
430
- pollCreatorJid,
431
- pollMsgId: creationMsgKey.id,
432
- voterJid
433
- })
515
+ if (!pollEncKey) {
516
+ logger?.warn({ vote: pollUpdate.vote, creationMsgKey }, 'poll creation: missing messageSecret for decryption')
517
+ } else {
518
+ const voteMsg = decryptPollVote(pollUpdate.vote, {
519
+ pollEncKey,
520
+ pollCreatorJid,
521
+ pollMsgId: creationMsgKey.id,
522
+ voterJid
523
+ })
434
524
 
435
- ev.emit('messages.update', [
436
- {
437
- key: creationMsgKey,
438
- update: {
439
- pollUpdates: [
440
- {
441
- pollUpdateMessageKey: message.key,
442
- vote: voteMsg,
443
- senderTimestampMs: content.pollUpdateMessage.senderTimestampMs.toNumber()
444
- }
445
- ]
446
- }
525
+ ev.emit('messages.update', [
526
+ {
527
+ key: creationMsgKey,
528
+ update: {
529
+ pollUpdates: [
530
+ {
531
+ pollUpdateMessageKey: message.key,
532
+ vote: voteMsg,
533
+ senderTimestampMs: content.pollUpdateMessage.senderTimestampMs.toNumber()
534
+ }
535
+ ]
447
536
  }
448
- ])
449
- }
537
+ }
538
+ ])
539
+ }
450
540
  }
451
541
  catch (err) {
452
542
  logger?.warn({ err, creationMsgKey }, 'failed to decrypt poll vote')
@@ -456,45 +546,98 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
456
546
  logger?.warn({ creationMsgKey }, 'poll creation message not found, cannot decrypt update')
457
547
  }
458
548
  }
549
+ else if (content?.secretEncryptedMessage) {
550
+ const encEventEdit = content.secretEncryptedMessage
551
+ const creationMsgKey = encEventEdit.targetMessageKey
552
+
553
+ if (proto.Message.SecretEncryptedMessage.SecretEncType[encEventEdit.secretEncType] !== 'EVENT_EDIT') return
554
+
555
+ // we need to fetch the event creation message to get the event enc key
556
+ const eventMsg = await getMessage(creationMsgKey)
557
+ if (eventMsg) {
558
+ try {
559
+ const meLidNormalised = jidNormalizedUser(meLid)
560
+ const eventCreatorJid = getKeyAuthor(message.key, meLidNormalised)
561
+ const responderJid = getKeyAuthor(message.key, meLidNormalised)
562
+ const eventEncKey = eventMsg.messageContextInfo?.messageSecret
563
+
564
+ if (!eventEncKey) {
565
+ logger?.warn({ encEventEdit, creationMsgKey }, 'event edit: missing messageSecret for decryption')
566
+ } else {
567
+ const responseMsg = decryptEventEdit(encEventEdit, {
568
+ eventEncKey,
569
+ eventCreatorJid,
570
+ eventMsgId: creationMsgKey.id,
571
+ responderJid
572
+ })
573
+ const content = normalizeMessageContent(responseMsg)
574
+ const protocolMsg = content?.protocolMessage
575
+
576
+ ev.emit('messages.update', [
577
+ {
578
+ key: { ...message.key, id: protocolMsg.key?.id },
579
+ update: {
580
+ message: {
581
+ messageContextInfo: responseMsg.messageContextInfo,
582
+ editedMessage: {
583
+ message: protocolMsg.editedMessage
584
+ }
585
+ },
586
+ messageTimestamp: protocolMsg.timestampMs
587
+ ? Math.floor(toNumber(protocolMsg.timestampMs) / 1000)
588
+ : message.messageTimestamp
589
+ }
590
+ }
591
+ ])
592
+ }
593
+ }
594
+ catch (err) {
595
+ logger?.warn({ err, creationMsgKey, encEventEdit }, 'failed to decrypt event edit')
596
+ }
597
+ }
598
+ else {
599
+ logger?.warn({ encEventEdit, creationMsgKey }, 'event creation message not found, cannot decrypt update')
600
+ }
601
+ }
459
602
  else if (content?.encEventResponseMessage) {
460
603
  const encEventResponse = content.encEventResponseMessage
461
604
  const creationMsgKey = encEventResponse.eventCreationMessageKey
462
-
605
+
463
606
  // we need to fetch the event creation message to get the event enc key
464
607
  const eventMsg = await getMessage(creationMsgKey)
465
608
  if (eventMsg) {
466
- try {
467
- const meIdNormalised = WABinary_1.jidNormalizedUser(meLid)
468
- const eventCreatorJid = generics_1.getKeyAuthor(creationMsgKey, meIdNormalised)
469
- const responderJid = generics_1.getKeyAuthor(message.key, meIdNormalised)
470
- const eventEncKey = eventMsg.messageContextInfo?.messageSecret
471
-
472
- if (!eventEncKey) {
473
- logger?.warn({ encEventResponse, creationMsgKey }, 'event response: missing messageSecret for decryption')
474
- } else {
475
- const responseMsg = decryptEventResponse(encEventResponse, {
476
- eventEncKey,
477
- eventCreatorJid,
478
- eventMsgId: creationMsgKey.id,
479
- responderJid
480
- })
481
-
482
- const eventResponse = {
483
- eventResponseMessageKey: message.key,
484
- senderTimestampMs: responseMsg.timestampMs,
485
- response: responseMsg
486
- }
487
-
488
- ev.emit('messages.update', [
489
- {
490
- key: creationMsgKey,
491
- update: {
492
- eventResponses: [eventResponse]
493
- }
494
- }
495
- ])
496
- }
497
- }
609
+ try {
610
+ const meLidNormalised = jidNormalizedUser(meLid)
611
+ const eventCreatorJid = getKeyAuthor(creationMsgKey, meLidNormalised)
612
+ const responderJid = getKeyAuthor(message.key, meLidNormalised)
613
+ const eventEncKey = eventMsg.messageContextInfo?.messageSecret
614
+
615
+ if (!eventEncKey) {
616
+ logger?.warn({ encEventResponse, creationMsgKey }, 'event response: missing messageSecret for decryption')
617
+ } else {
618
+ const responseMsg = decryptEventResponse(encEventResponse, {
619
+ eventEncKey,
620
+ eventCreatorJid,
621
+ eventMsgId: creationMsgKey.id,
622
+ responderJid
623
+ })
624
+
625
+ const eventResponse = {
626
+ eventResponseMessageKey: message.key,
627
+ senderTimestampMs: responseMsg.timestampMs,
628
+ response: responseMsg
629
+ }
630
+
631
+ ev.emit('messages.update', [
632
+ {
633
+ key: creationMsgKey,
634
+ update: {
635
+ eventResponses: [eventResponse]
636
+ }
637
+ }
638
+ ])
639
+ }
640
+ }
498
641
  catch (err) {
499
642
  logger?.warn({ err, creationMsgKey, encEventResponse }, 'failed to decrypt event response')
500
643
  }
@@ -503,6 +646,94 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
503
646
  logger?.warn({ encEventResponse, creationMsgKey }, 'event creation message not found, cannot decrypt update')
504
647
  }
505
648
  }
649
+ else if (content?.encCommentMessage) {
650
+ const encComment = content.encCommentMessage
651
+ const creationMsgKey = encComment.targetMessageKey
652
+
653
+ // we need to fetch the message to get the reaction enc key
654
+ const commentMsg = await getMessage(creationMsgKey)
655
+ if (commentMsg) {
656
+ try {
657
+ const meLidNormalised = jidNormalizedUser(meLid)
658
+ const commentCreatorJid = creationMsgKey.participant ? creationMsgKey.participant : message.key?.participant ? message.key.participant : meLidNormalised
659
+ const commentJid = message.key?.participant ? message.key.participant : creationMsgKey.participant ? creationMsgKey.participant : meLidNormalised
660
+ const commentEncKey = commentMsg.messageContextInfo?.messageSecret
661
+
662
+ if (!commentEncKey) {
663
+ logger?.warn({ encComment, creationMsgKey }, 'comment message: missing messageSecret for decryption')
664
+ } else {
665
+ const responseMsg = decryptComment(encComment, {
666
+ commentEncKey,
667
+ commentCreatorJid,
668
+ commentMsgId: creationMsgKey.id,
669
+ commentJid
670
+ })
671
+
672
+ ev.emit('messages.upsert', {
673
+ messages: [{
674
+ key: message.key,
675
+ message: responseMsg
676
+ }],
677
+ type: 'append'
678
+ })
679
+ }
680
+ }
681
+ catch (err) {
682
+ logger?.warn({ err, creationMsgKey, encComment }, 'failed to decrypt comment message')
683
+ }
684
+ }
685
+ else {
686
+ logger?.warn({ encComment, creationMsgKey }, 'creation message not found, cannot decrypt')
687
+ }
688
+ }
689
+ else if (content?.encReactionMessage) {
690
+ const encReaction = content.encReactionMessage
691
+ const creationMsgKey = encReaction.targetMessageKey
692
+
693
+ // we need to fetch the message to get the reaction enc key
694
+ const reactMsg = await getMessage(creationMsgKey)
695
+ if (reactMsg) {
696
+ try {
697
+ const meLidNormalised = jidNormalizedUser(meLid)
698
+ const reactionCreatorJid = creationMsgKey.participant ? creationMsgKey.participant : message.key?.participant ? message.key.participant : meLidNormalised
699
+ const reactionJid = message.key?.participant ? message.key.participant : creationMsgKey.participant ? creationMsgKey.participant : meLidNormalised
700
+ const reactionEncKey = reactMsg.messageContextInfo?.messageSecret
701
+
702
+ if (!reactionEncKey) {
703
+ logger?.warn({ encReaction, creationMsgKey }, 'reaction: missing messageSecret for decryption')
704
+ } else {
705
+ const responseMsg = decryptReaction(encReaction, {
706
+ reactionEncKey,
707
+ reactionCreatorJid,
708
+ reactionMsgId: creationMsgKey.id,
709
+ reactionJid
710
+ })
711
+
712
+ const Reaction = {
713
+ key: message.key,
714
+ message: {
715
+ reactionMessage: {
716
+ key: creationMsgKey,
717
+ text: responseMsg.text,
718
+ senderTimestampMs: responseMsg.senderTimestampMs
719
+ }
720
+ }
721
+ }
722
+
723
+ ev.emit('messages.upsert', {
724
+ messages: [Reaction],
725
+ type: 'append'
726
+ })
727
+ }
728
+ }
729
+ catch (err) {
730
+ logger?.warn({ err, creationMsgKey, encReaction }, 'failed to decrypt reaction')
731
+ }
732
+ }
733
+ else {
734
+ logger?.warn({ encReaction, creationMsgKey }, 'creation message not found, cannot decrypt')
735
+ }
736
+ }
506
737
  if (Object.keys(chat).length > 1) {
507
738
  ev.emit('chats.update', [chat])
508
739
  }
@@ -514,6 +745,9 @@ module.exports = {
514
745
  shouldIncrementChatUnread,
515
746
  getChatId,
516
747
  decryptPollVote,
748
+ decryptEventEdit,
517
749
  decryptEventResponse,
750
+ decryptComment,
751
+ decryptReaction,
518
752
  processMessage
519
753
  }