Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make message hash compatible with decoded message #1993

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion packages/message-hash/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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);
});
});
20 changes: 14 additions & 6 deletions packages/message-hash/src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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(
[
Expand All @@ -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);
Expand Down
Loading