@zervonexx/baileys 5.0.0 → 5.0.2

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 (127) hide show
  1. package/LICENSE +22 -22
  2. package/README.md +4124 -4112
  3. package/WAProto/WAProto.json +1 -0
  4. package/WAProto/WAProto.proto +7160 -0
  5. package/WAProto/WAProtoCompile.js +264 -322
  6. package/WAProto/index.js +55 -95
  7. package/lib/Builders/AIRich.js +673 -673
  8. package/lib/Builders/auto.js +49 -49
  9. package/lib/Builders/base.js +144 -144
  10. package/lib/Builders/builders.js +588 -588
  11. package/lib/Builders/index.js +3 -3
  12. package/lib/Defaults/index.js +249 -248
  13. package/lib/Signal/index.js +2 -2
  14. package/lib/Signal/libsignal.js +526 -526
  15. package/lib/Signal/lid-mapping.js +282 -282
  16. package/lib/Socket/Client/index.js +1 -1
  17. package/lib/Socket/Client/types.js +9 -9
  18. package/lib/Socket/Client/websocket.js +83 -83
  19. package/lib/Socket/business.js +151 -151
  20. package/lib/Socket/chats.js +854 -854
  21. package/lib/Socket/communities.js +146 -146
  22. package/lib/Socket/groups.js +293 -293
  23. package/lib/Socket/index.js +17 -14
  24. package/lib/Socket/messages-recv.js +993 -993
  25. package/lib/Socket/messages-send.js +722 -722
  26. package/lib/Socket/mex.js +33 -33
  27. package/lib/Socket/newsletter.js +783 -783
  28. package/lib/Socket/registration.js +154 -154
  29. package/lib/Socket/socket.js +797 -797
  30. package/lib/Socket/spam-report.js +143 -143
  31. package/lib/Socket/username.js +81 -81
  32. package/lib/Socket/zervonex-handler.js +57 -57
  33. package/lib/Store/index.js +6 -6
  34. package/lib/Store/make-cache-manager-store.js +80 -80
  35. package/lib/Store/make-in-memory-store.js +426 -426
  36. package/lib/Store/make-ordered-dictionary.js +81 -81
  37. package/lib/Store/object-repository.js +30 -30
  38. package/lib/Store/reporting-info-store.js +37 -37
  39. package/lib/Types/Auth.js +1 -1
  40. package/lib/Types/Bussines.js +1 -1
  41. package/lib/Types/Call.js +1 -1
  42. package/lib/Types/Chat.js +7 -7
  43. package/lib/Types/Contact.js +1 -1
  44. package/lib/Types/Events.js +1 -1
  45. package/lib/Types/GroupMetadata.js +1 -1
  46. package/lib/Types/Label.js +24 -24
  47. package/lib/Types/LabelAssociation.js +6 -6
  48. package/lib/Types/Message.js +14 -14
  49. package/lib/Types/Newsletter.js +99 -99
  50. package/lib/Types/Product.js +1 -1
  51. package/lib/Types/Signal.js +1 -1
  52. package/lib/Types/Socket.js +14 -14
  53. package/lib/Types/State.js +26 -26
  54. package/lib/Types/USync.js +1 -1
  55. package/lib/Types/index.js +30 -30
  56. package/lib/Utils/auth-utils.js +159 -159
  57. package/lib/Utils/baileys-event-stream.js +32 -32
  58. package/lib/Utils/bot-capabilities.js +40 -40
  59. package/lib/Utils/bot-planning-replay.js +43 -43
  60. package/lib/Utils/browser-utils.js +29 -29
  61. package/lib/Utils/business.js +74 -74
  62. package/lib/Utils/chat-utils.js +316 -316
  63. package/lib/Utils/companion-reg-client-utils.js +19 -19
  64. package/lib/Utils/consumer-application.js +37 -37
  65. package/lib/Utils/crypto.js +52 -52
  66. package/lib/Utils/cstoken.js +14 -14
  67. package/lib/Utils/decode-wa-message.js +199 -199
  68. package/lib/Utils/event-buffer.js +272 -272
  69. package/lib/Utils/extras.js +186 -186
  70. package/lib/Utils/generics.js +195 -194
  71. package/lib/Utils/history.js +50 -50
  72. package/lib/Utils/identity-change-handler.js +18 -18
  73. package/lib/Utils/index.js +40 -40
  74. package/lib/Utils/link-preview.js +50 -50
  75. package/lib/Utils/logger.js +3 -2
  76. package/lib/Utils/lt-hash.js +3 -3
  77. package/lib/Utils/make-mutex.js +15 -15
  78. package/lib/Utils/message-composer.js +61 -61
  79. package/lib/Utils/message-retry-manager.js +47 -47
  80. package/lib/Utils/messages-media.js +501 -501
  81. package/lib/Utils/messages.js +944 -942
  82. package/lib/Utils/meta-ai-msmsg.js +121 -121
  83. package/lib/Utils/meta-compositing.js +74 -74
  84. package/lib/Utils/modern-messages.js +234 -234
  85. package/lib/Utils/native-flow.js +80 -80
  86. package/lib/Utils/noise-handler.js +160 -160
  87. package/lib/Utils/pre-key-manager.js +75 -75
  88. package/lib/Utils/process-message.js +400 -400
  89. package/lib/Utils/reporting-utils.js +170 -170
  90. package/lib/Utils/signal.js +166 -166
  91. package/lib/Utils/spam-report-utils.js +165 -165
  92. package/lib/Utils/sync-action-utils.js +33 -33
  93. package/lib/Utils/tc-token-utils.js +153 -153
  94. package/lib/Utils/use-keyv-auth-state.js +199 -199
  95. package/lib/Utils/use-multi-file-auth-state.js +83 -83
  96. package/lib/Utils/validate-connection.js +171 -171
  97. package/lib/WABinary/constants.js +1320 -1320
  98. package/lib/WABinary/decode.js +151 -151
  99. package/lib/WABinary/encode.js +139 -139
  100. package/lib/WABinary/generic-utils.js +202 -202
  101. package/lib/WABinary/index.js +6 -6
  102. package/lib/WABinary/jid-utils.js +235 -235
  103. package/lib/WABinary/types.js +4 -4
  104. package/lib/WAM/BinaryInfo.js +9 -9
  105. package/lib/WAM/constants.js +22852 -22852
  106. package/lib/WAM/encode.js +149 -149
  107. package/lib/WAM/index.js +3 -3
  108. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +30 -30
  109. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +48 -48
  110. package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
  111. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +28 -28
  112. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +15 -15
  113. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +24 -24
  114. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +9 -9
  115. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +21 -21
  116. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +19 -19
  117. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +19 -19
  118. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +22 -22
  119. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +16 -16
  120. package/lib/WAUSync/Protocols/index.js +12 -12
  121. package/lib/WAUSync/USyncQuery.js +88 -88
  122. package/lib/WAUSync/USyncUser.js +20 -20
  123. package/lib/WAUSync/index.js +3 -3
  124. package/lib/index.js +66 -66
  125. package/package.json +187 -192
  126. package/WAProto/fetcher.js +0 -68
  127. package/WAProto/parser.js +0 -334
