mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-02-03 05:23:10 +00:00
* feat: lighten retry logic for LightPush * update tests * remove base protocol sdk from light push, add unit tests for light push * remove replaced test * ensure numPeersToUse is respected * turn off check for missing messages * fix recurring ping * add useful logs * skip tests * remove comment * feat: check filter subscriptions against lightPush (#2185)
62 lines
1.4 KiB
TypeScript
62 lines
1.4 KiB
TypeScript
import { sha256 } from "@noble/hashes/sha256";
|
|
import type { IDecodedMessage, IProtoMessage } from "@waku/interfaces";
|
|
import { isDefined } from "@waku/utils";
|
|
import {
|
|
bytesToHex,
|
|
concat,
|
|
numberToBytes,
|
|
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 | IDecodedMessage
|
|
): Uint8Array {
|
|
const pubsubTopicBytes = utf8ToBytes(pubsubTopic);
|
|
const contentTopicBytes = utf8ToBytes(message.contentTopic);
|
|
const timestampBytes = tryConvertTimestampToBytes(message.timestamp);
|
|
|
|
const bytes = concat(
|
|
[
|
|
pubsubTopicBytes,
|
|
message.payload,
|
|
contentTopicBytes,
|
|
message.meta,
|
|
timestampBytes
|
|
].filter(isDefined)
|
|
);
|
|
|
|
return sha256(bytes);
|
|
}
|
|
|
|
function tryConvertTimestampToBytes(
|
|
timestamp: Date | number | bigint | undefined
|
|
): undefined | Uint8Array {
|
|
if (!timestamp) {
|
|
return;
|
|
}
|
|
|
|
let bigIntTimestamp: bigint;
|
|
|
|
if (typeof timestamp === "bigint") {
|
|
bigIntTimestamp = timestamp;
|
|
} else {
|
|
bigIntTimestamp = BigInt(timestamp.valueOf()) * 1000000n;
|
|
}
|
|
|
|
return numberToBytes(bigIntTimestamp);
|
|
}
|
|
|
|
export function messageHashStr(
|
|
pubsubTopic: string,
|
|
message: IProtoMessage | IDecodedMessage
|
|
): string {
|
|
const hash = messageHash(pubsubTopic, message);
|
|
const hashStr = bytesToHex(hash);
|
|
return hashStr;
|
|
}
|