From 497588bc360b4a77fb8af5081234b83c5eb269d4 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Mon, 13 Mar 2023 14:15:57 +1100 Subject: [PATCH] chore: make topics arbitrary data --- .../core/src/lib/message/version_0.spec.ts | 77 ++++++++++--------- packages/message-encryption/src/ecies.spec.ts | 43 ++++++----- .../message-encryption/src/symmetric.spec.ts | 37 ++++----- 3 files changed, 80 insertions(+), 77 deletions(-) diff --git a/packages/core/src/lib/message/version_0.spec.ts b/packages/core/src/lib/message/version_0.spec.ts index a71fc84f21..14a5e5bc08 100644 --- a/packages/core/src/lib/message/version_0.spec.ts +++ b/packages/core/src/lib/message/version_0.spec.ts @@ -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; + } + ) ); }); }); diff --git a/packages/message-encryption/src/ecies.spec.ts b/packages/message-encryption/src/ecies.spec.ts index fb2f969d99..71d68dc87b 100644 --- a/packages/message-encryption/src/ecies.spec.ts +++ b/packages/message-encryption/src/ecies.spec.ts @@ -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; diff --git a/packages/message-encryption/src/symmetric.spec.ts b/packages/message-encryption/src/symmetric.spec.ts index 477848477f..837c88fa10 100644 --- a/packages/message-encryption/src/symmetric.spec.ts +++ b/packages/message-encryption/src/symmetric.spec.ts @@ -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;