@vinzsocket/baileys 2.8.10 → 2.8.20
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/lib/Defaults/index.js +75 -73
- package/lib/Signal/Group/ciphertext-message.js +9 -9
- package/lib/Signal/Group/group-session-builder.js +21 -21
- package/lib/Signal/Group/group_cipher.js +79 -81
- package/lib/Signal/Group/index.js +2 -2
- package/lib/Signal/Group/keyhelper.js +9 -9
- package/lib/Signal/Group/queue-job.js +43 -46
- package/lib/Signal/Group/sender-chain-key.js +24 -25
- package/lib/Signal/Group/sender-key-distribution-message.js +50 -58
- package/lib/Signal/Group/sender-key-message.js +55 -60
- package/lib/Signal/Group/sender-key-name.js +38 -39
- package/lib/Signal/Group/sender-key-record.js +39 -41
- package/lib/Signal/Group/sender-key-state.js +85 -89
- package/lib/Signal/Group/sender-message-key.js +22 -22
- package/lib/Signal/libsignal.js +387 -292
- package/lib/Signal/lid-mapping.js +98 -93
- package/lib/Socket/Client/types.js +6 -6
- package/lib/Socket/Client/websocket.js +44 -44
- package/lib/Socket/business.js +350 -365
- package/lib/Socket/chats.js +1019 -914
- package/lib/Socket/communities.js +454 -376
- package/lib/Socket/groups.js +378 -310
- package/lib/Socket/index.js +11 -11
- package/lib/Socket/messages-recv.js +1355 -1182
- package/lib/Socket/messages-send.js +1224 -1081
- package/lib/Socket/mex.js +40 -34
- package/lib/Socket/newsletter.js +212 -169
- package/lib/Socket/socket.js +896 -757
- package/lib/Store/index.js +1 -1
- package/lib/Store/make-cache-manager-store.js +75 -79
- package/lib/Store/make-in-memory-store.js +450 -389
- package/lib/Store/make-ordered-dictionary.js +73 -76
- package/lib/Store/object-repository.js +24 -22
- package/lib/Types/Chat.js +1 -7
- package/lib/Types/Label.js +20 -20
- package/lib/Types/LabelAssociation.js +2 -2
- package/lib/Types/Message.js +2 -2
- package/lib/Types/Newsletter.js +24 -24
- package/lib/Types/Socket.js +1 -1
- package/lib/Types/State.js +8 -8
- package/lib/Types/index.js +10 -10
- package/lib/Utils/auth-utils.js +408 -401
- package/lib/Utils/baileys-event-stream.js +44 -35
- package/lib/Utils/business.js +200 -197
- package/lib/Utils/chat-utils.js +775 -708
- package/lib/Utils/crypto.js +97 -85
- package/lib/Utils/decode-wa-message.js +240 -227
- package/lib/Utils/event-buffer.js +547 -518
- package/lib/Utils/extended-messages.js +250 -203
- package/lib/Utils/generics.js +300 -289
- package/lib/Utils/history.js +76 -69
- package/lib/Utils/lid-resolver.js +47 -42
- package/lib/Utils/link-preview.js +75 -65
- package/lib/Utils/logger.js +3 -1
- package/lib/Utils/lt-hash.js +38 -34
- package/lib/Utils/make-mutex.js +32 -34
- package/lib/Utils/mention.js +79 -86
- package/lib/Utils/message-retry-manager.js +148 -136
- package/lib/Utils/messages-media.js +652 -562
- package/lib/Utils/messages.js +753 -707
- package/lib/Utils/noise-handler.js +156 -135
- package/lib/Utils/process-message.js +404 -341
- package/lib/Utils/signal.js +180 -118
- package/lib/Utils/use-multi-file-auth-state.js +87 -91
- package/lib/Utils/validate-connection.js +167 -142
- package/lib/WABinary/constants.js +28 -1293
- package/lib/WABinary/decode.js +220 -218
- package/lib/WABinary/encode.js +199 -201
- package/lib/WABinary/generic-utils.js +80 -77
- package/lib/WABinary/jid-utils.js +41 -35
- package/lib/WAM/BinaryInfo.js +7 -7
- package/lib/WAM/constants.js +1 -15241
- package/lib/WAM/encode.js +124 -131
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +51 -47
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -22
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -32
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -19
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -43
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +17 -17
- package/lib/WAUSync/USyncQuery.js +82 -84
- package/lib/WAUSync/USyncUser.js +20 -20
- package/package.json +5 -2
|
@@ -3,144 +3,165 @@ import { proto } from '../../WAProto/index.js';
|
|
|
3
3
|
import { NOISE_MODE, WA_CERT_DETAILS } from '../Defaults/index.js';
|
|
4
4
|
import { decodeBinaryNode } from '../WABinary/index.js';
|
|
5
5
|
import { aesDecryptGCM, aesEncryptGCM, Curve, hkdf, sha256 } from './crypto.js';
|
|
6
|
-
const generateIV =
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
const generateIV = counter => {
|
|
7
|
+
const iv = new ArrayBuffer(12);
|
|
8
|
+
new DataView(iv).setUint32(8, counter);
|
|
9
|
+
return new Uint8Array(iv);
|
|
10
10
|
};
|
|
11
|
-
export const makeNoiseHandler = ({
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
export const makeNoiseHandler = ({
|
|
12
|
+
keyPair: {
|
|
13
|
+
private: privateKey,
|
|
14
|
+
public: publicKey
|
|
15
|
+
},
|
|
16
|
+
NOISE_HEADER,
|
|
17
|
+
logger,
|
|
18
|
+
routingInfo
|
|
19
|
+
}) => {
|
|
20
|
+
logger = logger.child({
|
|
21
|
+
class: 'ns'
|
|
22
|
+
});
|
|
23
|
+
const authenticate = data => {
|
|
24
|
+
if (!isFinished) {
|
|
25
|
+
hash = sha256(Buffer.concat([hash, data]));
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const encrypt = plaintext => {
|
|
29
|
+
const result = aesEncryptGCM(plaintext, encKey, generateIV(writeCounter), hash);
|
|
30
|
+
writeCounter += 1;
|
|
31
|
+
authenticate(result);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
const decrypt = ciphertext => {
|
|
35
|
+
// before the handshake is finished, we use the same counter
|
|
36
|
+
// after handshake, the counters are different
|
|
37
|
+
const iv = generateIV(isFinished ? readCounter : writeCounter);
|
|
38
|
+
const result = aesDecryptGCM(ciphertext, decKey, iv, hash);
|
|
39
|
+
if (isFinished) {
|
|
40
|
+
readCounter += 1;
|
|
41
|
+
} else {
|
|
42
|
+
writeCounter += 1;
|
|
43
|
+
}
|
|
44
|
+
authenticate(ciphertext);
|
|
45
|
+
return result;
|
|
46
|
+
};
|
|
47
|
+
const localHKDF = async data => {
|
|
48
|
+
const key = await hkdf(Buffer.from(data), 64, {
|
|
49
|
+
salt,
|
|
50
|
+
info: ''
|
|
51
|
+
});
|
|
52
|
+
return [key.slice(0, 32), key.slice(32)];
|
|
53
|
+
};
|
|
54
|
+
const mixIntoKey = async data => {
|
|
55
|
+
const [write, read] = await localHKDF(data);
|
|
56
|
+
salt = write;
|
|
57
|
+
encKey = read;
|
|
58
|
+
decKey = read;
|
|
59
|
+
readCounter = 0;
|
|
60
|
+
writeCounter = 0;
|
|
61
|
+
};
|
|
62
|
+
const finishInit = async () => {
|
|
63
|
+
const [write, read] = await localHKDF(new Uint8Array(0));
|
|
64
|
+
encKey = write;
|
|
65
|
+
decKey = read;
|
|
66
|
+
hash = Buffer.from([]);
|
|
67
|
+
readCounter = 0;
|
|
68
|
+
writeCounter = 0;
|
|
69
|
+
isFinished = true;
|
|
70
|
+
};
|
|
71
|
+
const data = Buffer.from(NOISE_MODE);
|
|
72
|
+
let hash = data.byteLength === 32 ? data : sha256(data);
|
|
73
|
+
let salt = hash;
|
|
74
|
+
let encKey = hash;
|
|
75
|
+
let decKey = hash;
|
|
76
|
+
let readCounter = 0;
|
|
77
|
+
let writeCounter = 0;
|
|
78
|
+
let isFinished = false;
|
|
79
|
+
let sentIntro = false;
|
|
80
|
+
let inBytes = Buffer.alloc(0);
|
|
81
|
+
authenticate(NOISE_HEADER);
|
|
82
|
+
authenticate(publicKey);
|
|
83
|
+
return {
|
|
84
|
+
encrypt,
|
|
85
|
+
decrypt,
|
|
86
|
+
authenticate,
|
|
87
|
+
mixIntoKey,
|
|
88
|
+
finishInit,
|
|
89
|
+
processHandshake: async ({
|
|
90
|
+
serverHello
|
|
91
|
+
}, noiseKey) => {
|
|
92
|
+
authenticate(serverHello.ephemeral);
|
|
93
|
+
await mixIntoKey(Curve.sharedKey(privateKey, serverHello.ephemeral));
|
|
94
|
+
const decStaticContent = decrypt(serverHello.static);
|
|
95
|
+
await mixIntoKey(Curve.sharedKey(privateKey, decStaticContent));
|
|
96
|
+
const certDecoded = decrypt(serverHello.payload);
|
|
97
|
+
const {
|
|
98
|
+
intermediate: certIntermediate
|
|
99
|
+
} = proto.CertChain.decode(certDecoded);
|
|
100
|
+
const {
|
|
101
|
+
issuerSerial
|
|
102
|
+
} = proto.CertChain.NoiseCertificate.Details.decode(certIntermediate.details);
|
|
103
|
+
if (issuerSerial !== WA_CERT_DETAILS.SERIAL) {
|
|
104
|
+
throw new Boom('certification match failed', {
|
|
105
|
+
statusCode: 400
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
const keyEnc = encrypt(noiseKey.public);
|
|
109
|
+
await mixIntoKey(Curve.sharedKey(noiseKey.private, serverHello.ephemeral));
|
|
110
|
+
return keyEnc;
|
|
111
|
+
},
|
|
112
|
+
encodeFrame: data => {
|
|
113
|
+
if (isFinished) {
|
|
114
|
+
data = encrypt(data);
|
|
115
|
+
}
|
|
116
|
+
let header;
|
|
117
|
+
if (routingInfo) {
|
|
118
|
+
header = Buffer.alloc(7);
|
|
119
|
+
header.write('ED', 0, 'utf8');
|
|
120
|
+
header.writeUint8(0, 2);
|
|
121
|
+
header.writeUint8(1, 3);
|
|
122
|
+
header.writeUint8(routingInfo.byteLength >> 16, 4);
|
|
123
|
+
header.writeUint16BE(routingInfo.byteLength & 65535, 5);
|
|
124
|
+
header = Buffer.concat([header, routingInfo, NOISE_HEADER]);
|
|
125
|
+
} else {
|
|
126
|
+
header = Buffer.from(NOISE_HEADER);
|
|
127
|
+
}
|
|
128
|
+
const introSize = sentIntro ? 0 : header.length;
|
|
129
|
+
const frame = Buffer.alloc(introSize + 3 + data.byteLength);
|
|
130
|
+
if (!sentIntro) {
|
|
131
|
+
frame.set(header);
|
|
132
|
+
sentIntro = true;
|
|
133
|
+
}
|
|
134
|
+
frame.writeUInt8(data.byteLength >> 16, introSize);
|
|
135
|
+
frame.writeUInt16BE(65535 & data.byteLength, introSize + 1);
|
|
136
|
+
frame.set(data, introSize + 3);
|
|
137
|
+
return frame;
|
|
138
|
+
},
|
|
139
|
+
decodeFrame: async (newData, onFrame) => {
|
|
140
|
+
// the binary protocol uses its own framing mechanism
|
|
141
|
+
// on top of the WS frames
|
|
142
|
+
// so we get this data and separate out the frames
|
|
143
|
+
const getBytesSize = () => {
|
|
144
|
+
if (inBytes.length >= 3) {
|
|
145
|
+
return inBytes.readUInt8() << 16 | inBytes.readUInt16BE(1);
|
|
16
146
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const decrypt = (ciphertext) => {
|
|
25
|
-
// before the handshake is finished, we use the same counter
|
|
26
|
-
// after handshake, the counters are different
|
|
27
|
-
const iv = generateIV(isFinished ? readCounter : writeCounter);
|
|
28
|
-
const result = aesDecryptGCM(ciphertext, decKey, iv, hash);
|
|
147
|
+
};
|
|
148
|
+
inBytes = Buffer.concat([inBytes, newData]);
|
|
149
|
+
logger.trace(`recv ${newData.length} bytes, total recv ${inBytes.length} bytes`);
|
|
150
|
+
let size = getBytesSize();
|
|
151
|
+
while (size && inBytes.length >= size + 3) {
|
|
152
|
+
let frame = inBytes.slice(3, size + 3);
|
|
153
|
+
inBytes = inBytes.slice(size + 3);
|
|
29
154
|
if (isFinished) {
|
|
30
|
-
|
|
155
|
+
const result = decrypt(frame);
|
|
156
|
+
frame = await decodeBinaryNode(result);
|
|
31
157
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return [key.slice(0, 32), key.slice(32)];
|
|
41
|
-
};
|
|
42
|
-
const mixIntoKey = async (data) => {
|
|
43
|
-
const [write, read] = await localHKDF(data);
|
|
44
|
-
salt = write;
|
|
45
|
-
encKey = read;
|
|
46
|
-
decKey = read;
|
|
47
|
-
readCounter = 0;
|
|
48
|
-
writeCounter = 0;
|
|
49
|
-
};
|
|
50
|
-
const finishInit = async () => {
|
|
51
|
-
const [write, read] = await localHKDF(new Uint8Array(0));
|
|
52
|
-
encKey = write;
|
|
53
|
-
decKey = read;
|
|
54
|
-
hash = Buffer.from([]);
|
|
55
|
-
readCounter = 0;
|
|
56
|
-
writeCounter = 0;
|
|
57
|
-
isFinished = true;
|
|
58
|
-
};
|
|
59
|
-
const data = Buffer.from(NOISE_MODE);
|
|
60
|
-
let hash = data.byteLength === 32 ? data : sha256(data);
|
|
61
|
-
let salt = hash;
|
|
62
|
-
let encKey = hash;
|
|
63
|
-
let decKey = hash;
|
|
64
|
-
let readCounter = 0;
|
|
65
|
-
let writeCounter = 0;
|
|
66
|
-
let isFinished = false;
|
|
67
|
-
let sentIntro = false;
|
|
68
|
-
let inBytes = Buffer.alloc(0);
|
|
69
|
-
authenticate(NOISE_HEADER);
|
|
70
|
-
authenticate(publicKey);
|
|
71
|
-
return {
|
|
72
|
-
encrypt,
|
|
73
|
-
decrypt,
|
|
74
|
-
authenticate,
|
|
75
|
-
mixIntoKey,
|
|
76
|
-
finishInit,
|
|
77
|
-
processHandshake: async ({ serverHello }, noiseKey) => {
|
|
78
|
-
authenticate(serverHello.ephemeral);
|
|
79
|
-
await mixIntoKey(Curve.sharedKey(privateKey, serverHello.ephemeral));
|
|
80
|
-
const decStaticContent = decrypt(serverHello.static);
|
|
81
|
-
await mixIntoKey(Curve.sharedKey(privateKey, decStaticContent));
|
|
82
|
-
const certDecoded = decrypt(serverHello.payload);
|
|
83
|
-
const { intermediate: certIntermediate } = proto.CertChain.decode(certDecoded);
|
|
84
|
-
const { issuerSerial } = proto.CertChain.NoiseCertificate.Details.decode(certIntermediate.details);
|
|
85
|
-
if (issuerSerial !== WA_CERT_DETAILS.SERIAL) {
|
|
86
|
-
throw new Boom('certification match failed', { statusCode: 400 });
|
|
87
|
-
}
|
|
88
|
-
const keyEnc = encrypt(noiseKey.public);
|
|
89
|
-
await mixIntoKey(Curve.sharedKey(noiseKey.private, serverHello.ephemeral));
|
|
90
|
-
return keyEnc;
|
|
91
|
-
},
|
|
92
|
-
encodeFrame: (data) => {
|
|
93
|
-
if (isFinished) {
|
|
94
|
-
data = encrypt(data);
|
|
95
|
-
}
|
|
96
|
-
let header;
|
|
97
|
-
if (routingInfo) {
|
|
98
|
-
header = Buffer.alloc(7);
|
|
99
|
-
header.write('ED', 0, 'utf8');
|
|
100
|
-
header.writeUint8(0, 2);
|
|
101
|
-
header.writeUint8(1, 3);
|
|
102
|
-
header.writeUint8(routingInfo.byteLength >> 16, 4);
|
|
103
|
-
header.writeUint16BE(routingInfo.byteLength & 65535, 5);
|
|
104
|
-
header = Buffer.concat([header, routingInfo, NOISE_HEADER]);
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
header = Buffer.from(NOISE_HEADER);
|
|
108
|
-
}
|
|
109
|
-
const introSize = sentIntro ? 0 : header.length;
|
|
110
|
-
const frame = Buffer.alloc(introSize + 3 + data.byteLength);
|
|
111
|
-
if (!sentIntro) {
|
|
112
|
-
frame.set(header);
|
|
113
|
-
sentIntro = true;
|
|
114
|
-
}
|
|
115
|
-
frame.writeUInt8(data.byteLength >> 16, introSize);
|
|
116
|
-
frame.writeUInt16BE(65535 & data.byteLength, introSize + 1);
|
|
117
|
-
frame.set(data, introSize + 3);
|
|
118
|
-
return frame;
|
|
119
|
-
},
|
|
120
|
-
decodeFrame: async (newData, onFrame) => {
|
|
121
|
-
// the binary protocol uses its own framing mechanism
|
|
122
|
-
// on top of the WS frames
|
|
123
|
-
// so we get this data and separate out the frames
|
|
124
|
-
const getBytesSize = () => {
|
|
125
|
-
if (inBytes.length >= 3) {
|
|
126
|
-
return (inBytes.readUInt8() << 16) | inBytes.readUInt16BE(1);
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
inBytes = Buffer.concat([inBytes, newData]);
|
|
130
|
-
logger.trace(`recv ${newData.length} bytes, total recv ${inBytes.length} bytes`);
|
|
131
|
-
let size = getBytesSize();
|
|
132
|
-
while (size && inBytes.length >= size + 3) {
|
|
133
|
-
let frame = inBytes.slice(3, size + 3);
|
|
134
|
-
inBytes = inBytes.slice(size + 3);
|
|
135
|
-
if (isFinished) {
|
|
136
|
-
const result = decrypt(frame);
|
|
137
|
-
frame = await decodeBinaryNode(result);
|
|
138
|
-
}
|
|
139
|
-
logger.trace({ msg: frame?.attrs?.id }, 'recv frame');
|
|
140
|
-
onFrame(frame);
|
|
141
|
-
size = getBytesSize();
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
};
|
|
158
|
+
logger.trace({
|
|
159
|
+
msg: frame?.attrs?.id
|
|
160
|
+
}, 'recv frame');
|
|
161
|
+
onFrame(frame);
|
|
162
|
+
size = getBytesSize();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
145
166
|
};
|
|
146
167
|
//# sourceMappingURL=noise-handler.js.map
|