@bufferstudio/baileys-natsu 1.2.2
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/README.md +180 -0
- package/WAProto/index.d.ts +14017 -0
- package/WAProto/index.js +97691 -0
- package/lib/Defaults/index.d.ts +6 -0
- package/lib/Defaults/index.d.ts.map +1 -0
- package/lib/Defaults/index.js +42 -0
- package/lib/Defaults/index.js.map +1 -0
- package/lib/Signal/index.d.ts +48 -0
- package/lib/Signal/index.d.ts.map +1 -0
- package/lib/Signal/index.js +171 -0
- package/lib/Signal/index.js.map +1 -0
- package/lib/Socket/auth.d.ts +3 -0
- package/lib/Socket/auth.d.ts.map +1 -0
- package/lib/Socket/auth.js +110 -0
- package/lib/Socket/auth.js.map +1 -0
- package/lib/Socket/index.d.ts +3 -0
- package/lib/Socket/index.d.ts.map +1 -0
- package/lib/Socket/index.js +606 -0
- package/lib/Socket/index.js.map +1 -0
- package/lib/Socket/messages-recv.d.ts +4 -0
- package/lib/Socket/messages-recv.d.ts.map +1 -0
- package/lib/Socket/messages-recv.js +18 -0
- package/lib/Socket/messages-recv.js.map +1 -0
- package/lib/Store/index.d.ts +8 -0
- package/lib/Store/index.d.ts.map +1 -0
- package/lib/Store/index.js +39 -0
- package/lib/Store/index.js.map +1 -0
- package/lib/Types/Chat.d.ts +43 -0
- package/lib/Types/Chat.d.ts.map +1 -0
- package/lib/Types/Chat.js +2 -0
- package/lib/Types/Chat.js.map +1 -0
- package/lib/Types/Message.d.ts +57 -0
- package/lib/Types/Message.d.ts.map +1 -0
- package/lib/Types/Message.js +2 -0
- package/lib/Types/Message.js.map +1 -0
- package/lib/Types/Socket.d.ts +169 -0
- package/lib/Types/Socket.d.ts.map +1 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/Socket.js.map +1 -0
- package/lib/Types/index.d.ts +4 -0
- package/lib/Types/index.d.ts.map +1 -0
- package/lib/Types/index.js +4 -0
- package/lib/Types/index.js.map +1 -0
- package/lib/Utils/auth-utils.d.ts +12 -0
- package/lib/Utils/auth-utils.d.ts.map +1 -0
- package/lib/Utils/auth-utils.js +83 -0
- package/lib/Utils/auth-utils.js.map +1 -0
- package/lib/Utils/crypto.js +48 -0
- package/lib/Utils/decode-wa-message.d.ts +15 -0
- package/lib/Utils/decode-wa-message.d.ts.map +1 -0
- package/lib/Utils/decode-wa-message.js +174 -0
- package/lib/Utils/decode-wa-message.js.map +1 -0
- package/lib/Utils/generics.d.ts +11 -0
- package/lib/Utils/generics.d.ts.map +1 -0
- package/lib/Utils/generics.js +46 -0
- package/lib/Utils/generics.js.map +1 -0
- package/lib/Utils/index.d.ts +5 -0
- package/lib/Utils/index.d.ts.map +1 -0
- package/lib/Utils/index.js +5 -0
- package/lib/Utils/index.js.map +1 -0
- package/lib/Utils/logger.d.ts +5 -0
- package/lib/Utils/logger.d.ts.map +1 -0
- package/lib/Utils/logger.js +17 -0
- package/lib/Utils/logger.js.map +1 -0
- package/lib/Utils/message-features.js +206 -0
- package/lib/Utils/messages-media.d.ts +3 -0
- package/lib/Utils/messages-media.d.ts.map +1 -0
- package/lib/Utils/messages-media.js +62 -0
- package/lib/Utils/messages-media.js.map +1 -0
- package/lib/Utils/messages.d.ts +2 -0
- package/lib/Utils/messages.d.ts.map +1 -0
- package/lib/Utils/messages.js +6 -0
- package/lib/Utils/messages.js.map +1 -0
- package/lib/Utils/noise-handler.js +194 -0
- package/lib/Utils/process-message.d.ts +9 -0
- package/lib/Utils/process-message.d.ts.map +1 -0
- package/lib/Utils/process-message.js +63 -0
- package/lib/Utils/process-message.js.map +1 -0
- package/lib/Utils/signal.js +7 -0
- package/lib/Utils/validate-connection.js +197 -0
- package/lib/WABinary/binary-constants.d.ts +34 -0
- package/lib/WABinary/binary-constants.d.ts.map +1 -0
- package/lib/WABinary/binary-constants.js +538 -0
- package/lib/WABinary/binary-constants.js.map +1 -0
- package/lib/WABinary/decode.d.ts +19 -0
- package/lib/WABinary/decode.d.ts.map +1 -0
- package/lib/WABinary/decode.js +176 -0
- package/lib/WABinary/decode.js.map +1 -0
- package/lib/WABinary/encode.d.ts +3 -0
- package/lib/WABinary/encode.d.ts.map +1 -0
- package/lib/WABinary/encode.js +142 -0
- package/lib/WABinary/encode.js.map +1 -0
- package/lib/WABinary/index.d.ts +6 -0
- package/lib/WABinary/index.d.ts.map +1 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/index.js.map +1 -0
- package/lib/WABinary/jid-utils.d.ts +24 -0
- package/lib/WABinary/jid-utils.d.ts.map +1 -0
- package/lib/WABinary/jid-utils.js +74 -0
- package/lib/WABinary/jid-utils.js.map +1 -0
- package/lib/WABinary/types.d.ts +16 -0
- package/lib/WABinary/types.d.ts.map +1 -0
- package/lib/WABinary/types.js +33 -0
- package/lib/WABinary/types.js.map +1 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +12 -0
- package/lib/index.js.map +1 -0
- package/package.json +48 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
import { NOISE_MODE, WA_CERT_DETAILS } from '../Defaults/index.js';
|
|
4
|
+
import { decodeBinaryNode } from '../WABinary/index.js';
|
|
5
|
+
import { aesDecryptGCM, aesEncryptGCM, Curve, hkdf, sha256 } from './crypto.js';
|
|
6
|
+
const IV_LENGTH = 12;
|
|
7
|
+
const EMPTY_BUFFER = Buffer.alloc(0);
|
|
8
|
+
const generateIV = (counter) => {
|
|
9
|
+
const iv = new ArrayBuffer(IV_LENGTH);
|
|
10
|
+
new DataView(iv).setUint32(8, counter);
|
|
11
|
+
return new Uint8Array(iv);
|
|
12
|
+
};
|
|
13
|
+
class TransportState {
|
|
14
|
+
constructor(encKey, decKey) {
|
|
15
|
+
this.encKey = encKey;
|
|
16
|
+
this.decKey = decKey;
|
|
17
|
+
this.readCounter = 0;
|
|
18
|
+
this.writeCounter = 0;
|
|
19
|
+
this.iv = new Uint8Array(IV_LENGTH);
|
|
20
|
+
}
|
|
21
|
+
encrypt(plaintext) {
|
|
22
|
+
const c = this.writeCounter++;
|
|
23
|
+
this.iv[8] = (c >>> 24) & 0xff;
|
|
24
|
+
this.iv[9] = (c >>> 16) & 0xff;
|
|
25
|
+
this.iv[10] = (c >>> 8) & 0xff;
|
|
26
|
+
this.iv[11] = c & 0xff;
|
|
27
|
+
return aesEncryptGCM(plaintext, this.encKey, this.iv, EMPTY_BUFFER);
|
|
28
|
+
}
|
|
29
|
+
decrypt(ciphertext) {
|
|
30
|
+
const c = this.readCounter++;
|
|
31
|
+
this.iv[8] = (c >>> 24) & 0xff;
|
|
32
|
+
this.iv[9] = (c >>> 16) & 0xff;
|
|
33
|
+
this.iv[10] = (c >>> 8) & 0xff;
|
|
34
|
+
this.iv[11] = c & 0xff;
|
|
35
|
+
return aesDecryptGCM(ciphertext, this.decKey, this.iv, EMPTY_BUFFER);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey }, NOISE_HEADER, logger, routingInfo }) => {
|
|
39
|
+
logger = logger.child({ class: 'ns' });
|
|
40
|
+
const data = Buffer.from(NOISE_MODE);
|
|
41
|
+
let hash = data.byteLength === 32 ? data : sha256(data);
|
|
42
|
+
let salt = hash;
|
|
43
|
+
let encKey = hash;
|
|
44
|
+
let decKey = hash;
|
|
45
|
+
let counter = 0;
|
|
46
|
+
let sentIntro = false;
|
|
47
|
+
let inBytes = Buffer.alloc(0);
|
|
48
|
+
let transport = null;
|
|
49
|
+
let isWaitingForTransport = false;
|
|
50
|
+
let pendingOnFrame = null;
|
|
51
|
+
let introHeader;
|
|
52
|
+
if (routingInfo) {
|
|
53
|
+
introHeader = Buffer.alloc(7 + routingInfo.byteLength + NOISE_HEADER.length);
|
|
54
|
+
introHeader.write('ED', 0, 'utf8');
|
|
55
|
+
introHeader.writeUint8(0, 2);
|
|
56
|
+
introHeader.writeUint8(1, 3);
|
|
57
|
+
introHeader.writeUint8(routingInfo.byteLength >> 16, 4);
|
|
58
|
+
introHeader.writeUint16BE(routingInfo.byteLength & 65535, 5);
|
|
59
|
+
introHeader.set(routingInfo, 7);
|
|
60
|
+
introHeader.set(NOISE_HEADER, 7 + routingInfo.byteLength);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
introHeader = Buffer.from(NOISE_HEADER);
|
|
64
|
+
}
|
|
65
|
+
const authenticate = (data) => {
|
|
66
|
+
if (!transport) {
|
|
67
|
+
hash = sha256(Buffer.concat([hash, data]));
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const encrypt = (plaintext) => {
|
|
71
|
+
if (transport) {
|
|
72
|
+
return transport.encrypt(plaintext);
|
|
73
|
+
}
|
|
74
|
+
const result = aesEncryptGCM(plaintext, encKey, generateIV(counter++), hash);
|
|
75
|
+
authenticate(result);
|
|
76
|
+
return result;
|
|
77
|
+
};
|
|
78
|
+
const decrypt = (ciphertext) => {
|
|
79
|
+
if (transport) {
|
|
80
|
+
return transport.decrypt(ciphertext);
|
|
81
|
+
}
|
|
82
|
+
const result = aesDecryptGCM(ciphertext, decKey, generateIV(counter++), hash);
|
|
83
|
+
authenticate(ciphertext);
|
|
84
|
+
return result;
|
|
85
|
+
};
|
|
86
|
+
const localHKDF = (data) => {
|
|
87
|
+
const key = hkdf(Buffer.from(data), 64, { salt, info: '' });
|
|
88
|
+
return [key.subarray(0, 32), key.subarray(32)];
|
|
89
|
+
};
|
|
90
|
+
const mixIntoKey = (data) => {
|
|
91
|
+
const [write, read] = localHKDF(data);
|
|
92
|
+
salt = write;
|
|
93
|
+
encKey = read;
|
|
94
|
+
decKey = read;
|
|
95
|
+
counter = 0;
|
|
96
|
+
};
|
|
97
|
+
const finishInit = async () => {
|
|
98
|
+
isWaitingForTransport = true;
|
|
99
|
+
const [write, read] = localHKDF(new Uint8Array(0));
|
|
100
|
+
transport = new TransportState(write, read);
|
|
101
|
+
isWaitingForTransport = false;
|
|
102
|
+
if (pendingOnFrame) {
|
|
103
|
+
await processData(pendingOnFrame);
|
|
104
|
+
pendingOnFrame = null;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const processData = async (onFrame) => {
|
|
108
|
+
let size;
|
|
109
|
+
while (true) {
|
|
110
|
+
if (inBytes.length < 3)
|
|
111
|
+
return;
|
|
112
|
+
size = (inBytes[0] << 16) | (inBytes[1] << 8) | inBytes[2];
|
|
113
|
+
if (inBytes.length < size + 3)
|
|
114
|
+
return;
|
|
115
|
+
let frame = inBytes.subarray(3, size + 3);
|
|
116
|
+
inBytes = inBytes.subarray(size + 3);
|
|
117
|
+
if (transport) {
|
|
118
|
+
const result = transport.decrypt(frame);
|
|
119
|
+
frame = await decodeBinaryNode(result);
|
|
120
|
+
}
|
|
121
|
+
onFrame(frame);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
authenticate(NOISE_HEADER);
|
|
125
|
+
authenticate(publicKey);
|
|
126
|
+
return {
|
|
127
|
+
encrypt,
|
|
128
|
+
decrypt,
|
|
129
|
+
authenticate,
|
|
130
|
+
mixIntoKey,
|
|
131
|
+
finishInit,
|
|
132
|
+
processHandshake: ({ serverHello }, noiseKey) => {
|
|
133
|
+
authenticate(serverHello.ephemeral);
|
|
134
|
+
mixIntoKey(Curve.sharedKey(privateKey, serverHello.ephemeral));
|
|
135
|
+
const decStaticContent = decrypt(serverHello.static);
|
|
136
|
+
mixIntoKey(Curve.sharedKey(privateKey, decStaticContent));
|
|
137
|
+
const certDecoded = decrypt(serverHello.payload);
|
|
138
|
+
const { intermediate: certIntermediate, leaf } = proto.CertChain.decode(certDecoded);
|
|
139
|
+
if (!leaf?.details || !leaf?.signature) {
|
|
140
|
+
throw new Boom('invalid noise leaf certificate', { statusCode: 400 });
|
|
141
|
+
}
|
|
142
|
+
if (!certIntermediate?.details || !certIntermediate?.signature) {
|
|
143
|
+
throw new Boom('invalid noise intermediate certificate', { statusCode: 400 });
|
|
144
|
+
}
|
|
145
|
+
const details = proto.CertChain.NoiseCertificate.Details.decode(certIntermediate.details);
|
|
146
|
+
const { issuerSerial } = details;
|
|
147
|
+
const verify = Curve.verify(details.key, leaf.details, leaf.signature);
|
|
148
|
+
const verifyIntermediate = Curve.verify(WA_CERT_DETAILS.PUBLIC_KEY, certIntermediate.details, certIntermediate.signature);
|
|
149
|
+
if (!verify) {
|
|
150
|
+
throw new Boom('noise certificate signature invalid', { statusCode: 400 });
|
|
151
|
+
}
|
|
152
|
+
if (!verifyIntermediate) {
|
|
153
|
+
throw new Boom('noise intermediate certificate signature invalid', { statusCode: 400 });
|
|
154
|
+
}
|
|
155
|
+
if (issuerSerial !== WA_CERT_DETAILS.SERIAL) {
|
|
156
|
+
throw new Boom('certification match failed', { statusCode: 400 });
|
|
157
|
+
}
|
|
158
|
+
const keyEnc = encrypt(noiseKey.public);
|
|
159
|
+
mixIntoKey(Curve.sharedKey(noiseKey.private, serverHello.ephemeral));
|
|
160
|
+
return keyEnc;
|
|
161
|
+
},
|
|
162
|
+
encodeFrame: (data) => {
|
|
163
|
+
if (transport) {
|
|
164
|
+
data = transport.encrypt(data);
|
|
165
|
+
}
|
|
166
|
+
const dataLen = data.byteLength;
|
|
167
|
+
const introSize = sentIntro ? 0 : introHeader.length;
|
|
168
|
+
const frame = Buffer.allocUnsafe(introSize + 3 + dataLen);
|
|
169
|
+
if (!sentIntro) {
|
|
170
|
+
frame.set(introHeader);
|
|
171
|
+
sentIntro = true;
|
|
172
|
+
}
|
|
173
|
+
frame[introSize] = (dataLen >>> 16) & 0xff;
|
|
174
|
+
frame[introSize + 1] = (dataLen >>> 8) & 0xff;
|
|
175
|
+
frame[introSize + 2] = dataLen & 0xff;
|
|
176
|
+
frame.set(data, introSize + 3);
|
|
177
|
+
return frame;
|
|
178
|
+
},
|
|
179
|
+
decodeFrame: async (newData, onFrame) => {
|
|
180
|
+
if (isWaitingForTransport) {
|
|
181
|
+
inBytes = Buffer.concat([inBytes, newData]);
|
|
182
|
+
pendingOnFrame = onFrame;
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (inBytes.length === 0) {
|
|
186
|
+
inBytes = Buffer.from(newData);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
inBytes = Buffer.concat([inBytes, newData]);
|
|
190
|
+
}
|
|
191
|
+
await processData(onFrame);
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { WAMessage } from '../Types';
|
|
2
|
+
export declare const cleanMessage: (message: WAMessage, meId: string, meLid: string) => void;
|
|
3
|
+
export declare const getChatId: (key: {
|
|
4
|
+
remoteJid: string;
|
|
5
|
+
participant?: string;
|
|
6
|
+
fromMe: boolean;
|
|
7
|
+
}) => string;
|
|
8
|
+
export declare const isRealMessage: (message: WAMessage) => boolean;
|
|
9
|
+
//# sourceMappingURL=process-message.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-message.d.ts","sourceRoot":"","sources":["../../src/Utils/process-message.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAGzC,eAAO,MAAM,YAAY,GAAI,SAAS,SAAS,EAAE,MAAM,MAAM,EAAE,OAAO,MAAM,SAyC3E,CAAA;AAqBD,eAAO,MAAM,SAAS,GAAI,KAAK;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,KAAG,MAE7F,CAAA;AAED,eAAO,MAAM,aAAa,GAAI,SAAS,SAAS,KAAG,OAGlD,CAAA"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { jidNormalizedUser, isHostedPnUser, isHostedLidUser, jidDecode, jidEncode } from '../WABinary/index.js';
|
|
2
|
+
import { isSystemNotification } from './message-features.js';
|
|
3
|
+
|
|
4
|
+
export const cleanMessage = (message, meId, meLid) => {
|
|
5
|
+
message.isSystemNotification = isSystemNotification(message)
|
|
6
|
+
if (isHostedPnUser(message.key.remoteJid) || isHostedLidUser(message.key.remoteJid)) {
|
|
7
|
+
message.key.remoteJid = jidEncode(jidDecode(message.key.remoteJid)?.user, isHostedPnUser(message.key.remoteJid) ? 's.whatsapp.net' : 'lid');
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
message.key.remoteJid = jidNormalizedUser(message.key.remoteJid);
|
|
11
|
+
}
|
|
12
|
+
if (message.key.participant) {
|
|
13
|
+
if (isHostedPnUser(message.key.participant) || isHostedLidUser(message.key.participant)) {
|
|
14
|
+
message.key.participant = jidEncode(jidDecode(message.key.participant)?.user, isHostedPnUser(message.key.participant) ? 's.whatsapp.net' : 'lid');
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
message.key.participant = jidNormalizedUser(message.key.participant);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const content = normalizeMessageContent(message.message);
|
|
21
|
+
if (content?.reactionMessage) {
|
|
22
|
+
normaliseKey(content.reactionMessage.key);
|
|
23
|
+
}
|
|
24
|
+
if (content?.pollUpdateMessage) {
|
|
25
|
+
normaliseKey(content.pollUpdateMessage.pollCreationMessageKey);
|
|
26
|
+
}
|
|
27
|
+
function normaliseKey(msgKey) {
|
|
28
|
+
if (!message.key.fromMe) {
|
|
29
|
+
msgKey.fromMe = !msgKey.fromMe
|
|
30
|
+
? areJidsSameUser(msgKey.participant || msgKey.remoteJid, meId) ||
|
|
31
|
+
areJidsSameUser(msgKey.participant || msgKey.remoteJid, meLid)
|
|
32
|
+
: false;
|
|
33
|
+
msgKey.remoteJid = message.key.remoteJid;
|
|
34
|
+
msgKey.participant = msgKey.participant || message.key.participant;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const normalizeMessageContent = (message) => {
|
|
39
|
+
if (!message)
|
|
40
|
+
return undefined;
|
|
41
|
+
const type = Object.keys(message).find(key => key !== 'messageContextInfo');
|
|
42
|
+
if (type) {
|
|
43
|
+
const content = message[type];
|
|
44
|
+
if (content?.message) {
|
|
45
|
+
return normalizeMessageContent(content.message);
|
|
46
|
+
}
|
|
47
|
+
return content || message;
|
|
48
|
+
}
|
|
49
|
+
return message;
|
|
50
|
+
};
|
|
51
|
+
const areJidsSameUser = (jid1, jid2) => {
|
|
52
|
+
const user1 = jidDecode(jid1);
|
|
53
|
+
const user2 = jidDecode(jid2);
|
|
54
|
+
return user1?.user === user2?.user;
|
|
55
|
+
};
|
|
56
|
+
export const getChatId = (key) => {
|
|
57
|
+
return key.remoteJid;
|
|
58
|
+
};
|
|
59
|
+
export const isRealMessage = (message) => {
|
|
60
|
+
const normalizedContent = normalizeMessageContent(message.message);
|
|
61
|
+
return !!normalizedContent && !normalizedContent.protocolMessage;
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=process-message.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-message.js","sourceRoot":"","sources":["../../src/Utils/process-message.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,eAAe,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAEtG,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,OAAkB,EAAE,IAAY,EAAE,KAAa,EAAE,EAAE;IAC9E,IAAI,cAAc,CAAC,OAAO,CAAC,GAAG,CAAC,SAAU,CAAC,IAAI,eAAe,CAAC,OAAO,CAAC,GAAG,CAAC,SAAU,CAAC,EAAE,CAAC;QACtF,OAAO,CAAC,GAAG,CAAC,SAAS,GAAG,SAAS,CAC/B,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,SAAU,CAAC,EAAE,IAAK,EACxC,cAAc,CAAC,OAAO,CAAC,GAAG,CAAC,SAAU,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,KAAK,CAClE,CA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AAkB,EAAW,EAAE;IAC3D,MAAM,iBAAiB,GAAG,uBAAuB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IAClE,OAAO,CAAC,CAAC,iBAAiB,IAAI,CAAC,iBAAiB,CAAC,eAAe,CAAA;AAClE,CAAC,CAAA"}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { createHash } from 'crypto';
|
|
3
|
+
import { proto } from '../../WAProto/index.js';
|
|
4
|
+
import { KEY_BUNDLE_TYPE, WA_ADV_ACCOUNT_SIG_PREFIX, WA_ADV_DEVICE_SIG_PREFIX, WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX } from '../Defaults/index.js';
|
|
5
|
+
import { getBinaryNodeChild, jidDecode, S_WHATSAPP_NET } from '../WABinary/index.js';
|
|
6
|
+
import { Curve, hmacSign } from './crypto.js';
|
|
7
|
+
import { encodeBigEndian } from './generics.js';
|
|
8
|
+
import { createSignalIdentity } from './signal.js';
|
|
9
|
+
const getUserAgent = (config) => {
|
|
10
|
+
return {
|
|
11
|
+
appVersion: {
|
|
12
|
+
primary: config.version[0],
|
|
13
|
+
secondary: config.version[1],
|
|
14
|
+
tertiary: config.version[2]
|
|
15
|
+
},
|
|
16
|
+
platform: proto.ClientPayload.UserAgent.Platform.WEB,
|
|
17
|
+
releaseChannel: proto.ClientPayload.UserAgent.ReleaseChannel.RELEASE,
|
|
18
|
+
osVersion: '0.1',
|
|
19
|
+
device: 'Desktop',
|
|
20
|
+
osBuildNumber: '0.1',
|
|
21
|
+
localeLanguageIso6391: 'en',
|
|
22
|
+
mnc: '000',
|
|
23
|
+
mcc: '000',
|
|
24
|
+
localeCountryIso31661Alpha2: config.countryCode
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
const PLATFORM_MAP = {
|
|
28
|
+
'Mac OS': proto.ClientPayload.WebInfo.WebSubPlatform.DARWIN,
|
|
29
|
+
Windows: proto.ClientPayload.WebInfo.WebSubPlatform.WIN32
|
|
30
|
+
};
|
|
31
|
+
const getWebInfo = (config) => {
|
|
32
|
+
let webSubPlatform = proto.ClientPayload.WebInfo.WebSubPlatform.WEB_BROWSER;
|
|
33
|
+
if (config.syncFullHistory &&
|
|
34
|
+
PLATFORM_MAP[config.browser[0]] &&
|
|
35
|
+
config.browser[1] === 'Desktop') {
|
|
36
|
+
webSubPlatform = PLATFORM_MAP[config.browser[0]];
|
|
37
|
+
}
|
|
38
|
+
return { webSubPlatform };
|
|
39
|
+
};
|
|
40
|
+
const getClientPayload = (config) => {
|
|
41
|
+
const payload = {
|
|
42
|
+
connectType: proto.ClientPayload.ConnectType.WIFI_UNKNOWN,
|
|
43
|
+
connectReason: proto.ClientPayload.ConnectReason.USER_ACTIVATED,
|
|
44
|
+
userAgent: getUserAgent(config)
|
|
45
|
+
};
|
|
46
|
+
payload.webInfo = getWebInfo(config);
|
|
47
|
+
if (config.pushName) {
|
|
48
|
+
payload.pushName = config.pushName;
|
|
49
|
+
}
|
|
50
|
+
return payload;
|
|
51
|
+
};
|
|
52
|
+
export const generateLoginNode = (userJid, config) => {
|
|
53
|
+
const { user, device } = jidDecode(userJid);
|
|
54
|
+
const payload = {
|
|
55
|
+
...getClientPayload(config),
|
|
56
|
+
passive: true,
|
|
57
|
+
pull: true,
|
|
58
|
+
username: +user,
|
|
59
|
+
device: device,
|
|
60
|
+
lidDbMigrated: false
|
|
61
|
+
};
|
|
62
|
+
return proto.ClientPayload.fromObject(payload);
|
|
63
|
+
};
|
|
64
|
+
const getPlatformType = (platform) => {
|
|
65
|
+
const platformType = platform.toUpperCase();
|
|
66
|
+
return (proto.DeviceProps.PlatformType[platformType] ||
|
|
67
|
+
proto.DeviceProps.PlatformType.CHROME);
|
|
68
|
+
};
|
|
69
|
+
export const generateRegistrationNode = ({ registrationId, signedPreKey, signedIdentityKey }, config) => {
|
|
70
|
+
const appVersionBuf = createHash('md5')
|
|
71
|
+
.update(config.version.join('.'))
|
|
72
|
+
.digest();
|
|
73
|
+
const companion = {
|
|
74
|
+
os: config.browser[0],
|
|
75
|
+
platformType: getPlatformType(config.browser[1]),
|
|
76
|
+
requireFullSync: config.syncFullHistory,
|
|
77
|
+
historySyncConfig: {
|
|
78
|
+
storageQuotaMb: 10240,
|
|
79
|
+
inlineInitialPayloadInE2EeMsg: true,
|
|
80
|
+
recentSyncDaysLimit: undefined,
|
|
81
|
+
supportCallLogHistory: false,
|
|
82
|
+
supportBotUserAgentChatHistory: true,
|
|
83
|
+
supportCagReactionsAndPolls: true,
|
|
84
|
+
supportBizHostedMsg: true,
|
|
85
|
+
supportRecentSyncChunkMessageCountTuning: true,
|
|
86
|
+
supportHostedGroupMsg: true,
|
|
87
|
+
supportFbidBotChatHistory: true,
|
|
88
|
+
supportAddOnHistorySyncMigration: undefined,
|
|
89
|
+
supportMessageAssociation: true,
|
|
90
|
+
supportGroupHistory: false,
|
|
91
|
+
onDemandReady: undefined,
|
|
92
|
+
supportGuestChat: undefined
|
|
93
|
+
},
|
|
94
|
+
version: {
|
|
95
|
+
primary: 10,
|
|
96
|
+
secondary: 15,
|
|
97
|
+
tertiary: 7
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const companionProto = proto.DeviceProps.encode(companion).finish();
|
|
101
|
+
const registerPayload = {
|
|
102
|
+
...getClientPayload(config),
|
|
103
|
+
passive: false,
|
|
104
|
+
pull: false,
|
|
105
|
+
devicePairingData: {
|
|
106
|
+
buildHash: appVersionBuf,
|
|
107
|
+
deviceProps: companionProto,
|
|
108
|
+
eRegid: encodeBigEndian(registrationId),
|
|
109
|
+
eKeytype: KEY_BUNDLE_TYPE,
|
|
110
|
+
eIdent: signedIdentityKey.public,
|
|
111
|
+
eSkeyId: encodeBigEndian(signedPreKey.keyId, 3),
|
|
112
|
+
eSkeyVal: signedPreKey.keyPair.public,
|
|
113
|
+
eSkeySig: signedPreKey.signature
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
return proto.ClientPayload.fromObject(registerPayload);
|
|
117
|
+
};
|
|
118
|
+
export const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentityKey, signalIdentities }) => {
|
|
119
|
+
const msgId = stanza.attrs.id;
|
|
120
|
+
const pairSuccessNode = getBinaryNodeChild(stanza, 'pair-success');
|
|
121
|
+
const deviceIdentityNode = getBinaryNodeChild(pairSuccessNode, 'device-identity');
|
|
122
|
+
const platformNode = getBinaryNodeChild(pairSuccessNode, 'platform');
|
|
123
|
+
const deviceNode = getBinaryNodeChild(pairSuccessNode, 'device');
|
|
124
|
+
const businessNode = getBinaryNodeChild(pairSuccessNode, 'biz');
|
|
125
|
+
if (!deviceIdentityNode || !deviceNode) {
|
|
126
|
+
throw new Boom('Missing device-identity or device in pair success node', { data: stanza });
|
|
127
|
+
}
|
|
128
|
+
const bizName = businessNode?.attrs.name;
|
|
129
|
+
const jid = deviceNode.attrs.jid;
|
|
130
|
+
const lid = deviceNode.attrs.lid;
|
|
131
|
+
const { details, hmac, accountType } = proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content);
|
|
132
|
+
let hmacPrefix = Buffer.from([]);
|
|
133
|
+
if (accountType !== undefined && accountType === proto.ADVEncryptionType.HOSTED) {
|
|
134
|
+
hmacPrefix = WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX;
|
|
135
|
+
}
|
|
136
|
+
const advSign = hmacSign(Buffer.concat([hmacPrefix, details]), Buffer.from(advSecretKey, 'base64'));
|
|
137
|
+
if (Buffer.compare(hmac, advSign) !== 0) {
|
|
138
|
+
throw new Boom('Invalid account signature');
|
|
139
|
+
}
|
|
140
|
+
const account = proto.ADVSignedDeviceIdentity.decode(details);
|
|
141
|
+
const { accountSignatureKey, accountSignature, details: deviceDetails } = account;
|
|
142
|
+
const deviceIdentity = proto.ADVDeviceIdentity.decode(deviceDetails);
|
|
143
|
+
const accountSignaturePrefix = deviceIdentity.deviceType === proto.ADVEncryptionType.HOSTED
|
|
144
|
+
? WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX
|
|
145
|
+
: WA_ADV_ACCOUNT_SIG_PREFIX;
|
|
146
|
+
const accountMsg = Buffer.concat([accountSignaturePrefix, deviceDetails, signedIdentityKey.public]);
|
|
147
|
+
if (!Curve.verify(accountSignatureKey, accountMsg, accountSignature)) {
|
|
148
|
+
throw new Boom('Failed to verify account signature');
|
|
149
|
+
}
|
|
150
|
+
const deviceMsg = Buffer.concat([
|
|
151
|
+
WA_ADV_DEVICE_SIG_PREFIX,
|
|
152
|
+
deviceDetails,
|
|
153
|
+
signedIdentityKey.public,
|
|
154
|
+
accountSignatureKey
|
|
155
|
+
]);
|
|
156
|
+
account.deviceSignature = Curve.sign(signedIdentityKey.private, deviceMsg);
|
|
157
|
+
const identity = createSignalIdentity(lid, accountSignatureKey);
|
|
158
|
+
const accountEnc = encodeSignedDeviceIdentity(account, false);
|
|
159
|
+
const reply = {
|
|
160
|
+
tag: 'iq',
|
|
161
|
+
attrs: {
|
|
162
|
+
to: S_WHATSAPP_NET,
|
|
163
|
+
type: 'result',
|
|
164
|
+
id: msgId
|
|
165
|
+
},
|
|
166
|
+
content: [
|
|
167
|
+
{
|
|
168
|
+
tag: 'pair-device-sign',
|
|
169
|
+
attrs: {},
|
|
170
|
+
content: [
|
|
171
|
+
{
|
|
172
|
+
tag: 'device-identity',
|
|
173
|
+
attrs: { 'key-index': deviceIdentity.keyIndex.toString() },
|
|
174
|
+
content: accountEnc
|
|
175
|
+
}
|
|
176
|
+
]
|
|
177
|
+
}
|
|
178
|
+
]
|
|
179
|
+
};
|
|
180
|
+
const authUpdate = {
|
|
181
|
+
account,
|
|
182
|
+
me: { id: jid, name: bizName, lid },
|
|
183
|
+
signalIdentities: [...(signalIdentities || []), identity],
|
|
184
|
+
platform: platformNode?.attrs.name
|
|
185
|
+
};
|
|
186
|
+
return {
|
|
187
|
+
creds: authUpdate,
|
|
188
|
+
reply
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
export const encodeSignedDeviceIdentity = (account, includeSignatureKey) => {
|
|
192
|
+
account = { ...account };
|
|
193
|
+
if (!includeSignatureKey || !account.accountSignatureKey?.length) {
|
|
194
|
+
account.accountSignatureKey = null;
|
|
195
|
+
}
|
|
196
|
+
return proto.ADVSignedDeviceIdentity.encode(account).finish();
|
|
197
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export declare const TAGS: {
|
|
2
|
+
readonly LIST_EMPTY: 0;
|
|
3
|
+
readonly STREAM_END: 2;
|
|
4
|
+
readonly DICTIONARY_0: 236;
|
|
5
|
+
readonly DICTIONARY_1: 237;
|
|
6
|
+
readonly DICTIONARY_2: 238;
|
|
7
|
+
readonly DICTIONARY_3: 239;
|
|
8
|
+
readonly COMPOSITION: 248;
|
|
9
|
+
readonly LIST_8: 248;
|
|
10
|
+
readonly LIST_16: 249;
|
|
11
|
+
readonly LIST_32: 250;
|
|
12
|
+
readonly JID_PAIR: 251;
|
|
13
|
+
readonly HEX_8: 252;
|
|
14
|
+
readonly BINARY_8: 252;
|
|
15
|
+
readonly BINARY_20: 253;
|
|
16
|
+
readonly BINARY_32: 254;
|
|
17
|
+
readonly NIBBLE_8: 255;
|
|
18
|
+
readonly SINGLE_BYTE_MAX: 256;
|
|
19
|
+
readonly PACKED_MAX: 254;
|
|
20
|
+
readonly AD_JID: 253;
|
|
21
|
+
};
|
|
22
|
+
export declare const DOUBLE_BYTE_TOKENS: {
|
|
23
|
+
[key: number]: string;
|
|
24
|
+
};
|
|
25
|
+
export declare const SINGLE_BYTE_TOKENS: {
|
|
26
|
+
[key: number]: string;
|
|
27
|
+
};
|
|
28
|
+
export declare const TOKENS_TO_DOUBLE_BYTES: {
|
|
29
|
+
[key: string]: number;
|
|
30
|
+
};
|
|
31
|
+
export declare const TOKENS_TO_SINGLE_BYTES: {
|
|
32
|
+
[k: string]: number;
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=binary-constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary-constants.d.ts","sourceRoot":"","sources":["../../src/WABinary/binary-constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,IAAI;;;;;;;;;;;;;;;;;;;;CAoBP,CAAA;AAEV,eAAO,MAAM,kBAAkB,EAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAUvD,CAAA;AAED,eAAO,MAAM,kBAAkB,EAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAsfvD,CAAA;AAED,eAAO,MAAM,sBAAsB,EAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAE3D,CAAA;AAED,eAAO,MAAM,sBAAsB;;CAElC,CAAA"}
|