@mrlegendbot/baileys 1.2.4 → 1.2.6

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 (214) hide show
  1. package/WAProto/GenerateStatics.sh +3 -0
  2. package/WAProto/fix-imports.js +85 -0
  3. package/WAProto/index.d.ts +14017 -0
  4. package/WAProto/index.js +97691 -2
  5. package/lib/Defaults/index.d.ts +75 -0
  6. package/lib/Defaults/index.js +92 -89
  7. package/lib/Menu/index.d.ts +42 -0
  8. package/lib/Menu/index.js +283 -0
  9. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  10. package/lib/Signal/Group/ciphertext-message.js +10 -9
  11. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  12. package/lib/Signal/Group/group-session-builder.js +22 -21
  13. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  14. package/lib/Signal/Group/group_cipher.js +69 -75
  15. package/lib/Signal/Group/index.d.ts +12 -0
  16. package/lib/Signal/Group/index.js +1 -0
  17. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  18. package/lib/Signal/Group/keyhelper.js +10 -9
  19. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  20. package/lib/Signal/Group/sender-chain-key.js +22 -21
  21. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  22. package/lib/Signal/Group/sender-key-distribution-message.js +60 -51
  23. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  24. package/lib/Signal/Group/sender-key-message.js +62 -56
  25. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  26. package/lib/Signal/Group/sender-key-name.js +40 -38
  27. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  28. package/lib/Signal/Group/sender-key-record.js +34 -33
  29. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  30. package/lib/Signal/Group/sender-key-state.js +76 -72
  31. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  32. package/lib/Signal/Group/sender-message-key.js +23 -22
  33. package/lib/Signal/libsignal.d.ts +5 -0
  34. package/lib/Signal/libsignal.js +405 -506
  35. package/lib/Signal/lid-mapping.d.ts +23 -0
  36. package/lib/Signal/lid-mapping.js +258 -276
  37. package/lib/Socket/Client/index.d.ts +3 -0
  38. package/lib/Socket/Client/index.js +1 -0
  39. package/lib/Socket/Client/types.d.ts +16 -0
  40. package/lib/Socket/Client/types.js +7 -6
  41. package/lib/Socket/Client/websocket.d.ts +13 -0
  42. package/lib/Socket/Client/websocket.js +49 -48
  43. package/lib/Socket/business.d.ts +217 -0
  44. package/lib/Socket/business.js +369 -349
  45. package/lib/Socket/chats.d.ts +125 -0
  46. package/lib/Socket/chats.js +1148 -1172
  47. package/lib/Socket/communities.d.ts +273 -0
  48. package/lib/Socket/communities.js +413 -473
  49. package/lib/Socket/groups.d.ts +161 -0
  50. package/lib/Socket/groups.js +331 -387
  51. package/lib/Socket/index.d.ts +267 -0
  52. package/lib/Socket/index.js +19 -6
  53. package/lib/Socket/messages-recv.d.ts +203 -0
  54. package/lib/Socket/messages-recv.js +1719 -1782
  55. package/lib/Socket/messages-send.d.ts +199 -0
  56. package/lib/Socket/messages-send.js +2451 -1504
  57. package/lib/Socket/mex.d.ts +3 -0
  58. package/lib/Socket/mex.js +35 -40
  59. package/lib/Socket/newsletter.d.ts +170 -0
  60. package/lib/Socket/newsletter.js +191 -239
  61. package/lib/Socket/socket.d.ts +59 -0
  62. package/lib/Socket/socket.js +923 -992
  63. package/lib/Types/Auth.d.ts +117 -0
  64. package/lib/Types/Auth.js +1 -0
  65. package/lib/Types/Bussines.d.ts +25 -0
  66. package/lib/Types/Bussines.js +1 -0
  67. package/lib/Types/Call.d.ts +15 -0
  68. package/lib/Types/Call.js +1 -0
  69. package/lib/Types/Chat.d.ts +124 -0
  70. package/lib/Types/Chat.js +8 -1
  71. package/lib/Types/Contact.d.ts +26 -0
  72. package/lib/Types/Contact.js +1 -0
  73. package/lib/Types/Events.d.ts +256 -0
  74. package/lib/Types/Events.js +2 -1
  75. package/lib/Types/GroupMetadata.d.ts +71 -0
  76. package/lib/Types/GroupMetadata.js +1 -0
  77. package/lib/Types/Label.d.ts +47 -0
  78. package/lib/Types/Label.js +22 -20
  79. package/lib/Types/LabelAssociation.d.ts +30 -0
  80. package/lib/Types/LabelAssociation.js +4 -2
  81. package/lib/Types/Message.d.ts +320 -0
  82. package/lib/Types/Message.js +5 -3
  83. package/lib/Types/Mex.d.ts +141 -0
  84. package/lib/Types/Mex.js +31 -26
  85. package/lib/Types/Product.d.ts +79 -0
  86. package/lib/Types/Product.js +1 -0
  87. package/lib/Types/Signal.d.ts +87 -0
  88. package/lib/Types/Signal.js +2 -1
  89. package/lib/Types/Socket.d.ts +136 -0
  90. package/lib/Types/Socket.js +3 -2
  91. package/lib/Types/State.d.ts +97 -0
  92. package/lib/Types/State.js +52 -4
  93. package/lib/Types/USync.d.ts +26 -0
  94. package/lib/Types/USync.js +1 -0
  95. package/lib/Types/index.d.ts +66 -0
  96. package/lib/Types/index.js +11 -10
  97. package/lib/Utils/auth-utils.d.ts +24 -0
  98. package/lib/Utils/auth-utils.js +277 -240
  99. package/lib/Utils/browser-utils.d.ts +4 -0
  100. package/lib/Utils/browser-utils.js +23 -20
  101. package/lib/Utils/business.d.ts +23 -0
  102. package/lib/Utils/business.js +204 -200
  103. package/lib/Utils/chat-utils.d.ts +100 -0
  104. package/lib/Utils/chat-utils.js +810 -836
  105. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  106. package/lib/Utils/companion-reg-client-utils.js +25 -23
  107. package/lib/Utils/crypto.d.ts +37 -0
  108. package/lib/Utils/crypto.js +86 -70
  109. package/lib/Utils/decode-wa-message.d.ts +66 -0
  110. package/lib/Utils/decode-wa-message.js +284 -277
  111. package/lib/Utils/event-buffer.d.ts +36 -0
  112. package/lib/Utils/event-buffer.js +595 -592
  113. package/lib/Utils/generics.d.ts +92 -0
  114. package/lib/Utils/generics.js +335 -312
  115. package/lib/Utils/history.d.ts +24 -0
  116. package/lib/Utils/history.js +115 -123
  117. package/lib/Utils/identity-change-handler.d.ts +44 -0
  118. package/lib/Utils/identity-change-handler.js +50 -0
  119. package/lib/Utils/index.d.ts +22 -0
  120. package/lib/Utils/index.js +2 -1
  121. package/lib/Utils/link-preview.d.ts +21 -0
  122. package/lib/Utils/link-preview.js +73 -74
  123. package/lib/Utils/logger.d.ts +12 -0
  124. package/lib/Utils/logger.js +2 -3
  125. package/lib/Utils/lt-hash.d.ts +8 -0
  126. package/lib/Utils/lt-hash.js +6 -0
  127. package/lib/Utils/make-mutex.d.ts +9 -0
  128. package/lib/Utils/make-mutex.js +27 -27
  129. package/lib/Utils/message-retry-manager.d.ts +115 -0
  130. package/lib/Utils/message-retry-manager.js +254 -213
  131. package/lib/Utils/messages-media.d.ts +133 -0
  132. package/lib/Utils/messages-media.js +715 -741
  133. package/lib/Utils/messages.d.ts +91 -0
  134. package/lib/Utils/messages.js +827 -791
  135. package/lib/Utils/noise-handler.d.ts +20 -0
  136. package/lib/Utils/noise-handler.js +187 -215
  137. package/lib/Utils/offline-node-processor.d.ts +17 -0
  138. package/lib/Utils/offline-node-processor.js +40 -0
  139. package/lib/Utils/pre-key-manager.d.ts +28 -0
  140. package/lib/Utils/pre-key-manager.js +98 -69
  141. package/lib/Utils/process-message.d.ts +60 -0
  142. package/lib/Utils/process-message.js +586 -617
  143. package/lib/Utils/reporting-utils.d.ts +11 -0
  144. package/lib/Utils/reporting-utils.js +234 -553
  145. package/lib/Utils/signal.d.ts +47 -0
  146. package/lib/Utils/signal.js +170 -217
  147. package/lib/Utils/stanza-ack.d.ts +11 -0
  148. package/lib/Utils/stanza-ack.js +35 -27
  149. package/lib/Utils/sync-action-utils.d.ts +19 -0
  150. package/lib/Utils/sync-action-utils.js +41 -37
  151. package/lib/Utils/tc-token-utils.d.ts +37 -0
  152. package/lib/Utils/tc-token-utils.js +141 -120
  153. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  154. package/lib/Utils/use-multi-file-auth-state.js +105 -87
  155. package/lib/Utils/validate-connection.d.ts +11 -0
  156. package/lib/Utils/validate-connection.js +188 -190
  157. package/lib/WABinary/constants.d.ts +28 -0
  158. package/lib/WABinary/constants.js +1294 -28
  159. package/lib/WABinary/decode.d.ts +7 -0
  160. package/lib/WABinary/decode.js +249 -245
  161. package/lib/WABinary/encode.d.ts +3 -0
  162. package/lib/WABinary/encode.js +206 -201
  163. package/lib/WABinary/generic-utils.d.ts +15 -0
  164. package/lib/WABinary/generic-utils.js +89 -86
  165. package/lib/WABinary/index.d.ts +6 -0
  166. package/lib/WABinary/index.js +1 -0
  167. package/lib/WABinary/jid-utils.d.ts +48 -0
  168. package/lib/WABinary/jid-utils.js +77 -68
  169. package/lib/WABinary/types.d.ts +19 -0
  170. package/lib/WABinary/types.js +1 -0
  171. package/lib/WAM/BinaryInfo.d.ts +9 -0
  172. package/lib/WAM/BinaryInfo.js +8 -7
  173. package/lib/WAM/constants.d.ts +40 -0
  174. package/lib/WAM/constants.js +22853 -19892
  175. package/lib/WAM/encode.d.ts +3 -0
  176. package/lib/WAM/encode.js +132 -121
  177. package/lib/WAM/index.d.ts +4 -0
  178. package/lib/WAM/index.js +1 -0
  179. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  180. package/lib/WAUSync/Protocols/USyncContactProtocol.js +43 -46
  181. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  182. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +48 -47
  183. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  184. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +23 -22
  185. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  186. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +33 -30
  187. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  188. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +20 -19
  189. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  190. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +44 -53
  191. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  192. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
  193. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  194. package/lib/WAUSync/Protocols/index.js +1 -0
  195. package/lib/WAUSync/USyncQuery.d.ts +30 -0
  196. package/lib/WAUSync/USyncQuery.js +88 -79
  197. package/lib/WAUSync/USyncUser.d.ts +17 -0
  198. package/lib/WAUSync/USyncUser.js +29 -28
  199. package/lib/WAUSync/index.d.ts +4 -0
  200. package/lib/WAUSync/index.js +1 -0
  201. package/lib/index.d.ts +12 -0
  202. package/lib/index.js +2 -2
  203. package/package.json +86 -22
  204. package/README.upstream.md +0 -1312
  205. package/WAProto/WAProto.json +0 -1
  206. package/WAProto/WAProto.proto +0 -6275
  207. package/WAProto/compiler.js +0 -550
  208. package/lib/Store/index.js +0 -8
  209. package/lib/Store/keyed-db.js +0 -108
  210. package/lib/Store/make-cache-manager-store.js +0 -70
  211. package/lib/Store/make-in-memory-store.js +0 -417
  212. package/lib/Store/make-ordered-dictionary.js +0 -69
  213. package/lib/Store/object-repository.js +0 -27
  214. package/lib/Utils/use-sqlite-auth-state.js +0 -162
