@sanzoffc/baileys 3.0.1 → 3.0.3

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 (114) hide show
  1. package/LICENSE +1 -1
  2. package/WAProto/WAProto.proto +769 -233
  3. package/WAProto/index.js +65801 -141371
  4. package/lib/Defaults/index.js +117 -114
  5. package/lib/Defaults/index.js.bak +123 -0
  6. package/lib/KeyDB/BinarySearch.js +20 -0
  7. package/lib/KeyDB/KeyedDB.js +167 -0
  8. package/lib/KeyDB/index.js +4 -0
  9. package/lib/Signal/Group/ciphertext-message.js +12 -14
  10. package/lib/Signal/Group/group-session-builder.js +10 -42
  11. package/lib/Signal/Group/group_cipher.js +75 -87
  12. package/lib/Signal/Group/index.js +13 -57
  13. package/lib/Signal/Group/keyhelper.js +17 -52
  14. package/lib/Signal/Group/sender-chain-key.js +27 -33
  15. package/lib/Signal/Group/sender-key-distribution-message.js +62 -63
  16. package/lib/Signal/Group/sender-key-message.js +65 -66
  17. package/lib/Signal/Group/sender-key-name.js +45 -44
  18. package/lib/Signal/Group/sender-key-record.js +39 -49
  19. package/lib/Signal/Group/sender-key-state.js +80 -93
  20. package/lib/Signal/Group/sender-message-key.js +27 -28
  21. package/lib/Signal/libsignal.js +313 -163
  22. package/lib/Signal/lid-mapping.js +155 -0
  23. package/lib/Socket/Client/index.js +4 -18
  24. package/lib/Socket/Client/types.js +12 -12
  25. package/lib/Socket/Client/websocket.js +51 -71
  26. package/lib/Socket/Client/websocket.js.bak +53 -0
  27. package/lib/Socket/business.js +359 -242
  28. package/lib/Socket/chats.js +858 -945
  29. package/lib/Socket/communities.js +413 -0
  30. package/lib/Socket/groups.js +304 -324
  31. package/lib/Socket/index.js +15 -9
  32. package/lib/Socket/messages-recv.js +1105 -1046
  33. package/lib/Socket/messages-send.js +615 -389
  34. package/lib/Socket/mex.js +45 -0
  35. package/lib/Socket/newsletter.js +224 -227
  36. package/lib/Socket/socket.js +795 -621
  37. package/lib/Store/index.js +6 -8
  38. package/lib/Store/make-cache-manager-store.js +75 -0
  39. package/lib/Store/make-in-memory-store.js +286 -435
  40. package/lib/Store/make-ordered-dictionary.js +77 -79
  41. package/lib/Store/object-repository.js +24 -26
  42. package/lib/Types/Auth.js +3 -2
  43. package/lib/Types/Bussines.js +3 -0
  44. package/lib/Types/Call.js +3 -2
  45. package/lib/Types/Chat.js +9 -4
  46. package/lib/Types/Contact.js +3 -2
  47. package/lib/Types/Events.js +3 -2
  48. package/lib/Types/GroupMetadata.js +3 -2
  49. package/lib/Types/Label.js +24 -26
  50. package/lib/Types/LabelAssociation.js +6 -8
  51. package/lib/Types/Message.js +12 -7
  52. package/lib/Types/Newsletter.js +32 -17
  53. package/lib/Types/Newsletter.js.bak +33 -0
  54. package/lib/Types/Product.js +3 -2
  55. package/lib/Types/Signal.js +3 -2
  56. package/lib/Types/Socket.js +4 -2
  57. package/lib/Types/State.js +11 -2
  58. package/lib/Types/USync.js +3 -2
  59. package/lib/Types/index.js +27 -41
  60. package/lib/Utils/auth-utils.js +211 -191
  61. package/lib/Utils/baileys-event-stream.js +44 -0
  62. package/lib/Utils/browser-utils.js +21 -31
  63. package/lib/Utils/business.js +213 -214
  64. package/lib/Utils/chat-utils.js +711 -689
  65. package/lib/Utils/crypto.js +112 -175
  66. package/lib/Utils/decode-wa-message.js +254 -194
  67. package/lib/Utils/event-buffer.js +510 -500
  68. package/lib/Utils/generics.js +318 -430
  69. package/lib/Utils/history.js +83 -90
  70. package/lib/Utils/index.js +21 -35
  71. package/lib/Utils/link-preview.js +71 -116
  72. package/lib/Utils/logger.js +5 -7
  73. package/lib/Utils/lt-hash.js +40 -46
  74. package/lib/Utils/make-mutex.js +34 -41
  75. package/lib/Utils/message-retry-manager.js +33 -48
  76. package/lib/Utils/messages-media.js +573 -825
  77. package/lib/Utils/messages.js +349 -489
  78. package/lib/Utils/noise-handler.js +138 -144
  79. package/lib/Utils/pre-key-manager.js +85 -0
  80. package/lib/Utils/process-message.js +321 -384
  81. package/lib/Utils/signal.js +147 -139
  82. package/lib/Utils/use-multi-file-auth-state.js +95 -109
  83. package/lib/Utils/validate-connection.js +183 -212
  84. package/lib/WABinary/constants.js +1298 -1298
  85. package/lib/WABinary/decode.js +231 -256
  86. package/lib/WABinary/encode.js +207 -239
  87. package/lib/WABinary/generic-utils.js +119 -40
  88. package/lib/WABinary/index.js +7 -21
  89. package/lib/WABinary/jid-utils.js +87 -79
  90. package/lib/WABinary/types.js +3 -2
  91. package/lib/WAM/BinaryInfo.js +10 -12
  92. package/lib/WAM/constants.js +22851 -15348
  93. package/lib/WAM/encode.js +135 -136
  94. package/lib/WAM/index.js +5 -19
  95. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -30
  96. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +49 -53
  97. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -28
  98. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +36 -39
  99. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
  100. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +26 -20
  101. package/lib/WAUSync/Protocols/index.js +6 -20
  102. package/lib/WAUSync/USyncQuery.js +86 -85
  103. package/lib/WAUSync/USyncUser.js +23 -25
  104. package/lib/WAUSync/index.js +5 -19
  105. package/lib/index.js +18 -49
  106. package/package.json +65 -78
  107. package/README.MD +0 -1295
  108. package/WAProto/GenerateStatics.sh +0 -4
  109. package/WAProto/p.html +0 -1
  110. package/engine-requirements.js +0 -10
  111. package/lib/Defaults/wileys-version.json +0 -3
  112. package/lib/Signal/Group/queue-job.js +0 -57
  113. package/lib/Socket/usync.js +0 -70
  114. package/lib/Utils/wileys-event-stream.js +0 -63
