@gara31/void-baileys 7.0.0-rc.10

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 (95) hide show
  1. package/LICENSE +21 -0
  2. package/WAProto/index.js +117292 -0
  3. package/lib/Defaults/baileys-version.json +3 -0
  4. package/lib/Defaults/index.js +116 -0
  5. package/lib/Signal/Group/ciphertext-message.js +12 -0
  6. package/lib/Signal/Group/group-session-builder.js +42 -0
  7. package/lib/Signal/Group/group_cipher.js +109 -0
  8. package/lib/Signal/Group/index.js +12 -0
  9. package/lib/Signal/Group/keyhelper.js +18 -0
  10. package/lib/Signal/Group/sender-chain-key.js +32 -0
  11. package/lib/Signal/Group/sender-key-distribution-message.js +67 -0
  12. package/lib/Signal/Group/sender-key-message.js +80 -0
  13. package/lib/Signal/Group/sender-key-name.js +50 -0
  14. package/lib/Signal/Group/sender-key-record.js +47 -0
  15. package/lib/Signal/Group/sender-key-state.js +105 -0
  16. package/lib/Signal/Group/sender-message-key.js +30 -0
  17. package/lib/Signal/libsignal.js +416 -0
  18. package/lib/Signal/lid-mapping.js +189 -0
  19. package/lib/Socket/Client/index.js +3 -0
  20. package/lib/Socket/Client/types.js +11 -0
  21. package/lib/Socket/Client/websocket.js +61 -0
  22. package/lib/Socket/business.js +404 -0
  23. package/lib/Socket/chats.js +1146 -0
  24. package/lib/Socket/communities.js +505 -0
  25. package/lib/Socket/groups.js +404 -0
  26. package/lib/Socket/index.js +18 -0
  27. package/lib/Socket/messages-recv.js +1600 -0
  28. package/lib/Socket/messages-send.js +1203 -0
  29. package/lib/Socket/mex.js +56 -0
  30. package/lib/Socket/newsletter.js +240 -0
  31. package/lib/Socket/socket.js +1060 -0
  32. package/lib/Types/Auth.js +2 -0
  33. package/lib/Types/Bussines.js +2 -0
  34. package/lib/Types/Call.js +2 -0
  35. package/lib/Types/Chat.js +8 -0
  36. package/lib/Types/Contact.js +2 -0
  37. package/lib/Types/Events.js +2 -0
  38. package/lib/Types/GroupMetadata.js +2 -0
  39. package/lib/Types/Label.js +25 -0
  40. package/lib/Types/LabelAssociation.js +7 -0
  41. package/lib/Types/Message.js +11 -0
  42. package/lib/Types/Newsletter.js +31 -0
  43. package/lib/Types/Product.js +2 -0
  44. package/lib/Types/Signal.js +2 -0
  45. package/lib/Types/Socket.js +3 -0
  46. package/lib/Types/State.js +13 -0
  47. package/lib/Types/USync.js +2 -0
  48. package/lib/Types/index.js +32 -0
  49. package/lib/Utils/auth-utils.js +276 -0
  50. package/lib/Utils/browser-utils.js +32 -0
  51. package/lib/Utils/business.js +262 -0
  52. package/lib/Utils/chat-utils.js +941 -0
  53. package/lib/Utils/crypto.js +179 -0
  54. package/lib/Utils/decode-wa-message.js +333 -0
  55. package/lib/Utils/event-buffer.js +580 -0
  56. package/lib/Utils/generics.js +436 -0
  57. package/lib/Utils/history.js +103 -0
  58. package/lib/Utils/index.js +19 -0
  59. package/lib/Utils/link-preview.js +105 -0
  60. package/lib/Utils/logger.js +3 -0
  61. package/lib/Utils/lt-hash.js +56 -0
  62. package/lib/Utils/make-mutex.js +38 -0
  63. package/lib/Utils/message-retry-manager.js +181 -0
  64. package/lib/Utils/messages-media.js +727 -0
  65. package/lib/Utils/messages.js +1309 -0
  66. package/lib/Utils/noise-handler.js +162 -0
  67. package/lib/Utils/pre-key-manager.js +125 -0
  68. package/lib/Utils/process-message.js +594 -0
  69. package/lib/Utils/signal.js +194 -0
  70. package/lib/Utils/use-multi-file-auth-state.js +118 -0
  71. package/lib/Utils/validate-connection.js +240 -0
  72. package/lib/WABinary/constants.js +1301 -0
  73. package/lib/WABinary/decode.js +240 -0
  74. package/lib/WABinary/encode.js +216 -0
  75. package/lib/WABinary/generic-utils.js +104 -0
  76. package/lib/WABinary/index.js +6 -0
  77. package/lib/WABinary/jid-utils.js +95 -0
  78. package/lib/WABinary/types.js +2 -0
  79. package/lib/WAM/BinaryInfo.js +10 -0
  80. package/lib/WAM/constants.js +22863 -0
  81. package/lib/WAM/encode.js +152 -0
  82. package/lib/WAM/index.js +4 -0
  83. package/lib/WAUSync/Protocols/USyncContactProtocol.js +29 -0
  84. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +59 -0
  85. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  86. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +36 -0
  87. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +60 -0
  88. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  89. package/lib/WAUSync/Protocols/index.js +5 -0
  90. package/lib/WAUSync/USyncQuery.js +104 -0
  91. package/lib/WAUSync/USyncUser.js +23 -0
  92. package/lib/WAUSync/index.js +4 -0
  93. package/lib/index.js +11 -0
  94. package/package.json +31 -0
  95. package/readme.md +522 -0
