@kelvdra/baileys 1.0.4 → 1.0.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 (42) hide show
  1. package/LICENSE +21 -0
  2. package/WAProto/index.js +65472 -137440
  3. package/lib/Defaults/index.d.ts +1 -1
  4. package/lib/Defaults/index.js +22 -3
  5. package/lib/Socket/chats.js +12 -13
  6. package/lib/Socket/groups.js +140 -7
  7. package/lib/Socket/hydra.js +44 -0
  8. package/lib/Socket/messages-recv.js +736 -324
  9. package/lib/Socket/messages-send.js +481 -110
  10. package/lib/Socket/mex.js +44 -6
  11. package/lib/Socket/newsletter.d.ts +16 -9
  12. package/lib/Socket/newsletter.js +259 -70
  13. package/lib/Types/Mex.d.ts +141 -0
  14. package/lib/Types/Mex.js +37 -0
  15. package/lib/Types/State.js +54 -1
  16. package/lib/Utils/auth-utils.js +12 -1
  17. package/lib/Utils/chat-utils.js +36 -2
  18. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  19. package/lib/Utils/companion-reg-client-utils.js +35 -0
  20. package/lib/Utils/decode-wa-message.js +23 -4
  21. package/lib/Utils/generics.js +4 -1
  22. package/lib/Utils/identity-change-handler.d.ts +44 -0
  23. package/lib/Utils/identity-change-handler.js +50 -0
  24. package/lib/Utils/index.js +1 -1
  25. package/lib/Utils/message-retry-manager.js +25 -1
  26. package/lib/Utils/messages-media.js +162 -43
  27. package/lib/Utils/messages.d.ts +1 -1
  28. package/lib/Utils/messages.js +230 -9
  29. package/lib/Utils/offline-node-processor.d.ts +17 -0
  30. package/lib/Utils/offline-node-processor.js +40 -0
  31. package/lib/Utils/reporting-utils.d.ts +11 -0
  32. package/lib/Utils/reporting-utils.js +258 -0
  33. package/lib/Utils/signal.js +45 -1
  34. package/lib/Utils/stanza-ack.d.ts +11 -0
  35. package/lib/Utils/stanza-ack.js +38 -0
  36. package/lib/Utils/sync-action-utils.d.ts +19 -0
  37. package/lib/Utils/sync-action-utils.js +49 -0
  38. package/lib/Utils/tc-token-utils.d.ts +37 -0
  39. package/lib/Utils/tc-token-utils.js +163 -0
  40. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  41. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
  42. package/package.json +3 -1
