@nexustechpro/baileys 2.0.1 → 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 +667 -393
- 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 -88
- 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 -532
- 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
package/lib/Utils/generics.js
CHANGED
|
@@ -1,298 +1,298 @@
|
|
|
1
|
-
import { Boom } from '@hapi/boom'
|
|
2
|
-
import { createHash, randomBytes } from 'crypto'
|
|
3
|
-
import { proto } from '../../WAProto/index.js'
|
|
4
|
-
const baileysVersion = [2, 3000,
|
|
5
|
-
import { DisconnectReason } from '../Types/index.js'
|
|
6
|
-
import { getAllBinaryNodeChildren, jidDecode } from '../WABinary/index.js'
|
|
7
|
-
import { sha256 } from './crypto.js'
|
|
8
|
-
|
|
9
|
-
export const BufferJSON = {
|
|
10
|
-
replacer: (k, value) => {
|
|
11
|
-
if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
|
|
12
|
-
return { type: 'Buffer', data: Buffer.from(value?.data || value).toString('base64') }
|
|
13
|
-
}
|
|
14
|
-
return value
|
|
15
|
-
},
|
|
16
|
-
reviver: (_, value) => {
|
|
17
|
-
if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
|
|
18
|
-
return Buffer.from(value.data, 'base64')
|
|
19
|
-
}
|
|
20
|
-
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
21
|
-
const keys = Object.keys(value)
|
|
22
|
-
if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
|
|
23
|
-
const values = Object.values(value)
|
|
24
|
-
if (values.every(v => typeof v === 'number')) {
|
|
25
|
-
return Buffer.from(values)
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return value
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export const getKeyAuthor = (key, meId = 'me') => (key?.fromMe ? meId : key?.participantAlt || key?.remoteJidAlt || key?.participant || key?.remoteJid) || ''
|
|
34
|
-
|
|
35
|
-
export const isStringNullOrEmpty = (value) => value == null || value === ''
|
|
36
|
-
|
|
37
|
-
export const writeRandomPadMax16 = (msg) => {
|
|
38
|
-
const pad = randomBytes(1)
|
|
39
|
-
const padLength = (pad[0] & 0x0f) + 1
|
|
40
|
-
return Buffer.concat([msg, Buffer.alloc(padLength, padLength)])
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export const unpadRandomMax16 = (e) => {
|
|
44
|
-
const t = new Uint8Array(e)
|
|
45
|
-
if (0 === t.length) throw new Error('unpadPkcs7 given empty bytes')
|
|
46
|
-
var r = t[t.length - 1]
|
|
47
|
-
if (r > t.length) throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`)
|
|
48
|
-
return new Uint8Array(t.buffer, t.byteOffset, t.length - r)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export const generateParticipantHashV2 = (participants) => {
|
|
52
|
-
participants.sort()
|
|
53
|
-
const sha256Hash = sha256(Buffer.from(participants.join(''))).toString('base64')
|
|
54
|
-
return '2:' + sha256Hash.slice(0, 6)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export const encodeWAMessage = (message) => {
|
|
58
|
-
try {
|
|
59
|
-
if (message && typeof message === 'object' && message.$type === proto.Message) {
|
|
60
|
-
return writeRandomPadMax16(proto.Message.encode(message).finish())
|
|
61
|
-
}
|
|
62
|
-
if (message && typeof message === 'object') {
|
|
63
|
-
const protoMessage = proto.Message.fromObject(message)
|
|
64
|
-
return writeRandomPadMax16(proto.Message.encode(protoMessage).finish())
|
|
65
|
-
}
|
|
66
|
-
return writeRandomPadMax16(proto.Message.encode(message).finish())
|
|
67
|
-
} catch (error) {
|
|
68
|
-
try {
|
|
69
|
-
return writeRandomPadMax16(proto.Message.encode(message).finish())
|
|
70
|
-
} catch (e) {
|
|
71
|
-
console.warn('Message encoding failed:', error.message, e.message)
|
|
72
|
-
throw new Error(`Failed to encode message: ${error.message}`)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export const generateRegistrationId = () => Uint16Array.from(randomBytes(2))[0] & 16383
|
|
78
|
-
|
|
79
|
-
export const encodeBigEndian = (e, t = 4) => {
|
|
80
|
-
let r = e
|
|
81
|
-
const a = new Uint8Array(t)
|
|
82
|
-
for (let i = t - 1; i >= 0; i--) {
|
|
83
|
-
a[i] = 255 & r
|
|
84
|
-
r >>>= 8
|
|
85
|
-
}
|
|
86
|
-
return a
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export const toNumber = (t) => typeof t === 'object' && t ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0
|
|
90
|
-
|
|
91
|
-
export const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000)
|
|
92
|
-
|
|
93
|
-
export const debouncedTimeout = (intervalMs = 1000, task) => {
|
|
94
|
-
let timeout
|
|
95
|
-
return {
|
|
96
|
-
start: (newIntervalMs, newTask) => {
|
|
97
|
-
task = newTask || task
|
|
98
|
-
intervalMs = newIntervalMs || intervalMs
|
|
99
|
-
timeout && clearTimeout(timeout)
|
|
100
|
-
timeout = setTimeout(() => task?.(), intervalMs)
|
|
101
|
-
},
|
|
102
|
-
cancel: () => {
|
|
103
|
-
timeout && clearTimeout(timeout)
|
|
104
|
-
timeout = undefined
|
|
105
|
-
},
|
|
106
|
-
setTask: (newTask) => (task = newTask),
|
|
107
|
-
setInterval: (newInterval) => (intervalMs = newInterval)
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export const delay = (ms) => delayCancellable(ms).delay
|
|
112
|
-
|
|
113
|
-
export const delayCancellable = (ms) => {
|
|
114
|
-
const stack = new Error().stack
|
|
115
|
-
let timeout
|
|
116
|
-
let reject
|
|
117
|
-
const delay = new Promise((resolve, _reject) => {
|
|
118
|
-
timeout = setTimeout(resolve, ms)
|
|
119
|
-
reject = _reject
|
|
120
|
-
})
|
|
121
|
-
const cancel = () => {
|
|
122
|
-
clearTimeout(timeout)
|
|
123
|
-
reject(new Boom('Cancelled', { statusCode: 500, data: { stack } }))
|
|
124
|
-
}
|
|
125
|
-
return { delay, cancel }
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export async function promiseTimeout(ms, promise) {
|
|
129
|
-
if (!ms) return new Promise(promise)
|
|
130
|
-
const stack = new Error().stack
|
|
131
|
-
const { delay, cancel } = delayCancellable(ms)
|
|
132
|
-
const p = new Promise((resolve, reject) => {
|
|
133
|
-
delay.then(() => reject(new Boom('Timed Out', { statusCode: DisconnectReason.timedOut, data: { stack } }))).catch(err => reject(err))
|
|
134
|
-
promise(resolve, reject)
|
|
135
|
-
}).finally(cancel)
|
|
136
|
-
return p
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export const generateMessageIDV2 = (userId) => {
|
|
140
|
-
const data = Buffer.alloc(8 + 20 + 16)
|
|
141
|
-
data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)))
|
|
142
|
-
if (userId) {
|
|
143
|
-
const id = jidDecode(userId)
|
|
144
|
-
if (id?.user) {
|
|
145
|
-
data.write(id.user, 8)
|
|
146
|
-
data.write('@c.us', 8 + id.user.length)
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
const random = randomBytes(16)
|
|
150
|
-
random.copy(data, 28)
|
|
151
|
-
const hash = createHash('sha256').update(data).digest()
|
|
152
|
-
return '3EB0' + hash.toString('hex').toUpperCase().substring(0, 18)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export const generateMessageID = () => '3EB0' + randomBytes(18).toString('hex').toUpperCase()
|
|
156
|
-
|
|
157
|
-
export function bindWaitForEvent(ev, event) {
|
|
158
|
-
return async (check, timeoutMs) => {
|
|
159
|
-
let listener
|
|
160
|
-
let closeListener
|
|
161
|
-
await promiseTimeout(timeoutMs, (resolve, reject) => {
|
|
162
|
-
closeListener = ({ connection, lastDisconnect }) => {
|
|
163
|
-
if (connection === 'close') reject(lastDisconnect?.error || new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed }))
|
|
164
|
-
}
|
|
165
|
-
ev.on('connection.update', closeListener)
|
|
166
|
-
listener = async (update) => {
|
|
167
|
-
if (await check(update)) resolve()
|
|
168
|
-
}
|
|
169
|
-
ev.on(event, listener)
|
|
170
|
-
}).finally(() => {
|
|
171
|
-
ev.off(event, listener)
|
|
172
|
-
ev.off('connection.update', closeListener)
|
|
173
|
-
})
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update')
|
|
178
|
-
|
|
179
|
-
export const fetchLatestBaileysVersion = async (options = {}) => {
|
|
180
|
-
const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/index.ts'
|
|
181
|
-
try {
|
|
182
|
-
const response = await fetch(URL, { dispatcher: options.dispatcher, method: 'GET', headers: options.headers })
|
|
183
|
-
if (!response.ok) throw new Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, { statusCode: response.status })
|
|
184
|
-
const text = await response.text()
|
|
185
|
-
const lines = text.split('\n')
|
|
186
|
-
const versionLine = lines[6]
|
|
187
|
-
const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/)
|
|
188
|
-
if (versionMatch) {
|
|
189
|
-
const version = [parseInt(versionMatch[1]), parseInt(versionMatch[2]), parseInt(versionMatch[3])]
|
|
190
|
-
return { version, isLatest: true }
|
|
191
|
-
} else throw new Error('Could not parse version from Defaults/index.ts')
|
|
192
|
-
} catch (error) {
|
|
193
|
-
return { version: baileysVersion, isLatest: false, error }
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export const fetchLatestWaWebVersion = async (options = {}) => {
|
|
198
|
-
try {
|
|
199
|
-
const defaultHeaders = { 'sec-fetch-site': 'none', 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' }
|
|
200
|
-
const headers = { ...defaultHeaders, ...options.headers }
|
|
201
|
-
const response = await fetch('https://web.whatsapp.com/sw.js', { ...options, method: 'GET', headers })
|
|
202
|
-
if (!response.ok) throw new Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status })
|
|
203
|
-
const data = await response.text()
|
|
204
|
-
const regex = /\\?"client_revision\\?":\s*(\d+)/
|
|
205
|
-
const match = data.match(regex)
|
|
206
|
-
if (!match?.[1]) return { version: baileysVersion, isLatest: false, error: { message: 'Could not find client revision in the fetched content' } }
|
|
207
|
-
const clientRevision = match[1]
|
|
208
|
-
return { version: [2, 3000, +clientRevision], isLatest: true }
|
|
209
|
-
} catch (error) {
|
|
210
|
-
return { version: baileysVersion, isLatest: false, error }
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export const generateMdTagPrefix = () => {
|
|
215
|
-
const bytes = randomBytes(4)
|
|
216
|
-
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const STATUS_MAP = { sender: proto.WebMessageInfo.Status.SERVER_ACK, played: proto.WebMessageInfo.Status.PLAYED, read: proto.WebMessageInfo.Status.READ, 'read-self': proto.WebMessageInfo.Status.READ }
|
|
220
|
-
|
|
221
|
-
export const getStatusFromReceiptType = (type) => {
|
|
222
|
-
const status = STATUS_MAP[type]
|
|
223
|
-
if (typeof type === 'undefined') return proto.WebMessageInfo.Status.DELIVERY_ACK
|
|
224
|
-
return status
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const CODE_MAP = { conflict: DisconnectReason.connectionReplaced }
|
|
228
|
-
|
|
229
|
-
export const getErrorCodeFromStreamError = (node) => {
|
|
230
|
-
const [reasonNode] = getAllBinaryNodeChildren(node)
|
|
231
|
-
let reason = reasonNode?.tag || 'unknown'
|
|
232
|
-
const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession)
|
|
233
|
-
if (statusCode === DisconnectReason.restartRequired) reason = 'restart required'
|
|
234
|
-
return { reason, statusCode }
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
export const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
238
|
-
let status
|
|
239
|
-
switch (tag) {
|
|
240
|
-
case 'offer':
|
|
241
|
-
case 'offer_notice':
|
|
242
|
-
status = 'offer'
|
|
243
|
-
break
|
|
244
|
-
case 'terminate':
|
|
245
|
-
status = attrs.reason === 'timeout' ? 'timeout' : 'terminate'
|
|
246
|
-
break
|
|
247
|
-
case 'reject':
|
|
248
|
-
status = 'reject'
|
|
249
|
-
break
|
|
250
|
-
case 'accept':
|
|
251
|
-
status = 'accept'
|
|
252
|
-
break
|
|
253
|
-
default:
|
|
254
|
-
status = 'ringing'
|
|
255
|
-
break
|
|
256
|
-
}
|
|
257
|
-
return status
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: '
|
|
261
|
-
|
|
262
|
-
export const getCodeFromWSError = (error) => {
|
|
263
|
-
let statusCode = 500
|
|
264
|
-
if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
|
|
265
|
-
const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length)
|
|
266
|
-
if (!Number.isNaN(code) && code >= 400) statusCode = code
|
|
267
|
-
} else if (error?.code?.startsWith('E') || error?.message?.includes('timed out')) statusCode = 408
|
|
268
|
-
return statusCode
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
export const isWABusinessPlatform = (platform) => platform === 'smbi' || platform === 'smba'
|
|
272
|
-
|
|
273
|
-
export function trimUndefined(obj) {
|
|
274
|
-
for (const key in obj) if (typeof obj[key] === 'undefined') delete obj[key]
|
|
275
|
-
return obj
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ'
|
|
279
|
-
|
|
280
|
-
export function bytesToCrockford(buffer) {
|
|
281
|
-
let value = 0
|
|
282
|
-
let bitCount = 0
|
|
283
|
-
const crockford = []
|
|
284
|
-
for (const element of buffer) {
|
|
285
|
-
value = (value << 8) | (element & 0xff)
|
|
286
|
-
bitCount += 8
|
|
287
|
-
while (bitCount >= 5) {
|
|
288
|
-
crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31))
|
|
289
|
-
bitCount -= 5
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
if (bitCount > 0) crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31))
|
|
293
|
-
return crockford.join('')
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
export function encodeNewsletterMessage(message) {
|
|
297
|
-
return proto.Message.encode(message).finish()
|
|
1
|
+
import { Boom } from '@hapi/boom'
|
|
2
|
+
import { createHash, randomBytes } from 'crypto'
|
|
3
|
+
import { proto } from '../../WAProto/index.js'
|
|
4
|
+
const baileysVersion = [2, 3000, 1036200034]
|
|
5
|
+
import { DisconnectReason } from '../Types/index.js'
|
|
6
|
+
import { getAllBinaryNodeChildren, jidDecode } from '../WABinary/index.js'
|
|
7
|
+
import { sha256 } from './crypto.js'
|
|
8
|
+
|
|
9
|
+
export const BufferJSON = {
|
|
10
|
+
replacer: (k, value) => {
|
|
11
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
|
|
12
|
+
return { type: 'Buffer', data: Buffer.from(value?.data || value).toString('base64') }
|
|
13
|
+
}
|
|
14
|
+
return value
|
|
15
|
+
},
|
|
16
|
+
reviver: (_, value) => {
|
|
17
|
+
if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
|
|
18
|
+
return Buffer.from(value.data, 'base64')
|
|
19
|
+
}
|
|
20
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
21
|
+
const keys = Object.keys(value)
|
|
22
|
+
if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
|
|
23
|
+
const values = Object.values(value)
|
|
24
|
+
if (values.every(v => typeof v === 'number')) {
|
|
25
|
+
return Buffer.from(values)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return value
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const getKeyAuthor = (key, meId = 'me') => (key?.fromMe ? meId : key?.participantAlt || key?.remoteJidAlt || key?.participant || key?.remoteJid) || ''
|
|
34
|
+
|
|
35
|
+
export const isStringNullOrEmpty = (value) => value == null || value === ''
|
|
36
|
+
|
|
37
|
+
export const writeRandomPadMax16 = (msg) => {
|
|
38
|
+
const pad = randomBytes(1)
|
|
39
|
+
const padLength = (pad[0] & 0x0f) + 1
|
|
40
|
+
return Buffer.concat([msg, Buffer.alloc(padLength, padLength)])
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const unpadRandomMax16 = (e) => {
|
|
44
|
+
const t = new Uint8Array(e)
|
|
45
|
+
if (0 === t.length) throw new Error('unpadPkcs7 given empty bytes')
|
|
46
|
+
var r = t[t.length - 1]
|
|
47
|
+
if (r > t.length) throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`)
|
|
48
|
+
return new Uint8Array(t.buffer, t.byteOffset, t.length - r)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const generateParticipantHashV2 = (participants) => {
|
|
52
|
+
participants.sort()
|
|
53
|
+
const sha256Hash = sha256(Buffer.from(participants.join(''))).toString('base64')
|
|
54
|
+
return '2:' + sha256Hash.slice(0, 6)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const encodeWAMessage = (message) => {
|
|
58
|
+
try {
|
|
59
|
+
if (message && typeof message === 'object' && message.$type === proto.Message) {
|
|
60
|
+
return writeRandomPadMax16(proto.Message.encode(message).finish())
|
|
61
|
+
}
|
|
62
|
+
if (message && typeof message === 'object') {
|
|
63
|
+
const protoMessage = proto.Message.fromObject(message)
|
|
64
|
+
return writeRandomPadMax16(proto.Message.encode(protoMessage).finish())
|
|
65
|
+
}
|
|
66
|
+
return writeRandomPadMax16(proto.Message.encode(message).finish())
|
|
67
|
+
} catch (error) {
|
|
68
|
+
try {
|
|
69
|
+
return writeRandomPadMax16(proto.Message.encode(message).finish())
|
|
70
|
+
} catch (e) {
|
|
71
|
+
console.warn('Message encoding failed:', error.message, e.message)
|
|
72
|
+
throw new Error(`Failed to encode message: ${error.message}`)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const generateRegistrationId = () => Uint16Array.from(randomBytes(2))[0] & 16383
|
|
78
|
+
|
|
79
|
+
export const encodeBigEndian = (e, t = 4) => {
|
|
80
|
+
let r = e
|
|
81
|
+
const a = new Uint8Array(t)
|
|
82
|
+
for (let i = t - 1; i >= 0; i--) {
|
|
83
|
+
a[i] = 255 & r
|
|
84
|
+
r >>>= 8
|
|
85
|
+
}
|
|
86
|
+
return a
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const toNumber = (t) => typeof t === 'object' && t ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0
|
|
90
|
+
|
|
91
|
+
export const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000)
|
|
92
|
+
|
|
93
|
+
export const debouncedTimeout = (intervalMs = 1000, task) => {
|
|
94
|
+
let timeout
|
|
95
|
+
return {
|
|
96
|
+
start: (newIntervalMs, newTask) => {
|
|
97
|
+
task = newTask || task
|
|
98
|
+
intervalMs = newIntervalMs || intervalMs
|
|
99
|
+
timeout && clearTimeout(timeout)
|
|
100
|
+
timeout = setTimeout(() => task?.(), intervalMs)
|
|
101
|
+
},
|
|
102
|
+
cancel: () => {
|
|
103
|
+
timeout && clearTimeout(timeout)
|
|
104
|
+
timeout = undefined
|
|
105
|
+
},
|
|
106
|
+
setTask: (newTask) => (task = newTask),
|
|
107
|
+
setInterval: (newInterval) => (intervalMs = newInterval)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export const delay = (ms) => delayCancellable(ms).delay
|
|
112
|
+
|
|
113
|
+
export const delayCancellable = (ms) => {
|
|
114
|
+
const stack = new Error().stack
|
|
115
|
+
let timeout
|
|
116
|
+
let reject
|
|
117
|
+
const delay = new Promise((resolve, _reject) => {
|
|
118
|
+
timeout = setTimeout(resolve, ms)
|
|
119
|
+
reject = _reject
|
|
120
|
+
})
|
|
121
|
+
const cancel = () => {
|
|
122
|
+
clearTimeout(timeout)
|
|
123
|
+
reject(new Boom('Cancelled', { statusCode: 500, data: { stack } }))
|
|
124
|
+
}
|
|
125
|
+
return { delay, cancel }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export async function promiseTimeout(ms, promise) {
|
|
129
|
+
if (!ms) return new Promise(promise)
|
|
130
|
+
const stack = new Error().stack
|
|
131
|
+
const { delay, cancel } = delayCancellable(ms)
|
|
132
|
+
const p = new Promise((resolve, reject) => {
|
|
133
|
+
delay.then(() => reject(new Boom('Timed Out', { statusCode: DisconnectReason.timedOut, data: { stack } }))).catch(err => reject(err))
|
|
134
|
+
promise(resolve, reject)
|
|
135
|
+
}).finally(cancel)
|
|
136
|
+
return p
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export const generateMessageIDV2 = (userId) => {
|
|
140
|
+
const data = Buffer.alloc(8 + 20 + 16)
|
|
141
|
+
data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)))
|
|
142
|
+
if (userId) {
|
|
143
|
+
const id = jidDecode(userId)
|
|
144
|
+
if (id?.user) {
|
|
145
|
+
data.write(id.user, 8)
|
|
146
|
+
data.write('@c.us', 8 + id.user.length)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const random = randomBytes(16)
|
|
150
|
+
random.copy(data, 28)
|
|
151
|
+
const hash = createHash('sha256').update(data).digest()
|
|
152
|
+
return '3EB0' + hash.toString('hex').toUpperCase().substring(0, 18)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export const generateMessageID = () => '3EB0' + randomBytes(18).toString('hex').toUpperCase()
|
|
156
|
+
|
|
157
|
+
export function bindWaitForEvent(ev, event) {
|
|
158
|
+
return async (check, timeoutMs) => {
|
|
159
|
+
let listener
|
|
160
|
+
let closeListener
|
|
161
|
+
await promiseTimeout(timeoutMs, (resolve, reject) => {
|
|
162
|
+
closeListener = ({ connection, lastDisconnect }) => {
|
|
163
|
+
if (connection === 'close') reject(lastDisconnect?.error || new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed }))
|
|
164
|
+
}
|
|
165
|
+
ev.on('connection.update', closeListener)
|
|
166
|
+
listener = async (update) => {
|
|
167
|
+
if (await check(update)) resolve()
|
|
168
|
+
}
|
|
169
|
+
ev.on(event, listener)
|
|
170
|
+
}).finally(() => {
|
|
171
|
+
ev.off(event, listener)
|
|
172
|
+
ev.off('connection.update', closeListener)
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update')
|
|
178
|
+
|
|
179
|
+
export const fetchLatestBaileysVersion = async (options = {}) => {
|
|
180
|
+
const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/index.ts'
|
|
181
|
+
try {
|
|
182
|
+
const response = await fetch(URL, { dispatcher: options.dispatcher, method: 'GET', headers: options.headers })
|
|
183
|
+
if (!response.ok) throw new Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, { statusCode: response.status })
|
|
184
|
+
const text = await response.text()
|
|
185
|
+
const lines = text.split('\n')
|
|
186
|
+
const versionLine = lines[6]
|
|
187
|
+
const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/)
|
|
188
|
+
if (versionMatch) {
|
|
189
|
+
const version = [parseInt(versionMatch[1]), parseInt(versionMatch[2]), parseInt(versionMatch[3])]
|
|
190
|
+
return { version, isLatest: true }
|
|
191
|
+
} else throw new Error('Could not parse version from Defaults/index.ts')
|
|
192
|
+
} catch (error) {
|
|
193
|
+
return { version: baileysVersion, isLatest: false, error }
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export const fetchLatestWaWebVersion = async (options = {}) => {
|
|
198
|
+
try {
|
|
199
|
+
const defaultHeaders = { 'sec-fetch-site': 'none', 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' }
|
|
200
|
+
const headers = { ...defaultHeaders, ...options.headers }
|
|
201
|
+
const response = await fetch('https://web.whatsapp.com/sw.js', { ...options, method: 'GET', headers })
|
|
202
|
+
if (!response.ok) throw new Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status })
|
|
203
|
+
const data = await response.text()
|
|
204
|
+
const regex = /\\?"client_revision\\?":\s*(\d+)/
|
|
205
|
+
const match = data.match(regex)
|
|
206
|
+
if (!match?.[1]) return { version: baileysVersion, isLatest: false, error: { message: 'Could not find client revision in the fetched content' } }
|
|
207
|
+
const clientRevision = match[1]
|
|
208
|
+
return { version: [2, 3000, +clientRevision], isLatest: true }
|
|
209
|
+
} catch (error) {
|
|
210
|
+
return { version: baileysVersion, isLatest: false, error }
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export const generateMdTagPrefix = () => {
|
|
215
|
+
const bytes = randomBytes(4)
|
|
216
|
+
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const STATUS_MAP = { sender: proto.WebMessageInfo.Status.SERVER_ACK, played: proto.WebMessageInfo.Status.PLAYED, read: proto.WebMessageInfo.Status.READ, 'read-self': proto.WebMessageInfo.Status.READ }
|
|
220
|
+
|
|
221
|
+
export const getStatusFromReceiptType = (type) => {
|
|
222
|
+
const status = STATUS_MAP[type]
|
|
223
|
+
if (typeof type === 'undefined') return proto.WebMessageInfo.Status.DELIVERY_ACK
|
|
224
|
+
return status
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const CODE_MAP = { conflict: DisconnectReason.connectionReplaced }
|
|
228
|
+
|
|
229
|
+
export const getErrorCodeFromStreamError = (node) => {
|
|
230
|
+
const [reasonNode] = getAllBinaryNodeChildren(node)
|
|
231
|
+
let reason = reasonNode?.tag || 'unknown'
|
|
232
|
+
const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession)
|
|
233
|
+
if (statusCode === DisconnectReason.restartRequired) reason = 'restart required'
|
|
234
|
+
return { reason, statusCode }
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
238
|
+
let status
|
|
239
|
+
switch (tag) {
|
|
240
|
+
case 'offer':
|
|
241
|
+
case 'offer_notice':
|
|
242
|
+
status = 'offer'
|
|
243
|
+
break
|
|
244
|
+
case 'terminate':
|
|
245
|
+
status = attrs.reason === 'timeout' ? 'timeout' : 'terminate'
|
|
246
|
+
break
|
|
247
|
+
case 'reject':
|
|
248
|
+
status = 'reject'
|
|
249
|
+
break
|
|
250
|
+
case 'accept':
|
|
251
|
+
status = 'accept'
|
|
252
|
+
break
|
|
253
|
+
default:
|
|
254
|
+
status = 'ringing'
|
|
255
|
+
break
|
|
256
|
+
}
|
|
257
|
+
return status
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: '
|
|
261
|
+
|
|
262
|
+
export const getCodeFromWSError = (error) => {
|
|
263
|
+
let statusCode = 500
|
|
264
|
+
if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
|
|
265
|
+
const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length)
|
|
266
|
+
if (!Number.isNaN(code) && code >= 400) statusCode = code
|
|
267
|
+
} else if (error?.code?.startsWith('E') || error?.message?.includes('timed out')) statusCode = 408
|
|
268
|
+
return statusCode
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export const isWABusinessPlatform = (platform) => platform === 'smbi' || platform === 'smba'
|
|
272
|
+
|
|
273
|
+
export function trimUndefined(obj) {
|
|
274
|
+
for (const key in obj) if (typeof obj[key] === 'undefined') delete obj[key]
|
|
275
|
+
return obj
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ'
|
|
279
|
+
|
|
280
|
+
export function bytesToCrockford(buffer) {
|
|
281
|
+
let value = 0
|
|
282
|
+
let bitCount = 0
|
|
283
|
+
const crockford = []
|
|
284
|
+
for (const element of buffer) {
|
|
285
|
+
value = (value << 8) | (element & 0xff)
|
|
286
|
+
bitCount += 8
|
|
287
|
+
while (bitCount >= 5) {
|
|
288
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31))
|
|
289
|
+
bitCount -= 5
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (bitCount > 0) crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31))
|
|
293
|
+
return crockford.join('')
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export function encodeNewsletterMessage(message) {
|
|
297
|
+
return proto.Message.encode(message).finish()
|
|
298
298
|
}
|