@vinzsocket/baileys 2.8.10 → 2.8.30

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