@neelegirl/baileys 1.5.3 → 1.5.4

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 (438) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +195 -195
  3. package/WAProto/GenerateStatics.sh +3 -0
  4. package/WAProto/WAProto.proto +478 -1153
  5. package/WAProto/fix-imports.js +29 -0
  6. package/WAProto/index.d.ts +2645 -51971
  7. package/WAProto/index.js +48649 -154035
  8. package/engine-requirements.js +10 -0
  9. package/lib/Defaults/index.d.ts +62 -78
  10. package/lib/Defaults/index.d.ts.map +1 -0
  11. package/lib/Defaults/index.js +115 -149
  12. package/lib/Defaults/index.js.map +1 -0
  13. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  14. package/lib/Signal/Group/ciphertext-message.d.ts.map +1 -0
  15. package/lib/Signal/Group/ciphertext-message.js +12 -0
  16. package/lib/Signal/Group/ciphertext-message.js.map +1 -0
  17. package/lib/Signal/{WASignalGroup → Group}/group-session-builder.d.ts +11 -13
  18. package/lib/Signal/Group/group-session-builder.d.ts.map +1 -0
  19. package/lib/Signal/Group/group-session-builder.js +30 -0
  20. package/lib/Signal/Group/group-session-builder.js.map +1 -0
  21. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  22. package/lib/Signal/Group/group_cipher.d.ts.map +1 -0
  23. package/lib/Signal/{WASignalGroup → Group}/group_cipher.js +41 -70
  24. package/lib/Signal/Group/group_cipher.js.map +1 -0
  25. package/lib/Signal/Group/index.d.ts +12 -0
  26. package/lib/Signal/Group/index.d.ts.map +1 -0
  27. package/lib/Signal/Group/index.js +12 -0
  28. package/lib/Signal/Group/index.js.map +1 -0
  29. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  30. package/lib/Signal/Group/keyhelper.d.ts.map +1 -0
  31. package/lib/Signal/Group/keyhelper.js +18 -0
  32. package/lib/Signal/Group/keyhelper.js.map +1 -0
  33. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  34. package/lib/Signal/Group/sender-chain-key.d.ts.map +1 -0
  35. package/lib/Signal/Group/sender-chain-key.js +26 -0
  36. package/lib/Signal/Group/sender-chain-key.js.map +1 -0
  37. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  38. package/lib/Signal/Group/sender-key-distribution-message.d.ts.map +1 -0
  39. package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
  40. package/lib/Signal/Group/sender-key-distribution-message.js.map +1 -0
  41. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  42. package/lib/Signal/Group/sender-key-message.d.ts.map +1 -0
  43. package/lib/Signal/Group/sender-key-message.js +66 -0
  44. package/lib/Signal/Group/sender-key-message.js.map +1 -0
  45. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  46. package/lib/Signal/Group/sender-key-name.d.ts.map +1 -0
  47. package/lib/Signal/Group/sender-key-name.js +48 -0
  48. package/lib/Signal/Group/sender-key-name.js.map +1 -0
  49. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  50. package/lib/Signal/Group/sender-key-record.d.ts.map +1 -0
  51. package/lib/Signal/Group/sender-key-record.js +41 -0
  52. package/lib/Signal/Group/sender-key-record.js.map +1 -0
  53. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  54. package/lib/Signal/Group/sender-key-state.d.ts.map +1 -0
  55. package/lib/Signal/Group/sender-key-state.js +84 -0
  56. package/lib/Signal/Group/sender-key-state.js.map +1 -0
  57. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  58. package/lib/Signal/Group/sender-message-key.d.ts.map +1 -0
  59. package/lib/Signal/{WASignalGroup/sender_message_key.js → Group/sender-message-key.js} +26 -39
  60. package/lib/Signal/Group/sender-message-key.js.map +1 -0
  61. package/lib/Signal/libsignal.d.ts +5 -8
  62. package/lib/Signal/libsignal.d.ts.map +1 -0
  63. package/lib/Signal/libsignal.js +342 -391
  64. package/lib/Signal/libsignal.js.map +1 -0
  65. package/lib/Signal/lid-mapping.d.ts +23 -28
  66. package/lib/Signal/lid-mapping.d.ts.map +1 -0
  67. package/lib/Signal/lid-mapping.js +171 -184
  68. package/lib/Signal/lid-mapping.js.map +1 -0
  69. package/lib/Socket/Client/index.d.ts +3 -2
  70. package/lib/Socket/Client/index.d.ts.map +1 -0
  71. package/lib/Socket/Client/index.js +3 -22
  72. package/lib/Socket/Client/index.js.map +1 -0
  73. package/lib/Socket/Client/types.d.ts +15 -15
  74. package/lib/Socket/Client/types.d.ts.map +1 -0
  75. package/lib/Socket/Client/types.js +8 -15
  76. package/lib/Socket/Client/types.js.map +1 -0
  77. package/lib/Socket/Client/websocket.d.ts +12 -12
  78. package/lib/Socket/Client/websocket.d.ts.map +1 -0
  79. package/lib/Socket/Client/websocket.js +24 -36
  80. package/lib/Socket/Client/websocket.js.map +1 -0
  81. package/lib/Socket/business.d.ts +178 -177
  82. package/lib/Socket/business.d.ts.map +1 -0
  83. package/lib/Socket/business.js +81 -120
  84. package/lib/Socket/business.js.map +1 -0
  85. package/lib/Socket/chats.d.ts +93 -92
  86. package/lib/Socket/chats.d.ts.map +1 -0
  87. package/lib/Socket/chats.js +462 -618
  88. package/lib/Socket/chats.js.map +1 -0
  89. package/lib/Socket/communities.d.ts +244 -223
  90. package/lib/Socket/communities.d.ts.map +1 -0
  91. package/lib/Socket/communities.js +431 -433
  92. package/lib/Socket/communities.js.map +1 -0
  93. package/lib/Socket/groups.d.ts +118 -110
  94. package/lib/Socket/groups.d.ts.map +1 -0
  95. package/lib/Socket/groups.js +148 -181
  96. package/lib/Socket/groups.js.map +1 -0
  97. package/lib/Socket/index.d.ts +222 -182
  98. package/lib/Socket/index.d.ts.map +1 -0
  99. package/lib/Socket/index.js +12 -18
  100. package/lib/Socket/index.js.map +1 -0
  101. package/lib/Socket/messages-recv.d.ts +165 -166
  102. package/lib/Socket/messages-recv.d.ts.map +1 -0
  103. package/lib/Socket/messages-recv.js +753 -1031
  104. package/lib/Socket/messages-recv.js.map +1 -0
  105. package/lib/Socket/messages-send.d.ts +161 -157
  106. package/lib/Socket/messages-send.d.ts.map +1 -0
  107. package/lib/Socket/messages-send.js +564 -1396
  108. package/lib/Socket/messages-send.js.map +1 -0
  109. package/lib/Socket/mex.d.ts +3 -2
  110. package/lib/Socket/mex.d.ts.map +1 -0
  111. package/lib/Socket/mex.js +42 -47
  112. package/lib/Socket/mex.js.map +1 -0
  113. package/lib/Socket/newsletter.d.ts +139 -137
  114. package/lib/Socket/newsletter.d.ts.map +1 -0
  115. package/lib/Socket/newsletter.js +181 -295
  116. package/lib/Socket/newsletter.js.map +1 -0
  117. package/lib/Socket/socket.d.ts +42 -36
  118. package/lib/Socket/socket.d.ts.map +1 -0
  119. package/lib/Socket/socket.js +487 -557
  120. package/lib/Socket/socket.js.map +1 -0
  121. package/lib/Types/Auth.d.ts +87 -97
  122. package/lib/Types/Auth.d.ts.map +1 -0
  123. package/lib/Types/Auth.js +2 -3
  124. package/lib/Types/Auth.js.map +1 -0
  125. package/lib/Types/Bussines.d.ts +25 -0
  126. package/lib/Types/Bussines.d.ts.map +1 -0
  127. package/lib/Types/Bussines.js +2 -3
  128. package/lib/Types/Bussines.js.map +1 -0
  129. package/lib/Types/Call.d.ts +13 -13
  130. package/lib/Types/Call.d.ts.map +1 -0
  131. package/lib/Types/Call.js +2 -3
  132. package/lib/Types/Call.js.map +1 -0
  133. package/lib/Types/Chat.d.ts +77 -97
  134. package/lib/Types/Chat.d.ts.map +1 -0
  135. package/lib/Types/Chat.js +8 -9
  136. package/lib/Types/Chat.js.map +1 -0
  137. package/lib/Types/Contact.d.ts +10 -9
  138. package/lib/Types/Contact.d.ts.map +1 -0
  139. package/lib/Types/Contact.js +2 -3
  140. package/lib/Types/Contact.js.map +1 -0
  141. package/lib/Types/Events.d.ts +151 -175
  142. package/lib/Types/Events.d.ts.map +1 -0
  143. package/lib/Types/Events.js +2 -3
  144. package/lib/Types/Events.js.map +1 -0
  145. package/lib/Types/GroupMetadata.d.ts +49 -48
  146. package/lib/Types/GroupMetadata.d.ts.map +1 -0
  147. package/lib/Types/GroupMetadata.js +2 -3
  148. package/lib/Types/GroupMetadata.js.map +1 -0
  149. package/lib/Types/Label.d.ts +13 -14
  150. package/lib/Types/Label.d.ts.map +1 -0
  151. package/lib/Types/Label.js +24 -30
  152. package/lib/Types/Label.js.map +1 -0
  153. package/lib/Types/LabelAssociation.d.ts +15 -20
  154. package/lib/Types/LabelAssociation.d.ts.map +1 -0
  155. package/lib/Types/LabelAssociation.js +6 -12
  156. package/lib/Types/LabelAssociation.js.map +1 -0
  157. package/lib/Types/Message.d.ts +231 -412
  158. package/lib/Types/Message.d.ts.map +1 -0
  159. package/lib/Types/Message.js +11 -19
  160. package/lib/Types/Message.js.map +1 -0
  161. package/lib/Types/Newsletter.d.ts +130 -104
  162. package/lib/Types/Newsletter.d.ts.map +1 -0
  163. package/lib/Types/Newsletter.js +31 -40
  164. package/lib/Types/Newsletter.js.map +1 -0
  165. package/lib/Types/Product.d.ts +58 -71
  166. package/lib/Types/Product.d.ts.map +1 -0
  167. package/lib/Types/Product.js +2 -3
  168. package/lib/Types/Product.js.map +1 -0
  169. package/lib/Types/Signal.d.ts +60 -82
  170. package/lib/Types/Signal.d.ts.map +1 -0
  171. package/lib/Types/Signal.js +2 -3
  172. package/lib/Types/Signal.js.map +1 -0
  173. package/lib/Types/Socket.d.ts +73 -81
  174. package/lib/Types/Socket.d.ts.map +1 -0
  175. package/lib/Types/Socket.js +3 -3
  176. package/lib/Types/Socket.js.map +1 -0
  177. package/lib/Types/State.d.ts +17 -19
  178. package/lib/Types/State.d.ts.map +1 -0
  179. package/lib/Types/State.js +13 -14
  180. package/lib/Types/State.js.map +1 -0
  181. package/lib/Types/USync.d.ts +8 -8
  182. package/lib/Types/USync.d.ts.map +1 -0
  183. package/lib/Types/USync.js +2 -3
  184. package/lib/Types/USync.js.map +1 -0
  185. package/lib/Types/index.d.ts +47 -62
  186. package/lib/Types/index.d.ts.map +1 -0
  187. package/lib/Types/index.js +26 -50
  188. package/lib/Types/index.js.map +1 -0
  189. package/lib/Utils/auth-utils.d.ts +19 -21
  190. package/lib/Utils/auth-utils.d.ts.map +1 -0
  191. package/lib/Utils/auth-utils.js +257 -528
  192. package/lib/Utils/auth-utils.js.map +1 -0
  193. package/lib/Utils/baileys-event-stream.d.ts +17 -18
  194. package/lib/Utils/baileys-event-stream.d.ts.map +1 -0
  195. package/lib/Utils/baileys-event-stream.js +56 -70
  196. package/lib/Utils/baileys-event-stream.js.map +1 -0
  197. package/lib/Utils/browser-utils.d.ts +4 -0
  198. package/lib/Utils/browser-utils.d.ts.map +1 -0
  199. package/lib/Utils/browser-utils.js +28 -0
  200. package/lib/Utils/browser-utils.js.map +1 -0
  201. package/lib/Utils/business.d.ts +23 -29
  202. package/lib/Utils/business.d.ts.map +1 -0
  203. package/lib/Utils/business.js +231 -255
  204. package/lib/Utils/business.js.map +1 -0
  205. package/lib/Utils/chat-utils.d.ts +70 -82
  206. package/lib/Utils/chat-utils.d.ts.map +1 -0
  207. package/lib/Utils/chat-utils.js +763 -809
  208. package/lib/Utils/chat-utils.js.map +1 -0
  209. package/lib/Utils/crypto.d.ts +41 -56
  210. package/lib/Utils/crypto.d.ts.map +1 -0
  211. package/lib/Utils/crypto.js +142 -189
  212. package/lib/Utils/crypto.js.map +1 -0
  213. package/lib/Utils/decode-wa-message.d.ts +48 -53
  214. package/lib/Utils/decode-wa-message.d.ts.map +1 -0
  215. package/lib/Utils/decode-wa-message.js +279 -323
  216. package/lib/Utils/decode-wa-message.js.map +1 -0
  217. package/lib/Utils/event-buffer.d.ts +34 -39
  218. package/lib/Utils/event-buffer.d.ts.map +1 -0
  219. package/lib/Utils/event-buffer.js +548 -595
  220. package/lib/Utils/event-buffer.js.map +1 -0
  221. package/lib/Utils/generics.d.ts +90 -131
  222. package/lib/Utils/generics.d.ts.map +1 -0
  223. package/lib/Utils/generics.js +381 -630
  224. package/lib/Utils/generics.js.map +1 -0
  225. package/lib/Utils/history.d.ts +19 -23
  226. package/lib/Utils/history.d.ts.map +1 -0
  227. package/lib/Utils/history.js +84 -104
  228. package/lib/Utils/history.js.map +1 -0
  229. package/lib/Utils/index.d.ts +20 -20
  230. package/lib/Utils/index.d.ts.map +1 -0
  231. package/lib/Utils/index.js +19 -40
  232. package/lib/Utils/index.js.map +1 -0
  233. package/lib/Utils/link-preview.d.ts +21 -23
  234. package/lib/Utils/link-preview.d.ts.map +1 -0
  235. package/lib/Utils/link-preview.js +85 -120
  236. package/lib/Utils/link-preview.js.map +1 -0
  237. package/lib/Utils/logger.d.ts +12 -13
  238. package/lib/Utils/logger.d.ts.map +1 -0
  239. package/lib/Utils/logger.js +3 -7
  240. package/lib/Utils/logger.js.map +1 -0
  241. package/lib/Utils/lt-hash.d.ts +13 -14
  242. package/lib/Utils/lt-hash.d.ts.map +1 -0
  243. package/lib/Utils/lt-hash.js +48 -58
  244. package/lib/Utils/lt-hash.js.map +1 -0
  245. package/lib/Utils/make-mutex.d.ts +8 -9
  246. package/lib/Utils/make-mutex.d.ts.map +1 -0
  247. package/lib/Utils/make-mutex.js +40 -49
  248. package/lib/Utils/make-mutex.js.map +1 -0
  249. package/lib/Utils/message-retry-manager.d.ts +82 -88
  250. package/lib/Utils/message-retry-manager.d.ts.map +1 -0
  251. package/lib/Utils/message-retry-manager.js +149 -160
  252. package/lib/Utils/message-retry-manager.js.map +1 -0
  253. package/lib/Utils/messages-media.d.ts +114 -135
  254. package/lib/Utils/messages-media.d.ts.map +1 -0
  255. package/lib/Utils/messages-media.js +663 -869
  256. package/lib/Utils/messages-media.js.map +1 -0
  257. package/lib/Utils/messages.d.ts +76 -105
  258. package/lib/Utils/messages.d.ts.map +1 -0
  259. package/lib/Utils/messages.js +820 -1745
  260. package/lib/Utils/messages.js.map +1 -0
  261. package/lib/Utils/noise-handler.d.ts +20 -21
  262. package/lib/Utils/noise-handler.d.ts.map +1 -0
  263. package/lib/Utils/noise-handler.js +147 -165
  264. package/lib/Utils/noise-handler.js.map +1 -0
  265. package/lib/Utils/pre-key-manager.d.ts +28 -0
  266. package/lib/Utils/pre-key-manager.d.ts.map +1 -0
  267. package/lib/Utils/pre-key-manager.js +106 -0
  268. package/lib/Utils/pre-key-manager.js.map +1 -0
  269. package/lib/Utils/process-message.d.ts +42 -49
  270. package/lib/Utils/process-message.d.ts.map +1 -0
  271. package/lib/Utils/process-message.js +413 -427
  272. package/lib/Utils/process-message.js.map +1 -0
  273. package/lib/Utils/signal.d.ts +34 -42
  274. package/lib/Utils/signal.d.ts.map +1 -0
  275. package/lib/Utils/signal.js +159 -166
  276. package/lib/Utils/signal.js.map +1 -0
  277. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -18
  278. package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
  279. package/lib/Utils/use-multi-file-auth-state.js +121 -238
  280. package/lib/Utils/use-multi-file-auth-state.js.map +1 -0
  281. package/lib/Utils/validate-connection.d.ts +11 -13
  282. package/lib/Utils/validate-connection.d.ts.map +1 -0
  283. package/lib/Utils/validate-connection.js +195 -220
  284. package/lib/Utils/validate-connection.js.map +1 -0
  285. package/lib/WABinary/constants.d.ts +28 -30
  286. package/lib/WABinary/constants.d.ts.map +1 -0
  287. package/lib/WABinary/constants.js +1301 -1316
  288. package/lib/WABinary/constants.js.map +1 -0
  289. package/lib/WABinary/decode.d.ts +7 -9
  290. package/lib/WABinary/decode.d.ts.map +1 -0
  291. package/lib/WABinary/decode.js +238 -288
  292. package/lib/WABinary/decode.js.map +1 -0
  293. package/lib/WABinary/encode.d.ts +3 -3
  294. package/lib/WABinary/encode.d.ts.map +1 -0
  295. package/lib/WABinary/encode.js +216 -265
  296. package/lib/WABinary/encode.js.map +1 -0
  297. package/lib/WABinary/generic-utils.d.ts +15 -28
  298. package/lib/WABinary/generic-utils.d.ts.map +1 -0
  299. package/lib/WABinary/generic-utils.js +102 -142
  300. package/lib/WABinary/generic-utils.js.map +1 -0
  301. package/lib/WABinary/index.d.ts +6 -5
  302. package/lib/WABinary/index.d.ts.map +1 -0
  303. package/lib/WABinary/index.js +6 -25
  304. package/lib/WABinary/index.js.map +1 -0
  305. package/lib/WABinary/jid-utils.d.ts +48 -58
  306. package/lib/WABinary/jid-utils.d.ts.map +1 -0
  307. package/lib/WABinary/jid-utils.js +96 -104
  308. package/lib/WABinary/jid-utils.js.map +1 -0
  309. package/lib/WABinary/types.d.ts +19 -22
  310. package/lib/WABinary/types.d.ts.map +1 -0
  311. package/lib/WABinary/types.js +2 -3
  312. package/lib/WABinary/types.js.map +1 -0
  313. package/lib/WAM/BinaryInfo.d.ts +9 -16
  314. package/lib/WAM/BinaryInfo.d.ts.map +1 -0
  315. package/lib/WAM/BinaryInfo.js +10 -17
  316. package/lib/WAM/BinaryInfo.js.map +1 -0
  317. package/lib/WAM/constants.d.ts +40 -47
  318. package/lib/WAM/constants.d.ts.map +1 -0
  319. package/lib/WAM/constants.js +22853 -15371
  320. package/lib/WAM/constants.js.map +1 -0
  321. package/lib/WAM/encode.d.ts +3 -3
  322. package/lib/WAM/encode.d.ts.map +1 -0
  323. package/lib/WAM/encode.js +150 -164
  324. package/lib/WAM/encode.js.map +1 -0
  325. package/lib/WAM/index.d.ts +4 -3
  326. package/lib/WAM/index.d.ts.map +1 -0
  327. package/lib/WAM/index.js +4 -23
  328. package/lib/WAM/index.js.map +1 -0
  329. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -9
  330. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
  331. package/lib/WAUSync/Protocols/USyncContactProtocol.js +12 -19
  332. package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
  333. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +19 -22
  334. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
  335. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +18 -26
  336. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
  337. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +11 -12
  338. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
  339. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +12 -20
  340. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
  341. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +11 -12
  342. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
  343. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +16 -24
  344. package/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
  345. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  346. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
  347. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
  348. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
  349. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  350. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
  351. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
  352. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
  353. package/lib/WAUSync/Protocols/index.d.ts +5 -6
  354. package/lib/WAUSync/Protocols/index.d.ts.map +1 -0
  355. package/lib/WAUSync/Protocols/index.js +5 -26
  356. package/lib/WAUSync/Protocols/index.js.map +1 -0
  357. package/lib/WAUSync/USyncQuery.d.ts +26 -28
  358. package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
  359. package/lib/WAUSync/USyncQuery.js +64 -62
  360. package/lib/WAUSync/USyncQuery.js.map +1 -0
  361. package/lib/WAUSync/USyncUser.d.ts +11 -10
  362. package/lib/WAUSync/USyncUser.d.ts.map +1 -0
  363. package/lib/WAUSync/USyncUser.js +12 -19
  364. package/lib/WAUSync/USyncUser.js.map +1 -0
  365. package/lib/WAUSync/index.d.ts +4 -3
  366. package/lib/WAUSync/index.d.ts.map +1 -0
  367. package/lib/WAUSync/index.js +4 -23
  368. package/lib/WAUSync/index.js.map +1 -0
  369. package/lib/index.d.ts +12 -13
  370. package/lib/index.d.ts.map +1 -0
  371. package/lib/index.js +11 -33
  372. package/lib/index.js.map +1 -0
  373. package/package.json +99 -96
  374. package/lib/Defaults/baileys-version.json +0 -3
  375. package/lib/Defaults/phonenumber-mcc.json +0 -223
  376. package/lib/Signal/WASignalGroup/GroupProtocol.js +0 -1909
  377. package/lib/Signal/WASignalGroup/ciphertext-message.d.ts +0 -9
  378. package/lib/Signal/WASignalGroup/ciphertext-message.js +0 -19
  379. package/lib/Signal/WASignalGroup/ciphertext_message.js +0 -16
  380. package/lib/Signal/WASignalGroup/generate-proto.sh +0 -1
  381. package/lib/Signal/WASignalGroup/group-session-builder.js +0 -72
  382. package/lib/Signal/WASignalGroup/group.proto +0 -42
  383. package/lib/Signal/WASignalGroup/group_cipher.d.ts +0 -19
  384. package/lib/Signal/WASignalGroup/group_session_builder.js +0 -46
  385. package/lib/Signal/WASignalGroup/index.d.ts +0 -11
  386. package/lib/Signal/WASignalGroup/index.js +0 -61
  387. package/lib/Signal/WASignalGroup/keyhelper.d.ts +0 -16
  388. package/lib/Signal/WASignalGroup/keyhelper.js +0 -66
  389. package/lib/Signal/WASignalGroup/protobufs.js +0 -3
  390. package/lib/Signal/WASignalGroup/queue_job.js +0 -69
  391. package/lib/Signal/WASignalGroup/readme.md +0 -6
  392. package/lib/Signal/WASignalGroup/sender-chain-key.d.ts +0 -14
  393. package/lib/Signal/WASignalGroup/sender-chain-key.js +0 -47
  394. package/lib/Signal/WASignalGroup/sender-key-distribution-message.d.ts +0 -17
  395. package/lib/Signal/WASignalGroup/sender-key-distribution-message.js +0 -71
  396. package/lib/Signal/WASignalGroup/sender-key-message.d.ts +0 -19
  397. package/lib/Signal/WASignalGroup/sender-key-message.js +0 -73
  398. package/lib/Signal/WASignalGroup/sender-key-name.d.ts +0 -19
  399. package/lib/Signal/WASignalGroup/sender-key-name.js +0 -59
  400. package/lib/Signal/WASignalGroup/sender-key-record.d.ts +0 -32
  401. package/lib/Signal/WASignalGroup/sender-key-record.js +0 -58
  402. package/lib/Signal/WASignalGroup/sender-key-state.d.ts +0 -44
  403. package/lib/Signal/WASignalGroup/sender-key-state.js +0 -147
  404. package/lib/Signal/WASignalGroup/sender-message-key.d.ts +0 -11
  405. package/lib/Signal/WASignalGroup/sender-message-key.js +0 -33
  406. package/lib/Signal/WASignalGroup/sender_chain_key.js +0 -50
  407. package/lib/Signal/WASignalGroup/sender_key_distribution_message.js +0 -78
  408. package/lib/Signal/WASignalGroup/sender_key_message.js +0 -92
  409. package/lib/Signal/WASignalGroup/sender_key_name.js +0 -70
  410. package/lib/Signal/WASignalGroup/sender_key_record.js +0 -56
  411. package/lib/Signal/WASignalGroup/sender_key_state.js +0 -129
  412. package/lib/Socket/Client/abstract-socket-client.d.ts +0 -15
  413. package/lib/Socket/Client/abstract-socket-client.js +0 -13
  414. package/lib/Socket/Client/mobile-socket-client.d.ts +0 -12
  415. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  416. package/lib/Socket/usync.d.ts +0 -37
  417. package/lib/Socket/usync.js +0 -83
  418. package/lib/Store/index.d.ts +0 -4
  419. package/lib/Store/index.js +0 -24
  420. package/lib/Store/make-cache-manager-store.d.ts +0 -14
  421. package/lib/Store/make-cache-manager-store.js +0 -90
  422. package/lib/Store/make-in-memory-store.d.ts +0 -123
  423. package/lib/Store/make-in-memory-store.js +0 -429
  424. package/lib/Store/make-ordered-dictionary.d.ts +0 -12
  425. package/lib/Store/make-ordered-dictionary.js +0 -86
  426. package/lib/Store/object-repository.d.ts +0 -10
  427. package/lib/Store/object-repository.js +0 -31
  428. package/lib/Types/Bussiness.d.ts +0 -28
  429. package/lib/Types/MexUpdates.d.ts +0 -9
  430. package/lib/Types/MexUpdates.js +0 -18
  431. package/lib/Utils/use-mongo-file-auth-state.d.ts +0 -6
  432. package/lib/Utils/use-mongo-file-auth-state.js +0 -84
  433. package/lib/Utils/use-single-file-auth-state.d.ts +0 -13
  434. package/lib/Utils/use-single-file-auth-state.js +0 -80
  435. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.d.ts +0 -28
  436. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +0 -69
  437. package/lib/WAUSync/Protocols/USyncLIDProtocol.d.ts +0 -10
  438. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -38
