@sanzoffc/baileys 3.0.1 → 3.0.3
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 +769 -233
- package/WAProto/index.js +65801 -141371
- package/lib/Defaults/index.js +117 -114
- package/lib/Defaults/index.js.bak +123 -0
- 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 +12 -14
- package/lib/Signal/Group/group-session-builder.js +10 -42
- package/lib/Signal/Group/group_cipher.js +75 -87
- package/lib/Signal/Group/index.js +13 -57
- package/lib/Signal/Group/keyhelper.js +17 -52
- package/lib/Signal/Group/sender-chain-key.js +27 -33
- package/lib/Signal/Group/sender-key-distribution-message.js +62 -63
- package/lib/Signal/Group/sender-key-message.js +65 -66
- package/lib/Signal/Group/sender-key-name.js +45 -44
- package/lib/Signal/Group/sender-key-record.js +39 -49
- package/lib/Signal/Group/sender-key-state.js +80 -93
- package/lib/Signal/Group/sender-message-key.js +27 -28
- package/lib/Signal/libsignal.js +313 -163
- package/lib/Signal/lid-mapping.js +155 -0
- package/lib/Socket/Client/index.js +4 -18
- package/lib/Socket/Client/types.js +12 -12
- package/lib/Socket/Client/websocket.js +51 -71
- package/lib/Socket/Client/websocket.js.bak +53 -0
- package/lib/Socket/business.js +359 -242
- package/lib/Socket/chats.js +858 -945
- package/lib/Socket/communities.js +413 -0
- package/lib/Socket/groups.js +304 -324
- package/lib/Socket/index.js +15 -9
- package/lib/Socket/messages-recv.js +1105 -1046
- package/lib/Socket/messages-send.js +615 -389
- package/lib/Socket/mex.js +45 -0
- package/lib/Socket/newsletter.js +224 -227
- package/lib/Socket/socket.js +795 -621
- package/lib/Store/index.js +6 -8
- package/lib/Store/make-cache-manager-store.js +75 -0
- package/lib/Store/make-in-memory-store.js +286 -435
- 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/Newsletter.js.bak +33 -0
- 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 -191
- package/lib/Utils/baileys-event-stream.js +44 -0
- package/lib/Utils/browser-utils.js +21 -31
- package/lib/Utils/business.js +213 -214
- package/lib/Utils/chat-utils.js +711 -689
- package/lib/Utils/crypto.js +112 -175
- package/lib/Utils/decode-wa-message.js +254 -194
- package/lib/Utils/event-buffer.js +510 -500
- package/lib/Utils/generics.js +318 -430
- package/lib/Utils/history.js +83 -90
- package/lib/Utils/index.js +21 -35
- 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 +33 -48
- package/lib/Utils/messages-media.js +573 -825
- package/lib/Utils/messages.js +349 -489
- package/lib/Utils/noise-handler.js +138 -144
- package/lib/Utils/pre-key-manager.js +85 -0
- package/lib/Utils/process-message.js +321 -384
- package/lib/Utils/signal.js +147 -139
- package/lib/Utils/use-multi-file-auth-state.js +95 -109
- package/lib/Utils/validate-connection.js +183 -212
- package/lib/WABinary/constants.js +1298 -1298
- 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 +87 -79
- 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 -49
- package/package.json +65 -78
- package/README.MD +0 -1295
- package/WAProto/GenerateStatics.sh +0 -4
- package/WAProto/p.html +0 -1
- package/engine-requirements.js +0 -10
- package/lib/Defaults/wileys-version.json +0 -3
- package/lib/Signal/Group/queue-job.js +0 -57
- package/lib/Socket/usync.js +0 -70
- package/lib/Utils/wileys-event-stream.js +0 -63
package/lib/Utils/generics.js
CHANGED
|
@@ -1,467 +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 wileys_version_json_1 = require("../Defaults/wileys-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
|
|
157
|
-
};
|
|
158
|
-
};
|
|
159
|
-
exports.debouncedTimeout = debouncedTimeout;
|
|
160
|
-
const delay = (ms) => (0, exports.delayCancellable)(ms).delay;
|
|
161
|
-
exports.delay = delay;
|
|
162
|
-
const delayCancellable = (ms) => {
|
|
163
|
-
const stack = new Error().stack;
|
|
164
|
-
let timeout;
|
|
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
|
-
}));
|
|
189
|
+
version: version,
|
|
190
|
+
isLatest: false,
|
|
191
|
+
error
|
|
178
192
|
};
|
|
179
|
-
|
|
180
|
-
};
|
|
181
|
-
exports.delayCancellable = delayCancellable;
|
|
182
|
-
async function promiseTimeout(ms, promise) {
|
|
183
|
-
if (!ms) {
|
|
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 '3EB0' + hash.toString('hex').toUpperCase().substring(0, 18);
|
|
193
|
+
}
|
|
217
194
|
};
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
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
|
-
}));
|
|
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"
|
|
245
201
|
};
|
|
246
|
-
}
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
-
const fetchLatestWileysVersion = async (options = {}) => {
|
|
263
|
-
try {
|
|
264
|
-
const { data } = await axios_1.default.get('https://registry.npmjs.org/wileys', {
|
|
265
|
-
...options,
|
|
266
|
-
responseType: 'json'
|
|
267
|
-
});
|
|
268
|
-
const versionStr = data.version;
|
|
269
|
-
const [major, minor, patch] = versionStr.split('.').map(Number);
|
|
270
|
-
return {
|
|
271
|
-
version: [major, minor, patch],
|
|
272
|
-
isLatest: true
|
|
273
|
-
};
|
|
274
|
-
} catch (error) {
|
|
275
|
-
return {
|
|
276
|
-
version: wileys_version_json_1.version,
|
|
277
|
-
isLatest: false,
|
|
278
|
-
error
|
|
279
|
-
};
|
|
208
|
+
if (!response.ok) {
|
|
209
|
+
throw new Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status });
|
|
280
210
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
const versionStr = data.version;
|
|
291
|
-
const [major, minor, patch] = versionStr.split('.').map(Number);
|
|
292
|
-
return {
|
|
293
|
-
version: [major, minor, patch],
|
|
294
|
-
isLatest: true
|
|
295
|
-
};
|
|
296
|
-
} catch (error) {
|
|
297
|
-
return {
|
|
298
|
-
version: wileys_version_json_1.version,
|
|
299
|
-
isLatest: false,
|
|
300
|
-
error
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
exports.fetchLatestBaileysVersion = fetchLatestBaileysVersion;
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* A utility that fetches the latest web version of whatsapp.
|
|
308
|
-
* Use to ensure your WA connection is always on the latest version
|
|
309
|
-
*/
|
|
310
|
-
const fetchLatestWaWebVersion = async (options) => {
|
|
311
|
-
try {
|
|
312
|
-
const { data } = await axios_1.default.get('https://web.whatsapp.com/sw.js', {
|
|
313
|
-
...options,
|
|
314
|
-
responseType: 'json'
|
|
315
|
-
});
|
|
316
|
-
const regex = /\\?"client_revision\\?":\s*(\d+)/;
|
|
317
|
-
const match = data.match(regex);
|
|
318
|
-
if (!(match === null || match === void 0 ? void 0 : match[1])) {
|
|
319
|
-
return {
|
|
320
|
-
version: wileys_version_json_1.version,
|
|
321
|
-
isLatest: false,
|
|
322
|
-
error: {
|
|
323
|
-
message: 'Could not find client revision in the fetched content'
|
|
324
|
-
}
|
|
325
|
-
};
|
|
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"
|
|
326
220
|
}
|
|
327
|
-
|
|
328
|
-
return {
|
|
329
|
-
version: [2, 3000, +clientRevision],
|
|
330
|
-
isLatest: true
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
catch (error) {
|
|
334
|
-
return {
|
|
335
|
-
version: wileys_version_json_1.version,
|
|
336
|
-
isLatest: false,
|
|
337
|
-
error
|
|
338
|
-
};
|
|
221
|
+
};
|
|
339
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
|
+
}
|
|
340
236
|
};
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const
|
|
344
|
-
|
|
345
|
-
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
|
|
237
|
+
//=======================================================//
|
|
238
|
+
export const generateMdTagPrefix = () => {
|
|
239
|
+
const bytes = randomBytes(4);
|
|
240
|
+
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
|
|
346
241
|
};
|
|
347
|
-
|
|
242
|
+
//=======================================================//
|
|
348
243
|
const STATUS_MAP = {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
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
|
|
353
248
|
};
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
return WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK;
|
|
362
|
-
}
|
|
363
|
-
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;
|
|
364
256
|
};
|
|
365
|
-
|
|
257
|
+
//=======================================================//
|
|
366
258
|
const CODE_MAP = {
|
|
367
|
-
|
|
259
|
+
conflict: DisconnectReason.connectionReplaced
|
|
368
260
|
};
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
const
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
reason,
|
|
382
|
-
statusCode
|
|
383
|
-
};
|
|
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
|
+
};
|
|
384
273
|
};
|
|
385
|
-
|
|
386
|
-
const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
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;
|
|
413
301
|
};
|
|
414
|
-
|
|
415
|
-
const UNEXPECTED_SERVER_CODE_TEXT =
|
|
416
|
-
const getCodeFromWSError = (error) => {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
statusCode = code;
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
else if (((_b = error === null || error === void 0 ? void 0 : error.code) === null || _b === void 0 ? void 0 : _b.startsWith('E'))
|
|
426
|
-
|| ((_c = error === null || error === void 0 ? void 0 : error.message) === null || _c === void 0 ? void 0 : _c.includes('timed out'))) { // handle ETIMEOUT, ENOTFOUND etc
|
|
427
|
-
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;
|
|
428
310
|
}
|
|
429
|
-
|
|
311
|
+
}
|
|
312
|
+
else if (
|
|
313
|
+
error?.code?.startsWith("E") ||
|
|
314
|
+
error?.message?.includes("timed out")) {
|
|
315
|
+
statusCode = 408;
|
|
316
|
+
}
|
|
317
|
+
return statusCode;
|
|
430
318
|
};
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
* @param platform AuthenticationCreds.platform
|
|
435
|
-
*/
|
|
436
|
-
const isWABusinessPlatform = (platform) => {
|
|
437
|
-
return platform === 'smbi' || platform === 'smba';
|
|
319
|
+
//=======================================================//
|
|
320
|
+
export const isWABusinessPlatform = (platform) => {
|
|
321
|
+
return platform === "smbi" || platform === "smba";
|
|
438
322
|
};
|
|
439
|
-
|
|
440
|
-
function trimUndefined(obj) {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
}
|
|
323
|
+
//=======================================================//
|
|
324
|
+
export function trimUndefined(obj) {
|
|
325
|
+
for (const key in obj) {
|
|
326
|
+
if (typeof obj[key] === "undefined") {
|
|
327
|
+
delete obj[key];
|
|
445
328
|
}
|
|
446
|
-
|
|
329
|
+
}
|
|
330
|
+
return obj;
|
|
447
331
|
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
}
|
|
461
|
-
if (bitCount > 0) {
|
|
462
|
-
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;
|
|
463
344
|
}
|
|
464
|
-
|
|
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();
|
|
465
354
|
}
|
|
466
|
-
|
|
467
|
-
exports.bytesToCrockford = bytesToCrockford;
|
|
355
|
+
//=======================================================//
|