gifted-baileys 1.5.0 → 1.5.5
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 +1 -1
- package/README.md +1429 -684
- package/package.json +11 -26
- package/src/Defaults/baileys-version.json +3 -0
- package/{lib → src}/Defaults/index.js +16 -8
- package/src/Defaults/index.ts +131 -0
- package/src/Defaults/phonenumber-mcc.json +223 -0
- package/src/Signal/libsignal.js +180 -0
- package/src/Signal/libsignal.ts +141 -0
- package/src/Socket/Client/abstract-socket-client.ts +19 -0
- package/src/Socket/Client/index.ts +3 -0
- package/src/Socket/Client/mobile-socket-client.js +78 -0
- package/src/Socket/Client/mobile-socket-client.ts +66 -0
- package/src/Socket/Client/web-socket-client.js +75 -0
- package/src/Socket/Client/web-socket-client.ts +57 -0
- package/{lib → src}/Socket/business.js +33 -27
- package/src/Socket/business.ts +281 -0
- package/{lib → src}/Socket/chats.js +197 -178
- package/src/Socket/chats.ts +1030 -0
- package/{lib → src}/Socket/groups.js +69 -79
- package/src/Socket/groups.ts +356 -0
- package/{lib → src}/Socket/index.js +1 -4
- package/src/Socket/index.ts +13 -0
- package/{lib → src}/Socket/messages-recv.js +160 -108
- package/src/Socket/messages-recv.ts +985 -0
- package/{lib → src}/Socket/messages-send.js +183 -100
- package/src/Socket/messages-send.ts +871 -0
- package/src/Socket/newsletter.js +227 -0
- package/src/Socket/newsletter.ts +282 -0
- package/{lib → src}/Socket/registration.js +55 -63
- package/src/Socket/registration.ts +250 -0
- package/{lib → src}/Socket/socket.js +107 -66
- package/src/Socket/socket.ts +777 -0
- package/src/Store/index.ts +3 -0
- package/{lib → src}/Store/make-cache-manager-store.js +34 -25
- package/src/Store/make-cache-manager-store.ts +100 -0
- package/{lib → src}/Store/make-in-memory-store.js +51 -61
- package/src/Store/make-in-memory-store.ts +475 -0
- package/src/Store/make-ordered-dictionary.ts +86 -0
- package/{lib → src}/Store/object-repository.js +1 -1
- package/src/Store/object-repository.ts +32 -0
- package/src/Tests/test.app-state-sync.js +204 -0
- package/src/Tests/test.app-state-sync.ts +207 -0
- package/src/Tests/test.event-buffer.js +270 -0
- package/src/Tests/test.event-buffer.ts +319 -0
- package/src/Tests/test.key-store.js +76 -0
- package/src/Tests/test.key-store.ts +92 -0
- package/src/Tests/test.libsignal.js +141 -0
- package/src/Tests/test.libsignal.ts +186 -0
- package/src/Tests/test.media-download.js +93 -0
- package/src/Tests/test.media-download.ts +76 -0
- package/src/Tests/test.messages.js +33 -0
- package/src/Tests/test.messages.ts +37 -0
- package/src/Tests/utils.js +34 -0
- package/src/Tests/utils.ts +36 -0
- package/src/Types/Auth.ts +113 -0
- package/src/Types/Call.ts +15 -0
- package/src/Types/Chat.ts +106 -0
- package/{lib/Types/Contact.d.ts → src/Types/Contact.ts} +9 -8
- package/src/Types/Events.ts +93 -0
- package/src/Types/GroupMetadata.ts +53 -0
- package/{lib → src}/Types/Label.js +1 -1
- package/src/Types/Label.ts +36 -0
- package/{lib → src}/Types/LabelAssociation.js +1 -1
- package/{lib/Types/LabelAssociation.d.ts → src/Types/LabelAssociation.ts} +22 -16
- package/src/Types/Message.ts +288 -0
- package/src/Types/Newsletter.js +32 -0
- package/src/Types/Newsletter.ts +98 -0
- package/src/Types/Product.ts +85 -0
- package/src/Types/Signal.ts +68 -0
- package/{lib/Types/Socket.d.ts → src/Types/Socket.ts} +68 -56
- package/src/Types/State.ts +29 -0
- package/{lib → src}/Types/index.js +2 -1
- package/src/Types/index.ts +59 -0
- package/{lib → src}/Utils/auth-utils.js +95 -76
- package/src/Utils/auth-utils.ts +222 -0
- package/src/Utils/baileys-event-stream.js +92 -0
- package/src/Utils/baileys-event-stream.ts +66 -0
- package/{lib → src}/Utils/business.js +45 -17
- package/src/Utils/business.ts +275 -0
- package/{lib → src}/Utils/chat-utils.js +74 -46
- package/src/Utils/chat-utils.ts +860 -0
- package/{lib → src}/Utils/crypto.js +31 -21
- package/src/Utils/crypto.ts +131 -0
- package/src/Utils/decode-wa-message.js +211 -0
- package/src/Utils/decode-wa-message.ts +228 -0
- package/{lib → src}/Utils/event-buffer.js +13 -4
- package/src/Utils/event-buffer.ts +613 -0
- package/{lib → src}/Utils/generics.js +98 -45
- package/src/Utils/generics.ts +434 -0
- package/{lib → src}/Utils/history.js +39 -10
- package/src/Utils/history.ts +112 -0
- package/src/Utils/index.ts +17 -0
- package/{lib → src}/Utils/link-preview.js +54 -17
- package/src/Utils/link-preview.ts +122 -0
- package/src/Utils/logger.ts +3 -0
- package/src/Utils/lt-hash.ts +61 -0
- package/{lib → src}/Utils/make-mutex.js +13 -4
- package/src/Utils/make-mutex.ts +44 -0
- package/{lib → src}/Utils/messages-media.js +296 -192
- package/src/Utils/messages-media.ts +847 -0
- package/{lib → src}/Utils/messages.js +124 -113
- package/src/Utils/messages.ts +956 -0
- package/{lib → src}/Utils/noise-handler.js +16 -3
- package/src/Utils/noise-handler.ts +197 -0
- package/{lib → src}/Utils/process-message.js +33 -29
- package/src/Utils/process-message.ts +414 -0
- package/{lib → src}/Utils/signal.js +23 -14
- package/src/Utils/signal.ts +177 -0
- package/{lib → src}/Utils/use-multi-file-auth-state.js +28 -19
- package/src/Utils/use-multi-file-auth-state.ts +90 -0
- package/{lib → src}/Utils/validate-connection.js +25 -42
- package/src/Utils/validate-connection.ts +238 -0
- package/src/WABinary/constants.ts +42 -0
- package/{lib → src}/WABinary/decode.js +17 -7
- package/src/WABinary/decode.ts +265 -0
- package/{lib → src}/WABinary/encode.js +17 -7
- package/src/WABinary/encode.ts +236 -0
- package/{lib → src}/WABinary/generic-utils.js +2 -2
- package/src/WABinary/generic-utils.ts +121 -0
- package/src/WABinary/index.ts +5 -0
- package/{lib → src}/WABinary/jid-utils.js +4 -1
- package/src/WABinary/jid-utils.ts +68 -0
- package/src/WABinary/types.ts +17 -0
- package/src/WAM/BinaryInfo.js +13 -0
- package/src/WAM/BinaryInfo.ts +12 -0
- package/src/WAM/constants.js +15350 -0
- package/src/WAM/constants.ts +15382 -0
- package/src/WAM/encode.js +155 -0
- package/src/WAM/encode.ts +174 -0
- package/src/WAM/index.js +19 -0
- package/src/WAM/index.ts +3 -0
- package/src/gifted +1 -0
- package/{lib → src}/index.js +1 -0
- package/src/index.ts +13 -0
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/index.d.ts +0 -284
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Signal/libsignal.d.ts +0 -3
- package/lib/Signal/libsignal.js +0 -152
- package/lib/Socket/Client/abstract-socket-client.d.ts +0 -17
- package/lib/Socket/Client/index.d.ts +0 -3
- package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.d.ts +0 -12
- package/lib/Socket/Client/web-socket-client.js +0 -62
- package/lib/Socket/business.d.ts +0 -135
- package/lib/Socket/chats.d.ts +0 -79
- package/lib/Socket/groups.d.ts +0 -113
- package/lib/Socket/index.d.ts +0 -137
- package/lib/Socket/messages-recv.d.ts +0 -124
- package/lib/Socket/messages-send.d.ts +0 -119
- package/lib/Socket/registration.d.ts +0 -232
- package/lib/Socket/socket.d.ts +0 -42
- package/lib/Store/index.d.ts +0 -3
- package/lib/Store/make-cache-manager-store.d.ts +0 -13
- package/lib/Store/make-in-memory-store.d.ts +0 -117
- package/lib/Store/make-ordered-dictionary.d.ts +0 -13
- package/lib/Store/object-repository.d.ts +0 -10
- package/lib/Types/Auth.d.ts +0 -108
- package/lib/Types/Call.d.ts +0 -13
- package/lib/Types/Chat.d.ts +0 -102
- package/lib/Types/Events.d.ts +0 -157
- package/lib/Types/GroupMetadata.d.ts +0 -52
- package/lib/Types/Label.d.ts +0 -35
- package/lib/Types/Message.d.ts +0 -261
- package/lib/Types/Product.d.ts +0 -78
- package/lib/Types/Signal.d.ts +0 -57
- package/lib/Types/State.d.ts +0 -27
- package/lib/Types/index.d.ts +0 -56
- package/lib/Utils/auth-utils.d.ts +0 -18
- package/lib/Utils/baileys-event-stream.d.ts +0 -16
- package/lib/Utils/baileys-event-stream.js +0 -63
- package/lib/Utils/business.d.ts +0 -22
- package/lib/Utils/chat-utils.d.ts +0 -71
- package/lib/Utils/crypto.d.ts +0 -41
- package/lib/Utils/decode-wa-message.d.ts +0 -19
- package/lib/Utils/decode-wa-message.js +0 -174
- package/lib/Utils/event-buffer.d.ts +0 -35
- package/lib/Utils/generics.d.ts +0 -94
- package/lib/Utils/history.d.ts +0 -15
- package/lib/Utils/index.d.ts +0 -17
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -4
- package/lib/Utils/lt-hash.d.ts +0 -12
- package/lib/Utils/make-mutex.d.ts +0 -7
- package/lib/Utils/messages-media.d.ts +0 -107
- package/lib/Utils/messages.d.ts +0 -76
- package/lib/Utils/noise-handler.d.ts +0 -20
- package/lib/Utils/process-message.d.ts +0 -41
- package/lib/Utils/signal.d.ts +0 -32
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -12
- package/lib/Utils/validate-connection.d.ts +0 -11
- package/lib/WABinary/constants.d.ts +0 -27
- package/lib/WABinary/decode.d.ts +0 -7
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -15
- package/lib/WABinary/index.d.ts +0 -5
- package/lib/WABinary/jid-utils.d.ts +0 -29
- package/lib/WABinary/types.d.ts +0 -18
- package/lib/index.d.ts +0 -10
- /package/{lib → src}/Socket/Client/abstract-socket-client.js +0 -0
- /package/{lib → src}/Socket/Client/index.js +0 -0
- /package/{lib → src}/Store/index.js +0 -0
- /package/{lib → src}/Store/make-ordered-dictionary.js +0 -0
- /package/{lib → src}/Types/Auth.js +0 -0
- /package/{lib → src}/Types/Call.js +0 -0
- /package/{lib → src}/Types/Chat.js +0 -0
- /package/{lib → src}/Types/Contact.js +0 -0
- /package/{lib → src}/Types/Events.js +0 -0
- /package/{lib → src}/Types/GroupMetadata.js +0 -0
- /package/{lib → src}/Types/Message.js +0 -0
- /package/{lib → src}/Types/Product.js +0 -0
- /package/{lib → src}/Types/Signal.js +0 -0
- /package/{lib → src}/Types/Socket.js +0 -0
- /package/{lib → src}/Types/State.js +0 -0
- /package/{lib → src}/Utils/index.js +0 -0
- /package/{lib → src}/Utils/logger.js +0 -0
- /package/{lib → src}/Utils/lt-hash.js +0 -0
- /package/{lib → src}/WABinary/constants.js +0 -0
- /package/{lib → src}/WABinary/index.js +0 -0
- /package/{lib → src}/WABinary/types.js +0 -0
|
@@ -15,18 +15,41 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
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
|
+
})();
|
|
25
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
37
|
};
|
|
28
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.
|
|
39
|
+
exports.signedKeyPair = exports.Curve = exports.generateSignalPubKey = void 0;
|
|
40
|
+
exports.aesEncryptGCM = aesEncryptGCM;
|
|
41
|
+
exports.aesDecryptGCM = aesDecryptGCM;
|
|
42
|
+
exports.aesEncryptCTR = aesEncryptCTR;
|
|
43
|
+
exports.aesDecryptCTR = aesDecryptCTR;
|
|
44
|
+
exports.aesDecrypt = aesDecrypt;
|
|
45
|
+
exports.aesDecryptWithIV = aesDecryptWithIV;
|
|
46
|
+
exports.aesEncrypt = aesEncrypt;
|
|
47
|
+
exports.aesEncrypWithIV = aesEncrypWithIV;
|
|
48
|
+
exports.hmacSign = hmacSign;
|
|
49
|
+
exports.sha256 = sha256;
|
|
50
|
+
exports.md5 = md5;
|
|
51
|
+
exports.hkdf = hkdf;
|
|
52
|
+
exports.derivePairingCodeKey = derivePairingCodeKey;
|
|
30
53
|
const crypto_1 = require("crypto");
|
|
31
54
|
const futoin_hkdf_1 = __importDefault(require("futoin-hkdf"));
|
|
32
55
|
const libsignal = __importStar(require("libsignal"));
|
|
@@ -77,7 +100,6 @@ function aesEncryptGCM(plaintext, key, iv, additionalData) {
|
|
|
77
100
|
cipher.setAAD(additionalData);
|
|
78
101
|
return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
|
|
79
102
|
}
|
|
80
|
-
exports.aesEncryptGCM = aesEncryptGCM;
|
|
81
103
|
/**
|
|
82
104
|
* decrypt AES 256 GCM;
|
|
83
105
|
* where the auth tag is suffixed to the ciphertext
|
|
@@ -92,60 +114,48 @@ function aesDecryptGCM(ciphertext, key, iv, additionalData) {
|
|
|
92
114
|
decipher.setAuthTag(tag);
|
|
93
115
|
return Buffer.concat([decipher.update(enc), decipher.final()]);
|
|
94
116
|
}
|
|
95
|
-
exports.aesDecryptGCM = aesDecryptGCM;
|
|
96
117
|
function aesEncryptCTR(plaintext, key, iv) {
|
|
97
118
|
const cipher = (0, crypto_1.createCipheriv)('aes-256-ctr', key, iv);
|
|
98
119
|
return Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
99
120
|
}
|
|
100
|
-
exports.aesEncryptCTR = aesEncryptCTR;
|
|
101
121
|
function aesDecryptCTR(ciphertext, key, iv) {
|
|
102
122
|
const decipher = (0, crypto_1.createDecipheriv)('aes-256-ctr', key, iv);
|
|
103
123
|
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
104
124
|
}
|
|
105
|
-
exports.aesDecryptCTR = aesDecryptCTR;
|
|
106
125
|
/** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
|
|
107
126
|
function aesDecrypt(buffer, key) {
|
|
108
127
|
return aesDecryptWithIV(buffer.slice(16, buffer.length), key, buffer.slice(0, 16));
|
|
109
128
|
}
|
|
110
|
-
exports.aesDecrypt = aesDecrypt;
|
|
111
129
|
/** decrypt AES 256 CBC */
|
|
112
130
|
function aesDecryptWithIV(buffer, key, IV) {
|
|
113
131
|
const aes = (0, crypto_1.createDecipheriv)('aes-256-cbc', key, IV);
|
|
114
132
|
return Buffer.concat([aes.update(buffer), aes.final()]);
|
|
115
133
|
}
|
|
116
|
-
exports.aesDecryptWithIV = aesDecryptWithIV;
|
|
117
134
|
// encrypt AES 256 CBC; where a random IV is prefixed to the buffer
|
|
118
135
|
function aesEncrypt(buffer, key) {
|
|
119
136
|
const IV = (0, crypto_1.randomBytes)(16);
|
|
120
137
|
const aes = (0, crypto_1.createCipheriv)('aes-256-cbc', key, IV);
|
|
121
138
|
return Buffer.concat([IV, aes.update(buffer), aes.final()]); // prefix IV to the buffer
|
|
122
139
|
}
|
|
123
|
-
exports.aesEncrypt = aesEncrypt;
|
|
124
140
|
// encrypt AES 256 CBC with a given IV
|
|
125
141
|
function aesEncrypWithIV(buffer, key, IV) {
|
|
126
142
|
const aes = (0, crypto_1.createCipheriv)('aes-256-cbc', key, IV);
|
|
127
143
|
return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer
|
|
128
144
|
}
|
|
129
|
-
exports.aesEncrypWithIV = aesEncrypWithIV;
|
|
130
145
|
// sign HMAC using SHA 256
|
|
131
146
|
function hmacSign(buffer, key, variant = 'sha256') {
|
|
132
147
|
return (0, crypto_1.createHmac)(variant, key).update(buffer).digest();
|
|
133
148
|
}
|
|
134
|
-
exports.hmacSign = hmacSign;
|
|
135
149
|
function sha256(buffer) {
|
|
136
150
|
return (0, crypto_1.createHash)('sha256').update(buffer).digest();
|
|
137
151
|
}
|
|
138
|
-
exports.sha256 = sha256;
|
|
139
152
|
function md5(buffer) {
|
|
140
153
|
return (0, crypto_1.createHash)('md5').update(buffer).digest();
|
|
141
154
|
}
|
|
142
|
-
exports.md5 = md5;
|
|
143
155
|
// HKDF key expansion
|
|
144
156
|
function hkdf(buffer, expandedLength, info) {
|
|
145
157
|
return (0, futoin_hkdf_1.default)(!Buffer.isBuffer(buffer) ? Buffer.from(buffer) : buffer, expandedLength, info);
|
|
146
158
|
}
|
|
147
|
-
exports.hkdf = hkdf;
|
|
148
159
|
function derivePairingCodeKey(pairingCode, salt) {
|
|
149
160
|
return (0, crypto_1.pbkdf2Sync)(pairingCode, salt, 2 << 16, 32, 'sha256');
|
|
150
161
|
}
|
|
151
|
-
exports.derivePairingCodeKey = derivePairingCodeKey;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHash, createHmac, pbkdf2Sync, randomBytes } from 'crypto'
|
|
2
|
+
import HKDF from 'futoin-hkdf'
|
|
3
|
+
import * as libsignal from 'libsignal'
|
|
4
|
+
import { KEY_BUNDLE_TYPE } from '../Defaults'
|
|
5
|
+
import { KeyPair } from '../Types'
|
|
6
|
+
|
|
7
|
+
/** prefix version byte to the pub keys, required for some curve crypto functions */
|
|
8
|
+
export const generateSignalPubKey = (pubKey: Uint8Array | Buffer) => (
|
|
9
|
+
pubKey.length === 33
|
|
10
|
+
? pubKey
|
|
11
|
+
: Buffer.concat([ KEY_BUNDLE_TYPE, pubKey ])
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
export const Curve = {
|
|
15
|
+
generateKeyPair: (): KeyPair => {
|
|
16
|
+
const { pubKey, privKey } = libsignal.curve.generateKeyPair()
|
|
17
|
+
return {
|
|
18
|
+
private: Buffer.from(privKey),
|
|
19
|
+
// remove version byte
|
|
20
|
+
public: Buffer.from((pubKey as Uint8Array).slice(1))
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
sharedKey: (privateKey: Uint8Array, publicKey: Uint8Array) => {
|
|
24
|
+
const shared = libsignal.curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey)
|
|
25
|
+
return Buffer.from(shared)
|
|
26
|
+
},
|
|
27
|
+
sign: (privateKey: Uint8Array, buf: Uint8Array) => (
|
|
28
|
+
libsignal.curve.calculateSignature(privateKey, buf)
|
|
29
|
+
),
|
|
30
|
+
verify: (pubKey: Uint8Array, message: Uint8Array, signature: Uint8Array) => {
|
|
31
|
+
try {
|
|
32
|
+
libsignal.curve.verifySignature(generateSignalPubKey(pubKey), message, signature)
|
|
33
|
+
return true
|
|
34
|
+
} catch(error) {
|
|
35
|
+
return false
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const signedKeyPair = (identityKeyPair: KeyPair, keyId: number) => {
|
|
41
|
+
const preKey = Curve.generateKeyPair()
|
|
42
|
+
const pubKey = generateSignalPubKey(preKey.public)
|
|
43
|
+
|
|
44
|
+
const signature = Curve.sign(identityKeyPair.private, pubKey)
|
|
45
|
+
|
|
46
|
+
return { keyPair: preKey, signature, keyId }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const GCM_TAG_LENGTH = 128 >> 3
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* encrypt AES 256 GCM;
|
|
53
|
+
* where the tag tag is suffixed to the ciphertext
|
|
54
|
+
* */
|
|
55
|
+
export function aesEncryptGCM(plaintext: Uint8Array, key: Uint8Array, iv: Uint8Array, additionalData: Uint8Array) {
|
|
56
|
+
const cipher = createCipheriv('aes-256-gcm', key, iv)
|
|
57
|
+
cipher.setAAD(additionalData)
|
|
58
|
+
return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()])
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* decrypt AES 256 GCM;
|
|
63
|
+
* where the auth tag is suffixed to the ciphertext
|
|
64
|
+
* */
|
|
65
|
+
export function aesDecryptGCM(ciphertext: Uint8Array, key: Uint8Array, iv: Uint8Array, additionalData: Uint8Array) {
|
|
66
|
+
const decipher = createDecipheriv('aes-256-gcm', key, iv)
|
|
67
|
+
// decrypt additional adata
|
|
68
|
+
const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH)
|
|
69
|
+
const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH)
|
|
70
|
+
// set additional data
|
|
71
|
+
decipher.setAAD(additionalData)
|
|
72
|
+
decipher.setAuthTag(tag)
|
|
73
|
+
|
|
74
|
+
return Buffer.concat([ decipher.update(enc), decipher.final() ])
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function aesEncryptCTR(plaintext: Uint8Array, key: Uint8Array, iv: Uint8Array) {
|
|
78
|
+
const cipher = createCipheriv('aes-256-ctr', key, iv)
|
|
79
|
+
return Buffer.concat([cipher.update(plaintext), cipher.final()])
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function aesDecryptCTR(ciphertext: Uint8Array, key: Uint8Array, iv: Uint8Array) {
|
|
83
|
+
const decipher = createDecipheriv('aes-256-ctr', key, iv)
|
|
84
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()])
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
|
|
88
|
+
export function aesDecrypt(buffer: Buffer, key: Buffer) {
|
|
89
|
+
return aesDecryptWithIV(buffer.slice(16, buffer.length), key, buffer.slice(0, 16))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** decrypt AES 256 CBC */
|
|
93
|
+
export function aesDecryptWithIV(buffer: Buffer, key: Buffer, IV: Buffer) {
|
|
94
|
+
const aes = createDecipheriv('aes-256-cbc', key, IV)
|
|
95
|
+
return Buffer.concat([aes.update(buffer), aes.final()])
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// encrypt AES 256 CBC; where a random IV is prefixed to the buffer
|
|
99
|
+
export function aesEncrypt(buffer: Buffer | Uint8Array, key: Buffer) {
|
|
100
|
+
const IV = randomBytes(16)
|
|
101
|
+
const aes = createCipheriv('aes-256-cbc', key, IV)
|
|
102
|
+
return Buffer.concat([IV, aes.update(buffer), aes.final()]) // prefix IV to the buffer
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// encrypt AES 256 CBC with a given IV
|
|
106
|
+
export function aesEncrypWithIV(buffer: Buffer, key: Buffer, IV: Buffer) {
|
|
107
|
+
const aes = createCipheriv('aes-256-cbc', key, IV)
|
|
108
|
+
return Buffer.concat([aes.update(buffer), aes.final()]) // prefix IV to the buffer
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// sign HMAC using SHA 256
|
|
112
|
+
export function hmacSign(buffer: Buffer | Uint8Array, key: Buffer | Uint8Array, variant: 'sha256' | 'sha512' = 'sha256') {
|
|
113
|
+
return createHmac(variant, key).update(buffer).digest()
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function sha256(buffer: Buffer) {
|
|
117
|
+
return createHash('sha256').update(buffer).digest()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function md5(buffer: Buffer) {
|
|
121
|
+
return createHash('md5').update(buffer).digest()
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// HKDF key expansion
|
|
125
|
+
export function hkdf(buffer: Uint8Array | Buffer, expandedLength: number, info: { salt?: Buffer, info?: string }) {
|
|
126
|
+
return HKDF(!Buffer.isBuffer(buffer) ? Buffer.from(buffer) : buffer, expandedLength, info)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function derivePairingCodeKey(pairingCode: string, salt: Buffer) {
|
|
130
|
+
return pbkdf2Sync(pairingCode, salt, 2 << 16, 32, 'sha256')
|
|
131
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.decryptMessageNode = void 0;
|
|
13
|
+
exports.decodeMessageNode = decodeMessageNode;
|
|
14
|
+
const boom_1 = require("@hapi/boom");
|
|
15
|
+
const WAProto_1 = require("../../WAProto");
|
|
16
|
+
const WABinary_1 = require("../WABinary");
|
|
17
|
+
const generics_1 = require("./generics");
|
|
18
|
+
const NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node';
|
|
19
|
+
/**
|
|
20
|
+
* Decode the received node as a message.
|
|
21
|
+
* @note this will only parse the message, not decrypt it
|
|
22
|
+
*/
|
|
23
|
+
function decodeMessageNode(stanza, meId, meLid) {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
let msgType;
|
|
26
|
+
let chatId;
|
|
27
|
+
let author;
|
|
28
|
+
const msgId = stanza.attrs.id;
|
|
29
|
+
const from = stanza.attrs.from;
|
|
30
|
+
const participant = stanza.attrs.participant;
|
|
31
|
+
const recipient = stanza.attrs.recipient;
|
|
32
|
+
const isMe = (jid) => (0, WABinary_1.areJidsSameUser)(jid, meId);
|
|
33
|
+
const isMeLid = (jid) => (0, WABinary_1.areJidsSameUser)(jid, meLid);
|
|
34
|
+
if ((0, WABinary_1.isJidUser)(from)) {
|
|
35
|
+
if (recipient) {
|
|
36
|
+
if (!isMe(from)) {
|
|
37
|
+
throw new boom_1.Boom('receipient present, but msg not from me', { data: stanza });
|
|
38
|
+
}
|
|
39
|
+
chatId = recipient;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
chatId = from;
|
|
43
|
+
}
|
|
44
|
+
msgType = 'chat';
|
|
45
|
+
author = from;
|
|
46
|
+
}
|
|
47
|
+
else if ((0, WABinary_1.isLidUser)(from)) {
|
|
48
|
+
if (recipient) {
|
|
49
|
+
if (!isMeLid(from)) {
|
|
50
|
+
throw new boom_1.Boom('receipient present, but msg not from me', { data: stanza });
|
|
51
|
+
}
|
|
52
|
+
chatId = recipient;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
chatId = from;
|
|
56
|
+
}
|
|
57
|
+
msgType = 'chat';
|
|
58
|
+
author = from;
|
|
59
|
+
}
|
|
60
|
+
else if ((0, WABinary_1.isJidGroup)(from)) {
|
|
61
|
+
if (!participant) {
|
|
62
|
+
throw new boom_1.Boom('No participant in group message');
|
|
63
|
+
}
|
|
64
|
+
msgType = 'group';
|
|
65
|
+
author = participant;
|
|
66
|
+
chatId = from;
|
|
67
|
+
}
|
|
68
|
+
else if ((0, WABinary_1.isJidBroadcast)(from)) {
|
|
69
|
+
if (!participant) {
|
|
70
|
+
throw new boom_1.Boom('No participant in group message');
|
|
71
|
+
}
|
|
72
|
+
const isParticipantMe = isMe(participant);
|
|
73
|
+
if ((0, WABinary_1.isJidStatusBroadcast)(from)) {
|
|
74
|
+
msgType = isParticipantMe ? 'direct_peer_status' : 'other_status';
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
msgType = isParticipantMe ? 'peer_broadcast' : 'other_broadcast';
|
|
78
|
+
}
|
|
79
|
+
chatId = from;
|
|
80
|
+
author = participant;
|
|
81
|
+
}
|
|
82
|
+
else if ((0, WABinary_1.isJidNewsLetter)(from)) {
|
|
83
|
+
msgType = 'newsletter';
|
|
84
|
+
author = from;
|
|
85
|
+
chatId = from;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
throw new boom_1.Boom('Unknown message type', { data: stanza });
|
|
89
|
+
}
|
|
90
|
+
const fromMe = (0, WABinary_1.isJidNewsLetter)(from) ? !!((_a = stanza.attrs) === null || _a === void 0 ? void 0 : _a.is_sender) : ((0, WABinary_1.isLidUser)(from) ? isMeLid : isMe)(stanza.attrs.participant || stanza.attrs.from);
|
|
91
|
+
const pushname = stanza.attrs.notify;
|
|
92
|
+
const key = {
|
|
93
|
+
remoteJid: chatId,
|
|
94
|
+
fromMe,
|
|
95
|
+
id: msgId,
|
|
96
|
+
participant
|
|
97
|
+
};
|
|
98
|
+
const fullMessage = {
|
|
99
|
+
key,
|
|
100
|
+
messageTimestamp: +stanza.attrs.t,
|
|
101
|
+
pushName: pushname,
|
|
102
|
+
broadcast: (0, WABinary_1.isJidBroadcast)(from)
|
|
103
|
+
};
|
|
104
|
+
if (msgType === 'newsletter') {
|
|
105
|
+
fullMessage.newsletterServerId = +((_b = stanza.attrs) === null || _b === void 0 ? void 0 : _b.server_id);
|
|
106
|
+
}
|
|
107
|
+
if (key.fromMe) {
|
|
108
|
+
fullMessage.status = WAProto_1.proto.WebMessageInfo.Status.SERVER_ACK;
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
fullMessage,
|
|
112
|
+
author,
|
|
113
|
+
sender: msgType === 'chat' ? author : chatId
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
117
|
+
const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid);
|
|
118
|
+
return {
|
|
119
|
+
fullMessage,
|
|
120
|
+
category: stanza.attrs.category,
|
|
121
|
+
author,
|
|
122
|
+
decrypt() {
|
|
123
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
124
|
+
var _a;
|
|
125
|
+
let decryptables = 0;
|
|
126
|
+
function processSenderKeyDistribution(msg) {
|
|
127
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
+
if (msg.senderKeyDistributionMessage) {
|
|
129
|
+
try {
|
|
130
|
+
yield repository.processSenderKeyDistributionMessage({
|
|
131
|
+
authorJid: author,
|
|
132
|
+
item: msg.senderKeyDistributionMessage
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
catch (err) {
|
|
136
|
+
logger.error({ key: fullMessage.key, err }, 'failed to process senderKeyDistribution');
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
if ((0, WABinary_1.isJidNewsLetter)(fullMessage.key.remoteJid)) {
|
|
142
|
+
const node = (0, WABinary_1.getBinaryNodeChild)(stanza, 'plaintext');
|
|
143
|
+
const msg = WAProto_1.proto.Message.decode(node === null || node === void 0 ? void 0 : node.content);
|
|
144
|
+
yield processSenderKeyDistribution(msg);
|
|
145
|
+
fullMessage.message = msg;
|
|
146
|
+
decryptables += 1;
|
|
147
|
+
}
|
|
148
|
+
else if (Array.isArray(stanza.content)) {
|
|
149
|
+
for (const { tag, attrs, content } of stanza.content) {
|
|
150
|
+
if (tag === 'verified_name' && content instanceof Uint8Array) {
|
|
151
|
+
const cert = WAProto_1.proto.VerifiedNameCertificate.decode(content);
|
|
152
|
+
const details = WAProto_1.proto.VerifiedNameCertificate.Details.decode(cert.details);
|
|
153
|
+
fullMessage.verifiedBizName = details.verifiedName;
|
|
154
|
+
}
|
|
155
|
+
if (tag !== 'enc') {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (!(content instanceof Uint8Array)) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
decryptables += 1;
|
|
162
|
+
let msgBuffer;
|
|
163
|
+
try {
|
|
164
|
+
const e2eType = attrs.type;
|
|
165
|
+
switch (e2eType) {
|
|
166
|
+
case 'skmsg':
|
|
167
|
+
msgBuffer = yield repository.decryptGroupMessage({
|
|
168
|
+
group: sender,
|
|
169
|
+
authorJid: author,
|
|
170
|
+
msg: content
|
|
171
|
+
});
|
|
172
|
+
break;
|
|
173
|
+
case 'pkmsg':
|
|
174
|
+
case 'msg':
|
|
175
|
+
const user = (0, WABinary_1.isJidUser)(sender) ? sender : author;
|
|
176
|
+
msgBuffer = yield repository.decryptMessage({
|
|
177
|
+
jid: user,
|
|
178
|
+
type: e2eType,
|
|
179
|
+
ciphertext: content
|
|
180
|
+
});
|
|
181
|
+
break;
|
|
182
|
+
default:
|
|
183
|
+
throw new Error(`Unknown e2e type: ${e2eType}`);
|
|
184
|
+
}
|
|
185
|
+
let msg = WAProto_1.proto.Message.decode((0, generics_1.unpadRandomMax16)(msgBuffer));
|
|
186
|
+
msg = ((_a = msg.deviceSentMessage) === null || _a === void 0 ? void 0 : _a.message) || msg;
|
|
187
|
+
yield processSenderKeyDistribution(msg);
|
|
188
|
+
if (fullMessage.message) {
|
|
189
|
+
Object.assign(fullMessage.message, msg);
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
fullMessage.message = msg;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
catch (err) {
|
|
196
|
+
logger.error({ key: fullMessage.key, err }, 'failed to decrypt message');
|
|
197
|
+
fullMessage.messageStubType = WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
198
|
+
fullMessage.messageStubParameters = [err.message];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// if nothing was found to decrypt
|
|
203
|
+
if (!decryptables) {
|
|
204
|
+
fullMessage.messageStubType = WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
205
|
+
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT, JSON.stringify(stanza, generics_1.BufferJSON.replacer)];
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
exports.decryptMessageNode = decryptMessageNode;
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom'
|
|
2
|
+
import { Logger } from 'pino'
|
|
3
|
+
import { proto } from '../../WAProto'
|
|
4
|
+
import { SignalRepository, WAMessageKey } from '../Types'
|
|
5
|
+
import { areJidsSameUser, BinaryNode, getBinaryNodeChild, isJidBroadcast, isJidGroup, isJidNewsLetter, isJidStatusBroadcast, isJidUser, isLidUser } from '../WABinary'
|
|
6
|
+
import { BufferJSON, unpadRandomMax16 } from './generics'
|
|
7
|
+
|
|
8
|
+
const NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node'
|
|
9
|
+
|
|
10
|
+
type MessageType = 'chat' | 'peer_broadcast' | 'other_broadcast' | 'group' | 'direct_peer_status' | 'other_status' | 'newsletter'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Decode the received node as a message.
|
|
14
|
+
* @note this will only parse the message, not decrypt it
|
|
15
|
+
*/
|
|
16
|
+
export function decodeMessageNode(
|
|
17
|
+
stanza: BinaryNode,
|
|
18
|
+
meId: string,
|
|
19
|
+
meLid: string
|
|
20
|
+
) {
|
|
21
|
+
let msgType: MessageType
|
|
22
|
+
let chatId: string
|
|
23
|
+
let author: string
|
|
24
|
+
|
|
25
|
+
const msgId = stanza.attrs.id
|
|
26
|
+
const from = stanza.attrs.from
|
|
27
|
+
const participant: string | undefined = stanza.attrs.participant
|
|
28
|
+
const recipient: string | undefined = stanza.attrs.recipient
|
|
29
|
+
|
|
30
|
+
const isMe = (jid: string) => areJidsSameUser(jid, meId)
|
|
31
|
+
const isMeLid = (jid: string) => areJidsSameUser(jid, meLid)
|
|
32
|
+
|
|
33
|
+
if(isJidUser(from)) {
|
|
34
|
+
if(recipient) {
|
|
35
|
+
if(!isMe(from)) {
|
|
36
|
+
throw new Boom('receipient present, but msg not from me', { data: stanza })
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
chatId = recipient
|
|
40
|
+
} else {
|
|
41
|
+
chatId = from
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
msgType = 'chat'
|
|
45
|
+
author = from
|
|
46
|
+
} else if(isLidUser(from)) {
|
|
47
|
+
if(recipient) {
|
|
48
|
+
if(!isMeLid(from)) {
|
|
49
|
+
throw new Boom('receipient present, but msg not from me', { data: stanza })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
chatId = recipient
|
|
53
|
+
} else {
|
|
54
|
+
chatId = from
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
msgType = 'chat'
|
|
58
|
+
author = from
|
|
59
|
+
} else if(isJidGroup(from)) {
|
|
60
|
+
if(!participant) {
|
|
61
|
+
throw new Boom('No participant in group message')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
msgType = 'group'
|
|
65
|
+
author = participant
|
|
66
|
+
chatId = from
|
|
67
|
+
} else if(isJidBroadcast(from)) {
|
|
68
|
+
if(!participant) {
|
|
69
|
+
throw new Boom('No participant in group message')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const isParticipantMe = isMe(participant)
|
|
73
|
+
if(isJidStatusBroadcast(from)) {
|
|
74
|
+
msgType = isParticipantMe ? 'direct_peer_status' : 'other_status'
|
|
75
|
+
} else {
|
|
76
|
+
msgType = isParticipantMe ? 'peer_broadcast' : 'other_broadcast'
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
chatId = from
|
|
80
|
+
author = participant
|
|
81
|
+
} else if (isJidNewsLetter(from)) {
|
|
82
|
+
msgType = 'newsletter'
|
|
83
|
+
author = from
|
|
84
|
+
chatId = from
|
|
85
|
+
} else {
|
|
86
|
+
throw new Boom('Unknown message type', { data: stanza })
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const fromMe = isJidNewsLetter(from) ? !!stanza.attrs?.is_sender : (isLidUser(from) ? isMeLid : isMe)(stanza.attrs.participant || stanza.attrs.from)
|
|
90
|
+
const pushname = stanza.attrs.notify
|
|
91
|
+
|
|
92
|
+
const key: WAMessageKey = {
|
|
93
|
+
remoteJid: chatId,
|
|
94
|
+
fromMe,
|
|
95
|
+
id: msgId,
|
|
96
|
+
participant
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const fullMessage: proto.IWebMessageInfo = {
|
|
100
|
+
key,
|
|
101
|
+
messageTimestamp: +stanza.attrs.t,
|
|
102
|
+
pushName: pushname,
|
|
103
|
+
broadcast: isJidBroadcast(from)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (msgType === 'newsletter') {
|
|
107
|
+
fullMessage.newsletterServerId = +stanza.attrs?.server_id
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if(key.fromMe) {
|
|
111
|
+
fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
fullMessage,
|
|
116
|
+
author,
|
|
117
|
+
sender: msgType === 'chat' ? author : chatId
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export const decryptMessageNode = (
|
|
122
|
+
stanza: BinaryNode,
|
|
123
|
+
meId: string,
|
|
124
|
+
meLid: string,
|
|
125
|
+
repository: SignalRepository,
|
|
126
|
+
logger: Logger
|
|
127
|
+
) => {
|
|
128
|
+
const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid)
|
|
129
|
+
return {
|
|
130
|
+
fullMessage,
|
|
131
|
+
category: stanza.attrs.category,
|
|
132
|
+
author,
|
|
133
|
+
async decrypt() {
|
|
134
|
+
let decryptables = 0
|
|
135
|
+
|
|
136
|
+
async function processSenderKeyDistribution(msg: proto.IMessage) {
|
|
137
|
+
if(msg.senderKeyDistributionMessage) {
|
|
138
|
+
try {
|
|
139
|
+
await repository.processSenderKeyDistributionMessage({
|
|
140
|
+
authorJid: author,
|
|
141
|
+
item: msg.senderKeyDistributionMessage
|
|
142
|
+
})
|
|
143
|
+
} catch(err) {
|
|
144
|
+
logger.error({ key: fullMessage.key, err }, 'failed to process senderKeyDistribution')
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (isJidNewsLetter(fullMessage.key.remoteJid!)) {
|
|
150
|
+
const node = getBinaryNodeChild(stanza, 'plaintext')
|
|
151
|
+
const msg = proto.Message.decode(node?.content as Uint8Array)
|
|
152
|
+
|
|
153
|
+
await processSenderKeyDistribution(msg)
|
|
154
|
+
|
|
155
|
+
fullMessage.message = msg
|
|
156
|
+
decryptables += 1
|
|
157
|
+
} else if(Array.isArray(stanza.content)) {
|
|
158
|
+
for(const { tag, attrs, content } of stanza.content) {
|
|
159
|
+
if(tag === 'verified_name' && content instanceof Uint8Array) {
|
|
160
|
+
const cert = proto.VerifiedNameCertificate.decode(content)
|
|
161
|
+
const details = proto.VerifiedNameCertificate.Details.decode(cert.details)
|
|
162
|
+
fullMessage.verifiedBizName = details.verifiedName
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if(tag !== 'enc') {
|
|
166
|
+
continue
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if(!(content instanceof Uint8Array)) {
|
|
170
|
+
continue
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
decryptables += 1
|
|
175
|
+
|
|
176
|
+
let msgBuffer: Uint8Array
|
|
177
|
+
|
|
178
|
+
try {
|
|
179
|
+
const e2eType = attrs.type
|
|
180
|
+
switch (e2eType) {
|
|
181
|
+
case 'skmsg':
|
|
182
|
+
msgBuffer = await repository.decryptGroupMessage({
|
|
183
|
+
group: sender,
|
|
184
|
+
authorJid: author,
|
|
185
|
+
msg: content
|
|
186
|
+
})
|
|
187
|
+
break
|
|
188
|
+
case 'pkmsg':
|
|
189
|
+
case 'msg':
|
|
190
|
+
const user = isJidUser(sender) ? sender : author
|
|
191
|
+
msgBuffer = await repository.decryptMessage({
|
|
192
|
+
jid: user,
|
|
193
|
+
type: e2eType,
|
|
194
|
+
ciphertext: content
|
|
195
|
+
})
|
|
196
|
+
break
|
|
197
|
+
default:
|
|
198
|
+
throw new Error(`Unknown e2e type: ${e2eType}`)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
let msg: proto.IMessage = proto.Message.decode(unpadRandomMax16(msgBuffer))
|
|
202
|
+
msg = msg.deviceSentMessage?.message || msg
|
|
203
|
+
await processSenderKeyDistribution(msg)
|
|
204
|
+
|
|
205
|
+
if(fullMessage.message) {
|
|
206
|
+
Object.assign(fullMessage.message, msg)
|
|
207
|
+
} else {
|
|
208
|
+
fullMessage.message = msg
|
|
209
|
+
}
|
|
210
|
+
} catch(err) {
|
|
211
|
+
logger.error(
|
|
212
|
+
{ key: fullMessage.key, err },
|
|
213
|
+
'failed to decrypt message'
|
|
214
|
+
)
|
|
215
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
|
|
216
|
+
fullMessage.messageStubParameters = [err.message]
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// if nothing was found to decrypt
|
|
222
|
+
if(!decryptables) {
|
|
223
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
|
|
224
|
+
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT, JSON.stringify(stanza, BufferJSON.replacer)]
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|