@nexustechpro/baileys 2.0.2 → 2.0.5

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 (102) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +924 -1299
  3. package/lib/Defaults/baileys-version.json +6 -2
  4. package/lib/Defaults/index.js +172 -172
  5. package/lib/Signal/libsignal.js +380 -292
  6. package/lib/Signal/lid-mapping.js +264 -171
  7. package/lib/Socket/Client/index.js +2 -2
  8. package/lib/Socket/Client/types.js +10 -10
  9. package/lib/Socket/Client/websocket.js +45 -310
  10. package/lib/Socket/business.js +375 -375
  11. package/lib/Socket/chats.js +909 -963
  12. package/lib/Socket/communities.js +430 -430
  13. package/lib/Socket/groups.js +342 -342
  14. package/lib/Socket/index.js +22 -22
  15. package/lib/Socket/messages-recv.js +777 -743
  16. package/lib/Socket/messages-send.js +295 -305
  17. package/lib/Socket/mex.js +50 -50
  18. package/lib/Socket/newsletter.js +148 -148
  19. package/lib/Socket/nexus-handler.js +75 -261
  20. package/lib/Socket/socket.js +709 -1201
  21. package/lib/Store/index.js +5 -5
  22. package/lib/Store/make-cache-manager-store.js +81 -81
  23. package/lib/Store/make-in-memory-store.js +416 -416
  24. package/lib/Store/make-ordered-dictionary.js +81 -81
  25. package/lib/Store/object-repository.js +30 -30
  26. package/lib/Types/Auth.js +1 -1
  27. package/lib/Types/Bussines.js +1 -1
  28. package/lib/Types/Call.js +1 -1
  29. package/lib/Types/Chat.js +7 -7
  30. package/lib/Types/Contact.js +1 -1
  31. package/lib/Types/Events.js +1 -1
  32. package/lib/Types/GroupMetadata.js +1 -1
  33. package/lib/Types/Label.js +24 -24
  34. package/lib/Types/LabelAssociation.js +6 -6
  35. package/lib/Types/Message.js +10 -10
  36. package/lib/Types/Newsletter.js +28 -28
  37. package/lib/Types/Product.js +1 -1
  38. package/lib/Types/Signal.js +1 -1
  39. package/lib/Types/Socket.js +2 -2
  40. package/lib/Types/State.js +12 -12
  41. package/lib/Types/USync.js +1 -1
  42. package/lib/Types/index.js +25 -25
  43. package/lib/Utils/auth-utils.js +264 -256
  44. package/lib/Utils/baileys-event-stream.js +55 -55
  45. package/lib/Utils/browser-utils.js +27 -27
  46. package/lib/Utils/business.js +228 -230
  47. package/lib/Utils/chat-utils.js +694 -764
  48. package/lib/Utils/crypto.js +109 -135
  49. package/lib/Utils/decode-wa-message.js +310 -314
  50. package/lib/Utils/event-buffer.js +547 -547
  51. package/lib/Utils/generics.js +297 -297
  52. package/lib/Utils/history.js +91 -83
  53. package/lib/Utils/index.js +21 -20
  54. package/lib/Utils/key-store.js +17 -0
  55. package/lib/Utils/link-preview.js +97 -98
  56. package/lib/Utils/logger.js +2 -2
  57. package/lib/Utils/lt-hash.js +47 -47
  58. package/lib/Utils/make-mutex.js +39 -39
  59. package/lib/Utils/message-retry-manager.js +148 -148
  60. package/lib/Utils/messages-media.js +534 -534
  61. package/lib/Utils/messages.js +705 -705
  62. package/lib/Utils/noise-handler.js +255 -255
  63. package/lib/Utils/pre-key-manager.js +105 -105
  64. package/lib/Utils/process-message.js +412 -412
  65. package/lib/Utils/signal.js +160 -158
  66. package/lib/Utils/use-multi-file-auth-state.js +120 -120
  67. package/lib/Utils/validate-connection.js +194 -194
  68. package/lib/WABinary/constants.js +1300 -1300
  69. package/lib/WABinary/decode.js +237 -237
  70. package/lib/WABinary/encode.js +232 -232
  71. package/lib/WABinary/generic-utils.js +252 -211
  72. package/lib/WABinary/index.js +5 -5
  73. package/lib/WABinary/jid-utils.js +279 -95
  74. package/lib/WABinary/types.js +1 -1
  75. package/lib/WAM/BinaryInfo.js +9 -9
  76. package/lib/WAM/constants.js +22852 -22852
  77. package/lib/WAM/encode.js +149 -149
  78. package/lib/WAM/index.js +3 -3
  79. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -28
  80. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -53
  81. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -26
  82. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -37
  83. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
  84. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -28
  85. package/lib/WAUSync/Protocols/index.js +4 -4
  86. package/lib/WAUSync/USyncQuery.js +93 -93
  87. package/lib/WAUSync/USyncUser.js +22 -22
  88. package/lib/WAUSync/index.js +3 -3
  89. package/lib/index.js +66 -66
  90. package/package.json +171 -144
  91. package/lib/Signal/Group/ciphertext-message.js +0 -12
  92. package/lib/Signal/Group/group-session-builder.js +0 -30
  93. package/lib/Signal/Group/group_cipher.js +0 -100
  94. package/lib/Signal/Group/index.js +0 -12
  95. package/lib/Signal/Group/keyhelper.js +0 -18
  96. package/lib/Signal/Group/sender-chain-key.js +0 -26
  97. package/lib/Signal/Group/sender-key-distribution-message.js +0 -63
  98. package/lib/Signal/Group/sender-key-message.js +0 -66
  99. package/lib/Signal/Group/sender-key-name.js +0 -48
  100. package/lib/Signal/Group/sender-key-record.js +0 -41
  101. package/lib/Signal/Group/sender-key-state.js +0 -84
  102. package/lib/Signal/Group/sender-message-key.js +0 -26
