30 lines
780 B
TypeScript
Raw Normal View History

import { sha256 } from "@noble/hashes/sha256";
import type { IProtoMessage } from "@waku/interfaces";
import { concat, utf8ToBytes } from "@waku/utils/bytes";
/**
* Deterministic Message Hashing as defined in
* [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/#deterministic-message-hashing)
*/
export function messageHash(
pubsubTopic: string,
message: IProtoMessage
): Uint8Array {
const pubsubTopicBytes = utf8ToBytes(pubsubTopic);
const contentTopicBytes = utf8ToBytes(message.contentTopic);
2023-03-14 15:27:47 +11:00
let bytes;
if (message.meta) {
2023-03-14 15:27:47 +11:00
bytes = concat([
pubsubTopicBytes,
message.payload,
contentTopicBytes,
message.meta
]);
} else {
2023-03-14 15:27:47 +11:00
bytes = concat([pubsubTopicBytes, message.payload, contentTopicBytes]);
}
2023-03-14 15:27:47 +11:00
return sha256(bytes);
}