mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-23 01:28:16 +00:00
chore: make topics arbitrary data
This commit is contained in:
parent
dbaf89831f
commit
497588bc36
@ -3,51 +3,58 @@ import fc from "fast-check";
|
||||
|
||||
import { createDecoder, createEncoder, DecodedMessage } from "./version_0.js";
|
||||
|
||||
const TestContentTopic = "/test/1/waku-message/utf8";
|
||||
const TestPubSubTopic = "/test/pubsub/topic";
|
||||
|
||||
describe("Waku Message version 0", function () {
|
||||
it("Round trip binary serialization", async function () {
|
||||
await fc.assert(
|
||||
fc.asyncProperty(fc.uint8Array({ minLength: 1 }), async (payload) => {
|
||||
const encoder = createEncoder({
|
||||
contentTopic: TestContentTopic,
|
||||
});
|
||||
const bytes = await encoder.toWire({ payload });
|
||||
const decoder = createDecoder(TestContentTopic);
|
||||
const protoResult = await decoder.fromWireToProtoObj(bytes);
|
||||
const result = (await decoder.fromProtoObj(
|
||||
TestPubSubTopic,
|
||||
protoResult!
|
||||
)) as DecodedMessage;
|
||||
fc.asyncProperty(
|
||||
fc.string(),
|
||||
fc.string(),
|
||||
fc.uint8Array({ minLength: 1 }),
|
||||
async (contentTopic, pubSubTopic, payload) => {
|
||||
const encoder = createEncoder({
|
||||
contentTopic,
|
||||
});
|
||||
const bytes = await encoder.toWire({ payload });
|
||||
const decoder = createDecoder(contentTopic);
|
||||
const protoResult = await decoder.fromWireToProtoObj(bytes);
|
||||
const result = (await decoder.fromProtoObj(
|
||||
pubSubTopic,
|
||||
protoResult!
|
||||
)) as DecodedMessage;
|
||||
|
||||
expect(result.contentTopic).to.eq(TestContentTopic);
|
||||
expect(result.pubSubTopic).to.eq(TestPubSubTopic);
|
||||
expect(result.version).to.eq(0);
|
||||
expect(result.ephemeral).to.be.false;
|
||||
expect(result.payload).to.deep.eq(payload);
|
||||
expect(result.timestamp).to.not.be.undefined;
|
||||
})
|
||||
expect(result.contentTopic).to.eq(contentTopic);
|
||||
expect(result.pubSubTopic).to.eq(pubSubTopic);
|
||||
expect(result.version).to.eq(0);
|
||||
expect(result.ephemeral).to.be.false;
|
||||
expect(result.payload).to.deep.eq(payload);
|
||||
expect(result.timestamp).to.not.be.undefined;
|
||||
}
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it("Ephemeral field set to true", async function () {
|
||||
await fc.assert(
|
||||
fc.asyncProperty(fc.uint8Array({ minLength: 1 }), async (payload) => {
|
||||
const encoder = createEncoder({
|
||||
contentTopic: TestContentTopic,
|
||||
ephemeral: true,
|
||||
});
|
||||
const bytes = await encoder.toWire({ payload });
|
||||
const decoder = createDecoder(TestContentTopic);
|
||||
const protoResult = await decoder.fromWireToProtoObj(bytes);
|
||||
const result = (await decoder.fromProtoObj(
|
||||
TestPubSubTopic,
|
||||
protoResult!
|
||||
)) as DecodedMessage;
|
||||
fc.asyncProperty(
|
||||
fc.string(),
|
||||
fc.string(),
|
||||
fc.uint8Array({ minLength: 1 }),
|
||||
async (contentTopic, pubSubTopic, payload) => {
|
||||
const encoder = createEncoder({
|
||||
contentTopic,
|
||||
ephemeral: true,
|
||||
});
|
||||
const bytes = await encoder.toWire({ payload });
|
||||
const decoder = createDecoder(contentTopic);
|
||||
const protoResult = await decoder.fromWireToProtoObj(bytes);
|
||||
const result = (await decoder.fromProtoObj(
|
||||
pubSubTopic,
|
||||
protoResult!
|
||||
)) as DecodedMessage;
|
||||
|
||||
expect(result.ephemeral).to.be.true;
|
||||
})
|
||||
expect(result.ephemeral).to.be.true;
|
||||
}
|
||||
)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
@ -4,35 +4,31 @@ import fc from "fast-check";
|
||||
import { getPublicKey } from "./crypto/index.js";
|
||||
import { createDecoder, createEncoder } from "./ecies.js";
|
||||
|
||||
const TestContentTopic = "/test/1/waku-message/utf8";
|
||||
const TestPubSubTopic = "/test/pubsub/topic";
|
||||
|
||||
describe("Ecies Encryption", function () {
|
||||
it("Round trip binary encryption [ecies, no signature]", async function () {
|
||||
await fc.assert(
|
||||
fc.asyncProperty(
|
||||
fc.string(),
|
||||
fc.string(),
|
||||
fc.uint8Array({ minLength: 1 }),
|
||||
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
||||
async (payload, privateKey) => {
|
||||
async (pubSubTopic, contentTopic, payload, privateKey) => {
|
||||
const publicKey = getPublicKey(privateKey);
|
||||
|
||||
const encoder = createEncoder({
|
||||
contentTopic: TestContentTopic,
|
||||
contentTopic,
|
||||
publicKey,
|
||||
});
|
||||
const bytes = await encoder.toWire({ payload });
|
||||
|
||||
const decoder = createDecoder(TestContentTopic, privateKey);
|
||||
const decoder = createDecoder(contentTopic, privateKey);
|
||||
const protoResult = await decoder.fromWireToProtoObj(bytes!);
|
||||
if (!protoResult) throw "Failed to proto decode";
|
||||
const result = await decoder.fromProtoObj(
|
||||
TestPubSubTopic,
|
||||
protoResult
|
||||
);
|
||||
const result = await decoder.fromProtoObj(pubSubTopic, protoResult);
|
||||
if (!result) throw "Failed to decode";
|
||||
|
||||
expect(result.contentTopic).to.equal(TestContentTopic);
|
||||
expect(result.pubSubTopic).to.equal(TestPubSubTopic);
|
||||
expect(result.contentTopic).to.equal(contentTopic);
|
||||
expect(result.pubSubTopic).to.equal(pubSubTopic);
|
||||
expect(result.version).to.equal(1);
|
||||
expect(result?.payload).to.deep.equal(payload);
|
||||
expect(result.signature).to.be.undefined;
|
||||
@ -47,31 +43,36 @@ describe("Ecies Encryption", function () {
|
||||
|
||||
await fc.assert(
|
||||
fc.asyncProperty(
|
||||
fc.string(),
|
||||
fc.string(),
|
||||
fc.uint8Array({ minLength: 1 }),
|
||||
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
||||
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
||||
async (payload, alicePrivateKey, bobPrivateKey) => {
|
||||
async (
|
||||
pubSubTopic,
|
||||
contentTopic,
|
||||
payload,
|
||||
alicePrivateKey,
|
||||
bobPrivateKey
|
||||
) => {
|
||||
const alicePublicKey = getPublicKey(alicePrivateKey);
|
||||
const bobPublicKey = getPublicKey(bobPrivateKey);
|
||||
|
||||
const encoder = createEncoder({
|
||||
contentTopic: TestContentTopic,
|
||||
contentTopic,
|
||||
publicKey: bobPublicKey,
|
||||
sigPrivKey: alicePrivateKey,
|
||||
});
|
||||
const bytes = await encoder.toWire({ payload });
|
||||
|
||||
const decoder = createDecoder(TestContentTopic, bobPrivateKey);
|
||||
const decoder = createDecoder(contentTopic, bobPrivateKey);
|
||||
const protoResult = await decoder.fromWireToProtoObj(bytes!);
|
||||
if (!protoResult) throw "Failed to proto decode";
|
||||
const result = await decoder.fromProtoObj(
|
||||
TestPubSubTopic,
|
||||
protoResult
|
||||
);
|
||||
const result = await decoder.fromProtoObj(pubSubTopic, protoResult);
|
||||
if (!result) throw "Failed to decode";
|
||||
|
||||
expect(result.contentTopic).to.equal(TestContentTopic);
|
||||
expect(result.pubSubTopic).to.equal(TestPubSubTopic);
|
||||
expect(result.contentTopic).to.equal(contentTopic);
|
||||
expect(result.pubSubTopic).to.equal(pubSubTopic);
|
||||
expect(result.version).to.equal(1);
|
||||
expect(result?.payload).to.deep.equal(payload);
|
||||
expect(result.signature).to.not.be.undefined;
|
||||
|
@ -4,33 +4,29 @@ import fc from "fast-check";
|
||||
import { getPublicKey } from "./crypto/index.js";
|
||||
import { createDecoder, createEncoder } from "./symmetric.js";
|
||||
|
||||
const TestContentTopic = "/test/1/waku-message/utf8";
|
||||
const TestPubSubTopic = "/test/pubsub/topic";
|
||||
|
||||
describe("Symmetric Encryption", function () {
|
||||
it("Round trip binary encryption [symmetric, no signature]", async function () {
|
||||
await fc.assert(
|
||||
fc.asyncProperty(
|
||||
fc.string(),
|
||||
fc.string(),
|
||||
fc.uint8Array({ minLength: 1 }),
|
||||
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
||||
async (payload, symKey) => {
|
||||
async (pubSubTopic, contentTopic, payload, symKey) => {
|
||||
const encoder = createEncoder({
|
||||
contentTopic: TestContentTopic,
|
||||
contentTopic,
|
||||
symKey,
|
||||
});
|
||||
const bytes = await encoder.toWire({ payload });
|
||||
|
||||
const decoder = createDecoder(TestContentTopic, symKey);
|
||||
const decoder = createDecoder(contentTopic, symKey);
|
||||
const protoResult = await decoder.fromWireToProtoObj(bytes!);
|
||||
if (!protoResult) throw "Failed to proto decode";
|
||||
const result = await decoder.fromProtoObj(
|
||||
TestPubSubTopic,
|
||||
protoResult
|
||||
);
|
||||
const result = await decoder.fromProtoObj(pubSubTopic, protoResult);
|
||||
if (!result) throw "Failed to decode";
|
||||
|
||||
expect(result.contentTopic).to.equal(TestContentTopic);
|
||||
expect(result.pubSubTopic).to.equal(TestPubSubTopic);
|
||||
expect(result.contentTopic).to.equal(contentTopic);
|
||||
expect(result.pubSubTopic).to.equal(pubSubTopic);
|
||||
expect(result.version).to.equal(1);
|
||||
expect(result?.payload).to.deep.equal(payload);
|
||||
expect(result.signature).to.be.undefined;
|
||||
@ -43,30 +39,29 @@ describe("Symmetric Encryption", function () {
|
||||
it("Round trip binary encryption [symmetric, signature]", async function () {
|
||||
await fc.assert(
|
||||
fc.asyncProperty(
|
||||
fc.string(),
|
||||
fc.string(),
|
||||
fc.uint8Array({ minLength: 1 }),
|
||||
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
||||
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
||||
async (payload, sigPrivKey, symKey) => {
|
||||
async (pubSubTopic, contentTopic, payload, sigPrivKey, symKey) => {
|
||||
const sigPubKey = getPublicKey(sigPrivKey);
|
||||
|
||||
const encoder = createEncoder({
|
||||
contentTopic: TestContentTopic,
|
||||
contentTopic,
|
||||
symKey,
|
||||
sigPrivKey,
|
||||
});
|
||||
const bytes = await encoder.toWire({ payload });
|
||||
|
||||
const decoder = createDecoder(TestContentTopic, symKey);
|
||||
const decoder = createDecoder(contentTopic, symKey);
|
||||
const protoResult = await decoder.fromWireToProtoObj(bytes!);
|
||||
if (!protoResult) throw "Failed to proto decode";
|
||||
const result = await decoder.fromProtoObj(
|
||||
TestPubSubTopic,
|
||||
protoResult
|
||||
);
|
||||
const result = await decoder.fromProtoObj(pubSubTopic, protoResult);
|
||||
if (!result) throw "Failed to decode";
|
||||
|
||||
expect(result.contentTopic).to.equal(TestContentTopic);
|
||||
expect(result.pubSubTopic).to.equal(TestPubSubTopic);
|
||||
expect(result.contentTopic).to.equal(contentTopic);
|
||||
expect(result.pubSubTopic).to.equal(pubSubTopic);
|
||||
expect(result.version).to.equal(1);
|
||||
expect(result?.payload).to.deep.equal(payload);
|
||||
expect(result.signature).to.not.be.undefined;
|
||||
|
Loading…
x
Reference in New Issue
Block a user