@ikyyjee/ikyysinggle 1.7.7

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 (142) hide show
  1. package/WAProto/GenerateStatics.sh +3 -0
  2. package/WAProto/WAProto.proto +8083 -0
  3. package/WAProto/fix-imports.js +85 -0
  4. package/WAProto/index.d.ts +29095 -0
  5. package/WAProto/index.js +172336 -0
  6. package/engine-requirements.js +10 -0
  7. package/lib/Defaults/index.js +194 -0
  8. package/lib/Signal/Group/ciphertext-message.js +12 -0
  9. package/lib/Signal/Group/group-session-builder.js +30 -0
  10. package/lib/Signal/Group/group_cipher.js +82 -0
  11. package/lib/Signal/Group/index.js +12 -0
  12. package/lib/Signal/Group/keyhelper.js +18 -0
  13. package/lib/Signal/Group/sender-chain-key.js +26 -0
  14. package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
  15. package/lib/Signal/Group/sender-key-message.js +66 -0
  16. package/lib/Signal/Group/sender-key-name.js +48 -0
  17. package/lib/Signal/Group/sender-key-record.js +41 -0
  18. package/lib/Signal/Group/sender-key-state.js +84 -0
  19. package/lib/Signal/Group/sender-message-key.js +26 -0
  20. package/lib/Signal/libsignal.js +431 -0
  21. package/lib/Signal/lid-mapping.js +277 -0
  22. package/lib/Socket/Client/index.js +3 -0
  23. package/lib/Socket/Client/types.js +11 -0
  24. package/lib/Socket/Client/websocket.js +102 -0
  25. package/lib/Socket/aigroups.js +221 -0
  26. package/lib/Socket/business.js +379 -0
  27. package/lib/Socket/chats.js +1193 -0
  28. package/lib/Socket/communities.js +431 -0
  29. package/lib/Socket/graphql.js +524 -0
  30. package/lib/Socket/groups.js +408 -0
  31. package/lib/Socket/index.js +49 -0
  32. package/lib/Socket/interop.js +341 -0
  33. package/lib/Socket/luxu.js +510 -0
  34. package/lib/Socket/managed-account.js +99 -0
  35. package/lib/Socket/messages-recv.js +2009 -0
  36. package/lib/Socket/messages-send.js +1608 -0
  37. package/lib/Socket/mex.js +41 -0
  38. package/lib/Socket/newsletter.js +399 -0
  39. package/lib/Socket/privacy.js +128 -0
  40. package/lib/Socket/registration.js +238 -0
  41. package/lib/Socket/socket.js +1000 -0
  42. package/lib/Socket/text-router.js +67 -0
  43. package/lib/Socket/username.js +234 -0
  44. package/lib/Store/index.js +10 -0
  45. package/lib/Store/keyed-db.js +108 -0
  46. package/lib/Store/make-cache-manager-store.js +85 -0
  47. package/lib/Store/make-in-memory-store.js +198 -0
  48. package/lib/Store/make-ordered-dictionary.js +75 -0
  49. package/lib/Store/object-repository.js +32 -0
  50. package/lib/Types/Auth.js +2 -0
  51. package/lib/Types/Bussines.js +2 -0
  52. package/lib/Types/Call.js +2 -0
  53. package/lib/Types/Chat.js +8 -0
  54. package/lib/Types/Contact.js +2 -0
  55. package/lib/Types/Events.js +2 -0
  56. package/lib/Types/GroupMetadata.js +2 -0
  57. package/lib/Types/Label.js +25 -0
  58. package/lib/Types/LabelAssociation.js +7 -0
  59. package/lib/Types/Message.js +11 -0
  60. package/lib/Types/Mex.js +114 -0
  61. package/lib/Types/Product.js +2 -0
  62. package/lib/Types/Signal.js +2 -0
  63. package/lib/Types/Socket.js +3 -0
  64. package/lib/Types/State.js +56 -0
  65. package/lib/Types/USync.js +2 -0
  66. package/lib/Types/index.js +26 -0
  67. package/lib/Utils/adaptive-healing.js +53 -0
  68. package/lib/Utils/auth-utils.js +302 -0
  69. package/lib/Utils/browser-utils.js +50 -0
  70. package/lib/Utils/business.js +231 -0
  71. package/lib/Utils/chat-utils.js +872 -0
  72. package/lib/Utils/command-loader.js +108 -0
  73. package/lib/Utils/companion-reg-client-utils.js +35 -0
  74. package/lib/Utils/consumer-application.js +106 -0
  75. package/lib/Utils/crypto.js +137 -0
  76. package/lib/Utils/curve25519-js.js +262 -0
  77. package/lib/Utils/decode-wa-message.js +498 -0
  78. package/lib/Utils/event-buffer.js +622 -0
  79. package/lib/Utils/generics.js +403 -0
  80. package/lib/Utils/group-history.js +47 -0
  81. package/lib/Utils/history.js +134 -0
  82. package/lib/Utils/identity-change-handler.js +50 -0
  83. package/lib/Utils/index.js +38 -0
  84. package/lib/Utils/jid-display-normalization.js +198 -0
  85. package/lib/Utils/link-preview.js +85 -0
  86. package/lib/Utils/logger.js +3 -0
  87. package/lib/Utils/lt-hash.js +8 -0
  88. package/lib/Utils/make-mutex.js +33 -0
  89. package/lib/Utils/message-composer.js +273 -0
  90. package/lib/Utils/message-retry-manager.js +267 -0
  91. package/lib/Utils/messages-media.js +791 -0
  92. package/lib/Utils/messages.js +1260 -0
  93. package/lib/Utils/meta-ai-msmsg.js +271 -0
  94. package/lib/Utils/native-bridge.js +77 -0
  95. package/lib/Utils/noise-handler.js +201 -0
  96. package/lib/Utils/offline-node-processor.js +40 -0
  97. package/lib/Utils/optimizer.js +90 -0
  98. package/lib/Utils/pre-key-manager.js +106 -0
  99. package/lib/Utils/process-message.js +630 -0
  100. package/lib/Utils/reporting-utils.js +258 -0
  101. package/lib/Utils/session-pool.js +73 -0
  102. package/lib/Utils/signal.js +207 -0
  103. package/lib/Utils/stanza-ack.js +38 -0
  104. package/lib/Utils/sticker.js +139 -0
  105. package/lib/Utils/sync-action-utils.js +49 -0
  106. package/lib/Utils/tc-token-utils.js +163 -0
  107. package/lib/Utils/use-multi-file-auth-state.js +121 -0
  108. package/lib/Utils/use-sqlite-auth-state.js +168 -0
  109. package/lib/Utils/validate-connection.js +203 -0
  110. package/lib/Utils/view-once-cache.js +79 -0
  111. package/lib/Utils/voip-rekey.js +25 -0
  112. package/lib/Utils/warmup.js +117 -0
  113. package/lib/WABinary/constants.js +1301 -0
  114. package/lib/WABinary/decode.js +262 -0
  115. package/lib/WABinary/encode.js +220 -0
  116. package/lib/WABinary/generic-utils.js +204 -0
  117. package/lib/WABinary/index.js +6 -0
  118. package/lib/WABinary/jid-utils.js +98 -0
  119. package/lib/WABinary/types.js +2 -0
  120. package/lib/WAM/BinaryInfo.js +10 -0
  121. package/lib/WAM/constants.js +22853 -0
  122. package/lib/WAM/encode.js +150 -0
  123. package/lib/WAM/index.js +4 -0
  124. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +41 -0
  125. package/lib/WAUSync/Protocols/USyncContactProtocol.js +52 -0
  126. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
  127. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  128. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +52 -0
  129. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +31 -0
  130. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +26 -0
  131. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
  132. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +35 -0
  133. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
  134. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
  135. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
  136. package/lib/WAUSync/Protocols/index.js +13 -0
  137. package/lib/WAUSync/USyncQuery.js +127 -0
  138. package/lib/WAUSync/USyncUser.js +31 -0
  139. package/lib/WAUSync/index.js +4 -0
  140. package/lib/antiban.js +4083 -0
  141. package/lib/index.js +24 -0
  142. package/package.json +147 -0