@@ -0,0 +1,17 @@
1
+ import type { WABrowserDescription } from '../Types/index.js';
2
+ export declare enum CompanionWebClientType {
3
+ UNKNOWN = 0,
4
+ CHROME = 1,
5
+ EDGE = 2,
6
+ FIREFOX = 3,
7
+ IE = 4,
8
+ OPERA = 5,
9
+ SAFARI = 6,
10
+ ELECTRON = 7,
11
+ UWP = 8,
12
+ OTHER_WEB_CLIENT = 9
13
+ }
14
+ export declare const getCompanionWebClientType: ([os, browserName]: WABrowserDescription) => CompanionWebClientType;
15
+ export declare const getCompanionPlatformId: (browser: WABrowserDescription) => string;
16
+ export declare const buildPairingQRData: (ref: string, noiseKeyB64: string, identityKeyB64: string, advB64: string, browser: WABrowserDescription) => string;
17
+ //# sourceMappingURL=companion-reg-client-utils.d.ts.map
@@ -1,33 +1,35 @@
1
1
  export var CompanionWebClientType;
2
2
  (function (CompanionWebClientType) {
3
- CompanionWebClientType[CompanionWebClientType["UNKNOWN"] = 0] = "UNKNOWN";
4
- CompanionWebClientType[CompanionWebClientType["CHROME"] = 1] = "CHROME";
5
- CompanionWebClientType[CompanionWebClientType["EDGE"] = 2] = "EDGE";
6
- CompanionWebClientType[CompanionWebClientType["FIREFOX"] = 3] = "FIREFOX";
7
- CompanionWebClientType[CompanionWebClientType["IE"] = 4] = "IE";
8
- CompanionWebClientType[CompanionWebClientType["OPERA"] = 5] = "OPERA";
9
- CompanionWebClientType[CompanionWebClientType["SAFARI"] = 6] = "SAFARI";
10
- CompanionWebClientType[CompanionWebClientType["ELECTRON"] = 7] = "ELECTRON";
11
- CompanionWebClientType[CompanionWebClientType["UWP"] = 8] = "UWP";
12
- CompanionWebClientType[CompanionWebClientType["OTHER_WEB_CLIENT"] = 9] = "OTHER_WEB_CLIENT";
3
+ CompanionWebClientType[CompanionWebClientType["UNKNOWN"] = 0] = "UNKNOWN";
4
+ CompanionWebClientType[CompanionWebClientType["CHROME"] = 1] = "CHROME";
5
+ CompanionWebClientType[CompanionWebClientType["EDGE"] = 2] = "EDGE";
6
+ CompanionWebClientType[CompanionWebClientType["FIREFOX"] = 3] = "FIREFOX";
7
+ CompanionWebClientType[CompanionWebClientType["IE"] = 4] = "IE";
8
+ CompanionWebClientType[CompanionWebClientType["OPERA"] = 5] = "OPERA";
9
+ CompanionWebClientType[CompanionWebClientType["SAFARI"] = 6] = "SAFARI";
10
+ CompanionWebClientType[CompanionWebClientType["ELECTRON"] = 7] = "ELECTRON";
11
+ CompanionWebClientType[CompanionWebClientType["UWP"] = 8] = "UWP";
12
+ CompanionWebClientType[CompanionWebClientType["OTHER_WEB_CLIENT"] = 9] = "OTHER_WEB_CLIENT";
13
13
  })(CompanionWebClientType || (CompanionWebClientType = {}));
