@sixcore/baileys 1.0.2 → 1.1.0

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.
Files changed (210) hide show
  1. package/LICENSE +21 -13
  2. package/WAProto/WAProto.proto +5311 -0
  3. package/WAProto/index.js +65813 -141372
  4. package/lib/Defaults/index.js +120 -133
  5. package/lib/KeyDB/BinarySearch.js +30 -0
  6. package/lib/KeyDB/KeyedDB.js +178 -0
  7. package/lib/KeyDB/index.js +14 -0
  8. package/lib/Signal/Group/ciphertext-message.js +22 -14
  9. package/lib/Signal/Group/group-session-builder.js +21 -42
  10. package/lib/Signal/Group/group_cipher.js +85 -87
  11. package/lib/Signal/Group/index.js +23 -57
  12. package/lib/Signal/Group/keyhelper.js +28 -52
  13. package/lib/Signal/Group/sender-chain-key.js +37 -33
  14. package/lib/Signal/Group/sender-key-distribution-message.js +73 -63
  15. package/lib/Signal/Group/sender-key-message.js +75 -66
  16. package/lib/Signal/Group/sender-key-name.js +55 -44
  17. package/lib/Signal/Group/sender-key-record.js +49 -49
  18. package/lib/Signal/Group/sender-key-state.js +90 -93
  19. package/lib/Signal/Group/sender-message-key.js +37 -28
  20. package/lib/Signal/libsignal.js +324 -163
  21. package/lib/Signal/lid-mapping.js +166 -0
  22. package/lib/Socket/Client/index.js +14 -19
  23. package/lib/Socket/Client/types.js +13 -0
  24. package/lib/Socket/Client/websocket.js +62 -0
  25. package/lib/Socket/business.js +359 -242
  26. package/lib/Socket/chats.js +850 -935
  27. package/lib/Socket/communities.js +413 -0
  28. package/lib/Socket/groups.js +304 -309
  29. package/lib/Socket/index.js +25 -10
  30. package/lib/Socket/messages-recv.js +1107 -1054
  31. package/lib/Socket/messages-send.js +639 -448
  32. package/lib/Socket/mex.js +45 -0
  33. package/lib/Socket/newsletter.js +244 -237
  34. package/lib/Socket/socket.js +803 -638
  35. package/lib/Store/index.js +18 -10
  36. package/lib/Store/make-cache-manager-store.js +73 -81
  37. package/lib/Store/make-in-memory-store.js +286 -423
  38. package/lib/Store/make-ordered-dictionary.js +77 -79
  39. package/lib/Store/object-repository.js +24 -26
  40. package/lib/Types/Auth.js +13 -2
  41. package/lib/Types/Bussines.js +13 -0
  42. package/lib/Types/Call.js +13 -2
  43. package/lib/Types/Chat.js +19 -4
  44. package/lib/Types/Contact.js +13 -2
  45. package/lib/Types/Events.js +13 -2
  46. package/lib/Types/GroupMetadata.js +13 -2
  47. package/lib/Types/Label.js +43 -26
  48. package/lib/Types/Label.js.bak +25 -0
  49. package/lib/Types/LabelAssociation.js +16 -8
  50. package/lib/Types/Message.js +22 -9
  51. package/lib/Types/Newsletter.js +42 -37
  52. package/lib/Types/Product.js +13 -2
  53. package/lib/Types/Signal.js +13 -2
  54. package/lib/Types/Socket.js +14 -2
  55. package/lib/Types/State.js +21 -2
  56. package/lib/Types/USync.js +13 -2
  57. package/lib/Types/index.js +37 -41
  58. package/lib/Utils/auth-utils.js +219 -196
  59. package/lib/Utils/baileys-event-stream.js +50 -59
  60. package/lib/Utils/browser-utils.js +25 -0
  61. package/lib/Utils/business.js +213 -214
  62. package/lib/Utils/chat-utils.js +710 -687
  63. package/lib/Utils/crypto.js +112 -133
  64. package/lib/Utils/decode-wa-message.js +252 -183
  65. package/lib/Utils/event-buffer.js +510 -496
  66. package/lib/Utils/generics.js +319 -387
  67. package/lib/Utils/history.js +83 -92
  68. package/lib/Utils/index.js +31 -33
  69. package/lib/Utils/link-preview.js +71 -83
  70. package/lib/Utils/logger.js +5 -7
  71. package/lib/Utils/lt-hash.js +40 -46
  72. package/lib/Utils/make-mutex.js +34 -41
  73. package/lib/Utils/message-retry-manager.js +113 -0
  74. package/lib/Utils/messages-media.js +553 -768
  75. package/lib/Utils/messages-media.js.bak2 +602 -0
  76. package/lib/Utils/messages.js +354 -263
  77. package/lib/Utils/noise-handler.js +138 -149
  78. package/lib/Utils/pre-key-manager.js +95 -0
  79. package/lib/Utils/process-message.js +333 -303
  80. package/lib/Utils/signal.js +159 -141
  81. package/lib/Utils/use-multi-file-auth-state.js +105 -103
  82. package/lib/Utils/validate-connection.js +184 -203
  83. package/lib/WABinary/constants.js +1308 -35
  84. package/lib/WABinary/decode.js +247 -249
  85. package/lib/WABinary/encode.js +221 -258
  86. package/lib/WABinary/generic-utils.js +68 -65
  87. package/lib/WABinary/index.js +17 -21
  88. package/lib/WABinary/jid-utils.js +99 -58
  89. package/lib/WABinary/types.js +13 -2
  90. package/lib/WAM/BinaryInfo.js +20 -12
  91. package/lib/WAM/constants.js +22863 -15348
  92. package/lib/WAM/encode.js +145 -136
  93. package/lib/WAM/index.js +15 -19
  94. package/lib/WAUSync/Protocols/USyncContactProtocol.js +39 -31
  95. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +61 -54
  96. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +39 -29
  97. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +48 -40
  98. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +62 -51
  99. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +38 -21
  100. package/lib/WAUSync/Protocols/index.js +17 -20
  101. package/lib/WAUSync/USyncQuery.js +98 -86
  102. package/lib/WAUSync/USyncUser.js +35 -26
  103. package/lib/WAUSync/index.js +16 -19
  104. package/lib/index.js +26 -94
  105. package/lib/index.js.bak +23 -0
  106. package/package.json +97 -113
  107. package/jessica.js +0 -91
  108. package/lib/Defaults/baileys-version.json +0 -3
  109. package/lib/Defaults/index.d.ts +0 -53
  110. package/lib/Defaults/phonenumber-mcc.json +0 -223
  111. package/lib/Signal/Group/ciphertext-message.d.ts +0 -9
  112. package/lib/Signal/Group/group-session-builder.d.ts +0 -14
  113. package/lib/Signal/Group/group_cipher.d.ts +0 -17
  114. package/lib/Signal/Group/index.d.ts +0 -11
  115. package/lib/Signal/Group/keyhelper.d.ts +0 -10
  116. package/lib/Signal/Group/queue-job.d.ts +0 -1
  117. package/lib/Signal/Group/queue-job.js +0 -57
  118. package/lib/Signal/Group/sender-chain-key.d.ts +0 -13
  119. package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -16
  120. package/lib/Signal/Group/sender-key-message.d.ts +0 -18
  121. package/lib/Signal/Group/sender-key-name.d.ts +0 -17
  122. package/lib/Signal/Group/sender-key-record.d.ts +0 -30
  123. package/lib/Signal/Group/sender-key-state.d.ts +0 -38
  124. package/lib/Signal/Group/sender-message-key.d.ts +0 -11
  125. package/lib/Signal/libsignal.d.ts +0 -3
  126. package/lib/Socket/Client/abstract-socket-client.d.ts +0 -17
  127. package/lib/Socket/Client/abstract-socket-client.js +0 -13
  128. package/lib/Socket/Client/index.d.ts +0 -3
  129. package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
  130. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  131. package/lib/Socket/Client/web-socket-client.d.ts +0 -12
  132. package/lib/Socket/Client/web-socket-client.js +0 -62
  133. package/lib/Socket/business.d.ts +0 -171
  134. package/lib/Socket/chats.d.ts +0 -267
  135. package/lib/Socket/dugong.d.ts +0 -254
  136. package/lib/Socket/dugong.js +0 -484
  137. package/lib/Socket/groups.d.ts +0 -115
  138. package/lib/Socket/index.d.ts +0 -173
  139. package/lib/Socket/messages-recv.d.ts +0 -161
  140. package/lib/Socket/messages-send.d.ts +0 -149
  141. package/lib/Socket/newsletter.d.ts +0 -134
  142. package/lib/Socket/registration.d.ts +0 -267
  143. package/lib/Socket/registration.js +0 -166
  144. package/lib/Socket/socket.d.ts +0 -43
  145. package/lib/Socket/usync.d.ts +0 -36
  146. package/lib/Socket/usync.js +0 -70
  147. package/lib/Store/index.d.ts +0 -3
  148. package/lib/Store/make-cache-manager-store.d.ts +0 -13
  149. package/lib/Store/make-in-memory-store.d.ts +0 -118
  150. package/lib/Store/make-ordered-dictionary.d.ts +0 -13
  151. package/lib/Store/object-repository.d.ts +0 -10
  152. package/lib/Types/Auth.d.ts +0 -110
  153. package/lib/Types/Call.d.ts +0 -13
  154. package/lib/Types/Chat.d.ts +0 -102
  155. package/lib/Types/Contact.d.ts +0 -19
  156. package/lib/Types/Events.d.ts +0 -157
  157. package/lib/Types/GroupMetadata.d.ts +0 -55
  158. package/lib/Types/Label.d.ts +0 -35
  159. package/lib/Types/LabelAssociation.d.ts +0 -29
  160. package/lib/Types/Message.d.ts +0 -273
  161. package/lib/Types/Newsletter.d.ts +0 -103
  162. package/lib/Types/Product.d.ts +0 -78
  163. package/lib/Types/Signal.d.ts +0 -57
  164. package/lib/Types/Socket.d.ts +0 -111
  165. package/lib/Types/State.d.ts +0 -27
  166. package/lib/Types/USync.d.ts +0 -25
  167. package/lib/Types/index.d.ts +0 -57
  168. package/lib/Utils/auth-utils.d.ts +0 -18
  169. package/lib/Utils/baileys-event-stream.d.ts +0 -16
  170. package/lib/Utils/business.d.ts +0 -22
  171. package/lib/Utils/chat-utils.d.ts +0 -71
  172. package/lib/Utils/crypto.d.ts +0 -41
  173. package/lib/Utils/decode-wa-message.d.ts +0 -19
  174. package/lib/Utils/event-buffer.d.ts +0 -35
  175. package/lib/Utils/generics.d.ts +0 -92
  176. package/lib/Utils/history.d.ts +0 -15
  177. package/lib/Utils/index.d.ts +0 -17
  178. package/lib/Utils/link-preview.d.ts +0 -21
  179. package/lib/Utils/logger.d.ts +0 -4
  180. package/lib/Utils/lt-hash.d.ts +0 -12
  181. package/lib/Utils/make-mutex.d.ts +0 -7
  182. package/lib/Utils/messages-media.d.ts +0 -116
  183. package/lib/Utils/messages.d.ts +0 -77
  184. package/lib/Utils/noise-handler.d.ts +0 -21
  185. package/lib/Utils/process-message.d.ts +0 -41
  186. package/lib/Utils/signal.d.ts +0 -32
  187. package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
  188. package/lib/Utils/validate-connection.d.ts +0 -11
  189. package/lib/WABinary/constants.d.ts +0 -30
  190. package/lib/WABinary/decode.d.ts +0 -7
  191. package/lib/WABinary/encode.d.ts +0 -3
  192. package/lib/WABinary/generic-utils.d.ts +0 -17
  193. package/lib/WABinary/index.d.ts +0 -5
  194. package/lib/WABinary/jid-utils.d.ts +0 -31
  195. package/lib/WABinary/types.d.ts +0 -18
  196. package/lib/WAM/BinaryInfo.d.ts +0 -17
  197. package/lib/WAM/constants.d.ts +0 -38
  198. package/lib/WAM/encode.d.ts +0 -3
  199. package/lib/WAM/index.d.ts +0 -3
  200. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -9
  201. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -22
  202. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -12
  203. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -12
  204. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -25
  205. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -8
  206. package/lib/WAUSync/Protocols/index.d.ts +0 -4
  207. package/lib/WAUSync/USyncQuery.d.ts +0 -28
  208. package/lib/WAUSync/USyncUser.d.ts +0 -12
  209. package/lib/WAUSync/index.d.ts +0 -3
  210. package/lib/index.d.ts +0 -12
