@fhynella/baileys 1.2.1

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.
Files changed (100) hide show
  1. package/LICENSE +21 -0
  2. package/WAProto/GenerateStatics.sh +4 -0
  3. package/WAProto/WAProto.proto +4775 -0
  4. package/WAProto/index.js +169661 -0
  5. package/WAProto/p.html +1 -0
  6. package/engine-requirements.js +10 -0
  7. package/lib/Defaults/index.js +118 -0
  8. package/lib/Defaults/wileys-version.json +3 -0
  9. package/lib/Signal/Group/ciphertext-message.js +15 -0
  10. package/lib/Signal/Group/group-session-builder.js +64 -0
  11. package/lib/Signal/Group/group_cipher.js +96 -0
  12. package/lib/Signal/Group/index.js +57 -0
  13. package/lib/Signal/Group/keyhelper.js +55 -0
  14. package/lib/Signal/Group/queue-job.js +57 -0
  15. package/lib/Signal/Group/sender-chain-key.js +34 -0
  16. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  17. package/lib/Signal/Group/sender-key-message.js +69 -0
  18. package/lib/Signal/Group/sender-key-name.js +51 -0
  19. package/lib/Signal/Group/sender-key-record.js +53 -0
  20. package/lib/Signal/Group/sender-key-state.js +99 -0
  21. package/lib/Signal/Group/sender-message-key.js +29 -0
  22. package/lib/Signal/libsignal.js +174 -0
  23. package/lib/Socket/Client/index.js +18 -0
  24. package/lib/Socket/Client/types.js +13 -0
  25. package/lib/Socket/Client/websocket.js +111 -0
  26. package/lib/Socket/business.js +260 -0
  27. package/lib/Socket/chats.js +915 -0
  28. package/lib/Socket/groups.js +332 -0
  29. package/lib/Socket/index.js +10 -0
  30. package/lib/Socket/messages-recv.js +1054 -0
  31. package/lib/Socket/messages-send.js +949 -0
  32. package/lib/Socket/newsletter.js +250 -0
  33. package/lib/Socket/socket.js +654 -0
  34. package/lib/Socket/usync.js +70 -0
  35. package/lib/Store/index.js +8 -0
  36. package/lib/Store/make-in-memory-store.js +439 -0
  37. package/lib/Store/make-ordered-dictionary.js +81 -0
  38. package/lib/Store/object-repository.js +27 -0
  39. package/lib/Types/Auth.js +2 -0
  40. package/lib/Types/Call.js +2 -0
  41. package/lib/Types/Chat.js +4 -0
  42. package/lib/Types/Contact.js +2 -0
  43. package/lib/Types/Events.js +2 -0
  44. package/lib/Types/GroupMetadata.js +2 -0
  45. package/lib/Types/Label.js +27 -0
  46. package/lib/Types/LabelAssociation.js +9 -0
  47. package/lib/Types/Message.js +7 -0
  48. package/lib/Types/Newsletter.js +18 -0
  49. package/lib/Types/Product.js +2 -0
  50. package/lib/Types/Signal.js +2 -0
  51. package/lib/Types/Socket.js +2 -0
  52. package/lib/Types/State.js +2 -0
  53. package/lib/Types/USync.js +2 -0
  54. package/lib/Types/index.js +42 -0
  55. package/lib/Utils/auth-utils.js +199 -0
  56. package/lib/Utils/browser-utils.js +35 -0
  57. package/lib/Utils/business.js +234 -0
  58. package/lib/Utils/chat-utils.js +730 -0
  59. package/lib/Utils/crypto.js +193 -0
  60. package/lib/Utils/decode-wa-message.js +207 -0
  61. package/lib/Utils/event-buffer.js +518 -0
  62. package/lib/Utils/generics.js +467 -0
  63. package/lib/Utils/history.js +94 -0
  64. package/lib/Utils/index.js +35 -0
  65. package/lib/Utils/link-preview.js +126 -0
  66. package/lib/Utils/logger.js +7 -0
  67. package/lib/Utils/lt-hash.js +51 -0
  68. package/lib/Utils/make-mutex.js +43 -0
  69. package/lib/Utils/message-retry-manager.js +128 -0
  70. package/lib/Utils/messages-media.js +957 -0
  71. package/lib/Utils/messages.js +1049 -0
  72. package/lib/Utils/noise-handler.js +150 -0
  73. package/lib/Utils/process-message.js +404 -0
  74. package/lib/Utils/signal.js +153 -0
  75. package/lib/Utils/use-multi-file-auth-state.js +125 -0
  76. package/lib/Utils/validate-connection.js +229 -0
  77. package/lib/Utils/wileys-event-stream.js +63 -0
  78. package/lib/WABinary/constants.js +1303 -0
  79. package/lib/WABinary/decode.js +265 -0
  80. package/lib/WABinary/encode.js +250 -0
  81. package/lib/WABinary/generic-utils.js +110 -0
  82. package/lib/WABinary/index.js +21 -0
  83. package/lib/WABinary/jid-utils.js +85 -0
  84. package/lib/WABinary/types.js +2 -0
  85. package/lib/WAM/BinaryInfo.js +13 -0
  86. package/lib/WAM/constants.js +15350 -0
  87. package/lib/WAM/encode.js +155 -0
  88. package/lib/WAM/index.js +19 -0
  89. package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
  90. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  91. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  92. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  93. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  94. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
  95. package/lib/WAUSync/Protocols/index.js +20 -0
  96. package/lib/WAUSync/USyncQuery.js +89 -0
  97. package/lib/WAUSync/USyncUser.js +26 -0
  98. package/lib/WAUSync/index.js +19 -0
  99. package/lib/index.js +49 -0
  100. package/package.json +115 -0
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.signedKeyPair = exports.Curve = exports.generateSignalPubKey = void 0;
37
+ exports.aesEncryptGCM = aesEncryptGCM;
38
+ exports.aesDecryptGCM = aesDecryptGCM;
39
+ exports.aesEncryptCTR = aesEncryptCTR;
40
+ exports.aesDecryptCTR = aesDecryptCTR;
41
+ exports.aesDecrypt = aesDecrypt;
42
+ exports.aesDecryptWithIV = aesDecryptWithIV;
43
+ exports.aesEncrypt = aesEncrypt;
44
+ exports.aesEncrypWithIV = aesEncrypWithIV;
45
+ exports.hmacSign = hmacSign;
46
+ exports.sha256 = sha256;
47
+ exports.md5 = md5;
48
+ exports.hkdf = hkdf;
49
+ exports.derivePairingCodeKey = derivePairingCodeKey;
50
+ const crypto_1 = require("crypto");
51
+ const libsignal = __importStar(require("libsignal"));
52
+ const Defaults_1 = require("../Defaults");
53
+ // insure browser & node compatibility
54
+ const { subtle } = globalThis.crypto;
55
+ /** prefix version byte to the pub keys, required for some curve crypto functions */
56
+ const generateSignalPubKey = (pubKey) => (pubKey.length === 33
57
+ ? pubKey
58
+ : Buffer.concat([Defaults_1.KEY_BUNDLE_TYPE, pubKey]));
59
+ exports.generateSignalPubKey = generateSignalPubKey;
60
+ exports.Curve = {
61
+ generateKeyPair: () => {
62
+ const { pubKey, privKey } = libsignal.curve.generateKeyPair();
63
+ return {
64
+ private: Buffer.from(privKey),
65
+ // remove version byte
66
+ public: Buffer.from(pubKey.slice(1))
67
+ };
68
+ },
69
+ sharedKey: (privateKey, publicKey) => {
70
+ const shared = libsignal.curve.calculateAgreement((0, exports.generateSignalPubKey)(publicKey), privateKey);
71
+ return Buffer.from(shared);
72
+ },
73
+ sign: (privateKey, buf) => (libsignal.curve.calculateSignature(privateKey, buf)),
74
+ verify: (pubKey, message, signature) => {
75
+ try {
76
+ libsignal.curve.verifySignature((0, exports.generateSignalPubKey)(pubKey), message, signature);
77
+ return true;
78
+ }
79
+ catch (error) {
80
+ return false;
81
+ }
82
+ }
83
+ };
84
+ const signedKeyPair = (identityKeyPair, keyId) => {
85
+ const preKey = exports.Curve.generateKeyPair();
86
+ const pubKey = (0, exports.generateSignalPubKey)(preKey.public);
87
+ const signature = exports.Curve.sign(identityKeyPair.private, pubKey);
88
+ return { keyPair: preKey, signature, keyId };
89
+ };
90
+ exports.signedKeyPair = signedKeyPair;
91
+ const GCM_TAG_LENGTH = 128 >> 3;
92
+ /**
93
+ * encrypt AES 256 GCM;
94
+ * where the tag tag is suffixed to the ciphertext
95
+ * */
96
+ function aesEncryptGCM(plaintext, key, iv, additionalData) {
97
+ const cipher = (0, crypto_1.createCipheriv)('aes-256-gcm', key, iv);
98
+ cipher.setAAD(additionalData);
99
+ return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
100
+ }
101
+ /**
102
+ * decrypt AES 256 GCM;
103
+ * where the auth tag is suffixed to the ciphertext
104
+ * */
105
+ function aesDecryptGCM(ciphertext, key, iv, additionalData) {
106
+ const decipher = (0, crypto_1.createDecipheriv)('aes-256-gcm', key, iv);
107
+ // decrypt additional adata
108
+ const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
109
+ const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
110
+ // set additional data
111
+ decipher.setAAD(additionalData);
112
+ decipher.setAuthTag(tag);
113
+ return Buffer.concat([decipher.update(enc), decipher.final()]);
114
+ }
115
+ function aesEncryptCTR(plaintext, key, iv) {
116
+ const cipher = (0, crypto_1.createCipheriv)('aes-256-ctr', key, iv);
117
+ return Buffer.concat([cipher.update(plaintext), cipher.final()]);
118
+ }
119
+ function aesDecryptCTR(ciphertext, key, iv) {
120
+ const decipher = (0, crypto_1.createDecipheriv)('aes-256-ctr', key, iv);
121
+ return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
122
+ }
123
+ /** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
124
+ function aesDecrypt(buffer, key) {
125
+ return aesDecryptWithIV(buffer.slice(16, buffer.length), key, buffer.slice(0, 16));
126
+ }
127
+ /** decrypt AES 256 CBC */
128
+ function aesDecryptWithIV(buffer, key, IV) {
129
+ const aes = (0, crypto_1.createDecipheriv)('aes-256-cbc', key, IV);
130
+ return Buffer.concat([aes.update(buffer), aes.final()]);
131
+ }
132
+ // encrypt AES 256 CBC; where a random IV is prefixed to the buffer
133
+ function aesEncrypt(buffer, key) {
134
+ const IV = (0, crypto_1.randomBytes)(16);
135
+ const aes = (0, crypto_1.createCipheriv)('aes-256-cbc', key, IV);
136
+ return Buffer.concat([IV, aes.update(buffer), aes.final()]); // prefix IV to the buffer
137
+ }
138
+ // encrypt AES 256 CBC with a given IV
139
+ function aesEncrypWithIV(buffer, key, IV) {
140
+ const aes = (0, crypto_1.createCipheriv)('aes-256-cbc', key, IV);
141
+ return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer
142
+ }
143
+ // sign HMAC using SHA 256
144
+ function hmacSign(buffer, key, variant = 'sha256') {
145
+ return (0, crypto_1.createHmac)(variant, key).update(buffer).digest();
146
+ }
147
+ function sha256(buffer) {
148
+ return (0, crypto_1.createHash)('sha256').update(buffer).digest();
149
+ }
150
+ function md5(buffer) {
151
+ return (0, crypto_1.createHash)('md5').update(buffer).digest();
152
+ }
153
+ // HKDF key expansion
154
+ async function hkdf(buffer, expandedLength, info) {
155
+ // Ensure we have a Uint8Array for the key material
156
+ const inputKeyMaterial = buffer instanceof Uint8Array
157
+ ? buffer
158
+ : new Uint8Array(buffer);
159
+ // Set default values if not provided
160
+ const salt = info.salt ? new Uint8Array(info.salt) : new Uint8Array(0);
161
+ const infoBytes = info.info
162
+ ? new TextEncoder().encode(info.info)
163
+ : new Uint8Array(0);
164
+ // Import the input key material
165
+ const importedKey = await subtle.importKey('raw', inputKeyMaterial, { name: 'HKDF' }, false, ['deriveBits']);
166
+ // Derive bits using HKDF
167
+ const derivedBits = await subtle.deriveBits({
168
+ name: 'HKDF',
169
+ hash: 'SHA-256',
170
+ salt: salt,
171
+ info: infoBytes
172
+ }, importedKey, expandedLength * 8 // Convert bytes to bits
173
+ );
174
+ return Buffer.from(derivedBits);
175
+ }
176
+ async function derivePairingCodeKey(pairingCode, salt) {
177
+ // Convert inputs to formats Web Crypto API can work with
178
+ const encoder = new TextEncoder();
179
+ const pairingCodeBuffer = encoder.encode(pairingCode);
180
+ const saltBuffer = salt instanceof Uint8Array ? salt : new Uint8Array(salt);
181
+ // Import the pairing code as key material
182
+ const keyMaterial = await subtle.importKey('raw', pairingCodeBuffer, { name: 'PBKDF2' }, false, ['deriveBits']);
183
+ // Derive bits using PBKDF2 with the same parameters
184
+ // 2 << 16 = 131,072 iterations
185
+ const derivedBits = await subtle.deriveBits({
186
+ name: 'PBKDF2',
187
+ salt: saltBuffer,
188
+ iterations: 2 << 16,
189
+ hash: 'SHA-256'
190
+ }, keyMaterial, 32 * 8 // 32 bytes * 8 = 256 bits
191
+ );
192
+ return Buffer.from(derivedBits);
193
+ }
@@ -0,0 +1,207 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decryptMessageNode = exports.NACK_REASONS = exports.MISSING_KEYS_ERROR_TEXT = exports.NO_MESSAGE_FOUND_ERROR_TEXT = void 0;
4
+ exports.decodeMessageNode = decodeMessageNode;
5
+ const boom_1 = require("@hapi/boom");
6
+ const WAProto_1 = require("../../WAProto");
7
+ const WABinary_1 = require("../WABinary");
8
+ const generics_1 = require("./generics");
9
+ exports.NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node';
10
+ exports.MISSING_KEYS_ERROR_TEXT = 'Key used already or never filled';
11
+ exports.NACK_REASONS = {
12
+ ParsingError: 487,
13
+ UnrecognizedStanza: 488,
14
+ UnrecognizedStanzaClass: 489,
15
+ UnrecognizedStanzaType: 490,
16
+ InvalidProtobuf: 491,
17
+ InvalidHostedCompanionStanza: 493,
18
+ MissingMessageSecret: 495,
19
+ SignalErrorOldCounter: 496,
20
+ MessageDeletedOnPeer: 499,
21
+ UnhandledError: 500,
22
+ UnsupportedAdminRevoke: 550,
23
+ UnsupportedLIDGroup: 551,
24
+ DBOperationFailed: 552
25
+ };
26
+ /**
27
+ * Decode the received node as a message.
28
+ * @note this will only parse the message, not decrypt it
29
+ */
30
+ function decodeMessageNode(stanza, meId, meLid) {
31
+ var _a, _b, _c, _d, _e, _f, _g;
32
+ let msgType;
33
+ let chatId;
34
+ let author;
35
+ const msgId = stanza.attrs.id;
36
+ const from = stanza.attrs.from;
37
+ const senderPn = (_a = stanza === null || stanza === void 0 ? void 0 : stanza.attrs) === null || _a === void 0 ? void 0 : _a.sender_pn;
38
+ const senderLid = (_b = stanza === null || stanza === void 0 ? void 0 : stanza.attrs) === null || _b === void 0 ? void 0 : _b.sender_lid;
39
+ const participant = stanza.attrs.participant;
40
+ const participantLid = (_c = stanza === null || stanza === void 0 ? void 0 : stanza.attrs) === null || _c === void 0 ? void 0 : _c.participant_lid;
41
+ const recipient = stanza.attrs.recipient;
42
+ const isMe = (jid) => (0, WABinary_1.areJidsSameUser)(jid, meId);
43
+ const isMeLid = (jid) => (0, WABinary_1.areJidsSameUser)(jid, meLid);
44
+ if ((0, WABinary_1.isJidUser)(from) || (0, WABinary_1.isLidUser)(from)) {
45
+ if (recipient && !(0, WABinary_1.isJidMetaAi)(recipient)) {
46
+ if (!isMe(from) && !isMeLid(from)) {
47
+ throw new boom_1.Boom('receipient present, but msg not from me', { data: stanza });
48
+ }
49
+ chatId = recipient;
50
+ }
51
+ else {
52
+ chatId = from;
53
+ }
54
+ msgType = 'chat';
55
+ author = from;
56
+ }
57
+ else if ((0, WABinary_1.isJidGroup)(from)) {
58
+ if (!participant) {
59
+ throw new boom_1.Boom('No participant in group message');
60
+ }
61
+ msgType = 'group';
62
+ author = participant;
63
+ chatId = from;
64
+ }
65
+ else if ((0, WABinary_1.isJidNewsletter)(from)) {
66
+ msgType = 'newsletter';
67
+ author = from;
68
+ chatId = from;
69
+ }
70
+ else if ((0, WABinary_1.isJidBroadcast)(from)) {
71
+ if (!participant) {
72
+ throw new boom_1.Boom('No participant in group message');
73
+ }
74
+ const isParticipantMe = isMe(participant);
75
+ if ((0, WABinary_1.isJidStatusBroadcast)(from)) {
76
+ msgType = isParticipantMe ? 'direct_peer_status' : 'other_status';
77
+ }
78
+ else {
79
+ msgType = isParticipantMe ? 'peer_broadcast' : 'other_broadcast';
80
+ }
81
+ chatId = from;
82
+ author = participant;
83
+ }
84
+ else {
85
+ throw new boom_1.Boom('Unknown message type', { data: stanza });
86
+ }
87
+ const fromMe = (0, WABinary_1.isJidNewsletter)(from) ? !!((_d = stanza.attrs) === null || _d === void 0 ? void 0 : _d.is_sender) || false : ((0, WABinary_1.isLidUser)(from) ? isMeLid : isMe)(stanza.attrs.participant || stanza.attrs.from);
88
+ const pushname = (_e = stanza === null || stanza === void 0 ? void 0 : stanza.attrs) === null || _e === void 0 ? void 0 : _e.notify;
89
+ const key = {
90
+ remoteJid: chatId,
91
+ fromMe,
92
+ id: msgId,
93
+ senderPn,
94
+ senderLid,
95
+ participant,
96
+ participantLid,
97
+ 'server_id': (_f = stanza.attrs) === null || _f === void 0 ? void 0 : _f.server_id
98
+ };
99
+ const fullMessage = {
100
+ key,
101
+ messageTimestamp: +stanza.attrs.t,
102
+ pushName: pushname,
103
+ broadcast: (0, WABinary_1.isJidBroadcast)(from)
104
+ };
105
+ if (msgType === 'newsletter') {
106
+ fullMessage.newsletterServerId = +((_g = stanza.attrs) === null || _g === void 0 ? void 0 : _g.server_id);
107
+ }
108
+ if (key.fromMe) {
109
+ fullMessage.status = WAProto_1.proto.WebMessageInfo.Status.SERVER_ACK;
110
+ }
111
+ return {
112
+ fullMessage,
113
+ author,
114
+ sender: msgType === 'chat' ? author : chatId
115
+ };
116
+ }
117
+ const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
118
+ const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid);
119
+ return {
120
+ fullMessage,
121
+ category: stanza.attrs.category,
122
+ author,
123
+ async decrypt() {
124
+ var _a;
125
+ let decryptables = 0;
126
+ if (Array.isArray(stanza.content)) {
127
+ for (const { tag, attrs, content } of stanza.content) {
128
+ if (tag === 'verified_name' && content instanceof Uint8Array) {
129
+ const cert = WAProto_1.proto.VerifiedNameCertificate.decode(content);
130
+ const details = WAProto_1.proto.VerifiedNameCertificate.Details.decode(cert.details);
131
+ fullMessage.verifiedBizName = details.verifiedName;
132
+ }
133
+ if (tag === 'unavailable' && attrs.type === 'view_once') {
134
+ fullMessage.key.isViewOnce = true;
135
+ }
136
+ if (tag !== 'enc' && tag !== 'plaintext') {
137
+ continue;
138
+ }
139
+ if (!(content instanceof Uint8Array)) {
140
+ continue;
141
+ }
142
+ decryptables += 1;
143
+ let msgBuffer;
144
+ try {
145
+ const e2eType = tag === 'plaintext' ? 'plaintext' : attrs.type;
146
+ switch (e2eType) {
147
+ case 'skmsg':
148
+ msgBuffer = await repository.decryptGroupMessage({
149
+ group: sender,
150
+ authorJid: author,
151
+ msg: content
152
+ });
153
+ break;
154
+ case 'pkmsg':
155
+ case 'msg':
156
+ const user = (0, WABinary_1.isJidUser)(sender) ? sender : author;
157
+ msgBuffer = await repository.decryptMessage({
158
+ jid: user,
159
+ type: e2eType,
160
+ ciphertext: content
161
+ });
162
+ break;
163
+ case 'plaintext':
164
+ msgBuffer = content;
165
+ break;
166
+ case undefined:
167
+ msgBuffer = content;
168
+ break;
169
+ default:
170
+ throw new Error(`Unknown e2e type: ${e2eType}`);
171
+ }
172
+ let msg = WAProto_1.proto.Message.decode(e2eType !== 'plaintext' ? (0, generics_1.unpadRandomMax16)(msgBuffer) : msgBuffer);
173
+ msg = ((_a = msg === null || msg === void 0 ? void 0 : msg.deviceSentMessage) === null || _a === void 0 ? void 0 : _a.message) || msg;
174
+ if (msg.senderKeyDistributionMessage) {
175
+ try {
176
+ await repository.processSenderKeyDistributionMessage({
177
+ authorJid: author,
178
+ item: msg.senderKeyDistributionMessage
179
+ });
180
+ }
181
+ catch (err) {
182
+ logger.error({ key: fullMessage.key, err }, 'failed to decrypt message');
183
+ }
184
+ }
185
+ if (fullMessage.message) {
186
+ Object.assign(fullMessage.message, msg);
187
+ }
188
+ else {
189
+ fullMessage.message = msg;
190
+ }
191
+ }
192
+ catch (err) {
193
+ logger.error({ key: fullMessage.key, err }, 'failed to decrypt message');
194
+ fullMessage.messageStubType = WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT;
195
+ fullMessage.messageStubParameters = [err.message];
196
+ }
197
+ }
198
+ }
199
+ // if nothing was found to decrypt
200
+ if (!decryptables) {
201
+ fullMessage.messageStubType = WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT;
202
+ fullMessage.messageStubParameters = [exports.NO_MESSAGE_FOUND_ERROR_TEXT];
203
+ }
204
+ }
205
+ };
206
+ };
207
+ exports.decryptMessageNode = decryptMessageNode;