@gara31/void-baileys 7.0.0-rc.14

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 (95) hide show
  1. package/LICENSE +21 -0
  2. package/WAProto/index.js +117292 -0
  3. package/lib/Defaults/baileys-version.json +3 -0
  4. package/lib/Defaults/index.js +116 -0
  5. package/lib/Signal/Group/ciphertext-message.js +12 -0
  6. package/lib/Signal/Group/group-session-builder.js +42 -0
  7. package/lib/Signal/Group/group_cipher.js +109 -0
  8. package/lib/Signal/Group/index.js +12 -0
  9. package/lib/Signal/Group/keyhelper.js +18 -0
  10. package/lib/Signal/Group/sender-chain-key.js +32 -0
  11. package/lib/Signal/Group/sender-key-distribution-message.js +67 -0
  12. package/lib/Signal/Group/sender-key-message.js +80 -0
  13. package/lib/Signal/Group/sender-key-name.js +50 -0
  14. package/lib/Signal/Group/sender-key-record.js +47 -0
  15. package/lib/Signal/Group/sender-key-state.js +105 -0
  16. package/lib/Signal/Group/sender-message-key.js +30 -0
  17. package/lib/Signal/libsignal.js +416 -0
  18. package/lib/Signal/lid-mapping.js +189 -0
  19. package/lib/Socket/Client/index.js +3 -0
  20. package/lib/Socket/Client/types.js +11 -0
  21. package/lib/Socket/Client/websocket.js +61 -0
  22. package/lib/Socket/business.js +404 -0
  23. package/lib/Socket/chats.js +1146 -0
  24. package/lib/Socket/communities.js +505 -0
  25. package/lib/Socket/groups.js +404 -0
  26. package/lib/Socket/index.js +18 -0
  27. package/lib/Socket/messages-recv.js +1600 -0
  28. package/lib/Socket/messages-send.js +1203 -0
  29. package/lib/Socket/mex.js +56 -0
  30. package/lib/Socket/newsletter.js +240 -0
  31. package/lib/Socket/socket.js +1060 -0
  32. package/lib/Types/Auth.js +2 -0
  33. package/lib/Types/Bussines.js +2 -0
  34. package/lib/Types/Call.js +2 -0
  35. package/lib/Types/Chat.js +8 -0
  36. package/lib/Types/Contact.js +2 -0
  37. package/lib/Types/Events.js +2 -0
  38. package/lib/Types/GroupMetadata.js +2 -0
  39. package/lib/Types/Label.js +25 -0
  40. package/lib/Types/LabelAssociation.js +7 -0
  41. package/lib/Types/Message.js +11 -0
  42. package/lib/Types/Newsletter.js +31 -0
  43. package/lib/Types/Product.js +2 -0
  44. package/lib/Types/Signal.js +2 -0
  45. package/lib/Types/Socket.js +3 -0
  46. package/lib/Types/State.js +13 -0
  47. package/lib/Types/USync.js +2 -0
  48. package/lib/Types/index.js +32 -0
  49. package/lib/Utils/auth-utils.js +276 -0
  50. package/lib/Utils/browser-utils.js +32 -0
  51. package/lib/Utils/business.js +262 -0
  52. package/lib/Utils/chat-utils.js +941 -0
  53. package/lib/Utils/crypto.js +179 -0
  54. package/lib/Utils/decode-wa-message.js +333 -0
  55. package/lib/Utils/event-buffer.js +580 -0
  56. package/lib/Utils/generics.js +436 -0
  57. package/lib/Utils/history.js +103 -0
  58. package/lib/Utils/index.js +19 -0
  59. package/lib/Utils/link-preview.js +99 -0
  60. package/lib/Utils/logger.js +3 -0
  61. package/lib/Utils/lt-hash.js +56 -0
  62. package/lib/Utils/make-mutex.js +38 -0
  63. package/lib/Utils/message-retry-manager.js +181 -0
  64. package/lib/Utils/messages-media.js +727 -0
  65. package/lib/Utils/messages.js +1309 -0
  66. package/lib/Utils/noise-handler.js +162 -0
  67. package/lib/Utils/pre-key-manager.js +125 -0
  68. package/lib/Utils/process-message.js +594 -0
  69. package/lib/Utils/signal.js +194 -0
  70. package/lib/Utils/use-multi-file-auth-state.js +118 -0
  71. package/lib/Utils/validate-connection.js +240 -0
  72. package/lib/WABinary/constants.js +1301 -0
  73. package/lib/WABinary/decode.js +240 -0
  74. package/lib/WABinary/encode.js +216 -0
  75. package/lib/WABinary/generic-utils.js +104 -0
  76. package/lib/WABinary/index.js +6 -0
  77. package/lib/WABinary/jid-utils.js +95 -0
  78. package/lib/WABinary/types.js +2 -0
  79. package/lib/WAM/BinaryInfo.js +10 -0
  80. package/lib/WAM/constants.js +22863 -0
  81. package/lib/WAM/encode.js +152 -0
  82. package/lib/WAM/index.js +4 -0
  83. package/lib/WAUSync/Protocols/USyncContactProtocol.js +29 -0
  84. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +59 -0
  85. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  86. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +36 -0
  87. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +60 -0
  88. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  89. package/lib/WAUSync/Protocols/index.js +5 -0
  90. package/lib/WAUSync/USyncQuery.js +104 -0
  91. package/lib/WAUSync/USyncUser.js +23 -0
  92. package/lib/WAUSync/index.js +4 -0
  93. package/lib/index.js +11 -0
  94. package/package.json +32 -0
  95. package/readme.md +1452 -0
