amiudmodz 4.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/WAProto/index.js +169661 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.js +156 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Signal/Group/ciphertext-message.js +15 -0
- package/lib/Signal/Group/group-session-builder.js +64 -0
- package/lib/Signal/Group/group_cipher.js +96 -0
- package/lib/Signal/Group/index.js +57 -0
- package/lib/Signal/Group/keyhelper.js +55 -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 +69 -0
- package/lib/Signal/Group/sender-key-name.js +51 -0
- package/lib/Signal/Group/sender-key-record.js +53 -0
- package/lib/Signal/Group/sender-key-state.js +99 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/libsignal.js +177 -0
- package/lib/Signal/lid-mapping.js +185 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.js +19 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/web-socket-client.js +66 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.js +1066 -0
- package/lib/Socket/groupStatus.js +637 -0
- package/lib/Socket/groups.js +317 -0
- package/lib/Socket/index.js +11 -0
- package/lib/Socket/messages-recv.js +1112 -0
- package/lib/Socket/messages-send.js +807 -0
- package/lib/Socket/newsletter.js +73 -0
- package/lib/Socket/registration.js +166 -0
- package/lib/Socket/socket.js +729 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.js +10 -0
- package/lib/Store/make-cache-manager-store.js +83 -0
- package/lib/Store/make-in-memory-store.js +427 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +4 -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 +27 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.js +9 -0
- package/lib/Types/Newsletter.js +38 -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 +2 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +42 -0
- package/lib/Utils/auth-utils.js +206 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.js +729 -0
- package/lib/Utils/crypto.js +151 -0
- package/lib/Utils/decode-wa-message.js +198 -0
- package/lib/Utils/event-buffer.js +525 -0
- package/lib/Utils/generics.js +502 -0
- package/lib/Utils/history.js +96 -0
- package/lib/Utils/index.js +33 -0
- package/lib/Utils/link-preview.js +93 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.js +43 -0
- package/lib/Utils/messages-media.js +814 -0
- package/lib/Utils/messages.js +819 -0
- package/lib/Utils/noise-handler.js +155 -0
- package/lib/Utils/process-message.js +321 -0
- package/lib/Utils/signal.js +153 -0
- package/lib/Utils/use-multi-file-auth-state.js +126 -0
- package/lib/Utils/validate-connection.js +222 -0
- package/lib/WABinary/constants.js +1304 -0
- package/lib/WABinary/decode.js +283 -0
- package/lib/WABinary/encode.js +263 -0
- package/lib/WABinary/generic-utils.js +198 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.js +62 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.js +89 -0
- package/lib/WAUSync/USyncUser.js +26 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.js +39 -0
- package/package.json +93 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeNoiseHandler = void 0;
|
|
4
|
+
const boom_1 = require("@hapi/boom");
|
|
5
|
+
const WAProto_1 = require("../../WAProto");
|
|
6
|
+
const Defaults_1 = require("../Defaults");
|
|
7
|
+
const WABinary_1 = require("../WABinary");
|
|
8
|
+
const crypto_1 = require("./crypto");
|
|
9
|
+
const generateIV = (counter) => {
|
|
10
|
+
const iv = new ArrayBuffer(12);
|
|
11
|
+
new DataView(iv).setUint32(8, counter);
|
|
12
|
+
return new Uint8Array(iv);
|
|
13
|
+
};
|
|
14
|
+
const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey }, NOISE_HEADER, mobile, logger, routingInfo }) => {
|
|
15
|
+
logger = logger.child({ class: 'ns' });
|
|
16
|
+
const authenticate = (data) => {
|
|
17
|
+
if (!isFinished) {
|
|
18
|
+
hash = (0, crypto_1.sha256)(Buffer.concat([hash, data]));
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const encrypt = (plaintext) => {
|
|
22
|
+
const result = (0, crypto_1.aesEncryptGCM)(plaintext, encKey, generateIV(writeCounter), hash);
|
|
23
|
+
writeCounter += 1;
|
|
24
|
+
authenticate(result);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
const decrypt = (ciphertext) => {
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
const iv = generateIV(isFinished ? readCounter : writeCounter);
|
|
31
|
+
const result = (0, crypto_1.aesDecryptGCM)(ciphertext, decKey, iv, hash);
|
|
32
|
+
if (isFinished) {
|
|
33
|
+
readCounter += 1;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
writeCounter += 1;
|
|
37
|
+
}
|
|
38
|
+
authenticate(ciphertext);
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
41
|
+
const localHKDF = (data) => {
|
|
42
|
+
const key = (0, crypto_1.hkdf)(Buffer.from(data), 64, { salt, info: '' });
|
|
43
|
+
return [key.slice(0, 32), key.slice(32)];
|
|
44
|
+
};
|
|
45
|
+
const mixIntoKey = (data) => {
|
|
46
|
+
const [write, read] = localHKDF(data);
|
|
47
|
+
salt = write;
|
|
48
|
+
encKey = read;
|
|
49
|
+
decKey = read;
|
|
50
|
+
readCounter = 0;
|
|
51
|
+
writeCounter = 0;
|
|
52
|
+
};
|
|
53
|
+
const finishInit = () => {
|
|
54
|
+
const [write, read] = localHKDF(new Uint8Array(0));
|
|
55
|
+
encKey = write;
|
|
56
|
+
decKey = read;
|
|
57
|
+
hash = Buffer.from([]);
|
|
58
|
+
readCounter = 0;
|
|
59
|
+
writeCounter = 0;
|
|
60
|
+
isFinished = true;
|
|
61
|
+
};
|
|
62
|
+
const data = Buffer.from(Defaults_1.NOISE_MODE);
|
|
63
|
+
let hash = Buffer.from(data.byteLength === 32 ? data : (0, crypto_1.sha256)(data));
|
|
64
|
+
let salt = hash;
|
|
65
|
+
let encKey = hash;
|
|
66
|
+
let decKey = hash;
|
|
67
|
+
let readCounter = 0;
|
|
68
|
+
let writeCounter = 0;
|
|
69
|
+
let isFinished = false;
|
|
70
|
+
let sentIntro = false;
|
|
71
|
+
let inBytes = Buffer.alloc(0);
|
|
72
|
+
authenticate(NOISE_HEADER);
|
|
73
|
+
authenticate(publicKey);
|
|
74
|
+
return {
|
|
75
|
+
encrypt,
|
|
76
|
+
decrypt,
|
|
77
|
+
authenticate,
|
|
78
|
+
mixIntoKey,
|
|
79
|
+
finishInit,
|
|
80
|
+
processHandshake: ({ serverHello }, noiseKey) => {
|
|
81
|
+
authenticate(serverHello.ephemeral);
|
|
82
|
+
mixIntoKey(crypto_1.Curve.sharedKey(privateKey, serverHello.ephemeral));
|
|
83
|
+
const decStaticContent = decrypt(serverHello.static);
|
|
84
|
+
mixIntoKey(crypto_1.Curve.sharedKey(privateKey, decStaticContent));
|
|
85
|
+
const certDecoded = decrypt(serverHello.payload);
|
|
86
|
+
if (mobile) {
|
|
87
|
+
WAProto_1.proto.CertChain.NoiseCertificate.decode(certDecoded);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const { intermediate: certIntermediate } = WAProto_1.proto.CertChain.decode(certDecoded);
|
|
91
|
+
const { issuerSerial } = WAProto_1.proto.CertChain.NoiseCertificate.Details.decode(certIntermediate.details);
|
|
92
|
+
if (issuerSerial !== Defaults_1.WA_CERT_DETAILS.SERIAL) {
|
|
93
|
+
throw new boom_1.Boom('certification match failed', { statusCode: 400 });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const keyEnc = encrypt(noiseKey.public);
|
|
97
|
+
mixIntoKey(crypto_1.Curve.sharedKey(noiseKey.private, serverHello.ephemeral));
|
|
98
|
+
return keyEnc;
|
|
99
|
+
},
|
|
100
|
+
encodeFrame: (data) => {
|
|
101
|
+
if (isFinished) {
|
|
102
|
+
data = encrypt(data);
|
|
103
|
+
}
|
|
104
|
+
let header;
|
|
105
|
+
if (routingInfo) {
|
|
106
|
+
header = Buffer.alloc(7);
|
|
107
|
+
header.write('ED', 0, 'utf8');
|
|
108
|
+
header.writeUint8(0, 2);
|
|
109
|
+
header.writeUint8(1, 3);
|
|
110
|
+
header.writeUint8(routingInfo.byteLength >> 16, 4);
|
|
111
|
+
header.writeUint16BE(routingInfo.byteLength & 65535, 5);
|
|
112
|
+
header = Buffer.concat([header, routingInfo, NOISE_HEADER]);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
header = Buffer.from(NOISE_HEADER);
|
|
116
|
+
}
|
|
117
|
+
const introSize = sentIntro ? 0 : header.length;
|
|
118
|
+
const frame = Buffer.alloc(introSize + 3 + data.byteLength);
|
|
119
|
+
if (!sentIntro) {
|
|
120
|
+
frame.set(header);
|
|
121
|
+
sentIntro = true;
|
|
122
|
+
}
|
|
123
|
+
frame.writeUInt8(data.byteLength >> 16, introSize);
|
|
124
|
+
frame.writeUInt16BE(65535 & data.byteLength, introSize + 1);
|
|
125
|
+
frame.set(data, introSize + 3);
|
|
126
|
+
return frame;
|
|
127
|
+
},
|
|
128
|
+
decodeFrame: (newData, onFrame) => {
|
|
129
|
+
var _a;
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
const getBytesSize = () => {
|
|
134
|
+
if (inBytes.length >= 3) {
|
|
135
|
+
return (inBytes.readUInt8() << 16) | inBytes.readUInt16BE(1);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
inBytes = Buffer.concat([inBytes, newData]);
|
|
139
|
+
logger.trace(`recv ${newData.length} bytes, total recv ${inBytes.length} bytes`);
|
|
140
|
+
let size = getBytesSize();
|
|
141
|
+
while (size && inBytes.length >= size + 3) {
|
|
142
|
+
let frame = inBytes.slice(3, size + 3);
|
|
143
|
+
inBytes = inBytes.slice(size + 3);
|
|
144
|
+
if (isFinished) {
|
|
145
|
+
const result = decrypt(frame);
|
|
146
|
+
frame = (0, WABinary_1.decodeBinaryNode)(result);
|
|
147
|
+
}
|
|
148
|
+
logger.trace({ msg: (_a = frame === null || frame === void 0 ? void 0 : frame.attrs) === null || _a === void 0 ? void 0 : _a.id }, 'recv frame');
|
|
149
|
+
onFrame(frame);
|
|
150
|
+
size = getBytesSize();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
exports.makeNoiseHandler = makeNoiseHandler;
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decryptPollVote = exports.getChatId = exports.shouldIncrementChatUnread = exports.isRealMessage = exports.cleanMessage = void 0;
|
|
4
|
+
const WAProto_1 = require("../../WAProto");
|
|
5
|
+
const Types_1 = require("../Types");
|
|
6
|
+
const messages_1 = require("../Utils/messages");
|
|
7
|
+
const WABinary_1 = require("../WABinary");
|
|
8
|
+
const crypto_1 = require("./crypto");
|
|
9
|
+
const generics_1 = require("./generics");
|
|
10
|
+
const history_1 = require("./history");
|
|
11
|
+
const REAL_MSG_STUB_TYPES = new Set([
|
|
12
|
+
Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
|
|
13
|
+
Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE,
|
|
14
|
+
Types_1.WAMessageStubType.CALL_MISSED_VIDEO,
|
|
15
|
+
Types_1.WAMessageStubType.CALL_MISSED_VOICE
|
|
16
|
+
]);
|
|
17
|
+
const REAL_MSG_REQ_ME_STUB_TYPES = new Set([
|
|
18
|
+
Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD
|
|
19
|
+
]);
|
|
20
|
+
/** Cleans a received message to further processing */
|
|
21
|
+
const cleanMessage = (message, meId) => {
|
|
22
|
+
|
|
23
|
+
message.key.remoteJid = (0, WABinary_1.jidNormalizedUser)(message.key.remoteJid);
|
|
24
|
+
message.key.participant = message.key.participant ? (0, WABinary_1.jidNormalizedUser)(message.key.participant) : undefined;
|
|
25
|
+
const content = (0, messages_1.normalizeMessageContent)(message.message);
|
|
26
|
+
|
|
27
|
+
if (content === null || content === void 0 ? void 0 : content.reactionMessage) {
|
|
28
|
+
normaliseKey(content.reactionMessage.key);
|
|
29
|
+
}
|
|
30
|
+
if (content === null || content === void 0 ? void 0 : content.pollUpdateMessage) {
|
|
31
|
+
normaliseKey(content.pollUpdateMessage.pollCreationMessageKey);
|
|
32
|
+
}
|
|
33
|
+
function normaliseKey(msgKey) {
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
if (!message.key.fromMe) {
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
msgKey.fromMe = !msgKey.fromMe
|
|
40
|
+
? (0, WABinary_1.areJidsSameUser)(msgKey.participant || msgKey.remoteJid, meId)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
: false;
|
|
44
|
+
|
|
45
|
+
msgKey.remoteJid = message.key.remoteJid;
|
|
46
|
+
|
|
47
|
+
msgKey.participant = msgKey.participant || message.key.participant;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
exports.cleanMessage = cleanMessage;
|
|
52
|
+
const isRealMessage = (message, meId) => {
|
|
53
|
+
var _a;
|
|
54
|
+
const normalizedContent = (0, messages_1.normalizeMessageContent)(message.message);
|
|
55
|
+
const hasSomeContent = !!(0, messages_1.getContentType)(normalizedContent);
|
|
56
|
+
return (!!normalizedContent
|
|
57
|
+
|| REAL_MSG_STUB_TYPES.has(message.messageStubType)
|
|
58
|
+
|| (REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType)
|
|
59
|
+
&& ((_a = message.messageStubParameters) === null || _a === void 0 ? void 0 : _a.some(p => (0, WABinary_1.areJidsSameUser)(meId, p)))))
|
|
60
|
+
&& hasSomeContent
|
|
61
|
+
&& !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.protocolMessage)
|
|
62
|
+
&& !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.reactionMessage)
|
|
63
|
+
&& !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.pollUpdateMessage);
|
|
64
|
+
};
|
|
65
|
+
exports.isRealMessage = isRealMessage;
|
|
66
|
+
const shouldIncrementChatUnread = (message) => (!message.key.fromMe && !message.messageStubType);
|
|
67
|
+
exports.shouldIncrementChatUnread = shouldIncrementChatUnread;
|
|
68
|
+
/**
|
|
69
|
+
* Get the ID of the chat from the given key.
|
|
70
|
+
* Typically -- that'll be the remoteJid, but for broadcasts, it'll be the participant
|
|
71
|
+
*/
|
|
72
|
+
const getChatId = ({ remoteJid, participant, fromMe }) => {
|
|
73
|
+
if ((0, WABinary_1.isJidBroadcast)(remoteJid)
|
|
74
|
+
&& !(0, WABinary_1.isJidStatusBroadcast)(remoteJid)
|
|
75
|
+
&& !fromMe) {
|
|
76
|
+
return participant;
|
|
77
|
+
}
|
|
78
|
+
return remoteJid;
|
|
79
|
+
};
|
|
80
|
+
exports.getChatId = getChatId;
|
|
81
|
+
/**
|
|
82
|
+
* Decrypt a poll vote
|
|
83
|
+
* @param vote encrypted vote
|
|
84
|
+
* @param ctx additional info about the poll required for decryption
|
|
85
|
+
* @returns list of SHA256 options
|
|
86
|
+
*/
|
|
87
|
+
function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid, }) {
|
|
88
|
+
const sign = Buffer.concat([
|
|
89
|
+
toBinary(pollMsgId),
|
|
90
|
+
toBinary(pollCreatorJid),
|
|
91
|
+
toBinary(voterJid),
|
|
92
|
+
toBinary('Poll Vote'),
|
|
93
|
+
new Uint8Array([1])
|
|
94
|
+
]);
|
|
95
|
+
const key0 = (0, crypto_1.hmacSign)(pollEncKey, new Uint8Array(32), 'sha256');
|
|
96
|
+
const decKey = (0, crypto_1.hmacSign)(sign, key0, 'sha256');
|
|
97
|
+
const aad = toBinary(`${pollMsgId}\u0000${voterJid}`);
|
|
98
|
+
const decrypted = (0, crypto_1.aesDecryptGCM)(encPayload, decKey, encIv, aad);
|
|
99
|
+
return WAProto_1.proto.Message.PollVoteMessage.decode(decrypted);
|
|
100
|
+
function toBinary(txt) {
|
|
101
|
+
return Buffer.from(txt);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.decryptPollVote = decryptPollVote;
|
|
105
|
+
const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, keyStore, logger, options, getMessage }) => {
|
|
106
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
107
|
+
const meId = creds.me.id;
|
|
108
|
+
const { accountSettings } = creds;
|
|
109
|
+
const chat = { id: (0, WABinary_1.jidNormalizedUser)((0, exports.getChatId)(message.key)) };
|
|
110
|
+
const isRealMsg = (0, exports.isRealMessage)(message, meId);
|
|
111
|
+
if (isRealMsg) {
|
|
112
|
+
chat.conversationTimestamp = (0, generics_1.toNumber)(message.messageTimestamp);
|
|
113
|
+
|
|
114
|
+
if ((0, exports.shouldIncrementChatUnread)(message)) {
|
|
115
|
+
chat.unreadCount = (chat.unreadCount || 0) + 1;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const content = (0, messages_1.normalizeMessageContent)(message.message);
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
if ((isRealMsg || ((_b = (_a = content === null || content === void 0 ? void 0 : content.reactionMessage) === null || _a === void 0 ? void 0 : _a.key) === null || _b === void 0 ? void 0 : _b.fromMe))
|
|
122
|
+
&& (accountSettings === null || accountSettings === void 0 ? void 0 : accountSettings.unarchiveChats)) {
|
|
123
|
+
chat.archived = false;
|
|
124
|
+
chat.readOnly = false;
|
|
125
|
+
}
|
|
126
|
+
const protocolMsg = content === null || content === void 0 ? void 0 : content.protocolMessage;
|
|
127
|
+
if (protocolMsg) {
|
|
128
|
+
switch (protocolMsg.type) {
|
|
129
|
+
case WAProto_1.proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
|
|
130
|
+
const histNotification = protocolMsg.historySyncNotification;
|
|
131
|
+
const process = shouldProcessHistoryMsg;
|
|
132
|
+
const isLatest = !((_c = creds.processedHistoryMessages) === null || _c === void 0 ? void 0 : _c.length);
|
|
133
|
+
logger === null || logger === void 0 ? void 0 : logger.info({
|
|
134
|
+
histNotification,
|
|
135
|
+
process,
|
|
136
|
+
id: message.key.id,
|
|
137
|
+
isLatest,
|
|
138
|
+
}, 'got history notification');
|
|
139
|
+
if (process) {
|
|
140
|
+
ev.emit('creds.update', {
|
|
141
|
+
processedHistoryMessages: [
|
|
142
|
+
...(creds.processedHistoryMessages || []),
|
|
143
|
+
{ key: message.key, messageTimestamp: message.messageTimestamp }
|
|
144
|
+
]
|
|
145
|
+
});
|
|
146
|
+
const data = await (0, history_1.downloadAndProcessHistorySyncNotification)(histNotification, options);
|
|
147
|
+
ev.emit('messaging-history.set', { ...data, isLatest });
|
|
148
|
+
}
|
|
149
|
+
break;
|
|
150
|
+
case WAProto_1.proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
|
|
151
|
+
const keys = protocolMsg.appStateSyncKeyShare.keys;
|
|
152
|
+
if (keys === null || keys === void 0 ? void 0 : keys.length) {
|
|
153
|
+
let newAppStateSyncKeyId = '';
|
|
154
|
+
await keyStore.transaction(async () => {
|
|
155
|
+
const newKeys = [];
|
|
156
|
+
for (const { keyData, keyId } of keys) {
|
|
157
|
+
const strKeyId = Buffer.from(keyId.keyId).toString('base64');
|
|
158
|
+
newKeys.push(strKeyId);
|
|
159
|
+
await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } });
|
|
160
|
+
newAppStateSyncKeyId = strKeyId;
|
|
161
|
+
}
|
|
162
|
+
logger === null || logger === void 0 ? void 0 : logger.info({ newAppStateSyncKeyId, newKeys }, 'injecting new app state sync keys');
|
|
163
|
+
});
|
|
164
|
+
ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId });
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
logger === null || logger === void 0 ? void 0 : logger.info({ protocolMsg }, 'recv app state sync with 0 keys');
|
|
168
|
+
}
|
|
169
|
+
break;
|
|
170
|
+
case WAProto_1.proto.Message.ProtocolMessage.Type.REVOKE:
|
|
171
|
+
ev.emit('messages.update', [
|
|
172
|
+
{
|
|
173
|
+
key: {
|
|
174
|
+
...message.key,
|
|
175
|
+
id: protocolMsg.key.id
|
|
176
|
+
},
|
|
177
|
+
update: { message: null, messageStubType: Types_1.WAMessageStubType.REVOKE, key: message.key }
|
|
178
|
+
}
|
|
179
|
+
]);
|
|
180
|
+
break;
|
|
181
|
+
case WAProto_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
|
|
182
|
+
Object.assign(chat, {
|
|
183
|
+
ephemeralSettingTimestamp: (0, generics_1.toNumber)(message.messageTimestamp),
|
|
184
|
+
ephemeralExpiration: protocolMsg.ephemeralExpiration || null
|
|
185
|
+
});
|
|
186
|
+
break;
|
|
187
|
+
case WAProto_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
|
|
188
|
+
const response = protocolMsg.peerDataOperationRequestResponseMessage;
|
|
189
|
+
if (response) {
|
|
190
|
+
const { peerDataOperationResult } = response;
|
|
191
|
+
for (const result of peerDataOperationResult) {
|
|
192
|
+
const { placeholderMessageResendResponse: retryResponse } = result;
|
|
193
|
+
if (retryResponse) {
|
|
194
|
+
const webMessageInfo = WAProto_1.proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes);
|
|
195
|
+
ev.emit('messages.update', [
|
|
196
|
+
{ key: webMessageInfo.key, update: { message: webMessageInfo.message } }
|
|
197
|
+
]);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else if (content === null || content === void 0 ? void 0 : content.reactionMessage) {
|
|
205
|
+
const reaction = {
|
|
206
|
+
...content.reactionMessage,
|
|
207
|
+
key: message.key,
|
|
208
|
+
};
|
|
209
|
+
ev.emit('messages.reaction', [{
|
|
210
|
+
reaction,
|
|
211
|
+
key: content.reactionMessage.key,
|
|
212
|
+
}]);
|
|
213
|
+
}
|
|
214
|
+
else if (message.messageStubType) {
|
|
215
|
+
const jid = message.key.remoteJid;
|
|
216
|
+
//let actor = whatsappID (message.participant)
|
|
217
|
+
let participants;
|
|
218
|
+
const emitParticipantsUpdate = (action) => (ev.emit('group-participants.update', { id: jid, author: message.participant, participants, action }));
|
|
219
|
+
const emitGroupUpdate = (update) => {
|
|
220
|
+
var _a;
|
|
221
|
+
ev.emit('groups.update', [{ id: jid, ...update, author: (_a = message.participant) !== null && _a !== void 0 ? _a : undefined }]);
|
|
222
|
+
};
|
|
223
|
+
const participantsIncludesMe = () => participants.find(jid => (0, WABinary_1.areJidsSameUser)(meId, jid));
|
|
224
|
+
switch (message.messageStubType) {
|
|
225
|
+
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
|
|
226
|
+
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
|
|
227
|
+
participants = message.messageStubParameters || [];
|
|
228
|
+
emitParticipantsUpdate('remove');
|
|
229
|
+
|
|
230
|
+
if (participantsIncludesMe()) {
|
|
231
|
+
chat.readOnly = true;
|
|
232
|
+
}
|
|
233
|
+
break;
|
|
234
|
+
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD:
|
|
235
|
+
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE:
|
|
236
|
+
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
|
|
237
|
+
participants = message.messageStubParameters || [];
|
|
238
|
+
if (participantsIncludesMe()) {
|
|
239
|
+
chat.readOnly = false;
|
|
240
|
+
}
|
|
241
|
+
emitParticipantsUpdate('add');
|
|
242
|
+
break;
|
|
243
|
+
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
|
|
244
|
+
participants = message.messageStubParameters || [];
|
|
245
|
+
emitParticipantsUpdate('demote');
|
|
246
|
+
break;
|
|
247
|
+
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
|
|
248
|
+
participants = message.messageStubParameters || [];
|
|
249
|
+
emitParticipantsUpdate('promote');
|
|
250
|
+
break;
|
|
251
|
+
case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
|
|
252
|
+
const announceValue = (_d = message.messageStubParameters) === null || _d === void 0 ? void 0 : _d[0];
|
|
253
|
+
emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' });
|
|
254
|
+
break;
|
|
255
|
+
case Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT:
|
|
256
|
+
const restrictValue = (_e = message.messageStubParameters) === null || _e === void 0 ? void 0 : _e[0];
|
|
257
|
+
emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' });
|
|
258
|
+
break;
|
|
259
|
+
case Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT:
|
|
260
|
+
const name = (_f = message.messageStubParameters) === null || _f === void 0 ? void 0 : _f[0];
|
|
261
|
+
chat.name = name;
|
|
262
|
+
emitGroupUpdate({ subject: name });
|
|
263
|
+
break;
|
|
264
|
+
case Types_1.WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
|
|
265
|
+
const code = (_g = message.messageStubParameters) === null || _g === void 0 ? void 0 : _g[0];
|
|
266
|
+
emitGroupUpdate({ inviteCode: code });
|
|
267
|
+
break;
|
|
268
|
+
case Types_1.WAMessageStubType.GROUP_MEMBER_ADD_MODE:
|
|
269
|
+
const memberAddValue = (_h = message.messageStubParameters) === null || _h === void 0 ? void 0 : _h[0];
|
|
270
|
+
emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' });
|
|
271
|
+
break;
|
|
272
|
+
case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
|
|
273
|
+
const approvalMode = (_j = message.messageStubParameters) === null || _j === void 0 ? void 0 : _j[0];
|
|
274
|
+
emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' });
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
else if (content === null || content === void 0 ? void 0 : content.pollUpdateMessage) {
|
|
279
|
+
const creationMsgKey = content.pollUpdateMessage.pollCreationMessageKey;
|
|
280
|
+
|
|
281
|
+
const pollMsg = await getMessage(creationMsgKey);
|
|
282
|
+
if (pollMsg) {
|
|
283
|
+
const meIdNormalised = (0, WABinary_1.jidNormalizedUser)(meId);
|
|
284
|
+
const pollCreatorJid = (0, generics_1.getKeyAuthor)(creationMsgKey, meIdNormalised);
|
|
285
|
+
const voterJid = (0, generics_1.getKeyAuthor)(message.key, meIdNormalised);
|
|
286
|
+
const pollEncKey = (_k = pollMsg.messageContextInfo) === null || _k === void 0 ? void 0 : _k.messageSecret;
|
|
287
|
+
try {
|
|
288
|
+
const voteMsg = decryptPollVote(content.pollUpdateMessage.vote, {
|
|
289
|
+
pollEncKey,
|
|
290
|
+
pollCreatorJid,
|
|
291
|
+
pollMsgId: creationMsgKey.id,
|
|
292
|
+
voterJid,
|
|
293
|
+
});
|
|
294
|
+
ev.emit('messages.update', [
|
|
295
|
+
{
|
|
296
|
+
key: creationMsgKey,
|
|
297
|
+
update: {
|
|
298
|
+
pollUpdates: [
|
|
299
|
+
{
|
|
300
|
+
pollUpdateMessageKey: message.key,
|
|
301
|
+
vote: voteMsg,
|
|
302
|
+
senderTimestampMs: content.pollUpdateMessage.senderTimestampMs.toNumber(),
|
|
303
|
+
}
|
|
304
|
+
]
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
]);
|
|
308
|
+
}
|
|
309
|
+
catch (err) {
|
|
310
|
+
logger === null || logger === void 0 ? void 0 : logger.warn({ err, creationMsgKey }, 'failed to decrypt poll vote');
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
logger === null || logger === void 0 ? void 0 : logger.warn({ creationMsgKey }, 'poll creation message not found, cannot decrypt update');
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (Object.keys(chat).length > 1) {
|
|
318
|
+
ev.emit('chats.update', [chat]);
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
exports.default = processMessage;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getNextPreKeysNode = exports.getNextPreKeys = exports.extractDeviceJids = exports.parseAndInjectE2ESessions = exports.xmppPreKey = exports.xmppSignedPreKey = exports.generateOrGetPreKeys = exports.getPreKeys = exports.createSignalIdentity = void 0;
|
|
4
|
+
const Defaults_1 = require("../Defaults");
|
|
5
|
+
const WABinary_1 = require("../WABinary");
|
|
6
|
+
const crypto_1 = require("./crypto");
|
|
7
|
+
const lodash_1 = require("lodash")
|
|
8
|
+
const generics_1 = require("./generics");
|
|
9
|
+
const createSignalIdentity = (wid, accountSignatureKey) => {
|
|
10
|
+
return {
|
|
11
|
+
identifier: { name: wid, deviceId: 0 },
|
|
12
|
+
identifierKey: (0, crypto_1.generateSignalPubKey)(accountSignatureKey)
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
exports.createSignalIdentity = createSignalIdentity;
|
|
16
|
+
const getPreKeys = async ({ get }, min, limit) => {
|
|
17
|
+
const idList = [];
|
|
18
|
+
for (let id = min; id < limit; id++) {
|
|
19
|
+
idList.push(id.toString());
|
|
20
|
+
}
|
|
21
|
+
return get('pre-key', idList);
|
|
22
|
+
};
|
|
23
|
+
exports.getPreKeys = getPreKeys;
|
|
24
|
+
const generateOrGetPreKeys = (creds, range) => {
|
|
25
|
+
const avaliable = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId;
|
|
26
|
+
const remaining = range - avaliable;
|
|
27
|
+
const lastPreKeyId = creds.nextPreKeyId + remaining - 1;
|
|
28
|
+
const newPreKeys = {};
|
|
29
|
+
if (remaining > 0) {
|
|
30
|
+
for (let i = creds.nextPreKeyId; i <= lastPreKeyId; i++) {
|
|
31
|
+
newPreKeys[i] = crypto_1.Curve.generateKeyPair();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
newPreKeys,
|
|
36
|
+
lastPreKeyId,
|
|
37
|
+
preKeysRange: [creds.firstUnuploadedPreKeyId, range],
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
exports.generateOrGetPreKeys = generateOrGetPreKeys;
|
|
41
|
+
const xmppSignedPreKey = (key) => ({
|
|
42
|
+
tag: 'skey',
|
|
43
|
+
attrs: {},
|
|
44
|
+
content: [
|
|
45
|
+
{ tag: 'id', attrs: {}, content: (0, generics_1.encodeBigEndian)(key.keyId, 3) },
|
|
46
|
+
{ tag: 'value', attrs: {}, content: key.keyPair.public },
|
|
47
|
+
{ tag: 'signature', attrs: {}, content: key.signature }
|
|
48
|
+
]
|
|
49
|
+
});
|
|
50
|
+
exports.xmppSignedPreKey = xmppSignedPreKey;
|
|
51
|
+
const xmppPreKey = (pair, id) => ({
|
|
52
|
+
tag: 'key',
|
|
53
|
+
attrs: {},
|
|
54
|
+
content: [
|
|
55
|
+
{ tag: 'id', attrs: {}, content: (0, generics_1.encodeBigEndian)(id, 3) },
|
|
56
|
+
{ tag: 'value', attrs: {}, content: pair.public }
|
|
57
|
+
]
|
|
58
|
+
});
|
|
59
|
+
exports.xmppPreKey = xmppPreKey;
|
|
60
|
+
const parseAndInjectE2ESessions = async (node, repository) => {
|
|
61
|
+
const extractKey = (key) => (key ? ({
|
|
62
|
+
keyId: WABinary_1.getBinaryNodeChildUInt(key, 'id', 3),
|
|
63
|
+
publicKey: crypto_1.generateSignalPubKey(WABinary_1.getBinaryNodeChildBuffer(key, 'value')),
|
|
64
|
+
signature: WABinary_1.getBinaryNodeChildBuffer(key, 'signature')
|
|
65
|
+
}) : undefined)
|
|
66
|
+
const nodes = WABinary_1.getBinaryNodeChildren(WABinary_1.getBinaryNodeChild(node, 'list'), 'user')
|
|
67
|
+
for (const node of nodes) {
|
|
68
|
+
WABinary_1.assertNodeErrorFree(node)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
const chunkSize = 100
|
|
76
|
+
const chunks = lodash_1.chunk(nodes, chunkSize)
|
|
77
|
+
for (const nodesChunk of chunks) {
|
|
78
|
+
await Promise.all(nodesChunk.map(async (node) => {
|
|
79
|
+
const signedKey = WABinary_1.getBinaryNodeChild(node, 'skey')
|
|
80
|
+
const key = WABinary_1.getBinaryNodeChild(node, 'key')
|
|
81
|
+
const identity = WABinary_1.getBinaryNodeChildBuffer(node, 'identity')
|
|
82
|
+
const jid = node.attrs.jid
|
|
83
|
+
const registrationId = WABinary_1.getBinaryNodeChildUInt(node, 'registration', 4)
|
|
84
|
+
await repository.injectE2ESession({
|
|
85
|
+
jid,
|
|
86
|
+
session: {
|
|
87
|
+
registrationId: registrationId,
|
|
88
|
+
identityKey: crypto_1.generateSignalPubKey(identity),
|
|
89
|
+
signedPreKey: extractKey(signedKey),
|
|
90
|
+
preKey: extractKey(key)
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
}))
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.parseAndInjectE2ESessions = parseAndInjectE2ESessions;
|
|
97
|
+
const extractDeviceJids = (result, myJid, excludeZeroDevices) => {
|
|
98
|
+
const { user: myUser, device: myDevice } = WABinary_1.jidDecode(myJid)
|
|
99
|
+
const extracted = []
|
|
100
|
+
for (const userResult of result) {
|
|
101
|
+
const { devices, id } = userResult
|
|
102
|
+
const { user } = WABinary_1.jidDecode(id)
|
|
103
|
+
const deviceList = devices?.deviceList
|
|
104
|
+
if (Array.isArray(deviceList)) {
|
|
105
|
+
for (const { id: device, keyIndex } of deviceList) {
|
|
106
|
+
if ((!excludeZeroDevices || device !== 0) &&
|
|
107
|
+
(myUser !== user || myDevice !== device) &&
|
|
108
|
+
(device === 0 || !!keyIndex)
|
|
109
|
+
) {
|
|
110
|
+
extracted.push({ user, device })
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return extracted
|
|
116
|
+
}
|
|
117
|
+
exports.extractDeviceJids = extractDeviceJids;
|
|
118
|
+
/**
|
|
119
|
+
* get the next N keys for upload or processing
|
|
120
|
+
* @param count number of pre-keys to get or generate
|
|
121
|
+
*/
|
|
122
|
+
const getNextPreKeys = async ({ creds, keys }, count) => {
|
|
123
|
+
const { newPreKeys, lastPreKeyId, preKeysRange } = (0, exports.generateOrGetPreKeys)(creds, count);
|
|
124
|
+
const update = {
|
|
125
|
+
nextPreKeyId: Math.max(lastPreKeyId + 1, creds.nextPreKeyId),
|
|
126
|
+
firstUnuploadedPreKeyId: Math.max(creds.firstUnuploadedPreKeyId, lastPreKeyId + 1)
|
|
127
|
+
};
|
|
128
|
+
await keys.set({ 'pre-key': newPreKeys });
|
|
129
|
+
const preKeys = await (0, exports.getPreKeys)(keys, preKeysRange[0], preKeysRange[0] + preKeysRange[1]);
|
|
130
|
+
return { update, preKeys };
|
|
131
|
+
};
|
|
132
|
+
exports.getNextPreKeys = getNextPreKeys;
|
|
133
|
+
const getNextPreKeysNode = async (state, count) => {
|
|
134
|
+
const { creds } = state;
|
|
135
|
+
const { update, preKeys } = await (0, exports.getNextPreKeys)(state, count);
|
|
136
|
+
const node = {
|
|
137
|
+
tag: 'iq',
|
|
138
|
+
attrs: {
|
|
139
|
+
xmlns: 'encrypt',
|
|
140
|
+
type: 'set',
|
|
141
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
142
|
+
},
|
|
143
|
+
content: [
|
|
144
|
+
{ tag: 'registration', attrs: {}, content: (0, generics_1.encodeBigEndian)(creds.registrationId) },
|
|
145
|
+
{ tag: 'type', attrs: {}, content: Defaults_1.KEY_BUNDLE_TYPE },
|
|
146
|
+
{ tag: 'identity', attrs: {}, content: creds.signedIdentityKey.public },
|
|
147
|
+
{ tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k => (0, exports.xmppPreKey)(preKeys[+k], +k)) },
|
|
148
|
+
(0, exports.xmppSignedPreKey)(creds.signedPreKey)
|
|
149
|
+
]
|
|
150
|
+
};
|
|
151
|
+
return { update, node };
|
|
152
|
+
};
|
|
153
|
+
exports.getNextPreKeysNode = getNextPreKeysNode;
|