14
14
  const BROWSER_TO_COMPANION_WEB_CLIENT = {
15
- Chrome: CompanionWebClientType.CHROME,
16
- Edge: CompanionWebClientType.EDGE,
17
- Firefox: CompanionWebClientType.FIREFOX,
18
- IE: CompanionWebClientType.IE,
19
- Opera: CompanionWebClientType.OPERA,
20
- Safari: CompanionWebClientType.SAFARI
15
+ Chrome: CompanionWebClientType.CHROME,
16
+ Edge: CompanionWebClientType.EDGE,
17
+ Firefox: CompanionWebClientType.FIREFOX,
18
+ IE: CompanionWebClientType.IE,
19
+ Opera: CompanionWebClientType.OPERA,
20
+ Safari: CompanionWebClientType.SAFARI
21
21
  };
22
22
  export const getCompanionWebClientType = ([os, browserName]) => {
23
- if (browserName === 'Desktop') {
24
- return os === 'Windows' ? CompanionWebClientType.UWP : CompanionWebClientType.ELECTRON;
25
- }
26
- return BROWSER_TO_COMPANION_WEB_CLIENT[browserName] || CompanionWebClientType.OTHER_WEB_CLIENT;
23
+ if (browserName === 'Desktop') {
24
+ return os === 'Windows' ? CompanionWebClientType.UWP : CompanionWebClientType.ELECTRON;
25
+ }
26
+ return BROWSER_TO_COMPANION_WEB_CLIENT[browserName] || CompanionWebClientType.OTHER_WEB_CLIENT;
27
27
  };
