@alannxd/baileys 6.0.4 → 6.0.5

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 (234) hide show
  1. package/WAProto/GenerateStatics.sh +3 -0
  2. package/WAProto/WAProto.proto +5479 -0
  3. package/WAProto/fix-imports.js +85 -0
  4. package/WAProto/index.d.ts +14017 -0
  5. package/WAProto/index.js +201 -160
  6. package/engine-requirements.js +1 -1
  7. package/lib/Defaults/index.d.ts +37 -15
  8. package/lib/Defaults/index.js +119 -136
  9. package/lib/Signal/Group/ciphertext-message.d.ts +1 -0
  10. package/lib/Signal/Group/ciphertext-message.js +2 -5
  11. package/lib/Signal/Group/group-session-builder.d.ts +4 -3
  12. package/lib/Signal/Group/group-session-builder.js +7 -41
  13. package/lib/Signal/Group/group_cipher.d.ts +4 -4
  14. package/lib/Signal/Group/group_cipher.js +37 -51
  15. package/lib/Signal/Group/index.d.ts +12 -11
  16. package/lib/Signal/Group/index.js +12 -57
  17. package/lib/Signal/Group/keyhelper.d.ts +2 -1
  18. package/lib/Signal/Group/keyhelper.js +7 -44
  19. package/lib/Signal/Group/sender-chain-key.d.ts +3 -2
  20. package/lib/Signal/Group/sender-chain-key.js +7 -15
  21. package/lib/Signal/Group/sender-key-distribution-message.d.ts +2 -1
  22. package/lib/Signal/Group/sender-key-distribution-message.js +8 -11
  23. package/lib/Signal/Group/sender-key-message.d.ts +2 -1
  24. package/lib/Signal/Group/sender-key-message.js +9 -12
  25. package/lib/Signal/Group/sender-key-name.d.ts +1 -0
  26. package/lib/Signal/Group/sender-key-name.js +2 -5
  27. package/lib/Signal/Group/sender-key-record.d.ts +3 -2
  28. package/lib/Signal/Group/sender-key-record.js +9 -21
  29. package/lib/Signal/Group/sender-key-state.d.ts +7 -6
  30. package/lib/Signal/Group/sender-key-state.js +27 -42
  31. package/lib/Signal/Group/sender-message-key.d.ts +1 -0
  32. package/lib/Signal/Group/sender-message-key.js +4 -7
  33. package/lib/Signal/libsignal.d.ts +5 -3
  34. package/lib/Signal/libsignal.js +347 -90
  35. package/lib/Signal/lid-mapping.d.ts +23 -0
  36. package/lib/Signal/lid-mapping.js +277 -0
  37. package/lib/Socket/Client/index.d.ts +3 -3
  38. package/lib/Socket/Client/index.js +3 -19
  39. package/lib/Socket/Client/{abstract-socket-client.d.ts → types.d.ts} +4 -5
  40. package/lib/Socket/Client/types.js +11 -0
  41. package/lib/Socket/Client/{web-socket-client.d.ts → websocket.d.ts} +3 -2
  42. package/lib/Socket/Client/websocket.js +54 -0
  43. package/lib/Socket/business.d.ts +154 -108
  44. package/lib/Socket/business.js +162 -43
  45. package/lib/Socket/chats.d.ts +96 -239
  46. package/lib/Socket/chats.js +627 -427
  47. package/lib/Socket/communities.d.ts +239 -146
  48. package/lib/Socket/communities.js +90 -80
  49. package/lib/Socket/groups.d.ts +104 -57
  50. package/lib/Socket/groups.js +154 -161
  51. package/lib/Socket/index.d.ts +202 -115
  52. package/lib/Socket/index.js +11 -10
  53. package/lib/Socket/luxu.d.ts +22 -266
  54. package/lib/Socket/luxu.js +422 -465
  55. package/lib/Socket/messages-recv.d.ts +136 -84
  56. package/lib/Socket/messages-recv.js +1421 -615
  57. package/lib/Socket/messages-send.d.ts +142 -126
  58. package/lib/Socket/messages-send.js +878 -671
  59. package/lib/Socket/mex.d.ts +3 -0
  60. package/lib/Socket/mex.js +42 -0
  61. package/lib/Socket/newsletter.d.ts +121 -85
  62. package/lib/Socket/newsletter.js +147 -272
  63. package/lib/Socket/socket.d.ts +34 -19
  64. package/lib/Socket/socket.js +544 -313
  65. package/lib/Store/index.d.ts +10 -3
  66. package/lib/Store/index.js +10 -10
  67. package/lib/Store/keyed-db.d.ts +22 -0
  68. package/lib/Store/keyed-db.js +108 -0
  69. package/lib/Store/make-cache-manager-store.d.ts +17 -11
  70. package/lib/Store/make-cache-manager-store.js +43 -41
  71. package/lib/Store/make-in-memory-store.d.ts +39 -118
  72. package/lib/Store/make-in-memory-store.js +112 -341
  73. package/lib/Store/make-ordered-dictionary.d.ts +11 -10
  74. package/lib/Store/make-ordered-dictionary.js +14 -20
  75. package/lib/Store/object-repository.d.ts +10 -9
  76. package/lib/Store/object-repository.js +11 -6
  77. package/lib/Types/Auth.d.ts +19 -12
  78. package/lib/Types/Auth.js +2 -2
  79. package/lib/Types/Bussines.d.ts +25 -0
  80. package/lib/Types/Bussines.js +2 -0
  81. package/lib/Types/Call.d.ts +3 -1
  82. package/lib/Types/Call.js +2 -2
  83. package/lib/Types/Chat.d.ts +35 -13
  84. package/lib/Types/Chat.js +8 -4
  85. package/lib/Types/Contact.d.ts +8 -1
  86. package/lib/Types/Contact.js +2 -2
  87. package/lib/Types/Events.d.ts +116 -17
  88. package/lib/Types/Events.js +2 -2
  89. package/lib/Types/GroupMetadata.d.ts +21 -5
  90. package/lib/Types/GroupMetadata.js +2 -2
  91. package/lib/Types/Label.d.ts +12 -0
  92. package/lib/Types/Label.js +3 -5
  93. package/lib/Types/LabelAssociation.d.ts +1 -0
  94. package/lib/Types/LabelAssociation.js +3 -5
  95. package/lib/Types/Message.d.ts +105 -58
  96. package/lib/Types/Message.js +11 -9
  97. package/lib/Types/Mex.d.ts +141 -0
  98. package/lib/Types/Mex.js +37 -0
  99. package/lib/Types/Product.d.ts +2 -1
  100. package/lib/Types/Product.js +2 -2
  101. package/lib/Types/Signal.d.ts +32 -2
  102. package/lib/Types/Signal.js +2 -2
  103. package/lib/Types/Socket.d.ts +50 -25
  104. package/lib/Types/Socket.js +3 -2
  105. package/lib/Types/State.d.ts +72 -2
  106. package/lib/Types/State.js +56 -2
  107. package/lib/Types/USync.d.ts +3 -2
  108. package/lib/Types/USync.js +2 -2
  109. package/lib/Types/index.d.ts +22 -14
  110. package/lib/Types/index.js +15 -31
  111. package/lib/Utils/auth-utils.d.ts +12 -6
  112. package/lib/Utils/auth-utils.js +239 -143
  113. package/lib/Utils/browser-utils.d.ts +4 -0
  114. package/lib/Utils/browser-utils.js +28 -0
  115. package/lib/Utils/business.d.ts +3 -2
  116. package/lib/Utils/business.js +66 -69
  117. package/lib/Utils/chat-utils.d.ts +52 -23
  118. package/lib/Utils/chat-utils.js +396 -253
  119. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  120. package/lib/Utils/companion-reg-client-utils.js +35 -0
  121. package/lib/Utils/crypto.d.ts +18 -22
  122. package/lib/Utils/crypto.js +57 -90
  123. package/lib/Utils/decode-wa-message.d.ts +55 -8
  124. package/lib/Utils/decode-wa-message.js +203 -84
  125. package/lib/Utils/event-buffer.d.ts +9 -8
  126. package/lib/Utils/event-buffer.js +185 -77
  127. package/lib/Utils/generics.d.ts +28 -29
  128. package/lib/Utils/generics.js +180 -210
  129. package/lib/Utils/history.d.ts +18 -9
  130. package/lib/Utils/history.js +93 -55
  131. package/lib/Utils/identity-change-handler.d.ts +44 -0
  132. package/lib/Utils/identity-change-handler.js +50 -0
  133. package/lib/Utils/index.d.ts +22 -17
  134. package/lib/Utils/index.js +22 -33
  135. package/lib/Utils/link-preview.d.ts +5 -5
  136. package/lib/Utils/link-preview.js +16 -24
  137. package/lib/Utils/logger.d.ts +11 -3
  138. package/lib/Utils/logger.js +3 -7
  139. package/lib/Utils/lt-hash.d.ts +8 -12
  140. package/lib/Utils/lt-hash.js +3 -46
  141. package/lib/Utils/make-mutex.d.ts +4 -2
  142. package/lib/Utils/make-mutex.js +24 -34
  143. package/lib/Utils/message-retry-manager.d.ts +115 -0
  144. package/lib/Utils/message-retry-manager.js +265 -0
  145. package/lib/Utils/messages-media.d.ts +61 -44
  146. package/lib/Utils/messages-media.js +451 -482
  147. package/lib/Utils/messages.d.ts +32 -18
  148. package/lib/Utils/messages.js +458 -369
  149. package/lib/Utils/noise-handler.d.ts +13 -14
  150. package/lib/Utils/noise-handler.js +145 -99
  151. package/lib/Utils/offline-node-processor.d.ts +17 -0
  152. package/lib/Utils/offline-node-processor.js +40 -0
  153. package/lib/Utils/pre-key-manager.d.ts +28 -0
  154. package/lib/Utils/pre-key-manager.js +106 -0
  155. package/lib/Utils/process-message.d.ts +31 -12
  156. package/lib/Utils/process-message.js +459 -150
  157. package/lib/Utils/reporting-utils.d.ts +11 -0
  158. package/lib/Utils/reporting-utils.js +258 -0
  159. package/lib/Utils/signal.d.ts +20 -5
  160. package/lib/Utils/signal.js +120 -72
  161. package/lib/Utils/stanza-ack.d.ts +11 -0
  162. package/lib/Utils/stanza-ack.js +38 -0
  163. package/lib/Utils/sync-action-utils.d.ts +19 -0
  164. package/lib/Utils/sync-action-utils.js +49 -0
  165. package/lib/Utils/tc-token-utils.d.ts +37 -0
  166. package/lib/Utils/tc-token-utils.js +163 -0
  167. package/lib/Utils/use-multi-file-auth-state.d.ts +2 -2
  168. package/lib/Utils/use-multi-file-auth-state.js +29 -27
  169. package/lib/Utils/validate-connection.d.ts +7 -7
  170. package/lib/Utils/validate-connection.js +73 -99
  171. package/lib/WABinary/constants.d.ts +25 -27
  172. package/lib/WABinary/constants.js +1281 -20
  173. package/lib/WABinary/decode.d.ts +5 -5
  174. package/lib/WABinary/decode.js +52 -42
  175. package/lib/WABinary/encode.d.ts +3 -3
  176. package/lib/WABinary/encode.js +110 -155
  177. package/lib/WABinary/generic-utils.d.ts +8 -7
  178. package/lib/WABinary/generic-utils.js +48 -49
  179. package/lib/WABinary/index.d.ts +6 -5
  180. package/lib/WABinary/index.js +6 -21
  181. package/lib/WABinary/jid-utils.d.ts +25 -8
  182. package/lib/WABinary/jid-utils.js +74 -40
  183. package/lib/WABinary/types.d.ts +2 -1
  184. package/lib/WABinary/types.js +2 -2
  185. package/lib/WAM/BinaryInfo.d.ts +3 -11
  186. package/lib/WAM/BinaryInfo.js +2 -5
  187. package/lib/WAM/constants.d.ts +5 -3
  188. package/lib/WAM/constants.js +19071 -11568
  189. package/lib/WAM/encode.d.ts +3 -3
  190. package/lib/WAM/encode.js +17 -22
  191. package/lib/WAM/index.d.ts +4 -3
  192. package/lib/WAM/index.js +4 -19
  193. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +4 -3
  194. package/lib/WAUSync/Protocols/USyncContactProtocol.js +33 -13
  195. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +3 -2
  196. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -14
  197. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +3 -2
  198. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -12
  199. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +3 -2
  200. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -13
  201. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  202. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
  203. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +4 -3
  204. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -22
  205. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +5 -3
  206. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +13 -8
  207. package/lib/WAUSync/Protocols/index.d.ts +6 -4
  208. package/lib/WAUSync/Protocols/index.js +6 -20
  209. package/lib/WAUSync/USyncQuery.d.ts +6 -4
  210. package/lib/WAUSync/USyncQuery.js +44 -35
  211. package/lib/WAUSync/USyncUser.d.ts +10 -5
  212. package/lib/WAUSync/USyncUser.js +10 -5
  213. package/lib/WAUSync/index.d.ts +4 -0
  214. package/lib/WAUSync/index.js +4 -19
  215. package/lib/index.d.ts +10 -9
  216. package/lib/index.js +12 -34
  217. package/package.json +84 -51
  218. package/WAProto/fix-import.js +0 -29
  219. package/lib/Defaults/baileys-version.json +0 -3
  220. package/lib/Defaults/phonenumber-mcc.json +0 -223
  221. package/lib/Signal/Group/queue-job.d.ts +0 -1
  222. package/lib/Signal/Group/queue-job.js +0 -57
  223. package/lib/Socket/Client/abstract-socket-client.js +0 -13
  224. package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
  225. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  226. package/lib/Socket/Client/web-socket-client.js +0 -62
  227. package/lib/Socket/registration.d.ts +0 -267
  228. package/lib/Socket/registration.js +0 -166
  229. package/lib/Socket/usync.d.ts +0 -36
  230. package/lib/Socket/usync.js +0 -70
  231. package/lib/Types/Newsletter.d.ts +0 -103
  232. package/lib/Types/Newsletter.js +0 -38
  233. package/lib/Utils/baileys-event-stream.d.ts +0 -16
  234. package/lib/Utils/baileys-event-stream.js +0 -63
