Libp2p nodes implement this interface.

Type Parameters

Hierarchy

Properties

#private: any
afterStart?: (() => void | Promise<void>)

Type declaration

    • (): void | Promise<void>
    • If implemented, this method will be invoked after the start method.

      All other components will have had their start method invoked before this method is called.

      Returns void | Promise<void>

afterStop?: (() => void | Promise<void>)

Type declaration

    • (): void | Promise<void>
    • If implemented, this method will be invoked after the stop method.

      All other components will have had their stop method invoked before this method is called.

      Returns void | Promise<void>

beforeStart?: (() => void | Promise<void>)

Type declaration

    • (): void | Promise<void>
    • If implemented, this method will be invoked before the start method.

      It should not assume any other components have been started.

      Returns void | Promise<void>

beforeStop?: (() => void | Promise<void>)

Type declaration

    • (): void | Promise<void>
    • If implemented, this method will be invoked before the stop method.

      Any other components will still be running when this method is called.

      Returns void | Promise<void>

contentRouting: ContentRouting

The content routing subsystem allows the user to find providers for content, let the network know they are providers for content, and get/put values to the DHT.

Example

for await (const peerInfo of libp2p.contentRouting.findProviders(cid)) {
console.info(peerInfo)
// { id: PeerId(12D3Foo...), multiaddrs: [] ... }
}
dial: ((peer: Multiaddr | Multiaddr[] | PeerId, options?: AbortOptions) => Promise<Connection>)

Type declaration

    • (peer: Multiaddr | Multiaddr[] | PeerId, options?: AbortOptions): Promise<Connection>
    • Dials to the provided peer. If successful, the known metadata of the peer will be added to the nodes peerStore.

      If a PeerId is passed as the first argument, the peer will need to have known multiaddrs for it in the PeerStore.

      Example

      const conn = await libp2p.dial(remotePeerId)

      // create a new stream within the connection
      const { stream, protocol } = await conn.newStream(['/echo/1.1.0', '/echo/1.0.0'])

      // protocol negotiated: 'echo/1.0.0' means that the other party only supports the older version

      // ...
      await conn.close()

      Parameters

      Returns Promise<Connection>

dialProtocol: ((peer: Multiaddr | Multiaddr[] | PeerId, protocols: string | string[], options?: AbortOptions) => Promise<Stream>)

Type declaration

    • (peer: Multiaddr | Multiaddr[] | PeerId, protocols: string | string[], options?: AbortOptions): Promise<Stream>
    • Dials to the provided peer and tries to handshake with the given protocols in order. If successful, the known metadata of the peer will be added to the nodes peerStore, and the MuxedStream will be returned together with the successful negotiated protocol.

      Example

      import { pipe } from 'it-pipe'

      const { stream, protocol } = await libp2p.dialProtocol(remotePeerId, protocols)

      // Use this new stream like any other duplex stream
      pipe([1, 2, 3], stream, consume)

      Parameters

      Returns Promise<Stream>

getConnections: ((peerId?: PeerId) => Connection[])

Type declaration

    • (peerId?: PeerId): Connection[]
    • Return a list of all connections this node has open, optionally filtering by a PeerId

      Example

      for (const connection of libp2p.getConnections()) {
      console.log(peerId, connection.remoteAddr.toString())
      // Logs the PeerId string and the observed remote multiaddr of each Connection
      }

      Parameters

      Returns Connection[]

getDialQueue: (() => PendingDial[])

Type declaration

    • (): PendingDial[]
    • Return the list of dials currently in progress or queued to start

      Example

      for (const pendingDial of libp2p.getDialQueue()) {
      console.log(pendingDial)
      }

      Returns PendingDial[]

getMultiaddrs: (() => Multiaddr[])

Type declaration

    • (): Multiaddr[]
    • Get a deduplicated list of peer advertising multiaddrs by concatenating the listen addresses used by transports with any configured announce addresses as well as observed addresses reported by peers.

      If Announce addrs are specified, configured listen addresses will be ignored though observed addresses will still be included.

      Example

      const listenMa = libp2p.getMultiaddrs()
      // [ <Multiaddr 047f00000106f9ba - /ip4/127.0.0.1/tcp/63930> ]

      Returns Multiaddr[]

getPeers: (() => PeerId[])

Type declaration

    • (): PeerId[]
    • Return a list of all peers we currently have a connection open to

      Returns PeerId[]

getProtocols: (() => string[])

Type declaration

    • (): string[]
    • Returns a list of supported protocols

      Example

      const protocols = libp2p.getProtocols()
      // [ '/ipfs/ping/1.0.0', '/ipfs/id/1.0.0' ]

      Returns string[]