28
- export const getCompanionPlatformId = browser => {
29
- return getCompanionWebClientType(browser).toString();
28
+ export const getCompanionPlatformId = (browser) => {
29
+ return getCompanionWebClientType(browser).toString();
30
30
  };
31
31
  export const buildPairingQRData = (ref, noiseKeyB64, identityKeyB64, advB64, browser) => {
32
- return 'https://wa.me/settings/linked_devices#' + [ref, noiseKeyB64, identityKeyB64, advB64, getCompanionPlatformId(browser)].join(',');
32
+ return ('https://wa.me/settings/linked_devices#' +
33
+ [ref, noiseKeyB64, identityKeyB64, advB64, getCompanionPlatformId(browser)].join(','));
33
34
  };
35
+ //# sourceMappingURL=companion-reg-client-utils.js.map
@@ -0,0 +1,37 @@
1
+ import type { KeyPair } from '../Types/index.js';
2
+ export { md5, hkdf } from 'whatsapp-rust-bridge';
3
+ /** prefix version byte to the pub keys, required for some curve crypto functions */
4
+ export declare const generateSignalPubKey: (pubKey: Uint8Array | Buffer) => Uint8Array<ArrayBufferLike> | Buffer<ArrayBufferLike>;
5
+ export declare const Curve: {
6
+ generateKeyPair: () => KeyPair;
7
+ sharedKey: (privateKey: Uint8Array, publicKey: Uint8Array) => Buffer<ArrayBuffer>;
8
+ sign: (privateKey: Uint8Array, buf: Uint8Array) => Uint8Array<ArrayBufferLike>;
9
+ verify: (pubKey: Uint8Array, message: Uint8Array, signature: Uint8Array) => boolean;
10
+ };
11
+ export declare const signedKeyPair: (identityKeyPair: KeyPair, keyId: number) => {
12
+ keyPair: KeyPair;
13
+ signature: Uint8Array<ArrayBufferLike>;
14
+ keyId: number;
15
+ };
16
+ /**
17
+ * encrypt AES 256 GCM;
18
+ * where the tag tag is suffixed to the ciphertext
19
+ * */
20
+ export declare function aesEncryptGCM(plaintext: Uint8Array, key: Uint8Array, iv: Uint8Array, additionalData: Uint8Array): Buffer<ArrayBuffer>;
21
+ /**
22
+ * decrypt AES 256 GCM;
23
+ * where the auth tag is suffixed to the ciphertext
24
+ * */
25
+ export declare function aesDecryptGCM(ciphertext: Uint8Array, key: Uint8Array, iv: Uint8Array, additionalData: Uint8Array): Buffer<ArrayBuffer>;
26
+ export declare function aesEncryptCTR(plaintext: Uint8Array, key: Uint8Array, iv: Uint8Array): Buffer<ArrayBuffer>;
27
+ export declare function aesDecryptCTR(ciphertext: Uint8Array, key: Uint8Array, iv: Uint8Array): Buffer<ArrayBuffer>;
28
+ /** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
29
+ export declare function aesDecrypt(buffer: Uint8Array, key: Uint8Array): Buffer<ArrayBuffer>;
30
+ /** decrypt AES 256 CBC */
31
+ export declare function aesDecryptWithIV(buffer: Uint8Array, key: Uint8Array, IV: Uint8Array): Buffer<ArrayBuffer>;
32
+ export declare function aesEncrypt(buffer: Uint8Array, key: Uint8Array): Buffer<ArrayBuffer>;
33
+ export declare function aesEncrypWithIV(buffer: Buffer, key: Buffer, IV: Buffer): Buffer<ArrayBuffer>;
34
+ export declare function hmacSign(buffer: Buffer | Uint8Array, key: Buffer | Uint8Array, variant?: 'sha256' | 'sha512'): Buffer<ArrayBufferLike>;
35
+ export declare function sha256(buffer: Buffer): Buffer<ArrayBufferLike>;
36
+ export declare function derivePairingCodeKey(pairingCode: string, salt: Buffer): Promise<Buffer>;
37
+ //# sourceMappingURL=crypto.d.ts.map
@@ -2,101 +2,117 @@ import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes }
2
2
  import * as curve from 'libsignal/src/curve.js';
