-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement deterministic message hash logic
- Loading branch information
1 parent
76d1d4f
commit fe57461
Showing
7 changed files
with
123 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { IProtoMessage } from "@waku/interfaces"; | ||
import { bytesToHex, hexToBytes } from "@waku/utils/bytes"; | ||
import { expect } from "chai"; | ||
|
||
import { messageHash } from "./index.js"; | ||
|
||
// https://rfc.vac.dev/spec/14/#test-vectors | ||
describe("RFC Test Vectors", () => { | ||
it("Waku message hash computation", () => { | ||
const expectedHash = | ||
"4fdde1099c9f77f6dae8147b6b3179aba1fc8e14a7bf35203fc253ee479f135f"; | ||
|
||
const pubSubTopic = "/waku/2/default-waku/proto"; | ||
const message: IProtoMessage = { | ||
payload: hexToBytes("0x010203045445535405060708"), | ||
contentTopic: "/waku/2/default-content/proto", | ||
meta: hexToBytes("0x73757065722d736563726574"), | ||
ephemeral: undefined, | ||
rateLimitProof: undefined, | ||
timestamp: undefined, | ||
version: undefined, | ||
}; | ||
|
||
const hash = messageHash(pubSubTopic, message); | ||
|
||
expect(bytesToHex(hash)).to.equal(expectedHash); | ||
}); | ||
|
||
it("Waku message hash computation (meta attribute not present)", () => { | ||
const expectedHash = | ||
"87619d05e563521d9126749b45bd4cc2430df0607e77e23572d874ed9c1aaa62"; | ||
|
||
const pubSubTopic = "/waku/2/default-waku/proto"; | ||
const message: IProtoMessage = { | ||
payload: hexToBytes("0x010203045445535405060708"), | ||
contentTopic: "/waku/2/default-content/proto", | ||
meta: undefined, | ||
ephemeral: undefined, | ||
rateLimitProof: undefined, | ||
timestamp: undefined, | ||
version: undefined, | ||
}; | ||
|
||
const hash = messageHash(pubSubTopic, message); | ||
|
||
expect(bytesToHex(hash)).to.equal(expectedHash); | ||
}); | ||
|
||
it("Waku message hash computation (payload length 0)", () => { | ||
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"), | ||
ephemeral: undefined, | ||
rateLimitProof: undefined, | ||
timestamp: undefined, | ||
version: undefined, | ||
}; | ||
|
||
const hash = messageHash(pubSubTopic, message); | ||
|
||
expect(bytesToHex(hash)).to.equal(expectedHash); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { sha256 } from "@noble/hashes/sha256"; | ||
import type { IProtoMessage } from "@waku/interfaces"; | ||
import { concat, utf8ToBytes } from "@waku/utils/bytes"; | ||
|
||
/** | ||
* Deterministic Message Hashing as defined in | ||
* [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/#deterministic-message-hashing) | ||
*/ | ||
export function messageHash( | ||
pubsubTopic: string, | ||
message: IProtoMessage | ||
): Uint8Array { | ||
const pubsubTopicBytes = utf8ToBytes(pubsubTopic); | ||
const contentTopicBytes = utf8ToBytes(message.contentTopic); | ||
|
||
let bytesToHash; | ||
|
||
if (message.meta) { | ||
bytesToHash = concat([ | ||
pubsubTopicBytes, | ||
message.payload, | ||
contentTopicBytes, | ||
message.meta, | ||
]); | ||
} else { | ||
bytesToHash = concat([ | ||
pubsubTopicBytes, | ||
message.payload, | ||
contentTopicBytes, | ||
]); | ||
} | ||
return sha256(bytesToHash); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters