@codexverified/baileys 2.7.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 (139) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +233 -0
  3. package/WAProto/V +1 -0
  4. package/WAProto/WAProto.proto +8051 -0
  5. package/WAProto/index.js +289292 -0
  6. package/engine-requirements.js +13 -0
  7. package/lib/Defaults/index.js +158 -0
  8. package/lib/Signal/Group/ciphertext-message.js +11 -0
  9. package/lib/Signal/Group/group-session-builder.js +29 -0
  10. package/lib/Signal/Group/group_cipher.js +81 -0
  11. package/lib/Signal/Group/index.js +11 -0
  12. package/lib/Signal/Group/keyhelper.js +17 -0
  13. package/lib/Signal/Group/sender-chain-key.js +25 -0
  14. package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
  15. package/lib/Signal/Group/sender-key-message.js +65 -0
  16. package/lib/Signal/Group/sender-key-name.js +47 -0
  17. package/lib/Signal/Group/sender-key-record.js +40 -0
  18. package/lib/Signal/Group/sender-key-state.js +83 -0
  19. package/lib/Signal/Group/sender-message-key.js +25 -0
  20. package/lib/Signal/libsignal.js +428 -0
  21. package/lib/Signal/lid-mapping.js +280 -0
  22. package/lib/Socket/Client/index.js +2 -0
  23. package/lib/Socket/Client/types.js +10 -0
  24. package/lib/Socket/Client/websocket.js +53 -0
  25. package/lib/Socket/aigroups.js +315 -0
  26. package/lib/Socket/business.js +407 -0
  27. package/lib/Socket/chats.js +1831 -0
  28. package/lib/Socket/communities.js +431 -0
  29. package/lib/Socket/graphql.js +899 -0
  30. package/lib/Socket/groups.js +549 -0
  31. package/lib/Socket/index.js +79 -0
  32. package/lib/Socket/interactive-handler.js +600 -0
  33. package/lib/Socket/interop.js +563 -0
  34. package/lib/Socket/managed-account.js +208 -0
  35. package/lib/Socket/messages-recv.js +2144 -0
  36. package/lib/Socket/messages-send.js +2030 -0
  37. package/lib/Socket/mex.js +41 -0
  38. package/lib/Socket/newsletter.js +226 -0
  39. package/lib/Socket/privacy.js +446 -0
  40. package/lib/Socket/registration.js +428 -0
  41. package/lib/Socket/socket.js +1017 -0
  42. package/lib/Socket/username.js +229 -0
  43. package/lib/Store/index.js +4 -0
  44. package/lib/Store/make-cache-manager-store.js +89 -0
  45. package/lib/Store/make-in-memory-store.js +420 -0
  46. package/lib/Store/make-ordered-dictionary.js +78 -0
  47. package/lib/Store/object-repository.js +23 -0
  48. package/lib/Types/Auth.js +1 -0
  49. package/lib/Types/Bussines.js +1 -0
  50. package/lib/Types/Call.js +1 -0
  51. package/lib/Types/Chat.js +7 -0
  52. package/lib/Types/Contact.js +1 -0
  53. package/lib/Types/Events.js +1 -0
  54. package/lib/Types/GroupMetadata.js +1 -0
  55. package/lib/Types/Label.js +24 -0
  56. package/lib/Types/LabelAssociation.js +6 -0
  57. package/lib/Types/Message.js +17 -0
  58. package/lib/Types/Mex.js +39 -0
  59. package/lib/Types/Product.js +1 -0
  60. package/lib/Types/RichType.js +22 -0
  61. package/lib/Types/Signal.js +1 -0
  62. package/lib/Types/Socket.js +2 -0
  63. package/lib/Types/State.js +48 -0
  64. package/lib/Types/USync.js +1 -0
  65. package/lib/Types/index.js +25 -0
  66. package/lib/Utils/auth-utils.js +301 -0
  67. package/lib/Utils/bot-planning-replay.js +169 -0
  68. package/lib/Utils/browser-utils.js +28 -0
  69. package/lib/Utils/business.js +230 -0
  70. package/lib/Utils/chat-utils.js +885 -0
  71. package/lib/Utils/companion-reg-client-utils.js +32 -0
  72. package/lib/Utils/consumer-application.js +102 -0
  73. package/lib/Utils/crypto.js +117 -0
  74. package/lib/Utils/decode-wa-message.js +484 -0
  75. package/lib/Utils/event-buffer.js +589 -0
  76. package/lib/Utils/generics.js +388 -0
  77. package/lib/Utils/group-history.js +56 -0
  78. package/lib/Utils/history.js +130 -0
  79. package/lib/Utils/identity-change-handler.js +48 -0
  80. package/lib/Utils/index.js +34 -0
  81. package/lib/Utils/jid-display-normalization.js +209 -0
  82. package/lib/Utils/link-preview.js +84 -0
  83. package/lib/Utils/logger.js +2 -0
  84. package/lib/Utils/lt-hash.js +7 -0
  85. package/lib/Utils/make-mutex.js +32 -0
  86. package/lib/Utils/message-composer.js +467 -0
  87. package/lib/Utils/message-inspect.js +67 -0
  88. package/lib/Utils/message-retry-manager.js +241 -0
  89. package/lib/Utils/messages-media.js +1006 -0
  90. package/lib/Utils/messages.js +1851 -0
  91. package/lib/Utils/meta-ai-msmsg.js +130 -0
  92. package/lib/Utils/meta-compositing.js +194 -0
  93. package/lib/Utils/noise-handler.js +200 -0
  94. package/lib/Utils/offline-node-processor.js +39 -0
  95. package/lib/Utils/pre-key-manager.js +105 -0
  96. package/lib/Utils/process-message.js +527 -0
  97. package/lib/Utils/reporting-utils.js +257 -0
  98. package/lib/Utils/rich-message-utils.js +439 -0
  99. package/lib/Utils/signal.js +200 -0
  100. package/lib/Utils/stanza-ack.js +37 -0
  101. package/lib/Utils/sync-action-utils.js +47 -0
  102. package/lib/Utils/tc-token-utils.js +162 -0
  103. package/lib/Utils/use-multi-file-auth-state.js +120 -0
  104. package/lib/Utils/use-single-file-auth-state.js +96 -0
  105. package/lib/Utils/use-sqlite-auth-state.js +108 -0
  106. package/lib/Utils/validate-connection.js +206 -0
  107. package/lib/Utils/voip-rekey.js +20 -0
  108. package/lib/Utils/welcome-flow.js +217 -0
  109. package/lib/WABinary/constants.js +1463 -0
  110. package/lib/WABinary/decode.js +261 -0
  111. package/lib/WABinary/encode.js +219 -0
  112. package/lib/WABinary/generic-utils.js +227 -0
  113. package/lib/WABinary/index.js +5 -0
  114. package/lib/WABinary/jid-utils.js +95 -0
  115. package/lib/WABinary/types.js +1 -0
  116. package/lib/WAM/BinaryInfo.js +9 -0
  117. package/lib/WAM/constants.js +22852 -0
  118. package/lib/WAM/encode.js +149 -0
  119. package/lib/WAM/index.js +3 -0
  120. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +96 -0
  121. package/lib/WAUSync/Protocols/USyncContactProtocol.js +56 -0
  122. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
  123. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
  124. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +70 -0
  125. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +28 -0
  126. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +25 -0
  127. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
  128. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +34 -0
  129. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
  130. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
  131. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  132. package/lib/WAUSync/Protocols/index.js +12 -0
  133. package/lib/WAUSync/USyncQuery.js +201 -0
  134. package/lib/WAUSync/USyncUser.js +54 -0
  135. package/lib/WAUSync/index.js +3 -0
  136. package/lib/antiban.cjs +4726 -0
  137. package/lib/index.d.ts +286 -0
  138. package/lib/index.js +18 -0
  139. package/package.json +121 -0
