torbaileys 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/WAProto/WAProto.proto +5311 -0
- package/WAProto/index.js +94091 -0
- package/lib/Defaults/index.js +123 -0
- package/lib/KeyDB/BinarySearch.js +20 -0
- package/lib/KeyDB/KeyedDB.js +167 -0
- package/lib/KeyDB/index.js +4 -0
- package/lib/Signal/Group/ciphertext-message.js +13 -0
- package/lib/Signal/Group/group-session-builder.js +32 -0
- package/lib/Signal/Group/group_cipher.js +84 -0
- package/lib/Signal/Group/index.js +13 -0
- package/lib/Signal/Group/keyhelper.js +20 -0
- package/lib/Signal/Group/sender-chain-key.js +28 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +65 -0
- package/lib/Signal/Group/sender-key-message.js +68 -0
- package/lib/Signal/Group/sender-key-name.js +52 -0
- package/lib/Signal/Group/sender-key-record.js +43 -0
- package/lib/Signal/Group/sender-key-state.js +86 -0
- package/lib/Signal/Group/sender-message-key.js +28 -0
- package/lib/Signal/libsignal.js +324 -0
- package/lib/Signal/lid-mapping.js +155 -0
- package/lib/Socket/Client/index.js +4 -0
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.js +52 -0
- package/lib/Socket/Client/websocket.js.bak +53 -0
- package/lib/Socket/business.js +377 -0
- package/lib/Socket/chats.js +881 -0
- package/lib/Socket/communities.js +413 -0
- package/lib/Socket/groups.js +312 -0
- package/lib/Socket/index.js +16 -0
- package/lib/Socket/messages-recv.js +1163 -0
- package/lib/Socket/messages-send.js +1100 -0
- package/lib/Socket/mex.js +45 -0
- package/lib/Socket/newsletter.js +262 -0
- package/lib/Socket/socket.js +820 -0
- package/lib/Store/index.js +6 -0
- package/lib/Store/make-cache-manager-store.js +75 -0
- package/lib/Store/make-in-memory-store.js +290 -0
- package/lib/Store/make-ordered-dictionary.js +79 -0
- package/lib/Store/object-repository.js +25 -0
- package/lib/Types/Auth.js +3 -0
- package/lib/Types/Bussines.js +3 -0
- package/lib/Types/Call.js +3 -0
- package/lib/Types/Chat.js +9 -0
- package/lib/Types/Contact.js +3 -0
- package/lib/Types/Events.js +3 -0
- package/lib/Types/GroupMetadata.js +3 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.js +12 -0
- package/lib/Types/Newsletter.js +33 -0
- package/lib/Types/Newsletter.js.bak +33 -0
- package/lib/Types/Product.js +3 -0
- package/lib/Types/Signal.js +3 -0
- package/lib/Types/Socket.js +4 -0
- package/lib/Types/State.js +11 -0
- package/lib/Types/USync.js +3 -0
- package/lib/Types/index.js +28 -0
- package/lib/Utils/auth-utils.js +219 -0
- package/lib/Utils/baileys-event-stream.js +44 -0
- package/lib/Utils/browser-utils.js +25 -0
- package/lib/Utils/business.js +233 -0
- package/lib/Utils/chat-utils.js +752 -0
- package/lib/Utils/crypto.js +130 -0
- package/lib/Utils/decode-wa-message.js +267 -0
- package/lib/Utils/decode-wa-message.js.bak +267 -0
- package/lib/Utils/event-buffer.js +528 -0
- package/lib/Utils/generics.js +355 -0
- package/lib/Utils/history.js +87 -0
- package/lib/Utils/index.js +21 -0
- package/lib/Utils/link-preview.js +81 -0
- package/lib/Utils/logger.js +5 -0
- package/lib/Utils/lt-hash.js +45 -0
- package/lib/Utils/make-mutex.js +36 -0
- package/lib/Utils/message-retry-manager.js +113 -0
- package/lib/Utils/messages-media.js +601 -0
- package/lib/Utils/messages.js +907 -0
- package/lib/Utils/noise-handler.js +144 -0
- package/lib/Utils/pre-key-manager.js +85 -0
- package/lib/Utils/process-message.js +341 -0
- package/lib/Utils/signal.js +161 -0
- package/lib/Utils/use-multi-file-auth-state.js +111 -0
- package/lib/Utils/validate-connection.js +200 -0
- package/lib/WABinary/constants.js +1303 -0
- package/lib/WABinary/decode.js +240 -0
- package/lib/WABinary/encode.js +218 -0
- package/lib/WABinary/generic-utils.js +189 -0
- package/lib/WABinary/index.js +7 -0
- package/lib/WABinary/jid-utils.js +93 -0
- package/lib/WABinary/types.js +3 -0
- package/lib/WAM/BinaryInfo.js +11 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.js +154 -0
- package/lib/WAM/index.js +5 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +29 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +39 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +30 -0
- package/lib/WAUSync/Protocols/index.js +6 -0
- package/lib/WAUSync/USyncQuery.js +90 -0
- package/lib/WAUSync/USyncUser.js +24 -0
- package/lib/WAUSync/index.js +5 -0
- package/lib/index.js +16 -0
- package/package.json +105 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { aesDecryptGCM, aesEncryptGCM, Curve, hkdf, sha256 } from "./crypto.js";
|
|
3
|
+
import { NOISE_MODE, WA_CERT_DETAILS } from "../Defaults/index.js";
|
|
4
|
+
import { decodeBinaryNode } from "../WABinary/index.js";
|
|
5
|
+
import { proto } from "../../WAProto/index.js";
|
|
6
|
+
import { Boom } from "@hapi/boom";
|
|
7
|
+
//=======================================================//
|
|
8
|
+
const generateIV = (counter) => {
|
|
9
|
+
const iv = new ArrayBuffer(12);
|
|
10
|
+
new DataView(iv).setUint32(8, counter);
|
|
11
|
+
return new Uint8Array(iv);
|
|
12
|
+
};
|
|
13
|
+
//=======================================================//
|
|
14
|
+
export const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey }, NOISE_HEADER, logger, routingInfo }) => {
|
|
15
|
+
logger = logger.child({ class: "ns" });
|
|
16
|
+
const authenticate = (data) => {
|
|
17
|
+
if (!isFinished) {
|
|
18
|
+
hash = sha256(Buffer.concat([hash, data]));
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const encrypt = (plaintext) => {
|
|
22
|
+
const result = aesEncryptGCM(plaintext, encKey, generateIV(writeCounter), hash);
|
|
23
|
+
writeCounter += 1;
|
|
24
|
+
authenticate(result);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
const decrypt = (ciphertext) => {
|
|
28
|
+
const iv = generateIV(isFinished ? readCounter : writeCounter);
|
|
29
|
+
const result = aesDecryptGCM(ciphertext, decKey, iv, hash);
|
|
30
|
+
if (isFinished) {
|
|
31
|
+
readCounter += 1;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
writeCounter += 1;
|
|
35
|
+
}
|
|
36
|
+
authenticate(ciphertext);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
const localHKDF = async (data) => {
|
|
40
|
+
const key = await hkdf(Buffer.from(data), 64, { salt, info: "" });
|
|
41
|
+
return [key.slice(0, 32), key.slice(32)];
|
|
42
|
+
};
|
|
43
|
+
const mixIntoKey = async (data) => {
|
|
44
|
+
const [write, read] = await localHKDF(data);
|
|
45
|
+
salt = write;
|
|
46
|
+
encKey = read;
|
|
47
|
+
decKey = read;
|
|
48
|
+
readCounter = 0;
|
|
49
|
+
writeCounter = 0;
|
|
50
|
+
};
|
|
51
|
+
const finishInit = async () => {
|
|
52
|
+
const [write, read] = await localHKDF(new Uint8Array(0));
|
|
53
|
+
encKey = write;
|
|
54
|
+
decKey = read;
|
|
55
|
+
hash = Buffer.from([]);
|
|
56
|
+
readCounter = 0;
|
|
57
|
+
writeCounter = 0;
|
|
58
|
+
isFinished = true;
|
|
59
|
+
};
|
|
60
|
+
const data = Buffer.from(NOISE_MODE);
|
|
61
|
+
let hash = data.byteLength === 32 ? data : sha256(data);
|
|
62
|
+
let salt = hash;
|
|
63
|
+
let encKey = hash;
|
|
64
|
+
let decKey = hash;
|
|
65
|
+
let readCounter = 0;
|
|
66
|
+
let writeCounter = 0;
|
|
67
|
+
let isFinished = false;
|
|
68
|
+
let sentIntro = false;
|
|
69
|
+
let inBytes = Buffer.alloc(0);
|
|
70
|
+
authenticate(NOISE_HEADER);
|
|
71
|
+
authenticate(publicKey);
|
|
72
|
+
return {
|
|
73
|
+
encrypt,
|
|
74
|
+
decrypt,
|
|
75
|
+
authenticate,
|
|
76
|
+
mixIntoKey,
|
|
77
|
+
finishInit,
|
|
78
|
+
processHandshake: async ({ serverHello }, noiseKey) => {
|
|
79
|
+
authenticate(serverHello.ephemeral);
|
|
80
|
+
await mixIntoKey(Curve.sharedKey(privateKey, serverHello.ephemeral));
|
|
81
|
+
const decStaticContent = decrypt(serverHello.static);
|
|
82
|
+
await mixIntoKey(Curve.sharedKey(privateKey, decStaticContent));
|
|
83
|
+
const certDecoded = decrypt(serverHello.payload);
|
|
84
|
+
const { intermediate: certIntermediate } = proto.CertChain.decode(certDecoded);
|
|
85
|
+
const { issuerSerial } = proto.CertChain.NoiseCertificate.Details.decode(certIntermediate.details);
|
|
86
|
+
if (issuerSerial !== WA_CERT_DETAILS.SERIAL) {
|
|
87
|
+
throw new Boom("certification match failed", { statusCode: 400 });
|
|
88
|
+
}
|
|
89
|
+
const keyEnc = encrypt(noiseKey.public);
|
|
90
|
+
await mixIntoKey(Curve.sharedKey(noiseKey.private, serverHello.ephemeral));
|
|
91
|
+
return keyEnc;
|
|
92
|
+
},
|
|
93
|
+
encodeFrame: (data) => {
|
|
94
|
+
if (isFinished) {
|
|
95
|
+
data = encrypt(data);
|
|
96
|
+
}
|
|
97
|
+
let header;
|
|
98
|
+
if (routingInfo) {
|
|
99
|
+
header = Buffer.alloc(7);
|
|
100
|
+
header.write("ED", 0, "utf8");
|
|
101
|
+
header.writeUint8(0, 2);
|
|
102
|
+
header.writeUint8(1, 3);
|
|
103
|
+
header.writeUint8(routingInfo.byteLength >> 16, 4);
|
|
104
|
+
header.writeUint16BE(routingInfo.byteLength & 65535, 5);
|
|
105
|
+
header = Buffer.concat([header, routingInfo, NOISE_HEADER]);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
header = Buffer.from(NOISE_HEADER);
|
|
109
|
+
}
|
|
110
|
+
const introSize = sentIntro ? 0 : header.length;
|
|
111
|
+
const frame = Buffer.alloc(introSize + 3 + data.byteLength);
|
|
112
|
+
if (!sentIntro) {
|
|
113
|
+
frame.set(header);
|
|
114
|
+
sentIntro = true;
|
|
115
|
+
}
|
|
116
|
+
frame.writeUInt8(data.byteLength >> 16, introSize);
|
|
117
|
+
frame.writeUInt16BE(65535 & data.byteLength, introSize + 1);
|
|
118
|
+
frame.set(data, introSize + 3);
|
|
119
|
+
return frame;
|
|
120
|
+
},
|
|
121
|
+
decodeFrame: async (newData, onFrame) => {
|
|
122
|
+
const getBytesSize = () => {
|
|
123
|
+
if (inBytes.length >= 3) {
|
|
124
|
+
return (inBytes.readUInt8() << 16) | inBytes.readUInt16BE(1);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
inBytes = Buffer.concat([inBytes, newData]);
|
|
128
|
+
logger.trace(`recv ${newData.length} bytes, total recv ${inBytes.length} bytes`);
|
|
129
|
+
let size = getBytesSize();
|
|
130
|
+
while (size && inBytes.length >= size + 3) {
|
|
131
|
+
let frame = inBytes.slice(3, size + 3);
|
|
132
|
+
inBytes = inBytes.slice(size + 3);
|
|
133
|
+
if (isFinished) {
|
|
134
|
+
const result = decrypt(frame);
|
|
135
|
+
frame = await decodeBinaryNode(result);
|
|
136
|
+
}
|
|
137
|
+
logger.trace({ msg: frame?.attrs?.id }, "recv frame");
|
|
138
|
+
onFrame(frame);
|
|
139
|
+
size = getBytesSize();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
//=======================================================//
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import PQueue from "p-queue";
|
|
3
|
+
//=======================================================//
|
|
4
|
+
export class PreKeyManager {
|
|
5
|
+
constructor(store, logger) {
|
|
6
|
+
this.store = store;
|
|
7
|
+
this.logger = logger;
|
|
8
|
+
this.queues = new Map();
|
|
9
|
+
}
|
|
10
|
+
getQueue(keyType) {
|
|
11
|
+
if (!this.queues.has(keyType)) {
|
|
12
|
+
this.queues.set(keyType, new PQueue({ concurrency: 1 }));
|
|
13
|
+
}
|
|
14
|
+
return this.queues.get(keyType);
|
|
15
|
+
}
|
|
16
|
+
async processOperations(data, keyType, transactionCache, mutations, isInTransaction) {
|
|
17
|
+
const keyData = data[keyType];
|
|
18
|
+
if (!keyData)
|
|
19
|
+
return;
|
|
20
|
+
return this.getQueue(keyType).add(async () => {
|
|
21
|
+
transactionCache[keyType] = transactionCache[keyType] || {};
|
|
22
|
+
mutations[keyType] = mutations[keyType] || {};
|
|
23
|
+
const deletions = [];
|
|
24
|
+
const updates = {};
|
|
25
|
+
for (const keyId in keyData) {
|
|
26
|
+
if (keyData[keyId] === null) {
|
|
27
|
+
deletions.push(keyId);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
updates[keyId] = keyData[keyId];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (Object.keys(updates).length > 0) {
|
|
34
|
+
Object.assign(transactionCache[keyType], updates);
|
|
35
|
+
Object.assign(mutations[keyType], updates);
|
|
36
|
+
}
|
|
37
|
+
if (deletions.length > 0) {
|
|
38
|
+
await this.processDeletions(keyType, deletions, transactionCache, mutations, isInTransaction);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
async processDeletions(keyType, ids, transactionCache, mutations, isInTransaction) {
|
|
43
|
+
if (isInTransaction) {
|
|
44
|
+
for (const keyId of ids) {
|
|
45
|
+
if (transactionCache[keyType]?.[keyId]) {
|
|
46
|
+
transactionCache[keyType][keyId] = null;
|
|
47
|
+
mutations[keyType][keyId] = null;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
this.logger.warn(`Skipping deletion of non-existent ${keyType} in transaction: ${keyId}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
const existingKeys = await this.store.get(keyType, ids);
|
|
56
|
+
for (const keyId of ids) {
|
|
57
|
+
if (existingKeys[keyId]) {
|
|
58
|
+
transactionCache[keyType][keyId] = null;
|
|
59
|
+
mutations[keyType][keyId] = null;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
this.logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async validateDeletions(data, keyType) {
|
|
68
|
+
const keyData = data[keyType];
|
|
69
|
+
if (!keyData)
|
|
70
|
+
return;
|
|
71
|
+
return this.getQueue(keyType).add(async () => {
|
|
72
|
+
const deletionIds = Object.keys(keyData).filter(id => keyData[id] === null);
|
|
73
|
+
if (deletionIds.length === 0)
|
|
74
|
+
return;
|
|
75
|
+
const existingKeys = await this.store.get(keyType, deletionIds);
|
|
76
|
+
for (const keyId of deletionIds) {
|
|
77
|
+
if (!existingKeys[keyId]) {
|
|
78
|
+
this.logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`);
|
|
79
|
+
delete data[keyType][keyId];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//=======================================================//
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { areJidsSameUser, isHostedLidUser, isHostedPnUser, isJidBroadcast, isJidStatusBroadcast, jidDecode, jidEncode, jidNormalizedUser } from "../WABinary/index.js";
|
|
3
|
+
import { getContentType, normalizeMessageContent } from "../Utils/messages.js";
|
|
4
|
+
import { downloadAndProcessHistorySyncNotification } from "./history.js";
|
|
5
|
+
import { WAMessageStubType } from "../Types/index.js";
|
|
6
|
+
import { aesDecryptGCM, hmacSign } from "./crypto.js";
|
|
7
|
+
import { proto } from "../../WAProto/index.js";
|
|
8
|
+
import { toNumber } from "./generics.js";
|
|
9
|
+
//=======================================================//
|
|
10
|
+
const REAL_MSG_STUB_TYPES = new Set([
|
|
11
|
+
WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
|
|
12
|
+
WAMessageStubType.CALL_MISSED_GROUP_VOICE,
|
|
13
|
+
WAMessageStubType.CALL_MISSED_VIDEO,
|
|
14
|
+
WAMessageStubType.CALL_MISSED_VOICE
|
|
15
|
+
]);
|
|
16
|
+
//=======================================================//
|
|
17
|
+
const REAL_MSG_REQ_ME_STUB_TYPES = new Set([WAMessageStubType.GROUP_PARTICIPANT_ADD]);
|
|
18
|
+
//=======================================================//
|
|
19
|
+
export const cleanMessage = (message, meId, meLid) => {
|
|
20
|
+
if (isHostedPnUser(message.key.remoteJid) || isHostedLidUser(message.key.remoteJid)) {
|
|
21
|
+
message.key.remoteJid = jidEncode(jidDecode(message.key?.remoteJid)?.user, isHostedPnUser(message.key.remoteJid) ? "s.whatsapp.net" : "lid");
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
message.key.remoteJid = jidNormalizedUser(message.key.remoteJid);
|
|
25
|
+
}
|
|
26
|
+
if (isHostedPnUser(message.key.participant) || isHostedLidUser(message.key.participant)) {
|
|
27
|
+
message.key.participant = jidEncode(jidDecode(message.key.participant)?.user, isHostedPnUser(message.key.participant) ? "s.whatsapp.net" : "lid");
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
message.key.participant = jidNormalizedUser(message.key.participant);
|
|
31
|
+
}
|
|
32
|
+
const content = normalizeMessageContent(message.message);
|
|
33
|
+
if (content?.reactionMessage) {
|
|
34
|
+
normaliseKey(content.reactionMessage.key);
|
|
35
|
+
}
|
|
36
|
+
if (content?.pollUpdateMessage) {
|
|
37
|
+
normaliseKey(content.pollUpdateMessage.pollCreationMessageKey);
|
|
38
|
+
}
|
|
39
|
+
function normaliseKey(msgKey) {
|
|
40
|
+
if (!message.key.fromMe) {
|
|
41
|
+
msgKey.fromMe = !msgKey.fromMe
|
|
42
|
+
? areJidsSameUser(msgKey.participant || msgKey.remoteJid, meId) ||
|
|
43
|
+
areJidsSameUser(msgKey.participant || msgKey.remoteJid, meLid)
|
|
44
|
+
:
|
|
45
|
+
false;
|
|
46
|
+
msgKey.remoteJid = message.key.remoteJid;
|
|
47
|
+
msgKey.participant = msgKey.participant || message.key.participant;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
//=======================================================//
|
|
52
|
+
export const isRealMessage = (message) => {
|
|
53
|
+
const normalizedContent = normalizeMessageContent(message.message);
|
|
54
|
+
const hasSomeContent = !!getContentType(normalizedContent);
|
|
55
|
+
return ((!!normalizedContent ||
|
|
56
|
+
REAL_MSG_STUB_TYPES.has(message.messageStubType) ||
|
|
57
|
+
REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType)) &&
|
|
58
|
+
hasSomeContent &&
|
|
59
|
+
!normalizedContent?.protocolMessage &&
|
|
60
|
+
!normalizedContent?.reactionMessage &&
|
|
61
|
+
!normalizedContent?.pollUpdateMessage);
|
|
62
|
+
};
|
|
63
|
+
//=======================================================//
|
|
64
|
+
export const shouldIncrementChatUnread = (message) => !message.key.fromMe && !message.messageStubType;
|
|
65
|
+
//=======================================================//
|
|
66
|
+
export const getChatId = ({ remoteJid, participant, fromMe }) => {
|
|
67
|
+
if (isJidBroadcast(remoteJid) && !isJidStatusBroadcast(remoteJid) && !fromMe) {
|
|
68
|
+
return participant;
|
|
69
|
+
}
|
|
70
|
+
return remoteJid;
|
|
71
|
+
};
|
|
72
|
+
//=======================================================//
|
|
73
|
+
export function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid }) {
|
|
74
|
+
const sign = Buffer.concat([
|
|
75
|
+
toBinary(pollMsgId),
|
|
76
|
+
toBinary(pollCreatorJid),
|
|
77
|
+
toBinary(voterJid),
|
|
78
|
+
toBinary("Poll Vote"),
|
|
79
|
+
new Uint8Array([1])
|
|
80
|
+
]);
|
|
81
|
+
const key0 = hmacSign(pollEncKey, new Uint8Array(32), "sha256");
|
|
82
|
+
const decKey = hmacSign(sign, key0, "sha256");
|
|
83
|
+
const aad = toBinary(`${pollMsgId}\u0000${voterJid}`);
|
|
84
|
+
const decrypted = aesDecryptGCM(encPayload, decKey, encIv, aad);
|
|
85
|
+
return proto.Message.PollVoteMessage.decode(decrypted);
|
|
86
|
+
function toBinary(txt) {
|
|
87
|
+
return Buffer.from(txt);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const processMessage = async (message, { shouldProcessHistoryMsg, placeholderResendCache, ev, creds, signalRepository, keyStore, logger, options }) => {
|
|
91
|
+
const meId = creds.me.id;
|
|
92
|
+
const { accountSettings } = creds;
|
|
93
|
+
const chat = { id: jidNormalizedUser(getChatId(message.key)) };
|
|
94
|
+
const isRealMsg = isRealMessage(message);
|
|
95
|
+
if (isRealMsg) {
|
|
96
|
+
chat.messages = [{ message }];
|
|
97
|
+
chat.conversationTimestamp = toNumber(message.messageTimestamp);
|
|
98
|
+
if (shouldIncrementChatUnread(message)) {
|
|
99
|
+
chat.unreadCount = (chat.unreadCount || 0) + 1;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const content = normalizeMessageContent(message.message);
|
|
103
|
+
if ((isRealMsg || content?.reactionMessage?.key?.fromMe) && accountSettings?.unarchiveChats) {
|
|
104
|
+
chat.archived = false;
|
|
105
|
+
chat.readOnly = false;
|
|
106
|
+
}
|
|
107
|
+
const protocolMsg = content?.protocolMessage;
|
|
108
|
+
if (protocolMsg) {
|
|
109
|
+
switch (protocolMsg.type) {
|
|
110
|
+
case proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
|
|
111
|
+
const histNotification = protocolMsg.historySyncNotification;
|
|
112
|
+
const process = shouldProcessHistoryMsg;
|
|
113
|
+
const isLatest = !creds.processedHistoryMessages?.length;
|
|
114
|
+
logger?.info({
|
|
115
|
+
histNotification,
|
|
116
|
+
process,
|
|
117
|
+
id: message.key.id,
|
|
118
|
+
isLatest
|
|
119
|
+
}, "got history notification");
|
|
120
|
+
if (process) {
|
|
121
|
+
if (histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND) {
|
|
122
|
+
ev.emit("creds.update", {
|
|
123
|
+
processedHistoryMessages: [
|
|
124
|
+
...(creds.processedHistoryMessages || []),
|
|
125
|
+
{ key: message.key, messageTimestamp: message.messageTimestamp }
|
|
126
|
+
]
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
const data = await downloadAndProcessHistorySyncNotification(histNotification, options);
|
|
130
|
+
ev.emit("messaging-history.set", {
|
|
131
|
+
...data,
|
|
132
|
+
isLatest: histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND ? isLatest : undefined,
|
|
133
|
+
peerDataRequestSessionId: histNotification.peerDataRequestSessionId
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
case proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
|
|
138
|
+
const keys = protocolMsg.appStateSyncKeyShare.keys;
|
|
139
|
+
if (keys?.length) {
|
|
140
|
+
let newAppStateSyncKeyId = "";
|
|
141
|
+
await keyStore.transaction(async () => {
|
|
142
|
+
const newKeys = [];
|
|
143
|
+
for (const { keyData, keyId } of keys) {
|
|
144
|
+
const strKeyId = Buffer.from(keyId.keyId).toString("base64");
|
|
145
|
+
newKeys.push(strKeyId);
|
|
146
|
+
await keyStore.set({ "app-state-sync-key": { [strKeyId]: keyData } });
|
|
147
|
+
newAppStateSyncKeyId = strKeyId;
|
|
148
|
+
}
|
|
149
|
+
logger?.info({ newAppStateSyncKeyId, newKeys }, "injecting new app state sync keys");
|
|
150
|
+
}, meId);
|
|
151
|
+
ev.emit("creds.update", { myAppStateKeyId: newAppStateSyncKeyId });
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
logger?.info({ protocolMsg }, "recv app state sync with 0 keys");
|
|
155
|
+
}
|
|
156
|
+
break;
|
|
157
|
+
case proto.Message.ProtocolMessage.Type.REVOKE:
|
|
158
|
+
ev.emit("messages.update", [
|
|
159
|
+
{
|
|
160
|
+
key: {
|
|
161
|
+
...message.key,
|
|
162
|
+
id: protocolMsg.key.id
|
|
163
|
+
},
|
|
164
|
+
update: { message: null, messageStubType: WAMessageStubType.REVOKE, key: message.key }
|
|
165
|
+
}
|
|
166
|
+
]);
|
|
167
|
+
break;
|
|
168
|
+
case proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
|
|
169
|
+
Object.assign(chat, {
|
|
170
|
+
ephemeralSettingTimestamp: toNumber(message.messageTimestamp),
|
|
171
|
+
ephemeralExpiration: protocolMsg.ephemeralExpiration || null
|
|
172
|
+
});
|
|
173
|
+
break;
|
|
174
|
+
case proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
|
|
175
|
+
const response = protocolMsg.peerDataOperationRequestResponseMessage;
|
|
176
|
+
if (response) {
|
|
177
|
+
await placeholderResendCache?.del(response.stanzaId);
|
|
178
|
+
const { peerDataOperationResult } = response;
|
|
179
|
+
for (const result of peerDataOperationResult) {
|
|
180
|
+
const { placeholderMessageResendResponse: retryResponse } = result;
|
|
181
|
+
if (retryResponse) {
|
|
182
|
+
const webMessageInfo = proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes);
|
|
183
|
+
setTimeout(() => {
|
|
184
|
+
ev.emit("messages.upsert", {
|
|
185
|
+
messages: [webMessageInfo],
|
|
186
|
+
type: "notify",
|
|
187
|
+
requestId: response.stanzaId
|
|
188
|
+
});
|
|
189
|
+
}, 500);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
break;
|
|
194
|
+
case proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
|
|
195
|
+
ev.emit("messages.update", [
|
|
196
|
+
{
|
|
197
|
+
key: { ...message.key, id: protocolMsg.key?.id },
|
|
198
|
+
update: {
|
|
199
|
+
message: {
|
|
200
|
+
editedMessage: {
|
|
201
|
+
message: protocolMsg.editedMessage
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
messageTimestamp: protocolMsg.timestampMs
|
|
205
|
+
? Math.floor(toNumber(protocolMsg.timestampMs) / 1000)
|
|
206
|
+
: message.messageTimestamp
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
]);
|
|
210
|
+
break;
|
|
211
|
+
case proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC:
|
|
212
|
+
const encodedPayload = protocolMsg.lidMigrationMappingSyncMessage?.encodedMappingPayload;
|
|
213
|
+
const { pnToLidMappings, chatDbMigrationTimestamp } = proto.LIDMigrationMappingSyncPayload.decode(encodedPayload);
|
|
214
|
+
logger?.debug({ pnToLidMappings, chatDbMigrationTimestamp }, "got lid mappings and chat db migration timestamp");
|
|
215
|
+
const pairs = [];
|
|
216
|
+
for (const { pn, latestLid, assignedLid } of pnToLidMappings) {
|
|
217
|
+
const lid = latestLid || assignedLid;
|
|
218
|
+
pairs.push({ lid: `${lid}@lid`, pn: `${pn}@s.whatsapp.net` });
|
|
219
|
+
}
|
|
220
|
+
await signalRepository.lidMapping.storeLIDPNMappings(pairs);
|
|
221
|
+
if (pairs.length) {
|
|
222
|
+
for (const { pn, lid } of pairs) {
|
|
223
|
+
await signalRepository.migrateSession(pn, lid);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
else if (content?.reactionMessage) {
|
|
229
|
+
const reaction = {
|
|
230
|
+
...content.reactionMessage,
|
|
231
|
+
key: message.key
|
|
232
|
+
};
|
|
233
|
+
ev.emit("messages.reaction", [
|
|
234
|
+
{
|
|
235
|
+
reaction,
|
|
236
|
+
key: content.reactionMessage?.key
|
|
237
|
+
}
|
|
238
|
+
]);
|
|
239
|
+
}
|
|
240
|
+
else if (message.messageStubType) {
|
|
241
|
+
const jid = message.key?.remoteJid;
|
|
242
|
+
let participants;
|
|
243
|
+
const emitParticipantsUpdate = (action) => ev.emit("group-participants.update", {
|
|
244
|
+
id: jid,
|
|
245
|
+
author: message.key.participant,
|
|
246
|
+
authorPn: message.key.participantAlt,
|
|
247
|
+
participants,
|
|
248
|
+
action
|
|
249
|
+
});
|
|
250
|
+
const emitGroupUpdate = (update) => {
|
|
251
|
+
ev.emit("groups.update", [
|
|
252
|
+
{ id: jid, ...update, author: message.key.participant ?? undefined, authorPn: message.key.participantAlt }
|
|
253
|
+
]);
|
|
254
|
+
};
|
|
255
|
+
const emitGroupRequestJoin = (participant, action, method) => {
|
|
256
|
+
ev.emit("group.join-request", {
|
|
257
|
+
id: jid,
|
|
258
|
+
author: message.key.participant,
|
|
259
|
+
authorPn: message.key.participantAlt,
|
|
260
|
+
participant: participant.lid,
|
|
261
|
+
participantPn: participant.pn,
|
|
262
|
+
action,
|
|
263
|
+
method: method
|
|
264
|
+
});
|
|
265
|
+
};
|
|
266
|
+
const participantsIncludesMe = () => participants.find(jid => areJidsSameUser(meId, jid.phoneNumber));
|
|
267
|
+
switch (message.messageStubType) {
|
|
268
|
+
case WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
|
|
269
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
270
|
+
emitParticipantsUpdate("modify");
|
|
271
|
+
break;
|
|
272
|
+
case WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
|
|
273
|
+
case WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
|
|
274
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
275
|
+
emitParticipantsUpdate("remove");
|
|
276
|
+
if (participantsIncludesMe()) {
|
|
277
|
+
chat.readOnly = true;
|
|
278
|
+
}
|
|
279
|
+
break;
|
|
280
|
+
case WAMessageStubType.GROUP_PARTICIPANT_ADD:
|
|
281
|
+
case WAMessageStubType.GROUP_PARTICIPANT_INVITE:
|
|
282
|
+
case WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
|
|
283
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
284
|
+
if (participantsIncludesMe()) {
|
|
285
|
+
chat.readOnly = false;
|
|
286
|
+
}
|
|
287
|
+
emitParticipantsUpdate("add");
|
|
288
|
+
break;
|
|
289
|
+
case WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
|
|
290
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
291
|
+
emitParticipantsUpdate("demote");
|
|
292
|
+
break;
|
|
293
|
+
case WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
|
|
294
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
295
|
+
emitParticipantsUpdate("promote");
|
|
296
|
+
break;
|
|
297
|
+
case WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
|
|
298
|
+
const announceValue = message.messageStubParameters?.[0];
|
|
299
|
+
emitGroupUpdate({ announce: announceValue === "true" || announceValue === "on" });
|
|
300
|
+
break;
|
|
301
|
+
case WAMessageStubType.GROUP_CHANGE_RESTRICT:
|
|
302
|
+
const restrictValue = message.messageStubParameters?.[0];
|
|
303
|
+
emitGroupUpdate({ restrict: restrictValue === "true" || restrictValue === "on" });
|
|
304
|
+
break;
|
|
305
|
+
case WAMessageStubType.GROUP_CHANGE_SUBJECT:
|
|
306
|
+
const name = message.messageStubParameters?.[0];
|
|
307
|
+
chat.name = name;
|
|
308
|
+
emitGroupUpdate({ subject: name });
|
|
309
|
+
break;
|
|
310
|
+
case WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
|
|
311
|
+
const description = message.messageStubParameters?.[0];
|
|
312
|
+
chat.description = description;
|
|
313
|
+
emitGroupUpdate({ desc: description });
|
|
314
|
+
break;
|
|
315
|
+
case WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
|
|
316
|
+
const code = message.messageStubParameters?.[0];
|
|
317
|
+
emitGroupUpdate({ inviteCode: code });
|
|
318
|
+
break;
|
|
319
|
+
case WAMessageStubType.GROUP_MEMBER_ADD_MODE:
|
|
320
|
+
const memberAddValue = message.messageStubParameters?.[0];
|
|
321
|
+
emitGroupUpdate({ memberAddMode: memberAddValue === "all_member_add" });
|
|
322
|
+
break;
|
|
323
|
+
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
|
|
324
|
+
const approvalMode = message.messageStubParameters?.[0];
|
|
325
|
+
emitGroupUpdate({ joinApprovalMode: approvalMode === "on" });
|
|
326
|
+
break;
|
|
327
|
+
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
|
|
328
|
+
const participant = JSON.parse(message.messageStubParameters?.[0]);
|
|
329
|
+
const action = message.messageStubParameters?.[1];
|
|
330
|
+
const method = message.messageStubParameters?.[2];
|
|
331
|
+
emitGroupRequestJoin(participant, action, method);
|
|
332
|
+
break;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (Object.keys(chat).length > 1) {
|
|
336
|
+
ev.emit("chats.update", [chat]);
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
//=======================================================//
|
|
340
|
+
export default processMessage;
|
|
341
|
+
//=======================================================//
|