xspacesocket 0.0.0-stage → 1.3.1

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 (477) hide show
  1. package/LICENSE +32 -0
  2. package/README.md +199 -2
  3. package/WAProto/GenerateStatics.sh +3 -0
  4. package/WAProto/WAProto.proto +5488 -0
  5. package/WAProto/fix-imports.js +85 -0
  6. package/WAProto/index.d.ts +14043 -0
  7. package/WAProto/index.js +107198 -0
  8. package/engine-requirements.js +10 -0
  9. package/lib/Defaults/index.d.ts +81 -0
  10. package/lib/Defaults/index.d.ts.map +1 -0
  11. package/lib/Defaults/index.js +146 -0
  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/Group/group-session-builder.d.ts +15 -0
  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/Group/group_cipher.js +82 -0
  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/Group/sender-message-key.js +26 -0
  60. package/lib/Signal/Group/sender-message-key.js.map +1 -0
  61. package/lib/Signal/libsignal.d.ts +5 -0
  62. package/lib/Signal/libsignal.d.ts.map +1 -0
  63. package/lib/Signal/libsignal.js +431 -0
  64. package/lib/Signal/libsignal.js.map +1 -0
  65. package/lib/Signal/lid-mapping.d.ts +23 -0
  66. package/lib/Signal/lid-mapping.d.ts.map +1 -0
  67. package/lib/Signal/lid-mapping.js +277 -0
  68. package/lib/Signal/lid-mapping.js.map +1 -0
  69. package/lib/Socket/Client/index.d.ts +3 -0
  70. package/lib/Socket/Client/index.d.ts.map +1 -0
  71. package/lib/Socket/Client/index.js +3 -0
  72. package/lib/Socket/Client/index.js.map +1 -0
  73. package/lib/Socket/Client/types.d.ts +16 -0
  74. package/lib/Socket/Client/types.d.ts.map +1 -0
  75. package/lib/Socket/Client/types.js +11 -0
  76. package/lib/Socket/Client/types.js.map +1 -0
  77. package/lib/Socket/Client/websocket.d.ts +13 -0
  78. package/lib/Socket/Client/websocket.d.ts.map +1 -0
  79. package/lib/Socket/Client/websocket.js +54 -0
  80. package/lib/Socket/Client/websocket.js.map +1 -0
  81. package/lib/Socket/business.d.ts +387 -0
  82. package/lib/Socket/business.d.ts.map +1 -0
  83. package/lib/Socket/business.js +379 -0
  84. package/lib/Socket/business.js.map +1 -0
  85. package/lib/Socket/channelPollVote.d.ts +44 -0
  86. package/lib/Socket/channelPollVote.d.ts.map +1 -0
  87. package/lib/Socket/channelPollVote.js +65 -0
  88. package/lib/Socket/channelPollVote.js.map +1 -0
  89. package/lib/Socket/channelSchedule.d.ts +229 -0
  90. package/lib/Socket/channelSchedule.d.ts.map +1 -0
  91. package/lib/Socket/channelSchedule.js +465 -0
  92. package/lib/Socket/channelSchedule.js.map +1 -0
  93. package/lib/Socket/channelStatus.d.ts +317 -0
  94. package/lib/Socket/channelStatus.d.ts.map +1 -0
  95. package/lib/Socket/channelStatus.js +479 -0
  96. package/lib/Socket/channelStatus.js.map +1 -0
  97. package/lib/Socket/chats.d.ts +150 -0
  98. package/lib/Socket/chats.d.ts.map +1 -0
  99. package/lib/Socket/chats.js +1313 -0
  100. package/lib/Socket/chats.js.map +1 -0
  101. package/lib/Socket/communities.d.ts +444 -0
  102. package/lib/Socket/communities.d.ts.map +1 -0
  103. package/lib/Socket/communities.js +447 -0
  104. package/lib/Socket/communities.js.map +1 -0
  105. package/lib/Socket/groupHistory.d.ts +38 -0
  106. package/lib/Socket/groupHistory.d.ts.map +1 -0
  107. package/lib/Socket/groupHistory.js +88 -0
  108. package/lib/Socket/groupHistory.js.map +1 -0
  109. package/lib/Socket/groupStatus.d.ts +28 -0
  110. package/lib/Socket/groupStatus.d.ts.map +1 -0
  111. package/lib/Socket/groupStatus.js +47 -0
  112. package/lib/Socket/groupStatus.js.map +1 -0
  113. package/lib/Socket/groups.d.ts +167 -0
  114. package/lib/Socket/groups.d.ts.map +1 -0
  115. package/lib/Socket/groups.js +354 -0
  116. package/lib/Socket/groups.js.map +1 -0
  117. package/lib/Socket/index.d.ts +430 -0
  118. package/lib/Socket/index.d.ts.map +1 -0
  119. package/lib/Socket/index.js +12 -0
  120. package/lib/Socket/index.js.map +1 -0
  121. package/lib/Socket/messages-recv.d.ts +373 -0
  122. package/lib/Socket/messages-recv.d.ts.map +1 -0
  123. package/lib/Socket/messages-recv.js +1897 -0
  124. package/lib/Socket/messages-recv.js.map +1 -0
  125. package/lib/Socket/messages-send.d.ts +404 -0
  126. package/lib/Socket/messages-send.d.ts.map +1 -0
  127. package/lib/Socket/messages-send.js +1528 -0
  128. package/lib/Socket/messages-send.js.map +1 -0
  129. package/lib/Socket/mex.d.ts +3 -0
  130. package/lib/Socket/mex.d.ts.map +1 -0
  131. package/lib/Socket/mex.js +42 -0
  132. package/lib/Socket/mex.js.map +1 -0
  133. package/lib/Socket/newsletter.d.ts +176 -0
  134. package/lib/Socket/newsletter.d.ts.map +1 -0
  135. package/lib/Socket/newsletter.js +181 -0
  136. package/lib/Socket/newsletter.js.map +1 -0
  137. package/lib/Socket/newsletterDirectory.d.ts +201 -0
  138. package/lib/Socket/newsletterDirectory.d.ts.map +1 -0
  139. package/lib/Socket/newsletterDirectory.js +291 -0
  140. package/lib/Socket/newsletterDirectory.js.map +1 -0
  141. package/lib/Socket/payment.d.ts +37 -0
  142. package/lib/Socket/payment.d.ts.map +1 -0
  143. package/lib/Socket/payment.js +124 -0
  144. package/lib/Socket/payment.js.map +1 -0
  145. package/lib/Socket/report.d.ts +70 -0
  146. package/lib/Socket/report.d.ts.map +1 -0
  147. package/lib/Socket/report.js +265 -0
  148. package/lib/Socket/report.js.map +1 -0
  149. package/lib/Socket/socket.d.ts +59 -0
  150. package/lib/Socket/socket.d.ts.map +1 -0
  151. package/lib/Socket/socket.js +982 -0
  152. package/lib/Socket/socket.js.map +1 -0
  153. package/lib/Socket/stickerPack.d.ts +29 -0
  154. package/lib/Socket/stickerPack.d.ts.map +1 -0
  155. package/lib/Socket/stickerPack.js +130 -0
  156. package/lib/Socket/stickerPack.js.map +1 -0
  157. package/lib/Socket/username.d.ts +55 -0
  158. package/lib/Socket/username.d.ts.map +1 -0
  159. package/lib/Socket/username.js +114 -0
  160. package/lib/Socket/username.js.map +1 -0
  161. package/lib/Types/Auth.d.ts +117 -0
  162. package/lib/Types/Auth.d.ts.map +1 -0
  163. package/lib/Types/Auth.js +2 -0
  164. package/lib/Types/Auth.js.map +1 -0
  165. package/lib/Types/Bussines.d.ts +25 -0
  166. package/lib/Types/Bussines.d.ts.map +1 -0
  167. package/lib/Types/Bussines.js +2 -0
  168. package/lib/Types/Bussines.js.map +1 -0
  169. package/lib/Types/Call.d.ts +15 -0
  170. package/lib/Types/Call.d.ts.map +1 -0
  171. package/lib/Types/Call.js +2 -0
  172. package/lib/Types/Call.js.map +1 -0
  173. package/lib/Types/ChannelStatus.d.ts +34 -0
  174. package/lib/Types/ChannelStatus.d.ts.map +1 -0
  175. package/lib/Types/ChannelStatus.js +2 -0
  176. package/lib/Types/ChannelStatus.js.map +1 -0
  177. package/lib/Types/Chat.d.ts +141 -0
  178. package/lib/Types/Chat.d.ts.map +1 -0
  179. package/lib/Types/Chat.js +8 -0
  180. package/lib/Types/Chat.js.map +1 -0
  181. package/lib/Types/Contact.d.ts +26 -0
  182. package/lib/Types/Contact.d.ts.map +1 -0
  183. package/lib/Types/Contact.js +2 -0
  184. package/lib/Types/Contact.js.map +1 -0
  185. package/lib/Types/Events.d.ts +260 -0
  186. package/lib/Types/Events.d.ts.map +1 -0
  187. package/lib/Types/Events.js +2 -0
  188. package/lib/Types/Events.js.map +1 -0
  189. package/lib/Types/GroupMetadata.d.ts +73 -0
  190. package/lib/Types/GroupMetadata.d.ts.map +1 -0
  191. package/lib/Types/GroupMetadata.js +2 -0
  192. package/lib/Types/GroupMetadata.js.map +1 -0
  193. package/lib/Types/Label.d.ts +47 -0
  194. package/lib/Types/Label.d.ts.map +1 -0
  195. package/lib/Types/Label.js +25 -0
  196. package/lib/Types/Label.js.map +1 -0
  197. package/lib/Types/LabelAssociation.d.ts +30 -0
  198. package/lib/Types/LabelAssociation.d.ts.map +1 -0
  199. package/lib/Types/LabelAssociation.js +7 -0
  200. package/lib/Types/LabelAssociation.js.map +1 -0
  201. package/lib/Types/Message.d.ts +466 -0
  202. package/lib/Types/Message.d.ts.map +1 -0
  203. package/lib/Types/Message.js +11 -0
  204. package/lib/Types/Message.js.map +1 -0
  205. package/lib/Types/Mex.d.ts +141 -0
  206. package/lib/Types/Mex.d.ts.map +1 -0
  207. package/lib/Types/Mex.js +37 -0
  208. package/lib/Types/Mex.js.map +1 -0
  209. package/lib/Types/Payment.d.ts +47 -0
  210. package/lib/Types/Payment.d.ts.map +1 -0
  211. package/lib/Types/Payment.js +2 -0
  212. package/lib/Types/Payment.js.map +1 -0
  213. package/lib/Types/Product.d.ts +79 -0
  214. package/lib/Types/Product.d.ts.map +1 -0
  215. package/lib/Types/Product.js +2 -0
  216. package/lib/Types/Product.js.map +1 -0
  217. package/lib/Types/Report.d.ts +219 -0
  218. package/lib/Types/Report.d.ts.map +1 -0
  219. package/lib/Types/Report.js +166 -0
  220. package/lib/Types/Report.js.map +1 -0
  221. package/lib/Types/Signal.d.ts +87 -0
  222. package/lib/Types/Signal.d.ts.map +1 -0
  223. package/lib/Types/Signal.js +2 -0
  224. package/lib/Types/Signal.js.map +1 -0
  225. package/lib/Types/Socket.d.ts +136 -0
  226. package/lib/Types/Socket.d.ts.map +1 -0
  227. package/lib/Types/Socket.js +3 -0
  228. package/lib/Types/Socket.js.map +1 -0
  229. package/lib/Types/State.d.ts +97 -0
  230. package/lib/Types/State.d.ts.map +1 -0
  231. package/lib/Types/State.js +56 -0
  232. package/lib/Types/State.js.map +1 -0
  233. package/lib/Types/StickerPack.d.ts +52 -0
  234. package/lib/Types/StickerPack.d.ts.map +1 -0
  235. package/lib/Types/StickerPack.js +2 -0
  236. package/lib/Types/StickerPack.js.map +1 -0
  237. package/lib/Types/USync.d.ts +26 -0
  238. package/lib/Types/USync.d.ts.map +1 -0
  239. package/lib/Types/USync.js +2 -0
  240. package/lib/Types/USync.js.map +1 -0
  241. package/lib/Types/Username.d.ts +19 -0
  242. package/lib/Types/Username.d.ts.map +1 -0
  243. package/lib/Types/Username.js +2 -0
  244. package/lib/Types/Username.js.map +1 -0
  245. package/lib/Types/index.d.ts +71 -0
  246. package/lib/Types/index.d.ts.map +1 -0
  247. package/lib/Types/index.js +31 -0
  248. package/lib/Types/index.js.map +1 -0
  249. package/lib/Utils/auth-utils.d.ts +24 -0
  250. package/lib/Utils/auth-utils.d.ts.map +1 -0
  251. package/lib/Utils/auth-utils.js +302 -0
  252. package/lib/Utils/auth-utils.js.map +1 -0
  253. package/lib/Utils/browser-utils.d.ts +4 -0
  254. package/lib/Utils/browser-utils.d.ts.map +1 -0
  255. package/lib/Utils/browser-utils.js +29 -0
  256. package/lib/Utils/browser-utils.js.map +1 -0
  257. package/lib/Utils/business.d.ts +23 -0
  258. package/lib/Utils/business.d.ts.map +1 -0
  259. package/lib/Utils/business.js +231 -0
  260. package/lib/Utils/business.js.map +1 -0
  261. package/lib/Utils/chat-utils.d.ts +100 -0
  262. package/lib/Utils/chat-utils.d.ts.map +1 -0
  263. package/lib/Utils/chat-utils.js +890 -0
  264. package/lib/Utils/chat-utils.js.map +1 -0
  265. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  266. package/lib/Utils/companion-reg-client-utils.d.ts.map +1 -0
  267. package/lib/Utils/companion-reg-client-utils.js +35 -0
  268. package/lib/Utils/companion-reg-client-utils.js.map +1 -0
  269. package/lib/Utils/crypto.d.ts +37 -0
  270. package/lib/Utils/crypto.d.ts.map +1 -0
  271. package/lib/Utils/crypto.js +118 -0
  272. package/lib/Utils/crypto.js.map +1 -0
  273. package/lib/Utils/decode-wa-message.d.ts +66 -0
  274. package/lib/Utils/decode-wa-message.d.ts.map +1 -0
  275. package/lib/Utils/decode-wa-message.js +317 -0
  276. package/lib/Utils/decode-wa-message.js.map +1 -0
  277. package/lib/Utils/event-buffer.d.ts +36 -0
  278. package/lib/Utils/event-buffer.d.ts.map +1 -0
  279. package/lib/Utils/event-buffer.js +642 -0
  280. package/lib/Utils/event-buffer.js.map +1 -0
  281. package/lib/Utils/generics.d.ts +92 -0
  282. package/lib/Utils/generics.d.ts.map +1 -0
  283. package/lib/Utils/generics.js +393 -0
  284. package/lib/Utils/generics.js.map +1 -0
  285. package/lib/Utils/group-history.d.ts +37 -0
  286. package/lib/Utils/group-history.d.ts.map +1 -0
  287. package/lib/Utils/group-history.js +55 -0
  288. package/lib/Utils/group-history.js.map +1 -0
  289. package/lib/Utils/history.d.ts +24 -0
  290. package/lib/Utils/history.d.ts.map +1 -0
  291. package/lib/Utils/history.js +134 -0
  292. package/lib/Utils/history.js.map +1 -0
  293. package/lib/Utils/identity-change-handler.d.ts +44 -0
  294. package/lib/Utils/identity-change-handler.d.ts.map +1 -0
  295. package/lib/Utils/identity-change-handler.js +50 -0
  296. package/lib/Utils/identity-change-handler.js.map +1 -0
  297. package/lib/Utils/index.d.ts +26 -0
  298. package/lib/Utils/index.d.ts.map +1 -0
  299. package/lib/Utils/index.js +26 -0
  300. package/lib/Utils/index.js.map +1 -0
  301. package/lib/Utils/link-preview.d.ts +21 -0
  302. package/lib/Utils/link-preview.d.ts.map +1 -0
  303. package/lib/Utils/link-preview.js +99 -0
  304. package/lib/Utils/link-preview.js.map +1 -0
  305. package/lib/Utils/logger.d.ts +13 -0
  306. package/lib/Utils/logger.d.ts.map +1 -0
  307. package/lib/Utils/logger.js +3 -0
  308. package/lib/Utils/logger.js.map +1 -0
  309. package/lib/Utils/lt-hash.d.ts +8 -0
  310. package/lib/Utils/lt-hash.d.ts.map +1 -0
  311. package/lib/Utils/lt-hash.js +8 -0
  312. package/lib/Utils/lt-hash.js.map +1 -0
  313. package/lib/Utils/make-mutex.d.ts +9 -0
  314. package/lib/Utils/make-mutex.d.ts.map +1 -0
  315. package/lib/Utils/make-mutex.js +33 -0
  316. package/lib/Utils/make-mutex.js.map +1 -0
  317. package/lib/Utils/message-retry-manager.d.ts +119 -0
  318. package/lib/Utils/message-retry-manager.d.ts.map +1 -0
  319. package/lib/Utils/message-retry-manager.js +276 -0
  320. package/lib/Utils/message-retry-manager.js.map +1 -0
  321. package/lib/Utils/messages-media.d.ts +149 -0
  322. package/lib/Utils/messages-media.d.ts.map +1 -0
  323. package/lib/Utils/messages-media.js +875 -0
  324. package/lib/Utils/messages-media.js.map +1 -0
  325. package/lib/Utils/messages.d.ts +178 -0
  326. package/lib/Utils/messages.d.ts.map +1 -0
  327. package/lib/Utils/messages.js +1325 -0
  328. package/lib/Utils/messages.js.map +1 -0
  329. package/lib/Utils/noise-handler.d.ts +20 -0
  330. package/lib/Utils/noise-handler.d.ts.map +1 -0
  331. package/lib/Utils/noise-handler.js +201 -0
  332. package/lib/Utils/noise-handler.js.map +1 -0
  333. package/lib/Utils/offline-node-processor.d.ts +17 -0
  334. package/lib/Utils/offline-node-processor.d.ts.map +1 -0
  335. package/lib/Utils/offline-node-processor.js +40 -0
  336. package/lib/Utils/offline-node-processor.js.map +1 -0
  337. package/lib/Utils/pre-key-manager.d.ts +28 -0
  338. package/lib/Utils/pre-key-manager.d.ts.map +1 -0
  339. package/lib/Utils/pre-key-manager.js +106 -0
  340. package/lib/Utils/pre-key-manager.js.map +1 -0
  341. package/lib/Utils/process-message.d.ts +85 -0
  342. package/lib/Utils/process-message.d.ts.map +1 -0
  343. package/lib/Utils/process-message.js +688 -0
  344. package/lib/Utils/process-message.js.map +1 -0
  345. package/lib/Utils/reporting-utils.d.ts +18 -0
  346. package/lib/Utils/reporting-utils.d.ts.map +1 -0
  347. package/lib/Utils/reporting-utils.js +265 -0
  348. package/lib/Utils/reporting-utils.js.map +1 -0
  349. package/lib/Utils/rich-response.d.ts +12 -0
  350. package/lib/Utils/rich-response.d.ts.map +1 -0
  351. package/lib/Utils/rich-response.js +97 -0
  352. package/lib/Utils/rich-response.js.map +1 -0
  353. package/lib/Utils/signal.d.ts +47 -0
  354. package/lib/Utils/signal.d.ts.map +1 -0
  355. package/lib/Utils/signal.js +201 -0
  356. package/lib/Utils/signal.js.map +1 -0
  357. package/lib/Utils/stanza-ack.d.ts +11 -0
  358. package/lib/Utils/stanza-ack.d.ts.map +1 -0
  359. package/lib/Utils/stanza-ack.js +38 -0
  360. package/lib/Utils/stanza-ack.js.map +1 -0
  361. package/lib/Utils/sync-action-utils.d.ts +19 -0
  362. package/lib/Utils/sync-action-utils.d.ts.map +1 -0
  363. package/lib/Utils/sync-action-utils.js +49 -0
  364. package/lib/Utils/sync-action-utils.js.map +1 -0
  365. package/lib/Utils/sync-key-auto-resolve.d.ts +10 -0
  366. package/lib/Utils/sync-key-auto-resolve.d.ts.map +1 -0
  367. package/lib/Utils/sync-key-auto-resolve.js +33 -0
  368. package/lib/Utils/sync-key-auto-resolve.js.map +1 -0
  369. package/lib/Utils/tc-token-utils.d.ts +37 -0
  370. package/lib/Utils/tc-token-utils.d.ts.map +1 -0
  371. package/lib/Utils/tc-token-utils.js +164 -0
  372. package/lib/Utils/tc-token-utils.js.map +1 -0
  373. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  374. package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
  375. package/lib/Utils/use-multi-file-auth-state.js +153 -0
  376. package/lib/Utils/use-multi-file-auth-state.js.map +1 -0
  377. package/lib/Utils/validate-connection.d.ts +11 -0
  378. package/lib/Utils/validate-connection.d.ts.map +1 -0
  379. package/lib/Utils/validate-connection.js +211 -0
  380. package/lib/Utils/validate-connection.js.map +1 -0
  381. package/lib/Utils/zip.d.ts +11 -0
  382. package/lib/Utils/zip.d.ts.map +1 -0
  383. package/lib/Utils/zip.js +88 -0
  384. package/lib/Utils/zip.js.map +1 -0
  385. package/lib/WABinary/constants.d.ts +28 -0
  386. package/lib/WABinary/constants.d.ts.map +1 -0
  387. package/lib/WABinary/constants.js +1301 -0
  388. package/lib/WABinary/constants.js.map +1 -0
  389. package/lib/WABinary/decode.d.ts +7 -0
  390. package/lib/WABinary/decode.d.ts.map +1 -0
  391. package/lib/WABinary/decode.js +262 -0
  392. package/lib/WABinary/decode.js.map +1 -0
  393. package/lib/WABinary/encode.d.ts +3 -0
  394. package/lib/WABinary/encode.d.ts.map +1 -0
  395. package/lib/WABinary/encode.js +220 -0
  396. package/lib/WABinary/encode.js.map +1 -0
  397. package/lib/WABinary/generic-utils.d.ts +22 -0
  398. package/lib/WABinary/generic-utils.d.ts.map +1 -0
  399. package/lib/WABinary/generic-utils.js +201 -0
  400. package/lib/WABinary/generic-utils.js.map +1 -0
  401. package/lib/WABinary/index.d.ts +6 -0
  402. package/lib/WABinary/index.d.ts.map +1 -0
  403. package/lib/WABinary/index.js +6 -0
  404. package/lib/WABinary/index.js.map +1 -0
  405. package/lib/WABinary/jid-utils.d.ts +48 -0
  406. package/lib/WABinary/jid-utils.d.ts.map +1 -0
  407. package/lib/WABinary/jid-utils.js +96 -0
  408. package/lib/WABinary/jid-utils.js.map +1 -0
  409. package/lib/WABinary/types.d.ts +19 -0
  410. package/lib/WABinary/types.d.ts.map +1 -0
  411. package/lib/WABinary/types.js +2 -0
  412. package/lib/WABinary/types.js.map +1 -0
  413. package/lib/WAM/BinaryInfo.d.ts +9 -0
  414. package/lib/WAM/BinaryInfo.d.ts.map +1 -0
  415. package/lib/WAM/BinaryInfo.js +10 -0
  416. package/lib/WAM/BinaryInfo.js.map +1 -0
  417. package/lib/WAM/constants.d.ts +40 -0
  418. package/lib/WAM/constants.d.ts.map +1 -0
  419. package/lib/WAM/constants.js +22853 -0
  420. package/lib/WAM/constants.js.map +1 -0
  421. package/lib/WAM/encode.d.ts +3 -0
  422. package/lib/WAM/encode.d.ts.map +1 -0
  423. package/lib/WAM/encode.js +150 -0
  424. package/lib/WAM/encode.js.map +1 -0
  425. package/lib/WAM/index.d.ts +4 -0
  426. package/lib/WAM/index.d.ts.map +1 -0
  427. package/lib/WAM/index.js +4 -0
  428. package/lib/WAM/index.js.map +1 -0
  429. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  430. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
  431. package/lib/WAUSync/Protocols/USyncContactProtocol.js +52 -0
  432. package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
  433. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  434. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
  435. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
  436. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
  437. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  438. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
  439. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  440. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
  441. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  442. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
  443. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
  444. package/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
  445. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  446. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts.map +1 -0
  447. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
  448. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js.map +1 -0
  449. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  450. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
  451. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
  452. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
  453. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  454. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
  455. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
  456. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
  457. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  458. package/lib/WAUSync/Protocols/index.d.ts.map +1 -0
  459. package/lib/WAUSync/Protocols/index.js +6 -0
  460. package/lib/WAUSync/Protocols/index.js.map +1 -0
  461. package/lib/WAUSync/USyncQuery.d.ts +30 -0
  462. package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
  463. package/lib/WAUSync/USyncQuery.js +98 -0
  464. package/lib/WAUSync/USyncQuery.js.map +1 -0
  465. package/lib/WAUSync/USyncUser.d.ts +17 -0
  466. package/lib/WAUSync/USyncUser.d.ts.map +1 -0
  467. package/lib/WAUSync/USyncUser.js +31 -0
  468. package/lib/WAUSync/USyncUser.js.map +1 -0
  469. package/lib/WAUSync/index.d.ts +4 -0
  470. package/lib/WAUSync/index.d.ts.map +1 -0
  471. package/lib/WAUSync/index.js +4 -0
  472. package/lib/WAUSync/index.js.map +1 -0
  473. package/lib/index.d.ts +12 -0
  474. package/lib/index.d.ts.map +1 -0
  475. package/lib/index.js +11 -0
  476. package/lib/index.js.map +1 -0
  477. package/package.json +136 -4
