mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-05 23:33:08 +00:00
* update to latest prettier * fix with prettier: added trailing comma * remove deps from test package, add sinon types, fix type hack in a test, update esling prettier config * update typescript eslint plugins * update package-locks
30 lines
782 B
TypeScript
30 lines
782 B
TypeScript
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);
|
|
|
|
let bytes;
|
|
|
|
if (message.meta) {
|
|
bytes = concat([
|
|
pubsubTopicBytes,
|
|
message.payload,
|
|
contentTopicBytes,
|
|
message.meta,
|
|
]);
|
|
} else {
|
|
bytes = concat([pubsubTopicBytes, message.payload, contentTopicBytes]);
|
|
}
|
|
return sha256(bytes);
|
|
}
|