js-waku/src/lib/waku.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-03-19 03:40:16 +00:00
import Libp2p from 'libp2p';
import Mplex from 'libp2p-mplex';
import { bytes } from 'libp2p-noise/dist/src/@types/basic';
import { Noise } from 'libp2p-noise/dist/src/noise';
2021-03-19 03:40:16 +00:00
import TCP from 'libp2p-tcp';
import Multiaddr from 'multiaddr';
import PeerId from 'peer-id';
2021-03-19 03:40:16 +00:00
import { CODEC, WakuRelay, WakuRelayPubsub } from './waku_relay';
2021-03-19 03:40:16 +00:00
export default class Waku {
private constructor(public libp2p: Libp2p, public relay: WakuRelay) {}
/**
* Create new waku node
* @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> {
2021-03-19 03:40:16 +00:00
const libp2p = await Libp2p.create({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0'],
},
modules: {
transport: [TCP],
streamMuxer: [Mplex],
connEncryption: [new Noise(staticNoiseKey)],
2021-03-19 03:40:16 +00:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Type needs update
pubsub: WakuRelayPubsub,
},
});
await libp2p.start();
return new Waku(libp2p, new WakuRelay(libp2p.pubsub));
}
async dialWithMultiAddr(peerId: PeerId, multiaddr: Multiaddr[]) {
this.libp2p.peerStore.addressBook.set(peerId, multiaddr);
2021-03-23 01:14:48 +00:00
await this.libp2p.dialProtocol(peerId, CODEC);
}
async stop() {
await this.libp2p.stop();
}
2021-03-19 03:40:16 +00:00
}