@pontalabs/baileys 1.0.10 → 1.1.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.
Files changed (119) hide show
  1. package/README.md +96 -85
  2. package/lib/Defaults/connection.js +39 -0
  3. package/lib/Defaults/constants.js +50 -0
  4. package/lib/Defaults/history.js +13 -0
  5. package/lib/Defaults/index.js +36 -151
  6. package/lib/Defaults/media.js +43 -0
  7. package/lib/Defaults/phonenumber-mcc.json +221 -221
  8. package/lib/Defaults/prefix.js +12 -0
  9. package/lib/Defaults/vialeys-version.json +7 -0
  10. package/lib/Signal/Group/ciphertext-message.js +10 -15
  11. package/lib/Signal/Group/group-session-builder.js +34 -60
  12. package/lib/Signal/Group/group_cipher.js +69 -76
  13. package/lib/Signal/Group/index.js +41 -60
  14. package/lib/Signal/Group/keyhelper.js +12 -57
  15. package/lib/Signal/Group/sender-chain-key.js +20 -33
  16. package/lib/Signal/Group/sender-key-distribution-message.js +43 -48
  17. package/lib/Signal/Group/sender-key-message.js +58 -53
  18. package/lib/Signal/Group/sender-key-name.js +21 -31
  19. package/lib/Signal/Group/sender-key-record.js +21 -34
  20. package/lib/Signal/Group/sender-key-state.js +59 -106
  21. package/lib/Signal/Group/sender-message-key.js +16 -21
  22. package/lib/Signal/libsignal.js +282 -203
  23. package/lib/Signal/lid-mapping.js +164 -184
  24. package/lib/Socket/Client/index.js +31 -22
  25. package/lib/Socket/Client/types.js +9 -15
  26. package/lib/Socket/Client/websocket.js +42 -37
  27. package/lib/Socket/business.js +212 -336
  28. package/lib/Socket/chats.js +696 -940
  29. package/lib/Socket/community.js +454 -369
  30. package/lib/Socket/groups.js +278 -280
  31. package/lib/Socket/index.js +9 -21
  32. package/lib/Socket/messages-recv.js +1027 -1570
  33. package/lib/Socket/messages-send.js +1180 -1136
  34. package/lib/Socket/mex.js +59 -0
  35. package/lib/Socket/newsletter.js +193 -251
  36. package/lib/Socket/socket.js +758 -898
  37. package/lib/Store/index.js +35 -24
  38. package/lib/Store/make-cache-manager-store.js +53 -66
  39. package/lib/Store/make-in-memory-store.js +319 -325
  40. package/lib/Store/make-ordered-dictionary.js +49 -57
  41. package/lib/Store/object-repository.js +10 -15
  42. package/lib/Types/Auth.js +2 -3
  43. package/lib/Types/Bussines.js +2 -3
  44. package/lib/Types/Call.js +2 -3
  45. package/lib/Types/Chat.js +10 -9
  46. package/lib/Types/Contact.js +2 -3
  47. package/lib/Types/Events.js +2 -3
  48. package/lib/Types/GroupMetadata.js +2 -3
  49. package/lib/Types/Label.js +6 -12
  50. package/lib/Types/LabelAssociation.js +4 -13
  51. package/lib/Types/Message.js +10 -18
  52. package/lib/Types/MexUpdates.js +15 -18
  53. package/lib/Types/Newsletter.js +26 -34
  54. package/lib/Types/Product.js +2 -3
  55. package/lib/Types/Signal.js +2 -3
  56. package/lib/Types/Socket.js +2 -3
  57. package/lib/Types/State.js +8 -13
  58. package/lib/Types/USync.js +2 -3
  59. package/lib/Types/index.js +49 -40
  60. package/lib/Utils/auth-utils.js +190 -495
  61. package/lib/Utils/browser-utils.js +29 -0
  62. package/lib/Utils/business.js +177 -199
  63. package/lib/Utils/chat-utils.js +711 -624
  64. package/lib/Utils/crypto.js +116 -161
  65. package/lib/Utils/decode-wa-message.js +225 -321
  66. package/lib/Utils/event-buffer.js +363 -405
  67. package/lib/Utils/generics.js +318 -472
  68. package/lib/Utils/history.js +72 -76
  69. package/lib/Utils/index.js +49 -41
  70. package/lib/Utils/link-preview.js +59 -103
  71. package/lib/Utils/logger.js +4 -7
  72. package/lib/Utils/lt-hash.js +29 -42
  73. package/lib/Utils/make-mutex.js +26 -42
  74. package/lib/Utils/message-retry-manager.js +134 -160
  75. package/lib/Utils/messages-media.js +665 -731
  76. package/lib/Utils/messages.js +518 -1
  77. package/lib/Utils/noise-handler.js +127 -125
  78. package/lib/Utils/pre-key-manager.js +86 -0
  79. package/lib/Utils/process-message.js +739 -351
  80. package/lib/Utils/signal.js +146 -129
  81. package/lib/Utils/use-multi-file-auth-state.js +68 -135
  82. package/lib/Utils/validate-connection.js +183 -142
  83. package/lib/Utils/vialeys-event-stream.js +41 -0
  84. package/lib/WABinary/constants.js +1281 -1289
  85. package/lib/WABinary/decode.js +145 -200
  86. package/lib/WABinary/encode.js +149 -202
  87. package/lib/WABinary/generic-utils.js +79 -97
  88. package/lib/WABinary/index.js +36 -25
  89. package/lib/WABinary/jid-utils.js +91 -123
  90. package/lib/WABinary/types.js +2 -3
  91. package/lib/WAM/BinaryInfo.js +8 -13
  92. package/lib/WAM/constants.js +16584 -11464
  93. package/lib/WAM/encode.js +117 -133
  94. package/lib/WAM/index.js +34 -23
  95. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +62 -69
  96. package/lib/WAUSync/Protocols/USyncContactProtocol.js +11 -25
  97. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +32 -44
  98. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +13 -23
  99. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +24 -38
  100. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +17 -31
  101. package/lib/WAUSync/Protocols/index.js +37 -26
  102. package/lib/WAUSync/USyncQuery.js +66 -66
  103. package/lib/WAUSync/USyncUser.js +13 -18
  104. package/lib/WAUSync/index.js +34 -23
  105. package/lib/index.js +95 -28
  106. package/package.json +4 -3
  107. package/lib/Defaults/baileys-version.json +0 -3
  108. package/lib/Signal/Group/queue-job.d.ts +0 -1
  109. package/lib/Signal/Group/queue-job.js +0 -57
  110. package/lib/Socket/usync.d.ts +0 -37
  111. package/lib/Socket/usync.js +0 -83
  112. package/lib/Utils/baileys-event-stream.d.ts +0 -18
  113. package/lib/Utils/baileys-event-stream.js +0 -70
  114. package/lib/Utils/signal-housekeeping.d.ts +0 -19
  115. package/lib/Utils/signal-housekeeping.js +0 -73
  116. package/lib/Utils/use-mongo-file-auth-state.d.ts +0 -6
  117. package/lib/Utils/use-mongo-file-auth-state.js +0 -84
  118. package/lib/Utils/use-single-file-auth-state.d.ts +0 -13
  119. package/lib/Utils/use-single-file-auth-state.js +0 -80
