|
| 1 | +import type { IProtoMessage } from "@waku/interfaces"; |
| 2 | +import { bytesToHex, hexToBytes } from "@waku/utils/bytes"; |
| 3 | +import { expect } from "chai"; |
| 4 | + |
| 5 | +import { messageHash } from "./index.js"; |
| 6 | + |
| 7 | +// https://rfc.vac.dev/spec/14/#test-vectors |
| 8 | +describe("RFC Test Vectors", () => { |
| 9 | + it("Waku message hash computation", () => { |
| 10 | + const expectedHash = |
| 11 | + "4fdde1099c9f77f6dae8147b6b3179aba1fc8e14a7bf35203fc253ee479f135f"; |
| 12 | + |
| 13 | + const pubSubTopic = "/waku/2/default-waku/proto"; |
| 14 | + const message: IProtoMessage = { |
| 15 | + payload: hexToBytes("0x010203045445535405060708"), |
| 16 | + contentTopic: "/waku/2/default-content/proto", |
| 17 | + meta: hexToBytes("0x73757065722d736563726574"), |
| 18 | + ephemeral: undefined, |
| 19 | + rateLimitProof: undefined, |
| 20 | + timestamp: undefined, |
| 21 | + version: undefined, |
| 22 | + }; |
| 23 | + |
| 24 | + const hash = messageHash(pubSubTopic, message); |
| 25 | + |
| 26 | + expect(bytesToHex(hash)).to.equal(expectedHash); |
| 27 | + }); |
| 28 | + |
| 29 | + it("Waku message hash computation (meta attribute not present)", () => { |
| 30 | + const expectedHash = |
| 31 | + "87619d05e563521d9126749b45bd4cc2430df0607e77e23572d874ed9c1aaa62"; |
| 32 | + |
| 33 | + const pubSubTopic = "/waku/2/default-waku/proto"; |
| 34 | + const message: IProtoMessage = { |
| 35 | + payload: hexToBytes("0x010203045445535405060708"), |
| 36 | + contentTopic: "/waku/2/default-content/proto", |
| 37 | + meta: undefined, |
| 38 | + ephemeral: undefined, |
| 39 | + rateLimitProof: undefined, |
| 40 | + timestamp: undefined, |
| 41 | + version: undefined, |
| 42 | + }; |
| 43 | + |
| 44 | + const hash = messageHash(pubSubTopic, message); |
| 45 | + |
| 46 | + expect(bytesToHex(hash)).to.equal(expectedHash); |
| 47 | + }); |
| 48 | + |
| 49 | + it("Waku message hash computation (payload length 0)", () => { |
| 50 | + const expectedHash = |
| 51 | + "e1a9596237dbe2cc8aaf4b838c46a7052df6bc0d42ba214b998a8bfdbe8487d6"; |
| 52 | + |
| 53 | + const pubSubTopic = "/waku/2/default-waku/proto"; |
| 54 | + const message: IProtoMessage = { |
| 55 | + payload: new Uint8Array(), |
| 56 | + contentTopic: "/waku/2/default-content/proto", |
| 57 | + meta: hexToBytes("0x73757065722d736563726574"), |
| 58 | + ephemeral: undefined, |
| 59 | + rateLimitProof: undefined, |
| 60 | + timestamp: undefined, |
| 61 | + version: undefined, |
| 62 | + }; |
| 63 | + |
| 64 | + const hash = messageHash(pubSubTopic, message); |
| 65 | + |
| 66 | + expect(bytesToHex(hash)).to.equal(expectedHash); |
| 67 | + }); |
| 68 | +}); |
0 commit comments