@@ -1,20 +1,20 @@
1
- export const CompanionWebClientType = Object.freeze({ UNKNOWN: 0, CHROME: 1, EDGE: 2, FIREFOX: 3, IE: 4, OPERA: 5, SAFARI: 6, ELECTRON: 7, UWP: 8, OTHER_WEB_CLIENT: 9 })
2
-
3
- const BROWSER_TO_COMPANION = {
4
- Chrome: CompanionWebClientType.CHROME,
5
- Edge: CompanionWebClientType.EDGE,
6
- Firefox: CompanionWebClientType.FIREFOX,
7
- IE: CompanionWebClientType.IE,
8
- Opera: CompanionWebClientType.OPERA,
9
- Safari: CompanionWebClientType.SAFARI
10
- }
11
-
12
- export const getCompanionWebClientType = ([os, browserName]) => {
13
- if (browserName === 'Desktop') return os === 'Windows' ? CompanionWebClientType.UWP : CompanionWebClientType.ELECTRON
14
- return BROWSER_TO_COMPANION[browserName] ?? CompanionWebClientType.OTHER_WEB_CLIENT
15
- }
16
-
17
- export const getCompanionPlatformId = browser => getCompanionWebClientType(browser).toString()
18
-
19
- export const buildPairingQRData = (ref, noiseKeyB64, identityKeyB64, advB64, browser) =>
1
+ export const CompanionWebClientType = Object.freeze({ UNKNOWN: 0, CHROME: 1, EDGE: 2, FIREFOX: 3, IE: 4, OPERA: 5, SAFARI: 6, ELECTRON: 7, UWP: 8, OTHER_WEB_CLIENT: 9 })
2
+
3
+ const BROWSER_TO_COMPANION = {
4
+ Chrome: CompanionWebClientType.CHROME,
5
+ Edge: CompanionWebClientType.EDGE,
6
+ Firefox: CompanionWebClientType.FIREFOX,
7
+ IE: CompanionWebClientType.IE,
8
+ Opera: CompanionWebClientType.OPERA,
9
+ Safari: CompanionWebClientType.SAFARI
10
+ }
11
+
12
+ export const getCompanionWebClientType = ([os, browserName]) => {
13
+ if (browserName === 'Desktop') return os === 'Windows' ? CompanionWebClientType.UWP : CompanionWebClientType.ELECTRON
14
+ return BROWSER_TO_COMPANION[browserName] ?? CompanionWebClientType.OTHER_WEB_CLIENT
15
+ }
16
+
17
+ export const getCompanionPlatformId = browser => getCompanionWebClientType(browser).toString()
18
+
19
+ export const buildPairingQRData = (ref, noiseKeyB64, identityKeyB64, advB64, browser) =>
20
20
  'https://wa.me/settings/linked_devices#' + [ref, noiseKeyB64, identityKeyB64, advB64, getCompanionPlatformId(browser)].join(',')