@@ -0,0 +1,890 @@
1
+ import { Boom } from '@hapi/boom';
2
+ import { expandAppStateKeys } from 'whatsapp-rust-bridge';
3
+ import { proto } from '../../WAProto/index.js';
4
+ import { LabelAssociationType } from '../Types/LabelAssociation.js';
5
+ import { getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidNormalizedUser } from '../WABinary/index.js';
6
+ import { aesDecrypt, aesEncrypt, hmacSign } from './crypto.js';
7
+ import { toNumber } from './generics.js';
8
+ import { LT_HASH_ANTI_TAMPERING } from './lt-hash.js';
9
+ import { downloadContentFromMessage } from './messages-media.js';
10
+ import { emitSyncActionResults, processContactAction } from './sync-action-utils.js';
11
+ const mutationKeys = (keydata) => {
12
+ const keys = expandAppStateKeys(keydata);
13
+ return {
14
+ indexKey: keys.indexKey,
15
+ valueEncryptionKey: keys.valueEncryptionKey,
16
+ valueMacKey: keys.valueMacKey,
17
+ snapshotMacKey: keys.snapshotMacKey,
18
+ patchMacKey: keys.patchMacKey
19
+ };
20
+ };
21
+ const generateMac = (operation, data, keyId, key) => {
22
+ const opByte = operation === proto.SyncdMutation.SyncdOperation.SET ? 0x01 : 0x02;
23
+ const keyIdBuffer = typeof keyId === 'string' ? Buffer.from(keyId, 'base64') : keyId;
24
+ const keyData = new Uint8Array(1 + keyIdBuffer.length);
25
+ keyData[0] = opByte;
26
+ keyData.set(keyIdBuffer, 1);
27
+ const last = new Uint8Array(8);
28
+ last[7] = keyData.length;
29
+ const total = new Uint8Array(keyData.length + data.length + last.length);
30
+ total.set(keyData, 0);
31
+ total.set(data, keyData.length);
32
+ total.set(last, keyData.length + data.length);
33
+ const hmac = hmacSign(total, key, 'sha512');
34
+ return hmac.subarray(0, 32);
35
+ };
36
+ const to64BitNetworkOrder = (e) => {
37
+ const buff = Buffer.alloc(8);
38
+ buff.writeUint32BE(e, 4);
39
+ return buff;
40
+ };
41
+ export const makeLtHashGenerator = ({ indexValueMap, hash }) => {
42
+ indexValueMap = { ...indexValueMap };
43
+ const addBuffs = [];
44
+ const subBuffs = [];
45
+ return {
46
+ mix: ({ indexMac, valueMac, operation }) => {
47
+ const indexMacBase64 = Buffer.from(indexMac).toString('base64');
48
+ const prevOp = indexValueMap[indexMacBase64];
49
+ if (operation === proto.SyncdMutation.SyncdOperation.REMOVE) {
50
+ if (!prevOp) {
51
+ // WA Web does not throw here — it logs a warning and skips the subtract.
52
+ // The missing REMOVE will cause an LTHash mismatch, which is handled
53
+ // by the MAC validation layer (snapshot recovery or retry).
54
+ return;
55
+ }
56
+ // remove from index value mac, since this mutation is erased
57
+ delete indexValueMap[indexMacBase64];
58
+ }
59
+ else {
60
+ addBuffs.push(valueMac);
61
+ // add this index into the history map
62
+ indexValueMap[indexMacBase64] = { valueMac };
63
+ }
64
+ if (prevOp) {
65
+ subBuffs.push(prevOp.valueMac);
66
+ }
67
+ },
68
+ finish: () => {
69
+ const result = LT_HASH_ANTI_TAMPERING.subtractThenAdd(hash, subBuffs, addBuffs);
70
+ return {
71
+ hash: Buffer.from(result),
72
+ indexValueMap
73
+ };
74
+ }
75
+ };
76
+ };
77
+ const generateSnapshotMac = (lthash, version, name, key) => {
78
+ const total = Buffer.concat([lthash, to64BitNetworkOrder(version), Buffer.from(name, 'utf-8')]);
79
+ return hmacSign(total, key, 'sha256');
80
+ };
81
+ const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
82
+ const total = Buffer.concat([snapshotMac, ...valueMacs, to64BitNetworkOrder(version), Buffer.from(type, 'utf-8')]);
83
+ return hmacSign(total, key);
84
+ };
85
+ export const newLTHashState = () => ({ version: 0, hash: Buffer.alloc(128), indexValueMap: {} });
86
+ export const ensureLTHashStateVersion = (state) => {
87
+ if (typeof state.version !== 'number' || isNaN(state.version)) {
88
+ state.version = 0;
89
+ }
90
+ return state;
91
+ };
92
+ export const MAX_SYNC_ATTEMPTS = 2;
93
+ /**
94
+ * Check if an error is a missing app state sync key.
95
+ * WA Web treats these as "Blocked" (waits for key arrival), not fatal.
96
+ * In Baileys we retry with a snapshot which may use a different key.
97
+ */
98
+ export const isMissingKeyError = (error) => {
99
+ return error?.data?.isMissingKey === true;
100
+ };
101
+ /**
102
+ * Determines if an app state sync error is unrecoverable.
103
+ * TypeError indicates a WASM crash; otherwise we give up after MAX_SYNC_ATTEMPTS.
104
+ * Missing keys are NOT checked here — they are handled separately as "Blocked".
105
+ */
106
+ export const isAppStateSyncIrrecoverable = (error, attempts) => {
107
+ return attempts >= MAX_SYNC_ATTEMPTS || error?.name === 'TypeError';
108
+ };
109
+ export const encodeSyncdPatch = async ({ type, index, syncAction, apiVersion, operation }, myAppStateKeyId, state, getAppStateSyncKey) => {
110
+ const key = !!myAppStateKeyId ? await getAppStateSyncKey(myAppStateKeyId) : undefined;
111
+ if (!key) {
112
+ throw new Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { data: { isMissingKey: true } });
113
+ }
114
+ const encKeyId = Buffer.from(myAppStateKeyId, 'base64');
115
+ state = { ...state, indexValueMap: { ...state.indexValueMap } };
116
+ const indexBuffer = Buffer.from(JSON.stringify(index));
117
+ const dataProto = proto.SyncActionData.fromObject({
118
+ index: indexBuffer,
119
+ value: syncAction,
120
+ padding: new Uint8Array(0),
121
+ version: apiVersion
122
+ });
123
+ const encoded = proto.SyncActionData.encode(dataProto).finish();
124
+ const keyValue = mutationKeys(key.keyData);
125
+ const encValue = aesEncrypt(encoded, keyValue.valueEncryptionKey);
126
+ const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey);
127
+ const indexMac = hmacSign(indexBuffer, keyValue.indexKey);
128
+ // update LT hash
129
+ const generator = makeLtHashGenerator(state);
130
+ generator.mix({ indexMac, valueMac, operation });
131
+ Object.assign(state, generator.finish());
132
+ state.version += 1;
133
+ const snapshotMac = generateSnapshotMac(state.hash, state.version, type, keyValue.snapshotMacKey);
134
+ const patch = {
135
+ patchMac: generatePatchMac(snapshotMac, [valueMac], state.version, type, keyValue.patchMacKey),
136
+ snapshotMac: snapshotMac,
137
+ keyId: { id: encKeyId },
138
+ mutations: [
139
+ {
140
+ operation: operation,
141
+ record: {
142
+ index: {
143
+ blob: indexMac
144
+ },
145
+ value: {
146
+ blob: Buffer.concat([encValue, valueMac])
147
+ },
148
+ keyId: { id: encKeyId }
149
+ }
150
+ }
151
+ ]
152
+ };
153
+ const base64Index = indexMac.toString('base64');
154
+ state.indexValueMap[base64Index] = { valueMac };
155
+ return { patch, state };
156
+ };
157
+ export const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
158
+ const ltGenerator = makeLtHashGenerator(initialState);
159
+ const derivedKeyCache = new Map();
160
+ // indexKey used to HMAC sign record.index.blob
161
+ // valueEncryptionKey used to AES-256-CBC encrypt record.value.blob[0:-32]
162
+ // 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
163
+ for (const msgMutation of msgMutations) {
164
+ // if it's a syncdmutation, get the operation property
165
+ // otherwise, if it's only a record -- it'll be a SET mutation
166
+ const operation = 'operation' in msgMutation ? msgMutation.operation : proto.SyncdMutation.SyncdOperation.SET;
167
+ const record = 'record' in msgMutation && !!msgMutation.record ? msgMutation.record : msgMutation;
168
+ let key;
169
+ try {
170
+ key = await getKey(record.keyId.id);
171
+ }
172
+ catch (err) {
173
+ // Missing-key errors must propagate so the orchestrator can park the
174
+ // collection (Blocked) and retry when APP_STATE_SYNC_KEY_SHARE arrives.
175
+ // Other errors → individual record corruption, skip and keep going.
176
+ if (isMissingKeyError(err))
177
+ throw err;
178
+ continue;
179
+ }
180
+ const content = record.value.blob;
181
+ const encContent = content.subarray(0, -32);
182
+ const ogValueMac = content.subarray(-32);
183
+ if (validateMacs) {
184
+ const contentHmac = generateMac(operation, encContent, record.keyId.id, key.valueMacKey);
185
+ if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
186
+ // HMAC verification failed — skip this record
187
+ continue;
188
+ }
189
+ }
190
+ let result;
191
+ try {
192
+ result = aesDecrypt(encContent, key.valueEncryptionKey);
193
+ }
194
+ catch {
195
+ // decrypt failed — skip this record instead of aborting
196
+ continue;
197
+ }
198
+ const syncAction = proto.SyncActionData.decode(result);
199
+ if (validateMacs) {
200
+ const hmac = hmacSign(syncAction.index, key.indexKey);
201
+ if (Buffer.compare(hmac, record.index.blob) !== 0) {
202
+ throw new Boom('HMAC index verification failed');
203
+ }
204
+ }
205
+ const indexStr = Buffer.from(syncAction.index).toString();
206
+ onMutation({ syncAction, index: JSON.parse(indexStr) });
207
+ ltGenerator.mix({
208
+ indexMac: record.index.blob,
209
+ valueMac: ogValueMac,
210
+ operation: operation
211
+ });
212
+ }
213
+ return ltGenerator.finish();
214
+ async function getKey(keyId) {
215
+ const base64Key = Buffer.from(keyId).toString('base64');
216
+ const cached = derivedKeyCache.get(base64Key);
217
+ if (cached) {
218
+ return cached;
219
+ }
220
+ const keyEnc = await getAppStateSyncKey(base64Key);
221
+ if (!keyEnc) {
222
+ throw new Boom(`failed to find key "${base64Key}" to decode mutation`, {
223
+ data: { isMissingKey: true, msgMutations }
224
+ });
225
+ }
226
+ const keys = mutationKeys(keyEnc.keyData);
227
+ derivedKeyCache.set(base64Key, keys);
228
+ return keys;
229
+ }
230
+ };
231
+ export const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
232
+ if (validateMacs) {
233
+ const base64Key = Buffer.from(msg.keyId.id).toString('base64');
234
+ const mainKeyObj = await getAppStateSyncKey(base64Key);
235
+ if (!mainKeyObj) {
236
+ throw new Boom(`failed to find key "${base64Key}" to decode patch`, { data: { isMissingKey: true, msg } });
237
+ }
238
+ const mainKey = mutationKeys(mainKeyObj.keyData);
239
+ const mutationmacs = msg.mutations.map(mutation => mutation.record.value.blob.slice(-32));
240
+ const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs, toNumber(msg.version.version), name, mainKey.patchMacKey);
241
+ if (Buffer.compare(patchMac, msg.patchMac) !== 0) {
242
+ throw new Boom('Invalid patch mac');
243
+ }
244
+ }
245
+ const result = await decodeSyncdMutations(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs);
246
+ return result;
247
+ };
248
+ export const extractSyncdPatches = async (result, options) => {
249
+ const syncNode = getBinaryNodeChild(result, 'sync');
250
+ const collectionNodes = getBinaryNodeChildren(syncNode, 'collection');
251
+ const final = {};
252
+ await Promise.all(collectionNodes.map(async (collectionNode) => {
253
+ const patchesNode = getBinaryNodeChild(collectionNode, 'patches');
254
+ const patches = getBinaryNodeChildren(patchesNode || collectionNode, 'patch');
255
+ const snapshotNode = getBinaryNodeChild(collectionNode, 'snapshot');
256
+ const syncds = [];
257
+ const name = collectionNode.attrs.name;
258
+ const hasMorePatches = collectionNode.attrs.has_more_patches === 'true';
259
+ let snapshot = undefined;
260
+ if (snapshotNode && !!snapshotNode.content) {
261
+ if (!Buffer.isBuffer(snapshotNode)) {
262
+ snapshotNode.content = Buffer.from(Object.values(snapshotNode.content));
263
+ }
264
+ const blobRef = proto.ExternalBlobReference.decode(snapshotNode.content);
265
+ const data = await downloadExternalBlob(blobRef, options);
266
+ snapshot = proto.SyncdSnapshot.decode(data);
267
+ }
268
+ for (let { content } of patches) {
269
+ if (content) {
270
+ if (!Buffer.isBuffer(content)) {
271
+ content = Buffer.from(Object.values(content));
272
+ }
273
+ const syncd = proto.SyncdPatch.decode(content);
274
+ if (!syncd.version) {
275
+ syncd.version = { version: +collectionNode.attrs.version + 1 };
276
+ }
277
+ syncds.push(syncd);
278
+ }
279
+ }
280
+ final[name] = { patches: syncds, hasMorePatches, snapshot };
281
+ }));
282
+ return final;
283
+ };
284
+ export const downloadExternalBlob = async (blob, options) => {
285
+ const stream = await downloadContentFromMessage(blob, 'md-app-state', { options });
286
+ const bufferArray = [];
287
+ for await (const chunk of stream) {
288
+ bufferArray.push(chunk);
289
+ }
290
+ return Buffer.concat(bufferArray);
291
+ };
292
+ export const downloadExternalPatch = async (blob, options) => {
293
+ const buffer = await downloadExternalBlob(blob, options);
294
+ const syncData = proto.SyncdMutations.decode(buffer);
295
+ return syncData;
296
+ };
297
+ export const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, minimumVersionNumber, validateMacs = true, logger) => {
298
+ const newState = newLTHashState();
299
+ newState.version = toNumber(snapshot.version.version);
300
+ const mutationMap = {};
301
+ const areMutationsRequired = typeof minimumVersionNumber === 'undefined' || newState.version > minimumVersionNumber;
302
+ const { hash, indexValueMap } = await decodeSyncdMutations(snapshot.records, newState, getAppStateSyncKey, areMutationsRequired
303
+ ? mutation => {
304
+ const index = mutation.syncAction.index?.toString();
305
+ mutationMap[index] = mutation;
306
+ }
307
+ : () => { }, validateMacs);
308
+ newState.hash = hash;
309
+ newState.indexValueMap = indexValueMap;
310
+ if (validateMacs) {
311
+ const base64Key = Buffer.from(snapshot.keyId.id).toString('base64');
312
+ const keyEnc = await getAppStateSyncKey(base64Key);
313
+ if (!keyEnc) {
314
+ throw new Boom(`failed to find key "${base64Key}" to decode mutation`, { data: { isMissingKey: true } });
315
+ }
316
+ const result = mutationKeys(keyEnc.keyData);
317
+ const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
318
+ if (Buffer.compare(snapshot.mac, computedSnapshotMac) !== 0) {
319
+ // LTHash verification may fail when decodeSyncdMutations skipped undecryptable
320
+ // records (poisoned server-side snapshot); the aggregate client hash diverges
321
+ // from the server-computed mac. Fall through with a warning so the session stays
322
+ // alive with partial state, symmetric to how decodePatches handles its own
323
+ // LTHash mismatch a few lines below.
324
+ logger?.warn({ name, version: newState.version }, 'LTHash verification failed on snapshot, continuing with partial state');
325
+ }
326
+ }
327
+ return {
328
+ state: newState,
329
+ mutationMap
330
+ };
331
+ };
332
+ export const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options, minimumVersionNumber, logger, validateMacs = true) => {
333
+ const newState = {
334
+ ...initial,
335
+ indexValueMap: { ...initial.indexValueMap }
336
+ };
337
+ const mutationMap = {};
338
+ for (const syncd of syncds) {
339
+ const { version, keyId, snapshotMac } = syncd;
340
+ if (syncd.externalMutations) {
341
+ logger?.trace({ name, version }, 'downloading external patch');
342
+ const ref = await downloadExternalPatch(syncd.externalMutations, options);
343
+ logger?.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch');
344
+ syncd.mutations?.push(...ref.mutations);
345
+ }
346
+ const patchVersion = toNumber(version.version);
347
+ newState.version = patchVersion;
348
+ const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber;
349
+ let decodeResult;
350
+ try {
351
+ decodeResult = await decodeSyncdPatch(syncd, name, newState, getAppStateSyncKey, shouldMutate
352
+ ? mutation => {
353
+ const index = mutation.syncAction.index?.toString();
354
+ mutationMap[index] = mutation;
355
+ }
356
+ : () => { }, validateMacs);
357
+ }
358
+ catch (err) {
359
+ if (isMissingKeyError(err))
360
+ throw err;
361
+ logger?.warn({ name, version: patchVersion, error: err.message }, 'failed to decode patch, skipping');
362
+ continue;
363
+ }
364
+ newState.hash = decodeResult.hash;
365
+ newState.indexValueMap = decodeResult.indexValueMap;
366
+ if (validateMacs) {
367
+ const base64Key = Buffer.from(keyId.id).toString('base64');
368
+ const keyEnc = await getAppStateSyncKey(base64Key);
369
+ if (!keyEnc) {
370
+ throw new Boom(`failed to find key "${base64Key}" to decode mutation`, { data: { isMissingKey: true } });
371
+ }
372
+ const result = mutationKeys(keyEnc.keyData);
373
+ const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
374
+ if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
375
+ logger?.warn({ name, version: newState.version }, 'LTHash verification failed, skipping remaining patches');
376
+ break;
377
+ }
378
+ }
379
+ // clear memory used up by the mutations
380
+ syncd.mutations = [];
381
+ }
382
+ return { state: newState, mutationMap };
383
+ };
384
+ export const chatModificationToAppPatch = (mod, jid) => {
385
+ const OP = proto.SyncdMutation.SyncdOperation;
386
+ const getMessageRange = (lastMessages) => {
387
+ let messageRange;
388
+ if (Array.isArray(lastMessages)) {
389
+ const lastMsg = lastMessages[lastMessages.length - 1];
390
+ messageRange = {
391
+ lastMessageTimestamp: lastMsg?.messageTimestamp,
392
+ messages: lastMessages?.length
393
+ ? lastMessages.map(m => {
394
+ if (!m.key?.id || !m.key?.remoteJid) {
395
+ throw new Boom('Incomplete key', { statusCode: 400, data: m });
396
+ }
397
+ if (isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
398
+ throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m });
399
+ }
400
+ if (!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
401
+ throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m });
402
+ }
403
+ if (m.key.participant) {
404
+ m.key.participant = jidNormalizedUser(m.key.participant);
405
+ }
406
+ return m;
407
+ })
408
+ : undefined
409
+ };
410
+ }
411
+ else {
412
+ messageRange = lastMessages;
413
+ }
414
+ return messageRange;
415
+ };
416
+ let patch;
417
+ if ('mute' in mod) {
418
+ patch = {
419
+ syncAction: {
420
+ muteAction: {
421
+ muted: !!mod.mute,
422
+ muteEndTimestamp: mod.mute || undefined
423
+ }
424
+ },
425
+ index: ['mute', jid],
426
+ type: 'regular_high',
427
+ apiVersion: 2,
428
+ operation: OP.SET
429
+ };
430
+ }
431
+ else if ('archive' in mod) {
432
+ patch = {
433
+ syncAction: {
434
+ archiveChatAction: {
435
+ archived: !!mod.archive,
436
+ messageRange: getMessageRange(mod.lastMessages)
437
+ }
438
+ },
439
+ index: ['archive', jid],
440
+ type: 'regular_low',
441
+ apiVersion: 3,
442
+ operation: OP.SET
443
+ };
444
+ }
445
+ else if ('markRead' in mod) {
446
+ patch = {
447
+ syncAction: {
448
+ markChatAsReadAction: {
449
+ read: mod.markRead,
450
+ messageRange: getMessageRange(mod.lastMessages)
451
+ }
452
+ },
453
+ index: ['markChatAsRead', jid],
454
+ type: 'regular_low',
455
+ apiVersion: 3,
456
+ operation: OP.SET
457
+ };
458
+ }
459
+ else if ('deleteForMe' in mod) {
460
+ const { timestamp, key, deleteMedia } = mod.deleteForMe;
461
+ patch = {
462
+ syncAction: {
463
+ deleteMessageForMeAction: {
464
+ deleteMedia,
465
+ messageTimestamp: timestamp
466
+ }
467
+ },
468
+ index: ['deleteMessageForMe', jid, key.id, key.fromMe ? '1' : '0', '0'],
469
+ type: 'regular_high',
470
+ apiVersion: 3,
471
+ operation: OP.SET
472
+ };
473
+ }
474
+ else if ('clear' in mod) {
475
+ patch = {
476
+ syncAction: {
477
+ clearChatAction: {
478
+ messageRange: getMessageRange(mod.lastMessages)
479
+ }
480
+ },
481
+ // index[2] is '1' to clear starred messages too (the app's "Clear starred messages" checkbox ticked),
482
+ // '0' to keep them — default '1', matching the previous behaviour
483
+ index: ['clearChat', jid, mod.deleteStarred === false ? '0' : '1', '0'],
484
+ type: 'regular_high',
485
+ apiVersion: 6,
486
+ operation: OP.SET
487
+ };
488
+ }
489
+ else if ('pin' in mod) {
490
+ patch = {
491
+ syncAction: {
492
+ pinAction: {
493
+ pinned: !!mod.pin
494
+ }
495
+ },
496
+ index: ['pin_v1', jid],
497
+ type: 'regular_low',
498
+ apiVersion: 5,
499
+ operation: OP.SET
500
+ };
501
+ }
502
+ else if ('contact' in mod) {
503
+ patch = {
504
+ syncAction: {
505
+ contactAction: mod.contact || {}
506
+ },
507
+ index: ['contact', jid],
508
+ type: 'critical_unblock_low',
509
+ apiVersion: 2,
510
+ operation: mod.contact ? OP.SET : OP.REMOVE
511
+ };
512
+ }
513
+ else if ('disableLinkPreviews' in mod) {
514
+ patch = {
515
+ syncAction: {
516
+ privacySettingDisableLinkPreviewsAction: mod.disableLinkPreviews || {}
517
+ },
518
+ index: ['setting_disableLinkPreviews'],
519
+ type: 'regular',
520
+ apiVersion: 8,
521
+ operation: OP.SET
522
+ };
523
+ }
524
+ else if ('star' in mod) {
525
+ const key = mod.star.messages[0];
526
+ patch = {
527
+ syncAction: {
528
+ starAction: {
529
+ starred: !!mod.star.star
530
+ }
531
+ },
532
+ index: ['star', jid, key.id, key.fromMe ? '1' : '0', '0'],
533
+ type: 'regular_low',
534
+ apiVersion: 2,
535
+ operation: OP.SET
536
+ };
537
+ }
538
+ else if ('delete' in mod) {
539
+ patch = {
540
+ syncAction: {
541
+ deleteChatAction: {
542
+ messageRange: getMessageRange(mod.lastMessages)
543
+ }
544
+ },
545
+ index: ['deleteChat', jid, '1'],
546
+ type: 'regular_high',
547
+ apiVersion: 6,
548
+ operation: OP.SET
549
+ };
550
+ }
551
+ else if ('pushNameSetting' in mod) {
552
+ patch = {
553
+ syncAction: {
554
+ pushNameSetting: {
555
+ name: mod.pushNameSetting
556
+ }
557
+ },
558
+ index: ['setting_pushName'],
559
+ type: 'critical_block',
560
+ apiVersion: 1,
561
+ operation: OP.SET
562
+ };
563
+ }
564
+ else if ('quickReply' in mod) {
565
+ patch = {
566
+ syncAction: {
567
+ quickReplyAction: {
568
+ count: 0,
569
+ deleted: mod.quickReply.deleted || false,
570
+ keywords: [],
571
+ message: mod.quickReply.message || '',
572
+ shortcut: mod.quickReply.shortcut || ''
573
+ }
574
+ },
575
+ index: ['quick_reply', mod.quickReply.timestamp || String(Math.floor(Date.now() / 1000))],
576
+ type: 'regular',
577
+ apiVersion: 2,
578
+ operation: OP.SET
579
+ };
580
+ }
581
+ else if ('addLabel' in mod) {
582
+ patch = {
583
+ syncAction: {
584
+ labelEditAction: {
585
+ name: mod.addLabel.name,
586
+ color: mod.addLabel.color,
587
+ predefinedId: mod.addLabel.predefinedId,
588
+ deleted: mod.addLabel.deleted
589
+ }
590
+ },
591
+ index: ['label_edit', mod.addLabel.id],
592
+ type: 'regular',
593
+ apiVersion: 3,
594
+ operation: OP.SET
595
+ };
596
+ }
597
+ else if ('addChatLabel' in mod) {
598
+ patch = {
599
+ syncAction: {
600
+ labelAssociationAction: {
601
+ labeled: true
602
+ }
603
+ },
604
+ index: [LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
605
+ type: 'regular',
606
+ apiVersion: 3,
607
+ operation: OP.SET
608
+ };
609
+ }
610
+ else if ('removeChatLabel' in mod) {
611
+ patch = {
612
+ syncAction: {
613
+ labelAssociationAction: {
614
+ labeled: false
615
+ }
616
+ },
617
+ index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
618
+ type: 'regular',
619
+ apiVersion: 3,
620
+ operation: OP.SET
621
+ };
622
+ }
623
+ else if ('addMessageLabel' in mod) {
624
+ patch = {
625
+ syncAction: {
626
+ labelAssociationAction: {
627
+ labeled: true
628
+ }
629
+ },
630
+ index: [LabelAssociationType.Message, mod.addMessageLabel.labelId, jid, mod.addMessageLabel.messageId, '0', '0'],
631
+ type: 'regular',
632
+ apiVersion: 3,
633
+ operation: OP.SET
634
+ };
635
+ }
636
+ else if ('removeMessageLabel' in mod) {
637
+ patch = {
638
+ syncAction: {
639
+ labelAssociationAction: {
640
+ labeled: false
641
+ }
642
+ },
643
+ index: [
644
+ LabelAssociationType.Message,
645
+ mod.removeMessageLabel.labelId,
646
+ jid,
647
+ mod.removeMessageLabel.messageId,
648
+ '0',
649
+ '0'
650
+ ],
651
+ type: 'regular',
652
+ apiVersion: 3,
653
+ operation: OP.SET
654
+ };
655
+ }
656
+ else {
657
+ throw new Boom('not supported');
658
+ }
659
+ patch.syncAction.timestamp = Date.now();
660
+ return patch;
661
+ };
662
+ export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
663
+ const isInitialSync = !!initialSyncOpts;
664
+ const accountSettings = initialSyncOpts?.accountSettings;
665
+ logger?.trace({ syncAction, initialSync: !!initialSyncOpts }, 'processing sync action');
666
+ const { syncAction: { value: action }, index: [type, id, msgId, fromMe] } = syncAction;
667
+ if (action?.muteAction) {
668
+ ev.emit('chats.update', [
669
+ {
670
+ id,
671
+ muteEndTime: action.muteAction?.muted ? toNumber(action.muteAction.muteEndTimestamp) : null,
672
+ conditional: getChatUpdateConditional(id, undefined)
673
+ }
674
+ ]);
675
+ }
676
+ else if (action?.archiveChatAction || type === 'archive' || type === 'unarchive') {
677
+ // okay so we've to do some annoying computation here
678
+ // when we're initially syncing the app state
679
+ // there are a few cases we need to handle
680
+ // 1. if the account unarchiveChats setting is true
681
+ // a. if the chat is archived, and no further messages have been received -- simple, keep archived
682
+ // b. if the chat was archived, and the user received messages from the other person afterwards
683
+ // then the chat should be marked unarchved --
684
+ // we compare the timestamp of latest message from the other person to determine this
685
+ // 2. if the account unarchiveChats setting is false -- then it doesn't matter,
686
+ // it'll always take an app state action to mark in unarchived -- which we'll get anyway
687
+ const archiveAction = action?.archiveChatAction;
688
+ const isArchived = archiveAction ? archiveAction.archived : type === 'archive';
689
+ // // basically we don't need to fire an "archive" update if the chat is being marked unarchvied
690
+ // // this only applies for the initial sync
691
+ // if(isInitialSync && !isArchived) {
692
+ // isArchived = false
693
+ // }
694
+ const msgRange = !accountSettings?.unarchiveChats ? undefined : archiveAction?.messageRange;
695
+ // logger?.debug({ chat: id, syncAction }, 'message range archive')
696
+ ev.emit('chats.update', [
697
+ {
698
+ id,
699
+ archived: isArchived,
700
+ conditional: getChatUpdateConditional(id, msgRange)
701
+ }
702
+ ]);
703
+ }
704
+ else if (action?.markChatAsReadAction) {
705
+ const markReadAction = action.markChatAsReadAction;
706
+ // basically we don't need to fire an "read" update if the chat is being marked as read
707
+ // because the chat is read by default
708
+ // this only applies for the initial sync
709
+ const isNullUpdate = isInitialSync && markReadAction.read;
710
+ ev.emit('chats.update', [
711
+ {
712
+ id,
713
+ unreadCount: isNullUpdate ? null : !!markReadAction?.read ? 0 : -1,
714
+ conditional: getChatUpdateConditional(id, markReadAction?.messageRange)
715
+ }
716
+ ]);
717
+ }
718
+ else if (action?.deleteMessageForMeAction || type === 'deleteMessageForMe') {
719
+ ev.emit('messages.delete', {
720
+ keys: [
721
+ {
722
+ remoteJid: id,
723
+ id: msgId,
724
+ fromMe: fromMe === '1'
725
+ }
726
+ ]
727
+ });
728
+ }
729
+ else if (action?.contactAction) {
730
+ const results = processContactAction(action.contactAction, id, logger);
731
+ emitSyncActionResults(ev, results);
732
+ }
733
+ else if (action?.pushNameSetting) {
734
+ const name = action?.pushNameSetting?.name;
735
+ if (name && me?.name !== name) {
736
+ ev.emit('creds.update', { me: { ...me, name } });
737
+ }
738
+ }
739
+ else if (action?.pinAction) {
740
+ ev.emit('chats.update', [
741
+ {
742
+ id,
743
+ pinned: action.pinAction?.pinned ? toNumber(action.timestamp) : null,
744
+ conditional: getChatUpdateConditional(id, undefined)
745
+ }
746
+ ]);
747
+ }
748
+ else if (action?.unarchiveChatsSetting) {
749
+ const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats;
750
+ ev.emit('creds.update', { accountSettings: { unarchiveChats } });
751
+ logger?.info(`archive setting updated => '${action.unarchiveChatsSetting.unarchiveChats}'`);
752
+ if (accountSettings) {
753
+ accountSettings.unarchiveChats = unarchiveChats;
754
+ }
755
+ }
756
+ else if (action?.starAction || type === 'star') {
757
+ let starred = action?.starAction?.starred;
758
+ if (typeof starred !== 'boolean') {
759
+ starred = syncAction.index[syncAction.index.length - 1] === '1';
760
+ }
761
+ ev.emit('messages.update', [
762
+ {
763
+ key: { remoteJid: id, id: msgId, fromMe: fromMe === '1' },
764
+ update: { starred }
765
+ }
766
+ ]);
767
+ }
768
+ else if (action?.deleteChatAction || type === 'deleteChat') {
769
+ if (!isInitialSync) {
770
+ ev.emit('chats.delete', [id]);
771
+ }
772
+ }
773
+ else if (action?.clearChatAction || type === 'clearChat') {
774
+ // a clear from another device arrives as an app-state patch with only a
775
+ // messageRange; fold it into chats.update with an empty messages list so
776
+ // listeners can see the chat was cleared. Like delete, only emit outside
777
+ // the initial sync — during the initial sync a clear is just part of the
778
+ // baseline state.
779
+ if (!isInitialSync) {
780
+ ev.emit('chats.update', [
781
+ {
782
+ id,
783
+ messages: [],
784
+ conditional: getChatUpdateConditional(id, action?.clearChatAction?.messageRange)
785
+ }
786
+ ]);
787
+ }
788
+ }
789
+ else if (action?.labelEditAction) {
790
+ const { name, color, deleted, predefinedId } = action.labelEditAction;
791
+ ev.emit('labels.edit', {
792
+ id: id,
793
+ name: name,
794
+ color: color,
795
+ deleted: deleted,
796
+ predefinedId: predefinedId ? String(predefinedId) : undefined
797
+ });
798
+ }
799
+ else if (action?.labelAssociationAction) {
800
+ ev.emit('labels.association', {
801
+ type: action.labelAssociationAction.labeled ? 'add' : 'remove',
802
+ association: type === LabelAssociationType.Chat
803
+ ? {
804
+ type: LabelAssociationType.Chat,
805
+ chatId: syncAction.index[2],
806
+ labelId: syncAction.index[1]
807
+ }
808
+ : {
809
+ type: LabelAssociationType.Message,
810
+ chatId: syncAction.index[2],
811
+ messageId: syncAction.index[3],
812
+ labelId: syncAction.index[1]
813
+ }
814
+ });
815
+ }
816
+ else if (action?.localeSetting?.locale) {
817
+ ev.emit('settings.update', { setting: 'locale', value: action.localeSetting.locale });
818
+ }
819
+ else if (action?.timeFormatAction) {
820
+ ev.emit('settings.update', { setting: 'timeFormat', value: action.timeFormatAction });
821
+ }
822
+ else if (action?.pnForLidChatAction) {
823
+ if (action.pnForLidChatAction.pnJid) {
824
+ ev.emit('lid-mapping.update', { lid: id, pn: action.pnForLidChatAction.pnJid });
825
+ }
826
+ }
827
+ else if (action?.privacySettingRelayAllCalls) {
828
+ ev.emit('settings.update', {
829
+ setting: 'privacySettingRelayAllCalls',
830
+ value: action.privacySettingRelayAllCalls
831
+ });
832
+ }
833
+ else if (action?.statusPrivacy) {
834
+ ev.emit('settings.update', { setting: 'statusPrivacy', value: action.statusPrivacy });
835
+ }
836
+ else if (action?.lockChatAction) {
837
+ ev.emit('chats.lock', { id: id, locked: !!action.lockChatAction.locked });
838
+ }
839
+ else if (action?.privacySettingDisableLinkPreviewsAction) {
840
+ ev.emit('settings.update', {
841
+ setting: 'disableLinkPreviews',
842
+ value: action.privacySettingDisableLinkPreviewsAction
843
+ });
844
+ }
845
+ else if (action?.notificationActivitySettingAction?.notificationActivitySetting) {
846
+ ev.emit('settings.update', {
847
+ setting: 'notificationActivitySetting',
848
+ value: action.notificationActivitySettingAction.notificationActivitySetting
849
+ });
850
+ }
851
+ else if (action?.lidContactAction) {
852
+ ev.emit('contacts.upsert', [
853
+ {
854
+ id: id,
855
+ name: action.lidContactAction.fullName ||
856
+ action.lidContactAction.firstName ||
857
+ action.lidContactAction.username ||
858
+ undefined,
859
+ username: action.lidContactAction.username || undefined,
860
+ lid: id,
861
+ phoneNumber: undefined
862
+ }
863
+ ]);
864
+ }
865
+ else if (action?.privacySettingChannelsPersonalisedRecommendationAction) {
866
+ ev.emit('settings.update', {
867
+ setting: 'channelsPersonalisedRecommendation',
868
+ value: action.privacySettingChannelsPersonalisedRecommendationAction
869
+ });
870
+ }
871
+ else {
872
+ logger?.debug({ syncAction, id }, 'unprocessable update');
873
+ }
874
+ function getChatUpdateConditional(id, msgRange) {
875
+ return isInitialSync
876
+ ? data => {
877
+ const chat = data.historySets.chats[id] || data.chatUpserts[id];
878
+ if (chat) {
879
+ return msgRange ? isValidPatchBasedOnMessageRange(chat, msgRange) : true;
880
+ }
881
+ }
882
+ : undefined;
883
+ }
884
+ function isValidPatchBasedOnMessageRange(chat, msgRange) {
885
+ const lastMsgTimestamp = Number(msgRange?.lastMessageTimestamp || msgRange?.lastSystemMessageTimestamp || 0);
886
+ const chatLastMsgTimestamp = Number(chat?.lastMessageRecvTimestamp || 0);
887
+ return lastMsgTimestamp >= chatLastMsgTimestamp;
888
+ }
889
+ };
890
+ //# sourceMappingURL=chat-utils.js.map