@synacwave/baileys 0.0.0-stage → 0.4.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 (229) hide show
  1. package/ATTRIBUTION.md +14 -0
  2. package/CHANGELOG.md +35 -0
  3. package/LICENSE +23 -0
  4. package/README.md +493 -2
  5. package/README.upstream.md +2077 -0
  6. package/WAProto/index.d.ts +15942 -0
  7. package/WAProto/index.js +115911 -0
  8. package/engine-requirements.js +13 -0
  9. package/lib/Defaults/index.d.ts +145 -0
  10. package/lib/Defaults/index.js +155 -0
  11. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  12. package/lib/Signal/Group/ciphertext-message.js +12 -0
  13. package/lib/Signal/Group/group-session-builder.d.ts +8 -0
  14. package/lib/Signal/Group/group-session-builder.js +30 -0
  15. package/lib/Signal/Group/group_cipher.d.ts +11 -0
  16. package/lib/Signal/Group/group_cipher.js +82 -0
  17. package/lib/Signal/Group/index.d.ts +12 -0
  18. package/lib/Signal/Group/index.js +12 -0
  19. package/lib/Signal/Group/keyhelper.d.ts +7 -0
  20. package/lib/Signal/Group/keyhelper.js +18 -0
  21. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  22. package/lib/Signal/Group/sender-chain-key.js +26 -0
  23. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  24. package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
  25. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  26. package/lib/Signal/Group/sender-key-message.js +66 -0
  27. package/lib/Signal/Group/sender-key-name.d.ts +12 -0
  28. package/lib/Signal/Group/sender-key-name.js +48 -0
  29. package/lib/Signal/Group/sender-key-record.d.ts +13 -0
  30. package/lib/Signal/Group/sender-key-record.js +41 -0
  31. package/lib/Signal/Group/sender-key-state.d.ts +17 -0
  32. package/lib/Signal/Group/sender-key-state.js +84 -0
  33. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  34. package/lib/Signal/Group/sender-message-key.js +26 -0
  35. package/lib/Signal/libsignal.d.ts +55 -0
  36. package/lib/Signal/libsignal.js +431 -0
  37. package/lib/Signal/lid-mapping.d.ts +21 -0
  38. package/lib/Signal/lid-mapping.js +277 -0
  39. package/lib/Socket/Client/index.d.ts +3 -0
  40. package/lib/Socket/Client/index.js +3 -0
  41. package/lib/Socket/Client/types.d.ts +6 -0
  42. package/lib/Socket/Client/types.js +11 -0
  43. package/lib/Socket/Client/websocket.d.ts +13 -0
  44. package/lib/Socket/Client/websocket.js +54 -0
  45. package/lib/Socket/business.d.ts +395 -0
  46. package/lib/Socket/business.js +380 -0
  47. package/lib/Socket/chats.d.ts +144 -0
  48. package/lib/Socket/chats.js +1219 -0
  49. package/lib/Socket/communities.d.ts +562 -0
  50. package/lib/Socket/communities.js +432 -0
  51. package/lib/Socket/groups.d.ts +286 -0
  52. package/lib/Socket/groups.js +348 -0
  53. package/lib/Socket/index.d.ts +531 -0
  54. package/lib/Socket/index.js +13 -0
  55. package/lib/Socket/messages-recv.d.ts +334 -0
  56. package/lib/Socket/messages-recv.js +1772 -0
  57. package/lib/Socket/messages-send.d.ts +330 -0
  58. package/lib/Socket/messages-send.js +1372 -0
  59. package/lib/Socket/mex.d.ts +2 -0
  60. package/lib/Socket/mex.js +42 -0
  61. package/lib/Socket/newsletter.d.ts +283 -0
  62. package/lib/Socket/newsletter.js +226 -0
  63. package/lib/Socket/socket.d.ts +61 -0
  64. package/lib/Socket/socket.js +970 -0
  65. package/lib/Store/index.d.ts +4 -0
  66. package/lib/Store/index.js +4 -0
  67. package/lib/Store/make-in-memory-store.d.ts +63 -0
  68. package/lib/Store/make-in-memory-store.js +421 -0
  69. package/lib/Store/make-ordered-dictionary.d.ts +13 -0
  70. package/lib/Store/make-ordered-dictionary.js +79 -0
  71. package/lib/Store/object-repository.d.ts +11 -0
  72. package/lib/Store/object-repository.js +24 -0
  73. package/lib/Types/Auth.d.ts +2 -0
  74. package/lib/Types/Auth.js +2 -0
  75. package/lib/Types/Bussines.d.ts +2 -0
  76. package/lib/Types/Bussines.js +2 -0
  77. package/lib/Types/Call.d.ts +2 -0
  78. package/lib/Types/Call.js +2 -0
  79. package/lib/Types/Chat.d.ts +2 -0
  80. package/lib/Types/Chat.js +8 -0
  81. package/lib/Types/Contact.d.ts +2 -0
  82. package/lib/Types/Contact.js +2 -0
  83. package/lib/Types/Events.d.ts +2 -0
  84. package/lib/Types/Events.js +2 -0
  85. package/lib/Types/GroupMetadata.d.ts +2 -0
  86. package/lib/Types/GroupMetadata.js +2 -0
  87. package/lib/Types/Label.d.ts +3 -0
  88. package/lib/Types/Label.js +25 -0
  89. package/lib/Types/LabelAssociation.d.ts +3 -0
  90. package/lib/Types/LabelAssociation.js +7 -0
  91. package/lib/Types/Message.d.ts +12 -0
  92. package/lib/Types/Message.js +18 -0
  93. package/lib/Types/Mex.d.ts +3 -0
  94. package/lib/Types/Mex.js +39 -0
  95. package/lib/Types/Product.d.ts +2 -0
  96. package/lib/Types/Product.js +2 -0
  97. package/lib/Types/RichType.d.ts +3 -0
  98. package/lib/Types/RichType.js +23 -0
  99. package/lib/Types/Signal.d.ts +2 -0
  100. package/lib/Types/Signal.js +2 -0
  101. package/lib/Types/Socket.d.ts +2 -0
  102. package/lib/Types/Socket.js +2 -0
  103. package/lib/Types/State.d.ts +6 -0
  104. package/lib/Types/State.js +56 -0
  105. package/lib/Types/USync.d.ts +2 -0
  106. package/lib/Types/USync.js +2 -0
  107. package/lib/Types/index.d.ts +14 -0
  108. package/lib/Types/index.js +26 -0
  109. package/lib/Utils/api-wrappers.d.ts +70 -0
  110. package/lib/Utils/api-wrappers.js +248 -0
  111. package/lib/Utils/auth-utils.d.ts +58 -0
  112. package/lib/Utils/auth-utils.js +306 -0
  113. package/lib/Utils/browser-utils.d.ts +10 -0
  114. package/lib/Utils/browser-utils.js +29 -0
  115. package/lib/Utils/business.d.ts +50 -0
  116. package/lib/Utils/business.js +231 -0
  117. package/lib/Utils/chat-utils.d.ts +409 -0
  118. package/lib/Utils/chat-utils.js +886 -0
  119. package/lib/Utils/companion-reg-client-utils.d.ts +5 -0
  120. package/lib/Utils/companion-reg-client-utils.js +40 -0
  121. package/lib/Utils/crypto.d.ts +40 -0
  122. package/lib/Utils/crypto.js +118 -0
  123. package/lib/Utils/decode-wa-message.d.ts +84 -0
  124. package/lib/Utils/decode-wa-message.js +317 -0
  125. package/lib/Utils/event-buffer.d.ts +13 -0
  126. package/lib/Utils/event-buffer.js +622 -0
  127. package/lib/Utils/generics.d.ts +64 -0
  128. package/lib/Utils/generics.js +395 -0
  129. package/lib/Utils/history.d.ts +76 -0
  130. package/lib/Utils/history.js +134 -0
  131. package/lib/Utils/identity-change-handler.d.ts +14 -0
  132. package/lib/Utils/identity-change-handler.js +50 -0
  133. package/lib/Utils/index.d.ts +28 -0
  134. package/lib/Utils/index.js +30 -0
  135. package/lib/Utils/link-preview.d.ts +13 -0
  136. package/lib/Utils/link-preview.js +85 -0
  137. package/lib/Utils/logger.d.ts +3 -0
  138. package/lib/Utils/logger.js +3 -0
  139. package/lib/Utils/lt-hash.d.ts +7 -0
  140. package/lib/Utils/lt-hash.js +8 -0
  141. package/lib/Utils/make-mutex.d.ts +7 -0
  142. package/lib/Utils/make-mutex.js +33 -0
  143. package/lib/Utils/message-retry-manager.d.ts +80 -0
  144. package/lib/Utils/message-retry-manager.js +265 -0
  145. package/lib/Utils/messages-media.d.ts +137 -0
  146. package/lib/Utils/messages-media.js +843 -0
  147. package/lib/Utils/messages.d.ts +46 -0
  148. package/lib/Utils/messages.js +1840 -0
  149. package/lib/Utils/noise-handler.d.ts +21 -0
  150. package/lib/Utils/noise-handler.js +201 -0
  151. package/lib/Utils/offline-node-processor.d.ts +10 -0
  152. package/lib/Utils/offline-node-processor.js +40 -0
  153. package/lib/Utils/pre-key-manager.d.ts +26 -0
  154. package/lib/Utils/pre-key-manager.js +106 -0
  155. package/lib/Utils/process-message.d.ts +84 -0
  156. package/lib/Utils/process-message.js +749 -0
  157. package/lib/Utils/reporting-utils.d.ts +13 -0
  158. package/lib/Utils/reporting-utils.js +258 -0
  159. package/lib/Utils/rich-message-utils.d.ts +49 -0
  160. package/lib/Utils/rich-message-utils.js +363 -0
  161. package/lib/Utils/signal.d.ts +83 -0
  162. package/lib/Utils/signal.js +201 -0
  163. package/lib/Utils/stanza-ack.d.ts +17 -0
  164. package/lib/Utils/stanza-ack.js +38 -0
  165. package/lib/Utils/status-utils.js +178 -0
  166. package/lib/Utils/sticker-reaction.js +104 -0
  167. package/lib/Utils/synacwave.d.ts +111 -0
  168. package/lib/Utils/synacwave.js +474 -0
  169. package/lib/Utils/sync-action-utils.d.ts +3 -0
  170. package/lib/Utils/sync-action-utils.js +49 -0
  171. package/lib/Utils/tc-token-utils.d.ts +30 -0
  172. package/lib/Utils/tc-token-utils.js +164 -0
  173. package/lib/Utils/use-multi-file-auth-state.d.ts +11 -0
  174. package/lib/Utils/use-multi-file-auth-state.js +121 -0
  175. package/lib/Utils/use-single-file-auth-state.d.ts +11 -0
  176. package/lib/Utils/use-single-file-auth-state.js +109 -0
  177. package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
  178. package/lib/Utils/use-sqlite-auth-state.js +109 -0
  179. package/lib/Utils/validate-connection.d.ts +44 -0
  180. package/lib/Utils/validate-connection.js +210 -0
  181. package/lib/Utils/view-once-text.d.ts +32 -0
  182. package/lib/Utils/view-once-text.js +213 -0
  183. package/lib/WABinary/constants.d.ts +62 -0
  184. package/lib/WABinary/constants.js +1467 -0
  185. package/lib/WABinary/decode.d.ts +10 -0
  186. package/lib/WABinary/decode.js +262 -0
  187. package/lib/WABinary/encode.d.ts +3 -0
  188. package/lib/WABinary/encode.js +220 -0
  189. package/lib/WABinary/generic-utils.d.ts +77 -0
  190. package/lib/WABinary/generic-utils.js +226 -0
  191. package/lib/WABinary/index.d.ts +6 -0
  192. package/lib/WABinary/index.js +6 -0
  193. package/lib/WABinary/jid-utils.d.ts +29 -0
  194. package/lib/WABinary/jid-utils.js +96 -0
  195. package/lib/WABinary/types.d.ts +2 -0
  196. package/lib/WABinary/types.js +2 -0
  197. package/lib/WAM/BinaryInfo.d.ts +8 -0
  198. package/lib/WAM/BinaryInfo.js +10 -0
  199. package/lib/WAM/constants.d.ts +34927 -0
  200. package/lib/WAM/constants.js +22853 -0
  201. package/lib/WAM/encode.d.ts +2 -0
  202. package/lib/WAM/encode.js +150 -0
  203. package/lib/WAM/index.d.ts +4 -0
  204. package/lib/WAM/index.js +4 -0
  205. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +37 -0
  206. package/lib/WAUSync/Protocols/USyncContactProtocol.js +52 -0
  207. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  208. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
  209. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  210. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  211. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  212. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
  213. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  214. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
  215. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +39 -0
  216. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
  217. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +15 -0
  218. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
  219. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  220. package/lib/WAUSync/Protocols/index.js +6 -0
  221. package/lib/WAUSync/USyncQuery.d.ts +21 -0
  222. package/lib/WAUSync/USyncQuery.js +98 -0
  223. package/lib/WAUSync/USyncUser.d.ts +17 -0
  224. package/lib/WAUSync/USyncUser.js +31 -0
  225. package/lib/WAUSync/index.d.ts +4 -0
  226. package/lib/WAUSync/index.js +4 -0
  227. package/lib/index.d.ts +12 -0
  228. package/lib/index.js +12 -0
  229. package/package.json +118 -5
