@badzz88/baileys 8.4.7 → 8.5.0

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 (144) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1055 -337
  3. package/WAProto/index.js +5 -5
  4. package/lib/Defaults/index.js +187 -127
  5. package/lib/Defaults/phonenumber-mcc.json +223 -0
  6. package/lib/Signal/Group/ciphertext-message.js +14 -11
  7. package/lib/Signal/Group/group-session-builder.js +91 -29
  8. package/lib/Signal/Group/group_cipher.js +88 -81
  9. package/lib/Signal/Group/index.js +136 -12
  10. package/lib/Signal/Group/keyhelper.js +70 -15
  11. package/lib/Signal/Group/sender-chain-key.js +31 -25
  12. package/lib/Signal/Group/sender-key-distribution-message.js +65 -62
  13. package/lib/Signal/Group/sender-key-message.js +68 -65
  14. package/lib/Signal/Group/sender-key-name.js +43 -41
  15. package/lib/Signal/Group/sender-key-record.js +43 -40
  16. package/lib/Signal/Group/sender-key-state.js +96 -83
  17. package/lib/Signal/Group/sender-message-key.js +29 -25
  18. package/lib/Signal/libsignal.js +483 -424
  19. package/lib/Signal/lid-mapping.js +261 -276
  20. package/lib/Socket/Client/index.js +30 -3
  21. package/lib/Socket/Client/types.js +12 -10
  22. package/lib/Socket/Client/websocket.js +61 -53
  23. package/lib/Socket/aigroups.js +240 -0
  24. package/lib/Socket/business.js +422 -379
  25. package/lib/Socket/chats.js +1816 -1193
  26. package/lib/Socket/communities.js +549 -431
  27. package/lib/Socket/graphql.js +656 -0
  28. package/lib/Socket/groups.js +764 -374
  29. package/lib/Socket/index.js +38 -11
  30. package/lib/Socket/interactive-handler.js +522 -0
  31. package/lib/Socket/interop.js +549 -0
  32. package/lib/Socket/luxu.js +6 -5
  33. package/lib/Socket/managed-account.js +183 -0
  34. package/lib/Socket/messages-recv.js +2439 -1915
  35. package/lib/Socket/messages-send.js +1642 -1242
  36. package/lib/Socket/mex.js +59 -40
  37. package/lib/Socket/newsletter.js +812 -249
  38. package/lib/Socket/privacy.js +449 -0
  39. package/lib/Socket/registration.js +427 -0
  40. package/lib/Socket/socket.js +1077 -965
  41. package/lib/Socket/text-router.js +83 -0
  42. package/lib/Socket/username.js +237 -140
  43. package/lib/Store/index.js +36 -10
  44. package/lib/Store/make-cache-manager-store.js +85 -80
  45. package/lib/Store/make-in-memory-store.js +591 -185
  46. package/lib/Store/make-ordered-dictionary.js +78 -72
  47. package/lib/Store/object-repository.js +25 -28
  48. package/lib/Types/Auth.js +38 -2
  49. package/lib/Types/Bussines.js +2 -2
  50. package/lib/Types/Call.js +2 -2
  51. package/lib/Types/Chat.js +4 -8
  52. package/lib/Types/Contact.js +2 -2
  53. package/lib/Types/Events.js +2 -2
  54. package/lib/Types/GroupMetadata.js +2 -2
  55. package/lib/Types/Label.js +26 -24
  56. package/lib/Types/LabelAssociation.js +8 -6
  57. package/lib/Types/Message.js +95 -11
  58. package/lib/Types/Mex.js +112 -37
  59. package/lib/Types/Newsletter.js +121 -0
  60. package/lib/Types/Product.js +2 -2
  61. package/lib/Types/Signal.js +2 -2
  62. package/lib/Types/Socket.js +2 -3
  63. package/lib/Types/State.js +70 -56
  64. package/lib/Types/USync.js +2 -2
  65. package/lib/Types/index.js +55 -26
  66. package/lib/Utils/auth-utils.js +292 -288
  67. package/lib/Utils/browser-utils.js +113 -48
  68. package/lib/Utils/business.js +240 -224
  69. package/lib/Utils/chat-utils.js +1205 -869
  70. package/lib/Utils/command-loader.d.ts +31 -0
  71. package/lib/Utils/command-loader.js +100 -0
  72. package/lib/Utils/companion-reg-client-utils.js +41 -34
  73. package/lib/Utils/consumer-application.js +107 -0
  74. package/lib/Utils/crypto.js +144 -107
  75. package/lib/Utils/curve25519-js.js +275 -0
  76. package/lib/Utils/decode-wa-message.js +518 -308
  77. package/lib/Utils/event-buffer.js +596 -611
  78. package/lib/Utils/generics.js +514 -354
  79. package/lib/Utils/group-history.js +60 -0
  80. package/lib/Utils/history.js +244 -134
  81. package/lib/Utils/identity-change-handler.js +51 -49
  82. package/lib/Utils/index.js +57 -23
  83. package/lib/Utils/jid-display-normalization.js +218 -0
  84. package/lib/Utils/link-preview.js +135 -77
  85. package/lib/Utils/logger.js +9 -3
  86. package/lib/Utils/lt-hash.js +37 -6
  87. package/lib/Utils/make-mutex.js +36 -33
  88. package/lib/Utils/message-composer.js +439 -233
  89. package/lib/Utils/message-retry-manager.js +265 -260
  90. package/lib/Utils/messages-media.js +855 -747
  91. package/lib/Utils/messages.js +2528 -884
  92. package/lib/Utils/meta-ai-msmsg.js +262 -0
  93. package/lib/Utils/native-bridge.js +82 -0
  94. package/lib/Utils/noise-handler.js +195 -200
  95. package/lib/Utils/offline-node-processor.js +35 -33
  96. package/lib/Utils/pre-key-manager.js +103 -102
  97. package/lib/Utils/process-message.js +978 -560
  98. package/lib/Utils/reporting-utils.js +257 -253
  99. package/lib/Utils/session-pool.d.ts +16 -0
  100. package/lib/Utils/session-pool.js +94 -0
  101. package/lib/Utils/signal.js +218 -195
  102. package/lib/Utils/stanza-ack.js +31 -29
  103. package/lib/Utils/sticker.d.ts +17 -0
  104. package/lib/Utils/sticker.js +145 -0
  105. package/lib/Utils/sync-action-utils.js +50 -45
  106. package/lib/Utils/tc-token-utils.js +137 -139
  107. package/lib/Utils/use-multi-file-auth-state.js +109 -109
  108. package/lib/Utils/validate-connection.js +218 -202
  109. package/lib/Utils/view-once-cache.d.ts +9 -0
  110. package/lib/Utils/view-once-cache.js +79 -0
  111. package/lib/Utils/voip-rekey.js +22 -0
  112. package/lib/WABinary/constants.js +1302 -1299
  113. package/lib/WABinary/decode.js +343 -262
  114. package/lib/WABinary/encode.js +12 -4
  115. package/lib/WABinary/generic-utils.js +223 -187
  116. package/lib/WABinary/index.js +33 -6
  117. package/lib/WABinary/jid-utils.js +351 -88
  118. package/lib/WABinary/types.js +2 -2
  119. package/lib/WAM/BinaryInfo.js +12 -9
  120. package/lib/WAM/constants.js +39486 -22853
  121. package/lib/WAM/encode.js +132 -140
  122. package/lib/WAM/index.js +31 -4
  123. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +55 -0
  124. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +43 -0
  125. package/lib/WAUSync/Protocols/USyncContactProtocol.js +53 -51
  126. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +56 -53
  127. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +29 -26
  128. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +59 -0
  129. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +31 -0
  130. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +32 -0
  131. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +29 -0
  132. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -37
  133. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
  134. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +26 -24
  135. package/lib/WAUSync/Protocols/index.js +39 -6
  136. package/lib/WAUSync/USyncQuery.js +132 -97
  137. package/lib/WAUSync/USyncUser.js +49 -30
  138. package/lib/WAUSync/index.js +31 -4
  139. package/lib/antiban.js +4637 -0
  140. package/lib/index.js +48 -15
  141. package/package.json +55 -92
  142. package/lib/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -263
  143. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +0 -51
  144. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +0 -29
