I'm trying to get a PeerInfo from a libp2p.Host instance. I'm able to get a list of multiaddr.Multiaddrs 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)
}
}
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:
pstore.InfoFromP2pAddr()requires a multiaddr with ap2poripfscomponent in order to populate thePeerInfo.IDelement.That said, we could definitely make it easier to obtain a
PeerInfofrom aHost. I'll work on it ;-)