violetics 7.0.1-alpha → 7.0.2-alpha
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 +3 -2
- package/README.md +1001 -232
- package/WAProto/index.js +75379 -142631
- package/engine-requirements.js +11 -8
- package/lib/Defaults/index.js +132 -146
- package/lib/Signal/Group/ciphertext-message.js +2 -6
- package/lib/Signal/Group/group-session-builder.js +7 -42
- package/lib/Signal/Group/group_cipher.js +37 -52
- package/lib/Signal/Group/index.js +11 -57
- package/lib/Signal/Group/keyhelper.js +7 -45
- package/lib/Signal/Group/sender-chain-key.js +7 -16
- package/lib/Signal/Group/sender-key-distribution-message.js +8 -12
- package/lib/Signal/Group/sender-key-message.js +9 -13
- package/lib/Signal/Group/sender-key-name.js +2 -6
- package/lib/Signal/Group/sender-key-record.js +9 -22
- package/lib/Signal/Group/sender-key-state.js +27 -43
- package/lib/Signal/Group/sender-message-key.js +4 -8
- package/lib/Signal/libsignal.js +319 -94
- package/lib/Signal/lid-mapping.js +224 -139
- package/lib/Socket/Client/index.js +2 -19
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +53 -0
- package/lib/Socket/business.js +162 -44
- package/lib/Socket/chats.js +477 -418
- package/lib/Socket/communities.js +430 -0
- package/lib/Socket/groups.js +110 -99
- package/lib/Socket/index.js +10 -10
- package/lib/Socket/messages-recv.js +884 -561
- package/lib/Socket/messages-send.js +859 -428
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter.js +195 -390
- package/lib/Socket/socket.js +465 -315
- package/lib/Store/index.js +3 -10
- package/lib/Store/make-in-memory-store.js +73 -79
- package/lib/Store/make-ordered-dictionary.js +4 -7
- package/lib/Store/object-repository.js +2 -6
- package/lib/Types/Auth.js +1 -2
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.js +1 -2
- package/lib/Types/Chat.js +7 -4
- package/lib/Types/Contact.js +1 -2
- package/lib/Types/Events.js +1 -2
- package/lib/Types/GroupMetadata.js +1 -2
- package/lib/Types/Label.js +2 -5
- package/lib/Types/LabelAssociation.js +2 -5
- package/lib/Types/Message.js +17 -9
- package/lib/Types/Newsletter.js +33 -38
- package/lib/Types/Product.js +1 -2
- package/lib/Types/Signal.js +1 -2
- package/lib/Types/Socket.js +2 -2
- package/lib/Types/State.js +12 -2
- package/lib/Types/USync.js +1 -2
- package/lib/Types/index.js +14 -31
- package/lib/Utils/auth-utils.js +228 -152
- package/lib/Utils/browser-utils.js +28 -0
- package/lib/Utils/business.js +66 -70
- package/lib/Utils/chat-utils.js +331 -249
- package/lib/Utils/crypto.js +57 -91
- package/lib/Utils/decode-wa-message.js +168 -84
- package/lib/Utils/event-buffer.js +138 -80
- package/lib/Utils/generics.js +180 -297
- package/lib/Utils/history.js +83 -49
- package/lib/Utils/identity-change-handler.js +48 -0
- package/lib/Utils/index.js +19 -33
- package/lib/Utils/link-preview.js +14 -23
- package/lib/Utils/logger.js +2 -7
- package/lib/Utils/lt-hash.js +2 -46
- package/lib/Utils/make-mutex.js +24 -47
- package/lib/Utils/message-retry-manager.js +224 -0
- package/lib/Utils/messages-media.js +501 -496
- package/lib/Utils/messages.js +1428 -362
- package/lib/Utils/noise-handler.js +145 -100
- package/lib/Utils/pre-key-manager.js +105 -0
- package/lib/Utils/process-message.js +356 -150
- package/lib/Utils/reporting-utils.js +257 -0
- package/lib/Utils/signal.js +78 -73
- package/lib/Utils/sync-action-utils.js +47 -0
- package/lib/Utils/tc-token-utils.js +17 -0
- package/lib/Utils/use-multi-file-auth-state.js +35 -45
- package/lib/Utils/validate-connection.js +91 -107
- package/lib/WABinary/constants.js +1300 -1304
- package/lib/WABinary/decode.js +26 -48
- package/lib/WABinary/encode.js +109 -155
- package/lib/WABinary/generic-utils.js +161 -149
- package/lib/WABinary/index.js +5 -21
- package/lib/WABinary/jid-utils.js +73 -40
- package/lib/WABinary/types.js +1 -2
- package/lib/WAM/BinaryInfo.js +2 -6
- package/lib/WAM/constants.js +19070 -11568
- package/lib/WAM/encode.js +17 -23
- package/lib/WAM/index.js +3 -19
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +8 -12
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -15
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -13
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -14
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -23
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +13 -9
- package/lib/WAUSync/Protocols/index.js +4 -20
- package/lib/WAUSync/USyncQuery.js +40 -36
- package/lib/WAUSync/USyncUser.js +2 -6
- package/lib/WAUSync/index.js +3 -19
- package/lib/index.js +11 -44
- package/package.json +74 -107
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Signal/Group/queue-job.js +0 -57
- package/lib/Socket/Client/abstract-socket-client.js +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.js +0 -118
- package/lib/Socket/groupStatus.js +0 -637
- package/lib/Socket/registration.js +0 -166
- package/lib/Socket/usync.js +0 -70
- package/lib/Store/make-cache-manager-store.js +0 -83
- package/lib/Utils/baileys-event-stream.js +0 -63
package/lib/Utils/generics.js
CHANGED
|
@@ -1,74 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const os_1 = require("os");
|
|
11
|
-
const fetch_1 = require("node-fetch")
|
|
12
|
-
const WAProto_1 = require("../../WAProto");
|
|
13
|
-
const baileys_version_json_1 = require("../Defaults/baileys-version.json");
|
|
14
|
-
const Types_1 = require("../Types");
|
|
15
|
-
const WABinary_1 = require("../WABinary");
|
|
16
|
-
const baileysVersion = [2, 3000, 1033846690]
|
|
17
|
-
const PLATFORM_MAP = {
|
|
18
|
-
'aix': 'AIX',
|
|
19
|
-
'darwin': 'Mac OS',
|
|
20
|
-
'win32': 'Windows',
|
|
21
|
-
'android': 'Android',
|
|
22
|
-
'freebsd': 'FreeBSD',
|
|
23
|
-
'openbsd': 'OpenBSD',
|
|
24
|
-
'sunos': 'Solaris',
|
|
25
|
-
'linux': undefined,
|
|
26
|
-
'haiku': undefined,
|
|
27
|
-
'cygwin': undefined,
|
|
28
|
-
'netbsd': undefined
|
|
29
|
-
};
|
|
30
|
-
const BrowsersFn = (browser) => {
|
|
31
|
-
return ['Windows', browser, '10.0.22631'];
|
|
32
|
-
};
|
|
33
|
-
BrowsersFn.ubuntu = (browser) => ['Ubuntu', browser, '22.04.4'];
|
|
34
|
-
BrowsersFn.macOS = (browser) => ['Mac OS', browser, '14.4.1'];
|
|
35
|
-
BrowsersFn.baileys = (browser) => ['Baileys', browser, '6.5.0'];
|
|
36
|
-
BrowsersFn.windows = (browser) => ['Windows', browser, '10.0.22631'];
|
|
37
|
-
BrowsersFn.appropriate = (browser) => [PLATFORM_MAP[require('os').platform()] || 'Ubuntu', browser, require('os').release()];
|
|
38
|
-
exports.Browsers = BrowsersFn;
|
|
39
|
-
|
|
40
|
-
const getPlatformId = (browser) => {
|
|
41
|
-
const platformType = WAProto_1.proto.DeviceProps.PlatformType[browser.toUpperCase()];
|
|
42
|
-
return platformType ? platformType.toString() : '1'; //chrome
|
|
43
|
-
};
|
|
44
|
-
exports.getPlatformId = getPlatformId;
|
|
45
|
-
exports.BufferJSON = {
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { createHash, randomBytes } from 'crypto';
|
|
3
|
+
import { proto } from '../../WAProto/index.js';
|
|
4
|
+
const baileysVersion = [2, 3000, 1033846690];
|
|
5
|
+
import { DisconnectReason } from '../Types/index.js';
|
|
6
|
+
import { getAllBinaryNodeChildren, jidDecode } from '../WABinary/index.js';
|
|
7
|
+
import { sha256 } from './crypto.js';
|
|
8
|
+
export const BufferJSON = {
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
46
10
|
replacer: (k, value) => {
|
|
47
|
-
if (Buffer.isBuffer(value) || value instanceof Uint8Array ||
|
|
48
|
-
return { type: 'Buffer', data: Buffer.from(
|
|
11
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
|
|
12
|
+
return { type: 'Buffer', data: Buffer.from(value?.data || value).toString('base64') };
|
|
49
13
|
}
|
|
50
14
|
return value;
|
|
51
15
|
},
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
17
|
reviver: (_, value) => {
|
|
53
|
-
if (typeof value === 'object' &&
|
|
54
|
-
|
|
55
|
-
|
|
18
|
+
if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
|
|
19
|
+
return Buffer.from(value.data, 'base64');
|
|
20
|
+
}
|
|
21
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
22
|
+
const keys = Object.keys(value);
|
|
23
|
+
if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
|
|
24
|
+
const values = Object.values(value);
|
|
25
|
+
if (values.every(v => typeof v === 'number')) {
|
|
26
|
+
return Buffer.from(values);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
56
29
|
}
|
|
57
30
|
return value;
|
|
58
31
|
}
|
|
59
32
|
};
|
|
60
|
-
const getKeyAuthor = (key, meId = 'me') => (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return Buffer.concat([msg, Buffer.alloc(pad[0], pad[0])]);
|
|
33
|
+
export const getKeyAuthor = (key, meId = 'me') => (key?.fromMe ? meId : key?.participantAlt || key?.remoteJidAlt || key?.participant || key?.remoteJid) || '';
|
|
34
|
+
export const isStringNullOrEmpty = (value) =>
|
|
35
|
+
// eslint-disable-next-line eqeqeq
|
|
36
|
+
value == null || value === '';
|
|
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)]);
|
|
69
41
|
};
|
|
70
|
-
|
|
71
|
-
const unpadRandomMax16 = (e) => {
|
|
42
|
+
export const unpadRandomMax16 = (e) => {
|
|
72
43
|
const t = new Uint8Array(e);
|
|
73
44
|
if (0 === t.length) {
|
|
74
45
|
throw new Error('unpadPkcs7 given empty bytes');
|
|
@@ -79,14 +50,17 @@ const unpadRandomMax16 = (e) => {
|
|
|
79
50
|
}
|
|
80
51
|
return new Uint8Array(t.buffer, t.byteOffset, t.length - r);
|
|
81
52
|
};
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
return
|
|
53
|
+
// code is inspired by whatsmeow
|
|
54
|
+
export const generateParticipantHashV2 = (participants) => {
|
|
55
|
+
participants.sort();
|
|
56
|
+
const sha256Hash = sha256(Buffer.from(participants.join(''))).toString('base64');
|
|
57
|
+
return '2:' + sha256Hash.slice(0, 6);
|
|
58
|
+
};
|
|
59
|
+
export const encodeWAMessage = (message) => writeRandomPadMax16(proto.Message.encode(message).finish());
|
|
60
|
+
export const generateRegistrationId = () => {
|
|
61
|
+
return Uint16Array.from(randomBytes(2))[0] & 16383;
|
|
87
62
|
};
|
|
88
|
-
|
|
89
|
-
const encodeBigEndian = (e, t = 4) => {
|
|
63
|
+
export const encodeBigEndian = (e, t = 4) => {
|
|
90
64
|
let r = e;
|
|
91
65
|
const a = new Uint8Array(t);
|
|
92
66
|
for (let i = t - 1; i >= 0; i--) {
|
|
@@ -95,33 +69,28 @@ const encodeBigEndian = (e, t = 4) => {
|
|
|
95
69
|
}
|
|
96
70
|
return a;
|
|
97
71
|
};
|
|
98
|
-
|
|
99
|
-
const toNumber = (t) => ((typeof t === 'object' && t) ? ('toNumber' in t ? t.toNumber() : t.low) : t);
|
|
100
|
-
exports.toNumber = toNumber;
|
|
72
|
+
export const toNumber = (t) => typeof t === 'object' && t ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0;
|
|
101
73
|
/** unix timestamp of a date in seconds */
|
|
102
|
-
const unixTimestampSeconds = (date =
|
|
103
|
-
|
|
104
|
-
const debouncedTimeout = (intervalMs = 1000, task) => {
|
|
74
|
+
export const unixTimestampSeconds = (date = Date.now()) => date / 1000 | 0; // Changed from new Date → Date.now for faster operation
|
|
75
|
+
export const debouncedTimeout = (intervalMs = 1000, task) => {
|
|
105
76
|
let timeout;
|
|
106
77
|
return {
|
|
107
78
|
start: (newIntervalMs, newTask) => {
|
|
108
79
|
task = newTask || task;
|
|
109
80
|
intervalMs = newIntervalMs || intervalMs;
|
|
110
81
|
timeout && clearTimeout(timeout);
|
|
111
|
-
timeout = setTimeout(() => task
|
|
82
|
+
timeout = setTimeout(() => task?.(), intervalMs);
|
|
112
83
|
},
|
|
113
84
|
cancel: () => {
|
|
114
85
|
timeout && clearTimeout(timeout);
|
|
115
86
|
timeout = undefined;
|
|
116
87
|
},
|
|
117
|
-
setTask: (newTask) => task = newTask,
|
|
118
|
-
setInterval: (newInterval) => intervalMs = newInterval
|
|
88
|
+
setTask: (newTask) => (task = newTask),
|
|
89
|
+
setInterval: (newInterval) => (intervalMs = newInterval)
|
|
119
90
|
};
|
|
120
91
|
};
|
|
121
|
-
|
|
122
|
-
const
|
|
123
|
-
exports.delay = delay;
|
|
124
|
-
const delayCancellable = (ms) => {
|
|
92
|
+
export const delay = (ms) => delayCancellable(ms).delay;
|
|
93
|
+
export const delayCancellable = (ms) => {
|
|
125
94
|
const stack = new Error().stack;
|
|
126
95
|
let timeout;
|
|
127
96
|
let reject;
|
|
@@ -131,7 +100,7 @@ const delayCancellable = (ms) => {
|
|
|
131
100
|
});
|
|
132
101
|
const cancel = () => {
|
|
133
102
|
clearTimeout(timeout);
|
|
134
|
-
reject(new
|
|
103
|
+
reject(new Boom('Cancelled', {
|
|
135
104
|
statusCode: 500,
|
|
136
105
|
data: {
|
|
137
106
|
stack
|
|
@@ -140,273 +109,190 @@ const delayCancellable = (ms) => {
|
|
|
140
109
|
};
|
|
141
110
|
return { delay, cancel };
|
|
142
111
|
};
|
|
143
|
-
|
|
144
|
-
const encoded = async (type, query) => {
|
|
145
|
-
const buffer = Buffer.from(query, 'utf-8');
|
|
146
|
-
switch (type.toLowerCase()) {
|
|
147
|
-
case 'base64':
|
|
148
|
-
return buffer.toString('base64');
|
|
149
|
-
case 'base32':
|
|
150
|
-
const base32Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
|
|
151
|
-
let bits = 0;
|
|
152
|
-
let value = 0;
|
|
153
|
-
let output = '';
|
|
154
|
-
for (let i = 0; i < buffer.length; i++) {
|
|
155
|
-
value = (value << 8) | buffer[i];
|
|
156
|
-
bits += 8;
|
|
157
|
-
while (bits >= 5) {
|
|
158
|
-
output += base32Alphabet[(value >>> (bits - 5)) & 31];
|
|
159
|
-
bits -= 5;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
if (bits > 0) {
|
|
163
|
-
output += base32Alphabet[(value << (5 - bits)) & 31];
|
|
164
|
-
}
|
|
165
|
-
while (output.length % 8 !== 0) {
|
|
166
|
-
output += '=';
|
|
167
|
-
}
|
|
168
|
-
return output;
|
|
169
|
-
case 'hex':
|
|
170
|
-
case 'base16':
|
|
171
|
-
return buffer.toString('hex');
|
|
172
|
-
case 'binary':
|
|
173
|
-
return Array.from(buffer).map(byte => byte.toString(2).padStart(8, '0')).join(' ');
|
|
174
|
-
case 'base64url':
|
|
175
|
-
return buffer.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
176
|
-
default:
|
|
177
|
-
throw new Error(`Type ${type} not supported`);
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
exports.encoded = encoded;
|
|
181
|
-
const decoded = async (type, encodedStr) => {
|
|
182
|
-
switch (type.toLowerCase()) {
|
|
183
|
-
case 'base64':
|
|
184
|
-
return Buffer.from(encodedStr, 'base64').toString('utf-8');
|
|
185
|
-
case 'base32':
|
|
186
|
-
const base32Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
|
|
187
|
-
const cleanStr = encodedStr.toUpperCase().replace(/=+$/, '');
|
|
188
|
-
let bits = 0;
|
|
189
|
-
let value = 0;
|
|
190
|
-
let output = [];
|
|
191
|
-
for (let i = 0; i < cleanStr.length; i++) {
|
|
192
|
-
const charIndex = base32Alphabet.indexOf(cleanStr[i]);
|
|
193
|
-
if (charIndex === -1) continue;
|
|
194
|
-
value = (value << 5) | charIndex;
|
|
195
|
-
bits += 5;
|
|
196
|
-
while (bits >= 8) {
|
|
197
|
-
output.push((value >>> (bits - 8)) & 255);
|
|
198
|
-
bits -= 8;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
return Buffer.from(output).toString('utf-8');
|
|
202
|
-
case 'hex':
|
|
203
|
-
case 'base16':
|
|
204
|
-
return Buffer.from(encodedStr, 'hex').toString('utf-8');
|
|
205
|
-
case 'binary':
|
|
206
|
-
const bytes = encodedStr.split(' ').map(bin => parseInt(bin, 2));
|
|
207
|
-
return Buffer.from(bytes).toString('utf-8');
|
|
208
|
-
case 'base64url':
|
|
209
|
-
const base64 = encodedStr.replace(/-/g, '+').replace(/_/g, '/');
|
|
210
|
-
return Buffer.from(base64, 'base64').toString('utf-8');
|
|
211
|
-
default:
|
|
212
|
-
throw new Error(`Type ${type} not supported`);
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
exports.decoded = decoded;
|
|
216
|
-
async function promiseTimeout(ms, promise) {
|
|
112
|
+
export async function promiseTimeout(ms, promise) {
|
|
217
113
|
if (!ms) {
|
|
218
114
|
return new Promise(promise);
|
|
219
115
|
}
|
|
220
116
|
const stack = new Error().stack;
|
|
221
117
|
// Create a promise that rejects in <ms> milliseconds
|
|
222
|
-
const { delay, cancel } =
|
|
118
|
+
const { delay, cancel } = delayCancellable(ms);
|
|
223
119
|
const p = new Promise((resolve, reject) => {
|
|
224
120
|
delay
|
|
225
|
-
.then(() => reject(new
|
|
226
|
-
statusCode:
|
|
121
|
+
.then(() => reject(new Boom('Timed Out', {
|
|
122
|
+
statusCode: DisconnectReason.timedOut,
|
|
227
123
|
data: {
|
|
228
124
|
stack
|
|
229
125
|
}
|
|
230
126
|
})))
|
|
231
127
|
.catch(err => reject(err));
|
|
232
128
|
promise(resolve, reject);
|
|
233
|
-
})
|
|
234
|
-
.finally(cancel);
|
|
129
|
+
}).finally(cancel);
|
|
235
130
|
return p;
|
|
236
131
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
132
|
+
// inspired from whatsmeow code
|
|
133
|
+
// https://github.com/tulir/whatsmeow/blob/64bc969fbe78d31ae0dd443b8d4c80a5d026d07a/send.go#L42
|
|
134
|
+
export const generateMessageIDV2 = (userId) => {
|
|
135
|
+
const data = Buffer.allocUnsafe(8 + 20 + 16);
|
|
136
|
+
data.writeBigUInt64BE(BigInt(unixTimestampSeconds()));
|
|
137
|
+
if (userId) {
|
|
138
|
+
const id = jidDecode(userId);
|
|
139
|
+
if (id?.user) {
|
|
140
|
+
const len = data.write(id.user, 8);
|
|
141
|
+
data.write('@c.us', 8 + len);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
randomBytes(16).copy(data, 28);
|
|
145
|
+
const hash = createHash('sha256').update(data).digest();
|
|
146
|
+
const hex = hash.toString('hex').toUpperCase();
|
|
147
|
+
const baseId = '3EB0' + hex.substring(0, 18);
|
|
148
|
+
const pos = 4 + (hash[0] & 15);
|
|
149
|
+
return baseId.slice(0, pos) + 'STARFALL' + baseId.slice(pos);
|
|
252
150
|
};
|
|
253
|
-
exports.generateMessageIDV2 = generateMessageIDV2;
|
|
254
151
|
// generate a random ID to attach to a message
|
|
255
|
-
const generateMessageID = () => '
|
|
256
|
-
|
|
257
|
-
function bindWaitForEvent(ev, event) {
|
|
152
|
+
export const generateMessageID = () => '3EB0' + randomBytes(18).toString('hex').toUpperCase();
|
|
153
|
+
export function bindWaitForEvent(ev, event) {
|
|
258
154
|
return async (check, timeoutMs) => {
|
|
259
155
|
let listener;
|
|
260
156
|
let closeListener;
|
|
261
|
-
await
|
|
157
|
+
await promiseTimeout(timeoutMs, (resolve, reject) => {
|
|
262
158
|
closeListener = ({ connection, lastDisconnect }) => {
|
|
263
159
|
if (connection === 'close') {
|
|
264
|
-
reject(
|
|
265
|
-
|| new boom_1.Boom('Connection Closed', { statusCode: Types_1.DisconnectReason.connectionClosed }));
|
|
160
|
+
reject(lastDisconnect?.error || new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed }));
|
|
266
161
|
}
|
|
267
162
|
};
|
|
268
163
|
ev.on('connection.update', closeListener);
|
|
269
|
-
listener = (update) => {
|
|
270
|
-
if (check(update)) {
|
|
164
|
+
listener = async (update) => {
|
|
165
|
+
if (await check(update)) {
|
|
271
166
|
resolve();
|
|
272
167
|
}
|
|
273
168
|
};
|
|
274
169
|
ev.on(event, listener);
|
|
275
|
-
})
|
|
276
|
-
.finally(() => {
|
|
170
|
+
}).finally(() => {
|
|
277
171
|
ev.off(event, listener);
|
|
278
172
|
ev.off('connection.update', closeListener);
|
|
279
|
-
})
|
|
173
|
+
});
|
|
280
174
|
};
|
|
281
175
|
}
|
|
282
|
-
|
|
283
|
-
const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update');
|
|
284
|
-
exports.bindWaitForConnectionUpdate = bindWaitForConnectionUpdate;
|
|
285
|
-
const printQRIfNecessaryListener = (ev, logger) => {
|
|
286
|
-
ev.on('connection.update', async ({ qr }) => {
|
|
287
|
-
if (qr) {
|
|
288
|
-
const QR = await import('qrcode-terminal')
|
|
289
|
-
.then(m => m.default || m)
|
|
290
|
-
.catch(() => {
|
|
291
|
-
logger.error('QR code terminal not added as dependency');
|
|
292
|
-
});
|
|
293
|
-
QR === null || QR === void 0 ? void 0 : QR.generate(qr, { small: true });
|
|
294
|
-
}
|
|
295
|
-
});
|
|
296
|
-
};
|
|
297
|
-
exports.printQRIfNecessaryListener = printQRIfNecessaryListener;
|
|
176
|
+
export const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update');
|
|
298
177
|
/**
|
|
299
178
|
* utility that fetches latest baileys version from the master branch.
|
|
300
179
|
* Use to ensure your WA connection is always on the latest version
|
|
301
180
|
*/
|
|
302
|
-
const
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
...options.headers
|
|
181
|
+
export const fetchLatestBaileysVersion = async (options = {}) => {
|
|
182
|
+
const URL = 'https://raw.githubusercontent.com/itsliaaa/baileys/main/lib/Defaults/index.js';
|
|
183
|
+
try {
|
|
184
|
+
const response = await fetch(URL, {
|
|
185
|
+
dispatcher: options.dispatcher,
|
|
186
|
+
method: 'GET',
|
|
187
|
+
headers: options.headers
|
|
188
|
+
});
|
|
189
|
+
if (!response.ok) {
|
|
190
|
+
throw new Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, { statusCode: response.status });
|
|
313
191
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
version: [2, 3000, +clientRevision],
|
|
339
|
-
isLatest: true
|
|
340
|
-
}
|
|
341
|
-
} catch (error) {
|
|
342
|
-
return {
|
|
343
|
-
version: baileysVersion,
|
|
344
|
-
isLatest: false,
|
|
345
|
-
error
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
exports.fetchLatestWaWebVersion = fetchLatestWaWebVersion;
|
|
192
|
+
const text = await response.text();
|
|
193
|
+
// Extract version from line 5 (const version = [...])
|
|
194
|
+
const lines = text.split('\n');
|
|
195
|
+
const versionLine = lines[4]; // Line 5 (0-indexed)
|
|
196
|
+
const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/);
|
|
197
|
+
if (versionMatch) {
|
|
198
|
+
const version = [parseInt(versionMatch[1]), parseInt(versionMatch[2]), parseInt(versionMatch[3])];
|
|
199
|
+
return {
|
|
200
|
+
version,
|
|
201
|
+
isLatest: true
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
throw new Error('Could not parse version from Defaults/index.ts');
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
return {
|
|
210
|
+
version: baileysVersion,
|
|
211
|
+
isLatest: false,
|
|
212
|
+
error
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
};
|
|
350
216
|
/**
|
|
351
|
-
* utility that fetches latest
|
|
217
|
+
* A utility that fetches the latest web version of whatsapp.
|
|
352
218
|
* Use to ensure your WA connection is always on the latest version
|
|
353
219
|
*/
|
|
354
|
-
const
|
|
355
|
-
const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/baileys-version.json';
|
|
220
|
+
export const fetchLatestWaWebVersion = async (options = {}) => {
|
|
356
221
|
try {
|
|
357
|
-
|
|
222
|
+
// Absolute minimal headers required to bypass anti-bot detection
|
|
223
|
+
const defaultHeaders = {
|
|
224
|
+
'sec-fetch-site': 'none',
|
|
225
|
+
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
|
|
226
|
+
};
|
|
227
|
+
const headers = { ...defaultHeaders, ...options.headers };
|
|
228
|
+
const response = await fetch('https://web.whatsapp.com/sw.js', {
|
|
358
229
|
...options,
|
|
359
|
-
|
|
230
|
+
method: 'GET',
|
|
231
|
+
headers
|
|
360
232
|
});
|
|
233
|
+
if (!response.ok) {
|
|
234
|
+
throw new Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status });
|
|
235
|
+
}
|
|
236
|
+
const data = await response.text();
|
|
237
|
+
const regex = /\\?"client_revision\\?":\s*(\d+)/;
|
|
238
|
+
const match = data.match(regex);
|
|
239
|
+
if (!match?.[1]) {
|
|
240
|
+
return {
|
|
241
|
+
version: baileysVersion,
|
|
242
|
+
isLatest: false,
|
|
243
|
+
error: {
|
|
244
|
+
message: 'Could not find client revision in the fetched content'
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
const clientRevision = match[1];
|
|
361
249
|
return {
|
|
362
|
-
version:
|
|
250
|
+
version: [2, 3000, +clientRevision],
|
|
363
251
|
isLatest: true
|
|
364
252
|
};
|
|
365
253
|
}
|
|
366
254
|
catch (error) {
|
|
367
255
|
return {
|
|
368
|
-
version:
|
|
256
|
+
version: baileysVersion,
|
|
369
257
|
isLatest: false,
|
|
370
258
|
error
|
|
371
259
|
};
|
|
372
260
|
}
|
|
373
261
|
};
|
|
374
|
-
exports.fetchLatestBaileysVersion = fetchLatestBaileysVersion;
|
|
375
262
|
/** unique message tag prefix for MD clients */
|
|
376
|
-
const generateMdTagPrefix = () => {
|
|
377
|
-
const bytes =
|
|
263
|
+
export const generateMdTagPrefix = () => {
|
|
264
|
+
const bytes = randomBytes(4);
|
|
378
265
|
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
|
|
379
266
|
};
|
|
380
|
-
exports.generateMdTagPrefix = generateMdTagPrefix;
|
|
381
267
|
const STATUS_MAP = {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
268
|
+
sender: proto.WebMessageInfo.Status.SERVER_ACK,
|
|
269
|
+
played: proto.WebMessageInfo.Status.PLAYED,
|
|
270
|
+
read: proto.WebMessageInfo.Status.READ,
|
|
271
|
+
'read-self': proto.WebMessageInfo.Status.READ
|
|
385
272
|
};
|
|
386
273
|
/**
|
|
387
274
|
* Given a type of receipt, returns what the new status of the message should be
|
|
388
275
|
* @param type type from receipt
|
|
389
276
|
*/
|
|
390
|
-
const getStatusFromReceiptType = (type) => {
|
|
277
|
+
export const getStatusFromReceiptType = (type) => {
|
|
391
278
|
const status = STATUS_MAP[type];
|
|
392
279
|
if (typeof type === 'undefined') {
|
|
393
|
-
return
|
|
280
|
+
return proto.WebMessageInfo.Status.DELIVERY_ACK;
|
|
394
281
|
}
|
|
395
282
|
return status;
|
|
396
283
|
};
|
|
397
|
-
exports.getStatusFromReceiptType = getStatusFromReceiptType;
|
|
398
284
|
const CODE_MAP = {
|
|
399
|
-
conflict:
|
|
285
|
+
conflict: DisconnectReason.connectionReplaced
|
|
400
286
|
};
|
|
401
287
|
/**
|
|
402
288
|
* Stream errors generally provide a reason, map that to a baileys DisconnectReason
|
|
403
289
|
* @param reason the string reason given, eg. "conflict"
|
|
404
290
|
*/
|
|
405
|
-
const getErrorCodeFromStreamError = (node) => {
|
|
406
|
-
const [reasonNode] =
|
|
407
|
-
let reason =
|
|
408
|
-
const statusCode = +(node.attrs.code || CODE_MAP[reason] ||
|
|
409
|
-
if (statusCode ===
|
|
291
|
+
export const getErrorCodeFromStreamError = (node) => {
|
|
292
|
+
const [reasonNode] = getAllBinaryNodeChildren(node);
|
|
293
|
+
let reason = reasonNode?.tag || 'unknown';
|
|
294
|
+
const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession);
|
|
295
|
+
if (statusCode === DisconnectReason.restartRequired) {
|
|
410
296
|
reason = 'restart required';
|
|
411
297
|
}
|
|
412
298
|
return {
|
|
@@ -414,8 +300,7 @@ const getErrorCodeFromStreamError = (node) => {
|
|
|
414
300
|
statusCode
|
|
415
301
|
};
|
|
416
302
|
};
|
|
417
|
-
|
|
418
|
-
const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
303
|
+
export const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
419
304
|
let status;
|
|
420
305
|
switch (tag) {
|
|
421
306
|
case 'offer':
|
|
@@ -427,7 +312,8 @@ const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
|
427
312
|
status = 'timeout';
|
|
428
313
|
}
|
|
429
314
|
else {
|
|
430
|
-
|
|
315
|
+
//fired when accepted/rejected/timeout/caller hangs up
|
|
316
|
+
status = 'terminate';
|
|
431
317
|
}
|
|
432
318
|
break;
|
|
433
319
|
case 'reject':
|
|
@@ -442,33 +328,33 @@ const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
|
442
328
|
}
|
|
443
329
|
return status;
|
|
444
330
|
};
|
|
445
|
-
exports.getCallStatusFromNode = getCallStatusFromNode;
|
|
446
331
|
const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: ';
|
|
447
|
-
const getCodeFromWSError = (error) => {
|
|
448
|
-
var _a, _b, _c;
|
|
332
|
+
export const getCodeFromWSError = (error) => {
|
|
449
333
|
let statusCode = 500;
|
|
450
|
-
if (
|
|
451
|
-
const code = +
|
|
334
|
+
if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
|
|
335
|
+
const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length);
|
|
452
336
|
if (!Number.isNaN(code) && code >= 400) {
|
|
453
337
|
statusCode = code;
|
|
454
338
|
}
|
|
455
339
|
}
|
|
456
|
-
else if (
|
|
457
|
-
|
|
340
|
+
else if (
|
|
341
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
342
|
+
error?.code?.startsWith('E') ||
|
|
343
|
+
error?.message?.includes('timed out')) {
|
|
344
|
+
// handle ETIMEOUT, ENOTFOUND etc
|
|
458
345
|
statusCode = 408;
|
|
459
346
|
}
|
|
460
347
|
return statusCode;
|
|
461
348
|
};
|
|
462
|
-
exports.getCodeFromWSError = getCodeFromWSError;
|
|
463
349
|
/**
|
|
464
350
|
* Is the given platform WA business
|
|
465
351
|
* @param platform AuthenticationCreds.platform
|
|
466
352
|
*/
|
|
467
|
-
const isWABusinessPlatform = (platform) => {
|
|
353
|
+
export const isWABusinessPlatform = (platform) => {
|
|
468
354
|
return platform === 'smbi' || platform === 'smba';
|
|
469
355
|
};
|
|
470
|
-
|
|
471
|
-
function trimUndefined(obj) {
|
|
356
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
357
|
+
export function trimUndefined(obj) {
|
|
472
358
|
for (const key in obj) {
|
|
473
359
|
if (typeof obj[key] === 'undefined') {
|
|
474
360
|
delete obj[key];
|
|
@@ -476,14 +362,13 @@ function trimUndefined(obj) {
|
|
|
476
362
|
}
|
|
477
363
|
return obj;
|
|
478
364
|
}
|
|
479
|
-
exports.trimUndefined = trimUndefined;
|
|
480
365
|
const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ';
|
|
481
|
-
function bytesToCrockford(buffer) {
|
|
366
|
+
export function bytesToCrockford(buffer) {
|
|
482
367
|
let value = 0;
|
|
483
368
|
let bitCount = 0;
|
|
484
369
|
const crockford = [];
|
|
485
|
-
for (
|
|
486
|
-
value = (value << 8) | (
|
|
370
|
+
for (const element of buffer) {
|
|
371
|
+
value = (value << 8) | (element & 0xff);
|
|
487
372
|
bitCount += 8;
|
|
488
373
|
while (bitCount >= 5) {
|
|
489
374
|
crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31));
|
|
@@ -495,8 +380,6 @@ function bytesToCrockford(buffer) {
|
|
|
495
380
|
}
|
|
496
381
|
return crockford.join('');
|
|
497
382
|
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
}
|
|
502
|
-
exports.encodeNewsletterMessage = encodeNewsletterMessage;
|
|
383
|
+
export function encodeNewsletterMessage(message) {
|
|
384
|
+
return proto.Message.encode(message).finish();
|
|
385
|
+
}
|