3
3
  import { KEY_BUNDLE_TYPE } from '../Defaults/index.js';
4
4
  export { md5, hkdf } from 'whatsapp-rust-bridge';
5
- const {
6
- subtle
7
- } = globalThis.crypto;
8
- export const generateSignalPubKey = pubKey => pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey]);
5
+ // insure browser & node compatibility
6
+ const { subtle } = globalThis.crypto;
7
+ /** 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]);
9
9
  export const Curve = {
10
- generateKeyPair: () => {
11
- const {
12
- pubKey,
13
- privKey
14
- } = curve.generateKeyPair();
15
- return {
16
- private: Buffer.from(privKey),
17
- public: Buffer.from(pubKey.slice(1))
18
- };
19
- },
20
- sharedKey: (privateKey, publicKey) => {
21
- const shared = curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey);
22
- return Buffer.from(shared);
23
- },
24
- sign: (privateKey, buf) => curve.calculateSignature(privateKey, buf),
25
- verify: (pubKey, message, signature) => {
26
- try {
27
- curve.verifySignature(generateSignalPubKey(pubKey), message, signature);
28
- return true;
29
- } catch (error) {
30
- return false;
10
+ generateKeyPair: () => {
11
+ const { pubKey, privKey } = 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 = curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey);
20
+ return Buffer.from(shared);
21
+ },
22
+ sign: (privateKey, buf) => curve.calculateSignature(privateKey, buf),
23
+ verify: (pubKey, message, signature) => {
24
+ try {
25
+ curve.verifySignature(generateSignalPubKey(pubKey), message, signature);
26
+ return true;
27
+ }
28
+ catch (error) {
29
+ return false;
30
+ }
31
31
  }
32
- }
33
32
  };
34
33
  export const signedKeyPair = (identityKeyPair, keyId) => {
35
- const preKey = Curve.generateKeyPair();
36
- const pubKey = generateSignalPubKey(preKey.public);
37
- const signature = Curve.sign(identityKeyPair.private, pubKey);
38
- return {
39
- keyPair: preKey,
40
- signature,
41
- keyId
42
- };
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 };
43
38
  };
44
39
  const GCM_TAG_LENGTH = 128 >> 3;
40
+ /**
41
+ * encrypt AES 256 GCM;
42
+ * where the tag tag is suffixed to the ciphertext
43
+ * */
45
44
  export function aesEncryptGCM(plaintext, key, iv, additionalData) {
46
- const cipher = createCipheriv('aes-256-gcm', key, iv);
47
- cipher.setAAD(additionalData);
48
- return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
45
+ const cipher = createCipheriv('aes-256-gcm', key, iv);
46
+ cipher.setAAD(additionalData);
47
+ return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
49
48
  }
