From e5293356d86231f413810f97e41d02430732f15c Mon Sep 17 00:00:00 2001 From: Sasha <118575614+weboko@users.noreply.github.com> Date: Tue, 30 Apr 2024 15:37:10 +0200 Subject: [PATCH] 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 --- packages/message-hash/src/index.spec.ts | 36 ++++++++++++++++++++++++- packages/message-hash/src/index.ts | 20 +++++++++----- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/packages/message-hash/src/index.spec.ts b/packages/message-hash/src/index.spec.ts index e650dfbdf..d8045be6b 100644 --- a/packages/message-hash/src/index.spec.ts +++ b/packages/message-hash/src/index.spec.ts @@ -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); + }); }); diff --git a/packages/message-hash/src/index.ts b/packages/message-hash/src/index.ts index 37f9c80c6..7cfd57a0d 100644 --- a/packages/message-hash/src/index.ts +++ b/packages/message-hash/src/index.ts @@ -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);