mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Added requestPairingCode method and handler
Fixed typo (trimUndefineds) Added bytesToCrockford Replaced advSecretKey with advKeyPair Added pairingCode prop Fixed formatting Added pairing code example
This commit is contained in:
@@ -1,11 +1,29 @@
|
||||
|
||||
import { Boom } from '@hapi/boom'
|
||||
import { randomBytes } from 'crypto'
|
||||
import NodeCache from 'node-cache'
|
||||
import { getBinaryNodeChildBuffer } from '../../lib'
|
||||
import { proto } from '../../WAProto'
|
||||
import { DEFAULT_CACHE_TTLS, KEY_BUNDLE_TYPE, MIN_PREKEY_COUNT } from '../Defaults'
|
||||
import { MessageReceiptType, MessageRelayOptions, MessageUserReceipt, SocketConfig, WACallEvent, WAMessageKey, WAMessageStatus, WAMessageStubType, WAPatchName } from '../Types'
|
||||
import { decodeMediaRetryNode, decryptMessageNode, delay, encodeBigEndian, encodeSignedDeviceIdentity, getCallStatusFromNode, getHistoryMsg, getNextPreKeys, getStatusFromReceiptType, unixTimestampSeconds, xmppPreKey, xmppSignedPreKey } from '../Utils'
|
||||
import {
|
||||
aesEncryptGCM,
|
||||
Curve,
|
||||
decodeMediaRetryNode,
|
||||
decryptMessageNode,
|
||||
delay, derivePairingKey,
|
||||
encodeBigEndian,
|
||||
encodeSignedDeviceIdentity,
|
||||
getCallStatusFromNode,
|
||||
getHistoryMsg,
|
||||
getNextPreKeys,
|
||||
getStatusFromReceiptType, hkdf,
|
||||
unixTimestampSeconds,
|
||||
xmppPreKey,
|
||||
xmppSignedPreKey
|
||||
} from '../Utils'
|
||||
import { cleanMessage } from '../Utils'
|
||||
import { makeMutex } from '../Utils/make-mutex'
|
||||
import { cleanMessage } from '../Utils/process-message'
|
||||
import { areJidsSameUser, BinaryNode, getAllBinaryNodeChildren, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, isJidUser, jidDecode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary'
|
||||
import { extractGroupMetadata } from './groups'
|
||||
import { makeMessagesSocket } from './messages-send'
|
||||
@@ -235,7 +253,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
case 'remove':
|
||||
case 'add':
|
||||
case 'leave':
|
||||
const stubType = `GROUP_PARTICIPANT_${child.tag!.toUpperCase()}`
|
||||
const stubType = `GROUP_PARTICIPANT_${child.tag.toUpperCase()}`
|
||||
msg.messageStubType = WAMessageStubType[stubType]
|
||||
|
||||
const participants = getBinaryNodeChildren(child, 'participant').map(p => p.attrs.jid)
|
||||
@@ -306,7 +324,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
break
|
||||
case 'devices':
|
||||
const devices = getBinaryNodeChildren(child, 'device')
|
||||
if(areJidsSameUser(child.attrs.jid, authState.creds!.me!.id)) {
|
||||
if(areJidsSameUser(child.attrs.jid, authState.creds.me!.id)) {
|
||||
const deviceJids = devices.map(d => d.attrs.jid)
|
||||
logger.info({ deviceJids }, 'got my own devices')
|
||||
}
|
||||
@@ -334,7 +352,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
result.messageStubType = WAMessageStubType.GROUP_CHANGE_ICON
|
||||
|
||||
if(setPicture) {
|
||||
result.messageStubParameters = [ setPicture.attrs.id ]
|
||||
result.messageStubParameters = [setPicture.attrs.id]
|
||||
}
|
||||
|
||||
result.participant = node?.attrs.author
|
||||
@@ -364,6 +382,63 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
}
|
||||
|
||||
break
|
||||
case 'link_code_companion_reg':
|
||||
const linkCodeCompanionReg = getBinaryNodeChild(node, 'link_code_companion_reg')
|
||||
const ref = toRequiredBuffer(getBinaryNodeChildBuffer(linkCodeCompanionReg, 'link_code_pairing_ref'))
|
||||
const primaryIdentityPublicKey = toRequiredBuffer(getBinaryNodeChildBuffer(linkCodeCompanionReg, 'primary_identity_pub'))
|
||||
const primaryEphemeralPublicKeyWrapped = toRequiredBuffer(getBinaryNodeChildBuffer(linkCodeCompanionReg, 'link_code_pairing_wrapped_primary_ephemeral_pub'))
|
||||
const codePairingPublicKey = await decipherLinkPublicKey(primaryEphemeralPublicKeyWrapped)
|
||||
const companionSharedKey = Curve.sharedKey(codePairingPublicKey, authState.creds.advKeyPair.private)
|
||||
const random = randomBytes(32)
|
||||
const linkCodeSalt = randomBytes(32)
|
||||
const linkCodePairingExpanded = hkdf(companionSharedKey, 32, {
|
||||
salt: linkCodeSalt,
|
||||
info: 'link_code_pairing_key_bundle_encryption_key'
|
||||
})
|
||||
const encryptPayload = Buffer.concat([Buffer.from(authState.creds.signedIdentityKey.public), Buffer.from(primaryIdentityPublicKey), random])
|
||||
const encryptIv = randomBytes(12)
|
||||
const encrypted = aesEncryptGCM(encryptPayload, linkCodePairingExpanded, encryptIv, Buffer.alloc(0))
|
||||
const encryptedPayload = Buffer.concat([linkCodeSalt, encryptIv, encrypted])
|
||||
const identitySharedKey = Curve.sharedKey(primaryIdentityPublicKey, authState.creds.signedIdentityKey.private)
|
||||
const identityPayload = Buffer.concat([companionSharedKey, identitySharedKey, random])
|
||||
authState.creds.advKeyPair.public = hkdf(identityPayload, 32, { info: 'adv_secret' })
|
||||
authState.creds.advKeyPair.private = Buffer.alloc(0)
|
||||
await sendNode({
|
||||
tag: 'iq',
|
||||
attrs: {
|
||||
to: S_WHATSAPP_NET,
|
||||
type: 'set',
|
||||
id: sock.generateMessageTag(),
|
||||
xmlns: 'md'
|
||||
},
|
||||
content: [
|
||||
{
|
||||
tag: 'link_code_companion_reg',
|
||||
attrs: {
|
||||
jid: authState.creds.me!.id,
|
||||
stage: 'companion_finish',
|
||||
},
|
||||
content: [
|
||||
{
|
||||
tag: 'link_code_pairing_wrapped_key_bundle',
|
||||
attrs: {},
|
||||
content: encryptedPayload
|
||||
},
|
||||
{
|
||||
tag: 'companion_identity_public',
|
||||
attrs: {},
|
||||
content: authState.creds.signedIdentityKey.public
|
||||
},
|
||||
{
|
||||
tag: 'link_code_pairing_ref',
|
||||
attrs: {},
|
||||
content: ref
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
authState.creds.registered = true
|
||||
}
|
||||
|
||||
if(Object.keys(result).length) {
|
||||
@@ -371,6 +446,28 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
}
|
||||
}
|
||||
|
||||
async function decipherLinkPublicKey(data: Uint8Array | Buffer) {
|
||||
const buffer = toRequiredBuffer(data)
|
||||
const salt = buffer.slice(0, 32)
|
||||
const secretKey = await derivePairingKey(authState.creds.pairingCode!, salt)
|
||||
const iv = buffer.slice(32, 48)
|
||||
const payload = buffer.slice(48, 80)
|
||||
const result = await crypto.subtle.decrypt({
|
||||
name: 'AES-CTR',
|
||||
length: 64,
|
||||
counter: iv
|
||||
}, secretKey, payload)
|
||||
return Buffer.from(result)
|
||||
}
|
||||
|
||||
function toRequiredBuffer(data: Uint8Array | Buffer | undefined) {
|
||||
if(data === undefined) {
|
||||
throw new Boom('Invalid buffer', { statusCode: 400 })
|
||||
}
|
||||
|
||||
return data instanceof Buffer ? data : Buffer.from(data)
|
||||
}
|
||||
|
||||
const willSendMessageAgain = (id: string, participant: string) => {
|
||||
const key = `${id}:${participant}`
|
||||
const retryCount = msgRetryCache.get<number>(key) || 0
|
||||
|
||||
@@ -1,13 +1,48 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { Boom } from '@hapi/boom'
|
||||
import { randomBytes } from 'crypto'
|
||||
import { URL } from 'url'
|
||||
import { promisify } from 'util'
|
||||
import { getBinaryNodeChildBuffer } from '../../lib'
|
||||
import { proto } from '../../WAProto'
|
||||
import { DEF_CALLBACK_PREFIX, DEF_TAG_PREFIX, INITIAL_PREKEY_COUNT, MIN_PREKEY_COUNT, MOBILE_ENDPOINT, MOBILE_NOISE_HEADER, MOBILE_PORT, NOISE_WA_HEADER } from '../Defaults'
|
||||
import {
|
||||
DEF_CALLBACK_PREFIX,
|
||||
DEF_TAG_PREFIX,
|
||||
INITIAL_PREKEY_COUNT,
|
||||
MIN_PREKEY_COUNT,
|
||||
MOBILE_ENDPOINT,
|
||||
MOBILE_NOISE_HEADER,
|
||||
MOBILE_PORT,
|
||||
NOISE_WA_HEADER
|
||||
} from '../Defaults'
|
||||
import { DisconnectReason, SocketConfig } from '../Types'
|
||||
import { addTransactionCapability, bindWaitForConnectionUpdate, configureSuccessfulPairing, Curve, generateLoginNode, generateMdTagPrefix, generateMobileNode, generateRegistrationNode, getCodeFromWSError, getErrorCodeFromStreamError, getNextPreKeysNode, makeNoiseHandler, printQRIfNecessaryListener, promiseTimeout } from '../Utils'
|
||||
import { makeEventBuffer } from '../Utils/event-buffer'
|
||||
import { assertNodeErrorFree, BinaryNode, binaryNodeToString, encodeBinaryNode, getBinaryNodeChild, getBinaryNodeChildren, S_WHATSAPP_NET } from '../WABinary'
|
||||
import {
|
||||
addTransactionCapability, aesEncryptGCM,
|
||||
bindWaitForConnectionUpdate,
|
||||
bytesToCrockford,
|
||||
configureSuccessfulPairing,
|
||||
Curve, derivePairingKey,
|
||||
generateLoginNode,
|
||||
generateMdTagPrefix,
|
||||
generateMobileNode,
|
||||
generateRegistrationNode,
|
||||
getCodeFromWSError,
|
||||
getErrorCodeFromStreamError,
|
||||
getNextPreKeysNode, hkdf,
|
||||
makeEventBuffer,
|
||||
makeNoiseHandler,
|
||||
printQRIfNecessaryListener,
|
||||
promiseTimeout
|
||||
} from '../Utils'
|
||||
import {
|
||||
assertNodeErrorFree,
|
||||
BinaryNode,
|
||||
binaryNodeToString,
|
||||
encodeBinaryNode,
|
||||
getBinaryNodeChild,
|
||||
getBinaryNodeChildren,
|
||||
jidEncode,
|
||||
S_WHATSAPP_NET
|
||||
} from '../WABinary'
|
||||
import { MobileSocketClient, WebSocketClient } from './Client'
|
||||
|
||||
/**
|
||||
@@ -141,15 +176,14 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
|
||||
/**
|
||||
* Wait for a message with a certain tag to be received
|
||||
* @param tag the message tag to await
|
||||
* @param json query that was sent
|
||||
* @param msgId the message tag to await
|
||||
* @param timeoutMs timeout after which the promise will reject
|
||||
*/
|
||||
const waitForMessage = async<T>(msgId: string, timeoutMs = defaultQueryTimeoutMs) => {
|
||||
let onRecv: (json) => void
|
||||
let onErr: (err) => void
|
||||
try {
|
||||
const result = await promiseTimeout<T>(timeoutMs,
|
||||
return await promiseTimeout<T>(timeoutMs,
|
||||
(resolve, reject) => {
|
||||
onRecv = resolve
|
||||
onErr = err => {
|
||||
@@ -161,7 +195,6 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
ws.off('error', onErr)
|
||||
},
|
||||
)
|
||||
return result
|
||||
} finally {
|
||||
ws.off(`TAG:${msgId}`, onRecv!)
|
||||
ws.off('close', onErr!) // if the socket closes, you'll never receive the message
|
||||
@@ -213,7 +246,7 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
node = generateRegistrationNode(creds, config)
|
||||
logger.info({ node }, 'not logged in, attempting registration...')
|
||||
} else {
|
||||
node = generateLoginNode(creds.me!.id, config)
|
||||
node = generateLoginNode(creds.me.id, config)
|
||||
logger.info({ node }, 'logging in...')
|
||||
}
|
||||
|
||||
@@ -448,6 +481,72 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
end(new Boom(msg || 'Intentional Logout', { statusCode: DisconnectReason.loggedOut }))
|
||||
}
|
||||
|
||||
const requestPairingCode = async(phoneNumber: string) => {
|
||||
authState.creds.me = {
|
||||
id: jidEncode(phoneNumber, 's.whatsapp.net'),
|
||||
name: '~'
|
||||
}
|
||||
await sendNode({
|
||||
tag: 'iq',
|
||||
attrs: {
|
||||
to: S_WHATSAPP_NET,
|
||||
type: 'set',
|
||||
id: generateMessageTag(),
|
||||
xmlns: 'md'
|
||||
},
|
||||
content: [
|
||||
{
|
||||
tag: 'link_code_companion_reg',
|
||||
attrs: {
|
||||
jid: authState.creds.me.id,
|
||||
stage: 'companion_hello',
|
||||
// eslint-disable-next-line camelcase
|
||||
should_show_push_notification: 'true'
|
||||
},
|
||||
content: [
|
||||
{
|
||||
tag: 'link_code_pairing_wrapped_companion_ephemeral_pub',
|
||||
attrs: {},
|
||||
content: await generatePairingKey(randomBytes(32))
|
||||
},
|
||||
{
|
||||
tag: 'companion_server_auth_key_pub',
|
||||
attrs: {},
|
||||
content: authState.creds.noiseKey.public
|
||||
},
|
||||
{
|
||||
tag: 'companion_platform_id',
|
||||
attrs: {},
|
||||
content: '49' // Chrome
|
||||
},
|
||||
{
|
||||
tag: 'companion_platform_display',
|
||||
attrs: {},
|
||||
content: config.browser[0]
|
||||
},
|
||||
{
|
||||
tag: 'link_code_pairing_nonce',
|
||||
attrs: {},
|
||||
content: '0'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
async function generatePairingKey(salt: Buffer) {
|
||||
authState.creds.pairingCode = bytesToCrockford(randomBytes(5))
|
||||
const key = await derivePairingKey(authState.creds.pairingCode, salt)
|
||||
const randomIv = randomBytes(16)
|
||||
const ciphered = await crypto.subtle.encrypt({
|
||||
name: 'AES-CTR',
|
||||
length: 64,
|
||||
counter: randomIv
|
||||
}, key, authState.creds.advKeyPair.public)
|
||||
return Buffer.concat([salt, randomIv, Buffer.from(ciphered)])
|
||||
}
|
||||
|
||||
ws.on('message', onMessageRecieved)
|
||||
ws.on('open', async() => {
|
||||
try {
|
||||
@@ -477,7 +576,7 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
const refNodes = getBinaryNodeChildren(pairDeviceNode, 'ref')
|
||||
const noiseKeyB64 = Buffer.from(creds.noiseKey.public).toString('base64')
|
||||
const identityKeyB64 = Buffer.from(creds.signedIdentityKey.public).toString('base64')
|
||||
const advB64 = creds.advSecretKey
|
||||
const advB64 = Buffer.from(creds.advKeyPair.public).toString('base64')
|
||||
|
||||
let qrMs = qrTimeout || 60_000 // time to let a QR live
|
||||
const genPairQR = () => {
|
||||
@@ -619,6 +718,7 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
onUnexpectedError,
|
||||
uploadPreKeys,
|
||||
uploadPreKeysToServerIfRequired,
|
||||
requestPairingCode,
|
||||
/** Waits for the connection to WA to reach a state */
|
||||
waitForConnectionUpdate: bindWaitForConnectionUpdate(ev),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user