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

`webrtc` transport breaks compilation for WASM even if it not specified in transports

Open v1rtl opened this issue 5 months ago • 2 comments

Currently if you try to compile the most basic libp2p host with 0 transports and no listeners like this:

package main

import (
	"log"

	"github.com/libp2p/go-libp2p"
	"github.com/libp2p/go-libp2p/core/crypto"
	"github.com/libp2p/go-libp2p/p2p/security/noise"
)

func main() {
	priv, _, err := crypto.GenerateKeyPair(
		crypto.Ed25519,
		-1,
	)
	if err != nil {
		panic(err)
	}

	if err != nil {
		panic(err)
	}
	h2, err := libp2p.NewWithoutDefaults(
		libp2p.Identity(priv),
		libp2p.NoListenAddrs,
		libp2p.Security(noise.ID, noise.New),
		libp2p.NoTransports,
	)
	if err != nil {
		panic(err)
	}
	defer h2.Close()
}

it will fail during compilation:

GOOS=js GOARCH=wasm go build
# github.com/libp2p/go-libp2p/p2p/transport/webrtc
/home/v1rtl/go/pkg/mod/github.com/libp2p/[email protected]/p2p/transport/webrtc/connection.go:109:12: pc.SCTP().OnClose undefined (type *webrtc.SCTPTransport has no field or method OnClose)
/home/v1rtl/go/pkg/mod/github.com/libp2p/[email protected]/p2p/transport/webrtc/listener.go:68:35: config.Certificates undefined (type webrtc.Configuration has no field or method Certificates)
/home/v1rtl/go/pkg/mod/github.com/libp2p/[email protected]/p2p/transport/webrtc/listener.go:199:40: unknown field LoggerFactory in struct literal of type webrtc.SettingEngine
/home/v1rtl/go/pkg/mod/github.com/libp2p/[email protected]/p2p/transport/webrtc/listener.go:200:16: settingEngine.SetAnsweringDTLSRole undefined (type webrtc.SettingEngine has no field or method SetAnsweringDTLSRole)
// ...

my guess is that it fails because webrtc transport is imported in multiple places (or maybe hust here) which breaks compilation:

/home/v1rtl/Coding/forks/go-libp2p/p2p/protocol/autonatv2/client.go:17: libp2pwebrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc"

v1rtl avatar Aug 05 '25 20:08 v1rtl

I could find some things that may be useful for you @sukunrt.

Upstream

https://github.com/libp2p/go-libp2p/blob/72894e318a0b14c35b7ea727d49483c7bb587d9f/p2p/transport/webrtc/connection.go#L109

This line assumes all platforms including WASM have the same implementation of SCTPTransport, DTLSTransport and SettingEngine.

  • In the case of SCTPTransport.

Non JS targets appear to have a full implementation of the expected interface.

While JS one only supports a small subsets of operations:

func (r *SCTPTransport) JSValue() js.Value
func (r *SCTPTransport) Transport() *DTLSTransport

libp2p

By guessing, I think it could be useful to check the default code included for libp2p clients. Maybe webrtc is included because the library things user may use of this transport to connect to other peers from within the browser.

Not limited to above sample

  • Using New also fails.
h2, err := libp2p.New(
	libp2p.Identity(priv),
	libp2p.NoListenAddrs,
	libp2p.Security(noise.ID, noise.New),
	libp2p.NoTransports,
)
  • Using only the identity also fails.
h2, err := libp2p.New(libp2p.Identity(priv))

Ideas.

Since webrtc is a must have since there are peers waiting for connections using this transport. Maybe the solution should not be disabled webrtc on WASM but rather target upstream?

@sukunrt Let me know how can I help.

shoriwe avatar Sep 01 '25 22:09 shoriwe

There's a draft PR for constructing a host with fx options, makes the binary smaller and omits unused packages

v1rtl avatar Sep 01 '25 23:09 v1rtl