From 8922511cc9fb181d007a5ffeddccd365090bb2cb Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Mon, 25 Jul 2022 20:28:52 +1000 Subject: [PATCH] fix: pass pubsub topic when creating waku filter --- src/lib/waku.ts | 3 +-- src/lib/waku_filter/index.ts | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/lib/waku.ts b/src/lib/waku.ts index 08c4d20e4d..2e8e21d558 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -104,8 +104,7 @@ export async function createWaku(options?: CreateOptions): Promise { const wakuStore = new WakuStore(libp2p, options); const wakuLightPush = new WakuLightPush(libp2p, options); - // TODO pass options - const wakuFilter = new WakuFilter(libp2p); + const wakuFilter = new WakuFilter(libp2p, options); await libp2p.start(); diff --git a/src/lib/waku_filter/index.ts b/src/lib/waku_filter/index.ts index c577f5d5b1..8150733c2a 100644 --- a/src/lib/waku_filter/index.ts +++ b/src/lib/waku_filter/index.ts @@ -20,6 +20,18 @@ export const FilterCodec = "/vac/waku/filter/2.0.0-beta1"; const log = debug("waku:filter"); +export interface CreateOptions { + /** + * The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}. + * + * The usage of the default pubsub topic is recommended. + * See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details. + * + * @default {@link DefaultPubSubTopic} + */ + pubSubTopic?: string; +} + type FilterSubscriptionOpts = { /** * The Pubsub topic for the subscription @@ -43,16 +55,17 @@ type UnsubscribeFunction = () => Promise; * - https://github.com/status-im/nwaku/issues/948 */ export class WakuFilter { + pubSubTopic: string; private subscriptions: Map; public decryptionKeys: Map< Uint8Array, { method?: DecryptionMethod; contentTopics?: string[] } >; - // TODO: Accept options (pubsubtopic) - constructor(public libp2p: Libp2p) { + constructor(public libp2p: Libp2p, options?: CreateOptions) { this.subscriptions = new Map(); this.decryptionKeys = new Map(); + this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic; this.libp2p .handle(FilterCodec, this.onRequest.bind(this)) .catch((e) => log("Failed to register filter protocol", e)); @@ -69,7 +82,7 @@ export class WakuFilter { contentTopics: string[], opts?: FilterSubscriptionOpts ): Promise { - const topic = opts?.pubsubTopic || DefaultPubSubTopic; + const topic = opts?.pubsubTopic ?? this.pubSubTopic; const contentFilters = contentTopics.map((contentTopic) => ({ contentTopic, }));