From c3cf6462cc7e3fe7f7f67c7185e65d2c9ca47b5e Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Mon, 29 Mar 2021 13:56:17 +1100 Subject: [PATCH] Pass listening addresses as argument --- chat/index.ts | 2 +- src/lib/waku.spec.ts | 2 +- src/lib/waku.ts | 20 +++++++++++++++++--- src/lib/waku_relay.spec.ts | 14 +++++++------- src/lib/waku_relay.ts | 1 - 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/chat/index.ts b/chat/index.ts index d865a9d3f8..a584d33e6c 100644 --- a/chat/index.ts +++ b/chat/index.ts @@ -7,7 +7,7 @@ import { delay } from '../build/main/test_utils/delay'; ;(async function() { - const waku = await Waku.create(); + const waku = await Waku.create({listenAddresses: ['/ip4/0.0.0.0/tcp/55123']}); // TODO: Bubble event to waku, infere topic, decode msg waku.libp2p.pubsub.on(TOPIC, event => { diff --git a/src/lib/waku.spec.ts b/src/lib/waku.spec.ts index 89c9421a10..690feccb86 100644 --- a/src/lib/waku.spec.ts +++ b/src/lib/waku.spec.ts @@ -11,7 +11,7 @@ describe('Waku', function () { describe('Interop: Nim', function () { it('nim connects to js', async function () { this.timeout(10_000); - const waku = await Waku.create(NOISE_KEY_1); + const waku = await Waku.create({ staticNoiseKey: NOISE_KEY_1 }); const peerId = waku.libp2p.peerId.toB58String(); diff --git a/src/lib/waku.ts b/src/lib/waku.ts index 7148dfb1d3..129313047c 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -8,24 +8,38 @@ import PeerId from 'peer-id'; import { CODEC, WakuRelay, WakuRelayPubsub } from './waku_relay'; +export interface CreateOptions { + listenAddresses: string[]; + staticNoiseKey: bytes | undefined; +} + export default class Waku { private constructor(public libp2p: Libp2p, public relay: WakuRelay) {} /** * Create new waku node + * @param listenAddresses: Array of Multiaddrs on which the node should listen. If not present, defaults to ['/ip4/0.0.0.0/tcp/0']. * @param staticNoiseKey: A static key to use for noise, * mainly used for test to reduce entropy usage. * @returns {Promise} */ - static async create(staticNoiseKey?: bytes): Promise { + static async create(options: Partial): Promise { + const opts = Object.assign( + { + listenAddresses: ['/ip4/0.0.0.0/tcp/0'], + staticNoiseKey: undefined, + }, + options + ); + const libp2p = await Libp2p.create({ addresses: { - listen: ['/ip4/0.0.0.0/tcp/55123'], + listen: opts.listenAddresses, }, modules: { transport: [TCP], streamMuxer: [Mplex], - connEncryption: [new Noise(staticNoiseKey)], + connEncryption: [new Noise(opts.staticNoiseKey)], // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore: Type needs update pubsub: WakuRelayPubsub, diff --git a/src/lib/waku_relay.spec.ts b/src/lib/waku_relay.spec.ts index 3d905c7217..24b9d465fa 100644 --- a/src/lib/waku_relay.spec.ts +++ b/src/lib/waku_relay.spec.ts @@ -21,8 +21,8 @@ describe('Waku Relay', () => { let waku2: Waku; beforeEach(async function () { [waku1, waku2] = await Promise.all([ - Waku.create(NOISE_KEY_1), - Waku.create(NOISE_KEY_2), + Waku.create({ staticNoiseKey: NOISE_KEY_1 }), + Waku.create({ staticNoiseKey: NOISE_KEY_2 }), ]); await waku1.dialWithMultiAddr(waku2.libp2p.peerId, waku2.libp2p.multiaddrs); @@ -109,7 +109,7 @@ describe('Waku Relay', () => { beforeEach(async function () { this.timeout(10_000); - waku = await Waku.create(NOISE_KEY_1); + waku = await Waku.create({ staticNoiseKey: NOISE_KEY_1 }); const peerId = waku.libp2p.peerId.toB58String(); const localMultiaddr = waku.libp2p.multiaddrs.find((addr) => @@ -180,7 +180,7 @@ describe('Waku Relay', () => { beforeEach(async function () { this.timeout(10_000); - waku = await Waku.create(NOISE_KEY_1); + waku = await Waku.create({ staticNoiseKey: NOISE_KEY_1 }); nimWaku = new NimWaku(this.test!.ctx!.currentTest!.title); await nimWaku.start(); @@ -250,7 +250,7 @@ describe('Waku Relay', () => { beforeEach(async function () { this.timeout(10_000); - waku = await Waku.create(NOISE_KEY_1); + waku = await Waku.create({ staticNoiseKey: NOISE_KEY_1 }); nimWaku = new NimWaku(makeLogFileName(this)); await nimWaku.start(); @@ -321,8 +321,8 @@ describe('Waku Relay', () => { beforeEach(async function () { this.timeout(10_000); [waku1, waku2] = await Promise.all([ - Waku.create(NOISE_KEY_1), - Waku.create(NOISE_KEY_2), + Waku.create({ staticNoiseKey: NOISE_KEY_1 }), + Waku.create({ staticNoiseKey: NOISE_KEY_2 }), ]); nimWaku = new NimWaku(this.test!.ctx!.currentTest!.title); diff --git a/src/lib/waku_relay.ts b/src/lib/waku_relay.ts index f8e91350f6..8cedd5e189 100644 --- a/src/lib/waku_relay.ts +++ b/src/lib/waku_relay.ts @@ -16,7 +16,6 @@ export class WakuRelayPubsub extends Gossipsub { /** * * @param libp2p: Libp2p - * @param options: Partial */ constructor(libp2p: Libp2p) { super(libp2p, {