@itsliaaa/baileys 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 +22 -0
- package/README.md +1078 -0
- package/WAProto/index.js +100441 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/index.js +144 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.js +29 -0
- package/lib/Signal/Group/group_cipher.js +81 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.js +17 -0
- package/lib/Signal/Group/sender-chain-key.js +25 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
- package/lib/Signal/Group/sender-key-message.js +65 -0
- package/lib/Signal/Group/sender-key-name.js +47 -0
- package/lib/Signal/Group/sender-key-record.js +40 -0
- package/lib/Signal/Group/sender-key-state.js +83 -0
- package/lib/Signal/Group/sender-message-key.js +25 -0
- package/lib/Signal/libsignal.js +402 -0
- package/lib/Signal/lid-mapping.js +270 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +53 -0
- package/lib/Socket/business.js +378 -0
- package/lib/Socket/chats.js +1048 -0
- package/lib/Socket/communities.js +430 -0
- package/lib/Socket/groups.js +328 -0
- package/lib/Socket/index.js +11 -0
- package/lib/Socket/messages-recv.js +1442 -0
- package/lib/Socket/messages-send.js +1153 -0
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter.js +227 -0
- package/lib/Socket/socket.js +936 -0
- package/lib/Store/index.js +3 -0
- package/lib/Store/make-in-memory-store.js +421 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.js +17 -0
- package/lib/Types/Newsletter.js +33 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +12 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.js +25 -0
- package/lib/Utils/auth-utils.js +289 -0
- package/lib/Utils/browser-utils.js +28 -0
- package/lib/Utils/business.js +230 -0
- package/lib/Utils/chat-utils.js +811 -0
- package/lib/Utils/crypto.js +117 -0
- package/lib/Utils/decode-wa-message.js +282 -0
- package/lib/Utils/event-buffer.js +573 -0
- package/lib/Utils/generics.js +385 -0
- package/lib/Utils/history.js +130 -0
- package/lib/Utils/identity-change-handler.js +48 -0
- package/lib/Utils/index.js +19 -0
- package/lib/Utils/link-preview.js +84 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.js +7 -0
- package/lib/Utils/make-mutex.js +32 -0
- package/lib/Utils/message-retry-manager.js +224 -0
- package/lib/Utils/messages-media.js +789 -0
- package/lib/Utils/messages.js +1832 -0
- package/lib/Utils/noise-handler.js +200 -0
- package/lib/Utils/pre-key-manager.js +105 -0
- package/lib/Utils/process-message.js +527 -0
- package/lib/Utils/reporting-utils.js +257 -0
- package/lib/Utils/signal.js +158 -0
- package/lib/Utils/sync-action-utils.js +47 -0
- package/lib/Utils/tc-token-utils.js +17 -0
- package/lib/Utils/use-multi-file-auth-state.js +120 -0
- package/lib/Utils/validate-connection.js +206 -0
- package/lib/WABinary/constants.js +1300 -0
- package/lib/WABinary/decode.js +261 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.js +197 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.js +95 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.js +22852 -0
- package/lib/WAM/encode.js +149 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
- package/lib/WAUSync/Protocols/index.js +4 -0
- package/lib/WAUSync/USyncQuery.js +93 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +11 -0
- package/package.json +72 -0
|
@@ -0,0 +1,257 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { KEY_BUNDLE_TYPE } from '../Defaults/index.js';
|
|
2
|
+
import { assertNodeErrorFree, getBinaryNodeChild, getBinaryNodeChildBuffer, getBinaryNodeChildren, getBinaryNodeChildUInt, getServerFromDomainType, jidDecode, S_WHATSAPP_NET, WAJIDDomains } from '../WABinary/index.js';
|
|
3
|
+
import { Curve, generateSignalPubKey } from './crypto.js';
|
|
4
|
+
import { encodeBigEndian } from './generics.js';
|
|
5
|
+
function chunk(array, size) {
|
|
6
|
+
const chunks = [];
|
|
7
|
+
for (let i = 0; i < array.length; i += size) {
|
|
8
|
+
chunks.push(array.slice(i, i + size));
|
|
9
|
+
}
|
|
10
|
+
return chunks;
|
|
11
|
+
}
|
|
12
|
+
export const createSignalIdentity = (wid, accountSignatureKey) => {
|
|
13
|
+
return {
|
|
14
|
+
identifier: { name: wid, deviceId: 0 },
|
|
15
|
+
identifierKey: generateSignalPubKey(accountSignatureKey)
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export const getPreKeys = async ({ get }, min, limit) => {
|
|
19
|
+
const idList = [];
|
|
20
|
+
for (let id = min; id < limit; id++) {
|
|
21
|
+
idList.push(id.toString());
|
|
22
|
+
}
|
|
23
|
+
return get('pre-key', idList);
|
|
24
|
+
};
|
|
25
|
+
export const generateOrGetPreKeys = (creds, range) => {
|
|
26
|
+
const avaliable = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId;
|
|
27
|
+
const remaining = range - avaliable;
|
|
28
|
+
const lastPreKeyId = creds.nextPreKeyId + remaining - 1;
|
|
29
|
+
const newPreKeys = {};
|
|
30
|
+
if (remaining > 0) {
|
|
31
|
+
for (let i = creds.nextPreKeyId; i <= lastPreKeyId; i++) {
|
|
32
|
+
newPreKeys[i] = Curve.generateKeyPair();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
newPreKeys,
|
|
37
|
+
lastPreKeyId,
|
|
38
|
+
preKeysRange: [creds.firstUnuploadedPreKeyId, range]
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
export const xmppSignedPreKey = (key) => ({
|
|
42
|
+
tag: 'skey',
|
|
43
|
+
attrs: {},
|
|
44
|
+
content: [
|
|
45
|
+
{ tag: 'id', attrs: {}, content: encodeBigEndian(key.keyId, 3) },
|
|
46
|
+
{ tag: 'value', attrs: {}, content: key.keyPair.public },
|
|
47
|
+
{ tag: 'signature', attrs: {}, content: key.signature }
|
|
48
|
+
]
|
|
49
|
+
});
|
|
50
|
+
export const xmppPreKey = (pair, id) => ({
|
|
51
|
+
tag: 'key',
|
|
52
|
+
attrs: {},
|
|
53
|
+
content: [
|
|
54
|
+
{ tag: 'id', attrs: {}, content: encodeBigEndian(id, 3) },
|
|
55
|
+
{ tag: 'value', attrs: {}, content: pair.public }
|
|
56
|
+
]
|
|
57
|
+
});
|
|
58
|
+
export const parseAndInjectE2ESessions = async (node, repository) => {
|
|
59
|
+
const extractKey = (key) => key
|
|
60
|
+
? {
|
|
61
|
+
keyId: getBinaryNodeChildUInt(key, 'id', 3),
|
|
62
|
+
publicKey: generateSignalPubKey(getBinaryNodeChildBuffer(key, 'value')),
|
|
63
|
+
signature: getBinaryNodeChildBuffer(key, 'signature')
|
|
64
|
+
}
|
|
65
|
+
: undefined;
|
|
66
|
+
const nodes = getBinaryNodeChildren(getBinaryNodeChild(node, 'list'), 'user');
|
|
67
|
+
for (const node of nodes) {
|
|
68
|
+
assertNodeErrorFree(node);
|
|
69
|
+
}
|
|
70
|
+
// Most of the work in repository.injectE2ESession is CPU intensive, not IO
|
|
71
|
+
// So Promise.all doesn't really help here,
|
|
72
|
+
// but blocks even loop if we're using it inside keys.transaction, and it makes it "sync" actually
|
|
73
|
+
// This way we chunk it in smaller parts and between those parts we can yield to the event loop
|
|
74
|
+
// It's rare case when you need to E2E sessions for so many users, but it's possible
|
|
75
|
+
const chunkSize = 100;
|
|
76
|
+
const chunks = chunk(nodes, chunkSize);
|
|
77
|
+
for (const nodesChunk of chunks) {
|
|
78
|
+
for (const node of nodesChunk) {
|
|
79
|
+
const signedKey = getBinaryNodeChild(node, 'skey');
|
|
80
|
+
const key = getBinaryNodeChild(node, 'key');
|
|
81
|
+
const identity = getBinaryNodeChildBuffer(node, 'identity');
|
|
82
|
+
const jid = node.attrs.jid;
|
|
83
|
+
const registrationId = getBinaryNodeChildUInt(node, 'registration', 4);
|
|
84
|
+
await repository.injectE2ESession({
|
|
85
|
+
jid,
|
|
86
|
+
session: {
|
|
87
|
+
registrationId: registrationId,
|
|
88
|
+
identityKey: generateSignalPubKey(identity),
|
|
89
|
+
signedPreKey: extractKey(signedKey),
|
|
90
|
+
preKey: extractKey(key)
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
export const extractDeviceJids = (result, myJid, myLid, excludeZeroDevices) => {
|
|
97
|
+
const { user: myUser, device: myDevice } = jidDecode(myJid);
|
|
98
|
+
const extracted = [];
|
|
99
|
+
for (const userResult of result) {
|
|
100
|
+
const { devices, id } = userResult;
|
|
101
|
+
const decoded = jidDecode(id), { user, server } = decoded;
|
|
102
|
+
let { domainType } = decoded;
|
|
103
|
+
const deviceList = devices?.deviceList;
|
|
104
|
+
if (!Array.isArray(deviceList))
|
|
105
|
+
continue;
|
|
106
|
+
for (const { id: device, keyIndex, isHosted } of deviceList) {
|
|
107
|
+
if ((!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
|
|
108
|
+
((myUser !== user && myLid !== user) || myDevice !== device) && // either different user or if me user, not this device
|
|
109
|
+
(device === 0 || !!keyIndex) // ensure that "key-index" is specified for "non-zero" devices, produces a bad req otherwise
|
|
110
|
+
) {
|
|
111
|
+
if (isHosted) {
|
|
112
|
+
domainType = domainType === WAJIDDomains.LID ? WAJIDDomains.HOSTED_LID : WAJIDDomains.HOSTED;
|
|
113
|
+
}
|
|
114
|
+
extracted.push({
|
|
115
|
+
user,
|
|
116
|
+
device,
|
|
117
|
+
domainType,
|
|
118
|
+
server: getServerFromDomainType(server, domainType)
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return extracted;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* get the next N keys for upload or processing
|
|
127
|
+
* @param count number of pre-keys to get or generate
|
|
128
|
+
*/
|
|
129
|
+
export const getNextPreKeys = async ({ creds, keys }, count) => {
|
|
130
|
+
const { newPreKeys, lastPreKeyId, preKeysRange } = generateOrGetPreKeys(creds, count);
|
|
131
|
+
const update = {
|
|
132
|
+
nextPreKeyId: Math.max(lastPreKeyId + 1, creds.nextPreKeyId),
|
|
133
|
+
firstUnuploadedPreKeyId: Math.max(creds.firstUnuploadedPreKeyId, lastPreKeyId + 1)
|
|
134
|
+
};
|
|
135
|
+
await keys.set({ 'pre-key': newPreKeys });
|
|
136
|
+
const preKeys = await getPreKeys(keys, preKeysRange[0], preKeysRange[0] + preKeysRange[1]);
|
|
137
|
+
return { update, preKeys };
|
|
138
|
+
};
|
|
139
|
+
export const getNextPreKeysNode = async (state, count) => {
|
|
140
|
+
const { creds } = state;
|
|
141
|
+
const { update, preKeys } = await getNextPreKeys(state, count);
|
|
142
|
+
const node = {
|
|
143
|
+
tag: 'iq',
|
|
144
|
+
attrs: {
|
|
145
|
+
xmlns: 'encrypt',
|
|
146
|
+
type: 'set',
|
|
147
|
+
to: S_WHATSAPP_NET
|
|
148
|
+
},
|
|
149
|
+
content: [
|
|
150
|
+
{ tag: 'registration', attrs: {}, content: encodeBigEndian(creds.registrationId) },
|
|
151
|
+
{ tag: 'type', attrs: {}, content: KEY_BUNDLE_TYPE },
|
|
152
|
+
{ tag: 'identity', attrs: {}, content: creds.signedIdentityKey.public },
|
|
153
|
+
{ tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k => xmppPreKey(preKeys[+k], +k)) },
|
|
154
|
+
xmppSignedPreKey(creds.signedPreKey)
|
|
155
|
+
]
|
|
156
|
+
};
|
|
157
|
+
return { update, node };
|
|
158
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
lid: lidJid || undefined,
|
|
25
|
+
phoneNumber
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
});
|
|
29
|
+
// Emit lid-mapping.update if we have valid LID-PN pair
|
|
30
|
+
if (lidJid && isLidUser(lidJid) && idIsPn) {
|
|
31
|
+
results.push({
|
|
32
|
+
event: 'lid-mapping.update',
|
|
33
|
+
data: { lid: lidJid, pn: id }
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return results;
|
|
37
|
+
};
|
|
38
|
+
export const emitSyncActionResults = (ev, results) => {
|
|
39
|
+
for (const result of results) {
|
|
40
|
+
if (result.event === 'contacts.upsert') {
|
|
41
|
+
ev.emit('contacts.upsert', result.data);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
ev.emit('lid-mapping.update', result.data);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export async function buildTcTokenFromJid({ authState, jid, baseContent = [] }) {
|
|
2
|
+
try {
|
|
3
|
+
const tcTokenData = await authState.keys.get('tctoken', [jid]);
|
|
4
|
+
const tcTokenBuffer = tcTokenData?.[jid]?.token;
|
|
5
|
+
if (!tcTokenBuffer)
|
|
6
|
+
return baseContent.length > 0 ? baseContent : undefined;
|
|
7
|
+
baseContent.push({
|
|
8
|
+
tag: 'tctoken',
|
|
9
|
+
attrs: {},
|
|
10
|
+
content: tcTokenBuffer
|
|
11
|
+
});
|
|
12
|
+
return baseContent;
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
return baseContent.length > 0 ? baseContent : undefined;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Mutex } from 'async-mutex';
|
|
2
|
+
import { mkdir, readFile, stat, unlink, writeFile } from 'fs/promises';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { proto } from '../../WAProto/index.js';
|
|
5
|
+
import { initAuthCreds } from './auth-utils.js';
|
|
6
|
+
import { BufferJSON } from './generics.js';
|
|
7
|
+
// We need to lock files due to the fact that we are using async functions to read and write files
|
|
8
|
+
// https://github.com/WhiskeySockets/Baileys/issues/794
|
|
9
|
+
// https://github.com/nodejs/node/issues/26338
|
|
10
|
+
// Use a Map to store mutexes for each file path
|
|
11
|
+
const fileLocks = new Map();
|
|
12
|
+
// Get or create a mutex for a specific file path
|
|
13
|
+
const getFileLock = (path) => {
|
|
14
|
+
let mutex = fileLocks.get(path);
|
|
15
|
+
if (!mutex) {
|
|
16
|
+
mutex = new Mutex();
|
|
17
|
+
fileLocks.set(path, mutex);
|
|
18
|
+
}
|
|
19
|
+
return mutex;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* stores the full authentication state in a single folder.
|
|
23
|
+
* Far more efficient than singlefileauthstate
|
|
24
|
+
*
|
|
25
|
+
* Again, I wouldn't endorse this for any production level use other than perhaps a bot.
|
|
26
|
+
* Would recommend writing an auth state for use with a proper SQL or No-SQL DB
|
|
27
|
+
* */
|
|
28
|
+
export const useMultiFileAuthState = async (folder) => {
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
const writeData = async (data, file) => {
|
|
31
|
+
const filePath = join(folder, fixFileName(file));
|
|
32
|
+
const mutex = getFileLock(filePath);
|
|
33
|
+
return mutex.acquire().then(async (release) => {
|
|
34
|
+
try {
|
|
35
|
+
await writeFile(filePath, JSON.stringify(data, BufferJSON.replacer));
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
release();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
const readData = async (file) => {
|
|
43
|
+
try {
|
|
44
|
+
const filePath = join(folder, fixFileName(file));
|
|
45
|
+
const mutex = getFileLock(filePath);
|
|
46
|
+
return await mutex.acquire().then(async (release) => {
|
|
47
|
+
try {
|
|
48
|
+
const data = await readFile(filePath, { encoding: 'utf-8' });
|
|
49
|
+
return JSON.parse(data, BufferJSON.reviver);
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
release();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const removeData = async (file) => {
|
|
61
|
+
try {
|
|
62
|
+
const filePath = join(folder, fixFileName(file));
|
|
63
|
+
const mutex = getFileLock(filePath);
|
|
64
|
+
return mutex.acquire().then(async (release) => {
|
|
65
|
+
try {
|
|
66
|
+
await unlink(filePath);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
release();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
catch { }
|
|
76
|
+
};
|
|
77
|
+
const folderInfo = await stat(folder).catch(() => { });
|
|
78
|
+
if (folderInfo) {
|
|
79
|
+
if (!folderInfo.isDirectory()) {
|
|
80
|
+
throw new Error(`found something that is not a directory at ${folder}, either delete it or specify a different location`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
await mkdir(folder, { recursive: true });
|
|
85
|
+
}
|
|
86
|
+
const fixFileName = (file) => file?.replace(/\//g, '__')?.replace(/:/g, '-');
|
|
87
|
+
const creds = (await readData('creds.json')) || initAuthCreds();
|
|
88
|
+
return {
|
|
89
|
+
state: {
|
|
90
|
+
creds,
|
|
91
|
+
keys: {
|
|
92
|
+
get: async (type, ids) => {
|
|
93
|
+
const data = {};
|
|
94
|
+
await Promise.all(ids.map(async (id) => {
|
|
95
|
+
let value = await readData(`${type}-${id}.json`);
|
|
96
|
+
if (type === 'app-state-sync-key' && value) {
|
|
97
|
+
value = proto.Message.AppStateSyncKeyData.fromObject(value);
|
|
98
|
+
}
|
|
99
|
+
data[id] = value;
|
|
100
|
+
}));
|
|
101
|
+
return data;
|
|
102
|
+
},
|
|
103
|
+
set: async (data) => {
|
|
104
|
+
const tasks = [];
|
|
105
|
+
for (const category in data) {
|
|
106
|
+
for (const id in data[category]) {
|
|
107
|
+
const value = data[category][id];
|
|
108
|
+
const file = `${category}-${id}.json`;
|
|
109
|
+
tasks.push(value ? writeData(value, file) : removeData(file));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
await Promise.all(tasks);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
saveCreds: async () => {
|
|
117
|
+
return writeData(creds, 'creds.json');
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
};
|