@@ -1,136 +1,110 @@
1
- import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from 'crypto';
2
- import * as curve from 'libsignal/src/curve.js';
3
- import { KEY_BUNDLE_TYPE } from '../Defaults/index.js';
4
- export { md5, hkdf } from 'whatsapp-rust-bridge';
5
-
6
- // insure browser & node compatibility
7
- const { subtle } = globalThis.crypto;
8
-
9
- /** prefix version byte to the pub keys, required for some curve crypto functions */
10
- export const generateSignalPubKey = (pubKey) => pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey]);
11
-
12
- export const Curve = {
13
- generateKeyPair: () => {
14
- const { pubKey, privKey } = curve.generateKeyPair();
15
- return {
16
- private: Buffer.from(privKey),
17
- // remove version byte
18
- public: Buffer.from(pubKey.slice(1))
19
- };
20
- },
21
- sharedKey: (privateKey, publicKey) => {
22
- const shared = curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey);
23
- return Buffer.from(shared);
24
- },
25
- sign: (privateKey, buf) => curve.calculateSignature(privateKey, buf),
26
- verify: (pubKey, message, signature) => {
27
- try {
28
- curve.verifySignature(generateSignalPubKey(pubKey), message, signature);
29
- return true;
30
- }
31
- catch (error) {
32
- return false;
33
- }
34
- }
35
- };
36
-
37
- export const signedKeyPair = (identityKeyPair, keyId) => {
38
- const preKey = Curve.generateKeyPair();
39
- const pubKey = generateSignalPubKey(preKey.public);
40
- const signature = Curve.sign(identityKeyPair.private, pubKey);
41
- return { keyPair: preKey, signature, keyId };
42
- };
43
-
44
- const GCM_TAG_LENGTH = 128 >> 3;
45
-
46
- /**
47
- * encrypt AES 256 GCM;
48
- * where the tag tag is suffixed to the ciphertext
49
- * */
50
- export function aesEncryptGCM(plaintext, key, iv, additionalData) {
51
- const cipher = createCipheriv('aes-256-gcm', key, iv);
52
- cipher.setAAD(additionalData);
53
- return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
54
- }
55
-
56
- /**
57
- * decrypt AES 256 GCM;
58
- * where the auth tag is suffixed to the ciphertext
59
- * */
60
- export function aesDecryptGCM(ciphertext, key, iv, additionalData) {
61
- const decipher = createDecipheriv('aes-256-gcm', key, iv);
62
- // decrypt additional adata
63
- const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
64
- const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
65
- // set additional data
66
- decipher.setAAD(additionalData);
67
- decipher.setAuthTag(tag);
68
- return Buffer.concat([decipher.update(enc), decipher.final()]);
69
- }
70
-
71
- export function aesEncryptCTR(plaintext, key, iv) {
72
- const cipher = createCipheriv('aes-256-ctr', key, iv);
73
- return Buffer.concat([cipher.update(plaintext), cipher.final()]);
74
- }
75
-
76
- export function aesDecryptCTR(ciphertext, key, iv) {
77
- const decipher = createDecipheriv('aes-256-ctr', key, iv);
78
- return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
79
- }
80
-
81
- /** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
82
- export function aesDecrypt(buffer, key) {
83
- return aesDecryptWithIV(buffer.subarray(16), key, buffer.subarray(0, 16));
84
- }
85
-
86
- /** decrypt AES 256 CBC */
87
- export function aesDecryptWithIV(buffer, key, IV) {
88
- const aes = createDecipheriv('aes-256-cbc', key, IV);
89
- return Buffer.concat([aes.update(buffer), aes.final()]);
90
- }
91
-
92
- // encrypt AES 256 CBC; where a random IV is prefixed to the buffer
93
- export function aesEncrypt(buffer, key) {
94
- const IV = randomBytes(16);
95
- const aes = createCipheriv('aes-256-cbc', key, IV);
96
- return Buffer.concat([IV, aes.update(buffer), aes.final()]); // prefix IV to the buffer
97
- }
98
-
99
- // encrypt AES 256 CBC with a given IV
100
- export function aesEncrypWithIV(buffer, key, IV) {
101
- const aes = createCipheriv('aes-256-cbc', key, IV);
102
- return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer
103
- }
104
-
105
- // sign HMAC using SHA 256
106
- export function hmacSign(buffer, key, variant = 'sha256') {
107
- return createHmac(variant, key).update(buffer).digest();
108
- }
109
-
110
- export function sha256(buffer) {
111
- return createHash('sha256').update(buffer).digest();
112
- }
113
-
114
- export async function derivePairingCodeKey(pairingCode, salt) {
115
- // Convert inputs to formats Web Crypto API can work with
116
- const encoder = new TextEncoder();
117
- const pairingCodeBuffer = encoder.encode(pairingCode);
118
- const saltBuffer = new Uint8Array(salt instanceof Uint8Array ? salt : new Uint8Array(salt));
119
-
120
- // Import the pairing code as key material
121
- const keyMaterial = await subtle.importKey('raw', pairingCodeBuffer, { name: 'PBKDF2' }, false, [
122
- 'deriveBits'
123
- ]);
124
-
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
-
135
- return Buffer.from(derivedBits);
1
+ import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from 'crypto'
2
+ import {
3
+ calculateAgreement,
4
+ calculateSignature,
5
+ verifySignature as rustVerifySignature,
6
+ generateKeyPair as rustGenerateKeyPair,
7
+ hkdf,
8
+ md5
9
+ } from 'whatsapp-rust-bridge'
10
+ import { KEY_BUNDLE_TYPE } from '../Defaults/index.js'
11
+
12
+ export { hkdf, md5 }
13
+
14
+ export const generateSignalPubKey = (pubKey) =>
15
+ pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey])
16
+
17
+ export const Curve = {
18
+ generateKeyPair: () => {
19
+ const { pubKey, privKey } = rustGenerateKeyPair()
20
+ return {
21
+ private: Buffer.from(privKey),
22
+ public: Buffer.from(pubKey.slice(1)) // remove version byte
23
+ }
24
+ },
25
+ sharedKey: (privateKey, publicKey) => {
26
+ const shared = calculateAgreement(generateSignalPubKey(publicKey), privateKey)
27
+ return Buffer.from(shared)
28
+ },
29
+ sign: (privateKey, buf) => calculateSignature(privateKey, buf),
30
+ verify: (pubKey, message, signature) => {
31
+ try {
32
+ return rustVerifySignature(generateSignalPubKey(pubKey), message, signature)
33
+ } catch {
34
+ return false
35
+ }
36
+ }
37
+ }
38
+
39
+ export const signedKeyPair = (identityKeyPair, keyId) => {
40
+ const preKey = Curve.generateKeyPair()
41
+ const pubKey = generateSignalPubKey(preKey.public)
42
+ const signature = Curve.sign(identityKeyPair.private, pubKey)
43
+ return { keyPair: preKey, signature, keyId }
44
+ }
45
+
46
+ const GCM_TAG_LENGTH = 128 >> 3
47
+
48
+ export function aesEncryptGCM(plaintext, key, iv, additionalData) {
49
+ const cipher = createCipheriv('aes-256-gcm', key, iv)
50
+ cipher.setAAD(additionalData)
51
+ return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()])
52
+ }
53
+
54
+ export function aesDecryptGCM(ciphertext, key, iv, additionalData) {
55
+ const decipher = createDecipheriv('aes-256-gcm', key, iv)
56
+ const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH)
57
+ const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH)
58
+ decipher.setAAD(additionalData)
59
+ decipher.setAuthTag(tag)
60
+ return Buffer.concat([decipher.update(enc), decipher.final()])
61
+ }
62
+
63
+ export function aesEncryptCTR(plaintext, key, iv) {
64
+ const cipher = createCipheriv('aes-256-ctr', key, iv)
65
+ return Buffer.concat([cipher.update(plaintext), cipher.final()])
66
+ }
67
+
68
+ export function aesDecryptCTR(ciphertext, key, iv) {
69
+ const decipher = createDecipheriv('aes-256-ctr', key, iv)
70
+ return Buffer.concat([decipher.update(ciphertext), decipher.final()])
71
+ }
72
+
73
+ export function aesDecrypt(buffer, key) {
74
+ return aesDecryptWithIV(buffer.subarray(16), key, buffer.subarray(0, 16))
75
+ }
76
+
77
+ export function aesDecryptWithIV(buffer, key, IV) {
78
+ const aes = createDecipheriv('aes-256-cbc', key, IV)
79
+ return Buffer.concat([aes.update(buffer), aes.final()])
80
+ }
81
+
82
+ export function aesEncrypt(buffer, key) {
83
+ const IV = randomBytes(16)
84
+ const aes = createCipheriv('aes-256-cbc', key, IV)
85
+ return Buffer.concat([IV, aes.update(buffer), aes.final()])
86
+ }
87
+
88
+ export function aesEncrypWithIV(buffer, key, IV) {
89
+ const aes = createCipheriv('aes-256-cbc', key, IV)
90
+ return Buffer.concat([aes.update(buffer), aes.final()])
91
+ }
92
+
93
+ export function hmacSign(buffer, key, variant = 'sha256') {
94
+ return createHmac(variant, key).update(buffer).digest()
95
+ }
96
+
97
+ export function sha256(buffer) {
98
+ return createHash('sha256').update(buffer).digest()
99
+ }
100
+
101
+ export async function derivePairingCodeKey(pairingCode, salt) {
102
+ const { subtle } = globalThis.crypto
103
+ const encoder = new TextEncoder()
104
+ const keyMaterial = await subtle.importKey('raw', encoder.encode(pairingCode), { name: 'PBKDF2' }, false, ['deriveBits'])
105
+ const derivedBits = await subtle.deriveBits(
106
+ { name: 'PBKDF2', salt: new Uint8Array(salt instanceof Uint8Array ? salt : new Uint8Array(salt)), iterations: 2 << 16, hash: 'SHA-256' },
107
+ keyMaterial, 256
108
+ )
109
+ return Buffer.from(derivedBits)
136
110
  }