@dhilzzkz/baileys 0.0.0-stage → 0.1.0
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/LICENSE +23 -0
- package/NOTICE.md +37 -0
- package/README.md +2083 -2
- package/WAProto/index.d.ts +22220 -0
- package/WAProto/index.js +5 -0
- package/lib/Addon/http-error.d.ts +18 -0
- package/lib/Addon/http-error.js +40 -0
- package/lib/Addon/index.d.ts +3 -0
- package/lib/Addon/index.js +4 -0
- package/lib/Addon/keyed-db.d.ts +14 -0
- package/lib/Addon/keyed-db.js +241 -0
- package/lib/Addon/link-preview.d.ts +15 -0
- package/lib/Addon/link-preview.js +79 -0
- package/lib/Defaults/index.d.ts +188 -0
- package/lib/Defaults/index.js +185 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-cipher.d.ts +10 -0
- package/lib/Signal/Group/group-cipher.js +81 -0
- package/lib/Signal/Group/group-session-builder.d.ts +7 -0
- package/lib/Signal/Group/group-session-builder.js +29 -0
- package/lib/Signal/Group/index.d.ts +11 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.d.ts +6 -0
- package/lib/Signal/Group/keyhelper.js +17 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
- package/lib/Signal/Group/sender-chain-key.js +25 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
- package/lib/Signal/Group/sender-key-message.d.ts +18 -0
- package/lib/Signal/Group/sender-key-message.js +65 -0
- package/lib/Signal/Group/sender-key-name.d.ts +11 -0
- package/lib/Signal/Group/sender-key-name.js +47 -0
- package/lib/Signal/Group/sender-key-record.d.ts +12 -0
- package/lib/Signal/Group/sender-key-record.js +40 -0
- package/lib/Signal/Group/sender-key-state.d.ts +16 -0
- package/lib/Signal/Group/sender-key-state.js +83 -0
- package/lib/Signal/Group/sender-message-key.d.ts +11 -0
- package/lib/Signal/Group/sender-message-key.js +25 -0
- package/lib/Signal/Protocol/crypto.d.ts +6 -0
- package/lib/Signal/Protocol/crypto.js +61 -0
- package/lib/Signal/Protocol/curve.d.ts +8 -0
- package/lib/Signal/Protocol/curve.js +61 -0
- package/lib/Signal/Protocol/index.d.ts +16 -0
- package/lib/Signal/Protocol/index.js +9 -0
- package/lib/Signal/Protocol/internals.d.ts +57 -0
- package/lib/Signal/Protocol/internals.js +155 -0
- package/lib/Signal/Protocol/session-builder.d.ts +40 -0
- package/lib/Signal/Protocol/session-builder.js +143 -0
- package/lib/Signal/Protocol/session-cipher.d.ts +26 -0
- package/lib/Signal/Protocol/session-cipher.js +301 -0
- package/lib/Signal/Protocol/session-record.d.ts +55 -0
- package/lib/Signal/Protocol/session-record.js +272 -0
- package/lib/Signal/libsignal.d.ts +54 -0
- package/lib/Signal/libsignal.js +426 -0
- package/lib/Signal/lid-mapping.d.ts +20 -0
- package/lib/Signal/lid-mapping.js +274 -0
- package/lib/Socket/Client/index.d.ts +2 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.d.ts +5 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.d.ts +12 -0
- package/lib/Socket/Client/websocket.js +119 -0
- package/lib/Socket/business.d.ts +387 -0
- package/lib/Socket/business.js +380 -0
- package/lib/Socket/chats.d.ts +144 -0
- package/lib/Socket/chats.js +1216 -0
- package/lib/Socket/communities.d.ts +554 -0
- package/lib/Socket/communities.js +431 -0
- package/lib/Socket/groups.d.ts +286 -0
- package/lib/Socket/groups.js +347 -0
- package/lib/Socket/index.d.ts +522 -0
- package/lib/Socket/index.js +11 -0
- package/lib/Socket/messages-recv.d.ts +326 -0
- package/lib/Socket/messages-recv.js +1822 -0
- package/lib/Socket/messages-send.d.ts +321 -0
- package/lib/Socket/messages-send.js +1378 -0
- package/lib/Socket/mex.d.ts +1 -0
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter.d.ts +283 -0
- package/lib/Socket/newsletter.js +225 -0
- package/lib/Socket/socket.d.ts +61 -0
- package/lib/Socket/socket.js +992 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +3 -0
- package/lib/Store/make-in-memory-store.d.ts +63 -0
- package/lib/Store/make-in-memory-store.js +418 -0
- package/lib/Store/make-ordered-dictionary.d.ts +12 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.d.ts +1 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Business.d.ts +1 -0
- package/lib/Types/Business.js +1 -0
- package/lib/Types/Call.d.ts +1 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.d.ts +1 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.d.ts +1 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.d.ts +1 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.d.ts +1 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.d.ts +2 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.d.ts +2 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.d.ts +11 -0
- package/lib/Types/Message.js +17 -0
- package/lib/Types/Mex.d.ts +2 -0
- package/lib/Types/Mex.js +38 -0
- package/lib/Types/Product.d.ts +1 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/RichType.d.ts +2 -0
- package/lib/Types/RichType.js +22 -0
- package/lib/Types/Signal.d.ts +1 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.d.ts +1 -0
- package/lib/Types/Socket.js +1 -0
- package/lib/Types/State.d.ts +5 -0
- package/lib/Types/State.js +54 -0
- package/lib/Types/USync.d.ts +1 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.d.ts +13 -0
- package/lib/Types/index.js +25 -0
- package/lib/Utils/Auth/auth-utils.d.ts +58 -0
- package/lib/Utils/Auth/auth-utils.js +316 -0
- package/lib/Utils/Auth/companion-reg-client-utils.d.ts +4 -0
- package/lib/Utils/Auth/companion-reg-client-utils.js +39 -0
- package/lib/Utils/Auth/pre-key-manager.d.ts +25 -0
- package/lib/Utils/Auth/pre-key-manager.js +105 -0
- package/lib/Utils/Auth/use-multi-file-auth-state.d.ts +10 -0
- package/lib/Utils/Auth/use-multi-file-auth-state.js +116 -0
- package/lib/Utils/Auth/use-single-file-auth-state.d.ts +11 -0
- package/lib/Utils/Auth/use-single-file-auth-state.js +94 -0
- package/lib/Utils/Auth/use-sqlite-auth-state.d.ts +11 -0
- package/lib/Utils/Auth/use-sqlite-auth-state.js +109 -0
- package/lib/Utils/Auth/validate-connection.d.ts +44 -0
- package/lib/Utils/Auth/validate-connection.js +210 -0
- package/lib/Utils/Common/browser-utils.d.ts +9 -0
- package/lib/Utils/Common/browser-utils.js +28 -0
- package/lib/Utils/Common/generics.d.ts +63 -0
- package/lib/Utils/Common/generics.js +392 -0
- package/lib/Utils/Common/logger.d.ts +12 -0
- package/lib/Utils/Common/logger.js +12 -0
- package/lib/Utils/Common/make-mutex.d.ts +6 -0
- package/lib/Utils/Common/make-mutex.js +32 -0
- package/lib/Utils/Crypto/crypto.d.ts +39 -0
- package/lib/Utils/Crypto/crypto.js +117 -0
- package/lib/Utils/Crypto/lt-hash.d.ts +6 -0
- package/lib/Utils/Crypto/lt-hash.js +7 -0
- package/lib/Utils/Crypto/noise-handler.d.ts +20 -0
- package/lib/Utils/Crypto/noise-handler.js +200 -0
- package/lib/Utils/Crypto/signal.d.ts +82 -0
- package/lib/Utils/Crypto/signal.js +200 -0
- package/lib/Utils/Message/business.d.ts +49 -0
- package/lib/Utils/Message/business.js +230 -0
- package/lib/Utils/Message/decode-wa-message.d.ts +83 -0
- package/lib/Utils/Message/decode-wa-message.js +316 -0
- package/lib/Utils/Message/history.d.ts +75 -0
- package/lib/Utils/Message/history.js +133 -0
- package/lib/Utils/Message/link-preview.d.ts +13 -0
- package/lib/Utils/Message/link-preview.js +73 -0
- package/lib/Utils/Message/message-retry-manager.d.ts +79 -0
- package/lib/Utils/Message/message-retry-manager.js +259 -0
- package/lib/Utils/Message/messages-media.d.ts +134 -0
- package/lib/Utils/Message/messages-media.js +802 -0
- package/lib/Utils/Message/messages.d.ts +47 -0
- package/lib/Utils/Message/messages.js +1907 -0
- package/lib/Utils/Message/offline-node-processor.d.ts +10 -0
- package/lib/Utils/Message/offline-node-processor.js +40 -0
- package/lib/Utils/Message/process-message.d.ts +83 -0
- package/lib/Utils/Message/process-message.js +748 -0
- package/lib/Utils/Message/reporting-utils.d.ts +12 -0
- package/lib/Utils/Message/reporting-utils.js +257 -0
- package/lib/Utils/Message/rich-message-utils.d.ts +48 -0
- package/lib/Utils/Message/rich-message-utils.js +366 -0
- package/lib/Utils/Message/stanza-ack.d.ts +16 -0
- package/lib/Utils/Message/stanza-ack.js +37 -0
- package/lib/Utils/State/chat-utils.d.ts +408 -0
- package/lib/Utils/State/chat-utils.js +871 -0
- package/lib/Utils/State/event-buffer.d.ts +12 -0
- package/lib/Utils/State/event-buffer.js +621 -0
- package/lib/Utils/State/identity-change-handler.d.ts +13 -0
- package/lib/Utils/State/identity-change-handler.js +48 -0
- package/lib/Utils/State/sync-action-utils.d.ts +2 -0
- package/lib/Utils/State/sync-action-utils.js +48 -0
- package/lib/Utils/State/tc-token-utils.d.ts +29 -0
- package/lib/Utils/State/tc-token-utils.js +163 -0
- package/lib/Utils/index.d.ts +24 -0
- package/lib/Utils/index.js +24 -0
- package/lib/WABinary/constants.d.ts +61 -0
- package/lib/WABinary/constants.js +1466 -0
- package/lib/WABinary/decode.d.ts +9 -0
- package/lib/WABinary/decode.js +261 -0
- package/lib/WABinary/encode.d.ts +2 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.d.ts +79 -0
- package/lib/WABinary/generic-utils.js +226 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.d.ts +28 -0
- package/lib/WABinary/jid-utils.js +95 -0
- package/lib/WABinary/types.d.ts +1 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.d.ts +7 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.d.ts +34926 -0
- package/lib/WAM/constants.js +22852 -0
- package/lib/WAM/encode.d.ts +1 -0
- package/lib/WAM/encode.js +149 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.d.ts +38 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +50 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +51 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.d.ts +14 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.d.ts +5 -0
- package/lib/WAUSync/Protocols/index.js +5 -0
- package/lib/WAUSync/USyncQuery.d.ts +20 -0
- package/lib/WAUSync/USyncQuery.js +97 -0
- package/lib/WAUSync/USyncUser.d.ts +16 -0
- package/lib/WAUSync/USyncUser.js +30 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +11 -0
- package/package.json +120 -5
- package/scripts/engine-requirements.js +16 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { proto } from '../../../WAProto/index.js';
|
|
2
|
+
import { isLidUser, isPnUser } from '../../WABinary/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Process contactAction and return events to emit.
|
|
5
|
+
* Pure function - no side effects.
|
|
6
|
+
*/
|
|
7
|
+
export const processContactAction = (action, id, logger) => {
|
|
8
|
+
const results = [];
|
|
9
|
+
if (!id) {
|
|
10
|
+
logger?.warn({ hasFullName: !!action.fullName, hasLidJid: !!action.lidJid, hasPnJid: !!action.pnJid }, 'contactAction sync: missing id in index');
|
|
11
|
+
return results;
|
|
12
|
+
}
|
|
13
|
+
const lidJid = action.lidJid;
|
|
14
|
+
const idIsPn = isPnUser(id);
|
|
15
|
+
// PN is in index[1], not in contactAction.pnJid which is usually null
|
|
16
|
+
const phoneNumber = idIsPn ? id : action.pnJid || undefined;
|
|
17
|
+
// Always emit contacts.upsert
|
|
18
|
+
results.push({
|
|
19
|
+
event: 'contacts.upsert',
|
|
20
|
+
data: [
|
|
21
|
+
{
|
|
22
|
+
id,
|
|
23
|
+
name: action.fullName || action.firstName || action.username || undefined,
|
|
24
|
+
username: action.username || undefined,
|
|
25
|
+
lid: lidJid || undefined,
|
|
26
|
+
phoneNumber
|
|
27
|
+
}
|
|
28
|
+
]
|
|
29
|
+
});
|
|
30
|
+
// Emit lid-mapping.update if we have valid LID-PN pair
|
|
31
|
+
if (lidJid && isLidUser(lidJid) && idIsPn) {
|
|
32
|
+
results.push({
|
|
33
|
+
event: 'lid-mapping.update',
|
|
34
|
+
data: { lid: lidJid, pn: id }
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return results;
|
|
38
|
+
};
|
|
39
|
+
export const emitSyncActionResults = (ev, results) => {
|
|
40
|
+
for (const result of results) {
|
|
41
|
+
if (result.event === 'contacts.upsert') {
|
|
42
|
+
ev.emit('contacts.upsert', result.data);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
ev.emit('lid-mapping.update', result.data);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Read the persisted tctoken JID index and return its entries (never contains the sentinel key itself). */
|
|
2
|
+
export function readTcTokenIndex(keys: any): Promise<any[]>;
|
|
3
|
+
/** Build a SignalDataSet fragment that writes the merged index (persisted ∪ added) under the sentinel key. */
|
|
4
|
+
export function buildMergedTcTokenIndexWrite(keys: any, addedJids: any): Promise<{
|
|
5
|
+
__index: {
|
|
6
|
+
token: any;
|
|
7
|
+
};
|
|
8
|
+
}>;
|
|
9
|
+
export function isTcTokenExpired(timestamp: any): boolean;
|
|
10
|
+
export function shouldSendNewTcToken(senderTimestamp: any): boolean;
|
|
11
|
+
/** Resolve JID to LID for tctoken storage (WA Web stores under LID) */
|
|
12
|
+
export function resolveTcTokenJid(jid: any, getLIDForPN: any): Promise<any>;
|
|
13
|
+
/** Resolve target JID for issuing privacy token based on AB prop 14303 */
|
|
14
|
+
export function resolveIssuanceJid(jid: any, issueToLid: any, getLIDForPN: any, getPNForLID: any): Promise<any>;
|
|
15
|
+
export function buildTcTokenFromJid({ authState, jid, baseContent, getLIDForPN }: {
|
|
16
|
+
authState: any;
|
|
17
|
+
jid: any;
|
|
18
|
+
baseContent?: never[] | undefined;
|
|
19
|
+
getLIDForPN: any;
|
|
20
|
+
}): Promise<any[] | undefined>;
|
|
21
|
+
export function storeTcTokensFromIqResult({ result, fallbackJid, keys, getLIDForPN, onNewJidStored }: {
|
|
22
|
+
result: any;
|
|
23
|
+
fallbackJid: any;
|
|
24
|
+
keys: any;
|
|
25
|
+
getLIDForPN: any;
|
|
26
|
+
onNewJidStored: any;
|
|
27
|
+
}): Promise<void>;
|
|
28
|
+
/** Sentinel key under `tctoken` store holding a JSON array of tracked storage JIDs for cross-session pruning. */
|
|
29
|
+
export const TC_TOKEN_INDEX_KEY: "__index";
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { getBinaryNodeChild, getBinaryNodeChildren, isHostedLidUser, isHostedPnUser, isJidMetaAI, isLidUser, isPnUser, jidNormalizedUser } from '../../WABinary/index.js';
|
|
2
|
+
// Same phone-number pattern as WABinary's isJidBot, applied against the user
|
|
3
|
+
// part so the check is invariant to @c.us ↔ @s.whatsapp.net normalization.
|
|
4
|
+
const BOT_PHONE_REGEX = /^1313555\d{4}$|^131655500\d{2}$/;
|
|
5
|
+
/**
|
|
6
|
+
* Mirrors WA Web's `Wid.isRegularUser()` (user ∧ ¬PSA ∧ ¬Bot). Used to gate tctoken
|
|
7
|
+
* storage against malformed notifications — WA Web filters server-side but we
|
|
8
|
+
* defend here for parity with `WAWebSetTcTokenChatAction.handleIncomingTcToken`.
|
|
9
|
+
* Works for both pre- and post-normalized JIDs (`@c.us` vs `@s.whatsapp.net`).
|
|
10
|
+
*/
|
|
11
|
+
function isRegularUser(jid) {
|
|
12
|
+
if (!jid)
|
|
13
|
+
return false;
|
|
14
|
+
const user = jid.split('@')[0] ?? '';
|
|
15
|
+
if (user === '0')
|
|
16
|
+
return false; // PSA
|
|
17
|
+
if (BOT_PHONE_REGEX.test(user))
|
|
18
|
+
return false; // Bot by phone pattern
|
|
19
|
+
if (isJidMetaAI(jid))
|
|
20
|
+
return false; // MetaAI (@bot server)
|
|
21
|
+
return !!(isPnUser(jid) || isLidUser(jid) || isHostedPnUser(jid) || isHostedLidUser(jid) || jid.endsWith('@c.us'));
|
|
22
|
+
}
|
|
23
|
+
const TC_TOKEN_BUCKET_DURATION = 604800; // 7 days
|
|
24
|
+
const TC_TOKEN_NUM_BUCKETS = 4; // ~28-day rolling window
|
|
25
|
+
/** Sentinel key under `tctoken` store holding a JSON array of tracked storage JIDs for cross-session pruning. */
|
|
26
|
+
export const TC_TOKEN_INDEX_KEY = '__index';
|
|
27
|
+
/** Read the persisted tctoken JID index and return its entries (never contains the sentinel key itself). */
|
|
28
|
+
export async function readTcTokenIndex(keys) {
|
|
29
|
+
const data = await keys.get('tctoken', [TC_TOKEN_INDEX_KEY]);
|
|
30
|
+
const entry = data[TC_TOKEN_INDEX_KEY];
|
|
31
|
+
if (!entry?.token?.length)
|
|
32
|
+
return [];
|
|
33
|
+
try {
|
|
34
|
+
const parsed = JSON.parse(Buffer.from(entry.token).toString());
|
|
35
|
+
if (!Array.isArray(parsed))
|
|
36
|
+
return [];
|
|
37
|
+
return parsed.filter((j) => typeof j === 'string' && j.length > 0 && j !== TC_TOKEN_INDEX_KEY);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Build a SignalDataSet fragment that writes the merged index (persisted ∪ added) under the sentinel key. */
|
|
44
|
+
export async function buildMergedTcTokenIndexWrite(keys, addedJids) {
|
|
45
|
+
const persisted = await readTcTokenIndex(keys);
|
|
46
|
+
const merged = new Set(persisted);
|
|
47
|
+
for (const jid of addedJids) {
|
|
48
|
+
if (jid && jid !== TC_TOKEN_INDEX_KEY)
|
|
49
|
+
merged.add(jid);
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
[TC_TOKEN_INDEX_KEY]: { token: Buffer.from(JSON.stringify([...merged])) }
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
// WA Web has separate sender/receiver AB props for these but they're identical today
|
|
56
|
+
export function isTcTokenExpired(timestamp) {
|
|
57
|
+
if (timestamp === null || timestamp === undefined)
|
|
58
|
+
return true;
|
|
59
|
+
const ts = typeof timestamp === 'string' ? parseInt(timestamp) : timestamp;
|
|
60
|
+
if (isNaN(ts))
|
|
61
|
+
return true;
|
|
62
|
+
const now = Math.floor(Date.now() / 1000);
|
|
63
|
+
const currentBucket = Math.floor(now / TC_TOKEN_BUCKET_DURATION);
|
|
64
|
+
const cutoffBucket = currentBucket - (TC_TOKEN_NUM_BUCKETS - 1);
|
|
65
|
+
const cutoffTimestamp = cutoffBucket * TC_TOKEN_BUCKET_DURATION;
|
|
66
|
+
return ts < cutoffTimestamp;
|
|
67
|
+
}
|
|
68
|
+
export function shouldSendNewTcToken(senderTimestamp) {
|
|
69
|
+
if (senderTimestamp === undefined)
|
|
70
|
+
return true;
|
|
71
|
+
const now = Math.floor(Date.now() / 1000);
|
|
72
|
+
const currentBucket = Math.floor(now / TC_TOKEN_BUCKET_DURATION);
|
|
73
|
+
const senderBucket = Math.floor(senderTimestamp / TC_TOKEN_BUCKET_DURATION);
|
|
74
|
+
return currentBucket > senderBucket;
|
|
75
|
+
}
|
|
76
|
+
/** Resolve JID to LID for tctoken storage (WA Web stores under LID) */
|
|
77
|
+
export async function resolveTcTokenJid(jid, getLIDForPN) {
|
|
78
|
+
if (isLidUser(jid))
|
|
79
|
+
return jid;
|
|
80
|
+
const lid = await getLIDForPN(jid);
|
|
81
|
+
return lid ?? jid;
|
|
82
|
+
}
|
|
83
|
+
/** Resolve target JID for issuing privacy token based on AB prop 14303 */
|
|
84
|
+
export async function resolveIssuanceJid(jid, issueToLid, getLIDForPN, getPNForLID) {
|
|
85
|
+
if (issueToLid) {
|
|
86
|
+
if (isLidUser(jid))
|
|
87
|
+
return jid;
|
|
88
|
+
const lid = await getLIDForPN(jid);
|
|
89
|
+
return lid ?? jid;
|
|
90
|
+
}
|
|
91
|
+
if (!isLidUser(jid))
|
|
92
|
+
return jid;
|
|
93
|
+
if (getPNForLID) {
|
|
94
|
+
const pn = await getPNForLID(jid);
|
|
95
|
+
return pn ?? jid;
|
|
96
|
+
}
|
|
97
|
+
return jid;
|
|
98
|
+
}
|
|
99
|
+
export async function buildTcTokenFromJid({ authState, jid, baseContent = [], getLIDForPN }) {
|
|
100
|
+
try {
|
|
101
|
+
const storageJid = await resolveTcTokenJid(jid, getLIDForPN);
|
|
102
|
+
const tcTokenData = await authState.keys.get('tctoken', [storageJid]);
|
|
103
|
+
const entry = tcTokenData?.[storageJid];
|
|
104
|
+
const tcTokenBuffer = entry?.token;
|
|
105
|
+
const timestamp = entry?.timestamp;
|
|
106
|
+
if (!tcTokenBuffer?.length || timestamp === undefined || isTcTokenExpired(timestamp)) {
|
|
107
|
+
if (tcTokenBuffer) {
|
|
108
|
+
// Preserve senderTimestamp so shouldSendNewTcToken() keeps its dedupe state
|
|
109
|
+
// after we drop the unusable peer token. Only wipe the record entirely when
|
|
110
|
+
// there's nothing worth keeping.
|
|
111
|
+
const cleared = entry?.senderTimestamp !== undefined
|
|
112
|
+
? { token: Buffer.alloc(0), senderTimestamp: entry.senderTimestamp }
|
|
113
|
+
: null;
|
|
114
|
+
await authState.keys.set({ tctoken: { [storageJid]: cleared } });
|
|
115
|
+
}
|
|
116
|
+
return baseContent.length > 0 ? baseContent : undefined;
|
|
117
|
+
}
|
|
118
|
+
baseContent.push({
|
|
119
|
+
tag: 'tctoken',
|
|
120
|
+
attrs: { t: String(timestamp) },
|
|
121
|
+
content: tcTokenBuffer
|
|
122
|
+
});
|
|
123
|
+
return baseContent;
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
return baseContent.length > 0 ? baseContent : undefined;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
export async function storeTcTokensFromIqResult({ result, fallbackJid, keys, getLIDForPN, onNewJidStored }) {
|
|
130
|
+
const tokensNode = getBinaryNodeChild(result, 'tokens');
|
|
131
|
+
if (!tokensNode)
|
|
132
|
+
return;
|
|
133
|
+
const tokenNodes = getBinaryNodeChildren(tokensNode, 'token');
|
|
134
|
+
for (const tokenNode of tokenNodes) {
|
|
135
|
+
if (tokenNode.attrs.type !== 'trusted_contact' || !(tokenNode.content instanceof Uint8Array)) {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
// In notifications tokenNode.attrs.jid is your own device JID, not the sender's
|
|
139
|
+
const rawJid = jidNormalizedUser(fallbackJid || tokenNode.attrs.jid);
|
|
140
|
+
if (!isRegularUser(rawJid))
|
|
141
|
+
continue;
|
|
142
|
+
const storageJid = await resolveTcTokenJid(rawJid, getLIDForPN);
|
|
143
|
+
const existingTcData = await keys.get('tctoken', [storageJid]);
|
|
144
|
+
const existingEntry = existingTcData[storageJid];
|
|
145
|
+
const existingTs = existingEntry?.timestamp ? Number(existingEntry.timestamp) : 0;
|
|
146
|
+
const incomingTs = tokenNode.attrs.t ? Number(tokenNode.attrs.t) : 0;
|
|
147
|
+
// timestamp-less tokens would be immediately expired
|
|
148
|
+
if (!incomingTs)
|
|
149
|
+
continue;
|
|
150
|
+
if (existingTs > 0 && existingTs > incomingTs)
|
|
151
|
+
continue;
|
|
152
|
+
await keys.set({
|
|
153
|
+
tctoken: {
|
|
154
|
+
[storageJid]: {
|
|
155
|
+
...existingEntry,
|
|
156
|
+
token: Buffer.from(tokenNode.content),
|
|
157
|
+
timestamp: tokenNode.attrs.t
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
onNewJidStored?.(storageJid);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export * from "./Common/generics.js";
|
|
2
|
+
export * from "./Message/decode-wa-message.js";
|
|
3
|
+
export * from "./Message/messages.js";
|
|
4
|
+
export * from "./Message/messages-media.js";
|
|
5
|
+
export * from "./Auth/validate-connection.js";
|
|
6
|
+
export * from "./Crypto/crypto.js";
|
|
7
|
+
export * from "./Crypto/signal.js";
|
|
8
|
+
export * from "./Crypto/noise-handler.js";
|
|
9
|
+
export * from "./Message/history.js";
|
|
10
|
+
export * from "./State/chat-utils.js";
|
|
11
|
+
export * from "./Crypto/lt-hash.js";
|
|
12
|
+
export * from "./Auth/auth-utils.js";
|
|
13
|
+
export * from "./Auth/use-multi-file-auth-state.js";
|
|
14
|
+
export * from "./Auth/use-single-file-auth-state.js";
|
|
15
|
+
export * from "./Auth/use-sqlite-auth-state.js";
|
|
16
|
+
export * from "./Message/link-preview.js";
|
|
17
|
+
export * from "./State/event-buffer.js";
|
|
18
|
+
export * from "./Message/process-message.js";
|
|
19
|
+
export * from "./Message/rich-message-utils.js";
|
|
20
|
+
export * from "./Message/message-retry-manager.js";
|
|
21
|
+
export * from "./Common/browser-utils.js";
|
|
22
|
+
export * from "./Auth/companion-reg-client-utils.js";
|
|
23
|
+
export * from "./State/identity-change-handler.js";
|
|
24
|
+
export * from "./Message/stanza-ack.js";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export * from './Common/generics.js';
|
|
2
|
+
export * from './Message/decode-wa-message.js';
|
|
3
|
+
export * from './Message/messages.js';
|
|
4
|
+
export * from './Message/messages-media.js';
|
|
5
|
+
export * from './Auth/validate-connection.js';
|
|
6
|
+
export * from './Crypto/crypto.js';
|
|
7
|
+
export * from './Crypto/signal.js';
|
|
8
|
+
export * from './Crypto/noise-handler.js';
|
|
9
|
+
export * from './Message/history.js';
|
|
10
|
+
export * from './State/chat-utils.js';
|
|
11
|
+
export * from './Crypto/lt-hash.js';
|
|
12
|
+
export * from './Auth/auth-utils.js';
|
|
13
|
+
export * from './Auth/use-multi-file-auth-state.js';
|
|
14
|
+
export * from './Auth/use-single-file-auth-state.js';
|
|
15
|
+
export * from './Auth/use-sqlite-auth-state.js';
|
|
16
|
+
export * from './Message/link-preview.js';
|
|
17
|
+
export * from './State/event-buffer.js';
|
|
18
|
+
export * from './Message/process-message.js';
|
|
19
|
+
export * from './Message/rich-message-utils.js';
|
|
20
|
+
export * from './Message/message-retry-manager.js';
|
|
21
|
+
export * from './Common/browser-utils.js';
|
|
22
|
+
export * from './Auth/companion-reg-client-utils.js';
|
|
23
|
+
export * from './State/identity-change-handler.js';
|
|
24
|
+
export * from './Message/stanza-ack.js';
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export namespace TAGS {
|
|
2
|
+
let LIST_EMPTY: number;
|
|
3
|
+
let DICTIONARY_0: number;
|
|
4
|
+
let DICTIONARY_1: number;
|
|
5
|
+
let DICTIONARY_2: number;
|
|
6
|
+
let DICTIONARY_3: number;
|
|
7
|
+
let INTEROP_JID: number;
|
|
8
|
+
let FB_JID: number;
|
|
9
|
+
let AD_JID: number;
|
|
10
|
+
let LIST_8: number;
|
|
11
|
+
let LIST_16: number;
|
|
12
|
+
let JID_PAIR: number;
|
|
13
|
+
let HEX_8: number;
|
|
14
|
+
let BINARY_8: number;
|
|
15
|
+
let BINARY_20: number;
|
|
16
|
+
let BINARY_32: number;
|
|
17
|
+
let NIBBLE_8: number;
|
|
18
|
+
let PACKED_MAX: number;
|
|
19
|
+
}
|
|
20
|
+
export const DOUBLE_BYTE_TOKENS: string[][];
|
|
21
|
+
export const SINGLE_BYTE_TOKENS: string[];
|
|
22
|
+
export const TOKEN_MAP: {};
|
|
23
|
+
export const CPP_KEYWORDS: Set<string>;
|
|
24
|
+
export const CSS_KEYWORDS: Set<string>;
|
|
25
|
+
export const GO_KEYWORDS: Set<string>;
|
|
26
|
+
export const HTML_KEYWORDS: Set<string>;
|
|
27
|
+
export const JS_KEYWORDS: Set<string>;
|
|
28
|
+
export const PYTHON_KEYWORDS: Set<string>;
|
|
29
|
+
export const RUST_KEYWORDS: Set<string>;
|
|
30
|
+
export const C_KEYWORDS: Set<string>;
|
|
31
|
+
export const CSHARP_KEYWORDS: Set<string>;
|
|
32
|
+
export const BASH_KEYWORDS: Set<string>;
|
|
33
|
+
export const CMD_KEYWORDS: Set<string>;
|
|
34
|
+
export const POWERSHELL_KEYWORDS: Set<string>;
|
|
35
|
+
export const LANGUAGE_KEYWORDS: {
|
|
36
|
+
css: Set<string>;
|
|
37
|
+
html: Set<string>;
|
|
38
|
+
javascript: Set<string>;
|
|
39
|
+
typescript: Set<string>;
|
|
40
|
+
js: Set<string>;
|
|
41
|
+
ts: Set<string>;
|
|
42
|
+
python: Set<string>;
|
|
43
|
+
py: Set<string>;
|
|
44
|
+
go: Set<string>;
|
|
45
|
+
golang: Set<string>;
|
|
46
|
+
cpp: Set<string>;
|
|
47
|
+
'c++': Set<string>;
|
|
48
|
+
rust: Set<string>;
|
|
49
|
+
rs: Set<string>;
|
|
50
|
+
c: Set<string>;
|
|
51
|
+
h: Set<string>;
|
|
52
|
+
csharp: Set<string>;
|
|
53
|
+
cs: Set<string>;
|
|
54
|
+
bash: Set<string>;
|
|
55
|
+
sh: Set<string>;
|
|
56
|
+
zsh: Set<string>;
|
|
57
|
+
cmd: Set<string>;
|
|
58
|
+
bat: Set<string>;
|
|
59
|
+
powershell: Set<string>;
|
|
60
|
+
ps1: Set<string>;
|
|
61
|
+
};
|