@@ -0,0 +1,267 @@
1
+ import { LRUCache } from 'lru-cache';
2
+ /** Number of sent messages to cache in memory for handling retry receipts */
3
+ const RECENT_MESSAGES_SIZE = 512;
4
+ const MESSAGE_KEY_SEPARATOR = '\u0000';
5
+ /** Timeout for session recreation - 1 hour */
6
+ const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000; // 1 hour in milliseconds
7
+ const PHONE_REQUEST_DELAY = 3000;
8
+ // Retry reason codes matching WhatsApp Web's Signal error codes.
9
+ export var RetryReason;
10
+ (function (RetryReason) {
11
+ RetryReason[RetryReason["UnknownError"] = 0] = "UnknownError";
12
+ RetryReason[RetryReason["SignalErrorNoSession"] = 1] = "SignalErrorNoSession";
13
+ RetryReason[RetryReason["SignalErrorInvalidKey"] = 2] = "SignalErrorInvalidKey";
14
+ RetryReason[RetryReason["SignalErrorInvalidKeyId"] = 3] = "SignalErrorInvalidKeyId";
15
+ /** MAC verification failed - most common cause of decryption failures */
16
+ RetryReason[RetryReason["SignalErrorInvalidMessage"] = 4] = "SignalErrorInvalidMessage";
17
+ RetryReason[RetryReason["SignalErrorInvalidSignature"] = 5] = "SignalErrorInvalidSignature";
18
+ RetryReason[RetryReason["SignalErrorFutureMessage"] = 6] = "SignalErrorFutureMessage";
19
+ /** Explicit MAC failure - session is definitely out of sync */
20
+ RetryReason[RetryReason["SignalErrorBadMac"] = 7] = "SignalErrorBadMac";
21
+ RetryReason[RetryReason["SignalErrorInvalidSession"] = 8] = "SignalErrorInvalidSession";
22
+ RetryReason[RetryReason["SignalErrorInvalidMsgKey"] = 9] = "SignalErrorInvalidMsgKey";
23
+ RetryReason[RetryReason["BadBroadcastEphemeralSetting"] = 10] = "BadBroadcastEphemeralSetting";
24
+ RetryReason[RetryReason["UnknownCompanionNoPrekey"] = 11] = "UnknownCompanionNoPrekey";
25
+ RetryReason[RetryReason["AdvFailure"] = 12] = "AdvFailure";
26
+ RetryReason[RetryReason["StatusRevokeDelay"] = 13] = "StatusRevokeDelay";
27
+ })(RetryReason || (RetryReason = {}));
28
+ /** Error codes that indicate a MAC failure and require immediate session recreation */
29
+ const MAC_ERROR_CODES = new Set([RetryReason.SignalErrorInvalidMessage, RetryReason.SignalErrorBadMac]);
30
+ export class MessageRetryManager {
31
+ constructor(logger, maxMsgRetryCount, cacheLimits = {}) {
32
+ this.logger = logger;
33
+ this.recentMessagesMap = new LRUCache({
34
+ max: RECENT_MESSAGES_SIZE,
35
+ ttl: 5 * 60 * 1000,
36
+ ttlAutopurge: true,
37
+ dispose: (_value, key) => {
38
+ const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR);
39
+ if (separatorIndex > -1) {
40
+ const messageId = key.slice(separatorIndex + MESSAGE_KEY_SEPARATOR.length);
41
+ this.messageKeyIndex.delete(messageId);
42
+ }
43
+ }
44
+ });
45
+ this.messageKeyIndex = new Map();
46
+ this.sessionRecreateHistory = new LRUCache({
47
+ max: cacheLimits.sessionRecreateHistoryMax ?? 2000, // hard cap so extreme-volume sessions can't grow this unbounded between TTL sweeps
48
+ ttl: RECREATE_SESSION_TIMEOUT * 2,
49
+ ttlAutopurge: true
50
+ });
51
+ this.retryCounters = new LRUCache({
52
+ max: cacheLimits.retryCountersMax ?? 5000, // same reasoning as above
53
+ ttl: 15 * 60 * 1000,
54
+ ttlAutopurge: true,
55
+ updateAgeOnGet: true
56
+ }); // 15 minutes TTL
57
+ this.baseKeys = new LRUCache({
58
+ max: 1024,
59
+ ttl: 15 * 60 * 1000,
60
+ ttlAutopurge: true
61
+ });
62
+ this.pendingPhoneRequests = {};
63
+ this.maxMsgRetryCount = 5;
64
+ this.statistics = {
65
+ totalRetries: 0,
66
+ successfulRetries: 0,
67
+ failedRetries: 0,
68
+ mediaRetries: 0,
69
+ sessionRecreations: 0,
70
+ phoneRequests: 0
71
+ };
72
+ this.maxMsgRetryCount = maxMsgRetryCount;
73
+ }
74
+ /**
75
+ * Add a recent message to the cache for retry handling
76
+ */
77
+ addRecentMessage(to, id, message) {
78
+ const key = { to, id };
79
+ const keyStr = this.keyToString(key);
80
+ // Add new message
81
+ this.recentMessagesMap.set(keyStr, {
82
+ message,
83
+ timestamp: Date.now()
84
+ });
85
+ this.messageKeyIndex.set(id, keyStr);
86
+ this.logger.debug(`Added message to retry cache: ${to}/${id}`);
87
+ }
88
+ /**
89
+ * Get a recent message from the cache
90
+ */
91
+ getRecentMessage(to, id) {
92
+ const key = { to, id };
93
+ const keyStr = this.keyToString(key);
94
+ return this.recentMessagesMap.get(keyStr);
95
+ }
96
+ /**
97
+ * Check if a session should be recreated based on retry count, history, and error code.
98
+ * MAC errors (codes 4 and 7) trigger immediate session recreation regardless of timeout.
99
+ */
100
+ shouldRecreateSession(jid, hasSession, errorCode) {
101
+ // If we don't have a session, always recreate
102
+ if (!hasSession) {
103
+ this.sessionRecreateHistory.set(jid, Date.now());
104
+ this.statistics.sessionRecreations++;
105
+ return {
106
+ reason: "we don't have a Signal session with them",
107
+ recreate: true
108
+ };
109
+ }
110
+ // IMMEDIATE recreation for MAC errors - session is definitely out of sync
111
+ if (errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)) {
112
+ this.sessionRecreateHistory.set(jid, Date.now());
113
+ this.statistics.sessionRecreations++;
114
+ this.logger.warn({ jid, errorCode: RetryReason[errorCode] }, 'MAC error detected, forcing immediate session recreation');
115
+ return {
116
+ reason: `MAC error (code ${errorCode}: ${RetryReason[errorCode]}), immediate session recreation`,
117
+ recreate: true
118
+ };
119
+ }
120
+ const now = Date.now();
121
+ const prevTime = this.sessionRecreateHistory.get(jid);
122
+ // If no previous recreation or it's been more than an hour
123
+ if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
124
+ this.sessionRecreateHistory.set(jid, now);
125
+ this.statistics.sessionRecreations++;
126
+ return {
127
+ reason: 'retry count > 1 and over an hour since last recreation',
128
+ recreate: true
129
+ };
130
+ }
131
+ return { reason: '', recreate: false };
132
+ }
133
+ /**
134
+ * Parse error code from retry receipt's retry node.
135
+ * Returns undefined if no error code is present.
136
+ */
137
+ parseRetryErrorCode(errorAttr) {
138
+ if (errorAttr === undefined || errorAttr === '') {
139
+ return undefined;
140
+ }
141
+ const code = parseInt(errorAttr, 10);
142
+ if (Number.isNaN(code)) {
143
+ return undefined;
144
+ }
145
+ // Validate it's a known RetryReason
146
+ if (code >= RetryReason.UnknownError && code <= RetryReason.StatusRevokeDelay) {
147
+ return code;
148
+ }
149
+ return RetryReason.UnknownError;
150
+ }
151
+ /**
152
+ * Check if an error code indicates a MAC failure
153
+ */
154
+ isMacError(errorCode) {
155
+ return errorCode !== undefined && MAC_ERROR_CODES.has(errorCode);
156
+ }
157
+ /**
158
+ * Increment retry counter for a message
159
+ */
160
+ incrementRetryCount(messageId) {
161
+ this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
162
+ this.statistics.totalRetries++;
163
+ return this.retryCounters.get(messageId);
164
+ }
165
+ /**
166
+ * Get retry count for a message
167
+ */
168
+ getRetryCount(messageId) {
169
+ return this.retryCounters.get(messageId) || 0;
170
+ }
171
+ /**
172
+ * Check if message has exceeded maximum retry attempts
173
+ */
174
+ hasExceededMaxRetries(messageId) {
175
+ return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
176
+ }
177
+ /**
178
+ * Mark retry as successful
179
+ */
180
+ markRetrySuccess(messageId) {
181
+ this.statistics.successfulRetries++;
182
+ // Clean up retry counter for successful message
183
+ this.retryCounters.delete(messageId);
184
+ this.cancelPendingPhoneRequest(messageId);
185
+ this.removeRecentMessage(messageId);
186
+ }
187
+ /**
188
+ * Mark retry as failed
189
+ */
190
+ markRetryFailed(messageId) {
191
+ this.statistics.failedRetries++;
192
+ this.retryCounters.delete(messageId);
193
+ this.cancelPendingPhoneRequest(messageId);
194
+ this.removeRecentMessage(messageId);
195
+ }
196
+ /**
197
+ * Schedule a phone request with delay
198
+ */
199
+ schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
200
+ // Cancel any existing request for this message
201
+ this.cancelPendingPhoneRequest(messageId);
202
+ this.pendingPhoneRequests[messageId] = setTimeout(() => {
203
+ delete this.pendingPhoneRequests[messageId];
204
+ this.statistics.phoneRequests++;
205
+ callback();
206
+ }, delay);
207
+ this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`);
208
+ }
209
+ /**
210
+ * Cancel pending phone request
211
+ */
212
+ cancelPendingPhoneRequest(messageId) {
213
+ const timeout = this.pendingPhoneRequests[messageId];
214
+ if (timeout) {
215
+ clearTimeout(timeout);
216
+ delete this.pendingPhoneRequests[messageId];
217
+ this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
218
+ }
219
+ }
220
+ clear() {
221
+ this.recentMessagesMap.clear();
222
+ this.messageKeyIndex.clear();
223
+ this.sessionRecreateHistory.clear();
224
+ this.retryCounters.clear();
225
+ this.baseKeys.clear();
226
+ for (const messageId of Object.keys(this.pendingPhoneRequests)) {
227
+ this.cancelPendingPhoneRequest(messageId);
228
+ }
229
+ this.statistics = {
230
+ totalRetries: 0,
231
+ successfulRetries: 0,
232
+ failedRetries: 0,
233
+ mediaRetries: 0,
234
+ sessionRecreations: 0,
235
+ phoneRequests: 0
236
+ };
237
+ }
238
+ saveBaseKey(addr, msgId, baseKey) {
239
+ this.baseKeys.set(`${addr}:${msgId}`, baseKey);
240
+ }
241
+ hasSameBaseKey(addr, msgId, baseKey) {
242
+ const stored = this.baseKeys.get(`${addr}:${msgId}`);
243
+ if (!stored || stored.length !== baseKey.length) {
244
+ return false;
245
+ }
246
+ for (let i = 0; i < stored.length; i++) {
247
+ if (stored[i] !== baseKey[i])
248
+ return false;
249
+ }
250
+ return true;
251
+ }
252
+ deleteBaseKey(addr, msgId) {
253
+ this.baseKeys.delete(`${addr}:${msgId}`);
254
+ }
255
+ keyToString(key) {
256
+ return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`;
257
+ }
258
+ removeRecentMessage(messageId) {
259
+ const keyStr = this.messageKeyIndex.get(messageId);
260
+ if (!keyStr) {
261
+ return;
262
+ }
263
+ this.recentMessagesMap.delete(keyStr);
264
+ this.messageKeyIndex.delete(messageId);
265
+ }
266
+ }
267
+ //# sourceMappingURL=message-retry-manager.js.map