@nexustechpro/baileys 2.0.2 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +924 -1299
- package/lib/Defaults/baileys-version.json +6 -2
- package/lib/Defaults/index.js +172 -172
- package/lib/Signal/libsignal.js +380 -292
- package/lib/Signal/lid-mapping.js +264 -171
- package/lib/Socket/Client/index.js +2 -2
- package/lib/Socket/Client/types.js +10 -10
- package/lib/Socket/Client/websocket.js +45 -310
- package/lib/Socket/business.js +375 -375
- package/lib/Socket/chats.js +909 -963
- package/lib/Socket/communities.js +430 -430
- package/lib/Socket/groups.js +342 -342
- package/lib/Socket/index.js +22 -22
- package/lib/Socket/messages-recv.js +777 -743
- package/lib/Socket/messages-send.js +295 -305
- package/lib/Socket/mex.js +50 -50
- package/lib/Socket/newsletter.js +148 -148
- package/lib/Socket/nexus-handler.js +75 -261
- package/lib/Socket/socket.js +709 -1201
- package/lib/Store/index.js +5 -5
- package/lib/Store/make-cache-manager-store.js +81 -81
- package/lib/Store/make-in-memory-store.js +416 -416
- package/lib/Store/make-ordered-dictionary.js +81 -81
- package/lib/Store/object-repository.js +30 -30
- package/lib/Types/Auth.js +1 -1
- package/lib/Types/Bussines.js +1 -1
- package/lib/Types/Call.js +1 -1
- package/lib/Types/Chat.js +7 -7
- package/lib/Types/Contact.js +1 -1
- package/lib/Types/Events.js +1 -1
- package/lib/Types/GroupMetadata.js +1 -1
- package/lib/Types/Label.js +24 -24
- package/lib/Types/LabelAssociation.js +6 -6
- package/lib/Types/Message.js +10 -10
- package/lib/Types/Newsletter.js +28 -28
- package/lib/Types/Product.js +1 -1
- package/lib/Types/Signal.js +1 -1
- package/lib/Types/Socket.js +2 -2
- package/lib/Types/State.js +12 -12
- package/lib/Types/USync.js +1 -1
- package/lib/Types/index.js +25 -25
- package/lib/Utils/auth-utils.js +264 -256
- package/lib/Utils/baileys-event-stream.js +55 -55
- package/lib/Utils/browser-utils.js +27 -27
- package/lib/Utils/business.js +228 -230
- package/lib/Utils/chat-utils.js +694 -764
- package/lib/Utils/crypto.js +109 -135
- package/lib/Utils/decode-wa-message.js +310 -314
- package/lib/Utils/event-buffer.js +547 -547
- package/lib/Utils/generics.js +297 -297
- package/lib/Utils/history.js +91 -83
- package/lib/Utils/index.js +21 -20
- package/lib/Utils/key-store.js +17 -0
- package/lib/Utils/link-preview.js +97 -98
- package/lib/Utils/logger.js +2 -2
- package/lib/Utils/lt-hash.js +47 -47
- package/lib/Utils/make-mutex.js +39 -39
- package/lib/Utils/message-retry-manager.js +148 -148
- package/lib/Utils/messages-media.js +534 -534
- package/lib/Utils/messages.js +705 -705
- package/lib/Utils/noise-handler.js +255 -255
- package/lib/Utils/pre-key-manager.js +105 -105
- package/lib/Utils/process-message.js +412 -412
- package/lib/Utils/signal.js +160 -158
- package/lib/Utils/use-multi-file-auth-state.js +120 -120
- package/lib/Utils/validate-connection.js +194 -194
- package/lib/WABinary/constants.js +1300 -1300
- package/lib/WABinary/decode.js +237 -237
- package/lib/WABinary/encode.js +232 -232
- package/lib/WABinary/generic-utils.js +252 -211
- package/lib/WABinary/index.js +5 -5
- package/lib/WABinary/jid-utils.js +279 -95
- package/lib/WABinary/types.js +1 -1
- package/lib/WAM/BinaryInfo.js +9 -9
- package/lib/WAM/constants.js +22852 -22852
- package/lib/WAM/encode.js +149 -149
- package/lib/WAM/index.js +3 -3
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -28
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -53
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -26
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -37
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -28
- package/lib/WAUSync/Protocols/index.js +4 -4
- package/lib/WAUSync/USyncQuery.js +93 -93
- package/lib/WAUSync/USyncUser.js +22 -22
- package/lib/WAUSync/index.js +3 -3
- package/lib/index.js +66 -66
- package/package.json +171 -144
- package/lib/Signal/Group/ciphertext-message.js +0 -12
- package/lib/Signal/Group/group-session-builder.js +0 -30
- package/lib/Signal/Group/group_cipher.js +0 -100
- package/lib/Signal/Group/index.js +0 -12
- package/lib/Signal/Group/keyhelper.js +0 -18
- package/lib/Signal/Group/sender-chain-key.js +0 -26
- package/lib/Signal/Group/sender-key-distribution-message.js +0 -63
- package/lib/Signal/Group/sender-key-message.js +0 -66
- package/lib/Signal/Group/sender-key-name.js +0 -48
- package/lib/Signal/Group/sender-key-record.js +0 -41
- package/lib/Signal/Group/sender-key-state.js +0 -84
- package/lib/Signal/Group/sender-message-key.js +0 -26
|
@@ -1,314 +1,310 @@
|
|
|
1
|
-
import { Boom } from "@hapi/boom"
|
|
2
|
-
import { proto } from "../../WAProto/index.js"
|
|
3
|
-
import {
|
|
4
|
-
areJidsSameUser,
|
|
5
|
-
isHostedLidUser,
|
|
6
|
-
isHostedPnUser,
|
|
7
|
-
isJidBroadcast,
|
|
8
|
-
isJidGroup,
|
|
9
|
-
isJidMetaAI,
|
|
10
|
-
isJidNewsletter,
|
|
11
|
-
isJidStatusBroadcast,
|
|
12
|
-
isLidUser,
|
|
13
|
-
isPnUser,
|
|
14
|
-
// transferDevice
|
|
15
|
-
} from "../WABinary/index.js"
|
|
16
|
-
import { unpadRandomMax16 } from "./generics.js"
|
|
17
|
-
export const getDecryptionJid = async (sender, repository, logger) => {
|
|
18
|
-
// Skip LID mapping for newsletters, status broadcasts, and meta AI - they don't use it
|
|
19
|
-
if (isJidNewsletter(sender) || isJidStatusBroadcast(sender) || isJidMetaAI(sender)) {
|
|
20
|
-
return sender
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (isLidUser(sender) || isHostedLidUser(sender)) {
|
|
24
|
-
return sender
|
|
25
|
-
}
|
|
26
|
-
const mapped = await repository.lidMapping.getLIDForPN(sender)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
logger.
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
msgType = "
|
|
134
|
-
author =
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
author,
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
case "
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
if (
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
|
|
298
|
-
fullMessage.messageStubParameters = [errorMessage]
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
// if nothing was found to decrypt
|
|
303
|
-
if (!decryptables) {
|
|
304
|
-
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
|
|
305
|
-
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT]
|
|
306
|
-
}
|
|
307
|
-
},
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
const errorMessage = error?.message || error?.toString() || ""
|
|
312
|
-
return DECRYPTION_RETRY_CONFIG.sessionRecordErrors.some((errorPattern) => errorMessage.includes(errorPattern))
|
|
313
|
-
}
|
|
314
|
-
//# sourceMappingURL=decode-wa-message.js.map
|
|
1
|
+
import { Boom } from "@hapi/boom"
|
|
2
|
+
import { proto } from "../../WAProto/index.js"
|
|
3
|
+
import {
|
|
4
|
+
areJidsSameUser,
|
|
5
|
+
isHostedLidUser,
|
|
6
|
+
isHostedPnUser,
|
|
7
|
+
isJidBroadcast,
|
|
8
|
+
isJidGroup,
|
|
9
|
+
isJidMetaAI,
|
|
10
|
+
isJidNewsletter,
|
|
11
|
+
isJidStatusBroadcast,
|
|
12
|
+
isLidUser,
|
|
13
|
+
isPnUser,
|
|
14
|
+
// transferDevice
|
|
15
|
+
} from "../WABinary/index.js"
|
|
16
|
+
import { unpadRandomMax16 } from "./generics.js"
|
|
17
|
+
export const getDecryptionJid = async (sender, repository, logger) => {
|
|
18
|
+
// Skip LID mapping for newsletters, status broadcasts, and meta AI - they don't use it
|
|
19
|
+
if (isJidNewsletter(sender) || isJidStatusBroadcast(sender) || isJidMetaAI(sender)) {
|
|
20
|
+
return sender
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (isLidUser(sender) || isHostedLidUser(sender)) {
|
|
24
|
+
return sender
|
|
25
|
+
}
|
|
26
|
+
const mapped = await repository.lidMapping.getLIDForPN(sender)
|
|
27
|
+
return mapped || sender
|
|
28
|
+
}
|
|
29
|
+
const storeMappingFromEnvelope = async (stanza, sender, repository, decryptionJid, logger, meId, meLid) => {
|
|
30
|
+
const { senderAlt } = extractAddressingContext(stanza)
|
|
31
|
+
if (!senderAlt) return
|
|
32
|
+
|
|
33
|
+
// Case 1: PN-addressed message — sender is PN, senderAlt is LID
|
|
34
|
+
if (isLidUser(senderAlt) && isPnUser(sender) && decryptionJid === sender) {
|
|
35
|
+
if (areJidsSameUser(sender, meId) || areJidsSameUser(senderAlt, meLid)) return // never remap own identity
|
|
36
|
+
try {
|
|
37
|
+
await repository.lidMapping.storeLIDPNMappings([{ lid: senderAlt, pn: sender }])
|
|
38
|
+
repository.migrateSession(sender, senderAlt).catch(err => logger?.warn?.({ sender, senderAlt, err }, 'Failed to migrate session (PN→LID)'))
|
|
39
|
+
logger.debug({ sender, senderAlt }, 'Stored LID mapping from envelope (PN→LID)')
|
|
40
|
+
} catch (error) {
|
|
41
|
+
logger.warn({ sender, senderAlt, error }, 'Failed to store LID mapping (PN→LID)')
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Case 2: LID-addressed message — sender is LID, senderAlt is PN
|
|
45
|
+
else if (isPnUser(senderAlt) && isLidUser(sender)) {
|
|
46
|
+
if (areJidsSameUser(sender, meLid) || areJidsSameUser(senderAlt, meId)) return // never remap own identity
|
|
47
|
+
try {
|
|
48
|
+
await repository.lidMapping.storeLIDPNMappings([{ lid: sender, pn: senderAlt }])
|
|
49
|
+
repository.migrateSession(senderAlt, sender).catch(err => logger?.warn?.({ sender, senderAlt, err }, 'Failed to migrate session (LID→PN)'))
|
|
50
|
+
logger.debug({ sender, senderAlt }, 'Stored LID mapping from envelope (LID→PN)')
|
|
51
|
+
} catch (error) {
|
|
52
|
+
logger.warn({ sender, senderAlt, error }, 'Failed to store LID mapping (LID→PN)')
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export const NO_MESSAGE_FOUND_ERROR_TEXT = "Message absent from node"
|
|
57
|
+
export const MISSING_KEYS_ERROR_TEXT = "Key used already or never filled"
|
|
58
|
+
// Retry configuration for failed decryption
|
|
59
|
+
export const DECRYPTION_RETRY_CONFIG = {
|
|
60
|
+
maxRetries: 3,
|
|
61
|
+
baseDelayMs: 100,
|
|
62
|
+
sessionRecordErrors: ["No session record", "SessionError: No session record"],
|
|
63
|
+
}
|
|
64
|
+
export const NACK_REASONS = {
|
|
65
|
+
ParsingError: 487,
|
|
66
|
+
UnrecognizedStanza: 488,
|
|
67
|
+
UnrecognizedStanzaClass: 489,
|
|
68
|
+
UnrecognizedStanzaType: 490,
|
|
69
|
+
InvalidProtobuf: 491,
|
|
70
|
+
InvalidHostedCompanionStanza: 493,
|
|
71
|
+
MissingMessageSecret: 495,
|
|
72
|
+
SignalErrorOldCounter: 496,
|
|
73
|
+
MessageDeletedOnPeer: 499,
|
|
74
|
+
UnhandledError: 500,
|
|
75
|
+
UnsupportedAdminRevoke: 550,
|
|
76
|
+
UnsupportedLIDGroup: 551,
|
|
77
|
+
DBOperationFailed: 552,
|
|
78
|
+
}
|
|
79
|
+
export const extractAddressingContext = (stanza) => {
|
|
80
|
+
let senderAlt
|
|
81
|
+
let recipientAlt
|
|
82
|
+
const sender = stanza.attrs.participant || stanza.attrs.from
|
|
83
|
+
const addressingMode = stanza.attrs.addressing_mode || (sender?.endsWith("lid") ? "lid" : "pn")
|
|
84
|
+
if (addressingMode === "lid") {
|
|
85
|
+
// Message is LID-addressed: sender is LID, extract corresponding PN
|
|
86
|
+
// without device data
|
|
87
|
+
senderAlt = stanza.attrs.participant_pn || stanza.attrs.sender_pn || stanza.attrs.peer_recipient_pn
|
|
88
|
+
recipientAlt = stanza.attrs.recipient_pn
|
|
89
|
+
// with device data
|
|
90
|
+
//if (sender && senderAlt) senderAlt = transferDevice(sender, senderAlt)
|
|
91
|
+
} else {
|
|
92
|
+
// Message is PN-addressed: sender is PN, extract corresponding LID
|
|
93
|
+
// without device data
|
|
94
|
+
senderAlt = stanza.attrs.participant_lid || stanza.attrs.sender_lid || stanza.attrs.peer_recipient_lid
|
|
95
|
+
recipientAlt = stanza.attrs.recipient_lid
|
|
96
|
+
//with device data
|
|
97
|
+
//if (sender && senderAlt) senderAlt = transferDevice(sender, senderAlt)
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
addressingMode,
|
|
101
|
+
senderAlt,
|
|
102
|
+
recipientAlt,
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Decode the received node as a message.
|
|
107
|
+
* @note this will only parse the message, not decrypt it
|
|
108
|
+
*/
|
|
109
|
+
export function decodeMessageNode(stanza, meId, meLid) {
|
|
110
|
+
let msgType
|
|
111
|
+
let chatId
|
|
112
|
+
let author
|
|
113
|
+
let fromMe = false
|
|
114
|
+
const msgId = stanza.attrs.id
|
|
115
|
+
const from = stanza.attrs.from
|
|
116
|
+
const participant = stanza.attrs.participant
|
|
117
|
+
const recipient = stanza.attrs.recipient
|
|
118
|
+
const addressingContext = extractAddressingContext(stanza)
|
|
119
|
+
const isMe = (jid) => areJidsSameUser(jid, meId)
|
|
120
|
+
const isMeLid = (jid) => areJidsSameUser(jid, meLid)
|
|
121
|
+
if (isPnUser(from) || isLidUser(from) || isHostedLidUser(from) || isHostedPnUser(from)) {
|
|
122
|
+
if (recipient && !isJidMetaAI(recipient)) {
|
|
123
|
+
if (!isMe(from) && !isMeLid(from)) {
|
|
124
|
+
throw new Boom("receipient present, but msg not from me", { data: stanza })
|
|
125
|
+
}
|
|
126
|
+
if (isMe(from) || isMeLid(from)) {
|
|
127
|
+
fromMe = true
|
|
128
|
+
}
|
|
129
|
+
chatId = recipient
|
|
130
|
+
} else {
|
|
131
|
+
chatId = from
|
|
132
|
+
}
|
|
133
|
+
msgType = "chat"
|
|
134
|
+
author = from
|
|
135
|
+
} else if (isJidGroup(from)) {
|
|
136
|
+
if (!participant) {
|
|
137
|
+
throw new Boom("No participant in group message")
|
|
138
|
+
}
|
|
139
|
+
if (isMe(participant) || isMeLid(participant)) {
|
|
140
|
+
fromMe = true
|
|
141
|
+
}
|
|
142
|
+
msgType = "group"
|
|
143
|
+
author = participant
|
|
144
|
+
chatId = from
|
|
145
|
+
} else if (isJidBroadcast(from)) {
|
|
146
|
+
if (!participant) {
|
|
147
|
+
throw new Boom("No participant in group message")
|
|
148
|
+
}
|
|
149
|
+
const isParticipantMe = isMe(participant)
|
|
150
|
+
if (isJidStatusBroadcast(from)) {
|
|
151
|
+
msgType = isParticipantMe ? "direct_peer_status" : "other_status"
|
|
152
|
+
} else {
|
|
153
|
+
msgType = isParticipantMe ? "peer_broadcast" : "other_broadcast"
|
|
154
|
+
}
|
|
155
|
+
fromMe = isParticipantMe
|
|
156
|
+
chatId = from
|
|
157
|
+
author = participant
|
|
158
|
+
} else if (isJidNewsletter(from)) {
|
|
159
|
+
msgType = "newsletter"
|
|
160
|
+
chatId = from
|
|
161
|
+
author = from
|
|
162
|
+
if (isMe(from) || isMeLid(from)) {
|
|
163
|
+
fromMe = true
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
throw new Boom("Unknown message type", { data: stanza })
|
|
167
|
+
}
|
|
168
|
+
const pushname = stanza?.attrs?.notify
|
|
169
|
+
const key = {
|
|
170
|
+
remoteJid: chatId,
|
|
171
|
+
remoteJidAlt: !isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
|
|
172
|
+
fromMe,
|
|
173
|
+
id: msgId,
|
|
174
|
+
participant,
|
|
175
|
+
participantAlt: isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
|
|
176
|
+
addressingMode: addressingContext.addressingMode,
|
|
177
|
+
...(msgType === "newsletter" && stanza.attrs.server_id ? { server_id: stanza.attrs.server_id } : {}),
|
|
178
|
+
}
|
|
179
|
+
const fullMessage = {
|
|
180
|
+
key,
|
|
181
|
+
messageTimestamp: +stanza.attrs.t,
|
|
182
|
+
pushName: pushname,
|
|
183
|
+
broadcast: isJidBroadcast(from),
|
|
184
|
+
}
|
|
185
|
+
if (key.fromMe) {
|
|
186
|
+
fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
fullMessage,
|
|
190
|
+
author,
|
|
191
|
+
sender: msgType === "chat" ? author : chatId,
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
export const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
195
|
+
const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid)
|
|
196
|
+
return {
|
|
197
|
+
fullMessage,
|
|
198
|
+
category: stanza.attrs.category,
|
|
199
|
+
author,
|
|
200
|
+
async decrypt() {
|
|
201
|
+
let decryptables = 0
|
|
202
|
+
if (Array.isArray(stanza.content)) {
|
|
203
|
+
for (const { tag, attrs, content } of stanza.content) {
|
|
204
|
+
if (tag === "verified_name" && content instanceof Uint8Array) {
|
|
205
|
+
const cert = proto.VerifiedNameCertificate.decode(content)
|
|
206
|
+
const details = proto.VerifiedNameCertificate.Details.decode(cert.details)
|
|
207
|
+
fullMessage.verifiedBizName = details.verifiedName
|
|
208
|
+
}
|
|
209
|
+
if (tag === "unavailable" && attrs.type === "view_once") {
|
|
210
|
+
fullMessage.key.isViewOnce = true // TODO: remove from here and add a STUB TYPE
|
|
211
|
+
}
|
|
212
|
+
if (tag !== "enc" && tag !== "plaintext") {
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
if (!(content instanceof Uint8Array)) {
|
|
216
|
+
continue
|
|
217
|
+
}
|
|
218
|
+
decryptables += 1
|
|
219
|
+
let msgBuffer
|
|
220
|
+
const decryptionJid = await getDecryptionJid(author, repository, logger)
|
|
221
|
+
let decryptionAltJid = null
|
|
222
|
+
const { senderAlt } = extractAddressingContext(stanza)
|
|
223
|
+
if (senderAlt) {
|
|
224
|
+
decryptionAltJid = await getDecryptionJid(senderAlt, repository, logger)
|
|
225
|
+
}
|
|
226
|
+
if (tag !== 'plaintext') {
|
|
227
|
+
storeMappingFromEnvelope(stanza, author, repository, decryptionJid, logger, meId, meLid).catch(err => logger?.warn?.({ err }, 'storeMappingFromEnvelope failed'))
|
|
228
|
+
}
|
|
229
|
+
try {
|
|
230
|
+
const e2eType = tag === "plaintext" ? "plaintext" : attrs.type
|
|
231
|
+
switch (e2eType) {
|
|
232
|
+
case "skmsg":
|
|
233
|
+
msgBuffer = await repository.decryptGroupMessage({
|
|
234
|
+
group: sender,
|
|
235
|
+
authorJid: author,
|
|
236
|
+
msg: content,
|
|
237
|
+
})
|
|
238
|
+
break
|
|
239
|
+
case "pkmsg":
|
|
240
|
+
case "msg":
|
|
241
|
+
try {
|
|
242
|
+
msgBuffer = await repository.decryptMessage({ jid: decryptionJid, type: e2eType, ciphertext: content })
|
|
243
|
+
} catch (decryptErr) {
|
|
244
|
+
const errMsg = decryptErr?.message || decryptErr?.toString() || ''
|
|
245
|
+
if (errMsg.includes('InvalidMessage') && e2eType === 'msg') {
|
|
246
|
+
logger?.debug?.({ jid: decryptionJid }, '[Signal] InvalidMessage — stale session, deleting')
|
|
247
|
+
try {
|
|
248
|
+
const toDelete = [decryptionJid]
|
|
249
|
+
await repository.deleteSession(toDelete)
|
|
250
|
+
} catch { }
|
|
251
|
+
}
|
|
252
|
+
const cleanError = new Error(errMsg || 'Decryption failed')
|
|
253
|
+
cleanError.stack = decryptErr?.stack
|
|
254
|
+
throw cleanError
|
|
255
|
+
}
|
|
256
|
+
break
|
|
257
|
+
case "plaintext":
|
|
258
|
+
msgBuffer = content
|
|
259
|
+
break
|
|
260
|
+
default:
|
|
261
|
+
throw new Error(`Unknown e2e type: ${e2eType}`)
|
|
262
|
+
}
|
|
263
|
+
let msg = proto.Message.decode(e2eType !== "plaintext" ? unpadRandomMax16(msgBuffer) : msgBuffer)
|
|
264
|
+
msg = msg.deviceSentMessage?.message || msg
|
|
265
|
+
if (msg.senderKeyDistributionMessage) {
|
|
266
|
+
//eslint-disable-next-line max-depth
|
|
267
|
+
try {
|
|
268
|
+
await repository.processSenderKeyDistributionMessage({
|
|
269
|
+
authorJid: author,
|
|
270
|
+
item: msg.senderKeyDistributionMessage,
|
|
271
|
+
})
|
|
272
|
+
} catch (err) {
|
|
273
|
+
logger.error({ key: fullMessage.key, err }, "failed to process sender key distribution message")
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (fullMessage.message) {
|
|
277
|
+
Object.assign(fullMessage.message, msg)
|
|
278
|
+
} else {
|
|
279
|
+
fullMessage.message = msg
|
|
280
|
+
}
|
|
281
|
+
const viewOnceInner =
|
|
282
|
+
msg?.viewOnceMessage?.message ||
|
|
283
|
+
msg?.viewOnceMessageV2?.message ||
|
|
284
|
+
msg?.viewOnceMessageV2Extension?.message
|
|
285
|
+
if (
|
|
286
|
+
viewOnceInner?.imageMessage?.viewOnce ||
|
|
287
|
+
viewOnceInner?.videoMessage?.viewOnce ||
|
|
288
|
+
viewOnceInner?.audioMessage?.viewOnce
|
|
289
|
+
) {
|
|
290
|
+
fullMessage.key.isViewOnce = true
|
|
291
|
+
}
|
|
292
|
+
} catch (err) {
|
|
293
|
+
const errorMessage = err?.message || err?.toString() || ""
|
|
294
|
+
const errStr = err?.message || (typeof err === "string" ? err : "") || ""
|
|
295
|
+
const isExpectedDecryptErr = errStr.includes("InvalidPreKeyId") || errStr.includes("SessionNotFound") || errStr.includes("InvalidMessage") || errStr.includes("no sender key state") || errStr.includes("old counter")
|
|
296
|
+
; (isExpectedDecryptErr ? logger?.debug?.bind(logger) : logger?.error?.bind(logger))?.({ key: fullMessage.key, err, errorMessage, messageType: tag === "plaintext" ? "plaintext" : attrs.type, sender, author }, "failed to decrypt message")
|
|
297
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
|
|
298
|
+
fullMessage.messageStubParameters = [errorMessage]
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
// if nothing was found to decrypt
|
|
303
|
+
if (!decryptables) {
|
|
304
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
|
|
305
|
+
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT]
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
//# sourceMappingURL=decode-wa-message.js.map
|