diff --git a/src/lib/waku.ts b/src/lib/waku.ts index 7ef89a9123..cd1e15ebc3 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -1,4 +1,4 @@ -import Libp2p from 'libp2p'; +import Libp2p, { Libp2pConfig, Libp2pModules, Libp2pOptions } from 'libp2p'; import Mplex from 'libp2p-mplex'; import { bytes } from 'libp2p-noise/dist/src/@types/basic'; import { Noise } from 'libp2p-noise/dist/src/noise'; @@ -12,16 +12,14 @@ import { StoreCodec, WakuStore } from './waku_store'; const transportKey = Websockets.prototype[Symbol.toStringTag]; -export interface CreateOptions { - listenAddresses: string[]; - staticNoiseKey: bytes | undefined; - modules: { - transport: import('libp2p-interfaces/src/transport/types').TransportFactory< - any, - any - >[]; - }; -} +export type CreateOptions = + | { + listenAddresses: string[] | undefined; + staticNoiseKey: bytes | undefined; + modules: Partial; + config: Partial; + } + | (Libp2pOptions & import('libp2p').CreateOptions); export default class Waku { public libp2p: Libp2p; @@ -52,6 +50,19 @@ export default class Waku { options ); + opts.config = Object.assign( + { + transport: { + [transportKey]: { + filter: filters.all, + }, + }, + }, + options.config + ); + + opts.modules = Object.assign({}, options.modules); + let transport = [Websockets]; if (opts.modules?.transport) { transport = transport.concat(opts.modules?.transport); @@ -73,13 +84,7 @@ export default class Waku { // @ts-ignore: Type needs update pubsub: WakuRelay, }, - config: { - transport: { - [transportKey]: { - filter: filters.all, - }, - }, - }, + config: opts.config, }); const wakuStore = new WakuStore(libp2p); diff --git a/src/lib/waku_relay/index.ts b/src/lib/waku_relay/index.ts index 487ca819bd..c04f5619d5 100644 --- a/src/lib/waku_relay/index.ts +++ b/src/lib/waku_relay/index.ts @@ -1,6 +1,15 @@ import Gossipsub from 'libp2p-gossipsub'; -import { Libp2p } from 'libp2p-gossipsub/src/interfaces'; +import { + AddrInfo, + Libp2p, + MessageIdFunction, +} from 'libp2p-gossipsub/src/interfaces'; import { ControlPrune, PeerInfo } from 'libp2p-gossipsub/src/message'; +import { MessageCache } from 'libp2p-gossipsub/src/message-cache'; +import { + PeerScoreParams, + PeerScoreThresholds, +} from 'libp2p-gossipsub/src/score'; import { createGossipRpc, messageIdToString, @@ -19,26 +28,50 @@ import { RelayHeartbeat } from './relay_heartbeat'; export * from './constants'; export * from './relay_heartbeat'; +/** + * See GossipOptions from libp2p-gossipsub + */ +interface GossipOptions { + emitSelf: boolean; + gossipIncoming: boolean; + fallbackToFloodsub: boolean; + floodPublish: boolean; + doPX: boolean; + msgIdFn: MessageIdFunction; + messageCache: MessageCache; + globalSignaturePolicy: string; + scoreParams: Partial; + scoreThresholds: Partial; + directPeers: AddrInfo[]; + D: number; + Dlo: number; + Dhi: number; + Dscore: number; + Dout: number; + Dlazy: number; +} + export class WakuRelay extends Gossipsub { heartbeat: RelayHeartbeat; /** * - * @param libp2p: Libp2p + * @param {Libp2p} libp2p + * @param {Partial} [options] */ - constructor(libp2p: Libp2p) { - super(libp2p, { - emitSelf: false, - // Ensure that no signature is expected in the messages. - globalSignaturePolicy: SignaturePolicy.StrictNoSign, - }); + constructor(libp2p: Libp2p, options?: Partial) { + super( + libp2p, + Object.assign(options, { + // Ensure that no signature is included nor expected in the messages. + globalSignaturePolicy: SignaturePolicy.StrictNoSign, + }) + ); this.heartbeat = new RelayHeartbeat(this); const multicodecs = [constants.RelayCodec]; - // This is the downside of using `libp2p-gossipsub` instead of - // implementing WakuRelay from scratch. Object.assign(this, { multicodecs }); } diff --git a/web-chat/src/App.tsx b/web-chat/src/App.tsx index 2cf1554022..ed76e6063d 100644 --- a/web-chat/src/App.tsx +++ b/web-chat/src/App.tsx @@ -24,7 +24,7 @@ export default function App() { useEffect(() => { async function initWaku() { try { - const waku = await Waku.create({}); + const waku = await Waku.create({ config: { pubsub: { enabled: true, emitSelf: true } } }); setState(({ messages }) => { return { waku, messages };