fix(types): remove non-null assertions across 14 files
Files fixed: - messages-recv.ts: 58 assertions → null guards, meId extraction, optional chaining - process-message.ts: 38 assertions → remoteJid/participant guards, proto field checks - chat-utils.ts: 31 assertions → proto field validation with Boom errors - messages-send.ts: 20 assertions → meId extraction, participant guards - chats.ts: 15 assertions → me guard, firstChild guard, jid guards - messages.ts: 14 assertions → originalFilePath guard, key guards - groups.ts: 12 assertions → attrs fallbacks with ?? - validate-connection.ts: 9 assertions → grouped proto field validation - generics.ts: 1 assertion → versionLine guard - history.ts: 2 assertions → key guards - libsignal.ts: 1 assertion → deviceId guard - sender-key-message.ts: 3 assertions → proto guards - UsyncBotProfileProtocol.ts: 2 assertions → attrs guards - example.ts: 3 assertions → optional chaining https://claude.ai/code/session_01E2cfX1N3sJgCJBTvzGazSG
This commit is contained in:
+164
-36
@@ -154,7 +154,12 @@ export const encodeSyncdPatch = async (
|
||||
})
|
||||
const encoded = proto.SyncActionData.encode(dataProto).finish()
|
||||
|
||||
const keyValue = mutationKeys(key.keyData!)
|
||||
const keyData = key.keyData
|
||||
if (!keyData) {
|
||||
throw new Boom('Missing keyData for encoding', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const keyValue = mutationKeys(keyData)
|
||||
|
||||
const encValue = aesEncrypt(encoded, keyValue.valueEncryptionKey)
|
||||
const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey)
|
||||
@@ -210,15 +215,34 @@ export const decodeSyncdMutations = async (
|
||||
// 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
|
||||
if (operation == null) {
|
||||
throw new Boom('Missing operation in mutation', { statusCode: 500 })
|
||||
}
|
||||
|
||||
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 keyIdBuf = record.keyId?.id
|
||||
if (!keyIdBuf) {
|
||||
throw new Boom('Missing keyId in record', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const recordBlob = record.value?.blob
|
||||
if (!recordBlob) {
|
||||
throw new Boom('Missing record blob', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const indexBlob = record.index?.blob
|
||||
if (!indexBlob) {
|
||||
throw new Boom('Missing index blob in record', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const key = await getKey(keyIdBuf)
|
||||
const content = Buffer.from(recordBlob)
|
||||
const encContent = content.slice(0, -32)
|
||||
const ogValueMac = content.slice(-32)
|
||||
if (validateMacs) {
|
||||
const contentHmac = generateMac(operation!, encContent, record.keyId!.id!, key.valueMacKey)
|
||||
const contentHmac = generateMac(operation, encContent, keyIdBuf, key.valueMacKey)
|
||||
if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
|
||||
throw new Boom('HMAC content verification failed')
|
||||
}
|
||||
@@ -227,20 +251,25 @@ export const decodeSyncdMutations = async (
|
||||
const result = aesDecrypt(encContent, key.valueEncryptionKey)
|
||||
const syncAction = proto.SyncActionData.decode(result)
|
||||
|
||||
const syncActionIndex = syncAction.index
|
||||
if (!syncActionIndex) {
|
||||
throw new Boom('Missing index in sync action data', { statusCode: 500 })
|
||||
}
|
||||
|
||||
if (validateMacs) {
|
||||
const hmac = hmacSign(syncAction.index!, key.indexKey)
|
||||
if (Buffer.compare(hmac, record.index!.blob!) !== 0) {
|
||||
const hmac = hmacSign(syncActionIndex, key.indexKey)
|
||||
if (Buffer.compare(hmac, indexBlob) !== 0) {
|
||||
throw new Boom('HMAC index verification failed')
|
||||
}
|
||||
}
|
||||
|
||||
const indexStr = Buffer.from(syncAction.index!).toString()
|
||||
const indexStr = Buffer.from(syncActionIndex).toString()
|
||||
onMutation({ syncAction, index: JSON.parse(indexStr) })
|
||||
|
||||
ltGenerator.mix({
|
||||
indexMac: record.index!.blob!,
|
||||
indexMac: indexBlob,
|
||||
valueMac: ogValueMac,
|
||||
operation: operation!
|
||||
operation: operation
|
||||
})
|
||||
}
|
||||
|
||||
@@ -256,7 +285,12 @@ export const decodeSyncdMutations = async (
|
||||
})
|
||||
}
|
||||
|
||||
return mutationKeys(keyEnc.keyData!)
|
||||
const keyData = keyEnc.keyData
|
||||
if (!keyData) {
|
||||
throw new Boom('Missing keyData in app state sync key', { statusCode: 500 })
|
||||
}
|
||||
|
||||
return mutationKeys(keyData)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,28 +303,71 @@ export const decodeSyncdPatch = async (
|
||||
validateMacs: boolean
|
||||
) => {
|
||||
if (validateMacs) {
|
||||
const base64Key = Buffer.from(msg.keyId!.id!).toString('base64')
|
||||
const msgKeyId = msg.keyId?.id
|
||||
if (!msgKeyId) {
|
||||
throw new Boom('Missing keyId in patch message', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const base64Key = Buffer.from(msgKeyId).toString('base64')
|
||||
const mainKeyObj = await getAppStateSyncKey(base64Key)
|
||||
if (!mainKeyObj) {
|
||||
throw new Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } })
|
||||
}
|
||||
|
||||
const mainKey = mutationKeys(mainKeyObj.keyData!)
|
||||
const mutationmacs = msg.mutations!.map(mutation => mutation.record!.value!.blob!.slice(-32))
|
||||
const mainKeyData = mainKeyObj.keyData
|
||||
if (!mainKeyData) {
|
||||
throw new Boom('Missing keyData in main key object', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const mainKey = mutationKeys(mainKeyData)
|
||||
|
||||
const msgMutations = msg.mutations
|
||||
if (!msgMutations) {
|
||||
throw new Boom('Missing mutations in patch message', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const mutationmacs = msgMutations.map(mutation => {
|
||||
const mutBlob = mutation.record?.value?.blob
|
||||
if (!mutBlob) {
|
||||
throw new Boom('Missing blob in mutation record', { statusCode: 500 })
|
||||
}
|
||||
|
||||
return mutBlob.slice(-32)
|
||||
})
|
||||
|
||||
const msgSnapshotMac = msg.snapshotMac
|
||||
if (!msgSnapshotMac) {
|
||||
throw new Boom('Missing snapshotMac in patch message', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const msgVersion = msg.version?.version
|
||||
if (msgVersion == null) {
|
||||
throw new Boom('Missing version in patch message', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const msgPatchMac = msg.patchMac
|
||||
if (!msgPatchMac) {
|
||||
throw new Boom('Missing patchMac in patch message', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const patchMac = generatePatchMac(
|
||||
msg.snapshotMac!,
|
||||
msgSnapshotMac,
|
||||
mutationmacs,
|
||||
toNumber(msg.version!.version),
|
||||
toNumber(msgVersion),
|
||||
name,
|
||||
mainKey.patchMacKey
|
||||
)
|
||||
if (Buffer.compare(patchMac, msg.patchMac!) !== 0) {
|
||||
if (Buffer.compare(patchMac, msgPatchMac) !== 0) {
|
||||
throw new Boom('Invalid patch mac')
|
||||
}
|
||||
}
|
||||
|
||||
const result = await decodeSyncdMutations(msg.mutations!, initialState, getAppStateSyncKey, onMutation, validateMacs)
|
||||
const patchMutations = msg.mutations
|
||||
if (!patchMutations) {
|
||||
throw new Boom('Missing mutations in patch message', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const result = await decodeSyncdMutations(patchMutations, initialState, getAppStateSyncKey, onMutation, validateMacs)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -332,7 +409,7 @@ export const extractSyncdPatches = async (result: BinaryNode, options: RequestIn
|
||||
|
||||
const syncd = proto.SyncdPatch.decode(content as Uint8Array)
|
||||
if (!syncd.version) {
|
||||
syncd.version = { version: +collectionNode.attrs.version! + 1 }
|
||||
syncd.version = { version: +(collectionNode.attrs.version ?? 0) + 1 }
|
||||
}
|
||||
|
||||
syncds.push(syncd)
|
||||
@@ -370,19 +447,30 @@ export const decodeSyncdSnapshot = async (
|
||||
validateMacs = true
|
||||
) => {
|
||||
const newState = newLTHashState()
|
||||
newState.version = toNumber(snapshot.version!.version)
|
||||
|
||||
const snapshotVersion = snapshot.version?.version
|
||||
if (snapshotVersion == null) {
|
||||
throw new Boom('Missing version in snapshot', { statusCode: 500 })
|
||||
}
|
||||
|
||||
newState.version = toNumber(snapshotVersion)
|
||||
|
||||
const mutationMap: ChatMutationMap = {}
|
||||
const areMutationsRequired = typeof minimumVersionNumber === 'undefined' || newState.version > minimumVersionNumber
|
||||
|
||||
const snapshotRecords = snapshot.records
|
||||
if (!snapshotRecords) {
|
||||
throw new Boom('Missing records in snapshot', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const { hash, indexValueMap } = await decodeSyncdMutations(
|
||||
snapshot.records!,
|
||||
snapshotRecords,
|
||||
newState,
|
||||
getAppStateSyncKey,
|
||||
areMutationsRequired
|
||||
? mutation => {
|
||||
const index = mutation.syncAction.index?.toString()
|
||||
mutationMap[index!] = mutation
|
||||
mutationMap[index ?? ''] = mutation
|
||||
}
|
||||
: () => {},
|
||||
validateMacs
|
||||
@@ -391,15 +479,31 @@ export const decodeSyncdSnapshot = async (
|
||||
newState.indexValueMap = indexValueMap
|
||||
|
||||
if (validateMacs) {
|
||||
const base64Key = Buffer.from(snapshot.keyId!.id!).toString('base64')
|
||||
const snapKeyId = snapshot.keyId?.id
|
||||
if (!snapKeyId) {
|
||||
throw new Boom('Missing keyId in snapshot', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const base64Key = Buffer.from(snapKeyId).toString('base64')
|
||||
const keyEnc = await getAppStateSyncKey(base64Key)
|
||||
if (!keyEnc) {
|
||||
throw new Boom(`failed to find key "${base64Key}" to decode mutation`)
|
||||
}
|
||||
|
||||
const result = mutationKeys(keyEnc.keyData!)
|
||||
const snapKeyData = keyEnc.keyData
|
||||
if (!snapKeyData) {
|
||||
throw new Boom('Missing keyData in snapshot key', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const result = mutationKeys(snapKeyData)
|
||||
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey)
|
||||
if (Buffer.compare(snapshot.mac!, computedSnapshotMac) !== 0) {
|
||||
|
||||
const snapshotMac = snapshot.mac
|
||||
if (!snapshotMac) {
|
||||
throw new Boom('Missing mac in snapshot', { statusCode: 500 })
|
||||
}
|
||||
|
||||
if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
|
||||
throw new Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`)
|
||||
}
|
||||
}
|
||||
@@ -436,7 +540,12 @@ export const decodePatches = async (
|
||||
syncd.mutations?.push(...ref.mutations)
|
||||
}
|
||||
|
||||
const patchVersion = toNumber(version!.version)
|
||||
const ver = version?.version
|
||||
if (ver == null) {
|
||||
throw new Boom('Missing version in patch', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const patchVersion = toNumber(ver)
|
||||
|
||||
newState.version = patchVersion
|
||||
const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber
|
||||
@@ -449,7 +558,7 @@ export const decodePatches = async (
|
||||
shouldMutate
|
||||
? mutation => {
|
||||
const index = mutation.syncAction.index?.toString()
|
||||
mutationMap[index!] = mutation
|
||||
mutationMap[index ?? ''] = mutation
|
||||
}
|
||||
: () => {},
|
||||
true
|
||||
@@ -459,15 +568,30 @@ export const decodePatches = async (
|
||||
newState.indexValueMap = decodeResult.indexValueMap
|
||||
|
||||
if (validateMacs) {
|
||||
const base64Key = Buffer.from(keyId!.id!).toString('base64')
|
||||
const patchKeyId = keyId?.id
|
||||
if (!patchKeyId) {
|
||||
throw new Boom('Missing keyId in patch', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const base64Key = Buffer.from(patchKeyId).toString('base64')
|
||||
const keyEnc = await getAppStateSyncKey(base64Key)
|
||||
if (!keyEnc) {
|
||||
throw new Boom(`failed to find key "${base64Key}" to decode mutation`)
|
||||
}
|
||||
|
||||
const result = mutationKeys(keyEnc.keyData!)
|
||||
const patchKeyData = keyEnc.keyData
|
||||
if (!patchKeyData) {
|
||||
throw new Boom('Missing keyData in patch key', { statusCode: 500 })
|
||||
}
|
||||
|
||||
const result = mutationKeys(patchKeyData)
|
||||
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey)
|
||||
if (Buffer.compare(snapshotMac!, computedSnapshotMac) !== 0) {
|
||||
|
||||
if (!snapshotMac) {
|
||||
throw new Boom('Missing snapshotMac in patch', { statusCode: 500 })
|
||||
}
|
||||
|
||||
if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
|
||||
throw new Boom(`failed to verify LTHash at ${newState.version} of ${name}`)
|
||||
}
|
||||
}
|
||||
@@ -565,7 +689,7 @@ export const chatModificationToAppPatch = (mod: ChatModification, jid: string) =
|
||||
messageTimestamp: timestamp
|
||||
}
|
||||
},
|
||||
index: ['deleteMessageForMe', jid, key.id!, key.fromMe ? '1' : '0', '0'],
|
||||
index: ['deleteMessageForMe', jid, key.id ?? '', key.fromMe ? '1' : '0', '0'],
|
||||
type: 'regular_high',
|
||||
apiVersion: 3,
|
||||
operation: OP.SET
|
||||
@@ -615,7 +739,11 @@ export const chatModificationToAppPatch = (mod: ChatModification, jid: string) =
|
||||
operation: OP.SET
|
||||
}
|
||||
} else if ('star' in mod) {
|
||||
const key = mod.star.messages[0]!
|
||||
const key = mod.star.messages[0]
|
||||
if (!key) {
|
||||
throw new Boom('Missing star message key', { statusCode: 400 })
|
||||
}
|
||||
|
||||
patch = {
|
||||
syncAction: {
|
||||
starAction: {
|
||||
@@ -768,7 +896,7 @@ export const processSyncAction = (
|
||||
{
|
||||
id,
|
||||
muteEndTime: action.muteAction?.muted ? toNumber(action.muteAction.muteEndTimestamp) : null,
|
||||
conditional: getChatUpdateConditional(id!, undefined)
|
||||
conditional: getChatUpdateConditional(id ?? '', undefined)
|
||||
}
|
||||
])
|
||||
} else if (action?.archiveChatAction || type === 'archive' || type === 'unarchive') {
|
||||
@@ -797,7 +925,7 @@ export const processSyncAction = (
|
||||
{
|
||||
id,
|
||||
archived: isArchived,
|
||||
conditional: getChatUpdateConditional(id!, msgRange)
|
||||
conditional: getChatUpdateConditional(id ?? '', msgRange)
|
||||
}
|
||||
])
|
||||
} else if (action?.markChatAsReadAction) {
|
||||
@@ -811,7 +939,7 @@ export const processSyncAction = (
|
||||
{
|
||||
id,
|
||||
unreadCount: isNullUpdate ? null : !!markReadAction?.read ? 0 : -1,
|
||||
conditional: getChatUpdateConditional(id!, markReadAction?.messageRange)
|
||||
conditional: getChatUpdateConditional(id ?? '', markReadAction?.messageRange)
|
||||
}
|
||||
])
|
||||
} else if (action?.deleteMessageForMeAction || type === 'deleteMessageForMe') {
|
||||
@@ -837,7 +965,7 @@ export const processSyncAction = (
|
||||
{
|
||||
id,
|
||||
pinned: action.pinAction?.pinned ? toNumber(action.timestamp) : null,
|
||||
conditional: getChatUpdateConditional(id!, undefined)
|
||||
conditional: getChatUpdateConditional(id ?? '', undefined)
|
||||
}
|
||||
])
|
||||
} else if (action?.unarchiveChatsSetting) {
|
||||
@@ -862,7 +990,7 @@ export const processSyncAction = (
|
||||
])
|
||||
} else if (action?.deleteChatAction || type === 'deleteChat') {
|
||||
if (!isInitialSync) {
|
||||
ev.emit('chats.delete', [id!])
|
||||
ev.emit('chats.delete', [id ?? ''])
|
||||
}
|
||||
} else if (action?.labelEditAction) {
|
||||
const { name, color, deleted, predefinedId } = action.labelEditAction
|
||||
|
||||
+10
-6
@@ -56,7 +56,7 @@ export const isStringNullOrEmpty = (value: string | null | undefined): value is
|
||||
|
||||
export const writeRandomPadMax16 = (msg: Uint8Array) => {
|
||||
const pad = randomBytes(1)
|
||||
const padLength = (pad[0]! & 0x0f) + 1
|
||||
const padLength = ((pad[0] ?? 0) & 0x0f) + 1
|
||||
|
||||
return Buffer.concat([msg, Buffer.alloc(padLength, padLength)])
|
||||
}
|
||||
@@ -67,7 +67,7 @@ export const unpadRandomMax16 = (e: Uint8Array | Buffer) => {
|
||||
throw new Error('unpadPkcs7 given empty bytes')
|
||||
}
|
||||
|
||||
var r = t[t.length - 1]!
|
||||
var r = t[t.length - 1] ?? 0
|
||||
if (r > t.length) {
|
||||
throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`)
|
||||
}
|
||||
@@ -85,7 +85,7 @@ export const generateParticipantHashV2 = (participants: string[]): string => {
|
||||
export const encodeWAMessage = (message: proto.IMessage) => writeRandomPadMax16(proto.Message.encode(message).finish())
|
||||
|
||||
export const generateRegistrationId = (): number => {
|
||||
return Uint16Array.from(randomBytes(2))[0]! & 16383
|
||||
return (Uint16Array.from(randomBytes(2))[0] ?? 0) & 16383
|
||||
}
|
||||
|
||||
export const encodeBigEndian = (e: number, t = 4) => {
|
||||
@@ -246,10 +246,14 @@ export const fetchLatestBaileysVersion = async (options: RequestInit = {}) => {
|
||||
// Extract version from line 7 (const version = [...])
|
||||
const lines = text.split('\n')
|
||||
const versionLine = lines[6] // Line 7 (0-indexed)
|
||||
const versionMatch = versionLine!.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/)
|
||||
if (!versionLine) {
|
||||
throw new Error('Version line not found')
|
||||
}
|
||||
|
||||
const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/)
|
||||
|
||||
if (versionMatch) {
|
||||
const version = [parseInt(versionMatch[1]!), parseInt(versionMatch[2]!), parseInt(versionMatch[3]!)] as WAVersion
|
||||
const version = [parseInt(versionMatch[1] ?? '0'), parseInt(versionMatch[2] ?? '0'), parseInt(versionMatch[3] ?? '0')] as WAVersion
|
||||
|
||||
return {
|
||||
version,
|
||||
@@ -339,7 +343,7 @@ const STATUS_MAP: { [_: string]: proto.WebMessageInfo.Status } = {
|
||||
* @param type type from receipt
|
||||
*/
|
||||
export const getStatusFromReceiptType = (type: string | undefined) => {
|
||||
const status = STATUS_MAP[type!]
|
||||
const status = STATUS_MAP[type ?? '']
|
||||
if (typeof type === 'undefined') {
|
||||
return proto.WebMessageInfo.Status.DELIVERY_ACK
|
||||
}
|
||||
|
||||
@@ -293,8 +293,8 @@ export const processHistoryMessage = (item: proto.IHistorySync, logger?: ILogger
|
||||
case proto.HistorySync.HistorySyncType.RECENT:
|
||||
case proto.HistorySync.HistorySyncType.FULL:
|
||||
case proto.HistorySync.HistorySyncType.ON_DEMAND:
|
||||
for (const chat of item.conversations! as Chat[]) {
|
||||
const chatId = chat.id!
|
||||
for (const chat of (item.conversations ?? []) as Chat[]) {
|
||||
const chatId = chat.id ?? ''
|
||||
|
||||
// Source 2: Extract LID-PN mapping from conversation object
|
||||
// This handles cases where the mapping isn't in phoneNumberToLidMappings
|
||||
@@ -328,7 +328,8 @@ export const processHistoryMessage = (item: proto.IHistorySync, logger?: ILogger
|
||||
delete chat.messages
|
||||
|
||||
for (const item of msgs) {
|
||||
const message = item.message! as WAMessage
|
||||
if (!item.message) continue
|
||||
const message = item.message as WAMessage
|
||||
messages.push(message)
|
||||
|
||||
// Source 3: Extract LID-PN mapping from message's alternative JID fields
|
||||
@@ -358,7 +359,7 @@ export const processHistoryMessage = (item: proto.IHistorySync, logger?: ILogger
|
||||
message.messageStubParameters?.[0]
|
||||
) {
|
||||
contacts.push({
|
||||
id: message.key.participant || message.key.remoteJid!,
|
||||
id: message.key.participant || message.key.remoteJid || '',
|
||||
verifiedName: message.messageStubParameters?.[0]
|
||||
})
|
||||
}
|
||||
@@ -369,8 +370,8 @@ export const processHistoryMessage = (item: proto.IHistorySync, logger?: ILogger
|
||||
|
||||
break
|
||||
case proto.HistorySync.HistorySyncType.PUSH_NAME:
|
||||
for (const c of item.pushnames!) {
|
||||
contacts.push({ id: c.id!, notify: c.pushname! })
|
||||
for (const c of (item.pushnames ?? [])) {
|
||||
contacts.push({ id: c.id ?? '', notify: c.pushname ?? '' })
|
||||
}
|
||||
|
||||
break
|
||||
@@ -424,7 +425,7 @@ export const downloadAndProcessHistorySyncNotification = async (
|
||||
*/
|
||||
export const getHistoryMsg = (message: proto.IMessage) => {
|
||||
const normalizedContent = !!message ? normalizeMessageContent(message) : undefined
|
||||
const anyHistoryMsg = normalizedContent?.protocolMessage?.historySyncNotification!
|
||||
const anyHistoryMsg = normalizedContent?.protocolMessage?.historySyncNotification
|
||||
|
||||
return anyHistoryMsg
|
||||
}
|
||||
|
||||
+76
-38
@@ -168,14 +168,17 @@ export const prepareWAMessageMedia = async (
|
||||
}
|
||||
|
||||
if (cacheableKey) {
|
||||
const mediaBuff = await options.mediaCache!.get<Buffer>(cacheableKey)
|
||||
const mediaBuff = await options.mediaCache?.get<Buffer>(cacheableKey)
|
||||
if (mediaBuff) {
|
||||
logger?.debug({ cacheableKey }, 'got media cache hit')
|
||||
|
||||
const obj = proto.Message.decode(mediaBuff)
|
||||
const key = `${mediaType}Message`
|
||||
|
||||
Object.assign(obj[key as keyof proto.Message]!, { ...uploadData, media: undefined })
|
||||
const mediaMsg = obj[key as keyof proto.Message]
|
||||
if (mediaMsg) {
|
||||
Object.assign(mediaMsg, { ...uploadData, media: undefined })
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
||||
@@ -222,7 +225,7 @@ export const prepareWAMessageMedia = async (
|
||||
|
||||
if (cacheableKey) {
|
||||
logger?.debug({ cacheableKey }, 'set cache')
|
||||
await options.mediaCache!.set(cacheableKey, WAProto.Message.encode(obj).finish())
|
||||
await options.mediaCache?.set(cacheableKey, WAProto.Message.encode(obj).finish())
|
||||
}
|
||||
|
||||
return obj
|
||||
@@ -257,9 +260,13 @@ export const prepareWAMessageMedia = async (
|
||||
})(),
|
||||
(async () => {
|
||||
try {
|
||||
if ((requiresThumbnailComputation || requiresDurationComputation || requiresWaveformProcessing) && !originalFilePath) {
|
||||
throw new Boom('Missing file path for processing')
|
||||
}
|
||||
|
||||
if (requiresThumbnailComputation) {
|
||||
const { thumbnail, originalImageDimensions } = await generateThumbnail(
|
||||
originalFilePath!,
|
||||
originalFilePath,
|
||||
mediaType as 'image' | 'video',
|
||||
options
|
||||
)
|
||||
@@ -274,12 +281,12 @@ export const prepareWAMessageMedia = async (
|
||||
}
|
||||
|
||||
if (requiresDurationComputation) {
|
||||
uploadData.seconds = await getAudioDuration(originalFilePath!)
|
||||
uploadData.seconds = await getAudioDuration(originalFilePath)
|
||||
logger?.debug('computed audio duration')
|
||||
}
|
||||
|
||||
if (requiresWaveformProcessing) {
|
||||
uploadData.waveform = await getAudioWaveform(originalFilePath!, logger)
|
||||
uploadData.waveform = await getAudioWaveform(originalFilePath, logger)
|
||||
logger?.debug('processed waveform')
|
||||
}
|
||||
|
||||
@@ -325,7 +332,7 @@ export const prepareWAMessageMedia = async (
|
||||
|
||||
if (cacheableKey) {
|
||||
logger?.debug({ cacheableKey }, 'set cache')
|
||||
await options.mediaCache!.set(cacheableKey, WAProto.Message.encode(obj).finish())
|
||||
await options.mediaCache?.set(cacheableKey, WAProto.Message.encode(obj).finish())
|
||||
}
|
||||
|
||||
return obj
|
||||
@@ -359,7 +366,11 @@ export const generateForwardMessageContent = (message: WAMessage, forceForward?:
|
||||
|
||||
// hacky copy
|
||||
content = normalizeMessageContent(content)
|
||||
content = proto.Message.decode(proto.Message.encode(content!).finish())
|
||||
if (!content) {
|
||||
throw new Boom('no content in message', { statusCode: 400 })
|
||||
}
|
||||
|
||||
content = proto.Message.decode(proto.Message.encode(content).finish())
|
||||
|
||||
let key = Object.keys(content)[0] as keyof proto.IMessage
|
||||
|
||||
@@ -591,7 +602,8 @@ export const generateCarouselMessage = async (
|
||||
|
||||
// Validate cards
|
||||
for (let i = 0; i < cards.length; i++) {
|
||||
const card = cards[i]!
|
||||
const card = cards[i]
|
||||
if (!card) continue
|
||||
|
||||
// Validate mutual exclusivity of media types
|
||||
if (card.image && card.video) {
|
||||
@@ -1042,7 +1054,9 @@ export const generateProductCarouselMessage = (
|
||||
|
||||
// Validate each product has a valid productId
|
||||
for (let i = 0; i < products.length; i++) {
|
||||
const product = products[i]!
|
||||
const product = products[i]
|
||||
if (!product) continue
|
||||
|
||||
if (!product.productId || typeof product.productId !== 'string' || product.productId.trim().length === 0) {
|
||||
throw new Boom(`Product at index ${i} must have a non-empty productId`, { statusCode: 400 })
|
||||
}
|
||||
@@ -1261,14 +1275,7 @@ export const generateWAMessageContent = async (
|
||||
options.logger?.warn('[EXPERIMENTAL] Sending buttonsMessage - this may not work and can cause bans')
|
||||
} else if (hasNonNullishProperty(message, 'text') && hasNonNullishProperty(message, 'templateButtons')) {
|
||||
// Process templateButtons
|
||||
const templateMessage: proto.Message.ITemplateMessage = {
|
||||
hydratedTemplate: {
|
||||
hydratedContentText: (message as any).text,
|
||||
hydratedFooterText: (message as any).footer
|
||||
}
|
||||
}
|
||||
|
||||
templateMessage.hydratedTemplate!.hydratedButtons = ((message as any).templateButtons as any[]).map((btn: any) => {
|
||||
const hydratedButtons = ((message as any).templateButtons as any[]).map((btn: any) => {
|
||||
if (btn.quickReplyButton) {
|
||||
return { index: btn.index, quickReplyButton: btn.quickReplyButton }
|
||||
} else if (btn.urlButton) {
|
||||
@@ -1279,6 +1286,14 @@ export const generateWAMessageContent = async (
|
||||
return btn
|
||||
})
|
||||
|
||||
const templateMessage: proto.Message.ITemplateMessage = {
|
||||
hydratedTemplate: {
|
||||
hydratedContentText: (message as any).text,
|
||||
hydratedFooterText: (message as any).footer,
|
||||
hydratedButtons
|
||||
}
|
||||
}
|
||||
|
||||
m.templateMessage = templateMessage
|
||||
options.logger?.warn('[EXPERIMENTAL] Sending templateMessage - this may not work and can cause bans')
|
||||
} else if (hasNonNullishProperty(message, 'sections')) {
|
||||
@@ -1354,7 +1369,9 @@ export const generateWAMessageContent = async (
|
||||
let expectedVideoCount = 0
|
||||
|
||||
for (let i = 0; i < medias.length; i++) {
|
||||
const media = medias[i]!
|
||||
const media = medias[i]
|
||||
if (!media) continue
|
||||
|
||||
if (hasNonNullishProperty(media as AnyMessageContent, 'image')) {
|
||||
expectedImageCount++
|
||||
} else if (hasNonNullishProperty(media as AnyMessageContent, 'video')) {
|
||||
@@ -1653,13 +1670,15 @@ export const generateWAMessageContent = async (
|
||||
}
|
||||
|
||||
if (hasOptionalProperty(message, 'mentions') && message.mentions?.length) {
|
||||
const messageType = Object.keys(m)[0]! as Extract<keyof proto.IMessage, MessageWithContextInfo>
|
||||
const key = m[messageType]
|
||||
if ('contextInfo' in key! && !!key.contextInfo) {
|
||||
key.contextInfo.mentionedJid = message.mentions
|
||||
} else if (key!) {
|
||||
key.contextInfo = {
|
||||
mentionedJid: message.mentions
|
||||
const messageType = Object.keys(m)[0] as Extract<keyof proto.IMessage, MessageWithContextInfo>
|
||||
if (messageType) {
|
||||
const key = m[messageType]
|
||||
if (key && 'contextInfo' in key && !!key.contextInfo) {
|
||||
key.contextInfo.mentionedJid = message.mentions
|
||||
} else if (key) {
|
||||
key.contextInfo = {
|
||||
mentionedJid: message.mentions
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1676,12 +1695,14 @@ export const generateWAMessageContent = async (
|
||||
}
|
||||
|
||||
if (hasOptionalProperty(message, 'contextInfo') && !!message.contextInfo) {
|
||||
const messageType = Object.keys(m)[0]! as Extract<keyof proto.IMessage, MessageWithContextInfo>
|
||||
const key = m[messageType]
|
||||
if ('contextInfo' in key! && !!key.contextInfo) {
|
||||
key.contextInfo = { ...key.contextInfo, ...message.contextInfo }
|
||||
} else if (key!) {
|
||||
key.contextInfo = message.contextInfo
|
||||
const messageType = Object.keys(m)[0] as Extract<keyof proto.IMessage, MessageWithContextInfo>
|
||||
if (messageType) {
|
||||
const key = m[messageType]
|
||||
if (key && 'contextInfo' in key && !!key.contextInfo) {
|
||||
key.contextInfo = { ...key.contextInfo, ...message.contextInfo }
|
||||
} else if (key) {
|
||||
key.contextInfo = message.contextInfo
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1708,8 +1729,16 @@ export const generateWAMessageFromContent = (
|
||||
options.timestamp = new Date()
|
||||
}
|
||||
|
||||
const innerMessage = normalizeMessageContent(message)!
|
||||
const key = getContentType(innerMessage)! as Exclude<keyof proto.IMessage, 'conversation'>
|
||||
const innerMessage = normalizeMessageContent(message)
|
||||
if (!innerMessage) {
|
||||
throw new Boom('no content in message', { statusCode: 400 })
|
||||
}
|
||||
|
||||
const key = getContentType(innerMessage)
|
||||
if (!key) {
|
||||
throw new Boom('unable to determine message content type', { statusCode: 400 })
|
||||
}
|
||||
|
||||
const timestamp = unixTimestampSeconds(options.timestamp)
|
||||
const { quoted, userJid } = options
|
||||
|
||||
@@ -1718,8 +1747,16 @@ export const generateWAMessageFromContent = (
|
||||
? userJid // TODO: Add support for LIDs
|
||||
: quoted.participant || quoted.key.participant || quoted.key.remoteJid
|
||||
|
||||
let quotedMsg = normalizeMessageContent(quoted.message)!
|
||||
const msgType = getContentType(quotedMsg)!
|
||||
let quotedMsg = normalizeMessageContent(quoted.message)
|
||||
if (!quotedMsg) {
|
||||
throw new Boom('no content in quoted message', { statusCode: 400 })
|
||||
}
|
||||
|
||||
const msgType = getContentType(quotedMsg)
|
||||
if (!msgType) {
|
||||
throw new Boom('unable to determine quoted message content type', { statusCode: 400 })
|
||||
}
|
||||
|
||||
// strip any redundant properties
|
||||
quotedMsg = proto.Message.create({ [msgType]: quotedMsg[msgType] })
|
||||
|
||||
@@ -1728,9 +1765,10 @@ export const generateWAMessageFromContent = (
|
||||
delete quotedContent.contextInfo
|
||||
}
|
||||
|
||||
const innerContent = innerMessage[key as Exclude<keyof proto.IMessage, 'conversation'>]
|
||||
const contextInfo: proto.IContextInfo =
|
||||
('contextInfo' in innerMessage[key]! && innerMessage[key]?.contextInfo) || {}
|
||||
contextInfo.participant = jidNormalizedUser(participant!)
|
||||
(innerContent && 'contextInfo' in innerContent && innerMessage[key as Exclude<keyof proto.IMessage, 'conversation'>]?.contextInfo) || {}
|
||||
contextInfo.participant = jidNormalizedUser(participant ?? '')
|
||||
contextInfo.stanzaId = quoted.key.id
|
||||
contextInfo.quotedMessage = quotedMsg
|
||||
|
||||
|
||||
@@ -59,32 +59,40 @@ const REAL_MSG_REQ_ME_STUB_TYPES = new Set([WAMessageStubType.GROUP_PARTICIPANT_
|
||||
/** Cleans a received message to further processing */
|
||||
export const cleanMessage = (message: WAMessage, meId: string, meLid: string) => {
|
||||
// ensure remoteJid and participant doesn't have device or agent in it
|
||||
if (isHostedPnUser(message.key.remoteJid!) || isHostedLidUser(message.key.remoteJid!)) {
|
||||
const remoteJid = message.key.remoteJid ?? ''
|
||||
if (isHostedPnUser(remoteJid) || isHostedLidUser(remoteJid)) {
|
||||
message.key.remoteJid = jidEncode(
|
||||
jidDecode(message.key?.remoteJid!)?.user!,
|
||||
isHostedPnUser(message.key.remoteJid!) ? 's.whatsapp.net' : 'lid'
|
||||
jidDecode(remoteJid)?.user ?? '',
|
||||
isHostedPnUser(remoteJid) ? 's.whatsapp.net' : 'lid'
|
||||
)
|
||||
} else {
|
||||
message.key.remoteJid = jidNormalizedUser(message.key.remoteJid!)
|
||||
message.key.remoteJid = jidNormalizedUser(remoteJid)
|
||||
}
|
||||
|
||||
if (isHostedPnUser(message.key.participant!) || isHostedLidUser(message.key.participant!)) {
|
||||
const participantJid = message.key.participant ?? ''
|
||||
if (isHostedPnUser(participantJid) || isHostedLidUser(participantJid)) {
|
||||
message.key.participant = jidEncode(
|
||||
jidDecode(message.key.participant!)?.user!,
|
||||
isHostedPnUser(message.key.participant!) ? 's.whatsapp.net' : 'lid'
|
||||
jidDecode(participantJid)?.user ?? '',
|
||||
isHostedPnUser(participantJid) ? 's.whatsapp.net' : 'lid'
|
||||
)
|
||||
} else {
|
||||
message.key.participant = jidNormalizedUser(message.key.participant!)
|
||||
message.key.participant = jidNormalizedUser(participantJid)
|
||||
}
|
||||
|
||||
const content = normalizeMessageContent(message.message)
|
||||
// if the message has a reaction, ensure fromMe & remoteJid are from our perspective
|
||||
if (content?.reactionMessage) {
|
||||
normaliseKey(content.reactionMessage.key!)
|
||||
const reactionKey = content.reactionMessage.key
|
||||
if (reactionKey) {
|
||||
normaliseKey(reactionKey)
|
||||
}
|
||||
}
|
||||
|
||||
if (content?.pollUpdateMessage) {
|
||||
normaliseKey(content.pollUpdateMessage.pollCreationMessageKey!)
|
||||
const pollCreationKey = content.pollUpdateMessage.pollCreationMessageKey
|
||||
if (pollCreationKey) {
|
||||
normaliseKey(pollCreationKey)
|
||||
}
|
||||
}
|
||||
|
||||
function normaliseKey(msgKey: WAMessageKey) {
|
||||
@@ -94,8 +102,8 @@ export const cleanMessage = (message: WAMessage, meId: string, meLid: string) =>
|
||||
// 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) ||
|
||||
areJidsSameUser(msgKey.participant || msgKey.remoteJid!, meLid)
|
||||
? areJidsSameUser(msgKey.participant || (msgKey.remoteJid ?? ''), meId) ||
|
||||
areJidsSameUser(msgKey.participant || (msgKey.remoteJid ?? ''), meLid)
|
||||
: // if the message being reacted to, was from them
|
||||
// fromMe automatically becomes false
|
||||
false
|
||||
@@ -150,10 +158,11 @@ export const normalizeMessageJids = async (
|
||||
export const isRealMessage = (message: WAMessage) => {
|
||||
const normalizedContent = normalizeMessageContent(message.message)
|
||||
const hasSomeContent = !!getContentType(normalizedContent)
|
||||
const stubType = message.messageStubType ?? 0
|
||||
return (
|
||||
(!!normalizedContent ||
|
||||
REAL_MSG_STUB_TYPES.has(message.messageStubType!) ||
|
||||
REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType!)) &&
|
||||
REAL_MSG_STUB_TYPES.has(stubType) ||
|
||||
REAL_MSG_REQ_ME_STUB_TYPES.has(stubType)) &&
|
||||
hasSomeContent &&
|
||||
!normalizedContent?.protocolMessage &&
|
||||
!normalizedContent?.reactionMessage &&
|
||||
@@ -168,11 +177,12 @@ export const shouldIncrementChatUnread = (message: WAMessage) => !message.key.fr
|
||||
* Typically -- that'll be the remoteJid, but for broadcasts, it'll be the participant
|
||||
*/
|
||||
export const getChatId = ({ remoteJid, participant, fromMe }: WAMessageKey) => {
|
||||
if (isJidBroadcast(remoteJid!) && !isJidStatusBroadcast(remoteJid!) && !fromMe) {
|
||||
return participant!
|
||||
const jid = remoteJid ?? ''
|
||||
if (isJidBroadcast(jid) && !isJidStatusBroadcast(jid) && !fromMe) {
|
||||
return participant ?? ''
|
||||
}
|
||||
|
||||
return remoteJid!
|
||||
return jid
|
||||
}
|
||||
|
||||
type PollContext = {
|
||||
@@ -219,7 +229,11 @@ export function decryptPollVote(
|
||||
const decKey = hmacSign(sign, key0, 'sha256')
|
||||
const aad = toBinary(`${pollMsgId}\u0000${voterJid}`)
|
||||
|
||||
const decrypted = aesDecryptGCM(encPayload!, decKey, encIv!, aad)
|
||||
if (!encPayload || !encIv) {
|
||||
throw new Error('Missing encPayload or encIv for poll vote decryption')
|
||||
}
|
||||
|
||||
const decrypted = aesDecryptGCM(encPayload, decKey, encIv, aad)
|
||||
return proto.Message.PollVoteMessage.decode(decrypted)
|
||||
|
||||
function toBinary(txt: string) {
|
||||
@@ -249,7 +263,11 @@ export function decryptEventResponse(
|
||||
const decKey = hmacSign(sign, key0, 'sha256')
|
||||
const aad = toBinary(`${eventMsgId}\u0000${responderJid}`)
|
||||
|
||||
const decrypted = aesDecryptGCM(encPayload!, decKey, encIv!, aad)
|
||||
if (!encPayload || !encIv) {
|
||||
throw new Error('Missing encPayload or encIv for event response decryption')
|
||||
}
|
||||
|
||||
const decrypted = aesDecryptGCM(encPayload, decKey, encIv, aad)
|
||||
return proto.Message.EventResponseMessage.decode(decrypted)
|
||||
|
||||
function toBinary(txt: string) {
|
||||
@@ -271,7 +289,12 @@ const processMessage = async (
|
||||
getMessage
|
||||
}: ProcessMessageContext
|
||||
) => {
|
||||
const meId = creds.me!.id
|
||||
const meUser = creds.me
|
||||
if (!meUser) {
|
||||
return
|
||||
}
|
||||
|
||||
const meId = meUser.id
|
||||
const { accountSettings } = creds
|
||||
|
||||
const chat: Partial<Chat> = { id: jidNormalizedUser(getChatId(message.key)) }
|
||||
@@ -299,7 +322,10 @@ const processMessage = async (
|
||||
if (protocolMsg) {
|
||||
switch (protocolMsg.type) {
|
||||
case proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
|
||||
const histNotification = protocolMsg.historySyncNotification!
|
||||
const histNotification = protocolMsg.historySyncNotification
|
||||
if (!histNotification) {
|
||||
break
|
||||
}
|
||||
const process = shouldProcessHistoryMsg
|
||||
const isLatest = !creds.processedHistoryMessages?.length
|
||||
|
||||
@@ -366,16 +392,23 @@ const processMessage = async (
|
||||
|
||||
break
|
||||
case proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
|
||||
const keys = protocolMsg.appStateSyncKeyShare!.keys
|
||||
const keys = protocolMsg.appStateSyncKeyShare?.keys
|
||||
if (keys?.length) {
|
||||
let newAppStateSyncKeyId = ''
|
||||
await keyStore.transaction(async () => {
|
||||
const newKeys: string[] = []
|
||||
for (const { keyData, keyId } of keys) {
|
||||
const strKeyId = Buffer.from(keyId!.keyId!).toString('base64')
|
||||
const keyIdValue = keyId?.keyId
|
||||
if (!keyIdValue) {
|
||||
continue
|
||||
}
|
||||
|
||||
const strKeyId = Buffer.from(keyIdValue).toString('base64')
|
||||
newKeys.push(strKeyId)
|
||||
|
||||
await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData! } })
|
||||
if (keyData) {
|
||||
await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } })
|
||||
}
|
||||
|
||||
newAppStateSyncKeyId = strKeyId
|
||||
}
|
||||
@@ -394,7 +427,7 @@ const processMessage = async (
|
||||
{
|
||||
key: {
|
||||
...message.key,
|
||||
id: protocolMsg.key!.id
|
||||
id: protocolMsg.key?.id
|
||||
},
|
||||
update: { message: null, messageStubType: WAMessageStubType.REVOKE, key: message.key }
|
||||
}
|
||||
@@ -407,17 +440,27 @@ const processMessage = async (
|
||||
})
|
||||
break
|
||||
case proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
|
||||
const response = protocolMsg.peerDataOperationRequestResponseMessage!
|
||||
const response = protocolMsg.peerDataOperationRequestResponseMessage
|
||||
if (response) {
|
||||
await placeholderResendCache?.del(response.stanzaId!)
|
||||
if (response.stanzaId) {
|
||||
await placeholderResendCache?.del(response.stanzaId)
|
||||
}
|
||||
// TODO: IMPLEMENT HISTORY SYNC ETC (sticker uploads etc.).
|
||||
const { peerDataOperationResult } = response
|
||||
if (!peerDataOperationResult) {
|
||||
break
|
||||
}
|
||||
|
||||
let recoveredCount = 0
|
||||
for (const result of peerDataOperationResult!) {
|
||||
for (const result of peerDataOperationResult) {
|
||||
const { placeholderMessageResendResponse: retryResponse } = result
|
||||
//eslint-disable-next-line max-depth
|
||||
if (retryResponse) {
|
||||
const webMessageInfo = proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes!)
|
||||
if (!retryResponse.webMessageInfoBytes) {
|
||||
continue
|
||||
}
|
||||
|
||||
const webMessageInfo = proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes)
|
||||
|
||||
// Track CTWA message recovery success
|
||||
recoveredCount++
|
||||
@@ -435,7 +478,7 @@ const processMessage = async (
|
||||
ev.emit('messages.upsert', {
|
||||
messages: [webMessageInfo as WAMessage],
|
||||
type: 'notify',
|
||||
requestId: response.stanzaId!
|
||||
requestId: response.stanzaId ?? ''
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -474,17 +517,21 @@ const processMessage = async (
|
||||
const labelAssociationMsg = protocolMsg.memberLabel
|
||||
if (labelAssociationMsg?.label) {
|
||||
ev.emit('group.member-tag.update', {
|
||||
groupId: chat.id!,
|
||||
groupId: chat.id ?? '',
|
||||
label: labelAssociationMsg.label,
|
||||
participant: message.key.participant!,
|
||||
participantAlt: message.key.participantAlt!,
|
||||
participant: message.key.participant ?? '',
|
||||
participantAlt: message.key.participantAlt ?? '',
|
||||
messageTimestamp: Number(message.messageTimestamp)
|
||||
})
|
||||
}
|
||||
|
||||
break
|
||||
case proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC:
|
||||
const encodedPayload = protocolMsg.lidMigrationMappingSyncMessage?.encodedMappingPayload!
|
||||
const encodedPayload = protocolMsg.lidMigrationMappingSyncMessage?.encodedMappingPayload
|
||||
if (!encodedPayload) {
|
||||
break
|
||||
}
|
||||
|
||||
const { pnToLidMappings, chatDbMigrationTimestamp } =
|
||||
proto.LIDMigrationMappingSyncPayload.decode(encodedPayload)
|
||||
logger?.debug({ pnToLidMappings, chatDbMigrationTimestamp }, 'got lid mappings and chat db migration timestamp')
|
||||
@@ -502,6 +549,11 @@ const processMessage = async (
|
||||
}
|
||||
}
|
||||
} else if (content?.reactionMessage) {
|
||||
const reactionKey = content.reactionMessage.key
|
||||
if (!reactionKey) {
|
||||
return
|
||||
}
|
||||
|
||||
const reaction: proto.IReaction = {
|
||||
...content.reactionMessage,
|
||||
key: message.key
|
||||
@@ -509,12 +561,15 @@ const processMessage = async (
|
||||
ev.emit('messages.reaction', [
|
||||
{
|
||||
reaction,
|
||||
key: content.reactionMessage?.key!
|
||||
key: reactionKey
|
||||
}
|
||||
])
|
||||
} else if (content?.encEventResponseMessage) {
|
||||
const encEventResponse = content.encEventResponseMessage
|
||||
const creationMsgKey = encEventResponse.eventCreationMessageKey!
|
||||
const creationMsgKey = encEventResponse.eventCreationMessageKey
|
||||
if (!creationMsgKey) {
|
||||
return
|
||||
}
|
||||
|
||||
// we need to fetch the event creation message to get the event enc key
|
||||
const eventMsg = await getMessage(creationMsgKey)
|
||||
@@ -523,12 +578,16 @@ const processMessage = async (
|
||||
const meIdNormalised = jidNormalizedUser(meId)
|
||||
|
||||
// all jids need to be PN
|
||||
const eventCreatorKey = creationMsgKey.participant || creationMsgKey.remoteJid!
|
||||
const eventCreatorKey = creationMsgKey.participant || (creationMsgKey.remoteJid ?? '')
|
||||
const eventCreatorPn = isLidUser(eventCreatorKey)
|
||||
? await signalRepository.lidMapping.getPNForLID(eventCreatorKey)
|
||||
: eventCreatorKey
|
||||
if (!eventCreatorPn) {
|
||||
return
|
||||
}
|
||||
|
||||
const eventCreatorJid = getKeyAuthor(
|
||||
{ remoteJid: jidNormalizedUser(eventCreatorPn!), fromMe: meIdNormalised === eventCreatorPn },
|
||||
{ remoteJid: jidNormalizedUser(eventCreatorPn), fromMe: meIdNormalised === eventCreatorPn },
|
||||
meIdNormalised
|
||||
)
|
||||
|
||||
@@ -541,7 +600,7 @@ const processMessage = async (
|
||||
const responseMsg = decryptEventResponse(encEventResponse, {
|
||||
eventEncKey,
|
||||
eventCreatorJid,
|
||||
eventMsgId: creationMsgKey.id!,
|
||||
eventMsgId: creationMsgKey.id ?? '',
|
||||
responderJid
|
||||
})
|
||||
|
||||
|
||||
@@ -64,7 +64,12 @@ const getClientPayload = (config: SocketConfig) => {
|
||||
}
|
||||
|
||||
export const generateLoginNode = (userJid: string, config: SocketConfig): proto.IClientPayload => {
|
||||
const { user, device } = jidDecode(userJid)!
|
||||
const decoded = jidDecode(userJid)
|
||||
if (!decoded) {
|
||||
throw new Boom('Invalid user JID', { statusCode: 400 })
|
||||
}
|
||||
|
||||
const { user, device } = decoded
|
||||
const payload: proto.IClientPayload = {
|
||||
...getClientPayload(config),
|
||||
passive: true,
|
||||
@@ -153,6 +158,9 @@ export const configureSuccessfulPairing = (
|
||||
}: Pick<AuthenticationCreds, 'advSecretKey' | 'signedIdentityKey' | 'signalIdentities'>
|
||||
) => {
|
||||
const msgId = stanza.attrs.id
|
||||
if (!msgId) {
|
||||
throw new Boom('Missing message ID', { statusCode: 400 })
|
||||
}
|
||||
|
||||
const pairSuccessNode = getBinaryNodeChild(stanza, 'pair-success')
|
||||
|
||||
@@ -168,42 +176,51 @@ export const configureSuccessfulPairing = (
|
||||
const bizName = businessNode?.attrs.name
|
||||
const jid = deviceNode.attrs.jid
|
||||
const lid = deviceNode.attrs.lid
|
||||
if (!jid || !lid) {
|
||||
throw new Boom('Missing JID or LID in device node', { statusCode: 400 })
|
||||
}
|
||||
|
||||
const { details, hmac, accountType } = proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content as Buffer)
|
||||
if (!details || !hmac) {
|
||||
throw new Boom('Missing ADV signature fields', { statusCode: 400 })
|
||||
}
|
||||
|
||||
let hmacPrefix = Buffer.from([])
|
||||
if (accountType !== undefined && accountType === proto.ADVEncryptionType.HOSTED) {
|
||||
hmacPrefix = WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX
|
||||
}
|
||||
|
||||
const advSign = hmacSign(Buffer.concat([hmacPrefix, details!]), Buffer.from(advSecretKey, 'base64'))
|
||||
if (Buffer.compare(hmac!, advSign) !== 0) {
|
||||
const advSign = hmacSign(Buffer.concat([hmacPrefix, details]), Buffer.from(advSecretKey, 'base64'))
|
||||
if (Buffer.compare(hmac, advSign) !== 0) {
|
||||
throw new Boom('Invalid account signature')
|
||||
}
|
||||
|
||||
const account = proto.ADVSignedDeviceIdentity.decode(details!)
|
||||
const account = proto.ADVSignedDeviceIdentity.decode(details)
|
||||
const { accountSignatureKey, accountSignature, details: deviceDetails } = account
|
||||
if (!accountSignatureKey || !accountSignature || !deviceDetails) {
|
||||
throw new Boom('Missing ADV account fields', { statusCode: 400 })
|
||||
}
|
||||
|
||||
const deviceIdentity = proto.ADVDeviceIdentity.decode(deviceDetails!)
|
||||
const deviceIdentity = proto.ADVDeviceIdentity.decode(deviceDetails)
|
||||
|
||||
const accountSignaturePrefix =
|
||||
deviceIdentity.deviceType === proto.ADVEncryptionType.HOSTED
|
||||
? WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX
|
||||
: WA_ADV_ACCOUNT_SIG_PREFIX
|
||||
const accountMsg = Buffer.concat([accountSignaturePrefix, deviceDetails!, signedIdentityKey.public])
|
||||
if (!Curve.verify(accountSignatureKey!, accountMsg, accountSignature!)) {
|
||||
const accountMsg = Buffer.concat([accountSignaturePrefix, deviceDetails, signedIdentityKey.public])
|
||||
if (!Curve.verify(accountSignatureKey, accountMsg, accountSignature)) {
|
||||
throw new Boom('Failed to verify account signature')
|
||||
}
|
||||
|
||||
const deviceMsg = Buffer.concat([
|
||||
WA_ADV_DEVICE_SIG_PREFIX,
|
||||
deviceDetails!,
|
||||
deviceDetails,
|
||||
signedIdentityKey.public,
|
||||
accountSignatureKey!
|
||||
accountSignatureKey
|
||||
])
|
||||
account.deviceSignature = Curve.sign(signedIdentityKey.private, deviceMsg)
|
||||
|
||||
const identity = createSignalIdentity(lid!, accountSignatureKey!)
|
||||
const identity = createSignalIdentity(lid, accountSignatureKey)
|
||||
const accountEnc = encodeSignedDeviceIdentity(account, false)
|
||||
|
||||
const reply: BinaryNode = {
|
||||
@@ -211,7 +228,7 @@ export const configureSuccessfulPairing = (
|
||||
attrs: {
|
||||
to: S_WHATSAPP_NET,
|
||||
type: 'result',
|
||||
id: msgId!
|
||||
id: msgId
|
||||
},
|
||||
content: [
|
||||
{
|
||||
@@ -220,7 +237,7 @@ export const configureSuccessfulPairing = (
|
||||
content: [
|
||||
{
|
||||
tag: 'device-identity',
|
||||
attrs: { 'key-index': deviceIdentity.keyIndex!.toString() },
|
||||
attrs: { 'key-index': String(deviceIdentity.keyIndex ?? '') },
|
||||
content: accountEnc
|
||||
}
|
||||
]
|
||||
@@ -230,7 +247,7 @@ export const configureSuccessfulPairing = (
|
||||
|
||||
const authUpdate: Partial<AuthenticationCreds> = {
|
||||
account,
|
||||
me: { id: jid!, name: bizName, lid },
|
||||
me: { id: jid, name: bizName, lid },
|
||||
signalIdentities: [...(signalIdentities || []), identity],
|
||||
platform: platformNode?.attrs.name
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user