@pontalabs/baileys 1.0.6 → 1.0.7

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 (185) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +839 -267
  3. package/WAProto/GenerateStatics.sh +4 -3
  4. package/WAProto/WAProto.proto +511 -1215
  5. package/WAProto/index.d.ts +55 -14017
  6. package/WAProto/index.js +29 -97689
  7. package/lib/Defaults/index.d.ts +74 -72
  8. package/lib/Defaults/index.js +108 -94
  9. package/lib/Signal/Group/ciphertext-message.d.ts +7 -8
  10. package/lib/Signal/Group/ciphertext-message.js +16 -9
  11. package/lib/Signal/Group/group-session-builder.d.ts +13 -11
  12. package/lib/Signal/Group/group-session-builder.js +61 -19
  13. package/lib/Signal/Group/group_cipher.d.ts +16 -14
  14. package/lib/Signal/Group/group_cipher.js +70 -41
  15. package/lib/Signal/Group/index.d.ts +11 -12
  16. package/lib/Signal/Group/index.js +61 -12
  17. package/lib/Signal/Group/keyhelper.d.ts +14 -9
  18. package/lib/Signal/Group/keyhelper.js +58 -10
  19. package/lib/Signal/Group/sender-chain-key.d.ts +13 -13
  20. package/lib/Signal/Group/sender-chain-key.js +34 -13
  21. package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -16
  22. package/lib/Signal/Group/sender-key-distribution-message.js +36 -28
  23. package/lib/Signal/Group/sender-key-message.d.ts +18 -18
  24. package/lib/Signal/Group/sender-key-message.js +45 -38
  25. package/lib/Signal/Group/sender-key-name.d.ts +15 -14
  26. package/lib/Signal/Group/sender-key-name.js +31 -20
  27. package/lib/Signal/Group/sender-key-record.d.ts +25 -24
  28. package/lib/Signal/Group/sender-key-record.js +35 -18
  29. package/lib/Signal/Group/sender-key-state.d.ts +34 -29
  30. package/lib/Signal/Group/sender-key-state.js +100 -37
  31. package/lib/Signal/Group/sender-message-key.d.ts +10 -11
  32. package/lib/Signal/Group/sender-message-key.js +22 -15
  33. package/lib/Signal/libsignal.d.ts +8 -5
  34. package/lib/Signal/libsignal.js +142 -288
  35. package/lib/Signal/lid-mapping.d.ts +28 -23
  36. package/lib/Signal/lid-mapping.js +184 -277
  37. package/lib/Socket/Client/index.d.ts +2 -3
  38. package/lib/Socket/Client/index.js +22 -3
  39. package/lib/Socket/Client/types.d.ts +15 -15
  40. package/lib/Socket/Client/types.js +15 -8
  41. package/lib/Socket/Client/websocket.d.ts +12 -12
  42. package/lib/Socket/Client/websocket.js +37 -29
  43. package/lib/Socket/business.d.ts +178 -208
  44. package/lib/Socket/business.js +127 -91
  45. package/lib/Socket/chats.d.ts +93 -120
  46. package/lib/Socket/chats.js +652 -727
  47. package/lib/Socket/community.js +10 -2
  48. package/lib/Socket/groups.d.ts +111 -143
  49. package/lib/Socket/groups.js +186 -165
  50. package/lib/Socket/index.d.ts +183 -252
  51. package/lib/Socket/index.js +18 -38
  52. package/lib/Socket/messages-recv.d.ts +167 -196
  53. package/lib/Socket/messages-recv.js +1567 -1443
  54. package/lib/Socket/messages-send.d.ts +158 -192
  55. package/lib/Socket/messages-send.js +1208 -1102
  56. package/lib/Socket/newsletter.d.ts +138 -163
  57. package/lib/Socket/newsletter.js +249 -153
  58. package/lib/Socket/socket.d.ts +36 -50
  59. package/lib/Socket/socket.js +820 -749
  60. package/lib/Store/index.d.ts +4 -4
  61. package/lib/Store/index.js +24 -4
  62. package/lib/Store/make-in-memory-store.d.ts +104 -44
  63. package/lib/Store/make-in-memory-store.js +213 -199
  64. package/lib/Store/make-ordered-dictionary.d.ts +12 -13
  65. package/lib/Store/make-ordered-dictionary.js +45 -38
  66. package/lib/Store/object-repository.d.ts +10 -11
  67. package/lib/Store/object-repository.js +16 -9
  68. package/lib/Types/Auth.d.ts +97 -93
  69. package/lib/Types/Auth.js +3 -2
  70. package/lib/Types/Bussines.js +3 -2
  71. package/lib/Types/Call.d.ts +13 -14
  72. package/lib/Types/Call.js +3 -2
  73. package/lib/Types/Chat.d.ts +97 -78
  74. package/lib/Types/Chat.js +9 -8
  75. package/lib/Types/Contact.d.ts +9 -12
  76. package/lib/Types/Contact.js +3 -2
  77. package/lib/Types/Events.d.ts +176 -206
  78. package/lib/Types/Events.js +3 -2
  79. package/lib/Types/GroupMetadata.d.ts +48 -53
  80. package/lib/Types/GroupMetadata.js +3 -2
  81. package/lib/Types/Label.d.ts +14 -13
  82. package/lib/Types/Label.js +30 -24
  83. package/lib/Types/LabelAssociation.d.ts +20 -15
  84. package/lib/Types/LabelAssociation.js +12 -6
  85. package/lib/Types/Message.d.ts +412 -248
  86. package/lib/Types/Message.js +19 -17
  87. package/lib/Types/Product.d.ts +71 -58
  88. package/lib/Types/Product.js +3 -2
  89. package/lib/Types/Signal.d.ts +82 -71
  90. package/lib/Types/Signal.js +3 -2
  91. package/lib/Types/Socket.d.ts +81 -76
  92. package/lib/Types/Socket.js +3 -3
  93. package/lib/Types/State.d.ts +19 -75
  94. package/lib/Types/State.js +14 -56
  95. package/lib/Types/USync.d.ts +8 -8
  96. package/lib/Types/USync.js +3 -2
  97. package/lib/Types/index.d.ts +62 -47
  98. package/lib/Types/index.js +50 -26
  99. package/lib/Utils/auth-utils.d.ts +10 -13
  100. package/lib/Utils/auth-utils.js +472 -213
  101. package/lib/Utils/business.d.ts +20 -14
  102. package/lib/Utils/business.js +134 -110
  103. package/lib/Utils/chat-utils.d.ts +70 -88
  104. package/lib/Utils/chat-utils.js +403 -466
  105. package/lib/Utils/crypto.d.ts +46 -27
  106. package/lib/Utils/crypto.js +188 -117
  107. package/lib/Utils/decode-wa-message.d.ts +45 -58
  108. package/lib/Utils/decode-wa-message.js +311 -212
  109. package/lib/Utils/event-buffer.d.ts +17 -14
  110. package/lib/Utils/event-buffer.js +296 -321
  111. package/lib/Utils/generics.d.ts +92 -66
  112. package/lib/Utils/generics.js +436 -270
  113. package/lib/Utils/history.d.ts +23 -24
  114. package/lib/Utils/history.js +76 -106
  115. package/lib/Utils/index.d.ts +21 -24
  116. package/lib/Utils/index.js +41 -24
  117. package/lib/Utils/link-preview.d.ts +14 -12
  118. package/lib/Utils/link-preview.js +75 -40
  119. package/lib/Utils/logger.d.ts +11 -10
  120. package/lib/Utils/logger.js +7 -3
  121. package/lib/Utils/lt-hash.d.ts +14 -8
  122. package/lib/Utils/lt-hash.js +53 -3
  123. package/lib/Utils/make-mutex.d.ts +7 -7
  124. package/lib/Utils/make-mutex.js +44 -28
  125. package/lib/Utils/message-retry-manager.d.ts +88 -115
  126. package/lib/Utils/message-retry-manager.js +160 -265
  127. package/lib/Utils/messages-media.d.ts +107 -105
  128. package/lib/Utils/messages-media.js +619 -548
  129. package/lib/Utils/messages.d.ts +66 -52
  130. package/lib/Utils/messages.js +1558 -1730
  131. package/lib/Utils/noise-handler.d.ts +18 -18
  132. package/lib/Utils/noise-handler.js +130 -176
  133. package/lib/Utils/process-message.d.ts +32 -43
  134. package/lib/Utils/process-message.js +282 -484
  135. package/lib/Utils/rich-message-utils.js +724 -1220
  136. package/lib/Utils/signal.d.ts +32 -37
  137. package/lib/Utils/signal.js +105 -140
  138. package/lib/Utils/use-multi-file-auth-state.d.ts +5 -5
  139. package/lib/Utils/use-multi-file-auth-state.js +120 -70
  140. package/lib/Utils/use-single-file-auth-state.d.ts +13 -11
  141. package/lib/Utils/use-single-file-auth-state.js +66 -92
  142. package/lib/Utils/validate-connection.d.ts +13 -11
  143. package/lib/Utils/validate-connection.js +113 -128
  144. package/lib/WABinary/constants.d.ts +27 -25
  145. package/lib/WABinary/constants.js +1292 -1451
  146. package/lib/WABinary/decode.d.ts +9 -7
  147. package/lib/WABinary/decode.js +189 -163
  148. package/lib/WABinary/encode.d.ts +3 -3
  149. package/lib/WABinary/encode.js +155 -110
  150. package/lib/WABinary/generic-utils.d.ts +27 -14
  151. package/lib/WABinary/generic-utils.js +105 -177
  152. package/lib/WABinary/index.d.ts +5 -6
  153. package/lib/WABinary/index.js +25 -6
  154. package/lib/WABinary/jid-utils.d.ts +54 -41
  155. package/lib/WABinary/jid-utils.js +152 -83
  156. package/lib/WABinary/types.d.ts +13 -10
  157. package/lib/WABinary/types.js +3 -2
  158. package/lib/WAM/BinaryInfo.d.ts +15 -8
  159. package/lib/WAM/BinaryInfo.js +14 -7
  160. package/lib/WAM/constants.d.ts +37 -30
  161. package/lib/WAM/constants.js +11983 -19465
  162. package/lib/WAM/encode.d.ts +3 -3
  163. package/lib/WAM/encode.js +109 -95
  164. package/lib/WAM/index.d.ts +3 -4
  165. package/lib/WAM/index.js +23 -4
  166. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -9
  167. package/lib/WAUSync/Protocols/USyncContactProtocol.js +21 -37
  168. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -19
  169. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +26 -18
  170. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -11
  171. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +20 -12
  172. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -11
  173. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +24 -16
  174. package/lib/WAUSync/Protocols/index.d.ts +6 -6
  175. package/lib/WAUSync/Protocols/index.js +26 -6
  176. package/lib/WAUSync/USyncQuery.d.ts +28 -27
  177. package/lib/WAUSync/USyncQuery.js +62 -68
  178. package/lib/WAUSync/USyncUser.d.ts +10 -15
  179. package/lib/WAUSync/USyncUser.js +19 -20
  180. package/lib/WAUSync/index.d.ts +3 -4
  181. package/lib/WAUSync/index.js +23 -4
  182. package/lib/index.d.ts +12 -11
  183. package/lib/index.js +39 -12
  184. package/package.json +42 -86
  185. package/engine-requirements.js +0 -10