@@ -1,189 +1,144 @@
1
- "use strict"
2
-
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k
5
- var desc = Object.getOwnPropertyDescriptor(m, k)
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k] } }
8
- }
9
- Object.defineProperty(o, k2, desc)
10
- } : function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k
12
- o[k2] = m[k]
13
- })
14
-
15
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? function(o, v) {
16
- Object.defineProperty(o, "default", { enumerable: true, value: v })
17
- } : function(o, v) {
18
- o["default"] = v
19
- })
20
-
21
- var __importStar = (this && this.__importStar) || function(mod) {
22
- if (mod && mod.__esModule) return mod
23
- var result = {}
24
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k)
25
- __setModuleDefault(result, mod)
26
- return result
27
- }
28
-
29
- Object.defineProperty(exports, "__esModule", { value: true })
30
-
31
- const crypto_1 = require("crypto")
32
- const libsignal = __importStar(require("libsignal"))
33
- const Defaults_1 = require("../Defaults")
34
-
35
- const generateSignalPubKey = (pubKey) => (
36
- pubKey.length === 33 ? pubKey : Buffer.concat([Defaults_1.KEY_BUNDLE_TYPE, pubKey])
37
- )
38
-
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const {
4
+ createCipheriv: createCipheriv,
5
+ createDecipheriv: createDecipheriv,
6
+ createHash: createHash,
7
+ createHmac: createHmac,
8
+ randomBytes: randomBytes,
9
+ } = require("crypto");
10
+ const { curve: curve } = require("libsignal");
11
+ const { KEY_BUNDLE_TYPE: KEY_BUNDLE_TYPE } = require("../Defaults/constants");
12
+ const generateSignalPubKey = (pubKey) =>
13
+ pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey]);
39
14
  const Curve = {
40
- generateKeyPair: () => {
41
- const { pubKey, privKey } = libsignal.curve.generateKeyPair()
42
- return {
43
- private: Buffer.from(privKey),
44
- public: Buffer.from(pubKey.slice(1))
45
- }
46
- },
47
- sharedKey: (privateKey, publicKey) => {
48
- const shared = libsignal.curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey)
49
- return Buffer.from(shared)
50
- },
51
- sign: (privateKey, buf) => libsignal.curve.calculateSignature(privateKey, buf),
52
- verify: (pubKey, message, signature) => {
53
- try {
54
- libsignal.curve.verifySignature(generateSignalPubKey(pubKey), message, signature)
55
- return true
56
- } catch {
57
- return false
58
- }
59
- }
60
- }
61
-
15
+ generateKeyPair: () => {
16
+ const { pubKey: pubKey, privKey: privKey } = curve.generateKeyPair();
17
+ return {
18
+ private: Buffer.from(privKey),
19
+ public: Buffer.from(pubKey.slice(1)),
20
+ };
21
+ },
22
+ sharedKey: (privateKey, publicKey) => {
23
+ const shared = curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey);
24
+ return Buffer.from(shared);
25
+ },
26
+ sign: (privateKey, buf) => curve.calculateSignature(privateKey, buf),
27
+ verify: (pubKey, message, signature) => {
28
+ try {
29
+ curve.verifySignature(generateSignalPubKey(pubKey), message, signature);
30
+ return true;
31
+ } catch {
32
+ return false;
33
+ }
34
+ },
35
+ };
62
36
  const signedKeyPair = (identityKeyPair, keyId) => {
63
- const preKey = Curve.generateKeyPair()
64
- const pubKey = generateSignalPubKey(preKey.public)
65
- const signature = Curve.sign(identityKeyPair.private, pubKey)
66
- return { keyPair: preKey, signature, keyId }
67
- }
68
-
69
- const GCM_TAG_LENGTH = 128 >> 3
70
-
37
+ const preKey = Curve.generateKeyPair();
38
+ const pubKey = generateSignalPubKey(preKey.public);
39
+ const signature = Curve.sign(identityKeyPair.private, pubKey);
40
+ return { keyPair: preKey, signature: signature, keyId: keyId };
41
+ };
42
+ const GCM_TAG_LENGTH = 128 >> 3;
71
43
  function aesEncryptGCM(plaintext, key, iv, additionalData) {
72
- const cipher = crypto_1.createCipheriv('aes-256-gcm', key, iv)
73
- cipher.setAAD(additionalData)
74
- return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()])
44
+ const cipher = createCipheriv("aes-256-gcm", key, iv);
45
+ cipher.setAAD(additionalData);
46
+ return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
75
47
  }