@@ -0,0 +1,56 @@
1
+ import { hkdf } from "./crypto.js";
2
+ /**
3
+ * LT Hash is a summation based hash algorithm that maintains the integrity of a piece of data
4
+ * over a series of mutations. You can add/remove mutations and it'll return a hash equal to
5
+ * if the same series of mutations was made sequentially.
6
+ */
7
+ const o = 128;
8
+ class LTHash {
9
+ constructor(e) {
10
+ this.salt = e;
11
+ }
12
+ async add(e, t) {
13
+ for (const item of t) {
14
+ e = await this._addSingle(e, item);
15
+ }
16
+ return e;
17
+ }
18
+ async subtract(e, t) {
19
+ for (const item of t) {
20
+ e = await this._subtractSingle(e, item);
21
+ }
22
+ return e;
23
+ }
24
+ async subtractThenAdd(e, addList, subtractList) {
25
+ const subtracted = await this.subtract(e, subtractList);
26
+ return this.add(subtracted, addList);
27
+ }
28
+ async _addSingle(e, t) {
29
+ const derived = new Uint8Array(
30
+ await hkdf(Buffer.from(t), o, { info: this.salt }),
31
+ ).buffer;
32
+ return this.performPointwiseWithOverflow(e, derived, (a, b) => a + b);
33
+ }
34
+ async _subtractSingle(e, t) {
35
+ const derived = new Uint8Array(
36
+ await hkdf(Buffer.from(t), o, { info: this.salt }),
37
+ ).buffer;
38
+ return this.performPointwiseWithOverflow(e, derived, (a, b) => a - b);
39
+ }
40
+ performPointwiseWithOverflow(e, t, op) {
41
+ const n = new DataView(e);
42
+ const i = new DataView(t);
43
+ const out = new ArrayBuffer(n.byteLength);
44
+ const s = new DataView(out);
45
+ for (let offset = 0; offset < n.byteLength; offset += 2) {
46
+ s.setUint16(
47
+ offset,
48
+ op(n.getUint16(offset, true), i.getUint16(offset, true)),
49
+ true,
50
+ );
51
+ }
52
+ return out;
53
+ }
54
+ }
55
+ export const LT_HASH_ANTI_TAMPERING = new LTHash("WhatsApp Patch Integrity");
56
+ //# sourceMappingURL=lt-hash.js.map
@@ -0,0 +1,38 @@
1
+ export const makeMutex = () => {
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ let task = Promise.resolve();
4
+ let taskTimeout;
5
+ return {
6
+ mutex(code) {
7
+ task = (async () => {
8
+ // wait for the previous task to complete
9
+ // if there is an error, we swallow so as to not block the queue
10
+ try {
11
+ await task;
12
+ } catch {}
13
+ try {
14
+ // execute the current task
15
+ const result = await code();
16
+ return result;
17
+ } finally {
18
+ clearTimeout(taskTimeout);
19
+ }
20
+ })();
21
+ // we replace the existing task, appending the new piece of execution to it
22
+ // so the next task will have to wait for this one to finish
23
+ return task;
24
+ },
25
+ };
26
+ };
27
+ export const makeKeyedMutex = () => {
28
+ const map = {};
29
+ return {
30
+ mutex(key, task) {
31
+ if (!map[key]) {
32
+ map[key] = makeMutex();
33
+ }
34
+ return map[key].mutex(task);
35
+ },
36
+ };
37
+ };
38
+ //# sourceMappingURL=make-mutex.js.map
@@ -0,0 +1,181 @@
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
+ export class MessageRetryManager {
9
+ constructor(logger, maxMsgRetryCount) {
10
+ this.logger = logger;
11
+ this.recentMessagesMap = new LRUCache({
12
+ max: RECENT_MESSAGES_SIZE,
13
+ ttl: 5 * 60 * 1000,
14
+ ttlAutopurge: true,
15
+ dispose: (_value, key) => {
16
+ const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR);
17
+ if (separatorIndex > -1) {
18
+ const messageId = key.slice(
19
+ separatorIndex + MESSAGE_KEY_SEPARATOR.length,
20
+ );
21
+ this.messageKeyIndex.delete(messageId);
22
+ }
23
+ },
24
+ });
25
+ this.messageKeyIndex = new Map();
26
+ this.sessionRecreateHistory = new LRUCache({
27
+ ttl: RECREATE_SESSION_TIMEOUT * 2,
28
+ ttlAutopurge: true,
29
+ });
30
+ this.retryCounters = new LRUCache({
31
+ ttl: 15 * 60 * 1000,
32
+ ttlAutopurge: true,
33
+ updateAgeOnGet: true,
34
+ }); // 15 minutes TTL
35
+ this.pendingPhoneRequests = {};
36
+ this.maxMsgRetryCount = 5;
37
+ this.statistics = {
38
+ totalRetries: 0,
39
+ successfulRetries: 0,
40
+ failedRetries: 0,
41
+ mediaRetries: 0,
42
+ sessionRecreations: 0,
43
+ phoneRequests: 0,
44
+ };
45
+ this.maxMsgRetryCount = maxMsgRetryCount;
46
+ }
47
+ /**
48
+ * Add a recent message to the cache for retry handling
49
+ */
50
+ addRecentMessage(to, id, message) {
51
+ const key = { to, id };
52
+ const keyStr = this.keyToString(key);
53
+ // Add new message
54
+ this.recentMessagesMap.set(keyStr, {
55
+ message,
56
+ timestamp: Date.now(),
57
+ });
58
+ this.messageKeyIndex.set(id, keyStr);
59
+ this.logger.debug(`Added message to retry cache: ${to}/${id}`);
60
+ }
61
+ /**
62
+ * Get a recent message from the cache
63
+ */
64
+ getRecentMessage(to, id) {
65
+ const key = { to, id };
66
+ const keyStr = this.keyToString(key);
67
+ return this.recentMessagesMap.get(keyStr);
68
+ }
69
+ /**
70
+ * Check if a session should be recreated based on retry count and history
71
+ */
72
+ shouldRecreateSession(jid, retryCount, hasSession) {
73
+ // If we don't have a session, always recreate
74
+ if (!hasSession) {
75
+ this.sessionRecreateHistory.set(jid, Date.now());
76
+ this.statistics.sessionRecreations++;
77
+ return {
78
+ reason: "we don't have a Signal session with them",
79
+ recreate: true,
80
+ };
81
+ }
82
+ // Only consider recreation if retry count > 1
83
+ if (retryCount < 2) {
84
+ return { reason: "", recreate: false };
85
+ }
86
+ const now = Date.now();
87
+ const prevTime = this.sessionRecreateHistory.get(jid);
88
+ // If no previous recreation or it's been more than an hour
89
+ if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
90
+ this.sessionRecreateHistory.set(jid, now);
91
+ this.statistics.sessionRecreations++;
92
+ return {
93
+ reason: "retry count > 1 and over an hour since last recreation",
94
+ recreate: true,
95
+ };
96
+ }
97
+ return { reason: "", recreate: false };
98
+ }
99
+ /**
100
+ * Increment retry counter for a message
101
+ */
102
+ incrementRetryCount(messageId) {
103
+ this.retryCounters.set(
104
+ messageId,
105
+ (this.retryCounters.get(messageId) || 0) + 1,
106
+ );
107
+ this.statistics.totalRetries++;
108
+ return this.retryCounters.get(messageId);
109
+ }
110
+ /**
111
+ * Get retry count for a message
112
+ */
113
+ getRetryCount(messageId) {
114
+ return this.retryCounters.get(messageId) || 0;
115
+ }
116
+ /**
117
+ * Check if message has exceeded maximum retry attempts
118
+ */
119
+ hasExceededMaxRetries(messageId) {
120
+ return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
121
+ }
122
+ /**
123
+ * Mark retry as successful
124
+ */
125
+ markRetrySuccess(messageId) {
126
+ this.statistics.successfulRetries++;
127
+ // Clean up retry counter for successful message
128
+ this.retryCounters.delete(messageId);
129
+ this.cancelPendingPhoneRequest(messageId);
130
+ this.removeRecentMessage(messageId);
131
+ }
132
+ /**
133
+ * Mark retry as failed
134
+ */
135
+ markRetryFailed(messageId) {
136
+ this.statistics.failedRetries++;
137
+ this.retryCounters.delete(messageId);
138
+ this.cancelPendingPhoneRequest(messageId);
139
+ this.removeRecentMessage(messageId);
140
+ }
141
+ /**
142
+ * Schedule a phone request with delay
143
+ */
144
+ schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
145
+ // Cancel any existing request for this message
146
+ this.cancelPendingPhoneRequest(messageId);
147
+ this.pendingPhoneRequests[messageId] = setTimeout(() => {
148
+ delete this.pendingPhoneRequests[messageId];
149
+ this.statistics.phoneRequests++;
150
+ callback();
151
+ }, delay);
152
+ this.logger.debug(
153
+ `Scheduled phone request for message ${messageId} with ${delay}ms delay`,
154
+ );
155
+ }
156
+ /**
157
+ * Cancel pending phone request
158
+ */
159
+ cancelPendingPhoneRequest(messageId) {
160
+ const timeout = this.pendingPhoneRequests[messageId];
161
+ if (timeout) {
162
+ clearTimeout(timeout);
163
+ delete this.pendingPhoneRequests[messageId];
164
+ this.logger.debug(
165
+ `Cancelled pending phone request for message ${messageId}`,
166
+ );
167
+ }
168
+ }
169
+ keyToString(key) {
170
+ return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`;
171
+ }
172
+ removeRecentMessage(messageId) {
173
+ const keyStr = this.messageKeyIndex.get(messageId);
174
+ if (!keyStr) {
175
+ return;
176
+ }
177
+ this.recentMessagesMap.delete(keyStr);
178
+ this.messageKeyIndex.delete(messageId);
179
+ }
180
+ }
181
+ //# sourceMappingURL=message-retry-manager.js.map