diff --git a/src/Signal/lid-mapping.ts b/src/Signal/lid-mapping.ts index acb3232c..4f11c89a 100644 --- a/src/Signal/lid-mapping.ts +++ b/src/Signal/lid-mapping.ts @@ -1,7 +1,7 @@ import { LRUCache } from 'lru-cache' import type { LIDMapping, SignalKeyStoreWithTransaction } from '../Types' import type { ILogger } from '../Utils/logger' -import { isLidUser, isPnUser, jidDecode, jidNormalizedUser } from '../WABinary' +import { isJidHostedLidUser, isJidHostedPnUser, isLidUser, isPnUser, jidDecode, jidNormalizedUser } from '../WABinary' export class LIDMappingStore { private readonly mappingCache = new LRUCache({ @@ -113,11 +113,15 @@ export class LIDMappingStore { } else { this.logger.trace(`No LID mapping found for PN user ${pnUser}; batch getting from USync`) const device = decoded.device || 0 - const normalizedPn = jidNormalizedUser(pn) + let normalizedPn = jidNormalizedUser(pn) + if (isJidHostedLidUser(normalizedPn) || isJidHostedPnUser(normalizedPn)) { + normalizedPn = `${pnUser}@s.whatsapp.net` + } + if (!usyncFetch[normalizedPn]) { usyncFetch[normalizedPn] = [device] } else { - usyncFetch[normalizedPn].push(device) + usyncFetch[normalizedPn]?.push(device) } continue @@ -132,7 +136,7 @@ export class LIDMappingStore { // Push the PN device ID to the LID to maintain device separation const pnDevice = decoded.device !== undefined ? decoded.device : 0 - const deviceSpecificLid = `${lidUser}${!!pnDevice ? `:${pnDevice}` : ``}@lid` + const deviceSpecificLid = `${lidUser}${!!pnDevice ? `:${pnDevice}` : ``}@${decoded.server === 'hosted' ? 'hosted.lid' : 'lid'}` this.logger.trace(`getLIDForPN: ${pn} → ${deviceSpecificLid} (user mapping with device ${pnDevice})`) successfulPairs[pn] = { lid: deviceSpecificLid, pn } @@ -143,19 +147,20 @@ export class LIDMappingStore { if (result && result.length > 0) { this.storeLIDPNMappings(result) for (const pair of result) { - const pnUser = jidDecode(pair.pn)?.user + const pnDecoded = jidDecode(pair.pn) + const pnUser = pnDecoded?.user if (!pnUser) continue const lidUser = jidDecode(pair.lid)?.user if (!lidUser) continue for (const device of usyncFetch[pair.pn]!) { - const deviceSpecificLid = `${lidUser}${!!device ? `:${device}` : ``}@lid` + const deviceSpecificLid = `${lidUser}${!!device ? `:${device}` : ``}@${device == 99 ? 'hosted.lid' : 'lid'}` this.logger.trace( `getLIDForPN: USYNC success for ${pair.pn} → ${deviceSpecificLid} (user mapping with device ${device})` ) - const deviceSpecificPn = `${pnUser}${!!device ? `:${device}` : ``}@s.whatsapp.net` + const deviceSpecificPn = `${pnUser}${!!device ? `:${device}` : ``}@${device == 99 ? 'hosted' : 's.whatsapp.net'}` successfulPairs[deviceSpecificPn] = { lid: deviceSpecificLid, pn: deviceSpecificPn } } diff --git a/src/Utils/signal.ts b/src/Utils/signal.ts index a1c235e5..346838d1 100644 --- a/src/Utils/signal.ts +++ b/src/Utils/signal.ts @@ -18,7 +18,7 @@ import { getBinaryNodeChildUInt, jidDecode, S_WHATSAPP_NET, - WAJIDDomains + WAJIDDomains, } from '../WABinary' import type { DeviceListData, ParsedDeviceInfo, USyncQueryResultList } from '../WAUSync' import { Curve, generateSignalPubKey } from './crypto' @@ -153,7 +153,7 @@ export const extractDeviceJids = ( ((myUser !== user && myLid !== user) || myDevice !== device) && // either different user or if me user, not this device (device === 0 || !!keyIndex) // ensure that "key-index" is specified for "non-zero" devices, produces a bad req otherwise ) { - extracted.push({ user, device, domainType, server }) + extracted.push({ user, device, domainType: isHosted ? domainType === WAJIDDomains.LID ? WAJIDDomains.HOSTED_LID : WAJIDDomains.HOSTED : domainType, server }) } } }