libp2p-test-plans/ping/go/compat/libp2p.v0.20.go

38 lines
865 B
Go
Raw Normal View History

2022-07-28 17:10:27 +02:00
//go:build v0.20 || v0.21
// +build v0.20 v0.21
package compat
import (
"context"
"fmt"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/config"
noise "github.com/libp2p/go-libp2p/p2p/security/noise"
tls "github.com/libp2p/go-libp2p/p2p/security/tls"
)
type PeerAddrInfo = peer.AddrInfo
func NewLibp2(ctx context.Context, secureChannel string, opts ...config.Option) (host.Host, error) {
security := getSecurityByName(secureChannel)
return libp2p.New(
append(opts, security)...,
)
}
func getSecurityByName(secureChannel string) libp2p.Option {
switch secureChannel {
case "noise":
return libp2p.Security(noise.ID, noise.New)
case "tls":
return libp2p.Security(tls.ID, tls.New)
}
panic(fmt.Sprintf("unknown secure channel: %s", secureChannel))
}