49
+ /**
50
+ * decrypt AES 256 GCM;
51
+ * where the auth tag is suffixed to the ciphertext
52
+ * */
50
53
  export function aesDecryptGCM(ciphertext, key, iv, additionalData) {
51
- const decipher = createDecipheriv('aes-256-gcm', key, iv);
52
- const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
53
- const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
54
- decipher.setAAD(additionalData);
55
- decipher.setAuthTag(tag);
56
- return Buffer.concat([decipher.update(enc), decipher.final()]);
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()]);
57
62
  }
58
63
  export function aesEncryptCTR(plaintext, key, iv) {
59
- const cipher = createCipheriv('aes-256-ctr', key, iv);
60
- return Buffer.concat([cipher.update(plaintext), cipher.final()]);
64
+ const cipher = createCipheriv('aes-256-ctr', key, iv);
65
+ return Buffer.concat([cipher.update(plaintext), cipher.final()]);
61
66
  }
62
67
  export function aesDecryptCTR(ciphertext, key, iv) {
63
- const decipher = createDecipheriv('aes-256-ctr', key, iv);
64
- return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
68
+ const decipher = createDecipheriv('aes-256-ctr', key, iv);
69
+ return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
65
70
  }
71
+ /** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
66
72
  export function aesDecrypt(buffer, key) {
67
- return aesDecryptWithIV(buffer.subarray(16), key, buffer.subarray(0, 16));
73
+ return aesDecryptWithIV(buffer.subarray(16), key, buffer.subarray(0, 16));
68
74
  }
75
+ /** decrypt AES 256 CBC */
69
76
  export function aesDecryptWithIV(buffer, key, IV) {
70
- const aes = createDecipheriv('aes-256-cbc', key, IV);
71
- return Buffer.concat([aes.update(buffer), aes.final()]);
77
+ const aes = createDecipheriv('aes-256-cbc', key, IV);
78
+ return Buffer.concat([aes.update(buffer), aes.final()]);
72
79
  }
