go-libp2p-core/host/helpers.go

34 lines
1005 B
Go
Raw Normal View History

package host
import (
"errors"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing"
)
// InfoFromHost returns a peer.AddrInfo struct with the Host's ID and all of its Addrs.
func InfoFromHost(h Host) *peer.AddrInfo {
return &peer.AddrInfo{
ID: h.ID(),
Addrs: h.Addrs(),
}
}
// RoutingStateFromHost returns a routing.RoutingState record that contains the Host's
// ID and all of its listen Addrs.
func RoutingStateFromHost(h Host) *routing.RoutingState {
return routing.RoutingStateFromAddrInfo(InfoFromHost(h))
}
2019-11-09 00:12:14 +08:00
// SignedRoutingStateFromHost wraps a routing.RoutingState record in a
// SignedEnvelope, signed with the Host's private key.
func SignedRoutingStateFromHost(h Host) (*crypto.SignedEnvelope, error) {
privKey := h.Peerstore().PrivKey(h.ID())
if privKey == nil {
return nil, errors.New("unable to find host's private key in peerstore")
}
return RoutingStateFromHost(h).ToSignedEnvelope(privKey)
}