chore: format everything

This commit is contained in:
canove
2025-05-06 12:10:19 -03:00
parent 04afa20244
commit fa706d0b50
76 changed files with 8241 additions and 7142 deletions

View File

@@ -1,6 +1,7 @@
lib
coverage
*.lock
*.json
src/WABinary/index.ts
WAProto
WASignalGroup

View File

@@ -30,8 +30,9 @@
"changelog:update": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
"example": "node --inspect -r ts-node/register Example/example.ts",
"gen:protobuf": "sh WAProto/GenerateStatics.sh",
"format": "prettier --write \"src/**/*.{ts,js,json,md}\"",
"lint": "eslint src --ext .js,.ts",
"lint:fix": "yarn lint --fix",
"lint:fix": "yarn format && yarn lint --fix",
"prepack": "tsc",
"prepare": "tsc",
"preinstall": "node ./engine-requirements.js",

View File

@@ -17,14 +17,12 @@ export const WA_DEFAULT_EPHEMERAL = 7 * 24 * 60 * 60
export const NOISE_MODE = 'Noise_XX_25519_AESGCM_SHA256\0\0\0\0'
export const DICT_VERSION = 2
export const KEY_BUNDLE_TYPE = Buffer.from([5])
export const NOISE_WA_HEADER = Buffer.from(
[ 87, 65, 6, DICT_VERSION ]
) // last is "DICT_VERSION"
export const NOISE_WA_HEADER = Buffer.from([87, 65, 6, DICT_VERSION]) // last is "DICT_VERSION"
/** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */
export const URL_REGEX = /https:\/\/(?![^:@\/\s]+:[^:@\/\s]+@)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(:\d+)?(\/[^\s]*)?/g
export const WA_CERT_DETAILS = {
SERIAL: 0,
SERIAL: 0
}
export const PROCESSABLE_HISTORY_TYPES = [
@@ -32,7 +30,7 @@ export const PROCESSABLE_HISTORY_TYPES = [
proto.Message.HistorySyncNotification.HistorySyncType.PUSH_NAME,
proto.Message.HistorySyncNotification.HistorySyncType.RECENT,
proto.Message.HistorySyncNotification.HistorySyncType.FULL,
proto.Message.HistorySyncNotification.HistorySyncType.ON_DEMAND,
proto.Message.HistorySyncNotification.HistorySyncType.ON_DEMAND
]
export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
@@ -57,14 +55,14 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
linkPreviewImageThumbnailWidth: 192,
transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 },
generateHighQualityLinkPreview: false,
options: { },
options: {},
appStateMacVerification: {
patch: false,
snapshot: false,
snapshot: false
},
countryCode: 'US',
getMessage: async() => undefined,
cachedGroupMetadata: async() => undefined,
getMessage: async () => undefined,
cachedGroupMetadata: async () => undefined,
makeSignalRepository: makeLibSignalRepository
}
@@ -77,19 +75,19 @@ export const MEDIA_PATH_MAP: { [T in MediaType]?: string } = {
'thumbnail-link': '/mms/image',
'product-catalog-image': '/product/image',
'md-app-state': '',
'md-msg-hist': '/mms/md-app-state',
'md-msg-hist': '/mms/md-app-state'
}
export const MEDIA_HKDF_KEY_MAPPING = {
'audio': 'Audio',
'document': 'Document',
'gif': 'Video',
'image': 'Image',
'ppic': '',
'product': 'Image',
'ptt': 'Audio',
'sticker': 'Image',
'video': 'Video',
audio: 'Audio',
document: 'Document',
gif: 'Video',
image: 'Image',
ppic: '',
product: 'Image',
ptt: 'Audio',
sticker: 'Image',
video: 'Video',
'thumbnail-document': 'Document Thumbnail',
'thumbnail-image': 'Image Thumbnail',
'thumbnail-video': 'Video Thumbnail',
@@ -98,7 +96,7 @@ export const MEDIA_HKDF_KEY_MAPPING = {
'md-app-state': 'App State',
'product-catalog-image': '',
'payment-bg-image': 'Payment Background',
'ptv': 'Video'
ptv: 'Video'
}
export const MEDIA_KEYS = Object.keys(MEDIA_PATH_MAP) as MediaType[]
@@ -111,5 +109,5 @@ export const DEFAULT_CACHE_TTLS = {
SIGNAL_STORE: 5 * 60, // 5 minutes
MSG_RETRY: 60 * 60, // 1 hour
CALL_OFFER: 5 * 60, // 5 minutes
USER_DEVICES: 5 * 60, // 5 minutes
USER_DEVICES: 5 * 60 // 5 minutes
}

View File

@@ -1,5 +1,11 @@
import * as libsignal from 'libsignal'
import { GroupCipher, GroupSessionBuilder, SenderKeyDistributionMessage, SenderKeyName, SenderKeyRecord } from '../../WASignalGroup'
import {
GroupCipher,
GroupSessionBuilder,
SenderKeyDistributionMessage,
SenderKeyName,
SenderKeyRecord
} from '../../WASignalGroup'
import { SignalAuthState } from '../Types'
import { SignalRepository } from '../Types/Signal'
import { generateSignalPubKey } from '../Utils'
@@ -18,9 +24,15 @@ export function makeLibSignalRepository(auth: SignalAuthState): SignalRepository
const builder = new GroupSessionBuilder(storage)
const senderName = jidToSignalSenderKeyName(item.groupId!, authorJid)
const senderMsg = new SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage)
const senderMsg = new SenderKeyDistributionMessage(
null,
null,
null,
null,
item.axolotlSenderKeyDistributionMessage
)
const { [senderName]: senderKey } = await auth.keys.get('sender-key', [senderName])
if(!senderKey) {
if (!senderKey) {
await storage.storeSenderKey(senderName, new SenderKeyRecord())
}
@@ -31,12 +43,12 @@ export function makeLibSignalRepository(auth: SignalAuthState): SignalRepository
const session = new libsignal.SessionCipher(storage, addr)
let result: Buffer
switch (type) {
case 'pkmsg':
result = await session.decryptPreKeyWhisperMessage(ciphertext)
break
case 'msg':
result = await session.decryptWhisperMessage(ciphertext)
break
case 'pkmsg':
result = await session.decryptPreKeyWhisperMessage(ciphertext)
break
case 'msg':
result = await session.decryptWhisperMessage(ciphertext)
break
}
return result
@@ -54,7 +66,7 @@ export function makeLibSignalRepository(auth: SignalAuthState): SignalRepository
const builder = new GroupSessionBuilder(storage)
const { [senderName]: senderKey } = await auth.keys.get('sender-key', [senderName])
if(!senderKey) {
if (!senderKey) {
await storage.storeSenderKey(senderName, new SenderKeyRecord())
}
@@ -64,7 +76,7 @@ export function makeLibSignalRepository(auth: SignalAuthState): SignalRepository
return {
ciphertext,
senderKeyDistributionMessage: senderKeyDistributionMessage.serialize(),
senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
}
},
async injectE2ESession({ jid, session }) {
@@ -73,7 +85,7 @@ export function makeLibSignalRepository(auth: SignalAuthState): SignalRepository
},
jidToSignalProtocolAddress(jid) {
return jidToSignalProtocolAddress(jid).toString()
},
}
}
}
@@ -88,22 +100,22 @@ const jidToSignalSenderKeyName = (group: string, user: string): string => {
function signalStorage({ creds, keys }: SignalAuthState) {
return {
loadSession: async(id: string) => {
loadSession: async (id: string) => {
const { [id]: sess } = await keys.get('session', [id])
if(sess) {
if (sess) {
return libsignal.SessionRecord.deserialize(sess)
}
},
storeSession: async(id, session) => {
await keys.set({ 'session': { [id]: session.serialize() } })
storeSession: async (id, session) => {
await keys.set({ session: { [id]: session.serialize() } })
},
isTrustedIdentity: () => {
return true
},
loadPreKey: async(id: number | string) => {
loadPreKey: async (id: number | string) => {
const keyId = id.toString()
const { [keyId]: key } = await keys.get('pre-key', [keyId])
if(key) {
if (key) {
return {
privKey: Buffer.from(key.private),
pubKey: Buffer.from(key.public)
@@ -118,24 +130,22 @@ function signalStorage({ creds, keys }: SignalAuthState) {
pubKey: Buffer.from(key.keyPair.public)
}
},
loadSenderKey: async(keyId: string) => {
loadSenderKey: async (keyId: string) => {
const { [keyId]: key } = await keys.get('sender-key', [keyId])
if(key) {
if (key) {
return new SenderKeyRecord(key)
}
},
storeSenderKey: async(keyId, key) => {
storeSenderKey: async (keyId, key) => {
await keys.set({ 'sender-key': { [keyId]: key.serialize() } })
},
getOurRegistrationId: () => (
creds.registrationId
),
getOurRegistrationId: () => creds.registrationId,
getOurIdentity: () => {
const { signedIdentityKey } = creds
return {
privKey: Buffer.from(signedIdentityKey.private),
pubKey: generateSignalPubKey(signedIdentityKey.public),
pubKey: generateSignalPubKey(signedIdentityKey.public)
}
}
}
}
}

View File

@@ -1,2 +1,2 @@
export * from './types'
export * from './websocket'
export * from './websocket'

View File

@@ -8,12 +8,15 @@ export abstract class AbstractSocketClient extends EventEmitter {
abstract get isClosing(): boolean
abstract get isConnecting(): boolean
constructor(public url: URL, public config: SocketConfig) {
constructor(
public url: URL,
public config: SocketConfig
) {
super()
this.setMaxListeners(0)
}
abstract connect(): Promise<void>
abstract close(): Promise<void>
abstract send(str: Uint8Array | string, cb?: (err?: Error) => void): boolean;
}
abstract send(str: Uint8Array | string, cb?: (err?: Error) => void): boolean
}

View File

@@ -3,7 +3,6 @@ import { DEFAULT_ORIGIN } from '../../Defaults'
import { AbstractSocketClient } from './types'
export class WebSocketClient extends AbstractSocketClient {
protected socket: WebSocket | null = null
get isOpen(): boolean {
@@ -20,7 +19,7 @@ export class WebSocketClient extends AbstractSocketClient {
}
async connect(): Promise<void> {
if(this.socket) {
if (this.socket) {
return
}
@@ -29,20 +28,20 @@ export class WebSocketClient extends AbstractSocketClient {
headers: this.config.options?.headers as {},
handshakeTimeout: this.config.connectTimeoutMs,
timeout: this.config.connectTimeoutMs,
agent: this.config.agent,
agent: this.config.agent
})
this.socket.setMaxListeners(0)
const events = ['close', 'error', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response']
for(const event of events) {
for (const event of events) {
this.socket?.on(event, (...args: any[]) => this.emit(event, ...args))
}
}
async close(): Promise<void> {
if(!this.socket) {
if (!this.socket) {
return
}

View File

@@ -1,43 +1,46 @@
import { GetCatalogOptions, ProductCreate, ProductUpdate, SocketConfig } from '../Types'
import { parseCatalogNode, parseCollectionsNode, parseOrderDetailsNode, parseProductNode, toProductNode, uploadingNecessaryImagesOfProduct } from '../Utils/business'
import {
parseCatalogNode,
parseCollectionsNode,
parseOrderDetailsNode,
parseProductNode,
toProductNode,
uploadingNecessaryImagesOfProduct
} from '../Utils/business'
import { BinaryNode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary'
import { getBinaryNodeChild } from '../WABinary/generic-utils'
import { makeMessagesRecvSocket } from './messages-recv'
export const makeBusinessSocket = (config: SocketConfig) => {
const sock = makeMessagesRecvSocket(config)
const {
authState,
query,
waUploadToServer
} = sock
const { authState, query, waUploadToServer } = sock
const getCatalog = async({ jid, limit, cursor }: GetCatalogOptions) => {
const getCatalog = async ({ jid, limit, cursor }: GetCatalogOptions) => {
jid = jid || authState.creds.me?.id
jid = jidNormalizedUser(jid)
const queryParamNodes: BinaryNode[] = [
{
tag: 'limit',
attrs: { },
attrs: {},
content: Buffer.from((limit || 10).toString())
},
{
tag: 'width',
attrs: { },
attrs: {},
content: Buffer.from('100')
},
{
tag: 'height',
attrs: { },
attrs: {},
content: Buffer.from('100')
},
}
]
if(cursor) {
if (cursor) {
queryParamNodes.push({
tag: 'after',
attrs: { },
attrs: {},
content: cursor
})
}
@@ -54,7 +57,7 @@ export const makeBusinessSocket = (config: SocketConfig) => {
tag: 'product_catalog',
attrs: {
jid,
'allow_shop_source': 'true'
allow_shop_source: 'true'
},
content: queryParamNodes
}
@@ -63,7 +66,7 @@ export const makeBusinessSocket = (config: SocketConfig) => {
return parseCatalogNode(result)
}
const getCollections = async(jid?: string, limit = 51) => {
const getCollections = async (jid?: string, limit = 51) => {
jid = jid || authState.creds.me?.id
jid = jidNormalizedUser(jid)
const result = await query({
@@ -72,33 +75,33 @@ export const makeBusinessSocket = (config: SocketConfig) => {
to: S_WHATSAPP_NET,
type: 'get',
xmlns: 'w:biz:catalog',
'smax_id': '35'
smax_id: '35'
},
content: [
{
tag: 'collections',
attrs: {
'biz_jid': jid,
biz_jid: jid
},
content: [
{
tag: 'collection_limit',
attrs: { },
attrs: {},
content: Buffer.from(limit.toString())
},
{
tag: 'item_limit',
attrs: { },
attrs: {},
content: Buffer.from(limit.toString())
},
{
tag: 'width',
attrs: { },
attrs: {},
content: Buffer.from('100')
},
{
tag: 'height',
attrs: { },
attrs: {},
content: Buffer.from('100')
}
]
@@ -109,14 +112,14 @@ export const makeBusinessSocket = (config: SocketConfig) => {
return parseCollectionsNode(result)
}
const getOrderDetails = async(orderId: string, tokenBase64: string) => {
const getOrderDetails = async (orderId: string, tokenBase64: string) => {
const result = await query({
tag: 'iq',
attrs: {
to: S_WHATSAPP_NET,
type: 'get',
xmlns: 'fb:thrift_iq',
'smax_id': '5'
smax_id: '5'
},
content: [
{
@@ -128,23 +131,23 @@ export const makeBusinessSocket = (config: SocketConfig) => {
content: [
{
tag: 'image_dimensions',
attrs: { },
attrs: {},
content: [
{
tag: 'width',
attrs: { },
attrs: {},
content: Buffer.from('100')
},
{
tag: 'height',
attrs: { },
attrs: {},
content: Buffer.from('100')
}
]
},
{
tag: 'token',
attrs: { },
attrs: {},
content: Buffer.from(tokenBase64)
}
]
@@ -155,7 +158,7 @@ export const makeBusinessSocket = (config: SocketConfig) => {
return parseOrderDetailsNode(result)
}
const productUpdate = async(productId: string, update: ProductUpdate) => {
const productUpdate = async (productId: string, update: ProductUpdate) => {
update = await uploadingNecessaryImagesOfProduct(update, waUploadToServer)
const editNode = toProductNode(productId, update)
@@ -174,12 +177,12 @@ export const makeBusinessSocket = (config: SocketConfig) => {
editNode,
{
tag: 'width',
attrs: { },
attrs: {},
content: '100'
},
{
tag: 'height',
attrs: { },
attrs: {},
content: '100'
}
]
@@ -193,7 +196,7 @@ export const makeBusinessSocket = (config: SocketConfig) => {
return parseProductNode(productNode!)
}
const productCreate = async(create: ProductCreate) => {
const productCreate = async (create: ProductCreate) => {
// ensure isHidden is defined
create.isHidden = !!create.isHidden
create = await uploadingNecessaryImagesOfProduct(create, waUploadToServer)
@@ -214,12 +217,12 @@ export const makeBusinessSocket = (config: SocketConfig) => {
createNode,
{
tag: 'width',
attrs: { },
attrs: {},
content: '100'
},
{
tag: 'height',
attrs: { },
attrs: {},
content: '100'
}
]
@@ -233,7 +236,7 @@ export const makeBusinessSocket = (config: SocketConfig) => {
return parseProductNode(productNode!)
}
const productDelete = async(productIds: string[]) => {
const productDelete = async (productIds: string[]) => {
const result = await query({
tag: 'iq',
attrs: {
@@ -245,19 +248,17 @@ export const makeBusinessSocket = (config: SocketConfig) => {
{
tag: 'product_catalog_delete',
attrs: { v: '1' },
content: productIds.map(
id => ({
tag: 'product',
attrs: { },
content: [
{
tag: 'id',
attrs: { },
content: Buffer.from(id)
}
]
})
)
content: productIds.map(id => ({
tag: 'product',
attrs: {},
content: [
{
tag: 'id',
attrs: {},
content: Buffer.from(id)
}
]
}))
}
]
})

File diff suppressed because it is too large Load Diff

View File

@@ -1,62 +1,70 @@
import { proto } from '../../WAProto'
import { GroupMetadata, GroupParticipant, ParticipantAction, SocketConfig, WAMessageKey, WAMessageStubType } from '../Types'
import {
GroupMetadata,
GroupParticipant,
ParticipantAction,
SocketConfig,
WAMessageKey,
WAMessageStubType
} from '../Types'
import { generateMessageIDV2, unixTimestampSeconds } from '../Utils'
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, jidEncode, jidNormalizedUser } from '../WABinary'
import {
BinaryNode,
getBinaryNodeChild,
getBinaryNodeChildren,
getBinaryNodeChildString,
jidEncode,
jidNormalizedUser
} from '../WABinary'
import { makeChatsSocket } from './chats'
export const makeGroupsSocket = (config: SocketConfig) => {
const sock = makeChatsSocket(config)
const { authState, ev, query, upsertMessage } = sock
const groupQuery = async(jid: string, type: 'get' | 'set', content: BinaryNode[]) => (
const groupQuery = async (jid: string, type: 'get' | 'set', content: BinaryNode[]) =>
query({
tag: 'iq',
attrs: {
type,
xmlns: 'w:g2',
to: jid,
to: jid
},
content
})
)
const groupMetadata = async(jid: string) => {
const result = await groupQuery(
jid,
'get',
[ { tag: 'query', attrs: { request: 'interactive' } } ]
)
const groupMetadata = async (jid: string) => {
const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }])
return extractGroupMetadata(result)
}
const groupFetchAllParticipating = async() => {
const groupFetchAllParticipating = async () => {
const result = await query({
tag: 'iq',
attrs: {
to: '@g.us',
xmlns: 'w:g2',
type: 'get',
type: 'get'
},
content: [
{
tag: 'participating',
attrs: { },
attrs: {},
content: [
{ tag: 'participants', attrs: { } },
{ tag: 'description', attrs: { } }
{ tag: 'participants', attrs: {} },
{ tag: 'description', attrs: {} }
]
}
]
})
const data: { [_: string]: GroupMetadata } = { }
const data: { [_: string]: GroupMetadata } = {}
const groupsChild = getBinaryNodeChild(result, 'groups')
if(groupsChild) {
if (groupsChild) {
const groups = getBinaryNodeChildren(groupsChild, 'group')
for(const groupNode of groups) {
for (const groupNode of groups) {
const meta = extractGroupMetadata({
tag: 'result',
attrs: { },
attrs: {},
content: [groupNode]
})
data[meta.id] = meta
@@ -68,9 +76,9 @@ export const makeGroupsSocket = (config: SocketConfig) => {
return data
}
sock.ws.on('CB:ib,,dirty', async(node: BinaryNode) => {
sock.ws.on('CB:ib,,dirty', async (node: BinaryNode) => {
const { attrs } = getBinaryNodeChild(node, 'dirty')!
if(attrs.type !== 'groups') {
if (attrs.type !== 'groups') {
return
}
@@ -81,89 +89,69 @@ export const makeGroupsSocket = (config: SocketConfig) => {
return {
...sock,
groupMetadata,
groupCreate: async(subject: string, participants: string[]) => {
groupCreate: async (subject: string, participants: string[]) => {
const key = generateMessageIDV2()
const result = await groupQuery(
'@g.us',
'set',
[
{
tag: 'create',
attrs: {
subject,
key
},
content: participants.map(jid => ({
tag: 'participant',
attrs: { jid }
}))
}
]
)
const result = await groupQuery('@g.us', 'set', [
{
tag: 'create',
attrs: {
subject,
key
},
content: participants.map(jid => ({
tag: 'participant',
attrs: { jid }
}))
}
])
return extractGroupMetadata(result)
},
groupLeave: async(id: string) => {
await groupQuery(
'@g.us',
'set',
[
{
tag: 'leave',
attrs: { },
content: [
{ tag: 'group', attrs: { id } }
]
}
]
)
groupLeave: async (id: string) => {
await groupQuery('@g.us', 'set', [
{
tag: 'leave',
attrs: {},
content: [{ tag: 'group', attrs: { id } }]
}
])
},
groupUpdateSubject: async(jid: string, subject: string) => {
await groupQuery(
jid,
'set',
[
{
tag: 'subject',
attrs: { },
content: Buffer.from(subject, 'utf-8')
}
]
)
groupUpdateSubject: async (jid: string, subject: string) => {
await groupQuery(jid, 'set', [
{
tag: 'subject',
attrs: {},
content: Buffer.from(subject, 'utf-8')
}
])
},
groupRequestParticipantsList: async(jid: string) => {
const result = await groupQuery(
jid,
'get',
[
{
tag: 'membership_approval_requests',
attrs: {}
}
]
)
groupRequestParticipantsList: async (jid: string) => {
const result = await groupQuery(jid, 'get', [
{
tag: 'membership_approval_requests',
attrs: {}
}
])
const node = getBinaryNodeChild(result, 'membership_approval_requests')
const participants = getBinaryNodeChildren(node, 'membership_approval_request')
return participants.map(v => v.attrs)
},
groupRequestParticipantsUpdate: async(jid: string, participants: string[], action: 'approve' | 'reject') => {
const result = await groupQuery(
jid,
'set',
[{
groupRequestParticipantsUpdate: async (jid: string, participants: string[], action: 'approve' | 'reject') => {
const result = await groupQuery(jid, 'set', [
{
tag: 'membership_requests_action',
attrs: {},
content: [
content: [
{
tag: action,
attrs: { },
attrs: {},
content: participants.map(jid => ({
tag: 'participant',
attrs: { jid }
}))
}
]
}]
)
}
])
const node = getBinaryNodeChild(result, 'membership_requests_action')
const nodeAction = getBinaryNodeChild(node, action)
const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant')
@@ -171,63 +159,49 @@ export const makeGroupsSocket = (config: SocketConfig) => {
return { status: p.attrs.error || '200', jid: p.attrs.jid }
})
},
groupParticipantsUpdate: async(
jid: string,
participants: string[],
action: ParticipantAction
) => {
const result = await groupQuery(
jid,
'set',
[
{
tag: action,
attrs: { },
content: participants.map(jid => ({
tag: 'participant',
attrs: { jid }
}))
}
]
)
groupParticipantsUpdate: async (jid: string, participants: string[], action: ParticipantAction) => {
const result = await groupQuery(jid, 'set', [
{
tag: action,
attrs: {},
content: participants.map(jid => ({
tag: 'participant',
attrs: { jid }
}))
}
])
const node = getBinaryNodeChild(result, action)
const participantsAffected = getBinaryNodeChildren(node, 'participant')
return participantsAffected.map(p => {
return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p }
})
},
groupUpdateDescription: async(jid: string, description?: string) => {
groupUpdateDescription: async (jid: string, description?: string) => {
const metadata = await groupMetadata(jid)
const prev = metadata.descId ?? null
await groupQuery(
jid,
'set',
[
{
tag: 'description',
attrs: {
...(description ? { id: generateMessageIDV2() } : { delete: 'true' }),
...(prev ? { prev } : {})
},
content: description ? [
{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }
] : undefined
}
]
)
await groupQuery(jid, 'set', [
{
tag: 'description',
attrs: {
...(description ? { id: generateMessageIDV2() } : { delete: 'true' }),
...(prev ? { prev } : {})
},
content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
}
])
},
groupInviteCode: async(jid: string) => {
groupInviteCode: async (jid: string) => {
const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }])
const inviteNode = getBinaryNodeChild(result, 'invite')
return inviteNode?.attrs.code
},
groupRevokeInvite: async(jid: string) => {
groupRevokeInvite: async (jid: string) => {
const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }])
const inviteNode = getBinaryNodeChild(result, 'invite')
return inviteNode?.attrs.code
},
groupAcceptInvite: async(code: string) => {
groupAcceptInvite: async (code: string) => {
const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }])
const result = getBinaryNodeChild(results, 'group')
return result?.attrs.jid
@@ -239,8 +213,10 @@ export const makeGroupsSocket = (config: SocketConfig) => {
* @param invitedJid jid of person you invited
* @returns true if successful
*/
groupRevokeInviteV4: async(groupJid: string, invitedJid: string) => {
const result = await groupQuery(groupJid, 'set', [{ tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }])
groupRevokeInviteV4: async (groupJid: string, invitedJid: string) => {
const result = await groupQuery(groupJid, 'set', [
{ tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
])
return !!result
},
@@ -249,87 +225,90 @@ export const makeGroupsSocket = (config: SocketConfig) => {
* @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
* @param inviteMessage the message to accept
*/
groupAcceptInviteV4: ev.createBufferedFunction(async(key: string | WAMessageKey, inviteMessage: proto.Message.IGroupInviteMessage) => {
key = typeof key === 'string' ? { remoteJid: key } : key
const results = await groupQuery(inviteMessage.groupJid!, 'set', [{
tag: 'accept',
attrs: {
code: inviteMessage.inviteCode!,
expiration: inviteMessage.inviteExpiration!.toString(),
admin: key.remoteJid!
}
}])
// if we have the full message key
// update the invite message to be expired
if(key.id) {
// create new invite message that is expired
inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage)
inviteMessage.inviteExpiration = 0
inviteMessage.inviteCode = ''
ev.emit('messages.update', [
groupAcceptInviteV4: ev.createBufferedFunction(
async (key: string | WAMessageKey, inviteMessage: proto.Message.IGroupInviteMessage) => {
key = typeof key === 'string' ? { remoteJid: key } : key
const results = await groupQuery(inviteMessage.groupJid!, 'set', [
{
key,
update: {
message: {
groupInviteMessage: inviteMessage
}
tag: 'accept',
attrs: {
code: inviteMessage.inviteCode!,
expiration: inviteMessage.inviteExpiration!.toString(),
admin: key.remoteJid!
}
}
])
}
// generate the group add message
await upsertMessage(
{
key: {
remoteJid: inviteMessage.groupJid,
id: generateMessageIDV2(sock.user?.id),
fromMe: false,
// if we have the full message key
// update the invite message to be expired
if (key.id) {
// create new invite message that is expired
inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage)
inviteMessage.inviteExpiration = 0
inviteMessage.inviteCode = ''
ev.emit('messages.update', [
{
key,
update: {
message: {
groupInviteMessage: inviteMessage
}
}
}
])
}
// generate the group add message
await upsertMessage(
{
key: {
remoteJid: inviteMessage.groupJid,
id: generateMessageIDV2(sock.user?.id),
fromMe: false,
participant: key.remoteJid
},
messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
messageStubParameters: [authState.creds.me!.id],
participant: key.remoteJid,
messageTimestamp: unixTimestampSeconds()
},
messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
messageStubParameters: [
authState.creds.me!.id
],
participant: key.remoteJid,
messageTimestamp: unixTimestampSeconds()
},
'notify'
)
'notify'
)
return results.attrs.from
}),
groupGetInviteInfo: async(code: string) => {
return results.attrs.from
}
),
groupGetInviteInfo: async (code: string) => {
const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }])
return extractGroupMetadata(results)
},
groupToggleEphemeral: async(jid: string, ephemeralExpiration: number) => {
const content: BinaryNode = ephemeralExpiration ?
{ tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } } :
{ tag: 'not_ephemeral', attrs: { } }
groupToggleEphemeral: async (jid: string, ephemeralExpiration: number) => {
const content: BinaryNode = ephemeralExpiration
? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
: { tag: 'not_ephemeral', attrs: {} }
await groupQuery(jid, 'set', [content])
},
groupSettingUpdate: async(jid: string, setting: 'announcement' | 'not_announcement' | 'locked' | 'unlocked') => {
await groupQuery(jid, 'set', [ { tag: setting, attrs: { } } ])
groupSettingUpdate: async (jid: string, setting: 'announcement' | 'not_announcement' | 'locked' | 'unlocked') => {
await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }])
},
groupMemberAddMode: async(jid: string, mode: 'admin_add' | 'all_member_add') => {
await groupQuery(jid, 'set', [ { tag: 'member_add_mode', attrs: { }, content: mode } ])
groupMemberAddMode: async (jid: string, mode: 'admin_add' | 'all_member_add') => {
await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }])
},
groupJoinApprovalMode: async(jid: string, mode: 'on' | 'off') => {
await groupQuery(jid, 'set', [ { tag: 'membership_approval_mode', attrs: { }, content: [ { tag: 'group_join', attrs: { state: mode } } ] } ])
groupJoinApprovalMode: async (jid: string, mode: 'on' | 'off') => {
await groupQuery(jid, 'set', [
{ tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }
])
},
groupFetchAllParticipating
}
}
export const extractGroupMetadata = (result: BinaryNode) => {
const group = getBinaryNodeChild(result, 'group')!
const descChild = getBinaryNodeChild(group, 'description')
let desc: string | undefined
let descId: string | undefined
if(descChild) {
if (descChild) {
desc = getBinaryNodeChildString(descChild, 'body')
descId = descChild.attrs.id
}
@@ -355,14 +334,12 @@ export const extractGroupMetadata = (result: BinaryNode) => {
isCommunityAnnounce: !!getBinaryNodeChild(group, 'default_sub_group'),
joinApprovalMode: !!getBinaryNodeChild(group, 'membership_approval_mode'),
memberAddMode,
participants: getBinaryNodeChildren(group, 'participant').map(
({ attrs }) => {
return {
id: attrs.jid,
admin: (attrs.type || null) as GroupParticipant['admin'],
}
participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
return {
id: attrs.jid,
admin: (attrs.type || null) as GroupParticipant['admin']
}
),
}),
ephemeralDuration: eph ? +eph : undefined
}
return metadata

View File

@@ -3,11 +3,10 @@ import { UserFacingSocketConfig } from '../Types'
import { makeBusinessSocket } from './business'
// export the last socket layer
const makeWASocket = (config: UserFacingSocketConfig) => (
const makeWASocket = (config: UserFacingSocketConfig) =>
makeBusinessSocket({
...DEFAULT_CONNECTION_CONFIG,
...config
})
)
export default makeWASocket
export default makeWASocket

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -28,7 +28,7 @@ import {
getPlatformId,
makeEventBuffer,
makeNoiseHandler,
promiseTimeout,
promiseTimeout
} from '../Utils'
import {
assertNodeErrorFree,
@@ -61,21 +61,22 @@ export const makeSocket = (config: SocketConfig) => {
defaultQueryTimeoutMs,
transactionOpts,
qrTimeout,
makeSignalRepository,
makeSignalRepository
} = config
if(printQRInTerminal) {
console.warn('⚠️ The printQRInTerminal option has been deprecated. You will no longer receive QR codes in the terminal automatically. Please listen to the connection.update event yourself and handle the QR your way. You can remove this message by removing this opttion. This message will be removed in a future version.')
if (printQRInTerminal) {
console.warn(
'⚠️ The printQRInTerminal option has been deprecated. You will no longer receive QR codes in the terminal automatically. Please listen to the connection.update event yourself and handle the QR your way. You can remove this message by removing this opttion. This message will be removed in a future version.'
)
}
const url = typeof waWebSocketUrl === 'string' ? new URL(waWebSocketUrl) : waWebSocketUrl
if(config.mobile || url.protocol === 'tcp:') {
if (config.mobile || url.protocol === 'tcp:') {
throw new Boom('Mobile API is not supported anymore', { statusCode: DisconnectReason.loggedOut })
}
if(url.protocol === 'wss' && authState?.creds?.routingInfo) {
if (url.protocol === 'wss' && authState?.creds?.routingInfo) {
url.searchParams.append('ED', authState.creds.routingInfo.toString('base64url'))
}
@@ -110,28 +111,25 @@ export const makeSocket = (config: SocketConfig) => {
const sendPromise = promisify(ws.send)
/** send a raw buffer */
const sendRawMessage = async(data: Uint8Array | Buffer) => {
if(!ws.isOpen) {
const sendRawMessage = async (data: Uint8Array | Buffer) => {
if (!ws.isOpen) {
throw new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed })
}
const bytes = noise.encodeFrame(data)
await promiseTimeout<void>(
connectTimeoutMs,
async(resolve, reject) => {
try {
await sendPromise.call(ws, bytes)
resolve()
} catch(error) {
reject(error)
}
await promiseTimeout<void>(connectTimeoutMs, async (resolve, reject) => {
try {
await sendPromise.call(ws, bytes)
resolve()
} catch (error) {
reject(error)
}
)
})
}
/** send a binary node */
const sendNode = (frame: BinaryNode) => {
if(logger.level === 'trace') {
if (logger.level === 'trace') {
logger.trace({ xml: binaryNodeToString(frame), msg: 'xml send' })
}
@@ -141,15 +139,12 @@ export const makeSocket = (config: SocketConfig) => {
/** log & process any unexpected errors */
const onUnexpectedError = (err: Error | Boom, msg: string) => {
logger.error(
{ err },
`unexpected error in '${msg}'`
)
logger.error({ err }, `unexpected error in '${msg}'`)
}
/** await the next incoming message */
const awaitNextMessage = async<T>(sendMsg?: Uint8Array) => {
if(!ws.isOpen) {
const awaitNextMessage = async <T>(sendMsg?: Uint8Array) => {
if (!ws.isOpen) {
throw new Boom('Connection Closed', {
statusCode: DisconnectReason.connectionClosed
})
@@ -164,14 +159,13 @@ export const makeSocket = (config: SocketConfig) => {
ws.on('frame', onOpen)
ws.on('close', onClose)
ws.on('error', onClose)
}).finally(() => {
ws.off('frame', onOpen)
ws.off('close', onClose)
ws.off('error', onClose)
})
.finally(() => {
ws.off('frame', onOpen)
ws.off('close', onClose)
ws.off('error', onClose)
})
if(sendMsg) {
if (sendMsg) {
sendRawMessage(sendMsg).catch(onClose!)
}
@@ -183,22 +177,20 @@ export const makeSocket = (config: SocketConfig) => {
* @param msgId the message tag to await
* @param timeoutMs timeout after which the promise will reject
*/
const waitForMessage = async<T>(msgId: string, timeoutMs = defaultQueryTimeoutMs) => {
const waitForMessage = async <T>(msgId: string, timeoutMs = defaultQueryTimeoutMs) => {
let onRecv: (json) => void
let onErr: (err) => void
try {
const result = await promiseTimeout<T>(timeoutMs,
(resolve, reject) => {
onRecv = resolve
onErr = err => {
reject(err || new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed }))
}
const result = await promiseTimeout<T>(timeoutMs, (resolve, reject) => {
onRecv = resolve
onErr = err => {
reject(err || new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed }))
}
ws.on(`TAG:${msgId}`, onRecv)
ws.on('close', onErr) // if the socket closes, you'll never receive the message
ws.off('error', onErr)
},
)
ws.on(`TAG:${msgId}`, onRecv)
ws.on('close', onErr) // if the socket closes, you'll never receive the message
ws.off('error', onErr)
})
return result as any
} finally {
@@ -209,19 +201,16 @@ export const makeSocket = (config: SocketConfig) => {
}
/** send a query, and wait for its response. auto-generates message ID if not provided */
const query = async(node: BinaryNode, timeoutMs?: number) => {
if(!node.attrs.id) {
const query = async (node: BinaryNode, timeoutMs?: number) => {
if (!node.attrs.id) {
node.attrs.id = generateMessageTag()
}
const msgId = node.attrs.id
const [result] = await Promise.all([
waitForMessage(msgId, timeoutMs),
sendNode(node)
])
const [result] = await Promise.all([waitForMessage(msgId, timeoutMs), sendNode(node)])
if('tag' in result) {
if ('tag' in result) {
assertNodeErrorFree(result)
}
@@ -229,7 +218,7 @@ export const makeSocket = (config: SocketConfig) => {
}
/** connection handshake */
const validateConnection = async() => {
const validateConnection = async () => {
let helloMsg: proto.IHandshakeMessage = {
clientHello: { ephemeral: ephemeralKeyPair.public }
}
@@ -247,7 +236,7 @@ export const makeSocket = (config: SocketConfig) => {
const keyEnc = await noise.processHandshake(handshake, creds.noiseKey)
let node: proto.IClientPayload
if(!creds.me) {
if (!creds.me) {
node = generateRegistrationNode(creds, config)
logger.info({ node }, 'not logged in, attempting registration...')
} else {
@@ -255,22 +244,20 @@ export const makeSocket = (config: SocketConfig) => {
logger.info({ node }, 'logging in...')
}
const payloadEnc = noise.encrypt(
proto.ClientPayload.encode(node).finish()
)
const payloadEnc = noise.encrypt(proto.ClientPayload.encode(node).finish())
await sendRawMessage(
proto.HandshakeMessage.encode({
clientFinish: {
static: keyEnc,
payload: payloadEnc,
},
payload: payloadEnc
}
}).finish()
)
noise.finishInit()
startKeepAliveRequest()
}
const getAvailablePreKeysOnServer = async() => {
const getAvailablePreKeysOnServer = async () => {
const result = await query({
tag: 'iq',
attrs: {
@@ -279,33 +266,29 @@ export const makeSocket = (config: SocketConfig) => {
type: 'get',
to: S_WHATSAPP_NET
},
content: [
{ tag: 'count', attrs: {} }
]
content: [{ tag: 'count', attrs: {} }]
})
const countChild = getBinaryNodeChild(result, 'count')
return +countChild!.attrs.value
}
/** generates and uploads a set of pre-keys to the server */
const uploadPreKeys = async(count = INITIAL_PREKEY_COUNT) => {
await keys.transaction(
async() => {
logger.info({ count }, 'uploading pre-keys')
const { update, node } = await getNextPreKeysNode({ creds, keys }, count)
const uploadPreKeys = async (count = INITIAL_PREKEY_COUNT) => {
await keys.transaction(async () => {
logger.info({ count }, 'uploading pre-keys')
const { update, node } = await getNextPreKeysNode({ creds, keys }, count)
await query(node)
ev.emit('creds.update', update)
await query(node)
ev.emit('creds.update', update)
logger.info({ count }, 'uploaded pre-keys')
}
)
logger.info({ count }, 'uploaded pre-keys')
})
}
const uploadPreKeysToServerIfRequired = async() => {
const uploadPreKeysToServerIfRequired = async () => {
const preKeyCount = await getAvailablePreKeysOnServer()
logger.info(`${preKeyCount} pre-keys found on server`)
if(preKeyCount <= MIN_PREKEY_COUNT) {
if (preKeyCount <= MIN_PREKEY_COUNT) {
await uploadPreKeys()
}
}
@@ -319,10 +302,10 @@ export const makeSocket = (config: SocketConfig) => {
anyTriggered = ws.emit('frame', frame)
// if it's a binary node
if(!(frame instanceof Uint8Array)) {
if (!(frame instanceof Uint8Array)) {
const msgId = frame.attrs.id
if(logger.level === 'trace') {
if (logger.level === 'trace') {
logger.trace({ xml: binaryNodeToString(frame), msg: 'recv xml' })
}
@@ -333,7 +316,7 @@ export const makeSocket = (config: SocketConfig) => {
const l1 = frame.attrs || {}
const l2 = Array.isArray(frame.content) ? frame.content[0]?.tag : ''
for(const key of Object.keys(l1)) {
for (const key of Object.keys(l1)) {
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0},${key}:${l1[key]},${l2}`, frame) || anyTriggered
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0},${key}:${l1[key]}`, frame) || anyTriggered
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0},${key}`, frame) || anyTriggered
@@ -342,7 +325,7 @@ export const makeSocket = (config: SocketConfig) => {
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0},,${l2}`, frame) || anyTriggered
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0}`, frame) || anyTriggered
if(!anyTriggered && logger.level === 'debug') {
if (!anyTriggered && logger.level === 'debug') {
logger.debug({ unhandled: true, msgId, fromMe: false, frame }, 'communication recv')
}
}
@@ -350,16 +333,13 @@ export const makeSocket = (config: SocketConfig) => {
}
const end = (error: Error | undefined) => {
if(closed) {
if (closed) {
logger.trace({ trace: error?.stack }, 'connection already closed')
return
}
closed = true
logger.info(
{ trace: error?.stack },
error ? 'connection errored' : 'connection closed'
)
logger.info({ trace: error?.stack }, error ? 'connection errored' : 'connection closed')
clearInterval(keepAliveReq)
clearTimeout(qrTimer)
@@ -369,10 +349,10 @@ export const makeSocket = (config: SocketConfig) => {
ws.removeAllListeners('open')
ws.removeAllListeners('message')
if(!ws.isClosed && !ws.isClosing) {
if (!ws.isClosed && !ws.isClosing) {
try {
ws.close()
} catch{ }
} catch {}
}
ev.emit('connection.update', {
@@ -385,12 +365,12 @@ export const makeSocket = (config: SocketConfig) => {
ev.removeAllListeners('connection.update')
}
const waitForSocketOpen = async() => {
if(ws.isOpen) {
const waitForSocketOpen = async () => {
if (ws.isOpen) {
return
}
if(ws.isClosed || ws.isClosing) {
if (ws.isClosed || ws.isClosing) {
throw new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed })
}
@@ -402,17 +382,16 @@ export const makeSocket = (config: SocketConfig) => {
ws.on('open', onOpen)
ws.on('close', onClose)
ws.on('error', onClose)
}).finally(() => {
ws.off('open', onOpen)
ws.off('close', onClose)
ws.off('error', onClose)
})
.finally(() => {
ws.off('open', onOpen)
ws.off('close', onClose)
ws.off('error', onClose)
})
}
const startKeepAliveRequest = () => (
keepAliveReq = setInterval(() => {
if(!lastDateRecv) {
const startKeepAliveRequest = () =>
(keepAliveReq = setInterval(() => {
if (!lastDateRecv) {
lastDateRecv = new Date()
}
@@ -421,49 +400,42 @@ export const makeSocket = (config: SocketConfig) => {
check if it's been a suspicious amount of time since the server responded with our last seen
it could be that the network is down
*/
if(diff > keepAliveIntervalMs + 5000) {
if (diff > keepAliveIntervalMs + 5000) {
end(new Boom('Connection was lost', { statusCode: DisconnectReason.connectionLost }))
} else if(ws.isOpen) {
} else if (ws.isOpen) {
// if its all good, send a keep alive request
query(
{
tag: 'iq',
attrs: {
id: generateMessageTag(),
to: S_WHATSAPP_NET,
type: 'get',
xmlns: 'w:p',
},
content: [{ tag: 'ping', attrs: {} }]
}
)
.catch(err => {
logger.error({ trace: err.stack }, 'error in sending keep alive')
})
query({
tag: 'iq',
attrs: {
id: generateMessageTag(),
to: S_WHATSAPP_NET,
type: 'get',
xmlns: 'w:p'
},
content: [{ tag: 'ping', attrs: {} }]
}).catch(err => {
logger.error({ trace: err.stack }, 'error in sending keep alive')
})
} else {
logger.warn('keep alive called when WS not open')
}
}, keepAliveIntervalMs)
)
}, keepAliveIntervalMs))
/** i have no idea why this exists. pls enlighten me */
const sendPassiveIq = (tag: 'passive' | 'active') => (
const sendPassiveIq = (tag: 'passive' | 'active') =>
query({
tag: 'iq',
attrs: {
to: S_WHATSAPP_NET,
xmlns: 'passive',
type: 'set',
type: 'set'
},
content: [
{ tag, attrs: {} }
]
content: [{ tag, attrs: {} }]
})
)
/** logout & invalidate connection */
const logout = async(msg?: string) => {
const logout = async (msg?: string) => {
const jid = authState.creds.me?.id
if(jid) {
if (jid) {
await sendNode({
tag: 'iq',
attrs: {
@@ -487,7 +459,7 @@ export const makeSocket = (config: SocketConfig) => {
end(new Boom(msg || 'Intentional Logout', { statusCode: DisconnectReason.loggedOut }))
}
const requestPairingCode = async(phoneNumber: string): Promise<string> => {
const requestPairingCode = async (phoneNumber: string): Promise<string> => {
authState.creds.pairingCode = bytesToCrockford(randomBytes(5))
authState.creds.me = {
id: jidEncode(phoneNumber, 's.whatsapp.net'),
@@ -572,10 +544,10 @@ export const makeSocket = (config: SocketConfig) => {
ws.on('message', onMessageReceived)
ws.on('open', async() => {
ws.on('open', async () => {
try {
await validateConnection()
} catch(err) {
} catch (err) {
logger.error({ err }, 'error in validating connection')
end(err)
}
@@ -583,15 +555,17 @@ export const makeSocket = (config: SocketConfig) => {
ws.on('error', mapWebSocketError(end))
ws.on('close', () => end(new Boom('Connection Terminated', { statusCode: DisconnectReason.connectionClosed })))
// the server terminated the connection
ws.on('CB:xmlstreamend', () => end(new Boom('Connection Terminated by Server', { statusCode: DisconnectReason.connectionClosed })))
ws.on('CB:xmlstreamend', () =>
end(new Boom('Connection Terminated by Server', { statusCode: DisconnectReason.connectionClosed }))
)
// QR gen
ws.on('CB:iq,type:set,pair-device', async(stanza: BinaryNode) => {
ws.on('CB:iq,type:set,pair-device', async (stanza: BinaryNode) => {
const iq: BinaryNode = {
tag: 'iq',
attrs: {
to: S_WHATSAPP_NET,
type: 'result',
id: stanza.attrs.id,
id: stanza.attrs.id
}
}
await sendNode(iq)
@@ -604,12 +578,12 @@ export const makeSocket = (config: SocketConfig) => {
let qrMs = qrTimeout || 60_000 // time to let a QR live
const genPairQR = () => {
if(!ws.isOpen) {
if (!ws.isOpen) {
return
}
const refNode = refNodes.shift()
if(!refNode) {
if (!refNode) {
end(new Boom('QR refs attempts ended', { statusCode: DisconnectReason.timedOut }))
return
}
@@ -627,7 +601,7 @@ export const makeSocket = (config: SocketConfig) => {
})
// device paired for the first time
// if device pairs successfully, the server asks to restart the connection
ws.on('CB:iq,,pair-success', async(stanza: BinaryNode) => {
ws.on('CB:iq,,pair-success', async (stanza: BinaryNode) => {
logger.debug('pair success recv')
try {
const { reply, creds: updatedCreds } = configureSuccessfulPairing(stanza, creds)
@@ -641,13 +615,13 @@ export const makeSocket = (config: SocketConfig) => {
ev.emit('connection.update', { isNewLogin: true, qr: undefined })
await sendNode(reply)
} catch(error) {
} catch (error) {
logger.info({ trace: error.stack }, 'error in pairing')
end(error)
}
})
// login complete
ws.on('CB:success', async(node: BinaryNode) => {
ws.on('CB:success', async (node: BinaryNode) => {
await uploadPreKeysToServerIfRequired()
await sendPassiveIq('active')
@@ -677,7 +651,7 @@ export const makeSocket = (config: SocketConfig) => {
})
ws.on('CB:ib,,offline_preview', (node: BinaryNode) => {
logger.info('offline preview received', JSON.stringify(node))
logger.info('offline preview received', JSON.stringify(node))
sendNode({
tag: 'ib',
attrs: {},
@@ -688,7 +662,7 @@ export const makeSocket = (config: SocketConfig) => {
ws.on('CB:ib,,edge_routing', (node: BinaryNode) => {
const edgeRoutingNode = getBinaryNodeChild(node, 'edge_routing')
const routingInfo = getBinaryNodeChild(edgeRoutingNode, 'routing_info')
if(routingInfo?.content) {
if (routingInfo?.content) {
authState.creds.routingInfo = Buffer.from(routingInfo?.content as Uint8Array)
ev.emit('creds.update', authState.creds)
}
@@ -696,7 +670,7 @@ export const makeSocket = (config: SocketConfig) => {
let didStartBuffer = false
process.nextTick(() => {
if(creds.me?.id) {
if (creds.me?.id) {
// start buffering important events
// if we're logged in
ev.buffer()
@@ -712,7 +686,7 @@ export const makeSocket = (config: SocketConfig) => {
const offlineNotifs = +(child?.attrs.count || 0)
logger.info(`handled ${offlineNotifs} offline messages/notifications`)
if(didStartBuffer) {
if (didStartBuffer) {
ev.flush()
logger.trace('flushed events for initial buffer')
}
@@ -724,21 +698,19 @@ export const makeSocket = (config: SocketConfig) => {
ev.on('creds.update', update => {
const name = update.me?.name
// if name has just been received
if(creds.me?.name !== name) {
if (creds.me?.name !== name) {
logger.debug({ name }, 'updated pushName')
sendNode({
tag: 'presence',
attrs: { name: name! }
}).catch(err => {
logger.warn({ trace: err.stack }, 'error in sending presence update on name change')
})
.catch(err => {
logger.warn({ trace: err.stack }, 'error in sending presence update on name change')
})
}
Object.assign(creds, update)
})
return {
type: 'md' as 'md',
ws,
@@ -762,7 +734,7 @@ export const makeSocket = (config: SocketConfig) => {
requestPairingCode,
/** Waits for the connection to WA to reach a state */
waitForConnectionUpdate: bindWaitForConnectionUpdate(ev),
sendWAMBuffer,
sendWAMBuffer
}
}
@@ -772,11 +744,6 @@ export const makeSocket = (config: SocketConfig) => {
* */
function mapWebSocketError(handler: (err: Error) => void) {
return (error: Error) => {
handler(
new Boom(
`WebSocket Error (${error?.message})`,
{ statusCode: getCodeFromWSError(error), data: error }
)
)
handler(new Boom(`WebSocket Error (${error?.message})`, { statusCode: getCodeFromWSError(error), data: error }))
}
}

View File

@@ -7,13 +7,10 @@ import { makeSocket } from './socket'
export const makeUSyncSocket = (config: SocketConfig) => {
const sock = makeSocket(config)
const {
generateMessageTag,
query,
} = sock
const { generateMessageTag, query } = sock
const executeUSyncQuery = async(usyncQuery: USyncQuery) => {
if(usyncQuery.protocols.length === 0) {
const executeUSyncQuery = async (usyncQuery: USyncQuery) => {
if (usyncQuery.protocols.length === 0) {
throw new Boom('USyncQuery must have at least one protocol')
}
@@ -21,15 +18,13 @@ export const makeUSyncSocket = (config: SocketConfig) => {
// variable below has only validated users
const validUsers = usyncQuery.users
const userNodes = validUsers.map((user) => {
const userNodes = validUsers.map(user => {
return {
tag: 'user',
attrs: {
jid: !user.phone ? user.id : undefined,
jid: !user.phone ? user.id : undefined
},
content: usyncQuery.protocols
.map((a) => a.getUserElement(user))
.filter(a => a !== null)
content: usyncQuery.protocols.map(a => a.getUserElement(user)).filter(a => a !== null)
} as BinaryNode
})
@@ -42,14 +37,14 @@ export const makeUSyncSocket = (config: SocketConfig) => {
const queryNode: BinaryNode = {
tag: 'query',
attrs: {},
content: usyncQuery.protocols.map((a) => a.getQueryElement())
content: usyncQuery.protocols.map(a => a.getQueryElement())
}
const iq = {
tag: 'iq',
attrs: {
to: S_WHATSAPP_NET,
type: 'get',
xmlns: 'usync',
xmlns: 'usync'
},
content: [
{
@@ -59,14 +54,11 @@ export const makeUSyncSocket = (config: SocketConfig) => {
mode: usyncQuery.mode,
sid: generateMessageTag(),
last: 'true',
index: '0',
index: '0'
},
content: [
queryNode,
listNode
]
content: [queryNode, listNode]
}
],
]
}
const result = await query(iq)
@@ -76,6 +68,6 @@ export const makeUSyncSocket = (config: SocketConfig) => {
return {
...sock,
executeUSyncQuery,
executeUSyncQuery
}
}
}

View File

@@ -4,7 +4,6 @@ import { processSyncAction } from '../Utils/chat-utils'
import logger from '../Utils/logger'
describe('App State Sync Tests', () => {
const me: Contact = { id: randomJid() }
// case when initial sync is off
it('should return archive=false event', () => {
@@ -57,7 +56,7 @@ describe('App State Sync Tests', () => {
]
]
for(const mutations of CASES) {
for (const mutations of CASES) {
const events = processSyncAction(mutations, me, undefined, logger)
expect(events['chats.update']).toHaveLength(1)
const event = events['chats.update']?.[0]
@@ -129,7 +128,7 @@ describe('App State Sync Tests', () => {
}
}
}
],
]
]
const ctx: InitialAppStateSyncOptions = {
@@ -139,7 +138,7 @@ describe('App State Sync Tests', () => {
accountSettings: { unarchiveChats: true }
}
for(const mutations of CASES) {
for (const mutations of CASES) {
const events = processSyncActions(mutations, me, ctx, logger)
expect(events['chats.update']?.length).toBeFalsy()
}
@@ -152,7 +151,7 @@ describe('App State Sync Tests', () => {
const index = ['archive', jid]
const now = unixTimestampSeconds()
const CASES: { settings: AccountSettings, mutations: ChatMutation[] }[] = [
const CASES: { settings: AccountSettings; mutations: ChatMutation[] }[] = [
{
settings: { unarchiveChats: true },
mutations: [
@@ -169,7 +168,7 @@ describe('App State Sync Tests', () => {
}
}
}
],
]
},
{
settings: { unarchiveChats: false },
@@ -187,11 +186,11 @@ describe('App State Sync Tests', () => {
}
}
}
],
]
}
]
for(const { mutations, settings } of CASES) {
for (const { mutations, settings } of CASES) {
const ctx: InitialAppStateSyncOptions = {
recvChats: {
[jid]: { lastMsgRecvTimestamp: now }
@@ -204,4 +203,4 @@ describe('App State Sync Tests', () => {
expect(event.archive).toEqual(true)
}
})
})
})

View File

@@ -5,15 +5,14 @@ import logger from '../Utils/logger'
import { randomJid } from './utils'
describe('Event Buffer Tests', () => {
let ev: ReturnType<typeof makeEventBuffer>
beforeEach(() => {
const _logger = logger.child({ })
const _logger = logger.child({})
_logger.level = 'trace'
ev = makeEventBuffer(_logger)
})
it('should buffer a chat upsert & update event', async() => {
it('should buffer a chat upsert & update event', async () => {
const chatId = randomJid()
const chats: Chat[] = []
@@ -23,14 +22,14 @@ describe('Event Buffer Tests', () => {
ev.buffer()
await Promise.all([
(async() => {
(async () => {
ev.buffer()
await delay(100)
ev.emit('chats.upsert', [{ id: chatId, conversationTimestamp: 123, unreadCount: 1 }])
const flushed = ev.flush()
expect(flushed).toBeFalsy()
})(),
(async() => {
(async () => {
ev.buffer()
await delay(200)
ev.emit('chats.update', [{ id: chatId, conversationTimestamp: 124, unreadCount: 1 }])
@@ -47,7 +46,7 @@ describe('Event Buffer Tests', () => {
expect(chats[0].unreadCount).toEqual(2)
})
it('should overwrite a chats.delete event', async() => {
it('should overwrite a chats.delete event', async () => {
const chatId = randomJid()
const chats: Partial<Chat>[] = []
@@ -65,7 +64,7 @@ describe('Event Buffer Tests', () => {
expect(chats).toHaveLength(1)
})
it('should overwrite a chats.update event', async() => {
it('should overwrite a chats.update event', async () => {
const chatId = randomJid()
const chatsDeleted: string[] = []
@@ -82,7 +81,7 @@ describe('Event Buffer Tests', () => {
expect(chatsDeleted).toHaveLength(1)
})
it('should release a conditional update at the right time', async() => {
it('should release a conditional update at the right time', async () => {
const chatId = randomJid()
const chatId2 = randomJid()
const chatsUpserted: Chat[] = []
@@ -93,41 +92,49 @@ describe('Event Buffer Tests', () => {
ev.on('chats.update', () => fail('not should have emitted'))
ev.buffer()
ev.emit('chats.update', [{
id: chatId,
archived: true,
conditional(buff) {
if(buff.chatUpserts[chatId]) {
return true
ev.emit('chats.update', [
{
id: chatId,
archived: true,
conditional(buff) {
if (buff.chatUpserts[chatId]) {
return true
}
}
}
}])
ev.emit('chats.update', [{
id: chatId2,
archived: true,
conditional(buff) {
if(buff.historySets.chats[chatId2]) {
return true
])
ev.emit('chats.update', [
{
id: chatId2,
archived: true,
conditional(buff) {
if (buff.historySets.chats[chatId2]) {
return true
}
}
}
}])
])
ev.flush()
ev.buffer()
ev.emit('chats.upsert', [{
id: chatId,
conversationTimestamp: 123,
unreadCount: 1,
muteEndTime: 123
}])
ev.emit('messaging-history.set', {
chats: [{
id: chatId2,
ev.emit('chats.upsert', [
{
id: chatId,
conversationTimestamp: 123,
unreadCount: 1,
muteEndTime: 123
}],
}
])
ev.emit('messaging-history.set', {
chats: [
{
id: chatId2,
conversationTimestamp: 123,
unreadCount: 1,
muteEndTime: 123
}
],
contacts: [],
messages: [],
isLatest: false
@@ -144,7 +151,7 @@ describe('Event Buffer Tests', () => {
expect(chatsSynced[0].archived).toEqual(true)
})
it('should discard a conditional update', async() => {
it('should discard a conditional update', async () => {
const chatId = randomJid()
const chatsUpserted: Chat[] = []
@@ -152,21 +159,25 @@ describe('Event Buffer Tests', () => {
ev.on('chats.update', () => fail('not should have emitted'))
ev.buffer()
ev.emit('chats.update', [{
id: chatId,
archived: true,
conditional(buff) {
if(buff.chatUpserts[chatId]) {
return false
ev.emit('chats.update', [
{
id: chatId,
archived: true,
conditional(buff) {
if (buff.chatUpserts[chatId]) {
return false
}
}
}
}])
ev.emit('chats.upsert', [{
id: chatId,
conversationTimestamp: 123,
unreadCount: 1,
muteEndTime: 123
}])
])
ev.emit('chats.upsert', [
{
id: chatId,
conversationTimestamp: 123,
unreadCount: 1,
muteEndTime: 123
}
])
ev.flush()
@@ -174,7 +185,7 @@ describe('Event Buffer Tests', () => {
expect(chatsUpserted[0].archived).toBeUndefined()
})
it('should overwrite a chats.update event with a history event', async() => {
it('should overwrite a chats.update event with a history event', async () => {
const chatId = randomJid()
let chatRecv: Chat | undefined
@@ -199,7 +210,7 @@ describe('Event Buffer Tests', () => {
expect(chatRecv?.archived).toBeTruthy()
})
it('should buffer message upsert events', async() => {
it('should buffer message upsert events', async () => {
const messageTimestamp = unixTimestampSeconds()
const msg: proto.IWebMessageInfo = {
key: {
@@ -235,7 +246,7 @@ describe('Event Buffer Tests', () => {
expect(msgs[0].status).toEqual(WAMessageStatus.READ)
})
it('should buffer a message receipt update', async() => {
it('should buffer a message receipt update', async () => {
const msg: proto.IWebMessageInfo = {
key: {
remoteJid: randomJid(),
@@ -269,7 +280,7 @@ describe('Event Buffer Tests', () => {
expect(msgs[0].userReceipt).toHaveLength(1)
})
it('should buffer multiple status updates', async() => {
it('should buffer multiple status updates', async () => {
const key: WAMessageKey = {
remoteJid: randomJid(),
id: generateMessageID(),
@@ -290,7 +301,7 @@ describe('Event Buffer Tests', () => {
expect(msgs[0].update.status).toEqual(WAMessageStatus.READ)
})
it('should remove chat unread counter', async() => {
it('should remove chat unread counter', async () => {
const msg: proto.IWebMessageInfo = {
key: {
remoteJid: '12345@s.whatsapp.net',
@@ -316,4 +327,4 @@ describe('Event Buffer Tests', () => {
expect(chats[0].unreadCount).toBeUndefined()
})
})
})

View File

@@ -5,55 +5,44 @@ import { makeMockSignalKeyStore } from './utils'
logger.level = 'trace'
describe('Key Store w Transaction Tests', () => {
const rawStore = makeMockSignalKeyStore()
const store = addTransactionCapability(
rawStore,
logger,
{
maxCommitRetries: 1,
delayBetweenTriesMs: 10
}
)
const store = addTransactionCapability(rawStore, logger, {
maxCommitRetries: 1,
delayBetweenTriesMs: 10
})
it('should use transaction cache when mutated', async() => {
it('should use transaction cache when mutated', async () => {
const key = '123'
const value = new Uint8Array(1)
const ogGet = rawStore.get
await store.transaction(
async() => {
await store.set({ 'session': { [key]: value } })
await store.transaction(async () => {
await store.set({ session: { [key]: value } })
rawStore.get = () => {
throw new Error('should not have been called')
}
const { [key]: stored } = await store.get('session', [key])
expect(stored).toEqual(new Uint8Array(1))
rawStore.get = () => {
throw new Error('should not have been called')
}
)
const { [key]: stored } = await store.get('session', [key])
expect(stored).toEqual(new Uint8Array(1))
})
rawStore.get = ogGet
})
it('should not commit a failed transaction', async() => {
it('should not commit a failed transaction', async () => {
const key = 'abcd'
await expect(
store.transaction(
async() => {
await store.set({ 'session': { [key]: new Uint8Array(1) } })
throw new Error('fail')
}
)
).rejects.toThrowError(
'fail'
)
store.transaction(async () => {
await store.set({ session: { [key]: new Uint8Array(1) } })
throw new Error('fail')
})
).rejects.toThrowError('fail')
const { [key]: stored } = await store.get('session', [key])
expect(stored).toBeUndefined()
})
it('should handle overlapping transactions', async() => {
it('should handle overlapping transactions', async () => {
// promise to let transaction 2
// know that transaction 1 has started
let promiseResolve: () => void
@@ -61,32 +50,28 @@ describe('Key Store w Transaction Tests', () => {
promiseResolve = resolve
})
store.transaction(
async() => {
await store.set({
'session': {
'1': new Uint8Array(1)
}
})
// wait for the other transaction to start
await delay(5)
// reolve the promise to let the other transaction continue
promiseResolve()
}
)
store.transaction(async () => {
await store.set({
session: {
'1': new Uint8Array(1)
}
})
// wait for the other transaction to start
await delay(5)
// reolve the promise to let the other transaction continue
promiseResolve()
})
await store.transaction(
async() => {
await promise
await delay(5)
await store.transaction(async () => {
await promise
await delay(5)
expect(store.isInTransaction()).toBe(true)
}
)
expect(store.isInTransaction()).toBe(true)
})
expect(store.isInTransaction()).toBe(false)
// ensure that the transaction were committed
const { ['1']: stored } = await store.get('session', ['1'])
expect(stored).toEqual(new Uint8Array(1))
})
})
})

View File

@@ -3,8 +3,7 @@ import { SignalAuthState, SignalDataTypeMap } from '../Types'
import { Curve, generateRegistrationId, generateSignalPubKey, signedKeyPair } from '../Utils'
describe('Signal Tests', () => {
it('should correctly encrypt/decrypt 1 message', async() => {
it('should correctly encrypt/decrypt 1 message', async () => {
const user1 = makeUser()
const user2 = makeUser()
@@ -12,39 +11,31 @@ describe('Signal Tests', () => {
await prepareForSendingMessage(user1, user2)
const result = await user1.repository.encryptMessage(
{ jid: user2.jid, data: msg }
)
const result = await user1.repository.encryptMessage({ jid: user2.jid, data: msg })
const dec = await user2.repository.decryptMessage(
{ jid: user1.jid, ...result }
)
const dec = await user2.repository.decryptMessage({ jid: user1.jid, ...result })
expect(dec).toEqual(msg)
})
it('should correctly override a session', async() => {
it('should correctly override a session', async () => {
const user1 = makeUser()
const user2 = makeUser()
const msg = Buffer.from('hello there!')
for(let preKeyId = 2; preKeyId <= 3;preKeyId++) {
for (let preKeyId = 2; preKeyId <= 3; preKeyId++) {
await prepareForSendingMessage(user1, user2, preKeyId)
const result = await user1.repository.encryptMessage(
{ jid: user2.jid, data: msg }
)
const result = await user1.repository.encryptMessage({ jid: user2.jid, data: msg })
const dec = await user2.repository.decryptMessage(
{ jid: user1.jid, ...result }
)
const dec = await user2.repository.decryptMessage({ jid: user1.jid, ...result })
expect(dec).toEqual(msg)
}
})
it('should correctly encrypt/decrypt multiple messages', async() => {
it('should correctly encrypt/decrypt multiple messages', async () => {
const user1 = makeUser()
const user2 = makeUser()
@@ -52,56 +43,46 @@ describe('Signal Tests', () => {
await prepareForSendingMessage(user1, user2)
for(let i = 0;i < 10;i++) {
const result = await user1.repository.encryptMessage(
{ jid: user2.jid, data: msg }
)
for (let i = 0; i < 10; i++) {
const result = await user1.repository.encryptMessage({ jid: user2.jid, data: msg })
const dec = await user2.repository.decryptMessage(
{ jid: user1.jid, ...result }
)
const dec = await user2.repository.decryptMessage({ jid: user1.jid, ...result })
expect(dec).toEqual(msg)
}
})
it('should encrypt/decrypt messages from group', async() => {
it('should encrypt/decrypt messages from group', async () => {
const groupId = '123456@g.us'
const participants = [...Array(5)].map(makeUser)
const msg = Buffer.from('hello there!')
const sender = participants[0]
const enc = await sender.repository.encryptGroupMessage(
{
group: groupId,
meId: sender.jid,
data: msg
}
)
const enc = await sender.repository.encryptGroupMessage({
group: groupId,
meId: sender.jid,
data: msg
})
for(const participant of participants) {
if(participant === sender) {
for (const participant of participants) {
if (participant === sender) {
continue
}
await participant.repository.processSenderKeyDistributionMessage(
{
item: {
groupId,
axolotlSenderKeyDistributionMessage: enc.senderKeyDistributionMessage
},
authorJid: sender.jid
}
)
await participant.repository.processSenderKeyDistributionMessage({
item: {
groupId,
axolotlSenderKeyDistributionMessage: enc.senderKeyDistributionMessage
},
authorJid: sender.jid
})
const dec = await participant.repository.decryptGroupMessage(
{
group: groupId,
authorJid: sender.jid,
msg: enc.ciphertext
}
)
const dec = await participant.repository.decryptGroupMessage({
group: groupId,
authorJid: sender.jid,
msg: enc.ciphertext
})
expect(dec).toEqual(msg)
}
})
@@ -116,30 +97,24 @@ function makeUser() {
return { store, jid, repository }
}
async function prepareForSendingMessage(
sender: User,
receiver: User,
preKeyId = 2
) {
async function prepareForSendingMessage(sender: User, receiver: User, preKeyId = 2) {
const preKey = Curve.generateKeyPair()
await sender.repository.injectE2ESession(
{
jid: receiver.jid,
session: {
registrationId: receiver.store.creds.registrationId,
identityKey: generateSignalPubKey(receiver.store.creds.signedIdentityKey.public),
signedPreKey: {
keyId: receiver.store.creds.signedPreKey.keyId,
publicKey: generateSignalPubKey(receiver.store.creds.signedPreKey.keyPair.public),
signature: receiver.store.creds.signedPreKey.signature,
},
preKey: {
keyId: preKeyId,
publicKey: generateSignalPubKey(preKey.public),
}
await sender.repository.injectE2ESession({
jid: receiver.jid,
session: {
registrationId: receiver.store.creds.registrationId,
identityKey: generateSignalPubKey(receiver.store.creds.signedIdentityKey.public),
signedPreKey: {
keyId: receiver.store.creds.signedPreKey.keyId,
publicKey: generateSignalPubKey(receiver.store.creds.signedPreKey.keyPair.public),
signature: receiver.store.creds.signedPreKey.signature
},
preKey: {
keyId: preKeyId,
publicKey: generateSignalPubKey(preKey.public)
}
}
)
})
await receiver.store.keys.set({
'pre-key': {
@@ -156,14 +131,14 @@ function makeTestAuthState(): SignalAuthState {
creds: {
signedIdentityKey: identityKey,
registrationId: generateRegistrationId(),
signedPreKey: signedKeyPair(identityKey, 1),
signedPreKey: signedKeyPair(identityKey, 1)
},
keys: {
get(type, ids) {
const data: { [_: string]: SignalDataTypeMap[typeof type] } = { }
for(const id of ids) {
const data: { [_: string]: SignalDataTypeMap[typeof type] } = {}
for (const id of ids) {
const item = store[getUniqueId(type, id)]
if(typeof item !== 'undefined') {
if (typeof item !== 'undefined') {
data[id] = item
}
}
@@ -171,16 +146,16 @@ function makeTestAuthState(): SignalAuthState {
return data
},
set(data) {
for(const type in data) {
for(const id in data[type]) {
for (const type in data) {
for (const id in data[type]) {
store[getUniqueId(type, id)] = data[type][id]
}
}
},
}
}
}
function getUniqueId(type: string, id: string) {
return `${type}.${id}`
}
}
}

View File

@@ -31,38 +31,37 @@ const TEST_VECTORS: TestVector[] = [
)
),
plaintext: readFileSync('./Media/icon.png')
},
}
]
describe('Media Download Tests', () => {
it('should download a full encrypted media correctly', async() => {
for(const { type, message, plaintext } of TEST_VECTORS) {
it('should download a full encrypted media correctly', async () => {
for (const { type, message, plaintext } of TEST_VECTORS) {
const readPipe = await downloadContentFromMessage(message, type)
let buffer = Buffer.alloc(0)
for await (const read of readPipe) {
buffer = Buffer.concat([ buffer, read ])
buffer = Buffer.concat([buffer, read])
}
expect(buffer).toEqual(plaintext)
}
})
it('should download an encrypted media correctly piece', async() => {
for(const { type, message, plaintext } of TEST_VECTORS) {
it('should download an encrypted media correctly piece', async () => {
for (const { type, message, plaintext } of TEST_VECTORS) {
// check all edge cases
const ranges = [
{ startByte: 51, endByte: plaintext.length - 100 }, // random numbers
{ startByte: 1024, endByte: 2038 }, // larger random multiples of 16
{ startByte: 1, endByte: plaintext.length - 1 } // borders
]
for(const range of ranges) {
for (const range of ranges) {
const readPipe = await downloadContentFromMessage(message, type, range)
let buffer = Buffer.alloc(0)
for await (const read of readPipe) {
buffer = Buffer.concat([ buffer, read ])
buffer = Buffer.concat([buffer, read])
}
const hex = buffer.toString('hex')
@@ -73,4 +72,4 @@ describe('Media Download Tests', () => {
}
}
})
})
})

View File

@@ -2,9 +2,8 @@ import { WAMessageContent } from '../Types'
import { normalizeMessageContent } from '../Utils'
describe('Messages Tests', () => {
it('should correctly unwrap messages', () => {
const CONTENT = { imageMessage: { } }
const CONTENT = { imageMessage: {} }
expectRightContent(CONTENT)
expectRightContent({
ephemeralMessage: { message: CONTENT }
@@ -29,9 +28,7 @@ describe('Messages Tests', () => {
})
function expectRightContent(content: WAMessageContent) {
expect(
normalizeMessageContent(content)
).toHaveProperty('imageMessage')
expect(normalizeMessageContent(content)).toHaveProperty('imageMessage')
}
})
})
})

View File

@@ -11,10 +11,10 @@ export function makeMockSignalKeyStore(): SignalKeyStore {
return {
get(type, ids) {
const data: { [_: string]: SignalDataTypeMap[typeof type] } = { }
for(const id of ids) {
const data: { [_: string]: SignalDataTypeMap[typeof type] } = {}
for (const id of ids) {
const item = store[getUniqueId(type, id)]
if(typeof item !== 'undefined') {
if (typeof item !== 'undefined') {
data[id] = item
}
}
@@ -22,15 +22,15 @@ export function makeMockSignalKeyStore(): SignalKeyStore {
return data
},
set(data) {
for(const type in data) {
for(const id in data[type]) {
for (const type in data) {
for (const id in data[type]) {
store[getUniqueId(type, id)] = data[type][id]
}
}
},
}
}
function getUniqueId(type: string, id: string) {
return `${type}.${id}`
}
}
}

View File

@@ -2,12 +2,12 @@ import type { proto } from '../../WAProto'
import type { Contact } from './Contact'
import type { MinimalMessage } from './Message'
export type KeyPair = { public: Uint8Array, private: Uint8Array }
export type KeyPair = { public: Uint8Array; private: Uint8Array }
export type SignedKeyPair = {
keyPair: KeyPair
signature: Uint8Array
keyId: number
timestampS?: number
keyPair: KeyPair
signature: Uint8Array
keyId: number
timestampS?: number
}
export type ProtocolAddress = {
@@ -20,58 +20,58 @@ export type SignalIdentity = {
}
export type LTHashState = {
version: number
hash: Buffer
indexValueMap: {
[indexMacBase64: string]: { valueMac: Uint8Array | Buffer }
}
version: number
hash: Buffer
indexValueMap: {
[indexMacBase64: string]: { valueMac: Uint8Array | Buffer }
}
}
export type SignalCreds = {
readonly signedIdentityKey: KeyPair
readonly signedPreKey: SignedKeyPair
readonly registrationId: number
readonly signedIdentityKey: KeyPair
readonly signedPreKey: SignedKeyPair
readonly registrationId: number
}
export type AccountSettings = {
/** unarchive chats when a new message is received */
unarchiveChats: boolean
/** the default mode to start new conversations with */
defaultDisappearingMode?: Pick<proto.IConversation, 'ephemeralExpiration' | 'ephemeralSettingTimestamp'>
/** unarchive chats when a new message is received */
unarchiveChats: boolean
/** the default mode to start new conversations with */
defaultDisappearingMode?: Pick<proto.IConversation, 'ephemeralExpiration' | 'ephemeralSettingTimestamp'>
}
export type AuthenticationCreds = SignalCreds & {
readonly noiseKey: KeyPair
readonly pairingEphemeralKeyPair: KeyPair
advSecretKey: string
readonly noiseKey: KeyPair
readonly pairingEphemeralKeyPair: KeyPair
advSecretKey: string
me?: Contact
account?: proto.IADVSignedDeviceIdentity
signalIdentities?: SignalIdentity[]
myAppStateKeyId?: string
firstUnuploadedPreKeyId: number
nextPreKeyId: number
me?: Contact
account?: proto.IADVSignedDeviceIdentity
signalIdentities?: SignalIdentity[]
myAppStateKeyId?: string
firstUnuploadedPreKeyId: number
nextPreKeyId: number
lastAccountSyncTimestamp?: number
platform?: string
lastAccountSyncTimestamp?: number
platform?: string
processedHistoryMessages: MinimalMessage[]
/** number of times history & app state has been synced */
accountSyncCounter: number
accountSettings: AccountSettings
registered: boolean
pairingCode: string | undefined
lastPropHash: string | undefined
routingInfo: Buffer | undefined
processedHistoryMessages: MinimalMessage[]
/** number of times history & app state has been synced */
accountSyncCounter: number
accountSettings: AccountSettings
registered: boolean
pairingCode: string | undefined
lastPropHash: string | undefined
routingInfo: Buffer | undefined
}
export type SignalDataTypeMap = {
'pre-key': KeyPair
'session': Uint8Array
'sender-key': Uint8Array
'sender-key-memory': { [jid: string]: boolean }
'app-state-sync-key': proto.Message.IAppStateSyncKeyData
'app-state-sync-version': LTHashState
'pre-key': KeyPair
session: Uint8Array
'sender-key': Uint8Array
'sender-key-memory': { [jid: string]: boolean }
'app-state-sync-key': proto.Message.IAppStateSyncKeyData
'app-state-sync-version': LTHashState
}
export type SignalDataSet = { [T in keyof SignalDataTypeMap]?: { [id: string]: SignalDataTypeMap[T] | null } }
@@ -79,15 +79,15 @@ export type SignalDataSet = { [T in keyof SignalDataTypeMap]?: { [id: string]: S
type Awaitable<T> = T | Promise<T>
export type SignalKeyStore = {
get<T extends keyof SignalDataTypeMap>(type: T, ids: string[]): Awaitable<{ [id: string]: SignalDataTypeMap[T] }>
set(data: SignalDataSet): Awaitable<void>
/** clear all the data in the store */
clear?(): Awaitable<void>
get<T extends keyof SignalDataTypeMap>(type: T, ids: string[]): Awaitable<{ [id: string]: SignalDataTypeMap[T] }>
set(data: SignalDataSet): Awaitable<void>
/** clear all the data in the store */
clear?(): Awaitable<void>
}
export type SignalKeyStoreWithTransaction = SignalKeyStore & {
isInTransaction: () => boolean
transaction<T>(exec: () => Promise<T>): Promise<T>
isInTransaction: () => boolean
transaction<T>(exec: () => Promise<T>): Promise<T>
}
export type TransactionCapabilityOptions = {
@@ -96,11 +96,11 @@ export type TransactionCapabilityOptions = {
}
export type SignalAuthState = {
creds: SignalCreds
keys: SignalKeyStore | SignalKeyStoreWithTransaction
creds: SignalCreds
keys: SignalKeyStore | SignalKeyStoreWithTransaction
}
export type AuthenticationState = {
creds: AuthenticationCreds
keys: SignalKeyStore
}
creds: AuthenticationCreds
keys: SignalKeyStore
}

View File

@@ -1,4 +1,3 @@
export type WACallUpdateType = 'offer' | 'ringing' | 'timeout' | 'reject' | 'accept' | 'terminate'
export type WACallEvent = {

View File

@@ -22,50 +22,58 @@ export type WAPrivacyMessagesValue = 'all' | 'contacts'
/** set of statuses visible to other people; see updatePresence() in WhatsAppWeb.Send */
export type WAPresence = 'unavailable' | 'available' | 'composing' | 'recording' | 'paused'
export const ALL_WA_PATCH_NAMES = ['critical_block', 'critical_unblock_low', 'regular_high', 'regular_low', 'regular'] as const
export const ALL_WA_PATCH_NAMES = [
'critical_block',
'critical_unblock_low',
'regular_high',
'regular_low',
'regular'
] as const
export type WAPatchName = typeof ALL_WA_PATCH_NAMES[number]
export type WAPatchName = (typeof ALL_WA_PATCH_NAMES)[number]
export interface PresenceData {
lastKnownPresence: WAPresence
lastSeen?: number
lastKnownPresence: WAPresence
lastSeen?: number
}
export type BotListInfo = {
jid: string
personaId: string
jid: string
personaId: string
}
export type ChatMutation = {
syncAction: proto.ISyncActionData
index: string[]
syncAction: proto.ISyncActionData
index: string[]
}
export type WAPatchCreate = {
syncAction: proto.ISyncActionValue
index: string[]
type: WAPatchName
apiVersion: number
operation: proto.SyncdMutation.SyncdOperation
syncAction: proto.ISyncActionValue
index: string[]
type: WAPatchName
apiVersion: number
operation: proto.SyncdMutation.SyncdOperation
}
export type Chat = proto.IConversation & {
/** unix timestamp of when the last message was received in the chat */
lastMessageRecvTimestamp?: number
/** unix timestamp of when the last message was received in the chat */
lastMessageRecvTimestamp?: number
}
export type ChatUpdate = Partial<Chat & {
/**
* if specified in the update,
* the EV buffer will check if the condition gets fulfilled before applying the update
* Right now, used to determine when to release an app state sync event
*
* @returns true, if the update should be applied;
* false if it can be discarded;
* undefined if the condition is not yet fulfilled
* */
conditional: (bufferedData: BufferedEventData) => boolean | undefined
}>
export type ChatUpdate = Partial<
Chat & {
/**
* if specified in the update,
* the EV buffer will check if the condition gets fulfilled before applying the update
* Right now, used to determine when to release an app state sync event
*
* @returns true, if the update should be applied;
* false if it can be discarded;
* undefined if the condition is not yet fulfilled
* */
conditional: (bufferedData: BufferedEventData) => boolean | undefined
}
>
/**
* the last messages in a chat, sorted reverse-chronologically. That is, the latest message should be first in the chat
@@ -74,49 +82,50 @@ export type ChatUpdate = Partial<Chat & {
export type LastMessageList = MinimalMessage[] | proto.SyncActionValue.ISyncActionMessageRange
export type ChatModification =
{
archive: boolean
lastMessages: LastMessageList
}
| { pushNameSetting: string }
| { pin: boolean }
| {
/** mute for duration, or provide timestamp of mute to remove*/
mute: number | null
}
| {
clear: boolean
} | {
deleteForMe: { deleteMedia: boolean, key: WAMessageKey, timestamp: number }
}
| {
star: {
messages: { id: string, fromMe?: boolean }[]
star: boolean
}
}
| {
markRead: boolean
lastMessages: LastMessageList
}
| { delete: true, lastMessages: LastMessageList }
// Label
| { addLabel: LabelActionBody }
// Label assosiation
| { addChatLabel: ChatLabelAssociationActionBody }
| { removeChatLabel: ChatLabelAssociationActionBody }
| { addMessageLabel: MessageLabelAssociationActionBody }
| { removeMessageLabel: MessageLabelAssociationActionBody }
| {
archive: boolean
lastMessages: LastMessageList
}
| { pushNameSetting: string }
| { pin: boolean }
| {
/** mute for duration, or provide timestamp of mute to remove*/
mute: number | null
}
| {
clear: boolean
}
| {
deleteForMe: { deleteMedia: boolean; key: WAMessageKey; timestamp: number }
}
| {
star: {
messages: { id: string; fromMe?: boolean }[]
star: boolean
}
}
| {
markRead: boolean
lastMessages: LastMessageList
}
| { delete: true; lastMessages: LastMessageList }
// Label
| { addLabel: LabelActionBody }
// Label assosiation
| { addChatLabel: ChatLabelAssociationActionBody }
| { removeChatLabel: ChatLabelAssociationActionBody }
| { addMessageLabel: MessageLabelAssociationActionBody }
| { removeMessageLabel: MessageLabelAssociationActionBody }
export type InitialReceivedChatsState = {
[jid: string]: {
/** the last message received from the other party */
lastMsgRecvTimestamp?: number
/** the absolute last message in the chat */
lastMsgTimestamp: number
}
[jid: string]: {
/** the last message received from the other party */
lastMsgRecvTimestamp?: number
/** the absolute last message in the chat */
lastMsgTimestamp: number
}
}
export type InitialAppStateSyncOptions = {
accountSettings: AccountSettings
accountSettings: AccountSettings
}

View File

@@ -1,20 +1,20 @@
export interface Contact {
id: string
lid?: string
/** name of the contact, you have saved on your WA */
name?: string
/** name of the contact, the contact has set on their own on WA */
notify?: string
/** I have no idea */
verifiedName?: string
// Baileys Added
/**
* Url of the profile picture of the contact
*
* 'changed' => if the profile picture has changed
* null => if the profile picture has not been set (default profile picture)
* any other string => url of the profile picture
*/
imgUrl?: string | null
status?: string
}
id: string
lid?: string
/** name of the contact, you have saved on your WA */
name?: string
/** name of the contact, the contact has set on their own on WA */
notify?: string
/** I have no idea */
verifiedName?: string
// Baileys Added
/**
* Url of the profile picture of the contact
*
* 'changed' => if the profile picture has changed
* null => if the profile picture has not been set (default profile picture)
* any other string => url of the profile picture
*/
imgUrl?: string | null
status?: string
}

View File

@@ -11,91 +11,97 @@ import { MessageUpsertType, MessageUserReceiptUpdate, WAMessage, WAMessageKey, W
import { ConnectionState } from './State'
export type BaileysEventMap = {
/** connection state has been updated -- WS closed, opened, connecting etc. */
/** connection state has been updated -- WS closed, opened, connecting etc. */
'connection.update': Partial<ConnectionState>
/** credentials updated -- some metadata, keys or something */
'creds.update': Partial<AuthenticationCreds>
/** set chats (history sync), everything is reverse chronologically sorted */
'messaging-history.set': {
chats: Chat[]
contacts: Contact[]
messages: WAMessage[]
isLatest?: boolean
progress?: number | null
syncType?: proto.HistorySync.HistorySyncType
peerDataRequestSessionId?: string | null
}
/** upsert chats */
'chats.upsert': Chat[]
/** update the given chats */
'chats.update': ChatUpdate[]
'chats.phoneNumberShare': {lid: string, jid: string}
/** delete chats with given ID */
'chats.delete': string[]
/** presence of contact in a chat updated */
'presence.update': { id: string, presences: { [participant: string]: PresenceData } }
/** credentials updated -- some metadata, keys or something */
'creds.update': Partial<AuthenticationCreds>
/** set chats (history sync), everything is reverse chronologically sorted */
'messaging-history.set': {
chats: Chat[]
contacts: Contact[]
messages: WAMessage[]
isLatest?: boolean
progress?: number | null
syncType?: proto.HistorySync.HistorySyncType
peerDataRequestSessionId?: string | null
}
/** upsert chats */
'chats.upsert': Chat[]
/** update the given chats */
'chats.update': ChatUpdate[]
'chats.phoneNumberShare': { lid: string; jid: string }
/** delete chats with given ID */
'chats.delete': string[]
/** presence of contact in a chat updated */
'presence.update': { id: string; presences: { [participant: string]: PresenceData } }
'contacts.upsert': Contact[]
'contacts.update': Partial<Contact>[]
'contacts.upsert': Contact[]
'contacts.update': Partial<Contact>[]
'messages.delete': { keys: WAMessageKey[] } | { jid: string, all: true }
'messages.update': WAMessageUpdate[]
'messages.media-update': { key: WAMessageKey, media?: { ciphertext: Uint8Array, iv: Uint8Array }, error?: Boom }[]
/**
* add/update the given messages. If they were received while the connection was online,
* the update will have type: "notify"
* if requestId is provided, then the messages was received from the phone due to it being unavailable
* */
'messages.upsert': { messages: WAMessage[], type: MessageUpsertType, requestId?: string }
/** message was reacted to. If reaction was removed -- then "reaction.text" will be falsey */
'messages.reaction': { key: WAMessageKey, reaction: proto.IReaction }[]
'messages.delete': { keys: WAMessageKey[] } | { jid: string; all: true }
'messages.update': WAMessageUpdate[]
'messages.media-update': { key: WAMessageKey; media?: { ciphertext: Uint8Array; iv: Uint8Array }; error?: Boom }[]
/**
* add/update the given messages. If they were received while the connection was online,
* the update will have type: "notify"
* if requestId is provided, then the messages was received from the phone due to it being unavailable
* */
'messages.upsert': { messages: WAMessage[]; type: MessageUpsertType; requestId?: string }
/** message was reacted to. If reaction was removed -- then "reaction.text" will be falsey */
'messages.reaction': { key: WAMessageKey; reaction: proto.IReaction }[]
'message-receipt.update': MessageUserReceiptUpdate[]
'message-receipt.update': MessageUserReceiptUpdate[]
'groups.upsert': GroupMetadata[]
'groups.update': Partial<GroupMetadata>[]
/** apply an action to participants in a group */
'group-participants.update': { id: string, author: string, participants: string[], action: ParticipantAction }
'group.join-request': { id: string, author: string, participant: string, action: RequestJoinAction, method: RequestJoinMethod }
'groups.upsert': GroupMetadata[]
'groups.update': Partial<GroupMetadata>[]
/** apply an action to participants in a group */
'group-participants.update': { id: string; author: string; participants: string[]; action: ParticipantAction }
'group.join-request': {
id: string
author: string
participant: string
action: RequestJoinAction
method: RequestJoinMethod
}
'blocklist.set': { blocklist: string[] }
'blocklist.update': { blocklist: string[], type: 'add' | 'remove' }
'blocklist.set': { blocklist: string[] }
'blocklist.update': { blocklist: string[]; type: 'add' | 'remove' }
/** Receive an update on a call, including when the call was received, rejected, accepted */
'call': WACallEvent[]
'labels.edit': Label
'labels.association': { association: LabelAssociation, type: 'add' | 'remove' }
/** Receive an update on a call, including when the call was received, rejected, accepted */
call: WACallEvent[]
'labels.edit': Label
'labels.association': { association: LabelAssociation; type: 'add' | 'remove' }
}
export type BufferedEventData = {
historySets: {
chats: { [jid: string]: Chat }
contacts: { [jid: string]: Contact }
messages: { [uqId: string]: WAMessage }
empty: boolean
isLatest: boolean
progress?: number | null
syncType?: proto.HistorySync.HistorySyncType
peerDataRequestSessionId?: string
}
chatUpserts: { [jid: string]: Chat }
chatUpdates: { [jid: string]: ChatUpdate }
chatDeletes: Set<string>
contactUpserts: { [jid: string]: Contact }
contactUpdates: { [jid: string]: Partial<Contact> }
messageUpserts: { [key: string]: { type: MessageUpsertType, message: WAMessage } }
messageUpdates: { [key: string]: WAMessageUpdate }
messageDeletes: { [key: string]: WAMessageKey }
messageReactions: { [key: string]: { key: WAMessageKey, reactions: proto.IReaction[] } }
messageReceipts: { [key: string]: { key: WAMessageKey, userReceipt: proto.IUserReceipt[] } }
groupUpdates: { [jid: string]: Partial<GroupMetadata> }
historySets: {
chats: { [jid: string]: Chat }
contacts: { [jid: string]: Contact }
messages: { [uqId: string]: WAMessage }
empty: boolean
isLatest: boolean
progress?: number | null
syncType?: proto.HistorySync.HistorySyncType
peerDataRequestSessionId?: string
}
chatUpserts: { [jid: string]: Chat }
chatUpdates: { [jid: string]: ChatUpdate }
chatDeletes: Set<string>
contactUpserts: { [jid: string]: Contact }
contactUpdates: { [jid: string]: Partial<Contact> }
messageUpserts: { [key: string]: { type: MessageUpsertType; message: WAMessage } }
messageUpdates: { [key: string]: WAMessageUpdate }
messageDeletes: { [key: string]: WAMessageKey }
messageReactions: { [key: string]: { key: WAMessageKey; reactions: proto.IReaction[] } }
messageReceipts: { [key: string]: { key: WAMessageKey; userReceipt: proto.IUserReceipt[] } }
groupUpdates: { [jid: string]: Partial<GroupMetadata> }
}
export type BaileysEvent = keyof BaileysEventMap
export interface BaileysEventEmitter {
on<T extends keyof BaileysEventMap>(event: T, listener: (arg: BaileysEventMap[T]) => void): void
off<T extends keyof BaileysEventMap>(event: T, listener: (arg: BaileysEventMap[T]) => void): void
removeAllListeners<T extends keyof BaileysEventMap>(event: T): void
off<T extends keyof BaileysEventMap>(event: T, listener: (arg: BaileysEventMap[T]) => void): void
removeAllListeners<T extends keyof BaileysEventMap>(event: T): void
emit<T extends keyof BaileysEventMap>(event: T, arg: BaileysEventMap[T]): boolean
}
}

View File

@@ -1,6 +1,10 @@
import { Contact } from './Contact'
export type GroupParticipant = (Contact & { isAdmin?: boolean, isSuperAdmin?: boolean, admin?: 'admin' | 'superadmin' | null })
export type GroupParticipant = Contact & {
isAdmin?: boolean
isSuperAdmin?: boolean
admin?: 'admin' | 'superadmin' | null
}
export type ParticipantAction = 'add' | 'remove' | 'promote' | 'demote' | 'modify'
@@ -9,51 +13,50 @@ export type RequestJoinAction = 'created' | 'revoked' | 'rejected'
export type RequestJoinMethod = 'invite_link' | 'linked_group_join' | 'non_admin_add' | undefined
export interface GroupMetadata {
id: string
/** group uses 'lid' or 'pn' to send messages */
addressingMode: string
owner: string | undefined
subject: string
/** group subject owner */
subjectOwner?: string
/** group subject modification date */
subjectTime?: number
creation?: number
desc?: string
descOwner?: string
descId?: string
/** if this group is part of a community, it returns the jid of the community to which it belongs */
linkedParent?: string
/** is set when the group only allows admins to change group settings */
restrict?: boolean
/** is set when the group only allows admins to write messages */
announce?: boolean
/** is set when the group also allows members to add participants */
memberAddMode?: boolean
/** Request approval to join the group */
joinApprovalMode?: boolean
/** is this a community */
isCommunity?: boolean
/** is this the announce of a community */
isCommunityAnnounce?: boolean
/** number of group participants */
size?: number
// Baileys modified array
participants: GroupParticipant[]
ephemeralDuration?: number
inviteCode?: string
/** the person who added you to group or changed some setting in group */
author?: string
id: string
/** group uses 'lid' or 'pn' to send messages */
addressingMode: string
owner: string | undefined
subject: string
/** group subject owner */
subjectOwner?: string
/** group subject modification date */
subjectTime?: number
creation?: number
desc?: string
descOwner?: string
descId?: string
/** if this group is part of a community, it returns the jid of the community to which it belongs */
linkedParent?: string
/** is set when the group only allows admins to change group settings */
restrict?: boolean
/** is set when the group only allows admins to write messages */
announce?: boolean
/** is set when the group also allows members to add participants */
memberAddMode?: boolean
/** Request approval to join the group */
joinApprovalMode?: boolean
/** is this a community */
isCommunity?: boolean
/** is this the announce of a community */
isCommunityAnnounce?: boolean
/** number of group participants */
size?: number
// Baileys modified array
participants: GroupParticipant[]
ephemeralDuration?: number
inviteCode?: string
/** the person who added you to group or changed some setting in group */
author?: string
}
export interface WAGroupCreateResponse {
status: number
gid?: string
participants?: [{ [key: string]: {} }]
status: number
gid?: string
participants?: [{ [key: string]: {} }]
}
export interface GroupModificationResponse {
status: number
participants?: { [key: string]: {} }
status: number
participants?: { [key: string]: {} }
}

View File

@@ -1,48 +1,48 @@
export interface Label {
/** Label uniq ID */
id: string
/** Label name */
name: string
/** Label color ID */
color: number
/** Is label has been deleted */
deleted: boolean
/** WhatsApp has 5 predefined labels (New customer, New order & etc) */
predefinedId?: string
/** Label uniq ID */
id: string
/** Label name */
name: string
/** Label color ID */
color: number
/** Is label has been deleted */
deleted: boolean
/** WhatsApp has 5 predefined labels (New customer, New order & etc) */
predefinedId?: string
}
export interface LabelActionBody {
id: string
/** Label name */
name?: string
/** Label color ID */
color?: number
/** Is label has been deleted */
deleted?: boolean
/** WhatsApp has 5 predefined labels (New customer, New order & etc) */
predefinedId?: number
id: string
/** Label name */
name?: string
/** Label color ID */
color?: number
/** Is label has been deleted */
deleted?: boolean
/** WhatsApp has 5 predefined labels (New customer, New order & etc) */
predefinedId?: number
}
/** WhatsApp has 20 predefined colors */
export enum LabelColor {
Color1 = 0,
Color2,
Color3,
Color4,
Color5,
Color6,
Color7,
Color8,
Color9,
Color10,
Color11,
Color12,
Color13,
Color14,
Color15,
Color16,
Color17,
Color18,
Color19,
Color20,
}
Color1 = 0,
Color2,
Color3,
Color4,
Color5,
Color6,
Color7,
Color8,
Color9,
Color10,
Color11,
Color12,
Color13,
Color14,
Color15,
Color16,
Color17,
Color18,
Color19,
Color20
}

View File

@@ -1,35 +1,35 @@
/** Association type */
export enum LabelAssociationType {
Chat = 'label_jid',
Message = 'label_message'
Chat = 'label_jid',
Message = 'label_message'
}
export type LabelAssociationTypes = `${LabelAssociationType}`
/** Association for chat */
export interface ChatLabelAssociation {
type: LabelAssociationType.Chat
chatId: string
labelId: string
type: LabelAssociationType.Chat
chatId: string
labelId: string
}
/** Association for message */
export interface MessageLabelAssociation {
type: LabelAssociationType.Message
chatId: string
messageId: string
labelId: string
type: LabelAssociationType.Message
chatId: string
messageId: string
labelId: string
}
export type LabelAssociation = ChatLabelAssociation | MessageLabelAssociation
/** Body for add/remove chat label association action */
export interface ChatLabelAssociationActionBody {
labelId: string
labelId: string
}
/** body for add/remove message label association action */
export interface MessageLabelAssociationActionBody {
labelId: string
messageId: string
}
labelId: string
messageId: string
}

View File

@@ -17,7 +17,12 @@ export type WAMessageKey = proto.IMessageKey
export type WATextMessage = proto.Message.IExtendedTextMessage
export type WAContextInfo = proto.IContextInfo
export type WALocationMessage = proto.Message.ILocationMessage
export type WAGenericMediaMessage = proto.Message.IVideoMessage | proto.Message.IImageMessage | proto.Message.IAudioMessage | proto.Message.IDocumentMessage | proto.Message.IStickerMessage
export type WAGenericMediaMessage =
| proto.Message.IVideoMessage
| proto.Message.IImageMessage
| proto.Message.IAudioMessage
| proto.Message.IDocumentMessage
| proto.Message.IStickerMessage
export const WAMessageStubType = proto.WebMessageInfo.StubType
export const WAMessageStatus = proto.WebMessageInfo.Status
import { ILogger } from '../Utils/logger'
@@ -27,235 +32,261 @@ export type WAMediaUpload = Buffer | WAMediaPayloadStream | WAMediaPayloadURL
/** Set of message types that are supported by the library */
export type MessageType = keyof proto.Message
export type DownloadableMessage = { mediaKey?: Uint8Array | null, directPath?: string | null, url?: string | null }
export type DownloadableMessage = { mediaKey?: Uint8Array | null; directPath?: string | null; url?: string | null }
export type MessageReceiptType = 'read' | 'read-self' | 'hist_sync' | 'peer_msg' | 'sender' | 'inactive' | 'played' | undefined
export type MessageReceiptType =
| 'read'
| 'read-self'
| 'hist_sync'
| 'peer_msg'
| 'sender'
| 'inactive'
| 'played'
| undefined
export type MediaConnInfo = {
auth: string
ttl: number
hosts: { hostname: string, maxContentLengthBytes: number }[]
fetchDate: Date
auth: string
ttl: number
hosts: { hostname: string; maxContentLengthBytes: number }[]
fetchDate: Date
}
export interface WAUrlInfo {
'canonical-url': string
'matched-text': string
title: string
description?: string
jpegThumbnail?: Buffer
highQualityThumbnail?: proto.Message.IImageMessage
originalThumbnailUrl?: string
'canonical-url': string
'matched-text': string
title: string
description?: string
jpegThumbnail?: Buffer
highQualityThumbnail?: proto.Message.IImageMessage
originalThumbnailUrl?: string
}
// types to generate WA messages
type Mentionable = {
/** list of jids that are mentioned in the accompanying text */
mentions?: string[]
/** list of jids that are mentioned in the accompanying text */
mentions?: string[]
}
type Contextable = {
/** add contextInfo to the message */
contextInfo?: proto.IContextInfo
/** add contextInfo to the message */
contextInfo?: proto.IContextInfo
}
type ViewOnce = {
viewOnce?: boolean
viewOnce?: boolean
}
type Editable = {
edit?: WAMessageKey
edit?: WAMessageKey
}
type WithDimensions = {
width?: number
height?: number
width?: number
height?: number
}
export type PollMessageOptions = {
name: string
selectableCount?: number
values: string[]
/** 32 byte message secret to encrypt poll selections */
messageSecret?: Uint8Array
toAnnouncementGroup?: boolean
name: string
selectableCount?: number
values: string[]
/** 32 byte message secret to encrypt poll selections */
messageSecret?: Uint8Array
toAnnouncementGroup?: boolean
}
type SharePhoneNumber = {
sharePhoneNumber: boolean
sharePhoneNumber: boolean
}
type RequestPhoneNumber = {
requestPhoneNumber: boolean
requestPhoneNumber: boolean
}
export type MediaType = keyof typeof MEDIA_HKDF_KEY_MAPPING
export type AnyMediaMessageContent = (
({
image: WAMediaUpload
caption?: string
jpegThumbnail?: string
} & Mentionable & Contextable & WithDimensions)
| ({
video: WAMediaUpload
caption?: string
gifPlayback?: boolean
jpegThumbnail?: string
/** if set to true, will send as a `video note` */
ptv?: boolean
} & Mentionable & Contextable & WithDimensions)
| {
audio: WAMediaUpload
/** if set to true, will send as a `voice note` */
ptt?: boolean
/** optionally tell the duration of the audio */
seconds?: number
}
| ({
sticker: WAMediaUpload
isAnimated?: boolean
} & WithDimensions) | ({
document: WAMediaUpload
mimetype: string
fileName?: string
caption?: string
} & Contextable))
& { mimetype?: string } & Editable
| ({
image: WAMediaUpload
caption?: string
jpegThumbnail?: string
} & Mentionable &
Contextable &
WithDimensions)
| ({
video: WAMediaUpload
caption?: string
gifPlayback?: boolean
jpegThumbnail?: string
/** if set to true, will send as a `video note` */
ptv?: boolean
} & Mentionable &
Contextable &
WithDimensions)
| {
audio: WAMediaUpload
/** if set to true, will send as a `voice note` */
ptt?: boolean
/** optionally tell the duration of the audio */
seconds?: number
}
| ({
sticker: WAMediaUpload
isAnimated?: boolean
} & WithDimensions)
| ({
document: WAMediaUpload
mimetype: string
fileName?: string
caption?: string
} & Contextable)
) & { mimetype?: string } & Editable
export type ButtonReplyInfo = {
displayText: string
id: string
index: number
displayText: string
id: string
index: number
}
export type GroupInviteInfo = {
inviteCode: string
inviteExpiration: number
text: string
jid: string
subject: string
inviteCode: string
inviteExpiration: number
text: string
jid: string
subject: string
}
export type WASendableProduct = Omit<proto.Message.ProductMessage.IProductSnapshot, 'productImage'> & {
productImage: WAMediaUpload
productImage: WAMediaUpload
}
export type AnyRegularMessageContent = (
({
text: string
linkPreview?: WAUrlInfo | null
}
& Mentionable & Contextable & Editable)
| AnyMediaMessageContent
| ({
poll: PollMessageOptions
} & Mentionable & Contextable & Editable)
| {
contacts: {
displayName?: string
contacts: proto.Message.IContactMessage[]
}
}
| {
location: WALocationMessage
}
| { react: proto.Message.IReactionMessage }
| {
buttonReply: ButtonReplyInfo
type: 'template' | 'plain'
}
| {
groupInvite: GroupInviteInfo
}
| {
listReply: Omit<proto.Message.IListResponseMessage, 'contextInfo'>
}
| {
pin: WAMessageKey
type: proto.PinInChat.Type
/**
* 24 hours, 7 days, 30 days
*/
time?: 86400 | 604800 | 2592000
}
| {
product: WASendableProduct
businessOwnerJid?: string
body?: string
footer?: string
} | SharePhoneNumber | RequestPhoneNumber
) & ViewOnce
| ({
text: string
linkPreview?: WAUrlInfo | null
} & Mentionable &
Contextable &
Editable)
| AnyMediaMessageContent
| ({
poll: PollMessageOptions
} & Mentionable &
Contextable &
Editable)
| {
contacts: {
displayName?: string
contacts: proto.Message.IContactMessage[]
}
}
| {
location: WALocationMessage
}
| { react: proto.Message.IReactionMessage }
| {
buttonReply: ButtonReplyInfo
type: 'template' | 'plain'
}
| {
groupInvite: GroupInviteInfo
}
| {
listReply: Omit<proto.Message.IListResponseMessage, 'contextInfo'>
}
| {
pin: WAMessageKey
type: proto.PinInChat.Type
/**
* 24 hours, 7 days, 30 days
*/
time?: 86400 | 604800 | 2592000
}
| {
product: WASendableProduct
businessOwnerJid?: string
body?: string
footer?: string
}
| SharePhoneNumber
| RequestPhoneNumber
) &
ViewOnce
export type AnyMessageContent = AnyRegularMessageContent | {
forward: WAMessage
force?: boolean
} | {
/** Delete your message or anyone's message in a group (admin required) */
delete: WAMessageKey
} | {
disappearingMessagesInChat: boolean | number
}
export type AnyMessageContent =
| AnyRegularMessageContent
| {
forward: WAMessage
force?: boolean
}
| {
/** Delete your message or anyone's message in a group (admin required) */
delete: WAMessageKey
}
| {
disappearingMessagesInChat: boolean | number
}
export type GroupMetadataParticipants = Pick<GroupMetadata, 'participants'>
type MinimalRelayOptions = {
/** override the message ID with a custom provided string */
messageId?: string
/** should we use group metadata cache, or fetch afresh from the server; default assumed to be "true" */
useCachedGroupMetadata?: boolean
/** override the message ID with a custom provided string */
messageId?: string
/** should we use group metadata cache, or fetch afresh from the server; default assumed to be "true" */
useCachedGroupMetadata?: boolean
}
export type MessageRelayOptions = MinimalRelayOptions & {
/** only send to a specific participant; used when a message decryption fails for a single user */
participant?: { jid: string, count: number }
/** additional attributes to add to the WA binary node */
additionalAttributes?: { [_: string]: string }
additionalNodes?: BinaryNode[]
/** should we use the devices cache, or fetch afresh from the server; default assumed to be "true" */
useUserDevicesCache?: boolean
/** jid list of participants for status@broadcast */
statusJidList?: string[]
/** only send to a specific participant; used when a message decryption fails for a single user */
participant?: { jid: string; count: number }
/** additional attributes to add to the WA binary node */
additionalAttributes?: { [_: string]: string }
additionalNodes?: BinaryNode[]
/** should we use the devices cache, or fetch afresh from the server; default assumed to be "true" */
useUserDevicesCache?: boolean
/** jid list of participants for status@broadcast */
statusJidList?: string[]
}
export type MiscMessageGenerationOptions = MinimalRelayOptions & {
/** optional, if you want to manually set the timestamp of the message */
/** optional, if you want to manually set the timestamp of the message */
timestamp?: Date
/** the message you want to quote */
/** the message you want to quote */
quoted?: WAMessage
/** disappearing messages settings */
ephemeralExpiration?: number | string
/** timeout for media upload to WA server */
mediaUploadTimeoutMs?: number
/** jid list of participants for status@broadcast */
statusJidList?: string[]
/** backgroundcolor for status */
backgroundColor?: string
/** font type for status */
font?: number
/** if it is broadcast */
broadcast?: boolean
/** disappearing messages settings */
ephemeralExpiration?: number | string
/** timeout for media upload to WA server */
mediaUploadTimeoutMs?: number
/** jid list of participants for status@broadcast */
statusJidList?: string[]
/** backgroundcolor for status */
backgroundColor?: string
/** font type for status */
font?: number
/** if it is broadcast */
broadcast?: boolean
}
export type MessageGenerationOptionsFromContent = MiscMessageGenerationOptions & {
userJid: string
}
export type WAMediaUploadFunction = (readStream: Readable, opts: { fileEncSha256B64: string, mediaType: MediaType, timeoutMs?: number }) => Promise<{ mediaUrl: string, directPath: string }>
export type WAMediaUploadFunction = (
readStream: Readable,
opts: { fileEncSha256B64: string; mediaType: MediaType; timeoutMs?: number }
) => Promise<{ mediaUrl: string; directPath: string }>
export type MediaGenerationOptions = {
logger?: ILogger
mediaTypeOverride?: MediaType
upload: WAMediaUploadFunction
/** cache media so it does not have to be uploaded again */
mediaCache?: CacheStore
mediaTypeOverride?: MediaType
upload: WAMediaUploadFunction
/** cache media so it does not have to be uploaded again */
mediaCache?: CacheStore
mediaUploadTimeoutMs?: number
mediaUploadTimeoutMs?: number
options?: AxiosRequestConfig
options?: AxiosRequestConfig
backgroundColor?: string
backgroundColor?: string
font?: number
font?: number
}
export type MessageContentGenerationOptions = MediaGenerationOptions & {
getUrlInfo?: (text: string) => Promise<WAUrlInfo | undefined>
getProfilePicUrl?: (jid: string, type: 'image' | 'preview') => Promise<string | undefined>
getProfilePicUrl?: (jid: string, type: 'image' | 'preview') => Promise<string | undefined>
}
export type MessageGenerationOptions = MessageContentGenerationOptions & MessageGenerationOptionsFromContent
@@ -268,16 +299,16 @@ export type MessageUpsertType = 'append' | 'notify'
export type MessageUserReceipt = proto.IUserReceipt
export type WAMessageUpdate = { update: Partial<WAMessage>, key: proto.IMessageKey }
export type WAMessageUpdate = { update: Partial<WAMessage>; key: proto.IMessageKey }
export type WAMessageCursor = { before: WAMessageKey | undefined } | { after: WAMessageKey | undefined }
export type MessageUserReceiptUpdate = { key: proto.IMessageKey, receipt: MessageUserReceipt }
export type MessageUserReceiptUpdate = { key: proto.IMessageKey; receipt: MessageUserReceipt }
export type MediaDecryptionKeyInfo = {
iv: Buffer
cipherKey: Buffer
macKey?: Buffer
iv: Buffer
cipherKey: Buffer
macKey?: Buffer
}
export type MinimalMessage = Pick<proto.IWebMessageInfo, 'key' | 'messageTimestamp'>

View File

@@ -2,7 +2,7 @@ import { WAMediaUpload } from './Message'
export type CatalogResult = {
data: {
paging: { cursors: { before: string, after: string } }
paging: { cursors: { before: string; after: string } }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: any[]
}
@@ -82,4 +82,4 @@ export type GetCatalogOptions = {
limit?: number
jid?: string
}
}

View File

@@ -51,9 +51,7 @@ type E2ESessionOpts = {
export type SignalRepository = {
decryptGroupMessage(opts: DecryptGroupSignalOpts): Promise<Uint8Array>
processSenderKeyDistributionMessage(
opts: ProcessSenderKeyDistributionMessageOpts
): Promise<void>
processSenderKeyDistributionMessage(opts: ProcessSenderKeyDistributionMessageOpts): Promise<void>
decryptMessage(opts: DecryptSignalProtoOpts): Promise<Uint8Array>
encryptMessage(opts: EncryptMessageOpts): Promise<{
type: 'pkmsg' | 'msg'
@@ -65,4 +63,4 @@ export type SignalRepository = {
}>
injectE2ESession(opts: E2ESessionOpts): Promise<void>
jidToSignalProtocolAddress(jid: string): string
}
}

View File

@@ -1,4 +1,3 @@
import { AxiosRequestConfig } from 'axios'
import type { Agent } from 'https'
import type { URL } from 'url'
@@ -13,121 +12,124 @@ export type WAVersion = [number, number, number]
export type WABrowserDescription = [string, string, string]
export type CacheStore = {
/** get a cached key and change the stats */
get<T>(key: string): T | undefined
/** set a key in the cache */
set<T>(key: string, value: T): void
/** delete a key from the cache */
del(key: string): void
/** flush all data */
flushAll(): void
/** get a cached key and change the stats */
get<T>(key: string): T | undefined
/** set a key in the cache */
set<T>(key: string, value: T): void
/** delete a key from the cache */
del(key: string): void
/** flush all data */
flushAll(): void
}
export type PatchedMessageWithRecipientJID = proto.IMessage & {recipientJid?: string}
export type PatchedMessageWithRecipientJID = proto.IMessage & { recipientJid?: string }
export type SocketConfig = {
/** the WS url to connect to WA */
waWebSocketUrl: string | URL
/** Fails the connection if the socket times out in this interval */
connectTimeoutMs: number
/** Default timeout for queries, undefined for no timeout */
defaultQueryTimeoutMs: number | undefined
/** ping-pong interval for WS connection */
keepAliveIntervalMs: number
/** the WS url to connect to WA */
waWebSocketUrl: string | URL
/** Fails the connection if the socket times out in this interval */
connectTimeoutMs: number
/** Default timeout for queries, undefined for no timeout */
defaultQueryTimeoutMs: number | undefined
/** ping-pong interval for WS connection */
keepAliveIntervalMs: number
/** should baileys use the mobile api instead of the multi device api
* @deprecated This feature has been removed
*/
* @deprecated This feature has been removed
*/
mobile?: boolean
/** proxy agent */
agent?: Agent
/** logger */
logger: ILogger
/** version to connect with */
version: WAVersion
/** override browser config */
browser: WABrowserDescription
/** agent used for fetch requests -- uploading/downloading media */
fetchAgent?: Agent
/** should the QR be printed in the terminal
* @deprecated This feature has been removed
*/
printQRInTerminal?: boolean
/** should events be emitted for actions done by this socket connection */
emitOwnEvents: boolean
/** custom upload hosts to upload media to */
customUploadHosts: MediaConnInfo['hosts']
/** time to wait between sending new retry requests */
retryRequestDelayMs: number
/** max retry count */
maxMsgRetryCount: number
/** time to wait for the generation of the next QR in ms */
qrTimeout?: number
/** provide an auth state object to maintain the auth state */
auth: AuthenticationState
/** manage history processing with this control; by default will sync up everything */
shouldSyncHistoryMessage: (msg: proto.Message.IHistorySyncNotification) => boolean
/** transaction capability options for SignalKeyStore */
transactionOpts: TransactionCapabilityOptions
/** marks the client as online whenever the socket successfully connects */
markOnlineOnConnect: boolean
/** alphanumeric country code (USA -> US) for the number used */
countryCode: string
/** provide a cache to store media, so does not have to be re-uploaded */
mediaCache?: CacheStore
/**
* map to store the retry counts for failed messages;
* used to determine whether to retry a message or not */
msgRetryCounterCache?: CacheStore
/** provide a cache to store a user's device list */
userDevicesCache?: CacheStore
/** cache to store call offers */
callOfferCache?: CacheStore
/** cache to track placeholder resends */
placeholderResendCache?: CacheStore
/** width for link preview images */
linkPreviewImageThumbnailWidth: number
/** Should Baileys ask the phone for full history, will be received async */
syncFullHistory: boolean
/** Should baileys fire init queries automatically, default true */
fireInitQueries: boolean
/**
* generate a high quality link preview,
* entails uploading the jpegThumbnail to WA
* */
generateHighQualityLinkPreview: boolean
/** proxy agent */
agent?: Agent
/** logger */
logger: ILogger
/** version to connect with */
version: WAVersion
/** override browser config */
browser: WABrowserDescription
/** agent used for fetch requests -- uploading/downloading media */
fetchAgent?: Agent
/** should the QR be printed in the terminal
* @deprecated This feature has been removed
*/
printQRInTerminal?: boolean
/** should events be emitted for actions done by this socket connection */
emitOwnEvents: boolean
/** custom upload hosts to upload media to */
customUploadHosts: MediaConnInfo['hosts']
/** time to wait between sending new retry requests */
retryRequestDelayMs: number
/** max retry count */
maxMsgRetryCount: number
/** time to wait for the generation of the next QR in ms */
qrTimeout?: number
/** provide an auth state object to maintain the auth state */
auth: AuthenticationState
/** manage history processing with this control; by default will sync up everything */
shouldSyncHistoryMessage: (msg: proto.Message.IHistorySyncNotification) => boolean
/** transaction capability options for SignalKeyStore */
transactionOpts: TransactionCapabilityOptions
/** marks the client as online whenever the socket successfully connects */
markOnlineOnConnect: boolean
/** alphanumeric country code (USA -> US) for the number used */
countryCode: string
/** provide a cache to store media, so does not have to be re-uploaded */
mediaCache?: CacheStore
/**
* map to store the retry counts for failed messages;
* used to determine whether to retry a message or not */
msgRetryCounterCache?: CacheStore
/** provide a cache to store a user's device list */
userDevicesCache?: CacheStore
/** cache to store call offers */
callOfferCache?: CacheStore
/** cache to track placeholder resends */
placeholderResendCache?: CacheStore
/** width for link preview images */
linkPreviewImageThumbnailWidth: number
/** Should Baileys ask the phone for full history, will be received async */
syncFullHistory: boolean
/** Should baileys fire init queries automatically, default true */
fireInitQueries: boolean
/**
* generate a high quality link preview,
* entails uploading the jpegThumbnail to WA
* */
generateHighQualityLinkPreview: boolean
/**
* Returns if a jid should be ignored,
* no event for that jid will be triggered.
* Messages from that jid will also not be decrypted
* */
shouldIgnoreJid: (jid: string) => boolean | undefined
/**
* Returns if a jid should be ignored,
* no event for that jid will be triggered.
* Messages from that jid will also not be decrypted
* */
shouldIgnoreJid: (jid: string) => boolean | undefined
/**
* Optionally patch the message before sending out
* */
patchMessageBeforeSending: (
msg: proto.IMessage,
recipientJids?: string[],
) => Promise<PatchedMessageWithRecipientJID[] | PatchedMessageWithRecipientJID> | PatchedMessageWithRecipientJID[] | PatchedMessageWithRecipientJID
/**
* Optionally patch the message before sending out
* */
patchMessageBeforeSending: (
msg: proto.IMessage,
recipientJids?: string[]
) =>
| Promise<PatchedMessageWithRecipientJID[] | PatchedMessageWithRecipientJID>
| PatchedMessageWithRecipientJID[]
| PatchedMessageWithRecipientJID
/** verify app state MACs */
appStateMacVerification: {
patch: boolean
snapshot: boolean
}
/** verify app state MACs */
appStateMacVerification: {
patch: boolean
snapshot: boolean
}
/** options for axios */
options: AxiosRequestConfig<{}>
/**
* fetch a message from your store
* implement this so that messages failed to send
* (solves the "this message can take a while" issue) can be retried
* */
getMessage: (key: proto.IMessageKey) => Promise<proto.IMessage | undefined>
/** options for axios */
options: AxiosRequestConfig<{}>
/**
* fetch a message from your store
* implement this so that messages failed to send
* (solves the "this message can take a while" issue) can be retried
* */
getMessage: (key: proto.IMessageKey) => Promise<proto.IMessage | undefined>
/** cached group metadata, use to prevent redundant requests to WA & speed up msg sending */
cachedGroupMetadata: (jid: string) => Promise<GroupMetadata | undefined>
/** cached group metadata, use to prevent redundant requests to WA & speed up msg sending */
cachedGroupMetadata: (jid: string) => Promise<GroupMetadata | undefined>
makeSignalRepository: (auth: SignalAuthState) => SignalRepository
makeSignalRepository: (auth: SignalAuthState) => SignalRepository
}

View File

@@ -26,4 +26,4 @@ export type ConnectionState = {
* If this is false, the primary phone and other devices will receive notifs
* */
isOnline?: boolean
}
}

View File

@@ -5,23 +5,23 @@ import { USyncUser } from '../WAUSync'
* Defines the interface for a USyncQuery protocol
*/
export interface USyncQueryProtocol {
/**
* The name of the protocol
*/
name: string
/**
* Defines what goes inside the query part of a USyncQuery
*/
getQueryElement: () => BinaryNode
/**
* Defines what goes inside the user part of a USyncQuery
*/
getUserElement: (user: USyncUser) => BinaryNode | null
/**
* The name of the protocol
*/
name: string
/**
* Defines what goes inside the query part of a USyncQuery
*/
getQueryElement: () => BinaryNode
/**
* Defines what goes inside the user part of a USyncQuery
*/
getUserElement: (user: USyncUser) => BinaryNode | null
/**
* Parse the result of the query
* @param data Data from the result
* @returns Whatever the protocol is supposed to return
*/
parser: (data: BinaryNode) => unknown
}
/**
* Parse the result of the query
* @param data Data from the result
* @returns Whatever the protocol is supposed to return
*/
parser: (data: BinaryNode) => unknown
}

View File

@@ -16,51 +16,51 @@ import { SocketConfig } from './Socket'
export type UserFacingSocketConfig = Partial<SocketConfig> & { auth: AuthenticationState }
export type BrowsersMap = {
ubuntu(browser: string): [string, string, string]
macOS(browser: string): [string, string, string]
baileys(browser: string): [string, string, string]
windows(browser: string): [string, string, string]
appropriate(browser: string): [string, string, string]
ubuntu(browser: string): [string, string, string]
macOS(browser: string): [string, string, string]
baileys(browser: string): [string, string, string]
windows(browser: string): [string, string, string]
appropriate(browser: string): [string, string, string]
}
export enum DisconnectReason {
connectionClosed = 428,
connectionLost = 408,
connectionReplaced = 440,
timedOut = 408,
loggedOut = 401,
badSession = 500,
restartRequired = 515,
multideviceMismatch = 411,
forbidden = 403,
unavailableService = 503
connectionClosed = 428,
connectionLost = 408,
connectionReplaced = 440,
timedOut = 408,
loggedOut = 401,
badSession = 500,
restartRequired = 515,
multideviceMismatch = 411,
forbidden = 403,
unavailableService = 503
}
export type WAInitResponse = {
ref: string
ttl: number
status: 200
ref: string
ttl: number
status: 200
}
export type WABusinessHoursConfig = {
day_of_week: string
mode: string
open_time?: number
close_time?: number
day_of_week: string
mode: string
open_time?: number
close_time?: number
}
export type WABusinessProfile = {
description: string
email: string | undefined
business_hours: {
timezone?: string
config?: WABusinessHoursConfig[]
business_config?: WABusinessHoursConfig[]
}
website: string[]
category?: string
wid?: string
address?: string
description: string
email: string | undefined
business_hours: {
timezone?: string
config?: WABusinessHoursConfig[]
business_config?: WABusinessHoursConfig[]
}
website: string[]
category?: string
wid?: string
address?: string
}
export type CurveKeyPair = { private: Uint8Array, public: Uint8Array }
export type CurveKeyPair = { private: Uint8Array; public: Uint8Array }

View File

@@ -1,7 +1,15 @@
import NodeCache from '@cacheable/node-cache'
import { randomBytes } from 'crypto'
import { DEFAULT_CACHE_TTLS } from '../Defaults'
import type { AuthenticationCreds, CacheStore, SignalDataSet, SignalDataTypeMap, SignalKeyStore, SignalKeyStoreWithTransaction, TransactionCapabilityOptions } from '../Types'
import type {
AuthenticationCreds,
CacheStore,
SignalDataSet,
SignalDataTypeMap,
SignalKeyStore,
SignalKeyStoreWithTransaction,
TransactionCapabilityOptions
} from '../Types'
import { Curve, signedKeyPair } from './crypto'
import { delay, generateRegistrationId } from './generics'
import { ILogger } from './logger'
@@ -17,11 +25,13 @@ export function makeCacheableSignalKeyStore(
logger?: ILogger,
_cache?: CacheStore
): SignalKeyStore {
const cache = _cache || new NodeCache({
stdTTL: DEFAULT_CACHE_TTLS.SIGNAL_STORE, // 5 minutes
useClones: false,
deleteOnExpire: true,
})
const cache =
_cache ||
new NodeCache({
stdTTL: DEFAULT_CACHE_TTLS.SIGNAL_STORE, // 5 minutes
useClones: false,
deleteOnExpire: true
})
function getUniqueId(type: string, id: string) {
return `${type}.${id}`
@@ -29,23 +39,23 @@ export function makeCacheableSignalKeyStore(
return {
async get(type, ids) {
const data: { [_: string]: SignalDataTypeMap[typeof type] } = { }
const data: { [_: string]: SignalDataTypeMap[typeof type] } = {}
const idsToFetch: string[] = []
for(const id of ids) {
for (const id of ids) {
const item = cache.get<SignalDataTypeMap[typeof type]>(getUniqueId(type, id))
if(typeof item !== 'undefined') {
if (typeof item !== 'undefined') {
data[id] = item
} else {
idsToFetch.push(id)
}
}
if(idsToFetch.length) {
if (idsToFetch.length) {
logger?.trace({ items: idsToFetch.length }, 'loading from store')
const fetched = await store.get(type, idsToFetch)
for(const id of idsToFetch) {
for (const id of idsToFetch) {
const item = fetched[id]
if(item) {
if (item) {
data[id] = item
cache.set(getUniqueId(type, id), item)
}
@@ -56,8 +66,8 @@ export function makeCacheableSignalKeyStore(
},
async set(data) {
let keys = 0
for(const type in data) {
for(const id in data[type]) {
for (const type in data) {
for (const id in data[type]) {
cache.set(getUniqueId(type, id), data[type][id])
keys += 1
}
@@ -89,52 +99,45 @@ export const addTransactionCapability = (
// number of queries made to the DB during the transaction
// only there for logging purposes
let dbQueriesInTransaction = 0
let transactionCache: SignalDataSet = { }
let mutations: SignalDataSet = { }
let transactionCache: SignalDataSet = {}
let mutations: SignalDataSet = {}
let transactionsInProgress = 0
return {
get: async(type, ids) => {
if(isInTransaction()) {
get: async (type, ids) => {
if (isInTransaction()) {
const dict = transactionCache[type]
const idsRequiringFetch = dict
? ids.filter(item => typeof dict[item] === 'undefined')
: ids
const idsRequiringFetch = dict ? ids.filter(item => typeof dict[item] === 'undefined') : ids
// only fetch if there are any items to fetch
if(idsRequiringFetch.length) {
if (idsRequiringFetch.length) {
dbQueriesInTransaction += 1
const result = await state.get(type, idsRequiringFetch)
transactionCache[type] ||= {}
Object.assign(
transactionCache[type]!,
result
)
Object.assign(transactionCache[type]!, result)
}
return ids.reduce(
(dict, id) => {
const value = transactionCache[type]?.[id]
if(value) {
dict[id] = value
}
return ids.reduce((dict, id) => {
const value = transactionCache[type]?.[id]
if (value) {
dict[id] = value
}
return dict
}, { }
)
return dict
}, {})
} else {
return state.get(type, ids)
}
},
set: data => {
if(isInTransaction()) {
if (isInTransaction()) {
logger.trace({ types: Object.keys(data) }, 'caching in transaction')
for(const key in data) {
transactionCache[key] = transactionCache[key] || { }
for (const key in data) {
transactionCache[key] = transactionCache[key] || {}
Object.assign(transactionCache[key], data[key])
mutations[key] = mutations[key] || { }
mutations[key] = mutations[key] || {}
Object.assign(mutations[key], data[key])
}
} else {
@@ -145,27 +148,27 @@ export const addTransactionCapability = (
async transaction(work) {
let result: Awaited<ReturnType<typeof work>>
transactionsInProgress += 1
if(transactionsInProgress === 1) {
if (transactionsInProgress === 1) {
logger.trace('entering transaction')
}
try {
result = await work()
// commit if this is the outermost transaction
if(transactionsInProgress === 1) {
if(Object.keys(mutations).length) {
if (transactionsInProgress === 1) {
if (Object.keys(mutations).length) {
logger.trace('committing transaction')
// retry mechanism to ensure we've some recovery
// in case a transaction fails in the first attempt
let tries = maxCommitRetries
while(tries) {
while (tries) {
tries -= 1
//eslint-disable-next-line max-depth
try {
await state.set(mutations)
logger.trace({ dbQueriesInTransaction }, 'committed transaction')
break
} catch(error) {
} catch (error) {
logger.warn(`failed to commit ${Object.keys(mutations).length} mutations, tries left=${tries}`)
await delay(delayBetweenTriesMs)
}
@@ -176,9 +179,9 @@ export const addTransactionCapability = (
}
} finally {
transactionsInProgress -= 1
if(transactionsInProgress === 0) {
transactionCache = { }
mutations = { }
if (transactionsInProgress === 0) {
transactionCache = {}
mutations = {}
dbQueriesInTransaction = 0
}
}
@@ -211,6 +214,6 @@ export const initAuthCreds = (): AuthenticationCreds => {
registered: false,
pairingCode: undefined,
lastPropHash: undefined,
routingInfo: undefined,
routingInfo: undefined
}
}

View File

@@ -16,15 +16,13 @@ export const captureEventStream = (ev: BaileysEventEmitter, filename: string) =>
// write mutex so data is appended in order
const writeMutex = makeMutex()
// monkey patch eventemitter to capture all events
ev.emit = function(...args: any[]) {
ev.emit = function (...args: any[]) {
const content = JSON.stringify({ timestamp: Date.now(), event: args[0], data: args[1] }) + '\n'
const result = oldEmit.apply(ev, args)
writeMutex.mutex(
async() => {
await writeFile(filename, content, { flag: 'a' })
}
)
writeMutex.mutex(async () => {
await writeFile(filename, content, { flag: 'a' })
})
return result
}
@@ -38,7 +36,7 @@ export const captureEventStream = (ev: BaileysEventEmitter, filename: string) =>
export const readAndEmitEventStream = (filename: string, delayIntervalMs = 0) => {
const ev = new EventEmitter() as BaileysEventEmitter
const fireEvents = async() => {
const fireEvents = async () => {
// from: https://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js
const fileStream = createReadStream(filename)
@@ -49,10 +47,10 @@ export const readAndEmitEventStream = (filename: string, delayIntervalMs = 0) =>
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.
for await (const line of rl) {
if(line) {
if (line) {
const { event, data } = JSON.parse(line)
ev.emit(event, data)
delayIntervalMs && await delay(delayIntervalMs)
delayIntervalMs && (await delay(delayIntervalMs))
}
}
@@ -63,4 +61,4 @@ export const readAndEmitEventStream = (filename: string, delayIntervalMs = 0) =>
ev,
task: fireEvents()
}
}
}

View File

@@ -1,6 +1,16 @@
import { Boom } from '@hapi/boom'
import { createHash } from 'crypto'
import { CatalogCollection, CatalogStatus, OrderDetails, OrderProduct, Product, ProductCreate, ProductUpdate, WAMediaUpload, WAMediaUploadFunction } from '../Types'
import {
CatalogCollection,
CatalogStatus,
OrderDetails,
OrderProduct,
Product,
ProductCreate,
ProductUpdate,
WAMediaUpload,
WAMediaUploadFunction
} from '../Types'
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString } from '../WABinary'
import { getStream, getUrlFromDirectPath, toReadable } from './messages-media'
@@ -11,28 +21,24 @@ export const parseCatalogNode = (node: BinaryNode) => {
return {
products,
nextPageCursor: paging
? getBinaryNodeChildString(paging, 'after')
: undefined
nextPageCursor: paging ? getBinaryNodeChildString(paging, 'after') : undefined
}
}
export const parseCollectionsNode = (node: BinaryNode) => {
const collectionsNode = getBinaryNodeChild(node, 'collections')
const collections = getBinaryNodeChildren(collectionsNode, 'collection').map<CatalogCollection>(
collectionNode => {
const id = getBinaryNodeChildString(collectionNode, 'id')!
const name = getBinaryNodeChildString(collectionNode, 'name')!
const collections = getBinaryNodeChildren(collectionsNode, 'collection').map<CatalogCollection>(collectionNode => {
const id = getBinaryNodeChildString(collectionNode, 'id')!
const name = getBinaryNodeChildString(collectionNode, 'name')!
const products = getBinaryNodeChildren(collectionNode, 'product').map(parseProductNode)
return {
id,
name,
products,
status: parseStatusInfo(collectionNode)
}
const products = getBinaryNodeChildren(collectionNode, 'product').map(parseProductNode)
return {
id,
name,
products,
status: parseStatusInfo(collectionNode)
}
)
})
return {
collections
@@ -41,26 +47,24 @@ export const parseCollectionsNode = (node: BinaryNode) => {
export const parseOrderDetailsNode = (node: BinaryNode) => {
const orderNode = getBinaryNodeChild(node, 'order')
const products = getBinaryNodeChildren(orderNode, 'product').map<OrderProduct>(
productNode => {
const imageNode = getBinaryNodeChild(productNode, 'image')!
return {
id: getBinaryNodeChildString(productNode, 'id')!,
name: getBinaryNodeChildString(productNode, 'name')!,
imageUrl: getBinaryNodeChildString(imageNode, 'url')!,
price: +getBinaryNodeChildString(productNode, 'price')!,
currency: getBinaryNodeChildString(productNode, 'currency')!,
quantity: +getBinaryNodeChildString(productNode, 'quantity')!
}
const products = getBinaryNodeChildren(orderNode, 'product').map<OrderProduct>(productNode => {
const imageNode = getBinaryNodeChild(productNode, 'image')!
return {
id: getBinaryNodeChildString(productNode, 'id')!,
name: getBinaryNodeChildString(productNode, 'name')!,
imageUrl: getBinaryNodeChildString(imageNode, 'url')!,
price: +getBinaryNodeChildString(productNode, 'price')!,
currency: getBinaryNodeChildString(productNode, 'currency')!,
quantity: +getBinaryNodeChildString(productNode, 'quantity')!
}
)
})
const priceNode = getBinaryNodeChild(orderNode, 'price')
const orderDetails: OrderDetails = {
price: {
total: +getBinaryNodeChildString(priceNode, 'total')!,
currency: getBinaryNodeChildString(priceNode, 'currency')!,
currency: getBinaryNodeChildString(priceNode, 'currency')!
},
products
}
@@ -69,94 +73,92 @@ export const parseOrderDetailsNode = (node: BinaryNode) => {
}
export const toProductNode = (productId: string | undefined, product: ProductCreate | ProductUpdate) => {
const attrs: BinaryNode['attrs'] = { }
const content: BinaryNode[] = [ ]
const attrs: BinaryNode['attrs'] = {}
const content: BinaryNode[] = []
if(typeof productId !== 'undefined') {
if (typeof productId !== 'undefined') {
content.push({
tag: 'id',
attrs: { },
attrs: {},
content: Buffer.from(productId)
})
}
if(typeof product.name !== 'undefined') {
if (typeof product.name !== 'undefined') {
content.push({
tag: 'name',
attrs: { },
attrs: {},
content: Buffer.from(product.name)
})
}
if(typeof product.description !== 'undefined') {
if (typeof product.description !== 'undefined') {
content.push({
tag: 'description',
attrs: { },
attrs: {},
content: Buffer.from(product.description)
})
}
if(typeof product.retailerId !== 'undefined') {
if (typeof product.retailerId !== 'undefined') {
content.push({
tag: 'retailer_id',
attrs: { },
attrs: {},
content: Buffer.from(product.retailerId)
})
}
if(product.images.length) {
if (product.images.length) {
content.push({
tag: 'media',
attrs: { },
content: product.images.map(
img => {
if(!('url' in img)) {
throw new Boom('Expected img for product to already be uploaded', { statusCode: 400 })
}
return {
tag: 'image',
attrs: { },
content: [
{
tag: 'url',
attrs: { },
content: Buffer.from(img.url.toString())
}
]
}
attrs: {},
content: product.images.map(img => {
if (!('url' in img)) {
throw new Boom('Expected img for product to already be uploaded', { statusCode: 400 })
}
)
return {
tag: 'image',
attrs: {},
content: [
{
tag: 'url',
attrs: {},
content: Buffer.from(img.url.toString())
}
]
}
})
})
}
if(typeof product.price !== 'undefined') {
if (typeof product.price !== 'undefined') {
content.push({
tag: 'price',
attrs: { },
attrs: {},
content: Buffer.from(product.price.toString())
})
}
if(typeof product.currency !== 'undefined') {
if (typeof product.currency !== 'undefined') {
content.push({
tag: 'currency',
attrs: { },
attrs: {},
content: Buffer.from(product.currency)
})
}
if('originCountryCode' in product) {
if(typeof product.originCountryCode === 'undefined') {
if ('originCountryCode' in product) {
if (typeof product.originCountryCode === 'undefined') {
attrs['compliance_category'] = 'COUNTRY_ORIGIN_EXEMPT'
} else {
content.push({
tag: 'compliance_info',
attrs: { },
attrs: {},
content: [
{
tag: 'country_code_origin',
attrs: { },
attrs: {},
content: Buffer.from(product.originCountryCode)
}
]
@@ -164,8 +166,7 @@ export const toProductNode = (productId: string | undefined, product: ProductCre
}
}
if(typeof product.isHidden !== 'undefined') {
if (typeof product.isHidden !== 'undefined') {
attrs['is_hidden'] = product.isHidden.toString()
}
@@ -188,16 +189,16 @@ export const parseProductNode = (productNode: BinaryNode) => {
id,
imageUrls: parseImageUrls(mediaNode),
reviewStatus: {
whatsapp: getBinaryNodeChildString(statusInfoNode, 'status')!,
whatsapp: getBinaryNodeChildString(statusInfoNode, 'status')!
},
availability: 'in stock',
name: getBinaryNodeChildString(productNode, 'name')!,
retailerId: getBinaryNodeChildString(productNode, 'retailer_id'),
url: getBinaryNodeChildString(productNode, 'url'),
description: getBinaryNodeChildString(productNode, 'description')!,
price: +getBinaryNodeChildString(productNode, 'price')!,
price: +getBinaryNodeChildString(productNode, 'price')!,
currency: getBinaryNodeChildString(productNode, 'currency')!,
isHidden,
isHidden
}
return product
@@ -206,10 +207,16 @@ export const parseProductNode = (productNode: BinaryNode) => {
/**
* Uploads images not already uploaded to WA's servers
*/
export async function uploadingNecessaryImagesOfProduct<T extends ProductUpdate | ProductCreate>(product: T, waUploadToServer: WAMediaUploadFunction, timeoutMs = 30_000) {
export async function uploadingNecessaryImagesOfProduct<T extends ProductUpdate | ProductCreate>(
product: T,
waUploadToServer: WAMediaUploadFunction,
timeoutMs = 30_000
) {
product = {
...product,
images: product.images ? await uploadingNecessaryImages(product.images, waUploadToServer, timeoutMs) : product.images
images: product.images
? await uploadingNecessaryImages(product.images, waUploadToServer, timeoutMs)
: product.images
}
return product
}
@@ -217,43 +224,37 @@ export async function uploadingNecessaryImagesOfProduct<T extends ProductUpdate
/**
* Uploads images not already uploaded to WA's servers
*/
export const uploadingNecessaryImages = async(
export const uploadingNecessaryImages = async (
images: WAMediaUpload[],
waUploadToServer: WAMediaUploadFunction,
timeoutMs = 30_000
) => {
const results = await Promise.all(
images.map<Promise<{ url: string }>>(
async img => {
if('url' in img) {
const url = img.url.toString()
if(url.includes('.whatsapp.net')) {
return { url }
}
images.map<Promise<{ url: string }>>(async img => {
if ('url' in img) {
const url = img.url.toString()
if (url.includes('.whatsapp.net')) {
return { url }
}
const { stream } = await getStream(img)
const hasher = createHash('sha256')
const contentBlocks: Buffer[] = []
for await (const block of stream) {
hasher.update(block)
contentBlocks.push(block)
}
const sha = hasher.digest('base64')
const { directPath } = await waUploadToServer(
toReadable(Buffer.concat(contentBlocks)),
{
mediaType: 'product-catalog-image',
fileEncSha256B64: sha,
timeoutMs
}
)
return { url: getUrlFromDirectPath(directPath) }
}
)
const { stream } = await getStream(img)
const hasher = createHash('sha256')
const contentBlocks: Buffer[] = []
for await (const block of stream) {
hasher.update(block)
contentBlocks.push(block)
}
const sha = hasher.digest('base64')
const { directPath } = await waUploadToServer(toReadable(Buffer.concat(contentBlocks)), {
mediaType: 'product-catalog-image',
fileEncSha256B64: sha,
timeoutMs
})
return { url: getUrlFromDirectPath(directPath) }
})
)
return results
}
@@ -270,6 +271,6 @@ const parseStatusInfo = (mediaNode: BinaryNode): CatalogStatus => {
const node = getBinaryNodeChild(mediaNode, 'status_info')
return {
status: getBinaryNodeChildString(node, 'status')!,
canAppeal: getBinaryNodeChildString(node, 'can_appeal') === 'true',
canAppeal: getBinaryNodeChildString(node, 'can_appeal') === 'true'
}
}
}

View File

@@ -1,20 +1,32 @@
import { Boom } from '@hapi/boom'
import { AxiosRequestConfig } from 'axios'
import { proto } from '../../WAProto'
import { BaileysEventEmitter, Chat, ChatModification, ChatMutation, ChatUpdate, Contact, InitialAppStateSyncOptions, LastMessageList, LTHashState, WAPatchCreate, WAPatchName } from '../Types'
import {
BaileysEventEmitter,
Chat,
ChatModification,
ChatMutation,
ChatUpdate,
Contact,
InitialAppStateSyncOptions,
LastMessageList,
LTHashState,
WAPatchCreate,
WAPatchName
} from '../Types'
import { ChatLabelAssociation, LabelAssociationType, MessageLabelAssociation } from '../Types/LabelAssociation'
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidNormalizedUser } from '../WABinary'
import { aesDecrypt, aesEncrypt, hkdf, hmacSign } from './crypto'
import { toNumber } from './generics'
import { ILogger } from './logger'
import { LT_HASH_ANTI_TAMPERING } from './lt-hash'
import { downloadContentFromMessage, } from './messages-media'
import { downloadContentFromMessage } from './messages-media'
type FetchAppStateSyncKey = (keyId: string) => Promise<proto.Message.IAppStateSyncKeyData | null | undefined>
export type ChatMutationMap = { [index: string]: ChatMutation }
const mutationKeys = async(keydata: Uint8Array) => {
const mutationKeys = async (keydata: Uint8Array) => {
const expanded = await hkdf(keydata, 160, { info: 'WhatsApp Mutation Keys' })
return {
indexKey: expanded.slice(0, 32),
@@ -25,16 +37,21 @@ const mutationKeys = async(keydata: Uint8Array) => {
}
}
const generateMac = (operation: proto.SyncdMutation.SyncdOperation, data: Buffer, keyId: Uint8Array | string, key: Buffer) => {
const generateMac = (
operation: proto.SyncdMutation.SyncdOperation,
data: Buffer,
keyId: Uint8Array | string,
key: Buffer
) => {
const getKeyData = () => {
let r: number
switch (operation) {
case proto.SyncdMutation.SyncdOperation.SET:
r = 0x01
break
case proto.SyncdMutation.SyncdOperation.REMOVE:
r = 0x02
break
case proto.SyncdMutation.SyncdOperation.SET:
r = 0x01
break
case proto.SyncdMutation.SyncdOperation.REMOVE:
r = 0x02
break
}
const buff = Buffer.from([r])
@@ -58,7 +75,7 @@ const to64BitNetworkOrder = (e: number) => {
return buff
}
type Mac = { indexMac: Uint8Array, valueMac: Uint8Array, operation: proto.SyncdMutation.SyncdOperation }
type Mac = { indexMac: Uint8Array; valueMac: Uint8Array; operation: proto.SyncdMutation.SyncdOperation }
const makeLtHashGenerator = ({ indexValueMap, hash }: Pick<LTHashState, 'hash' | 'indexValueMap'>) => {
indexValueMap = { ...indexValueMap }
@@ -69,8 +86,8 @@ const makeLtHashGenerator = ({ indexValueMap, hash }: Pick<LTHashState, 'hash' |
mix: ({ indexMac, valueMac, operation }: Mac) => {
const indexMacBase64 = Buffer.from(indexMac).toString('base64')
const prevOp = indexValueMap[indexMacBase64]
if(operation === proto.SyncdMutation.SyncdOperation.REMOVE) {
if(!prevOp) {
if (operation === proto.SyncdMutation.SyncdOperation.REMOVE) {
if (!prevOp) {
throw new Boom('tried remove, but no previous op', { data: { indexMac, valueMac } })
}
@@ -82,11 +99,11 @@ const makeLtHashGenerator = ({ indexValueMap, hash }: Pick<LTHashState, 'hash' |
indexValueMap[indexMacBase64] = { valueMac }
}
if(prevOp) {
if (prevOp) {
subBuffs.push(new Uint8Array(prevOp.valueMac).buffer)
}
},
finish: async() => {
finish: async () => {
const hashArrayBuffer = new Uint8Array(hash).buffer
const result = await LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs)
const buffer = Buffer.from(result)
@@ -100,34 +117,31 @@ const makeLtHashGenerator = ({ indexValueMap, hash }: Pick<LTHashState, 'hash' |
}
const generateSnapshotMac = (lthash: Uint8Array, version: number, name: WAPatchName, key: Buffer) => {
const total = Buffer.concat([
lthash,
to64BitNetworkOrder(version),
Buffer.from(name, 'utf-8')
])
const total = Buffer.concat([lthash, to64BitNetworkOrder(version), Buffer.from(name, 'utf-8')])
return hmacSign(total, key, 'sha256')
}
const generatePatchMac = (snapshotMac: Uint8Array, valueMacs: Uint8Array[], version: number, type: WAPatchName, key: Buffer) => {
const total = Buffer.concat([
snapshotMac,
...valueMacs,
to64BitNetworkOrder(version),
Buffer.from(type, 'utf-8')
])
const generatePatchMac = (
snapshotMac: Uint8Array,
valueMacs: Uint8Array[],
version: number,
type: WAPatchName,
key: Buffer
) => {
const total = Buffer.concat([snapshotMac, ...valueMacs, to64BitNetworkOrder(version), Buffer.from(type, 'utf-8')])
return hmacSign(total, key)
}
export const newLTHashState = (): LTHashState => ({ version: 0, hash: Buffer.alloc(128), indexValueMap: {} })
export const encodeSyncdPatch = async(
export const encodeSyncdPatch = async (
{ type, index, syncAction, apiVersion, operation }: WAPatchCreate,
myAppStateKeyId: string,
state: LTHashState,
getAppStateSyncKey: FetchAppStateSyncKey
) => {
const key = !!myAppStateKeyId ? await getAppStateSyncKey(myAppStateKeyId) : undefined
if(!key) {
if (!key) {
throw new Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 })
}
@@ -185,7 +199,7 @@ export const encodeSyncdPatch = async(
return { patch, state }
}
export const decodeSyncdMutations = async(
export const decodeSyncdMutations = async (
msgMutations: (proto.ISyncdMutation | proto.ISyncdRecord)[],
initialState: LTHashState,
getAppStateSyncKey: FetchAppStateSyncKey,
@@ -196,19 +210,20 @@ export const decodeSyncdMutations = async(
// indexKey used to HMAC sign record.index.blob
// valueEncryptionKey used to AES-256-CBC encrypt record.value.blob[0:-32]
// the remaining record.value.blob[0:-32] is the mac, it the HMAC sign of key.keyId + decoded proto data + length of bytes in keyId
for(const msgMutation of msgMutations) {
for (const msgMutation of msgMutations) {
// if it's a syncdmutation, get the operation property
// otherwise, if it's only a record -- it'll be a SET mutation
const operation = 'operation' in msgMutation ? msgMutation.operation : proto.SyncdMutation.SyncdOperation.SET
const record = ('record' in msgMutation && !!msgMutation.record) ? msgMutation.record : msgMutation as proto.ISyncdRecord
const record =
'record' in msgMutation && !!msgMutation.record ? msgMutation.record : (msgMutation as proto.ISyncdRecord)
const key = await getKey(record.keyId!.id!)
const content = Buffer.from(record.value!.blob!)
const encContent = content.slice(0, -32)
const ogValueMac = content.slice(-32)
if(validateMacs) {
if (validateMacs) {
const contentHmac = generateMac(operation!, encContent, record.keyId!.id!, key.valueMacKey)
if(Buffer.compare(contentHmac, ogValueMac) !== 0) {
if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
throw new Boom('HMAC content verification failed')
}
}
@@ -216,9 +231,9 @@ export const decodeSyncdMutations = async(
const result = aesDecrypt(encContent, key.valueEncryptionKey)
const syncAction = proto.SyncActionData.decode(result)
if(validateMacs) {
if (validateMacs) {
const hmac = hmacSign(syncAction.index!, key.indexKey)
if(Buffer.compare(hmac, record.index!.blob!) !== 0) {
if (Buffer.compare(hmac, record.index!.blob!) !== 0) {
throw new Boom('HMAC index verification failed')
}
}
@@ -238,15 +253,18 @@ export const decodeSyncdMutations = async(
async function getKey(keyId: Uint8Array) {
const base64Key = Buffer.from(keyId).toString('base64')
const keyEnc = await getAppStateSyncKey(base64Key)
if(!keyEnc) {
throw new Boom(`failed to find key "${base64Key}" to decode mutation`, { statusCode: 404, data: { msgMutations } })
if (!keyEnc) {
throw new Boom(`failed to find key "${base64Key}" to decode mutation`, {
statusCode: 404,
data: { msgMutations }
})
}
return mutationKeys(keyEnc.keyData!)
}
}
export const decodeSyncdPatch = async(
export const decodeSyncdPatch = async (
msg: proto.ISyncdPatch,
name: WAPatchName,
initialState: LTHashState,
@@ -254,18 +272,24 @@ export const decodeSyncdPatch = async(
onMutation: (mutation: ChatMutation) => void,
validateMacs: boolean
) => {
if(validateMacs) {
if (validateMacs) {
const base64Key = Buffer.from(msg.keyId!.id!).toString('base64')
const mainKeyObj = await getAppStateSyncKey(base64Key)
if(!mainKeyObj) {
if (!mainKeyObj) {
throw new Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } })
}
const mainKey = await mutationKeys(mainKeyObj.keyData!)
const mutationmacs = msg.mutations!.map(mutation => mutation.record!.value!.blob!.slice(-32))
const patchMac = generatePatchMac(msg.snapshotMac!, mutationmacs, toNumber(msg.version!.version), name, mainKey.patchMacKey)
if(Buffer.compare(patchMac, msg.patchMac!) !== 0) {
const patchMac = generatePatchMac(
msg.snapshotMac!,
mutationmacs,
toNumber(msg.version!.version),
name,
mainKey.patchMacKey
)
if (Buffer.compare(patchMac, msg.patchMac!) !== 0) {
throw new Boom('Invalid patch mac')
}
}
@@ -274,68 +298,59 @@ export const decodeSyncdPatch = async(
return result
}
export const extractSyncdPatches = async(
result: BinaryNode,
options: AxiosRequestConfig<{}>
) => {
export const extractSyncdPatches = async (result: BinaryNode, options: AxiosRequestConfig<{}>) => {
const syncNode = getBinaryNodeChild(result, 'sync')
const collectionNodes = getBinaryNodeChildren(syncNode, 'collection')
const final = {} as { [T in WAPatchName]: { patches: proto.ISyncdPatch[], hasMorePatches: boolean, snapshot?: proto.ISyncdSnapshot } }
const final = {} as {
[T in WAPatchName]: { patches: proto.ISyncdPatch[]; hasMorePatches: boolean; snapshot?: proto.ISyncdSnapshot }
}
await Promise.all(
collectionNodes.map(
async collectionNode => {
const patchesNode = getBinaryNodeChild(collectionNode, 'patches')
collectionNodes.map(async collectionNode => {
const patchesNode = getBinaryNodeChild(collectionNode, 'patches')
const patches = getBinaryNodeChildren(patchesNode || collectionNode, 'patch')
const snapshotNode = getBinaryNodeChild(collectionNode, 'snapshot')
const patches = getBinaryNodeChildren(patchesNode || collectionNode, 'patch')
const snapshotNode = getBinaryNodeChild(collectionNode, 'snapshot')
const syncds: proto.ISyncdPatch[] = []
const name = collectionNode.attrs.name as WAPatchName
const syncds: proto.ISyncdPatch[] = []
const name = collectionNode.attrs.name as WAPatchName
const hasMorePatches = collectionNode.attrs.has_more_patches === 'true'
const hasMorePatches = collectionNode.attrs.has_more_patches === 'true'
let snapshot: proto.ISyncdSnapshot | undefined = undefined
if(snapshotNode && !!snapshotNode.content) {
if(!Buffer.isBuffer(snapshotNode)) {
snapshotNode.content = Buffer.from(Object.values(snapshotNode.content))
}
const blobRef = proto.ExternalBlobReference.decode(
snapshotNode.content as Buffer
)
const data = await downloadExternalBlob(blobRef, options)
snapshot = proto.SyncdSnapshot.decode(data)
let snapshot: proto.ISyncdSnapshot | undefined = undefined
if (snapshotNode && !!snapshotNode.content) {
if (!Buffer.isBuffer(snapshotNode)) {
snapshotNode.content = Buffer.from(Object.values(snapshotNode.content))
}
for(let { content } of patches) {
if(content) {
if(!Buffer.isBuffer(content)) {
content = Buffer.from(Object.values(content))
}
const syncd = proto.SyncdPatch.decode(content as Uint8Array)
if(!syncd.version) {
syncd.version = { version: +collectionNode.attrs.version + 1 }
}
syncds.push(syncd)
}
}
final[name] = { patches: syncds, hasMorePatches, snapshot }
const blobRef = proto.ExternalBlobReference.decode(snapshotNode.content as Buffer)
const data = await downloadExternalBlob(blobRef, options)
snapshot = proto.SyncdSnapshot.decode(data)
}
)
for (let { content } of patches) {
if (content) {
if (!Buffer.isBuffer(content)) {
content = Buffer.from(Object.values(content))
}
const syncd = proto.SyncdPatch.decode(content as Uint8Array)
if (!syncd.version) {
syncd.version = { version: +collectionNode.attrs.version + 1 }
}
syncds.push(syncd)
}
}
final[name] = { patches: syncds, hasMorePatches, snapshot }
})
)
return final
}
export const downloadExternalBlob = async(
blob: proto.IExternalBlobReference,
options: AxiosRequestConfig<{}>
) => {
export const downloadExternalBlob = async (blob: proto.IExternalBlobReference, options: AxiosRequestConfig<{}>) => {
const stream = await downloadContentFromMessage(blob, 'md-app-state', { options })
const bufferArray: Buffer[] = []
for await (const chunk of stream) {
@@ -345,16 +360,13 @@ export const downloadExternalBlob = async(
return Buffer.concat(bufferArray)
}
export const downloadExternalPatch = async(
blob: proto.IExternalBlobReference,
options: AxiosRequestConfig<{}>
) => {
export const downloadExternalPatch = async (blob: proto.IExternalBlobReference, options: AxiosRequestConfig<{}>) => {
const buffer = await downloadExternalBlob(blob, options)
const syncData = proto.SyncdMutations.decode(buffer)
return syncData
}
export const decodeSyncdSnapshot = async(
export const decodeSyncdSnapshot = async (
name: WAPatchName,
snapshot: proto.ISyncdSnapshot,
getAppStateSyncKey: FetchAppStateSyncKey,
@@ -365,34 +377,33 @@ export const decodeSyncdSnapshot = async(
newState.version = toNumber(snapshot.version!.version)
const mutationMap: ChatMutationMap = {}
const areMutationsRequired = typeof minimumVersionNumber === 'undefined'
|| newState.version > minimumVersionNumber
const areMutationsRequired = typeof minimumVersionNumber === 'undefined' || newState.version > minimumVersionNumber
const { hash, indexValueMap } = await decodeSyncdMutations(
snapshot.records!,
newState,
getAppStateSyncKey,
areMutationsRequired
? (mutation) => {
const index = mutation.syncAction.index?.toString()
mutationMap[index!] = mutation
}
: () => { },
? mutation => {
const index = mutation.syncAction.index?.toString()
mutationMap[index!] = mutation
}
: () => {},
validateMacs
)
newState.hash = hash
newState.indexValueMap = indexValueMap
if(validateMacs) {
if (validateMacs) {
const base64Key = Buffer.from(snapshot.keyId!.id!).toString('base64')
const keyEnc = await getAppStateSyncKey(base64Key)
if(!keyEnc) {
if (!keyEnc) {
throw new Boom(`failed to find key "${base64Key}" to decode mutation`)
}
const result = await mutationKeys(keyEnc.keyData!)
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey)
if(Buffer.compare(snapshot.mac!, computedSnapshotMac) !== 0) {
if (Buffer.compare(snapshot.mac!, computedSnapshotMac) !== 0) {
throw new Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`)
}
}
@@ -403,7 +414,7 @@ export const decodeSyncdSnapshot = async(
}
}
export const decodePatches = async(
export const decodePatches = async (
name: WAPatchName,
syncds: proto.ISyncdPatch[],
initial: LTHashState,
@@ -420,9 +431,9 @@ export const decodePatches = async(
const mutationMap: ChatMutationMap = {}
for(const syncd of syncds) {
for (const syncd of syncds) {
const { version, keyId, snapshotMac } = syncd
if(syncd.externalMutations) {
if (syncd.externalMutations) {
logger?.trace({ name, version }, 'downloading external patch')
const ref = await downloadExternalPatch(syncd.externalMutations, options)
logger?.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch')
@@ -441,26 +452,26 @@ export const decodePatches = async(
getAppStateSyncKey,
shouldMutate
? mutation => {
const index = mutation.syncAction.index?.toString()
mutationMap[index!] = mutation
}
: (() => { }),
const index = mutation.syncAction.index?.toString()
mutationMap[index!] = mutation
}
: () => {},
true
)
newState.hash = decodeResult.hash
newState.indexValueMap = decodeResult.indexValueMap
if(validateMacs) {
if (validateMacs) {
const base64Key = Buffer.from(keyId!.id!).toString('base64')
const keyEnc = await getAppStateSyncKey(base64Key)
if(!keyEnc) {
if (!keyEnc) {
throw new Boom(`failed to find key "${base64Key}" to decode mutation`)
}
const result = await mutationKeys(keyEnc.keyData!)
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey)
if(Buffer.compare(snapshotMac!, computedSnapshotMac) !== 0) {
if (Buffer.compare(snapshotMac!, computedSnapshotMac) !== 0) {
throw new Boom(`failed to verify LTHash at ${newState.version} of ${name}`)
}
}
@@ -472,38 +483,35 @@ export const decodePatches = async(
return { state: newState, mutationMap }
}
export const chatModificationToAppPatch = (
mod: ChatModification,
jid: string
) => {
export const chatModificationToAppPatch = (mod: ChatModification, jid: string) => {
const OP = proto.SyncdMutation.SyncdOperation
const getMessageRange = (lastMessages: LastMessageList) => {
let messageRange: proto.SyncActionValue.ISyncActionMessageRange
if(Array.isArray(lastMessages)) {
if (Array.isArray(lastMessages)) {
const lastMsg = lastMessages[lastMessages.length - 1]
messageRange = {
lastMessageTimestamp: lastMsg?.messageTimestamp,
messages: lastMessages?.length ? lastMessages.map(
m => {
if(!m.key?.id || !m.key?.remoteJid) {
throw new Boom('Incomplete key', { statusCode: 400, data: m })
}
messages: lastMessages?.length
? lastMessages.map(m => {
if (!m.key?.id || !m.key?.remoteJid) {
throw new Boom('Incomplete key', { statusCode: 400, data: m })
}
if(isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m })
}
if (isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m })
}
if(!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m })
}
if (!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m })
}
if(m.key.participant) {
m.key.participant = jidNormalizedUser(m.key.participant)
}
if (m.key.participant) {
m.key.participant = jidNormalizedUser(m.key.participant)
}
return m
}
) : undefined
return m
})
: undefined
}
} else {
messageRange = lastMessages
@@ -513,7 +521,7 @@ export const chatModificationToAppPatch = (
}
let patch: WAPatchCreate
if('mute' in mod) {
if ('mute' in mod) {
patch = {
syncAction: {
muteAction: {
@@ -526,7 +534,7 @@ export const chatModificationToAppPatch = (
apiVersion: 2,
operation: OP.SET
}
} else if('archive' in mod) {
} else if ('archive' in mod) {
patch = {
syncAction: {
archiveChatAction: {
@@ -539,7 +547,7 @@ export const chatModificationToAppPatch = (
apiVersion: 3,
operation: OP.SET
}
} else if('markRead' in mod) {
} else if ('markRead' in mod) {
patch = {
syncAction: {
markChatAsReadAction: {
@@ -552,7 +560,7 @@ export const chatModificationToAppPatch = (
apiVersion: 3,
operation: OP.SET
}
} else if('deleteForMe' in mod) {
} else if ('deleteForMe' in mod) {
const { timestamp, key, deleteMedia } = mod.deleteForMe
patch = {
syncAction: {
@@ -566,7 +574,7 @@ export const chatModificationToAppPatch = (
apiVersion: 3,
operation: OP.SET
}
} else if('clear' in mod) {
} else if ('clear' in mod) {
patch = {
syncAction: {
clearChatAction: {} // add message range later
@@ -576,7 +584,7 @@ export const chatModificationToAppPatch = (
apiVersion: 6,
operation: OP.SET
}
} else if('pin' in mod) {
} else if ('pin' in mod) {
patch = {
syncAction: {
pinAction: {
@@ -588,7 +596,7 @@ export const chatModificationToAppPatch = (
apiVersion: 5,
operation: OP.SET
}
} else if('star' in mod) {
} else if ('star' in mod) {
const key = mod.star.messages[0]
patch = {
syncAction: {
@@ -601,11 +609,11 @@ export const chatModificationToAppPatch = (
apiVersion: 2,
operation: OP.SET
}
} else if('delete' in mod) {
} else if ('delete' in mod) {
patch = {
syncAction: {
deleteChatAction: {
messageRange: getMessageRange(mod.lastMessages),
messageRange: getMessageRange(mod.lastMessages)
}
},
index: ['deleteChat', jid, '1'],
@@ -613,7 +621,7 @@ export const chatModificationToAppPatch = (
apiVersion: 6,
operation: OP.SET
}
} else if('pushNameSetting' in mod) {
} else if ('pushNameSetting' in mod) {
patch = {
syncAction: {
pushNameSetting: {
@@ -623,71 +631,64 @@ export const chatModificationToAppPatch = (
index: ['setting_pushName'],
type: 'critical_block',
apiVersion: 1,
operation: OP.SET,
operation: OP.SET
}
} else if('addLabel' in mod) {
} else if ('addLabel' in mod) {
patch = {
syncAction: {
labelEditAction: {
name: mod.addLabel.name,
color: mod.addLabel.color,
predefinedId : mod.addLabel.predefinedId,
predefinedId: mod.addLabel.predefinedId,
deleted: mod.addLabel.deleted
}
},
index: ['label_edit', mod.addLabel.id],
type: 'regular',
apiVersion: 3,
operation: OP.SET,
operation: OP.SET
}
} else if('addChatLabel' in mod) {
} else if ('addChatLabel' in mod) {
patch = {
syncAction: {
labelAssociationAction: {
labeled: true,
labeled: true
}
},
index: [LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
type: 'regular',
apiVersion: 3,
operation: OP.SET,
operation: OP.SET
}
} else if('removeChatLabel' in mod) {
} else if ('removeChatLabel' in mod) {
patch = {
syncAction: {
labelAssociationAction: {
labeled: false,
labeled: false
}
},
index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
type: 'regular',
apiVersion: 3,
operation: OP.SET,
operation: OP.SET
}
} else if('addMessageLabel' in mod) {
} else if ('addMessageLabel' in mod) {
patch = {
syncAction: {
labelAssociationAction: {
labeled: true,
labeled: true
}
},
index: [
LabelAssociationType.Message,
mod.addMessageLabel.labelId,
jid,
mod.addMessageLabel.messageId,
'0',
'0'
],
index: [LabelAssociationType.Message, mod.addMessageLabel.labelId, jid, mod.addMessageLabel.messageId, '0', '0'],
type: 'regular',
apiVersion: 3,
operation: OP.SET,
operation: OP.SET
}
} else if('removeMessageLabel' in mod) {
} else if ('removeMessageLabel' in mod) {
patch = {
syncAction: {
labelAssociationAction: {
labeled: false,
labeled: false
}
},
index: [
@@ -700,7 +701,7 @@ export const chatModificationToAppPatch = (
],
type: 'regular',
apiVersion: 3,
operation: OP.SET,
operation: OP.SET
}
} else {
throw new Boom('not supported')
@@ -716,7 +717,7 @@ export const processSyncAction = (
ev: BaileysEventEmitter,
me: Contact,
initialSyncOpts?: InitialAppStateSyncOptions,
logger?: ILogger,
logger?: ILogger
) => {
const isInitialSync = !!initialSyncOpts
const accountSettings = initialSyncOpts?.accountSettings
@@ -728,20 +729,15 @@ export const processSyncAction = (
index: [type, id, msgId, fromMe]
} = syncAction
if(action?.muteAction) {
ev.emit(
'chats.update',
[
{
id,
muteEndTime: action.muteAction?.muted
? toNumber(action.muteAction.muteEndTimestamp)
: null,
conditional: getChatUpdateConditional(id, undefined)
}
]
)
} else if(action?.archiveChatAction || type === 'archive' || type === 'unarchive') {
if (action?.muteAction) {
ev.emit('chats.update', [
{
id,
muteEndTime: action.muteAction?.muted ? toNumber(action.muteAction.muteEndTimestamp) : null,
conditional: getChatUpdateConditional(id, undefined)
}
])
} else if (action?.archiveChatAction || type === 'archive' || type === 'unarchive') {
// okay so we've to do some annoying computation here
// when we're initially syncing the app state
// there are a few cases we need to handle
@@ -753,9 +749,7 @@ export const processSyncAction = (
// 2. if the account unarchiveChats setting is false -- then it doesn't matter,
// it'll always take an app state action to mark in unarchived -- which we'll get anyway
const archiveAction = action?.archiveChatAction
const isArchived = archiveAction
? archiveAction.archived
: type === 'archive'
const isArchived = archiveAction ? archiveAction.archived : type === 'archive'
// // basically we don't need to fire an "archive" update if the chat is being marked unarchvied
// // this only applies for the initial sync
// if(isInitialSync && !isArchived) {
@@ -765,24 +759,28 @@ export const processSyncAction = (
const msgRange = !accountSettings?.unarchiveChats ? undefined : archiveAction?.messageRange
// logger?.debug({ chat: id, syncAction }, 'message range archive')
ev.emit('chats.update', [{
id,
archived: isArchived,
conditional: getChatUpdateConditional(id, msgRange)
}])
} else if(action?.markChatAsReadAction) {
ev.emit('chats.update', [
{
id,
archived: isArchived,
conditional: getChatUpdateConditional(id, msgRange)
}
])
} else if (action?.markChatAsReadAction) {
const markReadAction = action.markChatAsReadAction
// basically we don't need to fire an "read" update if the chat is being marked as read
// because the chat is read by default
// this only applies for the initial sync
const isNullUpdate = isInitialSync && markReadAction.read
ev.emit('chats.update', [{
id,
unreadCount: isNullUpdate ? null : !!markReadAction?.read ? 0 : -1,
conditional: getChatUpdateConditional(id, markReadAction?.messageRange)
}])
} else if(action?.deleteMessageForMeAction || type === 'deleteMessageForMe') {
ev.emit('chats.update', [
{
id,
unreadCount: isNullUpdate ? null : !!markReadAction?.read ? 0 : -1,
conditional: getChatUpdateConditional(id, markReadAction?.messageRange)
}
])
} else if (action?.deleteMessageForMeAction || type === 'deleteMessageForMe') {
ev.emit('messages.delete', {
keys: [
{
@@ -792,30 +790,32 @@ export const processSyncAction = (
}
]
})
} else if(action?.contactAction) {
} else if (action?.contactAction) {
ev.emit('contacts.upsert', [{ id, name: action.contactAction.fullName! }])
} else if(action?.pushNameSetting) {
} else if (action?.pushNameSetting) {
const name = action?.pushNameSetting?.name
if(name && me?.name !== name) {
if (name && me?.name !== name) {
ev.emit('creds.update', { me: { ...me, name } })
}
} else if(action?.pinAction) {
ev.emit('chats.update', [{
id,
pinned: action.pinAction?.pinned ? toNumber(action.timestamp) : null,
conditional: getChatUpdateConditional(id, undefined)
}])
} else if(action?.unarchiveChatsSetting) {
} else if (action?.pinAction) {
ev.emit('chats.update', [
{
id,
pinned: action.pinAction?.pinned ? toNumber(action.timestamp) : null,
conditional: getChatUpdateConditional(id, undefined)
}
])
} else if (action?.unarchiveChatsSetting) {
const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats
ev.emit('creds.update', { accountSettings: { unarchiveChats } })
logger?.info(`archive setting updated => '${action.unarchiveChatsSetting.unarchiveChats}'`)
if(accountSettings) {
if (accountSettings) {
accountSettings.unarchiveChats = unarchiveChats
}
} else if(action?.starAction || type === 'star') {
} else if (action?.starAction || type === 'star') {
let starred = action?.starAction?.starred
if(typeof starred !== 'boolean') {
if (typeof starred !== 'boolean') {
starred = syncAction.index[syncAction.index.length - 1] === '1'
}
@@ -825,11 +825,11 @@ export const processSyncAction = (
update: { starred }
}
])
} else if(action?.deleteChatAction || type === 'deleteChat') {
if(!isInitialSync) {
} else if (action?.deleteChatAction || type === 'deleteChat') {
if (!isInitialSync) {
ev.emit('chats.delete', [id])
}
} else if(action?.labelEditAction) {
} else if (action?.labelEditAction) {
const { name, color, deleted, predefinedId } = action.labelEditAction
ev.emit('labels.edit', {
@@ -839,42 +839,47 @@ export const processSyncAction = (
deleted: deleted!,
predefinedId: predefinedId ? String(predefinedId) : undefined
})
} else if(action?.labelAssociationAction) {
} else if (action?.labelAssociationAction) {
ev.emit('labels.association', {
type: action.labelAssociationAction.labeled
? 'add'
: 'remove',
association: type === LabelAssociationType.Chat
? {
type: LabelAssociationType.Chat,
chatId: syncAction.index[2],
labelId: syncAction.index[1]
} as ChatLabelAssociation
: {
type: LabelAssociationType.Message,
chatId: syncAction.index[2],
messageId: syncAction.index[3],
labelId: syncAction.index[1]
} as MessageLabelAssociation
type: action.labelAssociationAction.labeled ? 'add' : 'remove',
association:
type === LabelAssociationType.Chat
? ({
type: LabelAssociationType.Chat,
chatId: syncAction.index[2],
labelId: syncAction.index[1]
} as ChatLabelAssociation)
: ({
type: LabelAssociationType.Message,
chatId: syncAction.index[2],
messageId: syncAction.index[3],
labelId: syncAction.index[1]
} as MessageLabelAssociation)
})
} else {
logger?.debug({ syncAction, id }, 'unprocessable update')
}
function getChatUpdateConditional(id: string, msgRange: proto.SyncActionValue.ISyncActionMessageRange | null | undefined): ChatUpdate['conditional'] {
function getChatUpdateConditional(
id: string,
msgRange: proto.SyncActionValue.ISyncActionMessageRange | null | undefined
): ChatUpdate['conditional'] {
return isInitialSync
? (data) => {
const chat = data.historySets.chats[id] || data.chatUpserts[id]
if(chat) {
return msgRange ? isValidPatchBasedOnMessageRange(chat, msgRange) : true
? data => {
const chat = data.historySets.chats[id] || data.chatUpserts[id]
if (chat) {
return msgRange ? isValidPatchBasedOnMessageRange(chat, msgRange) : true
}
}
}
: undefined
}
function isValidPatchBasedOnMessageRange(chat: Chat, msgRange: proto.SyncActionValue.ISyncActionMessageRange | null | undefined) {
const lastMsgTimestamp = Number(msgRange?.lastMessageTimestamp || msgRange?.lastSystemMessageTimestamp || 0)
const chatLastMsgTimestamp = Number(chat?.lastMessageRecvTimestamp || 0)
return lastMsgTimestamp >= chatLastMsgTimestamp
function isValidPatchBasedOnMessageRange(
chat: Chat,
msgRange: proto.SyncActionValue.ISyncActionMessageRange | null | undefined
) {
const lastMsgTimestamp = Number(msgRange?.lastMessageTimestamp || msgRange?.lastSystemMessageTimestamp || 0)
const chatLastMsgTimestamp = Number(chat?.lastMessageRecvTimestamp || 0)
return lastMsgTimestamp >= chatLastMsgTimestamp
}
}

View File

@@ -7,11 +7,8 @@ import { KeyPair } from '../Types'
const { subtle } = globalThis.crypto
/** prefix version byte to the pub keys, required for some curve crypto functions */
export const generateSignalPubKey = (pubKey: Uint8Array | Buffer) => (
pubKey.length === 33
? pubKey
: Buffer.concat([ KEY_BUNDLE_TYPE, pubKey ])
)
export const generateSignalPubKey = (pubKey: Uint8Array | Buffer) =>
pubKey.length === 33 ? pubKey : Buffer.concat([KEY_BUNDLE_TYPE, pubKey])
export const Curve = {
generateKeyPair: (): KeyPair => {
@@ -26,14 +23,12 @@ export const Curve = {
const shared = libsignal.curve.calculateAgreement(generateSignalPubKey(publicKey), privateKey)
return Buffer.from(shared)
},
sign: (privateKey: Uint8Array, buf: Uint8Array) => (
libsignal.curve.calculateSignature(privateKey, buf)
),
sign: (privateKey: Uint8Array, buf: Uint8Array) => libsignal.curve.calculateSignature(privateKey, buf),
verify: (pubKey: Uint8Array, message: Uint8Array, signature: Uint8Array) => {
try {
libsignal.curve.verifySignature(generateSignalPubKey(pubKey), message, signature)
return true
} catch(error) {
} catch (error) {
return false
}
}
@@ -73,7 +68,7 @@ export function aesDecryptGCM(ciphertext: Uint8Array, key: Uint8Array, iv: Uint8
decipher.setAAD(additionalData)
decipher.setAuthTag(tag)
return Buffer.concat([ decipher.update(enc), decipher.final() ])
return Buffer.concat([decipher.update(enc), decipher.final()])
}
export function aesEncryptCTR(plaintext: Uint8Array, key: Uint8Array, iv: Uint8Array) {
@@ -111,7 +106,11 @@ export function aesEncrypWithIV(buffer: Buffer, key: Buffer, IV: Buffer) {
}
// sign HMAC using SHA 256
export function hmacSign(buffer: Buffer | Uint8Array, key: Buffer | Uint8Array, variant: 'sha256' | 'sha512' = 'sha256') {
export function hmacSign(
buffer: Buffer | Uint8Array,
key: Buffer | Uint8Array,
variant: 'sha256' | 'sha512' = 'sha256'
) {
return createHmac(variant, key).update(buffer).digest()
}
@@ -127,27 +126,17 @@ export function md5(buffer: Buffer) {
export async function hkdf(
buffer: Uint8Array | Buffer,
expandedLength: number,
info: { salt?: Buffer, info?: string }
info: { salt?: Buffer; info?: string }
): Promise<Buffer> {
// Ensure we have a Uint8Array for the key material
const inputKeyMaterial = buffer instanceof Uint8Array
? buffer
: new Uint8Array(buffer)
const inputKeyMaterial = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer)
// Set default values if not provided
const salt = info.salt ? new Uint8Array(info.salt) : new Uint8Array(0)
const infoBytes = info.info
? new TextEncoder().encode(info.info)
: new Uint8Array(0)
const infoBytes = info.info ? new TextEncoder().encode(info.info) : new Uint8Array(0)
// Import the input key material
const importedKey = await subtle.importKey(
'raw',
inputKeyMaterial,
{ name: 'HKDF' },
false,
['deriveBits']
)
const importedKey = await subtle.importKey('raw', inputKeyMaterial, { name: 'HKDF' }, false, ['deriveBits'])
// Derive bits using HKDF
const derivedBits = await subtle.deriveBits(
@@ -164,7 +153,6 @@ export async function hkdf(
return Buffer.from(derivedBits)
}
export async function derivePairingCodeKey(pairingCode: string, salt: Buffer): Promise<Buffer> {
// Convert inputs to formats Web Crypto API can work with
const encoder = new TextEncoder()
@@ -172,13 +160,7 @@ export async function derivePairingCodeKey(pairingCode: string, salt: Buffer): P
const saltBuffer = salt instanceof Uint8Array ? salt : new Uint8Array(salt)
// Import the pairing code as key material
const keyMaterial = await subtle.importKey(
'raw',
pairingCodeBuffer,
{ name: 'PBKDF2' },
false,
['deriveBits']
)
const keyMaterial = await subtle.importKey('raw', pairingCodeBuffer, { name: 'PBKDF2' }, false, ['deriveBits'])
// Derive bits using PBKDF2 with the same parameters
// 2 << 16 = 131,072 iterations

View File

@@ -1,7 +1,17 @@
import { Boom } from '@hapi/boom'
import { proto } from '../../WAProto'
import { SignalRepository, WAMessageKey } from '../Types'
import { areJidsSameUser, BinaryNode, isJidBroadcast, isJidGroup, isJidMetaIa, isJidNewsletter, isJidStatusBroadcast, isJidUser, isLidUser } from '../WABinary'
import {
areJidsSameUser,
BinaryNode,
isJidBroadcast,
isJidGroup,
isJidMetaIa,
isJidNewsletter,
isJidStatusBroadcast,
isJidUser,
isLidUser
} from '../WABinary'
import { unpadRandomMax16 } from './generics'
import { ILogger } from './logger'
@@ -24,17 +34,20 @@ export const NACK_REASONS = {
DBOperationFailed: 552
}
type MessageType = 'chat' | 'peer_broadcast' | 'other_broadcast' | 'group' | 'direct_peer_status' | 'other_status' | 'newsletter'
type MessageType =
| 'chat'
| 'peer_broadcast'
| 'other_broadcast'
| 'group'
| 'direct_peer_status'
| 'other_status'
| 'newsletter'
/**
* Decode the received node as a message.
* @note this will only parse the message, not decrypt it
*/
export function decodeMessageNode(
stanza: BinaryNode,
meId: string,
meLid: string
) {
export function decodeMessageNode(stanza: BinaryNode, meId: string, meLid: string) {
let msgType: MessageType
let chatId: string
let author: string
@@ -47,9 +60,9 @@ export function decodeMessageNode(
const isMe = (jid: string) => areJidsSameUser(jid, meId)
const isMeLid = (jid: string) => areJidsSameUser(jid, meLid)
if(isJidUser(from) || isLidUser(from)) {
if(recipient && !isJidMetaIa(recipient)) {
if(!isMe(from) && !isMeLid(from)) {
if (isJidUser(from) || isLidUser(from)) {
if (recipient && !isJidMetaIa(recipient)) {
if (!isMe(from) && !isMeLid(from)) {
throw new Boom('receipient present, but msg not from me', { data: stanza })
}
@@ -60,21 +73,21 @@ export function decodeMessageNode(
msgType = 'chat'
author = from
} else if(isJidGroup(from)) {
if(!participant) {
} else if (isJidGroup(from)) {
if (!participant) {
throw new Boom('No participant in group message')
}
msgType = 'group'
author = participant
chatId = from
} else if(isJidBroadcast(from)) {
if(!participant) {
} else if (isJidBroadcast(from)) {
if (!participant) {
throw new Boom('No participant in group message')
}
const isParticipantMe = isMe(participant)
if(isJidStatusBroadcast(from)) {
if (isJidStatusBroadcast(from)) {
msgType = isParticipantMe ? 'direct_peer_status' : 'other_status'
} else {
msgType = isParticipantMe ? 'peer_broadcast' : 'other_broadcast'
@@ -82,7 +95,7 @@ export function decodeMessageNode(
chatId = from
author = participant
} else if(isJidNewsletter(from)) {
} else if (isJidNewsletter(from)) {
msgType = 'newsletter'
chatId = from
author = from
@@ -107,7 +120,7 @@ export function decodeMessageNode(
broadcast: isJidBroadcast(from)
}
if(key.fromMe) {
if (key.fromMe) {
fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK
}
@@ -132,19 +145,19 @@ export const decryptMessageNode = (
author,
async decrypt() {
let decryptables = 0
if(Array.isArray(stanza.content)) {
for(const { tag, attrs, content } of stanza.content) {
if(tag === 'verified_name' && content instanceof Uint8Array) {
if (Array.isArray(stanza.content)) {
for (const { tag, attrs, content } of stanza.content) {
if (tag === 'verified_name' && content instanceof Uint8Array) {
const cert = proto.VerifiedNameCertificate.decode(content)
const details = proto.VerifiedNameCertificate.Details.decode(cert.details!)
fullMessage.verifiedBizName = details.verifiedName
}
if(tag !== 'enc' && tag !== 'plaintext') {
if (tag !== 'enc' && tag !== 'plaintext') {
continue
}
if(!(content instanceof Uint8Array)) {
if (!(content instanceof Uint8Array)) {
continue
}
@@ -155,53 +168,52 @@ export const decryptMessageNode = (
try {
const e2eType = tag === 'plaintext' ? 'plaintext' : attrs.type
switch (e2eType) {
case 'skmsg':
msgBuffer = await repository.decryptGroupMessage({
group: sender,
authorJid: author,
msg: content
})
break
case 'pkmsg':
case 'msg':
const user = isJidUser(sender) ? sender : author
msgBuffer = await repository.decryptMessage({
jid: user,
type: e2eType,
ciphertext: content
})
break
case 'plaintext':
msgBuffer = content
break
default:
throw new Error(`Unknown e2e type: ${e2eType}`)
case 'skmsg':
msgBuffer = await repository.decryptGroupMessage({
group: sender,
authorJid: author,
msg: content
})
break
case 'pkmsg':
case 'msg':
const user = isJidUser(sender) ? sender : author
msgBuffer = await repository.decryptMessage({
jid: user,
type: e2eType,
ciphertext: content
})
break
case 'plaintext':
msgBuffer = content
break
default:
throw new Error(`Unknown e2e type: ${e2eType}`)
}
let msg: proto.IMessage = proto.Message.decode(e2eType !== 'plaintext' ? unpadRandomMax16(msgBuffer) : msgBuffer)
let msg: proto.IMessage = proto.Message.decode(
e2eType !== 'plaintext' ? unpadRandomMax16(msgBuffer) : msgBuffer
)
msg = msg.deviceSentMessage?.message || msg
if(msg.senderKeyDistributionMessage) {
if (msg.senderKeyDistributionMessage) {
//eslint-disable-next-line max-depth
try {
try {
await repository.processSenderKeyDistributionMessage({
authorJid: author,
item: msg.senderKeyDistributionMessage
})
} catch(err) {
} catch (err) {
logger.error({ key: fullMessage.key, err }, 'failed to decrypt message')
}
}
}
if(fullMessage.message) {
if (fullMessage.message) {
Object.assign(fullMessage.message, msg)
} else {
fullMessage.message = msg
}
} catch(err) {
logger.error(
{ key: fullMessage.key, err },
'failed to decrypt message'
)
} catch (err) {
logger.error({ key: fullMessage.key, err }, 'failed to decrypt message')
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
fullMessage.messageStubParameters = [err.message]
}
@@ -209,7 +221,7 @@ export const decryptMessageNode = (
}
// if nothing was found to decrypt
if(!decryptables) {
if (!decryptables) {
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT]
}

View File

@@ -1,6 +1,16 @@
import EventEmitter from 'events'
import { proto } from '../../WAProto'
import { BaileysEvent, BaileysEventEmitter, BaileysEventMap, BufferedEventData, Chat, ChatUpdate, Contact, WAMessage, WAMessageStatus } from '../Types'
import {
BaileysEvent,
BaileysEventEmitter,
BaileysEventMap,
BufferedEventData,
Chat,
ChatUpdate,
Contact,
WAMessage,
WAMessageStatus
} from '../Types'
import { trimUndefined } from './generics'
import { ILogger } from './logger'
import { updateMessageWithReaction, updateMessageWithReceipt } from './messages'
@@ -18,10 +28,10 @@ const BUFFERABLE_EVENT = [
'messages.delete',
'messages.reaction',
'message-receipt.update',
'groups.update',
'groups.update'
] as const
type BufferableEvent = typeof BUFFERABLE_EVENT[number]
type BufferableEvent = (typeof BUFFERABLE_EVENT)[number]
/**
* A map that contains a list of all events that have been triggered
@@ -36,14 +46,14 @@ const BUFFERABLE_EVENT_SET = new Set<BaileysEvent>(BUFFERABLE_EVENT)
type BaileysBufferableEventEmitter = BaileysEventEmitter & {
/** Use to process events in a batch */
process(handler: (events: BaileysEventData) => void | Promise<void>): (() => void)
process(handler: (events: BaileysEventData) => void | Promise<void>): () => void
/**
* starts buffering events, call flush() to release them
* */
buffer(): void
/** buffers all events till the promise completes */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
createBufferedFunction<A extends any[], T>(work: (...args: A) => Promise<T>): ((...args: A) => Promise<T>)
createBufferedFunction<A extends any[], T>(work: (...args: A) => Promise<T>): (...args: A) => Promise<T>
/**
* flushes all buffered events
* @param force if true, will flush all data regardless of any pending buffers
@@ -68,7 +78,7 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter
// take the generic event and fire it as a baileys event
ev.on('event', (map: BaileysEventData) => {
for(const event in map) {
for (const event in map) {
ev.emit(event, map[event])
}
})
@@ -79,16 +89,16 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter
function flush(force = false) {
// no buffer going on
if(!buffersInProgress) {
if (!buffersInProgress) {
return false
}
if(!force) {
if (!force) {
// reduce the number of buffers in progress
buffersInProgress -= 1
// if there are still some buffers going on
// then we don't flush now
if(buffersInProgress) {
if (buffersInProgress) {
return false
}
}
@@ -97,8 +107,8 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter
const chatUpdates = Object.values(data.chatUpdates)
// gather the remaining conditional events so we re-queue them
let conditionalChatUpdatesLeft = 0
for(const update of chatUpdates) {
if(update.conditional) {
for (const update of chatUpdates) {
if (update.conditional) {
conditionalChatUpdatesLeft += 1
newData.chatUpdates[update.id!] = update
delete data.chatUpdates[update.id!]
@@ -106,16 +116,13 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter
}
const consolidatedData = consolidateEvents(data)
if(Object.keys(consolidatedData).length) {
if (Object.keys(consolidatedData).length) {
ev.emit('event', consolidatedData)
}
data = newData
logger.trace(
{ conditionalChatUpdatesLeft },
'released buffered events'
)
logger.trace({ conditionalChatUpdatesLeft }, 'released buffered events')
return true
}
@@ -132,7 +139,7 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter
}
},
emit<T extends BaileysEvent>(event: BaileysEvent, evData: BaileysEventMap[T]) {
if(buffersInProgress && BUFFERABLE_EVENT_SET.has(event)) {
if (buffersInProgress && BUFFERABLE_EVENT_SET.has(event)) {
append(data, historyCache, event as BufferableEvent, evData, logger)
return true
}
@@ -145,7 +152,7 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter
buffer,
flush,
createBufferedFunction(work) {
return async(...args) => {
return async (...args) => {
buffer()
try {
const result = await work(...args)
@@ -157,30 +164,30 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter
},
on: (...args) => ev.on(...args),
off: (...args) => ev.off(...args),
removeAllListeners: (...args) => ev.removeAllListeners(...args),
removeAllListeners: (...args) => ev.removeAllListeners(...args)
}
}
const makeBufferData = (): BufferedEventData => {
return {
historySets: {
chats: { },
messages: { },
contacts: { },
chats: {},
messages: {},
contacts: {},
isLatest: false,
empty: true
},
chatUpserts: { },
chatUpdates: { },
chatUpserts: {},
chatUpdates: {},
chatDeletes: new Set(),
contactUpserts: { },
contactUpdates: { },
messageUpserts: { },
messageUpdates: { },
messageReactions: { },
messageDeletes: { },
messageReceipts: { },
groupUpdates: { }
contactUpserts: {},
contactUpdates: {},
messageUpserts: {},
messageUpdates: {},
messageReactions: {},
messageDeletes: {},
messageReceipts: {},
groupUpdates: {}
}
}
@@ -193,305 +200,298 @@ function append<E extends BufferableEvent>(
logger: ILogger
) {
switch (event) {
case 'messaging-history.set':
for(const chat of eventData.chats as Chat[]) {
const existingChat = data.historySets.chats[chat.id]
if(existingChat) {
existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType
}
if(!existingChat && !historyCache.has(chat.id)) {
data.historySets.chats[chat.id] = chat
historyCache.add(chat.id)
absorbingChatUpdate(chat)
}
}
for(const contact of eventData.contacts as Contact[]) {
const existingContact = data.historySets.contacts[contact.id]
if(existingContact) {
Object.assign(existingContact, trimUndefined(contact))
} else {
const historyContactId = `c:${contact.id}`
const hasAnyName = contact.notify || contact.name || contact.verifiedName
if(!historyCache.has(historyContactId) || hasAnyName) {
data.historySets.contacts[contact.id] = contact
historyCache.add(historyContactId)
case 'messaging-history.set':
for (const chat of eventData.chats as Chat[]) {
const existingChat = data.historySets.chats[chat.id]
if (existingChat) {
existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType
}
}
}
for(const message of eventData.messages as WAMessage[]) {
const key = stringifyMessageKey(message.key)
const existingMsg = data.historySets.messages[key]
if(!existingMsg && !historyCache.has(key)) {
data.historySets.messages[key] = message
historyCache.add(key)
}
}
if (!existingChat && !historyCache.has(chat.id)) {
data.historySets.chats[chat.id] = chat
historyCache.add(chat.id)
data.historySets.empty = false
data.historySets.syncType = eventData.syncType
data.historySets.progress = eventData.progress
data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId
data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest
break
case 'chats.upsert':
for(const chat of eventData as Chat[]) {
let upsert = data.chatUpserts[chat.id]
if(!upsert) {
upsert = data.historySets[chat.id]
if(upsert) {
logger.debug({ chatId: chat.id }, 'absorbed chat upsert in chat set')
absorbingChatUpdate(chat)
}
}
if(upsert) {
upsert = concatChats(upsert, chat)
} else {
upsert = chat
data.chatUpserts[chat.id] = upsert
}
absorbingChatUpdate(upsert)
if(data.chatDeletes.has(chat.id)) {
data.chatDeletes.delete(chat.id)
}
}
break
case 'chats.update':
for(const update of eventData as ChatUpdate[]) {
const chatId = update.id!
const conditionMatches = update.conditional ? update.conditional(data) : true
if(conditionMatches) {
delete update.conditional
// if there is an existing upsert, merge the update into it
const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId]
if(upsert) {
concatChats(upsert, update)
for (const contact of eventData.contacts as Contact[]) {
const existingContact = data.historySets.contacts[contact.id]
if (existingContact) {
Object.assign(existingContact, trimUndefined(contact))
} else {
// merge the update into the existing update
const chatUpdate = data.chatUpdates[chatId] || { }
data.chatUpdates[chatId] = concatChats(chatUpdate, update)
}
} else if(conditionMatches === undefined) {
// condition yet to be fulfilled
data.chatUpdates[chatId] = update
}
// otherwise -- condition not met, update is invalid
// if the chat has been updated
// ignore any existing chat delete
if(data.chatDeletes.has(chatId)) {
data.chatDeletes.delete(chatId)
}
}
break
case 'chats.delete':
for(const chatId of eventData as string[]) {
if(!data.chatDeletes.has(chatId)) {
data.chatDeletes.add(chatId)
}
// remove any prior updates & upserts
if(data.chatUpdates[chatId]) {
delete data.chatUpdates[chatId]
}
if(data.chatUpserts[chatId]) {
delete data.chatUpserts[chatId]
}
if(data.historySets.chats[chatId]) {
delete data.historySets.chats[chatId]
}
}
break
case 'contacts.upsert':
for(const contact of eventData as Contact[]) {
let upsert = data.contactUpserts[contact.id]
if(!upsert) {
upsert = data.historySets.contacts[contact.id]
if(upsert) {
logger.debug({ contactId: contact.id }, 'absorbed contact upsert in contact set')
const historyContactId = `c:${contact.id}`
const hasAnyName = contact.notify || contact.name || contact.verifiedName
if (!historyCache.has(historyContactId) || hasAnyName) {
data.historySets.contacts[contact.id] = contact
historyCache.add(historyContactId)
}
}
}
if(upsert) {
upsert = Object.assign(upsert, trimUndefined(contact))
} else {
upsert = contact
data.contactUpserts[contact.id] = upsert
}
if(data.contactUpdates[contact.id]) {
upsert = Object.assign(data.contactUpdates[contact.id], trimUndefined(contact)) as Contact
delete data.contactUpdates[contact.id]
}
}
break
case 'contacts.update':
const contactUpdates = eventData as BaileysEventMap['contacts.update']
for(const update of contactUpdates) {
const id = update.id!
// merge into prior upsert
const upsert = data.historySets.contacts[id] || data.contactUpserts[id]
if(upsert) {
Object.assign(upsert, update)
} else {
// merge into prior update
const contactUpdate = data.contactUpdates[id] || { }
data.contactUpdates[id] = Object.assign(contactUpdate, update)
}
}
break
case 'messages.upsert':
const { messages, type } = eventData as BaileysEventMap['messages.upsert']
for(const message of messages) {
const key = stringifyMessageKey(message.key)
let existing = data.messageUpserts[key]?.message
if(!existing) {
existing = data.historySets.messages[key]
if(existing) {
logger.debug({ messageId: key }, 'absorbed message upsert in message set')
for (const message of eventData.messages as WAMessage[]) {
const key = stringifyMessageKey(message.key)
const existingMsg = data.historySets.messages[key]
if (!existingMsg && !historyCache.has(key)) {
data.historySets.messages[key] = message
historyCache.add(key)
}
}
if(existing) {
message.messageTimestamp = existing.messageTimestamp
}
data.historySets.empty = false
data.historySets.syncType = eventData.syncType
data.historySets.progress = eventData.progress
data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId
data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest
if(data.messageUpdates[key]) {
logger.debug('absorbed prior message update in message upsert')
Object.assign(message, data.messageUpdates[key].update)
delete data.messageUpdates[key]
}
break
case 'chats.upsert':
for (const chat of eventData as Chat[]) {
let upsert = data.chatUpserts[chat.id]
if (!upsert) {
upsert = data.historySets[chat.id]
if (upsert) {
logger.debug({ chatId: chat.id }, 'absorbed chat upsert in chat set')
}
}
if(data.historySets.messages[key]) {
data.historySets.messages[key] = message
} else {
data.messageUpserts[key] = {
message,
type: type === 'notify' || data.messageUpserts[key]?.type === 'notify'
? 'notify'
: type
if (upsert) {
upsert = concatChats(upsert, chat)
} else {
upsert = chat
data.chatUpserts[chat.id] = upsert
}
absorbingChatUpdate(upsert)
if (data.chatDeletes.has(chat.id)) {
data.chatDeletes.delete(chat.id)
}
}
}
break
case 'messages.update':
const msgUpdates = eventData as BaileysEventMap['messages.update']
for(const { key, update } of msgUpdates) {
const keyStr = stringifyMessageKey(key)
const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message
if(existing) {
Object.assign(existing, update)
// if the message was received & read by us
// the chat counter must have been incremented
// so we need to decrement it
if(update.status === WAMessageStatus.READ && !key.fromMe) {
decrementChatReadCounterIfMsgDidUnread(existing)
break
case 'chats.update':
for (const update of eventData as ChatUpdate[]) {
const chatId = update.id!
const conditionMatches = update.conditional ? update.conditional(data) : true
if (conditionMatches) {
delete update.conditional
// if there is an existing upsert, merge the update into it
const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId]
if (upsert) {
concatChats(upsert, update)
} else {
// merge the update into the existing update
const chatUpdate = data.chatUpdates[chatId] || {}
data.chatUpdates[chatId] = concatChats(chatUpdate, update)
}
} else if (conditionMatches === undefined) {
// condition yet to be fulfilled
data.chatUpdates[chatId] = update
}
} else {
const msgUpdate = data.messageUpdates[keyStr] || { key, update: { } }
Object.assign(msgUpdate.update, update)
data.messageUpdates[keyStr] = msgUpdate
}
}
// otherwise -- condition not met, update is invalid
break
case 'messages.delete':
const deleteData = eventData as BaileysEventMap['messages.delete']
if('keys' in deleteData) {
const { keys } = deleteData
for(const key of keys) {
// if the chat has been updated
// ignore any existing chat delete
if (data.chatDeletes.has(chatId)) {
data.chatDeletes.delete(chatId)
}
}
break
case 'chats.delete':
for (const chatId of eventData as string[]) {
if (!data.chatDeletes.has(chatId)) {
data.chatDeletes.add(chatId)
}
// remove any prior updates & upserts
if (data.chatUpdates[chatId]) {
delete data.chatUpdates[chatId]
}
if (data.chatUpserts[chatId]) {
delete data.chatUpserts[chatId]
}
if (data.historySets.chats[chatId]) {
delete data.historySets.chats[chatId]
}
}
break
case 'contacts.upsert':
for (const contact of eventData as Contact[]) {
let upsert = data.contactUpserts[contact.id]
if (!upsert) {
upsert = data.historySets.contacts[contact.id]
if (upsert) {
logger.debug({ contactId: contact.id }, 'absorbed contact upsert in contact set')
}
}
if (upsert) {
upsert = Object.assign(upsert, trimUndefined(contact))
} else {
upsert = contact
data.contactUpserts[contact.id] = upsert
}
if (data.contactUpdates[contact.id]) {
upsert = Object.assign(data.contactUpdates[contact.id], trimUndefined(contact)) as Contact
delete data.contactUpdates[contact.id]
}
}
break
case 'contacts.update':
const contactUpdates = eventData as BaileysEventMap['contacts.update']
for (const update of contactUpdates) {
const id = update.id!
// merge into prior upsert
const upsert = data.historySets.contacts[id] || data.contactUpserts[id]
if (upsert) {
Object.assign(upsert, update)
} else {
// merge into prior update
const contactUpdate = data.contactUpdates[id] || {}
data.contactUpdates[id] = Object.assign(contactUpdate, update)
}
}
break
case 'messages.upsert':
const { messages, type } = eventData as BaileysEventMap['messages.upsert']
for (const message of messages) {
const key = stringifyMessageKey(message.key)
let existing = data.messageUpserts[key]?.message
if (!existing) {
existing = data.historySets.messages[key]
if (existing) {
logger.debug({ messageId: key }, 'absorbed message upsert in message set')
}
}
if (existing) {
message.messageTimestamp = existing.messageTimestamp
}
if (data.messageUpdates[key]) {
logger.debug('absorbed prior message update in message upsert')
Object.assign(message, data.messageUpdates[key].update)
delete data.messageUpdates[key]
}
if (data.historySets.messages[key]) {
data.historySets.messages[key] = message
} else {
data.messageUpserts[key] = {
message,
type: type === 'notify' || data.messageUpserts[key]?.type === 'notify' ? 'notify' : type
}
}
}
break
case 'messages.update':
const msgUpdates = eventData as BaileysEventMap['messages.update']
for (const { key, update } of msgUpdates) {
const keyStr = stringifyMessageKey(key)
if(!data.messageDeletes[keyStr]) {
data.messageDeletes[keyStr] = key
}
if(data.messageUpserts[keyStr]) {
delete data.messageUpserts[keyStr]
}
if(data.messageUpdates[keyStr]) {
delete data.messageUpdates[keyStr]
const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message
if (existing) {
Object.assign(existing, update)
// if the message was received & read by us
// the chat counter must have been incremented
// so we need to decrement it
if (update.status === WAMessageStatus.READ && !key.fromMe) {
decrementChatReadCounterIfMsgDidUnread(existing)
}
} else {
const msgUpdate = data.messageUpdates[keyStr] || { key, update: {} }
Object.assign(msgUpdate.update, update)
data.messageUpdates[keyStr] = msgUpdate
}
}
} else {
// TODO: add support
}
break
case 'messages.reaction':
const reactions = eventData as BaileysEventMap['messages.reaction']
for(const { key, reaction } of reactions) {
const keyStr = stringifyMessageKey(key)
const existing = data.messageUpserts[keyStr]
if(existing) {
updateMessageWithReaction(existing.message, reaction)
break
case 'messages.delete':
const deleteData = eventData as BaileysEventMap['messages.delete']
if ('keys' in deleteData) {
const { keys } = deleteData
for (const key of keys) {
const keyStr = stringifyMessageKey(key)
if (!data.messageDeletes[keyStr]) {
data.messageDeletes[keyStr] = key
}
if (data.messageUpserts[keyStr]) {
delete data.messageUpserts[keyStr]
}
if (data.messageUpdates[keyStr]) {
delete data.messageUpdates[keyStr]
}
}
} else {
data.messageReactions[keyStr] = data.messageReactions[keyStr]
|| { key, reactions: [] }
updateMessageWithReaction(data.messageReactions[keyStr], reaction)
// TODO: add support
}
}
break
case 'message-receipt.update':
const receipts = eventData as BaileysEventMap['message-receipt.update']
for(const { key, receipt } of receipts) {
const keyStr = stringifyMessageKey(key)
const existing = data.messageUpserts[keyStr]
if(existing) {
updateMessageWithReceipt(existing.message, receipt)
} else {
data.messageReceipts[keyStr] = data.messageReceipts[keyStr]
|| { key, userReceipt: [] }
updateMessageWithReceipt(data.messageReceipts[keyStr], receipt)
break
case 'messages.reaction':
const reactions = eventData as BaileysEventMap['messages.reaction']
for (const { key, reaction } of reactions) {
const keyStr = stringifyMessageKey(key)
const existing = data.messageUpserts[keyStr]
if (existing) {
updateMessageWithReaction(existing.message, reaction)
} else {
data.messageReactions[keyStr] = data.messageReactions[keyStr] || { key, reactions: [] }
updateMessageWithReaction(data.messageReactions[keyStr], reaction)
}
}
}
break
case 'groups.update':
const groupUpdates = eventData as BaileysEventMap['groups.update']
for(const update of groupUpdates) {
const id = update.id!
const groupUpdate = data.groupUpdates[id] || { }
if(!data.groupUpdates[id]) {
data.groupUpdates[id] = Object.assign(groupUpdate, update)
break
case 'message-receipt.update':
const receipts = eventData as BaileysEventMap['message-receipt.update']
for (const { key, receipt } of receipts) {
const keyStr = stringifyMessageKey(key)
const existing = data.messageUpserts[keyStr]
if (existing) {
updateMessageWithReceipt(existing.message, receipt)
} else {
data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || { key, userReceipt: [] }
updateMessageWithReceipt(data.messageReceipts[keyStr], receipt)
}
}
}
break
default:
throw new Error(`"${event}" cannot be buffered`)
break
case 'groups.update':
const groupUpdates = eventData as BaileysEventMap['groups.update']
for (const update of groupUpdates) {
const id = update.id!
const groupUpdate = data.groupUpdates[id] || {}
if (!data.groupUpdates[id]) {
data.groupUpdates[id] = Object.assign(groupUpdate, update)
}
}
break
default:
throw new Error(`"${event}" cannot be buffered`)
}
function absorbingChatUpdate(existing: Chat) {
const chatId = existing.id
const update = data.chatUpdates[chatId]
if(update) {
if (update) {
const conditionMatches = update.conditional ? update.conditional(data) : true
if(conditionMatches) {
if (conditionMatches) {
delete update.conditional
logger.debug({ chatId }, 'absorbed chat update in existing chat')
Object.assign(existing, concatChats(update as Chat, existing))
delete data.chatUpdates[chatId]
} else if(conditionMatches === false) {
} else if (conditionMatches === false) {
logger.debug({ chatId }, 'chat update condition fail, removing')
delete data.chatUpdates[chatId]
}
@@ -503,15 +503,15 @@ function append<E extends BufferableEvent>(
// if the message has already been marked read by us
const chatId = message.key.remoteJid!
const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId]
if(
isRealMessage(message, '')
&& shouldIncrementChatUnread(message)
&& typeof chat?.unreadCount === 'number'
&& chat.unreadCount > 0
if (
isRealMessage(message, '') &&
shouldIncrementChatUnread(message) &&
typeof chat?.unreadCount === 'number' &&
chat.unreadCount > 0
) {
logger.debug({ chatId: chat.id }, 'decrementing chat counter')
chat.unreadCount -= 1
if(chat.unreadCount === 0) {
if (chat.unreadCount === 0) {
delete chat.unreadCount
}
}
@@ -519,9 +519,9 @@ function append<E extends BufferableEvent>(
}
function consolidateEvents(data: BufferedEventData) {
const map: BaileysEventData = { }
const map: BaileysEventData = {}
if(!data.historySets.empty) {
if (!data.historySets.empty) {
map['messaging-history.set'] = {
chats: Object.values(data.historySets.chats),
messages: Object.values(data.historySets.messages),
@@ -534,22 +534,22 @@ function consolidateEvents(data: BufferedEventData) {
}
const chatUpsertList = Object.values(data.chatUpserts)
if(chatUpsertList.length) {
if (chatUpsertList.length) {
map['chats.upsert'] = chatUpsertList
}
const chatUpdateList = Object.values(data.chatUpdates)
if(chatUpdateList.length) {
if (chatUpdateList.length) {
map['chats.update'] = chatUpdateList
}
const chatDeleteList = Array.from(data.chatDeletes)
if(chatDeleteList.length) {
if (chatDeleteList.length) {
map['chats.delete'] = chatDeleteList
}
const messageUpsertList = Object.values(data.messageUpserts)
if(messageUpsertList.length) {
if (messageUpsertList.length) {
const type = messageUpsertList[0].type
map['messages.upsert'] = {
messages: messageUpsertList.map(m => m.message),
@@ -558,41 +558,41 @@ function consolidateEvents(data: BufferedEventData) {
}
const messageUpdateList = Object.values(data.messageUpdates)
if(messageUpdateList.length) {
if (messageUpdateList.length) {
map['messages.update'] = messageUpdateList
}
const messageDeleteList = Object.values(data.messageDeletes)
if(messageDeleteList.length) {
if (messageDeleteList.length) {
map['messages.delete'] = { keys: messageDeleteList }
}
const messageReactionList = Object.values(data.messageReactions).flatMap(
({ key, reactions }) => reactions.flatMap(reaction => ({ key, reaction }))
const messageReactionList = Object.values(data.messageReactions).flatMap(({ key, reactions }) =>
reactions.flatMap(reaction => ({ key, reaction }))
)
if(messageReactionList.length) {
if (messageReactionList.length) {
map['messages.reaction'] = messageReactionList
}
const messageReceiptList = Object.values(data.messageReceipts).flatMap(
({ key, userReceipt }) => userReceipt.flatMap(receipt => ({ key, receipt }))
const messageReceiptList = Object.values(data.messageReceipts).flatMap(({ key, userReceipt }) =>
userReceipt.flatMap(receipt => ({ key, receipt }))
)
if(messageReceiptList.length) {
if (messageReceiptList.length) {
map['message-receipt.update'] = messageReceiptList
}
const contactUpsertList = Object.values(data.contactUpserts)
if(contactUpsertList.length) {
if (contactUpsertList.length) {
map['contacts.upsert'] = contactUpsertList
}
const contactUpdateList = Object.values(data.contactUpdates)
if(contactUpdateList.length) {
if (contactUpdateList.length) {
map['contacts.update'] = contactUpdateList
}
const groupUpdateList = Object.values(data.groupUpdates)
if(groupUpdateList.length) {
if (groupUpdateList.length) {
map['groups.update'] = groupUpdateList
}
@@ -600,15 +600,17 @@ function consolidateEvents(data: BufferedEventData) {
}
function concatChats<C extends Partial<Chat>>(a: C, b: Partial<Chat>) {
if(b.unreadCount === null && // neutralize unread counter
a.unreadCount! < 0) {
if (
b.unreadCount === null && // neutralize unread counter
a.unreadCount! < 0
) {
a.unreadCount = undefined
b.unreadCount = undefined
}
if(typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
if (typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
b = { ...b }
if(b.unreadCount! >= 0) {
if (b.unreadCount! >= 0) {
b.unreadCount = Math.max(b.unreadCount!, 0) + Math.max(a.unreadCount, 0)
}
}
@@ -616,4 +618,4 @@ function concatChats<C extends Partial<Chat>>(a: C, b: Partial<Chat>) {
return Object.assign(a, b)
}
const stringifyMessageKey = (key: proto.IMessageKey) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`
const stringifyMessageKey = (key: proto.IMessageKey) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`

View File

@@ -4,26 +4,34 @@ import { createHash, randomBytes } from 'crypto'
import { platform, release } from 'os'
import { proto } from '../../WAProto'
import { version as baileysVersion } from '../Defaults/baileys-version.json'
import { BaileysEventEmitter, BaileysEventMap, BrowsersMap, ConnectionState, DisconnectReason, WACallUpdateType, WAVersion } from '../Types'
import {
BaileysEventEmitter,
BaileysEventMap,
BrowsersMap,
ConnectionState,
DisconnectReason,
WACallUpdateType,
WAVersion
} from '../Types'
import { BinaryNode, getAllBinaryNodeChildren, jidDecode } from '../WABinary'
const PLATFORM_MAP = {
'aix': 'AIX',
'darwin': 'Mac OS',
'win32': 'Windows',
'android': 'Android',
'freebsd': 'FreeBSD',
'openbsd': 'OpenBSD',
'sunos': 'Solaris'
aix: 'AIX',
darwin: 'Mac OS',
win32: 'Windows',
android: 'Android',
freebsd: 'FreeBSD',
openbsd: 'OpenBSD',
sunos: 'Solaris'
}
export const Browsers: BrowsersMap = {
ubuntu: (browser) => ['Ubuntu', browser, '22.04.4'],
macOS: (browser) => ['Mac OS', browser, '14.4.1'],
baileys: (browser) => ['Baileys', browser, '6.5.0'],
windows: (browser) => ['Windows', browser, '10.0.22631'],
ubuntu: browser => ['Ubuntu', browser, '22.04.4'],
macOS: browser => ['Mac OS', browser, '14.4.1'],
baileys: browser => ['Baileys', browser, '6.5.0'],
windows: browser => ['Windows', browser, '10.0.22631'],
/** The appropriate browser based on your OS & release */
appropriate: (browser) => [ PLATFORM_MAP[platform()] || 'Ubuntu', browser, release() ]
appropriate: browser => [PLATFORM_MAP[platform()] || 'Ubuntu', browser, release()]
}
export const getPlatformId = (browser: string) => {
@@ -34,7 +42,7 @@ export const getPlatformId = (browser: string) => {
export const BufferJSON = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
replacer: (k, value: any) => {
if(Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
return { type: 'Buffer', data: Buffer.from(value?.data || value).toString('base64') }
}
@@ -43,7 +51,7 @@ export const BufferJSON = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reviver: (_, value: any) => {
if(typeof value === 'object' && !!value && (value.buffer === true || value.type === 'Buffer')) {
if (typeof value === 'object' && !!value && (value.buffer === true || value.type === 'Buffer')) {
const val = value.data || value.value
return typeof val === 'string' ? Buffer.from(val, 'base64') : Buffer.from(val || [])
}
@@ -52,17 +60,13 @@ export const BufferJSON = {
}
}
export const getKeyAuthor = (
key: proto.IMessageKey | undefined | null,
meId = 'me'
) => (
export const getKeyAuthor = (key: proto.IMessageKey | undefined | null, meId = 'me') =>
(key?.fromMe ? meId : key?.participant || key?.remoteJid) || ''
)
export const writeRandomPadMax16 = (msg: Uint8Array) => {
const pad = randomBytes(1)
pad[0] &= 0xf
if(!pad[0]) {
if (!pad[0]) {
pad[0] = 0xf
}
@@ -71,23 +75,19 @@ export const writeRandomPadMax16 = (msg: Uint8Array) => {
export const unpadRandomMax16 = (e: Uint8Array | Buffer) => {
const t = new Uint8Array(e)
if(0 === t.length) {
if (0 === t.length) {
throw new Error('unpadPkcs7 given empty bytes')
}
var r = t[t.length - 1]
if(r > t.length) {
if (r > t.length) {
throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`)
}
return new Uint8Array(t.buffer, t.byteOffset, t.length - r)
}
export const encodeWAMessage = (message: proto.IMessage) => (
writeRandomPadMax16(
proto.Message.encode(message).finish()
)
)
export const encodeWAMessage = (message: proto.IMessage) => writeRandomPadMax16(proto.Message.encode(message).finish())
export const generateRegistrationId = (): number => {
return Uint16Array.from(randomBytes(2))[0] & 16383
@@ -96,7 +96,7 @@ export const generateRegistrationId = (): number => {
export const encodeBigEndian = (e: number, t = 4) => {
let r = e
const a = new Uint8Array(t)
for(let i = t - 1; i >= 0; i--) {
for (let i = t - 1; i >= 0; i--) {
a[i] = 255 & r
r >>>= 8
}
@@ -104,7 +104,8 @@ export const encodeBigEndian = (e: number, t = 4) => {
return a
}
export const toNumber = (t: Long | number | null | undefined): number => ((typeof t === 'object' && t) ? ('toNumber' in t ? t.toNumber() : (t as Long).low) : t || 0)
export const toNumber = (t: Long | number | null | undefined): number =>
typeof t === 'object' && t ? ('toNumber' in t ? t.toNumber() : (t as Long).low) : t || 0
/** unix timestamp of a date in seconds */
export const unixTimestampSeconds = (date: Date = new Date()) => Math.floor(date.getTime() / 1000)
@@ -124,12 +125,12 @@ export const debouncedTimeout = (intervalMs = 1000, task?: () => void) => {
timeout && clearTimeout(timeout)
timeout = undefined
},
setTask: (newTask: () => void) => task = newTask,
setInterval: (newInterval: number) => intervalMs = newInterval
setTask: (newTask: () => void) => (task = newTask),
setInterval: (newInterval: number) => (intervalMs = newInterval)
}
}
export const delay = (ms: number) => delayCancellable (ms).delay
export const delay = (ms: number) => delayCancellable(ms).delay
export const delayCancellable = (ms: number) => {
const stack = new Error().stack
@@ -140,7 +141,7 @@ export const delayCancellable = (ms: number) => {
reject = _reject
})
const cancel = () => {
clearTimeout (timeout)
clearTimeout(timeout)
reject(
new Boom('Cancelled', {
statusCode: 500,
@@ -154,29 +155,33 @@ export const delayCancellable = (ms: number) => {
return { delay, cancel }
}
export async function promiseTimeout<T>(ms: number | undefined, promise: (resolve: (v: T) => void, reject: (error) => void) => void) {
if(!ms) {
export async function promiseTimeout<T>(
ms: number | undefined,
promise: (resolve: (v: T) => void, reject: (error) => void) => void
) {
if (!ms) {
return new Promise(promise)
}
const stack = new Error().stack
// Create a promise that rejects in <ms> milliseconds
const { delay, cancel } = delayCancellable (ms)
const { delay, cancel } = delayCancellable(ms)
const p = new Promise((resolve, reject) => {
delay
.then(() => reject(
new Boom('Timed Out', {
statusCode: DisconnectReason.timedOut,
data: {
stack
}
})
))
.catch (err => reject(err))
.then(() =>
reject(
new Boom('Timed Out', {
statusCode: DisconnectReason.timedOut,
data: {
stack
}
})
)
)
.catch(err => reject(err))
promise (resolve, reject)
})
.finally (cancel)
promise(resolve, reject)
}).finally(cancel)
return p as Promise<T>
}
@@ -186,9 +191,9 @@ export const generateMessageIDV2 = (userId?: string): string => {
const data = Buffer.alloc(8 + 20 + 16)
data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)))
if(userId) {
if (userId) {
const id = jidDecode(userId)
if(id?.user) {
if (id?.user) {
data.write(id.user, 8)
data.write('@c.us', 8 + id.user.length)
}
@@ -205,37 +210,30 @@ export const generateMessageIDV2 = (userId?: string): string => {
export const generateMessageID = () => '3EB0' + randomBytes(18).toString('hex').toUpperCase()
export function bindWaitForEvent<T extends keyof BaileysEventMap>(ev: BaileysEventEmitter, event: T) {
return async(check: (u: BaileysEventMap[T]) => Promise<boolean | undefined>, timeoutMs?: number) => {
return async (check: (u: BaileysEventMap[T]) => Promise<boolean | undefined>, timeoutMs?: number) => {
let listener: (item: BaileysEventMap[T]) => void
let closeListener: (state: Partial<ConnectionState>) => void
await (
promiseTimeout<void>(
timeoutMs,
(resolve, reject) => {
closeListener = ({ connection, lastDisconnect }) => {
if(connection === 'close') {
reject(
lastDisconnect?.error
|| new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed })
)
}
}
ev.on('connection.update', closeListener)
listener = async(update) => {
if(await check(update)) {
resolve()
}
}
ev.on(event, listener)
await promiseTimeout<void>(timeoutMs, (resolve, reject) => {
closeListener = ({ connection, lastDisconnect }) => {
if (connection === 'close') {
reject(
lastDisconnect?.error || new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed })
)
}
)
.finally(() => {
ev.off(event, listener)
ev.off('connection.update', closeListener)
})
)
}
ev.on('connection.update', closeListener)
listener = async update => {
if (await check(update)) {
resolve()
}
}
ev.on(event, listener)
}).finally(() => {
ev.off(event, listener)
ev.off('connection.update', closeListener)
})
}
}
@@ -245,21 +243,18 @@ export const bindWaitForConnectionUpdate = (ev: BaileysEventEmitter) => bindWait
* utility that fetches latest baileys version from the master branch.
* Use to ensure your WA connection is always on the latest version
*/
export const fetchLatestBaileysVersion = async(options: AxiosRequestConfig<{}> = { }) => {
export const fetchLatestBaileysVersion = async (options: AxiosRequestConfig<{}> = {}) => {
const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/baileys-version.json'
try {
const result = await axios.get<{ version: WAVersion }>(
URL,
{
...options,
responseType: 'json'
}
)
const result = await axios.get<{ version: WAVersion }>(URL, {
...options,
responseType: 'json'
})
return {
version: result.data.version,
isLatest: true
}
} catch(error) {
} catch (error) {
return {
version: baileysVersion as WAVersion,
isLatest: false,
@@ -272,20 +267,17 @@ export const fetchLatestBaileysVersion = async(options: AxiosRequestConfig<{}> =
* A utility that fetches the latest web version of whatsapp.
* Use to ensure your WA connection is always on the latest version
*/
export const fetchLatestWaWebVersion = async(options: AxiosRequestConfig<{}>) => {
export const fetchLatestWaWebVersion = async (options: AxiosRequestConfig<{}>) => {
try {
const { data } = await axios.get(
'https://web.whatsapp.com/sw.js',
{
...options,
responseType: 'json'
}
)
const { data } = await axios.get('https://web.whatsapp.com/sw.js', {
...options,
responseType: 'json'
})
const regex = /\\?"client_revision\\?":\s*(\d+)/
const match = data.match(regex)
if(!match?.[1]) {
if (!match?.[1]) {
return {
version: baileysVersion as WAVersion,
isLatest: false,
@@ -301,7 +293,7 @@ export const fetchLatestWaWebVersion = async(options: AxiosRequestConfig<{}>) =>
version: [2, 3000, +clientRevision] as WAVersion,
isLatest: true
}
} catch(error) {
} catch (error) {
return {
version: baileysVersion as WAVersion,
isLatest: false,
@@ -317,9 +309,9 @@ export const generateMdTagPrefix = () => {
}
const STATUS_MAP: { [_: string]: proto.WebMessageInfo.Status } = {
'sender': proto.WebMessageInfo.Status.SERVER_ACK,
'played': proto.WebMessageInfo.Status.PLAYED,
'read': proto.WebMessageInfo.Status.READ,
sender: proto.WebMessageInfo.Status.SERVER_ACK,
played: proto.WebMessageInfo.Status.PLAYED,
read: proto.WebMessageInfo.Status.READ,
'read-self': proto.WebMessageInfo.Status.READ
}
/**
@@ -328,7 +320,7 @@ const STATUS_MAP: { [_: string]: proto.WebMessageInfo.Status } = {
*/
export const getStatusFromReceiptType = (type: string | undefined) => {
const status = STATUS_MAP[type!]
if(typeof type === 'undefined') {
if (typeof type === 'undefined') {
return proto.WebMessageInfo.Status.DELIVERY_ACK
}
@@ -348,7 +340,7 @@ export const getErrorCodeFromStreamError = (node: BinaryNode) => {
let reason = reasonNode?.tag || 'unknown'
const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession)
if(statusCode === DisconnectReason.restartRequired) {
if (statusCode === DisconnectReason.restartRequired) {
reason = 'restart required'
}
@@ -361,28 +353,28 @@ export const getErrorCodeFromStreamError = (node: BinaryNode) => {
export const getCallStatusFromNode = ({ tag, attrs }: BinaryNode) => {
let status: WACallUpdateType
switch (tag) {
case 'offer':
case 'offer_notice':
status = 'offer'
break
case 'terminate':
if(attrs.reason === 'timeout') {
status = 'timeout'
} else {
//fired when accepted/rejected/timeout/caller hangs up
status = 'terminate'
}
case 'offer':
case 'offer_notice':
status = 'offer'
break
case 'terminate':
if (attrs.reason === 'timeout') {
status = 'timeout'
} else {
//fired when accepted/rejected/timeout/caller hangs up
status = 'terminate'
}
break
case 'reject':
status = 'reject'
break
case 'accept':
status = 'accept'
break
default:
status = 'ringing'
break
break
case 'reject':
status = 'reject'
break
case 'accept':
status = 'accept'
break
default:
status = 'ringing'
break
}
return status
@@ -392,16 +384,17 @@ const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: '
export const getCodeFromWSError = (error: Error) => {
let statusCode = 500
if(error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length)
if(!Number.isNaN(code) && code >= 400) {
if (!Number.isNaN(code) && code >= 400) {
statusCode = code
}
} else if(
} else if (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(error as any)?.code?.startsWith('E')
|| error?.message?.includes('timed out')
) { // handle ETIMEOUT, ENOTFOUND etc
(error as any)?.code?.startsWith('E') ||
error?.message?.includes('timed out')
) {
// handle ETIMEOUT, ENOTFOUND etc
statusCode = 408
}
@@ -417,9 +410,9 @@ export const isWABusinessPlatform = (platform: string) => {
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function trimUndefined(obj: {[_: string]: any}) {
for(const key in obj) {
if(typeof obj[key] === 'undefined') {
export function trimUndefined(obj: { [_: string]: any }) {
for (const key in obj) {
if (typeof obj[key] === 'undefined') {
delete obj[key]
}
}
@@ -434,17 +427,17 @@ export function bytesToCrockford(buffer: Buffer): string {
let bitCount = 0
const crockford: string[] = []
for(const element of buffer) {
for (const element of buffer) {
value = (value << 8) | (element & 0xff)
bitCount += 8
while(bitCount >= 5) {
while (bitCount >= 5) {
crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31))
bitCount -= 5
}
}
if(bitCount > 0) {
if (bitCount > 0) {
crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31))
}

View File

@@ -10,10 +10,7 @@ import { downloadContentFromMessage } from './messages-media'
const inflatePromise = promisify(inflate)
export const downloadHistory = async(
msg: proto.Message.IHistorySyncNotification,
options: AxiosRequestConfig<{}>
) => {
export const downloadHistory = async (msg: proto.Message.IHistorySyncNotification, options: AxiosRequestConfig<{}>) => {
const stream = await downloadContentFromMessage(msg, 'md-msg-hist', { options })
const bufferArray: Buffer[] = []
for await (const chunk of stream) {
@@ -35,59 +32,58 @@ export const processHistoryMessage = (item: proto.IHistorySync) => {
const chats: Chat[] = []
switch (item.syncType) {
case proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP:
case proto.HistorySync.HistorySyncType.RECENT:
case proto.HistorySync.HistorySyncType.FULL:
case proto.HistorySync.HistorySyncType.ON_DEMAND:
for(const chat of item.conversations! as Chat[]) {
contacts.push({ id: chat.id, name: chat.name || undefined })
case proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP:
case proto.HistorySync.HistorySyncType.RECENT:
case proto.HistorySync.HistorySyncType.FULL:
case proto.HistorySync.HistorySyncType.ON_DEMAND:
for (const chat of item.conversations! as Chat[]) {
contacts.push({ id: chat.id, name: chat.name || undefined })
const msgs = chat.messages || []
delete chat.messages
delete chat.archived
delete chat.muteEndTime
delete chat.pinned
const msgs = chat.messages || []
delete chat.messages
delete chat.archived
delete chat.muteEndTime
delete chat.pinned
for(const item of msgs) {
const message = item.message!
messages.push(message)
for (const item of msgs) {
const message = item.message!
messages.push(message)
if(!chat.messages?.length) {
// keep only the most recent message in the chat array
chat.messages = [{ message }]
if (!chat.messages?.length) {
// keep only the most recent message in the chat array
chat.messages = [{ message }]
}
if (!message.key.fromMe && !chat.lastMessageRecvTimestamp) {
chat.lastMessageRecvTimestamp = toNumber(message.messageTimestamp)
}
if (
(message.messageStubType === WAMessageStubType.BIZ_PRIVACY_MODE_TO_BSP ||
message.messageStubType === WAMessageStubType.BIZ_PRIVACY_MODE_TO_FB) &&
message.messageStubParameters?.[0]
) {
contacts.push({
id: message.key.participant || message.key.remoteJid!,
verifiedName: message.messageStubParameters?.[0]
})
}
}
if(!message.key.fromMe && !chat.lastMessageRecvTimestamp) {
chat.lastMessageRecvTimestamp = toNumber(message.messageTimestamp)
if (isJidUser(chat.id) && chat.readOnly && chat.archived) {
delete chat.readOnly
}
if(
(message.messageStubType === WAMessageStubType.BIZ_PRIVACY_MODE_TO_BSP
|| message.messageStubType === WAMessageStubType.BIZ_PRIVACY_MODE_TO_FB
)
&& message.messageStubParameters?.[0]
) {
contacts.push({
id: message.key.participant || message.key.remoteJid!,
verifiedName: message.messageStubParameters?.[0],
})
}
chats.push({ ...chat })
}
if(isJidUser(chat.id) && chat.readOnly && chat.archived) {
delete chat.readOnly
break
case proto.HistorySync.HistorySyncType.PUSH_NAME:
for (const c of item.pushnames!) {
contacts.push({ id: c.id!, notify: c.pushname! })
}
chats.push({ ...chat })
}
break
case proto.HistorySync.HistorySyncType.PUSH_NAME:
for(const c of item.pushnames!) {
contacts.push({ id: c.id!, notify: c.pushname! })
}
break
break
}
return {
@@ -99,7 +95,7 @@ export const processHistoryMessage = (item: proto.IHistorySync) => {
}
}
export const downloadAndProcessHistorySyncNotification = async(
export const downloadAndProcessHistorySyncNotification = async (
msg: proto.Message.IHistorySyncNotification,
options: AxiosRequestConfig<{}>
) => {
@@ -112,4 +108,4 @@ export const getHistoryMsg = (message: proto.IMessage) => {
const anyHistoryMsg = normalizedContent?.protocolMessage?.historySyncNotification
return anyHistoryMsg
}
}

View File

@@ -7,10 +7,7 @@ import { extractImageThumb, getHttpStream } from './messages-media'
const THUMBNAIL_WIDTH_PX = 192
/** Fetches an image and generates a thumbnail for it */
const getCompressedJpegThumbnail = async(
url: string,
{ thumbnailWidth, fetchOpts }: URLGenerationOptions
) => {
const getCompressedJpegThumbnail = async (url: string, { thumbnailWidth, fetchOpts }: URLGenerationOptions) => {
const stream = await getHttpStream(url, fetchOpts)
const result = await extractImageThumb(stream, thumbnailWidth)
return result
@@ -34,12 +31,12 @@ export type URLGenerationOptions = {
* @param text first matched URL in text
* @returns the URL info required to generate link preview
*/
export const getUrlInfo = async(
export const getUrlInfo = async (
text: string,
opts: URLGenerationOptions = {
thumbnailWidth: THUMBNAIL_WIDTH_PX,
fetchOpts: { timeout: 3000 }
},
}
): Promise<WAUrlInfo | undefined> => {
try {
// retries
@@ -48,7 +45,7 @@ export const getUrlInfo = async(
const { getLinkPreview } = await import('link-preview-js')
let previewLink = text
if(!text.startsWith('https://') && !text.startsWith('http://')) {
if (!text.startsWith('https://') && !text.startsWith('http://')) {
previewLink = 'https://' + previewLink
}
@@ -58,14 +55,14 @@ export const getUrlInfo = async(
handleRedirects: (baseURL: string, forwardedURL: string) => {
const urlObj = new URL(baseURL)
const forwardedURLObj = new URL(forwardedURL)
if(retries >= maxRetry) {
if (retries >= maxRetry) {
return false
}
if(
forwardedURLObj.hostname === urlObj.hostname
|| forwardedURLObj.hostname === 'www.' + urlObj.hostname
|| 'www.' + forwardedURLObj.hostname === urlObj.hostname
if (
forwardedURLObj.hostname === urlObj.hostname ||
forwardedURLObj.hostname === 'www.' + urlObj.hostname ||
'www.' + forwardedURLObj.hostname === urlObj.hostname
) {
retries + 1
return true
@@ -75,7 +72,7 @@ export const getUrlInfo = async(
},
headers: opts.fetchOpts as {}
})
if(info && 'title' in info && info.title) {
if (info && 'title' in info && info.title) {
const [image] = info.images
const urlInfo: WAUrlInfo = {
@@ -86,7 +83,7 @@ export const getUrlInfo = async(
originalThumbnailUrl: image
}
if(opts.uploadImage) {
if (opts.uploadImage) {
const { imageMessage } = await prepareWAMessageMedia(
{ image: { url: image } },
{
@@ -95,28 +92,21 @@ export const getUrlInfo = async(
options: opts.fetchOpts
}
)
urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail
? Buffer.from(imageMessage.jpegThumbnail)
: undefined
urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail ? Buffer.from(imageMessage.jpegThumbnail) : undefined
urlInfo.highQualityThumbnail = imageMessage || undefined
} else {
try {
urlInfo.jpegThumbnail = image
? (await getCompressedJpegThumbnail(image, opts)).buffer
: undefined
} catch(error) {
opts.logger?.debug(
{ err: error.stack, url: previewLink },
'error in generating thumbnail'
)
urlInfo.jpegThumbnail = image ? (await getCompressedJpegThumbnail(image, opts)).buffer : undefined
} catch (error) {
opts.logger?.debug({ err: error.stack, url: previewLink }, 'error in generating thumbnail')
}
}
return urlInfo
}
} catch(error) {
if(!error.message.includes('receive a valid')) {
} catch (error) {
if (!error.message.includes('receive a valid')) {
throw error
}
}
}
}

View File

@@ -1,13 +1,13 @@
import P from 'pino'
export interface ILogger {
level: string
child(obj: Record<string, unknown>): ILogger
trace(obj: unknown, msg?: string)
debug(obj: unknown, msg?: string)
info(obj: unknown, msg?: string)
warn(obj: unknown, msg?: string)
error(obj: unknown, msg?: string)
level: string
child(obj: Record<string, unknown>): ILogger
trace(obj: unknown, msg?: string)
debug(obj: unknown, msg?: string)
info(obj: unknown, msg?: string)
warn(obj: unknown, msg?: string)
error(obj: unknown, msg?: string)
}
export default P({ timestamp: () => `,"time":"${new Date().toJSON()}"` })

View File

@@ -9,7 +9,6 @@ import { hkdf } from './crypto'
const o = 128
class d {
salt: string
constructor(e: string) {
@@ -17,7 +16,7 @@ class d {
}
add(e, t) {
var r = this
for(const item of t) {
for (const item of t) {
e = r._addSingle(e, item)
}
@@ -25,7 +24,7 @@ class d {
}
subtract(e, t) {
var r = this
for(const item of t) {
for (const item of t) {
e = r._subtractSingle(e, item)
}
@@ -38,20 +37,20 @@ class d {
async _addSingle(e, t) {
var r = this
const n = new Uint8Array(await hkdf(Buffer.from(t), o, { info: r.salt })).buffer
return r.performPointwiseWithOverflow(await e, n, ((e, t) => e + t))
return r.performPointwiseWithOverflow(await e, n, (e, t) => e + t)
}
async _subtractSingle(e, t) {
var r = this
const n = new Uint8Array(await hkdf(Buffer.from(t), o, { info: r.salt })).buffer
return r.performPointwiseWithOverflow(await e, n, ((e, t) => e - t))
return r.performPointwiseWithOverflow(await e, n, (e, t) => e - t)
}
performPointwiseWithOverflow(e, t, r) {
const n = new DataView(e)
, i = new DataView(t)
, a = new ArrayBuffer(n.byteLength)
, s = new DataView(a)
for(let e = 0; e < n.byteLength; e += 2) {
const n = new DataView(e),
i = new DataView(t),
a = new ArrayBuffer(n.byteLength),
s = new DataView(a)
for (let e = 0; e < n.byteLength; e += 2) {
s.setUint16(e, r(n.getUint16(e, !0), i.getUint16(e, !0)), !0)
}

View File

@@ -6,12 +6,12 @@ export const makeMutex = () => {
return {
mutex<T>(code: () => Promise<T> | T): Promise<T> {
task = (async() => {
task = (async () => {
// wait for the previous task to complete
// if there is an error, we swallow so as to not block the queue
try {
await task
} catch{ }
} catch {}
try {
// execute the current task
@@ -24,7 +24,7 @@ export const makeMutex = () => {
// we replace the existing task, appending the new piece of execution to it
// so the next task will have to wait for this one to finish
return task
},
}
}
}
@@ -35,11 +35,11 @@ export const makeKeyedMutex = () => {
return {
mutex<T>(key: string, task: () => Promise<T> | T): Promise<T> {
if(!map[key]) {
if (!map[key]) {
map[key] = makeMutex()
}
return map[key].mutex(task)
}
}
}
}

View File

@@ -11,7 +11,20 @@ import { Readable, Transform } from 'stream'
import { URL } from 'url'
import { proto } from '../../WAProto'
import { DEFAULT_ORIGIN, MEDIA_HKDF_KEY_MAPPING, MEDIA_PATH_MAP } from '../Defaults'
import { BaileysEventMap, DownloadableMessage, MediaConnInfo, MediaDecryptionKeyInfo, MediaType, MessageType, SocketConfig, WAGenericMediaMessage, WAMediaPayloadURL, WAMediaUpload, WAMediaUploadFunction, WAMessageContent } from '../Types'
import {
BaileysEventMap,
DownloadableMessage,
MediaConnInfo,
MediaDecryptionKeyInfo,
MediaType,
MessageType,
SocketConfig,
WAGenericMediaMessage,
WAMediaPayloadURL,
WAMediaUpload,
WAMediaUploadFunction,
WAMessageContent
} from '../Types'
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildBuffer, jidNormalizedUser } from '../WABinary'
import { aesDecryptGCM, aesEncryptGCM, hkdf } from './crypto'
import { generateMessageIDV2 } from './generics'
@@ -19,30 +32,24 @@ import { ILogger } from './logger'
const getTmpFilesDirectory = () => tmpdir()
const getImageProcessingLibrary = async() => {
const getImageProcessingLibrary = async () => {
const [_jimp, sharp] = await Promise.all([
(async() => {
const jimp = await (
import('jimp')
.catch(() => { })
)
(async () => {
const jimp = await import('jimp').catch(() => {})
return jimp
})(),
(async() => {
const sharp = await (
import('sharp')
.catch(() => { })
)
(async () => {
const sharp = await import('sharp').catch(() => {})
return sharp
})()
])
if(sharp) {
if (sharp) {
return { sharp }
}
const jimp = _jimp?.default || _jimp
if(jimp) {
if (jimp) {
return { jimp }
}
@@ -55,12 +62,15 @@ export const hkdfInfoKey = (type: MediaType) => {
}
/** generates all the keys required to encrypt/decrypt & sign a media message */
export async function getMediaKeys(buffer: Uint8Array | string | null | undefined, mediaType: MediaType): Promise<MediaDecryptionKeyInfo> {
if(!buffer) {
export async function getMediaKeys(
buffer: Uint8Array | string | null | undefined,
mediaType: MediaType
): Promise<MediaDecryptionKeyInfo> {
if (!buffer) {
throw new Boom('Cannot derive from empty media key')
}
if(typeof buffer === 'string') {
if (typeof buffer === 'string') {
buffer = Buffer.from(buffer.replace('data:;base64,', ''), 'base64')
}
@@ -69,49 +79,47 @@ export async function getMediaKeys(buffer: Uint8Array | string | null | undefine
return {
iv: expandedMediaKey.slice(0, 16),
cipherKey: expandedMediaKey.slice(16, 48),
macKey: expandedMediaKey.slice(48, 80),
macKey: expandedMediaKey.slice(48, 80)
}
}
/** Extracts video thumb using FFMPEG */
const extractVideoThumb = async(
const extractVideoThumb = async (
path: string,
destPath: string,
time: string,
size: { width: number, height: number },
) => new Promise<void>((resolve, reject) => {
const cmd = `ffmpeg -ss ${time} -i ${path} -y -vf scale=${size.width}:-1 -vframes 1 -f image2 ${destPath}`
exec(cmd, (err) => {
if(err) {
reject(err)
} else {
resolve()
}
})
})
size: { width: number; height: number }
) =>
new Promise<void>((resolve, reject) => {
const cmd = `ffmpeg -ss ${time} -i ${path} -y -vf scale=${size.width}:-1 -vframes 1 -f image2 ${destPath}`
exec(cmd, err => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
export const extractImageThumb = async(bufferOrFilePath: Readable | Buffer | string, width = 32) => {
if(bufferOrFilePath instanceof Readable) {
export const extractImageThumb = async (bufferOrFilePath: Readable | Buffer | string, width = 32) => {
if (bufferOrFilePath instanceof Readable) {
bufferOrFilePath = await toBuffer(bufferOrFilePath)
}
const lib = await getImageProcessingLibrary()
if('sharp' in lib && typeof lib.sharp?.default === 'function') {
if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
const img = lib.sharp.default(bufferOrFilePath)
const dimensions = await img.metadata()
const buffer = await img
.resize(width)
.jpeg({ quality: 50 })
.toBuffer()
const buffer = await img.resize(width).jpeg({ quality: 50 }).toBuffer()
return {
buffer,
original: {
width: dimensions.width,
height: dimensions.height,
},
height: dimensions.height
}
}
} else if('jimp' in lib && typeof lib.jimp?.read === 'function') {
} else if ('jimp' in lib && typeof lib.jimp?.read === 'function') {
const { read, MIME_JPEG, RESIZE_BILINEAR, AUTO } = lib.jimp
const jimp = await read(bufferOrFilePath as string)
@@ -119,10 +127,7 @@ export const extractImageThumb = async(bufferOrFilePath: Readable | Buffer | str
width: jimp.getWidth(),
height: jimp.getHeight()
}
const buffer = await jimp
.quality(50)
.resize(width, AUTO, RESIZE_BILINEAR)
.getBufferAsync(MIME_JPEG)
const buffer = await jimp.quality(50).resize(width, AUTO, RESIZE_BILINEAR).getBufferAsync(MIME_JPEG)
return {
buffer,
original: dimensions
@@ -132,20 +137,14 @@ export const extractImageThumb = async(bufferOrFilePath: Readable | Buffer | str
}
}
export const encodeBase64EncodedStringForUpload = (b64: string) => (
encodeURIComponent(
b64
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/\=+$/, '')
)
)
export const encodeBase64EncodedStringForUpload = (b64: string) =>
encodeURIComponent(b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, ''))
export const generateProfilePicture = async(mediaUpload: WAMediaUpload) => {
export const generateProfilePicture = async (mediaUpload: WAMediaUpload) => {
let bufferOrFilePath: Buffer | string
if(Buffer.isBuffer(mediaUpload)) {
if (Buffer.isBuffer(mediaUpload)) {
bufferOrFilePath = mediaUpload
} else if('url' in mediaUpload) {
} else if ('url' in mediaUpload) {
bufferOrFilePath = mediaUpload.url.toString()
} else {
bufferOrFilePath = await toBuffer(mediaUpload.stream)
@@ -153,44 +152,42 @@ export const generateProfilePicture = async(mediaUpload: WAMediaUpload) => {
const lib = await getImageProcessingLibrary()
let img: Promise<Buffer>
if('sharp' in lib && typeof lib.sharp?.default === 'function') {
img = lib.sharp.default(bufferOrFilePath)
if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
img = lib.sharp
.default(bufferOrFilePath)
.resize(640, 640)
.jpeg({
quality: 50,
quality: 50
})
.toBuffer()
} else if('jimp' in lib && typeof lib.jimp?.read === 'function') {
} else if ('jimp' in lib && typeof lib.jimp?.read === 'function') {
const { read, MIME_JPEG, RESIZE_BILINEAR } = lib.jimp
const jimp = await read(bufferOrFilePath as string)
const min = Math.min(jimp.getWidth(), jimp.getHeight())
const cropped = jimp.crop(0, 0, min, min)
img = cropped
.quality(50)
.resize(640, 640, RESIZE_BILINEAR)
.getBufferAsync(MIME_JPEG)
img = cropped.quality(50).resize(640, 640, RESIZE_BILINEAR).getBufferAsync(MIME_JPEG)
} else {
throw new Boom('No image processing library available')
}
return {
img: await img,
img: await img
}
}
/** gets the SHA256 of the given media message */
export const mediaMessageSHA256B64 = (message: WAMessageContent) => {
const media = Object.values(message)[0] as WAGenericMediaMessage
return media?.fileSha256 && Buffer.from(media.fileSha256).toString ('base64')
return media?.fileSha256 && Buffer.from(media.fileSha256).toString('base64')
}
export async function getAudioDuration(buffer: Buffer | string | Readable) {
const musicMetadata = await import('music-metadata')
let metadata: IAudioMetadata
if(Buffer.isBuffer(buffer)) {
if (Buffer.isBuffer(buffer)) {
metadata = await musicMetadata.parseBuffer(buffer, undefined, { duration: true })
} else if(typeof buffer === 'string') {
} else if (typeof buffer === 'string') {
const rStream = createReadStream(buffer)
try {
metadata = await musicMetadata.parseStream(rStream, undefined, { duration: true })
@@ -209,11 +206,11 @@ export async function getAudioDuration(buffer: Buffer | string | Readable) {
*/
export async function getAudioWaveform(buffer: Buffer | string | Readable, logger?: ILogger) {
try {
const { default: decoder } = await eval('import(\'audio-decode\')')
const { default: decoder } = await eval("import('audio-decode')")
let audioData: Buffer
if(Buffer.isBuffer(buffer)) {
if (Buffer.isBuffer(buffer)) {
audioData = buffer
} else if(typeof buffer === 'string') {
} else if (typeof buffer === 'string') {
const rStream = createReadStream(buffer)
audioData = await toBuffer(rStream)
} else {
@@ -226,10 +223,10 @@ export async function getAudioWaveform(buffer: Buffer | string | Readable, logge
const samples = 64 // Number of samples we want to have in our final data set
const blockSize = Math.floor(rawData.length / samples) // the number of samples in each subdivision
const filteredData: number[] = []
for(let i = 0; i < samples; i++) {
const blockStart = blockSize * i // the location of the first sample in the block
let sum = 0
for(let j = 0; j < blockSize; j++) {
for (let i = 0; i < samples; i++) {
const blockStart = blockSize * i // the location of the first sample in the block
let sum = 0
for (let j = 0; j < blockSize; j++) {
sum = sum + Math.abs(rawData[blockStart + j]) // find the sum of all the samples in the block
}
@@ -238,20 +235,17 @@ export async function getAudioWaveform(buffer: Buffer | string | Readable, logge
// This guarantees that the largest data point will be set to 1, and the rest of the data will scale proportionally.
const multiplier = Math.pow(Math.max(...filteredData), -1)
const normalizedData = filteredData.map((n) => n * multiplier)
const normalizedData = filteredData.map(n => n * multiplier)
// Generate waveform like WhatsApp
const waveform = new Uint8Array(
normalizedData.map((n) => Math.floor(100 * n))
)
const waveform = new Uint8Array(normalizedData.map(n => Math.floor(100 * n)))
return waveform
} catch(e) {
} catch (e) {
logger?.debug('Failed to generate waveform: ' + e)
}
}
export const toReadable = (buffer: Buffer) => {
const readable = new Readable({ read: () => {} })
readable.push(buffer)
@@ -259,7 +253,7 @@ export const toReadable = (buffer: Buffer) => {
return readable
}
export const toBuffer = async(stream: Readable) => {
export const toBuffer = async (stream: Readable) => {
const chunks: Buffer[] = []
for await (const chunk of stream) {
chunks.push(chunk)
@@ -269,16 +263,16 @@ export const toBuffer = async(stream: Readable) => {
return Buffer.concat(chunks)
}
export const getStream = async(item: WAMediaUpload, opts?: AxiosRequestConfig) => {
if(Buffer.isBuffer(item)) {
export const getStream = async (item: WAMediaUpload, opts?: AxiosRequestConfig) => {
if (Buffer.isBuffer(item)) {
return { stream: toReadable(item), type: 'buffer' } as const
}
if('stream' in item) {
if ('stream' in item) {
return { stream: item.stream, type: 'readable' } as const
}
if(item.url.toString().startsWith('http://') || item.url.toString().startsWith('https://')) {
if (item.url.toString().startsWith('http://') || item.url.toString().startsWith('https://')) {
return { stream: await getHttpStream(item.url, opts), type: 'remote' } as const
}
@@ -290,21 +284,21 @@ export async function generateThumbnail(
file: string,
mediaType: 'video' | 'image',
options: {
logger?: ILogger
}
logger?: ILogger
}
) {
let thumbnail: string | undefined
let originalImageDimensions: { width: number, height: number } | undefined
if(mediaType === 'image') {
let originalImageDimensions: { width: number; height: number } | undefined
if (mediaType === 'image') {
const { buffer, original } = await extractImageThumb(file)
thumbnail = buffer.toString('base64')
if(original.width && original.height) {
if (original.width && original.height) {
originalImageDimensions = {
width: original.width,
height: original.height,
height: original.height
}
}
} else if(mediaType === 'video') {
} else if (mediaType === 'video') {
const imgFilename = join(getTmpFilesDirectory(), generateMessageIDV2() + '.jpg')
try {
await extractVideoThumb(file, imgFilename, '00:00:00', { width: 32, height: 32 })
@@ -312,7 +306,7 @@ export async function generateThumbnail(
thumbnail = buff.toString('base64')
await fs.unlink(imgFilename)
} catch(err) {
} catch (err) {
options.logger?.debug('could not generate video thumb: ' + err)
}
}
@@ -323,7 +317,7 @@ export async function generateThumbnail(
}
}
export const getHttpStream = async(url: string | URL, options: AxiosRequestConfig & { isStream?: true } = {}) => {
export const getHttpStream = async (url: string | URL, options: AxiosRequestConfig & { isStream?: true } = {}) => {
const fetched = await axios.get(url.toString(), { ...options, responseType: 'stream' })
return fetched.data as Readable
}
@@ -334,7 +328,7 @@ type EncryptedStreamOptions = {
opts?: AxiosRequestConfig
}
export const encryptedStream = async(
export const encryptedStream = async (
media: WAMediaUpload,
mediaType: MediaType,
{ logger, saveOriginalFileIfRequired, opts }: EncryptedStreamOptions = {}
@@ -350,9 +344,9 @@ export const encryptedStream = async(
let bodyPath: string | undefined
let writeStream: WriteStream | undefined
let didSaveToTmpPath = false
if(type === 'file') {
if (type === 'file') {
bodyPath = (media as WAMediaPayloadURL).url.toString()
} else if(saveOriginalFileIfRequired) {
} else if (saveOriginalFileIfRequired) {
bodyPath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2())
writeStream = createWriteStream(bodyPath)
didSaveToTmpPath = true
@@ -368,21 +362,14 @@ export const encryptedStream = async(
for await (const data of stream) {
fileLength += data.length
if(
type === 'remote'
&& opts?.maxContentLength
&& fileLength + data.length > opts.maxContentLength
) {
throw new Boom(
`content length exceeded when encrypting "${type}"`,
{
data: { media, type }
}
)
if (type === 'remote' && opts?.maxContentLength && fileLength + data.length > opts.maxContentLength) {
throw new Boom(`content length exceeded when encrypting "${type}"`, {
data: { media, type }
})
}
sha256Plain = sha256Plain.update(data)
if(writeStream && !writeStream.write(data)) {
if (writeStream && !writeStream.write(data)) {
await once(writeStream, 'drain')
}
@@ -415,7 +402,7 @@ export const encryptedStream = async(
fileLength,
didSaveToTmpPath
}
} catch(error) {
} catch (error) {
// destroy all streams with error
encWriteStream.destroy()
writeStream?.destroy()
@@ -425,10 +412,10 @@ export const encryptedStream = async(
sha256Enc.destroy()
stream.destroy()
if(didSaveToTmpPath) {
if (didSaveToTmpPath) {
try {
await fs.unlink(bodyPath!)
} catch(err) {
} catch (err) {
logger?.error({ err }, 'failed to save to tmp path')
}
}
@@ -451,17 +438,17 @@ const toSmallestChunkSize = (num: number) => {
}
export type MediaDownloadOptions = {
startByte?: number
endByte?: number
startByte?: number
endByte?: number
options?: AxiosRequestConfig<{}>
}
export const getUrlFromDirectPath = (directPath: string) => `https://${DEF_HOST}${directPath}`
export const downloadContentFromMessage = async(
export const downloadContentFromMessage = async (
{ mediaKey, directPath, url }: DownloadableMessage,
type: MediaType,
opts: MediaDownloadOptions = { }
opts: MediaDownloadOptions = {}
) => {
const downloadUrl = url || getUrlFromDirectPath(directPath!)
const keys = await getMediaKeys(mediaKey, type)
@@ -473,18 +460,18 @@ export const downloadContentFromMessage = async(
* Decrypts and downloads an AES256-CBC encrypted file given the keys.
* Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
* */
export const downloadEncryptedContent = async(
export const downloadEncryptedContent = async (
downloadUrl: string,
{ cipherKey, iv }: MediaDecryptionKeyInfo,
{ startByte, endByte, options }: MediaDownloadOptions = { }
{ startByte, endByte, options }: MediaDownloadOptions = {}
) => {
let bytesFetched = 0
let startChunk = 0
let firstBlockIsIV = false
// if a start byte is specified -- then we need to fetch the previous chunk as that will form the IV
if(startByte) {
if (startByte) {
const chunk = toSmallestChunkSize(startByte || 0)
if(chunk) {
if (chunk) {
startChunk = chunk - AES_CHUNK_SIZE
bytesFetched = chunk
@@ -495,33 +482,30 @@ export const downloadEncryptedContent = async(
const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined
const headers: AxiosRequestConfig['headers'] = {
...options?.headers || { },
Origin: DEFAULT_ORIGIN,
...(options?.headers || {}),
Origin: DEFAULT_ORIGIN
}
if(startChunk || endChunk) {
if (startChunk || endChunk) {
headers.Range = `bytes=${startChunk}-`
if(endChunk) {
if (endChunk) {
headers.Range += endChunk
}
}
// download the message
const fetched = await getHttpStream(
downloadUrl,
{
...options || { },
headers,
maxBodyLength: Infinity,
maxContentLength: Infinity,
}
)
const fetched = await getHttpStream(downloadUrl, {
...(options || {}),
headers,
maxBodyLength: Infinity,
maxContentLength: Infinity
})
let remainingBytes = Buffer.from([])
let aes: Crypto.Decipher
const pushBytes = (bytes: Buffer, push: (bytes: Buffer) => void) => {
if(startByte || endByte) {
if (startByte || endByte) {
const start = bytesFetched >= startByte! ? undefined : Math.max(startByte! - bytesFetched, 0)
const end = bytesFetched + bytes.length < endByte! ? undefined : Math.max(endByte! - bytesFetched, 0)
@@ -541,9 +525,9 @@ export const downloadEncryptedContent = async(
remainingBytes = data.slice(decryptLength)
data = data.slice(0, decryptLength)
if(!aes) {
if (!aes) {
let ivValue = iv
if(firstBlockIsIV) {
if (firstBlockIsIV) {
ivValue = data.slice(0, AES_CHUNK_SIZE)
data = data.slice(AES_CHUNK_SIZE)
}
@@ -551,16 +535,15 @@ export const downloadEncryptedContent = async(
aes = Crypto.createDecipheriv('aes-256-cbc', cipherKey, ivValue)
// if an end byte that is not EOF is specified
// stop auto padding (PKCS7) -- otherwise throws an error for decryption
if(endByte) {
if (endByte) {
aes.setAutoPadding(false)
}
}
try {
pushBytes(aes.update(data), b => this.push(b))
callback()
} catch(error) {
} catch (error) {
callback(error)
}
},
@@ -568,10 +551,10 @@ export const downloadEncryptedContent = async(
try {
pushBytes(aes.final(), b => this.push(b))
callback()
} catch(error) {
} catch (error) {
callback(error)
}
},
}
})
return fetched.pipe(output, { end: true })
}
@@ -580,11 +563,7 @@ export function extensionForMediaMessage(message: WAMessageContent) {
const getExtension = (mimetype: string) => mimetype.split(';')[0].split('/')[1]
const type = Object.keys(message)[0] as MessageType
let extension: string
if(
type === 'locationMessage' ||
type === 'liveLocationMessage' ||
type === 'productMessage'
) {
if (type === 'locationMessage' || type === 'liveLocationMessage' || type === 'productMessage') {
extension = '.jpeg'
} else {
const messageContent = message[type] as WAGenericMediaMessage
@@ -596,18 +575,18 @@ export function extensionForMediaMessage(message: WAMessageContent) {
export const getWAUploadToServer = (
{ customUploadHosts, fetchAgent, logger, options }: SocketConfig,
refreshMediaConn: (force: boolean) => Promise<MediaConnInfo>,
refreshMediaConn: (force: boolean) => Promise<MediaConnInfo>
): WAMediaUploadFunction => {
return async(stream, { mediaType, fileEncSha256B64, timeoutMs }) => {
return async (stream, { mediaType, fileEncSha256B64, timeoutMs }) => {
// send a query JSON to obtain the url & auth token to upload our media
let uploadInfo = await refreshMediaConn(false)
let urls: { mediaUrl: string, directPath: string } | undefined
const hosts = [ ...customUploadHosts, ...uploadInfo.hosts ]
let urls: { mediaUrl: string; directPath: string } | undefined
const hosts = [...customUploadHosts, ...uploadInfo.hosts]
fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64)
for(const { hostname } of hosts) {
for (const { hostname } of hosts) {
logger.debug(`uploading to "${hostname}"`)
const auth = encodeURIComponent(uploadInfo.auth) // the auth token
@@ -615,27 +594,22 @@ export const getWAUploadToServer = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let result: any
try {
const body = await axios.post(
url,
stream,
{
...options,
headers: {
...options.headers || { },
'Content-Type': 'application/octet-stream',
'Origin': DEFAULT_ORIGIN
},
httpsAgent: fetchAgent,
timeout: timeoutMs,
responseType: 'json',
maxBodyLength: Infinity,
maxContentLength: Infinity,
}
)
const body = await axios.post(url, stream, {
...options,
headers: {
...(options.headers || {}),
'Content-Type': 'application/octet-stream',
Origin: DEFAULT_ORIGIN
},
httpsAgent: fetchAgent,
timeout: timeoutMs,
responseType: 'json',
maxBodyLength: Infinity,
maxContentLength: Infinity
})
result = body.data
if(result?.url || result?.directPath) {
if (result?.url || result?.directPath) {
urls = {
mediaUrl: result.url,
directPath: result.direct_path
@@ -645,21 +619,21 @@ export const getWAUploadToServer = (
uploadInfo = await refreshMediaConn(true)
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`)
}
} catch(error) {
if(axios.isAxiosError(error)) {
} catch (error) {
if (axios.isAxiosError(error)) {
result = error.response?.data
}
const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname
logger.warn({ trace: error.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`)
logger.warn(
{ trace: error.stack, uploadResult: result },
`Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`
)
}
}
if(!urls) {
throw new Boom(
'Media upload failed on all hosts',
{ statusCode: 500 }
)
if (!urls) {
throw new Boom('Media upload failed on all hosts', { statusCode: 500 })
}
return urls
@@ -673,11 +647,7 @@ const getMediaRetryKey = (mediaKey: Buffer | Uint8Array) => {
/**
* Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
*/
export const encryptMediaRetryRequest = async(
key: proto.IMessageKey,
mediaKey: Buffer | Uint8Array,
meId: string
) => {
export const encryptMediaRetryRequest = async (key: proto.IMessageKey, mediaKey: Buffer | Uint8Array, meId: string) => {
const recp: proto.IServerErrorReceipt = { stanzaId: key.id }
const recpBuffer = proto.ServerErrorReceipt.encode(recp).finish()
@@ -698,17 +668,17 @@ export const encryptMediaRetryRequest = async(
// keeping it here to maintain parity with WA Web
{
tag: 'encrypt',
attrs: { },
attrs: {},
content: [
{ tag: 'enc_p', attrs: { }, content: ciphertext },
{ tag: 'enc_iv', attrs: { }, content: iv }
{ tag: 'enc_p', attrs: {}, content: ciphertext },
{ tag: 'enc_iv', attrs: {}, content: iv }
]
},
{
tag: 'rmr',
attrs: {
jid: key.remoteJid!,
'from_me': (!!key.fromMe).toString(),
from_me: (!!key.fromMe).toString(),
// @ts-ignore
participant: key.participant || undefined
}
@@ -732,17 +702,17 @@ export const decodeMediaRetryNode = (node: BinaryNode) => {
}
const errorNode = getBinaryNodeChild(node, 'error')
if(errorNode) {
if (errorNode) {
const errorCode = +errorNode.attrs.code
event.error = new Boom(
`Failed to re-upload media (${errorCode})`,
{ data: errorNode.attrs, statusCode: getStatusCodeForMediaRetry(errorCode) }
)
event.error = new Boom(`Failed to re-upload media (${errorCode})`, {
data: errorNode.attrs,
statusCode: getStatusCodeForMediaRetry(errorCode)
})
} else {
const encryptedInfoNode = getBinaryNodeChild(node, 'encrypt')
const ciphertext = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_p')
const iv = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_iv')
if(ciphertext && iv) {
if (ciphertext && iv) {
event.media = { ciphertext, iv }
} else {
event.error = new Boom('Failed to re-upload media (missing ciphertext)', { statusCode: 404 })
@@ -752,8 +722,8 @@ export const decodeMediaRetryNode = (node: BinaryNode) => {
return event
}
export const decryptMediaRetryData = async(
{ ciphertext, iv }: { ciphertext: Uint8Array, iv: Uint8Array },
export const decryptMediaRetryData = async (
{ ciphertext, iv }: { ciphertext: Uint8Array; iv: Uint8Array },
mediaKey: Uint8Array,
msgId: string
) => {
@@ -768,5 +738,5 @@ const MEDIA_RETRY_STATUS_MAP = {
[proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
[proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
[proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
[proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418,
} as const
[proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418
} as const

File diff suppressed because it is too large Load Diff

View File

@@ -27,7 +27,7 @@ export const makeNoiseHandler = ({
logger = logger.child({ class: 'ns' })
const authenticate = (data: Uint8Array) => {
if(!isFinished) {
if (!isFinished) {
hash = sha256(Buffer.concat([hash, data]))
}
}
@@ -47,7 +47,7 @@ export const makeNoiseHandler = ({
const iv = generateIV(isFinished ? readCounter : writeCounter)
const result = aesDecryptGCM(ciphertext, decKey, iv, hash)
if(isFinished) {
if (isFinished) {
readCounter += 1
} else {
writeCounter += 1
@@ -57,12 +57,12 @@ export const makeNoiseHandler = ({
return result
}
const localHKDF = async(data: Uint8Array) => {
const localHKDF = async (data: Uint8Array) => {
const key = await hkdf(Buffer.from(data), 64, { salt, info: '' })
return [key.slice(0, 32), key.slice(32)]
}
const mixIntoKey = async(data: Uint8Array) => {
const mixIntoKey = async (data: Uint8Array) => {
const [write, read] = await localHKDF(data)
salt = write
encKey = read
@@ -71,7 +71,7 @@ export const makeNoiseHandler = ({
writeCounter = 0
}
const finishInit = async() => {
const finishInit = async () => {
const [write, read] = await localHKDF(new Uint8Array(0))
encKey = write
decKey = read
@@ -102,7 +102,7 @@ export const makeNoiseHandler = ({
authenticate,
mixIntoKey,
finishInit,
processHandshake: async({ serverHello }: proto.HandshakeMessage, noiseKey: KeyPair) => {
processHandshake: async ({ serverHello }: proto.HandshakeMessage, noiseKey: KeyPair) => {
authenticate(serverHello!.ephemeral!)
await mixIntoKey(Curve.sharedKey(privateKey, serverHello!.ephemeral!))
@@ -115,7 +115,7 @@ export const makeNoiseHandler = ({
const { issuerSerial } = proto.CertChain.NoiseCertificate.Details.decode(certIntermediate!.details!)
if(issuerSerial !== WA_CERT_DETAILS.SERIAL) {
if (issuerSerial !== WA_CERT_DETAILS.SERIAL) {
throw new Boom('certification match failed', { statusCode: 400 })
}
@@ -125,13 +125,13 @@ export const makeNoiseHandler = ({
return keyEnc
},
encodeFrame: (data: Buffer | Uint8Array) => {
if(isFinished) {
if (isFinished) {
data = encrypt(data)
}
let header: Buffer
if(routingInfo) {
if (routingInfo) {
header = Buffer.alloc(7)
header.write('ED', 0, 'utf8')
header.writeUint8(0, 2)
@@ -146,7 +146,7 @@ export const makeNoiseHandler = ({
const introSize = sentIntro ? 0 : header.length
const frame = Buffer.alloc(introSize + 3 + data.byteLength)
if(!sentIntro) {
if (!sentIntro) {
frame.set(header)
sentIntro = true
}
@@ -157,26 +157,26 @@ export const makeNoiseHandler = ({
return frame
},
decodeFrame: async(newData: Buffer | Uint8Array, onFrame: (buff: Uint8Array | BinaryNode) => void) => {
decodeFrame: async (newData: Buffer | Uint8Array, onFrame: (buff: Uint8Array | BinaryNode) => void) => {
// the binary protocol uses its own framing mechanism
// on top of the WS frames
// so we get this data and separate out the frames
const getBytesSize = () => {
if(inBytes.length >= 3) {
if (inBytes.length >= 3) {
return (inBytes.readUInt8() << 16) | inBytes.readUInt16BE(1)
}
}
inBytes = Buffer.concat([ inBytes, newData ])
inBytes = Buffer.concat([inBytes, newData])
logger.trace(`recv ${newData.length} bytes, total recv ${inBytes.length} bytes`)
let size = getBytesSize()
while(size && inBytes.length >= size + 3) {
while (size && inBytes.length >= size + 3) {
let frame: Uint8Array | BinaryNode = inBytes.slice(3, size + 3)
inBytes = inBytes.slice(size + 3)
if(isFinished) {
if (isFinished) {
const result = decrypt(frame)
frame = await decodeBinaryNode(result)
}
@@ -188,4 +188,4 @@ export const makeNoiseHandler = ({
}
}
}
}
}

View File

@@ -1,6 +1,17 @@
import { AxiosRequestConfig } from 'axios'
import { proto } from '../../WAProto'
import { AuthenticationCreds, BaileysEventEmitter, CacheStore, Chat, GroupMetadata, ParticipantAction, RequestJoinAction, RequestJoinMethod, SignalKeyStoreWithTransaction, WAMessageStubType } from '../Types'
import {
AuthenticationCreds,
BaileysEventEmitter,
CacheStore,
Chat,
GroupMetadata,
ParticipantAction,
RequestJoinAction,
RequestJoinMethod,
SignalKeyStoreWithTransaction,
WAMessageStubType
} from '../Types'
import { getContentType, normalizeMessageContent } from '../Utils/messages'
import { areJidsSameUser, isJidBroadcast, isJidStatusBroadcast, jidNormalizedUser } from '../WABinary'
import { aesDecryptGCM, hmacSign } from './crypto'
@@ -25,9 +36,7 @@ const REAL_MSG_STUB_TYPES = new Set([
WAMessageStubType.CALL_MISSED_VOICE
])
const REAL_MSG_REQ_ME_STUB_TYPES = new Set([
WAMessageStubType.GROUP_PARTICIPANT_ADD
])
const REAL_MSG_REQ_ME_STUB_TYPES = new Set([WAMessageStubType.GROUP_PARTICIPANT_ADD])
/** Cleans a received message to further processing */
export const cleanMessage = (message: proto.IWebMessageInfo, meId: string) => {
@@ -36,25 +45,25 @@ export const cleanMessage = (message: proto.IWebMessageInfo, meId: string) => {
message.key.participant = message.key.participant ? jidNormalizedUser(message.key.participant) : undefined
const content = normalizeMessageContent(message.message)
// if the message has a reaction, ensure fromMe & remoteJid are from our perspective
if(content?.reactionMessage) {
if (content?.reactionMessage) {
normaliseKey(content.reactionMessage.key!)
}
if(content?.pollUpdateMessage) {
if (content?.pollUpdateMessage) {
normaliseKey(content.pollUpdateMessage.pollCreationMessageKey!)
}
function normaliseKey(msgKey: proto.IMessageKey) {
// if the reaction is from another user
// we've to correctly map the key to this user's perspective
if(!message.key.fromMe) {
if (!message.key.fromMe) {
// if the sender believed the message being reacted to is not from them
// we've to correct the key to be from them, or some other participant
msgKey.fromMe = !msgKey.fromMe
? areJidsSameUser(msgKey.participant || msgKey.remoteJid!, meId)
// if the message being reacted to, was from them
// fromMe automatically becomes false
: false
: // if the message being reacted to, was from them
// fromMe automatically becomes false
false
// set the remoteJid to being the same as the chat the message came from
msgKey.remoteJid = message.key.remoteJid
// set participant of the message
@@ -67,33 +76,26 @@ export const isRealMessage = (message: proto.IWebMessageInfo, meId: string) => {
const normalizedContent = normalizeMessageContent(message.message)
const hasSomeContent = !!getContentType(normalizedContent)
return (
!!normalizedContent
|| REAL_MSG_STUB_TYPES.has(message.messageStubType!)
|| (
REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType!)
&& message.messageStubParameters?.some(p => areJidsSameUser(meId, p))
)
(!!normalizedContent ||
REAL_MSG_STUB_TYPES.has(message.messageStubType!) ||
(REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType!) &&
message.messageStubParameters?.some(p => areJidsSameUser(meId, p)))) &&
hasSomeContent &&
!normalizedContent?.protocolMessage &&
!normalizedContent?.reactionMessage &&
!normalizedContent?.pollUpdateMessage
)
&& hasSomeContent
&& !normalizedContent?.protocolMessage
&& !normalizedContent?.reactionMessage
&& !normalizedContent?.pollUpdateMessage
}
export const shouldIncrementChatUnread = (message: proto.IWebMessageInfo) => (
export const shouldIncrementChatUnread = (message: proto.IWebMessageInfo) =>
!message.key.fromMe && !message.messageStubType
)
/**
* Get the ID of the chat from the given key.
* Typically -- that'll be the remoteJid, but for broadcasts, it'll be the participant
*/
export const getChatId = ({ remoteJid, participant, fromMe }: proto.IMessageKey) => {
if(
isJidBroadcast(remoteJid!)
&& !isJidStatusBroadcast(remoteJid!)
&& !fromMe
) {
if (isJidBroadcast(remoteJid!) && !isJidStatusBroadcast(remoteJid!) && !fromMe) {
return participant!
}
@@ -119,22 +121,15 @@ type PollContext = {
*/
export function decryptPollVote(
{ encPayload, encIv }: proto.Message.IPollEncValue,
{
pollCreatorJid,
pollMsgId,
pollEncKey,
voterJid,
}: PollContext
{ pollCreatorJid, pollMsgId, pollEncKey, voterJid }: PollContext
) {
const sign = Buffer.concat(
[
toBinary(pollMsgId),
toBinary(pollCreatorJid),
toBinary(voterJid),
toBinary('Poll Vote'),
new Uint8Array([1])
]
)
const sign = Buffer.concat([
toBinary(pollMsgId),
toBinary(pollCreatorJid),
toBinary(voterJid),
toBinary('Poll Vote'),
new Uint8Array([1])
])
const key0 = hmacSign(pollEncKey, new Uint8Array(32), 'sha256')
const decKey = hmacSign(sign, key0, 'sha256')
@@ -148,17 +143,9 @@ export function decryptPollVote(
}
}
const processMessage = async(
const processMessage = async (
message: proto.IWebMessageInfo,
{
shouldProcessHistoryMsg,
placeholderResendCache,
ev,
creds,
keyStore,
logger,
options
}: ProcessMessageContext
{ shouldProcessHistoryMsg, placeholderResendCache, ev, creds, keyStore, logger, options }: ProcessMessageContext
) => {
const meId = creds.me!.id
const { accountSettings } = creds
@@ -166,11 +153,11 @@ const processMessage = async(
const chat: Partial<Chat> = { id: jidNormalizedUser(getChatId(message.key)) }
const isRealMsg = isRealMessage(message, meId)
if(isRealMsg) {
if (isRealMsg) {
chat.messages = [{ message }]
chat.conversationTimestamp = toNumber(message.messageTimestamp)
// only increment unread count if not CIPHERTEXT and from another person
if(shouldIncrementChatUnread(message)) {
if (shouldIncrementChatUnread(message)) {
chat.unreadCount = (chat.unreadCount || 0) + 1
}
}
@@ -179,63 +166,56 @@ const processMessage = async(
// unarchive chat if it's a real message, or someone reacted to our message
// and we've the unarchive chats setting on
if(
(isRealMsg || content?.reactionMessage?.key?.fromMe)
&& accountSettings?.unarchiveChats
) {
if ((isRealMsg || content?.reactionMessage?.key?.fromMe) && accountSettings?.unarchiveChats) {
chat.archived = false
chat.readOnly = false
}
const protocolMsg = content?.protocolMessage
if(protocolMsg) {
if (protocolMsg) {
switch (protocolMsg.type) {
case proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
const histNotification = protocolMsg.historySyncNotification!
const process = shouldProcessHistoryMsg
const isLatest = !creds.processedHistoryMessages?.length
case proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
const histNotification = protocolMsg.historySyncNotification!
const process = shouldProcessHistoryMsg
const isLatest = !creds.processedHistoryMessages?.length
logger?.info({
histNotification,
process,
id: message.key.id,
isLatest,
}, 'got history notification')
logger?.info(
{
histNotification,
process,
id: message.key.id,
isLatest
},
'got history notification'
)
if(process) {
if(histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND) {
ev.emit('creds.update', {
processedHistoryMessages: [
...(creds.processedHistoryMessages || []),
{ key: message.key, messageTimestamp: message.messageTimestamp }
]
if (process) {
if (histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND) {
ev.emit('creds.update', {
processedHistoryMessages: [
...(creds.processedHistoryMessages || []),
{ key: message.key, messageTimestamp: message.messageTimestamp }
]
})
}
const data = await downloadAndProcessHistorySyncNotification(histNotification, options)
ev.emit('messaging-history.set', {
...data,
isLatest: histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND ? isLatest : undefined,
peerDataRequestSessionId: histNotification.peerDataRequestSessionId
})
}
const data = await downloadAndProcessHistorySyncNotification(
histNotification,
options
)
ev.emit('messaging-history.set', {
...data,
isLatest:
histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND
? isLatest
: undefined,
peerDataRequestSessionId: histNotification.peerDataRequestSessionId
})
}
break
case proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
const keys = protocolMsg.appStateSyncKeyShare!.keys
if(keys?.length) {
let newAppStateSyncKeyId = ''
await keyStore.transaction(
async() => {
break
case proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
const keys = protocolMsg.appStateSyncKeyShare!.keys
if (keys?.length) {
let newAppStateSyncKeyId = ''
await keyStore.transaction(async () => {
const newKeys: string[] = []
for(const { keyData, keyId } of keys) {
for (const { keyData, keyId } of keys) {
const strKeyId = Buffer.from(keyId!.keyId!).toString('base64')
newKeys.push(strKeyId)
@@ -244,65 +224,59 @@ const processMessage = async(
newAppStateSyncKeyId = strKeyId
}
logger?.info(
{ newAppStateSyncKeyId, newKeys },
'injecting new app state sync keys'
)
}
)
logger?.info({ newAppStateSyncKeyId, newKeys }, 'injecting new app state sync keys')
})
ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId })
} else {
logger?.info({ protocolMsg }, 'recv app state sync with 0 keys')
}
break
case proto.Message.ProtocolMessage.Type.REVOKE:
ev.emit('messages.update', [
{
key: {
...message.key,
id: protocolMsg.key!.id
},
update: { message: null, messageStubType: WAMessageStubType.REVOKE, key: message.key }
ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId })
} else {
logger?.info({ protocolMsg }, 'recv app state sync with 0 keys')
}
])
break
case proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
Object.assign(chat, {
ephemeralSettingTimestamp: toNumber(message.messageTimestamp),
ephemeralExpiration: protocolMsg.ephemeralExpiration || null
})
break
case proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
const response = protocolMsg.peerDataOperationRequestResponseMessage!
if(response) {
placeholderResendCache?.del(response.stanzaId!)
// TODO: IMPLEMENT HISTORY SYNC ETC (sticker uploads etc.).
const { peerDataOperationResult } = response
for(const result of peerDataOperationResult!) {
const { placeholderMessageResendResponse: retryResponse } = result
//eslint-disable-next-line max-depth
if(retryResponse) {
const webMessageInfo = proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes!)
// wait till another upsert event is available, don't want it to be part of the PDO response message
setTimeout(() => {
ev.emit('messages.upsert', {
messages: [webMessageInfo],
type: 'notify',
requestId: response.stanzaId!
})
}, 500)
}
}
}
case proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
ev.emit(
'messages.update',
[
break
case proto.Message.ProtocolMessage.Type.REVOKE:
ev.emit('messages.update', [
{
// flip the sender / fromMe properties because they're in the perspective of the sender
key: {
...message.key,
id: protocolMsg.key!.id
},
update: { message: null, messageStubType: WAMessageStubType.REVOKE, key: message.key }
}
])
break
case proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
Object.assign(chat, {
ephemeralSettingTimestamp: toNumber(message.messageTimestamp),
ephemeralExpiration: protocolMsg.ephemeralExpiration || null
})
break
case proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
const response = protocolMsg.peerDataOperationRequestResponseMessage!
if (response) {
placeholderResendCache?.del(response.stanzaId!)
// TODO: IMPLEMENT HISTORY SYNC ETC (sticker uploads etc.).
const { peerDataOperationResult } = response
for (const result of peerDataOperationResult!) {
const { placeholderMessageResendResponse: retryResponse } = result
//eslint-disable-next-line max-depth
if (retryResponse) {
const webMessageInfo = proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes!)
// wait till another upsert event is available, don't want it to be part of the PDO response message
setTimeout(() => {
ev.emit('messages.upsert', {
messages: [webMessageInfo],
type: 'notify',
requestId: response.stanzaId!
})
}, 500)
}
}
}
case proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
ev.emit('messages.update', [
{
// flip the sender / fromMe properties because they're in the perspective of the sender
key: { ...message.key, id: protocolMsg.key?.id },
update: {
message: {
@@ -315,26 +289,26 @@ const processMessage = async(
: message.messageTimestamp
}
}
]
)
break
])
break
}
} else if(content?.reactionMessage) {
} else if (content?.reactionMessage) {
const reaction: proto.IReaction = {
...content.reactionMessage,
key: message.key,
key: message.key
}
ev.emit('messages.reaction', [{
reaction,
key: content.reactionMessage?.key!,
}])
} else if(message.messageStubType) {
ev.emit('messages.reaction', [
{
reaction,
key: content.reactionMessage?.key!
}
])
} else if (message.messageStubType) {
const jid = message.key?.remoteJid!
//let actor = whatsappID (message.participant)
let participants: string[]
const emitParticipantsUpdate = (action: ParticipantAction) => (
const emitParticipantsUpdate = (action: ParticipantAction) =>
ev.emit('group-participants.update', { id: jid, author: message.participant!, participants, action })
)
const emitGroupUpdate = (update: Partial<GroupMetadata>) => {
ev.emit('groups.update', [{ id: jid, ...update, author: message.participant ?? undefined }])
}
@@ -346,76 +320,75 @@ const processMessage = async(
const participantsIncludesMe = () => participants.find(jid => areJidsSameUser(meId, jid))
switch (message.messageStubType) {
case WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
participants = message.messageStubParameters || []
emitParticipantsUpdate('modify')
break
case WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
case WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
participants = message.messageStubParameters || []
emitParticipantsUpdate('remove')
// mark the chat read only if you left the group
if(participantsIncludesMe()) {
chat.readOnly = true
}
case WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
participants = message.messageStubParameters || []
emitParticipantsUpdate('modify')
break
case WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
case WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
participants = message.messageStubParameters || []
emitParticipantsUpdate('remove')
// mark the chat read only if you left the group
if (participantsIncludesMe()) {
chat.readOnly = true
}
break
case WAMessageStubType.GROUP_PARTICIPANT_ADD:
case WAMessageStubType.GROUP_PARTICIPANT_INVITE:
case WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
participants = message.messageStubParameters || []
if(participantsIncludesMe()) {
chat.readOnly = false
}
break
case WAMessageStubType.GROUP_PARTICIPANT_ADD:
case WAMessageStubType.GROUP_PARTICIPANT_INVITE:
case WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
participants = message.messageStubParameters || []
if (participantsIncludesMe()) {
chat.readOnly = false
}
emitParticipantsUpdate('add')
break
case WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
participants = message.messageStubParameters || []
emitParticipantsUpdate('demote')
break
case WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
participants = message.messageStubParameters || []
emitParticipantsUpdate('promote')
break
case WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
const announceValue = message.messageStubParameters?.[0]
emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' })
break
case WAMessageStubType.GROUP_CHANGE_RESTRICT:
const restrictValue = message.messageStubParameters?.[0]
emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' })
break
case WAMessageStubType.GROUP_CHANGE_SUBJECT:
const name = message.messageStubParameters?.[0]
chat.name = name
emitGroupUpdate({ subject: name })
break
case WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
const description = message.messageStubParameters?.[0]
chat.description = description
emitGroupUpdate({ desc: description })
break
case WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
const code = message.messageStubParameters?.[0]
emitGroupUpdate({ inviteCode: code })
break
case WAMessageStubType.GROUP_MEMBER_ADD_MODE:
const memberAddValue = message.messageStubParameters?.[0]
emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' })
break
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
const approvalMode = message.messageStubParameters?.[0]
emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' })
break
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
const participant = message.messageStubParameters?.[0] as string
const action = message.messageStubParameters?.[1] as RequestJoinAction
const method = message.messageStubParameters?.[2] as RequestJoinMethod
emitGroupRequestJoin(participant, action, method)
break
emitParticipantsUpdate('add')
break
case WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
participants = message.messageStubParameters || []
emitParticipantsUpdate('demote')
break
case WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
participants = message.messageStubParameters || []
emitParticipantsUpdate('promote')
break
case WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
const announceValue = message.messageStubParameters?.[0]
emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' })
break
case WAMessageStubType.GROUP_CHANGE_RESTRICT:
const restrictValue = message.messageStubParameters?.[0]
emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' })
break
case WAMessageStubType.GROUP_CHANGE_SUBJECT:
const name = message.messageStubParameters?.[0]
chat.name = name
emitGroupUpdate({ subject: name })
break
case WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
const description = message.messageStubParameters?.[0]
chat.description = description
emitGroupUpdate({ desc: description })
break
case WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
const code = message.messageStubParameters?.[0]
emitGroupUpdate({ inviteCode: code })
break
case WAMessageStubType.GROUP_MEMBER_ADD_MODE:
const memberAddValue = message.messageStubParameters?.[0]
emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' })
break
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
const approvalMode = message.messageStubParameters?.[0]
emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' })
break
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
const participant = message.messageStubParameters?.[0] as string
const action = message.messageStubParameters?.[1] as RequestJoinAction
const method = message.messageStubParameters?.[2] as RequestJoinMethod
emitGroupRequestJoin(participant, action, method)
break
}
} /* else if(content?.pollUpdateMessage) {
const creationMsgKey = content.pollUpdateMessage.pollCreationMessageKey!
// we need to fetch the poll creation message to get the poll enc key
@@ -466,7 +439,7 @@ const processMessage = async(
}
} */
if(Object.keys(chat).length > 1) {
if (Object.keys(chat).length > 1) {
ev.emit('chats.update', [chat])
}
}

View File

@@ -1,25 +1,39 @@
import { chunk } from 'lodash'
import { KEY_BUNDLE_TYPE } from '../Defaults'
import { SignalRepository } from '../Types'
import { AuthenticationCreds, AuthenticationState, KeyPair, SignalIdentity, SignalKeyStore, SignedKeyPair } from '../Types/Auth'
import { assertNodeErrorFree, BinaryNode, getBinaryNodeChild, getBinaryNodeChildBuffer, getBinaryNodeChildren, getBinaryNodeChildUInt, jidDecode, JidWithDevice, S_WHATSAPP_NET } from '../WABinary'
import {
AuthenticationCreds,
AuthenticationState,
KeyPair,
SignalIdentity,
SignalKeyStore,
SignedKeyPair
} from '../Types/Auth'
import {
assertNodeErrorFree,
BinaryNode,
getBinaryNodeChild,
getBinaryNodeChildBuffer,
getBinaryNodeChildren,
getBinaryNodeChildUInt,
jidDecode,
JidWithDevice,
S_WHATSAPP_NET
} from '../WABinary'
import { DeviceListData, ParsedDeviceInfo, USyncQueryResultList } from '../WAUSync'
import { Curve, generateSignalPubKey } from './crypto'
import { encodeBigEndian } from './generics'
export const createSignalIdentity = (
wid: string,
accountSignatureKey: Uint8Array
): SignalIdentity => {
export const createSignalIdentity = (wid: string, accountSignatureKey: Uint8Array): SignalIdentity => {
return {
identifier: { name: wid, deviceId: 0 },
identifierKey: generateSignalPubKey(accountSignatureKey)
}
}
export const getPreKeys = async({ get }: SignalKeyStore, min: number, limit: number) => {
export const getPreKeys = async ({ get }: SignalKeyStore, min: number, limit: number) => {
const idList: string[] = []
for(let id = min; id < limit;id++) {
for (let id = min; id < limit; id++) {
idList.push(id.toString())
}
@@ -30,9 +44,9 @@ export const generateOrGetPreKeys = (creds: AuthenticationCreds, range: number)
const avaliable = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId
const remaining = range - avaliable
const lastPreKeyId = creds.nextPreKeyId + remaining - 1
const newPreKeys: { [id: number]: KeyPair } = { }
if(remaining > 0) {
for(let i = creds.nextPreKeyId;i <= lastPreKeyId;i++) {
const newPreKeys: { [id: number]: KeyPair } = {}
if (remaining > 0) {
for (let i = creds.nextPreKeyId; i <= lastPreKeyId; i++) {
newPreKeys[i] = Curve.generateKeyPair()
}
}
@@ -40,46 +54,40 @@ export const generateOrGetPreKeys = (creds: AuthenticationCreds, range: number)
return {
newPreKeys,
lastPreKeyId,
preKeysRange: [creds.firstUnuploadedPreKeyId, range] as const,
preKeysRange: [creds.firstUnuploadedPreKeyId, range] as const
}
}
export const xmppSignedPreKey = (key: SignedKeyPair): BinaryNode => (
{
tag: 'skey',
attrs: { },
content: [
{ tag: 'id', attrs: { }, content: encodeBigEndian(key.keyId, 3) },
{ tag: 'value', attrs: { }, content: key.keyPair.public },
{ tag: 'signature', attrs: { }, content: key.signature }
]
}
)
export const xmppSignedPreKey = (key: SignedKeyPair): BinaryNode => ({
tag: 'skey',
attrs: {},
content: [
{ tag: 'id', attrs: {}, content: encodeBigEndian(key.keyId, 3) },
{ tag: 'value', attrs: {}, content: key.keyPair.public },
{ tag: 'signature', attrs: {}, content: key.signature }
]
})
export const xmppPreKey = (pair: KeyPair, id: number): BinaryNode => (
{
tag: 'key',
attrs: { },
content: [
{ tag: 'id', attrs: { }, content: encodeBigEndian(id, 3) },
{ tag: 'value', attrs: { }, content: pair.public }
]
}
)
export const xmppPreKey = (pair: KeyPair, id: number): BinaryNode => ({
tag: 'key',
attrs: {},
content: [
{ tag: 'id', attrs: {}, content: encodeBigEndian(id, 3) },
{ tag: 'value', attrs: {}, content: pair.public }
]
})
export const parseAndInjectE2ESessions = async(
node: BinaryNode,
repository: SignalRepository
) => {
const extractKey = (key: BinaryNode) => (
key ? ({
keyId: getBinaryNodeChildUInt(key, 'id', 3)!,
publicKey: generateSignalPubKey(getBinaryNodeChildBuffer(key, 'value')!),
signature: getBinaryNodeChildBuffer(key, 'signature')!,
}) : undefined
)
export const parseAndInjectE2ESessions = async (node: BinaryNode, repository: SignalRepository) => {
const extractKey = (key: BinaryNode) =>
key
? {
keyId: getBinaryNodeChildUInt(key, 'id', 3)!,
publicKey: generateSignalPubKey(getBinaryNodeChildBuffer(key, 'value')!),
signature: getBinaryNodeChildBuffer(key, 'signature')!
}
: undefined
const nodes = getBinaryNodeChildren(getBinaryNodeChild(node, 'list'), 'user')
for(const node of nodes) {
for (const node of nodes) {
assertNodeErrorFree(node)
}
@@ -90,27 +98,25 @@ export const parseAndInjectE2ESessions = async(
// It's rare case when you need to E2E sessions for so many users, but it's possible
const chunkSize = 100
const chunks = chunk(nodes, chunkSize)
for(const nodesChunk of chunks) {
for (const nodesChunk of chunks) {
await Promise.all(
nodesChunk.map(
async node => {
const signedKey = getBinaryNodeChild(node, 'skey')!
const key = getBinaryNodeChild(node, 'key')!
const identity = getBinaryNodeChildBuffer(node, 'identity')!
const jid = node.attrs.jid
const registrationId = getBinaryNodeChildUInt(node, 'registration', 4)
nodesChunk.map(async node => {
const signedKey = getBinaryNodeChild(node, 'skey')!
const key = getBinaryNodeChild(node, 'key')!
const identity = getBinaryNodeChildBuffer(node, 'identity')!
const jid = node.attrs.jid
const registrationId = getBinaryNodeChildUInt(node, 'registration', 4)
await repository.injectE2ESession({
jid,
session: {
registrationId: registrationId!,
identityKey: generateSignalPubKey(identity),
signedPreKey: extractKey(signedKey)!,
preKey: extractKey(key)!
}
})
}
)
await repository.injectE2ESession({
jid,
session: {
registrationId: registrationId!,
identityKey: generateSignalPubKey(identity),
signedPreKey: extractKey(signedKey)!,
preKey: extractKey(key)!
}
})
})
)
}
}
@@ -120,14 +126,13 @@ export const extractDeviceJids = (result: USyncQueryResultList[], myJid: string,
const extracted: JidWithDevice[] = []
for(const userResult of result) {
const { devices, id } = userResult as { devices: ParsedDeviceInfo, id: string }
for (const userResult of result) {
const { devices, id } = userResult as { devices: ParsedDeviceInfo; id: string }
const { user } = jidDecode(id)!
const deviceList = devices?.deviceList as DeviceListData[]
if(Array.isArray(deviceList)) {
for(const { id: device, keyIndex } of deviceList) {
if(
if (Array.isArray(deviceList)) {
for (const { id: device, keyIndex } of deviceList) {
if (
(!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
(myUser !== 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
@@ -145,7 +150,7 @@ export const extractDeviceJids = (result: USyncQueryResultList[], myJid: string,
* get the next N keys for upload or processing
* @param count number of pre-keys to get or generate
*/
export const getNextPreKeys = async({ creds, keys }: AuthenticationState, count: number) => {
export const getNextPreKeys = async ({ creds, keys }: AuthenticationState, count: number) => {
const { newPreKeys, lastPreKeyId, preKeysRange } = generateOrGetPreKeys(creds, count)
const update: Partial<AuthenticationCreds> = {
@@ -160,7 +165,7 @@ export const getNextPreKeys = async({ creds, keys }: AuthenticationState, count:
return { update, preKeys }
}
export const getNextPreKeysNode = async(state: AuthenticationState, count: number) => {
export const getNextPreKeysNode = async (state: AuthenticationState, count: number) => {
const { creds } = state
const { update, preKeys } = await getNextPreKeys(state, count)
@@ -169,13 +174,13 @@ export const getNextPreKeysNode = async(state: AuthenticationState, count: numbe
attrs: {
xmlns: 'encrypt',
type: 'set',
to: S_WHATSAPP_NET,
to: S_WHATSAPP_NET
},
content: [
{ tag: 'registration', attrs: { }, content: encodeBigEndian(creds.registrationId) },
{ tag: 'type', attrs: { }, content: KEY_BUNDLE_TYPE },
{ tag: 'identity', attrs: { }, content: creds.signedIdentityKey.public },
{ tag: 'list', attrs: { }, content: Object.keys(preKeys).map(k => xmppPreKey(preKeys[+k], +k)) },
{ tag: 'registration', attrs: {}, content: encodeBigEndian(creds.registrationId) },
{ tag: 'type', attrs: {}, content: KEY_BUNDLE_TYPE },
{ tag: 'identity', attrs: {}, content: creds.signedIdentityKey.public },
{ tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k => xmppPreKey(preKeys[+k], +k)) },
xmppSignedPreKey(creds.signedPreKey)
]
}

View File

@@ -15,7 +15,7 @@ const fileLocks = new Map<string, Mutex>()
// Get or create a mutex for a specific file path
const getFileLock = (path: string): Mutex => {
let mutex = fileLocks.get(path)
if(!mutex) {
if (!mutex) {
mutex = new Mutex()
fileLocks.set(path, mutex)
}
@@ -30,13 +30,15 @@ const getFileLock = (path: string): Mutex => {
* Again, I wouldn't endorse this for any production level use other than perhaps a bot.
* Would recommend writing an auth state for use with a proper SQL or No-SQL DB
* */
export const useMultiFileAuthState = async(folder: string): Promise<{ state: AuthenticationState, saveCreds: () => Promise<void> }> => {
export const useMultiFileAuthState = async (
folder: string
): Promise<{ state: AuthenticationState; saveCreds: () => Promise<void> }> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const writeData = async(data: any, file: string) => {
const writeData = async (data: any, file: string) => {
const filePath = join(folder, fixFileName(file)!)
const mutex = getFileLock(filePath)
return mutex.acquire().then(async(release) => {
return mutex.acquire().then(async release => {
try {
await writeFile(filePath, JSON.stringify(data, BufferJSON.replacer))
} finally {
@@ -45,12 +47,12 @@ export const useMultiFileAuthState = async(folder: string): Promise<{ state: Aut
})
}
const readData = async(file: string) => {
const readData = async (file: string) => {
try {
const filePath = join(folder, fixFileName(file)!)
const mutex = getFileLock(filePath)
return await mutex.acquire().then(async(release) => {
return await mutex.acquire().then(async release => {
try {
const data = await readFile(filePath, { encoding: 'utf-8' })
return JSON.parse(data, BufferJSON.reviver)
@@ -58,32 +60,33 @@ export const useMultiFileAuthState = async(folder: string): Promise<{ state: Aut
release()
}
})
} catch(error) {
} catch (error) {
return null
}
}
const removeData = async(file: string) => {
const removeData = async (file: string) => {
try {
const filePath = join(folder, fixFileName(file)!)
const mutex = getFileLock(filePath)
return mutex.acquire().then(async(release) => {
return mutex.acquire().then(async release => {
try {
await unlink(filePath)
} catch{
} catch {
} finally {
release()
}
})
} catch{
}
} catch {}
}
const folderInfo = await stat(folder).catch(() => { })
if(folderInfo) {
if(!folderInfo.isDirectory()) {
throw new Error(`found something that is not a directory at ${folder}, either delete it or specify a different location`)
const folderInfo = await stat(folder).catch(() => {})
if (folderInfo) {
if (!folderInfo.isDirectory()) {
throw new Error(
`found something that is not a directory at ${folder}, either delete it or specify a different location`
)
}
} else {
await mkdir(folder, { recursive: true })
@@ -91,33 +94,31 @@ export const useMultiFileAuthState = async(folder: string): Promise<{ state: Aut
const fixFileName = (file?: string) => file?.replace(/\//g, '__')?.replace(/:/g, '-')
const creds: AuthenticationCreds = await readData('creds.json') || initAuthCreds()
const creds: AuthenticationCreds = (await readData('creds.json')) || initAuthCreds()
return {
state: {
creds,
keys: {
get: async(type, ids) => {
const data: { [_: string]: SignalDataTypeMap[typeof type] } = { }
get: async (type, ids) => {
const data: { [_: string]: SignalDataTypeMap[typeof type] } = {}
await Promise.all(
ids.map(
async id => {
let value = await readData(`${type}-${id}.json`)
if(type === 'app-state-sync-key' && value) {
value = proto.Message.AppStateSyncKeyData.fromObject(value)
}
data[id] = value
ids.map(async id => {
let value = await readData(`${type}-${id}.json`)
if (type === 'app-state-sync-key' && value) {
value = proto.Message.AppStateSyncKeyData.fromObject(value)
}
)
data[id] = value
})
)
return data
},
set: async(data) => {
set: async data => {
const tasks: Promise<void>[] = []
for(const category in data) {
for(const id in data[category]) {
for (const category in data) {
for (const id in data[category]) {
const value = data[category][id]
const file = `${category}-${id}.json`
tasks.push(value ? writeData(value, file) : removeData(file))
@@ -128,8 +129,8 @@ export const useMultiFileAuthState = async(folder: string): Promise<{ state: Aut
}
}
},
saveCreds: async() => {
saveCreds: async () => {
return writeData(creds, 'creds.json')
}
}
}
}

View File

@@ -13,7 +13,7 @@ const getUserAgent = (config: SocketConfig): proto.ClientPayload.IUserAgent => {
appVersion: {
primary: config.version[0],
secondary: config.version[1],
tertiary: config.version[2],
tertiary: config.version[2]
},
platform: proto.ClientPayload.UserAgent.Platform.WEB,
releaseChannel: proto.ClientPayload.UserAgent.ReleaseChannel.RELEASE,
@@ -23,30 +23,29 @@ const getUserAgent = (config: SocketConfig): proto.ClientPayload.IUserAgent => {
localeLanguageIso6391: 'en',
mnc: '000',
mcc: '000',
localeCountryIso31661Alpha2: config.countryCode,
localeCountryIso31661Alpha2: config.countryCode
}
}
const PLATFORM_MAP = {
'Mac OS': proto.ClientPayload.WebInfo.WebSubPlatform.DARWIN,
'Windows': proto.ClientPayload.WebInfo.WebSubPlatform.WIN32
Windows: proto.ClientPayload.WebInfo.WebSubPlatform.WIN32
}
const getWebInfo = (config: SocketConfig): proto.ClientPayload.IWebInfo => {
let webSubPlatform = proto.ClientPayload.WebInfo.WebSubPlatform.WEB_BROWSER
if(config.syncFullHistory && PLATFORM_MAP[config.browser[0]]) {
if (config.syncFullHistory && PLATFORM_MAP[config.browser[0]]) {
webSubPlatform = PLATFORM_MAP[config.browser[0]]
}
return { webSubPlatform }
}
const getClientPayload = (config: SocketConfig) => {
const payload: proto.IClientPayload = {
connectType: proto.ClientPayload.ConnectType.WIFI_UNKNOWN,
connectReason: proto.ClientPayload.ConnectReason.USER_ACTIVATED,
userAgent: getUserAgent(config),
userAgent: getUserAgent(config)
}
payload.webInfo = getWebInfo(config)
@@ -54,7 +53,6 @@ const getClientPayload = (config: SocketConfig) => {
return payload
}
export const generateLoginNode = (userJid: string, config: SocketConfig): proto.IClientPayload => {
const { user, device } = jidDecode(userJid)!
const payload: proto.IClientPayload = {
@@ -62,7 +60,7 @@ export const generateLoginNode = (userJid: string, config: SocketConfig): proto.
passive: false,
pull: true,
username: +user,
device: device,
device: device
}
return proto.ClientPayload.fromObject(payload)
}
@@ -85,7 +83,7 @@ export const generateRegistrationNode = (
const companion: proto.IDeviceProps = {
os: config.browser[0],
platformType: getPlatformType(config.browser[1]),
requireFullSync: config.syncFullHistory,
requireFullSync: config.syncFullHistory
}
const companionProto = proto.DeviceProps.encode(companion).finish()
@@ -102,8 +100,8 @@ export const generateRegistrationNode = (
eIdent: signedIdentityKey.public,
eSkeyId: encodeBigEndian(signedPreKey.keyId, 3),
eSkeyVal: signedPreKey.keyPair.public,
eSkeySig: signedPreKey.signature,
},
eSkeySig: signedPreKey.signature
}
}
return proto.ClientPayload.fromObject(registerPayload)
@@ -111,7 +109,11 @@ export const generateRegistrationNode = (
export const configureSuccessfulPairing = (
stanza: BinaryNode,
{ advSecretKey, signedIdentityKey, signalIdentities }: Pick<AuthenticationCreds, 'advSecretKey' | 'signedIdentityKey' | 'signalIdentities'>
{
advSecretKey,
signedIdentityKey,
signalIdentities
}: Pick<AuthenticationCreds, 'advSecretKey' | 'signedIdentityKey' | 'signalIdentities'>
) => {
const msgId = stanza.attrs.id
@@ -122,7 +124,7 @@ export const configureSuccessfulPairing = (
const deviceNode = getBinaryNodeChild(pairSuccessNode, 'device')
const businessNode = getBinaryNodeChild(pairSuccessNode, 'biz')
if(!deviceIdentityNode || !deviceNode) {
if (!deviceIdentityNode || !deviceNode) {
throw new Boom('Missing device-identity or device in pair success node', { data: stanza })
}
@@ -132,20 +134,20 @@ export const configureSuccessfulPairing = (
const { details, hmac } = proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content as Buffer)
// check HMAC matches
const advSign = hmacSign(details!, Buffer.from(advSecretKey, 'base64'))
if(Buffer.compare(hmac!, advSign) !== 0) {
if (Buffer.compare(hmac!, advSign) !== 0) {
throw new Boom('Invalid account signature')
}
const account = proto.ADVSignedDeviceIdentity.decode(details!)
const { accountSignatureKey, accountSignature, details: deviceDetails } = account
// verify the device signature matches
const accountMsg = Buffer.concat([ Buffer.from([6, 0]), deviceDetails!, signedIdentityKey.public ])
if(!Curve.verify(accountSignatureKey!, accountMsg, accountSignature!)) {
const accountMsg = Buffer.concat([Buffer.from([6, 0]), deviceDetails!, signedIdentityKey.public])
if (!Curve.verify(accountSignatureKey!, accountMsg, accountSignature!)) {
throw new Boom('Failed to verify account signature')
}
// sign the details with our identity key
const deviceMsg = Buffer.concat([ Buffer.from([6, 1]), deviceDetails!, signedIdentityKey.public, accountSignatureKey! ])
const deviceMsg = Buffer.concat([Buffer.from([6, 1]), deviceDetails!, signedIdentityKey.public, accountSignatureKey!])
account.deviceSignature = Curve.sign(signedIdentityKey.private, deviceMsg)
const identity = createSignalIdentity(jid, accountSignatureKey!)
@@ -158,12 +160,12 @@ export const configureSuccessfulPairing = (
attrs: {
to: S_WHATSAPP_NET,
type: 'result',
id: msgId,
id: msgId
},
content: [
{
tag: 'pair-device-sign',
attrs: { },
attrs: {},
content: [
{
tag: 'device-identity',
@@ -178,10 +180,7 @@ export const configureSuccessfulPairing = (
const authUpdate: Partial<AuthenticationCreds> = {
account,
me: { id: jid, name: bizName },
signalIdentities: [
...(signalIdentities || []),
identity
],
signalIdentities: [...(signalIdentities || []), identity],
platform: platformNode?.attrs.name
}
@@ -191,18 +190,13 @@ export const configureSuccessfulPairing = (
}
}
export const encodeSignedDeviceIdentity = (
account: proto.IADVSignedDeviceIdentity,
includeSignatureKey: boolean
) => {
export const encodeSignedDeviceIdentity = (account: proto.IADVSignedDeviceIdentity, includeSignatureKey: boolean) => {
account = { ...account }
// set to null if we are not to include the signature key
// or if we are including the signature key but it is empty
if(!includeSignatureKey || !account.accountSignatureKey?.length) {
if (!includeSignatureKey || !account.accountSignatureKey?.length) {
account.accountSignatureKey = null
}
return proto.ADVSignedDeviceIdentity
.encode(account)
.finish()
return proto.ADVSignedDeviceIdentity.encode(account).finish()
}

File diff suppressed because one or more lines are too long

View File

@@ -6,10 +6,11 @@ import type { BinaryNode, BinaryNodeCodingOptions } from './types'
const inflatePromise = promisify(inflate)
export const decompressingIfRequired = async(buffer: Buffer) => {
if(2 & buffer.readUInt8()) {
export const decompressingIfRequired = async (buffer: Buffer) => {
if (2 & buffer.readUInt8()) {
buffer = await inflatePromise(buffer.slice(1))
} else { // nodes with no compression have a 0x00 prefix, we remove that
} else {
// nodes with no compression have a 0x00 prefix, we remove that
buffer = buffer.slice(1)
}
@@ -24,7 +25,7 @@ export const decodeDecompressedBinaryNode = (
const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts
const checkEOS = (length: number) => {
if(indexRef.index + length > buffer.length) {
if (indexRef.index + length > buffer.length) {
throw new Error('end of stream')
}
}
@@ -54,7 +55,7 @@ export const decodeDecompressedBinaryNode = (
const readInt = (n: number, littleEndian = false) => {
checkEOS(n)
let val = 0
for(let i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
const shift = littleEndian ? i : n - 1 - i
val |= next() << (shift * 8)
}
@@ -68,7 +69,7 @@ export const decodeDecompressedBinaryNode = (
}
const unpackHex = (value: number) => {
if(value >= 0 && value < 16) {
if (value >= 0 && value < 16) {
return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10
}
@@ -76,26 +77,26 @@ export const decodeDecompressedBinaryNode = (
}
const unpackNibble = (value: number) => {
if(value >= 0 && value <= 9) {
if (value >= 0 && value <= 9) {
return '0'.charCodeAt(0) + value
}
switch (value) {
case 10:
return '-'.charCodeAt(0)
case 11:
return '.'.charCodeAt(0)
case 15:
return '\0'.charCodeAt(0)
default:
throw new Error('invalid nibble: ' + value)
case 10:
return '-'.charCodeAt(0)
case 11:
return '.'.charCodeAt(0)
case 15:
return '\0'.charCodeAt(0)
default:
throw new Error('invalid nibble: ' + value)
}
}
const unpackByte = (tag: number, value: number) => {
if(tag === TAGS.NIBBLE_8) {
if (tag === TAGS.NIBBLE_8) {
return unpackNibble(value)
} else if(tag === TAGS.HEX_8) {
} else if (tag === TAGS.HEX_8) {
return unpackHex(value)
} else {
throw new Error('unknown tag: ' + tag)
@@ -106,13 +107,13 @@ export const decodeDecompressedBinaryNode = (
const startByte = readByte()
let value = ''
for(let i = 0; i < (startByte & 127); i++) {
for (let i = 0; i < (startByte & 127); i++) {
const curByte = readByte()
value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4))
value += String.fromCharCode(unpackByte(tag, curByte & 0x0f))
}
if(startByte >> 7 !== 0) {
if (startByte >> 7 !== 0) {
value = value.slice(0, -1)
}
@@ -125,21 +126,21 @@ export const decodeDecompressedBinaryNode = (
const readListSize = (tag: number) => {
switch (tag) {
case TAGS.LIST_EMPTY:
return 0
case TAGS.LIST_8:
return readByte()
case TAGS.LIST_16:
return readInt(2)
default:
throw new Error('invalid tag for list size: ' + tag)
case TAGS.LIST_EMPTY:
return 0
case TAGS.LIST_8:
return readByte()
case TAGS.LIST_16:
return readInt(2)
default:
throw new Error('invalid tag for list size: ' + tag)
}
}
const readJidPair = () => {
const i = readString(readByte())
const j = readString(readByte())
if(j) {
if (j) {
return (i || '') + '@' + j
}
@@ -153,48 +154,44 @@ export const decodeDecompressedBinaryNode = (
const device = readByte()
const user = readString(readByte())
return jidEncode(
user,
domainType === 0 || domainType === 128 ? 's.whatsapp.net' : 'lid',
device
)
return jidEncode(user, domainType === 0 || domainType === 128 ? 's.whatsapp.net' : 'lid', device)
}
const readString = (tag: number): string => {
if(tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
return SINGLE_BYTE_TOKENS[tag] || ''
}
switch (tag) {
case TAGS.DICTIONARY_0:
case TAGS.DICTIONARY_1:
case TAGS.DICTIONARY_2:
case TAGS.DICTIONARY_3:
return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte())
case TAGS.LIST_EMPTY:
return ''
case TAGS.BINARY_8:
return readStringFromChars(readByte())
case TAGS.BINARY_20:
return readStringFromChars(readInt20())
case TAGS.BINARY_32:
return readStringFromChars(readInt(4))
case TAGS.JID_PAIR:
return readJidPair()
case TAGS.AD_JID:
return readAdJid()
case TAGS.HEX_8:
case TAGS.NIBBLE_8:
return readPacked8(tag)
default:
throw new Error('invalid string with tag: ' + tag)
case TAGS.DICTIONARY_0:
case TAGS.DICTIONARY_1:
case TAGS.DICTIONARY_2:
case TAGS.DICTIONARY_3:
return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte())
case TAGS.LIST_EMPTY:
return ''
case TAGS.BINARY_8:
return readStringFromChars(readByte())
case TAGS.BINARY_20:
return readStringFromChars(readInt20())
case TAGS.BINARY_32:
return readStringFromChars(readInt(4))
case TAGS.JID_PAIR:
return readJidPair()
case TAGS.AD_JID:
return readAdJid()
case TAGS.HEX_8:
case TAGS.NIBBLE_8:
return readPacked8(tag)
default:
throw new Error('invalid string with tag: ' + tag)
}
}
const readList = (tag: number) => {
const items: BinaryNode[] = []
const size = readListSize(tag)
for(let i = 0;i < size;i++) {
for (let i = 0; i < size; i++) {
items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef))
}
@@ -203,12 +200,12 @@ export const decodeDecompressedBinaryNode = (
const getTokenDouble = (index1: number, index2: number) => {
const dict = DOUBLE_BYTE_TOKENS[index1]
if(!dict) {
if (!dict) {
throw new Error(`Invalid double token dict (${index1})`)
}
const value = dict[index2]
if(typeof value === 'undefined') {
if (typeof value === 'undefined') {
throw new Error(`Invalid double token (${index2})`)
}
@@ -217,44 +214,44 @@ export const decodeDecompressedBinaryNode = (
const listSize = readListSize(readByte())
const header = readString(readByte())
if(!listSize || !header.length) {
if (!listSize || !header.length) {
throw new Error('invalid node')
}
const attrs: BinaryNode['attrs'] = { }
const attrs: BinaryNode['attrs'] = {}
let data: BinaryNode['content']
if(listSize === 0 || !header) {
if (listSize === 0 || !header) {
throw new Error('invalid node')
}
// read the attributes in
const attributesLength = (listSize - 1) >> 1
for(let i = 0; i < attributesLength; i++) {
for (let i = 0; i < attributesLength; i++) {
const key = readString(readByte())
const value = readString(readByte())
attrs[key] = value
}
if(listSize % 2 === 0) {
if (listSize % 2 === 0) {
const tag = readByte()
if(isListTag(tag)) {
if (isListTag(tag)) {
data = readList(tag)
} else {
let decoded: Buffer | string
switch (tag) {
case TAGS.BINARY_8:
decoded = readBytes(readByte())
break
case TAGS.BINARY_20:
decoded = readBytes(readInt20())
break
case TAGS.BINARY_32:
decoded = readBytes(readInt(4))
break
default:
decoded = readString(tag)
break
case TAGS.BINARY_8:
decoded = readBytes(readByte())
break
case TAGS.BINARY_20:
decoded = readBytes(readInt20())
break
case TAGS.BINARY_32:
decoded = readBytes(readInt(4))
break
default:
decoded = readString(tag)
break
}
data = decoded
@@ -268,7 +265,7 @@ export const decodeDecompressedBinaryNode = (
}
}
export const decodeBinaryNode = async(buff: Buffer): Promise<BinaryNode> => {
export const decodeBinaryNode = async (buff: Buffer): Promise<BinaryNode> => {
const decompBuff = await decompressingIfRequired(buff)
return decodeDecompressedBinaryNode(decompBuff, constants)
}

View File

@@ -1,4 +1,3 @@
import * as constants from './constants'
import { FullJid, jidDecode } from './jid-utils'
import type { BinaryNode, BinaryNodeCodingOptions } from './types'
@@ -22,14 +21,14 @@ const encodeBinaryNodeInner = (
const pushByte = (value: number) => buffer.push(value & 0xff)
const pushInt = (value: number, n: number, littleEndian = false) => {
for(let i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
const curShift = littleEndian ? i : n - 1 - i
buffer.push((value >> (curShift * 8)) & 0xff)
}
}
const pushBytes = (bytes: Uint8Array | Buffer | number[]) => {
for(const b of bytes) {
for (const b of bytes) {
buffer.push(b)
}
}
@@ -38,18 +37,16 @@ const encodeBinaryNodeInner = (
pushBytes([(value >> 8) & 0xff, value & 0xff])
}
const pushInt20 = (value: number) => (
pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff])
)
const pushInt20 = (value: number) => pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff])
const writeByteLength = (length: number) => {
if(length >= 4294967296) {
if (length >= 4294967296) {
throw new Error('string too large to encode: ' + length)
}
if(length >= 1 << 20) {
if (length >= 1 << 20) {
pushByte(TAGS.BINARY_32)
pushInt(length, 4) // 32 bit integer
} else if(length >= 256) {
} else if (length >= 256) {
pushByte(TAGS.BINARY_20)
pushInt20(length)
} else {
@@ -59,20 +56,20 @@ const encodeBinaryNodeInner = (
}
const writeStringRaw = (str: string) => {
const bytes = Buffer.from (str, 'utf-8')
const bytes = Buffer.from(str, 'utf-8')
writeByteLength(bytes.length)
pushBytes(bytes)
}
const writeJid = ({ domainType, device, user, server }: FullJid) => {
if(typeof device !== 'undefined') {
if (typeof device !== 'undefined') {
pushByte(TAGS.AD_JID)
pushByte(domainType || 0)
pushByte(device || 0)
writeString(user)
} else {
pushByte(TAGS.JID_PAIR)
if(user.length) {
if (user.length) {
writeString(user)
} else {
pushByte(TAGS.LIST_EMPTY)
@@ -84,35 +81,35 @@ const encodeBinaryNodeInner = (
const packNibble = (char: string) => {
switch (char) {
case '-':
return 10
case '.':
return 11
case '\0':
return 15
default:
if(char >= '0' && char <= '9') {
return char.charCodeAt(0) - '0'.charCodeAt(0)
}
case '-':
return 10
case '.':
return 11
case '\0':
return 15
default:
if (char >= '0' && char <= '9') {
return char.charCodeAt(0) - '0'.charCodeAt(0)
}
throw new Error(`invalid byte for nibble "${char}"`)
throw new Error(`invalid byte for nibble "${char}"`)
}
}
const packHex = (char: string) => {
if(char >= '0' && char <= '9') {
if (char >= '0' && char <= '9') {
return char.charCodeAt(0) - '0'.charCodeAt(0)
}
if(char >= 'A' && char <= 'F') {
if (char >= 'A' && char <= 'F') {
return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0)
}
if(char >= 'a' && char <= 'f') {
if (char >= 'a' && char <= 'f') {
return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0)
}
if(char === '\0') {
if (char === '\0') {
return 15
}
@@ -120,14 +117,14 @@ const encodeBinaryNodeInner = (
}
const writePackedBytes = (str: string, type: 'nibble' | 'hex') => {
if(str.length > TAGS.PACKED_MAX) {
if (str.length > TAGS.PACKED_MAX) {
throw new Error('Too many bytes to pack')
}
pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8)
let roundedLength = Math.ceil(str.length / 2.0)
if(str.length % 2 !== 0) {
if (str.length % 2 !== 0) {
roundedLength |= 128
}
@@ -140,23 +137,23 @@ const encodeBinaryNodeInner = (
}
const strLengthHalf = Math.floor(str.length / 2)
for(let i = 0; i < strLengthHalf;i++) {
for (let i = 0; i < strLengthHalf; i++) {
pushByte(packBytePair(str[2 * i], str[2 * i + 1]))
}
if(str.length % 2 !== 0) {
if (str.length % 2 !== 0) {
pushByte(packBytePair(str[str.length - 1], '\x00'))
}
}
const isNibble = (str?: string) => {
if(!str || str.length > TAGS.PACKED_MAX) {
if (!str || str.length > TAGS.PACKED_MAX) {
return false
}
for(const char of str) {
for (const char of str) {
const isInNibbleRange = char >= '0' && char <= '9'
if(!isInNibbleRange && char !== '-' && char !== '.') {
if (!isInNibbleRange && char !== '-' && char !== '.') {
return false
}
}
@@ -165,13 +162,13 @@ const encodeBinaryNodeInner = (
}
const isHex = (str?: string) => {
if(!str || str.length > TAGS.PACKED_MAX) {
if (!str || str.length > TAGS.PACKED_MAX) {
return false
}
for(const char of str) {
for (const char of str) {
const isInNibbleRange = char >= '0' && char <= '9'
if(!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
return false
}
}
@@ -180,25 +177,25 @@ const encodeBinaryNodeInner = (
}
const writeString = (str?: string) => {
if(str === undefined || str === null) {
if (str === undefined || str === null) {
pushByte(TAGS.LIST_EMPTY)
return
}
const tokenIndex = TOKEN_MAP[str]
if(tokenIndex) {
if(typeof tokenIndex.dict === 'number') {
if (tokenIndex) {
if (typeof tokenIndex.dict === 'number') {
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict)
}
pushByte(tokenIndex.index)
} else if(isNibble(str)) {
} else if (isNibble(str)) {
writePackedBytes(str, 'nibble')
} else if(isHex(str)) {
} else if (isHex(str)) {
writePackedBytes(str, 'hex')
} else if(str) {
} else if (str) {
const decodedJid = jidDecode(str)
if(decodedJid) {
if (decodedJid) {
writeJid(decodedJid)
} else {
writeStringRaw(str)
@@ -207,9 +204,9 @@ const encodeBinaryNodeInner = (
}
const writeListStart = (listSize: number) => {
if(listSize === 0) {
if (listSize === 0) {
pushByte(TAGS.LIST_EMPTY)
} else if(listSize < 256) {
} else if (listSize < 256) {
pushBytes([TAGS.LIST_8, listSize])
} else {
pushByte(TAGS.LIST_16)
@@ -217,37 +214,36 @@ const encodeBinaryNodeInner = (
}
}
if(!tag) {
if (!tag) {
throw new Error('Invalid node: tag cannot be undefined')
}
const validAttributes = Object.keys(attrs || {}).filter(k => (
typeof attrs[k] !== 'undefined' && attrs[k] !== null
))
const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null)
writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0))
writeString(tag)
for(const key of validAttributes) {
if(typeof attrs[key] === 'string') {
for (const key of validAttributes) {
if (typeof attrs[key] === 'string') {
writeString(key)
writeString(attrs[key])
}
}
if(typeof content === 'string') {
if (typeof content === 'string') {
writeString(content)
} else if(Buffer.isBuffer(content) || content instanceof Uint8Array) {
} else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
writeByteLength(content.length)
pushBytes(content)
} else if(Array.isArray(content)) {
const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string')
} else if (Array.isArray(content)) {
const validContent = content.filter(
item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string')
)
writeListStart(validContent.length)
for(const item of validContent) {
for (const item of validContent) {
encodeBinaryNodeInner(item, opts, buffer)
}
} else if(typeof content === 'undefined') {
} else if (typeof content === 'undefined') {
// do nothing
} else {
throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`)

View File

@@ -5,7 +5,7 @@ import { BinaryNode } from './types'
// some extra useful utilities
export const getBinaryNodeChildren = (node: BinaryNode | undefined, childTag: string) => {
if(Array.isArray(node?.content)) {
if (Array.isArray(node?.content)) {
return node.content.filter(item => item.tag === childTag)
}
@@ -13,7 +13,7 @@ export const getBinaryNodeChildren = (node: BinaryNode | undefined, childTag: st
}
export const getAllBinaryNodeChildren = ({ content }: BinaryNode) => {
if(Array.isArray(content)) {
if (Array.isArray(content)) {
return content
}
@@ -21,37 +21,37 @@ export const getAllBinaryNodeChildren = ({ content }: BinaryNode) => {
}
export const getBinaryNodeChild = (node: BinaryNode | undefined, childTag: string) => {
if(Array.isArray(node?.content)) {
if (Array.isArray(node?.content)) {
return node?.content.find(item => item.tag === childTag)
}
}
export const getBinaryNodeChildBuffer = (node: BinaryNode | undefined, childTag: string) => {
const child = getBinaryNodeChild(node, childTag)?.content
if(Buffer.isBuffer(child) || child instanceof Uint8Array) {
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
return child
}
}
export const getBinaryNodeChildString = (node: BinaryNode | undefined, childTag: string) => {
const child = getBinaryNodeChild(node, childTag)?.content
if(Buffer.isBuffer(child) || child instanceof Uint8Array) {
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
return Buffer.from(child).toString('utf-8')
} else if(typeof child === 'string') {
} else if (typeof child === 'string') {
return child
}
}
export const getBinaryNodeChildUInt = (node: BinaryNode, childTag: string, length: number) => {
const buff = getBinaryNodeChildBuffer(node, childTag)
if(buff) {
if (buff) {
return bufferToUInt(buff, length)
}
}
export const assertNodeErrorFree = (node: BinaryNode) => {
const errNode = getBinaryNodeChild(node, 'error')
if(errNode) {
if (errNode) {
throw new Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code })
}
}
@@ -62,16 +62,17 @@ export const reduceBinaryNodeToDictionary = (node: BinaryNode, tag: string) => {
(dict, { attrs }) => {
dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value
return dict
}, { } as { [_: string]: string }
},
{} as { [_: string]: string }
)
return dict
}
export const getBinaryNodeMessages = ({ content }: BinaryNode) => {
const msgs: proto.WebMessageInfo[] = []
if(Array.isArray(content)) {
for(const item of content) {
if(item.tag === 'message') {
if (Array.isArray(content)) {
for (const item of content) {
if (item.tag === 'message') {
msgs.push(proto.WebMessageInfo.decode(item.content as Buffer))
}
}
@@ -82,7 +83,7 @@ export const getBinaryNodeMessages = ({ content }: BinaryNode) => {
function bufferToUInt(e: Uint8Array | Buffer, t: number) {
let a = 0
for(let i = 0; i < t; i++) {
for (let i = 0; i < t; i++) {
a = 256 * a + e[i]
}
@@ -92,20 +93,20 @@ function bufferToUInt(e: Uint8Array | Buffer, t: number) {
const tabs = (n: number) => '\t'.repeat(n)
export function binaryNodeToString(node: BinaryNode | BinaryNode['content'], i = 0) {
if(!node) {
if (!node) {
return node
}
if(typeof node === 'string') {
if (typeof node === 'string') {
return tabs(i) + node
}
if(node instanceof Uint8Array) {
if (node instanceof Uint8Array) {
return tabs(i) + Buffer.from(node).toString('hex')
}
if(Array.isArray(node)) {
return node.map((x) => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n')
if (Array.isArray(node)) {
return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n')
}
const children = binaryNodeToString(node.content, i + 1)
@@ -118,4 +119,4 @@ export function binaryNodeToString(node: BinaryNode | BinaryNode['content'], i =
const content: string = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>'
return tag + content
}
}

View File

@@ -8,8 +8,8 @@ export const META_AI_JID = '13135550002@c.us'
export type JidServer = 'c.us' | 'g.us' | 'broadcast' | 's.whatsapp.net' | 'call' | 'lid' | 'newsletter' | 'bot'
export type JidWithDevice = {
user: string
device?: number
user: string
device?: number
}
export type FullJid = JidWithDevice & {
@@ -17,14 +17,13 @@ export type FullJid = JidWithDevice & {
domainType?: number
}
export const jidEncode = (user: string | number | null, server: JidServer, device?: number, agent?: number) => {
return `${user || ''}${!!agent ? `_${agent}` : ''}${!!device ? `:${device}` : ''}@${server}`
}
export const jidDecode = (jid: string | undefined): FullJid | undefined => {
const sepIdx = typeof jid === 'string' ? jid.indexOf('@') : -1
if(sepIdx < 0) {
if (sepIdx < 0) {
return undefined
}
@@ -43,34 +42,33 @@ export const jidDecode = (jid: string | undefined): FullJid | undefined => {
}
/** is the jid a user */
export const areJidsSameUser = (jid1: string | undefined, jid2: string | undefined) => (
export const areJidsSameUser = (jid1: string | undefined, jid2: string | undefined) =>
jidDecode(jid1)?.user === jidDecode(jid2)?.user
)
/** is the jid Meta IA */
export const isJidMetaIa = (jid: string | undefined) => (jid?.endsWith('@bot'))
export const isJidMetaIa = (jid: string | undefined) => jid?.endsWith('@bot')
/** is the jid a user */
export const isJidUser = (jid: string | undefined) => (jid?.endsWith('@s.whatsapp.net'))
export const isJidUser = (jid: string | undefined) => jid?.endsWith('@s.whatsapp.net')
/** is the jid a group */
export const isLidUser = (jid: string | undefined) => (jid?.endsWith('@lid'))
export const isLidUser = (jid: string | undefined) => jid?.endsWith('@lid')
/** is the jid a broadcast */
export const isJidBroadcast = (jid: string | undefined) => (jid?.endsWith('@broadcast'))
export const isJidBroadcast = (jid: string | undefined) => jid?.endsWith('@broadcast')
/** is the jid a group */
export const isJidGroup = (jid: string | undefined) => (jid?.endsWith('@g.us'))
export const isJidGroup = (jid: string | undefined) => jid?.endsWith('@g.us')
/** is the jid the status broadcast */
export const isJidStatusBroadcast = (jid: string) => jid === 'status@broadcast'
/** is the jid a newsletter */
export const isJidNewsletter = (jid: string | undefined) => (jid?.endsWith('@newsletter'))
export const isJidNewsletter = (jid: string | undefined) => jid?.endsWith('@newsletter')
const botRegexp = /^1313555\d{4}$|^131655500\d{2}$/
export const isJidBot = (jid: string | undefined) => (jid && botRegexp.test(jid.split('@')[0]) && jid.endsWith('@c.us'))
export const isJidBot = (jid: string | undefined) => jid && botRegexp.test(jid.split('@')[0]) && jid.endsWith('@c.us')
export const jidNormalizedUser = (jid: string | undefined) => {
const result = jidDecode(jid)
if(!result) {
if (!result) {
return ''
}
const { user, server } = result
return jidEncode(user, server === 'c.us' ? 's.whatsapp.net' : server as JidServer)
return jidEncode(user, server === 'c.us' ? 's.whatsapp.net' : (server as JidServer))
}

View File

@@ -7,11 +7,11 @@ import * as constants from './constants'
* to maintain functional code structure
* */
export type BinaryNode = {
tag: string
attrs: { [key: string]: string }
tag: string
attrs: { [key: string]: string }
content?: BinaryNode[] | string | Uint8Array
}
export type BinaryNodeAttributes = BinaryNode['attrs']
export type BinaryNodeData = BinaryNode['content']
export type BinaryNodeCodingOptions = typeof constants
export type BinaryNodeCodingOptions = typeof constants

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,14 @@
import { BinaryInfo } from './BinaryInfo'
import { FLAG_BYTE, FLAG_EVENT, FLAG_EXTENDED, FLAG_FIELD, FLAG_GLOBAL, Value, WEB_EVENTS, WEB_GLOBALS } from './constants'
import {
FLAG_BYTE,
FLAG_EVENT,
FLAG_EXTENDED,
FLAG_FIELD,
FLAG_GLOBAL,
Value,
WEB_EVENTS,
WEB_GLOBALS
} from './constants'
const getHeaderBitLength = (key: number) => (key < 256 ? 2 : 3)
@@ -10,12 +19,10 @@ export const encodeWAM = (binaryInfo: BinaryInfo) => {
encodeEvents(binaryInfo)
console.log(binaryInfo.buffer)
const totalSize = binaryInfo.buffer
.map((a) => a.length)
.reduce((a, b) => a + b)
const totalSize = binaryInfo.buffer.map(a => a.length).reduce((a, b) => a + b)
const buffer = Buffer.alloc(totalSize)
let offset = 0
for(const buffer_ of binaryInfo.buffer) {
for (const buffer_ of binaryInfo.buffer) {
buffer_.copy(buffer, offset)
offset += buffer_.length
}
@@ -34,11 +41,11 @@ function encodeWAMHeader(binaryInfo: BinaryInfo) {
binaryInfo.buffer.push(headerBuffer)
}
function encodeGlobalAttributes(binaryInfo: BinaryInfo, globals: {[key: string]: Value}) {
for(const [key, _value] of Object.entries(globals)) {
function encodeGlobalAttributes(binaryInfo: BinaryInfo, globals: { [key: string]: Value }) {
for (const [key, _value] of Object.entries(globals)) {
const id = WEB_GLOBALS.find(a => a?.name === key)!.id
let value = _value
if(typeof value === 'boolean') {
if (typeof value === 'boolean') {
value = value ? 1 : 0
}
@@ -47,30 +54,27 @@ function encodeGlobalAttributes(binaryInfo: BinaryInfo, globals: {[key: string]:
}
function encodeEvents(binaryInfo: BinaryInfo) {
for(const [
name,
{ props, globals },
] of binaryInfo.events.map((a) => Object.entries(a)[0])) {
for (const [name, { props, globals }] of binaryInfo.events.map(a => Object.entries(a)[0])) {
encodeGlobalAttributes(binaryInfo, globals)
const event = WEB_EVENTS.find((a) => a.name === name)!
const event = WEB_EVENTS.find(a => a.name === name)!
const props_ = Object.entries(props)
let extended = false
for(const [, value] of props_) {
for (const [, value] of props_) {
extended ||= value !== null
}
const eventFlag = extended ? FLAG_EVENT : FLAG_EVENT | FLAG_EXTENDED
binaryInfo.buffer.push(serializeData(event.id, -event.weight, eventFlag))
for(let i = 0; i < props_.length; i++) {
for (let i = 0; i < props_.length; i++) {
const [key, _value] = props_[i]
const id = (event.props)[key][0]
extended = i < (props_.length - 1)
const id = event.props[key][0]
extended = i < props_.length - 1
let value = _value
if(typeof value === 'boolean') {
if (typeof value === 'boolean') {
value = value ? 1 : 0
}
@@ -80,34 +84,33 @@ function encodeEvents(binaryInfo: BinaryInfo) {
}
}
function serializeData(key: number, value: Value, flag: number): Buffer {
const bufferLength = getHeaderBitLength(key)
let buffer: Buffer
let offset = 0
if(value === null) {
if(flag === FLAG_GLOBAL) {
if (value === null) {
if (flag === FLAG_GLOBAL) {
buffer = Buffer.alloc(bufferLength)
offset = serializeHeader(buffer, offset, key, flag)
return buffer
}
} else if(typeof value === 'number' && Number.isInteger(value)) {
} else if (typeof value === 'number' && Number.isInteger(value)) {
// is number
if(value === 0 || value === 1) {
if (value === 0 || value === 1) {
buffer = Buffer.alloc(bufferLength)
offset = serializeHeader(buffer, offset, key, flag | ((value + 1) << 4))
return buffer
} else if(-128 <= value && value < 128) {
} else if (-128 <= value && value < 128) {
buffer = Buffer.alloc(bufferLength + 1)
offset = serializeHeader(buffer, offset, key, flag | (3 << 4))
buffer.writeInt8(value, offset)
return buffer
} else if(-32768 <= value && value < 32768) {
} else if (-32768 <= value && value < 32768) {
buffer = Buffer.alloc(bufferLength + 2)
offset = serializeHeader(buffer, offset, key, flag | (4 << 4))
buffer.writeInt16LE(value, offset)
return buffer
} else if(-2147483648 <= value && value < 2147483648) {
} else if (-2147483648 <= value && value < 2147483648) {
buffer = Buffer.alloc(bufferLength + 4)
offset = serializeHeader(buffer, offset, key, flag | (5 << 4))
buffer.writeInt32LE(value, offset)
@@ -118,20 +121,20 @@ function serializeData(key: number, value: Value, flag: number): Buffer {
buffer.writeDoubleLE(value, offset)
return buffer
}
} else if(typeof value === 'number') {
} else if (typeof value === 'number') {
// is float
buffer = Buffer.alloc(bufferLength + 8)
offset = serializeHeader(buffer, offset, key, flag | (7 << 4))
buffer.writeDoubleLE(value, offset)
return buffer
} else if(typeof value === 'string') {
} else if (typeof value === 'string') {
// is string
const utf8Bytes = Buffer.byteLength(value, 'utf8')
if(utf8Bytes < 256) {
if (utf8Bytes < 256) {
buffer = Buffer.alloc(bufferLength + 1 + utf8Bytes)
offset = serializeHeader(buffer, offset, key, flag | (8 << 4))
buffer.writeUint8(utf8Bytes, offset++)
} else if(utf8Bytes < 65536) {
} else if (utf8Bytes < 65536) {
buffer = Buffer.alloc(bufferLength + 2 + utf8Bytes)
offset = serializeHeader(buffer, offset, key, flag | (9 << 4))
buffer.writeUInt16LE(utf8Bytes, offset)
@@ -150,13 +153,8 @@ function serializeData(key: number, value: Value, flag: number): Buffer {
throw 'missing'
}
function serializeHeader(
buffer: Buffer,
offset: number,
key: number,
flag: number
) {
if(key < 256) {
function serializeHeader(buffer: Buffer, offset: number, key: number, flag: number) {
if (key < 256) {
buffer.writeUInt8(flag, offset)
offset += 1
buffer.writeUInt8(key, offset)
@@ -169,4 +167,4 @@ function serializeHeader(
}
return offset
}
}

View File

@@ -1,3 +1,3 @@
export * from './constants'
export * from './encode'
export * from './BinaryInfo'
export * from './BinaryInfo'

View File

@@ -8,7 +8,7 @@ export class USyncContactProtocol implements USyncQueryProtocol {
getQueryElement(): BinaryNode {
return {
tag: 'contact',
attrs: {},
attrs: {}
}
}
@@ -17,16 +17,16 @@ export class USyncContactProtocol implements USyncQueryProtocol {
return {
tag: 'contact',
attrs: {},
content: user.phone,
content: user.phone
}
}
parser(node: BinaryNode): boolean {
if(node.tag === 'contact') {
if (node.tag === 'contact') {
assertNodeErrorFree(node)
return node?.attrs?.type === 'in'
}
return false
}
}
}

View File

@@ -15,7 +15,7 @@ export type DeviceListData = {
}
export type ParsedDeviceInfo = {
deviceList?: DeviceListData[]
deviceList?: DeviceListData[]
keyIndex?: KeyIndexData
}
@@ -26,8 +26,8 @@ export class USyncDeviceProtocol implements USyncQueryProtocol {
return {
tag: 'devices',
attrs: {
version: '2',
},
version: '2'
}
}
}
@@ -42,16 +42,16 @@ export class USyncDeviceProtocol implements USyncQueryProtocol {
const deviceList: DeviceListData[] = []
let keyIndex: KeyIndexData | undefined = undefined
if(node.tag === 'devices') {
if (node.tag === 'devices') {
assertNodeErrorFree(node)
const deviceListNode = getBinaryNodeChild(node, 'device-list')
const keyIndexNode = getBinaryNodeChild(node, 'key-index-list')
if(Array.isArray(deviceListNode?.content)) {
for(const { tag, attrs } of deviceListNode.content) {
if (Array.isArray(deviceListNode?.content)) {
for (const { tag, attrs } of deviceListNode.content) {
const id = +attrs.id
const keyIndex = +attrs['key-index']
if(tag === 'device') {
if (tag === 'device') {
deviceList.push({
id,
keyIndex,
@@ -61,7 +61,7 @@ export class USyncDeviceProtocol implements USyncQueryProtocol {
}
}
if(keyIndexNode?.tag === 'key-index-list') {
if (keyIndexNode?.tag === 'key-index-list') {
keyIndex = {
timestamp: +keyIndexNode.attrs['ts'],
signedKeyIndex: keyIndexNode?.content as Uint8Array,
@@ -75,4 +75,4 @@ export class USyncDeviceProtocol implements USyncQueryProtocol {
keyIndex
}
}
}
}

View File

@@ -12,7 +12,7 @@ export class USyncDisappearingModeProtocol implements USyncQueryProtocol {
getQueryElement(): BinaryNode {
return {
tag: 'disappearing_mode',
attrs: {},
attrs: {}
}
}
@@ -21,15 +21,15 @@ export class USyncDisappearingModeProtocol implements USyncQueryProtocol {
}
parser(node: BinaryNode): DisappearingModeData | undefined {
if(node.tag === 'status') {
if (node.tag === 'status') {
assertNodeErrorFree(node)
const duration: number = +node?.attrs.duration
const setAt = new Date(+(node?.attrs.t || 0) * 1000)
return {
duration,
setAt,
setAt
}
}
}
}
}

View File

@@ -12,7 +12,7 @@ export class USyncStatusProtocol implements USyncQueryProtocol {
getQueryElement(): BinaryNode {
return {
tag: 'status',
attrs: {},
attrs: {}
}
}
@@ -21,24 +21,24 @@ export class USyncStatusProtocol implements USyncQueryProtocol {
}
parser(node: BinaryNode): StatusData | undefined {
if(node.tag === 'status') {
if (node.tag === 'status') {
assertNodeErrorFree(node)
let status: string | null = node?.content!.toString()
const setAt = new Date(+(node?.attrs.t || 0) * 1000)
if(!status) {
if(+node.attrs?.code === 401) {
if (!status) {
if (+node.attrs?.code === 401) {
status = ''
} else {
status = null
}
} else if(typeof status === 'string' && status.length === 0) {
} else if (typeof status === 'string' && status.length === 0) {
status = null
}
return {
status,
setAt,
setAt
}
}
}
}
}

View File

@@ -3,21 +3,21 @@ import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChi
import { USyncUser } from '../USyncUser'
export type BotProfileCommand = {
name: string
description: string
name: string
description: string
}
export type BotProfileInfo = {
jid: string
name: string
attributes: string
description: string
category: string
isDefault: boolean
prompts: string[]
personaId: string
commands: BotProfileCommand[]
commandsDescription: string
jid: string
name: string
attributes: string
description: string
category: string
isDefault: boolean
prompts: string[]
personaId: string
commands: BotProfileCommand[]
commandsDescription: string
}
export class USyncBotProfileProtocol implements USyncQueryProtocol {
@@ -26,7 +26,7 @@ export class USyncBotProfileProtocol implements USyncQueryProtocol {
getQueryElement(): BinaryNode {
return {
tag: 'bot',
attrs: { },
attrs: {},
content: [{ tag: 'profile', attrs: { v: '1' } }]
}
}
@@ -34,14 +34,14 @@ export class USyncBotProfileProtocol implements USyncQueryProtocol {
getUserElement(user: USyncUser): BinaryNode {
return {
tag: 'bot',
attrs: { },
content: [{ tag: 'profile', attrs: { 'persona_id': user.personaId } }]
attrs: {},
content: [{ tag: 'profile', attrs: { persona_id: user.personaId } }]
}
}
parser(node: BinaryNode): BotProfileInfo {
const botNode = getBinaryNodeChild(node, 'bot')
const profile = getBinaryNodeChild(botNode, 'profile')
const botNode = getBinaryNodeChild(node, 'bot')
const profile = getBinaryNodeChild(botNode, 'profile')
const commandsNode = getBinaryNodeChild(profile, 'commands')
const promptsNode = getBinaryNodeChild(profile, 'prompts')
@@ -49,21 +49,20 @@ export class USyncBotProfileProtocol implements USyncQueryProtocol {
const commands: BotProfileCommand[] = []
const prompts: string[] = []
for(const command of getBinaryNodeChildren(commandsNode, 'command')) {
commands.push({
for (const command of getBinaryNodeChildren(commandsNode, 'command')) {
commands.push({
name: getBinaryNodeChildString(command, 'name')!,
description: getBinaryNodeChildString(command, 'description')!
})
}
for(const prompt of getBinaryNodeChildren(promptsNode, 'prompt')) {
prompts.push(`${getBinaryNodeChildString(prompt, 'emoji')!} ${getBinaryNodeChildString(prompt, 'text')!}`)
for (const prompt of getBinaryNodeChildren(promptsNode, 'prompt')) {
prompts.push(`${getBinaryNodeChildString(prompt, 'emoji')!} ${getBinaryNodeChildString(prompt, 'text')!}`)
}
return {
isDefault: !!getBinaryNodeChild(profile, 'default'),
jid: node.attrs.jid,
isDefault: !!getBinaryNodeChild(profile, 'default'),
jid: node.attrs.jid,
name: getBinaryNodeChildString(profile, 'name')!,
attributes: getBinaryNodeChildString(profile, 'attributes')!,
description: getBinaryNodeChildString(profile, 'description')!,

View File

@@ -7,7 +7,7 @@ export class USyncLIDProtocol implements USyncQueryProtocol {
getQueryElement(): BinaryNode {
return {
tag: 'lid',
attrs: {},
attrs: {}
}
}
@@ -16,7 +16,7 @@ export class USyncLIDProtocol implements USyncQueryProtocol {
}
parser(node: BinaryNode): string | null {
if(node.tag === 'lid') {
if (node.tag === 'lid') {
return node.attrs.val
}

View File

@@ -1,4 +1,4 @@
export * from './USyncDeviceProtocol'
export * from './USyncContactProtocol'
export * from './USyncStatusProtocol'
export * from './USyncDisappearingModeProtocol'
export * from './USyncDisappearingModeProtocol'

View File

@@ -2,14 +2,19 @@ import { USyncQueryProtocol } from '../Types/USync'
import { BinaryNode, getBinaryNodeChild } from '../WABinary'
import { USyncBotProfileProtocol } from './Protocols/UsyncBotProfileProtocol'
import { USyncLIDProtocol } from './Protocols/UsyncLIDProtocol'
import { USyncContactProtocol, USyncDeviceProtocol, USyncDisappearingModeProtocol, USyncStatusProtocol } from './Protocols'
import {
USyncContactProtocol,
USyncDeviceProtocol,
USyncDisappearingModeProtocol,
USyncStatusProtocol
} from './Protocols'
import { USyncUser } from './USyncUser'
export type USyncQueryResultList = { [protocol: string]: unknown, id: string }
export type USyncQueryResultList = { [protocol: string]: unknown; id: string }
export type USyncQueryResult = {
list: USyncQueryResultList[]
sideList: USyncQueryResultList[]
list: USyncQueryResultList[]
sideList: USyncQueryResultList[]
}
export class USyncQuery {
@@ -41,18 +46,20 @@ export class USyncQuery {
}
parseUSyncQueryResult(result: BinaryNode): USyncQueryResult | undefined {
if(result.attrs.type !== 'result') {
if (result.attrs.type !== 'result') {
return
}
const protocolMap = Object.fromEntries(this.protocols.map((protocol) => {
return [protocol.name, protocol.parser]
}))
const protocolMap = Object.fromEntries(
this.protocols.map(protocol => {
return [protocol.name, protocol.parser]
})
)
const queryResult: USyncQueryResult = {
// TODO: implement errors etc.
list: [],
sideList: [],
sideList: []
}
const usyncNode = getBinaryNodeChild(result, 'usync')
@@ -62,18 +69,24 @@ export class USyncQuery {
//const resultNode = getBinaryNodeChild(usyncNode, 'result')
const listNode = getBinaryNodeChild(usyncNode, 'list')
if(Array.isArray(listNode?.content) && typeof listNode !== 'undefined') {
queryResult.list = listNode.content.map((node) => {
if (Array.isArray(listNode?.content) && typeof listNode !== 'undefined') {
queryResult.list = listNode.content.map(node => {
const id = node?.attrs.jid
const data = Array.isArray(node?.content) ? Object.fromEntries(node.content.map((content) => {
const protocol = content.tag
const parser = protocolMap[protocol]
if(parser) {
return [protocol, parser(content)]
} else {
return [protocol, null]
}
}).filter(([, b]) => b !== null) as [string, unknown][]) : {}
const data = Array.isArray(node?.content)
? Object.fromEntries(
node.content
.map(content => {
const protocol = content.tag
const parser = protocolMap[protocol]
if (parser) {
return [protocol, parser(content)]
} else {
return [protocol, null]
}
})
.filter(([, b]) => b !== null) as [string, unknown][]
)
: {}
return { ...data, id }
})
}

View File

@@ -26,7 +26,7 @@ export class USyncUser {
}
withPersonaId(personaId: string) {
this.personaId = personaId
return this
this.personaId = personaId
return this
}
}

View File

@@ -1,3 +1,3 @@
export * from './Protocols'
export * from './USyncQuery'
export * from './USyncUser'
export * from './USyncUser'