@baileys-md/baileys 11.2.4 → 12.0.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/LICENSE +1 -1
- package/WAProto/WAProto.proto +5311 -0
- package/WAProto/index.js +70084 -131686
- package/lib/Defaults/index.js +118 -117
- package/lib/KeyDB/BinarySearch.js +20 -0
- package/lib/KeyDB/KeyedDB.js +167 -0
- package/lib/KeyDB/index.js +4 -0
- package/lib/Signal/Group/ciphertext-message.js +13 -0
- package/lib/Signal/Group/group-session-builder.js +32 -0
- package/lib/Signal/Group/group_cipher.js +84 -0
- package/lib/Signal/Group/index.js +13 -0
- package/lib/Signal/Group/keyhelper.js +20 -0
- package/lib/Signal/Group/sender-chain-key.js +28 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +65 -0
- package/lib/Signal/Group/sender-key-message.js +68 -0
- package/{WASignalGroup/sender_key_name.js → lib/Signal/Group/sender-key-name.js} +10 -28
- package/lib/Signal/Group/sender-key-record.js +43 -0
- package/lib/Signal/Group/sender-key-state.js +86 -0
- package/lib/Signal/Group/sender-message-key.js +28 -0
- package/lib/Signal/libsignal.js +314 -151
- package/lib/Signal/lid-mapping.js +155 -0
- package/lib/Socket/Client/index.js +4 -19
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.js +52 -0
- package/lib/Socket/business.js +359 -242
- package/lib/Socket/chats.js +847 -844
- package/lib/Socket/communities.js +413 -0
- package/lib/Socket/groups.js +304 -319
- package/lib/Socket/index.js +15 -9
- package/lib/Socket/messages-recv.js +1109 -989
- package/lib/Socket/messages-send.js +611 -347
- package/lib/Socket/mex.js +45 -0
- package/lib/Socket/newsletter.js +230 -231
- package/lib/Socket/socket.js +795 -616
- package/lib/Store/index.js +6 -8
- package/lib/Store/make-cache-manager-store.js +73 -81
- package/lib/Store/make-in-memory-store.js +286 -427
- package/lib/Store/make-ordered-dictionary.js +77 -79
- package/lib/Store/object-repository.js +24 -26
- package/lib/Types/Auth.js +3 -2
- package/lib/Types/Bussines.js +3 -0
- package/lib/Types/Call.js +3 -2
- package/lib/Types/Chat.js +9 -4
- package/lib/Types/Contact.js +3 -2
- package/lib/Types/Events.js +3 -2
- package/lib/Types/GroupMetadata.js +3 -2
- package/lib/Types/Label.js +24 -26
- package/lib/Types/LabelAssociation.js +6 -8
- package/lib/Types/Message.js +12 -7
- package/lib/Types/Newsletter.js +32 -17
- package/lib/Types/Product.js +3 -2
- package/lib/Types/Signal.js +3 -2
- package/lib/Types/Socket.js +4 -2
- package/lib/Types/State.js +11 -2
- package/lib/Types/USync.js +3 -2
- package/lib/Types/index.js +27 -41
- package/lib/Utils/auth-utils.js +211 -198
- package/lib/Utils/baileys-event-stream.js +42 -61
- package/lib/Utils/browser-utils.js +25 -0
- package/lib/Utils/business.js +213 -214
- package/lib/Utils/chat-utils.js +711 -689
- package/lib/Utils/crypto.js +112 -133
- package/lib/Utils/decode-wa-message.js +254 -186
- package/lib/Utils/event-buffer.js +510 -502
- package/lib/Utils/generics.js +318 -408
- package/lib/Utils/history.js +83 -90
- package/lib/Utils/index.js +21 -33
- package/lib/Utils/link-preview.js +71 -116
- package/lib/Utils/logger.js +5 -7
- package/lib/Utils/lt-hash.js +40 -46
- package/lib/Utils/make-mutex.js +34 -41
- package/lib/Utils/message-retry-manager.js +113 -0
- package/lib/Utils/messages-media.js +575 -671
- package/lib/Utils/messages.js +354 -462
- package/lib/Utils/noise-handler.js +138 -149
- package/lib/Utils/pre-key-manager.js +85 -0
- package/lib/Utils/process-message.js +323 -354
- package/lib/Utils/signal.js +148 -130
- package/lib/Utils/use-multi-file-auth-state.js +109 -91
- package/lib/Utils/validate-connection.js +183 -190
- package/lib/WABinary/constants.js +1298 -35
- package/lib/WABinary/decode.js +231 -256
- package/lib/WABinary/encode.js +207 -239
- package/lib/WABinary/generic-utils.js +119 -40
- package/lib/WABinary/index.js +7 -21
- package/lib/WABinary/jid-utils.js +88 -64
- package/lib/WABinary/types.js +3 -2
- package/lib/WAM/BinaryInfo.js +10 -12
- package/lib/WAM/constants.js +22851 -15348
- package/lib/WAM/encode.js +135 -136
- package/lib/WAM/index.js +5 -19
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -30
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +49 -53
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -28
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +36 -39
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +26 -20
- package/lib/WAUSync/Protocols/index.js +6 -20
- package/lib/WAUSync/USyncQuery.js +86 -85
- package/lib/WAUSync/USyncUser.js +23 -25
- package/lib/WAUSync/index.js +5 -19
- package/lib/index.js +18 -33
- package/package.json +52 -57
- package/README.md +0 -2
- package/WASignalGroup/GroupProtocol.js +0 -1697
- package/WASignalGroup/ciphertext_message.js +0 -16
- package/WASignalGroup/group_cipher.js +0 -120
- package/WASignalGroup/group_session_builder.js +0 -46
- package/WASignalGroup/index.js +0 -5
- package/WASignalGroup/keyhelper.js +0 -21
- package/WASignalGroup/protobufs.js +0 -3
- package/WASignalGroup/queue_job.js +0 -69
- package/WASignalGroup/sender_chain_key.js +0 -50
- package/WASignalGroup/sender_key_distribution_message.js +0 -78
- package/WASignalGroup/sender_key_message.js +0 -92
- package/WASignalGroup/sender_key_record.js +0 -56
- package/WASignalGroup/sender_key_state.js +0 -129
- package/WASignalGroup/sender_message_key.js +0 -39
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- 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 -62
- package/lib/Socket/registration.js +0 -166
- package/lib/Socket/usync.js +0 -70
- package/lib/Store/make-mongo-store.js +0 -567
package/lib/Utils/generics.js
CHANGED
|
@@ -1,445 +1,355 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { getAllBinaryNodeChildren, jidDecode } from "../WABinary/index.js";
|
|
3
|
+
import { DisconnectReason } from "../Types/index.js";
|
|
4
|
+
import { createHash, randomBytes } from "crypto";
|
|
5
|
+
import { proto } from "../../WAProto/index.js";
|
|
6
|
+
import { version } from "../Defaults/index.js"
|
|
7
|
+
import { sha256 } from "./crypto.js";
|
|
8
|
+
import { Boom } from "@hapi/boom";
|
|
9
|
+
//=======================================================//
|
|
10
|
+
export const BufferJSON = {
|
|
11
|
+
replacer: (k, value) => {
|
|
12
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === "Buffer") {
|
|
13
|
+
return { type: "Buffer", data: Buffer.from(value?.data || value).toString("base64") };
|
|
7
14
|
}
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
return value;
|
|
16
|
+
},
|
|
17
|
+
reviver: (_, value) => {
|
|
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
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
37
32
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const boom_1 = require("@hapi/boom");
|
|
45
|
-
const axios_1 = __importDefault(require("axios"));
|
|
46
|
-
const crypto_1 = require("crypto");
|
|
47
|
-
const os_1 = require("os");
|
|
48
|
-
const WAProto_1 = require("../../WAProto");
|
|
49
|
-
const baileys_version_json_1 = require("../Defaults/baileys-version.json");
|
|
50
|
-
const Types_1 = require("../Types");
|
|
51
|
-
const WABinary_1 = require("../WABinary");
|
|
52
|
-
const COMPANION_PLATFORM_MAP = {
|
|
53
|
-
'Chrome': '49',
|
|
54
|
-
'Edge': '50',
|
|
55
|
-
'Firefox': '51',
|
|
56
|
-
'Opera': '53',
|
|
57
|
-
'Safari': '54'
|
|
33
|
+
//=======================================================//
|
|
34
|
+
export const getKeyAuthor = (key, meId = "me") => (key?.fromMe ? meId : key?.participant || key?.remoteJid) || "";
|
|
35
|
+
export const writeRandomPadMax16 = (msg) => {
|
|
36
|
+
const pad = randomBytes(1);
|
|
37
|
+
const padLength = (pad[0] & 0x0f) + 1;
|
|
38
|
+
return Buffer.concat([msg, Buffer.alloc(padLength, padLength)]);
|
|
58
39
|
};
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
40
|
+
//=======================================================//
|
|
41
|
+
export const unpadRandomMax16 = (e) => {
|
|
42
|
+
const t = new Uint8Array(e);
|
|
43
|
+
if (0 === t.length) {
|
|
44
|
+
throw new Error("unpadPkcs7 given empty bytes");
|
|
45
|
+
}
|
|
46
|
+
var r = t[t.length - 1];
|
|
47
|
+
if (r > t.length) {
|
|
48
|
+
throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`);
|
|
49
|
+
}
|
|
50
|
+
return new Uint8Array(t.buffer, t.byteOffset, t.length - r);
|
|
67
51
|
};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
/** The appropriate browser based on your OS & release */
|
|
74
|
-
appropriate: (browser) => [PLATFORM_MAP[(0, os_1.platform)()] || 'Ubuntu', browser, (0, os_1.release)()]
|
|
52
|
+
//=======================================================//
|
|
53
|
+
export const generateParticipantHashV2 = (participants) => {
|
|
54
|
+
participants.sort();
|
|
55
|
+
const sha256Hash = sha256(Buffer.from(participants.join(""))).toString("base64");
|
|
56
|
+
return "2:" + sha256Hash.slice(0, 6);
|
|
75
57
|
};
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
58
|
+
//=======================================================//
|
|
59
|
+
export const encodeWAMessage = (message) => writeRandomPadMax16(proto.Message.encode(message).finish());
|
|
60
|
+
export const generateRegistrationId = () => {
|
|
61
|
+
return Uint16Array.from(randomBytes(2))[0] & 16383;
|
|
79
62
|
};
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
63
|
+
//=======================================================//
|
|
64
|
+
export const encodeBigEndian = (e, t = 4) => {
|
|
65
|
+
let r = e;
|
|
66
|
+
const a = new Uint8Array(t);
|
|
67
|
+
for (let i = t - 1; i >= 0; i--) {
|
|
68
|
+
a[i] = 255 & r;
|
|
69
|
+
r >>>= 8;
|
|
70
|
+
}
|
|
71
|
+
return a;
|
|
72
|
+
};
|
|
73
|
+
export const toNumber = (t) => typeof t === "object" && t ? ("toNumber" in t ? t.toNumber() : t.low) : t || 0;
|
|
74
|
+
//=======================================================//
|
|
75
|
+
export const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000);
|
|
76
|
+
export const debouncedTimeout = (intervalMs = 1000, task) => {
|
|
77
|
+
let timeout;
|
|
78
|
+
return {
|
|
79
|
+
start: (newIntervalMs, newTask) => {
|
|
80
|
+
task = newTask || task;
|
|
81
|
+
intervalMs = newIntervalMs || intervalMs;
|
|
82
|
+
timeout && clearTimeout(timeout);
|
|
83
|
+
timeout = setTimeout(() => task?.(), intervalMs);
|
|
87
84
|
},
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
85
|
+
cancel: () => {
|
|
86
|
+
timeout && clearTimeout(timeout);
|
|
87
|
+
timeout = undefined;
|
|
88
|
+
},
|
|
89
|
+
setTask: (newTask) => (task = newTask),
|
|
90
|
+
setInterval: (newInterval) => (intervalMs = newInterval)
|
|
91
|
+
};
|
|
95
92
|
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
93
|
+
//=======================================================//
|
|
94
|
+
export const delay = (ms) => delayCancellable(ms).delay;
|
|
95
|
+
export const delayCancellable = (ms) => {
|
|
96
|
+
const stack = new Error().stack;
|
|
97
|
+
let timeout;
|
|
98
|
+
let reject;
|
|
99
|
+
const delay = new Promise((resolve, _reject) => {
|
|
100
|
+
timeout = setTimeout(resolve, ms);
|
|
101
|
+
reject = _reject;
|
|
102
|
+
});
|
|
103
|
+
const cancel = () => {
|
|
104
|
+
clearTimeout(timeout);
|
|
105
|
+
reject(new Boom("Cancelled", {
|
|
106
|
+
statusCode: 500,
|
|
107
|
+
data: {
|
|
108
|
+
stack
|
|
109
|
+
}
|
|
110
|
+
}));
|
|
111
|
+
};
|
|
112
|
+
return { delay, cancel };
|
|
113
|
+
};
|
|
114
|
+
//=======================================================//
|
|
115
|
+
export async function promiseTimeout(ms, promise) {
|
|
116
|
+
return new Promise(promise);
|
|
117
|
+
}
|
|
118
|
+
//=======================================================//
|
|
119
|
+
export const generateMessageIDV2 = (userId) => {
|
|
120
|
+
const data = Buffer.alloc(8 + 20 + 16);
|
|
121
|
+
data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)));
|
|
122
|
+
if (userId) {
|
|
123
|
+
const id = jidDecode(userId);
|
|
124
|
+
if (id?.user) {
|
|
125
|
+
data.write(id.user, 8);
|
|
126
|
+
data.write("@c.us", 8 + id.user.length);
|
|
103
127
|
}
|
|
104
|
-
|
|
128
|
+
}
|
|
129
|
+
const random = randomBytes(16);
|
|
130
|
+
random.copy(data, 28);
|
|
131
|
+
const hash = createHash("sha256").update(data).digest();
|
|
132
|
+
return "3EB0" + hash.toString("hex").toUpperCase().substring(0, 18);
|
|
105
133
|
};
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
134
|
+
//=======================================================//
|
|
135
|
+
export const generateMessageID = () => "3EB0" + randomBytes(18).toString("hex").toUpperCase();
|
|
136
|
+
export function bindWaitForEvent(ev, event) {
|
|
137
|
+
return async (check, timeoutMs) => {
|
|
138
|
+
let listener;
|
|
139
|
+
let closeListener;
|
|
140
|
+
await promiseTimeout(timeoutMs, (resolve, reject) => {
|
|
141
|
+
closeListener = ({ connection, lastDisconnect }) => {
|
|
142
|
+
if (connection === "close") {
|
|
143
|
+
reject(lastDisconnect?.error || new Boom("Connection Closed", { statusCode: DisconnectReason.connectionClosed }));
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
ev.on("connection.update", closeListener);
|
|
147
|
+
listener = async (update) => {
|
|
148
|
+
if (await check(update)) {
|
|
149
|
+
resolve();
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
ev.on(event, listener);
|
|
153
|
+
}).finally(() => {
|
|
154
|
+
ev.off(event, listener);
|
|
155
|
+
ev.off("connection.update", closeListener);
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
export const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, "connection.update");
|
|
160
|
+
//=======================================================//
|
|
161
|
+
export const fetchLatestBaileysVersion = async (options = {}) => {
|
|
162
|
+
const URL = "https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/index.ts";
|
|
163
|
+
try {
|
|
164
|
+
const response = await fetch(URL, {
|
|
165
|
+
dispatcher: options.dispatcher,
|
|
166
|
+
method: "GET",
|
|
167
|
+
headers: options.headers
|
|
168
|
+
});
|
|
169
|
+
if (!response.ok) {
|
|
170
|
+
throw new Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, { statusCode: response.status });
|
|
111
171
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
172
|
+
const text = await response.text();
|
|
173
|
+
const lines = text.split("\n");
|
|
174
|
+
const versionLine = lines[6];
|
|
175
|
+
const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/);
|
|
176
|
+
if (versionMatch) {
|
|
177
|
+
const version = [parseInt(versionMatch[1]), parseInt(versionMatch[2]), parseInt(versionMatch[3])];
|
|
178
|
+
return {
|
|
179
|
+
version,
|
|
180
|
+
isLatest: true
|
|
181
|
+
};
|
|
115
182
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
exports.unpadRandomMax16 = unpadRandomMax16;
|
|
119
|
-
const encodeWAMessage = (message) => ((0, exports.writeRandomPadMax16)(WAProto_1.proto.Message.encode(message).finish()));
|
|
120
|
-
exports.encodeWAMessage = encodeWAMessage;
|
|
121
|
-
const encodeNewsletterMessage = (message) => (WAProto_1.proto.Message.encode(message).finish());
|
|
122
|
-
exports.encodeNewsletterMessage = encodeNewsletterMessage;
|
|
123
|
-
const generateRegistrationId = () => {
|
|
124
|
-
return Uint16Array.from((0, crypto_1.randomBytes)(2))[0] & 16383;
|
|
125
|
-
};
|
|
126
|
-
exports.generateRegistrationId = generateRegistrationId;
|
|
127
|
-
const encodeBigEndian = (e, t = 4) => {
|
|
128
|
-
let r = e;
|
|
129
|
-
const a = new Uint8Array(t);
|
|
130
|
-
for (let i = t - 1; i >= 0; i--) {
|
|
131
|
-
a[i] = 255 & r;
|
|
132
|
-
r >>>= 8;
|
|
183
|
+
else {
|
|
184
|
+
throw new Error("Could not parse version from Defaults/index.ts");
|
|
133
185
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
exports.encodeBigEndian = encodeBigEndian;
|
|
137
|
-
const toNumber = (t) => ((typeof t === 'object' && t) ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0);
|
|
138
|
-
exports.toNumber = toNumber;
|
|
139
|
-
/** unix timestamp of a date in seconds */
|
|
140
|
-
const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000);
|
|
141
|
-
exports.unixTimestampSeconds = unixTimestampSeconds;
|
|
142
|
-
const debouncedTimeout = (intervalMs = 1000, task) => {
|
|
143
|
-
let timeout;
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
144
188
|
return {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
timeout && clearTimeout(timeout);
|
|
149
|
-
timeout = setTimeout(() => task === null || task === void 0 ? void 0 : task(), intervalMs);
|
|
150
|
-
},
|
|
151
|
-
cancel: () => {
|
|
152
|
-
timeout && clearTimeout(timeout);
|
|
153
|
-
timeout = undefined;
|
|
154
|
-
},
|
|
155
|
-
setTask: (newTask) => task = newTask,
|
|
156
|
-
setInterval: (newInterval) => intervalMs = newInterval
|
|
189
|
+
version: version,
|
|
190
|
+
isLatest: false,
|
|
191
|
+
error
|
|
157
192
|
};
|
|
193
|
+
}
|
|
158
194
|
};
|
|
159
|
-
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
let reject;
|
|
166
|
-
const delay = new Promise((resolve, _reject) => {
|
|
167
|
-
timeout = setTimeout(resolve, ms);
|
|
168
|
-
reject = _reject;
|
|
169
|
-
});
|
|
170
|
-
const cancel = () => {
|
|
171
|
-
clearTimeout(timeout);
|
|
172
|
-
reject(new boom_1.Boom('Cancelled', {
|
|
173
|
-
statusCode: 500,
|
|
174
|
-
data: {
|
|
175
|
-
stack
|
|
176
|
-
}
|
|
177
|
-
}));
|
|
195
|
+
//=======================================================//
|
|
196
|
+
export const fetchLatestWaWebVersion = async (options = {}) => {
|
|
197
|
+
try {
|
|
198
|
+
const defaultHeaders = {
|
|
199
|
+
"sec-fetch-site": "none",
|
|
200
|
+
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
|
|
178
201
|
};
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
return new Promise(promise);
|
|
185
|
-
}
|
|
186
|
-
const stack = new Error().stack;
|
|
187
|
-
// Create a promise that rejects in <ms> milliseconds
|
|
188
|
-
const { delay, cancel } = (0, exports.delayCancellable)(ms);
|
|
189
|
-
const p = new Promise((resolve, reject) => {
|
|
190
|
-
delay
|
|
191
|
-
.then(() => reject(new boom_1.Boom('Timed Out', {
|
|
192
|
-
statusCode: Types_1.DisconnectReason.timedOut,
|
|
193
|
-
data: {
|
|
194
|
-
stack
|
|
195
|
-
}
|
|
196
|
-
})))
|
|
197
|
-
.catch(err => reject(err));
|
|
198
|
-
promise(resolve, reject);
|
|
199
|
-
})
|
|
200
|
-
.finally(cancel);
|
|
201
|
-
return p;
|
|
202
|
-
}
|
|
203
|
-
const generateMessageIDV2 = (userId) => {
|
|
204
|
-
const data = Buffer.alloc(8 + 20 + 16);
|
|
205
|
-
data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)));
|
|
206
|
-
if (userId) {
|
|
207
|
-
const id = (0, WABinary_1.jidDecode)(userId);
|
|
208
|
-
if (id === null || id === void 0 ? void 0 : id.user) {
|
|
209
|
-
data.write(id.user, 8);
|
|
210
|
-
data.write('@c.us', 8 + id.user.length);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
const random = (0, crypto_1.randomBytes)(16);
|
|
214
|
-
random.copy(data, 28);
|
|
215
|
-
const hash = (0, crypto_1.createHash)('sha256').update(data).digest();
|
|
216
|
-
return 'BAE5' + hash.toString('hex').toUpperCase().substring(0, 12);
|
|
217
|
-
};
|
|
218
|
-
exports.generateMessageIDV2 = generateMessageIDV2;
|
|
219
|
-
// generate a random ID to attach to a message
|
|
220
|
-
const generateMessageID = () => 'BAE5' + (0, crypto_1.randomBytes)(6).toString('hex').toUpperCase();
|
|
221
|
-
exports.generateMessageID = generateMessageID;
|
|
222
|
-
function bindWaitForEvent(ev, event) {
|
|
223
|
-
return async (check, timeoutMs) => {
|
|
224
|
-
let listener;
|
|
225
|
-
let closeListener;
|
|
226
|
-
await (promiseTimeout(timeoutMs, (resolve, reject) => {
|
|
227
|
-
closeListener = ({ connection, lastDisconnect }) => {
|
|
228
|
-
if (connection === 'close') {
|
|
229
|
-
reject((lastDisconnect === null || lastDisconnect === void 0 ? void 0 : lastDisconnect.error)
|
|
230
|
-
|| new boom_1.Boom('Connection Closed', { statusCode: Types_1.DisconnectReason.connectionClosed }));
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
ev.on('connection.update', closeListener);
|
|
234
|
-
listener = async (update) => {
|
|
235
|
-
if (await check(update)) {
|
|
236
|
-
resolve();
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
ev.on(event, listener);
|
|
240
|
-
})
|
|
241
|
-
.finally(() => {
|
|
242
|
-
ev.off(event, listener);
|
|
243
|
-
ev.off('connection.update', closeListener);
|
|
244
|
-
}));
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update');
|
|
248
|
-
exports.bindWaitForConnectionUpdate = bindWaitForConnectionUpdate;
|
|
249
|
-
const printQRIfNecessaryListener = (ev, logger) => {
|
|
250
|
-
ev.on('connection.update', async ({ qr }) => {
|
|
251
|
-
if (qr) {
|
|
252
|
-
const QR = await Promise.resolve().then(() => __importStar(require('qrcode-terminal'))).then(m => m.default || m)
|
|
253
|
-
.catch(() => {
|
|
254
|
-
logger.error('QR code terminal not added as dependency');
|
|
255
|
-
});
|
|
256
|
-
QR === null || QR === void 0 ? void 0 : QR.generate(qr, { small: true });
|
|
257
|
-
}
|
|
202
|
+
const headers = { ...defaultHeaders, ...options.headers };
|
|
203
|
+
const response = await fetch("https://web.whatsapp.com/sw.js", {
|
|
204
|
+
...options,
|
|
205
|
+
method: "GET",
|
|
206
|
+
headers
|
|
258
207
|
});
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* utility that fetches latest baileys version from the master branch.
|
|
263
|
-
* Use to ensure your WA connection is always on the latest version
|
|
264
|
-
*/
|
|
265
|
-
const fetchLatestBaileysVersion = async (options = {}) => {
|
|
266
|
-
const URL = 'https://raw.githubusercontent.com/ruhend2001/apk-dl/refs/heads/master/version.json';
|
|
267
|
-
try {
|
|
268
|
-
const result = await axios_1.default.get(URL, {
|
|
269
|
-
...options,
|
|
270
|
-
responseType: 'json'
|
|
271
|
-
});
|
|
272
|
-
return {
|
|
273
|
-
version: result.data.version,
|
|
274
|
-
isLatest: true
|
|
275
|
-
};
|
|
208
|
+
if (!response.ok) {
|
|
209
|
+
throw new Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status });
|
|
276
210
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* A utility that fetches the latest web version of whatsapp.
|
|
288
|
-
* Use to ensure your WA connection is always on the latest version
|
|
289
|
-
*/
|
|
290
|
-
const fetchLatestWaWebVersion = async (options) => {
|
|
291
|
-
try {
|
|
292
|
-
const { data } = await axios_1.default.get('https://web.whatsapp.com/sw.js', {
|
|
293
|
-
...options,
|
|
294
|
-
responseType: 'json'
|
|
295
|
-
});
|
|
296
|
-
const regex = /\\?"client_revision\\?":\s*(\d+)/;
|
|
297
|
-
const match = data.match(regex);
|
|
298
|
-
if (!(match === null || match === void 0 ? void 0 : match[1])) {
|
|
299
|
-
return {
|
|
300
|
-
version: baileys_version_json_1.version,
|
|
301
|
-
isLatest: false,
|
|
302
|
-
error: {
|
|
303
|
-
message: 'Could not find client revision in the fetched content'
|
|
304
|
-
}
|
|
305
|
-
};
|
|
211
|
+
const data = await response.text();
|
|
212
|
+
const regex = /\\?"client_revision\\?":\s*(\d+)/;
|
|
213
|
+
const match = data.match(regex);
|
|
214
|
+
if (!match?.[1]) {
|
|
215
|
+
return {
|
|
216
|
+
version: version,
|
|
217
|
+
isLatest: false,
|
|
218
|
+
error: {
|
|
219
|
+
message: "Could not find client revision in the fetched content"
|
|
306
220
|
}
|
|
307
|
-
|
|
308
|
-
return {
|
|
309
|
-
version: [2, 3000, +clientRevision],
|
|
310
|
-
isLatest: true
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
catch (error) {
|
|
314
|
-
return {
|
|
315
|
-
version: baileys_version_json_1.version,
|
|
316
|
-
isLatest: false,
|
|
317
|
-
error
|
|
318
|
-
};
|
|
221
|
+
};
|
|
319
222
|
}
|
|
223
|
+
const clientRevision = match[1];
|
|
224
|
+
return {
|
|
225
|
+
version: [2, 3000, +clientRevision],
|
|
226
|
+
isLatest: true
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
return {
|
|
231
|
+
version: version,
|
|
232
|
+
isLatest: false,
|
|
233
|
+
error
|
|
234
|
+
};
|
|
235
|
+
}
|
|
320
236
|
};
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
|
|
237
|
+
//=======================================================//
|
|
238
|
+
export const generateMdTagPrefix = () => {
|
|
239
|
+
const bytes = randomBytes(4);
|
|
240
|
+
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
|
|
326
241
|
};
|
|
327
|
-
|
|
242
|
+
//=======================================================//
|
|
328
243
|
const STATUS_MAP = {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
244
|
+
"sender": proto.WebMessageInfo.Status.SERVER_ACK,
|
|
245
|
+
"played": proto.WebMessageInfo.Status.PLAYED,
|
|
246
|
+
"read": proto.WebMessageInfo.Status.READ,
|
|
247
|
+
"read-self": proto.WebMessageInfo.Status.READ
|
|
333
248
|
};
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
return WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK;
|
|
342
|
-
}
|
|
343
|
-
return status;
|
|
249
|
+
//=======================================================//
|
|
250
|
+
export const getStatusFromReceiptType = (type) => {
|
|
251
|
+
const status = STATUS_MAP[type];
|
|
252
|
+
if (typeof type === "undefined") {
|
|
253
|
+
return proto.WebMessageInfo.Status.DELIVERY_ACK;
|
|
254
|
+
}
|
|
255
|
+
return status;
|
|
344
256
|
};
|
|
345
|
-
|
|
257
|
+
//=======================================================//
|
|
346
258
|
const CODE_MAP = {
|
|
347
|
-
|
|
259
|
+
conflict: DisconnectReason.connectionReplaced
|
|
348
260
|
};
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
const
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
reason,
|
|
362
|
-
statusCode
|
|
363
|
-
};
|
|
261
|
+
//=======================================================//
|
|
262
|
+
export const getErrorCodeFromStreamError = (node) => {
|
|
263
|
+
const [reasonNode] = getAllBinaryNodeChildren(node);
|
|
264
|
+
let reason = reasonNode?.tag || "unknown";
|
|
265
|
+
const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession);
|
|
266
|
+
if (statusCode === DisconnectReason.restartRequired) {
|
|
267
|
+
reason = "restart required";
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
reason,
|
|
271
|
+
statusCode
|
|
272
|
+
};
|
|
364
273
|
};
|
|
365
|
-
|
|
366
|
-
const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
return status;
|
|
274
|
+
//=======================================================//
|
|
275
|
+
export const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
276
|
+
let status;
|
|
277
|
+
switch (tag) {
|
|
278
|
+
case "offer":
|
|
279
|
+
case "offer_notice":
|
|
280
|
+
status = "offer";
|
|
281
|
+
break;
|
|
282
|
+
case "terminate":
|
|
283
|
+
if (attrs.reason === "timeout") {
|
|
284
|
+
status = "timeout";
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
status = "terminate";
|
|
288
|
+
}
|
|
289
|
+
break;
|
|
290
|
+
case "reject":
|
|
291
|
+
status = "reject";
|
|
292
|
+
break;
|
|
293
|
+
case "accept":
|
|
294
|
+
status = "accept";
|
|
295
|
+
break;
|
|
296
|
+
default:
|
|
297
|
+
status = "ringing";
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
return status;
|
|
393
301
|
};
|
|
394
|
-
|
|
395
|
-
const UNEXPECTED_SERVER_CODE_TEXT =
|
|
396
|
-
const getCodeFromWSError = (error) => {
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
statusCode = code;
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
else if (((_b = error === null || error === void 0 ? void 0 : error.code) === null || _b === void 0 ? void 0 : _b.startsWith('E'))
|
|
406
|
-
|| ((_c = error === null || error === void 0 ? void 0 : error.message) === null || _c === void 0 ? void 0 : _c.includes('timed out'))) { // handle ETIMEOUT, ENOTFOUND etc
|
|
407
|
-
statusCode = 408;
|
|
302
|
+
//=======================================================//
|
|
303
|
+
const UNEXPECTED_SERVER_CODE_TEXT = "Unexpected server response: ";
|
|
304
|
+
export const getCodeFromWSError = (error) => {
|
|
305
|
+
let statusCode = 500;
|
|
306
|
+
if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
|
|
307
|
+
const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length);
|
|
308
|
+
if (!Number.isNaN(code) && code >= 400) {
|
|
309
|
+
statusCode = code;
|
|
408
310
|
}
|
|
409
|
-
|
|
311
|
+
}
|
|
312
|
+
else if (
|
|
313
|
+
error?.code?.startsWith("E") ||
|
|
314
|
+
error?.message?.includes("timed out")) {
|
|
315
|
+
statusCode = 408;
|
|
316
|
+
}
|
|
317
|
+
return statusCode;
|
|
410
318
|
};
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
* @param platform AuthenticationCreds.platform
|
|
415
|
-
*/
|
|
416
|
-
const isWABusinessPlatform = (platform) => {
|
|
417
|
-
return platform === 'smbi' || platform === 'smba';
|
|
319
|
+
//=======================================================//
|
|
320
|
+
export const isWABusinessPlatform = (platform) => {
|
|
321
|
+
return platform === "smbi" || platform === "smba";
|
|
418
322
|
};
|
|
419
|
-
|
|
420
|
-
function trimUndefined(obj) {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
323
|
+
//=======================================================//
|
|
324
|
+
export function trimUndefined(obj) {
|
|
325
|
+
for (const key in obj) {
|
|
326
|
+
if (typeof obj[key] === "undefined") {
|
|
327
|
+
delete obj[key];
|
|
425
328
|
}
|
|
426
|
-
|
|
329
|
+
}
|
|
330
|
+
return obj;
|
|
427
331
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
}
|
|
441
|
-
if (bitCount > 0) {
|
|
442
|
-
crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31));
|
|
332
|
+
//=======================================================//
|
|
333
|
+
const CROCKFORD_CHARACTERS = "123456789ABCDEFGHJKLMNPQRSTVWXYZ";
|
|
334
|
+
export function bytesToCrockford(buffer) {
|
|
335
|
+
let value = 0;
|
|
336
|
+
let bitCount = 0;
|
|
337
|
+
const crockford = [];
|
|
338
|
+
for (const element of buffer) {
|
|
339
|
+
value = (value << 8) | (element & 0xff);
|
|
340
|
+
bitCount += 8;
|
|
341
|
+
while (bitCount >= 5) {
|
|
342
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31));
|
|
343
|
+
bitCount -= 5;
|
|
443
344
|
}
|
|
444
|
-
|
|
345
|
+
}
|
|
346
|
+
if (bitCount > 0) {
|
|
347
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31));
|
|
348
|
+
}
|
|
349
|
+
return crockford.join("");
|
|
350
|
+
}
|
|
351
|
+
//=======================================================//
|
|
352
|
+
export function encodeNewsletterMessage(message) {
|
|
353
|
+
return proto.Message.encode(message).finish();
|
|
445
354
|
}
|
|
355
|
+
//=======================================================//
|