@@ -0,0 +1,262 @@
1
+ 'use strict'
2
+
3
+ const Crypto_1 = require('./crypto')
4
+ const { proto } = require('../../WAProto')
5
+
6
+ const BOT_MESSAGE_INFO = 'Bot Message'
7
+ const KEY_LENGTH = 32
8
+ const AUTH_TAG_LENGTH = 16
9
+
10
+ const MSG_ID_HEX_RE = /^[0-9A-Fa-f]{32}$/
11
+
12
+ const unpadRandomMax16 = value => {
13
+ const bytes = new Uint8Array(value)
14
+ if (!bytes.length) {
15
+ throw new Error('unpadPkcs7 given empty bytes')
16
+ }
17
+
18
+ const padLength = bytes[bytes.length - 1]
19
+ if (padLength > bytes.length) {
20
+ throw new Error(`unpad given ${bytes.length} bytes, but pad is ${padLength}`)
21
+ }
22
+
23
+ return new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.length - padLength)
24
+ }
25
+
26
+ const toBuffer = value => {
27
+ if (Buffer.isBuffer(value)) {
28
+ return value
29
+ }
30
+
31
+ if (value instanceof Uint8Array) {
32
+ return Buffer.from(value.buffer, value.byteOffset, value.byteLength)
33
+ }
34
+
35
+ return Buffer.from(value)
36
+ }
37
+
38
+ const normalizeLidJid = jid => {
39
+ if (!jid || !jid.endsWith('@lid') || !jid.includes(':')) {
40
+ return jid
41
+ }
42
+
43
+ return `${jid.split(':')[0]}@lid`
44
+ }
45
+
46
+ const buildMessageIdRepresentations = messageId => {
47
+ const ascii = Buffer.from(messageId)
48
+ const binary = MSG_ID_HEX_RE.test(messageId) ? Buffer.from(messageId, 'hex') : ascii
49
+ return [
50
+ { label: 'msgIdAscii', value: ascii },
51
+ ...(binary.equals(ascii) ? [] : [{ label: 'msgIdBinary', value: binary }])
52
+ ]
53
+ }
54
+
55
+ const pushUnique = (items, seen, item) => {
56
+ const key = JSON.stringify([
57
+ item.messageId,
58
+ item.idSource,
59
+ item.idSources,
60
+ item.infoSource,
61
+ item.aadSource,
62
+ item.info.toString('hex'),
63
+ item.aad.toString('hex')
64
+ ])
65
+
66
+ if (!seen.has(key)) {
67
+ seen.add(key)
68
+ items.push(item)
69
+ }
70
+ }
71
+
72
+ const getCandidateIds = messageKey => {
73
+ const orderedCandidates = [
74
+ messageKey?.botType === 'full'
75
+ ? { source: 'stanzaId', messageId: messageKey?.stanzaId }
76
+ : { source: 'botEditTargetId', messageId: messageKey?.botEditTargetId },
77
+ { source: 'targetId', messageId: messageKey?.targetId },
78
+ { source: 'metaTargetId', messageId: messageKey?.metaTargetId },
79
+ { source: 'stanzaId', messageId: messageKey?.stanzaId }
80
+ ]
81
+
82
+ const targetIdCandidates = Array.isArray(messageKey?.targetIdCandidates) ? messageKey.targetIdCandidates : []
83
+ for (let index = 0; index < targetIdCandidates.length; index += 1) {
84
+ orderedCandidates.push({
85
+ source: `targetIdCandidates[${index}]`,
86
+ messageId: targetIdCandidates[index]
87
+ })
88
+ }
89
+
90
+ const grouped = new Map()
91
+ for (const candidate of orderedCandidates) {
92
+ if (!candidate.messageId) {
93
+ continue
94
+ }
95
+
96
+ const messageId = String(candidate.messageId)
97
+ const existing = grouped.get(messageId)
98
+ if (existing) {
99
+ if (!existing.idSources.includes(candidate.source)) {
100
+ existing.idSources.push(candidate.source)
101
+ }
102
+ } else {
103
+ grouped.set(messageId, {
104
+ messageId,
105
+ idSource: candidate.source,
106
+ idSources: [candidate.source]
107
+ })
108
+ }
109
+ }
110
+
111
+ return Array.from(grouped.values())
112
+ }
113
+
114
+ const getJidCandidates = messageKey => {
115
+ const ordered = [
116
+ { source: 'meId', jid: messageKey?.meId },
117
+ { source: 'conversationJid', jid: messageKey?.conversationJid },
118
+ { source: 'senderJid', jid: messageKey?.senderJid },
119
+ { source: 'meLidNormalized', jid: normalizeLidJid(messageKey?.meLid) }
120
+ ]
121
+
122
+ const seen = new Set()
123
+ const candidates = []
124
+ for (const candidate of ordered) {
125
+ if (!candidate.jid) {
126
+ continue
127
+ }
128
+
129
+ const jid = String(candidate.jid)
130
+ if (!seen.has(jid)) {
131
+ seen.add(jid)
132
+ candidates.push({ source: candidate.source, jid, value: Buffer.from(jid) })
133
+ }
134
+ }
135
+
136
+ return candidates
137
+ }
138
+
139
+ const buildMsmsgDecryptionStrategies = messageKey => {
140
+ const botJid = String(messageKey?.participant || '')
141
+ const botJidBuffer = Buffer.from(botJid)
142
+ const targetIds = getCandidateIds(messageKey)
143
+ const jidCandidates = getJidCandidates(messageKey)
144
+ const primaryJid = jidCandidates[0]
145
+ const alternateJid = jidCandidates.find(
146
+ candidate => candidate.source !== primaryJid?.source && candidate.jid !== botJid
147
+ )
148
+ const strategies = []
149
+ const seen = new Set()
150
+
151
+ for (const idCandidate of targetIds) {
152
+ const idForms = buildMessageIdRepresentations(idCandidate.messageId)
153
+ for (const idForm of idForms) {
154
+ pushUnique(strategies, seen, {
155
+ mode: '2step',
156
+ idSource: idCandidate.idSource,
157
+ idSources: idCandidate.idSources,
158
+ infoSource: `${idForm.label}+meId+botJid`,
159
+ aadSource: `${idForm.label}+0+botJid`,
160
+ authTagLayout: 'trailing',
161
+ messageId: idCandidate.messageId,
162
+ info: Buffer.concat([idForm.value, primaryJid.value, botJidBuffer, Buffer.alloc(0)]),
163
+ aad: Buffer.concat([idForm.value, Buffer.from([0]), botJidBuffer]),
164
+ attemptLabel: `${idCandidate.idSource}:${idForm.label}:primary`
165
+ })
166
+
167
+ if (alternateJid) {
168
+ pushUnique(strategies, seen, {
169
+ mode: '2step',
170
+ idSource: idCandidate.idSource,
171
+ idSources: idCandidate.idSources,
172
+ infoSource: `${idForm.label}+${alternateJid.source}+botJid`,
173
+ aadSource: `${idForm.label}+0+${alternateJid.source}`,
174
+ authTagLayout: 'trailing',
175
+ messageId: idCandidate.messageId,
176
+ info: Buffer.concat([idForm.value, alternateJid.value, botJidBuffer, Buffer.alloc(0)]),
177
+ aad: Buffer.concat([idForm.value, Buffer.from([0]), alternateJid.value]),
178
+ attemptLabel: `${idCandidate.idSource}:${idForm.label}:${alternateJid.source}`
179
+ })
180
+ }
181
+ }
182
+ }
183
+
184
+ return strategies.slice(0, 12)
185
+ }
186
+
187
+ const assertRequired = (value, label) => {
188
+ if (
189
+ !value ||
190
+ (Buffer.isBuffer(value) && value.length === 0) ||
191
+ (value instanceof Uint8Array && value.byteLength === 0)
192
+ ) {
193
+ throw new Error(`Missing required ${label} for msmsg decryption`)
194
+ }
195
+ }
196
+
197
+ const decryptWithStrategy = (messageSecret, msMsg, strategy) => {
198
+ const baseSecret = Buffer.from(Crypto_1.hkdf(toBuffer(messageSecret), KEY_LENGTH, { info: BOT_MESSAGE_INFO }))
199
+ const key = Buffer.from(Crypto_1.hkdf(baseSecret, KEY_LENGTH, { info: strategy.info }))
200
+ const payload = toBuffer(msMsg.encPayload)
201
+ // ciphertext = payload without last 16 bytes (auth tag) + auth tag appended → standard GCM layout
202
+ const ciphertextWithTag = Buffer.concat([payload.slice(0, -AUTH_TAG_LENGTH), payload.slice(-AUTH_TAG_LENGTH)])
203
+ return Buffer.from(Crypto_1.aesDecryptGCM(ciphertextWithTag, key, toBuffer(msMsg.encIv), strategy.aad))
204
+ }
205
+
206
+ const decodeDecryptedMsmsgMessage = decrypted => {
207
+ const messageBuffer = toBuffer(decrypted)
208
+
209
+ try {
210
+ const unpadded = Buffer.from(unpadRandomMax16(messageBuffer))
211
+ const decoded = proto.Message.decode(unpadded)
212
+ const hasContent = Object.keys(decoded).some(key => key !== 'messageContextInfo' && decoded[key] != null)
213
+ if (hasContent) {
214
+ return decoded
215
+ }
216
+ } catch {}
217
+
218
+ return proto.Message.decode(messageBuffer)
219
+ }
220
+
221
+ const decryptMsmsgBotMessage = async (messageSecret, messageKey, msMsg) => {
222
+ assertRequired(messageSecret, 'messageSecret')
223
+ assertRequired(messageKey?.participant, 'participant')
224
+ assertRequired(messageKey?.meId, 'meId')
225
+ assertRequired(msMsg?.encIv, 'encIv')
226
+ assertRequired(msMsg?.encPayload, 'encPayload')
227
+ if (getCandidateIds(messageKey).length === 0) {
228
+ throw new Error('Missing required target message id for msmsg decryption')
229
+ }
230
+
231
+ const strategies = buildMsmsgDecryptionStrategies(messageKey)
232
+ const attemptedStrategies = []
233
+ let lastError
234
+
235
+ for (const strategy of strategies) {
236
+ const attempt = {
237
+ idSource: strategy.idSource,
238
+ idSources: strategy.idSources,
239
+ infoSource: strategy.infoSource,
240
+ aadSource: strategy.aadSource,
241
+ messageId: strategy.messageId
242
+ }
243
+
244
+ try {
245
+ return await decryptWithStrategy(messageSecret, msMsg, strategy)
246
+ } catch (error) {
247
+ attemptedStrategies.push(attempt)
248
+ lastError = error
249
+ }
250
+ }
251
+
252
+ const error = new Error('Failed to decrypt msmsg with bounded deterministic strategies')
253
+ error.attemptedStrategies = attemptedStrategies
254
+ error.cause = lastError
255
+ throw error
256
+ }
257
+
258
+ module.exports = {
259
+ buildMsmsgDecryptionStrategies,
260
+ decodeDecryptedMsmsgMessage,
261
+ decryptMsmsgBotMessage
262
+ }
@@ -0,0 +1,82 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', { value: true })
3
+ exports.loadRustBridge = void 0
4
+ const fs = require('fs')
5
+ const path = require('path')
6
+
7
+ const BRIDGE_NAME = 'whatsapp-rust-bridge'
8
+ let cached
9
+ let attempted = false
10
+
11
+ /**
12
+ * Finds the installed package directory for `pkgName` by walking the same
13
+ * node_modules search path Node itself would use, without going through
14
+ * Node's package "exports" resolution gate.
15
+ */
16
+ const findPackageDir = pkgName => {
17
+ let searchPaths
18
+ try {
19
+ searchPaths = require.resolve.paths(pkgName) || []
20
+ } catch {
21
+ searchPaths = []
22
+ }
23
+ for (const dir of searchPaths) {
24
+ const candidate = path.join(dir, pkgName)
25
+ if (fs.existsSync(path.join(candidate, 'package.json'))) {
26
+ return candidate
27
+ }
28
+ }
29
+ return null
30
+ }
31
+
32
+ /**
33
+ * Loads whatsapp-rust-bridge, tolerating published versions/forks whose
34
+ * package.json ships an "exports" map with no "." entry (or an otherwise
35
+ * broken one) -- which makes a plain `require('whatsapp-rust-bridge')` throw
36
+ * ERR_PACKAGE_PATH_NOT_EXPORTED even though the module's files are present
37
+ * and perfectly usable. In that case, we locate the package directory
38
+ * ourselves and require its real entry file directly, bypassing the broken
39
+ * "exports" gate. Returns the loaded module, or null if it's unavailable.
40
+ */
41
+ const loadRustBridge = () => {
42
+ if (attempted) return cached
43
+ attempted = true
44
+ try {
45
+ cached = require(BRIDGE_NAME)
46
+ return cached
47
+ } catch (error) {
48
+ const recoverableCodes = new Set(['ERR_PACKAGE_PATH_NOT_EXPORTED', 'ERR_PACKAGE_IMPORT_NOT_DEFINED'])
49
+ if (!recoverableCodes.has(error?.code)) {
50
+ cached = null
51
+ return null
52
+ }
53
+ try {
54
+ const dir = findPackageDir(BRIDGE_NAME)
55
+ if (!dir) {
56
+ cached = null
57
+ return null
58
+ }
59
+ const pkgJson = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf-8'))
60
+ const candidates = [
61
+ pkgJson.main,
62
+ 'index.js',
63
+ 'index.node',
64
+ 'dist/index.js',
65
+ 'lib/index.js',
66
+ 'build/index.js'
67
+ ].filter(Boolean)
68
+ for (const rel of candidates) {
69
+ const fullPath = path.join(dir, rel)
70
+ if (fs.existsSync(fullPath)) {
71
+ cached = require(fullPath)
72
+ return cached
73
+ }
74
+ }
75
+ } catch {
76
+ // fall through to returning null below
77
+ }
78
+ cached = null
79
+ return null
80
+ }
81
+ }
82
+ exports.loadRustBridge = loadRustBridge