mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-02-09 18:55:25 +00:00
* chore: upgrade libp2p to v2 and related deps * chore: fix ENR * chore(core): remove CustomEvent polyfill import * chore: `peer-id-factory` has been removed * chore(discovery): fix local-cache & remove CustomEvent imports * chore(sdk): update config * chore(tests): update tests without peer-id-factory * fix: spec tests * chore: fix test * chore: upgrade dataset-core * chore: upgrade libp2p and stale references * chore: upgrade playwright * chore: rm console log * fix: lock
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { generateKeyPair } from "@libp2p/crypto/keys";
|
|
import { TopicValidatorResult } from "@libp2p/interface";
|
|
import type { UnsignedMessage } from "@libp2p/interface";
|
|
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
|
|
import { createEncoder } from "@waku/core";
|
|
import { determinePubsubTopic } from "@waku/utils";
|
|
import { expect } from "chai";
|
|
import fc from "fast-check";
|
|
|
|
import { messageValidator } from "./message_validator.js";
|
|
|
|
const TestContentTopic = "/app/1/topic/utf8";
|
|
const TestPubsubTopic = determinePubsubTopic(TestContentTopic);
|
|
|
|
describe("Message Validator", () => {
|
|
it("Accepts a valid Waku Message", async () => {
|
|
await fc.assert(
|
|
fc.asyncProperty(fc.uint8Array({ minLength: 1 }), async (payload) => {
|
|
const privateKey = await generateKeyPair("secp256k1");
|
|
const peerId = peerIdFromPrivateKey(privateKey);
|
|
|
|
const encoder = createEncoder({
|
|
contentTopic: TestContentTopic,
|
|
pubsubTopic: TestPubsubTopic
|
|
});
|
|
const bytes = await encoder.toWire({ payload });
|
|
|
|
const message: UnsignedMessage = {
|
|
type: "unsigned",
|
|
topic: TestPubsubTopic,
|
|
data: bytes
|
|
};
|
|
|
|
const result = messageValidator(peerId, message);
|
|
|
|
expect(result).to.eq(TopicValidatorResult.Accept);
|
|
})
|
|
);
|
|
});
|
|
|
|
it("Rejects garbage", async () => {
|
|
await fc.assert(
|
|
fc.asyncProperty(fc.uint8Array(), async (data) => {
|
|
const peerId =
|
|
await generateKeyPair("secp256k1").then(peerIdFromPrivateKey);
|
|
|
|
const message: UnsignedMessage = {
|
|
type: "unsigned",
|
|
topic: TestPubsubTopic,
|
|
data
|
|
};
|
|
|
|
const result = messageValidator(peerId, message);
|
|
|
|
expect(result).to.eq(TopicValidatorResult.Reject);
|
|
})
|
|
);
|
|
});
|
|
});
|