Skip to content

peer:discovery maddrs are missing the /p2p/{PEER_ID}/ part for circuit relay maddrs #3239

@2color

Description

@2color

Problem

The browser WebRTC guide uses @libp2p/pubsub-peer-discovery to discover peers. While the module correctly emits the peer event with maddrs of discovered webrtc peers (source), js-libp2p emits a peer:discovery event, however it's missing the /p2p/PEER_ID component that is present in the original peer event.

I've worked around this as follows:

  libp2p.addEventListener('peer:discovery', async (evt) => {
    console.log(
      `Discovered new peer (${evt.detail.id.toString()}). Dialling...`,
      evt.detail.multiaddrs.map((ma) => ma.toString()),
    )
    try {
      const maddrs = evt.detail.multiaddrs.map((ma) => ma.encapsulate(`/p2p/${evt.detail.id.toString()}`))
      await libp2p.dial(maddrs) // dial the new peer
      console.log(`Successfully dialed peer (${evt.detail.id.toString()})`)
    } catch (err) {
      console.error(`Failed to dial peer (${evt.detail.id.toString()}):`, err)
    }
  })

The problem is that if you just dial the maddrs in the event the dial will fail for webrtc maddrs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedSeeking public contribution on this issueneed/analysisNeeds further analysis before proceeding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions