I'm trying to get a PeerInfo
from a libp2p.Host
instance. I'm able to get a list of multiaddr.Multiaddr
s by calling Host.Addrs()
, but I'm unable to convert these into a valid peerstore.PeerInfo
.
Here is my attempt, which panics with the error panic: invalid p2p multiaddr
.
package main
import (
"log"
"github.com/SentimensRG/ctx"
"github.com/SentimensRG/ctx/sigctx"
libp2p "github.com/libp2p/go-libp2p"
ps "github.com/libp2p/go-libp2p-peerstore"
)
var c = ctx.AsContext(sigctx.New())
func main() {
h0, err := libp2p.New(c)
if err != nil {
panic(err)
}
addr0 := h0.Addrs()[1]
_, err := ps.InfoFromP2pAddr(addr0)
if err != nil {
panic(err)
}
}
peerstore.PeerInfo
from a libp2p.Host
?PeerInfo is a struct that encapsulates a peer ID and its multiaddrs. To build a PeerInfo from a Host easily, you can simply do the following:
pi := PeerInfo{
ID: host.ID(),
Addrs: host.Addrs(),
}
pstore.InfoFromP2pAddr()
requires a multiaddr with a p2p
or ipfs
component in order to populate the PeerInfo.ID
element.
That said, we could definitely make it easier to obtain a PeerInfo
from a Host
. I'll work on it ;-)