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