76
-
77
48
  function aesDecryptGCM(ciphertext, key, iv, additionalData) {
78
- const decipher = crypto_1.createDecipheriv('aes-256-gcm', key, iv)
79
- const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH)
80
- const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH)
81
- decipher.setAAD(additionalData)
82
- decipher.setAuthTag(tag)
83
- return Buffer.concat([decipher.update(enc), decipher.final()])
49
+ const decipher = createDecipheriv("aes-256-gcm", key, iv);
50
+ const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
51
+ const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
52
+ if (additionalData) decipher.setAAD(additionalData);
53
+ decipher.setAuthTag(tag);
54
+ return Buffer.concat([decipher.update(enc), decipher.final()]);
84
55
  }
85
-
86
56
  function aesEncryptCTR(plaintext, key, iv) {
87
- const cipher = crypto_1.createCipheriv('aes-256-ctr', key, iv)
88
- return Buffer.concat([cipher.update(plaintext), cipher.final()])
57
+ const cipher = createCipheriv("aes-256-ctr", key, iv);
58
+ return Buffer.concat([cipher.update(plaintext), cipher.final()]);
89
59
  }
90
-
91
60
  function aesDecryptCTR(ciphertext, key, iv) {
92
- const decipher = crypto_1.createDecipheriv('aes-256-ctr', key, iv)
93
- return Buffer.concat([decipher.update(ciphertext), decipher.final()])
61
+ const decipher = createDecipheriv("aes-256-ctr", key, iv);
62
+ return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
94
63
  }
