Skip to content

Commit

Permalink
feat: make message hash compatible with decoded message (#1993)
Browse files Browse the repository at this point in the history
* feat: make message hash compatible with decoded message

* add util fn

* fix test

* up hashes

* up
  • Loading branch information
weboko authored Apr 30, 2024
1 parent 5b03709 commit e529335
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
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

0 comments on commit e529335

Please sign in to comment.