@@ -1,193 +1,130 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
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 () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.signedKeyPair = exports.Curve = exports.generateSignalPubKey = void 0;
37
- exports.aesEncryptGCM = aesEncryptGCM;
38
- exports.aesDecryptGCM = aesDecryptGCM;
39
- exports.aesEncryptCTR = aesEncryptCTR;
40
- exports.aesDecryptCTR = aesDecryptCTR;
41
- exports.aesDecrypt = aesDecrypt;
42
- exports.aesDecryptWithIV = aesDecryptWithIV;
43
- exports.aesEncrypt = aesEncrypt;
44
- exports.aesEncrypWithIV = aesEncrypWithIV;
45
- exports.hmacSign = hmacSign;
46
- exports.sha256 = sha256;
47
- exports.md5 = md5;
48
- exports.hkdf = hkdf;
49
- exports.derivePairingCodeKey = derivePairingCodeKey;
50
- const crypto_1 = require("crypto");
51
- const libsignal = __importStar(require("libsignal"));
52
- const Defaults_1 = require("../Defaults");
53
- // insure browser & node compatibility
1
+ //=======================================================//
2
+ import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from "crypto";
3
+ import { KEY_BUNDLE_TYPE } from "../Defaults/index.js";
4
+ import * as curve from "@skycodee/libsignal/src/curve.js";
5
+ //=======================================================//
54
6
  const { subtle } = globalThis.crypto;