@@ -1,38 +1,38 @@
1
- import { proto } from '../../WAProto/index.js'
2
-
3
- export const decodeConsumerApplication = buffer => {
4
- if (!Buffer.isBuffer(buffer) && !(buffer instanceof Uint8Array)) throw new TypeError('decodeConsumerApplication: buffer must be Buffer or Uint8Array')
5
- return proto.ConsumerApplication.decode(buffer)
6
- }
7
-
8
- const textOf = mt => (mt && typeof mt.text === 'string' ? mt.text : undefined)
9
-
10
- const mapContent = content => {
11
- if (!content) return null
12
- switch (content.content) {
13
- case 'messageText': return { conversation: textOf(content.messageText) ?? '' }
14
- case 'extendedTextMessage': { const e = content.extendedTextMessage; return { extendedTextMessage: { text: textOf(e.text), matchedText: e.matchedText, canonicalUrl: e.canonicalUrl, description: e.description, title: e.title } } }
15
- case 'imageMessage': return { imageMessage: { mediaPayload: content.imageMessage.image, caption: textOf(content.imageMessage.caption) } }
16
- case 'videoMessage': return { videoMessage: { mediaPayload: content.videoMessage.video, caption: textOf(content.videoMessage.caption) } }
17
- case 'audioMessage': return { audioMessage: { mediaPayload: content.audioMessage.audio, ptt: content.audioMessage.ptt } }
18
- case 'documentMessage': return { documentMessage: { mediaPayload: content.documentMessage.document, fileName: content.documentMessage.fileName } }
19
- case 'stickerMessage': return { stickerMessage: { mediaPayload: content.stickerMessage.sticker } }
20
- case 'contactMessage': return { contactMessage: { mediaPayload: content.contactMessage.contact } }
21
- case 'contactsArrayMessage': return { contactsArrayMessage: { displayName: content.contactsArrayMessage.displayName, contacts: content.contactsArrayMessage.contacts || [] } }
22
- case 'locationMessage': { const loc = content.locationMessage.location || {}; return { locationMessage: { degreesLatitude: loc.degreesLatitude, degreesLongitude: loc.degreesLongitude, name: loc.name } } }
23
- case 'liveLocationMessage': { const loc = content.liveLocationMessage.location || {}; return { liveLocationMessage: { degreesLatitude: loc.degreesLatitude, degreesLongitude: loc.degreesLongitude, caption: textOf(content.liveLocationMessage.caption) } } }
24
- case 'reactionMessage': return { reactionMessage: content.reactionMessage }
25
- default: return null
26
- }
27
- }
28
-
29
- // primary use: newsletter/channel message decoding pipeline
30
- export const consumerApplicationToMessage = app => {
31
- const payload = app?.payload
32
- if (!payload) return null
33
- switch (payload.payload) {
34
- case 'content': return mapContent(payload.content)
35
- case 'applicationData': { const ad = payload.applicationData; if (ad?.applicationContent === 'revoke' && ad.revoke?.key) return { protocolMessage: { key: ad.revoke.key, type: proto.Message.ProtocolMessage.Type.REVOKE } }; return null }
36
- default: return null
37
- }
1
+ import { proto } from '../../WAProto/index.js'
2
+
3
+ export const decodeConsumerApplication = buffer => {
4
+ if (!Buffer.isBuffer(buffer) && !(buffer instanceof Uint8Array)) throw new TypeError('decodeConsumerApplication: buffer must be Buffer or Uint8Array')
5
+ return proto.ConsumerApplication.decode(buffer)
6
+ }
7
+
8
+ const textOf = mt => (mt && typeof mt.text === 'string' ? mt.text : undefined)
9
+
10
+ const mapContent = content => {
11
+ if (!content) return null
12
+ switch (content.content) {
13
+ case 'messageText': return { conversation: textOf(content.messageText) ?? '' }
14
+ case 'extendedTextMessage': { const e = content.extendedTextMessage; return { extendedTextMessage: { text: textOf(e.text), matchedText: e.matchedText, canonicalUrl: e.canonicalUrl, description: e.description, title: e.title } } }
15
+ case 'imageMessage': return { imageMessage: { mediaPayload: content.imageMessage.image, caption: textOf(content.imageMessage.caption) } }
16
+ case 'videoMessage': return { videoMessage: { mediaPayload: content.videoMessage.video, caption: textOf(content.videoMessage.caption) } }
17
+ case 'audioMessage': return { audioMessage: { mediaPayload: content.audioMessage.audio, ptt: content.audioMessage.ptt } }
18
+ case 'documentMessage': return { documentMessage: { mediaPayload: content.documentMessage.document, fileName: content.documentMessage.fileName } }
19
+ case 'stickerMessage': return { stickerMessage: { mediaPayload: content.stickerMessage.sticker } }
20
+ case 'contactMessage': return { contactMessage: { mediaPayload: content.contactMessage.contact } }
21
+ case 'contactsArrayMessage': return { contactsArrayMessage: { displayName: content.contactsArrayMessage.displayName, contacts: content.contactsArrayMessage.contacts || [] } }
22
+ case 'locationMessage': { const loc = content.locationMessage.location || {}; return { locationMessage: { degreesLatitude: loc.degreesLatitude, degreesLongitude: loc.degreesLongitude, name: loc.name } } }
23
+ case 'liveLocationMessage': { const loc = content.liveLocationMessage.location || {}; return { liveLocationMessage: { degreesLatitude: loc.degreesLatitude, degreesLongitude: loc.degreesLongitude, caption: textOf(content.liveLocationMessage.caption) } } }
24
+ case 'reactionMessage': return { reactionMessage: content.reactionMessage }
25
+ default: return null
26
+ }
27
+ }
28
+
29
+ // primary use: newsletter/channel message decoding pipeline
30
+ export const consumerApplicationToMessage = app => {
31
+ const payload = app?.payload
32
+ if (!payload) return null
33
+ switch (payload.payload) {
34
+ case 'content': return mapContent(payload.content)
35
+ case 'applicationData': { const ad = payload.applicationData; if (ad?.applicationContent === 'revoke' && ad.revoke?.key) return { protocolMessage: { key: ad.revoke.key, type: proto.Message.ProtocolMessage.Type.REVOKE } }; return null }
36
+ default: return null
37
+ }
38
38
  }
@@ -1,53 +1,53 @@
1
- import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from 'crypto'
2
- import { calculateAgreement, calculateSignature, verifySignature as rustVerifySignature, generateKeyPair as rustGenerateKeyPair, hkdf, md5 } from 'whatsapp-rust-bridge'
3
- import { KEY_BUNDLE_TYPE } from '../Defaults/index.js'
4
-
5
- export { hkdf, md5 }
6
-
7
- const GCM_TAG_LENGTH = 16 // 128 >> 3
8
-
9
- export const generateSignalPubKey = pubKey => pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey])
10
-
11
- export const Curve = {
12
- generateKeyPair: () => { const { pubKey, privKey } = rustGenerateKeyPair(); return { private: Buffer.from(privKey), public: Buffer.from(pubKey.slice(1)) } },
13
- sharedKey: (privateKey, publicKey) => Buffer.from(calculateAgreement(generateSignalPubKey(publicKey), privateKey)),
14
- sign: (privateKey, buf) => calculateSignature(privateKey, buf),
15
- verify: (pubKey, message, signature) => { try { return rustVerifySignature(generateSignalPubKey(pubKey), message, signature) } catch { return false } }
16
- }
17
-
18
- export const signedKeyPair = (identityKeyPair, keyId) => {
19
- const preKey = Curve.generateKeyPair()
20
- const pubKey = generateSignalPubKey(preKey.public)
21
- return { keyPair: preKey, signature: Curve.sign(identityKeyPair.private, pubKey), keyId }
22
- }
23
-
24
- export const aesEncryptGCM = (plaintext, key, iv, additionalData) => {
25
- const cipher = createCipheriv('aes-256-gcm', key, iv)
26
- cipher.setAAD(additionalData)
27
- return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()])
28
- }
29
-
30
- export const aesDecryptGCM = (ciphertext, key, iv, additionalData) => {
31
- const decipher = createDecipheriv('aes-256-gcm', key, iv, { authTagLength: GCM_TAG_LENGTH })
32
- decipher.setAAD(additionalData)
33
- decipher.setAuthTag(ciphertext.subarray(-GCM_TAG_LENGTH))
34
- return Buffer.concat([decipher.update(ciphertext.subarray(0, -GCM_TAG_LENGTH)), decipher.final()])
35
- }
36
-
37
- export const aesEncryptCTR = (plaintext, key, iv) => { const c = createCipheriv('aes-256-ctr', key, iv); return Buffer.concat([c.update(plaintext), c.final()]) }
38
- export const aesDecryptCTR = (ciphertext, key, iv) => { const d = createDecipheriv('aes-256-ctr', key, iv); return Buffer.concat([d.update(ciphertext), d.final()]) }
39
-
40
- // aes-256-cbc is required by the WhatsApp media encryption spec (CBC + HMAC-SHA256)
41
- export const aesDecryptWithIV = (buffer, key, IV) => { const d = createDecipheriv('aes-256-cbc', key, IV); return Buffer.concat([d.update(buffer), d.final()]) }
42
- export const aesDecrypt = (buffer, key) => aesDecryptWithIV(buffer.subarray(16), key, buffer.subarray(0, 16))
43
-
44
- export const aesEncryptWithIV = (buffer, key, IV) => { const c = createCipheriv('aes-256-cbc', key, IV); return Buffer.concat([c.update(buffer), c.final()]) }
45
- export const aesEncrypt = (buffer, key) => { const IV = randomBytes(16); return Buffer.concat([IV, ...(() => { const c = createCipheriv('aes-256-cbc', key, IV); return [c.update(buffer), c.final()] })()]) }
46
-
47
- export const hmacSign = (buffer, key, variant = 'sha256') => createHmac(variant, key).update(buffer).digest()
48
- export const sha256 = buffer => createHash('sha256').update(buffer).digest()
49
-
50
- export const derivePairingCodeKey = async (pairingCode, salt) => {
51
- const keyMaterial = await crypto.subtle.importKey('raw', new TextEncoder().encode(pairingCode), { name: 'PBKDF2' }, false, ['deriveBits'])
52
- return Buffer.from(await crypto.subtle.deriveBits({ name: 'PBKDF2', salt: salt instanceof Uint8Array ? salt : new Uint8Array(salt), iterations: 2 << 16, hash: 'SHA-256' }, keyMaterial, 256))
1
+ import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from 'crypto'
2
+ import { calculateAgreement, calculateSignature, verifySignature as rustVerifySignature, generateKeyPair as rustGenerateKeyPair, hkdf, md5 } from 'whatsapp-rust-bridge'
3
+ import { KEY_BUNDLE_TYPE } from '../Defaults/index.js'
4
+
5
+ export { hkdf, md5 }
6
+
7
+ const GCM_TAG_LENGTH = 16 // 128 >> 3
8
+
9
+ export const generateSignalPubKey = pubKey => pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey])
10
+
11
+ export const Curve = {
12
+ generateKeyPair: () => { const { pubKey, privKey } = rustGenerateKeyPair(); return { private: Buffer.from(privKey), public: Buffer.from(pubKey.slice(1)) } },
13
+ sharedKey: (privateKey, publicKey) => Buffer.from(calculateAgreement(generateSignalPubKey(publicKey), privateKey)),
14
+ sign: (privateKey, buf) => calculateSignature(privateKey, buf),
15
+ verify: (pubKey, message, signature) => { try { return rustVerifySignature(generateSignalPubKey(pubKey), message, signature) } catch { return false } }
16
+ }
17
+
18
+ export const signedKeyPair = (identityKeyPair, keyId) => {
19
+ const preKey = Curve.generateKeyPair()
20
+ const pubKey = generateSignalPubKey(preKey.public)
21
+ return { keyPair: preKey, signature: Curve.sign(identityKeyPair.private, pubKey), keyId }
22
+ }
23
+
24
+ export const aesEncryptGCM = (plaintext, key, iv, additionalData) => {
25
+ const cipher = createCipheriv('aes-256-gcm', key, iv)
26
+ cipher.setAAD(additionalData)
27
+ return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()])
28
+ }
29
+
30
+ export const aesDecryptGCM = (ciphertext, key, iv, additionalData) => {
31
+ const decipher = createDecipheriv('aes-256-gcm', key, iv, { authTagLength: GCM_TAG_LENGTH })
32
+ decipher.setAAD(additionalData)
33
+ decipher.setAuthTag(ciphertext.subarray(-GCM_TAG_LENGTH))
34
+ return Buffer.concat([decipher.update(ciphertext.subarray(0, -GCM_TAG_LENGTH)), decipher.final()])
35
+ }
36
+
37
+ export const aesEncryptCTR = (plaintext, key, iv) => { const c = createCipheriv('aes-256-ctr', key, iv); return Buffer.concat([c.update(plaintext), c.final()]) }
38
+ export const aesDecryptCTR = (ciphertext, key, iv) => { const d = createDecipheriv('aes-256-ctr', key, iv); return Buffer.concat([d.update(ciphertext), d.final()]) }
39
+
40
+ // aes-256-cbc is required by the WhatsApp media encryption spec (CBC + HMAC-SHA256)
41
+ export const aesDecryptWithIV = (buffer, key, IV) => { const d = createDecipheriv('aes-256-cbc', key, IV); return Buffer.concat([d.update(buffer), d.final()]) }
42
+ export const aesDecrypt = (buffer, key) => aesDecryptWithIV(buffer.subarray(16), key, buffer.subarray(0, 16))
43
+
44
+ export const aesEncryptWithIV = (buffer, key, IV) => { const c = createCipheriv('aes-256-cbc', key, IV); return Buffer.concat([c.update(buffer), c.final()]) }
45
+ export const aesEncrypt = (buffer, key) => { const IV = randomBytes(16); return Buffer.concat([IV, ...(() => { const c = createCipheriv('aes-256-cbc', key, IV); return [c.update(buffer), c.final()] })()]) }
46
+
47
+ export const hmacSign = (buffer, key, variant = 'sha256') => createHmac(variant, key).update(buffer).digest()
48
+ export const sha256 = buffer => createHash('sha256').update(buffer).digest()
49
+
50
+ export const derivePairingCodeKey = async (pairingCode, salt) => {
51
+ const keyMaterial = await crypto.subtle.importKey('raw', new TextEncoder().encode(pairingCode), { name: 'PBKDF2' }, false, ['deriveBits'])
52
+ return Buffer.from(await crypto.subtle.deriveBits({ name: 'PBKDF2', salt: salt instanceof Uint8Array ? salt : new Uint8Array(salt), iterations: 2 << 16, hash: 'SHA-256' }, keyMaterial, 256))
53
53
  }