getPublicKey: ((peer: PeerId, options?: AbortOptions) => Promise<Uint8Array>)

Type declaration

handle: ((protocol: string | string[], handler: StreamHandler, options?: StreamHandlerOptions) => Promise<void>)

Type declaration

    • (protocol: string | string[], handler: StreamHandler, options?: StreamHandlerOptions): Promise<void>
    • Sets up multistream-select routing of protocols to their application handlers. Whenever a stream is opened on one of the provided protocols, the handler will be called. handle must be called in order to register a handler and support for a given protocol. This also informs other peers of the protocols you support.

      libp2p.handle(protocols, handler, options)

      In the event of a new handler for the same protocol being added, the first one is discarded.

      Example

      const handler = ({ connection, stream, protocol }) => {
      // use stream or connection according to the needs
      }

      libp2p.handle('/echo/1.0.0', handler, {
      maxInboundStreams: 5,
      maxOutboundStreams: 5
      })

      Parameters

      Returns Promise<void>

hangUp: ((peer: Multiaddr | PeerId) => Promise<void>)

Type declaration

    • (peer: Multiaddr | PeerId): Promise<void>
    • Attempts to gracefully close an open connection to the given peer. If the connection is not closed in the grace period, it will be forcefully closed.

      Example

      await libp2p.hangUp(remotePeerId)
      

      Parameters

      Returns Promise<void>

isStarted: (() => boolean)

Type declaration

    • (): boolean
    • Returns boolean

keychain: KeyChain

The keychain contains the keys used by the current node, and can create new keys, export them, import them, etc.

Example

const keyInfo = await libp2p.keychain.createKey('new key')
console.info(keyInfo)
// { id: '...', name: 'new key' }
metrics?: Metrics

The metrics subsystem allows recording values to assess the health/performance of the running node.

Example

const metric = libp2p.metrics.registerMetric({
'my-metric'
})

// later
metric.update(5)
peerId: PeerId

The PeerId is a unique identifier for a node on the network.

It is the hash of an RSA public key or, for Ed25519 or secp256k1 keys, the key itself.

Example

console.info(libp2p.peerId)
// PeerId(12D3Foo...)
peerRouting: PeerRouting

The peer routing subsystem allows the user to find peers on the network or to find peers close to binary keys.

Example

const peerInfo = await libp2p.peerRouting.findPeer(peerId)
console.info(peerInfo)
// { id: PeerId(12D3Foo...), multiaddrs: [] ... }

Example

for await (const peerInfo of libp2p.peerRouting.getClosestPeers(key)) {
console.info(peerInfo)
// { id: PeerId(12D3Foo...), multiaddrs: [] ... }
}
peerStore: PeerStore

The peer store holds information we know about other peers on the network.

  • multiaddrs, supported protocols, etc.

Example

const peer = await libp2p.peerStore.get(peerId)
console.info(peer)
// { id: PeerId(12D3Foo...), addresses: [] ... }
register: ((protocol: string, topology: Topology) => Promise<string>)

Type declaration

    • (protocol: string, topology: Topology): Promise<string>
    • Register a topology to be informed when peers are encountered that support the specified protocol

      Example

      import { createTopology } from '@libp2p/topology'

      const id = await libp2p.register('/echo/1.0.0', createTopology({
      onConnect: (peer, connection) => {
      // handle connect
      },
      onDisconnect: (peer, connection) => {
      // handle disconnect
      }
      }))

      Parameters

      Returns Promise<string>

services: T

A set of user defined services

start: (() => void | Promise<void>)

Type declaration

    • (): void | Promise<void>
    • This method will be invoked to start the component.

      It should not assume that any other components have been started.

      Returns void | Promise<void>

stop: (() => void | Promise<void>)

Type declaration

    • (): void | Promise<void>
    • This method will be invoked to stop the component.

      It should not assume any other components are running when it is called.

      Returns void | Promise<void>

unhandle: ((protocols: string | string[]) => Promise<void>)

Type declaration

    • (protocols: string | string[]): Promise<void>
    • Removes the handler for each protocol. The protocol will no longer be supported on streams.

      Example

      libp2p.unhandle(['/echo/1.0.0'])
      

      Parameters

      • protocols: string | string[]

      Returns Promise<void>

unregister: ((id: string) => void)

Type declaration

    • (id: string): void
    • Unregister topology to no longer be informed when peers connect or disconnect.

      Example

      const id = await libp2p.register(...)

      libp2p.unregister(id)

      Parameters

      • id: string

      Returns void

Methods

  • Parameters

    • type: string

    Returns number

  • Type Parameters

    • Detail

    Parameters

    • type: keyof Libp2pEvents<T>
    • detail: CustomEventInit<Detail>

    Returns boolean