@japofc/baileys 2.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 (342) hide show
  1. package/NOTICE.md +111 -0
  2. package/README.md +1254 -0
  3. package/WAProto/index.d.ts +9 -0
  4. package/WAProto/index.js +142276 -0
  5. package/engine-requirements.js +13 -0
  6. package/lib/Builders/A2UI.d.ts +242 -0
  7. package/lib/Builders/A2UI.js +308 -0
  8. package/lib/Builders/AIRich.d.ts +105 -0
  9. package/lib/Builders/AIRich.js +2286 -0
  10. package/lib/Builders/Button.d.ts +103 -0
  11. package/lib/Builders/Button.js +883 -0
  12. package/lib/Builders/ButtonV2.d.ts +28 -0
  13. package/lib/Builders/ButtonV2.js +144 -0
  14. package/lib/Builders/ButtonV3.d.ts +25 -0
  15. package/lib/Builders/ButtonV3.js +184 -0
  16. package/lib/Builders/Carousel.d.ts +22 -0
  17. package/lib/Builders/Carousel.js +111 -0
  18. package/lib/Builders/JapBaileys.d.ts +34 -0
  19. package/lib/Builders/JapBaileys.js +108 -0
  20. package/lib/Builders/Poll.d.ts +25 -0
  21. package/lib/Builders/Poll.js +128 -0
  22. package/lib/Builders/index.d.ts +17 -0
  23. package/lib/Builders/index.js +11 -0
  24. package/lib/Builders/shared.d.ts +67 -0
  25. package/lib/Builders/shared.js +652 -0
  26. package/lib/Defaults/index.d.ts +154 -0
  27. package/lib/Defaults/index.js +178 -0
  28. package/lib/Framework/Bot.d.ts +61 -0
  29. package/lib/Framework/Bot.js +248 -0
  30. package/lib/Framework/Context.d.ts +21 -0
  31. package/lib/Framework/Context.js +89 -0
  32. package/lib/Framework/MediaManager.d.ts +17 -0
  33. package/lib/Framework/MediaManager.js +151 -0
  34. package/lib/Framework/SessionManager.d.ts +13 -0
  35. package/lib/Framework/SessionManager.js +33 -0
  36. package/lib/Framework/StatsManager.d.ts +26 -0
  37. package/lib/Framework/StatsManager.js +119 -0
  38. package/lib/Framework/Store/SQLiteStore.d.ts +11 -0
  39. package/lib/Framework/Store/SQLiteStore.js +73 -0
  40. package/lib/Framework/index.d.ts +9 -0
  41. package/lib/Framework/index.js +10 -0
  42. package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
  43. package/lib/Signal/Group/ciphertext-message.js +11 -0
  44. package/lib/Signal/Group/group-session-builder.d.ts +7 -0
  45. package/lib/Signal/Group/group-session-builder.js +29 -0
  46. package/lib/Signal/Group/group_cipher.d.ts +10 -0
  47. package/lib/Signal/Group/group_cipher.js +81 -0
  48. package/lib/Signal/Group/index.d.ts +11 -0
  49. package/lib/Signal/Group/index.js +11 -0
  50. package/lib/Signal/Group/keyhelper.d.ts +6 -0
  51. package/lib/Signal/Group/keyhelper.js +17 -0
  52. package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
  53. package/lib/Signal/Group/sender-chain-key.js +25 -0
  54. package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
  55. package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
  56. package/lib/Signal/Group/sender-key-message.d.ts +18 -0
  57. package/lib/Signal/Group/sender-key-message.js +65 -0
  58. package/lib/Signal/Group/sender-key-name.d.ts +11 -0
  59. package/lib/Signal/Group/sender-key-name.js +47 -0
  60. package/lib/Signal/Group/sender-key-record.d.ts +12 -0
  61. package/lib/Signal/Group/sender-key-record.js +40 -0
  62. package/lib/Signal/Group/sender-key-state.d.ts +16 -0
  63. package/lib/Signal/Group/sender-key-state.js +83 -0
  64. package/lib/Signal/Group/sender-message-key.d.ts +11 -0
  65. package/lib/Signal/Group/sender-message-key.js +25 -0
  66. package/lib/Signal/libsignal.d.ts +54 -0
  67. package/lib/Signal/libsignal.js +430 -0
  68. package/lib/Signal/lid-mapping.d.ts +20 -0
  69. package/lib/Signal/lid-mapping.js +276 -0
  70. package/lib/Socket/Client/index.d.ts +2 -0
  71. package/lib/Socket/Client/index.js +2 -0
  72. package/lib/Socket/Client/types.d.ts +5 -0
  73. package/lib/Socket/Client/types.js +10 -0
  74. package/lib/Socket/Client/websocket.d.ts +12 -0
  75. package/lib/Socket/Client/websocket.js +53 -0
  76. package/lib/Socket/business.d.ts +395 -0
  77. package/lib/Socket/business.js +379 -0
  78. package/lib/Socket/chats.d.ts +136 -0
  79. package/lib/Socket/chats.js +1327 -0
  80. package/lib/Socket/communities.d.ts +562 -0
  81. package/lib/Socket/communities.js +439 -0
  82. package/lib/Socket/dugong.d.ts +117 -0
  83. package/lib/Socket/dugong.js +798 -0
  84. package/lib/Socket/groups.d.ts +285 -0
  85. package/lib/Socket/groups.js +354 -0
  86. package/lib/Socket/index.d.ts +585 -0
  87. package/lib/Socket/index.js +52 -0
  88. package/lib/Socket/messages-recv.d.ts +333 -0
  89. package/lib/Socket/messages-recv.js +1781 -0
  90. package/lib/Socket/messages-send.d.ts +353 -0
  91. package/lib/Socket/messages-send.js +1524 -0
  92. package/lib/Socket/mex.d.ts +1 -0
  93. package/lib/Socket/mex.js +41 -0
  94. package/lib/Socket/newsletter.d.ts +286 -0
  95. package/lib/Socket/newsletter.js +364 -0
  96. package/lib/Socket/socket.d.ts +60 -0
  97. package/lib/Socket/socket.js +1012 -0
  98. package/lib/Socket/username.d.ts +82 -0
  99. package/lib/Socket/username.js +159 -0
  100. package/lib/Store/index.d.ts +3 -0
  101. package/lib/Store/index.js +3 -0
  102. package/lib/Store/make-in-memory-store.d.ts +62 -0
  103. package/lib/Store/make-in-memory-store.js +428 -0
  104. package/lib/Store/make-ordered-dictionary.d.ts +12 -0
  105. package/lib/Store/make-ordered-dictionary.js +78 -0
  106. package/lib/Store/object-repository.d.ts +10 -0
  107. package/lib/Store/object-repository.js +23 -0
  108. package/lib/Types/Auth.d.ts +117 -0
  109. package/lib/Types/Auth.js +1 -0
  110. package/lib/Types/Bussines.d.ts +25 -0
  111. package/lib/Types/Call.d.ts +15 -0
  112. package/lib/Types/Call.js +1 -0
  113. package/lib/Types/Chat.d.ts +124 -0
  114. package/lib/Types/Chat.js +7 -0
  115. package/lib/Types/Contact.d.ts +26 -0
  116. package/lib/Types/Contact.js +1 -0
  117. package/lib/Types/Events.d.ts +256 -0
  118. package/lib/Types/Events.js +1 -0
  119. package/lib/Types/GroupMetadata.d.ts +71 -0
  120. package/lib/Types/GroupMetadata.js +1 -0
  121. package/lib/Types/Label.d.ts +5 -0
  122. package/lib/Types/Label.js +24 -0
  123. package/lib/Types/LabelAssociation.d.ts +6 -0
  124. package/lib/Types/LabelAssociation.js +6 -0
  125. package/lib/Types/Message.d.ts +62 -0
  126. package/lib/Types/Message.js +17 -0
  127. package/lib/Types/Mex.d.ts +2 -0
  128. package/lib/Types/Mex.js +38 -0
  129. package/lib/Types/Product.d.ts +79 -0
  130. package/lib/Types/Product.js +1 -0
  131. package/lib/Types/RichType.d.ts +2 -0
  132. package/lib/Types/RichType.js +22 -0
  133. package/lib/Types/Signal.d.ts +87 -0
  134. package/lib/Types/Signal.js +1 -0
  135. package/lib/Types/Socket.d.ts +138 -0
  136. package/lib/Types/Socket.js +1 -0
  137. package/lib/Types/State.d.ts +8 -0
  138. package/lib/Types/State.js +55 -0
  139. package/lib/Types/USync.d.ts +26 -0
  140. package/lib/Types/index.d.ts +68 -0
  141. package/lib/Types/index.js +27 -0
  142. package/lib/Utils/MessageBuilder.d.ts +2 -0
  143. package/lib/Utils/MessageBuilder.js +2 -0
  144. package/lib/Utils/PersistentStore.d.ts +60 -0
  145. package/lib/Utils/PersistentStore.js +592 -0
  146. package/lib/Utils/anti-delete.d.ts +74 -0
  147. package/lib/Utils/anti-delete.js +221 -0
  148. package/lib/Utils/auth-secure.d.ts +86 -0
  149. package/lib/Utils/auth-secure.js +764 -0
  150. package/lib/Utils/auth-utils.d.ts +57 -0
  151. package/lib/Utils/auth-utils.js +306 -0
  152. package/lib/Utils/auto-reply.d.ts +47 -0
  153. package/lib/Utils/auto-reply.js +157 -0
  154. package/lib/Utils/baileys-event-stream.d.ts +8 -0
  155. package/lib/Utils/baileys-event-stream.js +62 -0
  156. package/lib/Utils/browser-utils.d.ts +8 -0
  157. package/lib/Utils/browser-utils.js +27 -0
  158. package/lib/Utils/business.d.ts +49 -0
  159. package/lib/Utils/business.js +239 -0
  160. package/lib/Utils/button-helper-utils.d.ts +53 -0
  161. package/lib/Utils/button-helper-utils.js +312 -0
  162. package/lib/Utils/button-sender.d.ts +54 -0
  163. package/lib/Utils/button-sender.js +822 -0
  164. package/lib/Utils/chat-control.d.ts +159 -0
  165. package/lib/Utils/chat-control.js +230 -0
  166. package/lib/Utils/chat-history-helpers.d.ts +21 -0
  167. package/lib/Utils/chat-history-helpers.js +70 -0
  168. package/lib/Utils/chat-utils.d.ts +407 -0
  169. package/lib/Utils/chat-utils.js +885 -0
  170. package/lib/Utils/companion-reg-client-utils.d.ts +4 -0
  171. package/lib/Utils/companion-reg-client-utils.js +39 -0
  172. package/lib/Utils/crypto.d.ts +41 -0
  173. package/lib/Utils/crypto.js +147 -0
  174. package/lib/Utils/decode-wa-message.d.ts +83 -0
  175. package/lib/Utils/decode-wa-message.js +316 -0
  176. package/lib/Utils/event-buffer.d.ts +12 -0
  177. package/lib/Utils/event-buffer.js +635 -0
  178. package/lib/Utils/generics.d.ts +69 -0
  179. package/lib/Utils/generics.js +438 -0
  180. package/lib/Utils/history-share.d.ts +11 -0
  181. package/lib/Utils/history-share.js +70 -0
  182. package/lib/Utils/history.d.ts +90 -0
  183. package/lib/Utils/history.js +146 -0
  184. package/lib/Utils/humanizer.d.ts +12 -0
  185. package/lib/Utils/humanizer.js +73 -0
  186. package/lib/Utils/identity-change-handler.d.ts +13 -0
  187. package/lib/Utils/identity-change-handler.js +49 -0
  188. package/lib/Utils/index.d.ts +57 -0
  189. package/lib/Utils/index.js +56 -0
  190. package/lib/Utils/jid-tools.d.ts +6 -0
  191. package/lib/Utils/jid-tools.js +77 -0
  192. package/lib/Utils/link-preview.d.ts +12 -0
  193. package/lib/Utils/link-preview.js +84 -0
  194. package/lib/Utils/logger.d.ts +4 -0
  195. package/lib/Utils/logger.js +2 -0
  196. package/lib/Utils/lt-hash.d.ts +12 -0
  197. package/lib/Utils/lt-hash.js +26 -0
  198. package/lib/Utils/make-mutex.d.ts +6 -0
  199. package/lib/Utils/make-mutex.js +32 -0
  200. package/lib/Utils/media-messages.d.ts +18 -0
  201. package/lib/Utils/media-messages.js +70 -0
  202. package/lib/Utils/media-set.d.ts +13 -0
  203. package/lib/Utils/media-set.js +171 -0
  204. package/lib/Utils/message-kind.d.ts +32 -0
  205. package/lib/Utils/message-kind.js +136 -0
  206. package/lib/Utils/message-retry-manager.d.ts +79 -0
  207. package/lib/Utils/message-retry-manager.js +264 -0
  208. package/lib/Utils/message-search.d.ts +44 -0
  209. package/lib/Utils/message-search.js +188 -0
  210. package/lib/Utils/messages-media.d.ts +135 -0
  211. package/lib/Utils/messages-media.js +868 -0
  212. package/lib/Utils/messages.d.ts +44 -0
  213. package/lib/Utils/messages.js +2326 -0
  214. package/lib/Utils/meta-ai.d.ts +13 -0
  215. package/lib/Utils/meta-ai.js +74 -0
  216. package/lib/Utils/mini-app.d.ts +35 -0
  217. package/lib/Utils/mini-app.js +213 -0
  218. package/lib/Utils/noise-handler.d.ts +20 -0
  219. package/lib/Utils/noise-handler.js +200 -0
  220. package/lib/Utils/offline-node-processor.d.ts +9 -0
  221. package/lib/Utils/offline-node-processor.js +39 -0
  222. package/lib/Utils/past-participants.d.ts +14 -0
  223. package/lib/Utils/past-participants.js +40 -0
  224. package/lib/Utils/pre-key-manager.d.ts +25 -0
  225. package/lib/Utils/pre-key-manager.js +105 -0
  226. package/lib/Utils/process-message.d.ts +82 -0
  227. package/lib/Utils/process-message.js +808 -0
  228. package/lib/Utils/qr-render.d.ts +59 -0
  229. package/lib/Utils/qr-render.js +179 -0
  230. package/lib/Utils/qrcodegen.d.ts +79 -0
  231. package/lib/Utils/qrcodegen.js +848 -0
  232. package/lib/Utils/reporting-utils.d.ts +12 -0
  233. package/lib/Utils/reporting-utils.js +257 -0
  234. package/lib/Utils/rich-message-utils.d.ts +48 -0
  235. package/lib/Utils/rich-message-utils.js +444 -0
  236. package/lib/Utils/router.d.ts +37 -0
  237. package/lib/Utils/router.js +165 -0
  238. package/lib/Utils/scheduled-posts.d.ts +30 -0
  239. package/lib/Utils/scheduled-posts.js +110 -0
  240. package/lib/Utils/scheduling.d.ts +42 -0
  241. package/lib/Utils/scheduling.js +139 -0
  242. package/lib/Utils/signal.d.ts +82 -0
  243. package/lib/Utils/signal.js +200 -0
  244. package/lib/Utils/stanza-ack.d.ts +16 -0
  245. package/lib/Utils/stanza-ack.js +37 -0
  246. package/lib/Utils/status.d.ts +50 -0
  247. package/lib/Utils/status.js +107 -0
  248. package/lib/Utils/stickerpack.d.ts +51 -0
  249. package/lib/Utils/stickerpack.js +274 -0
  250. package/lib/Utils/sync-action-utils.d.ts +2 -0
  251. package/lib/Utils/sync-action-utils.js +48 -0
  252. package/lib/Utils/tag.d.ts +12 -0
  253. package/lib/Utils/tag.js +44 -0
  254. package/lib/Utils/tc-token-utils.d.ts +29 -0
  255. package/lib/Utils/tc-token-utils.js +162 -0
  256. package/lib/Utils/templates.d.ts +76 -0
  257. package/lib/Utils/templates.js +150 -0
  258. package/lib/Utils/transcribe.d.ts +12 -0
  259. package/lib/Utils/transcribe.js +107 -0
  260. package/lib/Utils/use-cache-manager-auth-state.d.ts +13 -0
  261. package/lib/Utils/use-cache-manager-auth-state.js +81 -0
  262. package/lib/Utils/use-multi-file-auth-state.d.ts +10 -0
  263. package/lib/Utils/use-multi-file-auth-state.js +166 -0
  264. package/lib/Utils/use-single-file-auth-state.d.ts +10 -0
  265. package/lib/Utils/use-single-file-auth-state.js +113 -0
  266. package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
  267. package/lib/Utils/use-sqlite-auth-state.js +169 -0
  268. package/lib/Utils/validate-connection.d.ts +42 -0
  269. package/lib/Utils/validate-connection.js +198 -0
  270. package/lib/Utils/vcard.d.ts +58 -0
  271. package/lib/Utils/vcard.js +104 -0
  272. package/lib/Utils/voice-note.d.ts +15 -0
  273. package/lib/Utils/voice-note.js +63 -0
  274. package/lib/VoIP/audio-feeder.d.ts +18 -0
  275. package/lib/VoIP/audio-feeder.js +160 -0
  276. package/lib/VoIP/call-recorder.d.ts +13 -0
  277. package/lib/VoIP/call-recorder.js +73 -0
  278. package/lib/VoIP/index.d.ts +86 -0
  279. package/lib/VoIP/index.js +611 -0
  280. package/lib/VoIP/relay-transport.d.ts +43 -0
  281. package/lib/VoIP/relay-transport.js +563 -0
  282. package/lib/VoIP/signaling.d.ts +26 -0
  283. package/lib/VoIP/signaling.js +662 -0
  284. package/lib/VoIP/types.d.ts +69 -0
  285. package/lib/VoIP/types.js +17 -0
  286. package/lib/VoIP/wasm-engine.d.ts +120 -0
  287. package/lib/VoIP/wasm-engine.js +1358 -0
  288. package/lib/VoIP/worker-bootstrap.d.ts +4 -0
  289. package/lib/VoIP/worker-bootstrap.js +1046 -0
  290. package/lib/WABinary/constants.d.ts +61 -0
  291. package/lib/WABinary/constants.js +1466 -0
  292. package/lib/WABinary/decode.d.ts +9 -0
  293. package/lib/WABinary/decode.js +261 -0
  294. package/lib/WABinary/encode.d.ts +2 -0
  295. package/lib/WABinary/encode.js +219 -0
  296. package/lib/WABinary/generic-utils.d.ts +74 -0
  297. package/lib/WABinary/generic-utils.js +276 -0
  298. package/lib/WABinary/index.d.ts +5 -0
  299. package/lib/WABinary/index.js +5 -0
  300. package/lib/WABinary/jid-utils.d.ts +28 -0
  301. package/lib/WABinary/jid-utils.js +95 -0
  302. package/lib/WABinary/types.d.ts +19 -0
  303. package/lib/WABinary/types.js +1 -0
  304. package/lib/WAM/BinaryInfo.d.ts +7 -0
  305. package/lib/WAM/BinaryInfo.js +9 -0
  306. package/lib/WAM/constants.d.ts +34926 -0
  307. package/lib/WAM/constants.js +22852 -0
  308. package/lib/WAM/encode.d.ts +1 -0
  309. package/lib/WAM/encode.js +149 -0
  310. package/lib/WAM/index.d.ts +3 -0
  311. package/lib/WAM/index.js +3 -0
  312. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
  313. package/lib/WAUSync/Protocols/USyncContactProtocol.js +51 -0
  314. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +29 -0
  315. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +62 -0
  316. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  317. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
  318. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  319. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
  320. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
  321. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
  322. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
  323. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
  324. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
  325. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  326. package/lib/WAUSync/Protocols/index.d.ts +7 -0
  327. package/lib/WAUSync/Protocols/index.js +7 -0
  328. package/lib/WAUSync/USyncQuery.d.ts +20 -0
  329. package/lib/WAUSync/USyncQuery.js +121 -0
  330. package/lib/WAUSync/USyncUser.d.ts +16 -0
  331. package/lib/WAUSync/USyncUser.js +30 -0
  332. package/lib/WAUSync/index.d.ts +3 -0
  333. package/lib/WAUSync/index.js +3 -0
  334. package/lib/assets/wasm/loader.d.ts +5 -0
  335. package/lib/assets/wasm/loader.js +5 -0
  336. package/lib/assets/wasm/whatsapp.wasm +0 -0
  337. package/lib/assets/wasm/worker-modules.d.ts +5 -0
  338. package/lib/assets/wasm/worker-modules.js +273 -0
  339. package/lib/index.d.ts +18 -0
  340. package/lib/index.js +23 -0
  341. package/package.json +180 -0
  342. package/postinstall-banner.js +82 -0