@@ -0,0 +1,11 @@
1
+ import { proto } from '../../WAProto/index.js';
2
+ import type { WAMessageContent, WAMessageKey } from '../Types/index.js';
3
+ import type { BinaryNode } from '../WABinary/index.js';
4
+ export type ReportingField = {
5
+ f: number;
6
+ m?: boolean;
7
+ s?: ReportingField[];
8
+ };
9
+ export declare const shouldIncludeReportingToken: (message: proto.IMessage) => boolean;
10
+ export declare const getMessageReportingToken: (msgProtobuf: Buffer, message: WAMessageContent, key: WAMessageKey) => Promise<BinaryNode | null>;
11
+ //# 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
@@ -1,6 +1,7 @@
1
- import { SignalRepository } from '../Types';
2
- import { AuthenticationCreds, AuthenticationState, KeyPair, SignalIdentity, SignalKeyStore, SignedKeyPair } from '../Types/Auth';
3
- import { BinaryNode, JidWithDevice } from '../WABinary';
1
+ import type { SignalRepositoryWithLIDStore } from '../Types/index.js';
2
+ import type { AuthenticationCreds, AuthenticationState, KeyPair, SignalIdentity, SignalKeyStore, SignedKeyPair } from '../Types/Auth.js';
3
+ import { type BinaryNode, type FullJid } from '../WABinary/index.js';
4
+ import type { USyncQueryResultList } from '../WAUSync/index.js';
4
5
  export declare const createSignalIdentity: (wid: string, accountSignatureKey: Uint8Array) => SignalIdentity;
