@nerxa/socketon 0.0.0-stage → 1.21.0

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 (289) hide show
  1. package/EXPERIMENTAL.md +350 -0
  2. package/LICENSE +24 -0
  3. package/README.md +6317 -2
  4. package/WAProto/index.d.ts +17301 -0
  5. package/WAProto/index.js +125088 -0
  6. package/engine-requirements.js +14 -0
  7. package/package.json +100 -4
  8. package/src/Defaults/index.d.ts +152 -0
  9. package/src/Defaults/index.js +164 -0
  10. package/src/MessageBuilder/bot-signature.d.ts +22 -0
  11. package/src/MessageBuilder/bot-signature.js +122 -0
  12. package/src/MessageBuilder/extras.d.ts +449 -0
  13. package/src/MessageBuilder/extras.js +1356 -0
  14. package/src/MessageBuilder/index.d.ts +210 -0
  15. package/src/MessageBuilder/index.js +3594 -0
  16. package/src/MessageBuilder/metaai.d.ts +438 -0
  17. package/src/MessageBuilder/metaai.js +890 -0
  18. package/src/Signal/Group/ciphertext-message.d.ts +9 -0
  19. package/src/Signal/Group/ciphertext-message.js +11 -0
  20. package/src/Signal/Group/group-session-builder.d.ts +7 -0
  21. package/src/Signal/Group/group-session-builder.js +27 -0
  22. package/src/Signal/Group/group_cipher.d.ts +11 -0
  23. package/src/Signal/Group/group_cipher.js +78 -0
  24. package/src/Signal/Group/index.d.ts +11 -0
  25. package/src/Signal/Group/index.js +11 -0
  26. package/src/Signal/Group/keyhelper.d.ts +6 -0
  27. package/src/Signal/Group/keyhelper.js +17 -0
  28. package/src/Signal/Group/sender-chain-key.d.ts +13 -0
  29. package/src/Signal/Group/sender-chain-key.js +25 -0
  30. package/src/Signal/Group/sender-key-distribution-message.d.ts +16 -0
  31. package/src/Signal/Group/sender-key-distribution-message.js +62 -0
  32. package/src/Signal/Group/sender-key-message.d.ts +18 -0
  33. package/src/Signal/Group/sender-key-message.js +65 -0
  34. package/src/Signal/Group/sender-key-name.d.ts +11 -0
  35. package/src/Signal/Group/sender-key-name.js +47 -0
  36. package/src/Signal/Group/sender-key-record.d.ts +12 -0
  37. package/src/Signal/Group/sender-key-record.js +40 -0
  38. package/src/Signal/Group/sender-key-state.d.ts +16 -0
  39. package/src/Signal/Group/sender-key-state.js +83 -0
  40. package/src/Signal/Group/sender-message-key.d.ts +11 -0
  41. package/src/Signal/Group/sender-message-key.js +25 -0
  42. package/src/Signal/libsignal.d.ts +54 -0
  43. package/src/Signal/libsignal.js +427 -0
  44. package/src/Signal/lid-mapping.d.ts +20 -0
  45. package/src/Signal/lid-mapping.js +274 -0
  46. package/src/Socket/Client/index.d.ts +2 -0
  47. package/src/Socket/Client/index.js +3 -0
  48. package/src/Socket/Client/tcp.js +177 -0
  49. package/src/Socket/Client/types.d.ts +5 -0
  50. package/src/Socket/Client/types.js +9 -0
  51. package/src/Socket/Client/websocket.d.ts +12 -0
  52. package/src/Socket/Client/websocket.js +53 -0
  53. package/src/Socket/business.d.ts +431 -0
  54. package/src/Socket/business.js +379 -0
  55. package/src/Socket/chats.d.ts +247 -0
  56. package/src/Socket/chats.js +1484 -0
  57. package/src/Socket/communities.d.ts +598 -0
  58. package/src/Socket/communities.js +421 -0
  59. package/src/Socket/email.js +145 -0
  60. package/src/Socket/groups.d.ts +298 -0
  61. package/src/Socket/groups.js +339 -0
  62. package/src/Socket/index.d.ts +581 -0
  63. package/src/Socket/index.js +37 -0
  64. package/src/Socket/messages-recv.d.ts +370 -0
  65. package/src/Socket/messages-recv.js +1864 -0
  66. package/src/Socket/messages-send.d.ts +390 -0
  67. package/src/Socket/messages-send.js +1574 -0
  68. package/src/Socket/mex.d.ts +1 -0
  69. package/src/Socket/mex.js +44 -0
  70. package/src/Socket/mobile-builders.js +72 -0
  71. package/src/Socket/mobile.js +424 -0
  72. package/src/Socket/newsletter.d.ts +468 -0
  73. package/src/Socket/newsletter.js +671 -0
  74. package/src/Socket/socket.d.ts +77 -0
  75. package/src/Socket/socket.js +1106 -0
  76. package/src/Store/index.d.ts +3 -0
  77. package/src/Store/index.js +3 -0
  78. package/src/Store/make-in-memory-store.d.ts +62 -0
  79. package/src/Store/make-in-memory-store.js +396 -0
  80. package/src/Store/make-ordered-dictionary.d.ts +12 -0
  81. package/src/Store/make-ordered-dictionary.js +78 -0
  82. package/src/Store/object-repository.d.ts +10 -0
  83. package/src/Store/object-repository.js +23 -0
  84. package/src/Types/Auth.d.ts +1 -0
  85. package/src/Types/Auth.js +1 -0
  86. package/src/Types/Bussines.d.ts +1 -0
  87. package/src/Types/Bussines.js +1 -0
  88. package/src/Types/Call.d.ts +1 -0
  89. package/src/Types/Call.js +1 -0
  90. package/src/Types/Chat.d.ts +2 -0
  91. package/src/Types/Chat.js +24 -0
  92. package/src/Types/Contact.d.ts +1 -0
  93. package/src/Types/Contact.js +1 -0
  94. package/src/Types/Events.d.ts +1 -0
  95. package/src/Types/Events.js +0 -0
  96. package/src/Types/GroupMetadata.d.ts +5 -0
  97. package/src/Types/GroupMetadata.js +6 -0
  98. package/src/Types/Label.d.ts +2 -0
  99. package/src/Types/Label.js +24 -0
  100. package/src/Types/LabelAssociation.d.ts +2 -0
  101. package/src/Types/LabelAssociation.js +6 -0
  102. package/src/Types/Message.d.ts +13 -0
  103. package/src/Types/Message.js +23 -0
  104. package/src/Types/Mex.d.ts +2 -0
  105. package/src/Types/Mex.js +95 -0
  106. package/src/Types/Product.d.ts +1 -0
  107. package/src/Types/Product.js +1 -0
  108. package/src/Types/Signal.d.ts +1 -0
  109. package/src/Types/Signal.js +0 -0
  110. package/src/Types/Socket.d.ts +1 -0
  111. package/src/Types/Socket.js +0 -0
  112. package/src/Types/State.d.ts +5 -0
  113. package/src/Types/State.js +54 -0
  114. package/src/Types/USync.d.ts +1 -0
  115. package/src/Types/USync.js +0 -0
  116. package/src/Types/index.d.ts +13 -0
  117. package/src/Types/index.js +25 -0
  118. package/src/Utils/auth-store.d.ts +29 -0
  119. package/src/Utils/auth-store.js +76 -0
  120. package/src/Utils/auth-utils.d.ts +58 -0
  121. package/src/Utils/auth-utils.js +273 -0
  122. package/src/Utils/browser-utils.d.ts +9 -0
  123. package/src/Utils/browser-utils.js +28 -0
  124. package/src/Utils/business.d.ts +49 -0
  125. package/src/Utils/business.js +226 -0
  126. package/src/Utils/chat-utils.d.ts +408 -0
  127. package/src/Utils/chat-utils.js +846 -0
  128. package/src/Utils/companion-reg-client-utils.d.ts +4 -0
  129. package/src/Utils/companion-reg-client-utils.js +39 -0
  130. package/src/Utils/crypto.d.ts +39 -0
  131. package/src/Utils/crypto.js +110 -0
  132. package/src/Utils/decode-wa-message.d.ts +99 -0
  133. package/src/Utils/decode-wa-message.js +373 -0
  134. package/src/Utils/event-buffer.d.ts +22 -0
  135. package/src/Utils/event-buffer.js +624 -0
  136. package/src/Utils/generics.d.ts +63 -0
  137. package/src/Utils/generics.js +375 -0
  138. package/src/Utils/group-metadata-cache.d.ts +4 -0
  139. package/src/Utils/group-metadata-cache.js +25 -0
  140. package/src/Utils/history.d.ts +75 -0
  141. package/src/Utils/history.js +131 -0
  142. package/src/Utils/html-app.d.ts +19 -0
  143. package/src/Utils/html-app.js +96 -0
  144. package/src/Utils/html-multiplayer.d.ts +10 -0
  145. package/src/Utils/html-multiplayer.js +94 -0
  146. package/src/Utils/identity-change-handler.d.ts +13 -0
  147. package/src/Utils/identity-change-handler.js +48 -0
  148. package/src/Utils/index.d.ts +44 -0
  149. package/src/Utils/index.js +44 -0
  150. package/src/Utils/link-preview-metadata.d.ts +47 -0
  151. package/src/Utils/link-preview-metadata.js +88 -0
  152. package/src/Utils/link-preview.d.ts +12 -0
  153. package/src/Utils/link-preview.js +79 -0
  154. package/src/Utils/logger.d.ts +2 -0
  155. package/src/Utils/logger.js +2 -0
  156. package/src/Utils/lt-hash.d.ts +6 -0
  157. package/src/Utils/lt-hash.js +3 -0
  158. package/src/Utils/make-mutex.d.ts +6 -0
  159. package/src/Utils/make-mutex.js +32 -0
  160. package/src/Utils/message-retry-manager.d.ts +79 -0
  161. package/src/Utils/message-retry-manager.js +255 -0
  162. package/src/Utils/messages-media.d.ts +136 -0
  163. package/src/Utils/messages-media.js +814 -0
  164. package/src/Utils/messages.d.ts +62 -0
  165. package/src/Utils/messages.js +2223 -0
  166. package/src/Utils/mobile-companion-crypto.js +191 -0
  167. package/src/Utils/modern-messages.d.ts +150 -0
  168. package/src/Utils/modern-messages.js +496 -0
  169. package/src/Utils/native-flow.d.ts +11 -0
  170. package/src/Utils/native-flow.js +115 -0
  171. package/src/Utils/newsletter-status.d.ts +158 -0
  172. package/src/Utils/newsletter-status.js +651 -0
  173. package/src/Utils/noise-handler.d.ts +20 -0
  174. package/src/Utils/noise-handler.js +200 -0
  175. package/src/Utils/offline-node-processor.d.ts +9 -0
  176. package/src/Utils/offline-node-processor.js +33 -0
  177. package/src/Utils/optional-media.d.ts +3 -0
  178. package/src/Utils/optional-media.js +45 -0
  179. package/src/Utils/payment-messages.d.ts +66 -0
  180. package/src/Utils/payment-messages.js +160 -0
  181. package/src/Utils/pre-key-manager.d.ts +25 -0
  182. package/src/Utils/pre-key-manager.js +95 -0
  183. package/src/Utils/process-message.d.ts +60 -0
  184. package/src/Utils/process-message.js +688 -0
  185. package/src/Utils/reporting-utils.d.ts +12 -0
  186. package/src/Utils/reporting-utils.js +256 -0
  187. package/src/Utils/scheduled-message.d.ts +51 -0
  188. package/src/Utils/scheduled-message.js +112 -0
  189. package/src/Utils/sender-key-memory.d.ts +19 -0
  190. package/src/Utils/sender-key-memory.js +51 -0
  191. package/src/Utils/signal.d.ts +82 -0
  192. package/src/Utils/signal.js +193 -0
  193. package/src/Utils/stanza-ack.d.ts +16 -0
  194. package/src/Utils/stanza-ack.js +30 -0
  195. package/src/Utils/status-stickers.d.ts +127 -0
  196. package/src/Utils/status-stickers.js +260 -0
  197. package/src/Utils/sync-action-utils.d.ts +2 -0
  198. package/src/Utils/sync-action-utils.js +44 -0
  199. package/src/Utils/tc-token-utils.d.ts +29 -0
  200. package/src/Utils/tc-token-utils.js +155 -0
  201. package/src/Utils/ttl-cache.d.ts +28 -0
  202. package/src/Utils/ttl-cache.js +120 -0
  203. package/src/Utils/use-mongo-auth-state.d.ts +21 -0
  204. package/src/Utils/use-mongo-auth-state.js +67 -0
  205. package/src/Utils/use-multi-file-auth-state.d.ts +11 -0
  206. package/src/Utils/use-multi-file-auth-state.js +106 -0
  207. package/src/Utils/use-mysql-auth-state.d.ts +19 -0
  208. package/src/Utils/use-mysql-auth-state.js +79 -0
  209. package/src/Utils/use-postgres-auth-state.d.ts +19 -0
  210. package/src/Utils/use-postgres-auth-state.js +79 -0
  211. package/src/Utils/use-redis-auth-state.d.ts +19 -0
  212. package/src/Utils/use-redis-auth-state.js +89 -0
  213. package/src/Utils/use-single-file-auth-state.d.ts +10 -0
  214. package/src/Utils/use-single-file-auth-state.js +108 -0
  215. package/src/Utils/use-sqlite-auth-state.d.ts +12 -0
  216. package/src/Utils/use-sqlite-auth-state.js +120 -0
  217. package/src/Utils/useNekoDBAuth.d.ts +19 -0
  218. package/src/Utils/useNekoDBAuth.js +111 -0
  219. package/src/Utils/username.d.ts +12 -0
  220. package/src/Utils/username.js +58 -0
  221. package/src/Utils/validate-connection.d.ts +43 -0
  222. package/src/Utils/validate-connection.js +277 -0
  223. package/src/Utils/voice-recognition.d.ts +53 -0
  224. package/src/Utils/voice-recognition.js +219 -0
  225. package/src/Voip/audio-feeder.d.ts +24 -0
  226. package/src/Voip/audio-feeder.js +268 -0
  227. package/src/Voip/index.d.ts +88 -0
  228. package/src/Voip/index.js +936 -0
  229. package/src/Voip/relay-transport.d.ts +5 -0
  230. package/src/Voip/relay-transport.js +824 -0
  231. package/src/Voip/signaling.d.ts +11 -0
  232. package/src/Voip/signaling.js +1099 -0
  233. package/src/Voip/types.d.ts +2 -0
  234. package/src/Voip/types.js +12 -0
  235. package/src/Voip/video-feeder.d.ts +24 -0
  236. package/src/Voip/video-feeder.js +268 -0
  237. package/src/Voip/wasm-engine.d.ts +26 -0
  238. package/src/Voip/wasm-engine.js +1307 -0
  239. package/src/Voip/worker-bootstrap.js +1023 -0
  240. package/src/WABinary/constants.d.ts +22 -0
  241. package/src/WABinary/constants.js +1300 -0
  242. package/src/WABinary/decode.d.ts +9 -0
  243. package/src/WABinary/decode.js +261 -0
  244. package/src/WABinary/encode.d.ts +2 -0
  245. package/src/WABinary/encode.js +219 -0
  246. package/src/WABinary/generic-utils.d.ts +76 -0
  247. package/src/WABinary/generic-utils.js +214 -0
  248. package/src/WABinary/index.d.ts +5 -0
  249. package/src/WABinary/index.js +5 -0
  250. package/src/WABinary/jid-utils.d.ts +30 -0
  251. package/src/WABinary/jid-utils.js +97 -0
  252. package/src/WABinary/types.d.ts +1 -0
  253. package/src/WABinary/types.js +0 -0
  254. package/src/WAM/BinaryInfo.d.ts +7 -0
  255. package/src/WAM/BinaryInfo.js +9 -0
  256. package/src/WAM/constants.d.ts +34926 -0
  257. package/src/WAM/constants.js +22852 -0
  258. package/src/WAM/encode.d.ts +1 -0
  259. package/src/WAM/encode.js +148 -0
  260. package/src/WAM/index.d.ts +3 -0
  261. package/src/WAM/index.js +3 -0
  262. package/src/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
  263. package/src/WAUSync/Protocols/USyncContactProtocol.js +50 -0
  264. package/src/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
  265. package/src/WAUSync/Protocols/USyncDeviceProtocol.js +51 -0
  266. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  267. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
  268. package/src/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  269. package/src/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
  270. package/src/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
  271. package/src/WAUSync/Protocols/USyncUsernameProtocol.js +23 -0
  272. package/src/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
  273. package/src/WAUSync/Protocols/UsyncBotProfileProtocol.js +49 -0
  274. package/src/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
  275. package/src/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  276. package/src/WAUSync/Protocols/index.d.ts +5 -0
  277. package/src/WAUSync/Protocols/index.js +5 -0
  278. package/src/WAUSync/USyncQuery.d.ts +20 -0
  279. package/src/WAUSync/USyncQuery.js +93 -0
  280. package/src/WAUSync/USyncUser.d.ts +16 -0
  281. package/src/WAUSync/USyncUser.js +30 -0
  282. package/src/WAUSync/index.d.ts +3 -0
  283. package/src/WAUSync/index.js +3 -0
  284. package/src/assets/wasm/README.md +76 -0
  285. package/src/assets/wasm/loader.js +5 -0
  286. package/src/assets/wasm/whatsapp.wasm +0 -0
  287. package/src/assets/wasm/worker-modules.js +273 -0
  288. package/src/index.d.ts +43 -0
  289. package/src/index.js +48 -0
