@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.
- package/LICENSE +1 -1
- package/README.md +924 -1299
- package/WAProto/index.js +22 -18
- package/lib/Defaults/baileys-version.json +6 -2
- package/lib/Defaults/index.js +173 -172
- package/lib/Signal/libsignal.js +395 -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 +916 -963
- package/lib/Socket/communities.js +430 -430
- package/lib/Socket/groups.js +342 -342
- package/lib/Socket/index.js +21 -22
- package/lib/Socket/messages-recv.js +963 -743
- package/lib/Socket/messages-send.js +273 -321
- package/lib/Socket/mex.js +50 -50
- package/lib/Socket/newsletter.js +148 -148
- package/lib/Socket/nexus-handler.js +296 -247
- package/lib/Socket/registration.js +50 -33
- package/lib/Socket/socket.js +872 -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 +37 -29
- 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 +55 -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 +726 -764
- package/lib/Utils/companion-reg-client-utils.js +34 -0
- package/lib/Utils/crypto.js +109 -135
- package/lib/Utils/decode-wa-message.js +342 -314
- package/lib/Utils/event-buffer.js +547 -547
- package/lib/Utils/generics.js +295 -297
- package/lib/Utils/history.js +91 -83
- package/lib/Utils/index.js +25 -20
- package/lib/Utils/key-store.js +17 -0
- package/lib/Utils/link-preview.js +107 -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 +579 -535
- package/lib/Utils/messages.js +821 -706
- package/lib/Utils/noise-handler.js +255 -255
- package/lib/Utils/pre-key-manager.js +105 -105
- package/lib/Utils/process-message.js +430 -412
- package/lib/Utils/reporting-utils.js +155 -0
- package/lib/Utils/signal.js +191 -159
- package/lib/Utils/sync-action-utils.js +33 -0
- package/lib/Utils/tc-token-utils.js +162 -0
- package/lib/Utils/use-multi-file-auth-state.js +120 -120
- package/lib/Utils/validate-connection.js +194 -194
- package/lib/WABinary/constants.js +1306 -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 +6 -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 +65 -66
- package/package.json +172 -143
- 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,342 @@
|
|
|
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
|
-
|
|
134
|
-
|
|
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
|
-
participant
|
|
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
|
-
|
|
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
|
-
const
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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
|
-
|
|
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
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
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
|
+
export const ACCOUNT_RESTRICTED_TEXT = 'Your account has been restricted';
|
|
59
|
+
// Retry configuration for failed decryption
|
|
60
|
+
export const DECRYPTION_RETRY_CONFIG = {
|
|
61
|
+
maxRetries: 3,
|
|
62
|
+
baseDelayMs: 100,
|
|
63
|
+
sessionRecordErrors: ["No session record", "SessionError: No session record"],
|
|
64
|
+
}
|
|
65
|
+
export const NACK_REASONS = {
|
|
66
|
+
ParsingError: 487,
|
|
67
|
+
UnrecognizedStanza: 488,
|
|
68
|
+
UnrecognizedStanzaClass: 489,
|
|
69
|
+
UnrecognizedStanzaType: 490,
|
|
70
|
+
InvalidProtobuf: 491,
|
|
71
|
+
InvalidHostedCompanionStanza: 493,
|
|
72
|
+
MissingMessageSecret: 495,
|
|
73
|
+
SignalErrorOldCounter: 496,
|
|
74
|
+
MessageDeletedOnPeer: 499,
|
|
75
|
+
UnhandledError: 500,
|
|
76
|
+
UnsupportedAdminRevoke: 550,
|
|
77
|
+
UnsupportedLIDGroup: 551,
|
|
78
|
+
DBOperationFailed: 552,
|
|
79
|
+
}
|
|
80
|
+
export const extractAddressingContext = (stanza) => {
|
|
81
|
+
let senderAlt
|
|
82
|
+
let recipientAlt
|
|
83
|
+
const sender = stanza.attrs.participant || stanza.attrs.from
|
|
84
|
+
const addressingMode = stanza.attrs.addressing_mode || (sender?.endsWith("lid") ? "lid" : "pn")
|
|
85
|
+
if (addressingMode === "lid") {
|
|
86
|
+
// Message is LID-addressed: sender is LID, extract corresponding PN
|
|
87
|
+
// without device data
|
|
88
|
+
senderAlt = stanza.attrs.participant_pn || stanza.attrs.sender_pn || stanza.attrs.peer_recipient_pn
|
|
89
|
+
recipientAlt = stanza.attrs.recipient_pn
|
|
90
|
+
// with device data
|
|
91
|
+
//if (sender && senderAlt) senderAlt = transferDevice(sender, senderAlt)
|
|
92
|
+
} else {
|
|
93
|
+
// Message is PN-addressed: sender is PN, extract corresponding LID
|
|
94
|
+
// without device data
|
|
95
|
+
senderAlt = stanza.attrs.participant_lid || stanza.attrs.sender_lid || stanza.attrs.peer_recipient_lid
|
|
96
|
+
recipientAlt = stanza.attrs.recipient_lid
|
|
97
|
+
//with device data
|
|
98
|
+
//if (sender && senderAlt) senderAlt = transferDevice(sender, senderAlt)
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
addressingMode,
|
|
102
|
+
senderAlt,
|
|
103
|
+
recipientAlt,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Server-side error codes returned in ack stanzas (server → client) that we
|
|
108
|
+
* currently have dedicated handlers for. Extend as more handlers are added.
|
|
109
|
+
* Distinct from the client-side NackReason enum (WAWebCreateNackFromStanza).
|
|
110
|
+
*/
|
|
111
|
+
export const SERVER_ERROR_CODES = {
|
|
112
|
+
/**
|
|
113
|
+
* 1:1 message missing privacy token (tctoken). Usually means the account is
|
|
114
|
+
* restricted: WhatsApp blocks starting new chats but preserves existing ones,
|
|
115
|
+
* since established chats already carry a tctoken.
|
|
116
|
+
*/
|
|
117
|
+
MessageAccountRestriction: '463',
|
|
118
|
+
/** Stanza validation failure (SMAX_INVALID) — likely stale device session */
|
|
119
|
+
SmaxInvalid: '479'
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Decode the received node as a message.
|
|
123
|
+
* @note this will only parse the message, not decrypt it
|
|
124
|
+
*/
|
|
125
|
+
export function decodeMessageNode(stanza, meId, meLid) {
|
|
126
|
+
let msgType
|
|
127
|
+
let chatId
|
|
128
|
+
let author
|
|
129
|
+
let fromMe = false
|
|
130
|
+
const msgId = stanza.attrs.id
|
|
131
|
+
const from = stanza.attrs.from
|
|
132
|
+
const participant = stanza.attrs.participant
|
|
133
|
+
const recipient = stanza.attrs.recipient
|
|
134
|
+
const addressingContext = extractAddressingContext(stanza)
|
|
135
|
+
const isMe = (jid) => areJidsSameUser(jid, meId)
|
|
136
|
+
const isMeLid = (jid) => areJidsSameUser(jid, meLid)
|
|
137
|
+
if (isPnUser(from) || isLidUser(from) || isHostedLidUser(from) || isHostedPnUser(from)) {
|
|
138
|
+
if (recipient && !isJidMetaAI(recipient)) {
|
|
139
|
+
if (!isMe(from) && !isMeLid(from)) {
|
|
140
|
+
throw new Boom("receipient present, but msg not from me", { data: stanza })
|
|
141
|
+
}
|
|
142
|
+
if (isMe(from) || isMeLid(from)) {
|
|
143
|
+
fromMe = true
|
|
144
|
+
}
|
|
145
|
+
chatId = recipient
|
|
146
|
+
} else {
|
|
147
|
+
chatId = from
|
|
148
|
+
}
|
|
149
|
+
msgType = "chat"
|
|
150
|
+
author = from
|
|
151
|
+
} else if (isJidGroup(from)) {
|
|
152
|
+
if (!participant) {
|
|
153
|
+
throw new Boom("No participant in group message")
|
|
154
|
+
}
|
|
155
|
+
if (isMe(participant) || isMeLid(participant)) {
|
|
156
|
+
fromMe = true
|
|
157
|
+
}
|
|
158
|
+
msgType = "group"
|
|
159
|
+
author = participant
|
|
160
|
+
chatId = from
|
|
161
|
+
} else if (isJidBroadcast(from)) {
|
|
162
|
+
if (!participant) {
|
|
163
|
+
throw new Boom("No participant in group message")
|
|
164
|
+
}
|
|
165
|
+
const isParticipantMe = isMe(participant)
|
|
166
|
+
if (isJidStatusBroadcast(from)) {
|
|
167
|
+
msgType = isParticipantMe ? "direct_peer_status" : "other_status"
|
|
168
|
+
} else {
|
|
169
|
+
msgType = isParticipantMe ? "peer_broadcast" : "other_broadcast"
|
|
170
|
+
}
|
|
171
|
+
fromMe = isParticipantMe
|
|
172
|
+
chatId = from
|
|
173
|
+
author = participant
|
|
174
|
+
} else if (isJidNewsletter(from)) {
|
|
175
|
+
msgType = "newsletter"
|
|
176
|
+
chatId = from
|
|
177
|
+
author = from
|
|
178
|
+
if (isMe(from) || isMeLid(from)) {
|
|
179
|
+
fromMe = true
|
|
180
|
+
}
|
|
181
|
+
} else {
|
|
182
|
+
throw new Boom("Unknown message type", { data: stanza })
|
|
183
|
+
}
|
|
184
|
+
const pushname = stanza?.attrs?.notify
|
|
185
|
+
const key = {
|
|
186
|
+
remoteJid: chatId,
|
|
187
|
+
remoteJidAlt: !isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
|
|
188
|
+
fromMe,
|
|
189
|
+
id: msgId,
|
|
190
|
+
participant,
|
|
191
|
+
participantAlt: isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
|
|
192
|
+
addressingMode: addressingContext.addressingMode,
|
|
193
|
+
...(msgType === "newsletter" && stanza.attrs.server_id ? { server_id: stanza.attrs.server_id } : {}),
|
|
194
|
+
}
|
|
195
|
+
const fullMessage = {
|
|
196
|
+
key,
|
|
197
|
+
messageTimestamp: +stanza.attrs.t,
|
|
198
|
+
pushName: pushname,
|
|
199
|
+
broadcast: isJidBroadcast(from),
|
|
200
|
+
}
|
|
201
|
+
if (key.fromMe) {
|
|
202
|
+
fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
fullMessage,
|
|
206
|
+
author,
|
|
207
|
+
sender: msgType === "chat" ? author : chatId,
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
export const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
211
|
+
const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid)
|
|
212
|
+
return {
|
|
213
|
+
fullMessage,
|
|
214
|
+
category: stanza.attrs.category,
|
|
215
|
+
author,
|
|
216
|
+
async decrypt() {
|
|
217
|
+
let decryptables = 0
|
|
218
|
+
if (Array.isArray(stanza.content)) {
|
|
219
|
+
for (const { tag, attrs, content } of stanza.content) {
|
|
220
|
+
if (tag === "verified_name" && content instanceof Uint8Array) {
|
|
221
|
+
const cert = proto.VerifiedNameCertificate.decode(content)
|
|
222
|
+
const details = proto.VerifiedNameCertificate.Details.decode(cert.details)
|
|
223
|
+
fullMessage.verifiedBizName = details.verifiedName
|
|
224
|
+
}
|
|
225
|
+
if (tag === "unavailable" && attrs.type === "view_once") {
|
|
226
|
+
fullMessage.key.isViewOnce = true // TODO: remove from here and add a STUB TYPE
|
|
227
|
+
}
|
|
228
|
+
if (tag !== "enc" && tag !== "plaintext") {
|
|
229
|
+
continue
|
|
230
|
+
}
|
|
231
|
+
if (!(content instanceof Uint8Array)) {
|
|
232
|
+
continue
|
|
233
|
+
}
|
|
234
|
+
decryptables += 1
|
|
235
|
+
let msgBuffer
|
|
236
|
+
const decryptionJid = await getDecryptionJid(author, repository, logger)
|
|
237
|
+
let decryptionAltJid = null
|
|
238
|
+
const { senderAlt } = extractAddressingContext(stanza)
|
|
239
|
+
if (senderAlt) {
|
|
240
|
+
decryptionAltJid = await getDecryptionJid(senderAlt, repository, logger)
|
|
241
|
+
}
|
|
242
|
+
if (tag !== 'plaintext') {
|
|
243
|
+
storeMappingFromEnvelope(stanza, author, repository, decryptionJid, logger, meId, meLid).catch(err => logger?.warn?.({ err }, 'storeMappingFromEnvelope failed'))
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
const e2eType = tag === "plaintext" ? "plaintext" : attrs.type
|
|
247
|
+
switch (e2eType) {
|
|
248
|
+
case "skmsg":
|
|
249
|
+
try {
|
|
250
|
+
msgBuffer = await repository.decryptGroupMessage({
|
|
251
|
+
group: sender,
|
|
252
|
+
authorJid: author,
|
|
253
|
+
msg: content,
|
|
254
|
+
})
|
|
255
|
+
} catch (decryptErr) {
|
|
256
|
+
const errMsg = decryptErr?.message || decryptErr?.toString() || ''
|
|
257
|
+
if (errMsg.includes('invalid sender key session') || errMsg.includes('memory access out of bounds')) {
|
|
258
|
+
logger?.debug?.({ group: sender, author: author, errMsg }, '[Signal] Stale/corrupt sender key session, deleting')
|
|
259
|
+
try {
|
|
260
|
+
await repository.deleteSenderKey(sender, author)
|
|
261
|
+
} catch (e) {
|
|
262
|
+
logger?.warn?.({ err: e }, 'Failed to delete sender key')
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
throw decryptErr
|
|
266
|
+
}
|
|
267
|
+
break
|
|
268
|
+
case "pkmsg":
|
|
269
|
+
case "msg":
|
|
270
|
+
try {
|
|
271
|
+
msgBuffer = await repository.decryptMessage({ jid: decryptionJid, type: e2eType, ciphertext: content })
|
|
272
|
+
if (msgBuffer === null) {
|
|
273
|
+
throw new Error("DuplicatedMessage")
|
|
274
|
+
}
|
|
275
|
+
} catch (decryptErr) {
|
|
276
|
+
const errMsg = decryptErr?.message || decryptErr?.toString() || ''
|
|
277
|
+
if ((errMsg.includes('InvalidMessage') || errMsg.includes('BadMac')) && e2eType === 'msg') {
|
|
278
|
+
logger?.debug?.({ jid: decryptionJid, errMsg }, '[Signal] Stale session (InvalidMessage/BadMac), deleting')
|
|
279
|
+
try {
|
|
280
|
+
const toDelete = [decryptionJid]
|
|
281
|
+
await repository.deleteSession(toDelete)
|
|
282
|
+
} catch { }
|
|
283
|
+
}
|
|
284
|
+
const cleanError = new Error(errMsg || 'Decryption failed')
|
|
285
|
+
cleanError.stack = decryptErr?.stack
|
|
286
|
+
throw cleanError
|
|
287
|
+
}
|
|
288
|
+
break
|
|
289
|
+
case "plaintext":
|
|
290
|
+
msgBuffer = content
|
|
291
|
+
break
|
|
292
|
+
default:
|
|
293
|
+
throw new Error(`Unknown e2e type: ${e2eType}`)
|
|
294
|
+
}
|
|
295
|
+
let msg = proto.Message.decode(e2eType !== "plaintext" ? unpadRandomMax16(msgBuffer) : msgBuffer)
|
|
296
|
+
msg = msg.deviceSentMessage?.message || msg
|
|
297
|
+
if (msg.senderKeyDistributionMessage) {
|
|
298
|
+
//eslint-disable-next-line max-depth
|
|
299
|
+
try {
|
|
300
|
+
await repository.processSenderKeyDistributionMessage({
|
|
301
|
+
authorJid: author,
|
|
302
|
+
item: msg.senderKeyDistributionMessage,
|
|
303
|
+
})
|
|
304
|
+
} catch (err) {
|
|
305
|
+
logger.error({ key: fullMessage.key, err }, "failed to process sender key distribution message")
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (fullMessage.message) {
|
|
309
|
+
Object.assign(fullMessage.message, msg)
|
|
310
|
+
} else {
|
|
311
|
+
fullMessage.message = msg
|
|
312
|
+
}
|
|
313
|
+
const viewOnceInner =
|
|
314
|
+
msg?.viewOnceMessage?.message ||
|
|
315
|
+
msg?.viewOnceMessageV2?.message ||
|
|
316
|
+
msg?.viewOnceMessageV2Extension?.message
|
|
317
|
+
if (
|
|
318
|
+
viewOnceInner?.imageMessage?.viewOnce ||
|
|
319
|
+
viewOnceInner?.videoMessage?.viewOnce ||
|
|
320
|
+
viewOnceInner?.audioMessage?.viewOnce
|
|
321
|
+
) {
|
|
322
|
+
fullMessage.key.isViewOnce = true
|
|
323
|
+
}
|
|
324
|
+
} catch (err) {
|
|
325
|
+
const errorMessage = err?.message || err?.toString() || ""
|
|
326
|
+
const errStr = err?.message || (typeof err === "string" ? err : "") || ""
|
|
327
|
+
const isExpectedDecryptErr = errStr.includes("InvalidPreKeyId") || errStr.includes("SessionNotFound") || errStr.includes("InvalidMessage") || errStr.includes("no sender key state") || errStr.includes("invalid sender key session") || errStr.includes("memory access out of bounds") || errStr.includes("old counter") || errStr.includes("DuplicatedMessage") || errStr.includes("BadMac")
|
|
328
|
+
; (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")
|
|
329
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
|
|
330
|
+
fullMessage.messageStubParameters = [errorMessage]
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// if nothing was found to decrypt
|
|
335
|
+
if (!decryptables) {
|
|
336
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
|
|
337
|
+
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT]
|
|
338
|
+
}
|
|
339
|
+
},
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
//# sourceMappingURL=decode-wa-message.js.map
|