zapo-js 0.1.0 → 0.1.2

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 (366) hide show
  1. package/README.md +12 -7
  2. package/dist/appstate/WaAppStateCrypto.js +18 -25
  3. package/dist/appstate/WaAppStateSyncClient.js +181 -114
  4. package/dist/appstate/WaAppStateSyncResponseParser.js +16 -5
  5. package/dist/appstate/constants.js +4 -3
  6. package/dist/appstate/utils.js +10 -30
  7. package/dist/auth/WaAuthClient.js +48 -55
  8. package/dist/auth/flow/WaAuthCredentialsFlow.js +21 -14
  9. package/dist/auth/index.js +1 -3
  10. package/dist/auth/pairing/WaPairingFlow.js +21 -23
  11. package/dist/auth/pairing/WaQrFlow.js +37 -24
  12. package/dist/client/WaClient.js +103 -276
  13. package/dist/client/WaClientFactory.js +227 -110
  14. package/dist/client/connection/WaConnectionManager.js +292 -0
  15. package/dist/client/connection/WaKeyShareCoordinator.js +63 -0
  16. package/dist/client/connection/WaReceiptQueue.js +51 -0
  17. package/dist/client/coordinators/WaAppStateMutationCoordinator.js +471 -0
  18. package/dist/client/coordinators/WaGroupCoordinator.js +27 -17
  19. package/dist/client/coordinators/WaIncomingNodeCoordinator.js +20 -27
  20. package/dist/client/coordinators/WaMessageDispatchCoordinator.js +231 -686
  21. package/dist/client/coordinators/WaRetryCoordinator.js +70 -37
  22. package/dist/client/dirty.js +35 -29
  23. package/dist/client/events/chat.js +4 -3
  24. package/dist/client/events/group.js +59 -36
  25. package/dist/client/history-sync.js +53 -63
  26. package/dist/client/incoming.js +23 -20
  27. package/dist/client/mailbox.js +8 -8
  28. package/dist/client/messages.js +4 -4
  29. package/dist/client/messaging/fanout.js +189 -0
  30. package/dist/client/messaging/key-protocol.js +130 -0
  31. package/dist/client/messaging/participants.js +191 -0
  32. package/dist/crypto/core/hkdf.js +3 -8
  33. package/dist/crypto/core/index.js +1 -4
  34. package/dist/crypto/core/keys.js +2 -3
  35. package/dist/crypto/core/primitives.js +12 -15
  36. package/dist/crypto/core/random.js +7 -26
  37. package/dist/crypto/curves/Ed25519.js +7 -8
  38. package/dist/crypto/curves/X25519.js +13 -16
  39. package/dist/crypto/index.js +0 -5
  40. package/dist/esm/appstate/WaAppStateCrypto.js +6 -13
  41. package/dist/esm/appstate/WaAppStateSyncClient.js +174 -107
  42. package/dist/esm/appstate/WaAppStateSyncResponseParser.js +17 -6
  43. package/dist/esm/appstate/constants.js +3 -2
  44. package/dist/esm/appstate/utils.js +8 -27
  45. package/dist/esm/auth/WaAuthClient.js +48 -55
  46. package/dist/esm/auth/flow/WaAuthCredentialsFlow.js +21 -14
  47. package/dist/esm/auth/index.js +0 -1
  48. package/dist/esm/auth/pairing/WaPairingFlow.js +14 -16
  49. package/dist/esm/auth/pairing/WaQrFlow.js +37 -24
  50. package/dist/esm/client/WaClient.js +103 -276
  51. package/dist/esm/client/WaClientFactory.js +227 -110
  52. package/dist/esm/client/connection/WaConnectionManager.js +288 -0
  53. package/dist/esm/client/connection/WaKeyShareCoordinator.js +59 -0
  54. package/dist/esm/client/connection/WaReceiptQueue.js +47 -0
  55. package/dist/esm/client/coordinators/WaAppStateMutationCoordinator.js +467 -0
  56. package/dist/esm/client/coordinators/WaGroupCoordinator.js +20 -10
  57. package/dist/esm/client/coordinators/WaIncomingNodeCoordinator.js +20 -27
  58. package/dist/esm/client/coordinators/WaMessageDispatchCoordinator.js +232 -687
  59. package/dist/esm/client/coordinators/WaRetryCoordinator.js +71 -38
  60. package/dist/esm/client/dirty.js +30 -24
  61. package/dist/esm/client/events/chat.js +4 -3
  62. package/dist/esm/client/events/group.js +50 -28
  63. package/dist/esm/client/history-sync.js +50 -60
  64. package/dist/esm/client/incoming.js +23 -20
  65. package/dist/esm/client/mailbox.js +8 -8
  66. package/dist/esm/client/messages.js +1 -1
  67. package/dist/esm/client/messaging/fanout.js +186 -0
  68. package/dist/esm/client/messaging/key-protocol.js +127 -0
  69. package/dist/esm/client/messaging/participants.js +188 -0
  70. package/dist/esm/crypto/core/hkdf.js +3 -8
  71. package/dist/esm/crypto/core/index.js +0 -1
  72. package/dist/esm/crypto/core/keys.js +2 -3
  73. package/dist/esm/crypto/core/primitives.js +12 -15
  74. package/dist/esm/crypto/core/random.js +6 -25
  75. package/dist/esm/crypto/curves/Ed25519.js +4 -5
  76. package/dist/esm/crypto/curves/X25519.js +10 -13
  77. package/dist/esm/crypto/index.js +0 -2
  78. package/dist/esm/infra/log/ConsoleLogger.js +18 -17
  79. package/dist/esm/infra/log/PinoLogger.js +15 -9
  80. package/dist/esm/infra/log/types.js +11 -1
  81. package/dist/esm/infra/perf/BoundedTaskQueue.js +13 -17
  82. package/dist/esm/media/WaMediaCrypto.js +2 -4
  83. package/dist/esm/media/WaMediaTransferClient.js +226 -58
  84. package/dist/esm/media/conn.js +10 -6
  85. package/dist/esm/media/constants.js +4 -1
  86. package/dist/esm/message/WaMessageClient.js +4 -13
  87. package/dist/esm/message/ack.js +6 -6
  88. package/dist/esm/message/addon-crypto.js +59 -0
  89. package/dist/esm/message/incoming.js +106 -111
  90. package/dist/esm/message/index.js +2 -0
  91. package/dist/esm/message/reporting-token.js +438 -0
  92. package/dist/esm/message/use-case-secret.js +49 -0
  93. package/dist/esm/protocol/appstate.js +58 -0
  94. package/dist/esm/protocol/constants.js +2 -1
  95. package/dist/esm/protocol/index.js +2 -10
  96. package/dist/esm/protocol/jid.js +63 -51
  97. package/dist/esm/protocol/media.js +3 -3
  98. package/dist/esm/protocol/nodes.js +2 -0
  99. package/dist/esm/protocol/usync.js +11 -0
  100. package/dist/esm/retry/index.js +1 -0
  101. package/dist/esm/retry/outbound.js +4 -5
  102. package/dist/esm/retry/parse.js +58 -76
  103. package/dist/esm/retry/replay.js +48 -49
  104. package/dist/esm/retry/tracker.js +56 -0
  105. package/dist/esm/signal/api/SignalDeviceSyncApi.js +249 -82
  106. package/dist/esm/signal/api/SignalDigestSyncApi.js +6 -1
  107. package/dist/esm/signal/api/SignalIdentitySyncApi.js +49 -34
  108. package/dist/esm/signal/api/SignalMissingPreKeysSyncApi.js +70 -62
  109. package/dist/esm/signal/api/SignalSessionSyncApi.js +23 -30
  110. package/dist/esm/signal/crypto/WaAdvSignature.js +3 -5
  111. package/dist/esm/signal/group/SenderKeyChain.js +28 -23
  112. package/dist/esm/signal/group/SenderKeyCodec.js +2 -4
  113. package/dist/esm/signal/group/SenderKeyManager.js +26 -16
  114. package/dist/esm/signal/index.js +1 -0
  115. package/dist/esm/signal/session/SignalProtocol.js +49 -14
  116. package/dist/esm/signal/session/SignalRatchet.js +24 -15
  117. package/dist/esm/signal/session/SignalSession.js +14 -9
  118. package/dist/esm/signal/session/resolver.js +186 -0
  119. package/dist/esm/signal/store/sqlite.js +16 -37
  120. package/dist/esm/store/createStore.js +16 -18
  121. package/dist/esm/store/noop.store.js +3 -6
  122. package/dist/esm/store/providers/memory/appstate.store.js +30 -6
  123. package/dist/esm/store/providers/memory/contact.store.js +5 -0
  124. package/dist/esm/store/providers/memory/device-list.store.js +3 -30
  125. package/dist/esm/store/providers/memory/message.store.js +11 -5
  126. package/dist/esm/store/providers/memory/participants.store.js +1 -8
  127. package/dist/esm/store/providers/memory/sender-key.store.js +5 -7
  128. package/dist/esm/store/providers/memory/signal.store.js +13 -1
  129. package/dist/esm/store/providers/memory/thread.store.js +5 -0
  130. package/dist/esm/store/providers/sqlite/appstate.store.js +82 -1
  131. package/dist/esm/store/providers/sqlite/connection.js +18 -13
  132. package/dist/esm/store/providers/sqlite/contact.store.js +31 -18
  133. package/dist/esm/store/providers/sqlite/device-list.store.js +7 -35
  134. package/dist/esm/store/providers/sqlite/message.store.js +45 -32
  135. package/dist/esm/store/providers/sqlite/migrations.js +1 -1
  136. package/dist/esm/store/providers/sqlite/participants.store.js +1 -9
  137. package/dist/esm/store/providers/sqlite/retry.store.js +8 -11
  138. package/dist/esm/store/providers/sqlite/sender-key.store.js +25 -30
  139. package/dist/esm/store/providers/sqlite/signal.store.js +104 -22
  140. package/dist/esm/store/providers/sqlite/table-names.js +107 -0
  141. package/dist/esm/store/providers/sqlite/thread.store.js +35 -22
  142. package/dist/esm/transport/WaComms.js +25 -23
  143. package/dist/esm/transport/WaWebSocket.js +115 -12
  144. package/dist/esm/transport/binary/decoder.js +4 -4
  145. package/dist/esm/transport/binary/encoder.js +12 -4
  146. package/dist/esm/transport/index.js +1 -0
  147. package/dist/esm/transport/keepalive/WaKeepAlive.js +2 -8
  148. package/dist/esm/transport/node/WaNodeOrchestrator.js +2 -4
  149. package/dist/esm/transport/node/WaNodeTransport.js +0 -3
  150. package/dist/esm/transport/node/builders/{accountSync.js → account-sync.js} +16 -36
  151. package/dist/esm/transport/node/builders/index.js +2 -1
  152. package/dist/esm/transport/node/builders/message.js +9 -0
  153. package/dist/esm/transport/node/builders/pairing.js +4 -5
  154. package/dist/esm/transport/node/builders/usync.js +41 -0
  155. package/dist/esm/transport/node/helpers.js +107 -5
  156. package/dist/esm/transport/node/usync.js +35 -0
  157. package/dist/esm/transport/noise/WaFrameCodec.js +48 -33
  158. package/dist/esm/transport/noise/WaNoiseCert.js +3 -6
  159. package/dist/esm/transport/noise/WaNoiseSession.js +17 -10
  160. package/dist/esm/transport/proxy.js +27 -0
  161. package/dist/esm/transport/stream/parse.js +13 -48
  162. package/dist/esm/util/bytes.js +50 -32
  163. package/dist/esm/util/coercion.js +6 -14
  164. package/dist/esm/util/primitives.js +39 -14
  165. package/dist/infra/log/ConsoleLogger.js +18 -17
  166. package/dist/infra/log/PinoLogger.js +15 -9
  167. package/dist/infra/log/types.js +12 -0
  168. package/dist/infra/perf/BoundedTaskQueue.js +13 -17
  169. package/dist/media/WaMediaCrypto.js +1 -3
  170. package/dist/media/WaMediaTransferClient.js +259 -58
  171. package/dist/media/conn.js +10 -6
  172. package/dist/media/constants.js +4 -1
  173. package/dist/message/WaMessageClient.js +5 -14
  174. package/dist/message/ack.js +6 -6
  175. package/dist/message/addon-crypto.js +65 -0
  176. package/dist/message/incoming.js +104 -109
  177. package/dist/message/index.js +2 -0
  178. package/dist/message/reporting-token.js +443 -0
  179. package/dist/message/use-case-secret.js +55 -0
  180. package/dist/protocol/appstate.js +59 -1
  181. package/dist/protocol/constants.js +7 -1
  182. package/dist/protocol/index.js +20 -42
  183. package/dist/protocol/jid.js +64 -51
  184. package/dist/protocol/media.js +3 -3
  185. package/dist/protocol/nodes.js +2 -0
  186. package/dist/protocol/usync.js +14 -0
  187. package/dist/retry/index.js +3 -1
  188. package/dist/retry/outbound.js +6 -7
  189. package/dist/retry/parse.js +57 -75
  190. package/dist/retry/replay.js +46 -47
  191. package/dist/retry/tracker.js +59 -0
  192. package/dist/signal/api/SignalDeviceSyncApi.js +247 -80
  193. package/dist/signal/api/SignalDigestSyncApi.js +6 -1
  194. package/dist/signal/api/SignalIdentitySyncApi.js +49 -34
  195. package/dist/signal/api/SignalMissingPreKeysSyncApi.js +67 -59
  196. package/dist/signal/api/SignalSessionSyncApi.js +23 -30
  197. package/dist/signal/crypto/WaAdvSignature.js +2 -4
  198. package/dist/signal/group/SenderKeyChain.js +27 -22
  199. package/dist/signal/group/SenderKeyCodec.js +1 -3
  200. package/dist/signal/group/SenderKeyManager.js +26 -16
  201. package/dist/signal/index.js +3 -1
  202. package/dist/signal/session/SignalProtocol.js +49 -14
  203. package/dist/signal/session/SignalRatchet.js +24 -15
  204. package/dist/signal/session/SignalSession.js +14 -9
  205. package/dist/signal/session/resolver.js +189 -0
  206. package/dist/signal/store/sqlite.js +16 -37
  207. package/dist/store/createStore.js +16 -18
  208. package/dist/store/noop.store.js +3 -6
  209. package/dist/store/providers/memory/appstate.store.js +28 -4
  210. package/dist/store/providers/memory/contact.store.js +5 -0
  211. package/dist/store/providers/memory/device-list.store.js +3 -30
  212. package/dist/store/providers/memory/message.store.js +11 -5
  213. package/dist/store/providers/memory/participants.store.js +1 -8
  214. package/dist/store/providers/memory/sender-key.store.js +8 -10
  215. package/dist/store/providers/memory/signal.store.js +21 -9
  216. package/dist/store/providers/memory/thread.store.js +5 -0
  217. package/dist/store/providers/sqlite/appstate.store.js +81 -0
  218. package/dist/store/providers/sqlite/connection.js +18 -13
  219. package/dist/store/providers/sqlite/contact.store.js +31 -18
  220. package/dist/store/providers/sqlite/device-list.store.js +7 -35
  221. package/dist/store/providers/sqlite/message.store.js +45 -32
  222. package/dist/store/providers/sqlite/migrations.js +1 -1
  223. package/dist/store/providers/sqlite/participants.store.js +1 -9
  224. package/dist/store/providers/sqlite/retry.store.js +8 -11
  225. package/dist/store/providers/sqlite/sender-key.store.js +24 -29
  226. package/dist/store/providers/sqlite/signal.store.js +105 -23
  227. package/dist/store/providers/sqlite/table-names.js +113 -0
  228. package/dist/store/providers/sqlite/thread.store.js +35 -22
  229. package/dist/transport/WaComms.js +27 -25
  230. package/dist/transport/WaWebSocket.js +148 -12
  231. package/dist/transport/binary/decoder.js +4 -4
  232. package/dist/transport/binary/encoder.js +12 -4
  233. package/dist/transport/index.js +7 -1
  234. package/dist/transport/keepalive/WaKeepAlive.js +1 -7
  235. package/dist/transport/node/WaNodeOrchestrator.js +2 -4
  236. package/dist/transport/node/WaNodeTransport.js +0 -3
  237. package/dist/transport/node/builders/{accountSync.js → account-sync.js} +15 -35
  238. package/dist/transport/node/builders/index.js +12 -9
  239. package/dist/transport/node/builders/message.js +9 -0
  240. package/dist/transport/node/builders/pairing.js +4 -5
  241. package/dist/transport/node/builders/usync.js +45 -0
  242. package/dist/transport/node/helpers.js +112 -4
  243. package/dist/transport/node/usync.js +38 -0
  244. package/dist/transport/noise/WaFrameCodec.js +47 -32
  245. package/dist/transport/noise/WaNoiseCert.js +5 -8
  246. package/dist/transport/noise/WaNoiseSession.js +17 -10
  247. package/dist/transport/proxy.js +34 -0
  248. package/dist/transport/stream/parse.js +17 -53
  249. package/dist/types/appstate/WaAppStateCrypto.d.ts +0 -1
  250. package/dist/types/appstate/WaAppStateSyncClient.d.ts +5 -2
  251. package/dist/types/appstate/constants.d.ts +1 -0
  252. package/dist/types/appstate/store/sqlite.d.ts +4 -18
  253. package/dist/types/appstate/utils.d.ts +0 -1
  254. package/dist/types/auth/WaAuthClient.d.ts +10 -12
  255. package/dist/types/auth/index.d.ts +0 -2
  256. package/dist/types/auth/pairing/WaQrFlow.d.ts +1 -1
  257. package/dist/types/auth/types.d.ts +6 -9
  258. package/dist/types/client/WaClient.d.ts +27 -25
  259. package/dist/types/client/WaClientFactory.d.ts +22 -23
  260. package/dist/types/client/connection/WaConnectionManager.d.ts +64 -0
  261. package/dist/types/client/connection/WaKeyShareCoordinator.d.ts +14 -0
  262. package/dist/types/client/connection/WaReceiptQueue.d.ts +13 -0
  263. package/dist/types/client/coordinators/WaAppStateMutationCoordinator.d.ts +46 -0
  264. package/dist/types/client/coordinators/WaIncomingNodeCoordinator.d.ts +0 -1
  265. package/dist/types/client/coordinators/WaMessageDispatchCoordinator.d.ts +18 -41
  266. package/dist/types/client/coordinators/WaRetryCoordinator.d.ts +2 -0
  267. package/dist/types/client/dirty.d.ts +1 -0
  268. package/dist/types/client/events/group.d.ts +2 -1
  269. package/dist/types/client/index.d.ts +1 -1
  270. package/dist/types/client/messaging/fanout.d.ts +14 -0
  271. package/dist/types/client/messaging/key-protocol.d.ts +18 -0
  272. package/dist/types/client/messaging/participants.d.ts +13 -0
  273. package/dist/types/client/types.d.ts +24 -1
  274. package/dist/types/crypto/core/hkdf.d.ts +0 -6
  275. package/dist/types/crypto/core/index.d.ts +0 -1
  276. package/dist/types/crypto/core/random.d.ts +1 -7
  277. package/dist/types/crypto/index.d.ts +0 -2
  278. package/dist/types/index.d.ts +1 -1
  279. package/dist/types/infra/log/ConsoleLogger.d.ts +2 -1
  280. package/dist/types/infra/log/PinoLogger.d.ts +1 -1
  281. package/dist/types/infra/log/types.d.ts +1 -0
  282. package/dist/types/infra/perf/BoundedTaskQueue.d.ts +1 -1
  283. package/dist/types/media/WaMediaTransferClient.d.ts +13 -3
  284. package/dist/types/media/types.d.ts +5 -0
  285. package/dist/types/message/addon-crypto.d.ts +25 -0
  286. package/dist/types/message/index.d.ts +2 -0
  287. package/dist/types/message/reporting-token.d.ts +19 -0
  288. package/dist/types/message/use-case-secret.d.ts +20 -0
  289. package/dist/types/protocol/appstate.d.ts +58 -0
  290. package/dist/types/protocol/constants.d.ts +2 -1
  291. package/dist/types/protocol/index.d.ts +2 -10
  292. package/dist/types/protocol/jid.d.ts +3 -3
  293. package/dist/types/protocol/nodes.d.ts +2 -0
  294. package/dist/types/protocol/usync.d.ts +11 -0
  295. package/dist/types/retry/index.d.ts +1 -0
  296. package/dist/types/retry/replay.d.ts +0 -4
  297. package/dist/types/retry/tracker.d.ts +19 -0
  298. package/dist/types/retry/types.d.ts +4 -3
  299. package/dist/types/signal/api/SignalDeviceSyncApi.d.ts +13 -1
  300. package/dist/types/signal/group/SenderKeyCodec.d.ts +4 -6
  301. package/dist/types/signal/index.d.ts +1 -0
  302. package/dist/types/signal/session/SignalProtocol.d.ts +9 -0
  303. package/dist/types/signal/session/resolver.d.ts +17 -0
  304. package/dist/types/store/contracts/appstate.store.d.ts +3 -0
  305. package/dist/types/store/contracts/contact.store.d.ts +1 -0
  306. package/dist/types/store/contracts/device-list.store.d.ts +0 -3
  307. package/dist/types/store/contracts/message.store.d.ts +1 -0
  308. package/dist/types/store/contracts/participants.store.d.ts +0 -1
  309. package/dist/types/store/contracts/sender-key.store.d.ts +0 -1
  310. package/dist/types/store/contracts/signal.store.d.ts +6 -0
  311. package/dist/types/store/contracts/thread.store.d.ts +1 -0
  312. package/dist/types/store/index.d.ts +1 -1
  313. package/dist/types/store/providers/memory/appstate.store.d.ts +2 -0
  314. package/dist/types/store/providers/memory/contact.store.d.ts +1 -0
  315. package/dist/types/store/providers/memory/device-list.store.d.ts +0 -3
  316. package/dist/types/store/providers/memory/message.store.d.ts +1 -0
  317. package/dist/types/store/providers/memory/participants.store.d.ts +0 -1
  318. package/dist/types/store/providers/memory/sender-key.store.d.ts +0 -1
  319. package/dist/types/store/providers/memory/signal.store.d.ts +6 -0
  320. package/dist/types/store/providers/memory/thread.store.d.ts +1 -0
  321. package/dist/types/store/providers/sqlite/appstate.store.d.ts +2 -0
  322. package/dist/types/store/providers/sqlite/contact.store.d.ts +2 -0
  323. package/dist/types/store/providers/sqlite/device-list.store.d.ts +0 -3
  324. package/dist/types/store/providers/sqlite/message.store.d.ts +2 -0
  325. package/dist/types/store/providers/sqlite/participants.store.d.ts +0 -1
  326. package/dist/types/store/providers/sqlite/retry.store.d.ts +0 -1
  327. package/dist/types/store/providers/sqlite/sender-key.store.d.ts +0 -1
  328. package/dist/types/store/providers/sqlite/signal.store.d.ts +7 -0
  329. package/dist/types/store/providers/sqlite/table-names.d.ts +5 -0
  330. package/dist/types/store/providers/sqlite/thread.store.d.ts +2 -0
  331. package/dist/types/store/types.d.ts +3 -0
  332. package/dist/types/transport/WaWebSocket.d.ts +3 -0
  333. package/dist/types/transport/index.d.ts +2 -1
  334. package/dist/types/transport/keepalive/WaKeepAlive.d.ts +0 -1
  335. package/dist/types/transport/node/WaNodeTransport.d.ts +0 -9
  336. package/dist/types/transport/node/builders/group.d.ts +4 -6
  337. package/dist/types/transport/node/builders/index.d.ts +2 -1
  338. package/dist/types/transport/node/builders/message.d.ts +14 -25
  339. package/dist/types/transport/node/builders/retry.d.ts +2 -4
  340. package/dist/types/transport/node/builders/usync.d.ts +21 -0
  341. package/dist/types/transport/node/helpers.d.ts +8 -0
  342. package/dist/types/transport/node/usync.d.ts +2 -0
  343. package/dist/types/transport/noise/WaFrameCodec.d.ts +3 -0
  344. package/dist/types/transport/noise/WaNoiseSession.d.ts +1 -0
  345. package/dist/types/transport/proxy.d.ts +6 -0
  346. package/dist/types/transport/stream/parse.d.ts +0 -1
  347. package/dist/types/transport/types.d.ts +18 -1
  348. package/dist/types/util/bytes.d.ts +5 -0
  349. package/dist/types/util/primitives.d.ts +3 -0
  350. package/dist/util/bytes.js +55 -33
  351. package/dist/util/coercion.js +6 -14
  352. package/dist/util/primitives.js +42 -14
  353. package/package.json +27 -9
  354. package/proto/index.d.ts +1090 -1048
  355. package/proto/index.js +1 -1
  356. package/scripts/check-node-version.cjs +0 -1
  357. package/dist/crypto/core/encoding.js +0 -29
  358. package/dist/esm/crypto/core/encoding.js +0 -25
  359. package/dist/esm/util/base64.js +0 -18
  360. package/dist/esm/util/signal-address.js +0 -5
  361. package/dist/types/crypto/core/encoding.d.ts +0 -11
  362. package/dist/types/util/base64.d.ts +0 -4
  363. package/dist/types/util/signal-address.d.ts +0 -2
  364. package/dist/util/base64.js +0 -24
  365. package/dist/util/signal-address.js +0 -8
  366. /package/dist/types/transport/node/builders/{accountSync.d.ts → account-sync.d.ts} +0 -0
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createOutboundRetryTracker = createOutboundRetryTracker;
4
+ const constants_1 = require("./constants");
5
+ const outbound_1 = require("./outbound");
6
+ const primitives_1 = require("../util/primitives");
7
+ function createOutboundRetryTracker(options) {
8
+ const { retryStore, logger } = options;
9
+ const retryTtlMs = retryStore.getTtlMs?.() ?? constants_1.RETRY_OUTBOUND_TTL_MS;
10
+ const safeUpsertRetryOutboundRecord = async (record) => {
11
+ try {
12
+ await retryStore.upsertOutboundMessage(record);
13
+ }
14
+ catch (error) {
15
+ logger.warn('failed to persist retry outbound message record', {
16
+ messageId: record.messageId,
17
+ to: record.toJid,
18
+ mode: record.replayMode,
19
+ message: (0, primitives_1.toError)(error).message
20
+ });
21
+ return false;
22
+ }
23
+ return true;
24
+ };
25
+ return {
26
+ track: async (hint, publish) => {
27
+ const nowMs = Date.now();
28
+ const expiresAtMs = nowMs + retryTtlMs;
29
+ const hintedMessageId = hint.messageIdHint?.trim();
30
+ const replayMode = hint.replayPayload.mode;
31
+ const resolvedToJid = hint.toJid ?? (replayMode === 'opaque_node' ? '' : hint.replayPayload.to);
32
+ const replayPayload = (0, outbound_1.encodeRetryReplayPayload)(hint.replayPayload);
33
+ let hintedPersisted = false;
34
+ const createRetryOutboundRecord = (messageId, createdAtMs, updatedAtMs, expiresAtMs) => ({
35
+ messageId,
36
+ toJid: resolvedToJid,
37
+ participantJid: hint.participantJid,
38
+ recipientJid: hint.recipientJid,
39
+ messageType: hint.type,
40
+ replayMode,
41
+ replayPayload,
42
+ state: 'pending',
43
+ createdAtMs,
44
+ updatedAtMs,
45
+ expiresAtMs
46
+ });
47
+ if (hintedMessageId) {
48
+ hintedPersisted = await safeUpsertRetryOutboundRecord(createRetryOutboundRecord(hintedMessageId, nowMs, nowMs, expiresAtMs));
49
+ }
50
+ const result = await publish();
51
+ if (hintedPersisted && hintedMessageId && result.id === hintedMessageId) {
52
+ return result;
53
+ }
54
+ const persistedNowMs = Date.now();
55
+ await safeUpsertRetryOutboundRecord(createRetryOutboundRecord(result.id, hintedMessageId ? nowMs : persistedNowMs, persistedNowMs, persistedNowMs + retryTtlMs));
56
+ return result;
57
+ }
58
+ };
59
+ }
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SignalDeviceSyncApi = void 0;
4
- const node_crypto_1 = require("node:crypto");
5
4
  const constants_1 = require("../../protocol/constants");