95
-
96
64
  function aesDecrypt(buffer, key) {
97
- return aesDecryptWithIV(buffer.slice(16), key, buffer.slice(0, 16))
65
+ return aesDecryptWithIV(buffer.slice(16), key, buffer.slice(0, 16));
98
66
  }
99
-
100
67
  function aesDecryptWithIV(buffer, key, IV) {
101
- const aes = crypto_1.createDecipheriv('aes-256-cbc', key, IV)
102
- return Buffer.concat([aes.update(buffer), aes.final()])
68
+ const aes = createDecipheriv("aes-256-cbc", key, IV);
69
+ return Buffer.concat([aes.update(buffer), aes.final()]);
103
70
  }
104
-
105
71
  function aesEncrypt(buffer, key) {
106
- const IV = crypto_1.randomBytes(16)
107
- const aes = crypto_1.createCipheriv('aes-256-cbc', key, IV)
108
- return Buffer.concat([IV, aes.update(buffer), aes.final()])
72
+ const IV = randomBytes(16);
73
+ const aes = createCipheriv("aes-256-cbc", key, IV);
74
+ return Buffer.concat([IV, aes.update(buffer), aes.final()]);
109
75
  }
110
-
111
76
  function aesEncrypWithIV(buffer, key, IV) {
112
- const aes = crypto_1.createCipheriv('aes-256-cbc', key, IV)
113
- return Buffer.concat([aes.update(buffer), aes.final()])
77
+ const aes = createCipheriv("aes-256-cbc", key, IV);
78
+ return Buffer.concat([aes.update(buffer), aes.final()]);
114
79
  }
115
-
116
- function hmacSign(buffer, key, variant = 'sha256') {
117
- return crypto_1.createHmac(variant, key).update(buffer).digest()
80
+ function hmacSign(buffer, key, variant = "sha256") {
81
+ return createHmac(variant, key).update(buffer).digest();
118
82
  }
119
-
120
83
  function sha256(buffer) {
121
- return crypto_1.createHash('sha256').update(buffer).digest()
84
+ return createHash("sha256").update(buffer).digest();
122
85
  }
123
-
124
86
  function md5(buffer) {
125
- return crypto_1.createHash('md5').update(buffer).digest()
87
+ return createHash("md5").update(buffer).digest();
126
88
  }
127
-
128
89
  async function hkdf(buffer, expandedLength, info) {
129
- // Normalize to a Uint8Array whose underlying buffer is a regular ArrayBuffer (not ArrayBufferLike)
130
- // Cloning via new Uint8Array(...) guarantees the generic parameter is ArrayBuffer which satisfies WebCrypto types.
131
- const inputKeyMaterial = new Uint8Array(buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer))
132
-
133
- // Set default values if not provided
134
- const salt = info.salt ? new Uint8Array(info.salt) : new Uint8Array(0)
135
- const infoBytes = info.info ? new TextEncoder().encode(info.info) : new Uint8Array(0)
136
-
137
- // Import the input key material (cast to BufferSource to appease TS DOM typings)
138
- const importedKey = await crypto_1.subtle.importKey('raw', inputKeyMaterial, { name: 'HKDF' }, false, [
139
- 'deriveBits'
140
- ])
141
-
142
- const derivedBits = await crypto_1.subtle.deriveBits({
143
- name: 'HKDF',
144
- hash: 'SHA-256',
145
- salt,
146
- info: infoBytes
147
- }, importedKey, expandedLength * 8)
148
-
149
- return Buffer.from(derivedBits)
90
+ const inputKeyMaterial = new Uint8Array(
91
+ buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer)
92
+ );
93
+ const salt = info.salt ? new Uint8Array(info.salt) : new Uint8Array(0);
94
+ const infoBytes = info.info ? new TextEncoder().encode(info.info) : new Uint8Array(0);
95
+ const importedKey = await crypto.subtle.importKey(
96
+ "raw",
97
+ inputKeyMaterial,
98
+ { name: "HKDF" },
99
+ false,
100
+ ["deriveBits"]
101
+ );
102
+ const derivedBits = await crypto.subtle.deriveBits(
103
+ { name: "HKDF", hash: "SHA-256", salt: salt, info: infoBytes },
104
+ importedKey,
105
+ expandedLength * 8
106
+ );
107
+ return Buffer.from(derivedBits);
150
108
  }
