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,130 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from "crypto";
|
|
3
|
+
import { KEY_BUNDLE_TYPE } from "../Defaults/index.js";
|
|
4
|
+
import * as curve from "libsignal-xeuka/src/curve.js";
|
|
5
|
+
//=======================================================//
|
|
6
|
+
const { subtle } = globalThis.crypto;
|
|
7
|
+
export const generateSignalPubKey = (pubKey) => pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey]);
|
|
8
|
+
export const Curve = {
|
|
9
|
+
generateKeyPair: () => {
|
|
10
|
+
const { pubKey, privKey } = curve.generateKeyPair();
|
|
11
|
+
return {
|
|
12
|
+
private: Buffer.from(privKey),
|
|
13
|
+
public: Buffer.from(pubKey.slice(1))
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
sharedKey: (privateKey, publicKey) => {
|
|
17
|
+
const shared = curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey);
|
|
18
|
+
return Buffer.from(shared);
|
|
19
|
+
},
|
|
20
|
+
sign: (privateKey, buf) => curve.calculateSignature(privateKey, buf),
|
|
21
|
+
verify: (pubKey, message, signature) => {
|
|
22
|
+
try {
|
|
23
|
+
curve.verifySignature(generateSignalPubKey(pubKey), message, signature);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
//=======================================================//
|
|
32
|
+
export const signedKeyPair = (identityKeyPair, keyId) => {
|
|
33
|
+
const preKey = Curve.generateKeyPair();
|
|
34
|
+
const pubKey = generateSignalPubKey(preKey.public);
|
|
35
|
+
const signature = Curve.sign(identityKeyPair.private, pubKey);
|
|
36
|
+
return { keyPair: preKey, signature, keyId };
|
|
37
|
+
};
|
|
38
|
+
//=======================================================//
|
|
39
|
+
const GCM_TAG_LENGTH = 128 >> 3;
|
|
40
|
+
export function aesEncryptGCM(plaintext, key, iv, additionalData) {
|
|
41
|
+
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
42
|
+
cipher.setAAD(additionalData);
|
|
43
|
+
return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
|
|
44
|
+
}
|
|
45
|
+
//=======================================================//
|
|
46
|
+
export function aesDecryptGCM(ciphertext, key, iv, additionalData) {
|
|
47
|
+
const decipher = createDecipheriv("aes-256-gcm", key, iv);
|
|
48
|
+
const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
|
|
49
|
+
const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
|
|
50
|
+
decipher.setAAD(additionalData);
|
|
51
|
+
decipher.setAuthTag(tag);
|
|
52
|
+
return Buffer.concat([decipher.update(enc), decipher.final()]);
|
|
53
|
+
}
|
|
54
|
+
//=======================================================//
|
|
55
|
+
export function aesEncryptCTR(plaintext, key, iv) {
|
|
56
|
+
const cipher = createCipheriv("aes-256-ctr", key, iv);
|
|
57
|
+
return Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
58
|
+
}
|
|
59
|
+
//=======================================================//
|
|
60
|
+
export function aesDecryptCTR(ciphertext, key, iv) {
|
|
61
|
+
const decipher = createDecipheriv("aes-256-ctr", key, iv);
|
|
62
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
63
|
+
}
|
|
64
|
+
//=======================================================//
|
|
65
|
+
export function aesDecrypt(buffer, key) {
|
|
66
|
+
return aesDecryptWithIV(buffer.slice(16, buffer.length), key, buffer.slice(0, 16));
|
|
67
|
+
}
|
|
68
|
+
//=======================================================//
|
|
69
|
+
export function aesDecryptWithIV(buffer, key, IV) {
|
|
70
|
+
const aes = createDecipheriv("aes-256-cbc", key, IV);
|
|
71
|
+
return Buffer.concat([aes.update(buffer), aes.final()]);
|
|
72
|
+
}
|
|
73
|
+
//=======================================================//
|
|
74
|
+
export function aesEncrypt(buffer, key) {
|
|
75
|
+
const IV = randomBytes(16);
|
|
76
|
+
const aes = createCipheriv("aes-256-cbc", key, IV);
|
|
77
|
+
return Buffer.concat([IV, aes.update(buffer), aes.final()]);
|
|
78
|
+
}
|
|
79
|
+
//=======================================================//
|
|
80
|
+
export function aesEncrypWithIV(buffer, key, IV) {
|
|
81
|
+
const aes = createCipheriv("aes-256-cbc", key, IV);
|
|
82
|
+
return Buffer.concat([aes.update(buffer), aes.final()]);
|
|
83
|
+
}
|
|
84
|
+
//=======================================================//
|
|
85
|
+
export function hmacSign(buffer, key, variant = "sha256") {
|
|
86
|
+
return createHmac(variant, key).update(buffer).digest();
|
|
87
|
+
}
|
|
88
|
+
//=======================================================//
|
|
89
|
+
export function sha256(buffer) {
|
|
90
|
+
return createHash("sha256").update(buffer).digest();
|
|
91
|
+
}
|
|
92
|
+
//=======================================================//
|
|
93
|
+
export function md5(buffer) {
|
|
94
|
+
return createHash("md5").update(buffer).digest();
|
|
95
|
+
}
|
|
96
|
+
//=======================================================//
|
|
97
|
+
export async function hkdf(buffer, expandedLength, info) {
|
|
98
|
+
const inputKeyMaterial = new Uint8Array(buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer));
|
|
99
|
+
const salt = info.salt ? new Uint8Array(info.salt) : new Uint8Array(0);
|
|
100
|
+
const infoBytes = info.info ? new TextEncoder().encode(info.info) : new Uint8Array(0);
|
|
101
|
+
const importedKey = await subtle.importKey("raw", inputKeyMaterial, { name: "HKDF" }, false, [
|
|
102
|
+
"deriveBits"
|
|
103
|
+
]);
|
|
104
|
+
const derivedBits = await subtle.deriveBits({
|
|
105
|
+
name: "HKDF",
|
|
106
|
+
hash: "SHA-256",
|
|
107
|
+
salt: salt,
|
|
108
|
+
info: infoBytes
|
|
109
|
+
}, importedKey, expandedLength * 8
|
|
110
|
+
);
|
|
111
|
+
return Buffer.from(derivedBits);
|
|
112
|
+
}
|
|
113
|
+
//=======================================================//
|
|
114
|
+
export async function derivePairingCodeKey(pairingCode, salt) {
|
|
115
|
+
const encoder = new TextEncoder();
|
|
116
|
+
const pairingCodeBuffer = encoder.encode(pairingCode);
|
|
117
|
+
const saltBuffer = new Uint8Array(salt instanceof Uint8Array ? salt : new Uint8Array(salt));
|
|
118
|
+
const keyMaterial = await subtle.importKey("raw", pairingCodeBuffer, { name: "PBKDF2" }, false, [
|
|
119
|
+
"deriveBits"
|
|
120
|
+
]);
|
|
121
|
+
const derivedBits = await subtle.deriveBits({
|
|
122
|
+
name: "PBKDF2",
|
|
123
|
+
salt: saltBuffer,
|
|
124
|
+
iterations: 2 << 16,
|
|
125
|
+
hash: "SHA-256"
|
|
126
|
+
}, keyMaterial, 32 * 8
|
|
127
|
+
);
|
|
128
|
+
return Buffer.from(derivedBits);
|
|
129
|
+
}
|
|
130
|
+
//=======================================================//
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { areJidsSameUser, isHostedLidUser, isHostedPnUser, isJidBroadcast, isJidGroup, isJidMetaAI, isJidNewsletter, isJidStatusBroadcast, isLidUser, isPnUser } from "../WABinary/index.js";
|
|
3
|
+
import { unpadRandomMax16 } from "./generics.js";
|
|
4
|
+
import { proto } from "../../WAProto/index.js";
|
|
5
|
+
import { Boom } from "@hapi/boom";
|
|
6
|
+
//=======================================================//
|
|
7
|
+
export const getDecryptionJid = async (sender, repository) => {
|
|
8
|
+
if (isLidUser(sender) || isHostedLidUser(sender)) {
|
|
9
|
+
return sender;
|
|
10
|
+
}
|
|
11
|
+
const mapped = await repository.lidMapping.getLIDForPN(sender);
|
|
12
|
+
return mapped || sender;
|
|
13
|
+
};
|
|
14
|
+
//=======================================================//
|
|
15
|
+
const storeMappingFromEnvelope = async (stanza, sender, repository, decryptionJid, logger) => {
|
|
16
|
+
const { senderAlt } = extractAddressingContext(stanza);
|
|
17
|
+
if (senderAlt && isLidUser(senderAlt) && isPnUser(sender) && decryptionJid === sender) {
|
|
18
|
+
try {
|
|
19
|
+
await repository.lidMapping.storeLIDPNMappings([{ lid: senderAlt, pn: sender }]);
|
|
20
|
+
await repository.migrateSession(sender, senderAlt);
|
|
21
|
+
logger.debug({ sender, senderAlt }, "Stored LID mapping from envelope");
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
logger.warn({ sender, senderAlt, error }, "Failed to store LID mapping");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
//=======================================================//
|
|
29
|
+
export const NO_MESSAGE_FOUND_ERROR_TEXT = "Message absent from node";
|
|
30
|
+
export const MISSING_KEYS_ERROR_TEXT = "Key used already or never filled";
|
|
31
|
+
//=======================================================//
|
|
32
|
+
export const DECRYPTION_RETRY_CONFIG = {
|
|
33
|
+
maxRetries: 3,
|
|
34
|
+
baseDelayMs: 100,
|
|
35
|
+
sessionRecordErrors: ["No session record", "SessionError: No session record"]
|
|
36
|
+
};
|
|
37
|
+
//=======================================================//
|
|
38
|
+
export const NACK_REASONS = {
|
|
39
|
+
ParsingError: 487,
|
|
40
|
+
UnrecognizedStanza: 488,
|
|
41
|
+
UnrecognizedStanzaClass: 489,
|
|
42
|
+
UnrecognizedStanzaType: 490,
|
|
43
|
+
InvalidProtobuf: 491,
|
|
44
|
+
InvalidHostedCompanionStanza: 493,
|
|
45
|
+
MissingMessageSecret: 495,
|
|
46
|
+
SignalErrorOldCounter: 496,
|
|
47
|
+
MessageDeletedOnPeer: 499,
|
|
48
|
+
UnhandledError: 500,
|
|
49
|
+
UnsupportedAdminRevoke: 550,
|
|
50
|
+
UnsupportedLIDGroup: 551,
|
|
51
|
+
DBOperationFailed: 552
|
|
52
|
+
};
|
|
53
|
+
//=======================================================//
|
|
54
|
+
export const extractAddressingContext = (stanza) => {
|
|
55
|
+
let senderAlt;
|
|
56
|
+
let recipientAlt;
|
|
57
|
+
const sender = stanza.attrs.participant || stanza.attrs.from;
|
|
58
|
+
const addressingMode = stanza.attrs.addressing_mode || (sender?.endsWith("lid") ? "lid" : "pn");
|
|
59
|
+
if (addressingMode === "lid") {
|
|
60
|
+
senderAlt = stanza.attrs.participant_pn || stanza.attrs.sender_pn || stanza.attrs.peer_recipient_pn;
|
|
61
|
+
recipientAlt = stanza.attrs.recipient_pn;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
senderAlt = stanza.attrs.participant_lid || stanza.attrs.sender_lid || stanza.attrs.peer_recipient_lid;
|
|
65
|
+
recipientAlt = stanza.attrs.recipient_lid;
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
addressingMode,
|
|
69
|
+
senderAlt,
|
|
70
|
+
recipientAlt
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
//=======================================================//
|
|
74
|
+
export function decodeMessageNode(stanza, meId, meLid) {
|
|
75
|
+
let msgType;
|
|
76
|
+
let chatId;
|
|
77
|
+
let author;
|
|
78
|
+
let fromMe = false;
|
|
79
|
+
const msgId = stanza.attrs.id;
|
|
80
|
+
const from = stanza.attrs.from;
|
|
81
|
+
const participant = stanza.attrs.participant;
|
|
82
|
+
const recipient = stanza.attrs.recipient;
|
|
83
|
+
const addressingContext = extractAddressingContext(stanza);
|
|
84
|
+
const isMe = (jid) => areJidsSameUser(jid, meId);
|
|
85
|
+
const isMeLid = (jid) => areJidsSameUser(jid, meLid);
|
|
86
|
+
if (isPnUser(from) || isLidUser(from) || isHostedLidUser(from) || isHostedPnUser(from)) {
|
|
87
|
+
if (recipient && !isJidMetaAI(recipient)) {
|
|
88
|
+
if (!isMe(from) && !isMeLid(from)) {
|
|
89
|
+
throw new Boom("receipient present, but msg not from me", { data: stanza });
|
|
90
|
+
}
|
|
91
|
+
if (isMe(from) || isMeLid(from)) {
|
|
92
|
+
fromMe = true;
|
|
93
|
+
}
|
|
94
|
+
chatId = recipient;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
chatId = from;
|
|
98
|
+
}
|
|
99
|
+
msgType = "chat";
|
|
100
|
+
author = from;
|
|
101
|
+
}
|
|
102
|
+
else if (isJidGroup(from)) {
|
|
103
|
+
if (!participant) {
|
|
104
|
+
throw new Boom("No participant in group message");
|
|
105
|
+
}
|
|
106
|
+
if (isMe(participant) || isMeLid(participant)) {
|
|
107
|
+
fromMe = true;
|
|
108
|
+
}
|
|
109
|
+
msgType = "group";
|
|
110
|
+
author = participant;
|
|
111
|
+
chatId = from;
|
|
112
|
+
}
|
|
113
|
+
else if (isJidBroadcast(from)) {
|
|
114
|
+
if (!participant) {
|
|
115
|
+
throw new Boom("No participant in group message");
|
|
116
|
+
}
|
|
117
|
+
const isParticipantMe = isMe(participant);
|
|
118
|
+
if (isJidStatusBroadcast(from)) {
|
|
119
|
+
msgType = isParticipantMe ? "direct_peer_status" : "other_status";
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
msgType = isParticipantMe ? "peer_broadcast" : "other_broadcast";
|
|
123
|
+
}
|
|
124
|
+
fromMe = isParticipantMe;
|
|
125
|
+
chatId = from;
|
|
126
|
+
author = participant;
|
|
127
|
+
}
|
|
128
|
+
else if (isJidNewsletter(from)) {
|
|
129
|
+
msgType = "newsletter";
|
|
130
|
+
chatId = from;
|
|
131
|
+
author = from;
|
|
132
|
+
if (isMe(from) || isMeLid(from)) {
|
|
133
|
+
fromMe = true;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
throw new Boom("Unknown message type", { data: stanza });
|
|
138
|
+
}
|
|
139
|
+
const pushname = stanza?.attrs?.notify;
|
|
140
|
+
const key = {
|
|
141
|
+
remoteJid: chatId,
|
|
142
|
+
remoteJidAlt: !isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
|
|
143
|
+
fromMe,
|
|
144
|
+
id: msgId,
|
|
145
|
+
participant,
|
|
146
|
+
participantAlt: isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
|
|
147
|
+
addressingMode: addressingContext.addressingMode,
|
|
148
|
+
...(msgType === "newsletter" && stanza.attrs.server_id ? { server_id: stanza.attrs.server_id } : {})
|
|
149
|
+
};
|
|
150
|
+
const fullMessage = {
|
|
151
|
+
key,
|
|
152
|
+
messageTimestamp: +stanza.attrs.t,
|
|
153
|
+
pushName: pushname,
|
|
154
|
+
broadcast: isJidBroadcast(from)
|
|
155
|
+
};
|
|
156
|
+
if (key.fromMe) {
|
|
157
|
+
fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK;
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
fullMessage,
|
|
161
|
+
author,
|
|
162
|
+
sender: msgType === "chat" ? author : chatId
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
//=======================================================//
|
|
166
|
+
export const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
167
|
+
const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid);
|
|
168
|
+
return {
|
|
169
|
+
fullMessage,
|
|
170
|
+
category: stanza.attrs.category,
|
|
171
|
+
author,
|
|
172
|
+
async decrypt() {
|
|
173
|
+
let decryptables = 0;
|
|
174
|
+
if (Array.isArray(stanza.content)) {
|
|
175
|
+
for (const { tag, attrs, content } of stanza.content) {
|
|
176
|
+
if (tag === "verified_name" && content instanceof Uint8Array) {
|
|
177
|
+
const cert = proto.VerifiedNameCertificate.decode(content);
|
|
178
|
+
const details = proto.VerifiedNameCertificate.Details.decode(cert.details);
|
|
179
|
+
fullMessage.verifiedBizName = details.verifiedName;
|
|
180
|
+
}
|
|
181
|
+
if (tag === "unavailable" && attrs.type === "view_once") {
|
|
182
|
+
fullMessage.key.isViewOnce = true;
|
|
183
|
+
}
|
|
184
|
+
if (tag !== "enc" && tag !== "plaintext") {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (!(content instanceof Uint8Array)) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
decryptables += 1;
|
|
191
|
+
let msgBuffer;
|
|
192
|
+
const decryptionJid = await getDecryptionJid(author, repository);
|
|
193
|
+
if (tag !== "plaintext") {
|
|
194
|
+
await storeMappingFromEnvelope(stanza, author, repository, decryptionJid, logger);
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
const e2eType = tag === "plaintext" ? "plaintext" : attrs.type;
|
|
198
|
+
switch (e2eType) {
|
|
199
|
+
case "skmsg":
|
|
200
|
+
msgBuffer = await repository.decryptGroupMessage({
|
|
201
|
+
group: sender,
|
|
202
|
+
authorJid: author,
|
|
203
|
+
msg: content
|
|
204
|
+
});
|
|
205
|
+
break;
|
|
206
|
+
case "pkmsg":
|
|
207
|
+
case "msg":
|
|
208
|
+
msgBuffer = await repository.decryptMessage({
|
|
209
|
+
jid: decryptionJid,
|
|
210
|
+
type: e2eType,
|
|
211
|
+
ciphertext: content
|
|
212
|
+
});
|
|
213
|
+
break;
|
|
214
|
+
case "plaintext":
|
|
215
|
+
msgBuffer = content;
|
|
216
|
+
break;
|
|
217
|
+
default:
|
|
218
|
+
throw new Error(`Unknown e2e type: ${e2eType}`);
|
|
219
|
+
}
|
|
220
|
+
let msg = proto.Message.decode(e2eType !== "plaintext" ? unpadRandomMax16(msgBuffer) : msgBuffer);
|
|
221
|
+
msg = msg.deviceSentMessage?.message || msg;
|
|
222
|
+
if (msg.senderKeyDistributionMessage) {
|
|
223
|
+
try {
|
|
224
|
+
await repository.processSenderKeyDistributionMessage({
|
|
225
|
+
authorJid: author,
|
|
226
|
+
item: msg.senderKeyDistributionMessage
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
logger.error({ key: fullMessage.key, err }, "failed to process sender key distribution message");
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (fullMessage.message) {
|
|
234
|
+
Object.assign(fullMessage.message, msg);
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
fullMessage.message = msg;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
catch (err) {
|
|
241
|
+
const errorContext = {
|
|
242
|
+
key: fullMessage.key,
|
|
243
|
+
err,
|
|
244
|
+
messageType: tag === "plaintext" ? "plaintext" : attrs.type,
|
|
245
|
+
sender,
|
|
246
|
+
author,
|
|
247
|
+
isSessionRecordError: isSessionRecordError(err)
|
|
248
|
+
};
|
|
249
|
+
logger.error(errorContext, "failed to decrypt message");
|
|
250
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
251
|
+
fullMessage.messageStubParameters = [err.message.toString()];
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (!decryptables) {
|
|
256
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
257
|
+
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT];
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
//=======================================================//
|
|
263
|
+
function isSessionRecordError(error) {
|
|
264
|
+
const errorMessage = error?.message || error?.toString() || "";
|
|
265
|
+
return DECRYPTION_RETRY_CONFIG.sessionRecordErrors.some(errorPattern => errorMessage.includes(errorPattern));
|
|
266
|
+
}
|
|
267
|
+
//=======================================================//
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { areJidsSameUser, isHostedLidUser, isHostedPnUser, isJidBroadcast, isJidGroup, isJidMetaAI, isJidNewsletter, isJidStatusBroadcast, isLidUser, isPnUser } from "../WABinary/index.js";
|
|
3
|
+
import { unpadRandomMax16 } from "./generics.js";
|
|
4
|
+
import { proto } from "../../WAProto/index.js";
|
|
5
|
+
import { Boom } from "@hapi/boom";
|
|
6
|
+
//=======================================================//
|
|
7
|
+
export const getDecryptionJid = async (sender, repository) => {
|
|
8
|
+
if (isLidUser(sender) || isHostedLidUser(sender)) {
|
|
9
|
+
return sender;
|
|
10
|
+
}
|
|
11
|
+
const mapped = await repository.lidMapping.getLIDForPN(sender);
|
|
12
|
+
return mapped || sender;
|
|
13
|
+
};
|
|
14
|
+
//=======================================================//
|
|
15
|
+
const storeMappingFromEnvelope = async (stanza, sender, repository, decryptionJid, logger) => {
|
|
16
|
+
const { senderAlt } = extractAddressingContext(stanza);
|
|
17
|
+
if (senderAlt && isLidUser(senderAlt) && isPnUser(sender) && decryptionJid === sender) {
|
|
18
|
+
try {
|
|
19
|
+
await repository.lidMapping.storeLIDPNMappings([{ lid: senderAlt, pn: sender }]);
|
|
20
|
+
await repository.migrateSession(sender, senderAlt);
|
|
21
|
+
logger.debug({ sender, senderAlt }, "Stored LID mapping from envelope");
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
logger.warn({ sender, senderAlt, error }, "Failed to store LID mapping");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
//=======================================================//
|
|
29
|
+
export const NO_MESSAGE_FOUND_ERROR_TEXT = "Message absent from node";
|
|
30
|
+
export const MISSING_KEYS_ERROR_TEXT = "Key used already or never filled";
|
|
31
|
+
//=======================================================//
|
|
32
|
+
export const DECRYPTION_RETRY_CONFIG = {
|
|
33
|
+
maxRetries: 3,
|
|
34
|
+
baseDelayMs: 100,
|
|
35
|
+
sessionRecordErrors: ["No session record", "SessionError: No session record"]
|
|
36
|
+
};
|
|
37
|
+
//=======================================================//
|
|
38
|
+
export const NACK_REASONS = {
|
|
39
|
+
ParsingError: 487,
|
|
40
|
+
UnrecognizedStanza: 488,
|
|
41
|
+
UnrecognizedStanzaClass: 489,
|
|
42
|
+
UnrecognizedStanzaType: 490,
|
|
43
|
+
InvalidProtobuf: 491,
|
|
44
|
+
InvalidHostedCompanionStanza: 493,
|
|
45
|
+
MissingMessageSecret: 495,
|
|
46
|
+
SignalErrorOldCounter: 496,
|
|
47
|
+
MessageDeletedOnPeer: 499,
|
|
48
|
+
UnhandledError: 500,
|
|
49
|
+
UnsupportedAdminRevoke: 550,
|
|
50
|
+
UnsupportedLIDGroup: 551,
|
|
51
|
+
DBOperationFailed: 552
|
|
52
|
+
};
|
|
53
|
+
//=======================================================//
|
|
54
|
+
export const extractAddressingContext = (stanza) => {
|
|
55
|
+
let senderAlt;
|
|
56
|
+
let recipientAlt;
|
|
57
|
+
const sender = stanza.attrs.participant || stanza.attrs.from;
|
|
58
|
+
const addressingMode = stanza.attrs.addressing_mode || (sender?.endsWith("lid") ? "lid" : "pn");
|
|
59
|
+
if (addressingMode === "lid") {
|
|
60
|
+
senderAlt = stanza.attrs.participant_pn || stanza.attrs.sender_pn || stanza.attrs.peer_recipient_pn;
|
|
61
|
+
recipientAlt = stanza.attrs.recipient_pn;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
senderAlt = stanza.attrs.participant_lid || stanza.attrs.sender_lid || stanza.attrs.peer_recipient_lid;
|
|
65
|
+
recipientAlt = stanza.attrs.recipient_lid;
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
addressingMode,
|
|
69
|
+
senderAlt,
|
|
70
|
+
recipientAlt
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
//=======================================================//
|
|
74
|
+
export function decodeMessageNode(stanza, meId, meLid) {
|
|
75
|
+
let msgType;
|
|
76
|
+
let chatId;
|
|
77
|
+
let author;
|
|
78
|
+
let fromMe = false;
|
|
79
|
+
const msgId = stanza.attrs.id;
|
|
80
|
+
const from = stanza.attrs.from_pn;
|
|
81
|
+
const participant = stanza.attrs.participant_pn;
|
|
82
|
+
const recipient = stanza.attrs.recipient;
|
|
83
|
+
const addressingContext = extractAddressingContext(stanza);
|
|
84
|
+
const isMe = (jid) => areJidsSameUser(jid, meId);
|
|
85
|
+
const isMeLid = (jid) => areJidsSameUser(jid, meLid);
|
|
86
|
+
if (isPnUser(from) || isLidUser(from) || isHostedLidUser(from) || isHostedPnUser(from)) {
|
|
87
|
+
if (recipient && !isJidMetaAI(recipient)) {
|
|
88
|
+
if (!isMe(from) && !isMeLid(from)) {
|
|
89
|
+
throw new Boom("receipient present, but msg not from me", { data: stanza });
|
|
90
|
+
}
|
|
91
|
+
if (isMe(from) || isMeLid(from)) {
|
|
92
|
+
fromMe = true;
|
|
93
|
+
}
|
|
94
|
+
chatId = recipient;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
chatId = from;
|
|
98
|
+
}
|
|
99
|
+
msgType = "chat";
|
|
100
|
+
author = from;
|
|
101
|
+
}
|
|
102
|
+
else if (isJidGroup(from)) {
|
|
103
|
+
if (!participant) {
|
|
104
|
+
throw new Boom("No participant in group message");
|
|
105
|
+
}
|
|
106
|
+
if (isMe(participant) || isMeLid(participant)) {
|
|
107
|
+
fromMe = true;
|
|
108
|
+
}
|
|
109
|
+
msgType = "group";
|
|
110
|
+
author = participant;
|
|
111
|
+
chatId = from;
|
|
112
|
+
}
|
|
113
|
+
else if (isJidBroadcast(from)) {
|
|
114
|
+
if (!participant) {
|
|
115
|
+
throw new Boom("No participant in group message");
|
|
116
|
+
}
|
|
117
|
+
const isParticipantMe = isMe(participant);
|
|
118
|
+
if (isJidStatusBroadcast(from)) {
|
|
119
|
+
msgType = isParticipantMe ? "direct_peer_status" : "other_status";
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
msgType = isParticipantMe ? "peer_broadcast" : "other_broadcast";
|
|
123
|
+
}
|
|
124
|
+
fromMe = isParticipantMe;
|
|
125
|
+
chatId = from;
|
|
126
|
+
author = participant;
|
|
127
|
+
}
|
|
128
|
+
else if (isJidNewsletter(from)) {
|
|
129
|
+
msgType = "newsletter";
|
|
130
|
+
chatId = from;
|
|
131
|
+
author = from;
|
|
132
|
+
if (isMe(from) || isMeLid(from)) {
|
|
133
|
+
fromMe = true;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
throw new Boom("Unknown message type", { data: stanza });
|
|
138
|
+
}
|
|
139
|
+
const pushname = stanza?.attrs?.notify;
|
|
140
|
+
const key = {
|
|
141
|
+
remoteJid: chatId,
|
|
142
|
+
remoteJidAlt: !isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
|
|
143
|
+
fromMe,
|
|
144
|
+
id: msgId,
|
|
145
|
+
participant,
|
|
146
|
+
participantAlt: isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
|
|
147
|
+
addressingMode: addressingContext.addressingMode,
|
|
148
|
+
...(msgType === "newsletter" && stanza.attrs.server_id ? { server_id: stanza.attrs.server_id } : {})
|
|
149
|
+
};
|
|
150
|
+
const fullMessage = {
|
|
151
|
+
key,
|
|
152
|
+
messageTimestamp: +stanza.attrs.t,
|
|
153
|
+
pushName: pushname,
|
|
154
|
+
broadcast: isJidBroadcast(from)
|
|
155
|
+
};
|
|
156
|
+
if (key.fromMe) {
|
|
157
|
+
fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK;
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
fullMessage,
|
|
161
|
+
author,
|
|
162
|
+
sender: msgType === "chat" ? author : chatId
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
//=======================================================//
|
|
166
|
+
export const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
167
|
+
const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid);
|
|
168
|
+
return {
|
|
169
|
+
fullMessage,
|
|
170
|
+
category: stanza.attrs.category,
|
|
171
|
+
author,
|
|
172
|
+
async decrypt() {
|
|
173
|
+
let decryptables = 0;
|
|
174
|
+
if (Array.isArray(stanza.content)) {
|
|
175
|
+
for (const { tag, attrs, content } of stanza.content) {
|
|
176
|
+
if (tag === "verified_name" && content instanceof Uint8Array) {
|
|
177
|
+
const cert = proto.VerifiedNameCertificate.decode(content);
|
|
178
|
+
const details = proto.VerifiedNameCertificate.Details.decode(cert.details);
|
|
179
|
+
fullMessage.verifiedBizName = details.verifiedName;
|
|
180
|
+
}
|
|
181
|
+
if (tag === "unavailable" && attrs.type === "view_once") {
|
|
182
|
+
fullMessage.key.isViewOnce = true;
|
|
183
|
+
}
|
|
184
|
+
if (tag !== "enc" && tag !== "plaintext") {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (!(content instanceof Uint8Array)) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
decryptables += 1;
|
|
191
|
+
let msgBuffer;
|
|
192
|
+
const decryptionJid = await getDecryptionJid(author, repository);
|
|
193
|
+
if (tag !== "plaintext") {
|
|
194
|
+
await storeMappingFromEnvelope(stanza, author, repository, decryptionJid, logger);
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
const e2eType = tag === "plaintext" ? "plaintext" : attrs.type;
|
|
198
|
+
switch (e2eType) {
|
|
199
|
+
case "skmsg":
|
|
200
|
+
msgBuffer = await repository.decryptGroupMessage({
|
|
201
|
+
group: sender,
|
|
202
|
+
authorJid: author,
|
|
203
|
+
msg: content
|
|
204
|
+
});
|
|
205
|
+
break;
|
|
206
|
+
case "pkmsg":
|
|
207
|
+
case "msg":
|
|
208
|
+
msgBuffer = await repository.decryptMessage({
|
|
209
|
+
jid: decryptionJid,
|
|
210
|
+
type: e2eType,
|
|
211
|
+
ciphertext: content
|
|
212
|
+
});
|
|
213
|
+
break;
|
|
214
|
+
case "plaintext":
|
|
215
|
+
msgBuffer = content;
|
|
216
|
+
break;
|
|
217
|
+
default:
|
|
218
|
+
throw new Error(`Unknown e2e type: ${e2eType}`);
|
|
219
|
+
}
|
|
220
|
+
let msg = proto.Message.decode(e2eType !== "plaintext" ? unpadRandomMax16(msgBuffer) : msgBuffer);
|
|
221
|
+
msg = msg.deviceSentMessage?.message || msg;
|
|
222
|
+
if (msg.senderKeyDistributionMessage) {
|
|
223
|
+
try {
|
|
224
|
+
await repository.processSenderKeyDistributionMessage({
|
|
225
|
+
authorJid: author,
|
|
226
|
+
item: msg.senderKeyDistributionMessage
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
logger.error({ key: fullMessage.key, err }, "failed to process sender key distribution message");
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (fullMessage.message) {
|
|
234
|
+
Object.assign(fullMessage.message, msg);
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
fullMessage.message = msg;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
catch (err) {
|
|
241
|
+
const errorContext = {
|
|
242
|
+
key: fullMessage.key,
|
|
243
|
+
err,
|
|
244
|
+
messageType: tag === "plaintext" ? "plaintext" : attrs.type,
|
|
245
|
+
sender,
|
|
246
|
+
author,
|
|
247
|
+
isSessionRecordError: isSessionRecordError(err)
|
|
248
|
+
};
|
|
249
|
+
logger.error(errorContext, "failed to decrypt message");
|
|
250
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
251
|
+
fullMessage.messageStubParameters = [err.message.toString()];
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (!decryptables) {
|
|
256
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
257
|
+
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT];
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
//=======================================================//
|
|
263
|
+
function isSessionRecordError(error) {
|
|
264
|
+
const errorMessage = error?.message || error?.toString() || "";
|
|
265
|
+
return DECRYPTION_RETRY_CONFIG.sessionRecordErrors.some(errorPattern => errorMessage.includes(errorPattern));
|
|
266
|
+
}
|
|
267
|
+
//=======================================================//
|