6
5
  const jid_1 = require("../../protocol/jid");
6
+ const usync_1 = require("../../transport/node/builders/usync");
7
7
  const helpers_1 = require("../../transport/node/helpers");
8
8
  const query_1 = require("../../transport/node/query");
9
+ const usync_2 = require("../../transport/node/usync");
9
10
  class SignalDeviceSyncApi {
10
11
  constructor(options) {
11
12
  this.logger = options.logger;
@@ -14,6 +15,7 @@ class SignalDeviceSyncApi {
14
15
  this.defaultTimeoutMs =
15
16
  options.defaultTimeoutMs ?? constants_1.WA_DEFAULTS.SIGNAL_FETCH_KEY_BUNDLES_TIMEOUT_MS;
16
17
  this.hostDomain = options.hostDomain ?? constants_1.WA_DEFAULTS.HOST_DOMAIN;
18
+ this.generateSid = options.generateSid ?? (0, usync_2.createUsyncSidGenerator)();
17
19
  }
18
20
  async syncDeviceList(userJids, timeoutMs = this.defaultTimeoutMs) {
19
21
  const normalizedUsers = this.normalizeUsers(userJids);
@@ -26,12 +28,18 @@ class SignalDeviceSyncApi {
26
28
  ? await this.collectUsersToQuery(normalizedUsers, nowMs, cachedByUser, this.deviceListStore)
27
29
  : normalizedUsers;
28
30
  if (usersToQuery.length === 0) {
29
- return normalizedUsers.map((jid) => ({
30
- jid,
31
- deviceJids: cachedByUser.get(jid) ?? []
32
- }));
31
+ const fromCache = new Array(normalizedUsers.length);
32
+ for (let index = 0; index < normalizedUsers.length; index += 1) {
33
+ const jid = normalizedUsers[index];
34
+ fromCache[index] = {
35
+ jid,
36
+ deviceJids: cachedByUser.get(jid) ?? []
37
+ };
38
+ }
39
+ return fromCache;
33
40
  }
34
- const request = this.makeDeviceSyncRequest(usersToQuery);
41
+ const sid = await this.generateSid();
42
+ const request = this.makeDeviceSyncRequest(usersToQuery, sid);
35
43
  this.logger.debug('signal device sync request', {
36
44
  users: usersToQuery.length,
37
45
  timeoutMs
@@ -40,23 +48,82 @@ class SignalDeviceSyncApi {
40
48
  const parsed = this.parseDeviceSyncResponse(response, usersToQuery);
41
49
  if (this.deviceListStore) {
42
50
  const updatedAtMs = Date.now();
43
- await this.deviceListStore.upsertUserDevicesBatch(parsed.map((entry) => ({
44
- userJid: entry.jid,
45
- deviceJids: entry.deviceJids,
46
- updatedAtMs
47
- })));
48
- }
49
- const parsedByUser = new Map(parsed.map((entry) => [entry.jid, entry.deviceJids]));
50
- const merged = normalizedUsers.map((jid) => ({
51
- jid,
52
- deviceJids: parsedByUser.get(jid) ?? cachedByUser.get(jid) ?? []
53
- }));
51
+ const batch = new Array(parsed.length);
52
+ for (let index = 0; index < parsed.length; index += 1) {
53
+ const entry = parsed[index];
54
+ batch[index] = {
55
+ userJid: entry.jid,
56
+ deviceJids: entry.deviceJids,
57
+ updatedAtMs
58
+ };
59
+ }
60
+ await this.deviceListStore.upsertUserDevicesBatch(batch);
61
+ }
62
+ const parsedByUser = new Map();
63
+ for (let index = 0; index < parsed.length; index += 1) {
64
+ const entry = parsed[index];
65
+ parsedByUser.set(entry.jid, entry.deviceJids);
66
+ }
67
+ const merged = new Array(normalizedUsers.length);
68
+ let totalDevices = 0;
69
+ for (let index = 0; index < normalizedUsers.length; index += 1) {
70
+ const jid = normalizedUsers[index];
71
+ const deviceJids = parsedByUser.get(jid) ?? cachedByUser.get(jid) ?? [];
72
+ totalDevices += deviceJids.length;
73
+ merged[index] = {
74
+ jid,
75
+ deviceJids
76
+ };
77
+ }
54
78
  this.logger.debug('signal device sync success', {
55
79
  users: merged.length,
56
- devices: merged.reduce((total, entry) => total + entry.deviceJids.length, 0)
80
+ devices: totalDevices
57
81
  });
58
82
  return merged;
59
83
  }
84
+ async queryLidsByPhoneJids(phoneJids, timeoutMs = this.defaultTimeoutMs) {
85
+ const normalizedPhoneJids = this.normalizeUsers(phoneJids);
86
+ if (normalizedPhoneJids.length === 0) {
87
+ return [];
88
+ }
89
+ const sid = await this.generateSid();
90
+ const request = this.makeLidSyncRequest(normalizedPhoneJids, sid);
91
+ this.logger.debug('signal lid sync request', {
92
+ users: normalizedPhoneJids.length,
93
+ timeoutMs
94
+ });
95
+ const response = await this.query(request, timeoutMs);
96
+ const parsed = this.parseLidSyncResponse(response, normalizedPhoneJids);
97
+ const parsedByPhoneJid = new Map();
98
+ for (let index = 0; index < parsed.length; index += 1) {
99
+ const entry = parsed[index];
100
+ const phoneJid = entry.phoneJid ?? entry.jid;
101
+ parsedByPhoneJid.set(phoneJid, {
102
+ phoneJid,
103
+ lidJid: entry.lidJid,
104
+ exists: entry.exists
105
+ });
106
+ }
107
+ const result = new Array(normalizedPhoneJids.length);
108
+ let found = 0;
109
+ for (let index = 0; index < normalizedPhoneJids.length; index += 1) {
110
+ const phoneJid = normalizedPhoneJids[index];
111
+ const resolved = parsedByPhoneJid.get(phoneJid) ?? {
112
+ phoneJid,
113
+ lidJid: null,
114
+ exists: false
115
+ };
116
+ if (resolved.exists) {
117
+ found += 1;
118
+ }
119
+ result[index] = resolved;
120
+ }
121
+ this.logger.debug('signal lid sync success', {
122
+ users: result.length,
123
+ found
124
+ });
125
+ return result;
126
+ }
60
127
  async collectUsersToQuery(normalizedUsers, nowMs, cachedByUser, store) {
61
128
  const records = await store.getUserDevicesBatch(normalizedUsers, nowMs);
62
129
  const usersToQuery = [];
@@ -71,41 +138,59 @@ class SignalDeviceSyncApi {
71
138
  }
72
139
  return usersToQuery;
73
140
  }
74
- makeDeviceSyncRequest(userJids) {
75
- return (0, query_1.buildIqNode)('get', this.hostDomain, constants_1.WA_XMLNS.USYNC, [
76
- {
77
- tag: constants_1.WA_NODE_TAGS.USYNC,
78
- attrs: {
79
- sid: this.makeSid(),
80
- index: '0',
81
- last: 'true',
82
- mode: constants_1.WA_NODE_TAGS.QUERY,
83
- context: 'interactive'
84
- },
141
+ makeDeviceSyncRequest(userJids, sid) {
142
+ const users = new Array(userJids.length);
143
+ for (let index = 0; index < userJids.length; index += 1) {
144
+ users[index] = {
145
+ jid: userJids[index]
146
+ };
147
+ }
148
+ return (0, usync_1.buildUsyncIq)({
149
+ sid,
150
+ hostDomain: this.hostDomain,
151
+ context: constants_1.WA_USYNC_CONTEXTS.INTERACTIVE,
152
+ queryProtocolNodes: [
153
+ {
154
+ tag: constants_1.WA_NODE_TAGS.DEVICES,
155
+ attrs: {
156
+ version: '2'
157
+ }
158
+ }
159
+ ],
160
+ users
161
+ });
162
+ }
163
+ makeLidSyncRequest(userJids, sid) {
164
+ const users = new Array(userJids.length);
165
+ for (let index = 0; index < userJids.length; index += 1) {
166
+ const jid = userJids[index];
167
+ users[index] = {
168
+ jid,
85
169
  content: [
86
170
  {
87
- tag: constants_1.WA_NODE_TAGS.QUERY,
88
- attrs: {},
89
- content: [
90
- {
91
- tag: constants_1.WA_NODE_TAGS.DEVICES,
92
- attrs: {
93
- version: '2'
94
- }
95
- }
96
- ]
97
- },
98
- {
99
- tag: constants_1.WA_NODE_TAGS.LIST,
171
+ tag: constants_1.WA_NODE_TAGS.CONTACT,
100
172
  attrs: {},
101
- content: userJids.map((jid) => ({
102
- tag: constants_1.WA_NODE_TAGS.USER,
103
- attrs: { jid }
104
- }))
173
+ content: (0, jid_1.splitJid)(jid).user
105
174
  }
106
175
  ]
107
- }
108
- ]);
176
+ };
177
+ }
178
+ return (0, usync_1.buildUsyncIq)({
179
+ sid,
180
+ hostDomain: this.hostDomain,
181
+ context: constants_1.WA_USYNC_CONTEXTS.INTERACTIVE,
182
+ queryProtocolNodes: [
183
+ {
184
+ tag: constants_1.WA_NODE_TAGS.CONTACT,
185
+ attrs: {}
186
+ },
187
+ {
188
+ tag: constants_1.WA_NODE_TAGS.LID,
189
+ attrs: {}
190
+ }
191
+ ],
192
+ users
193
+ });
109
194
  }
110
195
  parseDeviceSyncResponse(node, requestedUsers) {
111
196
  (0, query_1.assertIqResult)(node, 'signal device sync');
@@ -119,22 +204,95 @@ class SignalDeviceSyncApi {
119
204
  }
120
205
  const requestedSet = new Set(requestedUsers);
121
206
  const userNodes = (0, helpers_1.getNodeChildrenByTag)(listNode, constants_1.WA_NODE_TAGS.USER);
122
- return userNodes.flatMap((userNode) => {
207
+ const parsed = [];
208
+ for (let index = 0; index < userNodes.length; index += 1) {
209
+ const userNode = userNodes[index];
123
210
  const userJid = userNode.attrs.jid;
124
211
  if (!userJid) {
125
- return [];
212
+ continue;
126
213
  }
127
214
  const normalizedUserJid = this.normalizeUserJid(userJid);
128
215
  if (!requestedSet.has(normalizedUserJid)) {
129
- return [];
216
+ continue;
130
217
  }
131
- return [
132
- {
218
+ parsed.push({
219
+ jid: normalizedUserJid,
220
+ deviceJids: this.parseUserDeviceJids(userNode, normalizedUserJid)
221
+ });
222
+ }
223
+ return parsed;
224
+ }
225
+ parseLidSyncResponse(node, requestedUsers) {
226
+ (0, query_1.assertIqResult)(node, 'signal lid sync');
227
+ const usyncNode = (0, helpers_1.findNodeChild)(node, constants_1.WA_NODE_TAGS.USYNC);
228
+ if (!usyncNode) {
229
+ throw new Error('signal lid sync response missing usync node');
230
+ }
231
+ const listNode = (0, helpers_1.findNodeChild)(usyncNode, constants_1.WA_NODE_TAGS.LIST);
232
+ if (!listNode) {
233
+ throw new Error('signal lid sync response missing list node');
234
+ }
235
+ const requestedSet = new Set(requestedUsers);
236
+ const userNodes = (0, helpers_1.getNodeChildrenByTag)(listNode, constants_1.WA_NODE_TAGS.USER);
237
+ const parsed = [];
238
+ for (let index = 0; index < userNodes.length; index += 1) {
239
+ const userNode = userNodes[index];
240
+ const userJid = userNode.attrs.jid;
241
+ if (!userJid) {
242
+ continue;
243
+ }
244
+ const normalizedUserJid = this.normalizeUserJid(userJid);
245
+ const normalizedPhoneJid = userNode.attrs.pn_jid
246
+ ? this.normalizeUserJid(userNode.attrs.pn_jid)
247
+ : null;
248
+ const wasRequested = requestedSet.has(normalizedUserJid) ||
249
+ (normalizedPhoneJid !== null && requestedSet.has(normalizedPhoneJid));
250
+ if (!wasRequested) {
251
+ continue;
252
+ }
253
+ const lidNode = (0, helpers_1.findNodeChild)(userNode, constants_1.WA_NODE_TAGS.LID);
254
+ const contactNode = (0, helpers_1.findNodeChild)(userNode, constants_1.WA_NODE_TAGS.CONTACT);
255
+ if (!lidNode) {
256
+ parsed.push(this.buildLidSyncResult(normalizedUserJid, normalizedPhoneJid, contactNode, null));
257
+ continue;
258
+ }
259
+ const errorNode = (0, helpers_1.findNodeChild)(lidNode, constants_1.WA_NODE_TAGS.ERROR);
260
+ if (errorNode) {
261
+ this.logger.warn('signal lid sync user error', {
133
262
  jid: normalizedUserJid,
134
- deviceJids: this.parseUserDeviceJids(userNode, normalizedUserJid)
135
- }
136
- ];
137
- });
263
+ code: errorNode.attrs.code,
264
+ text: errorNode.attrs.text
265
+ });
266
+ parsed.push(this.buildLidSyncResult(normalizedUserJid, normalizedPhoneJid, contactNode, null));
267
+ continue;
268
+ }
269
+ const lidJid = lidNode.attrs.val ? this.normalizeUserJid(lidNode.attrs.val) : null;
270
+ parsed.push(this.buildLidSyncResult(normalizedUserJid, normalizedPhoneJid, contactNode, lidJid));
271
+ }
272
+ return parsed;
273
+ }
274
+ buildLidSyncResult(jid, phoneJid, contactNode, lidJid) {
275
+ return {
276
+ jid,
277
+ lidJid,
278
+ phoneJid,
279
+ exists: this.parseLidSyncContactExists(contactNode, jid, lidJid !== null)
280
+ };
281
+ }
282
+ parseLidSyncContactExists(contactNode, userJid, defaultExists) {
283
+ if (!contactNode) {
284
+ return defaultExists;
285
+ }
286
+ const errorNode = (0, helpers_1.findNodeChild)(contactNode, constants_1.WA_NODE_TAGS.ERROR);
287
+ if (errorNode) {
288
+ this.logger.warn('signal lid sync contact error', {
289
+ jid: userJid,
290
+ code: errorNode.attrs.code,
291
+ text: errorNode.attrs.text
292
+ });
293
+ return false;
294
+ }
295
+ return contactNode.attrs.type === 'in';
138
296
  }
139
297
  parseUserDeviceJids(userNode, userJid) {
140
298
  const devicesNode = (0, helpers_1.findNodeChild)(userNode, constants_1.WA_NODE_TAGS.DEVICES);
@@ -154,36 +312,45 @@ class SignalDeviceSyncApi {
154
312
  if (!deviceListNode) {
155
313
  return [];
156
314
  }
157
- return [
158
- ...new Set((0, helpers_1.getNodeChildrenByTag)(deviceListNode, 'device')
159
- .map((deviceNode) => {
160
- const parsedId = deviceNode.attrs.id
161
- ? Number.parseInt(deviceNode.attrs.id, 10)
162
- : Number.NaN;
163
- return Number.isSafeInteger(parsedId) && parsedId >= 0
164
- ? this.toDeviceJid(userJid, parsedId)
165
- : null;
166
- })
167
- .filter((jid) => jid !== null))
168
- ];
315
+ const parsedUser = (0, jid_1.splitJid)(userJid);
316
+ const dedup = new Set();
317
+ for (const deviceNode of (0, helpers_1.getNodeChildrenByTag)(deviceListNode, 'device')) {
318
+ const parsedId = deviceNode.attrs.id
319
+ ? Number.parseInt(deviceNode.attrs.id, 10)
320
+ : Number.NaN;
321
+ if (!Number.isSafeInteger(parsedId) || parsedId < 0) {
322
+ continue;
323
+ }
324
+ dedup.add(this.toDeviceJid(parsedUser.user, parsedUser.server, parsedId));
325
+ }
326
+ const deviceJids = [];
327
+ for (const jid of dedup.values()) {
328
+ deviceJids.push(jid);
329
+ }
330
+ return deviceJids;
169
331
  }
170
332
  normalizeUsers(userJids) {
171
- return [...new Set(userJids.map((jid) => this.normalizeUserJid(jid)))];
333
+ const normalized = [];
334
+ const dedup = new Set();
335
+ for (let index = 0; index < userJids.length; index += 1) {
336
+ const normalizedJid = this.normalizeUserJid(userJids[index]);
337
+ if (dedup.has(normalizedJid)) {
338
+ continue;
339
+ }
340
+ dedup.add(normalizedJid);
341
+ normalized.push(normalizedJid);
342
+ }
343
+ return normalized;
172
344
  }
173
345
  normalizeUserJid(jid) {
174
- const { user } = (0, jid_1.parseSignalAddressFromJid)(jid);
175
- const { server } = (0, jid_1.splitJid)(jid);
176
- return `${user}@${server}`;
346
+ const address = (0, jid_1.parseSignalAddressFromJid)(jid);
347
+ return `${address.user}@${address.server}`;
177
348
  }
178
- toDeviceJid(userJid, deviceId) {
179
- const parsed = (0, jid_1.splitJid)(userJid);
349
+ toDeviceJid(user, server, deviceId) {
180
350
  if (deviceId === 0) {
181
- return `${parsed.user}@${parsed.server}`;
351
+ return `${user}@${server}`;
182
352
  }
183
- return `${parsed.user}:${deviceId}@${parsed.server}`;
184
- }
185
- makeSid() {
186
- return `${Date.now()}.${(0, node_crypto_1.randomInt)(1000000)}`;
353
+ return `${user}:${deviceId}@${server}`;
187
354
  }
188
355
  }
189
356
  exports.SignalDeviceSyncApi = SignalDeviceSyncApi;
@@ -164,7 +164,12 @@ class SignalDigestSyncApi {
164
164
  const signedKeyId = (0, codec_1.parseUint)((0, codec_1.decodeExactLength)(signedKeyIdNode.content, 'signal digest skey.id', constants_2.SIGNAL_KEY_ID_LENGTH), 'signal digest skey.id');
165
165
  const signedKeyPublicKey = (0, codec_1.decodeExactLength)(signedKeyValueNode.content, 'signal digest skey.value', constants_2.SIGNAL_KEY_DATA_LENGTH);
166
166
  const signedKeySignature = (0, codec_1.decodeExactLength)(signedKeySignatureNode.content, 'signal digest skey.signature', constants_2.SIGNAL_SIGNATURE_LENGTH);
167
- const preKeyIds = (0, helpers_1.getNodeChildren)(listNode).map((child, index) => (0, codec_1.parseUint)((0, codec_1.decodeExactLength)(child.content, `signal digest list[${index}]`, constants_2.SIGNAL_KEY_ID_LENGTH), `signal digest list[${index}]`));
167
+ const preKeyIds = [];
168
+ const listChildren = (0, helpers_1.getNodeChildren)(listNode);
169
+ for (let index = 0; index < listChildren.length; index += 1) {
170
+ const child = listChildren[index];
171
+ preKeyIds.push((0, codec_1.parseUint)((0, codec_1.decodeExactLength)(child.content, `signal digest list[${index}]`, constants_2.SIGNAL_KEY_ID_LENGTH), `signal digest list[${index}]`));
172
+ }
168
173
  const hash = (0, helpers_1.decodeNodeContentBase64OrBytes)(hashNode.content, 'signal digest hash');
169
174
  return {
170
175
  registrationId,
@@ -7,6 +7,7 @@ const jid_1 = require("../../protocol/jid");
7
7
  const codec_1 = require("../api/codec");
8
8
  const constants_2 = require("../api/constants");
9
9
  const helpers_1 = require("../../transport/node/helpers");
10
+ const query_1 = require("../../transport/node/query");
10
11
  class SignalIdentitySyncApi {
11
12
  constructor(options) {
12
13
  this.logger = options.logger;
@@ -17,10 +18,28 @@ class SignalIdentitySyncApi {
17
18
  this.hostDomain = options.hostDomain ?? constants_1.WA_DEFAULTS.HOST_DOMAIN;
18
19
  }
19
20
  async syncIdentityKeys(targetJids, timeoutMs = this.defaultTimeoutMs) {
20
- const normalizedTargets = [...new Set(targetJids.map((jid) => (0, jid_1.normalizeDeviceJid)(jid)))];
21
+ const normalizedTargets = [];
22
+ const dedup = new Set();
23
+ for (let index = 0; index < targetJids.length; index += 1) {
24
+ const normalized = (0, jid_1.normalizeDeviceJid)(targetJids[index]);
25
+ if (dedup.has(normalized)) {
26
+ continue;
27
+ }
28
+ dedup.add(normalized);
29
+ normalizedTargets.push(normalized);
30
+ }
21
31
  if (normalizedTargets.length === 0) {
22
32
  return [];
23
33
  }
34
+ const users = new Array(normalizedTargets.length);
35
+ for (let index = 0; index < normalizedTargets.length; index += 1) {
36
+ users[index] = {
37
+ tag: constants_1.WA_NODE_TAGS.USER,
38
+ attrs: {
39
+ jid: normalizedTargets[index]
40
+ }
41
+ };
42
+ }
24
43
  this.logger.debug('signal identity sync request', {
25
44
  targets: normalizedTargets.length,
26
45
  timeoutMs
@@ -36,20 +55,22 @@ class SignalIdentitySyncApi {
36
55
  {
37
56
  tag: constants_1.WA_NODE_TAGS.IDENTITY,
38
57
  attrs: {},
39
- content: normalizedTargets.map((jid) => ({
40
- tag: constants_1.WA_NODE_TAGS.USER,
41
- attrs: { jid }
42
- }))
58
+ content: users
43
59
  }
44
60
  ]
45
61
  }, timeoutMs);
46
62
  const entries = this.parseIdentitySyncResponse(response, normalizedTargets);
47
63
  const { signalStore } = this;
48
64
  if (signalStore && entries.length > 0) {
49
- await signalStore.setRemoteIdentities(entries.map((entry) => ({
50
- address: (0, jid_1.parseSignalAddressFromJid)(entry.jid),
51
- identityKey: (0, keys_1.toSerializedPubKey)(entry.identity)
52
- })));
65
+ const identities = new Array(entries.length);
66
+ for (let index = 0; index < entries.length; index += 1) {
67
+ const entry = entries[index];
68
+ identities[index] = {
69
+ address: (0, jid_1.parseSignalAddressFromJid)(entry.jid),
70
+ identityKey: (0, keys_1.toSerializedPubKey)(entry.identity)
71
+ };
72
+ }
73
+ await signalStore.setRemoteIdentities(identities);
53
74
  }
54
75
  this.logger.debug('signal identity sync success', {
55
76
  requested: normalizedTargets.length,
@@ -58,31 +79,19 @@ class SignalIdentitySyncApi {
58
79
  return entries;
59
80
  }
60
81
  parseIdentitySyncResponse(node, requestedJids) {
61
- if (node.tag !== constants_1.WA_NODE_TAGS.IQ) {
62
- throw new Error(`invalid identity sync response tag: ${node.tag}`);
63
- }
64
- if (node.attrs.type === constants_1.WA_IQ_TYPES.ERROR) {
65
- const errorNode = (0, helpers_1.findNodeChild)(node, constants_1.WA_NODE_TAGS.ERROR);
66
- if (!errorNode) {
67
- throw new Error(`identity sync iq error for ${node.attrs.id ?? 'unknown id'}`);
68
- }
69
- const code = errorNode.attrs.code ?? 'unknown';
70
- const text = errorNode.attrs.text ?? errorNode.attrs.type ?? 'unknown';
71
- throw new Error(`identity sync iq error (${code} ${text})`);
72
- }
73
- if (node.attrs.type !== constants_1.WA_IQ_TYPES.RESULT) {
74
- throw new Error(`invalid identity sync response type: ${node.attrs.type ?? 'unknown'}`);
75
- }
82
+ (0, query_1.assertIqResult)(node, 'identity sync');
76
83
  const listNode = (0, helpers_1.findNodeChild)(node, constants_1.WA_NODE_TAGS.LIST);
77
84
  if (!listNode) {
78
85
  throw new Error('identity sync response missing list node');
79
86
  }
80
87
  const requested = new Set(requestedJids);
81
88
  const userNodes = (0, helpers_1.getNodeChildrenByTag)(listNode, constants_1.WA_NODE_TAGS.USER);
82
- return userNodes.flatMap((userNode) => {
89
+ const parsed = [];
90
+ for (let index = 0; index < userNodes.length; index += 1) {
91
+ const userNode = userNodes[index];
83
92
  const jid = userNode.attrs.jid ? (0, jid_1.normalizeDeviceJid)(userNode.attrs.jid) : '';
84
93
  if (!jid || !requested.has(jid)) {
85
- return [];
94
+ continue;
86
95
  }
87
96
  const errorNode = (0, helpers_1.findNodeChild)(userNode, constants_1.WA_NODE_TAGS.ERROR);
88
97
  if (errorNode) {
@@ -91,7 +100,7 @@ class SignalIdentitySyncApi {
91
100
  code: errorNode.attrs.code,
92
101
  text: errorNode.attrs.text
93
102
  });
94
- return [];
103
+ continue;
95
104
  }
96
105
  const identityNode = (0, helpers_1.findNodeChild)(userNode, constants_1.WA_NODE_TAGS.IDENTITY);
97
106
  if (!identityNode) {
@@ -102,14 +111,20 @@ class SignalIdentitySyncApi {
102
111
  const parsedType = typeNode
103
112
  ? (0, codec_1.parseUint)((0, codec_1.decodeExactLength)(typeNode.content, 'identity sync type', constants_2.SIGNAL_KEY_BUNDLE_TYPE_LENGTH), 'identity sync type')
104
113
  : undefined;
105
- return [
106
- {
114
+ if (parsedType === undefined) {
115
+ parsed.push({
107
116
  jid,
108
- identity,
109
- ...(parsedType !== undefined ? { type: parsedType } : {})
110
- }
111
- ];
112
- });
117
+ identity
118
+ });
119
+ continue;
120
+ }
121
+ parsed.push({
122
+ jid,
123
+ identity,
124
+ type: parsedType
125
+ });
126
+ }
127
+ return parsed;
113
128
  }
114
129
  }
115
130
  exports.SignalIdentitySyncApi = SignalIdentitySyncApi;