Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Socket/messages-send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import {
unixTimestampSeconds
} from "../Utils";
import { getUrlInfo } from "../Utils/link-preview";
import {
getMessageReportingToken,
shouldIncludeReportingToken
} from "../Utils/reporting-utils";
import {
areJidsSameUser,
BinaryNode,
Expand Down Expand Up @@ -747,6 +751,34 @@ export const makeMessagesSocket = (config: SocketConfig) => {
logger.debug({ jid }, `adding biz node for buttons message`);
}

if (
!isRetryResend &&
shouldIncludeReportingToken(message) &&
message.messageContextInfo?.messageSecret
) {
const reportingKey: WAMessageKey = {
id: msgId,
fromMe: true,
remoteJid: destinationJid,
participant: participant?.jid
};

const reportingNode = await getMessageReportingToken(
encodedMsg,
message,
reportingKey
).catch(err => {
logger.warn({ jid, err }, "failed to attach reporting token");

return null;
});

if (reportingNode) {
(stanza.content as BinaryNode[]).push(reportingNode);
logger.trace({ jid }, "added reporting token to message");
}
}

logger.debug(
{ msgId },
`sending message to ${participants.length} devices`
Expand Down
21 changes: 12 additions & 9 deletions src/Utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,20 @@ export function sha256(buffer: Buffer) {
}

// HKDF key expansion
export function hkdf(
export const hkdf = (
buffer: Uint8Array | Buffer,
expandedLength: number,
info: { salt?: Buffer; info?: string }
) {
return HKDF(
!Buffer.isBuffer(buffer) ? Buffer.from(buffer) : buffer,
expandedLength,
info
);
}
params: { salt?: Buffer; info?: Buffer | string }
) => {
const key = Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);

const normalized =
params.info && Buffer.isBuffer(params.info)
? { ...params, info: params.info.toString("latin1") }
: params;

return HKDF(key, expandedLength, normalized);
};

export async function derivePairingCodeKey(pairingCode: string, salt: Buffer) {
return await pbkdf2Promise(pairingCode, salt, 2 << 16, 32, "sha256");
Expand Down
9 changes: 9 additions & 0 deletions src/Utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
MediaDownloadOptions
} from "./messages-media";
import { sha256 } from "./crypto";
import { shouldIncludeReportingToken } from "./reporting-utils";
import { randomBytes } from "crypto";

type MediaUploadData = {
media: WAMediaUpload;
Expand Down Expand Up @@ -489,6 +491,13 @@ export const generateWAMessageContent = async (
};
}

if (shouldIncludeReportingToken(m)) {
m.messageContextInfo = m.messageContextInfo || {};
if (!m.messageContextInfo.messageSecret) {
m.messageContextInfo.messageSecret = randomBytes(32);
}
}

return WAProto.Message.fromObject(m);
};

Expand Down
Loading