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