Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/validate-connection.js
1129 views
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
exports.encodeSignedDeviceIdentity = exports.configureSuccessfulPairing = exports.generateRegistrationNode = exports.generateLoginNode = void 0;
4
const boom_1 = require("@hapi/boom");
5
const crypto_1 = require("crypto");
6
const WAProto_1 = require("../../WAProto");
7
const Defaults_1 = require("../Defaults");
8
const WABinary_1 = require("../WABinary");
9
const crypto_2 = require("./crypto");
10
const generics_1 = require("./generics");
11
const signal_1 = require("./signal");
12
const getUserAgent = ({ version }) => {
13
const osVersion = '0.1';
14
return {
15
appVersion: {
16
primary: version[0],
17
secondary: version[1],
18
tertiary: version[2],
19
},
20
platform: WAProto_1.proto.ClientPayload.UserAgent.Platform.WEB,
21
releaseChannel: WAProto_1.proto.ClientPayload.UserAgent.ReleaseChannel.RELEASE,
22
mcc: '000',
23
mnc: '000',
24
osVersion: osVersion,
25
manufacturer: '',
26
device: 'Desktop',
27
osBuildNumber: osVersion,
28
localeLanguageIso6391: 'en',
29
localeCountryIso31661Alpha2: 'US',
30
};
31
};
32
const PLATFORM_MAP = {
33
'Mac OS': WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.DARWIN,
34
'Windows': WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.WIN32
35
};
36
const getWebInfo = (config) => {
37
let webSubPlatform = WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.WEB_BROWSER;
38
if (config.syncFullHistory && PLATFORM_MAP[config.browser[0]]) {
39
webSubPlatform = PLATFORM_MAP[config.browser[0]];
40
}
41
return { webSubPlatform };
42
};
43
const getClientPayload = (config) => {
44
return {
45
connectType: WAProto_1.proto.ClientPayload.ConnectType.WIFI_UNKNOWN,
46
connectReason: WAProto_1.proto.ClientPayload.ConnectReason.USER_ACTIVATED,
47
userAgent: getUserAgent(config),
48
webInfo: getWebInfo(config),
49
};
50
};
51
const generateLoginNode = (userJid, config) => {
52
const { user, device } = (0, WABinary_1.jidDecode)(userJid);
53
const payload = {
54
...getClientPayload(config),
55
passive: true,
56
username: +user,
57
device: device,
58
};
59
return WAProto_1.proto.ClientPayload.fromObject(payload);
60
};
61
exports.generateLoginNode = generateLoginNode;
62
const generateRegistrationNode = ({ registrationId, signedPreKey, signedIdentityKey }, config) => {
63
// the app version needs to be md5 hashed
64
// and passed in
65
const appVersionBuf = (0, crypto_1.createHash)('md5')
66
.update(config.version.join('.')) // join as string
67
.digest();
68
const browserVersion = config.browser[2].split('.');
69
const companion = {
70
os: config.browser[0],
71
version: {
72
primary: +(browserVersion[0] || 0),
73
secondary: +(browserVersion[1] || 1),
74
tertiary: +(browserVersion[2] || 0),
75
},
76
platformType: WAProto_1.proto.DeviceProps.PlatformType[config.browser[1].toUpperCase()]
77
|| WAProto_1.proto.DeviceProps.PlatformType.UNKNOWN,
78
requireFullSync: config.syncFullHistory,
79
};
80
const companionProto = WAProto_1.proto.DeviceProps.encode(companion).finish();
81
const registerPayload = {
82
...getClientPayload(config),
83
passive: false,
84
devicePairingData: {
85
buildHash: appVersionBuf,
86
deviceProps: companionProto,
87
eRegid: (0, generics_1.encodeBigEndian)(registrationId),
88
eKeytype: Defaults_1.KEY_BUNDLE_TYPE,
89
eIdent: signedIdentityKey.public,
90
eSkeyId: (0, generics_1.encodeBigEndian)(signedPreKey.keyId, 3),
91
eSkeyVal: signedPreKey.keyPair.public,
92
eSkeySig: signedPreKey.signature,
93
},
94
};
95
return WAProto_1.proto.ClientPayload.fromObject(registerPayload);
96
};
97
exports.generateRegistrationNode = generateRegistrationNode;
98
const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentityKey, signalIdentities }) => {
99
const msgId = stanza.attrs.id;
100
const pairSuccessNode = (0, WABinary_1.getBinaryNodeChild)(stanza, 'pair-success');
101
const deviceIdentityNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'device-identity');
102
const platformNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'platform');
103
const deviceNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'device');
104
const businessNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'biz');
105
if (!deviceIdentityNode || !deviceNode) {
106
throw new boom_1.Boom('Missing device-identity or device in pair success node', { data: stanza });
107
}
108
const bizName = businessNode === null || businessNode === void 0 ? void 0 : businessNode.attrs.name;
109
const jid = deviceNode.attrs.jid;
110
const { details, hmac } = WAProto_1.proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content);
111
// check HMAC matches
112
const advSign = (0, crypto_2.hmacSign)(details, Buffer.from(advSecretKey, 'base64'));
113
if (Buffer.compare(hmac, advSign) !== 0) {
114
throw new boom_1.Boom('Invalid account signature');
115
}
116
const account = WAProto_1.proto.ADVSignedDeviceIdentity.decode(details);
117
const { accountSignatureKey, accountSignature, details: deviceDetails } = account;
118
// verify the device signature matches
119
const accountMsg = Buffer.concat([Buffer.from([6, 0]), deviceDetails, signedIdentityKey.public]);
120
if (!crypto_2.Curve.verify(accountSignatureKey, accountMsg, accountSignature)) {
121
throw new boom_1.Boom('Failed to verify account signature');
122
}
123
// sign the details with our identity key
124
const deviceMsg = Buffer.concat([Buffer.from([6, 1]), deviceDetails, signedIdentityKey.public, accountSignatureKey]);
125
account.deviceSignature = crypto_2.Curve.sign(signedIdentityKey.private, deviceMsg);
126
const identity = (0, signal_1.createSignalIdentity)(jid, accountSignatureKey);
127
const accountEnc = (0, exports.encodeSignedDeviceIdentity)(account, false);
128
const deviceIdentity = WAProto_1.proto.ADVDeviceIdentity.decode(account.details);
129
const reply = {
130
tag: 'iq',
131
attrs: {
132
to: WABinary_1.S_WHATSAPP_NET,
133
type: 'result',
134
id: msgId,
135
},
136
content: [
137
{
138
tag: 'pair-device-sign',
139
attrs: {},
140
content: [
141
{
142
tag: 'device-identity',
143
attrs: { 'key-index': deviceIdentity.keyIndex.toString() },
144
content: accountEnc
145
}
146
]
147
}
148
]
149
};
150
const authUpdate = {
151
account,
152
me: { id: jid, name: bizName },
153
signalIdentities: [
154
...(signalIdentities || []),
155
identity
156
],
157
platform: platformNode === null || platformNode === void 0 ? void 0 : platformNode.attrs.name
158
};
159
return {
160
creds: authUpdate,
161
reply
162
};
163
};
164
exports.configureSuccessfulPairing = configureSuccessfulPairing;
165
const encodeSignedDeviceIdentity = (account, includeSignatureKey) => {
166
var _a;
167
account = { ...account };
168
// set to null if we are not to include the signature key
169
// or if we are including the signature key but it is empty
170
if (!includeSignatureKey || !((_a = account.accountSignatureKey) === null || _a === void 0 ? void 0 : _a.length)) {
171
account.accountSignatureKey = null;
172
}
173
const accountEnc = WAProto_1.proto.ADVSignedDeviceIdentity
174
.encode(account)
175
.finish();
176
return accountEnc;
177
};
178
exports.encodeSignedDeviceIdentity = encodeSignedDeviceIdentity;
179
180