55
- /** prefix version byte to the pub keys, required for some curve crypto functions */
56
- const generateSignalPubKey = (pubKey) => (pubKey.length === 33
57
- ? pubKey
58
- : Buffer.concat([Defaults_1.KEY_BUNDLE_TYPE, pubKey]));
59
- exports.generateSignalPubKey = generateSignalPubKey;
60
- exports.Curve = {
61
- generateKeyPair: () => {
62
- const { pubKey, privKey } = libsignal.curve.generateKeyPair();
63
- return {
64
- private: Buffer.from(privKey),
65
- // remove version byte
66
- public: Buffer.from(pubKey.slice(1))
67
- };
68
- },
69
- sharedKey: (privateKey, publicKey) => {
70
- const shared = libsignal.curve.calculateAgreement((0, exports.generateSignalPubKey)(publicKey), privateKey);
71
- return Buffer.from(shared);
72
- },
73
- sign: (privateKey, buf) => (libsignal.curve.calculateSignature(privateKey, buf)),
74
- verify: (pubKey, message, signature) => {
75
- try {
76
- libsignal.curve.verifySignature((0, exports.generateSignalPubKey)(pubKey), message, signature);
77
- return true;
78
- }
79
- catch (error) {
80
- return false;
81
- }
7
+ export const generateSignalPubKey = (pubKey) => pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey]);
8
+ export const Curve = {
9
+ generateKeyPair: () => {
10
+ const { pubKey, privKey } = curve.generateKeyPair();
11
+ return {
12
+ private: Buffer.from(privKey),
13
+ public: Buffer.from(pubKey.slice(1))
14
+ };
15
+ },
16
+ sharedKey: (privateKey, publicKey) => {
17
+ const shared = curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey);
18
+ return Buffer.from(shared);
19
+ },
20
+ sign: (privateKey, buf) => curve.calculateSignature(privateKey, buf),
21
+ verify: (pubKey, message, signature) => {
22
+ try {
23
+ curve.verifySignature(generateSignalPubKey(pubKey), message, signature);
24
+ return true;
25
+ }
26
+ catch (error) {
27
+ return false;
82
28
  }
29
+ }
83
30
  };
