From fea4f2577b67730dc35625c4347d9ac9adc66340 Mon Sep 17 00:00:00 2001 From: Danish Arora <35004822+danisharora099@users.noreply.github.com> Date: Thu, 1 Aug 2024 15:19:35 +0530 Subject: [PATCH] chore(message-hash): use `timestamp` in nanoseconds instead of milliseconds (#2094) * chore: convert timestamp from miliseconds to nanoseconds * chore: test against hash accounting for nanoseconds instead of milliseconds the hash used was calculated using timestamp in milliseconds, instead of nanoseconds (not part of the RFC test vectors)Y --- packages/message-hash/src/index.spec.ts | 2 +- packages/message-hash/src/index.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/message-hash/src/index.spec.ts b/packages/message-hash/src/index.spec.ts index d8045be6b9..62846c6415 100644 --- a/packages/message-hash/src/index.spec.ts +++ b/packages/message-hash/src/index.spec.ts @@ -95,7 +95,7 @@ describe("RFC Test Vectors", () => { it("Waku message hash computation (message is IDecodedMessage)", () => { const expectedHash = - "bd81b27902ad51f49e8f73ff8db4a96994040c9421da88b7ee8ba07bd39070b2"; + "3f11bc950dce0e3ffdcf205ae6414c01130bb5d9f20644869bff80407fa52c8f"; const pubsubTopic = "/waku/2/default-waku/proto"; const message: IDecodedMessage = { payload: new Uint8Array(), diff --git a/packages/message-hash/src/index.ts b/packages/message-hash/src/index.ts index 7cfd57a0dc..825143ca95 100644 --- a/packages/message-hash/src/index.ts +++ b/packages/message-hash/src/index.ts @@ -40,7 +40,15 @@ function tryConvertTimestampToBytes( return; } - return numberToBytes(timestamp.valueOf()); + let bigIntTimestamp: bigint; + + if (typeof timestamp === "bigint") { + bigIntTimestamp = timestamp; + } else { + bigIntTimestamp = BigInt(timestamp.valueOf()) * 1000000n; + } + + return numberToBytes(bigIntTimestamp); } export function messageHashStr(