chore: make topics arbitrary data

This commit is contained in:
fryorcraken.eth 2023-03-13 14:15:57 +11:00
parent dbaf89831f
commit 497588bc36
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 80 additions and 77 deletions

View File

@ -3,51 +3,58 @@ import fc from "fast-check";
import { createDecoder, createEncoder, DecodedMessage } from "./version_0.js"; 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 () { describe("Waku Message version 0", function () {
it("Round trip binary serialization", async function () { it("Round trip binary serialization", async function () {
await fc.assert( await fc.assert(
fc.asyncProperty(fc.uint8Array({ minLength: 1 }), async (payload) => { fc.asyncProperty(
const encoder = createEncoder({ fc.string(),
contentTopic: TestContentTopic, fc.string(),
}); fc.uint8Array({ minLength: 1 }),
const bytes = await encoder.toWire({ payload }); async (contentTopic, pubSubTopic, payload) => {
const decoder = createDecoder(TestContentTopic); const encoder = createEncoder({
const protoResult = await decoder.fromWireToProtoObj(bytes); contentTopic,
const result = (await decoder.fromProtoObj( });
TestPubSubTopic, const bytes = await encoder.toWire({ payload });
protoResult! const decoder = createDecoder(contentTopic);
)) as DecodedMessage; const protoResult = await decoder.fromWireToProtoObj(bytes);
const result = (await decoder.fromProtoObj(
pubSubTopic,
protoResult!
)) as DecodedMessage;
expect(result.contentTopic).to.eq(TestContentTopic); expect(result.contentTopic).to.eq(contentTopic);
expect(result.pubSubTopic).to.eq(TestPubSubTopic); expect(result.pubSubTopic).to.eq(pubSubTopic);
expect(result.version).to.eq(0); expect(result.version).to.eq(0);
expect(result.ephemeral).to.be.false; expect(result.ephemeral).to.be.false;
expect(result.payload).to.deep.eq(payload); expect(result.payload).to.deep.eq(payload);
expect(result.timestamp).to.not.be.undefined; expect(result.timestamp).to.not.be.undefined;
}) }
)
); );
}); });
it("Ephemeral field set to true", async function () { it("Ephemeral field set to true", async function () {
await fc.assert( await fc.assert(
fc.asyncProperty(fc.uint8Array({ minLength: 1 }), async (payload) => { fc.asyncProperty(
const encoder = createEncoder({ fc.string(),
contentTopic: TestContentTopic, fc.string(),
ephemeral: true, fc.uint8Array({ minLength: 1 }),
}); async (contentTopic, pubSubTopic, payload) => {
const bytes = await encoder.toWire({ payload }); const encoder = createEncoder({
const decoder = createDecoder(TestContentTopic); contentTopic,
const protoResult = await decoder.fromWireToProtoObj(bytes); ephemeral: true,
const result = (await decoder.fromProtoObj( });
TestPubSubTopic, const bytes = await encoder.toWire({ payload });
protoResult! const decoder = createDecoder(contentTopic);
)) as DecodedMessage; 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;
}) }
)
); );
}); });
}); });

View File