@@ -1,15 +1,15 @@
1
- import { isJidBot, isJidMetaAI, isLidUser, isPnUser, jidNormalizedUser, PSA_WID } from '../WABinary/index.js'
2
- import { hmacSign } from './crypto.js'
3
-
4
- export const shouldSendCsToken = (jid) => {
5
- const n = jidNormalizedUser(jid)
6
- return !!n && (isPnUser(n) || isLidUser(n)) && n !== jidNormalizedUser(PSA_WID) && !isJidBot(jid) && !isJidMetaAI(jid)
7
- }
8
-
9
- export const generateCsToken = async ({ jid, nctSalt, getLIDForPN }) => {
10
- if (!nctSalt?.length || !shouldSendCsToken(jid)) return undefined
11
- const n = jidNormalizedUser(jid)
12
- const lid = isLidUser(n) ? n : jidNormalizedUser(await getLIDForPN(n))
13
- if (!lid || !isLidUser(lid)) return undefined
14
- return hmacSign(Buffer.from(lid), nctSalt, 'sha256')
1
+ import { isJidBot, isJidMetaAI, isLidUser, isPnUser, jidNormalizedUser, PSA_WID } from '../WABinary/index.js'
2
+ import { hmacSign } from './crypto.js'
3
+
4
+ export const shouldSendCsToken = (jid) => {
5
+ const n = jidNormalizedUser(jid)
6
+ return !!n && (isPnUser(n) || isLidUser(n)) && n !== jidNormalizedUser(PSA_WID) && !isJidBot(jid) && !isJidMetaAI(jid)
7
+ }
8
+
9
+ export const generateCsToken = async ({ jid, nctSalt, getLIDForPN }) => {
10
+ if (!nctSalt?.length || !shouldSendCsToken(jid)) return undefined
11
+ const n = jidNormalizedUser(jid)
12
+ const lid = isLidUser(n) ? n : jidNormalizedUser(await getLIDForPN(n))
13
+ if (!lid || !isLidUser(lid)) return undefined
14
+ return hmacSign(Buffer.from(lid), nctSalt, 'sha256')
15
15
  }