diff --git a/README.md b/README.md index b678d06..5e48284 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -# @libp2p/pubsub-peer-discovery - [![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/) [![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io) [![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-pubsub-peer-discovery.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-pubsub-peer-discovery) @@ -7,61 +5,27 @@ > A libp2p module that uses pubsub for mdns like peer discovery -## Table of contents - -- [Install](#install) - - [Browser ` -``` - -## Design +# About -### Flow +When the discovery module is started by libp2p it subscribes to the discovery pubsub topic(s) -- 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` +It will immediately broadcast your peer data via pubsub and repeat the broadcast on the configured `interval` -### Security Considerations +## 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. -## Usage - -### Requirements +## 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/master/doc/CONFIGURATION.md#customizing-libp2p). +For more information see the [docs on customizing libp2p](https://github.com/libp2p/js-libp2p/blob/main/doc/CONFIGURATION.md#customizing-libp2p). -### Usage in js-libp2p +## Example - Usage in js-libp2p -See the [js-libp2p configuration docs](https://github.com/libp2p/js-libp2p/blob/master/doc/CONFIGURATION.md#customizing-peer-discovery) for how to include this module as a peer discovery module 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: @@ -94,7 +58,7 @@ const node = await createLibp2p({ }) ``` -### Customizing Pubsub Peer Discovery +## 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. @@ -120,7 +84,7 @@ const node = await createLibp2p({ }) ``` -#### Options +## Options | Name | Type | Description | | ---------- | --------------- | -------------------------------------------------------------------------------------------------------------- | @@ -128,21 +92,35 @@ const node = await createLibp2p({ | 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 +## Default Topic The default pubsub topic the module subscribes to is `_peer-discovery._p2p._pubsub`, which is also set on `PubsubPeerDiscovery.TOPIC`. -## API Docs +# Install + +```console +$ npm i @libp2p/pubsub-peer-discovery +``` + +## Browser ` +``` + +# API Docs - -## License +# License Licensed under either of - Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / ) - MIT ([LICENSE-MIT](LICENSE-MIT) / ) -## Contribution +# Contribution Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. diff --git a/package.json b/package.json index 0858ce4..5b8e441 100644 --- a/package.json +++ b/package.json @@ -141,17 +141,18 @@ "docs": "aegir docs" }, "dependencies": { - "@libp2p/interface": "^0.1.2", - "@libp2p/interface-internal": "^0.1.5", - "@libp2p/logger": "^3.0.2", - "@libp2p/peer-id": "^3.0.2", + "@libp2p/interface": "^1.0.1", + "@libp2p/interface-internal": "^1.0.1", + "@libp2p/peer-id": "^4.0.1", "@multiformats/multiaddr": "^12.0.0", "protons-runtime": "^5.0.0", - "uint8arraylist": "^2.4.3" + "uint8arraylist": "^2.4.3", + "uint8arrays": "^4.0.9" }, "devDependencies": { - "@libp2p/interface-compliance-tests": "^4.0.6", - "@libp2p/peer-id-factory": "^3.0.4", + "@libp2p/interface-compliance-tests": "^5.0.5", + "@libp2p/logger": "^4.0.1", + "@libp2p/peer-id-factory": "^4.0.0", "aegir": "^41.0.5", "p-defer": "^4.0.0", "p-wait-for": "^5.0.0", diff --git a/src/index.ts b/src/index.ts index 0f8b5e0..e649a94 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,17 +1,103 @@ -import { CustomEvent, EventEmitter } from '@libp2p/interface/events' -import { peerDiscovery } from '@libp2p/interface/peer-discovery' -import { logger } from '@libp2p/logger' +/** + * @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: + * + * ```js + * 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: [ + * mplex() + * ], + * connectionEncryption: [ + * yamux() + * ], + * 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. + * + * ```js + * // ... 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 { peerIdFromKeys } from '@libp2p/peer-id' import { multiaddr } from '@multiformats/multiaddr' import { Peer as PBPeer } from './peer.js' -import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface/peer-discovery' -import type { PeerId } from '@libp2p/interface/peer-id' -import type { PeerInfo } from '@libp2p/interface/peer-info' -import type { Message, PubSub } from '@libp2p/interface/pubsub' -import type { Startable } from '@libp2p/interface/startable' -import type { AddressManager } from '@libp2p/interface-internal/address-manager' - -const log = logger('libp2p:discovery:pubsub') +import type { PeerDiscovery, PeerDiscoveryEvents, PeerId, PeerInfo, Message, PubSub, Startable, ComponentLogger, Logger } from '@libp2p/interface' +import type { AddressManager } from '@libp2p/interface-internal' + export const TOPIC = '_peer-discovery._p2p._pubsub' export interface PubsubPeerDiscoveryInit { @@ -35,13 +121,14 @@ export interface PubSubPeerDiscoveryComponents { peerId: PeerId pubsub?: PubSub addressManager: AddressManager + logger: ComponentLogger } /** * A Peer Discovery Service that leverages libp2p Pubsub to find peers. */ -export class PubSubPeerDiscovery extends EventEmitter implements PeerDiscovery, Startable { - public readonly [peerDiscovery] = true +export class PubSubPeerDiscovery extends TypedEventEmitter implements PeerDiscovery, Startable { + public readonly [peerDiscoverySymbol] = true public readonly [Symbol.toStringTag] = '@libp2p/pubsub-peer-discovery' private readonly interval: number @@ -49,6 +136,7 @@ export class PubSubPeerDiscovery extends EventEmitter imple private readonly topics: string[] private intervalId?: ReturnType private readonly components: PubSubPeerDiscoveryComponents + private readonly log: Logger constructor (components: PubSubPeerDiscoveryComponents, init: PubsubPeerDiscoveryInit = {}) { super() @@ -62,6 +150,7 @@ export class PubSubPeerDiscovery extends EventEmitter imple this.components = components this.interval = interval ?? 10000 this.listenOnly = listenOnly ?? false + this.log = components.logger.forComponent('libp2p:discovery:pubsub') // Ensure we have topics if (Array.isArray(topics) && topics.length > 0) { @@ -162,7 +251,7 @@ export class PubSubPeerDiscovery extends EventEmitter imple } for (const topic of this.topics) { - log('broadcasting our peer data on topic %s', topic) + this.log('broadcasting our peer data on topic %s', topic) void pubsub.publish(topic, encodedPeer) } } @@ -189,17 +278,16 @@ export class PubSubPeerDiscovery extends EventEmitter imple return } - log('discovered peer %p on %s', peerId, message.topic) + this.log('discovered peer %p on %s', peerId, message.topic) - this.dispatchEvent(new CustomEvent('peer', { + this.safeDispatchEvent('peer', { detail: { id: peerId, - multiaddrs: peer.addrs.map(b => multiaddr(b)), - protocols: [] + multiaddrs: peer.addrs.map(b => multiaddr(b)) } - })) + }) }).catch(err => { - log.error(err) + this.log.error(err) }) } } diff --git a/src/peer.ts b/src/peer.ts index 129ef2a..7ec7303 100644 --- a/src/peer.ts +++ b/src/peer.ts @@ -4,8 +4,8 @@ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */ /* eslint-disable @typescript-eslint/no-empty-interface */ -import { encodeMessage, decodeMessage, message } from 'protons-runtime' -import type { Codec } from 'protons-runtime' +import { type Codec, decodeMessage, encodeMessage, message } from 'protons-runtime' +import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc' import type { Uint8ArrayList } from 'uint8arraylist' export interface Peer { @@ -40,7 +40,7 @@ export namespace Peer { } }, (reader, length) => { const obj: any = { - publicKey: new Uint8Array(0), + publicKey: uint8ArrayAlloc(0), addrs: [] } @@ -50,15 +50,18 @@ export namespace Peer { const tag = reader.uint32() switch (tag >>> 3) { - case 1: + case 1: { obj.publicKey = reader.bytes() break - case 2: + } + case 2: { obj.addrs.push(reader.bytes()) break - default: + } + default: { reader.skipType(tag & 7) break + } } } diff --git a/test/compliance.spec.ts b/test/compliance.spec.ts index ac406f6..25a5ec9 100644 --- a/test/compliance.spec.ts +++ b/test/compliance.spec.ts @@ -1,14 +1,15 @@ /* eslint-env mocha */ -import { CustomEvent } from '@libp2p/interface/events' +import { CustomEvent } from '@libp2p/interface' import tests from '@libp2p/interface-compliance-tests/peer-discovery' +import { defaultLogger } from '@libp2p/logger' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { multiaddr } from '@multiformats/multiaddr' import { stubInterface } from 'ts-sinon' import { pubsubPeerDiscovery, TOPIC } from '../src/index.js' import { Peer as PBPeer } from '../src/peer.js' -import type { PubSub } from '@libp2p/interface/pubsub' -import type { AddressManager } from '@libp2p/interface-internal/address-manager' +import type { PubSub } from '@libp2p/interface' +import type { AddressManager } from '@libp2p/interface-internal' describe('compliance tests', () => { let intervalId: ReturnType @@ -26,7 +27,8 @@ describe('compliance tests', () => { const pubsubDiscovery = pubsubPeerDiscovery()({ pubsub: stubInterface(), peerId: await createEd25519PeerId(), - addressManager + addressManager, + logger: defaultLogger() }) intervalId = setInterval(() => { diff --git a/test/index.spec.ts b/test/index.spec.ts index 7172875..703fdd1 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ -import { CustomEvent } from '@libp2p/interface/events' -import { start, stop } from '@libp2p/interface/startable' +import { CustomEvent, start, stop } from '@libp2p/interface' +import { defaultLogger } from '@libp2p/logger' import { peerIdFromKeys } from '@libp2p/peer-id' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { multiaddr } from '@multiformats/multiaddr' @@ -12,10 +12,8 @@ import sinon from 'sinon' import { type StubbedInstance, stubInterface } from 'ts-sinon' import { pubsubPeerDiscovery, type PubSubPeerDiscoveryComponents, TOPIC } from '../src/index.js' import * as PB from '../src/peer.js' -import type { PeerDiscovery } from '@libp2p/interface/peer-discovery' -import type { PeerInfo } from '@libp2p/interface/peer-info' -import type { PubSub } from '@libp2p/interface/pubsub' -import type { AddressManager } from '@libp2p/interface-internal/address-manager' +import type { PeerDiscovery, PeerInfo, PubSub } from '@libp2p/interface' +import type { AddressManager } from '@libp2p/interface-internal' const listeningMultiaddr = multiaddr('/ip4/127.0.0.1/tcp/9000/ws') @@ -37,7 +35,8 @@ describe('PubSub Peer Discovery', () => { components = { peerId, pubsub: mockPubsub, - addressManager + addressManager, + logger: defaultLogger() } }) @@ -97,8 +96,7 @@ describe('PubSub Peer Discovery', () => { multiaddrs: [ multiaddr('/ip4/0.0.0.0/tcp/8080/ws'), multiaddr('/ip4/0.0.0.0/tcp/8081/ws') - ], - protocols: [] + ] } const peer = { publicKey: peerId.publicKey,