mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-01-14 14:03:11 +00:00
* chore: update noise * update: package.lock * update: @chainsafe/libp2p-gossipsub * rm unwanted libp2p interface deps & bump up libp2p * refactor code for new deps * update: new package.lock * setup prettier, refactor eslint and rm trailing commas * update package.lock * fix build * import type for interface * fix imports for merge * update typedoc exports * add: CustomEvent import * use new libp2p interface * add aegir as dev dep for tests
30 lines
780 B
TypeScript
30 lines
780 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);
|
|
}
|