@sgintokic/baileys 0.0.2
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.
Potentially problematic release.
This version of @sgintokic/baileys might be problematic. Click here for more details.
- package/LICENSE +22 -0
- package/README.md +1097 -0
- package/WAProto/index.js +2 -0
- package/engine-requirements.js +1 -0
- package/lib/Defaults/index.js +155 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.js +41 -0
- package/lib/Signal/Group/group_cipher.js +108 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.js +14 -0
- package/lib/Signal/Group/sender-chain-key.js +31 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +79 -0
- package/lib/Signal/Group/sender-key-name.js +49 -0
- package/lib/Signal/Group/sender-key-record.js +46 -0
- package/lib/Signal/Group/sender-key-state.js +104 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/libsignal.js +485 -0
- package/lib/Signal/lid-mapping.js +291 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +64 -0
- package/lib/Socket/business.js +293 -0
- package/lib/Socket/chats.js +1068 -0
- package/lib/Socket/communities.js +476 -0
- package/lib/Socket/groups.js +383 -0
- package/lib/Socket/index.js +8 -0
- package/lib/Socket/messages-recv.js +1830 -0
- package/lib/Socket/messages-send.js +1462 -0
- package/lib/Socket/mex.js +55 -0
- package/lib/Socket/newsletter.js +277 -0
- package/lib/Socket/socket.js +1087 -0
- package/lib/Store/index.js +3 -0
- package/lib/Store/make-in-memory-store.js +517 -0
- package/lib/Store/make-ordered-dictionary.js +75 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.js +18 -0
- package/lib/Types/Newsletter.js +33 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +15 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.js +31 -0
- package/lib/Utils/auth-utils.js +293 -0
- package/lib/Utils/browser-utils.js +32 -0
- package/lib/Utils/business.js +245 -0
- package/lib/Utils/chat-utils.js +959 -0
- package/lib/Utils/crypto.js +133 -0
- package/lib/Utils/decode-wa-message.js +376 -0
- package/lib/Utils/event-buffer.js +620 -0
- package/lib/Utils/generics.js +417 -0
- package/lib/Utils/history.js +150 -0
- package/lib/Utils/identity-change-handler.js +63 -0
- package/lib/Utils/index.js +21 -0
- package/lib/Utils/link-preview.js +91 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.js +6 -0
- package/lib/Utils/make-mutex.js +31 -0
- package/lib/Utils/message-retry-manager.js +240 -0
- package/lib/Utils/messages-media.js +901 -0
- package/lib/Utils/messages.js +2052 -0
- package/lib/Utils/noise-handler.js +229 -0
- package/lib/Utils/offline-node-processor.js +50 -0
- package/lib/Utils/pre-key-manager.js +119 -0
- package/lib/Utils/process-message.js +641 -0
- package/lib/Utils/reporting-utils.js +346 -0
- package/lib/Utils/signal.js +188 -0
- package/lib/Utils/stanza-ack.js +33 -0
- package/lib/Utils/sync-action-utils.js +53 -0
- package/lib/Utils/tc-token-utils.js +15 -0
- package/lib/Utils/use-multi-file-auth-state.js +116 -0
- package/lib/Utils/use-single-file-auth-state.js +94 -0
- package/lib/Utils/validate-connection.js +235 -0
- package/lib/WABinary/constants.js +1300 -0
- package/lib/WABinary/decode.js +258 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.js +203 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.js +93 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.js +20669 -0
- package/lib/WAM/encode.js +151 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +21 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +50 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +20 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +29 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +59 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +21 -0
- package/lib/WAUSync/Protocols/index.js +4 -0
- package/lib/WAUSync/USyncQuery.js +103 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +11 -0
- package/package.json +58 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createCipheriv,
|
|
3
|
+
createDecipheriv,
|
|
4
|
+
createHash,
|
|
5
|
+
createHmac,
|
|
6
|
+
randomBytes,
|
|
7
|
+
} from "crypto";
|
|
8
|
+
import * as curve from "libsignal/src/curve.js";
|
|
9
|
+
import { KEY_BUNDLE_TYPE } from "../Defaults/index.js";
|
|
10
|
+
export { md5, hkdf } from "whatsapp-rust-bridge";
|
|
11
|
+
// insure browser & node compatibility
|
|
12
|
+
const { subtle } = globalThis.crypto;
|
|
13
|
+
/** prefix version byte to the pub keys, required for some curve crypto functions */ export const generateSignalPubKey =
|
|
14
|
+
(pubKey) =>
|
|
15
|
+
pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey]);
|
|
16
|
+
export const Curve = {
|
|
17
|
+
generateKeyPair: () => {
|
|
18
|
+
const { pubKey, privKey } = curve.generateKeyPair();
|
|
19
|
+
return {
|
|
20
|
+
private: Buffer.from(privKey),
|
|
21
|
+
// remove version byte
|
|
22
|
+
public: Buffer.from(pubKey.slice(1)),
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
sharedKey: (privateKey, publicKey) => {
|
|
26
|
+
const shared = curve.calculateAgreement(
|
|
27
|
+
generateSignalPubKey(publicKey),
|
|
28
|
+
privateKey,
|
|
29
|
+
);
|
|
30
|
+
return Buffer.from(shared);
|
|
31
|
+
},
|
|
32
|
+
sign: (privateKey, buf) => curve.calculateSignature(privateKey, buf),
|
|
33
|
+
verify: (pubKey, message, signature) => {
|
|
34
|
+
try {
|
|
35
|
+
curve.verifySignature(generateSignalPubKey(pubKey), message, signature);
|
|
36
|
+
return true;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
export const signedKeyPair = (identityKeyPair, keyId) => {
|
|
43
|
+
const preKey = Curve.generateKeyPair();
|
|
44
|
+
const pubKey = generateSignalPubKey(preKey.public);
|
|
45
|
+
const signature = Curve.sign(identityKeyPair.private, pubKey);
|
|
46
|
+
return { keyPair: preKey, signature: signature, keyId: keyId };
|
|
47
|
+
};
|
|
48
|
+
const GCM_TAG_LENGTH = 128 >> 3;
|
|
49
|
+
/**
|
|
50
|
+
* encrypt AES 256 GCM;
|
|
51
|
+
* where the tag tag is suffixed to the ciphertext
|
|
52
|
+
* */ export function aesEncryptGCM(plaintext, key, iv, additionalData) {
|
|
53
|
+
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
54
|
+
cipher.setAAD(additionalData);
|
|
55
|
+
return Buffer.concat([
|
|
56
|
+
cipher.update(plaintext),
|
|
57
|
+
cipher.final(),
|
|
58
|
+
cipher.getAuthTag(),
|
|
59
|
+
]);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* decrypt AES 256 GCM;
|
|
63
|
+
* where the auth tag is suffixed to the ciphertext
|
|
64
|
+
* */ export function aesDecryptGCM(ciphertext, key, iv, additionalData) {
|
|
65
|
+
const decipher = createDecipheriv("aes-256-gcm", key, iv);
|
|
66
|
+
// decrypt additional adata
|
|
67
|
+
const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
|
|
68
|
+
const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
|
|
69
|
+
// set additional data
|
|
70
|
+
decipher.setAAD(additionalData);
|
|
71
|
+
decipher.setAuthTag(tag);
|
|
72
|
+
return Buffer.concat([decipher.update(enc), decipher.final()]);
|
|
73
|
+
}
|
|
74
|
+
export function aesEncryptCTR(plaintext, key, iv) {
|
|
75
|
+
const cipher = createCipheriv("aes-256-ctr", key, iv);
|
|
76
|
+
return Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
77
|
+
}
|
|
78
|
+
export function aesDecryptCTR(ciphertext, key, iv) {
|
|
79
|
+
const decipher = createDecipheriv("aes-256-ctr", key, iv);
|
|
80
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
81
|
+
}
|
|
82
|
+
/** decrypt AES 256 CBC; where the IV is prefixed to the buffer */ export function aesDecrypt(
|
|
83
|
+
buffer,
|
|
84
|
+
key,
|
|
85
|
+
) {
|
|
86
|
+
return aesDecryptWithIV(buffer.subarray(16), key, buffer.subarray(0, 16));
|
|
87
|
+
}
|
|
88
|
+
/** decrypt AES 256 CBC */ export function aesDecryptWithIV(buffer, key, IV) {
|
|
89
|
+
const aes = createDecipheriv("aes-256-cbc", key, IV);
|
|
90
|
+
return Buffer.concat([aes.update(buffer), aes.final()]);
|
|
91
|
+
}
|
|
92
|
+
// encrypt AES 256 CBC; where a random IV is prefixed to the buffer
|
|
93
|
+
export function aesEncrypt(buffer, key) {
|
|
94
|
+
const IV = randomBytes(16);
|
|
95
|
+
const aes = createCipheriv("aes-256-cbc", key, IV);
|
|
96
|
+
return Buffer.concat([IV, aes.update(buffer), aes.final()]); // prefix IV to the buffer
|
|
97
|
+
}
|
|
98
|
+
// encrypt AES 256 CBC with a given IV
|
|
99
|
+
export function aesEncrypWithIV(buffer, key, IV) {
|
|
100
|
+
const aes = createCipheriv("aes-256-cbc", key, IV);
|
|
101
|
+
return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer
|
|
102
|
+
}
|
|
103
|
+
// sign HMAC using SHA 256
|
|
104
|
+
export function hmacSign(buffer, key, variant = "sha256") {
|
|
105
|
+
return createHmac(variant, key).update(buffer).digest();
|
|
106
|
+
}
|
|
107
|
+
export function sha256(buffer) {
|
|
108
|
+
return createHash("sha256").update(buffer).digest();
|
|
109
|
+
}
|
|
110
|
+
export async function derivePairingCodeKey(pairingCode, salt) {
|
|
111
|
+
// Convert inputs to formats Web Crypto API can work with
|
|
112
|
+
const encoder = new TextEncoder();
|
|
113
|
+
const pairingCodeBuffer = encoder.encode(pairingCode);
|
|
114
|
+
const saltBuffer = new Uint8Array(
|
|
115
|
+
salt instanceof Uint8Array ? salt : new Uint8Array(salt),
|
|
116
|
+
);
|
|
117
|
+
// Import the pairing code as key material
|
|
118
|
+
const keyMaterial = await subtle.importKey(
|
|
119
|
+
"raw",
|
|
120
|
+
pairingCodeBuffer,
|
|
121
|
+
{ name: "PBKDF2" },
|
|
122
|
+
false,
|
|
123
|
+
["deriveBits"],
|
|
124
|
+
);
|
|
125
|
+
// Derive bits using PBKDF2 with the same parameters
|
|
126
|
+
// 2 << 16 = 131,072 iterations
|
|
127
|
+
const derivedBits = await subtle.deriveBits(
|
|
128
|
+
{ name: "PBKDF2", salt: saltBuffer, iterations: 2 << 16, hash: "SHA-256" },
|
|
129
|
+
keyMaterial,
|
|
130
|
+
32 * 8,
|
|
131
|
+
);
|
|
132
|
+
return Buffer.from(derivedBits);
|
|
133
|
+
}
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import { Boom } from "@hapi/boom";
|
|
2
|
+
import { proto } from "../../WAProto/index.js";
|
|
3
|
+
import {
|
|
4
|
+
areJidsSameUser,
|
|
5
|
+
isHostedLidUser,
|
|
6
|
+
isHostedPnUser,
|
|
7
|
+
isJidBroadcast,
|
|
8
|
+
isJidGroup,
|
|
9
|
+
isJidMetaAI,
|
|
10
|
+
isJidNewsletter,
|
|
11
|
+
isJidStatusBroadcast,
|
|
12
|
+
isLidUser,
|
|
13
|
+
isPnUser,
|
|
14
|
+
} from "../WABinary/index.js";
|
|
15
|
+
import { unpadRandomMax16 } from "./generics.js";
|
|
16
|
+
|
|
17
|
+
export const getDecryptionJid = async (sender, repository) => {
|
|
18
|
+
if (isLidUser(sender) || isHostedLidUser(sender)) return sender;
|
|
19
|
+
return (await repository.lidMapping.getLIDForPN(sender)) || sender;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const storeMappingFromEnvelope = async (
|
|
23
|
+
stanza,
|
|
24
|
+
sender,
|
|
25
|
+
repository,
|
|
26
|
+
decryptionJid,
|
|
27
|
+
logger,
|
|
28
|
+
) => {
|
|
29
|
+
const { senderAlt } = extractAddressingContext(stanza);
|
|
30
|
+
|
|
31
|
+
if (
|
|
32
|
+
senderAlt &&
|
|
33
|
+
isLidUser(senderAlt) &&
|
|
34
|
+
isPnUser(sender) &&
|
|
35
|
+
decryptionJid === sender
|
|
36
|
+
) {
|
|
37
|
+
try {
|
|
38
|
+
await repository.lidMapping.storeLIDPNMappings([
|
|
39
|
+
{ lid: senderAlt, pn: sender },
|
|
40
|
+
]);
|
|
41
|
+
await repository.migrateSession(sender, senderAlt);
|
|
42
|
+
|
|
43
|
+
logger.debug(
|
|
44
|
+
{ sender: sender, senderAlt: senderAlt },
|
|
45
|
+
"Stored LID mapping from envelope",
|
|
46
|
+
);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
logger.warn(
|
|
49
|
+
{ sender: sender, senderAlt: senderAlt, error: error },
|
|
50
|
+
"Failed to store LID mapping",
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const NO_MESSAGE_FOUND_ERROR_TEXT = "Message absent from node";
|
|
57
|
+
export const MISSING_KEYS_ERROR_TEXT = "Key used already or never filled";
|
|
58
|
+
|
|
59
|
+
export const DECRYPTION_RETRY_CONFIG = {
|
|
60
|
+
maxRetries: 3,
|
|
61
|
+
baseDelayMs: 100,
|
|
62
|
+
sessionRecordErrors: ["No session record", "SessionError: No session record"],
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const NACK_REASONS = {
|
|
66
|
+
ParsingError: 487,
|
|
67
|
+
UnrecognizedStanza: 488,
|
|
68
|
+
UnrecognizedStanzaClass: 489,
|
|
69
|
+
UnrecognizedStanzaType: 490,
|
|
70
|
+
InvalidProtobuf: 491,
|
|
71
|
+
InvalidHostedCompanionStanza: 493,
|
|
72
|
+
MissingMessageSecret: 495,
|
|
73
|
+
SignalErrorOldCounter: 496,
|
|
74
|
+
MessageDeletedOnPeer: 499,
|
|
75
|
+
UnhandledError: 500,
|
|
76
|
+
UnsupportedAdminRevoke: 550,
|
|
77
|
+
UnsupportedLIDGroup: 551,
|
|
78
|
+
DBOperationFailed: 552,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const extractAddressingContext = (stanza) => {
|
|
82
|
+
let senderAlt;
|
|
83
|
+
let recipientAlt;
|
|
84
|
+
|
|
85
|
+
const sender = stanza.attrs.participant || stanza.attrs.from;
|
|
86
|
+
const addressingMode =
|
|
87
|
+
stanza.attrs.addressing_mode || (sender?.endsWith("lid") ? "lid" : "pn");
|
|
88
|
+
|
|
89
|
+
if (addressingMode === "lid") {
|
|
90
|
+
senderAlt =
|
|
91
|
+
stanza.attrs.participant_pn ||
|
|
92
|
+
stanza.attrs.sender_pn ||
|
|
93
|
+
stanza.attrs.peer_recipient_pn;
|
|
94
|
+
|
|
95
|
+
recipientAlt = stanza.attrs.recipient_pn;
|
|
96
|
+
} else {
|
|
97
|
+
senderAlt =
|
|
98
|
+
stanza.attrs.participant_lid ||
|
|
99
|
+
stanza.attrs.sender_lid ||
|
|
100
|
+
stanza.attrs.peer_recipient_lid;
|
|
101
|
+
|
|
102
|
+
recipientAlt = stanza.attrs.recipient_lid;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return { addressingMode, senderAlt, recipientAlt };
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export function decodeMessageNode(stanza, meId, meLid) {
|
|
109
|
+
let msgType;
|
|
110
|
+
let chatId;
|
|
111
|
+
let author;
|
|
112
|
+
let fromMe = false;
|
|
113
|
+
|
|
114
|
+
const msgId = stanza.attrs.id;
|
|
115
|
+
const from = stanza.attrs.from;
|
|
116
|
+
const participant = stanza.attrs.participant;
|
|
117
|
+
const recipient = stanza.attrs.recipient;
|
|
118
|
+
|
|
119
|
+
const addressingContext = extractAddressingContext(stanza);
|
|
120
|
+
|
|
121
|
+
const isMe = (jid) => areJidsSameUser(jid, meId);
|
|
122
|
+
const isMeLid = (jid) => areJidsSameUser(jid, meLid);
|
|
123
|
+
|
|
124
|
+
if (
|
|
125
|
+
isPnUser(from) ||
|
|
126
|
+
isLidUser(from) ||
|
|
127
|
+
isHostedLidUser(from) ||
|
|
128
|
+
isHostedPnUser(from)
|
|
129
|
+
) {
|
|
130
|
+
if (recipient && !isJidMetaAI(recipient)) {
|
|
131
|
+
if (!isMe(from) && !isMeLid(from)) {
|
|
132
|
+
throw new Boom("receipient present, but msg not from me", {
|
|
133
|
+
data: stanza,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (isMe(from) || isMeLid(from)) {
|
|
138
|
+
fromMe = true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
chatId = recipient;
|
|
142
|
+
} else {
|
|
143
|
+
chatId = from;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
msgType = "chat";
|
|
147
|
+
author = from;
|
|
148
|
+
} else if (isJidGroup(from)) {
|
|
149
|
+
if (!participant) {
|
|
150
|
+
throw new Boom("No participant in group message");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (isMe(participant) || isMeLid(participant)) {
|
|
154
|
+
fromMe = true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
msgType = "group";
|
|
158
|
+
author = participant;
|
|
159
|
+
chatId = from;
|
|
160
|
+
} else if (isJidBroadcast(from)) {
|
|
161
|
+
if (!participant) {
|
|
162
|
+
throw new Boom("No participant in group message");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const isParticipantMe = isMe(participant);
|
|
166
|
+
|
|
167
|
+
msgType = isJidStatusBroadcast(from)
|
|
168
|
+
? isParticipantMe
|
|
169
|
+
? "direct_peer_status"
|
|
170
|
+
: "other_status"
|
|
171
|
+
: isParticipantMe
|
|
172
|
+
? "peer_broadcast"
|
|
173
|
+
: "other_broadcast";
|
|
174
|
+
|
|
175
|
+
fromMe = isParticipantMe;
|
|
176
|
+
chatId = from;
|
|
177
|
+
author = participant;
|
|
178
|
+
} else {
|
|
179
|
+
if (!isJidNewsletter(from)) {
|
|
180
|
+
throw new Boom("Unknown message type", { data: stanza });
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
msgType = "newsletter";
|
|
184
|
+
chatId = from;
|
|
185
|
+
author = from;
|
|
186
|
+
|
|
187
|
+
if (isMe(from) || isMeLid(from)) {
|
|
188
|
+
fromMe = true;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const pushName = stanza?.attrs?.notify;
|
|
193
|
+
|
|
194
|
+
const key = {
|
|
195
|
+
remoteJid: isJidGroup(chatId) ? chatId : addressingContext.senderAlt,
|
|
196
|
+
remoteJidAlt: chatId,
|
|
197
|
+
fromMe,
|
|
198
|
+
id: msgId,
|
|
199
|
+
participant: isJidGroup(chatId)
|
|
200
|
+
? addressingContext.senderAlt ||
|
|
201
|
+
(fromMe
|
|
202
|
+
? meId.includes(":")
|
|
203
|
+
? meId.split(":")[0] + "@s.whatsapp.net"
|
|
204
|
+
: meId
|
|
205
|
+
: "")
|
|
206
|
+
: "",
|
|
207
|
+
participantAlt: participant,
|
|
208
|
+
addressingMode: addressingContext.addressingMode,
|
|
209
|
+
...(msgType === "newsletter" && stanza.attrs.server_id
|
|
210
|
+
? { server_id: stanza.attrs.server_id }
|
|
211
|
+
: {}),
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const fullMessage = {
|
|
215
|
+
key,
|
|
216
|
+
category: stanza.attrs.category,
|
|
217
|
+
messageTimestamp: +stanza.attrs.t,
|
|
218
|
+
pushName,
|
|
219
|
+
broadcast: isJidBroadcast(from),
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
if (key.fromMe) {
|
|
223
|
+
fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return {
|
|
227
|
+
fullMessage,
|
|
228
|
+
author,
|
|
229
|
+
sender: msgType === "chat" ? author : chatId,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
234
|
+
const { fullMessage, author, sender } = decodeMessageNode(
|
|
235
|
+
stanza,
|
|
236
|
+
meId,
|
|
237
|
+
meLid,
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
return {
|
|
241
|
+
fullMessage,
|
|
242
|
+
category: stanza.attrs.category,
|
|
243
|
+
author,
|
|
244
|
+
|
|
245
|
+
async decrypt() {
|
|
246
|
+
let decryptables = 0;
|
|
247
|
+
|
|
248
|
+
if (Array.isArray(stanza.content)) {
|
|
249
|
+
for (const { tag, attrs, content } of stanza.content) {
|
|
250
|
+
if (tag === "verified_name" && content instanceof Uint8Array) {
|
|
251
|
+
const cert = proto.VerifiedNameCertificate.decode(content);
|
|
252
|
+
const details = proto.VerifiedNameCertificate.Details.decode(
|
|
253
|
+
cert.details,
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
fullMessage.verifiedBizName = details.verifiedName;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (tag === "unavailable" && attrs.type === "view_once") {
|
|
260
|
+
fullMessage.key.isViewOnce = true;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (attrs.count && tag === "enc") {
|
|
264
|
+
fullMessage.retryCount = Number(attrs.count);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (tag !== "enc" && tag !== "plaintext") continue;
|
|
268
|
+
if (!(content instanceof Uint8Array)) continue;
|
|
269
|
+
|
|
270
|
+
decryptables++;
|
|
271
|
+
|
|
272
|
+
const decryptionJid = await getDecryptionJid(author, repository);
|
|
273
|
+
|
|
274
|
+
if (tag !== "plaintext") {
|
|
275
|
+
await storeMappingFromEnvelope(
|
|
276
|
+
stanza,
|
|
277
|
+
author,
|
|
278
|
+
repository,
|
|
279
|
+
decryptionJid,
|
|
280
|
+
logger,
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
try {
|
|
285
|
+
const e2eType = tag === "plaintext" ? "plaintext" : attrs.type;
|
|
286
|
+
|
|
287
|
+
let msgBuffer;
|
|
288
|
+
|
|
289
|
+
switch (e2eType) {
|
|
290
|
+
case "skmsg":
|
|
291
|
+
msgBuffer = await repository.decryptGroupMessage({
|
|
292
|
+
group: sender,
|
|
293
|
+
authorJid: author,
|
|
294
|
+
msg: content,
|
|
295
|
+
});
|
|
296
|
+
break;
|
|
297
|
+
|
|
298
|
+
case "pkmsg":
|
|
299
|
+
case "msg":
|
|
300
|
+
msgBuffer = await repository.decryptMessage({
|
|
301
|
+
jid: decryptionJid,
|
|
302
|
+
type: e2eType,
|
|
303
|
+
ciphertext: content,
|
|
304
|
+
});
|
|
305
|
+
break;
|
|
306
|
+
|
|
307
|
+
case "plaintext":
|
|
308
|
+
msgBuffer = content;
|
|
309
|
+
break;
|
|
310
|
+
|
|
311
|
+
default:
|
|
312
|
+
throw new Error(`Unknown e2e type: ${e2eType}`);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
let msg = proto.Message.decode(
|
|
316
|
+
e2eType !== "plaintext" ? unpadRandomMax16(msgBuffer) : msgBuffer,
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
msg = msg.deviceSentMessage?.message || msg;
|
|
320
|
+
|
|
321
|
+
if (msg.senderKeyDistributionMessage) {
|
|
322
|
+
try {
|
|
323
|
+
await repository.processSenderKeyDistributionMessage({
|
|
324
|
+
authorJid: author,
|
|
325
|
+
item: msg.senderKeyDistributionMessage,
|
|
326
|
+
});
|
|
327
|
+
} catch (err) {
|
|
328
|
+
logger.error(
|
|
329
|
+
{ key: fullMessage.key, err },
|
|
330
|
+
"failed to process sender key distribution message",
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (fullMessage.message) {
|
|
336
|
+
Object.assign(fullMessage.message, msg);
|
|
337
|
+
} else {
|
|
338
|
+
fullMessage.message = msg;
|
|
339
|
+
}
|
|
340
|
+
} catch (err) {
|
|
341
|
+
logger.error(
|
|
342
|
+
{
|
|
343
|
+
key: fullMessage.key,
|
|
344
|
+
err,
|
|
345
|
+
messageType: tag === "plaintext" ? "plaintext" : attrs.type,
|
|
346
|
+
sender,
|
|
347
|
+
author,
|
|
348
|
+
isSessionRecordError: isSessionRecordError(err),
|
|
349
|
+
},
|
|
350
|
+
"failed to decrypt message",
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
fullMessage.messageStubType =
|
|
354
|
+
proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
355
|
+
|
|
356
|
+
fullMessage.messageStubParameters = [err.message.toString()];
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (!decryptables && !fullMessage.key?.isViewOnce) {
|
|
362
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
363
|
+
|
|
364
|
+
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT];
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
};
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
function isSessionRecordError(error) {
|
|
371
|
+
const errorMessage = error?.message || error?.toString() || "";
|
|
372
|
+
|
|
373
|
+
return DECRYPTION_RETRY_CONFIG.sessionRecordErrors.some((pattern) =>
|
|
374
|
+
errorMessage.includes(pattern),
|
|
375
|
+
);
|
|
376
|
+
}
|