@ -4,35 +4,31 @@ import fc from "fast-check";
import { getPublicKey } from "./crypto/index.js"; import { getPublicKey } from "./crypto/index.js";
import { createDecoder, createEncoder } from "./ecies.js"; import { createDecoder, createEncoder } from "./ecies.js";
const TestContentTopic = "/test/1/waku-message/utf8";
const TestPubSubTopic = "/test/pubsub/topic";
describe("Ecies Encryption", function () { describe("Ecies Encryption", function () {
it("Round trip binary encryption [ecies, no signature]", async function () { it("Round trip binary encryption [ecies, no signature]", async function () {
await fc.assert( await fc.assert(
fc.asyncProperty( fc.asyncProperty(
fc.string(),
fc.string(),
fc.uint8Array({ minLength: 1 }), fc.uint8Array({ minLength: 1 }),
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }), fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
async (payload, privateKey) => { async (pubSubTopic, contentTopic, payload, privateKey) => {
const publicKey = getPublicKey(privateKey); const publicKey = getPublicKey(privateKey);
const encoder = createEncoder({ const encoder = createEncoder({
contentTopic: TestContentTopic, contentTopic,
publicKey, publicKey,
}); });
const bytes = await encoder.toWire({ payload }); const bytes = await encoder.toWire({ payload });
const decoder = createDecoder(TestContentTopic, privateKey); const decoder = createDecoder(contentTopic, privateKey);
const protoResult = await decoder.fromWireToProtoObj(bytes!); const protoResult = await decoder.fromWireToProtoObj(bytes!);
if (!protoResult) throw "Failed to proto decode"; if (!protoResult) throw "Failed to proto decode";
const result = await decoder.fromProtoObj( const result = await decoder.fromProtoObj(pubSubTopic, protoResult);
TestPubSubTopic,
protoResult
);
if (!result) throw "Failed to decode"; if (!result) throw "Failed to decode";
expect(result.contentTopic).to.equal(TestContentTopic); expect(result.contentTopic).to.equal(contentTopic);
expect(result.pubSubTopic).to.equal(TestPubSubTopic); expect(result.pubSubTopic).to.equal(pubSubTopic);
expect(result.version).to.equal(1); expect(result.version).to.equal(1);
expect(result?.payload).to.deep.equal(payload); expect(result?.payload).to.deep.equal(payload);
expect(result.signature).to.be.undefined; expect(result.signature).to.be.undefined;
@ -47,31 +43,36 @@ describe("Ecies Encryption", function () {
await fc.assert( await fc.assert(
fc.asyncProperty( fc.asyncProperty(
fc.string(),
fc.string(),
fc.uint8Array({ minLength: 1 }), fc.uint8Array({ minLength: 1 }),
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }), fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
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 alicePublicKey = getPublicKey(alicePrivateKey);
const bobPublicKey = getPublicKey(bobPrivateKey); const bobPublicKey = getPublicKey(bobPrivateKey);
const encoder = createEncoder({ const encoder = createEncoder({
contentTopic: TestContentTopic, contentTopic,
publicKey: bobPublicKey, publicKey: bobPublicKey,
sigPrivKey: alicePrivateKey, sigPrivKey: alicePrivateKey,
}); });
const bytes = await encoder.toWire({ payload }); const bytes = await encoder.toWire({ payload });
const decoder = createDecoder(TestContentTopic, bobPrivateKey); const decoder = createDecoder(contentTopic, bobPrivateKey);
const protoResult = await decoder.fromWireToProtoObj(bytes!); const protoResult = await decoder.fromWireToProtoObj(bytes!);
if (!protoResult) throw "Failed to proto decode"; if (!protoResult) throw "Failed to proto decode";
const result = await decoder.fromProtoObj( const result = await decoder.fromProtoObj(pubSubTopic, protoResult);
TestPubSubTopic,
protoResult
);
if (!result) throw "Failed to decode"; if (!result) throw "Failed to decode";
expect(result.contentTopic).to.equal(TestContentTopic); expect(result.contentTopic).to.equal(contentTopic);
expect(result.pubSubTopic).to.equal(TestPubSubTopic); expect(result.pubSubTopic).to.equal(pubSubTopic);
expect(result.version).to.equal(1); expect(result.version).to.equal(1);
expect(result?.payload).to.deep.equal(payload); expect(result?.payload).to.deep.equal(payload);
expect(result.signature).to.not.be.undefined; expect(result.signature).to.not.be.undefined;

View File

@ -4,33 +4,29 @@ import fc from "fast-check";
import { getPublicKey } from "./crypto/index.js"; import { getPublicKey } from "./crypto/index.js";
import { createDecoder, createEncoder } from "./symmetric.js"; import { createDecoder, createEncoder } from "./symmetric.js";
const TestContentTopic = "/test/1/waku-message/utf8";
const TestPubSubTopic = "/test/pubsub/topic";
describe("Symmetric Encryption", function () { describe("Symmetric Encryption", function () {
it("Round trip binary encryption [symmetric, no signature]", async function () { it("Round trip binary encryption [symmetric, no signature]", async function () {
await fc.assert( await fc.assert(
fc.asyncProperty( fc.asyncProperty(
fc.string(),
fc.string(),
fc.uint8Array({ minLength: 1 }), fc.uint8Array({ minLength: 1 }),
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }), fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
async (payload, symKey) => { async (pubSubTopic, contentTopic, payload, symKey) => {
const encoder = createEncoder({ const encoder = createEncoder({
contentTopic: TestContentTopic, contentTopic,
symKey, symKey,
}); });
const bytes = await encoder.toWire({ payload }); const bytes = await encoder.toWire({ payload });
const decoder = createDecoder(TestContentTopic, symKey); const decoder = createDecoder(contentTopic, symKey);
const protoResult = await decoder.fromWireToProtoObj(bytes!); const protoResult = await decoder.fromWireToProtoObj(bytes!);
if (!protoResult) throw "Failed to proto decode"; if (!protoResult) throw "Failed to proto decode";
const result = await decoder.fromProtoObj( const result = await decoder.fromProtoObj(pubSubTopic, protoResult);
TestPubSubTopic,
protoResult
);
if (!result) throw "Failed to decode"; if (!result) throw "Failed to decode";
expect(result.contentTopic).to.equal(TestContentTopic); expect(result.contentTopic).to.equal(contentTopic);
expect(result.pubSubTopic).to.equal(TestPubSubTopic); expect(result.pubSubTopic).to.equal(pubSubTopic);
expect(result.version).to.equal(1); expect(result.version).to.equal(1);
expect(result?.payload).to.deep.equal(payload); expect(result?.payload).to.deep.equal(payload);
expect(result.signature).to.be.undefined; expect(result.signature).to.be.undefined;
@ -43,30 +39,29 @@ describe("Symmetric Encryption", function () {
it("Round trip binary encryption [symmetric, signature]", async function () { it("Round trip binary encryption [symmetric, signature]", async function () {
await fc.assert( await fc.assert(
fc.asyncProperty( fc.asyncProperty(
fc.string(),
fc.string(),
fc.uint8Array({ minLength: 1 }), fc.uint8Array({ minLength: 1 }),
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }), fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
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 sigPubKey = getPublicKey(sigPrivKey);
const encoder = createEncoder({ const encoder = createEncoder({
contentTopic: TestContentTopic, contentTopic,
symKey, symKey,
sigPrivKey, sigPrivKey,
}); });
const bytes = await encoder.toWire({ payload }); const bytes = await encoder.toWire({ payload });
const decoder = createDecoder(TestContentTopic, symKey); const decoder = createDecoder(contentTopic, symKey);
const protoResult = await decoder.fromWireToProtoObj(bytes!); const protoResult = await decoder.fromWireToProtoObj(bytes!);
if (!protoResult) throw "Failed to proto decode"; if (!protoResult) throw "Failed to proto decode";
const result = await decoder.fromProtoObj( const result = await decoder.fromProtoObj(pubSubTopic, protoResult);
TestPubSubTopic,
protoResult
);
if (!result) throw "Failed to decode"; if (!result) throw "Failed to decode";
expect(result.contentTopic).to.equal(TestContentTopic); expect(result.contentTopic).to.equal(contentTopic);
expect(result.pubSubTopic).to.equal(TestPubSubTopic); expect(result.pubSubTopic).to.equal(pubSubTopic);
expect(result.version).to.equal(1); expect(result.version).to.equal(1);
expect(result?.payload).to.deep.equal(payload); expect(result?.payload).to.deep.equal(payload);
expect(result.signature).to.not.be.undefined; expect(result.signature).to.not.be.undefined;