151
-
152
109
  async function derivePairingCodeKey(pairingCode, salt) {
153
- const encoder = new TextEncoder()
154
- const pairingCodeBuffer = encoder.encode(pairingCode)
155
- const saltBuffer = new Uint8Array(salt instanceof Uint8Array ? salt : new Uint8Array(salt))
156
-
157
- // Import the pairing code as key material
158
- const keyMaterial = await crypto_1.subtle.importKey('raw', pairingCodeBuffer, { name: 'PBKDF2' }, false, [
159
- 'deriveBits'
160
- ])
161
-
162
- const derivedBits = await crypto_1.subtle.deriveBits({
163
- name: 'PBKDF2',
164
- salt: saltBuffer,
165
- iterations: 2 << 16,
166
- hash: 'SHA-256'
167
- }, keyMaterial, 32 * 8)
168
-
169
- return Buffer.from(derivedBits)
110
+ const encoder = new TextEncoder();
111
+ const pairingCodeBuffer = encoder.encode(pairingCode);
112
+ const saltBuffer = new Uint8Array(salt instanceof Uint8Array ? salt : new Uint8Array(salt));
113
+ const keyMaterial = await crypto.subtle.importKey(
114
+ "raw",
115
+ pairingCodeBuffer,
116
+ { name: "PBKDF2" },
117
+ false,
118
+ ["deriveBits"]
119
+ );
120
+ const derivedBits = await crypto.subtle.deriveBits(
121
+ { name: "PBKDF2", salt: saltBuffer, iterations: 2 << 16, hash: "SHA-256" },
122
+ keyMaterial,
123
+ 32 * 8
124
+ );
125
+ return Buffer.from(derivedBits);
170
126
  }
171
-
172
127
  module.exports = {
173
- generateSignalPubKey,
174
- Curve,
175
- signedKeyPair,
176
- aesEncryptGCM,
177
- aesDecryptGCM,
178
- aesEncryptCTR,
179
- aesDecryptCTR,
180
- aesDecrypt,
181
- aesDecryptWithIV,
182
- aesEncrypt,
183
- aesEncrypWithIV,
184
- hmacSign,
185
- sha256,
186
- md5,
187
- hkdf,
188
- derivePairingCodeKey
189
- }
128
+ generateSignalPubKey: generateSignalPubKey,
129
+ Curve: Curve,
130
+ signedKeyPair: signedKeyPair,
131
+ aesEncryptGCM: aesEncryptGCM,
132
+ aesDecryptGCM: aesDecryptGCM,
133
+ aesEncryptCTR: aesEncryptCTR,
134
+ aesDecryptCTR: aesDecryptCTR,
135
+ aesDecrypt: aesDecrypt,
136
+ aesDecryptWithIV: aesDecryptWithIV,
137
+ aesEncrypt: aesEncrypt,
138
+ aesEncrypWithIV: aesEncrypWithIV,
139
+ hmacSign: hmacSign,
140
+ sha256: sha256,
141
+ md5: md5,
142
+ hkdf: hkdf,
143
+ derivePairingCodeKey: derivePairingCodeKey,
144
+ };