@@ -1,37 +1,56 @@
1
- import type { KeyPair } from '../Types/index.js';
2
- export { md5, hkdf } from 'whatsapp-rust-bridge';
1
+ import { KeyPair } from '../Types'
2
+
3
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>;
4
+ export declare const generateSignalPubKey: (pubKey: Uint8Array | Buffer) => Uint8Array | Buffer
5
+
5
6
  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
- };
7
+ generateKeyPair: () => KeyPair
8
+ sharedKey: (privateKey: Uint8Array, publicKey: Uint8Array) => Buffer
9
+ sign: (privateKey: Uint8Array, buf: Uint8Array) => any
10
+ verify: (pubKey: Uint8Array, message: Uint8Array, signature: Uint8Array) => boolean
11
+ }
12
+
11
13
  export declare const signedKeyPair: (identityKeyPair: KeyPair, keyId: number) => {
12
- keyPair: KeyPair;
13
- signature: Uint8Array<ArrayBufferLike>;
14
- keyId: number;
15
- };
14
+ keyPair: KeyPair
15
+ signature: any
16
+ keyId: number
17
+ }
18
+
16
19
  /**
17
- * encrypt AES 256 GCM;
20
+ * encrypt AES 256 GCM
18
21
  * where the tag tag is suffixed to the ciphertext
19
22
  * */