@@ -0,0 +1,13 @@
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>;
13
+ //# sourceMappingURL=reporting-utils.d.ts.map
@@ -0,0 +1,258 @@
1
+ import { createHmac } from 'crypto';
2
+ import { proto } from '../../WAProto/index.js';
3
+ import { hkdf } from './crypto.js';
4
+ const reportingFields = [
5
+ { f: 1 },
6
+ {
7
+ f: 3,
8
+ s: [{ f: 2 }, { f: 3 }, { f: 8 }, { f: 11 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 25 }]
9
+ },
10
+ { f: 4, s: [{ f: 1 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
11
+ { f: 5, s: [{ f: 3 }, { f: 4 }, { f: 5 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
12
+ { f: 6, s: [{ f: 1 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 30 }] },
13
+ { f: 7, s: [{ f: 2 }, { f: 7 }, { f: 10 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
14
+ { f: 8, s: [{ f: 2 }, { f: 7 }, { f: 9 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 21 }] },
15
+ { f: 9, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
16
+ { f: 12, s: [{ f: 1 }, { f: 2 }, { f: 14, m: true }, { f: 15 }] },
17
+ { f: 18, s: [{ f: 6 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
18
+ { f: 26, s: [{ f: 4 }, { f: 5 }, { f: 8 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
19
+ { f: 28, s: [{ f: 1 }, { f: 2 }, { f: 4 }, { f: 5 }, { f: 6 }, { f: 7, s: [{ f: 21 }, { f: 22 }] }] },
20
+ { f: 37, s: [{ f: 1, m: true }] },
21
+ {
22
+ f: 49,
23
+ s: [
24
+ { f: 2 },
25
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
26
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
27
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
28
+ ]
29
+ },
30
+ { f: 53, s: [{ f: 1, m: true }] },
31
+ { f: 55, s: [{ f: 1, m: true }] },
32
+ { f: 58, s: [{ f: 1, m: true }] },
33
+ { f: 59, s: [{ f: 1, m: true }] },
34
+ {
35
+ f: 60,
36
+ s: [
37
+ { f: 2 },
38
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
39
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
40
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
41
+ ]
42
+ },
43
+ {
44
+ f: 64,
45
+ s: [
46
+ { f: 2 },
47
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
48
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
49
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
50
+ ]
51
+ },
52
+ { f: 66, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
53
+ { f: 74, s: [{ f: 1, m: true }] },
54
+ { f: 87, s: [{ f: 1, m: true }] },
55
+ { f: 88, s: [{ f: 1 }, { f: 2, s: [{ f: 1 }] }, { f: 3, s: [{ f: 21 }, { f: 22 }] }] },
56
+ { f: 92, s: [{ f: 1, m: true }] },
57
+ { f: 93, s: [{ f: 1, m: true }] },
58
+ { f: 94, s: [{ f: 1, m: true }] }
59
+ ];
60
+ const compileReportingFields = (fields) => {
61
+ const map = new Map();
62
+ for (const f of fields) {
63
+ map.set(f.f, {
64
+ m: f.m,
65
+ children: f.s ? compileReportingFields(f.s) : undefined
66
+ });
67
+ }
68
+ return map;
69
+ };
70
+ const compiledReportingFields = compileReportingFields(reportingFields);
71
+ const EMPTY_MAP = new Map();
72
+ const ENC_SECRET_REPORT_TOKEN = 'Report Token';
73
+ const WIRE = {
74
+ VARINT: 0,
75
+ FIXED64: 1,
76
+ BYTES: 2,
77
+ FIXED32: 5
78
+ };
79
+ export const shouldIncludeReportingToken = (message) => !message.reactionMessage &&
80
+ !message.encReactionMessage &&
81
+ !message.encEventResponseMessage &&
82
+ !message.pollUpdateMessage;
83
+ const generateMsgSecretKey = (modificationType, origMsgId, origMsgSender, modificationSender, origMsgSecret) => {
84
+ const useCaseSecret = Buffer.concat([
85
+ Buffer.from(origMsgId, 'utf8'),
86
+ Buffer.from(origMsgSender, 'utf8'),
87
+ Buffer.from(modificationSender, 'utf8'),
88
+ Buffer.from(modificationType, 'utf8')
89
+ ]);
90
+ return hkdf(origMsgSecret, 32, { info: useCaseSecret.toString('latin1') });
91
+ };
92
+ const extractReportingTokenContent = (data, cfg) => {
93
+ const out = [];
94
+ let i = 0;
95
+ while (i < data.length) {
96
+ const tag = decodeVarint(data, i);
97
+ if (!tag.ok) {
98
+ return null;
99
+ }
100
+ const fieldNum = tag.value >> 3;
101
+ const wireType = tag.value & 0x7;
102
+ const fieldStart = i;
103
+ i += tag.bytes;
104
+ const fieldCfg = cfg.get(fieldNum);
105
+ const pushSlice = (end) => {
106
+ if (end > data.length) {
107
+ return false;
108
+ }
109
+ out.push({ num: fieldNum, bytes: data.subarray(fieldStart, end) });
110
+ i = end;
111
+ return true;
112
+ };
113
+ const skip = (end) => {
114
+ if (end > data.length) {
115
+ return false;
116
+ }
117
+ i = end;
118
+ return true;
119
+ };
120
+ if (wireType === WIRE.VARINT) {
121
+ const v = decodeVarint(data, i);
122
+ if (!v.ok) {
123
+ return null;
124
+ }
125
+ const end = i + v.bytes;
126
+ if (!fieldCfg) {
127
+ if (!skip(end)) {
128
+ return null;
129
+ }
130
+ continue;
131
+ }
132
+ if (!pushSlice(end)) {
133
+ return null;
134
+ }
135
+ continue;
136
+ }
137
+ if (wireType === WIRE.FIXED64) {
138
+ const end = i + 8;
139
+ if (!fieldCfg) {
140
+ if (!skip(end)) {
141
+ return null;
142
+ }
143
+ continue;
144
+ }
145
+ if (!pushSlice(end)) {
146
+ return null;
147
+ }
148
+ continue;
149
+ }
150
+ if (wireType === WIRE.FIXED32) {
151
+ const end = i + 4;
152
+ if (!fieldCfg) {
153
+ if (!skip(end)) {
154
+ return null;
155
+ }
156
+ continue;
157
+ }
158
+ if (!pushSlice(end)) {
159
+ return null;
160
+ }
161
+ continue;
162
+ }
163
+ if (wireType === WIRE.BYTES) {
164
+ const len = decodeVarint(data, i);
165
+ if (!len.ok) {
166
+ return null;
167
+ }
168
+ const valStart = i + len.bytes;
169
+ const valEnd = valStart + len.value;
170
+ if (valEnd > data.length) {
171
+ return null;
172
+ }
173
+ if (!fieldCfg) {
174
+ i = valEnd;
175
+ continue;
176
+ }
177
+ if (fieldCfg.m || fieldCfg.children) {
178
+ const sub = extractReportingTokenContent(data.subarray(valStart, valEnd), fieldCfg.children ?? EMPTY_MAP);
179
+ if (sub === null) {
180
+ return null;
181
+ }
182
+ if (sub.length > 0) {
183
+ const newTag = encodeVarint(tag.value);
184
+ const newLen = encodeVarint(sub.length);
185
+ out.push({
186
+ num: fieldNum,
187
+ bytes: Buffer.concat([newTag, newLen, sub])
188
+ });
189
+ }
190
+ i = valEnd;
191
+ continue;
192
+ }
193
+ out.push({ num: fieldNum, bytes: data.subarray(fieldStart, valEnd) });
194
+ i = valEnd;
195
+ continue;
196
+ }
197
+ return null;
198
+ }
199
+ if (out.length === 0) {
200
+ return Buffer.alloc(0);
201
+ }
202
+ out.sort((a, b) => a.num - b.num);
203
+ return Buffer.concat(out.map(f => f.bytes));
204
+ };
205
+ const decodeVarint = (buffer, offset) => {
206
+ let value = 0;
207
+ let bytes = 0;
208
+ let shift = 0;
209
+ while (offset + bytes < buffer.length) {
210
+ const current = buffer[offset + bytes];
211
+ value |= (current & 0x7f) << shift;
212
+ bytes++;
213
+ if ((current & 0x80) === 0) {
214
+ return { value, bytes, ok: true };
215
+ }
216
+ shift += 7;
217
+ if (shift > 35) {
218
+ return { value: 0, bytes: 0, ok: false };
219
+ }
220
+ }
221
+ return { value: 0, bytes: 0, ok: false };
222
+ };
223
+ const encodeVarint = (value) => {
224
+ const parts = [];
225
+ let remaining = value >>> 0;
226
+ while (remaining > 0x7f) {
227
+ parts.push((remaining & 0x7f) | 0x80);
228
+ remaining >>>= 7;
229
+ }
230
+ parts.push(remaining);
231
+ return Buffer.from(parts);
232
+ };
233
+ export const getMessageReportingToken = async (msgProtobuf, message, key) => {
234
+ const msgSecret = message.messageContextInfo?.messageSecret;
235
+ if (!msgSecret || !key.id) {
236
+ return null;
237
+ }
238
+ const from = key.fromMe ? key.remoteJid : key.participant || key.remoteJid;
239
+ const to = key.fromMe ? key.participant || key.remoteJid : key.remoteJid;
240
+ const reportingSecret = generateMsgSecretKey(ENC_SECRET_REPORT_TOKEN, key.id, from, to, msgSecret);
241
+ const content = extractReportingTokenContent(msgProtobuf, compiledReportingFields);
242
+ if (!content || content.length === 0) {
243
+ return null;
244
+ }
245
+ const reportingToken = createHmac('sha256', reportingSecret).update(content).digest().subarray(0, 16);
246
+ return {
247
+ tag: 'reporting',
248
+ attrs: {},
249
+ content: [
250
+ {
251
+ tag: 'reporting_token',
252
+ attrs: { v: '2' },
253
+ content: reportingToken
254
+ }
255
+ ]
256
+ };
257
+ };
258
+ //# sourceMappingURL=reporting-utils.js.map
@@ -0,0 +1,49 @@
1
+ export function tokenizeCode(code: any, language?: string): {
2
+ highlightType: any;
3
+ codeContent: string | undefined;
4
+ }[];
5
+ export function toUnified(submessages: any, uuid: any): {
6
+ response_id: any;
7
+ sections: any;
8
+ };
9
+ export function prepareRichResponseMessage(content: any): {
10
+ messageContextInfo: {
11
+ botMetadata: {
12
+ verificationMetadata: {
13
+ proofs: {
14
+ certificateChain: Uint8Array<ArrayBuffer>[];
15
+ version: number;
16
+ useCase: number;
17
+ signature: Uint8Array<ArrayBuffer>;
18
+ }[];
19
+ };
20
+ };
21
+ };
22
+ botForwardedMessage: {
23
+ message: {
24
+ richResponseMessage: any;
25
+ };
26
+ };
27
+ };
28
+ export function botMetadataSignature(): Uint8Array<ArrayBuffer>;
29
+ export function botMetadataCertificate(length?: number): Uint8Array<ArrayBuffer>;
30
+ export function wrapToBotForwardedMessage(richResponseMessage: any): {
31
+ messageContextInfo: {
32
+ botMetadata: {
33
+ verificationMetadata: {
34
+ proofs: {
35
+ certificateChain: Uint8Array<ArrayBuffer>[];
36
+ version: number;
37
+ useCase: number;
38
+ signature: Uint8Array<ArrayBuffer>;
39
+ }[];
40
+ };
41
+ };
42
+ };
43
+ botForwardedMessage: {
44
+ message: {
45
+ richResponseMessage: any;
46
+ };
47
+ };
48
+ };
49
+ //# sourceMappingURL=rich-message-utils.d.ts.map
@@ -0,0 +1,363 @@
1
+ import { getRandomValues, randomUUID } from 'crypto';
2
+ import { DONATE_URL, LEXER_REGEX } from '../Defaults/index.js';
3
+ import { LANGUAGE_KEYWORDS } from '../WABinary/constants.js';
4
+ import { CodeHighlightType, RichSubMessageType } from '../Types/RichType.js';
5
+ import { proto } from '../../WAProto/index.js';
6
+ import { unixTimestampSeconds } from './generics.js';
7
+ const NOOP = new Set([]);
8
+ export const tokenizeCode = (code, language = 'javascript') => {
9
+ const keywords = LANGUAGE_KEYWORDS[language] || NOOP;
10
+ const blocks = [];
11
+ LEXER_REGEX.lastIndex = 0;
12
+ let match;
13
+ while ((match = LEXER_REGEX.exec(code)) !== null) {
14
+ if (match[1]) {
15
+ blocks.push({ highlightType: CodeHighlightType.COMMENT, codeContent: match[1] });
16
+ }
17
+ else if (match[2]) {
18
+ blocks.push({ highlightType: CodeHighlightType.STRING, codeContent: match[2] });
19
+ }
20
+ else if (match[3]) {
21
+ blocks.push({
22
+ highlightType: keywords.has(match[3]) ? CodeHighlightType.KEYWORD : CodeHighlightType.METHOD,
23
+ codeContent: match[3],
24
+ });
25
+ }
26
+ else if (match[4]) {
27
+ blocks.push({
28
+ highlightType: keywords.has(match[4]) ? CodeHighlightType.KEYWORD : CodeHighlightType.DEFAULT,
29
+ codeContent: match[4],
30
+ });
31
+ }
32
+ else if (match[5]) {
33
+ blocks.push({ highlightType: CodeHighlightType.NUMBER, codeContent: match[5] });
34
+ }
35
+ else {
36
+ blocks.push({ highlightType: CodeHighlightType.DEFAULT, codeContent: match[6] });
37
+ }
38
+ }
39
+ return blocks;
40
+ };
41
+
42
+ export const toUnified = (submessages, uuid) => ({
43
+ response_id: uuid || randomUUID(),
44
+ sections: submessages.map((submessage, index) => {
45
+ switch (submessage.messageType) {
46
+ case RichSubMessageType.CODE:
47
+ const codeMetadata = submessage.codeMetadata;
48
+ return {
49
+ view_model: {
50
+ primitive: {
51
+ language: codeMetadata.codeLanguage,
52
+ code_blocks: codeMetadata.codeBlocks.map((block) => ({ content: block.codeContent, type: CodeHighlightType[block.highlightType] })),
53
+ __typename: 'GenAICodeUXPrimitive'
54
+ },
55
+ __typename: 'GenAISingleLayoutViewModel'
56
+ }
57
+ };
58
+ case RichSubMessageType.CONTENT_ITEMS:
59
+ return {};
60
+ case RichSubMessageType.INLINE_IMAGE:
61
+ return {};
62
+ case RichSubMessageType.LATEX:
63
+ return {};
64
+ case RichSubMessageType.TABLE:
65
+ const tableMetadata = submessage.tableMetadata;
66
+ return {
67
+ view_model: {
68
+ primitive: {
69
+ title: tableMetadata.title,
70
+ rows: tableMetadata.rows.map((row) => ({
71
+ is_header: row.isHeading,
72
+ cells: row.items,
73
+ markdown_cells: row.items.map((item) => ({ text: item }))
74
+ })),
75
+ __typename: 'GenATableUXPrimitive'
76
+ },
77
+ __typename: 'GenAISingleLayoutViewModel'
78
+ }
79
+ };
80
+ case RichSubMessageType.TEXT:
81
+ return {
82
+ view_model: {
83
+ primitive: {
84
+ text: submessage.messageText,
85
+ inline_entities: submessage.inlineEntities || [],
86
+ __typename: 'GenAIMarkdownTextUXPrimitive'
87
+ },
88
+ __typename: 'GenAISingleLayoutViewModel'
89
+ }
90
+ };
91
+ }
92
+ return submessage;
93
+ })
94
+ });
95
+ export const prepareRichResponseMessage = (content) => {
96
+ const { alignment, code, contentText, disclaimerText, footerText, headerText, imageText, inlineImage, inlineVideo, items, language, latex, links, noHeading, posts, products, suggested, richResponse, table, tapLinkUrl, title } = content;
97
+ let submessages = [];
98
+ if (Array.isArray(richResponse)) {
99
+ submessages = richResponse.map((submessage) => {
100
+ if (submessage.text) {
101
+ return {
102
+ messageType: RichSubMessageType.TEXT,
103
+ messageText: submessage.text,
104
+ inlineEntities: submessage.inlineEntities
105
+ };
106
+ }
107
+ else if (submessage.code) {
108
+ return {
109
+ messageType: RichSubMessageType.CODE,
110
+ codeMetadata: {
111
+ codeLanguage: submessage.language,
112
+ codeBlocks: submessage.code
113
+ }
114
+ };
115
+ }
116
+ else if (submessage.items) {
117
+ return {
118
+ messageType: RichSubMessageType.CONTENT_ITEMS,
119
+ contentItemsMetadata: {
120
+ itemsMetadata: submessage.items,
121
+ contentType: proto.AIRichResponseContentItemsMetadata.ContentType.CAROUSEL
122
+ }
123
+ };
124
+ }
125
+ else if (submessage.inlineImage) {
126
+ return {
127
+ messageType: RichSubMessageType.INLINE_IMAGE,
128
+ imageMetadata: {
129
+ imageUrl: submessage.inlineImage,
130
+ imageText: submessage.imageText,
131
+ alignment: submessage.alignment,
132
+ tapLinkUrl: submessage.tapLinkUrl
133
+ }
134
+ };
135
+ }
136
+ else if (submessage.inlineVideo) {
137
+ return {
138
+ messageType: RichSubMessageType.TEXT,
139
+ messageText: 'INLINE_VIDEO'
140
+ };
141
+ }
142
+ else if (submessage.latex) {
143
+ return {
144
+ messageType: RichSubMessageType.LATEX,
145
+ latexMetadata: {
146
+ text: submessage.text,
147
+ expressions: submessage.latex
148
+ }
149
+ };
150
+ }
151
+ else if (submessage.posts) {
152
+ return {
153
+ messageType: RichSubMessageType.TEXT,
154
+ messageText: 'POSTS'
155
+ };
156
+ }
157
+ else if (submessage.products) {
158
+ return {
159
+ messageType: RichSubMessageType.TEXT,
160
+ messageText: 'PRODUCTS'
161
+ };
162
+ }
163
+ else if (submessage.suggested) {
164
+ return {
165
+ messageType: RichSubMessageType.TEXT,
166
+ messageText: 'SUGGESTED_PROMPT'
167
+ };
168
+ }
169
+ else if (submessage.table) {
170
+ return {
171
+ messageType: RichSubMessageType.TABLE,
172
+ tableMetadata: {
173
+ title: submessage.title,
174
+ rows: submessage.table
175
+ }
176
+ };
177
+ }
178
+ return submessage;
179
+ });
180
+ }
181
+ else {
182
+ if (headerText) {
183
+ submessages.push({
184
+ messageType: RichSubMessageType.TEXT,
185
+ messageText: headerText
186
+ });
187
+ }
188
+ if (contentText) {
189
+ submessages.push({
190
+ messageType: RichSubMessageType.TEXT,
191
+ messageText: contentText
192
+ });
193
+ }
194
+ if (code) {
195
+ language ||= 'javascript';
196
+ submessages.push({
197
+ messageType: RichSubMessageType.CODE,
198
+ codeMetadata: {
199
+ codeLanguage: language,
200
+ codeBlocks: tokenizeCode(code, language)
201
+ }
202
+ });
203
+ }
204
+ if (items) {
205
+ submessages.push({
206
+ messageType: RichSubMessageType.CONTENT_ITEMS,
207
+ contentItemsMetadata: {
208
+ itemsMetadata: items,
209
+ contentType: proto.AIRichResponseContentItemsMetadata.ContentType.CAROUSEL
210
+ }
211
+ });
212
+ }
213
+ if (inlineImage) {
214
+ submessages.push({
215
+ messageType: RichSubMessageType.INLINE_IMAGE,
216
+ imageMetadata: {
217
+ imageUrl: inlineImage,
218
+ imageText,
219
+ alignment,
220
+ tapLinkUrl
221
+ }
222
+ });
223
+ }
224
+ if (inlineVideo) {
225
+ submessages.push({
226
+ messageType: RichSubMessageType.TEXT,
227
+ messageText: 'INLINE_VIDEO'
228
+ });
229
+ }
230
+ if (latex) {
231
+ submessages.push({
232
+ messageType: RichSubMessageType.LATEX,
233
+ latexMetadata: {
234
+ text,
235
+ expressions: latex
236
+ }
237
+ });
238
+ }
239
+ if (links) {
240
+ links.forEach((linkField, index) => {
241
+ const prefix = 'SS_' + index;
242
+ const url = linkField.url || DONATE_URL;
243
+ const sources = linkField.sources?.map((sourceField) => ({
244
+ source_type: 'THIRD_PARTY',
245
+ source_display_name: sourceField.displayName || 'Donate',
246
+ source_subtitle: sourceField.subtitle || 'Saweria',
247
+ source_url: sourceField.url || url
248
+ }));
249
+ submessages.push({
250
+ messageType: RichSubMessageType.TEXT,
251
+ messageText: linkField.text + ` {{${prefix}}}¹{{/${prefix}}} `,
252
+ inlineEntities: [{
253
+ key: prefix,
254
+ metadata: {
255
+ reference_id: index + 1,
256
+ reference_url: url,
257
+ reference_title: linkField.title || 'For Donation via Saweria',
258
+ reference_display_name: linkField.displayName || 'Donation',
259
+ sources: sources || [],
260
+ __typename: 'GenAISearchCitationItem'
261
+ }
262
+ }]
263
+ });
264
+ });
265
+ }
266
+ if (posts) {
267
+ submessages.push({
268
+ messageType: RichSubMessageType.TEXT,
269
+ messageText: 'POSTS'
270
+ });
271
+ }
272
+ if (products) {
273
+ submessages.push({
274
+ messageType: RichSubMessageType.TEXT,
275
+ messageText: 'PRODUCTS'
276
+ });
277
+ }
278
+ if (suggested) {
279
+ submessages.push({
280
+ messageType: RichSubMessageType.TEXT,
281
+ messageText: 'SUGGESTED_PROMPT'
282
+ });
283
+ }
284
+ if (table) {
285
+ submessages.push({
286
+ messageType: RichSubMessageType.TABLE,
287
+ tableMetadata: {
288
+ title,
289
+ rows: table.map((items, index) => ({
290
+ isHeading: !noHeading && index == 0,
291
+ items
292
+ }))
293
+ }
294
+ });
295
+ }
296
+ if (footerText) {
297
+ submessages.push({
298
+ messageType: RichSubMessageType.TEXT,
299
+ messageText: footerText
300
+ });
301
+ }
302
+ }
303
+ const uuid = randomUUID();
304
+ const unified = toUnified(submessages, uuid);
305
+ const richResponseMessage = proto.AIRichResponseMessage.create({
306
+ submessages,
307
+ messageType: proto.AIRichResponseMessageType.AI_RICH_RESPONSE_TYPE_STANDARD,
308
+ unifiedResponse: {
309
+ data: Buffer.from(JSON.stringify(unified))
310
+ },
311
+ contextInfo: {
312
+ isForwarded: true,
313
+ forwardingScore: 1,
314
+ forwardedAiBotMessageInfo: { botJid: '867051314767696@bot' },
315
+ forwardOrigin: 4
316
+ }
317
+ });
318
+ const message = wrapToBotForwardedMessage(richResponseMessage);
319
+ const botMetadata = message.messageContextInfo.botMetadata;
320
+
321
+ if (disclaimerText) {
322
+ botMetadata.messageDisclaimerText = disclaimerText;
323
+ }
324
+
325
+ botMetadata.botResponseId = uuid;
326
+ return message;
327
+ };
328
+
329
+ export const botMetadataSignature = () => {
330
+ const signature = new Uint8Array(64);
331
+ getRandomValues(signature);
332
+ return signature;
333
+ };
334
+ export const botMetadataCertificate = (length = 685) => {
335
+ const certificate = new Uint8Array(length);
336
+ certificate[0] = 48;
337
+ certificate[1] = 130;
338
+ getRandomValues(certificate.subarray(2));
339
+ return certificate;
340
+ };
341
+ export const wrapToBotForwardedMessage = (richResponseMessage) => ({
342
+ messageContextInfo: {
343
+ botMetadata: {
344
+
345
+ verificationMetadata: {
346
+ proofs: [
347
+ {
348
+ certificateChain: [
349
+ botMetadataCertificate(),
350
+ botMetadataCertificate(892)
351
+ ],
352
+ version: 1,
353
+ useCase: 1,
354
+ signature: botMetadataSignature()
355
+ }
356
+ ]
357
+ }
358
+ }
359
+ },
360
+ botForwardedMessage: {
361
+ message: { richResponseMessage }
362
+ }
363
+ });