Files
hermes-agent/scripts/whatsapp-bridge/passkey_socket.js
kshitijk4poor fc60ebb7e6 fix(whatsapp): bind passkey code confirmation to its own attempt
The code reached the terminal prompt through a synthetic connection.update
event, and the prompt then called back into the flow's
confirmVerificationCode with a socket-lifetime abort controller. A newer
prologue request aborted the old attempt but left its question open, so a
late "y" sealed and sent the newer attempt's pairing request without the
owner having seen that code. A second request also queued behind the
current browser prompt for up to its 600 s bound.

The flow now awaits an injected confirmCode(code, signal) itself and sends
the pairing request only for the attempt that derived the code; signal is
that attempt's own controller, so a newer prologue (which now clears the
current session at once) or a socket close cancels the question. That
removes the event round trip, the returned confirmation wrapper and the
bridge's second controller.

Also removed from the adapted flow: the emitPrologueSent hook, the
pairingHandoffProof argument and getVerificationCode, none of which had a
caller; the proof now comes only from the stashed handoff key. The bridge
prints the passkey failure's name and message instead of one generic line.

Tests: a superseding request cancels the pending question and a late
answer sends nothing; the handoff-window cases assert whether confirmCode
runs. Removing the supersede clear, or forcing or skipping the prompt,
each turns a test red.
2026-11-08 15:52:48 +05:30

60 lines
2.4 KiB
JavaScript

import { proto } from '@whiskeysockets/baileys';
import { makeShortcakeFlow } from './passkey_flow.js';
export async function loadPasskeySigner(executablePath, { pairOnly, pairJson, isTTY },
load = () => import('./passkey_browser.js')) {
if (!executablePath) return undefined;
if (!pairOnly || pairJson || !isTTY) {
throw new Error('--passkey-browser requires interactive --pair-only setup.');
}
return (await load()).createBrowserPasskeySigner(executablePath);
}
// Baileys still owns acknowledgement of these notifications. The handshake
// runs separately so an interactive browser prompt cannot delay that ACK.
// confirmCode(code, signal) resolves once the owner confirms the code matches the phone and rejects
// otherwise; the signal aborts when this attempt is superseded or the socket closes.
export function attachPasskeyFlow(socket, { logger, signAssertion, confirmCode }) {
let closed = false;
let pending = Promise.resolve();
const flow = signAssertion ? makeShortcakeFlow({
logger,
query: node => socket.query(node),
signAssertion,
getCreds: () => socket.authState.creds,
updateCreds: patch => socket.ev.emit('creds.update', patch),
deviceType: proto.DeviceProps.PlatformType.CHROME,
confirmCode,
}) : null;
function notification(node) {
if (closed || !['s.whatsapp.net', '@s.whatsapp.net'].includes(node.attrs.from)) return;
if (!['passkey_prologue_request', 'crsc_continuation'].includes(node.attrs.type)) return;
if (node.attrs.type === 'passkey_prologue_request') {
socket.ev.emit('connection.update', { passkeyRequired: { hasSigner: Boolean(flow) } });
// A new request supersedes the current attempt: abort its browser prompt or code question now
// instead of queuing behind it.
flow?.clearSession();
}
if (!flow) return;
pending = pending.then(() => {
if (!closed) return flow.handleIncomingNotification(node);
}).catch(error => {
if (closed || error?.name === 'AbortError') return;
flow.clearSession();
socket.ev.emit('connection.update', { passkeyError: error });
});
}
function connection(update) {
if (update.connection !== 'close') return;
closed = true;
flow?.clearSession();
socket.ws.off('CB:notification', notification);
socket.ev.off('connection.update', connection);
}
socket.ws.on('CB:notification', notification);
socket.ev.on('connection.update', connection);
}