WABinary: supported @hosted & @hosted.lid ids

This commit is contained in:
Rajeh Taher
2025-10-02 21:41:54 +03:00
parent b8f695cc82
commit 2d656424d6
2 changed files with 42 additions and 4 deletions
+11 -2
View File
@@ -1,7 +1,7 @@
import { promisify } from 'util'
import { inflate } from 'zlib'
import * as constants from './constants'
import { jidEncode } from './jid-utils'
import { jidEncode, type JidServer, WAJIDDomains } from './jid-utils'
import type { BinaryNode, BinaryNodeCodingOptions } from './types'
const inflatePromise = promisify(inflate)
@@ -155,7 +155,16 @@ export const decodeDecompressedBinaryNode = (
const device = readByte()
const user = readString(readByte()!)
return jidEncode(user, domainType === 0 || domainType === 128 ? 's.whatsapp.net' : 'lid', device)
let server: JidServer = 's.whatsapp.net' // default whatsapp server
if (domainType === WAJIDDomains.LID) {
server = 'lid'
} else if (domainType === WAJIDDomains.HOSTED) {
server = 'hosted'
} else if (domainType === WAJIDDomains.HOSTED_LID) {
server = 'hosted.lid'
}
return jidEncode(user, server, device)
}
const readString = (tag: number): string => {
+31 -2
View File
@@ -5,7 +5,24 @@ export const PSA_WID = '0@c.us'
export const STORIES_JID = 'status@broadcast'
export const META_AI_JID = '13135550002@c.us'
export type JidServer = 'c.us' | 'g.us' | 'broadcast' | 's.whatsapp.net' | 'call' | 'lid' | 'newsletter' | 'bot' | 'hosted' | 'hosted.lid'
export type JidServer =
| 'c.us'
| 'g.us'
| 'broadcast'
| 's.whatsapp.net'
| 'call'
| 'lid'
| 'newsletter'
| 'bot'
| 'hosted'
| 'hosted.lid'
export enum WAJIDDomains {
WHATSAPP = 0,
LID = 1,
HOSTED = 128,
HOSTED_LID = 129
}
export type JidWithDevice = {
user: string
@@ -14,13 +31,15 @@ export type JidWithDevice = {
export type FullJid = JidWithDevice & {
server: JidServer
domainType?: number
}
export const jidEncode = (user: string | number | null, server: JidServer, device?: number, agent?: number) => {
return `${user || ''}${!!agent ? `.${agent}` : ''}${!!device ? `:${device}` : ''}@${server}`
return `${user || ''}${!!agent ? `_${agent}` : ''}${!!device ? `:${device}` : ''}@${server}`
}
export const jidDecode = (jid: string | undefined): FullJid | undefined => {
// todo: investigate how to implement hosted ids in this case
const sepIdx = typeof jid === 'string' ? jid.indexOf('@') : -1
if (sepIdx < 0) {
return undefined
@@ -32,9 +51,19 @@ export const jidDecode = (jid: string | undefined): FullJid | undefined => {
const [userAgent, device] = userCombined.split(':')
const user = userAgent!.split('_')[0]!
let domainType = WAJIDDomains.WHATSAPP
if (server === 'lid') {
domainType = WAJIDDomains.LID
} else if (server === 'hosted') {
domainType = WAJIDDomains.HOSTED
} else if (server === 'hosted.lid') {
domainType = WAJIDDomains.HOSTED_LID
}
return {
server: server as JidServer,
user,
domainType,
device: device ? +device : undefined
}
}