5
6
  export declare const getPreKeys: ({ get }: SignalKeyStore, min: number, limit: number) => Promise<{
6
7
  [id: string]: KeyPair;
@@ -14,8 +15,21 @@ export declare const generateOrGetPreKeys: (creds: AuthenticationCreds, range: n
14
15
  };
15
16
  export declare const xmppSignedPreKey: (key: SignedKeyPair) => BinaryNode;
16
17
  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[];
18
+ export declare const extractE2ESessionFromRetryReceipt: (receipt: BinaryNode) => {
19
+ registrationId: number;
20
+ identityKey: Uint8Array<ArrayBufferLike> | Buffer<ArrayBufferLike>;
21
+ signedPreKey: {
22
+ keyId: number;
23
+ publicKey: Uint8Array<ArrayBufferLike> | Buffer<ArrayBufferLike>;
24
+ signature: Uint8Array<ArrayBufferLike> | Buffer<ArrayBufferLike>;
25
+ };
26
+ preKey: {
27
+ keyId: number;
28
+ publicKey: Uint8Array;
29
+ } | undefined;
30
+ } | null;
31
+ export declare const parseAndInjectE2ESessions: (node: BinaryNode, repository: SignalRepositoryWithLIDStore) => Promise<void>;
32
+ export declare const extractDeviceJids: (result: USyncQueryResultList[], myJid: string, myLid: string, excludeZeroDevices: boolean) => FullJid[];
19
33
  /**
20
34
  * get the next N keys for upload or processing
21
35
  * @param count number of pre-keys to get or generate
@@ -30,3 +44,4 @@ export declare const getNextPreKeysNode: (state: AuthenticationState, count: num
30
44
  update: Partial<AuthenticationCreds>;
31
45
  node: BinaryNode;
32
46
  }>;
47
+ //# sourceMappingURL=signal.d.ts.map
@@ -1,153 +1,201 @@
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 lodash_1 = require("lodash")
8
- const generics_1 = require("./generics");
9
- const createSignalIdentity = (wid, accountSignatureKey) => {
1
+ import { KEY_BUNDLE_TYPE } from '../Defaults/index.js';
2
+ import { assertNodeErrorFree, getBinaryNodeChild, getBinaryNodeChildBuffer, getBinaryNodeChildren, getBinaryNodeChildUInt, getServerFromDomainType, jidDecode, S_WHATSAPP_NET, WAJIDDomains } from '../WABinary/index.js';
3
+ import { Curve, generateSignalPubKey } from './crypto.js';
4
+ import { encodeBigEndian } from './generics.js';
5
+ function chunk(array, size) {
6
+ const chunks = [];
7
+ for (let i = 0; i < array.length; i += size) {
8
+ chunks.push(array.slice(i, i + size));
9
+ }
10
+ return chunks;
11
+ }
12
+ export const createSignalIdentity = (wid, accountSignatureKey) => {
10
13
  return {
11
14
  identifier: { name: wid, deviceId: 0 },
12
- identifierKey: (0, crypto_1.generateSignalPubKey)(accountSignatureKey)
15
+ identifierKey: generateSignalPubKey(accountSignatureKey)
13
16
  };
14
17
  };
15
- exports.createSignalIdentity = createSignalIdentity;
16
- const getPreKeys = async ({ get }, min, limit) => {
18
+ export const getPreKeys = async ({ get }, min, limit) => {
17
19
  const idList = [];
18
20
  for (let id = min; id < limit; id++) {
19
21
  idList.push(id.toString());
20
22
  }
21
23
  return get('pre-key', idList);
22
24
  };
23
- exports.getPreKeys = getPreKeys;
24
- const generateOrGetPreKeys = (creds, range) => {
25
+ export const generateOrGetPreKeys = (creds, range) => {
25
26
  const avaliable = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId;
26
27
  const remaining = range - avaliable;
27
28
  const lastPreKeyId = creds.nextPreKeyId + remaining - 1;
28
29
  const newPreKeys = {};
29
30
  if (remaining > 0) {
30
31
  for (let i = creds.nextPreKeyId; i <= lastPreKeyId; i++) {
31
- newPreKeys[i] = crypto_1.Curve.generateKeyPair();
32
+ newPreKeys[i] = Curve.generateKeyPair();
32
33
  }
33
34
  }
34
35
  return {
35
36
  newPreKeys,
36
37
  lastPreKeyId,
37
- preKeysRange: [creds.firstUnuploadedPreKeyId, range],
38
+ preKeysRange: [creds.firstUnuploadedPreKeyId, range]
38
39
  };
39
40
  };
40
- exports.generateOrGetPreKeys = generateOrGetPreKeys;
41
- const xmppSignedPreKey = (key) => ({
41
+ export const xmppSignedPreKey = (key) => ({
42
42
  tag: 'skey',
43
43
  attrs: {},
44
44
  content: [
45
- { tag: 'id', attrs: {}, content: (0, generics_1.encodeBigEndian)(key.keyId, 3) },
45
+ { tag: 'id', attrs: {}, content: encodeBigEndian(key.keyId, 3) },
46
46
  { tag: 'value', attrs: {}, content: key.keyPair.public },
47
47
  { tag: 'signature', attrs: {}, content: key.signature }
48
48
  ]
49
49
  });
50
- exports.xmppSignedPreKey = xmppSignedPreKey;
51
- const xmppPreKey = (pair, id) => ({
50
+ export const xmppPreKey = (pair, id) => ({
52
51
  tag: 'key',
53
52
  attrs: {},
54
53
  content: [
55
- { tag: 'id', attrs: {}, content: (0, generics_1.encodeBigEndian)(id, 3) },
54
+ { tag: 'id', attrs: {}, content: encodeBigEndian(id, 3) },
56
55
  { tag: 'value', attrs: {}, content: pair.public }
57
56
  ]
58
57
  });
59
- exports.xmppPreKey = xmppPreKey;
60
- const parseAndInjectE2ESessions = async (node, repository) => {
61
- const extractKey = (key) => (key ? ({
62
- keyId: WABinary_1.getBinaryNodeChildUInt(key, 'id', 3),
63
- publicKey: crypto_1.generateSignalPubKey(WABinary_1.getBinaryNodeChildBuffer(key, 'value')),
64
- signature: WABinary_1.getBinaryNodeChildBuffer(key, 'signature')
65
- }) : undefined)
66
- const nodes = WABinary_1.getBinaryNodeChildren(WABinary_1.getBinaryNodeChild(node, 'list'), 'user')
58
+ const isValidUInt = (n) => typeof n === 'number' && Number.isInteger(n);
59
+ export const extractE2ESessionFromRetryReceipt = (receipt) => {
60
+ const keysNode = getBinaryNodeChild(receipt, 'keys');
61
+ if (!keysNode)
62
+ return null;
63
+ const typeBuf = getBinaryNodeChildBuffer(keysNode, 'type');
64
+ if (!typeBuf || typeBuf.length !== 1 || typeBuf[0] !== KEY_BUNDLE_TYPE[0])
65
+ return null;
66
+ const identity = getBinaryNodeChildBuffer(keysNode, 'identity');
67
+ const skey = getBinaryNodeChild(keysNode, 'skey');
68
+ if (!identity || identity.length !== 32 || !skey)
69
+ return null;
70
+ const registrationId = getBinaryNodeChildUInt(receipt, 'registration', 4);
71
+ if (!isValidUInt(registrationId))
72
+ return null;
73
+ const signedPubKey = getBinaryNodeChildBuffer(skey, 'value');
74
+ const signedSig = getBinaryNodeChildBuffer(skey, 'signature');
75
+ const signedKeyId = getBinaryNodeChildUInt(skey, 'id', 3);
76
+ if (!signedPubKey || signedPubKey.length !== 32 || !signedSig || !isValidUInt(signedKeyId)) {
77
+ return null;
78
+ }
79
+ const preKeyNode = getBinaryNodeChild(keysNode, 'key');
80
+ let preKey;
81
+ if (preKeyNode) {
82
+ const preKeyPub = getBinaryNodeChildBuffer(preKeyNode, 'value');
83
+ const preKeyId = getBinaryNodeChildUInt(preKeyNode, 'id', 3);
84
+ if (!preKeyPub || preKeyPub.length !== 32 || !isValidUInt(preKeyId)) {
85
+ return null;
86
+ }
87
+ preKey = { keyId: preKeyId, publicKey: generateSignalPubKey(preKeyPub) };
88
+ }
89
+ return {
90
+ registrationId,
91
+ identityKey: generateSignalPubKey(identity),
92
+ signedPreKey: {
93
+ keyId: signedKeyId,
94
+ publicKey: generateSignalPubKey(signedPubKey),
95
+ signature: signedSig
96
+ },
97
+ preKey
98
+ };
99
+ };
100
+ export const parseAndInjectE2ESessions = async (node, repository) => {
101
+ const extractKey = (key) => key
102
+ ? {
103
+ keyId: getBinaryNodeChildUInt(key, 'id', 3),
104
+ publicKey: generateSignalPubKey(getBinaryNodeChildBuffer(key, 'value')),
105
+ signature: getBinaryNodeChildBuffer(key, 'signature')
106
+ }
107
+ : undefined;
108
+ const nodes = getBinaryNodeChildren(getBinaryNodeChild(node, 'list'), 'user');
67
109
  for (const node of nodes) {
68
- WABinary_1.assertNodeErrorFree(node)
110
+ assertNodeErrorFree(node);
69
111
  }
70
112
  // Most of the work in repository.injectE2ESession is CPU intensive, not IO
71
113
  // So Promise.all doesn't really help here,
72
114
  // but blocks even loop if we're using it inside keys.transaction, and it makes it "sync" actually
73
115
  // This way we chunk it in smaller parts and between those parts we can yield to the event loop
74
116
  // It's rare case when you need to E2E sessions for so many users, but it's possible
75
- const chunkSize = 100
76
- const chunks = lodash_1.chunk(nodes, chunkSize)
117
+ const chunkSize = 100;
118
+ const chunks = chunk(nodes, chunkSize);
77
119
  for (const nodesChunk of chunks) {
78
- await Promise.all(nodesChunk.map(async (node) => {
79
- const signedKey = WABinary_1.getBinaryNodeChild(node, 'skey')
80
- const key = WABinary_1.getBinaryNodeChild(node, 'key')
81
- const identity = WABinary_1.getBinaryNodeChildBuffer(node, 'identity')
82
- const jid = node.attrs.jid
83
- const registrationId = WABinary_1.getBinaryNodeChildUInt(node, 'registration', 4)
120
+ for (const node of nodesChunk) {
121
+ const signedKey = getBinaryNodeChild(node, 'skey');
122
+ const key = getBinaryNodeChild(node, 'key');
123
+ const identity = getBinaryNodeChildBuffer(node, 'identity');
124
+ const jid = node.attrs.jid;
125
+ const registrationId = getBinaryNodeChildUInt(node, 'registration', 4);
84
126
  await repository.injectE2ESession({
85
127
  jid,
86
128
  session: {
87
129
  registrationId: registrationId,
88
- identityKey: crypto_1.generateSignalPubKey(identity),
130
+ identityKey: generateSignalPubKey(identity),
89
131
  signedPreKey: extractKey(signedKey),
90
132
  preKey: extractKey(key)
91
133
  }
92
- })
93
- }))
134
+ });
135
+ }
94
136
  }
95
- }
96
- exports.parseAndInjectE2ESessions = parseAndInjectE2ESessions;
97
- const extractDeviceJids = (result, myJid, excludeZeroDevices) => {
98
- const { user: myUser, device: myDevice } = WABinary_1.jidDecode(myJid)
99
- const extracted = []
137
+ };
138
+ export const extractDeviceJids = (result, myJid, myLid, excludeZeroDevices) => {
139
+ const { user: myUser, device: myDevice } = jidDecode(myJid);
140
+ const extracted = [];
100
141
  for (const userResult of result) {
101
- const { devices, id } = userResult
102
- const { user } = WABinary_1.jidDecode(id)
103
- const deviceList = devices?.deviceList
104
- if (Array.isArray(deviceList)) {
105
- for (const { id: device, keyIndex } of deviceList) {
106
- if ((!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
107
- (myUser !== user || myDevice !== device) && // either different user or if me user, not this device
108
- (device === 0 || !!keyIndex) // ensure that "key-index" is specified for "non-zero" devices, produces a bad req otherwise
109
- ) {
110
- extracted.push({ user, device })
142
+ const { devices, id } = userResult;
143
+ const decoded = jidDecode(id), { user, server } = decoded;
144
+ let { domainType } = decoded;
145
+ const deviceList = devices?.deviceList;
146
+ if (!Array.isArray(deviceList))
147
+ continue;
148
+ for (const { id: device, keyIndex, isHosted } of deviceList) {
149
+ if ((!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
150
+ ((myUser !== user && myLid !== user) || myDevice !== device) && // either different user or if me user, not this device
151
+ (device === 0 || !!keyIndex) // ensure that "key-index" is specified for "non-zero" devices, produces a bad req otherwise
152
+ ) {
153
+ if (isHosted) {
154
+ domainType = domainType === WAJIDDomains.LID ? WAJIDDomains.HOSTED_LID : WAJIDDomains.HOSTED;
111
155
  }
156
+ extracted.push({
157
+ user,
158
+ device,
159
+ domainType,
160
+ server: getServerFromDomainType(server, domainType)
161
+ });
112
162
  }
113
163
  }
114
164
  }
115
- return extracted
116
- }
117
- exports.extractDeviceJids = extractDeviceJids;
165
+ return extracted;
166
+ };
118
167
  /**
119
168
  * get the next N keys for upload or processing
120
169
  * @param count number of pre-keys to get or generate
121
170
  */
122
- const getNextPreKeys = async ({ creds, keys }, count) => {
123
- const { newPreKeys, lastPreKeyId, preKeysRange } = (0, exports.generateOrGetPreKeys)(creds, count);
171
+ export const getNextPreKeys = async ({ creds, keys }, count) => {
172
+ const { newPreKeys, lastPreKeyId, preKeysRange } = generateOrGetPreKeys(creds, count);
124
173
  const update = {
125
174
  nextPreKeyId: Math.max(lastPreKeyId + 1, creds.nextPreKeyId),
126
175
  firstUnuploadedPreKeyId: Math.max(creds.firstUnuploadedPreKeyId, lastPreKeyId + 1)
127
176
  };
128
177
  await keys.set({ 'pre-key': newPreKeys });
129
- const preKeys = await (0, exports.getPreKeys)(keys, preKeysRange[0], preKeysRange[0] + preKeysRange[1]);
178
+ const preKeys = await getPreKeys(keys, preKeysRange[0], preKeysRange[0] + preKeysRange[1]);
130
179
  return { update, preKeys };
131
180
  };
132
- exports.getNextPreKeys = getNextPreKeys;
133
- const getNextPreKeysNode = async (state, count) => {
181
+ export const getNextPreKeysNode = async (state, count) => {
134
182
  const { creds } = state;
135
- const { update, preKeys } = await (0, exports.getNextPreKeys)(state, count);
183
+ const { update, preKeys } = await getNextPreKeys(state, count);
136
184
  const node = {
137
185
  tag: 'iq',
138
186
  attrs: {
139
187
  xmlns: 'encrypt',
140
188
  type: 'set',
141
- to: WABinary_1.S_WHATSAPP_NET,
189
+ to: S_WHATSAPP_NET
142
190
  },
143
191
  content: [
144
- { tag: 'registration', attrs: {}, content: (0, generics_1.encodeBigEndian)(creds.registrationId) },
145
- { tag: 'type', attrs: {}, content: Defaults_1.KEY_BUNDLE_TYPE },
192
+ { tag: 'registration', attrs: {}, content: encodeBigEndian(creds.registrationId) },
193
+ { tag: 'type', attrs: {}, content: KEY_BUNDLE_TYPE },
146
194
  { tag: 'identity', attrs: {}, content: creds.signedIdentityKey.public },
147
- { tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k => (0, exports.xmppPreKey)(preKeys[+k], +k)) },
148
- (0, exports.xmppSignedPreKey)(creds.signedPreKey)
195
+ { tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k => xmppPreKey(preKeys[+k], +k)) },
196
+ xmppSignedPreKey(creds.signedPreKey)
149
197
  ]
150
198
  };
151
199
  return { update, node };
152
200
  };
153
- exports.getNextPreKeysNode = getNextPreKeysNode;
201
+ //# sourceMappingURL=signal.js.map
@@ -0,0 +1,11 @@
1
+ import type { BinaryNode } from '../WABinary/index.js';
2
+ /**
3
+ * Builds an ACK stanza for a received node.
4
+ * Pure function -- no I/O, no side effects.
5
+ *
6
+ * Mirrors WhatsApp Web's ACK construction:
7
+ * - WAWebHandleMsgSendAck.sendAck / sendNack
8
+ * - WAWebCreateNackFromStanza.createNackFromStanza
9
+ */
10
+ export declare function buildAckStanza(node: BinaryNode, errorCode?: number, meId?: string): BinaryNode;
11
+ //# sourceMappingURL=stanza-ack.d.ts.map
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Builds an ACK stanza for a received node.
3
+ * Pure function -- no I/O, no side effects.
4
+ *
5
+ * Mirrors WhatsApp Web's ACK construction:
6
+ * - WAWebHandleMsgSendAck.sendAck / sendNack
7
+ * - WAWebCreateNackFromStanza.createNackFromStanza
8
+ */
9
+ export function buildAckStanza(node, errorCode, meId) {
10
+ const { tag, attrs } = node;
11
+ const stanza = {
12
+ tag: 'ack',
13
+ attrs: {
14
+ id: attrs.id,
15
+ to: attrs.from,
16
+ class: tag
17
+ }
18
+ };
19
+ if (errorCode) {
20
+ stanza.attrs.error = errorCode.toString();
21
+ }
22
+ if (attrs.participant) {
23
+ stanza.attrs.participant = attrs.participant;
24
+ }
25
+ if (attrs.recipient) {
26
+ stanza.attrs.recipient = attrs.recipient;
27
+ }
28
+ // WA Web always includes type when present: `n.type || DROP_ATTR`
29
+ if (attrs.type) {
30
+ stanza.attrs.type = attrs.type;
31
+ }
32
+ // WA Web WAWebHandleMsgSendAck.sendAck/sendNack always include `from` for message-class ACKs
33
+ if (tag === 'message' && meId) {
34
+ stanza.attrs.from = meId;
35
+ }
36
+ return stanza;
37
+ }
38
+ //# sourceMappingURL=stanza-ack.js.map
@@ -0,0 +1,19 @@
1
+ import { proto } from '../../WAProto/index.js';
2
+ import type { BaileysEventEmitter, BaileysEventMap, Contact } from '../Types/index.js';
3
+ import type { ILogger } from './logger.js';
4
+ export type ContactsUpsertResult = {
5
+ event: 'contacts.upsert';
6
+ data: Contact[];
7
+ };
8
+ export type LidMappingUpdateResult = {
9
+ event: 'lid-mapping.update';
10
+ data: BaileysEventMap['lid-mapping.update'];
11
+ };
12
+ export type SyncActionResult = ContactsUpsertResult | LidMappingUpdateResult;
13
+ /**
14
+ * Process contactAction and return events to emit.
15
+ * Pure function - no side effects.
16
+ */
17
+ export declare const processContactAction: (action: proto.SyncActionValue.IContactAction, id: string | undefined, logger?: ILogger) => SyncActionResult[];
18
+ export declare const emitSyncActionResults: (ev: BaileysEventEmitter, results: SyncActionResult[]) => void;
19
+ //# sourceMappingURL=sync-action-utils.d.ts.map