Skip to content

Commit

Permalink
simplify encoder/decoder creation logic + update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Nov 22, 2023
1 parent 9d3e34b commit 25964a8
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 112 deletions.
27 changes: 8 additions & 19 deletions packages/core/src/lib/message/version_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,17 @@ export class Encoder implements IEncoder {
* messages.
*/
export function createEncoder({
pubsubTopic = DefaultPubsubTopic,
pubsubTopicShardInfo,
contentTopic,
ephemeral,
metaSetter
}: EncoderOptions): Encoder {
if (typeof pubsubTopic === "string" && pubsubTopic !== DefaultPubsubTopic) {
throw new Error(
`Error: cannot use custom named pubsub topic: ${pubsubTopic}, must be ${DefaultPubsubTopic}`
);
}

return new Encoder(
contentTopic,
ephemeral,
typeof pubsubTopic === "string"
? pubsubTopic
: singleTopicShardInfoToPubsubTopic(pubsubTopic),
pubsubTopicShardInfo?.index
? singleTopicShardInfoToPubsubTopic(pubsubTopicShardInfo)
: DefaultPubsubTopic,
metaSetter
);
}
Expand Down Expand Up @@ -196,17 +190,12 @@ export class Decoder implements IDecoder<DecodedMessage> {
*/
export function createDecoder(
contentTopic: string,
pubsubTopic: SingleTopicShardInfo | PubsubTopic = DefaultPubsubTopic
pubsubTopicShardInfo: SingleTopicShardInfo
): Decoder {
if (typeof pubsubTopic === "string" && pubsubTopic !== DefaultPubsubTopic) {
throw new Error(
`Error: cannot use custom named pubsub topic: ${pubsubTopic}, must be ${DefaultPubsubTopic}`
);
}
return new Decoder(
typeof pubsubTopic === "string"
? pubsubTopic
: singleTopicShardInfoToPubsubTopic(pubsubTopic),
pubsubTopicShardInfo?.index
? singleTopicShardInfoToPubsubTopic(pubsubTopicShardInfo)
: DefaultPubsubTopic,
contentTopic
);
}
28 changes: 8 additions & 20 deletions packages/message-encryption/src/ecies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,17 @@ export interface EncoderOptions extends BaseEncoderOptions {
* in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
*/
export function createEncoder({
pubsubTopic = DefaultPubsubTopic,
pubsubTopicShardInfo,
contentTopic,
publicKey,
sigPrivKey,
ephemeral = false,
metaSetter
}: EncoderOptions): Encoder {
if (typeof pubsubTopic === "string" && pubsubTopic !== DefaultPubsubTopic) {
throw new Error(
`Error: cannot use custom named pubsub topic: ${pubsubTopic}, must be ${DefaultPubsubTopic}`
);
}

return new Encoder(
typeof pubsubTopic === "string"
? pubsubTopic
: singleTopicShardInfoToPubsubTopic(pubsubTopic),
pubsubTopicShardInfo?.index
? singleTopicShardInfoToPubsubTopic(pubsubTopicShardInfo)
: DefaultPubsubTopic,
contentTopic,
publicKey,
sigPrivKey,
Expand Down Expand Up @@ -202,18 +196,12 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
export function createDecoder(
contentTopic: string,
privateKey: Uint8Array,
pubsubTopic: SingleTopicShardInfo | PubsubTopic = DefaultPubsubTopic
pubsubTopicShardInfo?: SingleTopicShardInfo
): Decoder {
if (typeof pubsubTopic === "string" && pubsubTopic !== DefaultPubsubTopic) {
throw new Error(
`Error: cannot use custom named pubsub topic: ${pubsubTopic}, must be ${DefaultPubsubTopic}`
);
}

return new Decoder(
typeof pubsubTopic === "string"
? pubsubTopic
: singleTopicShardInfoToPubsubTopic(pubsubTopic),
pubsubTopicShardInfo?.index
? singleTopicShardInfoToPubsubTopic(pubsubTopicShardInfo)
: DefaultPubsubTopic,
contentTopic,
privateKey
);
Expand Down
27 changes: 8 additions & 19 deletions packages/message-encryption/src/symmetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,17 @@ export interface EncoderOptions extends BaseEncoderOptions {
* in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
*/
export function createEncoder({
pubsubTopic = DefaultPubsubTopic,
pubsubTopicShardInfo,
contentTopic,
symKey,
sigPrivKey,
ephemeral = false,
metaSetter
}: EncoderOptions): Encoder {
if (typeof pubsubTopic === "string" && pubsubTopic !== DefaultPubsubTopic) {
throw new Error(
`Error: cannot use custom named pubsub topic: ${pubsubTopic}, must be ${DefaultPubsubTopic}`
);
}
return new Encoder(
typeof pubsubTopic === "string"
? pubsubTopic
: singleTopicShardInfoToPubsubTopic(pubsubTopic),
pubsubTopicShardInfo?.index
? singleTopicShardInfoToPubsubTopic(pubsubTopicShardInfo)
: DefaultPubsubTopic,
contentTopic,
symKey,
sigPrivKey,
Expand Down Expand Up @@ -197,18 +192,12 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
export function createDecoder(
contentTopic: string,
symKey: Uint8Array,
pubsubTopic: SingleTopicShardInfo | PubsubTopic = DefaultPubsubTopic
pubsubTopicShardInfo?: SingleTopicShardInfo
): Decoder {
if (typeof pubsubTopic === "string" && pubsubTopic !== DefaultPubsubTopic) {
throw new Error(
`Error: cannot use custom named pubsub topic: ${pubsubTopic}, must be ${DefaultPubsubTopic}`
);
}

return new Decoder(
typeof pubsubTopic === "string"
? pubsubTopic
: singleTopicShardInfoToPubsubTopic(pubsubTopic),
pubsubTopicShardInfo?.index
? singleTopicShardInfoToPubsubTopic(pubsubTopicShardInfo)
: DefaultPubsubTopic,
contentTopic,
symKey
);
Expand Down
4 changes: 2 additions & 2 deletions packages/tests/tests/filter/multiple_pubsub.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () {
const customContentTopic1 = "/test/2/waku-filter";
const customContentTopic2 = "/test/3/waku-filter";
const customEncoder1 = createEncoder({
pubsubTopic: singleTopicShardInfo1,
pubsubTopicShardInfo: singleTopicShardInfo1,
contentTopic: customContentTopic1
});
const customDecoder1 = createDecoder(
customContentTopic1,
singleTopicShardInfo1
);
const customEncoder2 = createEncoder({
pubsubTopic: singleTopicShardInfo2,
pubsubTopicShardInfo: singleTopicShardInfo2,
contentTopic: customContentTopic2
});
const customDecoder2 = createDecoder(
Expand Down
4 changes: 2 additions & 2 deletions packages/tests/tests/light-push/multiple_pubsub.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ describe("Waku Light Push : Multiple PubsubTopics", function () {
const customContentTopic1 = "/test/2/waku-light-push/utf8";
const customContentTopic2 = "/test/3/waku-light-push/utf8";
const customEncoder1 = createEncoder({
pubsubTopic: singleTopicShardInfo1,
pubsubTopicShardInfo: singleTopicShardInfo1,
contentTopic: customContentTopic1
});
const customEncoder2 = createEncoder({
pubsubTopic: singleTopicShardInfo2,
pubsubTopicShardInfo: singleTopicShardInfo2,
contentTopic: customContentTopic2
});

Expand Down
4 changes: 2 additions & 2 deletions packages/tests/tests/relay/multiple_pubsub.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ describe("Waku Relay, multiple pubsub topics", function () {
index: 2
};
const customEncoder1 = createEncoder({
pubsubTopic: singleTopicShardInfo1,
pubsubTopicShardInfo: singleTopicShardInfo1,
contentTopic: customContentTopic1
});
const customDecoder1 = createDecoder(
customContentTopic1,
singleTopicShardInfo1
);
const customEncoder2 = createEncoder({
pubsubTopic: singleTopicShardInfo2,
pubsubTopicShardInfo: singleTopicShardInfo2,
contentTopic: customContentTopic2
});
const customDecoder2 = createDecoder(
Expand Down
2 changes: 1 addition & 1 deletion packages/tests/tests/relay/publish.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe("Waku Relay, Publish", function () {

it("Fails to publish message with wrong pubsubtopic", async function () {
const wrong_encoder = createEncoder({
pubsubTopic: { cluster: 3, index: 1 },
pubsubTopicShardInfo: { cluster: 3, index: 1 },
contentTopic: TestContentTopic
});
const pushResponse = await waku1.relay.send(wrong_encoder, {
Expand Down
43 changes: 0 additions & 43 deletions packages/tests/tests/sharding/error_handling.node.spec.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/tests/tests/sharding/running_nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ describe("Static Sharding: Running Nodes", () => {

const encoder1 = createEncoder({
contentTopic: ContentTopic,
pubsubTopic: singleTopicShardInfo1
pubsubTopicShardInfo: singleTopicShardInfo1
});

const encoder2 = createEncoder({
contentTopic: ContentTopic,
pubsubTopic: singleTopicShardInfo2
pubsubTopicShardInfo: singleTopicShardInfo2
});

const request1 = await waku.lightPush.send(encoder1, {
Expand All @@ -66,7 +66,7 @@ describe("Static Sharding: Running Nodes", () => {
// use a pubsub topic that is not configured
const encoder = createEncoder({
contentTopic: ContentTopic,
pubsubTopic: singleTopicShardInfo2
pubsubTopicShardInfo: singleTopicShardInfo2
});

try {
Expand Down
2 changes: 1 addition & 1 deletion packages/tests/tests/store/index.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
totalMsgs
} from "./utils.js";

const secondDecoder = createDecoder(customContentTopic1, DefaultPubsubTopic);
const secondDecoder = createDecoder(customContentTopic1);

describe("Waku Store, general", function () {
this.timeout(15000);
Expand Down

0 comments on commit 25964a8

Please sign in to comment.