@aseppxyzz12/baileys 1.1.8
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 +25 -0
- package/README.md +2579 -0
- package/WAProto/AICommon/AICommon.js +19396 -0
- package/WAProto/AICommon/AICommon.proto +820 -0
- package/WAProto/Adv/Adv.js +1137 -0
- package/WAProto/Adv/Adv.proto +42 -0
- package/WAProto/BotMetadata/BotMetadata.js +8975 -0
- package/WAProto/BotMetadata/BotMetadata.proto +484 -0
- package/WAProto/Cert/Cert.js +893 -0
- package/WAProto/Cert/Cert.proto +30 -0
- package/WAProto/ChatLockSettings/ChatLockSettings.js +673 -0
- package/WAProto/ChatLockSettings/ChatLockSettings.proto +9 -0
- package/WAProto/CompanionReg/CompanionReg.js +2457 -0
- package/WAProto/CompanionReg/CompanionReg.proto +103 -0
- package/WAProto/DeviceCapabilities/DeviceCapabilities.js +846 -0
- package/WAProto/DeviceCapabilities/DeviceCapabilities.proto +38 -0
- package/WAProto/E2E/E2E.js +78728 -0
- package/WAProto/E2E/E2E.proto +1970 -0
- package/WAProto/Ephemeral/Ephemeral.js +143 -0
- package/WAProto/Ephemeral/Ephemeral.proto +7 -0
- package/WAProto/HistorySync/HistorySync.js +95931 -0
- package/WAProto/HistorySync/HistorySync.proto +229 -0
- package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.js +414 -0
- package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.proto +13 -0
- package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.js +980 -0
- package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.proto +38 -0
- package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.js +81185 -0
- package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.proto +88 -0
- package/WAProto/MmsRetry/MmsRetry.js +310 -0
- package/WAProto/MmsRetry/MmsRetry.proto +19 -0
- package/WAProto/Protocol/Protocol.js +399 -0
- package/WAProto/Protocol/Protocol.proto +22 -0
- package/WAProto/Reporting/Reporting.js +535 -0
- package/WAProto/Reporting/Reporting.proto +22 -0
- package/WAProto/ServerSync/ServerSync.js +1830 -0
- package/WAProto/ServerSync/ServerSync.proto +70 -0
- package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.js +3267 -0
- package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.proto +95 -0
- package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.js +1140 -0
- package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.proto +44 -0
- package/WAProto/StatusAttributions/StatusAttributions.js +1604 -0
- package/WAProto/StatusAttributions/StatusAttributions.proto +89 -0
- package/WAProto/SyncAction/SyncAction.js +17424 -0
- package/WAProto/SyncAction/SyncAction.proto +663 -0
- package/WAProto/UserPassword/UserPassword.js +544 -0
- package/WAProto/UserPassword/UserPassword.proto +28 -0
- package/WAProto/VnameCert/VnameCert.js +1466 -0
- package/WAProto/VnameCert/VnameCert.proto +65 -0
- package/WAProto/Wa6/Wa6.js +4601 -0
- package/WAProto/Wa6/Wa6.proto +241 -0
- package/WAProto/Web/Web.js +90819 -0
- package/WAProto/Web/Web.proto +605 -0
- package/WAProto/index.js +30 -0
- package/engine-requirements.js +9 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/connection.js +39 -0
- package/lib/Defaults/constants.js +50 -0
- package/lib/Defaults/history.js +13 -0
- package/lib/Defaults/index.js +36 -0
- package/lib/Defaults/media.js +43 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Defaults/prefix.js +12 -0
- package/lib/Function/Download/tiktok.js +19 -0
- package/lib/Function/Tools/bypass.js +19 -0
- package/lib/Function/index.js +13 -0
- package/lib/Signal/Group/ciphertext-message.js +14 -0
- package/lib/Signal/Group/group-session-builder.js +46 -0
- package/lib/Signal/Group/group_cipher.js +104 -0
- package/lib/Signal/Group/index.js +42 -0
- package/lib/Signal/Group/keyhelper.js +21 -0
- package/lib/Signal/Group/queue-job.js +57 -0
- package/lib/Signal/Group/sender-chain-key.js +34 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +78 -0
- package/lib/Signal/Group/sender-key-name.js +49 -0
- package/lib/Signal/Group/sender-key-record.js +45 -0
- package/lib/Signal/Group/sender-key-state.js +100 -0
- package/lib/Signal/Group/sender-message-key.js +28 -0
- package/lib/Signal/libsignal.js +364 -0
- package/lib/Signal/lid-mapping.js +164 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.js +31 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/types.js +12 -0
- package/lib/Socket/Client/web-socket-client.js +62 -0
- package/lib/Socket/Client/websocket.js +67 -0
- package/lib/Socket/business.js +291 -0
- package/lib/Socket/chats.js +874 -0
- package/lib/Socket/community.js +454 -0
- package/lib/Socket/dugong.js +658 -0
- package/lib/Socket/groups.js +357 -0
- package/lib/Socket/index.js +13 -0
- package/lib/Socket/messages-recv.js +1509 -0
- package/lib/Socket/messages-send.js +1505 -0
- package/lib/Socket/mex.js +54 -0
- package/lib/Socket/newsletter.js +171 -0
- package/lib/Socket/registration.js +167 -0
- package/lib/Socket/rich-content.js +279 -0
- package/lib/Socket/socket.js +905 -0
- package/lib/Socket/username.js +157 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.js +37 -0
- package/lib/Store/keyed-db.js +114 -0
- package/lib/Store/make-cache-manager-store.js +77 -0
- package/lib/Store/make-in-memory-store.js +423 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.js +26 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +10 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +4 -0
- package/lib/Types/Message.js +11 -0
- package/lib/Types/Mex.js +40 -0
- package/lib/Types/MexUpdates.js +15 -0
- package/lib/Types/Newsletter.js +32 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +9 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +60 -0
- package/lib/Utils/auth-utils.js +256 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/bot-toolkit.js +455 -0
- package/lib/Utils/browser-utils.js +29 -0
- package/lib/Utils/business.js +233 -0
- package/lib/Utils/chat-utils.js +904 -0
- package/lib/Utils/companion-reg-client-utils.js +41 -0
- package/lib/Utils/crypto.js +144 -0
- package/lib/Utils/decode-wa-message.js +305 -0
- package/lib/Utils/event-buffer.js +555 -0
- package/lib/Utils/generics.js +411 -0
- package/lib/Utils/history.js +100 -0
- package/lib/Utils/identity-change-handler.js +53 -0
- package/lib/Utils/index.js +57 -0
- package/lib/Utils/link-preview.js +76 -0
- package/lib/Utils/logger.js +4 -0
- package/lib/Utils/lt-hash.js +45 -0
- package/lib/Utils/make-mutex.js +33 -0
- package/lib/Utils/message-composer.js +289 -0
- package/lib/Utils/message-retry-manager.js +134 -0
- package/lib/Utils/messages-media.js +806 -0
- package/lib/Utils/messages.js +1908 -0
- package/lib/Utils/noise-handler.js +157 -0
- package/lib/Utils/offline-node-processor.js +42 -0
- package/lib/Utils/pre-key-manager.js +86 -0
- package/lib/Utils/process-message.js +814 -0
- package/lib/Utils/reporting-utils.js +264 -0
- package/lib/Utils/signal.js +183 -0
- package/lib/Utils/stanza-ack.js +36 -0
- package/lib/Utils/sync-action-utils.js +52 -0
- package/lib/Utils/tc-token-utils.js +167 -0
- package/lib/Utils/use-multi-file-auth-state.js +104 -0
- package/lib/Utils/validate-connection.js +229 -0
- package/lib/Utils/vialeys-event-stream.js +41 -0
- package/lib/WABinary/constants.js +1308 -0
- package/lib/WABinary/decode.js +233 -0
- package/lib/WABinary/encode.js +212 -0
- package/lib/WABinary/generic-utils.js +124 -0
- package/lib/WABinary/index.js +36 -0
- package/lib/WABinary/jid-utils.js +101 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +12 -0
- package/lib/WAM/constants.js +20491 -0
- package/lib/WAM/encode.js +148 -0
- package/lib/WAM/index.js +34 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +62 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +50 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +25 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.js +38 -0
- package/lib/WAUSync/USyncQuery.js +97 -0
- package/lib/WAUSync/USyncUser.js +33 -0
- package/lib/WAUSync/index.js +34 -0
- package/lib/index.js +113 -0
- package/lib/messages-send.js +1496 -0
- package/package.json +100 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Ported & adapted from PouCode fork for AsepXyz Baileys
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.buildPairingQRData = exports.getCompanionPlatformId = exports.getCompanionWebClientType = exports.CompanionWebClientType = void 0;
|
|
5
|
+
var CompanionWebClientType;
|
|
6
|
+
(function (CompanionWebClientType) {
|
|
7
|
+
CompanionWebClientType[CompanionWebClientType["UNKNOWN"] = 0] = "UNKNOWN";
|
|
8
|
+
CompanionWebClientType[CompanionWebClientType["CHROME"] = 1] = "CHROME";
|
|
9
|
+
CompanionWebClientType[CompanionWebClientType["EDGE"] = 2] = "EDGE";
|
|
10
|
+
CompanionWebClientType[CompanionWebClientType["FIREFOX"] = 3] = "FIREFOX";
|
|
11
|
+
CompanionWebClientType[CompanionWebClientType["IE"] = 4] = "IE";
|
|
12
|
+
CompanionWebClientType[CompanionWebClientType["OPERA"] = 5] = "OPERA";
|
|
13
|
+
CompanionWebClientType[CompanionWebClientType["SAFARI"] = 6] = "SAFARI";
|
|
14
|
+
CompanionWebClientType[CompanionWebClientType["ELECTRON"] = 7] = "ELECTRON";
|
|
15
|
+
CompanionWebClientType[CompanionWebClientType["UWP"] = 8] = "UWP";
|
|
16
|
+
CompanionWebClientType[CompanionWebClientType["OTHER_WEB_CLIENT"] = 9] = "OTHER_WEB_CLIENT";
|
|
17
|
+
})(CompanionWebClientType = exports.CompanionWebClientType || (exports.CompanionWebClientType = {}));
|
|
18
|
+
const BROWSER_TO_COMPANION_WEB_CLIENT = {
|
|
19
|
+
Chrome: CompanionWebClientType.CHROME,
|
|
20
|
+
Edge: CompanionWebClientType.EDGE,
|
|
21
|
+
Firefox: CompanionWebClientType.FIREFOX,
|
|
22
|
+
IE: CompanionWebClientType.IE,
|
|
23
|
+
Opera: CompanionWebClientType.OPERA,
|
|
24
|
+
Safari: CompanionWebClientType.SAFARI
|
|
25
|
+
};
|
|
26
|
+
const getCompanionWebClientType = ([os, browserName]) => {
|
|
27
|
+
if (browserName === 'Desktop') {
|
|
28
|
+
return os === 'Windows' ? CompanionWebClientType.UWP : CompanionWebClientType.ELECTRON;
|
|
29
|
+
}
|
|
30
|
+
return BROWSER_TO_COMPANION_WEB_CLIENT[browserName] || CompanionWebClientType.OTHER_WEB_CLIENT;
|
|
31
|
+
};
|
|
32
|
+
exports.getCompanionWebClientType = getCompanionWebClientType;
|
|
33
|
+
const getCompanionPlatformId = (browser) => {
|
|
34
|
+
return getCompanionWebClientType(browser).toString();
|
|
35
|
+
};
|
|
36
|
+
exports.getCompanionPlatformId = getCompanionPlatformId;
|
|
37
|
+
const buildPairingQRData = (ref, noiseKeyB64, identityKeyB64, advB64, browser) => {
|
|
38
|
+
return ('https://wa.me/settings/linked_devices#' +
|
|
39
|
+
[ref, noiseKeyB64, identityKeyB64, advB64, getCompanionPlatformId(browser)].join(','));
|
|
40
|
+
};
|
|
41
|
+
exports.buildPairingQRData = buildPairingQRData;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const {
|
|
4
|
+
createCipheriv: createCipheriv,
|
|
5
|
+
createDecipheriv: createDecipheriv,
|
|
6
|
+
createHash: createHash,
|
|
7
|
+
createHmac: createHmac,
|
|
8
|
+
randomBytes: randomBytes,
|
|
9
|
+
} = require("crypto");
|
|
10
|
+
const { curve: curve } = require("libsignal");
|
|
11
|
+
const { KEY_BUNDLE_TYPE: KEY_BUNDLE_TYPE } = require("../Defaults/constants");
|
|
12
|
+
const generateSignalPubKey = (pubKey) =>
|
|
13
|
+
pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey]);
|
|
14
|
+
const Curve = {
|
|
15
|
+
generateKeyPair: () => {
|
|
16
|
+
const { pubKey: pubKey, privKey: privKey } = curve.generateKeyPair();
|
|
17
|
+
return {
|
|
18
|
+
private: Buffer.from(privKey),
|
|
19
|
+
public: Buffer.from(pubKey.slice(1)),
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
sharedKey: (privateKey, publicKey) => {
|
|
23
|
+
const shared = curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey);
|
|
24
|
+
return Buffer.from(shared);
|
|
25
|
+
},
|
|
26
|
+
sign: (privateKey, buf) => curve.calculateSignature(privateKey, buf),
|
|
27
|
+
verify: (pubKey, message, signature) => {
|
|
28
|
+
try {
|
|
29
|
+
curve.verifySignature(generateSignalPubKey(pubKey), message, signature);
|
|
30
|
+
return true;
|
|
31
|
+
} catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
const signedKeyPair = (identityKeyPair, keyId) => {
|
|
37
|
+
const preKey = Curve.generateKeyPair();
|
|
38
|
+
const pubKey = generateSignalPubKey(preKey.public);
|
|
39
|
+
const signature = Curve.sign(identityKeyPair.private, pubKey);
|
|
40
|
+
return { keyPair: preKey, signature: signature, keyId: keyId };
|
|
41
|
+
};
|
|
42
|
+
const GCM_TAG_LENGTH = 128 >> 3;
|
|
43
|
+
function aesEncryptGCM(plaintext, key, iv, additionalData) {
|
|
44
|
+
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
45
|
+
cipher.setAAD(additionalData);
|
|
46
|
+
return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
|
|
47
|
+
}
|
|
48
|
+
function aesDecryptGCM(ciphertext, key, iv, additionalData) {
|
|
49
|
+
const decipher = createDecipheriv("aes-256-gcm", key, iv);
|
|
50
|
+
const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
|
|
51
|
+
const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
|
|
52
|
+
if (additionalData) decipher.setAAD(additionalData);
|
|
53
|
+
decipher.setAuthTag(tag);
|
|
54
|
+
return Buffer.concat([decipher.update(enc), decipher.final()]);
|
|
55
|
+
}
|
|
56
|
+
function aesEncryptCTR(plaintext, key, iv) {
|
|
57
|
+
const cipher = createCipheriv("aes-256-ctr", key, iv);
|
|
58
|
+
return Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
59
|
+
}
|
|
60
|
+
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
|
+
function aesDecrypt(buffer, key) {
|
|
65
|
+
return aesDecryptWithIV(buffer.slice(16), key, buffer.slice(0, 16));
|
|
66
|
+
}
|
|
67
|
+
function aesDecryptWithIV(buffer, key, IV) {
|
|
68
|
+
const aes = createDecipheriv("aes-256-cbc", key, IV);
|
|
69
|
+
return Buffer.concat([aes.update(buffer), aes.final()]);
|
|
70
|
+
}
|
|
71
|
+
function aesEncrypt(buffer, key) {
|
|
72
|
+
const IV = randomBytes(16);
|
|
73
|
+
const aes = createCipheriv("aes-256-cbc", key, IV);
|
|
74
|
+
return Buffer.concat([IV, aes.update(buffer), aes.final()]);
|
|
75
|
+
}
|
|
76
|
+
function aesEncrypWithIV(buffer, key, IV) {
|
|
77
|
+
const aes = createCipheriv("aes-256-cbc", key, IV);
|
|
78
|
+
return Buffer.concat([aes.update(buffer), aes.final()]);
|
|
79
|
+
}
|
|
80
|
+
function hmacSign(buffer, key, variant = "sha256") {
|
|
81
|
+
return createHmac(variant, key).update(buffer).digest();
|
|
82
|
+
}
|
|
83
|
+
function sha256(buffer) {
|
|
84
|
+
return createHash("sha256").update(buffer).digest();
|
|
85
|
+
}
|
|
86
|
+
function md5(buffer) {
|
|
87
|
+
return createHash("md5").update(buffer).digest();
|
|
88
|
+
}
|
|
89
|
+
async function hkdf(buffer, expandedLength, info) {
|
|
90
|
+
const inputKeyMaterial = new Uint8Array(
|
|
91
|
+
buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer)
|
|
92
|
+
);
|
|
93
|
+
const salt = info.salt ? new Uint8Array(info.salt) : new Uint8Array(0);
|
|
94
|
+
const infoBytes = info.info ? new TextEncoder().encode(info.info) : new Uint8Array(0);
|
|
95
|
+
const importedKey = await crypto.subtle.importKey(
|
|
96
|
+
"raw",
|
|
97
|
+
inputKeyMaterial,
|
|
98
|
+
{ name: "HKDF" },
|
|
99
|
+
false,
|
|
100
|
+
["deriveBits"]
|
|
101
|
+
);
|
|
102
|
+
const derivedBits = await crypto.subtle.deriveBits(
|
|
103
|
+
{ name: "HKDF", hash: "SHA-256", salt: salt, info: infoBytes },
|
|
104
|
+
importedKey,
|
|
105
|
+
expandedLength * 8
|
|
106
|
+
);
|
|
107
|
+
return Buffer.from(derivedBits);
|
|
108
|
+
}
|
|
109
|
+
async function derivePairingCodeKey(pairingCode, salt) {
|
|
110
|
+
const encoder = new TextEncoder();
|
|
111
|
+
const pairingCodeBuffer = encoder.encode(pairingCode);
|
|
112
|
+
const saltBuffer = new Uint8Array(salt instanceof Uint8Array ? salt : new Uint8Array(salt));
|
|
113
|
+
const keyMaterial = await crypto.subtle.importKey(
|
|
114
|
+
"raw",
|
|
115
|
+
pairingCodeBuffer,
|
|
116
|
+
{ name: "PBKDF2" },
|
|
117
|
+
false,
|
|
118
|
+
["deriveBits"]
|
|
119
|
+
);
|
|
120
|
+
const derivedBits = await crypto.subtle.deriveBits(
|
|
121
|
+
{ name: "PBKDF2", salt: saltBuffer, iterations: 2 << 16, hash: "SHA-256" },
|
|
122
|
+
keyMaterial,
|
|
123
|
+
32 * 8
|
|
124
|
+
);
|
|
125
|
+
return Buffer.from(derivedBits);
|
|
126
|
+
}
|
|
127
|
+
module.exports = {
|
|
128
|
+
generateSignalPubKey: generateSignalPubKey,
|
|
129
|
+
Curve: Curve,
|
|
130
|
+
signedKeyPair: signedKeyPair,
|
|
131
|
+
aesEncryptGCM: aesEncryptGCM,
|
|
132
|
+
aesDecryptGCM: aesDecryptGCM,
|
|
133
|
+
aesEncryptCTR: aesEncryptCTR,
|
|
134
|
+
aesDecryptCTR: aesDecryptCTR,
|
|
135
|
+
aesDecrypt: aesDecrypt,
|
|
136
|
+
aesDecryptWithIV: aesDecryptWithIV,
|
|
137
|
+
aesEncrypt: aesEncrypt,
|
|
138
|
+
aesEncrypWithIV: aesEncrypWithIV,
|
|
139
|
+
hmacSign: hmacSign,
|
|
140
|
+
sha256: sha256,
|
|
141
|
+
md5: md5,
|
|
142
|
+
hkdf: hkdf,
|
|
143
|
+
derivePairingCodeKey: derivePairingCodeKey,
|
|
144
|
+
};
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const { Boom: Boom } = require("@hapi/boom");
|
|
4
|
+
const { proto: proto } = require("../../WAProto");
|
|
5
|
+
const {
|
|
6
|
+
areJidsSameUser: areJidsSameUser,
|
|
7
|
+
isHostedLidUser: isHostedLidUser,
|
|
8
|
+
isHostedPnUser: isHostedPnUser,
|
|
9
|
+
isJidBroadcast: isJidBroadcast,
|
|
10
|
+
isJidGroup: isJidGroup,
|
|
11
|
+
isJidMetaAI: isJidMetaAI,
|
|
12
|
+
isJidNewsletter: isJidNewsletter,
|
|
13
|
+
isJidStatusBroadcast: isJidStatusBroadcast,
|
|
14
|
+
isLidUser: isLidUser,
|
|
15
|
+
isPnUser: isPnUser,
|
|
16
|
+
} = require("../WABinary");
|
|
17
|
+
const { unpadRandomMax16: unpadRandomMax16 } = require("./generics");
|
|
18
|
+
const { getDevice: getDevice } = require("./messages");
|
|
19
|
+
const NO_MESSAGE_FOUND_ERROR_TEXT = "Message absent from node";
|
|
20
|
+
const MISSING_KEYS_ERROR_TEXT = "Key used already or never filled";
|
|
21
|
+
const DECRYPTION_RETRY_CONFIG = {
|
|
22
|
+
maxRetries: 3,
|
|
23
|
+
baseDelayMs: 100,
|
|
24
|
+
sessionRecordErrors: ["No session record", "SessionError: No session record"],
|
|
25
|
+
};
|
|
26
|
+
const NACK_REASONS = {
|
|
27
|
+
ParsingError: 487,
|
|
28
|
+
UnrecognizedStanza: 488,
|
|
29
|
+
UnrecognizedStanzaClass: 489,
|
|
30
|
+
UnrecognizedStanzaType: 490,
|
|
31
|
+
InvalidProtobuf: 491,
|
|
32
|
+
InvalidHostedCompanionStanza: 493,
|
|
33
|
+
MissingMessageSecret: 495,
|
|
34
|
+
SignalErrorOldCounter: 496,
|
|
35
|
+
MessageDeletedOnPeer: 499,
|
|
36
|
+
UnhandledError: 500,
|
|
37
|
+
UnsupportedAdminRevoke: 550,
|
|
38
|
+
UnsupportedLIDGroup: 551,
|
|
39
|
+
DBOperationFailed: 552,
|
|
40
|
+
};
|
|
41
|
+
const extractAddressingContext = (stanza) => {
|
|
42
|
+
let senderAlt;
|
|
43
|
+
let recipientAlt;
|
|
44
|
+
const sender = stanza.attrs.participant || stanza.attrs.from;
|
|
45
|
+
const addressingMode = stanza.attrs.addressing_mode || (sender?.endsWith("lid") ? "lid" : "pn");
|
|
46
|
+
if (addressingMode === "lid") {
|
|
47
|
+
senderAlt =
|
|
48
|
+
stanza.attrs.participant_pn || stanza.attrs.sender_pn || stanza.attrs.peer_recipient_pn;
|
|
49
|
+
recipientAlt = stanza.attrs.recipient_pn;
|
|
50
|
+
} else {
|
|
51
|
+
senderAlt =
|
|
52
|
+
stanza.attrs.participant_lid ||
|
|
53
|
+
stanza.attrs.sender_lid ||
|
|
54
|
+
stanza.attrs.peer_recipient_lid;
|
|
55
|
+
recipientAlt = stanza.attrs.recipient_lid;
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
addressingMode: addressingMode,
|
|
59
|
+
senderAlt: senderAlt,
|
|
60
|
+
recipientAlt: recipientAlt,
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
const getDecryptionJid = async (sender, repository) => {
|
|
64
|
+
if (isLidUser(sender) || isHostedLidUser(sender)) {
|
|
65
|
+
return sender;
|
|
66
|
+
}
|
|
67
|
+
const mapped = await repository.lidMapping.getLIDForPN(sender);
|
|
68
|
+
return mapped || sender;
|
|
69
|
+
};
|
|
70
|
+
const storeMappingFromEnvelope = async (stanza, sender, repository, decryptionJid, logger) => {
|
|
71
|
+
const { senderAlt: senderAlt } = extractAddressingContext(stanza);
|
|
72
|
+
if (senderAlt && isLidUser(senderAlt) && isPnUser(sender) && decryptionJid === sender) {
|
|
73
|
+
try {
|
|
74
|
+
await repository.lidMapping.storeLIDPNMappings([{ lid: senderAlt, pn: sender }]);
|
|
75
|
+
await repository.migrateSession(sender, senderAlt);
|
|
76
|
+
logger.debug(
|
|
77
|
+
{ sender: sender, senderAlt: senderAlt },
|
|
78
|
+
"Stored LID mapping from envelope"
|
|
79
|
+
);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
logger.warn(
|
|
82
|
+
{ sender: sender, senderAlt: senderAlt, error: error },
|
|
83
|
+
"Failed to store LID mapping"
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
function decodeMessageNode(stanza, meId, meLid) {
|
|
89
|
+
let msgType;
|
|
90
|
+
let chatId;
|
|
91
|
+
let author;
|
|
92
|
+
let fromMe = false;
|
|
93
|
+
const msgId = stanza.attrs.id;
|
|
94
|
+
const from = stanza.attrs.from;
|
|
95
|
+
const participant = stanza.attrs.participant;
|
|
96
|
+
const recipient = stanza.attrs.recipient;
|
|
97
|
+
const addressingContext = extractAddressingContext(stanza);
|
|
98
|
+
const isMe = (jid) => areJidsSameUser(jid, meId);
|
|
99
|
+
const isMeLid = (jid) => areJidsSameUser(jid, meLid);
|
|
100
|
+
if (isPnUser(from) || isLidUser(from) || isHostedLidUser(from) || isHostedPnUser(from)) {
|
|
101
|
+
if (recipient && !isJidMetaAI(recipient)) {
|
|
102
|
+
if (!isMe(from) && !isMeLid(from)) {
|
|
103
|
+
throw new Boom("receipient present, but msg not from me", {
|
|
104
|
+
data: stanza,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (isMe(from) || isMeLid(from)) {
|
|
108
|
+
fromMe = true;
|
|
109
|
+
}
|
|
110
|
+
chatId = recipient;
|
|
111
|
+
} else {
|
|
112
|
+
chatId = from;
|
|
113
|
+
}
|
|
114
|
+
msgType = "chat";
|
|
115
|
+
author = from;
|
|
116
|
+
} else if (isJidGroup(from)) {
|
|
117
|
+
if (!participant) {
|
|
118
|
+
throw new Boom("No participant in group message");
|
|
119
|
+
}
|
|
120
|
+
if (isMe(participant) || isMeLid(participant)) {
|
|
121
|
+
fromMe = true;
|
|
122
|
+
}
|
|
123
|
+
msgType = "group";
|
|
124
|
+
author = participant;
|
|
125
|
+
chatId = from;
|
|
126
|
+
} else if (isJidBroadcast(from)) {
|
|
127
|
+
if (!participant) {
|
|
128
|
+
throw new Boom("No participant in group message");
|
|
129
|
+
}
|
|
130
|
+
const isParticipantMe = isMe(participant);
|
|
131
|
+
if (isJidStatusBroadcast(from)) {
|
|
132
|
+
msgType = isParticipantMe ? "direct_peer_status" : "other_status";
|
|
133
|
+
} else {
|
|
134
|
+
msgType = isParticipantMe ? "peer_broadcast" : "other_broadcast";
|
|
135
|
+
}
|
|
136
|
+
fromMe = isParticipantMe;
|
|
137
|
+
chatId = from;
|
|
138
|
+
author = participant;
|
|
139
|
+
} else if (isJidNewsletter(from)) {
|
|
140
|
+
msgType = "newsletter";
|
|
141
|
+
chatId = from;
|
|
142
|
+
author = from;
|
|
143
|
+
if (isMe(from) || isMeLid(from)) {
|
|
144
|
+
fromMe = true;
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
throw new Boom("Unknown message type", { data: stanza });
|
|
148
|
+
}
|
|
149
|
+
const pushname = stanza?.attrs?.notify;
|
|
150
|
+
const key = {
|
|
151
|
+
remoteJid: chatId,
|
|
152
|
+
remoteJidAlt: !isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
|
|
153
|
+
fromMe: fromMe,
|
|
154
|
+
id: msgId,
|
|
155
|
+
participant: participant,
|
|
156
|
+
participantAlt: isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
|
|
157
|
+
addressingMode: addressingContext.addressingMode,
|
|
158
|
+
...(msgType === "newsletter" && stanza.attrs.server_id
|
|
159
|
+
? { server_id: stanza.attrs.server_id }
|
|
160
|
+
: {}),
|
|
161
|
+
};
|
|
162
|
+
const fullMessage = {
|
|
163
|
+
key: key,
|
|
164
|
+
category: stanza.attrs.category,
|
|
165
|
+
messageTimestamp: +stanza.attrs.t,
|
|
166
|
+
pushName: pushname,
|
|
167
|
+
platform: getDevice(key.id),
|
|
168
|
+
broadcast: isJidBroadcast(from),
|
|
169
|
+
newsletter: isJidNewsletter(from),
|
|
170
|
+
};
|
|
171
|
+
if (key.fromMe) {
|
|
172
|
+
fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK;
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
fullMessage: fullMessage,
|
|
176
|
+
author: author,
|
|
177
|
+
sender: msgType === "chat" ? author : chatId,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
181
|
+
const {
|
|
182
|
+
fullMessage: fullMessage,
|
|
183
|
+
author: author,
|
|
184
|
+
sender: sender,
|
|
185
|
+
} = decodeMessageNode(stanza, meId, meLid);
|
|
186
|
+
return {
|
|
187
|
+
fullMessage: fullMessage,
|
|
188
|
+
category: stanza.attrs.category,
|
|
189
|
+
author: author,
|
|
190
|
+
async decrypt() {
|
|
191
|
+
let decryptables = 0;
|
|
192
|
+
if (Array.isArray(stanza.content)) {
|
|
193
|
+
for (const { tag: tag, attrs: attrs, content: content } of stanza.content) {
|
|
194
|
+
if (tag === "verified_name" && content instanceof Uint8Array) {
|
|
195
|
+
const cert = proto.VerifiedNameCertificate.decode(content);
|
|
196
|
+
const details = proto.VerifiedNameCertificate.Details.decode(cert.details);
|
|
197
|
+
fullMessage.verifiedBizName = details.verifiedName;
|
|
198
|
+
}
|
|
199
|
+
if (tag === "unavailable" && attrs.type === "view_once") {
|
|
200
|
+
fullMessage.key.isViewOnce = true;
|
|
201
|
+
}
|
|
202
|
+
if (attrs.count && tag === "enc") {
|
|
203
|
+
fullMessage.retryCount = Number(attrs.count);
|
|
204
|
+
}
|
|
205
|
+
if (tag !== "enc" && tag !== "plaintext") {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (!(content instanceof Uint8Array)) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
decryptables += 1;
|
|
212
|
+
let msgBuffer;
|
|
213
|
+
const decryptionJid = await getDecryptionJid(author, repository);
|
|
214
|
+
if (tag !== "plaintext") {
|
|
215
|
+
await storeMappingFromEnvelope(
|
|
216
|
+
stanza,
|
|
217
|
+
author,
|
|
218
|
+
repository,
|
|
219
|
+
decryptionJid,
|
|
220
|
+
logger
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
try {
|
|
224
|
+
const e2eType = tag === "plaintext" ? "plaintext" : attrs.type;
|
|
225
|
+
switch (e2eType) {
|
|
226
|
+
case "skmsg":
|
|
227
|
+
msgBuffer = await repository.decryptGroupMessage({
|
|
228
|
+
group: sender,
|
|
229
|
+
authorJid: author,
|
|
230
|
+
msg: content,
|
|
231
|
+
});
|
|
232
|
+
break;
|
|
233
|
+
case "pkmsg":
|
|
234
|
+
case "msg":
|
|
235
|
+
msgBuffer = await repository.decryptMessage({
|
|
236
|
+
jid: decryptionJid,
|
|
237
|
+
type: e2eType,
|
|
238
|
+
ciphertext: content,
|
|
239
|
+
});
|
|
240
|
+
break;
|
|
241
|
+
case "plaintext":
|
|
242
|
+
msgBuffer = content;
|
|
243
|
+
break;
|
|
244
|
+
default:
|
|
245
|
+
throw new Error(`Unknown e2e type: ${e2eType}`);
|
|
246
|
+
}
|
|
247
|
+
let msg = proto.Message.decode(
|
|
248
|
+
e2eType !== "plaintext" ? unpadRandomMax16(msgBuffer) : msgBuffer
|
|
249
|
+
);
|
|
250
|
+
msg = msg.deviceSentMessage?.message || msg;
|
|
251
|
+
if (msg.senderKeyDistributionMessage) {
|
|
252
|
+
try {
|
|
253
|
+
await repository.processSenderKeyDistributionMessage({
|
|
254
|
+
authorJid: author,
|
|
255
|
+
item: msg.senderKeyDistributionMessage,
|
|
256
|
+
});
|
|
257
|
+
} catch (err) {
|
|
258
|
+
logger.error(
|
|
259
|
+
{ key: fullMessage.key, err: err },
|
|
260
|
+
"failed to process sender key distribution message"
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (fullMessage.message) {
|
|
265
|
+
Object.assign(fullMessage.message, msg);
|
|
266
|
+
} else {
|
|
267
|
+
fullMessage.message = msg;
|
|
268
|
+
}
|
|
269
|
+
} catch (err) {
|
|
270
|
+
const errorContext = {
|
|
271
|
+
key: fullMessage.key,
|
|
272
|
+
err: err,
|
|
273
|
+
messageType: tag === "plaintext" ? "plaintext" : attrs.type,
|
|
274
|
+
sender: sender,
|
|
275
|
+
author: author,
|
|
276
|
+
isSessionRecordError: isSessionRecordError(err),
|
|
277
|
+
};
|
|
278
|
+
logger.error(errorContext, "failed to decrypt message");
|
|
279
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
280
|
+
fullMessage.messageStubParameters = [err.message.toString()];
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (!decryptables && !fullMessage.key?.isViewOnce) {
|
|
285
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
286
|
+
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT];
|
|
287
|
+
}
|
|
288
|
+
},
|
|
289
|
+
};
|
|
290
|
+
};
|
|
291
|
+
function isSessionRecordError(error) {
|
|
292
|
+
const errorMessage = error?.message || error?.toString() || "";
|
|
293
|
+
return DECRYPTION_RETRY_CONFIG.sessionRecordErrors.some((errorPattern) =>
|
|
294
|
+
errorMessage.includes(errorPattern)
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
module.exports = {
|
|
298
|
+
NACK_REASONS: NACK_REASONS,
|
|
299
|
+
decodeMessageNode: decodeMessageNode,
|
|
300
|
+
decryptMessageNode: decryptMessageNode,
|
|
301
|
+
extractAddressingContext: extractAddressingContext,
|
|
302
|
+
MISSING_KEYS_ERROR_TEXT: MISSING_KEYS_ERROR_TEXT,
|
|
303
|
+
DECRYPTION_RETRY_CONFIG: DECRYPTION_RETRY_CONFIG,
|
|
304
|
+
NO_MESSAGE_FOUND_ERROR_TEXT: NO_MESSAGE_FOUND_ERROR_TEXT,
|
|
305
|
+
};
|