@wenzyx1/bails 1.0.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 (218) hide show
  1. package/README.md +231 -0
  2. package/WAProto/fix-imports.js +85 -0
  3. package/WAProto/index.d.ts +79257 -0
  4. package/WAProto/index.js +242946 -0
  5. package/engine-requirements.js +10 -0
  6. package/lib/Defaults/baileys-version.json +3 -0
  7. package/lib/Defaults/index.d.ts +53 -0
  8. package/lib/Defaults/index.js +147 -0
  9. package/lib/Defaults/phonenumber-mcc.json +223 -0
  10. package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
  11. package/lib/Signal/Group/ciphertext-message.js +15 -0
  12. package/lib/Signal/Group/group-session-builder.d.ts +14 -0
  13. package/lib/Signal/Group/group-session-builder.js +64 -0
  14. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  15. package/lib/Signal/Group/group_cipher.js +93 -0
  16. package/lib/Signal/Group/index.d.ts +11 -0
  17. package/lib/Signal/Group/index.js +57 -0
  18. package/lib/Signal/Group/keyhelper.d.ts +10 -0
  19. package/lib/Signal/Group/keyhelper.js +55 -0
  20. package/lib/Signal/Group/queue-job.d.ts +1 -0
  21. package/lib/Signal/Group/queue-job.js +53 -0
  22. package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
  23. package/lib/Signal/Group/sender-chain-key.js +34 -0
  24. package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
  25. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  26. package/lib/Signal/Group/sender-key-message.d.ts +18 -0
  27. package/lib/Signal/Group/sender-key-message.js +69 -0
  28. package/lib/Signal/Group/sender-key-name.d.ts +17 -0
  29. package/lib/Signal/Group/sender-key-name.js +51 -0
  30. package/lib/Signal/Group/sender-key-record.d.ts +30 -0
  31. package/lib/Signal/Group/sender-key-record.js +53 -0
  32. package/lib/Signal/Group/sender-key-state.d.ts +38 -0
  33. package/lib/Signal/Group/sender-key-state.js +99 -0
  34. package/lib/Signal/Group/sender-message-key.d.ts +11 -0
  35. package/lib/Signal/Group/sender-message-key.js +29 -0
  36. package/lib/Signal/libsignal.d.ts +3 -0
  37. package/lib/Signal/libsignal.js +454 -0
  38. package/lib/Signal/lid-mapping.js +280 -0
  39. package/lib/Socket/Client/abstract-socket-client.d.ts +17 -0
  40. package/lib/Socket/Client/abstract-socket-client.js +13 -0
  41. package/lib/Socket/Client/index.d.ts +3 -0
  42. package/lib/Socket/Client/index.js +19 -0
  43. package/lib/Socket/Client/mobile-socket-client.d.ts +13 -0
  44. package/lib/Socket/Client/mobile-socket-client.js +65 -0
  45. package/lib/Socket/Client/web-socket-client.d.ts +12 -0
  46. package/lib/Socket/Client/web-socket-client.js +62 -0
  47. package/lib/Socket/business.d.ts +171 -0
  48. package/lib/Socket/business.js +342 -0
  49. package/lib/Socket/chats.d.ts +267 -0
  50. package/lib/Socket/chats.js +978 -0
  51. package/lib/Socket/communities.mjs +428 -0
  52. package/lib/Socket/dugong.d.ts +254 -0
  53. package/lib/Socket/dugong.js +558 -0
  54. package/lib/Socket/groups.d.ts +115 -0
  55. package/lib/Socket/groups.js +380 -0
  56. package/lib/Socket/index.d.ts +173 -0
  57. package/lib/Socket/index.js +32 -0
  58. package/lib/Socket/luxu.js +545 -0
  59. package/lib/Socket/messages-recv.d.ts +161 -0
  60. package/lib/Socket/messages-recv.js +1124 -0
  61. package/lib/Socket/messages-send.d.ts +149 -0
  62. package/lib/Socket/messages-send.js +1476 -0
  63. package/lib/Socket/mex.mjs +50 -0
  64. package/lib/Socket/newsletter.d.ts +134 -0
  65. package/lib/Socket/newsletter.js +351 -0
  66. package/lib/Socket/registration.d.ts +267 -0
  67. package/lib/Socket/registration.js +166 -0
  68. package/lib/Socket/socket.d.ts +43 -0
  69. package/lib/Socket/socket.js +904 -0
  70. package/lib/Socket/username.mjs +132 -0
  71. package/lib/Socket/usync.d.ts +36 -0
  72. package/lib/Socket/usync.js +70 -0
  73. package/lib/Store/index.d.ts +3 -0
  74. package/lib/Store/index.js +10 -0
  75. package/lib/Store/keyed-db.d.ts +30 -0
  76. package/lib/Store/keyed-db.js +233 -0
  77. package/lib/Store/make-cache-manager-store.d.ts +13 -0
  78. package/lib/Store/make-cache-manager-store.js +83 -0
  79. package/lib/Store/make-in-memory-store.d.ts +118 -0
  80. package/lib/Store/make-in-memory-store.js +427 -0
  81. package/lib/Store/make-ordered-dictionary.d.ts +13 -0
  82. package/lib/Store/make-ordered-dictionary.js +81 -0
  83. package/lib/Store/object-repository.d.ts +10 -0
  84. package/lib/Store/object-repository.js +27 -0
  85. package/lib/Types/Auth.d.ts +110 -0
  86. package/lib/Types/Auth.js +2 -0
  87. package/lib/Types/Call.d.ts +13 -0
  88. package/lib/Types/Call.js +2 -0
  89. package/lib/Types/Chat.d.ts +102 -0
  90. package/lib/Types/Chat.js +4 -0
  91. package/lib/Types/Contact.d.ts +19 -0
  92. package/lib/Types/Contact.js +2 -0
  93. package/lib/Types/Events.d.ts +157 -0
  94. package/lib/Types/Events.js +2 -0
  95. package/lib/Types/GroupMetadata.d.ts +55 -0
  96. package/lib/Types/GroupMetadata.js +2 -0
  97. package/lib/Types/Label.d.ts +35 -0
  98. package/lib/Types/Label.js +27 -0
  99. package/lib/Types/LabelAssociation.d.ts +29 -0
  100. package/lib/Types/LabelAssociation.js +9 -0
  101. package/lib/Types/Message.d.ts +273 -0
  102. package/lib/Types/Message.js +9 -0
  103. package/lib/Types/Mex.js +39 -0
  104. package/lib/Types/Newsletter.d.ts +103 -0
  105. package/lib/Types/Newsletter.js +38 -0
  106. package/lib/Types/Product.d.ts +78 -0
  107. package/lib/Types/Product.js +2 -0
  108. package/lib/Types/Signal.d.ts +57 -0
  109. package/lib/Types/Signal.js +2 -0
  110. package/lib/Types/Socket.d.ts +111 -0
  111. package/lib/Types/Socket.js +2 -0
  112. package/lib/Types/State.d.ts +27 -0
  113. package/lib/Types/State.js +2 -0
  114. package/lib/Types/USync.d.ts +25 -0
  115. package/lib/Types/USync.js +2 -0
  116. package/lib/Types/index.d.ts +57 -0
  117. package/lib/Types/index.js +43 -0
  118. package/lib/Utils/Mex.js +39 -0
  119. package/lib/Utils/auth-utils.d.ts +18 -0
  120. package/lib/Utils/auth-utils.js +206 -0
  121. package/lib/Utils/baileys-event-stream.d.ts +16 -0
  122. package/lib/Utils/baileys-event-stream.js +57 -0
  123. package/lib/Utils/browser-presets.js +64 -0
  124. package/lib/Utils/business.d.ts +22 -0
  125. package/lib/Utils/business.js +234 -0
  126. package/lib/Utils/chat-utils.d.ts +71 -0
  127. package/lib/Utils/chat-utils.js +726 -0
  128. package/lib/Utils/companion-reg-client-utils.js +40 -0
  129. package/lib/Utils/crypto.d.ts +41 -0
  130. package/lib/Utils/crypto.js +151 -0
  131. package/lib/Utils/decode-wa-message.d.ts +19 -0
  132. package/lib/Utils/decode-wa-message.js +198 -0
  133. package/lib/Utils/event-buffer.d.ts +35 -0
  134. package/lib/Utils/event-buffer.js +514 -0
  135. package/lib/Utils/extended-messages.js +231 -0
  136. package/lib/Utils/generics.d.ts +92 -0
  137. package/lib/Utils/generics.js +425 -0
  138. package/lib/Utils/history.d.ts +15 -0
  139. package/lib/Utils/history.js +96 -0
  140. package/lib/Utils/identity-change-handler.js +52 -0
  141. package/lib/Utils/index.d.ts +17 -0
  142. package/lib/Utils/index.js +40 -0
  143. package/lib/Utils/link-preview.d.ts +21 -0
  144. package/lib/Utils/link-preview.js +93 -0
  145. package/lib/Utils/logger.d.ts +4 -0
  146. package/lib/Utils/logger.js +7 -0
  147. package/lib/Utils/lt-hash.d.ts +12 -0
  148. package/lib/Utils/lt-hash.js +51 -0
  149. package/lib/Utils/make-mutex.d.ts +7 -0
  150. package/lib/Utils/make-mutex.js +43 -0
  151. package/lib/Utils/message-composer.js +293 -0
  152. package/lib/Utils/message-retry-manager.js +226 -0
  153. package/lib/Utils/messages-media.d.ts +116 -0
  154. package/lib/Utils/messages-media.js +815 -0
  155. package/lib/Utils/messages.d.ts +77 -0
  156. package/lib/Utils/messages.js +888 -0
  157. package/lib/Utils/noise-handler.d.ts +21 -0
  158. package/lib/Utils/noise-handler.js +155 -0
  159. package/lib/Utils/offline-node-processor.js +46 -0
  160. package/lib/Utils/pre-key-manager.js +95 -0
  161. package/lib/Utils/process-message.d.ts +41 -0
  162. package/lib/Utils/process-message.js +321 -0
  163. package/lib/Utils/reporting-utils.js +272 -0
  164. package/lib/Utils/signal.d.ts +32 -0
  165. package/lib/Utils/signal.js +160 -0
  166. package/lib/Utils/stanza-ack.js +38 -0
  167. package/lib/Utils/sync-action-utils.js +50 -0
  168. package/lib/Utils/tc-token-utils.js +169 -0
  169. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  170. package/lib/Utils/use-multi-file-auth-state.js +119 -0
  171. package/lib/Utils/validate-connection.d.ts +11 -0
  172. package/lib/Utils/validate-connection.js +229 -0
  173. package/lib/WABinary/constants.d.ts +30 -0
  174. package/lib/WABinary/constants.js +40 -0
  175. package/lib/WABinary/decode.d.ts +7 -0
  176. package/lib/WABinary/decode.js +252 -0
  177. package/lib/WABinary/encode.d.ts +3 -0
  178. package/lib/WABinary/encode.js +265 -0
  179. package/lib/WABinary/generic-utils.d.ts +17 -0
  180. package/lib/WABinary/generic-utils.js +204 -0
  181. package/lib/WABinary/index.d.ts +5 -0
  182. package/lib/WABinary/index.js +21 -0
  183. package/lib/WABinary/jid-utils.d.ts +31 -0
  184. package/lib/WABinary/jid-utils.js +132 -0
  185. package/lib/WABinary/types.d.ts +18 -0
  186. package/lib/WABinary/types.js +2 -0
  187. package/lib/WAM/BinaryInfo.d.ts +17 -0
  188. package/lib/WAM/BinaryInfo.js +13 -0
  189. package/lib/WAM/constants.d.ts +38 -0
  190. package/lib/WAM/constants.js +15350 -0
  191. package/lib/WAM/encode.d.ts +3 -0
  192. package/lib/WAM/encode.js +155 -0
  193. package/lib/WAM/index.d.ts +3 -0
  194. package/lib/WAM/index.js +19 -0
  195. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
  196. package/lib/WAUSync/Protocols/USyncContactProtocol.js +55 -0
  197. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
  198. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  199. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  200. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  201. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  202. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  203. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +27 -0
  204. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +25 -0
  205. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  206. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +8 -0
  207. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
  208. package/lib/WAUSync/Protocols/index.d.ts +4 -0
  209. package/lib/WAUSync/Protocols/index.js +21 -0
  210. package/lib/WAUSync/USyncQuery.d.ts +28 -0
  211. package/lib/WAUSync/USyncQuery.js +93 -0
  212. package/lib/WAUSync/USyncUser.d.ts +12 -0
  213. package/lib/WAUSync/USyncUser.js +34 -0
  214. package/lib/WAUSync/index.d.ts +3 -0
  215. package/lib/WAUSync/index.js +19 -0
  216. package/lib/index.d.ts +12 -0
  217. package/lib/index.js +52 -0
  218. package/package.json +112 -0
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shouldIncludeReportingToken = void 0;
4
+ exports.getMessageReportingToken = getMessageReportingToken;
5
+ const crypto_1 = require("crypto");
6
+ const crypto_2 = require("./crypto");
7
+ const reportingFields = [
8
+ { f: 1 },
9
+ {
10
+ f: 3,
11
+ s: [{ f: 2 }, { f: 3 }, { f: 8 }, { f: 11 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 25 }]
12
+ },
13
+ { f: 4, s: [{ f: 1 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
14
+ { f: 5, s: [{ f: 3 }, { f: 4 }, { f: 5 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
15
+ { f: 6, s: [{ f: 1 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 30 }] },
16
+ { f: 7, s: [{ f: 2 }, { f: 7 }, { f: 10 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
17
+ { f: 8, s: [{ f: 2 }, { f: 7 }, { f: 9 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 21 }] },
18
+ { f: 9, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
19
+ { f: 12, s: [{ f: 1 }, { f: 2 }, { f: 14, m: true }, { f: 15 }] },
20
+ { f: 18, s: [{ f: 6 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
21
+ { f: 26, s: [{ f: 4 }, { f: 5 }, { f: 8 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
22
+ { f: 28, s: [{ f: 1 }, { f: 2 }, { f: 4 }, { f: 5 }, { f: 6 }, { f: 7, s: [{ f: 21 }, { f: 22 }] }] },
23
+ { f: 37, s: [{ f: 1, m: true }] },
24
+ {
25
+ f: 49,
26
+ s: [
27
+ { f: 2 },
28
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
29
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
30
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
31
+ ]
32
+ },
33
+ { f: 53, s: [{ f: 1, m: true }] },
34
+ { f: 55, s: [{ f: 1, m: true }] },
35
+ { f: 58, s: [{ f: 1, m: true }] },
36
+ { f: 59, s: [{ f: 1, m: true }] },
37
+ {
38
+ f: 60,
39
+ s: [
40
+ { f: 2 },
41
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
42
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
43
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
44
+ ]
45
+ },
46
+ {
47
+ f: 64,
48
+ s: [
49
+ { f: 2 },
50
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
51
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
52
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
53
+ ]
54
+ },
55
+ { f: 66, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
56
+ { f: 74, s: [{ f: 1, m: true }] },
57
+ { f: 87, s: [{ f: 1, m: true }] },
58
+ { f: 88, s: [{ f: 1 }, { f: 2, s: [{ f: 1 }] }, { f: 3, s: [{ f: 21 }, { f: 22 }] }] },
59
+ { f: 92, s: [{ f: 1, m: true }] },
60
+ { f: 93, s: [{ f: 1, m: true }] },
61
+ { f: 94, s: [{ f: 1, m: true }] }
62
+ ];
63
+ const compileReportingFields = (fields) => {
64
+ const map = new Map();
65
+ for (const f of fields) {
66
+ map.set(f.f, {
67
+ m: f.m,
68
+ children: f.s ? compileReportingFields(f.s) : undefined
69
+ });
70
+ }
71
+ return map;
72
+ };
73
+ const compiledReportingFields = compileReportingFields(reportingFields);
74
+ const EMPTY_MAP = new Map();
75
+ const ENC_SECRET_REPORT_TOKEN = 'Report Token';
76
+ const WIRE = {
77
+ VARINT: 0,
78
+ FIXED64: 1,
79
+ BYTES: 2,
80
+ FIXED32: 5
81
+ };
82
+ /**
83
+ * Whether a "reporting_token" node should be attached to an outgoing
84
+ * message stanza (WA anti-abuse feature).
85
+ *
86
+ * NOTE: ported as an available utility, together with getMessageReportingToken
87
+ * below. NOT wired into edgar-bail's messages-send.js send path automatically
88
+ * -- that would require matching xata-bail's exact surrounding variables
89
+ * (reportingMessage/isNewsletter/isRetryResend) in the hot message-send
90
+ * path, which isn't safe to do blind without test coverage here.
91
+ */
92
+ const shouldIncludeReportingToken = (message) => !message.reactionMessage &&
93
+ !message.encReactionMessage &&
94
+ !message.encEventResponseMessage &&
95
+ !message.pollUpdateMessage;
96
+ exports.shouldIncludeReportingToken = shouldIncludeReportingToken;
97
+ const generateMsgSecretKey = (modificationType, origMsgId, origMsgSender, modificationSender, origMsgSecret) => {
98
+ const useCaseSecret = Buffer.concat([
99
+ Buffer.from(origMsgId, 'utf8'),
100
+ Buffer.from(origMsgSender, 'utf8'),
101
+ Buffer.from(modificationSender, 'utf8'),
102
+ Buffer.from(modificationType, 'utf8')
103
+ ]);
104
+ return (0, crypto_2.hkdf)(origMsgSecret, 32, { info: useCaseSecret.toString('latin1') });
105
+ };
106
+ const decodeVarint = (buffer, offset) => {
107
+ let value = 0;
108
+ let bytes = 0;
109
+ let shift = 0;
110
+ while (offset + bytes < buffer.length) {
111
+ const current = buffer[offset + bytes];
112
+ value |= (current & 0x7f) << shift;
113
+ bytes++;
114
+ if ((current & 0x80) === 0) {
115
+ return { value, bytes, ok: true };
116
+ }
117
+ shift += 7;
118
+ if (shift > 35) {
119
+ return { value: 0, bytes: 0, ok: false };
120
+ }
121
+ }
122
+ return { value: 0, bytes: 0, ok: false };
123
+ };
124
+ const encodeVarint = (value) => {
125
+ const parts = [];
126
+ let remaining = value >>> 0;
127
+ while (remaining > 0x7f) {
128
+ parts.push((remaining & 0x7f) | 0x80);
129
+ remaining >>>= 7;
130
+ }
131
+ parts.push(remaining);
132
+ return Buffer.from(parts);
133
+ };
134
+ const extractReportingTokenContent = (data, cfg) => {
135
+ const out = [];
136
+ let i = 0;
137
+ while (i < data.length) {
138
+ const tag = decodeVarint(data, i);
139
+ if (!tag.ok) {
140
+ return null;
141
+ }
142
+ const fieldNum = tag.value >> 3;
143
+ const wireType = tag.value & 0x7;
144
+ const fieldStart = i;
145
+ i += tag.bytes;
146
+ const fieldCfg = cfg.get(fieldNum);
147
+ const pushSlice = (end) => {
148
+ if (end > data.length) {
149
+ return false;
150
+ }
151
+ out.push({ num: fieldNum, bytes: data.subarray(fieldStart, end) });
152
+ i = end;
153
+ return true;
154
+ };
155
+ const skip = (end) => {
156
+ if (end > data.length) {
157
+ return false;
158
+ }
159
+ i = end;
160
+ return true;
161
+ };
162
+ if (wireType === WIRE.VARINT) {
163
+ const v = decodeVarint(data, i);
164
+ if (!v.ok) {
165
+ return null;
166
+ }
167
+ const end = i + v.bytes;
168
+ if (!fieldCfg) {
169
+ if (!skip(end)) {
170
+ return null;
171
+ }
172
+ continue;
173
+ }
174
+ if (!pushSlice(end)) {
175
+ return null;
176
+ }
177
+ continue;
178
+ }
179
+ if (wireType === WIRE.FIXED64) {
180
+ const end = i + 8;
181
+ if (!fieldCfg) {
182
+ if (!skip(end)) {
183
+ return null;
184
+ }
185
+ continue;
186
+ }
187
+ if (!pushSlice(end)) {
188
+ return null;
189
+ }
190
+ continue;
191
+ }
192
+ if (wireType === WIRE.FIXED32) {
193
+ const end = i + 4;
194
+ if (!fieldCfg) {
195
+ if (!skip(end)) {
196
+ return null;
197
+ }
198
+ continue;
199
+ }
200
+ if (!pushSlice(end)) {
201
+ return null;
202
+ }
203
+ continue;
204
+ }
205
+ if (wireType === WIRE.BYTES) {
206
+ const len = decodeVarint(data, i);
207
+ if (!len.ok) {
208
+ return null;
209
+ }
210
+ const valStart = i + len.bytes;
211
+ const valEnd = valStart + len.value;
212
+ if (valEnd > data.length) {
213
+ return null;
214
+ }
215
+ if (!fieldCfg) {
216
+ i = valEnd;
217
+ continue;
218
+ }
219
+ if (fieldCfg.m || fieldCfg.children) {
220
+ const sub = extractReportingTokenContent(data.subarray(valStart, valEnd), fieldCfg.children || EMPTY_MAP);
221
+ if (sub === null) {
222
+ return null;
223
+ }
224
+ if (sub.length > 0) {
225
+ const newTag = encodeVarint(tag.value);
226
+ const newLen = encodeVarint(sub.length);
227
+ out.push({
228
+ num: fieldNum,
229
+ bytes: Buffer.concat([newTag, newLen, sub])
230
+ });
231
+ }
232
+ i = valEnd;
233
+ continue;
234
+ }
235
+ out.push({ num: fieldNum, bytes: data.subarray(fieldStart, valEnd) });
236
+ i = valEnd;
237
+ continue;
238
+ }
239
+ return null;
240
+ }
241
+ if (out.length === 0) {
242
+ return Buffer.alloc(0);
243
+ }
244
+ out.sort((a, b) => a.num - b.num);
245
+ return Buffer.concat(out.map(f => f.bytes));
246
+ };
247
+ async function getMessageReportingToken(msgProtobuf, message, key) {
248
+ var _a;
249
+ const msgSecret = (_a = message.messageContextInfo) === null || _a === void 0 ? void 0 : _a.messageSecret;
250
+ if (!msgSecret || !key.id) {
251
+ return null;
252
+ }
253
+ const from = key.fromMe ? key.remoteJid : key.participant || key.remoteJid;
254
+ const to = key.fromMe ? key.participant || key.remoteJid : key.remoteJid;
255
+ const reportingSecret = generateMsgSecretKey(ENC_SECRET_REPORT_TOKEN, key.id, from, to, msgSecret);
256
+ const content = extractReportingTokenContent(msgProtobuf, compiledReportingFields);
257
+ if (!content || content.length === 0) {
258
+ return null;
259
+ }
260
+ const reportingToken = (0, crypto_1.createHmac)('sha256', reportingSecret).update(content).digest().subarray(0, 16);
261
+ return {
262
+ tag: 'reporting',
263
+ attrs: {},
264
+ content: [
265
+ {
266
+ tag: 'reporting_token',
267
+ attrs: { v: '2' },
268
+ content: reportingToken
269
+ }
270
+ ]
271
+ };
272
+ }
@@ -0,0 +1,32 @@
1
+ import { SignalRepository } from '../Types';
2
+ import { AuthenticationCreds, AuthenticationState, KeyPair, SignalIdentity, SignalKeyStore, SignedKeyPair } from '../Types/Auth';
3
+ import { BinaryNode, JidWithDevice } from '../WABinary';
4
+ export declare const createSignalIdentity: (wid: string, accountSignatureKey: Uint8Array) => SignalIdentity;
5
+ export declare const getPreKeys: ({ get }: SignalKeyStore, min: number, limit: number) => Promise<{
6
+ [id: string]: KeyPair;
7
+ }>;
8
+ export declare const generateOrGetPreKeys: (creds: AuthenticationCreds, range: number) => {
9
+ newPreKeys: {
10
+ [id: number]: KeyPair;
11
+ };
12
+ lastPreKeyId: number;
13
+ preKeysRange: readonly [number, number];
14
+ };
15
+ export declare const xmppSignedPreKey: (key: SignedKeyPair) => BinaryNode;
16
+ export declare const xmppPreKey: (pair: KeyPair, id: number) => BinaryNode;
17
+ export declare const parseAndInjectE2ESessions: (node: BinaryNode, repository: SignalRepository) => Promise<void>;
18
+ export declare const extractDeviceJids: (result: BinaryNode, myJid: string, excludeZeroDevices: boolean) => JidWithDevice[];
19
+ /**
20
+ * get the next N keys for upload or processing
21
+ * @param count number of pre-keys to get or generate
22
+ */
23
+ export declare const getNextPreKeys: ({ creds, keys }: AuthenticationState, count: number) => Promise<{
24
+ update: Partial<AuthenticationCreds>;
25
+ preKeys: {
26
+ [id: string]: KeyPair;
27
+ };
28
+ }>;
29
+ export declare const getNextPreKeysNode: (state: AuthenticationState, count: number) => Promise<{
30
+ update: Partial<AuthenticationCreds>;
31
+ node: BinaryNode;
32
+ }>;
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getNextPreKeysNode = exports.getNextPreKeys = exports.extractDeviceJids = exports.parseAndInjectE2ESessions = exports.xmppPreKey = exports.xmppSignedPreKey = exports.generateOrGetPreKeys = exports.getPreKeys = exports.createSignalIdentity = void 0;
4
+ const Defaults_1 = require("../Defaults");
5
+ const WABinary_1 = require("../WABinary");
6
+ const crypto_1 = require("./crypto");
7
+ const generics_1 = require("./generics");
8
+ // lightweight replacement for lodash.chunk — avoids pulling in the full lodash dependency
9
+ const chunkArray = (arr, size) => {
10
+ const out = [];
11
+ for (let i = 0; i < arr.length; i += size) {
12
+ out.push(arr.slice(i, i + size));
13
+ }
14
+ return out;
15
+ };
16
+ const createSignalIdentity = (wid, accountSignatureKey) => {
17
+ return {
18
+ identifier: { name: wid, deviceId: 0 },
19
+ identifierKey: (0, crypto_1.generateSignalPubKey)(accountSignatureKey)
20
+ };
21
+ };
22
+ exports.createSignalIdentity = createSignalIdentity;
23
+ const getPreKeys = async ({ get }, min, limit) => {
24
+ const idList = [];
25
+ for (let id = min; id < limit; id++) {
26
+ idList.push(id.toString());
27
+ }
28
+ return get('pre-key', idList);
29
+ };
30
+ exports.getPreKeys = getPreKeys;
31
+ const generateOrGetPreKeys = (creds, range) => {
32
+ const avaliable = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId;
33
+ const remaining = range - avaliable;
34
+ const lastPreKeyId = creds.nextPreKeyId + remaining - 1;
35
+ const newPreKeys = {};
36
+ if (remaining > 0) {
37
+ for (let i = creds.nextPreKeyId; i <= lastPreKeyId; i++) {
38
+ newPreKeys[i] = crypto_1.Curve.generateKeyPair();
39
+ }
40
+ }
41
+ return {
42
+ newPreKeys,
43
+ lastPreKeyId,
44
+ preKeysRange: [creds.firstUnuploadedPreKeyId, range],
45
+ };
46
+ };
47
+ exports.generateOrGetPreKeys = generateOrGetPreKeys;
48
+ const xmppSignedPreKey = (key) => ({
49
+ tag: 'skey',
50
+ attrs: {},
51
+ content: [
52
+ { tag: 'id', attrs: {}, content: (0, generics_1.encodeBigEndian)(key.keyId, 3) },
53
+ { tag: 'value', attrs: {}, content: key.keyPair.public },
54
+ { tag: 'signature', attrs: {}, content: key.signature }
55
+ ]
56
+ });
57
+ exports.xmppSignedPreKey = xmppSignedPreKey;
58
+ const xmppPreKey = (pair, id) => ({
59
+ tag: 'key',
60
+ attrs: {},
61
+ content: [
62
+ { tag: 'id', attrs: {}, content: (0, generics_1.encodeBigEndian)(id, 3) },
63
+ { tag: 'value', attrs: {}, content: pair.public }
64
+ ]
65
+ });
66
+ exports.xmppPreKey = xmppPreKey;
67
+ const parseAndInjectE2ESessions = async (node, repository) => {
68
+ const extractKey = (key) => (key ? ({
69
+ keyId: WABinary_1.getBinaryNodeChildUInt(key, 'id', 3),
70
+ publicKey: crypto_1.generateSignalPubKey(WABinary_1.getBinaryNodeChildBuffer(key, 'value')),
71
+ signature: WABinary_1.getBinaryNodeChildBuffer(key, 'signature')
72
+ }) : undefined)
73
+ const nodes = WABinary_1.getBinaryNodeChildren(WABinary_1.getBinaryNodeChild(node, 'list'), 'user')
74
+ for (const node of nodes) {
75
+ WABinary_1.assertNodeErrorFree(node)
76
+ }
77
+ // Most of the work in repository.injectE2ESession is CPU intensive, not IO
78
+ // So Promise.all doesn't really help here,
79
+ // but blocks even loop if we're using it inside keys.transaction, and it makes it "sync" actually
80
+ // This way we chunk it in smaller parts and between those parts we can yield to the event loop
81
+ // It's rare case when you need to E2E sessions for so many users, but it's possible
82
+ const chunkSize = 100
83
+ const chunks = chunkArray(nodes, chunkSize)
84
+ for (const nodesChunk of chunks) {
85
+ await Promise.all(nodesChunk.map(async (node) => {
86
+ const signedKey = WABinary_1.getBinaryNodeChild(node, 'skey')
87
+ const key = WABinary_1.getBinaryNodeChild(node, 'key')
88
+ const identity = WABinary_1.getBinaryNodeChildBuffer(node, 'identity')
89
+ const jid = node.attrs.jid
90
+ const registrationId = WABinary_1.getBinaryNodeChildUInt(node, 'registration', 4)
91
+ await repository.injectE2ESession({
92
+ jid,
93
+ session: {
94
+ registrationId: registrationId,
95
+ identityKey: crypto_1.generateSignalPubKey(identity),
96
+ signedPreKey: extractKey(signedKey),
97
+ preKey: extractKey(key)
98
+ }
99
+ })
100
+ }))
101
+ }
102
+ }
103
+ exports.parseAndInjectE2ESessions = parseAndInjectE2ESessions;
104
+ const extractDeviceJids = (result, myJid, excludeZeroDevices) => {
105
+ const { user: myUser, device: myDevice } = WABinary_1.jidDecode(myJid)
106
+ const extracted = []
107
+ for (const userResult of result) {
108
+ const { devices, id } = userResult
109
+ const { user } = WABinary_1.jidDecode(id)
110
+ const deviceList = devices?.deviceList
111
+ if (Array.isArray(deviceList)) {
112
+ for (const { id: device, keyIndex } of deviceList) {
113
+ if ((!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
114
+ (myUser !== user || myDevice !== device) && // either different user or if me user, not this device
115
+ (device === 0 || !!keyIndex) // ensure that "key-index" is specified for "non-zero" devices, produces a bad req otherwise
116
+ ) {
117
+ extracted.push({ user, device })
118
+ }
119
+ }
120
+ }
121
+ }
122
+ return extracted
123
+ }
124
+ exports.extractDeviceJids = extractDeviceJids;
125
+ /**
126
+ * get the next N keys for upload or processing
127
+ * @param count number of pre-keys to get or generate
128
+ */
129
+ const getNextPreKeys = async ({ creds, keys }, count) => {
130
+ const { newPreKeys, lastPreKeyId, preKeysRange } = (0, exports.generateOrGetPreKeys)(creds, count);
131
+ const update = {
132
+ nextPreKeyId: Math.max(lastPreKeyId + 1, creds.nextPreKeyId),
133
+ firstUnuploadedPreKeyId: Math.max(creds.firstUnuploadedPreKeyId, lastPreKeyId + 1)
134
+ };
135
+ await keys.set({ 'pre-key': newPreKeys });
136
+ const preKeys = await (0, exports.getPreKeys)(keys, preKeysRange[0], preKeysRange[0] + preKeysRange[1]);
137
+ return { update, preKeys };
138
+ };
139
+ exports.getNextPreKeys = getNextPreKeys;
140
+ const getNextPreKeysNode = async (state, count) => {
141
+ const { creds } = state;
142
+ const { update, preKeys } = await (0, exports.getNextPreKeys)(state, count);
143
+ const node = {
144
+ tag: 'iq',
145
+ attrs: {
146
+ xmlns: 'encrypt',
147
+ type: 'set',
148
+ to: WABinary_1.S_WHATSAPP_NET,
149
+ },
150
+ content: [
151
+ { tag: 'registration', attrs: {}, content: (0, generics_1.encodeBigEndian)(creds.registrationId) },
152
+ { tag: 'type', attrs: {}, content: Defaults_1.KEY_BUNDLE_TYPE },
153
+ { tag: 'identity', attrs: {}, content: creds.signedIdentityKey.public },
154
+ { tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k => (0, exports.xmppPreKey)(preKeys[+k], +k)) },
155
+ (0, exports.xmppSignedPreKey)(creds.signedPreKey)
156
+ ]
157
+ };
158
+ return { update, node };
159
+ };
160
+ exports.getNextPreKeysNode = getNextPreKeysNode;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildAckStanza = buildAckStanza;
4
+ /**
5
+ * Builds an ACK stanza for a received node.
6
+ * Pure function -- no I/O, no side effects.
7
+ *
8
+ * NOTE: ported as an available utility. Not automatically wired into
9
+ * edgar-bail's existing ack-sending call sites (which build their ack
10
+ * stanzas inline) to avoid touching the message receive hot path blindly.
11
+ */
12
+ function buildAckStanza(node, errorCode, meId) {
13
+ const { tag, attrs } = node;
14
+ const stanza = {
15
+ tag: 'ack',
16
+ attrs: {
17
+ id: attrs.id,
18
+ to: attrs.from,
19
+ class: tag
20
+ }
21
+ };
22
+ if (errorCode) {
23
+ stanza.attrs.error = errorCode.toString();
24
+ }
25
+ if (attrs.participant) {
26
+ stanza.attrs.participant = attrs.participant;
27
+ }
28
+ if (attrs.recipient) {
29
+ stanza.attrs.recipient = attrs.recipient;
30
+ }
31
+ if (attrs.type) {
32
+ stanza.attrs.type = attrs.type;
33
+ }
34
+ if (tag === 'message' && meId) {
35
+ stanza.attrs.from = meId;
36
+ }
37
+ return stanza;
38
+ }
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.emitSyncActionResults = exports.processContactAction = void 0;
4
+ const WABinary_1 = require("../WABinary");
5
+ /**
6
+ * Process contactAction and return events to emit.
7
+ * Pure function - no side effects.
8
+ */
9
+ const processContactAction = (action, id, logger) => {
10
+ const results = [];
11
+ if (!id) {
12
+ logger === null || logger === void 0 ? void 0 : logger.warn({ hasFullName: !!action.fullName, hasLidJid: !!action.lidJid, hasPnJid: !!action.pnJid }, 'contactAction sync: missing id in index');
13
+ return results;
14
+ }
15
+ const lidJid = action.lidJid;
16
+ const idIsPn = (0, WABinary_1.isPnUser)(id);
17
+ // PN is in index[1], not in contactAction.pnJid which is usually null
18
+ const phoneNumber = idIsPn ? id : action.pnJid || undefined;
19
+ results.push({
20
+ event: 'contacts.upsert',
21
+ data: [
22
+ {
23
+ id,
24
+ name: action.fullName || action.firstName || action.username || undefined,
25
+ username: action.username || undefined,
26
+ lid: lidJid || undefined,
27
+ phoneNumber
28
+ }
29
+ ]
30
+ });
31
+ if (lidJid && (0, WABinary_1.isLidUser)(lidJid) && idIsPn) {
32
+ results.push({
33
+ event: 'lid-mapping.update',
34
+ data: { lid: lidJid, pn: id }
35
+ });
36
+ }
37
+ return results;
38
+ };
39
+ exports.processContactAction = processContactAction;
40
+ const emitSyncActionResults = (ev, results) => {
41
+ for (const result of results) {
42
+ if (result.event === 'contacts.upsert') {
43
+ ev.emit('contacts.upsert', result.data);
44
+ }
45
+ else {
46
+ ev.emit('lid-mapping.update', result.data);
47
+ }
48
+ }
49
+ };
50
+ exports.emitSyncActionResults = emitSyncActionResults;