20
- export declare function aesEncryptGCM(plaintext: Uint8Array, key: Uint8Array, iv: Uint8Array, additionalData: Uint8Array): Buffer<ArrayBuffer>;
23
+ export declare function aesEncryptGCM(plaintext: Uint8Array, key: Uint8Array, iv: Uint8Array, additionalData: Uint8Array): Buffer
24
+
21
25
  /**
22
- * decrypt AES 256 GCM;
26
+ * decrypt AES 256 GCM
23
27
  * where the auth tag is suffixed to the ciphertext
24
28
  * */
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>;
29
+ export declare function aesDecryptGCM(ciphertext: Uint8Array, key: Uint8Array, iv: Uint8Array, additionalData: Uint8Array): Buffer
30
+
31
+ export declare function aesEncryptCTR(plaintext: Uint8Array, key: Uint8Array, iv: Uint8Array): Buffer
32
+
33
+ export declare function aesDecryptCTR(ciphertext: Uint8Array, key: Uint8Array, iv: Uint8Array): Buffer
34
+
35
+ /** decrypt AES 256 CBC where the IV is prefixed to the buffer */
36
+ export declare function aesDecrypt(buffer: Buffer, key: Buffer): Buffer
30
37
  /** 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
38
+
39
+ export declare function aesDecryptWithIV(buffer: Buffer, key: Buffer, IV: Buffer): Buffer
40
+
41
+ export declare function aesEncrypt(buffer: Buffer | Uint8Array, key: Buffer): Buffer
42
+
43
+ export declare function aesEncrypWithIV(buffer: Buffer, key: Buffer, IV: Buffer): Buffer
44
+
45
+ export declare function hmacSign(buffer: Buffer | Uint8Array, key: Buffer | Uint8Array, variant?: 'sha256' | 'sha512'): Buffer
46
+
47
+ export declare function sha256(buffer: Buffer): Buffer
48
+
49
+ export declare function md5(buffer: Buffer): Buffer
50
+
51
+ export declare function hkdf(buffer: Uint8Array | Buffer, expandedLength: number, info: {
52
+ salt?: Buffer
53
+ info?: string
54
+ }): Promise<Buffer>
55
+
56
+ export declare function derivePairingCodeKey(pairingCode: string, salt: Buffer): Promise<Buffer>
@@ -1,118 +1,189 @@
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
- // 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
- export const Curve = {
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
- }
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
+
39
+ 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
31
58
  }
32
- };
33
- export const signedKeyPair = (identityKeyPair, keyId) => {
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 };
38
- };
39
- const GCM_TAG_LENGTH = 128 >> 3;
40
- /**
41
- * encrypt AES 256 GCM;
42
- * where the tag tag is suffixed to the ciphertext
43
- * */
44
- export function aesEncryptGCM(plaintext, key, iv, additionalData) {
45
- const cipher = createCipheriv('aes-256-gcm', key, iv);
46
- cipher.setAAD(additionalData);
47
- return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
48
- }
49
- /**
50
- * decrypt AES 256 GCM;
51
- * where the auth tag is suffixed to the ciphertext
52
- * */
53
- export function aesDecryptGCM(ciphertext, key, iv, additionalData) {
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()]);
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
- export function aesDecryptCTR(ciphertext, key, iv) {
68
- const decipher = createDecipheriv('aes-256-ctr', key, iv);
69
- return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
70
- }
71
- /** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
72
- export function aesDecrypt(buffer, key) {
73
- return aesDecryptWithIV(buffer.subarray(16), key, buffer.subarray(0, 16));
74
- }
75
- /** decrypt AES 256 CBC */
76
- export function aesDecryptWithIV(buffer, key, IV) {
77
- const aes = createDecipheriv('aes-256-cbc', key, IV);
78
- return Buffer.concat([aes.update(buffer), aes.final()]);
79
- }
80
- // encrypt AES 256 CBC; where a random IV is prefixed to the buffer
81
- export function aesEncrypt(buffer, key) {
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
85
- }
86
- // encrypt AES 256 CBC with a given IV
87
- export function aesEncrypWithIV(buffer, key, IV) {
88
- const aes = createCipheriv('aes-256-cbc', key, IV);
89
- return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer
90
- }
91
- // sign HMAC using SHA 256
92
- export function hmacSign(buffer, key, variant = 'sha256') {
93
- return createHmac(variant, key).update(buffer).digest();
94
- }
95
- export function sha256(buffer) {
96
- return createHash('sha256').update(buffer).digest();
97
- }
98
- export async function derivePairingCodeKey(pairingCode, salt) {
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);
117
- }
118
- //# sourceMappingURL=crypto.js.map
59
+ }
60
+ }
61
+
62
+ 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
+
71
+ 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()])
75
+ }
76
+
77
+ 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()])
84
+ }
85
+
86
+ 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()])
89
+ }
90
+
91
+ 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()])
94
+ }
95
+
96
+ function aesDecrypt(buffer, key) {
97
+ return aesDecryptWithIV(buffer.slice(16), key, buffer.slice(0, 16))
98
+ }
99
+
100
+ 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()])
103
+ }
104
+
105
+ 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()])
109
+ }
110
+
111
+ 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()])
114
+ }
115
+
116
+ function hmacSign(buffer, key, variant = 'sha256') {
117
+ return crypto_1.createHmac(variant, key).update(buffer).digest()
118
+ }
119
+
120
+ function sha256(buffer) {
121
+ return crypto_1.createHash('sha256').update(buffer).digest()
122
+ }
123
+
124
+ function md5(buffer) {
125
+ return crypto_1.createHash('md5').update(buffer).digest()
126
+ }
127
+
128
+ 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)
150
+ }
151
+
152
+ 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)
170
+ }
171
+
172
+ 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
+ }
@@ -1,66 +1,53 @@
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";
1
+ import { proto } from '../../WAProto'
2
+ import { SignalRepository } from '../Types'
3
+ import { BinaryNode } from '../WABinary'
4
+ import { ILogger } from './logger'
5
+
6
+ export declare const NO_MESSAGE_FOUND_ERROR_TEXT = "Message absent from node"
7
+
8
+ export declare const MISSING_KEYS_ERROR_TEXT = "Key used already or never filled"
9
+
9
10
  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) */
11
+ maxRetries: number
12
+ baseDelayMs: number
13
+ sessionRecordErrors: string[]
14
+ }
15
+
15
16
  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
- };
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
+
46
32
  export declare const extractAddressingContext: (stanza: BinaryNode) => {
47
- addressingMode: string;
48
- senderAlt: string | undefined;
49
- recipientAlt: string | undefined;
50
- };
33
+ addressingMode: string
34
+ senderAlt: string | undefined
35
+ recipientAlt: string | undefined
36
+ }
37
+
51
38
  /**
52
39
  * Decode the received node as a message.
53
40
  * @note this will only parse the message, not decrypt it
54
41
  */
55
42
  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
43
+ fullMessage: proto.IWebMessageInfo
44
+ author: string
45
+ sender: string
46
+ }
47
+
48
+ export declare const decryptMessageNode: (stanza: BinaryNode, meId: string, meLid: string, repository: SignalRepository, logger: ILogger) => {
49
+ fullMessage: proto.IWebMessageInfo
50
+ category: string
51
+ author: string
52
+ decrypt(): Promise<void>
53
+ }