From 69aee6c1cc2a89c71b1c66ff2256fed1458f743c Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Mon, 1 Aug 2022 15:43:43 +1000 Subject: [PATCH] refactor: move `createWaku` to own module --- src/index.ts | 3 +- src/lib/create_waku.ts | 80 ++++++++++++++++++++++ src/lib/enr/enr.node.spec.ts | 3 +- src/lib/wait_for_remote_peer.node.spec.ts | 3 +- src/lib/waku.node.spec.ts | 3 +- src/lib/waku.spec.ts | 3 +- src/lib/waku.ts | 77 ++------------------- src/lib/waku_filter/index.node.spec.ts | 3 +- src/lib/waku_light_push/index.node.spec.ts | 3 +- src/lib/waku_message/index.node.spec.ts | 3 +- src/lib/waku_relay/index.node.spec.ts | 3 +- src/lib/waku_relay/index.ts | 19 ++++- src/lib/waku_store/index.node.spec.ts | 3 +- 13 files changed, 123 insertions(+), 83 deletions(-) create mode 100644 src/lib/create_waku.ts diff --git a/src/index.ts b/src/index.ts index 58d36d02a7..acbc51b3b1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,7 +18,7 @@ export { waitForRemotePeer } from "./lib/wait_for_remote_peer"; export * as proto_message from "./proto/message"; export * as waku from "./lib/waku"; -export { createWaku, Waku, Protocols } from "./lib/waku"; +export { Waku, Protocols } from "./lib/waku"; export * as waku_message from "./lib/waku_message"; export { WakuMessage } from "./lib/waku_message"; @@ -38,3 +38,4 @@ export { WakuRelay } from "./lib/waku_relay"; export * as waku_store from "./lib/waku_store"; export { PageDirection, WakuStore, StoreCodecs } from "./lib/waku_store"; +export { createWaku } from "./lib/create_waku"; diff --git a/src/lib/create_waku.ts b/src/lib/create_waku.ts new file mode 100644 index 0000000000..36bf6132c5 --- /dev/null +++ b/src/lib/create_waku.ts @@ -0,0 +1,80 @@ +import { Noise } from "@chainsafe/libp2p-noise"; +import { Mplex } from "@libp2p/mplex"; +import { WebSockets } from "@libp2p/websockets"; +import { all as filterAll } from "@libp2p/websockets/dist/src/filters"; +import { createLibp2p, Libp2pOptions } from "libp2p"; + +import { Bootstrap, BootstrapOptions } from "./discovery"; +import { Waku, WakuOptions } from "./waku"; +import { WakuFilter } from "./waku_filter"; +import { WakuLightPush } from "./waku_light_push"; +import { WakuRelay } from "./waku_relay"; +import { WakuStore } from "./waku_store"; + +export interface CreateOptions { + /** + * The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}. + * + * One and only one pubsub topic is used by Waku. This is used by: + * - WakuRelay to receive, route and send messages, + * - WakuLightPush to send messages, + * - WakuStore to retrieve messages. + * + * 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; + /** + * You can pass options to the `Libp2p` instance used by {@link Waku} using the {@link CreateOptions.libp2p} property. + * This property is the same type than the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create) + * apart that we made the `modules` property optional and partial, + * allowing its omission and letting Waku set good defaults. + * Notes that some values are overridden by {@link Waku} to ensure it implements the Waku protocol. + */ + libp2p?: Partial; + /** + * Byte array used as key for the noise protocol used for connection encryption + * by [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create) + * This is only used for test purposes to not run out of entropy during CI runs. + */ + staticNoiseKey?: Uint8Array; + /** + * Use libp2p-bootstrap to discover and connect to new nodes. + * + * See [[BootstrapOptions]] for available parameters. + * + * Note: It overrides any other peerDiscovery modules that may have been set via + * {@link CreateOptions.libp2p}. + */ + bootstrap?: BootstrapOptions; +} + +export async function createWaku( + options?: CreateOptions & WakuOptions +): Promise { + const peerDiscovery = []; + if (options?.bootstrap) { + peerDiscovery.push(new Bootstrap(options?.bootstrap)); + } + + const libp2pOpts = Object.assign( + { + transports: [new WebSockets({ filter: filterAll })], + streamMuxers: [new Mplex()], + pubsub: new WakuRelay(options), + connectionEncryption: [new Noise()], + peerDiscovery: peerDiscovery, + }, + options?.libp2p ?? {} + ); + + const libp2p = await createLibp2p(libp2pOpts); + + const wakuStore = new WakuStore(libp2p, options); + const wakuLightPush = new WakuLightPush(libp2p, options); + const wakuFilter = new WakuFilter(libp2p, options); + + return new Waku(options ?? {}, libp2p, wakuStore, wakuLightPush, wakuFilter); +} diff --git a/src/lib/enr/enr.node.spec.ts b/src/lib/enr/enr.node.spec.ts index 81c45c1c24..3c6415249e 100644 --- a/src/lib/enr/enr.node.spec.ts +++ b/src/lib/enr/enr.node.spec.ts @@ -1,8 +1,9 @@ import { expect } from "chai"; import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../../test_utils"; +import { createWaku } from "../create_waku"; import { waitForRemotePeer } from "../wait_for_remote_peer"; -import { createWaku, Protocols, Waku } from "../waku"; +import { Protocols, Waku } from "../waku"; import { ENR } from "./enr"; diff --git a/src/lib/wait_for_remote_peer.node.spec.ts b/src/lib/wait_for_remote_peer.node.spec.ts index 92c1ea489a..859ea36244 100644 --- a/src/lib/wait_for_remote_peer.node.spec.ts +++ b/src/lib/wait_for_remote_peer.node.spec.ts @@ -3,8 +3,9 @@ import { expect } from "chai"; import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../test_utils"; import { delay } from "../test_utils/delay"; +import { createWaku } from "./create_waku"; import { waitForRemotePeer } from "./wait_for_remote_peer"; -import { createWaku, Protocols, Waku } from "./waku"; +import { Protocols, Waku } from "./waku"; describe("Wait for remote peer", function () { let waku: Waku; diff --git a/src/lib/waku.node.spec.ts b/src/lib/waku.node.spec.ts index b27c5c8f37..9dc57ff3f0 100644 --- a/src/lib/waku.node.spec.ts +++ b/src/lib/waku.node.spec.ts @@ -8,9 +8,10 @@ import { Nwaku, } from "../test_utils/"; +import { createWaku } from "./create_waku"; import { generateSymmetricKey } from "./crypto"; import { waitForRemotePeer } from "./wait_for_remote_peer"; -import { createWaku, Protocols, Waku } from "./waku"; +import { Protocols, Waku } from "./waku"; import { WakuMessage } from "./waku_message"; const TestContentTopic = "/test/1/waku/utf8"; diff --git a/src/lib/waku.spec.ts b/src/lib/waku.spec.ts index 21d181a4ee..ff188b4671 100644 --- a/src/lib/waku.spec.ts +++ b/src/lib/waku.spec.ts @@ -1,7 +1,8 @@ import type { PeerId } from "@libp2p/interface-peer-id"; import { expect } from "chai"; -import { createWaku, Waku } from "./waku"; +import { createWaku } from "./create_waku"; +import { Waku } from "./waku"; describe("Waku Dial", function () { describe("Bootstrap [live data]", function () { diff --git a/src/lib/waku.ts b/src/lib/waku.ts index a23cc6dddb..da40250eb6 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -1,15 +1,11 @@ -import { Noise } from "@chainsafe/libp2p-noise"; import type { Stream } from "@libp2p/interface-connection"; import type { PeerId } from "@libp2p/interface-peer-id"; -import { Mplex } from "@libp2p/mplex"; import { peerIdFromString } from "@libp2p/peer-id"; -import { WebSockets } from "@libp2p/websockets"; -import { all as filterAll } from "@libp2p/websockets/filters"; -import { Multiaddr, multiaddr } from "@multiformats/multiaddr"; +import type { Multiaddr } from "@multiformats/multiaddr"; +import { multiaddr } from "@multiformats/multiaddr"; import debug from "debug"; -import { createLibp2p, Libp2p, Libp2pOptions } from "libp2p"; +import type { Libp2p } from "libp2p"; -import { Bootstrap, BootstrapOptions } from "./discovery"; import { FilterCodec, WakuFilter } from "./waku_filter"; import { LightPushCodec, WakuLightPush } from "./waku_light_push"; import { DecryptionMethod, WakuMessage } from "./waku_message"; @@ -29,21 +25,7 @@ export enum Protocols { Filter = "filter", } -export interface CreateOptions { - /** - * The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}. - * - * One and only one pubsub topic is used by Waku. This is used by: - * - WakuRelay to receive, route and send messages, - * - WakuLightPush to send messages, - * - WakuStore to retrieve messages. - * - * 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; +export interface WakuOptions { /** * Set keep alive frequency in seconds: Waku will send a `/ipfs/ping/1.0.0` * request to each peer after the set number of seconds. Set to 0 to disable. @@ -58,58 +40,9 @@ export interface CreateOptions { * @default {@link DefaultRelayKeepAliveValueSecs} */ relayKeepAlive?: number; - /** - * You can pass options to the `Libp2p` instance used by {@link Waku} using the {@link CreateOptions.libp2p} property. - * This property is the same type than the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create) - * apart that we made the `modules` property optional and partial, - * allowing its omission and letting Waku set good defaults. - * Notes that some values are overridden by {@link Waku} to ensure it implements the Waku protocol. - */ - libp2p?: Partial; - /** - * Byte array used as key for the noise protocol used for connection encryption - * by [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create) - * This is only used for test purposes to not run out of entropy during CI runs. - */ - staticNoiseKey?: Uint8Array; - /** - * Use libp2p-bootstrap to discover and connect to new nodes. - * - * See [[BootstrapOptions]] for available parameters. - * - * Note: It overrides any other peerDiscovery modules that may have been set via - * {@link CreateOptions.libp2p}. - */ - bootstrap?: BootstrapOptions; decryptionKeys?: Array; } -export async function createWaku(options?: CreateOptions): Promise { - const peerDiscovery = []; - if (options?.bootstrap) { - peerDiscovery.push(new Bootstrap(options?.bootstrap)); - } - - const libp2pOpts = Object.assign( - { - transports: [new WebSockets({ filter: filterAll })], - streamMuxers: [new Mplex()], - pubsub: new WakuRelay(options), - connectionEncryption: [new Noise()], - peerDiscovery: peerDiscovery, - }, - options?.libp2p ?? {} - ); - - const libp2p = await createLibp2p(libp2pOpts); - - const wakuStore = new WakuStore(libp2p, options); - const wakuLightPush = new WakuLightPush(libp2p, options); - const wakuFilter = new WakuFilter(libp2p, options); - - return new Waku(options ?? {}, libp2p, wakuStore, wakuLightPush, wakuFilter); -} - export class Waku { public libp2p: Libp2p; public relay: WakuRelay; @@ -125,7 +58,7 @@ export class Waku { }; constructor( - options: CreateOptions, + options: WakuOptions, libp2p: Libp2p, store: WakuStore, lightPush: WakuLightPush, diff --git a/src/lib/waku_filter/index.node.spec.ts b/src/lib/waku_filter/index.node.spec.ts index 05054e31f3..75a766253c 100644 --- a/src/lib/waku_filter/index.node.spec.ts +++ b/src/lib/waku_filter/index.node.spec.ts @@ -3,8 +3,9 @@ import debug from "debug"; import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../../test_utils"; import { delay } from "../../test_utils/delay"; +import { createWaku } from "../create_waku"; import { waitForRemotePeer } from "../wait_for_remote_peer"; -import { createWaku, Protocols, Waku } from "../waku"; +import { Protocols, Waku } from "../waku"; import { WakuMessage } from "../waku_message"; const log = debug("waku:test"); diff --git a/src/lib/waku_light_push/index.node.spec.ts b/src/lib/waku_light_push/index.node.spec.ts index 1ea42c49a5..486fe3b830 100644 --- a/src/lib/waku_light_push/index.node.spec.ts +++ b/src/lib/waku_light_push/index.node.spec.ts @@ -3,8 +3,9 @@ import debug from "debug"; import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../../test_utils"; import { delay } from "../../test_utils/delay"; +import { createWaku } from "../create_waku"; import { waitForRemotePeer } from "../wait_for_remote_peer"; -import { createWaku, Protocols, Waku } from "../waku"; +import { Protocols, Waku } from "../waku"; import { WakuMessage } from "../waku_message"; const dbg = debug("waku:test:lightpush"); diff --git a/src/lib/waku_message/index.node.spec.ts b/src/lib/waku_message/index.node.spec.ts index e26843b505..1bd88686d0 100644 --- a/src/lib/waku_message/index.node.spec.ts +++ b/src/lib/waku_message/index.node.spec.ts @@ -8,6 +8,7 @@ import { WakuRelayMessage, } from "../../test_utils"; import { delay } from "../../test_utils/delay"; +import { createWaku } from "../create_waku"; import { generatePrivateKey, generateSymmetricKey, @@ -15,7 +16,7 @@ import { } from "../crypto"; import { bytesToHex, bytesToUtf8, hexToBytes, utf8ToBytes } from "../utils"; import { waitForRemotePeer } from "../wait_for_remote_peer"; -import { createWaku, Protocols, Waku } from "../waku"; +import { Protocols, Waku } from "../waku"; import { DecryptionMethod, WakuMessage } from "./index"; diff --git a/src/lib/waku_relay/index.node.spec.ts b/src/lib/waku_relay/index.node.spec.ts index 1670c1f9cb..472c3d363b 100644 --- a/src/lib/waku_relay/index.node.spec.ts +++ b/src/lib/waku_relay/index.node.spec.ts @@ -11,13 +11,14 @@ import { } from "../../test_utils"; import { delay } from "../../test_utils/delay"; import { DefaultPubSubTopic } from "../constants"; +import { createWaku } from "../create_waku"; import { generatePrivateKey, generateSymmetricKey, getPublicKey, } from "../crypto"; import { waitForRemotePeer } from "../wait_for_remote_peer"; -import { createWaku, Protocols, Waku } from "../waku"; +import { Protocols, Waku } from "../waku"; import { DecryptionMethod, WakuMessage } from "../waku_message"; const log = debug("waku:test"); diff --git a/src/lib/waku_relay/index.ts b/src/lib/waku_relay/index.ts index 5d2bd454d3..aa84464226 100644 --- a/src/lib/waku_relay/index.ts +++ b/src/lib/waku_relay/index.ts @@ -12,13 +12,30 @@ import debug from "debug"; import { DefaultPubSubTopic } from "../constants"; import { hexToBytes } from "../utils"; -import { CreateOptions } from "../waku"; import { DecryptionMethod, WakuMessage } from "../waku_message"; import * as constants from "./constants"; const dbg = debug("waku:relay"); +export interface CreateOptions { + /** + * The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}. + * + * One and only one pubsub topic is used by Waku. This is used by: + * - WakuRelay to receive, route and send messages, + * - WakuLightPush to send messages, + * - WakuStore to retrieve messages. + * + * 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; + decryptionKeys?: Array; +} + /** * Implements the [Waku v2 Relay protocol]{@link https://rfc.vac.dev/spec/11/}. * Must be passed as a `pubsub` module to a {Libp2p} instance. diff --git a/src/lib/waku_store/index.node.spec.ts b/src/lib/waku_store/index.node.spec.ts index e814819865..00b3e6925a 100644 --- a/src/lib/waku_store/index.node.spec.ts +++ b/src/lib/waku_store/index.node.spec.ts @@ -7,13 +7,14 @@ import { NOISE_KEY_2, Nwaku, } from "../../test_utils"; +import { createWaku } from "../create_waku"; import { generatePrivateKey, generateSymmetricKey, getPublicKey, } from "../crypto"; import { waitForRemotePeer } from "../wait_for_remote_peer"; -import { createWaku, Protocols, Waku } from "../waku"; +import { Protocols, Waku } from "../waku"; import { DecryptionMethod, WakuMessage } from "../waku_message"; import { PageDirection } from "./history_rpc";