@@ -0,0 +1,602 @@
1
+ //=======================================================//
2
+ import { getBinaryNodeChild, getBinaryNodeChildBuffer, jidNormalizedUser } from "../WABinary/index.js";
3
+ import { DEFAULT_ORIGIN, MEDIA_HKDF_KEY_MAPPING, MEDIA_PATH_MAP } from "../Defaults/index.js";
4
+ import { createReadStream, createWriteStream, promises as fs, WriteStream } from "fs";
5
+ import { aesDecryptGCM, aesEncryptGCM, hkdf } from "./crypto.js";
6
+ import { generateMessageIDV2 } from "./generics.js";
7
+ import { proto } from "../../WAProto/index.js";
8
+ import { Readable, Transform } from "stream";
9
+ import { exec } from "child_process";
10
+ import { Boom } from "@hapi/boom";
11
+ import * as Crypto from "crypto";
12
+ import { once } from "events";
13
+ import { tmpdir } from "os";
14
+ import { join } from "path";
15
+ import { URL } from "url";
16
+ import * as Jimp from "jimp"
17
+ //import Jimp from "jimp";
18
+ //=======================================================//
19
+ const getTmpFilesDirectory = () => tmpdir();
20
+ //=======================================================//
21
+ export const hkdfInfoKey = (type) => {
22
+ const hkdfInfo = MEDIA_HKDF_KEY_MAPPING[type];
23
+ return `WhatsApp ${hkdfInfo} Keys`;
24
+ };
25
+ //=======================================================//
26
+ export const getRawMediaUploadData = async (media, mediaType, logger) => {
27
+ const { stream } = await getStream(media);
28
+ logger?.debug("got stream for raw upload");
29
+ const hasher = Crypto.createHash("sha256");
30
+ const filePath = join(tmpdir(), mediaType + generateMessageIDV2());
31
+ const fileWriteStream = createWriteStream(filePath);
32
+ let fileLength = 0;
33
+ try {
34
+ for await (const data of stream) {
35
+ fileLength += data.length;
36
+ hasher.update(data);
37
+ if (!fileWriteStream.write(data)) {
38
+ await once(fileWriteStream, "drain");
39
+ }
40
+ }
41
+ fileWriteStream.end();
42
+ await once(fileWriteStream, "finish");
43
+ stream.destroy();
44
+ const fileSha256 = hasher.digest();
45
+ logger?.debug("hashed data for raw upload");
46
+ return {
47
+ filePath: filePath,
48
+ fileSha256,
49
+ fileLength
50
+ };
51
+ }
52
+ catch (error) {
53
+ fileWriteStream.destroy();
54
+ stream.destroy();
55
+ try {
56
+ await fs.unlink(filePath);
57
+ }
58
+ catch {
59
+ }
60
+ throw error;
61
+ }
62
+ };
63
+ //=======================================================//
64
+ export async function getMediaKeys(buffer, mediaType) {
65
+ if (!buffer) {
66
+ throw new Boom("Cannot derive from empty media key");
67
+ }
68
+ if (typeof buffer === "string") {
69
+ buffer = Buffer.from(buffer.replace("data:;base64,", ""), "base64");
70
+ }
71
+ const expandedMediaKey = await hkdf(buffer, 112, { info: hkdfInfoKey(mediaType) });
72
+ return {
73
+ iv: expandedMediaKey.slice(0, 16),
74
+ cipherKey: expandedMediaKey.slice(16, 48),
75
+ macKey: expandedMediaKey.slice(48, 80)
76
+ };
77
+ }
78
+ //=======================================================//
79
+ const extractVideoThumb = async (path, destPath, time, size) => new Promise((resolve, reject) => {
80
+ const cmd = `ffmpeg -ss ${time} -i ${path} -y -vf scale=${size.width}:-1 -vframes 1 -f image2 ${destPath}`;
81
+ exec(cmd, err => {
82
+ if (err) {
83
+ reject(err);
84
+ }
85
+ else {
86
+ resolve();
87
+ }
88
+ });
89
+ });
90
+ //=======================================================//
91
+ export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
92
+ if (bufferOrFilePath instanceof Readable) {
93
+ bufferOrFilePath = await toBuffer(bufferOrFilePath);
94
+ }
95
+ const image = await Jimp.read(bufferOrFilePath);
96
+ const dimensions = { width: image.bitmap.width, height: image.bitmap.height };
97
+ const resized = image.resize(width, Jimp.RESIZE_BILINEAR).quality(50);
98
+ const buffer = await resized.getBufferAsync(Jimp.MIME_JPEG);
99
+ return { buffer, original: dimensions };
100
+ };
101
+ //=======================================================//
102
+ export const encodeBase64EncodedStringForUpload = (b64) => encodeURIComponent(b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/\=+$/, ""));
103
+ export const generateProfilePicture = async (mediaUpload, dimensions) => {
104
+ let buffer;
105
+ const { width: w = 640, height: h = 640 } = dimensions || {};
106
+ if (Buffer.isBuffer(mediaUpload)) {
107
+ buffer = mediaUpload;
108
+ } else {
109
+ const { stream } = await getStream(mediaUpload);
110
+ buffer = await toBuffer(stream);
111
+ }
112
+ const jimp = await Jimp.read(buffer);
113
+ const min = Math.min(jimp.bitmap.width, jimp.bitmap.height);
114
+ const cropped = jimp.crop(0, 0, min, min);
115
+ const resized = cropped.resize(w, h, Jimp.RESIZE_BILINEAR).quality(50);
116
+ const img = await resized.getBufferAsync(Jimp.MIME_JPEG);
117
+ return { img };
118
+ };
119
+ //=======================================================//
120
+ export const mediaMessageSHA256B64 = (message) => {
121
+ const media = Object.values(message)[0];
122
+ return media?.fileSha256 && Buffer.from(media.fileSha256).toString("base64");
123
+ };
124
+ //=======================================================//
125
+ export async function getAudioDuration(buffer) {
126
+ const musicMetadata = await import("music-metadata");
127
+ let metadata;
128
+ const options = {
129
+ duration: true
130
+ };
131
+ if (Buffer.isBuffer(buffer)) {
132
+ metadata = await musicMetadata.parseBuffer(buffer, undefined, options);
133
+ }
134
+ else if (typeof buffer === "string") {
135
+ metadata = await musicMetadata.parseFile(buffer, options);
136
+ }
137
+ else {
138
+ metadata = await musicMetadata.parseStream(buffer, undefined, options);
139
+ }
140
+ return metadata.format.duration;
141
+ }
142
+ //=======================================================//
143
+ export async function getAudioWaveform(buffer, logger) {
144
+ try {
145
+ const { default: decoder } = await import("audio-decode");
146
+ let audioData;
147
+ if (Buffer.isBuffer(buffer)) {
148
+ audioData = buffer;
149
+ }
150
+ else if (typeof buffer === "string") {
151
+ const rStream = createReadStream(buffer);
152
+ audioData = await toBuffer(rStream);
153
+ }
154
+ else {
155
+ audioData = await toBuffer(buffer);
156
+ }
157
+ const audioBuffer = await decoder(audioData);
158
+ const rawData = audioBuffer.getChannelData(0);
159
+ const samples = 64;
160
+ const blockSize = Math.floor(rawData.length / samples);
161
+ const filteredData = [];
162
+ for (let i = 0; i < samples; i++) {
163
+ const blockStart = blockSize * i;
164
+ let sum = 0;
165
+ for (let j = 0; j < blockSize; j++) {
166
+ sum = sum + Math.abs(rawData[blockStart + j]);
167
+ }
168
+ filteredData.push(sum / blockSize);
169
+ }
170
+ const multiplier = Math.pow(Math.max(...filteredData), -1);
171
+ const normalizedData = filteredData.map(n => n * multiplier);
172
+ const waveform = new Uint8Array(normalizedData.map(n => Math.floor(100 * n)));
173
+ return waveform;
174
+ }
175
+ catch (e) {
176
+ logger?.debug("Failed to generate waveform: " + e);
177
+ }
178
+ }
179
+ //=======================================================//
180
+ export const toReadable = (buffer) => {
181
+ const readable = new Readable({ read: () => { } });
182
+ readable.push(buffer);
183
+ readable.push(null);
184
+ return readable;
185
+ };
186
+ //=======================================================//
187
+ export const toBuffer = async (stream) => {
188
+ const chunks = [];
189
+ for await (const chunk of stream) {
190
+ chunks.push(chunk);
191
+ }
192
+ stream.destroy();
193
+ return Buffer.concat(chunks);
194
+ };
195
+ //=======================================================//
196
+ export const getStream = async (item, opts) => {
197
+ if (Buffer.isBuffer(item)) {
198
+ return { stream: toReadable(item), type: "buffer" };
199
+ }
200
+ if ("stream" in item) {
201
+ return { stream: item.stream, type: "readable" };
202
+ }
203
+ const urlStr = item.url.toString();
204
+ if (urlStr.startsWith("data:")) {
205
+ const buffer = Buffer.from(urlStr.split(",")[1], "base64");
206
+ return { stream: toReadable(buffer), type: "buffer" };
207
+ }
208
+ if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
209
+ return { stream: await getHttpStream(item.url, opts), type: "remote" };
210
+ }
211
+ return { stream: createReadStream(item.url), type: "file" };
212
+ };
213
+ //=======================================================//
214
+ export async function generateThumbnail(file, mediaType, options) {
215
+ let thumbnail;
216
+ let originalImageDimensions;
217
+ if (mediaType === "image") {
218
+ const { buffer, original } = await extractImageThumb(file);
219
+ thumbnail = buffer.toString("base64");
220
+ if (original.width && original.height) {
221
+ originalImageDimensions = {
222
+ width: original.width,
223
+ height: original.height
224
+ };
225
+ }
226
+ }
227
+ else if (mediaType === "video") {
228
+ const imgFilename = join(getTmpFilesDirectory(), generateMessageIDV2() + ".jpg");
229
+ try {
230
+ await extractVideoThumb(file, imgFilename, "00:00:00", { width: 32, height: 32 });
231
+ const buff = await fs.readFile(imgFilename);
232
+ thumbnail = buff.toString("base64");
233
+ await fs.unlink(imgFilename);
234
+ }
235
+ catch (err) {
236
+ options.logger?.debug("could not generate video thumb: " + err);
237
+ }
238
+ }
239
+ return {
240
+ thumbnail,
241
+ originalImageDimensions
242
+ };
243
+ }
244
+ //=======================================================//
245
+ export const getHttpStream = async (url, options = {}) => {
246
+ const response = await fetch(url.toString(), {
247
+ dispatcher: options.dispatcher,
248
+ method: "GET",
249
+ headers: options.headers
250
+ });
251
+ if (!response.ok) {
252
+ throw new Boom(`Failed to fetch stream from ${url}`, { statusCode: response.status, data: { url } });
253
+ }
254
+ return Readable.fromWeb(response.body);
255
+ };
256
+ //=======================================================//
257
+ export const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) => {
258
+ const { stream, type } = await getStream(media, opts);
259
+ logger?.debug("fetched media stream");
260
+ const mediaKey = Crypto.randomBytes(32);
261
+ const { cipherKey, iv, macKey } = await getMediaKeys(mediaKey, mediaType);
262
+ const encFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + "-enc");
263
+ const encFileWriteStream = createWriteStream(encFilePath);
264
+ let originalFileStream;
265
+ let originalFilePath;
266
+ if (saveOriginalFileIfRequired) {
267
+ originalFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + "-original");
268
+ originalFileStream = createWriteStream(originalFilePath);
269
+ }
270
+ let fileLength = 0;
271
+ const aes = Crypto.createCipheriv("aes-256-cbc", cipherKey, iv);
272
+ const hmac = Crypto.createHmac("sha256", macKey).update(iv);
273
+ const sha256Plain = Crypto.createHash("sha256");
274
+ const sha256Enc = Crypto.createHash("sha256");
275
+ const onChunk = (buff) => {
276
+ sha256Enc.update(buff);
277
+ hmac.update(buff);
278
+ encFileWriteStream.write(buff);
279
+ };
280
+ try {
281
+ for await (const data of stream) {
282
+ fileLength += data.length;
283
+ if (type === "remote" &&
284
+ opts?.maxContentLength &&
285
+ fileLength + data.length > opts.maxContentLength) {
286
+ throw new Boom(`content length exceeded when encrypting "${type}"`, {
287
+ data: { media, type }
288
+ });
289
+ }
290
+ if (originalFileStream) {
291
+ if (!originalFileStream.write(data)) {
292
+ await once(originalFileStream, "drain");
293
+ }
294
+ }
295
+ sha256Plain.update(data);
296
+ onChunk(aes.update(data));
297
+ }
298
+ onChunk(aes.final());
299
+ const mac = hmac.digest().slice(0, 10);
300
+ sha256Enc.update(mac);
301
+ const fileSha256 = sha256Plain.digest();
302
+ const fileEncSha256 = sha256Enc.digest();
303
+ encFileWriteStream.write(mac);
304
+ encFileWriteStream.end();
305
+ originalFileStream?.end?.();
306
+ stream.destroy();
307
+ logger?.debug("encrypted data successfully");
308
+ return {
309
+ mediaKey,
310
+ originalFilePath,
311
+ encFilePath,
312
+ mac,
313
+ fileEncSha256,
314
+ fileSha256,
315
+ fileLength
316
+ };
317
+ }
318
+ catch (error) {
319
+ encFileWriteStream.destroy();
320
+ originalFileStream?.destroy?.();
321
+ aes.destroy();
322
+ hmac.destroy();
323
+ sha256Plain.destroy();
324
+ sha256Enc.destroy();
325
+ stream.destroy();
326
+ try {
327
+ await fs.unlink(encFilePath);
328
+ if (originalFilePath) {
329
+ await fs.unlink(originalFilePath);
330
+ }
331
+ }
332
+ catch (err) {
333
+ logger?.error({ err }, "failed deleting tmp files");
334
+ }
335
+ throw error;
336
+ }
337
+ };
338
+ //=======================================================//
339
+ const DEF_HOST = "mmg.whatsapp.net";
340
+ const AES_CHUNK_SIZE = 16;
341
+ const toSmallestChunkSize = (num) => {
342
+ return Math.floor(num / AES_CHUNK_SIZE) * AES_CHUNK_SIZE;
343
+ };
344
+ //=======================================================//
345
+ export const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`;
346
+ export const downloadContentFromMessage = async ({ mediaKey, directPath, url }, type, opts = {}) => {
347
+ const isValidMediaUrl = url?.startsWith("https://mmg.whatsapp.net/");
348
+ const downloadUrl = isValidMediaUrl ? url : getUrlFromDirectPath(directPath);
349
+ if (!downloadUrl) {
350
+ throw new Boom("No valid media URL or directPath present in message", { statusCode: 400 });
351
+ }
352
+ const keys = await getMediaKeys(mediaKey, type);
353
+ return downloadEncryptedContent(downloadUrl, keys, opts);
354
+ };
355
+ //=======================================================//
356
+ export const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) => {
357
+ let bytesFetched = 0;
358
+ let startChunk = 0;
359
+ let firstBlockIsIV = false;
360
+ if (startByte) {
361
+ const chunk = toSmallestChunkSize(startByte || 0);
362
+ if (chunk) {
363
+ startChunk = chunk - AES_CHUNK_SIZE;
364
+ bytesFetched = chunk;
365
+ firstBlockIsIV = true;
366
+ }
367
+ }
368
+ const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined;
369
+ const headersInit = options?.headers ? options.headers : undefined;
370
+ const headers = {
371
+ ...(headersInit
372
+ ? Array.isArray(headersInit)
373
+ ? Object.fromEntries(headersInit)
374
+ : headersInit
375
+ : {}),
376
+ Origin: DEFAULT_ORIGIN
377
+ };
378
+ if (startChunk || endChunk) {
379
+ headers.Range = `bytes=${startChunk}-`;
380
+ if (endChunk) {
381
+ headers.Range += endChunk;
382
+ }
383
+ }
384
+ const fetched = await getHttpStream(downloadUrl, {
385
+ ...(options || {}),
386
+ headers
387
+ });
388
+ let remainingBytes = Buffer.from([]);
389
+ let aes;
390
+ const pushBytes = (bytes, push) => {
391
+ if (startByte || endByte) {
392
+ const start = bytesFetched >= startByte ? undefined : Math.max(startByte - bytesFetched, 0);
393
+ const end = bytesFetched + bytes.length < endByte ? undefined : Math.max(endByte - bytesFetched, 0);
394
+ push(bytes.slice(start, end));
395
+ bytesFetched += bytes.length;
396
+ }
397
+ else {
398
+ push(bytes);
399
+ }
400
+ };
401
+ const output = new Transform({
402
+ transform(chunk, _, callback) {
403
+ let data = Buffer.concat([remainingBytes, chunk]);
404
+ const decryptLength = toSmallestChunkSize(data.length);
405
+ remainingBytes = data.slice(decryptLength);
406
+ data = data.slice(0, decryptLength);
407
+ if (!aes) {
408
+ let ivValue = iv;
409
+ if (firstBlockIsIV) {
410
+ ivValue = data.slice(0, AES_CHUNK_SIZE);
411
+ data = data.slice(AES_CHUNK_SIZE);
412
+ }
413
+ aes = Crypto.createDecipheriv("aes-256-cbc", cipherKey, ivValue);
414
+ if (endByte) {
415
+ aes.setAutoPadding(false);
416
+ }
417
+ }
418
+ try {
419
+ pushBytes(aes.update(data), b => this.push(b));
420
+ callback();
421
+ }
422
+ catch (error) {
423
+ callback(error);
424
+ }
425
+ },
426
+ final(callback) {
427
+ try {
428
+ pushBytes(aes.final(), b => this.push(b));
429
+ callback();
430
+ }
431
+ catch (error) {
432
+ callback(error);
433
+ }
434
+ }
435
+ });
436
+ return fetched.pipe(output, { end: true });
437
+ };
438
+ //=======================================================//
439
+ export function extensionForMediaMessage(message) {
440
+ const getExtension = (mimetype) => mimetype.split(";")[0]?.split("/")[1];
441
+ const type = Object.keys(message)[0];
442
+ let extension;
443
+ if (type === "locationMessage" || type === "liveLocationMessage" || type === "productMessage") {
444
+ extension = ".jpeg";
445
+ }
446
+ else {
447
+ const messageContent = message[type];
448
+ extension = getExtension(messageContent.mimetype);
449
+ }
450
+ return extension;
451
+ }
452
+ //=======================================================//
453
+ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
454
+ return async (filePath, { mediaType, fileEncSha256B64, timeoutMs }) => {
455
+ let uploadInfo = await refreshMediaConn(false);
456
+ let urls;
457
+ const hosts = [...customUploadHosts, ...uploadInfo.hosts];
458
+ fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64);
459
+ for (const { hostname } of hosts) {
460
+ logger.debug(`uploading to "${hostname}"`);
461
+ const auth = encodeURIComponent(uploadInfo.auth);
462
+ const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
463
+ let result;
464
+ try {
465
+ const stream = createReadStream(filePath);
466
+ const response = await fetch(url, {
467
+ dispatcher: fetchAgent,
468
+ method: "POST",
469
+ body: stream,
470
+ headers: {
471
+ ...(() => {
472
+ const hdrs = options?.headers;
473
+ if (!hdrs)
474
+ return {};
475
+ return Array.isArray(hdrs) ? Object.fromEntries(hdrs) : hdrs;
476
+ })(),
477
+ "Content-Type": "application/octet-stream",
478
+ Origin: DEFAULT_ORIGIN
479
+ },
480
+ duplex: "half",
481
+ signal: timeoutMs ? AbortSignal.timeout(timeoutMs) : undefined
482
+ });
483
+ let parsed = undefined;
484
+ try {
485
+ parsed = await response.json();
486
+ }
487
+ catch {
488
+ parsed = undefined;
489
+ }
490
+ result = parsed;
491
+ if (result?.url || result?.directPath) {
492
+ urls = {
493
+ mediaUrl: result.url,
494
+ directPath: result.direct_path,
495
+ meta_hmac: result.meta_hmac,
496
+ fbid: result.fbid,
497
+ ts: result.ts
498
+ };
499
+ break;
500
+ }
501
+ else {
502
+ uploadInfo = await refreshMediaConn(true);
503
+ throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
504
+ }
505
+ }
506
+ catch (error) {
507
+ const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname;
508
+ logger.warn({ trace: error?.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? "" : ", retrying..."}`);
509
+ }
510
+ }
511
+ if (!urls) {
512
+ throw new Boom("Media upload failed on all hosts", { statusCode: 500 });
513
+ }
514
+ return urls;
515
+ };
516
+ };
517
+ //=======================================================//
518
+ const getMediaRetryKey = (mediaKey) => {
519
+ return hkdf(mediaKey, 32, { info: "WhatsApp Media Retry Notification" });
520
+ };
521
+ //=======================================================//
522
+ export const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
523
+ const recp = { stanzaId: key.id };
524
+ const recpBuffer = proto.ServerErrorReceipt.encode(recp).finish();
525
+ const iv = Crypto.randomBytes(12);
526
+ const retryKey = await getMediaRetryKey(mediaKey);
527
+ const ciphertext = aesEncryptGCM(recpBuffer, retryKey, iv, Buffer.from(key.id));
528
+ const req = {
529
+ tag: "receipt",
530
+ attrs: {
531
+ id: key.id,
532
+ to: jidNormalizedUser(meId),
533
+ type: "server-error"
534
+ },
535
+ content: [
536
+ {
537
+ tag: "encrypt",
538
+ attrs: {},
539
+ content: [
540
+ { tag: "enc_p", attrs: {}, content: ciphertext },
541
+ { tag: "enc_iv", attrs: {}, content: iv }
542
+ ]
543
+ },
544
+ {
545
+ tag: "rmr",
546
+ attrs: {
547
+ jid: key.remoteJid,
548
+ from_me: (!!key.fromMe).toString(),
549
+ participant: key.participant || undefined
550
+ }
551
+ }
552
+ ]
553
+ };
554
+ return req;
555
+ };
556
+ //=======================================================//
557
+ export const decodeMediaRetryNode = (node) => {
558
+ const rmrNode = getBinaryNodeChild(node, "rmr");
559
+ const event = {
560
+ key: {
561
+ id: node.attrs.id,
562
+ remoteJid: rmrNode.attrs.jid,
563
+ fromMe: rmrNode.attrs.from_me === "true",
564
+ participant: rmrNode.attrs.participant
565
+ }
566
+ };
567
+ const errorNode = getBinaryNodeChild(node, "error");
568
+ if (errorNode) {
569
+ const errorCode = +errorNode.attrs.code;
570
+ event.error = new Boom(`Failed to re-upload media (${errorCode})`, {
571
+ data: errorNode.attrs,
572
+ statusCode: getStatusCodeForMediaRetry(errorCode)
573
+ });
574
+ }
575
+ else {
576
+ const encryptedInfoNode = getBinaryNodeChild(node, "encrypt");
577
+ const ciphertext = getBinaryNodeChildBuffer(encryptedInfoNode, "enc_p");
578
+ const iv = getBinaryNodeChildBuffer(encryptedInfoNode, "enc_iv");
579
+ if (ciphertext && iv) {
580
+ event.media = { ciphertext, iv };
581
+ }
582
+ else {
583
+ event.error = new Boom("Failed to re-upload media (missing ciphertext)", { statusCode: 404 });
584
+ }
585
+ }
586
+ return event;
587
+ };
588
+ //=======================================================//
589
+ export const decryptMediaRetryData = async ({ ciphertext, iv }, mediaKey, msgId) => {
590
+ const retryKey = await getMediaRetryKey(mediaKey);
591
+ const plaintext = aesDecryptGCM(ciphertext, retryKey, iv, Buffer.from(msgId));
592
+ return proto.MediaRetryNotification.decode(plaintext);
593
+ };
594
+ //=======================================================//
595
+ export const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code];
596
+ const MEDIA_RETRY_STATUS_MAP = {
597
+ [proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
598
+ [proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
599
+ [proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
600
+ [proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418
601
+ };
602
+ //=======================================================//