mirror of https://github.com/waku-org/js-waku.git
feat: make message hash compatible with decoded message (#1993)
* feat: make message hash compatible with decoded message * add util fn * fix test * up hashes * up
This commit is contained in:
parent
5b03709dfe
commit
e5293356d8
|
@ -1,4 +1,4 @@
|
|||
import type { IProtoMessage } from "@waku/interfaces";
|
||||
import type { IDecodedMessage, IProtoMessage } from "@waku/interfaces";
|
||||
import { bytesToHex, hexToBytes } from "@waku/utils/bytes";
|
||||
import { expect } from "chai";
|
||||
|
||||
|
@ -75,4 +75,38 @@ describe("RFC Test Vectors", () => {
|
|||
const hash = messageHash(pubsubTopic, message);
|
||||
expect(bytesToHex(hash)).to.equal(expectedHash);
|
||||
});
|
||||
|
||||
it("Waku message hash computation (no timestamp)", () => {
|
||||
const expectedHash =
|
||||
"e1a9596237dbe2cc8aaf4b838c46a7052df6bc0d42ba214b998a8bfdbe8487d6";
|
||||
const pubsubTopic = "/waku/2/default-waku/proto";
|
||||
const message: IProtoMessage = {
|
||||
payload: new Uint8Array(),
|
||||
contentTopic: "/waku/2/default-content/proto",
|
||||
meta: hexToBytes("0x73757065722d736563726574"),
|
||||
timestamp: undefined,
|
||||
ephemeral: undefined,
|
||||
rateLimitProof: undefined,
|
||||
version: undefined
|
||||
};
|
||||
const hash = messageHash(pubsubTopic, message);
|
||||
expect(bytesToHex(hash)).to.equal(expectedHash);
|
||||
});
|
||||
|
||||
it("Waku message hash computation (message is IDecodedMessage)", () => {
|
||||
const expectedHash =
|
||||
"bd81b27902ad51f49e8f73ff8db4a96994040c9421da88b7ee8ba07bd39070b2";
|
||||
const pubsubTopic = "/waku/2/default-waku/proto";
|
||||
const message: IDecodedMessage = {
|
||||
payload: new Uint8Array(),
|
||||
pubsubTopic,
|
||||
contentTopic: "/waku/2/default-content/proto",
|
||||
meta: hexToBytes("0x73757065722d736563726574"),
|
||||
timestamp: new Date("2024-04-30T10:54:14.978Z"),
|
||||
ephemeral: undefined,
|
||||
rateLimitProof: undefined
|
||||
};
|
||||
const hash = messageHash(pubsubTopic, message);
|
||||
expect(bytesToHex(hash)).to.equal(expectedHash);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { sha256 } from "@noble/hashes/sha256";
|
||||
import type { IProtoMessage } from "@waku/interfaces";
|
||||
import type { IDecodedMessage, IProtoMessage } from "@waku/interfaces";
|
||||
import { isDefined } from "@waku/utils";
|
||||
import {
|
||||
bytesToUtf8,
|
||||
|
@ -14,13 +14,11 @@ import {
|
|||
*/
|
||||
export function messageHash(
|
||||
pubsubTopic: string,
|
||||
message: IProtoMessage
|
||||
message: IProtoMessage | IDecodedMessage
|
||||
): Uint8Array {
|
||||
const pubsubTopicBytes = utf8ToBytes(pubsubTopic);
|
||||
const contentTopicBytes = utf8ToBytes(message.contentTopic);
|
||||
const timestampBytes = message.timestamp
|
||||
? numberToBytes(message.timestamp)
|
||||
: undefined;
|
||||
const timestampBytes = tryConvertTimestampToBytes(message.timestamp);
|
||||
|
||||
const bytes = concat(
|
||||
[
|
||||
|
@ -35,9 +33,19 @@ export function messageHash(
|
|||
return sha256(bytes);
|
||||
}
|
||||
|
||||
function tryConvertTimestampToBytes(
|
||||
timestamp: Date | number | bigint | undefined
|
||||
): undefined | Uint8Array {
|
||||
if (!timestamp) {
|
||||
return;
|
||||
}
|
||||
|
||||
return numberToBytes(timestamp.valueOf());
|
||||
}
|
||||
|
||||
export function messageHashStr(
|
||||
pubsubTopic: string,
|
||||
message: IProtoMessage
|
||||
message: IProtoMessage | IDecodedMessage
|
||||
): string {
|
||||
const hash = messageHash(pubsubTopic, message);
|
||||
const hashStr = bytesToUtf8(hash);
|
||||
|
|
Loading…
Reference in New Issue