80
+ // encrypt AES 256 CBC; where a random IV is prefixed to the buffer
73
81
  export function aesEncrypt(buffer, key) {
74
- const IV = randomBytes(16);
75
- const aes = createCipheriv('aes-256-cbc', key, IV);
76
- return Buffer.concat([IV, aes.update(buffer), aes.final()]);
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
77
85
  }
86
+ // encrypt AES 256 CBC with a given IV
78
87
  export function aesEncrypWithIV(buffer, key, IV) {
79
- const aes = createCipheriv('aes-256-cbc', key, IV);
80
- return Buffer.concat([aes.update(buffer), aes.final()]);
88
+ const aes = createCipheriv('aes-256-cbc', key, IV);
89
+ return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer
81
90
  }
91
+ // sign HMAC using SHA 256
82
92
  export function hmacSign(buffer, key, variant = 'sha256') {
83
- return createHmac(variant, key).update(buffer).digest();
93
+ return createHmac(variant, key).update(buffer).digest();
84
94
  }
85
95
  export function sha256(buffer) {
86
- return createHash('sha256').update(buffer).digest();
96
+ return createHash('sha256').update(buffer).digest();
87
97
  }
88
98
  export async function derivePairingCodeKey(pairingCode, salt) {
89
- const encoder = new TextEncoder();
90
- const pairingCodeBuffer = encoder.encode(pairingCode);
91
- const saltBuffer = new Uint8Array(salt instanceof Uint8Array ? salt : new Uint8Array(salt));
92
- const keyMaterial = await subtle.importKey('raw', pairingCodeBuffer, {
93
- name: 'PBKDF2'
94
- }, false, ['deriveBits']);
95
- const derivedBits = await subtle.deriveBits({
96
- name: 'PBKDF2',
97
- salt: saltBuffer,
98
- iterations: 2 << 16,
99
- hash: 'SHA-256'
100
- }, keyMaterial, 32 * 8);
101
- return Buffer.from(derivedBits);
99
+ // Convert inputs to formats Web Crypto API can work with
100
+ const encoder = new TextEncoder();
101
+ const pairingCodeBuffer = encoder.encode(pairingCode);
102
+ const saltBuffer = new Uint8Array(salt instanceof Uint8Array ? salt : new Uint8Array(salt));
103
+ // Import the pairing code as key material
104
+ const keyMaterial = await subtle.importKey('raw', pairingCodeBuffer, { name: 'PBKDF2' }, false, [
105
+ 'deriveBits'
106
+ ]);
107
+ // Derive bits using PBKDF2 with the same parameters
108
+ // 2 << 16 = 131,072 iterations
109
+ const derivedBits = await subtle.deriveBits({
110
+ name: 'PBKDF2',
111
+ salt: saltBuffer,
112
+ iterations: 2 << 16,
113
+ hash: 'SHA-256'
114
+ }, keyMaterial, 32 * 8 // 32 bytes * 8 = 256 bits
115
+ );
116
+ return Buffer.from(derivedBits);
102
117
  }
