@vinzsocket/baileys 2.8.10 → 2.8.20
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/lib/Defaults/index.js +75 -73
- package/lib/Signal/Group/ciphertext-message.js +9 -9
- package/lib/Signal/Group/group-session-builder.js +21 -21
- package/lib/Signal/Group/group_cipher.js +79 -81
- package/lib/Signal/Group/index.js +2 -2
- package/lib/Signal/Group/keyhelper.js +9 -9
- package/lib/Signal/Group/queue-job.js +43 -46
- package/lib/Signal/Group/sender-chain-key.js +24 -25
- package/lib/Signal/Group/sender-key-distribution-message.js +50 -58
- package/lib/Signal/Group/sender-key-message.js +55 -60
- package/lib/Signal/Group/sender-key-name.js +38 -39
- package/lib/Signal/Group/sender-key-record.js +39 -41
- package/lib/Signal/Group/sender-key-state.js +85 -89
- package/lib/Signal/Group/sender-message-key.js +22 -22
- package/lib/Signal/libsignal.js +387 -292
- package/lib/Signal/lid-mapping.js +98 -93
- package/lib/Socket/Client/types.js +6 -6
- package/lib/Socket/Client/websocket.js +44 -44
- package/lib/Socket/business.js +350 -365
- package/lib/Socket/chats.js +1019 -914
- package/lib/Socket/communities.js +454 -376
- package/lib/Socket/groups.js +378 -310
- package/lib/Socket/index.js +11 -11
- package/lib/Socket/messages-recv.js +1355 -1182
- package/lib/Socket/messages-send.js +1224 -1081
- package/lib/Socket/mex.js +40 -34
- package/lib/Socket/newsletter.js +212 -169
- package/lib/Socket/socket.js +896 -757
- package/lib/Store/index.js +1 -1
- package/lib/Store/make-cache-manager-store.js +75 -79
- package/lib/Store/make-in-memory-store.js +450 -389
- package/lib/Store/make-ordered-dictionary.js +73 -76
- package/lib/Store/object-repository.js +24 -22
- package/lib/Types/Chat.js +1 -7
- package/lib/Types/Label.js +20 -20
- package/lib/Types/LabelAssociation.js +2 -2
- package/lib/Types/Message.js +2 -2
- package/lib/Types/Newsletter.js +24 -24
- package/lib/Types/Socket.js +1 -1
- package/lib/Types/State.js +8 -8
- package/lib/Types/index.js +10 -10
- package/lib/Utils/auth-utils.js +408 -401
- package/lib/Utils/baileys-event-stream.js +44 -35
- package/lib/Utils/business.js +200 -197
- package/lib/Utils/chat-utils.js +775 -708
- package/lib/Utils/crypto.js +97 -85
- package/lib/Utils/decode-wa-message.js +240 -227
- package/lib/Utils/event-buffer.js +547 -518
- package/lib/Utils/extended-messages.js +250 -203
- package/lib/Utils/generics.js +300 -289
- package/lib/Utils/history.js +76 -69
- package/lib/Utils/lid-resolver.js +47 -42
- package/lib/Utils/link-preview.js +75 -65
- package/lib/Utils/logger.js +3 -1
- package/lib/Utils/lt-hash.js +38 -34
- package/lib/Utils/make-mutex.js +32 -34
- package/lib/Utils/mention.js +79 -86
- package/lib/Utils/message-retry-manager.js +148 -136
- package/lib/Utils/messages-media.js +652 -562
- package/lib/Utils/messages.js +753 -707
- package/lib/Utils/noise-handler.js +156 -135
- package/lib/Utils/process-message.js +404 -341
- package/lib/Utils/signal.js +180 -118
- package/lib/Utils/use-multi-file-auth-state.js +87 -91
- package/lib/Utils/validate-connection.js +167 -142
- package/lib/WABinary/constants.js +28 -1293
- package/lib/WABinary/decode.js +220 -218
- package/lib/WABinary/encode.js +199 -201
- package/lib/WABinary/generic-utils.js +80 -77
- package/lib/WABinary/jid-utils.js +41 -35
- package/lib/WAM/BinaryInfo.js +7 -7
- package/lib/WAM/constants.js +1 -15241
- package/lib/WAM/encode.js +124 -131
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +51 -47
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -22
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -32
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -19
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -43
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +17 -17
- package/lib/WAUSync/USyncQuery.js +82 -84
- package/lib/WAUSync/USyncUser.js +20 -20
- package/package.json +5 -2
package/lib/Utils/crypto.js
CHANGED
|
@@ -3,38 +3,46 @@ import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes }
|
|
|
3
3
|
import * as libsignal from 'libsignal';
|
|
4
4
|
import { KEY_BUNDLE_TYPE } from '../Defaults/index.js';
|
|
5
5
|
// insure browser & node compatibility
|
|
6
|
-
const {
|
|
6
|
+
const {
|
|
7
|
+
subtle
|
|
8
|
+
} = globalThis.crypto;
|
|
7
9
|
/** prefix version byte to the pub keys, required for some curve crypto functions */
|
|
8
|
-
export const generateSignalPubKey =
|
|
10
|
+
export const generateSignalPubKey = pubKey => pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey]);
|
|
9
11
|
export const Curve = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
12
|
+
generateKeyPair: () => {
|
|
13
|
+
const {
|
|
14
|
+
pubKey,
|
|
15
|
+
privKey
|
|
16
|
+
} = libsignal.curve.generateKeyPair();
|
|
17
|
+
return {
|
|
18
|
+
private: Buffer.from(privKey),
|
|
19
|
+
// remove version byte
|
|
20
|
+
public: Buffer.from(pubKey.slice(1))
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
sharedKey: (privateKey, publicKey) => {
|
|
24
|
+
const shared = libsignal.curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey);
|
|
25
|
+
return Buffer.from(shared);
|
|
26
|
+
},
|
|
27
|
+
sign: (privateKey, buf) => libsignal.curve.calculateSignature(privateKey, buf),
|
|
28
|
+
verify: (pubKey, message, signature) => {
|
|
29
|
+
try {
|
|
30
|
+
libsignal.curve.verifySignature(generateSignalPubKey(pubKey), message, signature);
|
|
31
|
+
return true;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
return false;
|
|
31
34
|
}
|
|
35
|
+
}
|
|
32
36
|
};
|
|
33
37
|
export const signedKeyPair = (identityKeyPair, keyId) => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
const preKey = Curve.generateKeyPair();
|
|
39
|
+
const pubKey = generateSignalPubKey(preKey.public);
|
|
40
|
+
const signature = Curve.sign(identityKeyPair.private, pubKey);
|
|
41
|
+
return {
|
|
42
|
+
keyPair: preKey,
|
|
43
|
+
signature,
|
|
44
|
+
keyId
|
|
45
|
+
};
|
|
38
46
|
};
|
|
39
47
|
const GCM_TAG_LENGTH = 128 >> 3;
|
|
40
48
|
/**
|
|
@@ -42,97 +50,101 @@ const GCM_TAG_LENGTH = 128 >> 3;
|
|
|
42
50
|
* where the tag tag is suffixed to the ciphertext
|
|
43
51
|
* */
|
|
44
52
|
export function aesEncryptGCM(plaintext, key, iv, additionalData) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
const cipher = createCipheriv('aes-256-gcm', key, iv);
|
|
54
|
+
cipher.setAAD(additionalData);
|
|
55
|
+
return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
|
|
48
56
|
}
|
|
49
57
|
/**
|
|
50
58
|
* decrypt AES 256 GCM;
|
|
51
59
|
* where the auth tag is suffixed to the ciphertext
|
|
52
60
|
* */
|
|
53
61
|
export function aesDecryptGCM(ciphertext, key, iv, additionalData) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
+
const decipher = createDecipheriv('aes-256-gcm', key, iv);
|
|
63
|
+
// decrypt additional adata
|
|
64
|
+
const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
|
|
65
|
+
const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
|
|
66
|
+
// set additional data
|
|
67
|
+
decipher.setAAD(additionalData);
|
|
68
|
+
decipher.setAuthTag(tag);
|
|
69
|
+
return Buffer.concat([decipher.update(enc), decipher.final()]);
|
|
62
70
|
}
|
|
63
71
|
export function aesEncryptCTR(plaintext, key, iv) {
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
const cipher = createCipheriv('aes-256-ctr', key, iv);
|
|
73
|
+
return Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
66
74
|
}
|
|
67
75
|
export function aesDecryptCTR(ciphertext, key, iv) {
|
|
68
|
-
|
|
69
|
-
|
|
76
|
+
const decipher = createDecipheriv('aes-256-ctr', key, iv);
|
|
77
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
70
78
|
}
|
|
71
79
|
/** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
|
|
72
80
|
export function aesDecrypt(buffer, key) {
|
|
73
|
-
|
|
81
|
+
return aesDecryptWithIV(buffer.slice(16, buffer.length), key, buffer.slice(0, 16));
|
|
74
82
|
}
|
|
75
83
|
/** decrypt AES 256 CBC */
|
|
76
84
|
export function aesDecryptWithIV(buffer, key, IV) {
|
|
77
|
-
|
|
78
|
-
|
|
85
|
+
const aes = createDecipheriv('aes-256-cbc', key, IV);
|
|
86
|
+
return Buffer.concat([aes.update(buffer), aes.final()]);
|
|
79
87
|
}
|
|
80
88
|
// encrypt AES 256 CBC; where a random IV is prefixed to the buffer
|
|
81
89
|
export function aesEncrypt(buffer, key) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
90
|
+
const IV = randomBytes(16);
|
|
91
|
+
const aes = createCipheriv('aes-256-cbc', key, IV);
|
|
92
|
+
return Buffer.concat([IV, aes.update(buffer), aes.final()]); // prefix IV to the buffer
|
|
85
93
|
}
|
|
86
94
|
// encrypt AES 256 CBC with a given IV
|
|
87
95
|
export function aesEncrypWithIV(buffer, key, IV) {
|
|
88
|
-
|
|
89
|
-
|
|
96
|
+
const aes = createCipheriv('aes-256-cbc', key, IV);
|
|
97
|
+
return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer
|
|
90
98
|
}
|
|
91
99
|
// sign HMAC using SHA 256
|
|
92
100
|
export function hmacSign(buffer, key, variant = 'sha256') {
|
|
93
|
-
|
|
101
|
+
return createHmac(variant, key).update(buffer).digest();
|
|
94
102
|
}
|
|
95
103
|
export function sha256(buffer) {
|
|
96
|
-
|
|
104
|
+
return createHash('sha256').update(buffer).digest();
|
|
97
105
|
}
|
|
98
106
|
export function md5(buffer) {
|
|
99
|
-
|
|
107
|
+
return createHash('md5').update(buffer).digest();
|
|
100
108
|
}
|
|
101
109
|
// HKDF key expansion
|
|
102
110
|
export async function hkdf(buffer, expandedLength, info) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
111
|
+
// Ensure we have a Uint8Array for the key material
|
|
112
|
+
const inputKeyMaterial = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
113
|
+
// Set default values if not provided
|
|
114
|
+
const salt = info.salt ? new Uint8Array(info.salt) : new Uint8Array(0);
|
|
115
|
+
const infoBytes = info.info ? new TextEncoder().encode(info.info) : new Uint8Array(0);
|
|
116
|
+
// Import the input key material
|
|
117
|
+
const importedKey = await subtle.importKey('raw', inputKeyMaterial, {
|
|
118
|
+
name: 'HKDF'
|
|
119
|
+
}, false, ['deriveBits']);
|
|
120
|
+
// Derive bits using HKDF
|
|
121
|
+
const derivedBits = await subtle.deriveBits({
|
|
122
|
+
name: 'HKDF',
|
|
123
|
+
hash: 'SHA-256',
|
|
124
|
+
salt: salt,
|
|
125
|
+
info: infoBytes
|
|
126
|
+
}, importedKey, expandedLength * 8 // Convert bytes to bits
|
|
127
|
+
);
|
|
128
|
+
return Buffer.from(derivedBits);
|
|
119
129
|
}
|
|
120
130
|
export async function derivePairingCodeKey(pairingCode, salt) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
131
|
+
// Convert inputs to formats Web Crypto API can work with
|
|
132
|
+
const encoder = new TextEncoder();
|
|
133
|
+
const pairingCodeBuffer = encoder.encode(pairingCode);
|
|
134
|
+
const saltBuffer = salt instanceof Uint8Array ? salt : new Uint8Array(salt);
|
|
135
|
+
// Import the pairing code as key material
|
|
136
|
+
const keyMaterial = await subtle.importKey('raw', pairingCodeBuffer, {
|
|
137
|
+
name: 'PBKDF2'
|
|
138
|
+
}, false, ['deriveBits']);
|
|
139
|
+
// Derive bits using PBKDF2 with the same parameters
|
|
140
|
+
// 2 << 16 = 131,072 iterations
|
|
141
|
+
const derivedBits = await subtle.deriveBits({
|
|
142
|
+
name: 'PBKDF2',
|
|
143
|
+
salt: saltBuffer,
|
|
144
|
+
iterations: 2 << 16,
|
|
145
|
+
hash: 'SHA-256'
|
|
146
|
+
}, keyMaterial, 32 * 8 // 32 bytes * 8 = 256 bits
|
|
147
|
+
);
|
|
148
|
+
return Buffer.from(derivedBits);
|
|
137
149
|
}
|
|
138
150
|
//# sourceMappingURL=crypto.js.map
|