@@ -0,0 +1,162 @@
1
+ import { Boom } from "@hapi/boom";
2
+ import { proto } from "../../WAProto/index.js";
3
+ import { NOISE_MODE, WA_CERT_DETAILS } from "../Defaults/index.js";
4
+ import { decodeBinaryNode } from "../WABinary/index.js";
5
+ import { aesDecryptGCM, aesEncryptGCM, Curve, hkdf, sha256 } from "./crypto.js";
6
+ const generateIV = (counter) => {
7
+ const iv = new ArrayBuffer(12);
8
+ new DataView(iv).setUint32(8, counter);
9
+ return new Uint8Array(iv);
10
+ };
11
+ export const makeNoiseHandler = ({
12
+ keyPair: { private: privateKey, public: publicKey },
13
+ NOISE_HEADER,
14
+ logger,
15
+ routingInfo,
16
+ }) => {
17
+ logger = logger.child({ class: "ns" });
18
+ const authenticate = (data) => {
19
+ if (!isFinished) {
20
+ hash = sha256(Buffer.concat([hash, data]));
21
+ }
22
+ };
23
+ const encrypt = (plaintext) => {
24
+ const result = aesEncryptGCM(
25
+ plaintext,
26
+ encKey,
27
+ generateIV(writeCounter),
28
+ hash,
29
+ );
30
+ writeCounter += 1;
31
+ authenticate(result);
32
+ return result;
33
+ };
34
+ const decrypt = (ciphertext) => {
35
+ // before the handshake is finished, we use the same counter
36
+ // after handshake, the counters are different
37
+ const iv = generateIV(isFinished ? readCounter : writeCounter);
38
+ const result = aesDecryptGCM(ciphertext, decKey, iv, hash);
39
+ if (isFinished) {
40
+ readCounter += 1;
41
+ } else {
42
+ writeCounter += 1;
43
+ }
44
+ authenticate(ciphertext);
45
+ return result;
46
+ };
47
+ const localHKDF = async (data) => {
48
+ const key = await hkdf(Buffer.from(data), 64, { salt, info: "" });
49
+ return [key.slice(0, 32), key.slice(32)];
50
+ };
51
+ const mixIntoKey = async (data) => {
52
+ const [write, read] = await localHKDF(data);
53
+ salt = write;
54
+ encKey = read;
55
+ decKey = read;
56
+ readCounter = 0;
57
+ writeCounter = 0;
58
+ };
59
+ const finishInit = async () => {
60
+ const [write, read] = await localHKDF(new Uint8Array(0));
61
+ encKey = write;
62
+ decKey = read;
63
+ hash = Buffer.from([]);
64
+ readCounter = 0;
65
+ writeCounter = 0;
66
+ isFinished = true;
67
+ };
68
+ const data = Buffer.from(NOISE_MODE);
69
+ let hash = data.byteLength === 32 ? data : sha256(data);
70
+ let salt = hash;
71
+ let encKey = hash;
72
+ let decKey = hash;
73
+ let readCounter = 0;
74
+ let writeCounter = 0;
75
+ let isFinished = false;
76
+ let sentIntro = false;
77
+ let inBytes = Buffer.alloc(0);
78
+ authenticate(NOISE_HEADER);
79
+ authenticate(publicKey);
80
+ return {
81
+ encrypt,
82
+ decrypt,
83
+ authenticate,
84
+ mixIntoKey,
85
+ finishInit,
86
+ processHandshake: async ({ serverHello }, noiseKey) => {
87
+ authenticate(serverHello.ephemeral);
88
+ await mixIntoKey(Curve.sharedKey(privateKey, serverHello.ephemeral));
89
+ const decStaticContent = decrypt(serverHello.static);
90
+ await mixIntoKey(Curve.sharedKey(privateKey, decStaticContent));
91
+ const certDecoded = decrypt(serverHello.payload);
92
+ const { intermediate: certIntermediate /*leaf*/ } =
93
+ proto.CertChain.decode(certDecoded);
94
+ // TODO: handle this leaf stuff
95
+ const { issuerSerial } = proto.CertChain.NoiseCertificate.Details.decode(
96
+ certIntermediate.details,
97
+ );
98
+ if (issuerSerial !== WA_CERT_DETAILS.SERIAL) {
99
+ throw new Boom("certification match failed", { statusCode: 400 });
100
+ }
101
+ const keyEnc = encrypt(noiseKey.public);
102
+ await mixIntoKey(
103
+ Curve.sharedKey(noiseKey.private, serverHello.ephemeral),
104
+ );
105
+ return keyEnc;
106
+ },
107
+ encodeFrame: (data) => {
108
+ if (isFinished) {
109
+ data = encrypt(data);
110
+ }
111
+ let header;
112
+ if (routingInfo) {
113
+ header = Buffer.alloc(7);
114
+ header.write("ED", 0, "utf8");
115
+ header.writeUint8(0, 2);
116
+ header.writeUint8(1, 3);
117
+ header.writeUint8(routingInfo.byteLength >> 16, 4);
118
+ header.writeUint16BE(routingInfo.byteLength & 65535, 5);
119
+ header = Buffer.concat([header, routingInfo, NOISE_HEADER]);
120
+ } else {
121
+ header = Buffer.from(NOISE_HEADER);
122
+ }
123
+ const introSize = sentIntro ? 0 : header.length;
124
+ const frame = Buffer.alloc(introSize + 3 + data.byteLength);
125
+ if (!sentIntro) {
126
+ frame.set(header);
127
+ sentIntro = true;
128
+ }
129
+ frame.writeUInt8(data.byteLength >> 16, introSize);
130
+ frame.writeUInt16BE(65535 & data.byteLength, introSize + 1);
131
+ frame.set(data, introSize + 3);
132
+ return frame;
133
+ },
134
+ decodeFrame: async (newData, onFrame) => {
135
+ // the binary protocol uses its own framing mechanism
136
+ // on top of the WS frames
137
+ // so we get this data and separate out the frames
138
+ const getBytesSize = () => {
139
+ if (inBytes.length >= 3) {
140
+ return (inBytes.readUInt8() << 16) | inBytes.readUInt16BE(1);
141
+ }
142
+ };
143
+ inBytes = Buffer.concat([inBytes, newData]);
144
+ logger.trace(
145
+ `recv ${newData.length} bytes, total recv ${inBytes.length} bytes`,
146
+ );
147
+ let size = getBytesSize();
148
+ while (size && inBytes.length >= size + 3) {
149
+ let frame = inBytes.slice(3, size + 3);
150
+ inBytes = inBytes.slice(size + 3);
151
+ if (isFinished) {
152
+ const result = decrypt(frame);
153
+ frame = await decodeBinaryNode(result);
154
+ }
155
+ logger.trace({ msg: frame?.attrs?.id }, "recv frame");
156
+ onFrame(frame);
157
+ size = getBytesSize();
158
+ }
159
+ },
160
+ };
161
+ };
162
+ //# sourceMappingURL=noise-handler.js.map
@@ -0,0 +1,125 @@
1
+ import PQueue from "p-queue";
2
+ /**
3
+ * Manages pre-key operations with proper concurrency control
4
+ */
5
+ export class PreKeyManager {
6
+ constructor(store, logger) {
7
+ this.store = store;
8
+ this.logger = logger;
9
+ this.queues = new Map();
10
+ }
11
+ /**
12
+ * Get or create a queue for a specific key type
13
+ */
14
+ getQueue(keyType) {
15
+ if (!this.queues.has(keyType)) {
16
+ this.queues.set(keyType, new PQueue({ concurrency: 1 }));
17
+ }
18
+ return this.queues.get(keyType);
19
+ }
20
+ /**
21
+ * Process pre-key operations (updates and deletions)
22
+ */
23
+ async processOperations(
24
+ data,
25
+ keyType,
26
+ transactionCache,
27
+ mutations,
28
+ isInTransaction,
29
+ ) {
30
+ const keyData = data[keyType];
31
+ if (!keyData) return;
32
+ return this.getQueue(keyType).add(async () => {
33
+ // Ensure structures exist
34
+ transactionCache[keyType] = transactionCache[keyType] || {};
35
+ mutations[keyType] = mutations[keyType] || {};
36
+ // Separate deletions from updates
37
+ const deletions = [];
38
+ const updates = {};
39
+ for (const keyId in keyData) {
40
+ if (keyData[keyId] === null) {
41
+ deletions.push(keyId);
42
+ } else {
43
+ updates[keyId] = keyData[keyId];
44
+ }
45
+ }
46
+ // Process updates (no validation needed)
47
+ if (Object.keys(updates).length > 0) {
48
+ Object.assign(transactionCache[keyType], updates);
49
+ Object.assign(mutations[keyType], updates);
50
+ }
51
+ // Process deletions with validation
52
+ if (deletions.length > 0) {
53
+ await this.processDeletions(
54
+ keyType,
55
+ deletions,
56
+ transactionCache,
57
+ mutations,
58
+ isInTransaction,
59
+ );
60
+ }
61
+ });
62
+ }
63
+ /**
64
+ * Process deletions with validation
65
+ */
66
+ async processDeletions(
67
+ keyType,
68
+ ids,
69
+ transactionCache,
70
+ mutations,
71
+ isInTransaction,
72
+ ) {
73
+ if (isInTransaction) {
74
+ // In transaction, only allow deletion if key exists in cache
75
+ for (const keyId of ids) {
76
+ if (transactionCache[keyType]?.[keyId]) {
77
+ transactionCache[keyType][keyId] = null;
78
+ mutations[keyType][keyId] = null;
79
+ } else {
80
+ this.logger.warn(
81
+ `Skipping deletion of non-existent ${keyType} in transaction: ${keyId}`,
82
+ );
83
+ }
84
+ }
85
+ } else {
86
+ // Outside transaction, validate against store
87
+ const existingKeys = await this.store.get(keyType, ids);
88
+ for (const keyId of ids) {
89
+ if (existingKeys[keyId]) {
90
+ transactionCache[keyType][keyId] = null;
91
+ mutations[keyType][keyId] = null;
92
+ } else {
93
+ this.logger.warn(
94
+ `Skipping deletion of non-existent ${keyType}: ${keyId}`,
95
+ );
96
+ }
97
+ }
98
+ }
99
+ }
100
+ /**
101
+ * Validate and process pre-key deletions outside transactions
102
+ */
103
+ async validateDeletions(data, keyType) {
104
+ const keyData = data[keyType];
105
+ if (!keyData) return;
106
+ return this.getQueue(keyType).add(async () => {
107
+ // Find all deletion requests
108
+ const deletionIds = Object.keys(keyData).filter(
109
+ (id) => keyData[id] === null,
110
+ );
111
+ if (deletionIds.length === 0) return;
112
+ // Validate deletions
113
+ const existingKeys = await this.store.get(keyType, deletionIds);
114
+ for (const keyId of deletionIds) {
115
+ if (!existingKeys[keyId]) {
116
+ this.logger.warn(
117
+ `Skipping deletion of non-existent ${keyType}: ${keyId}`,
118
+ );
119
+ delete data[keyType][keyId];
120
+ }
121
+ }
122
+ });
123
+ }
124
+ }
125
+ //# sourceMappingURL=pre-key-manager.js.map