@@ -0,0 +1,4 @@
1
+ export const CompanionWebClientType: any;
2
+ export function getCompanionWebClientType([os, browserName]: [any, any]): any;
3
+ export function getCompanionPlatformId(browser: any): any;
4
+ export function buildPairingQRData(ref: any, noiseKeyB64: any, identityKeyB64: any, advB64: any, browser: any): string;
@@ -0,0 +1,39 @@
1
+ export var CompanionWebClientType;
2
+ (function (CompanionWebClientType) {
3
+ CompanionWebClientType[CompanionWebClientType["UNKNOWN"] = 0] = "UNKNOWN";
4
+ CompanionWebClientType[CompanionWebClientType["CHROME"] = 1] = "CHROME";
5
+ CompanionWebClientType[CompanionWebClientType["EDGE"] = 2] = "EDGE";
6
+ CompanionWebClientType[CompanionWebClientType["FIREFOX"] = 3] = "FIREFOX";
7
+ CompanionWebClientType[CompanionWebClientType["IE"] = 4] = "IE";
8
+ CompanionWebClientType[CompanionWebClientType["OPERA"] = 5] = "OPERA";
9
+ CompanionWebClientType[CompanionWebClientType["SAFARI"] = 6] = "SAFARI";
10
+ CompanionWebClientType[CompanionWebClientType["ELECTRON"] = 7] = "ELECTRON";
11
+ CompanionWebClientType[CompanionWebClientType["UWP"] = 8] = "UWP";
12
+ CompanionWebClientType[CompanionWebClientType["OTHER_WEB_CLIENT"] = 9] = "OTHER_WEB_CLIENT";
13
+ })(CompanionWebClientType || (CompanionWebClientType = {}));
14
+ const BROWSER_TO_COMPANION_WEB_CLIENT = {
15
+ Chrome: CompanionWebClientType.CHROME,
16
+ Edge: CompanionWebClientType.EDGE,
17
+ Firefox: CompanionWebClientType.FIREFOX,
18
+ IE: CompanionWebClientType.IE,
19
+ Opera: CompanionWebClientType.OPERA,
20
+ Safari: CompanionWebClientType.SAFARI
21
+ };
22
+ export const getCompanionWebClientType = ([os, browserName]) => {
23
+ if (browserName === 'Desktop') {
24
+ return os === 'Windows' ? CompanionWebClientType.UWP : CompanionWebClientType.ELECTRON;
25
+ }
26
+ return BROWSER_TO_COMPANION_WEB_CLIENT[browserName] || CompanionWebClientType.OTHER_WEB_CLIENT;
27
+ };
28
+ export const getCompanionPlatformId = (browser) => {
29
+ return getCompanionWebClientType(browser).toString();
30
+ };
31
+ export const buildPairingQRData = (ref, noiseKeyB64, identityKeyB64, advB64, browser) => {
32
+ return 'https://wa.me/settings/linked_devices#' + [
33
+ ref,
34
+ noiseKeyB64,
35
+ identityKeyB64,
36
+ advB64,
37
+ getCompanionPlatformId(browser)
38
+ ].join(',');
39
+ };
@@ -0,0 +1,41 @@
1
+ /**
2
+ * encrypt AES 256 GCM;
3
+ * where the tag tag is suffixed to the ciphertext
4
+ * */
5
+ export function aesEncryptGCM(plaintext: any, key: any, iv: any, additionalData: any): any;
6
+ /**
7
+ * decrypt AES 256 GCM;
8
+ * where the auth tag is suffixed to the ciphertext
9
+ * */
10
+ export function aesDecryptGCM(ciphertext: any, key: any, iv: any, additionalData: any): any;
11
+ export function aesEncryptCTR(plaintext: any, key: any, iv: any): any;
12
+ export function aesDecryptCTR(ciphertext: any, key: any, iv: any): any;
13
+ /** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
14
+ export function aesDecrypt(buffer: any, key: any): any;
15
+ /** decrypt AES 256 CBC */
16
+ export function aesDecryptWithIV(buffer: any, key: any, IV: any): any;
17
+ export function aesEncrypt(buffer: any, key: any): any;
18
+ export function aesEncrypWithIV(buffer: any, key: any, IV: any): any;
19
+ export function hmacSign(buffer: any, key: any, variant?: string): any;
20
+ export function sha256(buffer: any): any;
21
+ export function derivePairingCodeKey(pairingCode: any, salt: any): Promise<any>;
22
+ export function generateSignalPubKey(pubKey: any): any;
23
+ export namespace Curve {
24
+ function generateKeyPair(): {
25
+ private: any;
26
+ public: any;
27
+ };
28
+ function sharedKey(privateKey: any, publicKey: any): any;
29
+ function sign(privateKey: any, buf: any): any;
30
+ function verify(pubKey: any, message: any, signature: any): boolean;
31
+ }
32
+ export function signedKeyPair(identityKeyPair: any, keyId: any): {
33
+ keyPair: {
34
+ private: any;
35
+ public: any;
36
+ };
37
+ signature: any;
38
+ keyId: any;
39
+ };
40
+ export let md5: any;
41
+ export let hkdf: any;
@@ -0,0 +1,147 @@
1
+ import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from 'crypto';
2
+ import * as curve from 'libsignal/src/curve.js';
3
+ import { KEY_BUNDLE_TYPE } from '../Defaults/index.js';
4
+
5
+ // JAP@Fix (bug 36): whatsapp-rust-bridge is a native module — crashes on ARM musl/Alpine/Termux.
6
+ // Wrap in try-catch and fall back to pure JS implementations.
7
+ let md5, hkdf;
8
+ try {
9
+ const rustBridge = await import('whatsapp-rust-bridge');
10
+ md5 = rustBridge.md5;
11
+ hkdf = rustBridge.hkdf;
12
+ } catch {
13
+ // Pure JS fallback for md5
14
+ md5 = (buf) => createHash('md5').update(buf).digest();
15
+ // Pure JS fallback for hkdf (HKDF-SHA256)
16
+ hkdf = (key, length, { salt, info } = {}) => {
17
+ const hashLen = 32; // SHA-256
18
+ const prk = salt
19
+ ? createHmac('sha256', salt).update(key).digest()
20
+ : createHmac('sha256', Buffer.alloc(hashLen)).update(key).digest();
21
+ const infoBuffer = info ? (typeof info === 'string' ? Buffer.from(info) : Buffer.from(info)) : Buffer.alloc(0);
22
+ const blocks = Math.ceil(length / hashLen);
23
+ let prev = Buffer.alloc(0);
24
+ const output = [];
25
+ for (let i = 1; i <= blocks; i++) {
26
+ prev = createHmac('sha256', prk)
27
+ .update(Buffer.concat([prev, infoBuffer, Buffer.from([i])]))
28
+ .digest();
29
+ output.push(prev);
30
+ }
31
+ return Buffer.concat(output).subarray(0, length);
32
+ };
33
+ }
34
+ export { md5, hkdf };
35
+ // insure browser & node compatibility
36
+ const { subtle } = globalThis.crypto;
37
+ /** prefix version byte to the pub keys, required for some curve crypto functions */
38
+ export const generateSignalPubKey = (pubKey) => pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey]);
39
+ export const Curve = {
40
+ generateKeyPair: () => {
41
+ const { pubKey, privKey } = curve.generateKeyPair();
42
+ return {
43
+ private: Buffer.from(privKey),
44
+ // remove version byte
45
+ public: Buffer.from(pubKey.slice(1))
46
+ };
47
+ },
48
+ sharedKey: (privateKey, publicKey) => {
49
+ const shared = curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey);
50
+ return Buffer.from(shared);
51
+ },
52
+ sign: (privateKey, buf) => curve.calculateSignature(privateKey, buf),
53
+ verify: (pubKey, message, signature) => {
54
+ try {
55
+ curve.verifySignature(generateSignalPubKey(pubKey), message, signature);
56
+ return true;
57
+ }
58
+ catch (error) {
59
+ return false;
60
+ }
61
+ }
62
+ };
63
+ export const signedKeyPair = (identityKeyPair, keyId) => {
64
+ const preKey = Curve.generateKeyPair();
65
+ const pubKey = generateSignalPubKey(preKey.public);
66
+ const signature = Curve.sign(identityKeyPair.private, pubKey);
67
+ return { keyPair: preKey, signature, keyId };
68
+ };
69
+ const GCM_TAG_LENGTH = 128 >> 3;
70
+ /**
71
+ * encrypt AES 256 GCM;
72
+ * where the tag tag is suffixed to the ciphertext
73
+ * */
74
+ export function aesEncryptGCM(plaintext, key, iv, additionalData) {
75
+ const cipher = createCipheriv('aes-256-gcm', key, iv);
76
+ cipher.setAAD(additionalData);
77
+ return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
78
+ }
79
+ /**
80
+ * decrypt AES 256 GCM;
81
+ * where the auth tag is suffixed to the ciphertext
82
+ * */
83
+ export function aesDecryptGCM(ciphertext, key, iv, additionalData) {
84
+ const decipher = createDecipheriv('aes-256-gcm', key, iv);
85
+ // decrypt additional adata
86
+ const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
87
+ const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
88
+ // set additional data
89
+ decipher.setAAD(additionalData);
90
+ decipher.setAuthTag(tag);
91
+ return Buffer.concat([decipher.update(enc), decipher.final()]);
92
+ }
93
+ export function aesEncryptCTR(plaintext, key, iv) {
94
+ const cipher = createCipheriv('aes-256-ctr', key, iv);
95
+ return Buffer.concat([cipher.update(plaintext), cipher.final()]);
96
+ }
97
+ export function aesDecryptCTR(ciphertext, key, iv) {
98
+ const decipher = createDecipheriv('aes-256-ctr', key, iv);
99
+ return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
100
+ }
101
+ /** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
102
+ export function aesDecrypt(buffer, key) {
103
+ return aesDecryptWithIV(buffer.subarray(16), key, buffer.subarray(0, 16));
104
+ }
105
+ /** decrypt AES 256 CBC */
106
+ export function aesDecryptWithIV(buffer, key, IV) {
107
+ const aes = createDecipheriv('aes-256-cbc', key, IV);
108
+ return Buffer.concat([aes.update(buffer), aes.final()]);
109
+ }
110
+ // encrypt AES 256 CBC; where a random IV is prefixed to the buffer
111
+ export function aesEncrypt(buffer, key) {
112
+ const IV = randomBytes(16);
113
+ const aes = createCipheriv('aes-256-cbc', key, IV);
114
+ return Buffer.concat([IV, aes.update(buffer), aes.final()]); // prefix IV to the buffer
115
+ }
116
+ // encrypt AES 256 CBC with a given IV
117
+ export function aesEncrypWithIV(buffer, key, IV) {
118
+ const aes = createCipheriv('aes-256-cbc', key, IV);
119
+ return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer
120
+ }
121
+ // sign HMAC using SHA 256
122
+ export function hmacSign(buffer, key, variant = 'sha256') {
123
+ return createHmac(variant, key).update(buffer).digest();
124
+ }
125
+ export function sha256(buffer) {
126
+ return createHash('sha256').update(buffer).digest();
127
+ }
128
+ export async function derivePairingCodeKey(pairingCode, salt) {
129
+ // Convert inputs to formats Web Crypto API can work with
130
+ const encoder = new TextEncoder();
131
+ const pairingCodeBuffer = encoder.encode(pairingCode);
132
+ const saltBuffer = new Uint8Array(salt instanceof Uint8Array ? salt : new Uint8Array(salt));
133
+ // Import the pairing code as key material
134
+ const keyMaterial = await subtle.importKey('raw', pairingCodeBuffer, { name: 'PBKDF2' }, false, [
135
+ 'deriveBits'
136
+ ]);
137
+ // Derive bits using PBKDF2 with the same parameters
138
+ // 2 << 16 = 131,072 iterations
139
+ const derivedBits = await subtle.deriveBits({
140
+ name: 'PBKDF2',
141
+ salt: saltBuffer,
142
+ iterations: 2 << 16,
143
+ hash: 'SHA-256'
144
+ }, keyMaterial, 32 * 8 // 32 bytes * 8 = 256 bits
145
+ );
146
+ return Buffer.from(derivedBits);
147
+ }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Decode the received node as a message.
3
+ * @note this will only parse the message, not decrypt it
4
+ */
5
+ export function decodeMessageNode(stanza: any, meId: any, meLid: any): {
6
+ fullMessage: {
7
+ key: {
8
+ server_id?: any;
9
+ remoteJid: any;
10
+ remoteJidAlt: any;
11
+ remoteJidUsername: any;
12
+ fromMe: boolean;
13
+ id: any;
14
+ participant: any;
15
+ participantAlt: any;
16
+ participantUsername: any;
17
+ addressingMode: any;
18
+ };
19
+ category: any;
20
+ messageTimestamp: number;
21
+ pushName: any;
22
+ broadcast: any;
23
+ };
24
+ author: any;
25
+ sender: any;
26
+ };
27
+ export function getDecryptionJid(sender: any, repository: any): Promise<any>;
28
+ export const NO_MESSAGE_FOUND_ERROR_TEXT: "Message absent from node";
29
+ export const MISSING_KEYS_ERROR_TEXT: "Key used already or never filled";
30
+ export const ACCOUNT_RESTRICTED_TEXT: "Your account has been restricted";
31
+ export namespace DECRYPTION_RETRY_CONFIG {
32
+ let maxRetries: number;
33
+ let baseDelayMs: number;
34
+ let sessionRecordErrors: string[];
35
+ }
36
+ export namespace NACK_REASONS {
37
+ let SenderReachoutTimelocked: number;
38
+ let ParsingError: number;
39
+ let UnrecognizedStanza: number;
40
+ let UnrecognizedStanzaClass: number;
41
+ let UnrecognizedStanzaType: number;
42
+ let InvalidProtobuf: number;
43
+ let InvalidHostedCompanionStanza: number;
44
+ let MissingMessageSecret: number;
45
+ let SignalErrorOldCounter: number;
46
+ let MessageDeletedOnPeer: number;
47
+ let UnhandledError: number;
48
+ let UnsupportedAdminRevoke: number;
49
+ let UnsupportedLIDGroup: number;
50
+ let DBOperationFailed: number;
51
+ }
52
+ export namespace SERVER_ERROR_CODES {
53
+ let MessageAccountRestriction: string;
54
+ let SmaxInvalid: string;
55
+ }
56
+ export function extractAddressingContext(stanza: any): {
57
+ addressingMode: any;
58
+ senderAlt: any;
59
+ recipientAlt: any;
60
+ };
61
+ export function decryptMessageNode(stanza: any, meId: any, meLid: any, repository: any, logger: any): {
62
+ fullMessage: {
63
+ key: {
64
+ server_id?: any;
65
+ remoteJid: any;
66
+ remoteJidAlt: any;
67
+ remoteJidUsername: any;
68
+ fromMe: boolean;
69
+ id: any;
70
+ participant: any;
71
+ participantAlt: any;
72
+ participantUsername: any;
73
+ addressingMode: any;
74
+ };
75
+ category: any;
76
+ messageTimestamp: number;
77
+ pushName: any;
78
+ broadcast: any;
79
+ };
80
+ category: any;
81
+ author: any;
82
+ decrypt(): Promise<void>;
83
+ };
@@ -0,0 +1,316 @@
1
+ import { Boom } from '@hapi/boom';
2
+ import { proto } from '../../WAProto/index.js';
3
+ import { areJidsSameUser, isHostedLidUser, isHostedPnUser, isJidBroadcast, isJidGroup, isJidMetaAI, isJidNewsletter, isJidStatusBroadcast, isLidUser, isPnUser } from '../WABinary/index.js';
4
+ import { unpadRandomMax16 } from './generics.js';
5
+ export const getDecryptionJid = async (sender, repository) => {
6
+ if (isLidUser(sender) || isHostedLidUser(sender)) {
7
+ return sender;
8
+ }
9
+ const mapped = await repository.lidMapping.getLIDForPN(sender);
10
+ return mapped || sender;
11
+ };
12
+ const storeMappingFromEnvelope = async (stanza, sender, repository, decryptionJid, logger) => {
13
+ // TODO: Handle hosted IDs
14
+ const { senderAlt } = extractAddressingContext(stanza);
15
+ if (senderAlt && isLidUser(senderAlt) && isPnUser(sender) && decryptionJid === sender) {
16
+ try {
17
+ await repository.lidMapping.storeLIDPNMappings([{ lid: senderAlt, pn: sender }]);
18
+ await repository.migrateSession(sender, senderAlt);
19
+ logger.debug({ sender, senderAlt }, 'Stored LID mapping from envelope');
20
+ }
21
+ catch (error) {
22
+ logger.warn({ sender, senderAlt, error }, 'Failed to store LID mapping');
23
+ }
24
+ }
25
+ };
26
+ export const NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node';
27
+ export const MISSING_KEYS_ERROR_TEXT = 'Key used already or never filled';
28
+ export const ACCOUNT_RESTRICTED_TEXT = 'Your account has been restricted';
29
+ // Retry configuration for failed decryption
30
+ export const DECRYPTION_RETRY_CONFIG = {
31
+ maxRetries: 3,
32
+ baseDelayMs: 100,
33
+ sessionRecordErrors: ['No session record', 'SessionError: No session record']
34
+ };
35
+ /** NACK reason codes we send to the server (client → server) */
36
+ export const NACK_REASONS = {
37
+ SenderReachoutTimelocked: 463,
38
+ ParsingError: 487,
39
+ UnrecognizedStanza: 488,
40
+ UnrecognizedStanzaClass: 489,
41
+ UnrecognizedStanzaType: 490,
42
+ InvalidProtobuf: 491,
43
+ InvalidHostedCompanionStanza: 493,
44
+ MissingMessageSecret: 495,
45
+ SignalErrorOldCounter: 496,
46
+ MessageDeletedOnPeer: 499,
47
+ UnhandledError: 500,
48
+ UnsupportedAdminRevoke: 550,
49
+ UnsupportedLIDGroup: 551,
50
+ DBOperationFailed: 552
51
+ };
52
+ /**
53
+ * Server-side error codes returned in ack stanzas (server → client) that we
54
+ * currently have dedicated handlers for. Extend as more handlers are added.
55
+ * Distinct from the client-side NackReason enum (WAWebCreateNackFromStanza).
56
+ */
57
+ export const SERVER_ERROR_CODES = {
58
+ /**
59
+ * 1:1 message missing privacy token (tctoken). Usually means the account is
60
+ * restricted: WhatsApp blocks starting new chats but preserves existing ones,
61
+ * since established chats already carry a tctoken.
62
+ */
63
+ MessageAccountRestriction: '463',
64
+ /** Stanza validation failure (SMAX_INVALID) — likely stale device session */
65
+ SmaxInvalid: '479'
66
+ };
67
+ export const extractAddressingContext = (stanza) => {
68
+ let senderAlt;
69
+ let recipientAlt;
70
+ const sender = stanza.attrs.participant || stanza.attrs.from;
71
+ const addressingMode = stanza.attrs.addressing_mode || (sender?.endsWith('lid') ? 'lid' : 'pn');
72
+ if (addressingMode === 'lid') {
73
+ // Message is LID-addressed: sender is LID, extract corresponding PN
74
+ // without device data
75
+ senderAlt = stanza.attrs.participant_pn || stanza.attrs.sender_pn || stanza.attrs.peer_recipient_pn;
76
+ recipientAlt = stanza.attrs.recipient_pn;
77
+ // with device data
78
+ //if (sender && senderAlt) senderAlt = transferDevice(sender, senderAlt)
79
+ }
80
+ else {
81
+ // Message is PN-addressed: sender is PN, extract corresponding LID
82
+ // without device data
83
+ senderAlt = stanza.attrs.participant_lid || stanza.attrs.sender_lid || stanza.attrs.peer_recipient_lid;
84
+ recipientAlt = stanza.attrs.recipient_lid;
85
+ //with device data
86
+ //if (sender && senderAlt) senderAlt = transferDevice(sender, senderAlt)
87
+ }
88
+ return {
89
+ addressingMode,
90
+ senderAlt,
91
+ recipientAlt
92
+ };
93
+ };
94
+ /**
95
+ * Decode the received node as a message.
96
+ * @note this will only parse the message, not decrypt it
97
+ */
98
+ export function decodeMessageNode(stanza, meId, meLid) {
99
+ let msgType;
100
+ let chatId;
101
+ let author;
102
+ let fromMe = false;
103
+ const msgId = stanza.attrs.id;
104
+ const from = stanza.attrs.from;
105
+ const participant = stanza.attrs.participant;
106
+ const recipient = stanza.attrs.recipient;
107
+ if (!msgId) {
108
+ throw new Boom('Invalid message stanza: missing id attribute', { data: stanza });
109
+ }
110
+ if (!from) {
111
+ throw new Boom('Invalid message stanza: missing from attribute', { data: stanza });
112
+ }
113
+ const addressingContext = extractAddressingContext(stanza);
114
+ const isMe = (jid) => areJidsSameUser(jid, meId);
115
+ const isMeLid = (jid) => areJidsSameUser(jid, meLid);
116
+ if (isPnUser(from) || isLidUser(from) || isHostedLidUser(from) || isHostedPnUser(from)) {
117
+ if (recipient && !isJidMetaAI(recipient)) {
118
+ if (!isMe(from) && !isMeLid(from)) {
119
+ throw new Boom('receipient present, but msg not from me', { data: stanza });
120
+ }
121
+ if (isMe(from) || isMeLid(from)) {
122
+ fromMe = true;
123
+ }
124
+ chatId = recipient;
125
+ }
126
+ else {
127
+ // Peer-routed self stanzas (history sync, app-state sync, etc.) arrive
128
+ // with `from` set to our own device but no `recipient` attribute —
129
+ // still mark as fromMe so self-only protocolMessage handlers run.
130
+ if (isMe(from) || isMeLid(from)) {
131
+ fromMe = true;
132
+ }
133
+ chatId = from;
134
+ }
135
+ msgType = 'chat';
136
+ author = from;
137
+ }
138
+ else if (isJidGroup(from)) {
139
+ if (!participant) {
140
+ throw new Boom('No participant in group message');
141
+ }
142
+ if (isMe(participant) || isMeLid(participant)) {
143
+ fromMe = true;
144
+ }
145
+ msgType = 'group';
146
+ author = participant;
147
+ chatId = from;
148
+ }
149
+ else if (isJidBroadcast(from)) {
150
+ if (!participant) {
151
+ throw new Boom('No participant in group message');
152
+ }
153
+ const isParticipantMe = isMe(participant);
154
+ if (isJidStatusBroadcast(from)) {
155
+ msgType = isParticipantMe ? 'direct_peer_status' : 'other_status';
156
+ }
157
+ else {
158
+ msgType = isParticipantMe ? 'peer_broadcast' : 'other_broadcast';
159
+ }
160
+ fromMe = isParticipantMe;
161
+ chatId = from;
162
+ author = participant;
163
+ }
164
+ else if (isJidNewsletter(from)) {
165
+ msgType = 'newsletter';
166
+ chatId = from;
167
+ author = from;
168
+ if (isMe(from) || isMeLid(from)) {
169
+ fromMe = true;
170
+ }
171
+ }
172
+ else {
173
+ throw new Boom('Unknown message type', { data: stanza });
174
+ }
175
+ const pushname = stanza?.attrs?.notify;
176
+ const key = {
177
+ remoteJid: chatId,
178
+ remoteJidAlt: !isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
179
+ remoteJidUsername: !isJidGroup(chatId)
180
+ ? stanza.attrs.peer_recipient_username || stanza.attrs.recipient_username
181
+ : undefined,
182
+ fromMe,
183
+ id: msgId,
184
+ participant,
185
+ participantAlt: isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
186
+ participantUsername: stanza.attrs.participant ? stanza.attrs.participant_username : undefined,
187
+ addressingMode: addressingContext.addressingMode,
188
+ ...(msgType === 'newsletter' && stanza.attrs.server_id ? { server_id: stanza.attrs.server_id } : {})
189
+ };
190
+ const fullMessage = {
191
+ key,
192
+ category: stanza.attrs.category,
193
+ messageTimestamp: +stanza.attrs.t,
194
+ pushName: pushname,
195
+ broadcast: isJidBroadcast(from)
196
+ };
197
+ if (key.fromMe) {
198
+ fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK;
199
+ }
200
+ return {
201
+ fullMessage,
202
+ author,
203
+ sender: msgType === 'chat' ? author : chatId
204
+ };
205
+ }
206
+ export const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
207
+ const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid);
208
+ return {
209
+ fullMessage,
210
+ category: stanza.attrs.category,
211
+ author,
212
+ async decrypt() {
213
+ let decryptables = 0;
214
+ if (Array.isArray(stanza.content)) {
215
+ for (const { tag, attrs, content } of stanza.content) {
216
+ if (tag === 'verified_name' && content instanceof Uint8Array) {
217
+ const cert = proto.VerifiedNameCertificate.decode(content);
218
+ const details = proto.VerifiedNameCertificate.Details.decode(cert.details);
219
+ fullMessage.verifiedBizName = details.verifiedName;
220
+ }
221
+ if (tag === 'unavailable' && attrs.type === 'view_once') {
222
+ // JAP@Fix (bug 48): was injected into key (wrong place) — store as top-level flag on message instead
223
+ fullMessage.isViewOnce = true;
224
+ fullMessage.key.isViewOnce = true; // keep for backward compat with existing code that checks key.isViewOnce
225
+ }
226
+ if (attrs.count && tag === 'enc') {
227
+ fullMessage.retryCount = Number(attrs.count);
228
+ }
229
+ if (tag !== 'enc' && tag !== 'plaintext') {
230
+ continue;
231
+ }
232
+ if (!(content instanceof Uint8Array)) {
233
+ continue;
234
+ }
235
+ decryptables += 1;
236
+ let msgBuffer;
237
+ const decryptionJid = await getDecryptionJid(author, repository);
238
+ if (tag !== 'plaintext') {
239
+ // TODO: Handle hosted devices
240
+ await storeMappingFromEnvelope(stanza, author, repository, decryptionJid, logger);
241
+ }
242
+ try {
243
+ const e2eType = tag === 'plaintext' ? 'plaintext' : attrs.type;
244
+ switch (e2eType) {
245
+ case 'skmsg':
246
+ msgBuffer = await repository.decryptGroupMessage({
247
+ group: sender,
248
+ authorJid: author,
249
+ msg: content
250
+ });
251
+ break;
252
+ case 'pkmsg':
253
+ case 'msg':
254
+ msgBuffer = await repository.decryptMessage({
255
+ jid: decryptionJid,
256
+ type: e2eType,
257
+ ciphertext: content
258
+ });
259
+ break;
260
+ case 'plaintext':
261
+ msgBuffer = content;
262
+ break;
263
+ default:
264
+ throw new Error(`Unknown e2e type: ${e2eType}`);
265
+ }
266
+ let msg = proto.Message.decode(e2eType !== 'plaintext' ? unpadRandomMax16(msgBuffer) : msgBuffer);
267
+ msg = msg.deviceSentMessage?.message || msg;
268
+ if (msg.senderKeyDistributionMessage) {
269
+ //eslint-disable-next-line max-depth
270
+ try {
271
+ await repository.processSenderKeyDistributionMessage({
272
+ authorJid: author,
273
+ item: msg.senderKeyDistributionMessage
274
+ });
275
+ }
276
+ catch (err) {
277
+ logger.error({ key: fullMessage.key, err }, 'failed to process sender key distribution message');
278
+ }
279
+ }
280
+ if (fullMessage.message) {
281
+ Object.assign(fullMessage.message, msg);
282
+ }
283
+ else {
284
+ fullMessage.message = msg;
285
+ }
286
+ }
287
+ catch (err) {
288
+ const errorContext = {
289
+ key: fullMessage.key,
290
+ err,
291
+ messageType: tag === 'plaintext' ? 'plaintext' : attrs.type,
292
+ sender,
293
+ author,
294
+ isSessionRecordError: isSessionRecordError(err)
295
+ };
296
+ logger.error(errorContext, 'failed to decrypt message');
297
+ fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
298
+ fullMessage.messageStubParameters = [err.message.toString()];
299
+ }
300
+ }
301
+ }
302
+ // if nothing was found to decrypt
303
+ if (!decryptables && !fullMessage.key?.isViewOnce) {
304
+ fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
305
+ fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT];
306
+ }
307
+ }
308
+ };
309
+ };
310
+ /**
311
+ * Utility function to check if an error is related to missing session record
312
+ */
313
+ function isSessionRecordError(error) {
314
+ const errorMessage = error?.message || error?.toString() || '';
315
+ return DECRYPTION_RETRY_CONFIG.sessionRecordErrors.some(errorPattern => errorMessage.includes(errorPattern));
316
+ }
@@ -0,0 +1,12 @@
1
+ export function makeEventBuffer(logger: any, timeoutMs: any): {
2
+ process(handler: any): () => void;
3
+ emit(event: any, evData: any): any;
4
+ isBuffering(): boolean;
5
+ buffer: () => void;
6
+ flush: () => boolean;
7
+ createBufferedFunction(work: any): (...args: any[]) => Promise<any>;
8
+ on: (...args: any[]) => any;
9
+ off: (...args: any[]) => any;
10
+ removeAllListeners: (...args: any[]) => any;
11
+ destroy(): void;
12
+ };