@@ -0,0 +1,12 @@
1
+ export function shouldIncludeReportingToken(message: any): boolean;
2
+ export function getMessageReportingToken(msgProtobuf: any, message: any, key: any): Promise<{
3
+ tag: string;
4
+ attrs: {};
5
+ content: {
6
+ tag: string;
7
+ attrs: {
8
+ v: string;
9
+ };
10
+ content: any;
11
+ }[];
12
+ } | null>;
@@ -0,0 +1,256 @@
1
+ import { createHmac } from 'crypto';
2
+ import { hkdf } from './crypto.js';
3
+ const reportingFields = [
4
+ { f: 1 },
5
+ {
6
+ f: 3,
7
+ s: [{ f: 2 }, { f: 3 }, { f: 8 }, { f: 11 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 25 }]
8
+ },
9
+ { f: 4, s: [{ f: 1 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
10
+ { f: 5, s: [{ f: 3 }, { f: 4 }, { f: 5 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
11
+ { f: 6, s: [{ f: 1 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 30 }] },
12
+ { f: 7, s: [{ f: 2 }, { f: 7 }, { f: 10 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
13
+ { f: 8, s: [{ f: 2 }, { f: 7 }, { f: 9 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 21 }] },
14
+ { f: 9, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
15
+ { f: 12, s: [{ f: 1 }, { f: 2 }, { f: 14, m: true }, { f: 15 }] },
16
+ { f: 18, s: [{ f: 6 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
17
+ { f: 26, s: [{ f: 4 }, { f: 5 }, { f: 8 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
18
+ { f: 28, s: [{ f: 1 }, { f: 2 }, { f: 4 }, { f: 5 }, { f: 6 }, { f: 7, s: [{ f: 21 }, { f: 22 }] }] },
19
+ { f: 37, s: [{ f: 1, m: true }] },
20
+ {
21
+ f: 49,
22
+ s: [
23
+ { f: 2 },
24
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
25
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
26
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
27
+ ]
28
+ },
29
+ { f: 53, s: [{ f: 1, m: true }] },
30
+ { f: 55, s: [{ f: 1, m: true }] },
31
+ { f: 58, s: [{ f: 1, m: true }] },
32
+ { f: 59, s: [{ f: 1, m: true }] },
33
+ {
34
+ f: 60,
35
+ s: [
36
+ { f: 2 },
37
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
38
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
39
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
40
+ ]
41
+ },
42
+ {
43
+ f: 64,
44
+ s: [
45
+ { f: 2 },
46
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
47
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
48
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
49
+ ]
50
+ },
51
+ { f: 66, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
52
+ { f: 74, s: [{ f: 1, m: true }] },
53
+ { f: 87, s: [{ f: 1, m: true }] },
54
+ { f: 88, s: [{ f: 1 }, { f: 2, s: [{ f: 1 }] }, { f: 3, s: [{ f: 21 }, { f: 22 }] }] },
55
+ { f: 92, s: [{ f: 1, m: true }] },
56
+ { f: 93, s: [{ f: 1, m: true }] },
57
+ { f: 94, s: [{ f: 1, m: true }] }
58
+ ];
59
+ const compileReportingFields = (fields) => {
60
+ const map = new Map();
61
+ for (const f of fields) {
62
+ map.set(f.f, {
63
+ m: f.m,
64
+ children: f.s ? compileReportingFields(f.s) : undefined
65
+ });
66
+ }
67
+ return map;
68
+ };
69
+ const compiledReportingFields = compileReportingFields(reportingFields);
70
+ const EMPTY_MAP = new Map();
71
+ const ENC_SECRET_REPORT_TOKEN = 'Report Token';
72
+ const WIRE = {
73
+ VARINT: 0,
74
+ FIXED64: 1,
75
+ BYTES: 2,
76
+ FIXED32: 5
77
+ };
78
+ export const shouldIncludeReportingToken = (message) => !message.reactionMessage &&
79
+ !message.encReactionMessage &&
80
+ !message.encEventResponseMessage &&
81
+ !message.pollUpdateMessage;
82
+ const generateMsgSecretKey = (modificationType, origMsgId, origMsgSender, modificationSender, origMsgSecret) => {
83
+ const useCaseSecret = Buffer.concat([
84
+ Buffer.from(origMsgId, 'utf8'),
85
+ Buffer.from(origMsgSender, 'utf8'),
86
+ Buffer.from(modificationSender, 'utf8'),
87
+ Buffer.from(modificationType, 'utf8')
88
+ ]);
89
+ return hkdf(origMsgSecret, 32, { info: useCaseSecret.toString('latin1') });
90
+ };
91
+ const extractReportingTokenContent = (data, cfg) => {
92
+ const out = [];
93
+ let i = 0;
94
+ while (i < data.length) {
95
+ const tag = decodeVarint(data, i);
96
+ if (!tag.ok) {
97
+ return null;
98
+ }
99
+ const fieldNum = tag.value >> 3;
100
+ const wireType = tag.value & 0x7;
101
+ const fieldStart = i;
102
+ i += tag.bytes;
103
+ const fieldCfg = cfg.get(fieldNum);
104
+ const pushSlice = (end) => {
105
+ if (end > data.length) {
106
+ return false;
107
+ }
108
+ out.push({ num: fieldNum, bytes: data.subarray(fieldStart, end) });
109
+ i = end;
110
+ return true;
111
+ };
112
+ const skip = (end) => {
113
+ if (end > data.length) {
114
+ return false;
115
+ }
116
+ i = end;
117
+ return true;
118
+ };
119
+ if (wireType === WIRE.VARINT) {
120
+ const v = decodeVarint(data, i);
121
+ if (!v.ok) {
122
+ return null;
123
+ }
124
+ const end = i + v.bytes;
125
+ if (!fieldCfg) {
126
+ if (!skip(end)) {
127
+ return null;
128
+ }
129
+ continue;
130
+ }
131
+ if (!pushSlice(end)) {
132
+ return null;
133
+ }
134
+ continue;
135
+ }
136
+ if (wireType === WIRE.FIXED64) {
137
+ const end = i + 8;
138
+ if (!fieldCfg) {
139
+ if (!skip(end)) {
140
+ return null;
141
+ }
142
+ continue;
143
+ }
144
+ if (!pushSlice(end)) {
145
+ return null;
146
+ }
147
+ continue;
148
+ }
149
+ if (wireType === WIRE.FIXED32) {
150
+ const end = i + 4;
151
+ if (!fieldCfg) {
152
+ if (!skip(end)) {
153
+ return null;
154
+ }
155
+ continue;
156
+ }
157
+ if (!pushSlice(end)) {
158
+ return null;
159
+ }
160
+ continue;
161
+ }
162
+ if (wireType === WIRE.BYTES) {
163
+ const len = decodeVarint(data, i);
164
+ if (!len.ok) {
165
+ return null;
166
+ }
167
+ const valStart = i + len.bytes;
168
+ const valEnd = valStart + len.value;
169
+ if (valEnd > data.length) {
170
+ return null;
171
+ }
172
+ if (!fieldCfg) {
173
+ i = valEnd;
174
+ continue;
175
+ }
176
+ if (fieldCfg.m || fieldCfg.children) {
177
+ const sub = extractReportingTokenContent(data.subarray(valStart, valEnd), fieldCfg.children ?? EMPTY_MAP);
178
+ if (sub === null) {
179
+ return null;
180
+ }
181
+ if (sub.length > 0) {
182
+ const newTag = encodeVarint(tag.value);
183
+ const newLen = encodeVarint(sub.length);
184
+ out.push({
185
+ num: fieldNum,
186
+ bytes: Buffer.concat([newTag, newLen, sub])
187
+ });
188
+ }
189
+ i = valEnd;
190
+ continue;
191
+ }
192
+ out.push({ num: fieldNum, bytes: data.subarray(fieldStart, valEnd) });
193
+ i = valEnd;
194
+ continue;
195
+ }
196
+ return null;
197
+ }
198
+ if (out.length === 0) {
199
+ return Buffer.alloc(0);
200
+ }
201
+ out.sort((a, b) => a.num - b.num);
202
+ return Buffer.concat(out.map(f => f.bytes));
203
+ };
204
+ const decodeVarint = (buffer, offset) => {
205
+ let value = 0;
206
+ let bytes = 0;
207
+ let shift = 0;
208
+ while (offset + bytes < buffer.length) {
209
+ const current = buffer[offset + bytes];
210
+ value |= (current & 0x7f) << shift;
211
+ bytes++;
212
+ if ((current & 0x80) === 0) {
213
+ return { value, bytes, ok: true };
214
+ }
215
+ shift += 7;
216
+ if (shift > 35) {
217
+ return { value: 0, bytes: 0, ok: false };
218
+ }
219
+ }
220
+ return { value: 0, bytes: 0, ok: false };
221
+ };
222
+ const encodeVarint = (value) => {
223
+ const parts = [];
224
+ let remaining = value >>> 0;
225
+ while (remaining > 0x7f) {
226
+ parts.push((remaining & 0x7f) | 0x80);
227
+ remaining >>>= 7;
228
+ }
229
+ parts.push(remaining);
230
+ return Buffer.from(parts);
231
+ };
232
+ export const getMessageReportingToken = async (msgProtobuf, message, key) => {
233
+ const msgSecret = message.messageContextInfo?.messageSecret;
234
+ if (!msgSecret || !key.id) {
235
+ return null;
236
+ }
237
+ const from = key.fromMe ? key.remoteJid : key.participant || key.remoteJid;
238
+ const to = key.fromMe ? key.participant || key.remoteJid : key.remoteJid;
239
+ const reportingSecret = generateMsgSecretKey(ENC_SECRET_REPORT_TOKEN, key.id, from, to, msgSecret);
240
+ const content = extractReportingTokenContent(msgProtobuf, compiledReportingFields);
241
+ if (!content || content.length === 0) {
242
+ return null;
243
+ }
244
+ const reportingToken = createHmac('sha256', reportingSecret).update(content).digest().subarray(0, 16);
245
+ return {
246
+ tag: 'reporting',
247
+ attrs: {},
248
+ content: [
249
+ {
250
+ tag: 'reporting_token',
251
+ attrs: { v: '2' },
252
+ content: reportingToken
253
+ }
254
+ ]
255
+ };
256
+ };
@@ -0,0 +1,51 @@
1
+ import type { proto } from '../../WAProto/index.js'
2
+
3
+ export declare const SCHEDULED_MSG_META_TYPE: 'scheduled_message'
4
+ export declare const SCHEDULED_MSG_MAX_PER_CHAT: number
5
+ export declare const SCHEDULED_MSG_MAX_MEDIA: number
6
+ export declare const SCHEDULED_MSG_RESOURCE_LIMIT_NACK_CODE: number
7
+ export declare const SCHEDULED_MSG_REVEAL_KEY_BYTES: number
8
+ export declare const SCHEDULED_MSG_REVEAL_KEY_IV_BYTES: number
9
+ export declare const SCHEDULED_MSG_REVEAL_KEY_RETENTION_DAYS: number
10
+ export declare const SCHEDULED_MSG_TAG_BYTES: number
11
+
12
+ export interface ScheduledMessageWindow {
13
+ minSeconds: number
14
+ maxSeconds: number
15
+ }
16
+
17
+ export declare const SCHEDULED_MSG_WINDOW: Readonly<{
18
+ chat: ScheduledMessageWindow
19
+ newsletter: ScheduledMessageWindow
20
+ }>
21
+
22
+ export declare function generateRevealKey(): Buffer
23
+ export declare function generateRevealKeyId(): string
24
+ export declare function encryptWithRevealKey(plaintext: Uint8Array, revealKey: Uint8Array): {
25
+ encIv: Buffer
26
+ encPayload: Buffer
27
+ }
28
+ export declare function decryptWithRevealKey(encPayload: Uint8Array, encIv: Uint8Array, revealKey: Uint8Array): Buffer
29
+ export declare function buildConditionalRevealMessage(input: {
30
+ encPayload: Uint8Array
31
+ encIv: Uint8Array
32
+ revealKeyId: string
33
+ }): { conditionalRevealMessage: proto.Message.IConditionalRevealMessage }
34
+ export declare function encodeScheduledMessage(message: proto.IMessage, revealKey?: Uint8Array): {
35
+ revealKey: Uint8Array
36
+ revealKeyId: string
37
+ encIv: Buffer
38
+ encPayload: Buffer
39
+ message: { conditionalRevealMessage: proto.Message.IConditionalRevealMessage }
40
+ }
41
+ export declare function decodeScheduledMessage(conditionalRevealMessage: any, revealKey: Uint8Array): proto.Message
42
+ export declare function buildScheduledMsgMetaNode(input: {
43
+ kind?: 'schedule' | 'reveal'
44
+ scheduledTimestampS?: number
45
+ revealKeyId: string
46
+ revealKey?: Uint8Array
47
+ }): any
48
+ export declare function buildUnscheduleProtocolMessage(key: proto.IMessageKey): {
49
+ protocolMessage: proto.Message.IProtocolMessage
50
+ }
51
+ export declare function isScheduledTimeValid(scheduledTimestampS: number, nowSeconds?: number, window?: ScheduledMessageWindow): boolean
@@ -0,0 +1,112 @@
1
+ import { createCipheriv, createDecipheriv, randomBytes, randomUUID } from 'crypto'
2
+ import { proto } from '../../WAProto/index.js'
3
+
4
+ export const SCHEDULED_MSG_META_TYPE = 'scheduled_message'
5
+ export const SCHEDULED_MSG_MAX_PER_CHAT = 30
6
+ export const SCHEDULED_MSG_MAX_MEDIA = 1
7
+ export const SCHEDULED_MSG_RESOURCE_LIMIT_NACK_CODE = 419
8
+ export const SCHEDULED_MSG_REVEAL_KEY_BYTES = 32
9
+ export const SCHEDULED_MSG_REVEAL_KEY_IV_BYTES = 12
10
+ export const SCHEDULED_MSG_REVEAL_KEY_RETENTION_DAYS = 30
11
+ export const SCHEDULED_MSG_TAG_BYTES = 16
12
+
13
+ export const SCHEDULED_MSG_WINDOW = Object.freeze({
14
+ chat: { minSeconds: 600, maxSeconds: 1209600 },
15
+ newsletter: { minSeconds: 600, maxSeconds: 2592000 }
16
+ })
17
+
18
+ export const generateRevealKey = () => randomBytes(SCHEDULED_MSG_REVEAL_KEY_BYTES)
19
+
20
+ export const generateRevealKeyId = () => randomUUID()
21
+
22
+ export const encryptWithRevealKey = (plaintext, revealKey) => {
23
+ const key = Buffer.from(revealKey)
24
+ if (key.length !== SCHEDULED_MSG_REVEAL_KEY_BYTES) {
25
+ throw new TypeError(`reveal key must be ${SCHEDULED_MSG_REVEAL_KEY_BYTES} bytes`)
26
+ }
27
+ const encIv = randomBytes(SCHEDULED_MSG_REVEAL_KEY_IV_BYTES)
28
+ const cipher = createCipheriv('aes-256-gcm', key, encIv)
29
+ const body = Buffer.concat([cipher.update(Buffer.from(plaintext)), cipher.final()])
30
+ return { encIv, encPayload: Buffer.concat([body, cipher.getAuthTag()]) }
31
+ }
32
+
33
+ export const decryptWithRevealKey = (encPayload, encIv, revealKey) => {
34
+ const key = Buffer.from(revealKey)
35
+ const payload = Buffer.from(encPayload)
36
+ if (payload.length < SCHEDULED_MSG_TAG_BYTES) {
37
+ throw new TypeError('encrypted payload is too short to carry an auth tag')
38
+ }
39
+ const decipher = createDecipheriv('aes-256-gcm', key, Buffer.from(encIv))
40
+ decipher.setAuthTag(payload.subarray(payload.length - SCHEDULED_MSG_TAG_BYTES))
41
+ return Buffer.concat([
42
+ decipher.update(payload.subarray(0, payload.length - SCHEDULED_MSG_TAG_BYTES)),
43
+ decipher.final()
44
+ ])
45
+ }
46
+
47
+ export const buildConditionalRevealMessage = ({ encPayload, encIv, revealKeyId }) => ({
48
+ conditionalRevealMessage: {
49
+ conditionalRevealMessageType: proto.Message.ConditionalRevealMessage.ConditionalRevealMessageType.SCHEDULED_MESSAGE,
50
+ encPayload: Buffer.from(encPayload),
51
+ encIv: Buffer.from(encIv),
52
+ revealKeyId
53
+ }
54
+ })
55
+
56
+ export const encodeScheduledMessage = (message, revealKey = generateRevealKey()) => {
57
+ const plaintext = proto.Message.encode(proto.Message.fromObject(message)).finish()
58
+ const { encIv, encPayload } = encryptWithRevealKey(plaintext, revealKey)
59
+ const revealKeyId = generateRevealKeyId()
60
+ return {
61
+ revealKey,
62
+ revealKeyId,
63
+ encIv,
64
+ encPayload,
65
+ message: buildConditionalRevealMessage({ encPayload, encIv, revealKeyId })
66
+ }
67
+ }
68
+
69
+ export const decodeScheduledMessage = (conditionalRevealMessage, revealKey) => {
70
+ const inner = conditionalRevealMessage?.conditionalRevealMessage ?? conditionalRevealMessage
71
+ if (!inner?.encPayload || !inner?.encIv) {
72
+ throw new TypeError('conditionalRevealMessage is missing encPayload or encIv')
73
+ }
74
+ return proto.Message.decode(decryptWithRevealKey(inner.encPayload, inner.encIv, revealKey))
75
+ }
76
+
77
+ export const buildScheduledMsgMetaNode = ({ kind = 'schedule', scheduledTimestampS, revealKeyId, revealKey }) => {
78
+ if (!revealKeyId) {
79
+ throw new TypeError('revealKeyId is required')
80
+ }
81
+ if (kind === 'schedule' && !revealKey) {
82
+ throw new TypeError('scheduling requires the reveal key')
83
+ }
84
+ const attrs = { type: SCHEDULED_MSG_META_TYPE }
85
+ if (kind === 'schedule') {
86
+ if (!Number.isFinite(Number(scheduledTimestampS))) {
87
+ throw new TypeError('scheduledTimestampS must be a unix timestamp in seconds')
88
+ }
89
+ attrs.st = String(Math.floor(Number(scheduledTimestampS)))
90
+ }
91
+ return {
92
+ tag: 'meta',
93
+ attrs,
94
+ content: [{
95
+ tag: 'key',
96
+ attrs: { rkid: revealKeyId },
97
+ content: kind === 'schedule' ? Buffer.from(revealKey) : undefined
98
+ }]
99
+ }
100
+ }
101
+
102
+ export const buildUnscheduleProtocolMessage = (key) => ({
103
+ protocolMessage: {
104
+ key,
105
+ type: proto.Message.ProtocolMessage.Type.MESSAGE_UNSCHEDULE
106
+ }
107
+ })
108
+
109
+ export const isScheduledTimeValid = (scheduledTimestampS, nowSeconds = Math.floor(Date.now() / 1000), window = SCHEDULED_MSG_WINDOW.chat) => {
110
+ const rounded = Math.floor(nowSeconds / 60) * 60
111
+ return scheduledTimestampS - rounded >= window.minSeconds && scheduledTimestampS - nowSeconds <= window.maxSeconds
112
+ }
@@ -0,0 +1,19 @@
1
+
2
+ export declare function pickSenderKeyRecipients(
3
+ devices: { jid?: string, device?: number }[],
4
+ senderKeyMap: Record<string, boolean>,
5
+ options?: { force?: boolean, skip?: (device: any) => boolean }
6
+ ): string[]
7
+
8
+ export declare function deliveredSenderKeyJids(nodes: { attrs?: { jid?: string } }[]): Set<string>
9
+
10
+ export declare function senderKeyResetSummary(
11
+ stored: Record<string, Record<string, boolean> | null | undefined> | undefined,
12
+ jid: string
13
+ ): { jid: string, cleared: number, devices: string[] }
14
+
15
+ export declare function commitSenderKeyDelivery(
16
+ senderKeyMap: Record<string, boolean>,
17
+ recipients: string[],
18
+ nodes: { attrs?: { jid?: string } }[]
19
+ ): { marked: string[], skipped: string[] }
@@ -0,0 +1,51 @@
1
+
2
+ export const pickSenderKeyRecipients = (devices, senderKeyMap, { force = false, skip } = {}) => {
3
+ const recipients = []
4
+ for (const device of devices) {
5
+ const deviceJid = device?.jid
6
+ if (!deviceJid) {
7
+ continue
8
+ }
9
+ if (!force && senderKeyMap[deviceJid]) {
10
+ continue
11
+ }
12
+ if (skip && skip(device)) {
13
+ continue
14
+ }
15
+ recipients.push(deviceJid)
16
+ }
17
+ return recipients
18
+ }
19
+
20
+ export const deliveredSenderKeyJids = nodes => {
21
+ const delivered = new Set()
22
+ for (const node of nodes || []) {
23
+ const jid = node?.attrs?.jid
24
+ if (jid) {
25
+ delivered.add(jid)
26
+ }
27
+ }
28
+ return delivered
29
+ }
30
+
31
+ export const senderKeyResetSummary = (stored, jid) => {
32
+ const devices = Object.keys(stored?.[jid] || {})
33
+ return { jid, cleared: devices.length, devices }
34
+ }
35
+
36
+ export const commitSenderKeyDelivery = (senderKeyMap, recipients, nodes) => {
37
+ const delivered = deliveredSenderKeyJids(nodes)
38
+ const marked = []
39
+ const skipped = []
40
+ for (const jid of recipients) {
41
+ if (delivered.has(jid)) {
42
+ senderKeyMap[jid] = true
43
+ marked.push(jid)
44
+ }
45
+ else {
46
+ delete senderKeyMap[jid]
47
+ skipped.push(jid)
48
+ }
49
+ }
50
+ return { marked, skipped }
51
+ }
@@ -0,0 +1,82 @@
1
+ export function createSignalIdentity(wid: any, accountSignatureKey: any): {
2
+ identifier: {
3
+ name: any;
4
+ deviceId: number;
5
+ };
6
+ identifierKey: any;
7
+ };
8
+ export function getPreKeys({ get }: {
9
+ get: any;
10
+ }, min: any, limit: any): Promise<any>;
11
+ export function generateOrGetPreKeys(creds: any, range: any): {
12
+ newPreKeys: {};
13
+ lastPreKeyId: number;
14
+ preKeysRange: any[];
15
+ };
16
+ export function xmppSignedPreKey(key: any): {
17
+ tag: string;
18
+ attrs: {};
19
+ content: {
20
+ tag: string;
21
+ attrs: {};
22
+ content: any;
23
+ }[];
24
+ };
25
+ export function xmppPreKey(pair: any, id: any): {
26
+ tag: string;
27
+ attrs: {};
28
+ content: {
29
+ tag: string;
30
+ attrs: {};
31
+ content: any;
32
+ }[];
33
+ };
34
+ export function extractE2ESessionFromRetryReceipt(receipt: any): {
35
+ registrationId: number | undefined;
36
+ identityKey: any;
37
+ signedPreKey: {
38
+ keyId: number | undefined;
39
+ publicKey: any;
40
+ signature: any;
41
+ };
42
+ preKey: {
43
+ keyId: number | undefined;
44
+ publicKey: any;
45
+ } | undefined;
46
+ } | null;
47
+ export function parseAndInjectE2ESessions(node: any, repository: any): Promise<void>;
48
+ export function extractDeviceJids(result: any, myJid: any, myLid: any, excludeZeroDevices: any): {
49
+ user: any;
50
+ device: any;
51
+ domainType: any;
52
+ server: any;
53
+ }[];
54
+ export function getNextPreKeys({ creds, keys }: {
55
+ creds: any;
56
+ keys: any;
57
+ }, count: any): Promise<{
58
+ update: {
59
+ nextPreKeyId: number;
60
+ firstUnuploadedPreKeyId: number;
61
+ };
62
+ preKeys: any;
63
+ }>;
64
+ export function getNextPreKeysNode(state: any, count: any): Promise<{
65
+ update: {
66
+ nextPreKeyId: number;
67
+ firstUnuploadedPreKeyId: number;
68
+ };
69
+ node: {
70
+ tag: string;
71
+ attrs: {
72
+ xmlns: string;
73
+ type: string;
74
+ to: string;
75
+ };
76
+ content: {
77
+ tag: string;
78
+ attrs: {};
79
+ content: any;
80
+ }[];
81
+ };
82
+ }>;