js-libp2p icon indicating copy to clipboard operation
js-libp2p copied to clipboard

TypeError: peer[0].getPeerId is not a function

Open AquiGorka opened this issue 3 months ago • 4 comments

I am aware of https://github.com/libp2p/js-libp2p/issues/2437, this is not the same issue as I'm trying to dial a node by its multiaddress.

  • Version:
"@libp2p/webrtc": "^5.2.24",
"@multiformats/multiaddr": "^13.0.1",
"libp2p": "^2.10.0"
  • Platform:

Darwin 24.3.0 Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:16 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6000 arm64

  • Subsystem:

WebRTC direct

Severity:

High or Medium depending on who you ask (someone that wants to connect nodes using webrtc-direct, things are not working)

Description:

I'm trying to connect a browser node to both a go-node and a nodejs node by dialing the multiaddresses using webrtc-direct as transport. In an attempt to figure out if this only happens with browsers I tried to connect the nodejs node to the go-node and the same error happened.

Steps to reproduce the error:

// go node
import (
	libwebrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc"
	golibp2p "github.com/libp2p/go-libp2p"
	"github.com/libp2p/go-libp2p/core/network"
	"github.com/libp2p/go-libp2p/core/peer"
)


opts := []golibp2p.Option{
	golibp2p.ListenAddrStrings("/ip4/0.0.0.0/udp/4444/webrtc-direct"),
	golibp2p.Identity(priv),
	golibp2p.DisableRelay(),
	golibp2p.Transport(libwebrtc.New),
}
n, err := golibp2p.New(opts...)
remoteInfo := peer.AddrInfo{ID: n.ID(), Addrs: n.Network().ListenAddresses()}
remoteAddrs, _ := peer.AddrInfoToP2pAddrs(&remoteInfo)
fmt.Println("p2p addr: ", remoteAddrs[0]) // something in line with: /ip4/0.0.0.0/udp/4444/webrtc-direct/certhash/<cert-hash>/p2p/<peer-id>
import { createLibp2p } from 'libp2p'
import { webRTCDirect } from '@libp2p/webrtc'
import { multiaddr } from '@multiformats/multiaddr'

const listener = await createLibp2p({
  addresses: {  listen: [ '/ip4/0.0.0.0/udp/4455/webrtc-direct' ] },
  transports: [ webRTCDirect() ]
})

await listener.start()

listener.dial(multiaddr("/ip4/127.0.0.1/udp/4444/webrtc-direct/certhash/<cert-hash>/p2p/<peer-id>)) // do notice the change from 0.0.0.0 -> 127.0.0.1

Both the browser and nodejs nodes throw the same error when using dial (and dialProtocol): TypeError: multiaddrs[0].getPeerId is not a function

Example code to dial taken from examples all around libp2p docs.

AquiGorka avatar Sep 16 '25 19:09 AquiGorka

Found what happened: getPeerId was removed by @achingbrain here https://github.com/multiformats/js-multiaddr/pull/427/files#diff-8201e49690f0d8a6e1217a7c0b0ac6d5af7aa63413c280ebc1fea1685ddd609aL247 and its usage updated here https://github.com/libp2p/js-libp2p/commit/b8ecade2a725d38d11dd8df888c5abb22e14f26b but that code is not in the latest version (I got v2.10.0 when installing latest but it still has getPeerId)?

Image

AquiGorka avatar Sep 16 '25 19:09 AquiGorka

@achingbrain any chance you could trigger a release with the latest code?

AquiGorka avatar Sep 16 '25 19:09 AquiGorka

It's not quite ready yet, please follow the release issue for progress.

To unblock yourself, please use @multiformats/[email protected] until [email protected] is released.

In general if you add deps to your project that are also used by libp2p, it's best to stick to the version libp2p uses, particularly if there's been a new major since the last libp2p release.

achingbrain avatar Sep 16 '25 20:09 achingbrain

Ahhh, I see, thank you for clarifying @achingbrain

And please don't take my comments the bad way, I am coming from a place where I had the assumption that the releases would be in sync - you've clarified that this is not the case.

Maybe it would be helpful to set peer dependencies for such adjacent libs? If anything, at least the ones you have control over the release cycle?

In this case I was following some examples and AI code that used multiaddr and I just went ahead and installed the libs expecting things would work - personally, I understand how PL, IPFS and libp2p moves so this is something I can work around but maybe others may get the wrong idea that things don't work? (specially since they may not dive into PRs and code like I do).

Anyhow, thanks for sharing the link to the release issue, I'm watching it now, and happy to to say 👋 .

AquiGorka avatar Sep 17 '25 11:09 AquiGorka