@d0v3riz/baileys 6.7.19 → 6.7.21

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