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:
Sasha 2024-04-30 15:37:10 +02:00 committed by GitHub
parent 5b03709dfe
commit e5293356d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 7 deletions

View File

@ -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 { bytesToHex, hexToBytes } from "@waku/utils/bytes";
import { expect } from "chai"; import { expect } from "chai";
@ -75,4 +75,38 @@ describe("RFC Test Vectors", () => {
const hash = messageHash(pubsubTopic, message); const hash = messageHash(pubsubTopic, message);
expect(bytesToHex(hash)).to.equal(expectedHash); 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);
});
}); });

View File

@ -1,5 +1,5 @@
import { sha256 } from "@noble/hashes/sha256"; 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 { isDefined } from "@waku/utils";
import { import {
bytesToUtf8, bytesToUtf8,
@ -14,13 +14,11 @@ import {
*/ */
export function messageHash( export function messageHash(
pubsubTopic: string, pubsubTopic: string,
message: IProtoMessage message: IProtoMessage | IDecodedMessage
): Uint8Array { ): Uint8Array {
const pubsubTopicBytes = utf8ToBytes(pubsubTopic); const pubsubTopicBytes = utf8ToBytes(pubsubTopic);
const contentTopicBytes = utf8ToBytes(message.contentTopic); const contentTopicBytes = utf8ToBytes(message.contentTopic);
const timestampBytes = message.timestamp const timestampBytes = tryConvertTimestampToBytes(message.timestamp);
? numberToBytes(message.timestamp)
: undefined;
const bytes = concat( const bytes = concat(
[ [
@ -35,9 +33,19 @@ export function messageHash(
return sha256(bytes); return sha256(bytes);
} }
function tryConvertTimestampToBytes(
timestamp: Date | number | bigint | undefined
): undefined | Uint8Array {
if (!timestamp) {
return;
}
return numberToBytes(timestamp.valueOf());
}
export function messageHashStr( export function messageHashStr(
pubsubTopic: string, pubsubTopic: string,
message: IProtoMessage message: IProtoMessage | IDecodedMessage
): string { ): string {
const hash = messageHash(pubsubTopic, message); const hash = messageHash(pubsubTopic, message);
const hashStr = bytesToUtf8(hash); const hashStr = bytesToUtf8(hash);