@cdteam/cdwa 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 +21 -0
- package/README.md +112 -2
- package/dist/auth/index.cjs +191 -0
- package/dist/auth/index.cjs.map +1 -0
- package/dist/auth/index.d.cts +25 -0
- package/dist/auth/index.d.ts +25 -0
- package/dist/auth/index.js +183 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/binary/index.cjs +414 -0
- package/dist/binary/index.cjs.map +1 -0
- package/dist/binary/index.d.cts +92 -0
- package/dist/binary/index.d.ts +92 -0
- package/dist/binary/index.js +407 -0
- package/dist/binary/index.js.map +1 -0
- package/dist/crypto/index.cjs +207 -0
- package/dist/crypto/index.cjs.map +1 -0
- package/dist/crypto/index.d.cts +91 -0
- package/dist/crypto/index.d.ts +91 -0
- package/dist/crypto/index.js +201 -0
- package/dist/crypto/index.js.map +1 -0
- package/dist/index-CcMoHfPz.d.cts +70 -0
- package/dist/index-CcMoHfPz.d.ts +70 -0
- package/dist/index.cjs +1254 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +97 -0
- package/dist/index.d.ts +97 -0
- package/dist/index.js +1224 -0
- package/dist/index.js.map +1 -0
- package/dist/messages/index.cjs +662 -0
- package/dist/messages/index.cjs.map +1 -0
- package/dist/messages/index.d.cts +122 -0
- package/dist/messages/index.d.ts +122 -0
- package/dist/messages/index.js +659 -0
- package/dist/messages/index.js.map +1 -0
- package/dist/signal/index.cjs +38 -0
- package/dist/signal/index.cjs.map +1 -0
- package/dist/signal/index.d.cts +30 -0
- package/dist/signal/index.d.ts +30 -0
- package/dist/signal/index.js +36 -0
- package/dist/signal/index.js.map +1 -0
- package/package.json +91 -4
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var crypto = require('crypto');
|
|
4
|
+
var sha256 = require('@noble/hashes/sha256');
|
|
5
|
+
var ed25519 = require('@noble/curves/ed25519');
|
|
6
|
+
var hkdf = require('@noble/hashes/hkdf');
|
|
7
|
+
var aes = require('@noble/ciphers/aes');
|
|
8
|
+
|
|
9
|
+
// src/messages/media.ts
|
|
10
|
+
|
|
11
|
+
// src/crypto/index.ts
|
|
12
|
+
var CryptoEngine = class {
|
|
13
|
+
/**
|
|
14
|
+
* Gera um novo par de chaves Curve25519 (Diffie-Hellman)
|
|
15
|
+
*/
|
|
16
|
+
static generateKeyPair() {
|
|
17
|
+
const priv = crypto.randomBytes(32);
|
|
18
|
+
const pub = ed25519.x25519.getPublicKey(priv);
|
|
19
|
+
return {
|
|
20
|
+
private: new Uint8Array(priv),
|
|
21
|
+
public: new Uint8Array(pub)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Executa Diffie-Hellman compartilhado (Shared Secret) usando Curve25519
|
|
26
|
+
*/
|
|
27
|
+
static sharedKey(privateKey, publicKey) {
|
|
28
|
+
return ed25519.x25519.getSharedSecret(privateKey, publicKey);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* HKDF baseado em SHA-256 para derivação segura de chaves (RFC 5869)
|
|
32
|
+
*/
|
|
33
|
+
static deriveKey(secret, info, salt, length = 32) {
|
|
34
|
+
const infoBytes = typeof info === "string" ? new TextEncoder().encode(info) : info;
|
|
35
|
+
return hkdf.hkdf(sha256.sha256, secret, salt, infoBytes, length);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Encriptação autenticada com AES-256-GCM
|
|
39
|
+
*/
|
|
40
|
+
static aesGcmEncrypt(key, plaintext, iv, additionalData) {
|
|
41
|
+
const cipher = aes.gcm(key, iv, additionalData);
|
|
42
|
+
return cipher.encrypt(plaintext);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Decriptação autenticada com AES-256-GCM
|
|
46
|
+
*/
|
|
47
|
+
static aesGcmDecrypt(key, ciphertext, iv, additionalData) {
|
|
48
|
+
const cipher = aes.gcm(key, iv, additionalData);
|
|
49
|
+
return cipher.decrypt(ciphertext);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Encripta um payload com uma senha mestra (para proteger credenciais at-rest)
|
|
53
|
+
*/
|
|
54
|
+
static encryptAtRest(payload, masterPassphrase) {
|
|
55
|
+
const salt = crypto.randomBytes(16);
|
|
56
|
+
const iv = crypto.randomBytes(12);
|
|
57
|
+
const key = hkdf.hkdf(sha256.sha256, new TextEncoder().encode(masterPassphrase), salt, new TextEncoder().encode("cdwa-at-rest"), 32);
|
|
58
|
+
const encrypted = this.aesGcmEncrypt(key, payload, iv);
|
|
59
|
+
return {
|
|
60
|
+
salt: Buffer.from(salt).toString("hex"),
|
|
61
|
+
iv: Buffer.from(iv).toString("hex"),
|
|
62
|
+
data: Buffer.from(encrypted).toString("base64")
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Decripta um payload protegido com a senha mestra
|
|
67
|
+
*/
|
|
68
|
+
static decryptAtRest(encryptedBundle, masterPassphrase) {
|
|
69
|
+
const salt = Buffer.from(encryptedBundle.salt, "hex");
|
|
70
|
+
const iv = Buffer.from(encryptedBundle.iv, "hex");
|
|
71
|
+
const data = Buffer.from(encryptedBundle.data, "base64");
|
|
72
|
+
const key = hkdf.hkdf(sha256.sha256, new TextEncoder().encode(masterPassphrase), salt, new TextEncoder().encode("cdwa-at-rest"), 32);
|
|
73
|
+
return this.aesGcmDecrypt(key, data, iv);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// src/messages/media.ts
|
|
78
|
+
var MEDIA_HKDF_INFO = {
|
|
79
|
+
image: "WhatsApp Image Keys",
|
|
80
|
+
video: "WhatsApp Video Keys",
|
|
81
|
+
audio: "WhatsApp Audio Keys",
|
|
82
|
+
document: "WhatsApp Document Keys"
|
|
83
|
+
};
|
|
84
|
+
var MediaCrypto = class {
|
|
85
|
+
/**
|
|
86
|
+
* Criptografa o arquivo de mídia de acordo com o padrão RFC do WhatsApp
|
|
87
|
+
*/
|
|
88
|
+
static encryptMedia(mediaBuffer, type) {
|
|
89
|
+
const mediaKey = crypto.randomBytes(32);
|
|
90
|
+
const info = MEDIA_HKDF_INFO[type];
|
|
91
|
+
const expandedKeys = CryptoEngine.deriveKey(new Uint8Array(mediaKey), info, void 0, 112);
|
|
92
|
+
const iv = expandedKeys.slice(0, 16);
|
|
93
|
+
const cipherKey = expandedKeys.slice(16, 48);
|
|
94
|
+
const macKey = expandedKeys.slice(48, 80);
|
|
95
|
+
const fileSha256 = sha256.sha256(mediaBuffer);
|
|
96
|
+
const cipher = crypto.createCipheriv("aes-256-cbc", Buffer.from(cipherKey), Buffer.from(iv));
|
|
97
|
+
const encryptedBody = Buffer.concat([cipher.update(Buffer.from(mediaBuffer)), cipher.final()]);
|
|
98
|
+
const hmac = crypto.createHmac("sha256", Buffer.from(macKey));
|
|
99
|
+
hmac.update(Buffer.from(iv));
|
|
100
|
+
hmac.update(encryptedBody);
|
|
101
|
+
const fullMac = hmac.digest();
|
|
102
|
+
const mac = fullMac.subarray(0, 10);
|
|
103
|
+
const finalCiphertext = new Uint8Array(encryptedBody.length + mac.length);
|
|
104
|
+
finalCiphertext.set(encryptedBody, 0);
|
|
105
|
+
finalCiphertext.set(mac, encryptedBody.length);
|
|
106
|
+
const fileEncSha256 = sha256.sha256(finalCiphertext);
|
|
107
|
+
return {
|
|
108
|
+
mediaKey: new Uint8Array(mediaKey),
|
|
109
|
+
ciphertext: finalCiphertext,
|
|
110
|
+
fileSha256: new Uint8Array(fileSha256),
|
|
111
|
+
fileEncSha256: new Uint8Array(fileEncSha256),
|
|
112
|
+
mac: new Uint8Array(mac)
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// src/binary/constants.ts
|
|
118
|
+
var WABinaryConstants = {
|
|
119
|
+
LIST_EMPTY: 0,
|
|
120
|
+
LIST_8: 248,
|
|
121
|
+
LIST_16: 249,
|
|
122
|
+
BINARY_8: 252,
|
|
123
|
+
BINARY_32: 254};
|
|
124
|
+
var SINGLE_BYTE_TOKENS = [
|
|
125
|
+
"",
|
|
126
|
+
"",
|
|
127
|
+
"",
|
|
128
|
+
"200",
|
|
129
|
+
"400",
|
|
130
|
+
"404",
|
|
131
|
+
"500",
|
|
132
|
+
"501",
|
|
133
|
+
"502",
|
|
134
|
+
"action",
|
|
135
|
+
"add",
|
|
136
|
+
"after",
|
|
137
|
+
"archive",
|
|
138
|
+
"author",
|
|
139
|
+
"available",
|
|
140
|
+
"battery",
|
|
141
|
+
"before",
|
|
142
|
+
"body",
|
|
143
|
+
"broadcast",
|
|
144
|
+
"chat",
|
|
145
|
+
"clear",
|
|
146
|
+
"code",
|
|
147
|
+
"composing",
|
|
148
|
+
"config",
|
|
149
|
+
"contacts",
|
|
150
|
+
"count",
|
|
151
|
+
"create",
|
|
152
|
+
"debug",
|
|
153
|
+
"delete",
|
|
154
|
+
"demote",
|
|
155
|
+
"duplicate",
|
|
156
|
+
"encoding",
|
|
157
|
+
"error",
|
|
158
|
+
"false",
|
|
159
|
+
"filehash",
|
|
160
|
+
"from",
|
|
161
|
+
"g.us",
|
|
162
|
+
"group",
|
|
163
|
+
"groups_v2",
|
|
164
|
+
"height",
|
|
165
|
+
"ib",
|
|
166
|
+
"id",
|
|
167
|
+
"image",
|
|
168
|
+
"in",
|
|
169
|
+
"index",
|
|
170
|
+
"invis",
|
|
171
|
+
"item",
|
|
172
|
+
"jid",
|
|
173
|
+
"kind",
|
|
174
|
+
"last",
|
|
175
|
+
"leave",
|
|
176
|
+
"live",
|
|
177
|
+
"log",
|
|
178
|
+
"media",
|
|
179
|
+
"message",
|
|
180
|
+
"mimetype",
|
|
181
|
+
"missing",
|
|
182
|
+
"modify",
|
|
183
|
+
"name",
|
|
184
|
+
"notification",
|
|
185
|
+
"notify",
|
|
186
|
+
"out",
|
|
187
|
+
"owner",
|
|
188
|
+
"participant",
|
|
189
|
+
"paused",
|
|
190
|
+
"picture",
|
|
191
|
+
"played",
|
|
192
|
+
"presence",
|
|
193
|
+
"preview",
|
|
194
|
+
"promote",
|
|
195
|
+
"query",
|
|
196
|
+
"raw",
|
|
197
|
+
"read",
|
|
198
|
+
"receipt",
|
|
199
|
+
"received",
|
|
200
|
+
"recipient",
|
|
201
|
+
"recording",
|
|
202
|
+
"relay",
|
|
203
|
+
"remove",
|
|
204
|
+
"response",
|
|
205
|
+
"resume",
|
|
206
|
+
"retry",
|
|
207
|
+
"s.whatsapp.net",
|
|
208
|
+
"seconds",
|
|
209
|
+
"set",
|
|
210
|
+
"size",
|
|
211
|
+
"status",
|
|
212
|
+
"subject",
|
|
213
|
+
"subscribe",
|
|
214
|
+
"t",
|
|
215
|
+
"text",
|
|
216
|
+
"to",
|
|
217
|
+
"true",
|
|
218
|
+
"type",
|
|
219
|
+
"unarchive",
|
|
220
|
+
"unavailable",
|
|
221
|
+
"url",
|
|
222
|
+
"user",
|
|
223
|
+
"value",
|
|
224
|
+
"web",
|
|
225
|
+
"width",
|
|
226
|
+
"mute",
|
|
227
|
+
"read_only",
|
|
228
|
+
"admin",
|
|
229
|
+
"creator",
|
|
230
|
+
"short",
|
|
231
|
+
"update",
|
|
232
|
+
"powersave",
|
|
233
|
+
"rich",
|
|
234
|
+
"modify_tag",
|
|
235
|
+
"message_info",
|
|
236
|
+
"opaque",
|
|
237
|
+
"contact",
|
|
238
|
+
"unlink",
|
|
239
|
+
"iq",
|
|
240
|
+
"ping",
|
|
241
|
+
"version",
|
|
242
|
+
"client",
|
|
243
|
+
"pair-device",
|
|
244
|
+
"pair-success",
|
|
245
|
+
"success",
|
|
246
|
+
"failure"
|
|
247
|
+
];
|
|
248
|
+
var TOKEN_MAP = /* @__PURE__ */ new Map();
|
|
249
|
+
SINGLE_BYTE_TOKENS.forEach((token, index) => {
|
|
250
|
+
if (token) {
|
|
251
|
+
TOKEN_MAP.set(token, index);
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// src/binary/encoder.ts
|
|
256
|
+
var BinaryEncoder = class {
|
|
257
|
+
buffer = [];
|
|
258
|
+
encode(node) {
|
|
259
|
+
this.buffer = [];
|
|
260
|
+
this.writeNode(node);
|
|
261
|
+
return new Uint8Array(this.buffer);
|
|
262
|
+
}
|
|
263
|
+
writeByte(byte) {
|
|
264
|
+
this.buffer.push(byte & 255);
|
|
265
|
+
}
|
|
266
|
+
writeBytes(bytes) {
|
|
267
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
268
|
+
this.writeByte(bytes[i]);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
writeInt16(value) {
|
|
272
|
+
this.writeByte(value >> 8 & 255);
|
|
273
|
+
this.writeByte(value & 255);
|
|
274
|
+
}
|
|
275
|
+
writeInt32(value) {
|
|
276
|
+
this.writeByte(value >> 24 & 255);
|
|
277
|
+
this.writeByte(value >> 16 & 255);
|
|
278
|
+
this.writeByte(value >> 8 & 255);
|
|
279
|
+
this.writeByte(value & 255);
|
|
280
|
+
}
|
|
281
|
+
writeListStart(size) {
|
|
282
|
+
if (size === 0) {
|
|
283
|
+
this.writeByte(WABinaryConstants.LIST_EMPTY);
|
|
284
|
+
} else if (size < 256) {
|
|
285
|
+
this.writeByte(WABinaryConstants.LIST_8);
|
|
286
|
+
this.writeByte(size);
|
|
287
|
+
} else {
|
|
288
|
+
this.writeByte(WABinaryConstants.LIST_16);
|
|
289
|
+
this.writeInt16(size);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
writeString(str) {
|
|
293
|
+
const tokenIndex = TOKEN_MAP.get(str);
|
|
294
|
+
if (tokenIndex !== void 0) {
|
|
295
|
+
this.writeByte(tokenIndex);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
const encoded = new TextEncoder().encode(str);
|
|
299
|
+
if (encoded.length < 256) {
|
|
300
|
+
this.writeByte(WABinaryConstants.BINARY_8);
|
|
301
|
+
this.writeByte(encoded.length);
|
|
302
|
+
} else {
|
|
303
|
+
this.writeByte(WABinaryConstants.BINARY_32);
|
|
304
|
+
this.writeInt32(encoded.length);
|
|
305
|
+
}
|
|
306
|
+
this.writeBytes(encoded);
|
|
307
|
+
}
|
|
308
|
+
writeNode(node) {
|
|
309
|
+
const numAttrs = Object.keys(node.attrs).length;
|
|
310
|
+
const hasContent = node.content !== void 0;
|
|
311
|
+
const listSize = 1 + numAttrs * 2 + (hasContent ? 1 : 0);
|
|
312
|
+
this.writeListStart(listSize);
|
|
313
|
+
this.writeString(node.tag);
|
|
314
|
+
for (const [key, value] of Object.entries(node.attrs)) {
|
|
315
|
+
if (value !== void 0) {
|
|
316
|
+
this.writeString(key);
|
|
317
|
+
this.writeString(String(value));
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (hasContent) {
|
|
321
|
+
if (Array.isArray(node.content)) {
|
|
322
|
+
this.writeListStart(node.content.length);
|
|
323
|
+
for (const child of node.content) {
|
|
324
|
+
this.writeNode(child);
|
|
325
|
+
}
|
|
326
|
+
} else if (typeof node.content === "string") {
|
|
327
|
+
this.writeString(node.content);
|
|
328
|
+
} else if (node.content instanceof Uint8Array) {
|
|
329
|
+
if (node.content.length < 256) {
|
|
330
|
+
this.writeByte(WABinaryConstants.BINARY_8);
|
|
331
|
+
this.writeByte(node.content.length);
|
|
332
|
+
} else {
|
|
333
|
+
this.writeByte(WABinaryConstants.BINARY_32);
|
|
334
|
+
this.writeInt32(node.content.length);
|
|
335
|
+
}
|
|
336
|
+
this.writeBytes(node.content);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
// src/binary/decoder.ts
|
|
343
|
+
var BinaryDecoder = class {
|
|
344
|
+
buffer = new Uint8Array(0);
|
|
345
|
+
offset = 0;
|
|
346
|
+
decode(buffer) {
|
|
347
|
+
this.buffer = buffer;
|
|
348
|
+
this.offset = 0;
|
|
349
|
+
return this.readNode();
|
|
350
|
+
}
|
|
351
|
+
readByte() {
|
|
352
|
+
if (this.offset >= this.buffer.length) {
|
|
353
|
+
throw new Error(`Unexpected EOF in WABinary stream at offset ${this.offset}`);
|
|
354
|
+
}
|
|
355
|
+
const b = this.buffer[this.offset];
|
|
356
|
+
this.offset += 1;
|
|
357
|
+
return b;
|
|
358
|
+
}
|
|
359
|
+
readBytes(length) {
|
|
360
|
+
if (this.offset + length > this.buffer.length) {
|
|
361
|
+
throw new Error(`Buffer underrun: requested ${length} bytes, have ${this.buffer.length - this.offset}`);
|
|
362
|
+
}
|
|
363
|
+
const slice = this.buffer.slice(this.offset, this.offset + length);
|
|
364
|
+
this.offset += length;
|
|
365
|
+
return slice;
|
|
366
|
+
}
|
|
367
|
+
readInt16() {
|
|
368
|
+
const b0 = this.readByte();
|
|
369
|
+
const b1 = this.readByte();
|
|
370
|
+
return b0 << 8 | b1;
|
|
371
|
+
}
|
|
372
|
+
readInt32() {
|
|
373
|
+
const b0 = this.readByte();
|
|
374
|
+
const b1 = this.readByte();
|
|
375
|
+
const b2 = this.readByte();
|
|
376
|
+
const b3 = this.readByte();
|
|
377
|
+
return b0 << 24 | b1 << 16 | b2 << 8 | b3;
|
|
378
|
+
}
|
|
379
|
+
readListSize() {
|
|
380
|
+
const tag = this.readByte();
|
|
381
|
+
if (tag === WABinaryConstants.LIST_EMPTY) {
|
|
382
|
+
return 0;
|
|
383
|
+
}
|
|
384
|
+
if (tag === WABinaryConstants.LIST_8) {
|
|
385
|
+
return this.readByte();
|
|
386
|
+
}
|
|
387
|
+
if (tag === WABinaryConstants.LIST_16) {
|
|
388
|
+
return this.readInt16();
|
|
389
|
+
}
|
|
390
|
+
throw new Error(`Invalid list tag byte: ${tag}`);
|
|
391
|
+
}
|
|
392
|
+
readString() {
|
|
393
|
+
const tag = this.readByte();
|
|
394
|
+
if (tag < SINGLE_BYTE_TOKENS.length && SINGLE_BYTE_TOKENS[tag]) {
|
|
395
|
+
return SINGLE_BYTE_TOKENS[tag];
|
|
396
|
+
}
|
|
397
|
+
if (tag === WABinaryConstants.BINARY_8) {
|
|
398
|
+
const length = this.readByte();
|
|
399
|
+
const bytes = this.readBytes(length);
|
|
400
|
+
return new TextDecoder().decode(bytes);
|
|
401
|
+
}
|
|
402
|
+
if (tag === WABinaryConstants.BINARY_32) {
|
|
403
|
+
const length = this.readInt32();
|
|
404
|
+
const bytes = this.readBytes(length);
|
|
405
|
+
return new TextDecoder().decode(bytes);
|
|
406
|
+
}
|
|
407
|
+
throw new Error(`Unsupported string tag: ${tag} at offset ${this.offset - 1}`);
|
|
408
|
+
}
|
|
409
|
+
readNode() {
|
|
410
|
+
const listSize = this.readListSize();
|
|
411
|
+
if (listSize === 0) {
|
|
412
|
+
throw new Error("Invalid empty node");
|
|
413
|
+
}
|
|
414
|
+
const tag = this.readString();
|
|
415
|
+
const attrs = {};
|
|
416
|
+
let remaining = listSize - 1;
|
|
417
|
+
while (remaining > 1) {
|
|
418
|
+
const key = this.readString();
|
|
419
|
+
const val = this.readString();
|
|
420
|
+
attrs[key] = val;
|
|
421
|
+
remaining -= 2;
|
|
422
|
+
}
|
|
423
|
+
let content;
|
|
424
|
+
if (remaining === 1) {
|
|
425
|
+
const peekByte = this.buffer[this.offset];
|
|
426
|
+
if (peekByte === WABinaryConstants.LIST_EMPTY || peekByte === WABinaryConstants.LIST_8 || peekByte === WABinaryConstants.LIST_16) {
|
|
427
|
+
const childCount = this.readListSize();
|
|
428
|
+
const children = [];
|
|
429
|
+
for (let i = 0; i < childCount; i++) {
|
|
430
|
+
children.push(this.readNode());
|
|
431
|
+
}
|
|
432
|
+
content = children;
|
|
433
|
+
} else if (peekByte === WABinaryConstants.BINARY_8 || peekByte === WABinaryConstants.BINARY_32) {
|
|
434
|
+
const nextTag = this.readByte();
|
|
435
|
+
const length = nextTag === WABinaryConstants.BINARY_8 ? this.readByte() : this.readInt32();
|
|
436
|
+
content = this.readBytes(length);
|
|
437
|
+
} else {
|
|
438
|
+
content = this.readString();
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return { tag, attrs, content };
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
// src/binary/index.ts
|
|
446
|
+
var BinaryProtocol = class {
|
|
447
|
+
static encoder = new BinaryEncoder();
|
|
448
|
+
static decoder = new BinaryDecoder();
|
|
449
|
+
/**
|
|
450
|
+
* Converte um BinaryNode para bytes WABinary
|
|
451
|
+
*/
|
|
452
|
+
static encode(node) {
|
|
453
|
+
return this.encoder.encode(node);
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Converte bytes WABinary de volta para um BinaryNode estruturado
|
|
457
|
+
*/
|
|
458
|
+
static decode(buffer) {
|
|
459
|
+
return this.decoder.decode(buffer);
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Constrói um nó binário com validação de formato
|
|
463
|
+
*/
|
|
464
|
+
static createNode(tag, attrs = {}, content) {
|
|
465
|
+
return {
|
|
466
|
+
tag,
|
|
467
|
+
attrs,
|
|
468
|
+
content
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Localiza o primeiro filho correspondente à tag fornecida
|
|
473
|
+
*/
|
|
474
|
+
static getChildNode(node, childTag) {
|
|
475
|
+
if (Array.isArray(node.content)) {
|
|
476
|
+
return node.content.find((child) => child.tag === childTag);
|
|
477
|
+
}
|
|
478
|
+
return void 0;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Extrai todos os nós filhos que batem com uma determinada tag
|
|
482
|
+
*/
|
|
483
|
+
static getChildrenNodes(node, childTag) {
|
|
484
|
+
if (Array.isArray(node.content)) {
|
|
485
|
+
return node.content.filter((child) => child.tag === childTag);
|
|
486
|
+
}
|
|
487
|
+
return [];
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Converte o nó para representação em string legível para debug
|
|
491
|
+
*/
|
|
492
|
+
static stringify(node, indent = 0) {
|
|
493
|
+
const spaces = " ".repeat(indent);
|
|
494
|
+
const attrsStr = Object.entries(node.attrs).map(([k, v]) => `${k}="${v}"`).join(" ");
|
|
495
|
+
const openTag = attrsStr ? `<${node.tag} ${attrsStr}>` : `<${node.tag}>`;
|
|
496
|
+
if (!node.content) {
|
|
497
|
+
return `${spaces}<${node.tag} ${attrsStr} />`;
|
|
498
|
+
}
|
|
499
|
+
if (Array.isArray(node.content)) {
|
|
500
|
+
const children = node.content.map((c) => this.stringify(c, indent + 1)).join("\n");
|
|
501
|
+
return `${spaces}${openTag}
|
|
502
|
+
${children}
|
|
503
|
+
${spaces}</${node.tag}>`;
|
|
504
|
+
}
|
|
505
|
+
const contentStr = node.content instanceof Uint8Array ? `[Buffer ${node.content.length}b]` : String(node.content);
|
|
506
|
+
return `${spaces}${openTag}${contentStr}</${node.tag}>`;
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
// src/messages/builder.ts
|
|
511
|
+
var MessageBuilder = class {
|
|
512
|
+
/**
|
|
513
|
+
* Converte qualquer conteúdo de mensagem em uma stanza <message> pronta para transmissão
|
|
514
|
+
*/
|
|
515
|
+
static async buildMessageNode(jid, content, options = {}) {
|
|
516
|
+
const messageId = `3EB0${Date.now().toString(16).toUpperCase()}${Math.floor(Math.random() * 1e4)}`;
|
|
517
|
+
const timestamp = options.timestamp || Math.floor(Date.now() / 1e3);
|
|
518
|
+
const messageAttrs = {
|
|
519
|
+
id: messageId,
|
|
520
|
+
to: jid,
|
|
521
|
+
type: "text",
|
|
522
|
+
t: timestamp
|
|
523
|
+
};
|
|
524
|
+
let payloadContent = new Uint8Array(0);
|
|
525
|
+
if ("text" in content) {
|
|
526
|
+
payloadContent = new TextEncoder().encode(content.text);
|
|
527
|
+
} else if ("image" in content) {
|
|
528
|
+
messageAttrs.type = "media";
|
|
529
|
+
messageAttrs.mediatype = "image";
|
|
530
|
+
const buffer = await this.resolveMediaBuffer(content.image);
|
|
531
|
+
const encrypted = MediaCrypto.encryptMedia(buffer, "image");
|
|
532
|
+
payloadContent = [
|
|
533
|
+
BinaryProtocol.createNode("image", {
|
|
534
|
+
mimetype: content.mimetype || "image/jpeg",
|
|
535
|
+
caption: content.caption,
|
|
536
|
+
fileLength: buffer.length,
|
|
537
|
+
fileSha256: Buffer.from(encrypted.fileSha256).toString("base64"),
|
|
538
|
+
fileEncSha256: Buffer.from(encrypted.fileEncSha256).toString("base64"),
|
|
539
|
+
mediaKey: Buffer.from(encrypted.mediaKey).toString("base64")
|
|
540
|
+
})
|
|
541
|
+
];
|
|
542
|
+
} else if ("video" in content) {
|
|
543
|
+
messageAttrs.type = "media";
|
|
544
|
+
messageAttrs.mediatype = "video";
|
|
545
|
+
const buffer = await this.resolveMediaBuffer(content.video);
|
|
546
|
+
const encrypted = MediaCrypto.encryptMedia(buffer, "video");
|
|
547
|
+
payloadContent = [
|
|
548
|
+
BinaryProtocol.createNode("video", {
|
|
549
|
+
mimetype: content.mimetype || "video/mp4",
|
|
550
|
+
caption: content.caption,
|
|
551
|
+
gifPlayback: content.gifPlayback ? "true" : void 0,
|
|
552
|
+
ptv: content.ptv ? "true" : void 0,
|
|
553
|
+
fileLength: buffer.length,
|
|
554
|
+
fileSha256: Buffer.from(encrypted.fileSha256).toString("base64"),
|
|
555
|
+
fileEncSha256: Buffer.from(encrypted.fileEncSha256).toString("base64"),
|
|
556
|
+
mediaKey: Buffer.from(encrypted.mediaKey).toString("base64")
|
|
557
|
+
})
|
|
558
|
+
];
|
|
559
|
+
} else if ("audio" in content) {
|
|
560
|
+
messageAttrs.type = "media";
|
|
561
|
+
messageAttrs.mediatype = "audio";
|
|
562
|
+
const buffer = await this.resolveMediaBuffer(content.audio);
|
|
563
|
+
const encrypted = MediaCrypto.encryptMedia(buffer, "audio");
|
|
564
|
+
payloadContent = [
|
|
565
|
+
BinaryProtocol.createNode("audio", {
|
|
566
|
+
mimetype: content.mimetype || (content.ptt ? "audio/ogg; codecs=opus" : "audio/mp4"),
|
|
567
|
+
ptt: content.ptt ? "true" : "false",
|
|
568
|
+
fileLength: buffer.length,
|
|
569
|
+
fileSha256: Buffer.from(encrypted.fileSha256).toString("base64"),
|
|
570
|
+
fileEncSha256: Buffer.from(encrypted.fileEncSha256).toString("base64"),
|
|
571
|
+
mediaKey: Buffer.from(encrypted.mediaKey).toString("base64")
|
|
572
|
+
})
|
|
573
|
+
];
|
|
574
|
+
} else if ("document" in content) {
|
|
575
|
+
messageAttrs.type = "media";
|
|
576
|
+
messageAttrs.mediatype = "document";
|
|
577
|
+
const buffer = await this.resolveMediaBuffer(content.document);
|
|
578
|
+
const encrypted = MediaCrypto.encryptMedia(buffer, "document");
|
|
579
|
+
payloadContent = [
|
|
580
|
+
BinaryProtocol.createNode("document", {
|
|
581
|
+
mimetype: content.mimetype,
|
|
582
|
+
fileName: content.fileName || "file",
|
|
583
|
+
caption: content.caption,
|
|
584
|
+
fileLength: buffer.length,
|
|
585
|
+
fileSha256: Buffer.from(encrypted.fileSha256).toString("base64"),
|
|
586
|
+
fileEncSha256: Buffer.from(encrypted.fileEncSha256).toString("base64"),
|
|
587
|
+
mediaKey: Buffer.from(encrypted.mediaKey).toString("base64")
|
|
588
|
+
})
|
|
589
|
+
];
|
|
590
|
+
} else if ("location" in content) {
|
|
591
|
+
messageAttrs.type = "location";
|
|
592
|
+
payloadContent = [
|
|
593
|
+
BinaryProtocol.createNode("location", {
|
|
594
|
+
degreesLatitude: content.location.degreesLatitude,
|
|
595
|
+
degreesLongitude: content.location.degreesLongitude,
|
|
596
|
+
name: content.location.name,
|
|
597
|
+
address: content.location.address,
|
|
598
|
+
url: content.location.url
|
|
599
|
+
})
|
|
600
|
+
];
|
|
601
|
+
} else if ("contact" in content) {
|
|
602
|
+
messageAttrs.type = "contact";
|
|
603
|
+
payloadContent = [
|
|
604
|
+
BinaryProtocol.createNode("contact", {
|
|
605
|
+
displayName: content.contact.displayName,
|
|
606
|
+
vcard: content.contact.vcard
|
|
607
|
+
})
|
|
608
|
+
];
|
|
609
|
+
} else if ("contacts" in content) {
|
|
610
|
+
messageAttrs.type = "contacts";
|
|
611
|
+
const contactNodes = content.contacts.contacts.map(
|
|
612
|
+
(c) => BinaryProtocol.createNode("contact", {
|
|
613
|
+
displayName: c.displayName,
|
|
614
|
+
vcard: c.vcard
|
|
615
|
+
})
|
|
616
|
+
);
|
|
617
|
+
payloadContent = [
|
|
618
|
+
BinaryProtocol.createNode(
|
|
619
|
+
"contacts",
|
|
620
|
+
{ displayName: content.contacts.displayName },
|
|
621
|
+
contactNodes
|
|
622
|
+
)
|
|
623
|
+
];
|
|
624
|
+
} else if ("react" in content) {
|
|
625
|
+
messageAttrs.type = "reaction";
|
|
626
|
+
payloadContent = [
|
|
627
|
+
BinaryProtocol.createNode("reaction", {
|
|
628
|
+
text: content.react.text,
|
|
629
|
+
targetId: content.react.key.id,
|
|
630
|
+
targetJid: content.react.key.remoteJid
|
|
631
|
+
})
|
|
632
|
+
];
|
|
633
|
+
}
|
|
634
|
+
const node = BinaryProtocol.createNode("message", messageAttrs, payloadContent);
|
|
635
|
+
return {
|
|
636
|
+
node,
|
|
637
|
+
messageId
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Resolve diferentes fontes de mídia (Buffer, Uint8Array ou URL) para um Uint8Array em memória
|
|
642
|
+
*/
|
|
643
|
+
static async resolveMediaBuffer(source) {
|
|
644
|
+
if (source instanceof Uint8Array) {
|
|
645
|
+
return source;
|
|
646
|
+
}
|
|
647
|
+
if (Buffer.isBuffer(source)) {
|
|
648
|
+
return new Uint8Array(source);
|
|
649
|
+
}
|
|
650
|
+
if (typeof source === "object" && source !== null && "url" in source) {
|
|
651
|
+
const res = await fetch(source.url);
|
|
652
|
+
const arrayBuf = await res.arrayBuffer();
|
|
653
|
+
return new Uint8Array(arrayBuf);
|
|
654
|
+
}
|
|
655
|
+
throw new Error("Unsupported media source format");
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
exports.MediaCrypto = MediaCrypto;
|
|
660
|
+
exports.MessageBuilder = MessageBuilder;
|
|
661
|
+
//# sourceMappingURL=index.cjs.map
|
|
662
|
+
//# sourceMappingURL=index.cjs.map
|