sauruspro-bail 0.0.0-stage → 5.0.1
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.
- package/README.md +128 -2
- package/WAProto/GenerateStatics.sh +3 -0
- package/WAProto/WAProto.proto +6754 -0
- package/WAProto/fix-imports.js +85 -0
- package/WAProto/index.d.ts +79257 -0
- package/WAProto/index.js +242946 -0
- package/docs/CHANGELOG.md +43 -0
- package/docs/NEWSLETTER-STATUS.md +120 -0
- package/docs/SPR.md +51 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/index.js +135 -0
- package/lib/Signal/Group/ciphertext-message.js +12 -0
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/lib/Signal/Group/group_cipher.js +82 -0
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/keyhelper.js +18 -0
- package/lib/Signal/Group/sender-chain-key.js +26 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
- package/lib/Signal/Group/sender-key-message.js +66 -0
- package/lib/Signal/Group/sender-key-name.js +48 -0
- package/lib/Signal/Group/sender-key-record.js +41 -0
- package/lib/Signal/Group/sender-key-state.js +84 -0
- package/lib/Signal/Group/sender-message-key.js +26 -0
- package/lib/Signal/libsignal.js +431 -0
- package/lib/Signal/lid-mapping.js +277 -0
- package/lib/Socket/Client/index.js +3 -0
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/websocket.js +54 -0
- package/lib/Socket/MessageBuilder.js +3729 -0
- package/lib/Socket/business.js +379 -0
- package/lib/Socket/chats.js +1198 -0
- package/lib/Socket/communities.js +431 -0
- package/lib/Socket/dinonic-handler.d.ts +27 -0
- package/lib/Socket/dinonic-handler.js +882 -0
- package/lib/Socket/graphql.js +716 -0
- package/lib/Socket/groups.js +374 -0
- package/lib/Socket/index.js +96 -0
- package/lib/Socket/interop.js +463 -0
- package/lib/Socket/luxu.js +387 -0
- package/lib/Socket/message-builder.js +556 -0
- package/lib/Socket/messages-recv.js +1942 -0
- package/lib/Socket/messages-send.js +1541 -0
- package/lib/Socket/mex.js +54 -0
- package/lib/Socket/newsletter.js +641 -0
- package/lib/Socket/privacy.js +318 -0
- package/lib/Socket/socket.js +1115 -0
- package/lib/Socket/username.js +236 -0
- package/lib/Store/index.js +10 -0
- package/lib/Store/keyed-db.js +108 -0
- package/lib/Store/make-cache-manager-store.js +85 -0
- package/lib/Store/make-in-memory-store.js +244 -0
- package/lib/Store/make-ordered-dictionary.js +75 -0
- package/lib/Store/object-repository.js +32 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +8 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.js +11 -0
- package/lib/Types/Mex.js +111 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +3 -0
- package/lib/Types/State.js +56 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +26 -0
- package/lib/Utils/MessageBuilder.d.ts +215 -0
- package/lib/Utils/MessageBuilder.js +3991 -0
- package/lib/Utils/a2ui.d.ts +202 -0
- package/lib/Utils/a2ui.js +732 -0
- package/lib/Utils/airich-registry.js +3 -0
- package/lib/Utils/auth-utils.js +302 -0
- package/lib/Utils/baileys-event-stream.js +58 -0
- package/lib/Utils/bot-signature.d.ts +22 -0
- package/lib/Utils/bot-signature.js +124 -0
- package/lib/Utils/browser-utils.js +48 -0
- package/lib/Utils/business.js +231 -0
- package/lib/Utils/chat-utils.js +872 -0
- package/lib/Utils/companion-reg-client-utils.js +35 -0
- package/lib/Utils/crypto.js +118 -0
- package/lib/Utils/decode-wa-message.js +350 -0
- package/lib/Utils/dinonic-spr.d.ts +26 -0
- package/lib/Utils/dinonic-spr.js +521 -0
- package/lib/Utils/event-buffer.js +622 -0
- package/lib/Utils/extras.d.ts +449 -0
- package/lib/Utils/extras.js +1377 -0
- package/lib/Utils/generics.js +403 -0
- package/lib/Utils/history.js +134 -0
- package/lib/Utils/html-app.d.ts +39 -0
- package/lib/Utils/html-app.js +291 -0
- package/lib/Utils/identity-change-handler.js +50 -0
- package/lib/Utils/index.js +38 -0
- package/lib/Utils/link-preview.js +85 -0
- package/lib/Utils/logger.js +3 -0
- package/lib/Utils/lt-hash.js +8 -0
- package/lib/Utils/make-mutex.js +33 -0
- package/lib/Utils/message-composer.js +273 -0
- package/lib/Utils/message-retry-manager.js +265 -0
- package/lib/Utils/messages-media.js +870 -0
- package/lib/Utils/messages.js +1394 -0
- package/lib/Utils/metaai.d.ts +438 -0
- package/lib/Utils/metaai.js +911 -0
- package/lib/Utils/modern-messages.d.ts +150 -0
- package/lib/Utils/modern-messages.js +496 -0
- package/lib/Utils/native-flow.d.ts +14 -0
- package/lib/Utils/native-flow.js +118 -0
- package/lib/Utils/newsletter-status.d.ts +163 -0
- package/lib/Utils/newsletter-status.js +694 -0
- package/lib/Utils/noise-handler.js +201 -0
- package/lib/Utils/offline-node-processor.js +40 -0
- package/lib/Utils/payment-messages.d.ts +59 -0
- package/lib/Utils/payment-messages.js +156 -0
- package/lib/Utils/pre-key-manager.js +106 -0
- package/lib/Utils/process-message.js +630 -0
- package/lib/Utils/reporting-utils.js +258 -0
- package/lib/Utils/scheduled-message.d.ts +82 -0
- package/lib/Utils/scheduled-message.js +113 -0
- package/lib/Utils/signal.js +201 -0
- package/lib/Utils/stanza-ack.js +38 -0
- package/lib/Utils/status-stickers.d.ts +78 -0
- package/lib/Utils/status-stickers.js +261 -0
- package/lib/Utils/sticker.js +133 -0
- package/lib/Utils/sync-action-utils.js +49 -0
- package/lib/Utils/tc-token-utils.js +163 -0
- package/lib/Utils/use-multi-file-auth-state.js +121 -0
- package/lib/Utils/use-sqlite-auth-state.js +162 -0
- package/lib/Utils/validate-connection.js +203 -0
- package/lib/WABinary/constants.js +1301 -0
- package/lib/WABinary/decode.js +262 -0
- package/lib/WABinary/encode.js +220 -0
- package/lib/WABinary/generic-utils.js +204 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/jid-utils.js +98 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +10 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.js +150 -0
- package/lib/WAM/index.js +4 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +43 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +54 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +58 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +39 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +27 -0
- package/lib/WAUSync/Protocols/index.js +12 -0
- package/lib/WAUSync/USyncQuery.js +151 -0
- package/lib/WAUSync/USyncUser.js +56 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +67 -0
- package/package.json +147 -5
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import NodeCache from '@cacheable/node-cache';
|
|
2
|
+
import { Boom } from '@hapi/boom';
|
|
3
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
4
|
+
import { Mutex } from 'async-mutex';
|
|
5
|
+
import { randomBytes } from 'crypto';
|
|
6
|
+
import PQueue from 'p-queue';
|
|
7
|
+
import { DEFAULT_CACHE_TTLS } from '../Defaults/index.js';
|
|
8
|
+
import { Curve, signedKeyPair } from './crypto.js';
|
|
9
|
+
import { delay, generateRegistrationId } from './generics.js';
|
|
10
|
+
import { PreKeyManager } from './pre-key-manager.js';
|
|
11
|
+
/**
|
|
12
|
+
* Adds caching capability to a SignalKeyStore
|
|
13
|
+
* @param store the store to add caching to
|
|
14
|
+
* @param logger to log trace events
|
|
15
|
+
* @param _cache cache store to use
|
|
16
|
+
*/
|
|
17
|
+
export function makeCacheableSignalKeyStore(store, logger, _cache) {
|
|
18
|
+
const cache = _cache ||
|
|
19
|
+
new NodeCache({
|
|
20
|
+
stdTTL: DEFAULT_CACHE_TTLS.SIGNAL_STORE, // 5 minutes
|
|
21
|
+
useClones: false,
|
|
22
|
+
deleteOnExpire: true
|
|
23
|
+
});
|
|
24
|
+
// Mutex for protecting cache operations
|
|
25
|
+
const cacheMutex = new Mutex();
|
|
26
|
+
function getUniqueId(type, id) {
|
|
27
|
+
return `${type}.${id}`;
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
async get(type, ids) {
|
|
31
|
+
return cacheMutex.runExclusive(async () => {
|
|
32
|
+
const data = {};
|
|
33
|
+
const idsToFetch = [];
|
|
34
|
+
for (const id of ids) {
|
|
35
|
+
const item = (await cache.get(getUniqueId(type, id)));
|
|
36
|
+
if (typeof item !== 'undefined') {
|
|
37
|
+
data[id] = item;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
idsToFetch.push(id);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (idsToFetch.length) {
|
|
44
|
+
logger?.trace({ items: idsToFetch.length }, 'loading from store');
|
|
45
|
+
const fetched = await store.get(type, idsToFetch);
|
|
46
|
+
for (const id of idsToFetch) {
|
|
47
|
+
const item = fetched[id];
|
|
48
|
+
if (item) {
|
|
49
|
+
data[id] = item;
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
51
|
+
await cache.set(getUniqueId(type, id), item);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return data;
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
async set(data) {
|
|
59
|
+
return cacheMutex.runExclusive(async () => {
|
|
60
|
+
let keys = 0;
|
|
61
|
+
for (const type in data) {
|
|
62
|
+
for (const id in data[type]) {
|
|
63
|
+
await cache.set(getUniqueId(type, id), data[type][id]);
|
|
64
|
+
keys += 1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
logger?.trace({ keys }, 'updated cache');
|
|
68
|
+
await store.set(data);
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
async clear() {
|
|
72
|
+
await cache.flushAll();
|
|
73
|
+
await store.clear?.();
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Adds DB-like transaction capability to the SignalKeyStore
|
|
79
|
+
* Uses AsyncLocalStorage for automatic context management
|
|
80
|
+
* @param state the key store to apply this capability to
|
|
81
|
+
* @param logger logger to log events
|
|
82
|
+
* @returns SignalKeyStore with transaction capability
|
|
83
|
+
*/
|
|
84
|
+
export const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetweenTriesMs }) => {
|
|
85
|
+
const txStorage = new AsyncLocalStorage();
|
|
86
|
+
// Queues for concurrency control (keyed by signal data type - bounded set)
|
|
87
|
+
const keyQueues = new Map();
|
|
88
|
+
// Transaction mutexes with reference counting for cleanup
|
|
89
|
+
const txMutexes = new Map();
|
|
90
|
+
const txMutexRefCounts = new Map();
|
|
91
|
+
// Pre-key manager for specialized operations
|
|
92
|
+
const preKeyManager = new PreKeyManager(state, logger);
|
|
93
|
+
/**
|
|
94
|
+
* Get or create a queue for a specific key type
|
|
95
|
+
*/
|
|
96
|
+
function getQueue(key) {
|
|
97
|
+
if (!keyQueues.has(key)) {
|
|
98
|
+
keyQueues.set(key, new PQueue({ concurrency: 1 }));
|
|
99
|
+
}
|
|
100
|
+
return keyQueues.get(key);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Get or create a transaction mutex
|
|
104
|
+
*/
|
|
105
|
+
function getTxMutex(key) {
|
|
106
|
+
if (!txMutexes.has(key)) {
|
|
107
|
+
txMutexes.set(key, new Mutex());
|
|
108
|
+
txMutexRefCounts.set(key, 0);
|
|
109
|
+
}
|
|
110
|
+
return txMutexes.get(key);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Acquire a reference to a transaction mutex
|
|
114
|
+
*/
|
|
115
|
+
function acquireTxMutexRef(key) {
|
|
116
|
+
const count = txMutexRefCounts.get(key) ?? 0;
|
|
117
|
+
txMutexRefCounts.set(key, count + 1);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Release a reference to a transaction mutex and cleanup if no longer needed
|
|
121
|
+
*/
|
|
122
|
+
function releaseTxMutexRef(key) {
|
|
123
|
+
const count = (txMutexRefCounts.get(key) ?? 1) - 1;
|
|
124
|
+
txMutexRefCounts.set(key, count);
|
|
125
|
+
// Cleanup if no more references and mutex is not locked
|
|
126
|
+
if (count <= 0) {
|
|
127
|
+
const mutex = txMutexes.get(key);
|
|
128
|
+
if (mutex && !mutex.isLocked()) {
|
|
129
|
+
txMutexes.delete(key);
|
|
130
|
+
txMutexRefCounts.delete(key);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Check if currently in a transaction
|
|
136
|
+
*/
|
|
137
|
+
function isInTransaction() {
|
|
138
|
+
return !!txStorage.getStore();
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Commit transaction with retries
|
|
142
|
+
*/
|
|
143
|
+
async function commitWithRetry(mutations) {
|
|
144
|
+
if (Object.keys(mutations).length === 0) {
|
|
145
|
+
logger.trace('no mutations in transaction');
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
logger.trace('committing transaction');
|
|
149
|
+
for (let attempt = 0; attempt < maxCommitRetries; attempt++) {
|
|
150
|
+
try {
|
|
151
|
+
await state.set(mutations);
|
|
152
|
+
logger.trace({ mutationCount: Object.keys(mutations).length }, 'committed transaction');
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
const retriesLeft = maxCommitRetries - attempt - 1;
|
|
157
|
+
logger.warn(`failed to commit mutations, retries left=${retriesLeft}`);
|
|
158
|
+
if (retriesLeft === 0) {
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
await delay(delayBetweenTriesMs);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
get: async (type, ids) => {
|
|
167
|
+
const ctx = txStorage.getStore();
|
|
168
|
+
if (!ctx) {
|
|
169
|
+
// No transaction - direct read without exclusive lock for concurrency
|
|
170
|
+
return state.get(type, ids);
|
|
171
|
+
}
|
|
172
|
+
// In transaction - check cache first
|
|
173
|
+
const cached = ctx.cache[type] || {};
|
|
174
|
+
const missing = ids.filter(id => !(id in cached));
|
|
175
|
+
if (missing.length > 0) {
|
|
176
|
+
ctx.dbQueries++;
|
|
177
|
+
logger.trace({ type, count: missing.length }, 'fetching missing keys in transaction');
|
|
178
|
+
const fetched = await getTxMutex(type).runExclusive(() => state.get(type, missing));
|
|
179
|
+
// Update cache
|
|
180
|
+
ctx.cache[type] = ctx.cache[type] || {};
|
|
181
|
+
Object.assign(ctx.cache[type], fetched);
|
|
182
|
+
}
|
|
183
|
+
// Return requested ids from cache
|
|
184
|
+
const result = {};
|
|
185
|
+
for (const id of ids) {
|
|
186
|
+
const value = ctx.cache[type]?.[id];
|
|
187
|
+
if (value !== undefined && value !== null) {
|
|
188
|
+
result[id] = value;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return result;
|
|
192
|
+
},
|
|
193
|
+
set: async (data) => {
|
|
194
|
+
const ctx = txStorage.getStore();
|
|
195
|
+
if (!ctx) {
|
|
196
|
+
// No transaction - direct write with queue protection
|
|
197
|
+
const types = Object.keys(data);
|
|
198
|
+
// Process pre-keys with validation
|
|
199
|
+
for (const type_ of types) {
|
|
200
|
+
const type = type_;
|
|
201
|
+
if (type === 'pre-key') {
|
|
202
|
+
await preKeyManager.validateDeletions(data, type);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// Write all data in parallel
|
|
206
|
+
await Promise.all(types.map(type => getQueue(type).add(async () => {
|
|
207
|
+
const typeData = { [type]: data[type] };
|
|
208
|
+
await state.set(typeData);
|
|
209
|
+
})));
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
// In transaction - update cache and mutations
|
|
213
|
+
logger.trace({ types: Object.keys(data) }, 'caching in transaction');
|
|
214
|
+
for (const key_ in data) {
|
|
215
|
+
const key = key_;
|
|
216
|
+
// Ensure structures exist
|
|
217
|
+
ctx.cache[key] = ctx.cache[key] || {};
|
|
218
|
+
ctx.mutations[key] = ctx.mutations[key] || {};
|
|
219
|
+
// Special handling for pre-keys
|
|
220
|
+
if (key === 'pre-key') {
|
|
221
|
+
await preKeyManager.processOperations(data, key, ctx.cache, ctx.mutations, true);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
// Normal key types
|
|
225
|
+
Object.assign(ctx.cache[key], data[key]);
|
|
226
|
+
Object.assign(ctx.mutations[key], data[key]);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
isInTransaction,
|
|
231
|
+
transaction: async (work, key) => {
|
|
232
|
+
const existing = txStorage.getStore();
|
|
233
|
+
// Nested transaction - reuse existing context
|
|
234
|
+
if (existing) {
|
|
235
|
+
logger.trace('reusing existing transaction context');
|
|
236
|
+
return work();
|
|
237
|
+
}
|
|
238
|
+
// New transaction - acquire mutex and create context
|
|
239
|
+
const mutex = getTxMutex(key);
|
|
240
|
+
acquireTxMutexRef(key);
|
|
241
|
+
try {
|
|
242
|
+
return await mutex.runExclusive(async () => {
|
|
243
|
+
const ctx = {
|
|
244
|
+
cache: {},
|
|
245
|
+
mutations: {},
|
|
246
|
+
dbQueries: 0
|
|
247
|
+
};
|
|
248
|
+
logger.trace('entering transaction');
|
|
249
|
+
try {
|
|
250
|
+
const result = await txStorage.run(ctx, work);
|
|
251
|
+
// Commit mutations
|
|
252
|
+
await commitWithRetry(ctx.mutations);
|
|
253
|
+
logger.trace({ dbQueries: ctx.dbQueries }, 'transaction completed');
|
|
254
|
+
return result;
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
logger.error({ error }, 'transaction failed, rolling back');
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
finally {
|
|
263
|
+
releaseTxMutexRef(key);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
};
|
|
268
|
+
/**
|
|
269
|
+
* Returns the authenticated user's JID, or throws a Boom-401 if creds are not yet authenticated.
|
|
270
|
+
* Use this anywhere we'd otherwise reach for `creds.me!.id` to fail fast with a descriptive error.
|
|
271
|
+
*/
|
|
272
|
+
export const assertMeId = (creds) => {
|
|
273
|
+
const id = creds.me?.id;
|
|
274
|
+
if (!id) {
|
|
275
|
+
throw new Boom('Cannot proceed: socket is not authenticated yet (creds.me.id is missing)', { statusCode: 401 });
|
|
276
|
+
}
|
|
277
|
+
return id;
|
|
278
|
+
};
|
|
279
|
+
export const initAuthCreds = () => {
|
|
280
|
+
const identityKey = Curve.generateKeyPair();
|
|
281
|
+
return {
|
|
282
|
+
noiseKey: Curve.generateKeyPair(),
|
|
283
|
+
pairingEphemeralKeyPair: Curve.generateKeyPair(),
|
|
284
|
+
signedIdentityKey: identityKey,
|
|
285
|
+
signedPreKey: signedKeyPair(identityKey, 1),
|
|
286
|
+
registrationId: generateRegistrationId(),
|
|
287
|
+
advSecretKey: randomBytes(32).toString('base64'),
|
|
288
|
+
processedHistoryMessages: [],
|
|
289
|
+
nextPreKeyId: 1,
|
|
290
|
+
firstUnuploadedPreKeyId: 1,
|
|
291
|
+
accountSyncCounter: 0,
|
|
292
|
+
accountSettings: {
|
|
293
|
+
unarchiveChats: false
|
|
294
|
+
},
|
|
295
|
+
registered: false,
|
|
296
|
+
pairingCode: undefined,
|
|
297
|
+
lastPropHash: undefined,
|
|
298
|
+
routingInfo: undefined,
|
|
299
|
+
additionalData: undefined
|
|
300
|
+
};
|
|
301
|
+
};
|
|
302
|
+
//# sourceMappingURL=auth-utils.js.map
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import events_1_d from 'events';
|
|
2
|
+
import * as fs_1 from 'fs';
|
|
3
|
+
import * as promises_1 from 'fs/promises';
|
|
4
|
+
import * as readline_1 from 'readline';
|
|
5
|
+
import * as generics_1 from './generics.js';
|
|
6
|
+
import * as make_mutex_1 from './make-mutex.js';
|
|
7
|
+
const events_1 = { default: events_1_d };
|
|
8
|
+
/**
|
|
9
|
+
* Captures events from a baileys event emitter & stores them in a file
|
|
10
|
+
* @param ev The event emitter to read events from
|
|
11
|
+
* @param filename File to save to
|
|
12
|
+
*/
|
|
13
|
+
const captureEventStream = (ev, filename) => {
|
|
14
|
+
const oldEmit = ev.emit;
|
|
15
|
+
// write mutex so data is appended in order
|
|
16
|
+
const writeMutex = (0, make_mutex_1.makeMutex)();
|
|
17
|
+
// monkey patch eventemitter to capture all events
|
|
18
|
+
ev.emit = function (...args) {
|
|
19
|
+
const content = JSON.stringify({ timestamp: Date.now(), event: args[0], data: args[1] }) + '\n';
|
|
20
|
+
const result = oldEmit.apply(ev, args);
|
|
21
|
+
writeMutex.mutex(async () => {
|
|
22
|
+
await (0, promises_1.writeFile)(filename, content, { flag: 'a' });
|
|
23
|
+
});
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Read event file and emit events from there
|
|
29
|
+
* @param filename filename containing event data
|
|
30
|
+
* @param delayIntervalMs delay between each event emit
|
|
31
|
+
*/
|
|
32
|
+
const readAndEmitEventStream = (filename, delayIntervalMs = 0) => {
|
|
33
|
+
const ev = new events_1.default();
|
|
34
|
+
const fireEvents = async () => {
|
|
35
|
+
// from: https://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js
|
|
36
|
+
const fileStream = (0, fs_1.createReadStream)(filename);
|
|
37
|
+
const rl = (0, readline_1.createInterface)({
|
|
38
|
+
input: fileStream,
|
|
39
|
+
crlfDelay: Infinity
|
|
40
|
+
});
|
|
41
|
+
// Note: we use the crlfDelay option to recognize all instances of CR LF
|
|
42
|
+
// ('\r\n') in input.txt as a single line break.
|
|
43
|
+
for await (const line of rl) {
|
|
44
|
+
if (line) {
|
|
45
|
+
const { event, data } = JSON.parse(line);
|
|
46
|
+
ev.emit(event, data);
|
|
47
|
+
delayIntervalMs && await (0, generics_1.delay)(delayIntervalMs);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
fileStream.close();
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
ev,
|
|
54
|
+
task: fireEvents()
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export { captureEventStream, readAndEmitEventStream };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare const BOT_SIGNATURE_VERSION: '1';
|
|
2
|
+
export declare const BOT_SIGNATURE_USE_CASE_WA_BOT_MSG: 1;
|
|
3
|
+
export declare const BOT_SIGNATURE_ROOT_VERSION: string;
|
|
4
|
+
export declare const BOT_SIGNATURE_ROOT_CERTIFICATE: string;
|
|
5
|
+
|
|
6
|
+
export declare function loadBotSignatureRoot(): import('crypto').X509Certificate;
|
|
7
|
+
|
|
8
|
+
export declare function constructSignaturePayload(options: {
|
|
9
|
+
botFbid: string;
|
|
10
|
+
messageDigest: Buffer | Uint8Array;
|
|
11
|
+
version?: string;
|
|
12
|
+
}): Buffer;
|
|
13
|
+
|
|
14
|
+
export declare function verifyBotSignature(options: {
|
|
15
|
+
botJid?: string;
|
|
16
|
+
unifiedResponseBytes?: Buffer | Uint8Array;
|
|
17
|
+
proof?: any;
|
|
18
|
+
at?: number;
|
|
19
|
+
root?: import('crypto').X509Certificate;
|
|
20
|
+
}): { status: 'passed' | 'failed'; reason?: string };
|
|
21
|
+
|
|
22
|
+
export declare function isParsableCertificateChain(chain?: unknown): boolean;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { X509Certificate, verify as cryptoVerify } from 'crypto';
|
|
2
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
3
|
+
|
|
4
|
+
export const BOT_SIGNATURE_VERSION = '1'
|
|
5
|
+
export const BOT_SIGNATURE_USE_CASE_WA_BOT_MSG = 1
|
|
6
|
+
export const BOT_SIGNATURE_ROOT_VERSION = '2025-10'
|
|
7
|
+
|
|
8
|
+
export const BOT_SIGNATURE_ROOT_CERTIFICATE = `-----BEGIN CERTIFICATE-----
|
|
9
|
+
MIIC7TCCApOgAwIBAgIUAtu5QuxmVhfGT8LPkjfm40mSl0AwCgYIKoZIzj0EAwIw
|
|
10
|
+
dzEgMB4GA1UEAwwXTWV0YSBXQSBGZWF0dXJlIFJvb3QgQ0ExCzAJBgNVBAYTAlVT
|
|
11
|
+
MRMwEQYDVQQIDApDYWxpZm9ybmlhMRMwEQYDVQQHDApNZW5sbyBQYXJrMRwwGgYD
|
|
12
|
+
VQQKDBNNZXRhIFBsYXRmb3JtcyBJbmMuMCAXDTI1MDkwNDE3MzEyNFoYDzIwNjUw
|
|
13
|
+
OTA0MTczMTI0WjB3MSAwHgYDVQQDDBdNZXRhIFdBIEZlYXR1cmUgUm9vdCBDQTEL
|
|
14
|
+
MAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExEzARBgNVBAcMCk1lbmxv
|
|
15
|
+
IFBhcmsxHDAaBgNVBAoME01ldGEgUGxhdGZvcm1zIEluYy4wWTATBgcqhkjOPQIB
|
|
16
|
+
BggqhkjOPQMBBwNCAAT60blw90ebreMkw8+Wpcs0ETAkr1VQjoZoyi7PSSQbsoiP
|
|
17
|
+
qYRnzfRrR+xiahaXbYU83qXiTHjVUiOU9wDxI83qo4H6MIH3MA8GA1UdEwEB/wQF
|
|
18
|
+
MAMBAf8wHQYDVR0OBBYEFNO7KMTVSYUxkL6VS3LyWJw7m76zMIG0BgNVHSMEgaww
|
|
19
|
+
gamAFNO7KMTVSYUxkL6VS3LyWJw7m76zoXukeTB3MSAwHgYDVQQDDBdNZXRhIFdB
|
|
20
|
+
IEZlYXR1cmUgUm9vdCBDQTELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3Ju
|
|
21
|
+
aWExEzARBgNVBAcMCk1lbmxvIFBhcmsxHDAaBgNVBAoME01ldGEgUGxhdGZvcm1z
|
|
22
|
+
IEluYy6CFALbuULsZlYXxk/Cz5I35uNJkpdAMA4GA1UdDwEB/wQEAwIBhjAKBggq
|
|
23
|
+
hkjOPQQDAgNIADBFAiAINhjk9DbP416vx/WjqdUfexgic08aQsxnpDDsNE5M0gIh
|
|
24
|
+
ANorq7KwCQVMtS2or5uKJAQsx1FxCHyDafq2GCk9t0AN
|
|
25
|
+
-----END CERTIFICATE-----`
|
|
26
|
+
|
|
27
|
+
let rootCertificate = null
|
|
28
|
+
|
|
29
|
+
export const loadBotSignatureRoot = () => {
|
|
30
|
+
if (!rootCertificate) {
|
|
31
|
+
rootCertificate = new X509Certificate(BOT_SIGNATURE_ROOT_CERTIFICATE)
|
|
32
|
+
}
|
|
33
|
+
return rootCertificate
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const constructSignaturePayload = ({ botFbid, messageDigest, version = BOT_SIGNATURE_VERSION }) =>
|
|
37
|
+
Buffer.concat([Buffer.from(version, 'utf8'), Buffer.from(String(botFbid), 'utf8'), Buffer.from(messageDigest)])
|
|
38
|
+
|
|
39
|
+
export const isParsableCertificateChain = (chain) => {
|
|
40
|
+
if (!Array.isArray(chain) || !chain.length) {
|
|
41
|
+
return false
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
for (const entry of chain) {
|
|
45
|
+
new X509Certificate(Buffer.from(entry))
|
|
46
|
+
}
|
|
47
|
+
return true
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const failed = reason => ({ status: 'failed', reason })
|
|
55
|
+
|
|
56
|
+
const validAt = (certificate, at) => {
|
|
57
|
+
const from = Date.parse(certificate.validFrom)
|
|
58
|
+
const to = Date.parse(certificate.validTo)
|
|
59
|
+
return Number.isFinite(from) && Number.isFinite(to) && at >= from && at <= to
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const verifyBotSignature = ({ botJid, unifiedResponseBytes, proof, at = Date.now(), root = loadBotSignatureRoot() } = {}) => {
|
|
63
|
+
if (!proof) {
|
|
64
|
+
return failed('no WA_BOT_MSG proof')
|
|
65
|
+
}
|
|
66
|
+
if (Number(proof.version) !== 1) {
|
|
67
|
+
return failed(`unsupported signature version ${proof.version}`)
|
|
68
|
+
}
|
|
69
|
+
if (!proof.signature?.length) {
|
|
70
|
+
return failed('proof carries no signature')
|
|
71
|
+
}
|
|
72
|
+
if (!proof.certificateChain?.length) {
|
|
73
|
+
return failed('empty certificate chain')
|
|
74
|
+
}
|
|
75
|
+
if (!unifiedResponseBytes?.length) {
|
|
76
|
+
return failed('no unified response bytes to verify')
|
|
77
|
+
}
|
|
78
|
+
const botFbid = String(botJid ?? '').split('@')[0]
|
|
79
|
+
if (!botFbid) {
|
|
80
|
+
return failed('no bot jid, the fbid is part of the signed payload')
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let chain
|
|
84
|
+
try {
|
|
85
|
+
chain = proof.certificateChain.map(entry => new X509Certificate(Buffer.from(entry)))
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
return failed(`certificate did not parse: ${error.message}`)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const [leaf] = chain
|
|
92
|
+
if (!validAt(leaf, at)) {
|
|
93
|
+
return failed('leaf certificate is outside its validity window')
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const full = [...chain, root]
|
|
97
|
+
for (let index = 0; index < full.length - 1; index += 1) {
|
|
98
|
+
const issuer = full[index + 1]
|
|
99
|
+
if (!validAt(issuer, at)) {
|
|
100
|
+
return failed(`issuer at position ${index + 1} is outside its validity window`)
|
|
101
|
+
}
|
|
102
|
+
let ok = false
|
|
103
|
+
try {
|
|
104
|
+
ok = full[index].verify(issuer.publicKey)
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
return failed(`chain verification threw at position ${index}: ${error.message}`)
|
|
108
|
+
}
|
|
109
|
+
if (!ok) {
|
|
110
|
+
return failed(`chain broken at position ${index}`)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const payload = constructSignaturePayload({ botFbid, messageDigest: unifiedResponseBytes })
|
|
115
|
+
let ok = false
|
|
116
|
+
try {
|
|
117
|
+
ok = cryptoVerify(null, payload, leaf.publicKey, Buffer.from(proof.signature))
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
return failed(`signature verification threw: ${error.message}`)
|
|
121
|
+
}
|
|
122
|
+
return ok ? { status: 'passed' } : failed('signature does not match the unified response bytes')
|
|
123
|
+
}
|
|
124
|
+
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { platform, release } from 'os';
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
const PLATFORM_MAP = {
|
|
4
|
+
aix: 'AIX',
|
|
5
|
+
darwin: 'Mac OS',
|
|
6
|
+
win32: 'Windows',
|
|
7
|
+
android: 'Android',
|
|
8
|
+
freebsd: 'FreeBSD',
|
|
9
|
+
openbsd: 'OpenBSD',
|
|
10
|
+
sunos: 'Solaris',
|
|
11
|
+
linux: 'Linux',
|
|
12
|
+
haiku: undefined,
|
|
13
|
+
cygwin: undefined,
|
|
14
|
+
netbsd: undefined
|
|
15
|
+
};
|
|
16
|
+
const BROWSER_MAP = {
|
|
17
|
+
safari: 'Safari',
|
|
18
|
+
chrome: 'Chrome',
|
|
19
|
+
edge: 'Edge',
|
|
20
|
+
firefox: 'Firefox',
|
|
21
|
+
opera: 'Opera',
|
|
22
|
+
brave: 'Brave',
|
|
23
|
+
samsung: 'Samsung Internet'
|
|
24
|
+
};
|
|
25
|
+
const getBrowserN = (bros) => {
|
|
26
|
+
const brosN = BROWSER_MAP[bros] || bros;
|
|
27
|
+
return brosN;
|
|
28
|
+
};
|
|
29
|
+
export const Browsers = {
|
|
30
|
+
ubuntu: browser => ['Ubuntu', getBrowserN(browser), '22.04.4'],
|
|
31
|
+
macOS: browser => ['Mac OS', getBrowserN(browser), '14.4.1'],
|
|
32
|
+
baileys: browser => ['Baileys', getBrowserN(browser), '6.5.0'],
|
|
33
|
+
windows: browser => ['Windows', getBrowserN(browser), '10.0.22631'],
|
|
34
|
+
iOS: browser => ['iOS', getBrowserN(browser), '18.2'],
|
|
35
|
+
android: browser => ['Android', getBrowserN(browser), '14.0.0'],
|
|
36
|
+
safari: browser => ['Safari', getBrowserN(browser), '26.5'],
|
|
37
|
+
custom: (platform, browser, ver) => {
|
|
38
|
+
let platformN = PLATFORM_MAP[platform].toLowerCase() || platform;
|
|
39
|
+
return [platformN, getBrowserN(browser), ver];
|
|
40
|
+
},
|
|
41
|
+
/** The appropriate browser based on your OS & release */
|
|
42
|
+
appropriate: browser => [PLATFORM_MAP[platform()] || 'Ubuntu', getBrowserN(browser), release()]
|
|
43
|
+
};
|
|
44
|
+
export const getPlatformId = (browser) => {
|
|
45
|
+
const platformType = proto.DeviceProps.PlatformType[browser.toUpperCase()];
|
|
46
|
+
return platformType ? platformType.toString() : '1'; //chrome
|
|
47
|
+
};
|
|
48
|
+
//# sourceMappingURL=browser-utils.js.map
|