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:
Alessandro Autiero
2023-07-16 18:35:46 +02:00
parent 0aaa0086f9
commit f498e1e56c
8 changed files with 293 additions and 30 deletions

View File

@@ -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),
}