/** * @packageDocumentation * * When the discovery module is started by libp2p it subscribes to the discovery pubsub topic(s) * * It will immediately broadcast your peer data via pubsub and repeat the broadcast on the configured `interval` * * ## Security Considerations * * It is worth noting that this module does not include any message signing for broadcasts. The reason for this is that libp2p-pubsub supports message signing and enables it by default, which means the message you received has been verified to be from the originator, so we can trust that the peer information we have received is indeed from the peer who owns it. This doesn't mean the peer can't falsify its own records, but this module isn't currently concerned with that scenario. * * ## Requirements * * This module *MUST* be used on a libp2p node that is running [Pubsub](https://github.com/libp2p/js-libp2p-pubsub). If Pubsub does not exist, or is not running, this module will not work. * * To run a PubSub service, include a `pubsub` implementation in your services map such as `@chainsafe/libp2p-gossipsub`. * * For more information see the [docs on customizing libp2p](https://github.com/libp2p/js-libp2p/blob/main/doc/CONFIGURATION.md#customizing-libp2p). * * @example Usage in js-libp2p * * See the [js-libp2p configuration docs](https://github.com/libp2p/js-libp2p/blob/main/doc/CONFIGURATION.md#customizing-peer-discovery) for how to include this module as a peer discovery module in js-libp2p. * * If you are only interested in listening to the global pubsub topic the minimal configuration for using this with libp2p is: * * ```ts * import { createLibp2p } from 'libp2p' * import { websockets } from '@libp2p/websockets' * import { yamux } from '@chainsafe/libp2p-yamux' * import { noise } from '@chainsafe/libp2p-noise' * import { gossipsub } from '@chainsafe/libp2p-gossipsub' * import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery' * import { identify } from 'libp2p/identify' * * const node = await createLibp2p({ * transports: [ * websockets() * ], // Any libp2p transport(s) can be used * streamMuxers: [ * yamux() * ], * connectionEncryption: [ * noise() * ], * peerDiscovery: [ * pubsubPeerDiscovery() * ], * services: { * pubsub: gossipsub(), * identify: identify() * } * }) * ``` * * @example Customizing Pubsub Peer Discovery * * There are a few options you can use to customize `Pubsub Peer Discovery`. You can see the detailed [options](#options) below. * * ```ts * // ... Other imports from above * import PubSubPeerDiscovery from '@libp2p/pubsub-peer-discovery' * * // Custom topics * const topics = [ * `myApp._peer-discovery._p2p._pubsub`, // It's recommended but not required to extend the global space * '_peer-discovery._p2p._pubsub' // Include if you want to participate in the global space * ] * * const node = await createLibp2p({ * // ... * peerDiscovery: [ * pubsubPeerDiscovery({ * interval: 10000, * topics: topics, // defaults to ['_peer-discovery._p2p._pubsub'] * listenOnly: false * }) * ] * }) * ``` * * ## Options * * | Name | Type | Description | * | ---------- | --------------- | -------------------------------------------------------------------------------------------------------------- | * | interval | `number` | How often (in `ms`), after initial broadcast, your node should broadcast your peer data. Default (`10000ms`) | * | topics | `Array` | An Array of topic strings. If set, the default topic will not be used and must be included explicitly here | * | listenOnly | `boolean` | If true it will not broadcast peer data. Dont set this unless you have a specific reason to. Default (`false`) | * * ## Default Topic * * The default pubsub topic the module subscribes to is `_peer-discovery._p2p._pubsub`, which is also set on `PubsubPeerDiscovery.TOPIC`. */ import { TypedEventEmitter, peerDiscoverySymbol } from '@libp2p/interface'; import type { PeerDiscovery, PeerDiscoveryEvents, PeerId, Startable, ComponentLogger, TypedEventTarget } from '@libp2p/interface'; import type { AddressManager } from '@libp2p/interface-internal'; export declare const TOPIC = "_peer-discovery._p2p._pubsub"; export interface PubsubPeerDiscoveryInit { /** * How often (ms) we should broadcast our infos */ interval?: number; /** * What topics to subscribe to. If set, the default will NOT be used. */ topics?: string[]; /** * If true, we will not broadcast our peer data */ listenOnly?: boolean; } export interface Message { topic: string; data: Uint8Array; } export interface PubSubEvents { message: CustomEvent; } export interface PubSub extends TypedEventTarget { subscribe(topic: string): void; unsubscribe(topic: string): void; getSubscribers(topic: string): PeerId[]; publish(topic: string, message: Uint8Array): void; } export interface PubSubPeerDiscoveryComponents { peerId: PeerId; pubsub?: PubSub; addressManager: AddressManager; logger: ComponentLogger; } /** * A Peer Discovery Service that leverages libp2p Pubsub to find peers. */ export declare class PubSubPeerDiscovery extends TypedEventEmitter implements PeerDiscovery, Startable { readonly [peerDiscoverySymbol] = true; readonly [Symbol.toStringTag] = "@libp2p/pubsub-peer-discovery"; private readonly interval; private readonly listenOnly; private readonly topics; private intervalId?; private readonly components; private readonly log; constructor(components: PubSubPeerDiscoveryComponents, init?: PubsubPeerDiscoveryInit); isStarted(): boolean; start(): void; /** * Subscribes to the discovery topic on `libp2p.pubsub` and performs a broadcast * immediately, and every `this.interval` */ afterStart(): void; beforeStop(): void; /** * Unsubscribes from the discovery topic */ stop(): void; /** * Performs a broadcast via Pubsub publish */ _broadcast(): void; /** * Handles incoming pubsub messages for our discovery topic */ _onMessage(event: CustomEvent): void; } export declare function pubsubPeerDiscovery(init?: PubsubPeerDiscoveryInit): (components: PubSubPeerDiscoveryComponents) => PeerDiscovery; //# sourceMappingURL=index.d.ts.map