@vinzsocket/baileys 2.8.35 → 3.0.10

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 (410) hide show
  1. package/WAProto/GenerateStatics.sh +2 -2
  2. package/WAProto/WAProto.proto +1339 -493
  3. package/WAProto/fix-imports.js +75 -19
  4. package/WAProto/index.d.ts +5274 -1945
  5. package/WAProto/index.js +81079 -20694
  6. package/lib/Defaults/index.d.ts +75 -0
  7. package/lib/Defaults/index.d.ts.map +1 -0
  8. package/lib/Defaults/index.js +122 -110
  9. package/lib/Defaults/index.js.map +1 -0
  10. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  11. package/lib/Signal/Group/ciphertext-message.d.ts.map +1 -0
  12. package/lib/Signal/Group/ciphertext-message.js +11 -18
  13. package/lib/Signal/Group/ciphertext-message.js.map +1 -0
  14. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  15. package/lib/Signal/Group/group-session-builder.d.ts.map +1 -0
  16. package/lib/Signal/Group/group-session-builder.js +27 -35
  17. package/lib/Signal/Group/group-session-builder.js.map +1 -0
  18. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  19. package/lib/Signal/Group/group_cipher.d.ts.map +1 -0
  20. package/lib/Signal/Group/group_cipher.js +75 -95
  21. package/lib/Signal/Group/group_cipher.js.map +1 -0
  22. package/lib/Signal/Group/index.d.ts +12 -0
  23. package/lib/Signal/Group/index.d.ts.map +1 -0
  24. package/lib/Signal/Group/index.js +12 -79
  25. package/lib/Signal/Group/index.js.map +1 -0
  26. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  27. package/lib/Signal/Group/keyhelper.d.ts.map +1 -0
  28. package/lib/Signal/Group/keyhelper.js +14 -25
  29. package/lib/Signal/Group/keyhelper.js.map +1 -0
  30. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  31. package/lib/Signal/Group/sender-chain-key.d.ts.map +1 -0
  32. package/lib/Signal/Group/sender-chain-key.js +24 -37
  33. package/lib/Signal/Group/sender-chain-key.js.map +1 -0
  34. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  35. package/lib/Signal/Group/sender-key-distribution-message.d.ts.map +1 -0
  36. package/lib/Signal/Group/sender-key-distribution-message.js +62 -61
  37. package/lib/Signal/Group/sender-key-distribution-message.js.map +1 -0
  38. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  39. package/lib/Signal/Group/sender-key-message.d.ts.map +1 -0
  40. package/lib/Signal/Group/sender-key-message.js +64 -68
  41. package/lib/Signal/Group/sender-key-message.js.map +1 -0
  42. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  43. package/lib/Signal/Group/sender-key-name.d.ts.map +1 -0
  44. package/lib/Signal/Group/sender-key-name.js +41 -47
  45. package/lib/Signal/Group/sender-key-name.js.map +1 -0
  46. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  47. package/lib/Signal/Group/sender-key-record.d.ts.map +1 -0
  48. package/lib/Signal/Group/sender-key-record.js +36 -50
  49. package/lib/Signal/Group/sender-key-record.js.map +1 -0
  50. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  51. package/lib/Signal/Group/sender-key-state.d.ts.map +1 -0
  52. package/lib/Signal/Group/sender-key-state.js +76 -96
  53. package/lib/Signal/Group/sender-key-state.js.map +1 -0
  54. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  55. package/lib/Signal/Group/sender-message-key.d.ts.map +1 -0
  56. package/lib/Signal/Group/sender-message-key.js +25 -34
  57. package/lib/Signal/Group/sender-message-key.js.map +1 -0
  58. package/lib/Signal/libsignal.d.ts +5 -0
  59. package/lib/Signal/libsignal.d.ts.map +1 -0
  60. package/lib/Signal/libsignal.js +415 -402
  61. package/lib/Signal/libsignal.js.map +1 -0
  62. package/lib/Signal/lid-mapping.d.ts +23 -0
  63. package/lib/Signal/lid-mapping.d.ts.map +1 -0
  64. package/lib/Signal/lid-mapping.js +273 -111
  65. package/lib/Signal/lid-mapping.js.map +1 -0
  66. package/lib/Socket/Client/index.d.ts +3 -0
  67. package/lib/Socket/Client/index.d.ts.map +1 -0
  68. package/lib/Socket/Client/index.js +3 -27
  69. package/lib/Socket/Client/index.js.map +1 -0
  70. package/lib/Socket/Client/types.d.ts +16 -0
  71. package/lib/Socket/Client/types.d.ts.map +1 -0
  72. package/lib/Socket/Client/types.js +10 -17
  73. package/lib/Socket/Client/types.js.map +1 -0
  74. package/lib/Socket/Client/websocket.d.ts +13 -0
  75. package/lib/Socket/Client/websocket.d.ts.map +1 -0
  76. package/lib/Socket/Client/websocket.js +53 -57
  77. package/lib/Socket/Client/websocket.js.map +1 -0
  78. package/lib/Socket/business.d.ts +217 -0
  79. package/lib/Socket/business.d.ts.map +1 -0
  80. package/lib/Socket/business.js +374 -363
  81. package/lib/Socket/business.js.map +1 -0
  82. package/lib/Socket/chats.d.ts +124 -0
  83. package/lib/Socket/chats.d.ts.map +1 -0
  84. package/lib/Socket/chats.js +1162 -1041
  85. package/lib/Socket/chats.js.map +1 -0
  86. package/lib/Socket/communities.d.ts +273 -0
  87. package/lib/Socket/communities.d.ts.map +1 -0
  88. package/lib/Socket/communities.js +418 -469
  89. package/lib/Socket/communities.js.map +1 -0
  90. package/lib/Socket/groups.d.ts +161 -0
  91. package/lib/Socket/groups.d.ts.map +1 -0
  92. package/lib/Socket/groups.js +337 -393
  93. package/lib/Socket/groups.js.map +1 -0
  94. package/lib/Socket/index.d.ts +260 -0
  95. package/lib/Socket/index.d.ts.map +1 -0
  96. package/lib/Socket/index.js +10 -20
  97. package/lib/Socket/index.js.map +1 -0
  98. package/lib/Socket/messages-recv.d.ts +203 -0
  99. package/lib/Socket/messages-recv.d.ts.map +1 -0
  100. package/lib/Socket/messages-recv.js +1738 -1394
  101. package/lib/Socket/messages-recv.js.map +1 -0
  102. package/lib/Socket/messages-send.d.ts +199 -0
  103. package/lib/Socket/messages-send.d.ts.map +1 -0
  104. package/lib/Socket/messages-send.js +1101 -1245
  105. package/lib/Socket/messages-send.js.map +1 -0
  106. package/lib/Socket/mex.d.ts +3 -0
  107. package/lib/Socket/mex.d.ts.map +1 -0
  108. package/lib/Socket/mex.js +38 -51
  109. package/lib/Socket/mex.js.map +1 -0
  110. package/lib/Socket/newsletter.d.ts +170 -0
  111. package/lib/Socket/newsletter.d.ts.map +1 -0
  112. package/lib/Socket/newsletter.js +175 -225
  113. package/lib/Socket/newsletter.js.map +1 -0
  114. package/lib/Socket/socket.d.ts +59 -0
  115. package/lib/Socket/socket.d.ts.map +1 -0
  116. package/lib/Socket/socket.js +930 -914
  117. package/lib/Socket/socket.js.map +1 -0
  118. package/lib/Types/Auth.d.ts +117 -0
  119. package/lib/Types/Auth.d.ts.map +1 -0
  120. package/lib/Types/Auth.js +2 -5
  121. package/lib/Types/Auth.js.map +1 -0
  122. package/lib/Types/Bussines.d.ts +25 -0
  123. package/lib/Types/Bussines.d.ts.map +1 -0
  124. package/lib/Types/Bussines.js +2 -5
  125. package/lib/Types/Bussines.js.map +1 -0
  126. package/lib/Types/Call.d.ts +15 -0
  127. package/lib/Types/Call.d.ts.map +1 -0
  128. package/lib/Types/Call.js +2 -5
  129. package/lib/Types/Call.js.map +1 -0
  130. package/lib/Types/Chat.d.ts +124 -0
  131. package/lib/Types/Chat.d.ts.map +1 -0
  132. package/lib/Types/Chat.js +7 -7
  133. package/lib/Types/Chat.js.map +1 -0
  134. package/lib/Types/Contact.d.ts +26 -0
  135. package/lib/Types/Contact.d.ts.map +1 -0
  136. package/lib/Types/Contact.js +2 -5
  137. package/lib/Types/Contact.js.map +1 -0
  138. package/lib/Types/Events.d.ts +256 -0
  139. package/lib/Types/Events.d.ts.map +1 -0
  140. package/lib/Types/Events.js +2 -3
  141. package/lib/Types/Events.js.map +1 -0
  142. package/lib/Types/GroupMetadata.d.ts +71 -0
  143. package/lib/Types/GroupMetadata.d.ts.map +1 -0
  144. package/lib/Types/GroupMetadata.js +2 -5
  145. package/lib/Types/GroupMetadata.js.map +1 -0
  146. package/lib/Types/Label.d.ts +47 -0
  147. package/lib/Types/Label.d.ts.map +1 -0
  148. package/lib/Types/Label.js +22 -28
  149. package/lib/Types/Label.js.map +1 -0
  150. package/lib/Types/LabelAssociation.d.ts +30 -0
  151. package/lib/Types/LabelAssociation.d.ts.map +1 -0
  152. package/lib/Types/LabelAssociation.js +4 -10
  153. package/lib/Types/LabelAssociation.js.map +1 -0
  154. package/lib/Types/Message.d.ts +320 -0
  155. package/lib/Types/Message.d.ts.map +1 -0
  156. package/lib/Types/Message.js +8 -20
  157. package/lib/Types/Message.js.map +1 -0
  158. package/lib/Types/Mex.d.ts +141 -0
  159. package/lib/Types/Mex.d.ts.map +1 -0
  160. package/lib/Types/Mex.js +37 -0
  161. package/lib/Types/Mex.js.map +1 -0
  162. package/lib/Types/Product.d.ts +79 -0
  163. package/lib/Types/Product.d.ts.map +1 -0
  164. package/lib/Types/Product.js +2 -5
  165. package/lib/Types/Product.js.map +1 -0
  166. package/lib/Types/Signal.d.ts +87 -0
  167. package/lib/Types/Signal.d.ts.map +1 -0
  168. package/lib/Types/Signal.js +2 -3
  169. package/lib/Types/Signal.js.map +1 -0
  170. package/lib/Types/Socket.d.ts +136 -0
  171. package/lib/Types/Socket.d.ts.map +1 -0
  172. package/lib/Types/Socket.js +3 -4
  173. package/lib/Types/Socket.js.map +1 -0
  174. package/lib/Types/State.d.ts +97 -0
  175. package/lib/Types/State.d.ts.map +1 -0
  176. package/lib/Types/State.js +54 -17
  177. package/lib/Types/State.js.map +1 -0
  178. package/lib/Types/USync.d.ts +26 -0
  179. package/lib/Types/USync.d.ts.map +1 -0
  180. package/lib/Types/USync.js +2 -3
  181. package/lib/Types/USync.js.map +1 -0
  182. package/lib/Types/index.d.ts +65 -0
  183. package/lib/Types/index.d.ts.map +1 -0
  184. package/lib/Types/index.js +24 -165
  185. package/lib/Types/index.js.map +1 -0
  186. package/lib/Utils/auth-utils.d.ts +24 -0
  187. package/lib/Utils/auth-utils.d.ts.map +1 -0
  188. package/lib/Utils/auth-utils.js +272 -461
  189. package/lib/Utils/auth-utils.js.map +1 -0
  190. package/lib/Utils/browser-utils.d.ts +4 -0
  191. package/lib/Utils/browser-utils.d.ts.map +1 -0
  192. package/lib/Utils/browser-utils.js +28 -0
  193. package/lib/Utils/browser-utils.js.map +1 -0
  194. package/lib/Utils/business.d.ts +23 -0
  195. package/lib/Utils/business.d.ts.map +1 -0
  196. package/lib/Utils/business.js +207 -223
  197. package/lib/Utils/business.js.map +1 -0
  198. package/lib/Utils/chat-utils.d.ts +100 -0
  199. package/lib/Utils/chat-utils.d.ts.map +1 -0
  200. package/lib/Utils/chat-utils.js +828 -803
  201. package/lib/Utils/chat-utils.js.map +1 -0
  202. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  203. package/lib/Utils/companion-reg-client-utils.d.ts.map +1 -0
  204. package/lib/Utils/companion-reg-client-utils.js +35 -0
  205. package/lib/Utils/companion-reg-client-utils.js.map +1 -0
  206. package/lib/Utils/crypto.d.ts +37 -0
  207. package/lib/Utils/crypto.d.ts.map +1 -0
  208. package/lib/Utils/crypto.js +87 -144
  209. package/lib/Utils/crypto.js.map +1 -0
  210. package/lib/Utils/decode-wa-message.d.ts +66 -0
  211. package/lib/Utils/decode-wa-message.d.ts.map +1 -0
  212. package/lib/Utils/decode-wa-message.js +291 -262
  213. package/lib/Utils/decode-wa-message.js.map +1 -0
  214. package/lib/Utils/event-buffer.d.ts +36 -0
  215. package/lib/Utils/event-buffer.d.ts.map +1 -0
  216. package/lib/Utils/event-buffer.js +598 -560
  217. package/lib/Utils/event-buffer.js.map +1 -0
  218. package/lib/Utils/generics.d.ts +91 -0
  219. package/lib/Utils/generics.d.ts.map +1 -0
  220. package/lib/Utils/generics.js +329 -363
  221. package/lib/Utils/generics.js.map +1 -0
  222. package/lib/Utils/history.d.ts +24 -0
  223. package/lib/Utils/history.d.ts.map +1 -0
  224. package/lib/Utils/history.js +129 -96
  225. package/lib/Utils/history.js.map +1 -0
  226. package/lib/Utils/identity-change-handler.d.ts +44 -0
  227. package/lib/Utils/identity-change-handler.d.ts.map +1 -0
  228. package/lib/Utils/identity-change-handler.js +50 -0
  229. package/lib/Utils/identity-change-handler.js.map +1 -0
  230. package/lib/Utils/index.d.ts +22 -0
  231. package/lib/Utils/index.d.ts.map +1 -0
  232. package/lib/Utils/index.js +22 -236
  233. package/lib/Utils/index.js.map +1 -0
  234. package/lib/Utils/link-preview.d.ts +21 -0
  235. package/lib/Utils/link-preview.d.ts.map +1 -0
  236. package/lib/Utils/link-preview.js +69 -86
  237. package/lib/Utils/link-preview.js.map +1 -0
  238. package/lib/Utils/logger.d.ts +12 -0
  239. package/lib/Utils/logger.d.ts.map +1 -0
  240. package/lib/Utils/logger.js +3 -11
  241. package/lib/Utils/logger.js.map +1 -0
  242. package/lib/Utils/lt-hash.d.ts +8 -0
  243. package/lib/Utils/lt-hash.d.ts.map +1 -0
  244. package/lib/Utils/lt-hash.js +2 -52
  245. package/lib/Utils/lt-hash.js.map +1 -0
  246. package/lib/Utils/make-mutex.d.ts +9 -0
  247. package/lib/Utils/make-mutex.d.ts.map +1 -0
  248. package/lib/Utils/make-mutex.js +30 -43
  249. package/lib/Utils/make-mutex.js.map +1 -0
  250. package/lib/Utils/message-retry-manager.d.ts +115 -0
  251. package/lib/Utils/message-retry-manager.d.ts.map +1 -0
  252. package/lib/Utils/message-retry-manager.js +259 -162
  253. package/lib/Utils/message-retry-manager.js.map +1 -0
  254. package/lib/Utils/messages-media.d.ts +133 -0
  255. package/lib/Utils/messages-media.d.ts.map +1 -0
  256. package/lib/Utils/messages-media.js +732 -711
  257. package/lib/Utils/messages-media.js.map +1 -0
  258. package/lib/Utils/messages.d.ts +91 -0
  259. package/lib/Utils/messages.d.ts.map +1 -0
  260. package/lib/Utils/messages.js +817 -803
  261. package/lib/Utils/messages.js.map +1 -0
  262. package/lib/Utils/noise-handler.d.ts +20 -0
  263. package/lib/Utils/noise-handler.d.ts.map +1 -0
  264. package/lib/Utils/noise-handler.js +195 -168
  265. package/lib/Utils/noise-handler.js.map +1 -0
  266. package/lib/Utils/offline-node-processor.d.ts +17 -0
  267. package/lib/Utils/offline-node-processor.d.ts.map +1 -0
  268. package/lib/Utils/offline-node-processor.js +40 -0
  269. package/lib/Utils/offline-node-processor.js.map +1 -0
  270. package/lib/Utils/pre-key-manager.d.ts +28 -0
  271. package/lib/Utils/pre-key-manager.d.ts.map +1 -0
  272. package/lib/Utils/pre-key-manager.js +106 -0
  273. package/lib/Utils/pre-key-manager.js.map +1 -0
  274. package/lib/Utils/process-message.d.ts +60 -0
  275. package/lib/Utils/process-message.d.ts.map +1 -0
  276. package/lib/Utils/process-message.js +601 -424
  277. package/lib/Utils/process-message.js.map +1 -0
  278. package/lib/Utils/reporting-utils.d.ts +11 -0
  279. package/lib/Utils/reporting-utils.d.ts.map +1 -0
  280. package/lib/Utils/reporting-utils.js +258 -0
  281. package/lib/Utils/reporting-utils.js.map +1 -0
  282. package/lib/Utils/signal.d.ts +47 -0
  283. package/lib/Utils/signal.d.ts.map +1 -0
  284. package/lib/Utils/signal.js +180 -206
  285. package/lib/Utils/signal.js.map +1 -0
  286. package/lib/Utils/stanza-ack.d.ts +11 -0
  287. package/lib/Utils/stanza-ack.d.ts.map +1 -0
  288. package/lib/Utils/stanza-ack.js +38 -0
  289. package/lib/Utils/stanza-ack.js.map +1 -0
  290. package/lib/Utils/sync-action-utils.d.ts +19 -0
  291. package/lib/Utils/sync-action-utils.d.ts.map +1 -0
  292. package/lib/Utils/sync-action-utils.js +49 -0
  293. package/lib/Utils/sync-action-utils.js.map +1 -0
  294. package/lib/Utils/tc-token-utils.d.ts +37 -0
  295. package/lib/Utils/tc-token-utils.d.ts.map +1 -0
  296. package/lib/Utils/tc-token-utils.js +163 -0
  297. package/lib/Utils/tc-token-utils.js.map +1 -0
  298. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  299. package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
  300. package/lib/Utils/use-multi-file-auth-state.js +98 -101
  301. package/lib/Utils/use-multi-file-auth-state.js.map +1 -0
  302. package/lib/Utils/validate-connection.d.ts +11 -0
  303. package/lib/Utils/validate-connection.d.ts.map +1 -0
  304. package/lib/Utils/validate-connection.js +191 -188
  305. package/lib/Utils/validate-connection.js.map +1 -0
  306. package/lib/WABinary/constants.d.ts +28 -0
  307. package/lib/WABinary/constants.d.ts.map +1 -0
  308. package/lib/WABinary/constants.js +1295 -36
  309. package/lib/WABinary/constants.js.map +1 -0
  310. package/lib/WABinary/decode.d.ts +7 -0
  311. package/lib/WABinary/decode.d.ts.map +1 -0
  312. package/lib/WABinary/decode.js +254 -232
  313. package/lib/WABinary/decode.js.map +1 -0
  314. package/lib/WABinary/encode.d.ts +3 -0
  315. package/lib/WABinary/encode.d.ts.map +1 -0
  316. package/lib/WABinary/encode.js +208 -210
  317. package/lib/WABinary/encode.js.map +1 -0
  318. package/lib/WABinary/generic-utils.d.ts +15 -0
  319. package/lib/WABinary/generic-utils.d.ts.map +1 -0
  320. package/lib/WABinary/generic-utils.js +97 -106
  321. package/lib/WABinary/generic-utils.js.map +1 -0
  322. package/lib/WABinary/index.d.ts +6 -0
  323. package/lib/WABinary/index.d.ts.map +1 -0
  324. package/lib/WABinary/index.js +6 -60
  325. package/lib/WABinary/index.js.map +1 -0
  326. package/lib/WABinary/jid-utils.d.ts +48 -0
  327. package/lib/WABinary/jid-utils.d.ts.map +1 -0
  328. package/lib/WABinary/jid-utils.js +83 -70
  329. package/lib/WABinary/jid-utils.js.map +1 -0
  330. package/lib/WABinary/types.d.ts +19 -0
  331. package/lib/WABinary/types.d.ts.map +1 -0
  332. package/lib/WABinary/types.js +2 -4
  333. package/lib/WABinary/types.js.map +1 -0
  334. package/lib/WAM/BinaryInfo.d.ts +9 -0
  335. package/lib/WAM/BinaryInfo.d.ts.map +1 -0
  336. package/lib/WAM/BinaryInfo.js +9 -16
  337. package/lib/WAM/BinaryInfo.js.map +1 -0
  338. package/lib/WAM/constants.d.ts +40 -0
  339. package/lib/WAM/constants.d.ts.map +1 -0
  340. package/lib/WAM/constants.js +22853 -13001
  341. package/lib/WAM/constants.js.map +1 -0
  342. package/lib/WAM/encode.d.ts +3 -0
  343. package/lib/WAM/encode.d.ts.map +1 -0
  344. package/lib/WAM/encode.js +133 -133
  345. package/lib/WAM/encode.js.map +1 -0
  346. package/lib/WAM/index.d.ts +4 -0
  347. package/lib/WAM/index.d.ts.map +1 -0
  348. package/lib/WAM/index.js +4 -38
  349. package/lib/WAM/index.js.map +1 -0
  350. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  351. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
  352. package/lib/WAUSync/Protocols/USyncContactProtocol.js +50 -34
  353. package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
  354. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  355. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
  356. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +50 -61
  357. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
  358. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  359. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
  360. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +25 -32
  361. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
  362. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  363. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
  364. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +35 -40
  365. package/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
  366. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  367. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts.map +1 -0
  368. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +23 -29
  369. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js.map +1 -0
  370. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  371. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
  372. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +47 -64
  373. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
  374. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  375. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
  376. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +27 -26
  377. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
  378. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  379. package/lib/WAUSync/Protocols/index.d.ts.map +1 -0
  380. package/lib/WAUSync/Protocols/index.js +6 -60
  381. package/lib/WAUSync/Protocols/index.js.map +1 -0
  382. package/lib/WAUSync/USyncQuery.d.ts +30 -0
  383. package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
  384. package/lib/WAUSync/USyncQuery.js +94 -97
  385. package/lib/WAUSync/USyncQuery.js.map +1 -0
  386. package/lib/WAUSync/USyncUser.d.ts +17 -0
  387. package/lib/WAUSync/USyncUser.d.ts.map +1 -0
  388. package/lib/WAUSync/USyncUser.js +30 -29
  389. package/lib/WAUSync/USyncUser.js.map +1 -0
  390. package/lib/WAUSync/index.d.ts +4 -0
  391. package/lib/WAUSync/index.d.ts.map +1 -0
  392. package/lib/WAUSync/index.js +4 -38
  393. package/lib/WAUSync/index.js.map +1 -0
  394. package/lib/index.d.ts +12 -0
  395. package/lib/index.d.ts.map +1 -0
  396. package/lib/index.js +11 -114
  397. package/lib/index.js.map +1 -0
  398. package/package.json +47 -20
  399. package/README.md +0 -1315
  400. package/lib/Signal/Group/queue-job.js +0 -61
  401. package/lib/Store/index.js +0 -49
  402. package/lib/Store/make-cache-manager-store.js +0 -106
  403. package/lib/Store/make-in-memory-store.js +0 -497
  404. package/lib/Store/make-ordered-dictionary.js +0 -83
  405. package/lib/Store/object-repository.js +0 -32
  406. package/lib/Types/Newsletter.js +0 -37
  407. package/lib/Utils/baileys-event-stream.js +0 -74
  408. package/lib/Utils/extended-messages.js +0 -285
  409. package/lib/Utils/lid-resolver.js +0 -81
  410. package/lib/Utils/mention.js +0 -137
