feat: force include identity when enc is missing

This commit is contained in:
Adhiraj Singh
2022-07-14 17:41:11 +05:30
parent fae4aafdcd
commit 33851cdd55

View File

@@ -2,10 +2,10 @@
import { proto } from '../../WAProto' import { proto } from '../../WAProto'
import { KEY_BUNDLE_TYPE, MIN_PREKEY_COUNT } from '../Defaults' import { KEY_BUNDLE_TYPE, MIN_PREKEY_COUNT } from '../Defaults'
import { MessageReceiptType, MessageRelayOptions, MessageUserReceipt, SocketConfig, WACallEvent, WAMessageKey, WAMessageStubType, WAPatchName } from '../Types' import { MessageReceiptType, MessageRelayOptions, MessageUserReceipt, SocketConfig, WACallEvent, WAMessageKey, WAMessageStubType, WAPatchName } from '../Types'
import { decodeMediaRetryNode, decodeMessageStanza, delay, encodeBigEndian, generateSignalPubKey, getCallStatusFromNode, getNextPreKeys, getStatusFromReceiptType, isHistoryMsg, unixTimestampSeconds, xmppPreKey, xmppSignedPreKey } from '../Utils' import { decodeMediaRetryNode, decodeMessageStanza, delay, encodeBigEndian, getCallStatusFromNode, getNextPreKeys, getStatusFromReceiptType, isHistoryMsg, unixTimestampSeconds, xmppPreKey, xmppSignedPreKey } from '../Utils'
import { makeMutex } from '../Utils/make-mutex' import { makeMutex } from '../Utils/make-mutex'
import { cleanMessage } from '../Utils/process-message' import { cleanMessage } from '../Utils/process-message'
import { areJidsSameUser, BinaryNode, BinaryNodeAttributes, getAllBinaryNodeChildren, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, isJidUser, jidDecode, jidEncode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary' import { areJidsSameUser, BinaryNode, BinaryNodeAttributes, getAllBinaryNodeChildren, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, isJidUser, jidDecode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary'
import { extractGroupMetadata } from './groups' import { extractGroupMetadata } from './groups'
import { makeMessagesSocket } from './messages-send' import { makeMessagesSocket } from './messages-send'
@@ -62,7 +62,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
await sendNode(stanza) await sendNode(stanza)
} }
const sendRetryRequest = async(node: BinaryNode) => { const sendRetryRequest = async(node: BinaryNode, forceIncludeKeys = false) => {
const msgId = node.attrs.id const msgId = node.attrs.id
let retryCount = msgRetryMap[msgId] || 0 let retryCount = msgRetryMap[msgId] || 0
@@ -75,19 +75,17 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
retryCount += 1 retryCount += 1
msgRetryMap[msgId] = retryCount msgRetryMap[msgId] = retryCount
const isGroup = !!node.attrs.participant
const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds
const deviceIdentity = proto.ADVSignedDeviceIdentity.encode(account!).finish() const deviceIdentity = proto.ADVSignedDeviceIdentity.encode(account!).finish()
await authState.keys.transaction( await authState.keys.transaction(
async() => { async() => {
const decFrom = node.attrs.from ? jidDecode(node.attrs.from) : undefined
const receipt: BinaryNode = { const receipt: BinaryNode = {
tag: 'receipt', tag: 'receipt',
attrs: { attrs: {
id: msgId, id: msgId,
type: 'retry', type: 'retry',
to: isGroup ? node.attrs.from : jidEncode(decFrom!.user, 's.whatsapp.net', decFrom!.device, 0) to: node.attrs.from
}, },
content: [ content: [
{ {
@@ -115,19 +113,18 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
receipt.attrs.participant = node.attrs.participant receipt.attrs.participant = node.attrs.participant
} }
if(retryCount > 1) { if(retryCount > 1 || forceIncludeKeys) {
const { update, preKeys } = await getNextPreKeys(authState, 1) const { update, preKeys } = await getNextPreKeys(authState, 1)
const [keyId] = Object.keys(preKeys) const [keyId] = Object.keys(preKeys)
const key = preKeys[+keyId] const key = preKeys[+keyId]
const exec = generateSignalPubKey(Buffer.from(KEY_BUNDLE_TYPE)).slice(0, 1)
const content = receipt.content! as BinaryNode[] const content = receipt.content! as BinaryNode[]
content.push({ content.push({
tag: 'keys', tag: 'keys',
attrs: { }, attrs: { },
content: [ content: [
{ tag: 'type', attrs: { }, content: exec }, { tag: 'type', attrs: { }, content: Buffer.from(KEY_BUNDLE_TYPE) },
{ tag: 'identity', attrs: { }, content: identityKey.public }, { tag: 'identity', attrs: { }, content: identityKey.public },
xmppPreKey(key, +keyId), xmppPreKey(key, +keyId),
xmppSignedPreKey(signedPreKey), xmppSignedPreKey(signedPreKey),
@@ -436,7 +433,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
retryMutex.mutex( retryMutex.mutex(
async() => { async() => {
if(ws.readyState === ws.OPEN) { if(ws.readyState === ws.OPEN) {
await sendRetryRequest(node) const encNode = getBinaryNodeChild(node, 'enc')
await sendRetryRequest(node, !encNode)
if(retryRequestDelayMs) { if(retryRequestDelayMs) {
await delay(retryRequestDelayMs) await delay(retryRequestDelayMs)
} }