@@ -0,0 +1,258 @@
1
+ import { createHmac } from 'crypto';
2
+ import { proto } from '../../WAProto/index.js';
3
+ import { hkdf } from './crypto.js';
4
+ const reportingFields = [
5
+ { f: 1 },
6
+ {
7
+ f: 3,
8
+ s: [{ f: 2 }, { f: 3 }, { f: 8 }, { f: 11 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 25 }]
9
+ },
10
+ { f: 4, s: [{ f: 1 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
11
+ { f: 5, s: [{ f: 3 }, { f: 4 }, { f: 5 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
12
+ { f: 6, s: [{ f: 1 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 30 }] },
13
+ { f: 7, s: [{ f: 2 }, { f: 7 }, { f: 10 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
14
+ { f: 8, s: [{ f: 2 }, { f: 7 }, { f: 9 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 21 }] },
15
+ { f: 9, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
16
+ { f: 12, s: [{ f: 1 }, { f: 2 }, { f: 14, m: true }, { f: 15 }] },
17
+ { f: 18, s: [{ f: 6 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
18
+ { f: 26, s: [{ f: 4 }, { f: 5 }, { f: 8 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
19
+ { f: 28, s: [{ f: 1 }, { f: 2 }, { f: 4 }, { f: 5 }, { f: 6 }, { f: 7, s: [{ f: 21 }, { f: 22 }] }] },
20
+ { f: 37, s: [{ f: 1, m: true }] },
21
+ {
22
+ f: 49,
23
+ s: [
24
+ { f: 2 },
25
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
26
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
27
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
28
+ ]
29
+ },
30
+ { f: 53, s: [{ f: 1, m: true }] },
31
+ { f: 55, s: [{ f: 1, m: true }] },
32
+ { f: 58, s: [{ f: 1, m: true }] },
33
+ { f: 59, s: [{ f: 1, m: true }] },
34
+ {
35
+ f: 60,
36
+ s: [
37
+ { f: 2 },
38
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
39
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
40
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
41
+ ]
42
+ },
43
+ {
44
+ f: 64,
45
+ s: [
46
+ { f: 2 },
47
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
48
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
49
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
50
+ ]
51
+ },
52
+ { f: 66, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
53
+ { f: 74, s: [{ f: 1, m: true }] },
54
+ { f: 87, s: [{ f: 1, m: true }] },
55
+ { f: 88, s: [{ f: 1 }, { f: 2, s: [{ f: 1 }] }, { f: 3, s: [{ f: 21 }, { f: 22 }] }] },
56
+ { f: 92, s: [{ f: 1, m: true }] },
57
+ { f: 93, s: [{ f: 1, m: true }] },
58
+ { f: 94, s: [{ f: 1, m: true }] }
59
+ ];
60
+ const compileReportingFields = (fields) => {
61
+ const map = new Map();
62
+ for (const f of fields) {
63
+ map.set(f.f, {
64
+ m: f.m,
65
+ children: f.s ? compileReportingFields(f.s) : undefined
66
+ });
67
+ }
68
+ return map;
69
+ };
70
+ const compiledReportingFields = compileReportingFields(reportingFields);
71
+ const EMPTY_MAP = new Map();
72
+ const ENC_SECRET_REPORT_TOKEN = 'Report Token';
73
+ const WIRE = {
74
+ VARINT: 0,
75
+ FIXED64: 1,
76
+ BYTES: 2,
77
+ FIXED32: 5
78
+ };
79
+ export const shouldIncludeReportingToken = (message) => !message.reactionMessage &&
80
+ !message.encReactionMessage &&
81
+ !message.encEventResponseMessage &&
82
+ !message.pollUpdateMessage;
83
+ const generateMsgSecretKey = (modificationType, origMsgId, origMsgSender, modificationSender, origMsgSecret) => {
84
+ const useCaseSecret = Buffer.concat([
85
+ Buffer.from(origMsgId, 'utf8'),
86
+ Buffer.from(origMsgSender, 'utf8'),
87
+ Buffer.from(modificationSender, 'utf8'),
88
+ Buffer.from(modificationType, 'utf8')
89
+ ]);
90
+ return hkdf(origMsgSecret, 32, { info: useCaseSecret.toString('latin1') });
91
+ };
92
+ const extractReportingTokenContent = (data, cfg) => {
93
+ const out = [];
94
+ let i = 0;
95
+ while (i < data.length) {
96
+ const tag = decodeVarint(data, i);
97
+ if (!tag.ok) {
98
+ return null;
99
+ }
100
+ const fieldNum = tag.value >> 3;
101
+ const wireType = tag.value & 0x7;
102
+ const fieldStart = i;
103
+ i += tag.bytes;
104
+ const fieldCfg = cfg.get(fieldNum);
105
+ const pushSlice = (end) => {
106
+ if (end > data.length) {
107
+ return false;
108
+ }
109
+ out.push({ num: fieldNum, bytes: data.subarray(fieldStart, end) });
110
+ i = end;
111
+ return true;
112
+ };
113
+ const skip = (end) => {
114
+ if (end > data.length) {
115
+ return false;
116
+ }
117
+ i = end;
118
+ return true;
119
+ };
120
+ if (wireType === WIRE.VARINT) {
121
+ const v = decodeVarint(data, i);
122
+ if (!v.ok) {
123
+ return null;
124
+ }
125
+ const end = i + v.bytes;
126
+ if (!fieldCfg) {
127
+ if (!skip(end)) {
128
+ return null;
129
+ }
130
+ continue;
131
+ }
132
+ if (!pushSlice(end)) {
133
+ return null;
134
+ }
135
+ continue;
136
+ }
137
+ if (wireType === WIRE.FIXED64) {
138
+ const end = i + 8;
139
+ if (!fieldCfg) {
140
+ if (!skip(end)) {
141
+ return null;
142
+ }
143
+ continue;
144
+ }
145
+ if (!pushSlice(end)) {
146
+ return null;
147
+ }
148
+ continue;
149
+ }
150
+ if (wireType === WIRE.FIXED32) {
151
+ const end = i + 4;
152
+ if (!fieldCfg) {
153
+ if (!skip(end)) {
154
+ return null;
155
+ }
156
+ continue;
157
+ }
158
+ if (!pushSlice(end)) {
159
+ return null;
160
+ }
161
+ continue;
162
+ }
163
+ if (wireType === WIRE.BYTES) {
164
+ const len = decodeVarint(data, i);
165
+ if (!len.ok) {
166
+ return null;
167
+ }
168
+ const valStart = i + len.bytes;
169
+ const valEnd = valStart + len.value;
170
+ if (valEnd > data.length) {
171
+ return null;
172
+ }
173
+ if (!fieldCfg) {
174
+ i = valEnd;
175
+ continue;
176
+ }
177
+ if (fieldCfg.m || fieldCfg.children) {
178
+ const sub = extractReportingTokenContent(data.subarray(valStart, valEnd), fieldCfg.children ?? EMPTY_MAP);
179
+ if (sub === null) {
180
+ return null;
181
+ }
182
+ if (sub.length > 0) {
183
+ const newTag = encodeVarint(tag.value);
184
+ const newLen = encodeVarint(sub.length);
185
+ out.push({
186
+ num: fieldNum,
187
+ bytes: Buffer.concat([newTag, newLen, sub])
188
+ });
189
+ }
190
+ i = valEnd;
191
+ continue;
192
+ }
193
+ out.push({ num: fieldNum, bytes: data.subarray(fieldStart, valEnd) });
194
+ i = valEnd;
195
+ continue;
196
+ }
197
+ return null;
198
+ }
199
+ if (out.length === 0) {
200
+ return Buffer.alloc(0);
201
+ }
202
+ out.sort((a, b) => a.num - b.num);
203
+ return Buffer.concat(out.map(f => f.bytes));
204
+ };
205
+ const decodeVarint = (buffer, offset) => {
206
+ let value = 0;
207
+ let bytes = 0;
208
+ let shift = 0;
209
+ while (offset + bytes < buffer.length) {
210
+ const current = buffer[offset + bytes];
211
+ value |= (current & 0x7f) << shift;
212
+ bytes++;
213
+ if ((current & 0x80) === 0) {
214
+ return { value, bytes, ok: true };
215
+ }
216
+ shift += 7;
217
+ if (shift > 35) {
218
+ return { value: 0, bytes: 0, ok: false };
219
+ }
220
+ }
221
+ return { value: 0, bytes: 0, ok: false };
222
+ };
223
+ const encodeVarint = (value) => {
224
+ const parts = [];
225
+ let remaining = value >>> 0;
226
+ while (remaining > 0x7f) {
227
+ parts.push((remaining & 0x7f) | 0x80);
228
+ remaining >>>= 7;
229
+ }
230
+ parts.push(remaining);
231
+ return Buffer.from(parts);
232
+ };
233
+ export const getMessageReportingToken = async (msgProtobuf, message, key) => {
234
+ const msgSecret = message.messageContextInfo?.messageSecret;
235
+ if (!msgSecret || !key.id) {
236
+ return null;
237
+ }
238
+ const from = key.fromMe ? key.remoteJid : key.participant || key.remoteJid;
239
+ const to = key.fromMe ? key.participant || key.remoteJid : key.remoteJid;
240
+ const reportingSecret = generateMsgSecretKey(ENC_SECRET_REPORT_TOKEN, key.id, from, to, msgSecret);
241
+ const content = extractReportingTokenContent(msgProtobuf, compiledReportingFields);
242
+ if (!content || content.length === 0) {
243
+ return null;
244
+ }
245
+ const reportingToken = createHmac('sha256', reportingSecret).update(content).digest().subarray(0, 16);
246
+ return {
247
+ tag: 'reporting',
248
+ attrs: {},
249
+ content: [
250
+ {
251
+ tag: 'reporting_token',
252
+ attrs: { v: '2' },
253
+ content: reportingToken
254
+ }
255
+ ]
256
+ };
257
+ };
258
+ //# sourceMappingURL=reporting-utils.js.map
@@ -156,4 +156,48 @@ export const getNextPreKeysNode = async (state, count) => {
156
156
  };
157
157
  return { update, node };
158
158
  };
159
- //# sourceMappingURL=signal.js.map
159
+ //# sourceMappingURL=signal.js.map
160
+
161
+ // Added from Baileys v7.0.0-rc10
162
+ export const extractE2ESessionFromRetryReceipt = (receipt) => {
163
+ const keysNode = getBinaryNodeChild(receipt, 'keys');
164
+ if (!keysNode)
165
+ return null;
166
+ const typeBuf = getBinaryNodeChildBuffer(keysNode, 'type');
167
+ if (!typeBuf || typeBuf.length !== 1 || typeBuf[0] !== KEY_BUNDLE_TYPE[0])
168
+ return null;
169
+ const identity = getBinaryNodeChildBuffer(keysNode, 'identity');
170
+ const skey = getBinaryNodeChild(keysNode, 'skey');
171
+ if (!identity || identity.length !== 32 || !skey)
172
+ return null;
173
+ const registrationId = getBinaryNodeChildUInt(receipt, 'registration', 4);
174
+ if (!isValidUInt(registrationId))
175
+ return null;
176
+ const signedPubKey = getBinaryNodeChildBuffer(skey, 'value');
177
+ const signedSig = getBinaryNodeChildBuffer(skey, 'signature');
178
+ const signedKeyId = getBinaryNodeChildUInt(skey, 'id', 3);
179
+ if (!signedPubKey || signedPubKey.length !== 32 || !signedSig || !isValidUInt(signedKeyId)) {
180
+ return null;
181
+ }
182
+ const preKeyNode = getBinaryNodeChild(keysNode, 'key');
183
+ let preKey;
184
+ if (preKeyNode) {
185
+ const preKeyPub = getBinaryNodeChildBuffer(preKeyNode, 'value');
186
+ const preKeyId = getBinaryNodeChildUInt(preKeyNode, 'id', 3);
187
+ if (!preKeyPub || preKeyPub.length !== 32 || !isValidUInt(preKeyId)) {
188
+ return null;
189
+ }
190
+ preKey = { keyId: preKeyId, publicKey: generateSignalPubKey(preKeyPub) };
191
+ }
192
+ return {
193
+ registrationId,
194
+ identityKey: generateSignalPubKey(identity),
195
+ signedPreKey: {
196
+ keyId: signedKeyId,
197
+ publicKey: generateSignalPubKey(signedPubKey),
198
+ signature: signedSig
199
+ },
200
+ preKey
201
+ };
202
+ };
203
+
@@ -0,0 +1,11 @@
1
+ import type { BinaryNode } from '../WABinary/index.js';
2
+ /**
3
+ * Builds an ACK stanza for a received node.
4
+ * Pure function -- no I/O, no side effects.
5
+ *
6
+ * Mirrors WhatsApp Web's ACK construction:
7
+ * - WAWebHandleMsgSendAck.sendAck / sendNack
8
+ * - WAWebCreateNackFromStanza.createNackFromStanza
9
+ */
10
+ export declare function buildAckStanza(node: BinaryNode, errorCode?: number, meId?: string): BinaryNode;
11
+ //# sourceMappingURL=stanza-ack.d.ts.map
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Builds an ACK stanza for a received node.
3
+ * Pure function -- no I/O, no side effects.
4
+ *
5
+ * Mirrors WhatsApp Web's ACK construction:
6
+ * - WAWebHandleMsgSendAck.sendAck / sendNack
7
+ * - WAWebCreateNackFromStanza.createNackFromStanza
8
+ */
9
+ export function buildAckStanza(node, errorCode, meId) {
10
+ const { tag, attrs } = node;
11
+ const stanza = {
12
+ tag: 'ack',
13
+ attrs: {
14
+ id: attrs.id,
15
+ to: attrs.from,
16
+ class: tag
17
+ }
18
+ };
19
+ if (errorCode) {
20
+ stanza.attrs.error = errorCode.toString();
21
+ }
22
+ if (attrs.participant) {
23
+ stanza.attrs.participant = attrs.participant;
24
+ }
25
+ if (attrs.recipient) {
26
+ stanza.attrs.recipient = attrs.recipient;
27
+ }
28
+ // WA Web always includes type when present: `n.type || DROP_ATTR`
29
+ if (attrs.type) {
30
+ stanza.attrs.type = attrs.type;
31
+ }
32
+ // WA Web WAWebHandleMsgSendAck.sendAck/sendNack always include `from` for message-class ACKs
33
+ if (tag === 'message' && meId) {
34
+ stanza.attrs.from = meId;
35
+ }
36
+ return stanza;
37
+ }
38
+ //# sourceMappingURL=stanza-ack.js.map
@@ -0,0 +1,19 @@
1
+ import { proto } from '../../WAProto/index.js';
2
+ import type { BaileysEventEmitter, BaileysEventMap, Contact } from '../Types/index.js';
3
+ import type { ILogger } from './logger.js';
4
+ export type ContactsUpsertResult = {
5
+ event: 'contacts.upsert';
6
+ data: Contact[];
7
+ };
8
+ export type LidMappingUpdateResult = {
9
+ event: 'lid-mapping.update';
10
+ data: BaileysEventMap['lid-mapping.update'];
11
+ };
12
+ export type SyncActionResult = ContactsUpsertResult | LidMappingUpdateResult;
13
+ /**
14
+ * Process contactAction and return events to emit.
15
+ * Pure function - no side effects.
16
+ */
17
+ export declare const processContactAction: (action: proto.SyncActionValue.IContactAction, id: string | undefined, logger?: ILogger) => SyncActionResult[];
18
+ export declare const emitSyncActionResults: (ev: BaileysEventEmitter, results: SyncActionResult[]) => void;
19
+ //# sourceMappingURL=sync-action-utils.d.ts.map
@@ -0,0 +1,49 @@
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
+ };
49
+ //# sourceMappingURL=sync-action-utils.js.map
@@ -0,0 +1,37 @@
1
+ import type { SignalKeyStoreWithTransaction } from '../Types/index.js';
2
+ import type { BinaryNode } from '../WABinary/index.js';
3
+ /** Sentinel key under `tctoken` store holding a JSON array of tracked storage JIDs for cross-session pruning. */
4
+ export declare const TC_TOKEN_INDEX_KEY = "__index";
5
+ /** Read the persisted tctoken JID index and return its entries (never contains the sentinel key itself). */
6
+ export declare function readTcTokenIndex(keys: SignalKeyStoreWithTransaction): Promise<string[]>;
7
+ /** Build a SignalDataSet fragment that writes the merged index (persisted ∪ added) under the sentinel key. */
8
+ export declare function buildMergedTcTokenIndexWrite(keys: SignalKeyStoreWithTransaction, addedJids: Iterable<string>): Promise<{
9
+ [TC_TOKEN_INDEX_KEY]: {
10
+ token: Buffer;
11
+ };
12
+ }>;
13
+ export declare function isTcTokenExpired(timestamp: number | string | null | undefined): boolean;
14
+ export declare function shouldSendNewTcToken(senderTimestamp: number | undefined): boolean;
15
+ /** Resolve JID to LID for tctoken storage (WA Web stores under LID) */
16
+ export declare function resolveTcTokenJid(jid: string, getLIDForPN: (pn: string) => Promise<string | null>): Promise<string>;
17
+ /** Resolve target JID for issuing privacy token based on AB prop 14303 */
18
+ export declare function resolveIssuanceJid(jid: string, issueToLid: boolean, getLIDForPN: (pn: string) => Promise<string | null>, getPNForLID?: (lid: string) => Promise<string | null>): Promise<string>;
19
+ type TcTokenParams = {
20
+ jid: string;
21
+ baseContent?: BinaryNode[];
22
+ authState: {
23
+ keys: SignalKeyStoreWithTransaction;
24
+ };
25
+ getLIDForPN: (pn: string) => Promise<string | null>;
26
+ };
27
+ export declare function buildTcTokenFromJid({ authState, jid, baseContent, getLIDForPN }: TcTokenParams): Promise<BinaryNode[] | undefined>;
28
+ type StoreTcTokensParams = {
29
+ result: BinaryNode;
30
+ fallbackJid: string;
31
+ keys: SignalKeyStoreWithTransaction;
32
+ getLIDForPN: (pn: string) => Promise<string | null>;
33
+ onNewJidStored?: (jid: string) => void;
34
+ };
35
+ export declare function storeTcTokensFromIqResult({ result, fallbackJid, keys, getLIDForPN, onNewJidStored }: StoreTcTokensParams): Promise<void>;
36
+ export {};
37
+ //# sourceMappingURL=tc-token-utils.d.ts.map
@@ -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
+ if (!tcTokenBuffer?.length || isTcTokenExpired(entry?.timestamp)) {
106
+ if (tcTokenBuffer) {
107
+ // Preserve senderTimestamp so shouldSendNewTcToken() keeps its dedupe state
108
+ // after we drop the unusable peer token. Only wipe the record entirely when
109
+ // there's nothing worth keeping.
110
+ const cleared = entry?.senderTimestamp !== undefined
111
+ ? { token: Buffer.alloc(0), senderTimestamp: entry.senderTimestamp }
112
+ : null;
113
+ await authState.keys.set({ tctoken: { [storageJid]: cleared } });
114
+ }
115
+ return baseContent.length > 0 ? baseContent : undefined;
116
+ }
117
+ baseContent.push({
118
+ tag: 'tctoken',
119
+ attrs: {},
120
+ content: tcTokenBuffer
121
+ });
122
+ return baseContent;
123
+ }
124
+ catch (error) {
125
+ return baseContent.length > 0 ? baseContent : undefined;
126
+ }
127
+ }
128
+ export async function storeTcTokensFromIqResult({ result, fallbackJid, keys, getLIDForPN, onNewJidStored }) {
129
+ const tokensNode = getBinaryNodeChild(result, 'tokens');
130
+ if (!tokensNode)
131
+ return;
132
+ const tokenNodes = getBinaryNodeChildren(tokensNode, 'token');
133
+ for (const tokenNode of tokenNodes) {
134
+ if (tokenNode.attrs.type !== 'trusted_contact' || !(tokenNode.content instanceof Uint8Array)) {
135
+ continue;
136
+ }
137
+ // In notifications tokenNode.attrs.jid is your own device JID, not the sender's
138
+ const rawJid = jidNormalizedUser(fallbackJid || tokenNode.attrs.jid);
139
+ if (!isRegularUser(rawJid))
140
+ continue;
141
+ const storageJid = await resolveTcTokenJid(rawJid, getLIDForPN);
142
+ const existingTcData = await keys.get('tctoken', [storageJid]);
143
+ const existingEntry = existingTcData[storageJid];
144
+ const existingTs = existingEntry?.timestamp ? Number(existingEntry.timestamp) : 0;
145
+ const incomingTs = tokenNode.attrs.t ? Number(tokenNode.attrs.t) : 0;
146
+ // timestamp-less tokens would be immediately expired
147
+ if (!incomingTs)
148
+ continue;
149
+ if (existingTs > 0 && existingTs > incomingTs)
150
+ continue;
151
+ await keys.set({
152
+ tctoken: {
153
+ [storageJid]: {
154
+ ...existingEntry,
155
+ token: Buffer.from(tokenNode.content),
156
+ timestamp: tokenNode.attrs.t
157
+ }
158
+ }
159
+ });
160
+ onNewJidStored?.(storageJid);
161
+ }
162
+ }
163
+ //# sourceMappingURL=tc-token-utils.js.map
@@ -0,0 +1,10 @@
1
+ import type { USyncQueryProtocol } from '../../Types/USync.js';
2
+ import { type BinaryNode } from '../../WABinary/index.js';
3
+ import { USyncUser } from '../USyncUser.js';
4
+ export declare class USyncUsernameProtocol implements USyncQueryProtocol {
5
+ name: string;
6
+ getQueryElement(): BinaryNode;
7
+ getUserElement(user: USyncUser): BinaryNode | null;
8
+ parser(node: BinaryNode): string | null;
9
+ }
10
+ //# sourceMappingURL=USyncUsernameProtocol.d.ts.map
@@ -0,0 +1,25 @@
1
+ import { assertNodeErrorFree } from '../../WABinary/index.js';
2
+ import { USyncUser } from '../USyncUser.js';
3
+ export class USyncUsernameProtocol {
4
+ constructor() {
5
+ this.name = 'username';
6
+ }
7
+ getQueryElement() {
8
+ return {
9
+ tag: 'username',
10
+ attrs: {}
11
+ };
12
+ }
13
+ getUserElement(user) {
14
+ void user;
15
+ return null;
16
+ }
17
+ parser(node) {
18
+ if (node.tag === 'username') {
19
+ assertNodeErrorFree(node);
20
+ return typeof node.content === 'string' ? node.content : null;
21
+ }
22
+ return null;
23
+ }
24
+ }
25
+ //# sourceMappingURL=USyncUsernameProtocol.js.map