2023-03-13 14:50:46 +11:00
|
|
|
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,
|
2023-08-16 20:18:13 +05:30
|
|
|
message: IProtoMessage
|
2023-03-13 14:50:46 +11:00
|
|
|
): Uint8Array {
|
|
|
|
|
const pubsubTopicBytes = utf8ToBytes(pubsubTopic);
|
|
|
|
|
const contentTopicBytes = utf8ToBytes(message.contentTopic);
|
|
|
|
|
|
2023-03-14 15:27:47 +11:00
|
|
|
let bytes;
|
2023-03-13 14:50:46 +11:00
|
|
|
|
|
|
|
|
if (message.meta) {
|
2023-03-14 15:27:47 +11:00
|
|
|
bytes = concat([
|
2023-03-13 14:50:46 +11:00
|
|
|
pubsubTopicBytes,
|
|
|
|
|
message.payload,
|
|
|
|
|
contentTopicBytes,
|
2023-08-16 20:18:13 +05:30
|
|
|
message.meta
|
2023-03-13 14:50:46 +11:00
|
|
|
]);
|
|
|
|
|
} else {
|
2023-03-14 15:27:47 +11:00
|
|
|
bytes = concat([pubsubTopicBytes, message.payload, contentTopicBytes]);
|
2023-03-13 14:50:46 +11:00
|
|
|
}
|
2023-03-14 15:27:47 +11:00
|
|
|
return sha256(bytes);
|
2023-03-13 14:50:46 +11:00
|
|
|
}
|