@@ -1,809 +1,763 @@
1
- "use strict"
2
-
3
- Object.defineProperty(exports, "__esModule", { value: true })
4
-
5
- const boom_1 = require("@hapi/boom")
6
- const WAProto_1 = require("../../WAProto")
7
- const LabelAssociation_1 = require("../Types/LabelAssociation")
8
- const WABinary_1 = require("../WABinary")
9
- const crypto_1 = require("./crypto")
10
- const generics_1 = require("./generics")
11
- const lt_hash_1 = require("./lt-hash")
12
- const messages_media_1 = require("./messages-media")
13
-
14
- const mutationKeys = async (keydata) => {
15
- const expanded = await crypto_1.hkdf(keydata, 160, { info: 'WhatsApp Mutation Keys' })
16
- return {
17
- indexKey: expanded.slice(0, 32),
18
- valueEncryptionKey: expanded.slice(32, 64),
19
- valueMacKey: expanded.slice(64, 96),
20
- snapshotMacKey: expanded.slice(96, 128),
21
- patchMacKey: expanded.slice(128, 160)
22
- }
23
- }
24
-
25
- const generateMac = (operation, data, keyId, key) => {
26
- const getKeyData = () => {
27
- let r
28
- switch (operation) {
29
- case WAProto_1.proto.SyncdMutation.SyncdOperation.SET:
30
- r = 0x01
31
- break
32
- case WAProto_1.proto.SyncdMutation.SyncdOperation.REMOVE:
33
- r = 0x02
34
- break
35
- }
36
- const buff = Buffer.from([r])
37
- return Buffer.concat([buff, Buffer.from(keyId, 'base64')])
38
- }
39
- const keyData = getKeyData()
40
- const last = Buffer.alloc(8) // 8 bytes
41
- last.set([keyData.length], last.length - 1)
42
- const total = Buffer.concat([keyData, data, last])
43
- const hmac = crypto_1.hmacSign(total, key, 'sha512')
44
- return hmac.slice(0, 32)
45
- }
46
-
47
- const to64BitNetworkOrder = (e) => {
48
- const buff = Buffer.alloc(8)
49
- buff.writeUint32BE(e, 4)
50
- return buff
51
- }
52
-
53
- const makeLtHashGenerator = ({ indexValueMap, hash }) => {
54
- indexValueMap = { ...indexValueMap }
55
- const addBuffs = []
56
- const subBuffs = []
57
- return {
58
- mix: ({ indexMac, valueMac, operation }) => {
59
- const indexMacBase64 = Buffer.from(indexMac).toString('base64')
60
- const prevOp = indexValueMap[indexMacBase64]
61
- if (operation === WAProto_1.proto.SyncdMutation.SyncdOperation.REMOVE) {
62
- if (!prevOp) {
63
- throw new boom_1.Boom('tried remove, but no previous op', { data: { indexMac, valueMac } })
64
- }
65
- // remove from index value mac, since this mutation is erased
66
- delete indexValueMap[indexMacBase64]
67
- }
68
- else {
69
- addBuffs.push(new Uint8Array(valueMac).buffer)
70
- // add this index into the history map
71
- indexValueMap[indexMacBase64] = { valueMac }
72
- }
73
- if (prevOp) {
74
- subBuffs.push(new Uint8Array(prevOp.valueMac).buffer)
75
- }
76
- },
77
- finish: async () => {
78
- const hashArrayBuffer = new Uint8Array(hash).buffer
79
- const result = await lt_hash_1.LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs)
80
- const buffer = Buffer.from(result)
81
- return {
82
- hash: buffer,
83
- indexValueMap
84
- }
85
- }
86
- }
87
- }
88
-
89
- const generateSnapshotMac = (lthash, version, name, key) => {
90
- const total = Buffer.concat([
91
- lthash,
92
- to64BitNetworkOrder(version),
93
- Buffer.from(name, 'utf-8')
94
- ])
95
- return crypto_1.hmacSign(total, key, 'sha256')
96
- }
97
-
98
- const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
99
- const total = Buffer.concat([
100
- snapshotMac,
101
- ...valueMacs,
102
- to64BitNetworkOrder(version),
103
- Buffer.from(type, 'utf-8')
104
- ])
105
- return crypto_1.hmacSign(total, key)
106
- }
107
-
108
- const newLTHashState = () => ({ version: 0, hash: Buffer.alloc(128), indexValueMap: {} })
109
-
110
- const encodeSyncdPatch = async ({ type, index, syncAction, apiVersion, operation }, myAppStateKeyId, state, getAppStateSyncKey) => {
111
- const key = !!myAppStateKeyId ? await getAppStateSyncKey(myAppStateKeyId) : undefined
112
- if (!key) {
113
- throw new boom_1.Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 })
114
- }
115
- const encKeyId = Buffer.from(myAppStateKeyId, 'base64')
116
- state = { ...state, indexValueMap: { ...state.indexValueMap } }
117
- const indexBuffer = Buffer.from(JSON.stringify(index))
118
- const dataProto = WAProto_1.proto.SyncActionData.fromObject({
119
- index: indexBuffer,
120
- value: syncAction,
121
- padding: new Uint8Array(0),
122
- version: apiVersion
123
- })
124
- const encoded = WAProto_1.proto.SyncActionData.encode(dataProto).finish()
125
- const keyValue = await mutationKeys(key.keyData)
126
- const encValue = crypto_1.aesEncrypt(encoded, keyValue.valueEncryptionKey)
127
- const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey)
128
- const indexMac = crypto_1.hmacSign(indexBuffer, keyValue.indexKey)
129
- // update LT hash
130
- const generator = makeLtHashGenerator(state)
131
- generator.mix({ indexMac, valueMac, operation })
132
- Object.assign(state, await generator.finish())
133
- state.version += 1
134
- const snapshotMac = generateSnapshotMac(state.hash, state.version, type, keyValue.snapshotMacKey)
135
- const patch = {
136
- patchMac: generatePatchMac(snapshotMac, [valueMac], state.version, type, keyValue.patchMacKey),
137
- snapshotMac: snapshotMac,
138
- keyId: { id: encKeyId },
139
- mutations: [
140
- {
141
- operation: operation,
142
- record: {
143
- index: {
144
- blob: indexMac
145
- },
146
- value: {
147
- blob: Buffer.concat([encValue, valueMac])
148
- },
149
- keyId: { id: encKeyId }
150
- }
151
- }
152
- ]
153
- }
154
- const base64Index = indexMac.toString('base64')
155
- state.indexValueMap[base64Index] = { valueMac }
156
- return { patch, state }
157
- }
158
-
159
- const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
160
- const ltGenerator = makeLtHashGenerator(initialState)
161
- // indexKey used to HMAC sign record.index.blob
162
- // valueEncryptionKey used to AES-256-CBC encrypt record.value.blob[0:-32]
163
- // the remaining record.value.blob[0:-32] is the mac, it the HMAC sign of key.keyId + decoded proto data + length of bytes in keyId
164
- for (const msgMutation of msgMutations) {
165
- // if it's a syncdmutation, get the operation property
166
- // otherwise, if it's only a record -- it'll be a SET mutation
167
- const operation = 'operation' in msgMutation ? msgMutation.operation : WAProto_1.proto.SyncdMutation.SyncdOperation.SET
168
- const record = ('record' in msgMutation && !!msgMutation.record) ? msgMutation.record : msgMutation
169
- const key = await getKey(record.keyId.id)
170
- const content = Buffer.from(record.value.blob)
171
- const encContent = content.slice(0, -32)
172
- const ogValueMac = content.slice(-32)
173
- if (validateMacs) {
174
- const contentHmac = generateMac(operation, encContent, record.keyId.id, key.valueMacKey)
175
- if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
176
- throw new boom_1.Boom('HMAC content verification failed')
177
- }
178
- }
179
- const result = crypto_1.aesDecrypt(encContent, key.valueEncryptionKey)
180
- const syncAction = WAProto_1.proto.SyncActionData.decode(result)
181
- if (validateMacs) {
182
- const hmac = crypto_1.hmacSign(syncAction.index, key.indexKey)
183
- if (Buffer.compare(hmac, record.index.blob) !== 0) {
184
- throw new boom_1.Boom('HMAC index verification failed')
185
- }
186
- }
187
- const indexStr = Buffer.from(syncAction.index).toString()
188
- onMutation({ syncAction, index: JSON.parse(indexStr) })
189
- ltGenerator.mix({
190
- indexMac: record.index.blob,
191
- valueMac: ogValueMac,
192
- operation: operation
193
- })
194
- }
195
- return await ltGenerator.finish()
196
- async function getKey(keyId) {
197
- const base64Key = Buffer.from(keyId).toString('base64')
198
- const keyEnc = await getAppStateSyncKey(base64Key)
199
- if (!keyEnc) {
200
- throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`, { statusCode: 404, data: { msgMutations } })
201
- }
202
- return mutationKeys(keyEnc.keyData)
203
- }
204
- }
205
-
206
- const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
207
- if (validateMacs) {
208
- const base64Key = Buffer.from(msg.keyId.id).toString('base64')
209
- const mainKeyObj = await getAppStateSyncKey(base64Key)
210
- if (!mainKeyObj) {
211
- throw new boom_1.Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } })
212
- }
213
- const mainKey = await mutationKeys(mainKeyObj.keyData)
214
- const mutationmacs = msg.mutations.map(mutation => mutation.record.value.blob.slice(-32))
215
- const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs, generics_1.toNumber(msg.version.version), name, mainKey.patchMacKey)
216
- if (Buffer.compare(patchMac, msg.patchMac) !== 0) {
217
- throw new boom_1.Boom('Invalid patch mac')
218
- }
219
- }
220
- const result = await decodeSyncdMutations(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs)
221
- return result
222
- }
223
-
224
- const extractSyncdPatches = async (result, options) => {
225
- const syncNode = WABinary_1.getBinaryNodeChild(result, 'sync')
226
- const collectionNodes = WABinary_1.getBinaryNodeChildren(syncNode, 'collection')
227
- const final = {}
228
- await Promise.all(collectionNodes.map(async (collectionNode) => {
229
- const patchesNode = WABinary_1.getBinaryNodeChild(collectionNode, 'patches')
230
- const patches = WABinary_1.getBinaryNodeChildren(patchesNode || collectionNode, 'patch')
231
- const snapshotNode = WABinary_1.getBinaryNodeChild(collectionNode, 'snapshot')
232
- const syncds = []
233
- const name = collectionNode.attrs.name
234
- const hasMorePatches = collectionNode.attrs.has_more_patches === 'true'
235
- let snapshot = undefined
236
- if (snapshotNode && !!snapshotNode.content) {
237
- if (!Buffer.isBuffer(snapshotNode)) {
238
- snapshotNode.content = Buffer.from(Object.values(snapshotNode.content))
239
- }
240
- const blobRef = WAProto_1.proto.ExternalBlobReference.decode(snapshotNode.content)
241
- const data = await downloadExternalBlob(blobRef, options)
242
- snapshot = WAProto_1.proto.SyncdSnapshot.decode(data)
243
- }
244
- for (let { content } of patches) {
245
- if (content) {
246
- if (!Buffer.isBuffer(content)) {
247
- content = Buffer.from(Object.values(content))
248
- }
249
- const syncd = WAProto_1.proto.SyncdPatch.decode(content)
250
- if (!syncd.version) {
251
- syncd.version = { version: +collectionNode.attrs.version + 1 }
252
- }
253
- syncds.push(syncd)
254
- }
255
- }
256
- final[name] = { patches: syncds, hasMorePatches, snapshot }
257
- }))
258
- return final
259
- }
260
-
261
- const downloadExternalBlob = async (blob, options) => {
262
- const stream = await messages_media_1.downloadContentFromMessage(blob, 'md-app-state', { options })
263
- const bufferArray = []
264
- for await (const chunk of stream) {
265
- bufferArray.push(chunk)
266
- }
267
- return Buffer.concat(bufferArray)
268
- }
269
-
270
- const downloadExternalPatch = async (blob, options) => {
271
- const buffer = await downloadExternalBlob(blob, options)
272
- const syncData = WAProto_1.proto.SyncdMutations.decode(buffer)
273
- return syncData
274
- }
275
-
276
- const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, minimumVersionNumber, validateMacs = true) => {
277
- const newState = newLTHashState()
278
- newState.version = generics_1.toNumber(snapshot.version.version)
279
- const mutationMap = {}
280
- const areMutationsRequired = typeof minimumVersionNumber === 'undefined'
281
- || newState.version > minimumVersionNumber
282
- const { hash, indexValueMap } = await decodeSyncdMutations(snapshot.records, newState, getAppStateSyncKey, areMutationsRequired
283
- ? (mutation) => {
284
- const index = mutation.syncAction.index?.toString()
285
- mutationMap[index] = mutation
286
- }
287
- : () => { }, validateMacs)
288
- newState.hash = hash
289
- newState.indexValueMap = indexValueMap
290
- if (validateMacs) {
291
- const base64Key = Buffer.from(snapshot.keyId.id).toString('base64')
292
- const keyEnc = await getAppStateSyncKey(base64Key)
293
- if (!keyEnc) {
294
- throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`)
295
- }
296
- const result = await mutationKeys(keyEnc.keyData)
297
- const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey)
298
- if (Buffer.compare(snapshot.mac, computedSnapshotMac) !== 0) {
299
- throw new boom_1.Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`)
300
- }
301
- }
302
- return {
303
- state: newState,
304
- mutationMap
305
- }
306
- }
307
-
308
- const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options, minimumVersionNumber, logger, validateMacs = true) => {
309
- const newState = {
310
- ...initial,
311
- indexValueMap: { ...initial.indexValueMap }
312
- }
313
- const mutationMap = {}
314
- for (const syncd of syncds) {
315
- const { version, keyId, snapshotMac } = syncd
316
- if (syncd.externalMutations) {
317
- logger?.trace({ name, version }, 'downloading external patch')
318
- const ref = await downloadExternalPatch(syncd.externalMutations, options)
319
- logger?.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch')
320
- syncd.mutations?.push(...ref.mutations)
321
- }
322
- const patchVersion = generics_1.toNumber(version.version)
323
- newState.version = patchVersion
324
- const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber
325
- const decodeResult = await decodeSyncdPatch(syncd, name, newState, getAppStateSyncKey, shouldMutate
326
- ? mutation => {
327
- var _a
328
- const index = mutation.syncAction.index?.toString()
329
- mutationMap[index] = mutation
330
- }
331
- : (() => { }), true)
332
- newState.hash = decodeResult.hash
333
- newState.indexValueMap = decodeResult.indexValueMap
334
- if (validateMacs) {
335
- const base64Key = Buffer.from(keyId.id).toString('base64')
336
- const keyEnc = await getAppStateSyncKey(base64Key)
337
- if (!keyEnc) {
338
- throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`)
339
- }
340
- const result = await mutationKeys(keyEnc.keyData)
341
- const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey)
342
- if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
343
- throw new boom_1.Boom(`failed to verify LTHash at ${newState.version} of ${name}`)
344
- }
345
- }
346
- // clear memory used up by the mutations
347
- syncd.mutations = []
348
- }
349
- return { state: newState, mutationMap }
350
- }
351
-
352
- const chatModificationToAppPatch = (mod, jid) => {
353
- const OP = WAProto_1.proto.SyncdMutation.SyncdOperation
354
- const getMessageRange = (lastMessages) => {
355
- let messageRange
356
- if (Array.isArray(lastMessages)) {
357
- const lastMsg = lastMessages[lastMessages.length - 1]
358
- messageRange = {
359
- lastMessageTimestamp: lastMsg?.messageTimestamp,
360
- messages: lastMessages?.length ? lastMessages.map(m => {
361
- if (!((m.key?.id) || (m.key?.remoteJid))) {
362
- throw new boom_1.Boom('Incomplete key', { statusCode: 400, data: m })
363
- }
364
- if (WABinary_1.isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
365
- throw new boom_1.Boom('Expected not from me message to have participant', { statusCode: 400, data: m })
366
- }
367
- if (!m.messageTimestamp || !generics_1.toNumber(m.messageTimestamp)) {
368
- throw new boom_1.Boom('Missing timestamp in last message list', { statusCode: 400, data: m })
369
- }
370
- if (m.key.participant) {
371
- m.key.participant = WABinary_1.jidNormalizedUser(m.key.participant)
372
- }
373
- return m
374
- }) : undefined
375
- }
376
- }
377
- else {
378
- messageRange = lastMessages
379
- }
380
- return messageRange
381
- }
382
- let patch
383
- if ('mute' in mod) {
384
- patch = {
385
- syncAction: {
386
- muteAction: {
387
- muted: !!mod.mute,
388
- muteEndTimestamp: mod.mute || undefined
389
- }
390
- },
391
- index: ['mute', jid],
392
- type: 'regular_high',
393
- apiVersion: 2,
394
- operation: OP.SET
395
- }
396
- }
397
- else if ('archive' in mod) {
398
- patch = {
399
- syncAction: {
400
- archiveChatAction: {
401
- archived: !!mod.archive,
402
- messageRange: getMessageRange(mod.lastMessages)
403
- }
404
- },
405
- index: ['archive', jid],
406
- type: 'regular_low',
407
- apiVersion: 3,
408
- operation: OP.SET
409
- }
410
- }
411
- else if ('markRead' in mod) {
412
- patch = {
413
- syncAction: {
414
- markChatAsReadAction: {
415
- read: mod.markRead,
416
- messageRange: getMessageRange(mod.lastMessages)
417
- }
418
- },
419
- index: ['markChatAsRead', jid],
420
- type: 'regular_low',
421
- apiVersion: 3,
422
- operation: OP.SET
423
- }
424
- }
425
- else if ('deleteForMe' in mod) {
426
- const { timestamp, key, deleteMedia } = mod.deleteForMe
427
- patch = {
428
- syncAction: {
429
- deleteMessageForMeAction: {
430
- deleteMedia,
431
- messageTimestamp: timestamp
432
- }
433
- },
434
- index: ['deleteMessageForMe', jid, key.id, key.fromMe ? '1' : '0', '0'],
435
- type: 'regular_high',
436
- apiVersion: 3,
437
- operation: OP.SET
438
- }
439
- }
440
- else if ('clear' in mod) {
441
- patch = {
442
- syncAction: {
443
- clearChatAction: {
444
- messageRange: getMessageRange(mod.lastMessages)
445
- }
446
- },
447
- index: ['clearChat', jid, '1' /*the option here is 0 when keep starred messages is enabled*/, '0'],
448
- type: 'regular_high',
449
- apiVersion: 6,
450
- operation: OP.SET
451
- }
452
- }
453
- else if ('pin' in mod) {
454
- patch = {
455
- syncAction: {
456
- pinAction: {
457
- pinned: !!mod.pin
458
- }
459
- },
460
- index: ['pin_v1', jid],
461
- type: 'regular_low',
462
- apiVersion: 5,
463
- operation: OP.SET
464
- }
465
- }
466
- else if ('contact' in mod) {
467
- patch = {
468
- syncAction: {
469
- contactAction: mod?.contact || {}
470
- },
471
- index: ['contact', jid],
472
- type: 'critical_unblock_low',
473
- apiVersion: 2,
474
- operation: mod.contact ? OP.SET : OP.REMOVE
475
- }
476
- }
477
- else if ('disableLinkPreviews' in mod) {
478
- patch = {
479
- syncAction: {
480
- privacySettingDisableLinkPreviewsAction: mod.disableLinkPreviews || {}
481
- },
482
- index: ['setting_disableLinkPreviews'],
483
- type: 'regular',
484
- apiVersion: 8,
485
- operation: OP.SET
486
- }
487
- }
488
- else if ('star' in mod) {
489
- const key = mod.star.messages[0]
490
- patch = {
491
- syncAction: {
492
- starAction: {
493
- starred: !!mod.star.star
494
- }
495
- },
496
- index: ['star', jid, key.id, key.fromMe ? '1' : '0', '0'],
497
- type: 'regular_low',
498
- apiVersion: 2,
499
- operation: OP.SET
500
- }
501
- }
502
- else if ('delete' in mod) {
503
- patch = {
504
- syncAction: {
505
- deleteChatAction: {
506
- messageRange: getMessageRange(mod.lastMessages),
507
- }
508
- },
509
- index: ['deleteChat', jid, '1'],
510
- type: 'regular_high',
511
- apiVersion: 6,
512
- operation: OP.SET
513
- }
514
- }
515
- else if ('pushNameSetting' in mod) {
516
- patch = {
517
- syncAction: {
518
- pushNameSetting: {
519
- name: mod.pushNameSetting
520
- }
521
- },
522
- index: ['setting_pushName'],
523
- type: 'critical_block',
524
- apiVersion: 1,
525
- operation: OP.SET,
526
- }
527
- }
528
- else if ('quickReply' in mod) {
529
- patch = {
530
- syncAction: {
531
- quickReplyAction: {
532
- count: 0,
533
- deleted: mod.quickReply.deleted || false,
534
- keywords: [],
535
- message: mod.quickReply.message || '',
536
- shortcut: mod.quickReply.shortcut || ''
537
- }
538
- },
539
- index: ['quick_reply', mod.quickReply.timestamp || String(Math.floor(Date.now() / 1000))],
540
- type: 'regular',
541
- apiVersion: 2,
542
- operation: OP.SET
543
- }
544
- }
545
- else if ('addLabel' in mod) {
546
- patch = {
547
- syncAction: {
548
- labelEditAction: {
549
- name: mod.addLabel.name,
550
- color: mod.addLabel.color,
551
- predefinedId: mod.addLabel.predefinedId,
552
- deleted: mod.addLabel.deleted
553
- }
554
- },
555
- index: ['label_edit', mod.addLabel.id],
556
- type: 'regular',
557
- apiVersion: 3,
558
- operation: OP.SET,
559
- }
560
- }
561
- else if ('addChatLabel' in mod) {
562
- patch = {
563
- syncAction: {
564
- labelAssociationAction: {
565
- labeled: true,
566
- }
567
- },
568
- index: [LabelAssociation_1.LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
569
- type: 'regular',
570
- apiVersion: 3,
571
- operation: OP.SET,
572
- }
573
- }
574
- else if ('removeChatLabel' in mod) {
575
- patch = {
576
- syncAction: {
577
- labelAssociationAction: {
578
- labeled: false,
579
- }
580
- },
581
- index: [LabelAssociation_1.LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
582
- type: 'regular',
583
- apiVersion: 3,
584
- operation: OP.SET,
585
- }
586
- }
587
- else if ('addMessageLabel' in mod) {
588
- patch = {
589
- syncAction: {
590
- labelAssociationAction: {
591
- labeled: true,
592
- }
593
- },
594
- index: [
595
- LabelAssociation_1.LabelAssociationType.Message,
596
- mod.addMessageLabel.labelId,
597
- jid,
598
- mod.addMessageLabel.messageId,
599
- '0',
600
- '0'
601
- ],
602
- type: 'regular',
603
- apiVersion: 3,
604
- operation: OP.SET,
605
- }
606
- }
607
- else if ('removeMessageLabel' in mod) {
608
- patch = {
609
- syncAction: {
610
- labelAssociationAction: {
611
- labeled: false,
612
- }
613
- },
614
- index: [
615
- LabelAssociation_1.LabelAssociationType.Message,
616
- mod.removeMessageLabel.labelId,
617
- jid,
618
- mod.removeMessageLabel.messageId,
619
- '0',
620
- '0'
621
- ],
622
- type: 'regular',
623
- apiVersion: 3,
624
- operation: OP.SET,
625
- }
626
- }
627
- else {
628
- throw new boom_1.Boom('not supported')
629
- }
630
- patch.syncAction.timestamp = Date.now()
631
- return patch
632
- }
633
-
634
- const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
635
- const isInitialSync = !!initialSyncOpts
636
- const accountSettings = initialSyncOpts?.accountSettings
637
- logger?.trace({ syncAction, initialSync: !!initialSyncOpts }, 'processing sync action')
638
- const { syncAction: { value: action }, index: [type, id, msgId, fromMe] } = syncAction
639
- if (action?.muteAction) {
640
- ev.emit('chats.update', [
641
- {
642
- id,
643
- muteEndTime: action.muteAction?.muted
644
- ? generics_1.toNumber(action.muteAction.muteEndTimestamp)
645
- : null,
646
- conditional: getChatUpdateConditional(id, undefined)
647
- }
648
- ])
649
- }
650
- else if (action?.archiveChatAction || type === 'archive' || type === 'unarchive') {
651
- // okay so we've to do some annoying computation here
652
- // when we're initially syncing the app state
653
- // there are a few cases we need to handle
654
- // 1. if the account unarchiveChats setting is true
655
- // a. if the chat is archived, and no further messages have been received -- simple, keep archived
656
- // b. if the chat was archived, and the user received messages from the other person afterwards
657
- // then the chat should be marked unarchved --
658
- // we compare the timestamp of latest message from the other person to determine this
659
- // 2. if the account unarchiveChats setting is false -- then it doesn't matter,
660
- // it'll always take an app state action to mark in unarchived -- which we'll get anyway
661
- const archiveAction = action?.archiveChatAction
662
- const isArchived = archiveAction
663
- ? archiveAction.archived
664
- : type === 'archive'
665
- // // basically we don't need to fire an "archive" update if the chat is being marked unarchvied
666
- // // this only applies for the initial sync
667
- // if(isInitialSync && !isArchived) {
668
- // isArchived = false
669
- // }
670
- const msgRange = !accountSettings?.unarchiveChats ? undefined : archiveAction?.messageRange
671
- // logger?.debug({ chat: id, syncAction }, 'message range archive')
672
- ev.emit('chats.update', [{
673
- id,
674
- archived: isArchived,
675
- conditional: getChatUpdateConditional(id, msgRange)
676
- }])
677
- }
678
- else if (action?.markChatAsReadAction) {
679
- const markReadAction = action.markChatAsReadAction
680
- // basically we don't need to fire an "read" update if the chat is being marked as read
681
- // because the chat is read by default
682
- // this only applies for the initial sync
683
- const isNullUpdate = isInitialSync && markReadAction.read
684
- ev.emit('chats.update', [{
685
- id,
686
- unreadCount: isNullUpdate ? null : !!markReadAction?.read ? 0 : -1,
687
- conditional: getChatUpdateConditional(id, markReadAction?.messageRange)
688
- }])
689
- }
690
- else if (action?.deleteMessageForMeAction || type === 'deleteMessageForMe') {
691
- ev.emit('messages.delete', {
692
- keys: [
693
- {
694
- remoteJid: id,
695
- id: msgId,
696
- fromMe: fromMe === '1'
697
- }
698
- ]
699
- })
700
- }
701
- else if (action?.contactAction) {
702
- ev.emit('contacts.upsert', [{
703
- id,
704
- name: action.contactAction.fullName,
705
- lid: action.contactAction.lidJid || undefined,
706
- phoneNumber: action.contactAction.pnJid || undefined
707
- }])
708
- }
709
- else if (action?.pushNameSetting) {
710
- const name = action?.pushNameSetting?.name
711
- if (name && me?.name !== name) {
712
- ev.emit('creds.update', { me: { ...me, name } })
713
- }
714
- }
715
- else if (action?.pinAction) {
716
- ev.emit('chats.update', [{
717
- id,
718
- pinned: action.pinAction?.pinned ? generics_1.toNumber(action.timestamp) : null,
719
- conditional: getChatUpdateConditional(id, undefined)
720
- }])
721
- }
722
- else if (action?.unarchiveChatsSetting) {
723
- const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats
724
- ev.emit('creds.update', { accountSettings: { unarchiveChats } })
725
- logger?.info(`archive setting updated => '${action.unarchiveChatsSetting.unarchiveChats}'`)
726
- if (accountSettings) {
727
- accountSettings.unarchiveChats = unarchiveChats
728
- }
729
- }
730
- else if (action?.starAction || type === 'star') {
731
- let starred = action?.starAction?.starred
732
- if (typeof starred !== 'boolean') {
733
- starred = syncAction.index[syncAction.index.length - 1] === '1'
734
- }
735
- ev.emit('messages.update', [
736
- {
737
- key: { remoteJid: id, id: msgId, fromMe: fromMe === '1' },
738
- update: { starred }
739
- }
740
- ])
741
- }
742
- else if (action?.deleteChatAction || type === 'deleteChat') {
743
- if (!isInitialSync) {
744
- ev.emit('chats.delete', [id])
745
- }
746
- }
747
- else if (action?.labelEditAction) {
748
- const { name, color, deleted, predefinedId } = action.labelEditAction
749
- ev.emit('labels.edit', {
750
- id,
751
- name: name,
752
- color: color,
753
- deleted: deleted,
754
- predefinedId: predefinedId ? String(predefinedId) : undefined
755
- })
756
- }
757
- else if (action?.labelAssociationAction) {
758
- ev.emit('labels.association', {
759
- type: action.labelAssociationAction.labeled
760
- ? 'add'
761
- : 'remove',
762
- association: type === LabelAssociation_1.LabelAssociationType.Chat
763
- ? {
764
- type: LabelAssociation_1.LabelAssociationType.Chat,
765
- chatId: syncAction.index[2],
766
- labelId: syncAction.index[1]
767
- }
768
- : {
769
- type: LabelAssociation_1.LabelAssociationType.Message,
770
- chatId: syncAction.index[2],
771
- messageId: syncAction.index[3],
772
- labelId: syncAction.index[1]
773
- }
774
- })
775
- }
776
- else {
777
- logger?.debug({ syncAction, id }, 'unprocessable update')
778
- }
779
- function getChatUpdateConditional(id, msgRange) {
780
- return isInitialSync
781
- ? (data) => {
782
- const chat = data.historySets.chats[id] || data.chatUpserts[id]
783
- if (chat) {
784
- return msgRange ? isValidPatchBasedOnMessageRange(chat, msgRange) : true
785
- }
786
- }
787
- : undefined
788
- }
789
- function isValidPatchBasedOnMessageRange(chat, msgRange) {
790
- const lastMsgTimestamp = Number(msgRange?.lastMessageTimestamp || msgRange?.lastSystemMessageTimestamp || 0)
791
- const chatLastMsgTimestamp = Number(chat?.lastMessageRecvTimestamp || 0)
792
- return lastMsgTimestamp >= chatLastMsgTimestamp
793
- }
794
- }
795
-
796
- module.exports = {
797
- mutationKeys,
798
- newLTHashState,
799
- encodeSyncdPatch,
800
- decodeSyncdMutations,
801
- decodeSyncdPatch,
802
- extractSyncdPatches,
803
- downloadExternalBlob,
804
- downloadExternalPatch,
805
- decodeSyncdSnapshot,
806
- decodePatches,
807
- chatModificationToAppPatch,
808
- processSyncAction
809
- }
1
+ import { Boom } from '@hapi/boom';
2
+ import { proto } from '../../WAProto/index.js';
3
+ import { LabelAssociationType } from '../Types/LabelAssociation.js';
4
+ import { getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidNormalizedUser } from '../WABinary/index.js';
5
+ import { aesDecrypt, aesEncrypt, hkdf, hmacSign } from './crypto.js';
6
+ import { toNumber } from './generics.js';
7
+ import { LT_HASH_ANTI_TAMPERING } from './lt-hash.js';
8
+ import { downloadContentFromMessage } from './messages-media.js';
9
+ const mutationKeys = async (keydata) => {
10
+ const expanded = await hkdf(keydata, 160, { info: 'WhatsApp Mutation Keys' });
11
+ return {
12
+ indexKey: expanded.slice(0, 32),
13
+ valueEncryptionKey: expanded.slice(32, 64),
14
+ valueMacKey: expanded.slice(64, 96),
15
+ snapshotMacKey: expanded.slice(96, 128),
16
+ patchMacKey: expanded.slice(128, 160)
17
+ };
18
+ };
19
+ const generateMac = (operation, data, keyId, key) => {
20
+ const getKeyData = () => {
21
+ let r;
22
+ switch (operation) {
23
+ case proto.SyncdMutation.SyncdOperation.SET:
24
+ r = 0x01;
25
+ break;
26
+ case proto.SyncdMutation.SyncdOperation.REMOVE:
27
+ r = 0x02;
28
+ break;
29
+ }
30
+ const buff = Buffer.from([r]);
31
+ return Buffer.concat([buff, Buffer.from(keyId, 'base64')]);
32
+ };
33
+ const keyData = getKeyData();
34
+ const last = Buffer.alloc(8); // 8 bytes
35
+ last.set([keyData.length], last.length - 1);
36
+ const total = Buffer.concat([keyData, data, last]);
37
+ const hmac = hmacSign(total, key, 'sha512');
38
+ return hmac.slice(0, 32);
39
+ };
40
+ const to64BitNetworkOrder = (e) => {
41
+ const buff = Buffer.alloc(8);
42
+ buff.writeUint32BE(e, 4);
43
+ return buff;
44
+ };
45
+ const makeLtHashGenerator = ({ indexValueMap, hash }) => {
46
+ indexValueMap = { ...indexValueMap };
47
+ const addBuffs = [];
48
+ const subBuffs = [];
49
+ return {
50
+ mix: ({ indexMac, valueMac, operation }) => {
51
+ const indexMacBase64 = Buffer.from(indexMac).toString('base64');
52
+ const prevOp = indexValueMap[indexMacBase64];
53
+ if (operation === proto.SyncdMutation.SyncdOperation.REMOVE) {
54
+ if (!prevOp) {
55
+ throw new Boom('tried remove, but no previous op', { data: { indexMac, valueMac } });
56
+ }
57
+ // remove from index value mac, since this mutation is erased
58
+ delete indexValueMap[indexMacBase64];
59
+ }
60
+ else {
61
+ addBuffs.push(new Uint8Array(valueMac).buffer);
62
+ // add this index into the history map
63
+ indexValueMap[indexMacBase64] = { valueMac };
64
+ }
65
+ if (prevOp) {
66
+ subBuffs.push(new Uint8Array(prevOp.valueMac).buffer);
67
+ }
68
+ },
69
+ finish: async () => {
70
+ const hashArrayBuffer = new Uint8Array(hash).buffer;
71
+ const result = await LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs);
72
+ const buffer = Buffer.from(result);
73
+ return {
74
+ hash: buffer,
75
+ indexValueMap
76
+ };
77
+ }
78
+ };
79
+ };
80
+ const generateSnapshotMac = (lthash, version, name, key) => {
81
+ const total = Buffer.concat([lthash, to64BitNetworkOrder(version), Buffer.from(name, 'utf-8')]);
82
+ return hmacSign(total, key, 'sha256');
83
+ };
84
+ const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
85
+ const total = Buffer.concat([snapshotMac, ...valueMacs, to64BitNetworkOrder(version), Buffer.from(type, 'utf-8')]);
86
+ return hmacSign(total, key);
87
+ };
88
+ export const newLTHashState = () => ({ version: 0, hash: Buffer.alloc(128), indexValueMap: {} });
89
+ export const encodeSyncdPatch = async ({ type, index, syncAction, apiVersion, operation }, myAppStateKeyId, state, getAppStateSyncKey) => {
90
+ const key = !!myAppStateKeyId ? await getAppStateSyncKey(myAppStateKeyId) : undefined;
91
+ if (!key) {
92
+ throw new Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 });
93
+ }
94
+ const encKeyId = Buffer.from(myAppStateKeyId, 'base64');
95
+ state = { ...state, indexValueMap: { ...state.indexValueMap } };
96
+ const indexBuffer = Buffer.from(JSON.stringify(index));
97
+ const dataProto = proto.SyncActionData.fromObject({
98
+ index: indexBuffer,
99
+ value: syncAction,
100
+ padding: new Uint8Array(0),
101
+ version: apiVersion
102
+ });
103
+ const encoded = proto.SyncActionData.encode(dataProto).finish();
104
+ const keyValue = await mutationKeys(key.keyData);
105
+ const encValue = aesEncrypt(encoded, keyValue.valueEncryptionKey);
106
+ const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey);
107
+ const indexMac = hmacSign(indexBuffer, keyValue.indexKey);
108
+ // update LT hash
109
+ const generator = makeLtHashGenerator(state);
110
+ generator.mix({ indexMac, valueMac, operation });
111
+ Object.assign(state, await generator.finish());
112
+ state.version += 1;
113
+ const snapshotMac = generateSnapshotMac(state.hash, state.version, type, keyValue.snapshotMacKey);
114
+ const patch = {
115
+ patchMac: generatePatchMac(snapshotMac, [valueMac], state.version, type, keyValue.patchMacKey),
116
+ snapshotMac: snapshotMac,
117
+ keyId: { id: encKeyId },
118
+ mutations: [
119
+ {
120
+ operation: operation,
121
+ record: {
122
+ index: {
123
+ blob: indexMac
124
+ },
125
+ value: {
126
+ blob: Buffer.concat([encValue, valueMac])
127
+ },
128
+ keyId: { id: encKeyId }
129
+ }
130
+ }
131
+ ]
132
+ };
133
+ const base64Index = indexMac.toString('base64');
134
+ state.indexValueMap[base64Index] = { valueMac };
135
+ return { patch, state };
136
+ };
137
+ export const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
138
+ const ltGenerator = makeLtHashGenerator(initialState);
139
+ // indexKey used to HMAC sign record.index.blob
140
+ // valueEncryptionKey used to AES-256-CBC encrypt record.value.blob[0:-32]
141
+ // the remaining record.value.blob[0:-32] is the mac, it the HMAC sign of key.keyId + decoded proto data + length of bytes in keyId
142
+ for (const msgMutation of msgMutations) {
143
+ // if it's a syncdmutation, get the operation property
144
+ // otherwise, if it's only a record -- it'll be a SET mutation
145
+ const operation = 'operation' in msgMutation ? msgMutation.operation : proto.SyncdMutation.SyncdOperation.SET;
146
+ const record = 'record' in msgMutation && !!msgMutation.record ? msgMutation.record : msgMutation;
147
+ const key = await getKey(record.keyId.id);
148
+ const content = Buffer.from(record.value.blob);
149
+ const encContent = content.slice(0, -32);
150
+ const ogValueMac = content.slice(-32);
151
+ if (validateMacs) {
152
+ const contentHmac = generateMac(operation, encContent, record.keyId.id, key.valueMacKey);
153
+ if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
154
+ throw new Boom('HMAC content verification failed');
155
+ }
156
+ }
157
+ const result = aesDecrypt(encContent, key.valueEncryptionKey);
158
+ const syncAction = proto.SyncActionData.decode(result);
159
+ if (validateMacs) {
160
+ const hmac = hmacSign(syncAction.index, key.indexKey);
161
+ if (Buffer.compare(hmac, record.index.blob) !== 0) {
162
+ throw new Boom('HMAC index verification failed');
163
+ }
164
+ }
165
+ const indexStr = Buffer.from(syncAction.index).toString();
166
+ onMutation({ syncAction, index: JSON.parse(indexStr) });
167
+ ltGenerator.mix({
168
+ indexMac: record.index.blob,
169
+ valueMac: ogValueMac,
170
+ operation: operation
171
+ });
172
+ }
173
+ return await ltGenerator.finish();
174
+ async function getKey(keyId) {
175
+ const base64Key = Buffer.from(keyId).toString('base64');
176
+ const keyEnc = await getAppStateSyncKey(base64Key);
177
+ if (!keyEnc) {
178
+ throw new Boom(`failed to find key "${base64Key}" to decode mutation`, {
179
+ statusCode: 404,
180
+ data: { msgMutations }
181
+ });
182
+ }
183
+ return mutationKeys(keyEnc.keyData);
184
+ }
185
+ };
186
+ export const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
187
+ if (validateMacs) {
188
+ const base64Key = Buffer.from(msg.keyId.id).toString('base64');
189
+ const mainKeyObj = await getAppStateSyncKey(base64Key);
190
+ if (!mainKeyObj) {
191
+ throw new Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } });
192
+ }
193
+ const mainKey = await mutationKeys(mainKeyObj.keyData);
194
+ const mutationmacs = msg.mutations.map(mutation => mutation.record.value.blob.slice(-32));
195
+ const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs, toNumber(msg.version.version), name, mainKey.patchMacKey);
196
+ if (Buffer.compare(patchMac, msg.patchMac) !== 0) {
197
+ throw new Boom('Invalid patch mac');
198
+ }
199
+ }
200
+ const result = await decodeSyncdMutations(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs);
201
+ return result;
202
+ };
203
+ export const extractSyncdPatches = async (result, options) => {
204
+ const syncNode = getBinaryNodeChild(result, 'sync');
205
+ const collectionNodes = getBinaryNodeChildren(syncNode, 'collection');
206
+ const final = {};
207
+ await Promise.all(collectionNodes.map(async (collectionNode) => {
208
+ const patchesNode = getBinaryNodeChild(collectionNode, 'patches');
209
+ const patches = getBinaryNodeChildren(patchesNode || collectionNode, 'patch');
210
+ const snapshotNode = getBinaryNodeChild(collectionNode, 'snapshot');
211
+ const syncds = [];
212
+ const name = collectionNode.attrs.name;
213
+ const hasMorePatches = collectionNode.attrs.has_more_patches === 'true';
214
+ let snapshot = undefined;
215
+ if (snapshotNode && !!snapshotNode.content) {
216
+ if (!Buffer.isBuffer(snapshotNode)) {
217
+ snapshotNode.content = Buffer.from(Object.values(snapshotNode.content));
218
+ }
219
+ const blobRef = proto.ExternalBlobReference.decode(snapshotNode.content);
220
+ const data = await downloadExternalBlob(blobRef, options);
221
+ snapshot = proto.SyncdSnapshot.decode(data);
222
+ }
223
+ for (let { content } of patches) {
224
+ if (content) {
225
+ if (!Buffer.isBuffer(content)) {
226
+ content = Buffer.from(Object.values(content));
227
+ }
228
+ const syncd = proto.SyncdPatch.decode(content);
229
+ if (!syncd.version) {
230
+ syncd.version = { version: +collectionNode.attrs.version + 1 };
231
+ }
232
+ syncds.push(syncd);
233
+ }
234
+ }
235
+ final[name] = { patches: syncds, hasMorePatches, snapshot };
236
+ }));
237
+ return final;
238
+ };
239
+ export const downloadExternalBlob = async (blob, options) => {
240
+ const stream = await downloadContentFromMessage(blob, 'md-app-state', { options });
241
+ const bufferArray = [];
242
+ for await (const chunk of stream) {
243
+ bufferArray.push(chunk);
244
+ }
245
+ return Buffer.concat(bufferArray);
246
+ };
247
+ export const downloadExternalPatch = async (blob, options) => {
248
+ const buffer = await downloadExternalBlob(blob, options);
249
+ const syncData = proto.SyncdMutations.decode(buffer);
250
+ return syncData;
251
+ };
252
+ export const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, minimumVersionNumber, validateMacs = true) => {
253
+ const newState = newLTHashState();
254
+ newState.version = toNumber(snapshot.version.version);
255
+ const mutationMap = {};
256
+ const areMutationsRequired = typeof minimumVersionNumber === 'undefined' || newState.version > minimumVersionNumber;
257
+ const { hash, indexValueMap } = await decodeSyncdMutations(snapshot.records, newState, getAppStateSyncKey, areMutationsRequired
258
+ ? mutation => {
259
+ const index = mutation.syncAction.index?.toString();
260
+ mutationMap[index] = mutation;
261
+ }
262
+ : () => { }, validateMacs);
263
+ newState.hash = hash;
264
+ newState.indexValueMap = indexValueMap;
265
+ if (validateMacs) {
266
+ const base64Key = Buffer.from(snapshot.keyId.id).toString('base64');
267
+ const keyEnc = await getAppStateSyncKey(base64Key);
268
+ if (!keyEnc) {
269
+ throw new Boom(`failed to find key "${base64Key}" to decode mutation`);
270
+ }
271
+ const result = await mutationKeys(keyEnc.keyData);
272
+ const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
273
+ if (Buffer.compare(snapshot.mac, computedSnapshotMac) !== 0) {
274
+ throw new Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`);
275
+ }
276
+ }
277
+ return {
278
+ state: newState,
279
+ mutationMap
280
+ };
281
+ };
282
+ export const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options, minimumVersionNumber, logger, validateMacs = true) => {
283
+ const newState = {
284
+ ...initial,
285
+ indexValueMap: { ...initial.indexValueMap }
286
+ };
287
+ const mutationMap = {};
288
+ for (const syncd of syncds) {
289
+ const { version, keyId, snapshotMac } = syncd;
290
+ if (syncd.externalMutations) {
291
+ logger?.trace({ name, version }, 'downloading external patch');
292
+ const ref = await downloadExternalPatch(syncd.externalMutations, options);
293
+ logger?.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch');
294
+ syncd.mutations?.push(...ref.mutations);
295
+ }
296
+ const patchVersion = toNumber(version.version);
297
+ newState.version = patchVersion;
298
+ const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber;
299
+ const decodeResult = await decodeSyncdPatch(syncd, name, newState, getAppStateSyncKey, shouldMutate
300
+ ? mutation => {
301
+ const index = mutation.syncAction.index?.toString();
302
+ mutationMap[index] = mutation;
303
+ }
304
+ : () => { }, true);
305
+ newState.hash = decodeResult.hash;
306
+ newState.indexValueMap = decodeResult.indexValueMap;
307
+ if (validateMacs) {
308
+ const base64Key = Buffer.from(keyId.id).toString('base64');
309
+ const keyEnc = await getAppStateSyncKey(base64Key);
310
+ if (!keyEnc) {
311
+ throw new Boom(`failed to find key "${base64Key}" to decode mutation`);
312
+ }
313
+ const result = await mutationKeys(keyEnc.keyData);
314
+ const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
315
+ if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
316
+ throw new Boom(`failed to verify LTHash at ${newState.version} of ${name}`);
317
+ }
318
+ }
319
+ // clear memory used up by the mutations
320
+ syncd.mutations = [];
321
+ }
322
+ return { state: newState, mutationMap };
323
+ };
324
+ export const chatModificationToAppPatch = (mod, jid) => {
325
+ const OP = proto.SyncdMutation.SyncdOperation;
326
+ const getMessageRange = (lastMessages) => {
327
+ let messageRange;
328
+ if (Array.isArray(lastMessages)) {
329
+ const lastMsg = lastMessages[lastMessages.length - 1];
330
+ messageRange = {
331
+ lastMessageTimestamp: lastMsg?.messageTimestamp,
332
+ messages: lastMessages?.length
333
+ ? lastMessages.map(m => {
334
+ if (!m.key?.id || !m.key?.remoteJid) {
335
+ throw new Boom('Incomplete key', { statusCode: 400, data: m });
336
+ }
337
+ if (isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
338
+ throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m });
339
+ }
340
+ if (!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
341
+ throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m });
342
+ }
343
+ if (m.key.participant) {
344
+ m.key.participant = jidNormalizedUser(m.key.participant);
345
+ }
346
+ return m;
347
+ })
348
+ : undefined
349
+ };
350
+ }
351
+ else {
352
+ messageRange = lastMessages;
353
+ }
354
+ return messageRange;
355
+ };
356
+ let patch;
357
+ if ('mute' in mod) {
358
+ patch = {
359
+ syncAction: {
360
+ muteAction: {
361
+ muted: !!mod.mute,
362
+ muteEndTimestamp: mod.mute || undefined
363
+ }
364
+ },
365
+ index: ['mute', jid],
366
+ type: 'regular_high',
367
+ apiVersion: 2,
368
+ operation: OP.SET
369
+ };
370
+ }
371
+ else if ('archive' in mod) {
372
+ patch = {
373
+ syncAction: {
374
+ archiveChatAction: {
375
+ archived: !!mod.archive,
376
+ messageRange: getMessageRange(mod.lastMessages)
377
+ }
378
+ },
379
+ index: ['archive', jid],
380
+ type: 'regular_low',
381
+ apiVersion: 3,
382
+ operation: OP.SET
383
+ };
384
+ }
385
+ else if ('markRead' in mod) {
386
+ patch = {
387
+ syncAction: {
388
+ markChatAsReadAction: {
389
+ read: mod.markRead,
390
+ messageRange: getMessageRange(mod.lastMessages)
391
+ }
392
+ },
393
+ index: ['markChatAsRead', jid],
394
+ type: 'regular_low',
395
+ apiVersion: 3,
396
+ operation: OP.SET
397
+ };
398
+ }
399
+ else if ('deleteForMe' in mod) {
400
+ const { timestamp, key, deleteMedia } = mod.deleteForMe;
401
+ patch = {
402
+ syncAction: {
403
+ deleteMessageForMeAction: {
404
+ deleteMedia,
405
+ messageTimestamp: timestamp
406
+ }
407
+ },
408
+ index: ['deleteMessageForMe', jid, key.id, key.fromMe ? '1' : '0', '0'],
409
+ type: 'regular_high',
410
+ apiVersion: 3,
411
+ operation: OP.SET
412
+ };
413
+ }
414
+ else if ('clear' in mod) {
415
+ patch = {
416
+ syncAction: {
417
+ clearChatAction: {
418
+ messageRange: getMessageRange(mod.lastMessages)
419
+ }
420
+ },
421
+ index: ['clearChat', jid, '1' /*the option here is 0 when keep starred messages is enabled*/, '0'],
422
+ type: 'regular_high',
423
+ apiVersion: 6,
424
+ operation: OP.SET
425
+ };
426
+ }
427
+ else if ('pin' in mod) {
428
+ patch = {
429
+ syncAction: {
430
+ pinAction: {
431
+ pinned: !!mod.pin
432
+ }
433
+ },
434
+ index: ['pin_v1', jid],
435
+ type: 'regular_low',
436
+ apiVersion: 5,
437
+ operation: OP.SET
438
+ };
439
+ }
440
+ else if ('contact' in mod) {
441
+ patch = {
442
+ syncAction: {
443
+ contactAction: mod.contact || {}
444
+ },
445
+ index: ['contact', jid],
446
+ type: 'critical_unblock_low',
447
+ apiVersion: 2,
448
+ operation: mod.contact ? OP.SET : OP.REMOVE
449
+ };
450
+ }
451
+ else if ('disableLinkPreviews' in mod) {
452
+ patch = {
453
+ syncAction: {
454
+ privacySettingDisableLinkPreviewsAction: mod.disableLinkPreviews || {}
455
+ },
456
+ index: ['setting_disableLinkPreviews'],
457
+ type: 'regular',
458
+ apiVersion: 8,
459
+ operation: OP.SET
460
+ };
461
+ }
462
+ else if ('star' in mod) {
463
+ const key = mod.star.messages[0];
464
+ patch = {
465
+ syncAction: {
466
+ starAction: {
467
+ starred: !!mod.star.star
468
+ }
469
+ },
470
+ index: ['star', jid, key.id, key.fromMe ? '1' : '0', '0'],
471
+ type: 'regular_low',
472
+ apiVersion: 2,
473
+ operation: OP.SET
474
+ };
475
+ }
476
+ else if ('delete' in mod) {
477
+ patch = {
478
+ syncAction: {
479
+ deleteChatAction: {
480
+ messageRange: getMessageRange(mod.lastMessages)
481
+ }
482
+ },
483
+ index: ['deleteChat', jid, '1'],
484
+ type: 'regular_high',
485
+ apiVersion: 6,
486
+ operation: OP.SET
487
+ };
488
+ }
489
+ else if ('pushNameSetting' in mod) {
490
+ patch = {
491
+ syncAction: {
492
+ pushNameSetting: {
493
+ name: mod.pushNameSetting
494
+ }
495
+ },
496
+ index: ['setting_pushName'],
497
+ type: 'critical_block',
498
+ apiVersion: 1,
499
+ operation: OP.SET
500
+ };
501
+ }
502
+ else if ('quickReply' in mod) {
503
+ patch = {
504
+ syncAction: {
505
+ quickReplyAction: {
506
+ count: 0,
507
+ deleted: mod.quickReply.deleted || false,
508
+ keywords: [],
509
+ message: mod.quickReply.message || '',
510
+ shortcut: mod.quickReply.shortcut || ''
511
+ }
512
+ },
513
+ index: ['quick_reply', mod.quickReply.timestamp || String(Math.floor(Date.now() / 1000))],
514
+ type: 'regular',
515
+ apiVersion: 2,
516
+ operation: OP.SET
517
+ };
518
+ }
519
+ else if ('addLabel' in mod) {
520
+ patch = {
521
+ syncAction: {
522
+ labelEditAction: {
523
+ name: mod.addLabel.name,
524
+ color: mod.addLabel.color,
525
+ predefinedId: mod.addLabel.predefinedId,
526
+ deleted: mod.addLabel.deleted
527
+ }
528
+ },
529
+ index: ['label_edit', mod.addLabel.id],
530
+ type: 'regular',
531
+ apiVersion: 3,
532
+ operation: OP.SET
533
+ };
534
+ }
535
+ else if ('addChatLabel' in mod) {
536
+ patch = {
537
+ syncAction: {
538
+ labelAssociationAction: {
539
+ labeled: true
540
+ }
541
+ },
542
+ index: [LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
543
+ type: 'regular',
544
+ apiVersion: 3,
545
+ operation: OP.SET
546
+ };
547
+ }
548
+ else if ('removeChatLabel' in mod) {
549
+ patch = {
550
+ syncAction: {
551
+ labelAssociationAction: {
552
+ labeled: false
553
+ }
554
+ },
555
+ index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
556
+ type: 'regular',
557
+ apiVersion: 3,
558
+ operation: OP.SET
559
+ };
560
+ }
561
+ else if ('addMessageLabel' in mod) {
562
+ patch = {
563
+ syncAction: {
564
+ labelAssociationAction: {
565
+ labeled: true
566
+ }
567
+ },
568
+ index: [LabelAssociationType.Message, mod.addMessageLabel.labelId, jid, mod.addMessageLabel.messageId, '0', '0'],
569
+ type: 'regular',
570
+ apiVersion: 3,
571
+ operation: OP.SET
572
+ };
573
+ }
574
+ else if ('removeMessageLabel' in mod) {
575
+ patch = {
576
+ syncAction: {
577
+ labelAssociationAction: {
578
+ labeled: false
579
+ }
580
+ },
581
+ index: [
582
+ LabelAssociationType.Message,
583
+ mod.removeMessageLabel.labelId,
584
+ jid,
585
+ mod.removeMessageLabel.messageId,
586
+ '0',
587
+ '0'
588
+ ],
589
+ type: 'regular',
590
+ apiVersion: 3,
591
+ operation: OP.SET
592
+ };
593
+ }
594
+ else {
595
+ throw new Boom('not supported');
596
+ }
597
+ patch.syncAction.timestamp = Date.now();
598
+ return patch;
599
+ };
600
+ export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
601
+ const isInitialSync = !!initialSyncOpts;
602
+ const accountSettings = initialSyncOpts?.accountSettings;
603
+ logger?.trace({ syncAction, initialSync: !!initialSyncOpts }, 'processing sync action');
604
+ const { syncAction: { value: action }, index: [type, id, msgId, fromMe] } = syncAction;
605
+ if (action?.muteAction) {
606
+ ev.emit('chats.update', [
607
+ {
608
+ id,
609
+ muteEndTime: action.muteAction?.muted ? toNumber(action.muteAction.muteEndTimestamp) : null,
610
+ conditional: getChatUpdateConditional(id, undefined)
611
+ }
612
+ ]);
613
+ }
614
+ else if (action?.archiveChatAction || type === 'archive' || type === 'unarchive') {
615
+ // okay so we've to do some annoying computation here
616
+ // when we're initially syncing the app state
617
+ // there are a few cases we need to handle
618
+ // 1. if the account unarchiveChats setting is true
619
+ // a. if the chat is archived, and no further messages have been received -- simple, keep archived
620
+ // b. if the chat was archived, and the user received messages from the other person afterwards
621
+ // then the chat should be marked unarchved --
622
+ // we compare the timestamp of latest message from the other person to determine this
623
+ // 2. if the account unarchiveChats setting is false -- then it doesn't matter,
624
+ // it'll always take an app state action to mark in unarchived -- which we'll get anyway
625
+ const archiveAction = action?.archiveChatAction;
626
+ const isArchived = archiveAction ? archiveAction.archived : type === 'archive';
627
+ // // basically we don't need to fire an "archive" update if the chat is being marked unarchvied
628
+ // // this only applies for the initial sync
629
+ // if(isInitialSync && !isArchived) {
630
+ // isArchived = false
631
+ // }
632
+ const msgRange = !accountSettings?.unarchiveChats ? undefined : archiveAction?.messageRange;
633
+ // logger?.debug({ chat: id, syncAction }, 'message range archive')
634
+ ev.emit('chats.update', [
635
+ {
636
+ id,
637
+ archived: isArchived,
638
+ conditional: getChatUpdateConditional(id, msgRange)
639
+ }
640
+ ]);
641
+ }
642
+ else if (action?.markChatAsReadAction) {
643
+ const markReadAction = action.markChatAsReadAction;
644
+ // basically we don't need to fire an "read" update if the chat is being marked as read
645
+ // because the chat is read by default
646
+ // this only applies for the initial sync
647
+ const isNullUpdate = isInitialSync && markReadAction.read;
648
+ ev.emit('chats.update', [
649
+ {
650
+ id,
651
+ unreadCount: isNullUpdate ? null : !!markReadAction?.read ? 0 : -1,
652
+ conditional: getChatUpdateConditional(id, markReadAction?.messageRange)
653
+ }
654
+ ]);
655
+ }
656
+ else if (action?.deleteMessageForMeAction || type === 'deleteMessageForMe') {
657
+ ev.emit('messages.delete', {
658
+ keys: [
659
+ {
660
+ remoteJid: id,
661
+ id: msgId,
662
+ fromMe: fromMe === '1'
663
+ }
664
+ ]
665
+ });
666
+ }
667
+ else if (action?.contactAction) {
668
+ ev.emit('contacts.upsert', [
669
+ {
670
+ id: id,
671
+ name: action.contactAction.fullName,
672
+ lid: action.contactAction.lidJid || undefined,
673
+ phoneNumber: action.contactAction.pnJid || undefined
674
+ }
675
+ ]);
676
+ }
677
+ else if (action?.pushNameSetting) {
678
+ const name = action?.pushNameSetting?.name;
679
+ if (name && me?.name !== name) {
680
+ ev.emit('creds.update', { me: { ...me, name } });
681
+ }
682
+ }
683
+ else if (action?.pinAction) {
684
+ ev.emit('chats.update', [
685
+ {
686
+ id,
687
+ pinned: action.pinAction?.pinned ? toNumber(action.timestamp) : null,
688
+ conditional: getChatUpdateConditional(id, undefined)
689
+ }
690
+ ]);
691
+ }
692
+ else if (action?.unarchiveChatsSetting) {
693
+ const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats;
694
+ ev.emit('creds.update', { accountSettings: { unarchiveChats } });
695
+ logger?.info(`archive setting updated => '${action.unarchiveChatsSetting.unarchiveChats}'`);
696
+ if (accountSettings) {
697
+ accountSettings.unarchiveChats = unarchiveChats;
698
+ }
699
+ }
700
+ else if (action?.starAction || type === 'star') {
701
+ let starred = action?.starAction?.starred;
702
+ if (typeof starred !== 'boolean') {
703
+ starred = syncAction.index[syncAction.index.length - 1] === '1';
704
+ }
705
+ ev.emit('messages.update', [
706
+ {
707
+ key: { remoteJid: id, id: msgId, fromMe: fromMe === '1' },
708
+ update: { starred }
709
+ }
710
+ ]);
711
+ }
712
+ else if (action?.deleteChatAction || type === 'deleteChat') {
713
+ if (!isInitialSync) {
714
+ ev.emit('chats.delete', [id]);
715
+ }
716
+ }
717
+ else if (action?.labelEditAction) {
718
+ const { name, color, deleted, predefinedId } = action.labelEditAction;
719
+ ev.emit('labels.edit', {
720
+ id: id,
721
+ name: name,
722
+ color: color,
723
+ deleted: deleted,
724
+ predefinedId: predefinedId ? String(predefinedId) : undefined
725
+ });
726
+ }
727
+ else if (action?.labelAssociationAction) {
728
+ ev.emit('labels.association', {
729
+ type: action.labelAssociationAction.labeled ? 'add' : 'remove',
730
+ association: type === LabelAssociationType.Chat
731
+ ? {
732
+ type: LabelAssociationType.Chat,
733
+ chatId: syncAction.index[2],
734
+ labelId: syncAction.index[1]
735
+ }
736
+ : {
737
+ type: LabelAssociationType.Message,
738
+ chatId: syncAction.index[2],
739
+ messageId: syncAction.index[3],
740
+ labelId: syncAction.index[1]
741
+ }
742
+ });
743
+ }
744
+ else {
745
+ logger?.debug({ syncAction, id }, 'unprocessable update');
746
+ }
747
+ function getChatUpdateConditional(id, msgRange) {
748
+ return isInitialSync
749
+ ? data => {
750
+ const chat = data.historySets.chats[id] || data.chatUpserts[id];
751
+ if (chat) {
752
+ return msgRange ? isValidPatchBasedOnMessageRange(chat, msgRange) : true;
753
+ }
754
+ }
755
+ : undefined;
756
+ }
757
+ function isValidPatchBasedOnMessageRange(chat, msgRange) {
758
+ const lastMsgTimestamp = Number(msgRange?.lastMessageTimestamp || msgRange?.lastSystemMessageTimestamp || 0);
759
+ const chatLastMsgTimestamp = Number(chat?.lastMessageRecvTimestamp || 0);
760
+ return lastMsgTimestamp >= chatLastMsgTimestamp;
761
+ }
762
+ };
763
+ //# sourceMappingURL=chat-utils.js.map