@@ -0,0 +1,229 @@
1
+ import { randomUUID } from 'crypto';
2
+ import { executeWMexQuery } from './mex.js';
3
+ import { USyncQuery, USyncUser } from '../WAUSync/index.js';
4
+ import { makeNewsletterSocket } from './newsletter.js';
5
+
6
+ /**
7
+ * w:mex query IDs for username operations.
8
+ *
9
+ * Source: whatsapp-android-mex_client_persist_ids.json (WA 2.26.26.4 APK assets)
10
+ * Cross-checked: live Frida capture on real device (wa-logger-2.26.26.4.js, 2026-06-30)
11
+ */
12
+ const USERNAME_QUERY_IDS = {
13
+ CHECK: '26124072630599518', // UsernameCheck
14
+ CHECK_MULTI: '27134626522840286', // UsernameCheckMulti
15
+ SET: '27108705368767936', // UsernameSet
16
+ GET: '32618050064506055', // UsernameGet
17
+ GET_RECOMMENDATIONS: '26077456248616957', // UsernameGetRecommendationsQuery
18
+ PIN_SET: '25529696019976770' // UsernamePinSet
19
+ };
20
+
21
+ /**
22
+ * Source enum values from EnumC141106Vn (UsernameCheck result)
23
+ * and EnumC141056Vi (rejection reasons) confirmed via C164057Wg.java
24
+ */
25
+ const USERNAME_CHECK_RESULT = {
26
+ SUCCESS: 'SUCCESS',
27
+ INVALID: 'INVALID'
28
+ };
29
+
30
+ const USERNAME_SOURCE = {
31
+ FB: 'FB',
32
+ IG: 'IG',
33
+ USER_INPUT: 'USER_INPUT',
34
+ SUGGESTION: 'SUGGESTION'
35
+ };
36
+
37
+ export const makeUsernameSocket = (config) => {
38
+ const sock = makeNewsletterSocket(config);
39
+ const { query, generateMessageTag, executeUSyncQuery } = sock;
40
+
41
+ const mexQuery = (variables, queryId, dataPath) =>
42
+ executeWMexQuery(variables, queryId, dataPath, query, generateMessageTag);
43
+
44
+ /**
45
+ * Check whether a username is available.
46
+ *
47
+ * @param {string} username - The @username to check (without @)
48
+ * @param {boolean} includeSuggestions - Request alternative suggestions when taken
49
+ * @returns {object}
50
+ * On success: { available: true, username }
51
+ * On taken: { available: false, username, suggestions: string[], rejectionReasons: string[], suggestionsEligible: boolean }
52
+ * On rate-limit: throws Boom with statusCode 429
53
+ *
54
+ * Confirmed fields from C164057Wg.java:
55
+ * data.xwa2_username_check.result 'SUCCESS' | 'INVALID'
56
+ * data.xwa2_username_check.suggestions
57
+ * data.xwa2_username_check.rejection_reasons
58
+ * data.xwa2_username_check.suggestions_eligible
59
+ */
60
+ const checkUsername = async (username, includeSuggestions = true, sessionId) => {
61
+ // session_id ties this check to the subsequent set step caller can pass one in
62
+ // so both use the same UUID (confirmed from live WA 2.26.26.4 capture).
63
+ const session_id = sessionId || randomUUID();
64
+ const data = await mexQuery(
65
+ {
66
+ username,
67
+ include_suggestions: includeSuggestions,
68
+ session_id,
69
+ source: USERNAME_SOURCE.USER_INPUT
70
+ },
71
+ USERNAME_QUERY_IDS.CHECK,
72
+ 'xwa2_username_check'
73
+ );
74
+ if (data?.result === USERNAME_CHECK_RESULT.SUCCESS) {
75
+ return { available: true, username, session_id };
76
+ }
77
+ return {
78
+ available: false,
79
+ username,
80
+ session_id,
81
+ suggestions: data?.suggestions ?? [],
82
+ rejectionReasons: data?.rejection_reasons ?? [],
83
+ suggestionsEligible: data?.suggestions_eligible ?? true
84
+ };
85
+ };
86
+
87
+ /**
88
+ * Set your own username.
89
+ *
90
+ * @param {string} username - The username to set (without @)
91
+ * @param {object} options
92
+ * @param {string} [options.source] - 'USER_INPUT' | 'FB' | 'IG' | 'SUGGESTION'
93
+ * @param {string} [options.sessionId] - Optional session tracking ID
94
+ * @param {string} [options.pin] - Optional PIN for protected usernames
95
+ *
96
+ * Confirmed variables from C1568872p.java A00():
97
+ * username, reserved (bool), session_id, source, pin
98
+ */
99
+ const setUsername = async (username, options = {}) => {
100
+ const { source = USERNAME_SOURCE.USER_INPUT, sessionId, pin } = options;
101
+ // session_id links the check and set steps generate one if caller doesn't supply it.
102
+ // reserved:true confirmed from live WA 2.26.26.4 capture (was wrongly false before).
103
+ const session_id = sessionId || randomUUID();
104
+ const variables = {
105
+ username,
106
+ reserved: true,
107
+ source,
108
+ session_id,
109
+ ...(pin ? { pin } : {})
110
+ };
111
+ return mexQuery(variables, USERNAME_QUERY_IDS.SET, 'xwa2_username_set');
112
+ };
113
+
114
+ /**
115
+ * Delete (unset) your own username.
116
+ *
117
+ * Confirmed from C1568872p.java:
118
+ * str4 = str == null ? "delete" : "set"
119
+ * sending username=null triggers the delete path on the server.
120
+ */
121
+ const deleteUsername = async () => {
122
+ return mexQuery({ username: null }, USERNAME_QUERY_IDS.SET, 'xwa2_username_delete');
123
+ };
124
+
125
+ /**
126
+ * Get your own current username.
127
+ *
128
+ * Confirmed from C1568872p.java A02():
129
+ * AbstractC41851rT.A0L(AbstractC130045pa.A0T(), C1363664w.class, "UsernameGet", false)
130
+ */
131
+ const getMyUsername = async () => {
132
+ const data = await mexQuery({}, USERNAME_QUERY_IDS.GET, 'xwa2_username_get');
133
+ return data?.username ?? null;
134
+ };
135
+
136
+ /**
137
+ * Set or delete the PIN that protects your username.
138
+ *
139
+ * @param {string|null} pin - New PIN, or null to delete the PIN
140
+ *
141
+ * Confirmed from MexUsernamePinProtocolApi.java:
142
+ * operation "UsernamePinSet", variable "pin"
143
+ * pin=null triggers the "delete" path on the server.
144
+ */
145
+ const setUsernamePin = async (pin) => {
146
+ // pin=null delete PIN (server interprets empty/null variables as delete)
147
+ const variables = pin != null ? { pin } : {};
148
+ return mexQuery(variables, USERNAME_QUERY_IDS.PIN_SET, 'xwa2_username_pin_set');
149
+ };
150
+
151
+ /**
152
+ * Look up a contact by their @username via USync.
153
+ *
154
+ * Confirmed via USyncContactProtocol.getUserElement():
155
+ * { tag: 'contact', attrs: { username, pin? } }
156
+ *
157
+ * @param {string} username - The username to look up (without @)
158
+ * @param {string} [pin] - Optional PIN if the username is PIN-protected
159
+ * @returns {{ jid, lid?, contact: boolean }|null}
160
+ */
161
+ const findUserByUsername = async (username, pin) => {
162
+ const usyncQuery = new USyncQuery().withContactProtocol();
163
+ const user = new USyncUser().withUsername(username);
164
+ if (pin) user.withUsernameKey(pin);
165
+ usyncQuery.withUser(user);
166
+ const result = await executeUSyncQuery(usyncQuery);
167
+ if (!result?.list?.length) return null;
168
+ const entry = result.list[0];
169
+ return {
170
+ jid: entry.id,
171
+ contact: entry.contact ?? false
172
+ };
173
+ };
174
+
175
+ /**
176
+ * Fetch the username of one or more contacts by their JID.
177
+ * Uses USync with the username protocol.
178
+ *
179
+ * @param {...string} jids - One or more JIDs
180
+ * @returns {Array<{ id, username: string|null }>}
181
+ */
182
+ const fetchContactUsernames = async (...jids) => {
183
+ const usyncQuery = new USyncQuery().withUsernameProtocol();
184
+ for (const jid of jids) {
185
+ usyncQuery.withUser(new USyncUser().withId(jid));
186
+ }
187
+ const result = await executeUSyncQuery(usyncQuery);
188
+ return result?.list ?? [];
189
+ };
190
+
191
+ /**
192
+ * Check multiple usernames for availability at once.
193
+ * @param {string[]} usernames - Array of usernames (without @)
194
+ */
195
+ const checkUsernameMulti = async (usernames) => {
196
+ const data = await mexQuery(
197
+ { usernames },
198
+ USERNAME_QUERY_IDS.CHECK_MULTI,
199
+ 'xwa2_username_check_multi'
200
+ );
201
+ return data;
202
+ };
203
+
204
+ /**
205
+ * Fetch username recommendations for the current user.
206
+ * @param {string} [source] - Source hint: 'FB' | 'IG' | 'USER_INPUT'
207
+ */
208
+ const getUsernameRecommendations = async (source = null) => {
209
+ const variables = {};
210
+ if (source) variables.source = source;
211
+ return mexQuery(variables, USERNAME_QUERY_IDS.GET_RECOMMENDATIONS, 'xwa2_username_get_recommendations');
212
+ };
213
+
214
+ return {
215
+ ...sock,
216
+ checkUsername,
217
+ checkUsernameMulti,
218
+ setUsername,
219
+ deleteUsername,
220
+ getMyUsername,
221
+ getUsernameRecommendations,
222
+ setUsernamePin,
223
+ findUserByUsername,
224
+ fetchContactUsernames,
225
+ USERNAME_QUERY_IDS,
226
+ USERNAME_CHECK_RESULT,
227
+ USERNAME_SOURCE
228
+ };
229
+ };
@@ -0,0 +1,4 @@
1
+ export * from './make-cache-manager-store.js';
2
+ export * from './make-in-memory-store.js';
3
+ export * from './make-ordered-dictionary.js';
4
+ export * from './object-repository.js';
@@ -0,0 +1,89 @@
1
+ import { createCache } from 'cache-manager';
2
+ import { proto } from '../../WAProto/index.js';
3
+ import { BufferJSON, initAuthCreds } from '../Utils/index.js';
4
+ import logger from '../Utils/logger.js';
5
+
6
+ /**
7
+ * Cache-manager backed auth state.
8
+ *
9
+ * `store` accepts anything cache-manager v7's createCache({ stores: [...] }) accepts:
10
+ * a Keyv-compatible store instance (e.g. keyv/redis, keyv/sqlite) or a store factory.
11
+ * Keys are namespaced under `${sessionKey}:` so multiple sessions can share one store.
12
+ */
13
+ export const makeCacheManagerAuthState = async (store, sessionKey) => {
14
+ const defaultKey = file => `${sessionKey}:${file}`;
15
+ const databaseConn = createCache({ stores: [store], ttl: 0 });
16
+ const writeData = async (file, data) => {
17
+ let ttl = undefined;
18
+ if (file === 'creds') {
19
+ ttl = 63115200; // 2 years
20
+ }
21
+ await databaseConn.set(defaultKey(file), JSON.stringify(data, BufferJSON.replacer), ttl);
22
+ };
23
+ const readData = async (file) => {
24
+ try {
25
+ const data = await databaseConn.get(defaultKey(file));
26
+ if (data) {
27
+ return JSON.parse(data, BufferJSON.reviver);
28
+ }
29
+ return null;
30
+ }
31
+ catch (error) {
32
+ logger.error(error);
33
+ return null;
34
+ }
35
+ };
36
+ const removeData = async (file) => {
37
+ try {
38
+ return await databaseConn.del(defaultKey(file));
39
+ }
40
+ catch (err) {
41
+ logger.error({ err }, `Error removing ${file} from session ${sessionKey}`);
42
+ }
43
+ };
44
+ const clearState = async () => {
45
+ try {
46
+ // Keyv-compatible stores expose keys(pattern); tolerate stores that don't.
47
+ const keyed = typeof store?.keys === 'function' ? store : databaseConn;
48
+ const keys = typeof keyed.keys === 'function' ? await keyed.keys(`${sessionKey}*`) : [];
49
+ await Promise.all(keys.map(async key => await databaseConn.del(key)));
50
+ }
51
+ catch (err) {
52
+ logger.warn({ err }, 'clearState failed');
53
+ }
54
+ };
55
+ const creds = (await readData('creds')) || initAuthCreds();
56
+ return {
57
+ clearState,
58
+ saveCreds: () => writeData('creds', creds),
59
+ state: {
60
+ creds,
61
+ keys: {
62
+ get: async (type, ids) => {
63
+ const data = {};
64
+ await Promise.all(
65
+ ids.map(async id => {
66
+ let value = await readData(`${type}-${id}`);
67
+ if (type === 'app-state-sync-key' && value) {
68
+ value = proto.Message.AppStateSyncKeyData.fromObject(value);
69
+ }
70
+ data[id] = value;
71
+ })
72
+ );
73
+ return data;
74
+ },
75
+ set: async (data) => {
76
+ const tasks = [];
77
+ for (const category in data) {
78
+ for (const id in data[category]) {
79
+ const value = data[category][id];
80
+ const key = `${category}-${id}`;
81
+ tasks.push(value ? writeData(key, value) : removeData(key));
82
+ }
83
+ }
84
+ await Promise.all(tasks);
85
+ }
86
+ }
87
+ }
88
+ };
89
+ };