@baileys-md/baileys 10.1.0 → 11.0.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 +1 -1
- package/README.md +1303 -2
- package/WAProto/GenerateStatics.sh +3 -0
- package/WAProto/WAProto.proto +4633 -0
- package/WAProto/fix-imports.js +29 -0
- package/WAProto/index.js +13516 -4182
- package/lib/Defaults/baileys-version.js +1 -0
- package/lib/Defaults/index.js +51 -72
- package/lib/Signal/Group/ciphertext-message.js +12 -0
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/lib/Signal/Group/group_cipher.js +94 -0
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/keyhelper.js +19 -0
- package/lib/Signal/Group/queue-job.js +54 -0
- package/lib/Signal/Group/sender-chain-key.js +32 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
- package/lib/Signal/Group/sender-key-message.js +67 -0
- package/lib/Signal/Group/sender-key-name.js +48 -0
- package/lib/Signal/Group/sender-key-record.js +50 -0
- package/lib/Signal/Group/sender-key-state.js +96 -0
- package/{WASignalGroup/sender_message_key.js → lib/Signal/Group/sender-message-key.js} +4 -16
- package/lib/Signal/libsignal.js +41 -61
- package/lib/Socket/Client/index.js +3 -19
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/websocket.js +50 -0
- package/lib/Socket/business.js +37 -42
- package/lib/Socket/chats.js +194 -187
- package/lib/Socket/communities.js +351 -0
- package/lib/Socket/groups.js +87 -90
- package/lib/Socket/index.js +7 -8
- package/lib/Socket/messages-recv.js +360 -335
- package/lib/Socket/messages-send.js +156 -279
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/newsletter.js +144 -213
- package/lib/Socket/socket.js +128 -161
- package/lib/Socket/usync.js +19 -26
- package/lib/Types/Auth.js +2 -2
- package/lib/Types/Call.js +2 -2
- package/lib/Types/Chat.js +8 -4
- package/lib/Types/Contact.js +2 -2
- package/lib/Types/Events.js +2 -2
- package/lib/Types/GroupMetadata.js +2 -2
- package/lib/Types/Label.js +3 -5
- package/lib/Types/LabelAssociation.js +3 -5
- package/lib/Types/Message.js +7 -7
- package/lib/Types/Newsletter.js +30 -17
- package/lib/Types/Product.js +2 -2
- package/lib/Types/Signal.js +2 -2
- package/lib/Types/Socket.js +3 -2
- package/lib/Types/State.js +2 -2
- package/lib/Types/USync.js +2 -2
- package/lib/Types/index.js +15 -31
- package/lib/Utils/auth-utils.js +31 -47
- package/lib/Utils/baileys-event-stream.js +15 -22
- package/lib/Utils/business.js +66 -69
- package/lib/Utils/chat-utils.js +200 -195
- package/lib/Utils/crypto.js +70 -85
- package/lib/Utils/decode-wa-message.js +47 -51
- package/lib/Utils/event-buffer.js +36 -46
- package/lib/Utils/generics.js +116 -188
- package/lib/Utils/history.js +37 -46
- package/lib/Utils/index.js +19 -33
- package/lib/Utils/link-preview.js +14 -55
- package/lib/Utils/logger.js +3 -7
- package/lib/Utils/lt-hash.js +23 -26
- package/lib/{Store → Utils}/make-in-memory-store.js +19 -27
- package/lib/Utils/make-mutex.js +7 -10
- package/lib/{Store → Utils}/make-ordered-dictionary.js +1 -3
- package/lib/Utils/messages-media.js +236 -368
- package/lib/Utils/messages.js +278 -510
- package/lib/Utils/noise-handler.js +22 -31
- package/lib/{Store → Utils}/object-repository.js +1 -4
- package/lib/Utils/process-message.js +144 -148
- package/lib/Utils/signal.js +71 -64
- package/lib/Utils/use-multi-file-auth-state.js +112 -84
- package/lib/Utils/validate-connection.js +72 -115
- package/lib/WABinary/constants.js +1281 -20
- package/lib/WABinary/decode.js +15 -52
- package/lib/WABinary/encode.js +14 -48
- package/lib/WABinary/generic-utils.js +31 -39
- package/lib/WABinary/index.js +6 -21
- package/lib/WABinary/jid-utils.js +23 -40
- package/lib/WABinary/types.js +2 -2
- package/lib/WAM/BinaryInfo.js +2 -5
- package/lib/WAM/constants.js +2257 -2366
- package/lib/WAM/encode.js +17 -21
- package/lib/WAM/index.js +4 -19
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +8 -11
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -14
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -12
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -13
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -22
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +3 -6
- package/lib/WAUSync/Protocols/index.js +5 -20
- package/lib/WAUSync/USyncQuery.js +34 -32
- package/lib/WAUSync/USyncUser.js +2 -5
- package/lib/WAUSync/index.js +4 -19
- package/lib/index.js +11 -33
- package/package.json +25 -54
- package/WASignalGroup/GroupProtocol.js +0 -1697
- package/WASignalGroup/ciphertext_message.js +0 -16
- package/WASignalGroup/group_cipher.js +0 -120
- package/WASignalGroup/group_session_builder.js +0 -46
- package/WASignalGroup/index.js +0 -5
- package/WASignalGroup/keyhelper.js +0 -21
- package/WASignalGroup/protobufs.js +0 -3
- package/WASignalGroup/queue_job.js +0 -69
- package/WASignalGroup/sender_chain_key.js +0 -50
- package/WASignalGroup/sender_key_distribution_message.js +0 -78
- package/WASignalGroup/sender_key_message.js +0 -92
- package/WASignalGroup/sender_key_name.js +0 -70
- package/WASignalGroup/sender_key_record.js +0 -56
- package/WASignalGroup/sender_key_state.js +0 -129
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Socket/Client/abstract-socket-client.js +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.js +0 -62
- package/lib/Socket/registration.js +0 -166
- package/lib/Store/index.js +0 -8
- package/lib/Store/make-cache-manager-store.js +0 -83
- package/lib/Store/make-mongo-store.js +0 -567
package/lib/Utils/crypto.js
CHANGED
|
@@ -1,42 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.derivePairingCodeKey = exports.hkdf = exports.md5 = exports.sha256 = exports.hmacSign = exports.aesEncrypWithIV = exports.aesEncrypt = exports.aesDecryptWithIV = exports.aesDecrypt = exports.aesDecryptCTR = exports.aesEncryptCTR = exports.aesDecryptGCM = exports.aesEncryptGCM = exports.signedKeyPair = exports.Curve = exports.generateSignalPubKey = void 0;
|
|
30
|
-
const crypto_1 = require("crypto");
|
|
31
|
-
const futoin_hkdf_1 = __importDefault(require("futoin-hkdf"));
|
|
32
|
-
const libsignal = __importStar(require("libsignal"));
|
|
33
|
-
const Defaults_1 = require("../Defaults");
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes, subtle } from 'crypto';
|
|
2
|
+
/* @ts-ignore */
|
|
3
|
+
import * as libsignal from 'libsignal';
|
|
4
|
+
import { KEY_BUNDLE_TYPE } from '../Defaults/index.js';
|
|
34
5
|
/** prefix version byte to the pub keys, required for some curve crypto functions */
|
|
35
|
-
const generateSignalPubKey = (pubKey) =>
|
|
36
|
-
|
|
37
|
-
: Buffer.concat([Defaults_1.KEY_BUNDLE_TYPE, pubKey]));
|
|
38
|
-
exports.generateSignalPubKey = generateSignalPubKey;
|
|
39
|
-
exports.Curve = {
|
|
6
|
+
export const generateSignalPubKey = (pubKey) => pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey]);
|
|
7
|
+
export const Curve = {
|
|
40
8
|
generateKeyPair: () => {
|
|
41
9
|
const { pubKey, privKey } = libsignal.curve.generateKeyPair();
|
|
42
10
|
return {
|
|
@@ -46,13 +14,13 @@ exports.Curve = {
|
|
|
46
14
|
};
|
|
47
15
|
},
|
|
48
16
|
sharedKey: (privateKey, publicKey) => {
|
|
49
|
-
const shared = libsignal.curve.calculateAgreement(
|
|
17
|
+
const shared = libsignal.curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey);
|
|
50
18
|
return Buffer.from(shared);
|
|
51
19
|
},
|
|
52
|
-
sign: (privateKey, buf) =>
|
|
20
|
+
sign: (privateKey, buf) => libsignal.curve.calculateSignature(privateKey, buf),
|
|
53
21
|
verify: (pubKey, message, signature) => {
|
|
54
22
|
try {
|
|
55
|
-
libsignal.curve.verifySignature(
|
|
23
|
+
libsignal.curve.verifySignature(generateSignalPubKey(pubKey), message, signature);
|
|
56
24
|
return true;
|
|
57
25
|
}
|
|
58
26
|
catch (error) {
|
|
@@ -60,30 +28,28 @@ exports.Curve = {
|
|
|
60
28
|
}
|
|
61
29
|
}
|
|
62
30
|
};
|
|
63
|
-
const signedKeyPair = (identityKeyPair, keyId) => {
|
|
64
|
-
const preKey =
|
|
65
|
-
const pubKey =
|
|
66
|
-
const signature =
|
|
31
|
+
export const signedKeyPair = (identityKeyPair, keyId) => {
|
|
32
|
+
const preKey = Curve.generateKeyPair();
|
|
33
|
+
const pubKey = generateSignalPubKey(preKey.public);
|
|
34
|
+
const signature = Curve.sign(identityKeyPair.private, pubKey);
|
|
67
35
|
return { keyPair: preKey, signature, keyId };
|
|
68
36
|
};
|
|
69
|
-
exports.signedKeyPair = signedKeyPair;
|
|
70
37
|
const GCM_TAG_LENGTH = 128 >> 3;
|
|
71
38
|
/**
|
|
72
39
|
* encrypt AES 256 GCM;
|
|
73
40
|
* where the tag tag is suffixed to the ciphertext
|
|
74
41
|
* */
|
|
75
|
-
function aesEncryptGCM(plaintext, key, iv, additionalData) {
|
|
76
|
-
const cipher =
|
|
42
|
+
export function aesEncryptGCM(plaintext, key, iv, additionalData) {
|
|
43
|
+
const cipher = createCipheriv('aes-256-gcm', key, iv);
|
|
77
44
|
cipher.setAAD(additionalData);
|
|
78
45
|
return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
|
|
79
46
|
}
|
|
80
|
-
exports.aesEncryptGCM = aesEncryptGCM;
|
|
81
47
|
/**
|
|
82
48
|
* decrypt AES 256 GCM;
|
|
83
49
|
* where the auth tag is suffixed to the ciphertext
|
|
84
50
|
* */
|
|
85
|
-
function aesDecryptGCM(ciphertext, key, iv, additionalData) {
|
|
86
|
-
const decipher =
|
|
51
|
+
export function aesDecryptGCM(ciphertext, key, iv, additionalData) {
|
|
52
|
+
const decipher = createDecipheriv('aes-256-gcm', key, iv);
|
|
87
53
|
// decrypt additional adata
|
|
88
54
|
const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
|
|
89
55
|
const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
|
|
@@ -92,60 +58,79 @@ function aesDecryptGCM(ciphertext, key, iv, additionalData) {
|
|
|
92
58
|
decipher.setAuthTag(tag);
|
|
93
59
|
return Buffer.concat([decipher.update(enc), decipher.final()]);
|
|
94
60
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const cipher = (0, crypto_1.createCipheriv)('aes-256-ctr', key, iv);
|
|
61
|
+
export function aesEncryptCTR(plaintext, key, iv) {
|
|
62
|
+
const cipher = createCipheriv('aes-256-ctr', key, iv);
|
|
98
63
|
return Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
99
64
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const decipher = (0, crypto_1.createDecipheriv)('aes-256-ctr', key, iv);
|
|
65
|
+
export function aesDecryptCTR(ciphertext, key, iv) {
|
|
66
|
+
const decipher = createDecipheriv('aes-256-ctr', key, iv);
|
|
103
67
|
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
104
68
|
}
|
|
105
|
-
exports.aesDecryptCTR = aesDecryptCTR;
|
|
106
69
|
/** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
|
|
107
|
-
function aesDecrypt(buffer, key) {
|
|
70
|
+
export function aesDecrypt(buffer, key) {
|
|
108
71
|
return aesDecryptWithIV(buffer.slice(16, buffer.length), key, buffer.slice(0, 16));
|
|
109
72
|
}
|
|
110
|
-
exports.aesDecrypt = aesDecrypt;
|
|
111
73
|
/** decrypt AES 256 CBC */
|
|
112
|
-
function aesDecryptWithIV(buffer, key, IV) {
|
|
113
|
-
const aes =
|
|
74
|
+
export function aesDecryptWithIV(buffer, key, IV) {
|
|
75
|
+
const aes = createDecipheriv('aes-256-cbc', key, IV);
|
|
114
76
|
return Buffer.concat([aes.update(buffer), aes.final()]);
|
|
115
77
|
}
|
|
116
|
-
exports.aesDecryptWithIV = aesDecryptWithIV;
|
|
117
78
|
// encrypt AES 256 CBC; where a random IV is prefixed to the buffer
|
|
118
|
-
function aesEncrypt(buffer, key) {
|
|
119
|
-
const IV =
|
|
120
|
-
const aes =
|
|
79
|
+
export function aesEncrypt(buffer, key) {
|
|
80
|
+
const IV = randomBytes(16);
|
|
81
|
+
const aes = createCipheriv('aes-256-cbc', key, IV);
|
|
121
82
|
return Buffer.concat([IV, aes.update(buffer), aes.final()]); // prefix IV to the buffer
|
|
122
83
|
}
|
|
123
|
-
exports.aesEncrypt = aesEncrypt;
|
|
124
84
|
// encrypt AES 256 CBC with a given IV
|
|
125
|
-
function aesEncrypWithIV(buffer, key, IV) {
|
|
126
|
-
const aes =
|
|
85
|
+
export function aesEncrypWithIV(buffer, key, IV) {
|
|
86
|
+
const aes = createCipheriv('aes-256-cbc', key, IV);
|
|
127
87
|
return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer
|
|
128
88
|
}
|
|
129
|
-
exports.aesEncrypWithIV = aesEncrypWithIV;
|
|
130
89
|
// sign HMAC using SHA 256
|
|
131
|
-
function hmacSign(buffer, key, variant = 'sha256') {
|
|
132
|
-
return
|
|
90
|
+
export function hmacSign(buffer, key, variant = 'sha256') {
|
|
91
|
+
return createHmac(variant, key).update(buffer).digest();
|
|
133
92
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return (0, crypto_1.createHash)('sha256').update(buffer).digest();
|
|
93
|
+
export function sha256(buffer) {
|
|
94
|
+
return createHash('sha256').update(buffer).digest();
|
|
137
95
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
return (0, crypto_1.createHash)('md5').update(buffer).digest();
|
|
96
|
+
export function md5(buffer) {
|
|
97
|
+
return createHash('md5').update(buffer).digest();
|
|
141
98
|
}
|
|
142
|
-
exports.md5 = md5;
|
|
143
99
|
// HKDF key expansion
|
|
144
|
-
function hkdf(buffer, expandedLength, info) {
|
|
145
|
-
|
|
100
|
+
export async function hkdf(buffer, expandedLength, info) {
|
|
101
|
+
// Ensure we have a Uint8Array for the key material
|
|
102
|
+
const inputKeyMaterial = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
103
|
+
// Set default values if not provided
|
|
104
|
+
const salt = info.salt ? new Uint8Array(info.salt) : new Uint8Array(0);
|
|
105
|
+
const infoBytes = info.info ? new TextEncoder().encode(info.info) : new Uint8Array(0);
|
|
106
|
+
// Import the input key material
|
|
107
|
+
const importedKey = await subtle.importKey('raw', inputKeyMaterial, { name: 'HKDF' }, false, ['deriveBits']);
|
|
108
|
+
// Derive bits using HKDF
|
|
109
|
+
const derivedBits = await subtle.deriveBits({
|
|
110
|
+
name: 'HKDF',
|
|
111
|
+
hash: 'SHA-256',
|
|
112
|
+
salt: salt,
|
|
113
|
+
info: infoBytes
|
|
114
|
+
}, importedKey, expandedLength * 8 // Convert bytes to bits
|
|
115
|
+
);
|
|
116
|
+
return Buffer.from(derivedBits);
|
|
146
117
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
118
|
+
export async function derivePairingCodeKey(pairingCode, salt) {
|
|
119
|
+
// Convert inputs to formats Web Crypto API can work with
|
|
120
|
+
const encoder = new TextEncoder();
|
|
121
|
+
const pairingCodeBuffer = encoder.encode(pairingCode);
|
|
122
|
+
const saltBuffer = salt instanceof Uint8Array ? salt : new Uint8Array(salt);
|
|
123
|
+
// Import the pairing code as key material
|
|
124
|
+
const keyMaterial = await subtle.importKey('raw', pairingCodeBuffer, { name: 'PBKDF2' }, false, ['deriveBits']);
|
|
125
|
+
// Derive bits using PBKDF2 with the same parameters
|
|
126
|
+
// 2 << 16 = 131,072 iterations
|
|
127
|
+
const derivedBits = await subtle.deriveBits({
|
|
128
|
+
name: 'PBKDF2',
|
|
129
|
+
salt: saltBuffer,
|
|
130
|
+
iterations: 2 << 16,
|
|
131
|
+
hash: 'SHA-256'
|
|
132
|
+
}, keyMaterial, 32 * 8 // 32 bytes * 8 = 256 bits
|
|
133
|
+
);
|
|
134
|
+
return Buffer.from(derivedBits);
|
|
150
135
|
}
|
|
151
|
-
|
|
136
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const generics_1 = require("./generics");
|
|
9
|
-
exports.NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node';
|
|
10
|
-
exports.MISSING_KEYS_ERROR_TEXT = 'Key used already or never filled';
|
|
11
|
-
exports.NACK_REASONS = {
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
import { areJidsSameUser, isJidBroadcast, isJidGroup, isJidMetaIa, isJidNewsletter, isJidStatusBroadcast, isJidUser, isLidUser } from '../WABinary/index.js';
|
|
4
|
+
import { unpadRandomMax16 } from './generics.js';
|
|
5
|
+
export const NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node';
|
|
6
|
+
export const MISSING_KEYS_ERROR_TEXT = 'Key used already or never filled';
|
|
7
|
+
export const NACK_REASONS = {
|
|
12
8
|
ParsingError: 487,
|
|
13
9
|
UnrecognizedStanza: 488,
|
|
14
10
|
UnrecognizedStanzaClass: 489,
|
|
@@ -27,8 +23,7 @@ exports.NACK_REASONS = {
|
|
|
27
23
|
* Decode the received node as a message.
|
|
28
24
|
* @note this will only parse the message, not decrypt it
|
|
29
25
|
*/
|
|
30
|
-
function decodeMessageNode(stanza, meId, meLid) {
|
|
31
|
-
var _a, _b, _c, _d;
|
|
26
|
+
export function decodeMessageNode(stanza, meId, meLid) {
|
|
32
27
|
let msgType;
|
|
33
28
|
let chatId;
|
|
34
29
|
let author;
|
|
@@ -36,12 +31,12 @@ function decodeMessageNode(stanza, meId, meLid) {
|
|
|
36
31
|
const from = stanza.attrs.from;
|
|
37
32
|
const participant = stanza.attrs.participant;
|
|
38
33
|
const recipient = stanza.attrs.recipient;
|
|
39
|
-
const isMe = (jid) =>
|
|
40
|
-
const isMeLid = (jid) =>
|
|
41
|
-
if (
|
|
42
|
-
if (recipient && !
|
|
34
|
+
const isMe = (jid) => areJidsSameUser(jid, meId);
|
|
35
|
+
const isMeLid = (jid) => areJidsSameUser(jid, meLid);
|
|
36
|
+
if (isJidUser(from) || isLidUser(from)) {
|
|
37
|
+
if (recipient && !isJidMetaIa(recipient)) {
|
|
43
38
|
if (!isMe(from) && !isMeLid(from)) {
|
|
44
|
-
throw new
|
|
39
|
+
throw new Boom('receipient present, but msg not from me', { data: stanza });
|
|
45
40
|
}
|
|
46
41
|
chatId = recipient;
|
|
47
42
|
}
|
|
@@ -51,25 +46,20 @@ function decodeMessageNode(stanza, meId, meLid) {
|
|
|
51
46
|
msgType = 'chat';
|
|
52
47
|
author = from;
|
|
53
48
|
}
|
|
54
|
-
else if (
|
|
49
|
+
else if (isJidGroup(from)) {
|
|
55
50
|
if (!participant) {
|
|
56
|
-
throw new
|
|
51
|
+
throw new Boom('No participant in group message');
|
|
57
52
|
}
|
|
58
53
|
msgType = 'group';
|
|
59
54
|
author = participant;
|
|
60
55
|
chatId = from;
|
|
61
56
|
}
|
|
62
|
-
else if ((
|
|
63
|
-
msgType = 'newsletter';
|
|
64
|
-
author = from;
|
|
65
|
-
chatId = from;
|
|
66
|
-
}
|
|
67
|
-
else if ((0, WABinary_1.isJidBroadcast)(from)) {
|
|
57
|
+
else if (isJidBroadcast(from)) {
|
|
68
58
|
if (!participant) {
|
|
69
|
-
throw new
|
|
59
|
+
throw new Boom('No participant in group message');
|
|
70
60
|
}
|
|
71
61
|
const isParticipantMe = isMe(participant);
|
|
72
|
-
if (
|
|
62
|
+
if (isJidStatusBroadcast(from)) {
|
|
73
63
|
msgType = isParticipantMe ? 'direct_peer_status' : 'other_status';
|
|
74
64
|
}
|
|
75
65
|
else {
|
|
@@ -78,29 +68,35 @@ function decodeMessageNode(stanza, meId, meLid) {
|
|
|
78
68
|
chatId = from;
|
|
79
69
|
author = participant;
|
|
80
70
|
}
|
|
71
|
+
else if (isJidNewsletter(from)) {
|
|
72
|
+
msgType = 'newsletter';
|
|
73
|
+
chatId = from;
|
|
74
|
+
author = from;
|
|
75
|
+
}
|
|
81
76
|
else {
|
|
82
|
-
throw new
|
|
77
|
+
throw new Boom('Unknown message type', { data: stanza });
|
|
83
78
|
}
|
|
84
|
-
const fromMe = (
|
|
85
|
-
const pushname =
|
|
79
|
+
const fromMe = (isLidUser(from) ? isMeLid : isMe)((stanza.attrs.participant || stanza.attrs.from));
|
|
80
|
+
const pushname = stanza?.attrs?.notify;
|
|
86
81
|
const key = {
|
|
87
82
|
remoteJid: chatId,
|
|
88
83
|
fromMe,
|
|
89
84
|
id: msgId,
|
|
85
|
+
senderLid: stanza?.attrs?.sender_lid,
|
|
86
|
+
senderPn: stanza?.attrs?.sender_pn,
|
|
90
87
|
participant,
|
|
91
|
-
|
|
88
|
+
participantPn: stanza?.attrs?.participant_pn,
|
|
89
|
+
participantLid: stanza?.attrs?.participant_lid,
|
|
90
|
+
...(msgType === 'newsletter' && stanza.attrs.server_id ? { server_id: stanza.attrs.server_id } : {})
|
|
92
91
|
};
|
|
93
92
|
const fullMessage = {
|
|
94
93
|
key,
|
|
95
94
|
messageTimestamp: +stanza.attrs.t,
|
|
96
95
|
pushName: pushname,
|
|
97
|
-
broadcast:
|
|
96
|
+
broadcast: isJidBroadcast(from)
|
|
98
97
|
};
|
|
99
|
-
if (msgType === 'newsletter') {
|
|
100
|
-
fullMessage.newsletterServerId = +((_d = stanza.attrs) === null || _d === void 0 ? void 0 : _d.server_id);
|
|
101
|
-
}
|
|
102
98
|
if (key.fromMe) {
|
|
103
|
-
fullMessage.status =
|
|
99
|
+
fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK;
|
|
104
100
|
}
|
|
105
101
|
return {
|
|
106
102
|
fullMessage,
|
|
@@ -108,22 +104,24 @@ function decodeMessageNode(stanza, meId, meLid) {
|
|
|
108
104
|
sender: msgType === 'chat' ? author : chatId
|
|
109
105
|
};
|
|
110
106
|
}
|
|
111
|
-
const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
107
|
+
export const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
112
108
|
const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid);
|
|
113
109
|
return {
|
|
114
110
|
fullMessage,
|
|
115
111
|
category: stanza.attrs.category,
|
|
116
112
|
author,
|
|
117
113
|
async decrypt() {
|
|
118
|
-
var _a;
|
|
119
114
|
let decryptables = 0;
|
|
120
115
|
if (Array.isArray(stanza.content)) {
|
|
121
116
|
for (const { tag, attrs, content } of stanza.content) {
|
|
122
117
|
if (tag === 'verified_name' && content instanceof Uint8Array) {
|
|
123
|
-
const cert =
|
|
124
|
-
const details =
|
|
118
|
+
const cert = proto.VerifiedNameCertificate.decode(content);
|
|
119
|
+
const details = proto.VerifiedNameCertificate.Details.decode(cert.details);
|
|
125
120
|
fullMessage.verifiedBizName = details.verifiedName;
|
|
126
121
|
}
|
|
122
|
+
if (tag === 'unavailable' && attrs.type === 'view_once') {
|
|
123
|
+
fullMessage.key.isViewOnce = true;
|
|
124
|
+
}
|
|
127
125
|
if (tag !== 'enc' && tag !== 'plaintext') {
|
|
128
126
|
continue;
|
|
129
127
|
}
|
|
@@ -144,7 +142,7 @@ const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
|
144
142
|
break;
|
|
145
143
|
case 'pkmsg':
|
|
146
144
|
case 'msg':
|
|
147
|
-
const user =
|
|
145
|
+
const user = isJidUser(sender) ? sender : author;
|
|
148
146
|
msgBuffer = await repository.decryptMessage({
|
|
149
147
|
jid: user,
|
|
150
148
|
type: e2eType,
|
|
@@ -154,15 +152,13 @@ const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
|
154
152
|
case 'plaintext':
|
|
155
153
|
msgBuffer = content;
|
|
156
154
|
break;
|
|
157
|
-
case undefined:
|
|
158
|
-
msgBuffer = content;
|
|
159
|
-
break;
|
|
160
155
|
default:
|
|
161
156
|
throw new Error(`Unknown e2e type: ${e2eType}`);
|
|
162
157
|
}
|
|
163
|
-
let msg =
|
|
164
|
-
msg =
|
|
158
|
+
let msg = proto.Message.decode(e2eType !== 'plaintext' ? unpadRandomMax16(msgBuffer) : msgBuffer);
|
|
159
|
+
msg = msg.deviceSentMessage?.message || msg;
|
|
165
160
|
if (msg.senderKeyDistributionMessage) {
|
|
161
|
+
//eslint-disable-next-line max-depth
|
|
166
162
|
try {
|
|
167
163
|
await repository.processSenderKeyDistributionMessage({
|
|
168
164
|
authorJid: author,
|
|
@@ -182,17 +178,17 @@ const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
|
182
178
|
}
|
|
183
179
|
catch (err) {
|
|
184
180
|
logger.error({ key: fullMessage.key, err }, 'failed to decrypt message');
|
|
185
|
-
fullMessage.messageStubType =
|
|
181
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
186
182
|
fullMessage.messageStubParameters = [err.message];
|
|
187
183
|
}
|
|
188
184
|
}
|
|
189
185
|
}
|
|
190
186
|
// if nothing was found to decrypt
|
|
191
187
|
if (!decryptables) {
|
|
192
|
-
fullMessage.messageStubType =
|
|
193
|
-
fullMessage.messageStubParameters = [
|
|
188
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
189
|
+
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT];
|
|
194
190
|
}
|
|
195
191
|
}
|
|
196
192
|
};
|
|
197
193
|
};
|
|
198
|
-
|
|
194
|
+
//# sourceMappingURL=decode-wa-message.js.map
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const events_1 = __importDefault(require("events"));
|
|
8
|
-
const Types_1 = require("../Types");
|
|
9
|
-
const generics_1 = require("./generics");
|
|
10
|
-
const messages_1 = require("./messages");
|
|
11
|
-
const process_message_1 = require("./process-message");
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
import { WAMessageStatus } from '../Types/index.js';
|
|
4
|
+
import { trimUndefined } from './generics.js';
|
|
5
|
+
import { updateMessageWithReaction, updateMessageWithReceipt } from './messages.js';
|
|
6
|
+
import { isRealMessage, shouldIncrementChatUnread } from './process-message.js';
|
|
12
7
|
const BUFFERABLE_EVENT = [
|
|
13
8
|
'messaging-history.set',
|
|
14
9
|
'chats.upsert',
|
|
@@ -21,7 +16,7 @@ const BUFFERABLE_EVENT = [
|
|
|
21
16
|
'messages.delete',
|
|
22
17
|
'messages.reaction',
|
|
23
18
|
'message-receipt.update',
|
|
24
|
-
'groups.update'
|
|
19
|
+
'groups.update'
|
|
25
20
|
];
|
|
26
21
|
const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT);
|
|
27
22
|
/**
|
|
@@ -29,8 +24,8 @@ const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT);
|
|
|
29
24
|
* making the data processing more efficient.
|
|
30
25
|
* @param ev the baileys event emitter
|
|
31
26
|
*/
|
|
32
|
-
const makeEventBuffer = (logger) => {
|
|
33
|
-
const ev = new
|
|
27
|
+
export const makeEventBuffer = (logger) => {
|
|
28
|
+
const ev = new EventEmitter();
|
|
34
29
|
const historyCache = new Set();
|
|
35
30
|
let data = makeBufferData();
|
|
36
31
|
let buffersInProgress = 0;
|
|
@@ -112,10 +107,9 @@ const makeEventBuffer = (logger) => {
|
|
|
112
107
|
},
|
|
113
108
|
on: (...args) => ev.on(...args),
|
|
114
109
|
off: (...args) => ev.off(...args),
|
|
115
|
-
removeAllListeners: (...args) => ev.removeAllListeners(...args)
|
|
110
|
+
removeAllListeners: (...args) => ev.removeAllListeners(...args)
|
|
116
111
|
};
|
|
117
112
|
};
|
|
118
|
-
exports.makeEventBuffer = makeEventBuffer;
|
|
119
113
|
const makeBufferData = () => {
|
|
120
114
|
return {
|
|
121
115
|
historySets: {
|
|
@@ -138,8 +132,9 @@ const makeBufferData = () => {
|
|
|
138
132
|
groupUpdates: {}
|
|
139
133
|
};
|
|
140
134
|
};
|
|
141
|
-
function append(data, historyCache, event,
|
|
142
|
-
|
|
135
|
+
function append(data, historyCache, event,
|
|
136
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
137
|
+
eventData, logger) {
|
|
143
138
|
switch (event) {
|
|
144
139
|
case 'messaging-history.set':
|
|
145
140
|
for (const chat of eventData.chats) {
|
|
@@ -156,7 +151,7 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
156
151
|
for (const contact of eventData.contacts) {
|
|
157
152
|
const existingContact = data.historySets.contacts[contact.id];
|
|
158
153
|
if (existingContact) {
|
|
159
|
-
Object.assign(existingContact,
|
|
154
|
+
Object.assign(existingContact, trimUndefined(contact));
|
|
160
155
|
}
|
|
161
156
|
else {
|
|
162
157
|
const historyContactId = `c:${contact.id}`;
|
|
@@ -185,7 +180,7 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
185
180
|
for (const chat of eventData) {
|
|
186
181
|
let upsert = data.chatUpserts[chat.id];
|
|
187
182
|
if (!upsert) {
|
|
188
|
-
upsert = data.historySets[chat.id];
|
|
183
|
+
upsert = data.historySets.chats[chat.id];
|
|
189
184
|
if (upsert) {
|
|
190
185
|
logger.debug({ chatId: chat.id }, 'absorbed chat upsert in chat set');
|
|
191
186
|
}
|
|
@@ -259,14 +254,14 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
259
254
|
}
|
|
260
255
|
}
|
|
261
256
|
if (upsert) {
|
|
262
|
-
upsert = Object.assign(upsert,
|
|
257
|
+
upsert = Object.assign(upsert, trimUndefined(contact));
|
|
263
258
|
}
|
|
264
259
|
else {
|
|
265
260
|
upsert = contact;
|
|
266
261
|
data.contactUpserts[contact.id] = upsert;
|
|
267
262
|
}
|
|
268
263
|
if (data.contactUpdates[contact.id]) {
|
|
269
|
-
upsert = Object.assign(data.contactUpdates[contact.id],
|
|
264
|
+
upsert = Object.assign(data.contactUpdates[contact.id], trimUndefined(contact));
|
|
270
265
|
delete data.contactUpdates[contact.id];
|
|
271
266
|
}
|
|
272
267
|
}
|
|
@@ -291,7 +286,7 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
291
286
|
const { messages, type } = eventData;
|
|
292
287
|
for (const message of messages) {
|
|
293
288
|
const key = stringifyMessageKey(message.key);
|
|
294
|
-
let existing =
|
|
289
|
+
let existing = data.messageUpserts[key]?.message;
|
|
295
290
|
if (!existing) {
|
|
296
291
|
existing = data.historySets.messages[key];
|
|
297
292
|
if (existing) {
|
|
@@ -312,9 +307,7 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
312
307
|
else {
|
|
313
308
|
data.messageUpserts[key] = {
|
|
314
309
|
message,
|
|
315
|
-
type: type === 'notify' ||
|
|
316
|
-
? 'notify'
|
|
317
|
-
: type
|
|
310
|
+
type: type === 'notify' || data.messageUpserts[key]?.type === 'notify' ? 'notify' : type
|
|
318
311
|
};
|
|
319
312
|
}
|
|
320
313
|
}
|
|
@@ -323,13 +316,13 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
323
316
|
const msgUpdates = eventData;
|
|
324
317
|
for (const { key, update } of msgUpdates) {
|
|
325
318
|
const keyStr = stringifyMessageKey(key);
|
|
326
|
-
const existing = data.historySets.messages[keyStr] ||
|
|
319
|
+
const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message;
|
|
327
320
|
if (existing) {
|
|
328
321
|
Object.assign(existing, update);
|
|
329
322
|
// if the message was received & read by us
|
|
330
323
|
// the chat counter must have been incremented
|
|
331
324
|
// so we need to decrement it
|
|
332
|
-
if (update.status ===
|
|
325
|
+
if (update.status === WAMessageStatus.READ && !key.fromMe) {
|
|
333
326
|
decrementChatReadCounterIfMsgDidUnread(existing);
|
|
334
327
|
}
|
|
335
328
|
}
|
|
@@ -367,12 +360,11 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
367
360
|
const keyStr = stringifyMessageKey(key);
|
|
368
361
|
const existing = data.messageUpserts[keyStr];
|
|
369
362
|
if (existing) {
|
|
370
|
-
|
|
363
|
+
updateMessageWithReaction(existing.message, reaction);
|
|
371
364
|
}
|
|
372
365
|
else {
|
|
373
|
-
data.messageReactions[keyStr] = data.messageReactions[keyStr]
|
|
374
|
-
|
|
375
|
-
(0, messages_1.updateMessageWithReaction)(data.messageReactions[keyStr], reaction);
|
|
366
|
+
data.messageReactions[keyStr] = data.messageReactions[keyStr] || { key, reactions: [] };
|
|
367
|
+
updateMessageWithReaction(data.messageReactions[keyStr], reaction);
|
|
376
368
|
}
|
|
377
369
|
}
|
|
378
370
|
break;
|
|
@@ -382,12 +374,11 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
382
374
|
const keyStr = stringifyMessageKey(key);
|
|
383
375
|
const existing = data.messageUpserts[keyStr];
|
|
384
376
|
if (existing) {
|
|
385
|
-
|
|
377
|
+
updateMessageWithReceipt(existing.message, receipt);
|
|
386
378
|
}
|
|
387
379
|
else {
|
|
388
|
-
data.messageReceipts[keyStr] = data.messageReceipts[keyStr]
|
|
389
|
-
|
|
390
|
-
(0, messages_1.updateMessageWithReceipt)(data.messageReceipts[keyStr], receipt);
|
|
380
|
+
data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || { key, userReceipt: [] };
|
|
381
|
+
updateMessageWithReceipt(data.messageReceipts[keyStr], receipt);
|
|
391
382
|
}
|
|
392
383
|
}
|
|
393
384
|
break;
|
|
@@ -426,10 +417,10 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
426
417
|
// if the message has already been marked read by us
|
|
427
418
|
const chatId = message.key.remoteJid;
|
|
428
419
|
const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId];
|
|
429
|
-
if (
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
420
|
+
if (isRealMessage(message, '') &&
|
|
421
|
+
shouldIncrementChatUnread(message) &&
|
|
422
|
+
typeof chat?.unreadCount === 'number' &&
|
|
423
|
+
chat.unreadCount > 0) {
|
|
433
424
|
logger.debug({ chatId: chat.id }, 'decrementing chat counter');
|
|
434
425
|
chat.unreadCount -= 1;
|
|
435
426
|
if (chat.unreadCount === 0) {
|
|
@@ -502,12 +493,10 @@ function consolidateEvents(data) {
|
|
|
502
493
|
return map;
|
|
503
494
|
}
|
|
504
495
|
function concatChats(a, b) {
|
|
505
|
-
if (b.unreadCount === null
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
b.unreadCount = undefined;
|
|
510
|
-
}
|
|
496
|
+
if (b.unreadCount === null && // neutralize unread counter
|
|
497
|
+
a.unreadCount < 0) {
|
|
498
|
+
a.unreadCount = undefined;
|
|
499
|
+
b.unreadCount = undefined;
|
|
511
500
|
}
|
|
512
501
|
if (typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
|
|
513
502
|
b = { ...b };
|
|
@@ -518,3 +507,4 @@ function concatChats(a, b) {
|
|
|
518
507
|
return Object.assign(a, b);
|
|
519
508
|
}
|
|
520
509
|
const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`;
|
|
510
|
+
//# sourceMappingURL=event-buffer.js.map
|