@@ -1,418 +1,431 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.makeLibSignalRepository = makeLibSignalRepository;
7
- var libsignal = _interopRequireWildcard(require("libsignal"));
8
- var _lruCache = require("lru-cache");
9
- var _index = require("../Utils/index.js");
10
- var _index2 = require("../WABinary/index.js");
11
- var _senderKeyName = require("./Group/sender-key-name.js");
12
- var _senderKeyRecord = require("./Group/sender-key-record.js");
13
- var _index3 = require("./Group/index.js");
14
- var _lidMapping = require("./lid-mapping.js");
15
- function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
16
- /* @ts-ignore */
17
-
18
- /* @ts-ignore */
19
-
20
- function makeLibSignalRepository(auth, onWhatsAppFunc) {
21
- const lidMapping = new _lidMapping.LIDMappingStore(auth.keys, onWhatsAppFunc);
22
- const storage = signalStorage(auth, lidMapping);
23
- const parsedKeys = auth.keys;
24
- function isLikelySyncMessage(addr) {
25
- const key = addr.toString();
26
- // Only bypass for WhatsApp system addresses, not regular user contacts
27
- // Be very specific about sync service patterns
28
- return key.includes('@lid.whatsapp.net') ||
29
- // WhatsApp system messages
30
- key.includes('@broadcast') ||
31
- // Broadcast messages
32
- key.includes('@newsletter');
33
- }
34
- // Simple operation-level deduplication (5 minutes)
35
- const recentMigrations = new _lruCache.LRUCache({
36
- max: 500,
37
- ttl: 5 * 60 * 1000
38
- });
39
- const repository = {
40
- decryptGroupMessage({
41
- group,
42
- authorJid,
43
- msg
44
- }) {
45
- const senderName = jidToSignalSenderKeyName(group, authorJid);
46
- const cipher = new _index3.GroupCipher(storage, senderName);
47
- // Use transaction to ensure atomicity
48
- return parsedKeys.transaction(async () => {
49
- return cipher.decrypt(msg);
50
- }, group);
51
- },
52
- async processSenderKeyDistributionMessage({
53
- item,
54
- authorJid
55
- }) {
56
- const builder = new _index3.GroupSessionBuilder(storage);
57
- if (!item.groupId) {
58
- throw new Error('Group ID is required for sender key distribution message');
59
- }
60
- const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
61
- const senderMsg = new _index3.SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
62
- const senderNameStr = senderName.toString();
63
- const {
64
- [senderNameStr]: senderKey
65
- } = await auth.keys.get('sender-key', [senderNameStr]);
66
- if (!senderKey) {
67
- await storage.storeSenderKey(senderName, new _senderKeyRecord.SenderKeyRecord());
68
- }
69
- return parsedKeys.transaction(async () => {
70
- const {
71
- [senderNameStr]: senderKey
72
- } = await auth.keys.get('sender-key', [senderNameStr]);
73
- if (!senderKey) {
74
- await storage.storeSenderKey(senderName, new _senderKeyRecord.SenderKeyRecord());
1
+ // @ts-ignore
2
+ import * as libsignal from 'libsignal';
3
+ // @ts-ignore
4
+ import { PreKeyWhisperMessage } from 'libsignal/src/protobufs.js';
5
+ import { LRUCache } from 'lru-cache';
6
+ import { generateSignalPubKey } from '../Utils/index.js';
7
+ import { isHostedLidUser, isHostedPnUser, isLidUser, isPnUser, jidDecode, transferDevice, WAJIDDomains } from '../WABinary/index.js';
8
+ import { SenderKeyName } from './Group/sender-key-name.js';
9
+ import { SenderKeyRecord } from './Group/sender-key-record.js';
10
+ import { GroupCipher, GroupSessionBuilder, SenderKeyDistributionMessage } from './Group/index.js';
11
+ import { LIDMappingStore } from './lid-mapping.js';
12
+ /** Extract identity key from PreKeyWhisperMessage for identity change detection */
13
+ function extractIdentityFromPkmsg(ciphertext) {
14
+ try {
15
+ if (!ciphertext || ciphertext.length < 2) {
16
+ return undefined;
75
17
  }
76
- await builder.process(senderName, senderMsg);
77
- }, item.groupId);
78
- },
79
- async decryptMessage({
80
- jid,
81
- type,
82
- ciphertext
83
- }) {
84
- const addr = jidToSignalProtocolAddress(jid);
85
- const session = new libsignal.SessionCipher(storage, addr);
86
- async function doDecrypt() {
87
- let result;
88
- switch (type) {
89
- case 'pkmsg':
90
- result = await session.decryptPreKeyWhisperMessage(ciphertext);
91
- break;
92
- case 'msg':
93
- result = await session.decryptWhisperMessage(ciphertext);
94
- break;
18
+ // Version byte check (version 3)
19
+ const version = ciphertext[0];
20
+ if ((version & 0xf) !== 3) {
21
+ return undefined;
95
22
  }
96
- return result;
97
- }
98
- if (isLikelySyncMessage(addr)) {
99
- // If it's a sync message, we can skip the transaction
100
- // as it is likely to be a system message that doesn't require strict atomicity
101
- return await doDecrypt();
102
- }
103
- // If it's not a sync message, we need to ensure atomicity
104
- // For regular messages, we use a transaction to ensure atomicity
105
- return parsedKeys.transaction(async () => {
106
- return await doDecrypt();
107
- }, jid);
108
- },
109
- async encryptMessage({
110
- jid,
111
- data
112
- }) {
113
- // LID SINGLE SOURCE OF TRUTH: Always prefer LID when available
114
- let encryptionJid = jid;
115
- // Check for LID mapping and use it if session exists
116
- if (jid.includes('@s.whatsapp.net')) {
117
- const lidForPN = await lidMapping.getLIDForPN(jid);
118
- if (lidForPN?.includes('@lid')) {
119
- const lidAddr = jidToSignalProtocolAddress(lidForPN);
120
- const {
121
- [lidAddr.toString()]: lidSession
122
- } = await auth.keys.get('session', [lidAddr.toString()]);
123
- if (lidSession) {
124
- // LID session exists, use it
125
- encryptionJid = lidForPN;
126
- } else {
127
- // Try to migrate if PN session exists
128
- const pnAddr = jidToSignalProtocolAddress(jid);
129
- const {
130
- [pnAddr.toString()]: pnSession
131
- } = await auth.keys.get('session', [pnAddr.toString()]);
132
- if (pnSession) {
133
- // Migrate PN to LID
134
- await repository.migrateSession(jid, lidForPN);
135
- encryptionJid = lidForPN;
136
- }
137
- }
23
+ // Parse protobuf (skip version byte)
24
+ const preKeyProto = PreKeyWhisperMessage.decode(ciphertext.slice(1));
25
+ if (preKeyProto.identityKey?.length === 33) {
26
+ return new Uint8Array(preKeyProto.identityKey);
138
27
  }
139
- }
140
- const addr = jidToSignalProtocolAddress(encryptionJid);
141
- const cipher = new libsignal.SessionCipher(storage, addr);
142
- // Use transaction to ensure atomicity
143
- return parsedKeys.transaction(async () => {
144
- const {
145
- type: sigType,
146
- body
147
- } = await cipher.encrypt(data);
148
- const type = sigType === 3 ? 'pkmsg' : 'msg';
149
- return {
150
- type,
151
- ciphertext: Buffer.from(body, 'binary')
152
- };
153
- }, jid);
154
- },
155
- async encryptGroupMessage({
156
- group,
157
- meId,
158
- data
159
- }) {
160
- const senderName = jidToSignalSenderKeyName(group, meId);
161
- const builder = new _index3.GroupSessionBuilder(storage);
162
- const senderNameStr = senderName.toString();
163
- return parsedKeys.transaction(async () => {
164
- const {
165
- [senderNameStr]: senderKey
166
- } = await auth.keys.get('sender-key', [senderNameStr]);
28
+ return undefined;
29
+ }
30
+ catch {
31
+ return undefined;
32
+ }
33
+ }
34
+ export function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
35
+ const lidMapping = new LIDMappingStore(auth.keys, logger, pnToLIDFunc);
36
+ const storage = signalStorage(auth, lidMapping);
37
+ const parsedKeys = auth.keys;
38
+ const migratedSessionCache = new LRUCache({
39
+ ttl: 3 * 24 * 60 * 60 * 1000, // 7 days
40
+ ttlAutopurge: true,
41
+ updateAgeOnGet: true
42
+ });
43
+ const ensureSenderKeyAndCreateSkdm = async (group, meId) => {
44
+ const senderName = jidToSignalSenderKeyName(group, meId);
45
+ const senderNameStr = senderName.toString();
46
+ const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
167
47
  if (!senderKey) {
168
- await storage.storeSenderKey(senderName, new _senderKeyRecord.SenderKeyRecord());
169
- }
170
- const senderKeyDistributionMessage = await builder.create(senderName);
171
- const session = new _index3.GroupCipher(storage, senderName);
172
- const ciphertext = await session.encrypt(data);
173
- return {
174
- ciphertext,
175
- senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
176
- };
177
- }, group);
178
- },
179
- async injectE2ESession({
180
- jid,
181
- session
182
- }) {
183
- const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
184
- return parsedKeys.transaction(async () => {
185
- await cipher.initOutgoing(session);
186
- }, jid);
187
- },
188
- jidToSignalProtocolAddress(jid) {
189
- return jidToSignalProtocolAddress(jid).toString();
190
- },
191
- async storeLIDPNMapping(lid, pn) {
192
- await lidMapping.storeLIDPNMapping(lid, pn);
193
- },
194
- getLIDMappingStore() {
195
- return lidMapping;
196
- },
197
- async validateSession(jid) {
198
- try {
199
- const addr = jidToSignalProtocolAddress(jid);
200
- const session = await storage.loadSession(addr.toString());
201
- if (!session) {
202
- return {
203
- exists: false,
204
- reason: 'no session'
205
- };
48
+ await storage.storeSenderKey(senderName, new SenderKeyRecord());
206
49
  }
207
- if (!session.haveOpenSession()) {
208
- return {
209
- exists: false,
210
- reason: 'no open session'
211
- };
212
- }
213
- return {
214
- exists: true
215
- };
216
- } catch (error) {
217
- return {
218
- exists: false,
219
- reason: 'validation error'
220
- };
221
- }
222
- },
223
- async deleteSession(jid) {
224
- const addr = jidToSignalProtocolAddress(jid);
225
- return parsedKeys.transaction(async () => {
226
- await auth.keys.set({
227
- session: {
228
- [addr.toString()]: null
229
- }
230
- });
231
- }, jid);
232
- },
233
- async migrateSession(fromJid, toJid) {
234
- // Only migrate PN → LID
235
- if (!fromJid.includes('@s.whatsapp.net') || !toJid.includes('@lid')) {
236
- return;
237
- }
238
- const fromDecoded = (0, _index2.jidDecode)(fromJid);
239
- const toDecoded = (0, _index2.jidDecode)(toJid);
240
- if (!fromDecoded || !toDecoded) return;
241
- const deviceId = fromDecoded.device || 0;
242
- const migrationKey = `${fromDecoded.user}.${deviceId}→${toDecoded.user}.${deviceId}`;
243
- // Check if recently migrated (5 min window)
244
- if (recentMigrations.has(migrationKey)) {
245
- return;
246
- }
247
- // Check if LID session already exists
248
- const lidAddr = jidToSignalProtocolAddress(toJid);
249
- const {
250
- [lidAddr.toString()]: lidExists
251
- } = await auth.keys.get('session', [lidAddr.toString()]);
252
- if (lidExists) {
253
- recentMigrations.set(migrationKey, true);
254
- return;
255
- }
256
- return parsedKeys.transaction(async () => {
257
- // Store mapping
258
- await lidMapping.storeLIDPNMapping(toJid, fromJid);
259
- // Load and copy session
260
- const fromAddr = jidToSignalProtocolAddress(fromJid);
261
- const fromSession = await storage.loadSession(fromAddr.toString());
262
- if (fromSession?.haveOpenSession()) {
263
- // Deep copy session to prevent reference issues
264
- const sessionBytes = fromSession.serialize();
265
- const copiedSession = libsignal.SessionRecord.deserialize(sessionBytes);
266
- // Store at LID address
267
- await storage.storeSession(lidAddr.toString(), copiedSession);
268
- // Delete PN session - maintain single encryption layer
269
- await auth.keys.set({
270
- session: {
271
- [fromAddr.toString()]: null
50
+ const skdm = await new GroupSessionBuilder(storage).create(senderName);
51
+ return { senderName, skdm };
52
+ };
53
+ const repository = {
54
+ decryptGroupMessage({ group, authorJid, msg }) {
55
+ const senderName = jidToSignalSenderKeyName(group, authorJid);
56
+ const cipher = new GroupCipher(storage, senderName);
57
+ // Use transaction to ensure atomicity
58
+ return parsedKeys.transaction(async () => {
59
+ return cipher.decrypt(msg);
60
+ }, group);
61
+ },
62
+ async processSenderKeyDistributionMessage({ item, authorJid }) {
63
+ const builder = new GroupSessionBuilder(storage);
64
+ if (!item.groupId) {
65
+ throw new Error('Group ID is required for sender key distribution message');
66
+ }
67
+ const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
68
+ const senderMsg = new SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
69
+ const senderNameStr = senderName.toString();
70
+ const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
71
+ if (!senderKey) {
72
+ await storage.storeSenderKey(senderName, new SenderKeyRecord());
73
+ }
74
+ return parsedKeys.transaction(async () => {
75
+ const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
76
+ if (!senderKey) {
77
+ await storage.storeSenderKey(senderName, new SenderKeyRecord());
78
+ }
79
+ await builder.process(senderName, senderMsg);
80
+ }, item.groupId);
81
+ },
82
+ async decryptMessage({ jid, type, ciphertext }) {
83
+ const addr = jidToSignalProtocolAddress(jid);
84
+ const session = new libsignal.SessionCipher(storage, addr);
85
+ // Extract and save sender's identity key before decryption for identity change detection
86
+ if (type === 'pkmsg') {
87
+ const identityKey = extractIdentityFromPkmsg(ciphertext);
88
+ if (identityKey) {
89
+ const addrStr = addr.toString();
90
+ const identityChanged = await storage.saveIdentity(addrStr, identityKey);
91
+ if (identityChanged) {
92
+ logger.info({ jid, addr: addrStr }, 'identity key changed or new contact, session will be re-established');
93
+ }
94
+ }
95
+ }
96
+ async function doDecrypt() {
97
+ let result;
98
+ switch (type) {
99
+ case 'pkmsg':
100
+ result = await session.decryptPreKeyWhisperMessage(ciphertext);
101
+ break;
102
+ case 'msg':
103
+ result = await session.decryptWhisperMessage(ciphertext);
104
+ break;
105
+ }
106
+ return result;
107
+ }
108
+ // If it's not a sync message, we need to ensure atomicity
109
+ // For regular messages, we use a transaction to ensure atomicity
110
+ return parsedKeys.transaction(async () => {
111
+ return await doDecrypt();
112
+ }, jid);
113
+ },
114
+ async encryptMessage({ jid, data }) {
115
+ const addr = jidToSignalProtocolAddress(jid);
116
+ const cipher = new libsignal.SessionCipher(storage, addr);
117
+ // Use transaction to ensure atomicity
118
+ return parsedKeys.transaction(async () => {
119
+ const { type: sigType, body } = await cipher.encrypt(data);
120
+ const type = sigType === 3 ? 'pkmsg' : 'msg';
121
+ return { type, ciphertext: Buffer.from(body, 'binary') };
122
+ }, jid);
123
+ },
124
+ async encryptGroupMessage({ group, meId, data }) {
125
+ return parsedKeys.transaction(async () => {
126
+ const { senderName, skdm } = await ensureSenderKeyAndCreateSkdm(group, meId);
127
+ const ciphertext = await new GroupCipher(storage, senderName).encrypt(data);
128
+ return { ciphertext, senderKeyDistributionMessage: skdm.serialize() };
129
+ }, group);
130
+ },
131
+ async getSenderKeyDistributionMessage({ group, meId }) {
132
+ return parsedKeys.transaction(async () => {
133
+ const { skdm } = await ensureSenderKeyAndCreateSkdm(group, meId);
134
+ return skdm.serialize();
135
+ }, group);
136
+ },
137
+ async hasSenderKey({ group, meId }) {
138
+ const senderName = jidToSignalSenderKeyName(group, meId).toString();
139
+ const { [senderName]: key } = await auth.keys.get('sender-key', [senderName]);
140
+ return !!key;
141
+ },
142
+ async getSessionInfo(jid) {
143
+ const addr = jidToSignalProtocolAddress(jid).toString();
144
+ const session = (await storage.loadSession(addr));
145
+ if (!session) {
146
+ return null;
147
+ }
148
+ const open = session.getOpenSession?.();
149
+ const baseKey = open?.indexInfo?.baseKey;
150
+ const registrationId = open?.registrationId;
151
+ if (!baseKey || typeof registrationId !== 'number') {
152
+ return null;
153
+ }
154
+ return { baseKey: new Uint8Array(baseKey), registrationId };
155
+ },
156
+ async injectE2ESession({ jid, session }) {
157
+ logger.trace({ jid }, 'injecting E2EE session');
158
+ const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
159
+ return parsedKeys.transaction(async () => {
160
+ // libsignal runtime accepts an absent prekey (initOutgoing checks `device.preKey && ...`)
161
+ // but the bundled .d.ts marks it required.
162
+ await cipher.initOutgoing(session);
163
+ }, jid);
164
+ },
165
+ jidToSignalProtocolAddress(jid) {
166
+ return jidToSignalProtocolAddress(jid).toString();
167
+ },
168
+ // Optimized direct access to LID mapping store
169
+ lidMapping,
170
+ async validateSession(jid) {
171
+ try {
172
+ const addr = jidToSignalProtocolAddress(jid);
173
+ const session = await storage.loadSession(addr.toString());
174
+ if (!session) {
175
+ return { exists: false, reason: 'no session' };
176
+ }
177
+ if (!session.haveOpenSession()) {
178
+ return { exists: false, reason: 'no open session' };
179
+ }
180
+ return { exists: true };
181
+ }
182
+ catch (error) {
183
+ return { exists: false, reason: 'validation error' };
184
+ }
185
+ },
186
+ async deleteSession(jids) {
187
+ if (!jids.length)
188
+ return;
189
+ // Convert JIDs to signal addresses and prepare for bulk deletion
190
+ const sessionUpdates = {};
191
+ jids.forEach(jid => {
192
+ const addr = jidToSignalProtocolAddress(jid);
193
+ sessionUpdates[addr.toString()] = null;
194
+ });
195
+ // Single transaction for all deletions
196
+ return parsedKeys.transaction(async () => {
197
+ await auth.keys.set({ session: sessionUpdates });
198
+ }, `delete-${jids.length}-sessions`);
199
+ },
200
+ close() {
201
+ migratedSessionCache.clear();
202
+ lidMapping.close();
203
+ },
204
+ async migrateSession(fromJid, toJid) {
205
+ // TODO: use usync to handle this entire mess
206
+ if (!fromJid || (!isLidUser(toJid) && !isHostedLidUser(toJid)))
207
+ return { migrated: 0, skipped: 0, total: 0 };
208
+ // Only support PN to LID migration
209
+ if (!isPnUser(fromJid) && !isHostedPnUser(fromJid)) {
210
+ return { migrated: 0, skipped: 0, total: 1 };
272
211
  }
273
- });
212
+ const { user } = jidDecode(fromJid);
213
+ logger.debug({ fromJid }, 'bulk device migration - loading all user devices');
214
+ // Get user's device list from storage
215
+ const { [user]: userDevices } = await parsedKeys.get('device-list', [user]);
216
+ if (!userDevices) {
217
+ return { migrated: 0, skipped: 0, total: 0 };
218
+ }
219
+ const { device: fromDevice } = jidDecode(fromJid);
220
+ const fromDeviceStr = fromDevice?.toString() || '0';
221
+ if (!userDevices.includes(fromDeviceStr)) {
222
+ userDevices.push(fromDeviceStr);
223
+ }
224
+ // Filter out cached devices before database fetch
225
+ const uncachedDevices = userDevices.filter(device => {
226
+ const deviceKey = `${user}.${device}`;
227
+ return !migratedSessionCache.has(deviceKey);
228
+ });
229
+ // Bulk check session existence only for uncached devices
230
+ const deviceSessionKeys = uncachedDevices.map(device => `${user}.${device}`);
231
+ const existingSessions = await parsedKeys.get('session', deviceSessionKeys);
232
+ // Step 3: Convert existing sessions to JIDs (only migrate sessions that exist)
233
+ const deviceJids = [];
234
+ for (const [sessionKey, sessionData] of Object.entries(existingSessions)) {
235
+ if (sessionData) {
236
+ // Session exists in storage
237
+ const deviceStr = sessionKey.split('.')[1];
238
+ if (!deviceStr)
239
+ continue;
240
+ const deviceNum = parseInt(deviceStr);
241
+ let jid = deviceNum === 0 ? `${user}@s.whatsapp.net` : `${user}:${deviceNum}@s.whatsapp.net`;
242
+ if (deviceNum === 99) {
243
+ jid = `${user}:99@hosted`;
244
+ }
245
+ deviceJids.push(jid);
246
+ }
247
+ }
248
+ logger.debug({
249
+ fromJid,
250
+ totalDevices: userDevices.length,
251
+ devicesWithSessions: deviceJids.length,
252
+ devices: deviceJids
253
+ }, 'bulk device migration complete - all user devices processed');
254
+ // Single transaction for all migrations
255
+ return parsedKeys.transaction(async () => {
256
+ const migrationOps = deviceJids.map(jid => {
257
+ const lidWithDevice = transferDevice(jid, toJid);
258
+ const fromDecoded = jidDecode(jid);
259
+ const toDecoded = jidDecode(lidWithDevice);
260
+ return {
261
+ fromJid: jid,
262
+ toJid: lidWithDevice,
263
+ pnUser: fromDecoded.user,
264
+ lidUser: toDecoded.user,
265
+ deviceId: fromDecoded.device || 0,
266
+ fromAddr: jidToSignalProtocolAddress(jid),
267
+ toAddr: jidToSignalProtocolAddress(lidWithDevice)
268
+ };
269
+ });
270
+ const totalOps = migrationOps.length;
271
+ let migratedCount = 0;
272
+ // Bulk fetch PN sessions - already exist (verified during device discovery)
273
+ const pnAddrStrings = Array.from(new Set(migrationOps.map(op => op.fromAddr.toString())));
274
+ const pnSessions = await parsedKeys.get('session', pnAddrStrings);
275
+ // Prepare bulk session updates (PN → LID migration + deletion)
276
+ const sessionUpdates = {};
277
+ for (const op of migrationOps) {
278
+ const pnAddrStr = op.fromAddr.toString();
279
+ const lidAddrStr = op.toAddr.toString();
280
+ const pnSession = pnSessions[pnAddrStr];
281
+ if (pnSession) {
282
+ // Session exists (guaranteed from device discovery)
283
+ const fromSession = libsignal.SessionRecord.deserialize(pnSession);
284
+ if (fromSession.haveOpenSession()) {
285
+ // Queue for bulk update: copy to LID, delete from PN
286
+ sessionUpdates[lidAddrStr] = fromSession.serialize();
287
+ sessionUpdates[pnAddrStr] = null;
288
+ migratedCount++;
289
+ }
290
+ }
291
+ }
292
+ // Single bulk session update for all migrations
293
+ if (Object.keys(sessionUpdates).length > 0) {
294
+ await parsedKeys.set({ session: sessionUpdates });
295
+ logger.debug({ migratedSessions: migratedCount }, 'bulk session migration complete');
296
+ // Cache device-level migrations
297
+ for (const op of migrationOps) {
298
+ if (sessionUpdates[op.toAddr.toString()]) {
299
+ const deviceKey = `${op.pnUser}.${op.deviceId}`;
300
+ migratedSessionCache.set(deviceKey, true);
301
+ }
302
+ }
303
+ }
304
+ const skippedCount = totalOps - migratedCount;
305
+ return { migrated: migratedCount, skipped: skippedCount, total: totalOps };
306
+ }, `migrate-${deviceJids.length}-sessions-${jidDecode(toJid)?.user}`);
274
307
  }
275
- recentMigrations.set(migrationKey, true);
276
- }, fromJid);
277
- },
278
- async encryptMessageWithWire({
279
- encryptionJid,
280
- wireJid,
281
- data
282
- }) {
283
- const result = await repository.encryptMessage({
284
- jid: encryptionJid,
285
- data
286
- });
287
- return {
288
- ...result,
289
- wireJid
290
- };
291
- },
292
- destroy() {
293
- recentMigrations.clear();
294
- }
295
- };
296
- return repository;
308
+ };
309
+ return repository;
297
310
  }
298
- const jidToSignalProtocolAddress = jid => {
299
- const decoded = (0, _index2.jidDecode)(jid);
300
- const {
301
- user,
302
- device,
303
- server
304
- } = decoded;
305
- // LID addresses get _1 suffix for Signal protocol
306
- const signalUser = server === 'lid' ? `${user}_1` : user;
307
- const finalDevice = device || 0;
308
- return new libsignal.ProtocolAddress(signalUser, finalDevice);
311
+ const jidToSignalProtocolAddress = (jid) => {
312
+ const decoded = jidDecode(jid);
313
+ const { user, device, server, domainType } = decoded;
314
+ if (!user) {
315
+ throw new Error(`JID decoded but user is empty: "${jid}" -> user: "${user}", server: "${server}", device: ${device}`);
316
+ }
317
+ const signalUser = domainType !== WAJIDDomains.WHATSAPP ? `${user}_${domainType}` : user;
318
+ const finalDevice = device || 0;
319
+ if (device === 99 && decoded.server !== 'hosted' && decoded.server !== 'hosted.lid') {
320
+ throw new Error('Unexpected non-hosted device JID with device 99. This ID seems invalid. ID:' + jid);
321
+ }
322
+ return new libsignal.ProtocolAddress(signalUser, finalDevice);
309
323
  };
310
324
  const jidToSignalSenderKeyName = (group, user) => {
311
- return new _senderKeyName.SenderKeyName(group, jidToSignalProtocolAddress(user));
325
+ return new SenderKeyName(group, jidToSignalProtocolAddress(user));
312
326
  };
313
- function signalStorage({
314
- creds,
315
- keys
316
- }, lidMapping) {
317
- return {
318
- loadSession: async id => {
319
- try {
320
- // LID SINGLE SOURCE OF TRUTH: Auto-redirect PN to LID if mapping exists
321
- let actualId = id;
322
- if (id.includes('.') && !id.includes('_1')) {
323
- // This is a PN signal address format (e.g., "1234567890.0")
324
- // Convert back to JID to check for LID mapping
325
- const parts = id.split('.');
326
- const device = parts[1] || '0';
327
- const pnJid = device === '0' ? `${parts[0]}@s.whatsapp.net` : `${parts[0]}:${device}@s.whatsapp.net`;
328
- const lidForPN = await lidMapping.getLIDForPN(pnJid);
329
- if (lidForPN?.includes('@lid')) {
330
- const lidAddr = jidToSignalProtocolAddress(lidForPN);
331
- const lidId = lidAddr.toString();
332
- // Check if LID session exists
333
- const {
334
- [lidId]: lidSession
335
- } = await keys.get('session', [lidId]);
336
- if (lidSession) {
337
- actualId = lidId;
327
+ function signalStorage({ creds, keys }, lidMapping) {
328
+ // Shared function to resolve PN signal address to LID if mapping exists
329
+ const resolveLIDSignalAddress = async (id) => {
330
+ if (id.includes('.')) {
331
+ const [deviceId, device] = id.split('.');
332
+ const [user, domainType_] = deviceId.split('_');
333
+ const domainType = parseInt(domainType_ || '0');
334
+ if (domainType === WAJIDDomains.LID || domainType === WAJIDDomains.HOSTED_LID)
335
+ return id;
336
+ const pnJid = `${user}${device !== '0' ? `:${device}` : ''}@${domainType === WAJIDDomains.HOSTED ? 'hosted' : 's.whatsapp.net'}`;
337
+ const lidForPN = await lidMapping.getLIDForPN(pnJid);
338
+ if (lidForPN) {
339
+ const lidAddr = jidToSignalProtocolAddress(lidForPN);
340
+ return lidAddr.toString();
338
341
  }
339
- }
340
- }
341
- const {
342
- [actualId]: sess
343
- } = await keys.get('session', [actualId]);
344
- if (sess) {
345
- return libsignal.SessionRecord.deserialize(sess);
346
- }
347
- } catch (e) {
348
- return null;
349
- }
350
- return null;
351
- },
352
- // TODO: Replace with libsignal.SessionRecord when type exports are added to libsignal
353
- storeSession: async (id, session) => {
354
- await keys.set({
355
- session: {
356
- [id]: session.serialize()
357
342
  }
358
- });
359
- },
360
- isTrustedIdentity: () => {
361
- return true;
362
- },
363
- loadPreKey: async id => {
364
- const keyId = id.toString();
365
- const {
366
- [keyId]: key
367
- } = await keys.get('pre-key', [keyId]);
368
- if (key) {
369
- return {
370
- privKey: Buffer.from(key.private),
371
- pubKey: Buffer.from(key.public)
372
- };
373
- }
374
- },
375
- removePreKey: id => keys.set({
376
- 'pre-key': {
377
- [id]: null
378
- }
379
- }),
380
- loadSignedPreKey: () => {
381
- const key = creds.signedPreKey;
382
- return {
383
- privKey: Buffer.from(key.keyPair.private),
384
- pubKey: Buffer.from(key.keyPair.public)
385
- };
386
- },
387
- loadSenderKey: async senderKeyName => {
388
- const keyId = senderKeyName.toString();
389
- const {
390
- [keyId]: key
391
- } = await keys.get('sender-key', [keyId]);
392
- if (key) {
393
- return _senderKeyRecord.SenderKeyRecord.deserialize(key);
394
- }
395
- return new _senderKeyRecord.SenderKeyRecord();
396
- },
397
- storeSenderKey: async (senderKeyName, key) => {
398
- const keyId = senderKeyName.toString();
399
- const serialized = JSON.stringify(key.serialize());
400
- await keys.set({
401
- 'sender-key': {
402
- [keyId]: Buffer.from(serialized, 'utf-8')
343
+ return id;
344
+ };
345
+ return {
346
+ loadSession: async (id) => {
347
+ try {
348
+ const wireJid = await resolveLIDSignalAddress(id);
349
+ const { [wireJid]: sess } = await keys.get('session', [wireJid]);
350
+ if (sess) {
351
+ return libsignal.SessionRecord.deserialize(sess);
352
+ }
353
+ }
354
+ catch (e) {
355
+ return null;
356
+ }
357
+ return null;
358
+ },
359
+ storeSession: async (id, session) => {
360
+ const wireJid = await resolveLIDSignalAddress(id);
361
+ await keys.set({ session: { [wireJid]: session.serialize() } });
362
+ },
363
+ isTrustedIdentity: () => {
364
+ return true; // TOFU - Trust on First Use (same as WhatsApp Web)
365
+ },
366
+ loadIdentityKey: async (id) => {
367
+ const wireJid = await resolveLIDSignalAddress(id);
368
+ const { [wireJid]: key } = await keys.get('identity-key', [wireJid]);
369
+ return key || undefined;
370
+ },
371
+ saveIdentity: async (id, identityKey) => {
372
+ const wireJid = await resolveLIDSignalAddress(id);
373
+ const { [wireJid]: existingKey } = await keys.get('identity-key', [wireJid]);
374
+ const keysMatch = existingKey?.length === identityKey.length && existingKey.every((byte, i) => byte === identityKey[i]);
375
+ if (existingKey && !keysMatch) {
376
+ // Identity changed - clear session and update key
377
+ await keys.set({
378
+ session: { [wireJid]: null },
379
+ 'identity-key': { [wireJid]: identityKey }
380
+ });
381
+ return true;
382
+ }
383
+ if (!existingKey) {
384
+ // New contact - Trust on First Use (TOFU)
385
+ await keys.set({ 'identity-key': { [wireJid]: identityKey } });
386
+ return true;
387
+ }
388
+ return false;
389
+ },
390
+ loadPreKey: async (id) => {
391
+ const keyId = id.toString();
392
+ const { [keyId]: key } = await keys.get('pre-key', [keyId]);
393
+ if (key) {
394
+ return {
395
+ privKey: Buffer.from(key.private),
396
+ pubKey: Buffer.from(key.public)
397
+ };
398
+ }
399
+ },
400
+ removePreKey: (id) => keys.set({ 'pre-key': { [id]: null } }),
401
+ loadSignedPreKey: () => {
402
+ const key = creds.signedPreKey;
403
+ return {
404
+ privKey: Buffer.from(key.keyPair.private),
405
+ pubKey: Buffer.from(key.keyPair.public)
406
+ };
407
+ },
408
+ loadSenderKey: async (senderKeyName) => {
409
+ const keyId = senderKeyName.toString();
410
+ const { [keyId]: key } = await keys.get('sender-key', [keyId]);
411
+ if (key) {
412
+ return SenderKeyRecord.deserialize(key);
413
+ }
414
+ return new SenderKeyRecord();
415
+ },
416
+ storeSenderKey: async (senderKeyName, key) => {
417
+ const keyId = senderKeyName.toString();
418
+ const serialized = JSON.stringify(key.serialize());
419
+ await keys.set({ 'sender-key': { [keyId]: Buffer.from(serialized, 'utf-8') } });
420
+ },
421
+ getOurRegistrationId: () => creds.registrationId,
422
+ getOurIdentity: () => {
423
+ const { signedIdentityKey } = creds;
424
+ return {
425
+ privKey: Buffer.from(signedIdentityKey.private),
426
+ pubKey: Buffer.from(generateSignalPubKey(signedIdentityKey.public))
427
+ };
403
428
  }
404
- });
405
- },
406
- getOurRegistrationId: () => creds.registrationId,
407
- getOurIdentity: () => {
408
- const {
409
- signedIdentityKey
410
- } = creds;
411
- return {
412
- privKey: Buffer.from(signedIdentityKey.private),
413
- pubKey: (0, _index.generateSignalPubKey)(signedIdentityKey.public)
414
- };
415
- }
416
- };
429
+ };
417
430
  }
418
431
  //# sourceMappingURL=libsignal.js.map