84
- const signedKeyPair = (identityKeyPair, keyId) => {
85
- const preKey = exports.Curve.generateKeyPair();
86
- const pubKey = (0, exports.generateSignalPubKey)(preKey.public);
87
- const signature = exports.Curve.sign(identityKeyPair.private, pubKey);
88
- return { keyPair: preKey, signature, keyId };
31
+ //=======================================================//
32
+ export const signedKeyPair = (identityKeyPair, keyId) => {
33
+ const preKey = Curve.generateKeyPair();
34
+ const pubKey = generateSignalPubKey(preKey.public);
35
+ const signature = Curve.sign(identityKeyPair.private, pubKey);
36
+ return { keyPair: preKey, signature, keyId };
89
37
  };
90
- exports.signedKeyPair = signedKeyPair;
38
+ //=======================================================//
91
39
  const GCM_TAG_LENGTH = 128 >> 3;
92
- /**
93
- * encrypt AES 256 GCM;
94
- * where the tag tag is suffixed to the ciphertext
95
- * */
96
- function aesEncryptGCM(plaintext, key, iv, additionalData) {
97
- const cipher = (0, crypto_1.createCipheriv)('aes-256-gcm', key, iv);
98
- cipher.setAAD(additionalData);
99
- return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
40
+ export function aesEncryptGCM(plaintext, key, iv, additionalData) {
41
+ const cipher = createCipheriv("aes-256-gcm", key, iv);
42
+ cipher.setAAD(additionalData);
43
+ return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
100
44
  }
101
- /**
102
- * decrypt AES 256 GCM;
103
- * where the auth tag is suffixed to the ciphertext
104
- * */
105
- function aesDecryptGCM(ciphertext, key, iv, additionalData) {
106
- const decipher = (0, crypto_1.createDecipheriv)('aes-256-gcm', key, iv);
107
- // decrypt additional adata
108
- const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
109
- const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
110
- // set additional data
111
- decipher.setAAD(additionalData);
112
- decipher.setAuthTag(tag);
113
- return Buffer.concat([decipher.update(enc), decipher.final()]);
45
+ //=======================================================//
46
+ export function aesDecryptGCM(ciphertext, key, iv, additionalData) {
47
+ const decipher = createDecipheriv("aes-256-gcm", key, iv);
48
+ const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
49
+ const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
50
+ decipher.setAAD(additionalData);
51
+ decipher.setAuthTag(tag);
52
+ return Buffer.concat([decipher.update(enc), decipher.final()]);
114
53
  }
115
- function aesEncryptCTR(plaintext, key, iv) {
116
- const cipher = (0, crypto_1.createCipheriv)('aes-256-ctr', key, iv);
117
- return Buffer.concat([cipher.update(plaintext), cipher.final()]);
54
+ //=======================================================//
55
+ export function aesEncryptCTR(plaintext, key, iv) {
56
+ const cipher = createCipheriv("aes-256-ctr", key, iv);
57
+ return Buffer.concat([cipher.update(plaintext), cipher.final()]);
118
58
  }
119
- function aesDecryptCTR(ciphertext, key, iv) {
120
- const decipher = (0, crypto_1.createDecipheriv)('aes-256-ctr', key, iv);
121
- return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
59
+ //=======================================================//
60
+ export function aesDecryptCTR(ciphertext, key, iv) {
61
+ const decipher = createDecipheriv("aes-256-ctr", key, iv);
62
+ return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
122
63
  }
123
- /** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
124
- function aesDecrypt(buffer, key) {
125
- return aesDecryptWithIV(buffer.slice(16, buffer.length), key, buffer.slice(0, 16));
64
+ //=======================================================//
65
+ export function aesDecrypt(buffer, key) {
66
+ return aesDecryptWithIV(buffer.slice(16, buffer.length), key, buffer.slice(0, 16));
126
67
  }
127
- /** decrypt AES 256 CBC */
128
- function aesDecryptWithIV(buffer, key, IV) {
129
- const aes = (0, crypto_1.createDecipheriv)('aes-256-cbc', key, IV);
130
- return Buffer.concat([aes.update(buffer), aes.final()]);
68
+ //=======================================================//
69
+ export function aesDecryptWithIV(buffer, key, IV) {
70
+ const aes = createDecipheriv("aes-256-cbc", key, IV);
71
+ return Buffer.concat([aes.update(buffer), aes.final()]);
131
72
  }
132
- // encrypt AES 256 CBC; where a random IV is prefixed to the buffer
133
- function aesEncrypt(buffer, key) {
134
- const IV = (0, crypto_1.randomBytes)(16);
135
- const aes = (0, crypto_1.createCipheriv)('aes-256-cbc', key, IV);
136
- return Buffer.concat([IV, aes.update(buffer), aes.final()]); // prefix IV to the buffer
73
+ //=======================================================//
74
+ export function aesEncrypt(buffer, key) {
75
+ const IV = randomBytes(16);
76
+ const aes = createCipheriv("aes-256-cbc", key, IV);
77
+ return Buffer.concat([IV, aes.update(buffer), aes.final()]);
137
78
  }
138
- // encrypt AES 256 CBC with a given IV
139
- function aesEncrypWithIV(buffer, key, IV) {
140
- const aes = (0, crypto_1.createCipheriv)('aes-256-cbc', key, IV);
141
- return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer
79
+ //=======================================================//
80
+ export function aesEncrypWithIV(buffer, key, IV) {
81
+ const aes = createCipheriv("aes-256-cbc", key, IV);
82
+ return Buffer.concat([aes.update(buffer), aes.final()]);
142
83
  }
143
- // sign HMAC using SHA 256
144
- function hmacSign(buffer, key, variant = 'sha256') {
145
- return (0, crypto_1.createHmac)(variant, key).update(buffer).digest();
84
+ //=======================================================//
85
+ export function hmacSign(buffer, key, variant = "sha256") {
86
+ return createHmac(variant, key).update(buffer).digest();
146
87
  }
147
- function sha256(buffer) {
148
- return (0, crypto_1.createHash)('sha256').update(buffer).digest();
88
+ //=======================================================//
89
+ export function sha256(buffer) {
90
+ return createHash("sha256").update(buffer).digest();
149
91
  }
150
- function md5(buffer) {
151
- return (0, crypto_1.createHash)('md5').update(buffer).digest();
92
+ //=======================================================//
93
+ export function md5(buffer) {
94
+ return createHash("md5").update(buffer).digest();
152
95
  }
153
- // HKDF key expansion
154
- async function hkdf(buffer, expandedLength, info) {
155
- // Ensure we have a Uint8Array for the key material
156
- const inputKeyMaterial = buffer instanceof Uint8Array
157
- ? buffer
158
- : new Uint8Array(buffer);
159
- // Set default values if not provided
160
- const salt = info.salt ? new Uint8Array(info.salt) : new Uint8Array(0);
161
- const infoBytes = info.info
162
- ? new TextEncoder().encode(info.info)
163
- : new Uint8Array(0);
164
- // Import the input key material
165
- const importedKey = await subtle.importKey('raw', inputKeyMaterial, { name: 'HKDF' }, false, ['deriveBits']);
166
- // Derive bits using HKDF
167
- const derivedBits = await subtle.deriveBits({
168
- name: 'HKDF',
169
- hash: 'SHA-256',
170
- salt: salt,
171
- info: infoBytes
172
- }, importedKey, expandedLength * 8 // Convert bytes to bits
173
- );
174
- return Buffer.from(derivedBits);
96
+ //=======================================================//
97
+ export async function hkdf(buffer, expandedLength, info) {
98
+ const inputKeyMaterial = new Uint8Array(buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer));
99
+ const salt = info.salt ? new Uint8Array(info.salt) : new Uint8Array(0);
100
+ const infoBytes = info.info ? new TextEncoder().encode(info.info) : new Uint8Array(0);
101
+ const importedKey = await subtle.importKey("raw", inputKeyMaterial, { name: "HKDF" }, false, [
102
+ "deriveBits"
103
+ ]);
104
+ const derivedBits = await subtle.deriveBits({
105
+ name: "HKDF",
106
+ hash: "SHA-256",
107
+ salt: salt,
108
+ info: infoBytes
109
+ }, importedKey, expandedLength * 8
110
+ );
111
+ return Buffer.from(derivedBits);
175
112
  }
176
- async function derivePairingCodeKey(pairingCode, salt) {
177
- // Convert inputs to formats Web Crypto API can work with
178
- const encoder = new TextEncoder();
179
- const pairingCodeBuffer = encoder.encode(pairingCode);
180
- const saltBuffer = salt instanceof Uint8Array ? salt : new Uint8Array(salt);
181
- // Import the pairing code as key material
182
- const keyMaterial = await subtle.importKey('raw', pairingCodeBuffer, { name: 'PBKDF2' }, false, ['deriveBits']);
183
- // Derive bits using PBKDF2 with the same parameters
184
- // 2 << 16 = 131,072 iterations
185
- const derivedBits = await subtle.deriveBits({
186
- name: 'PBKDF2',
187
- salt: saltBuffer,
188
- iterations: 2 << 16,
189
- hash: 'SHA-256'
190
- }, keyMaterial, 32 * 8 // 32 bytes * 8 = 256 bits
191
- );
192
- return Buffer.from(derivedBits);
113
+ //=======================================================//
114
+ export async function derivePairingCodeKey(pairingCode, salt) {
115
+ const encoder = new TextEncoder();
116
+ const pairingCodeBuffer = encoder.encode(pairingCode);
117
+ const saltBuffer = new Uint8Array(salt instanceof Uint8Array ? salt : new Uint8Array(salt));
118
+ const keyMaterial = await subtle.importKey("raw", pairingCodeBuffer, { name: "PBKDF2" }, false, [
119
+ "deriveBits"
120
+ ]);
121
+ const derivedBits = await subtle.deriveBits({
122
+ name: "PBKDF2",
123
+ salt: saltBuffer,
124
+ iterations: 2 << 16,
125
+ hash: "SHA-256"
126
+ }, keyMaterial, 32 * 8
127
+ );
128
+ return Buffer.from(derivedBits);
193
129
  }
130
+ //=======================================================//