118
+ //# sourceMappingURL=crypto.js.map
@@ -0,0 +1,66 @@
1
+ import type { WAMessage } from '../Types/index.js';
2
+ import type { SignalRepositoryWithLIDStore } from '../Types/Signal.js';
3
+ import { type BinaryNode } from '../WABinary/index.js';
4
+ import type { ILogger } from './logger.js';
5
+ export declare const getDecryptionJid: (sender: string, repository: SignalRepositoryWithLIDStore) => Promise<string>;
6
+ export declare const NO_MESSAGE_FOUND_ERROR_TEXT = "Message absent from node";
7
+ export declare const MISSING_KEYS_ERROR_TEXT = "Key used already or never filled";
8
+ export declare const ACCOUNT_RESTRICTED_TEXT = "Your account has been restricted";
9
+ export declare const DECRYPTION_RETRY_CONFIG: {
10
+ maxRetries: number;
11
+ baseDelayMs: number;
12
+ sessionRecordErrors: string[];
13
+ };
14
+ /** NACK reason codes we send to the server (client → server) */
15
+ export declare const NACK_REASONS: {
16
+ SenderReachoutTimelocked: number;
17
+ ParsingError: number;
18
+ UnrecognizedStanza: number;
19
+ UnrecognizedStanzaClass: number;
20
+ UnrecognizedStanzaType: number;
21
+ InvalidProtobuf: number;
22
+ InvalidHostedCompanionStanza: number;
23
+ MissingMessageSecret: number;
24
+ SignalErrorOldCounter: number;
25
+ MessageDeletedOnPeer: number;
26
+ UnhandledError: number;
27
+ UnsupportedAdminRevoke: number;
28
+ UnsupportedLIDGroup: number;
29
+ DBOperationFailed: number;
30
+ };
31
+ /**
32
+ * Server-side error codes returned in ack stanzas (server → client) that we
33
+ * currently have dedicated handlers for. Extend as more handlers are added.
34
+ * Distinct from the client-side NackReason enum (WAWebCreateNackFromStanza).
35
+ */
36
+ export declare const SERVER_ERROR_CODES: {
37
+ /**
38
+ * 1:1 message missing privacy token (tctoken). Usually means the account is
39
+ * restricted: WhatsApp blocks starting new chats but preserves existing ones,
40
+ * since established chats already carry a tctoken.
41
+ */
42
+ readonly MessageAccountRestriction: "463";
43
+ /** Stanza validation failure (SMAX_INVALID) — likely stale device session */
44
+ readonly SmaxInvalid: "479";
45
+ };
46
+ export declare const extractAddressingContext: (stanza: BinaryNode) => {
47
+ addressingMode: string;
48
+ senderAlt: string | undefined;
49
+ recipientAlt: string | undefined;
50
+ };
51
+ /**
52
+ * Decode the received node as a message.
53
+ * @note this will only parse the message, not decrypt it
54
+ */
55
+ export declare function decodeMessageNode(stanza: BinaryNode, meId: string, meLid: string): {
56
+ fullMessage: WAMessage;
57
+ author: string;
58
+ sender: string;
59
+ };
60
+ export declare const decryptMessageNode: (stanza: BinaryNode, meId: string, meLid: string, repository: SignalRepositoryWithLIDStore, logger: ILogger) => {
61
+ fullMessage: WAMessage;
62
+ category: string | undefined;
63
+ author: string;
64
+ decrypt(): Promise<void>;
65
+ };
66
+ //# sourceMappingURL=decode-wa-message.d.ts.map