Pass listening addresses as argument

This commit is contained in:
Franck Royer 2021-03-29 13:56:17 +11:00
parent d2f1995f3e
commit c3cf6462cc
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
5 changed files with 26 additions and 13 deletions

View File

@ -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 => {

View File

@ -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();

View File

@ -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<Waku>}
*/
static async create(staticNoiseKey?: bytes): Promise<Waku> {
static async create(options: Partial<CreateOptions>): Promise<Waku> {
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,

View File

@ -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);

View File

@ -16,7 +16,6 @@ export class WakuRelayPubsub extends Gossipsub {
/**
*
* @param libp2p: Libp2p
* @param options: Partial<GossipInputOptions>
*/
constructor(libp2p: Libp2p) {
super(libp2p, {