dcodeindra-baileyspro 2.3.9

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