Skip to content

Commit

Permalink
simplify encoder/decoder creation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Nov 22, 2023
1 parent 9d3e34b commit 821da15
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 58 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

0 comments on commit 821da15

Please sign in to comment.