depot/packages/networking/ipfs-cluster/api/util.go

30 lines
542 B
Go
Raw Permalink Normal View History

2022-10-19 23:23:11 +03:00
package api
import (
peer "github.com/libp2p/go-libp2p/core/peer"
2022-10-19 23:23:11 +03:00
)
// PeersToStrings Encodes a list of peers.
func PeersToStrings(peers []peer.ID) []string {
strs := make([]string, len(peers))
for i, p := range peers {
if p != "" {
strs[i] = p.String()
2022-10-19 23:23:11 +03:00
}
}
return strs
}
// StringsToPeers decodes peer.IDs from strings.
func StringsToPeers(strs []string) []peer.ID {
peers := []peer.ID{}
for _, p := range strs {
pid, err := peer.Decode(p)
if err != nil {
continue
}
peers = append(peers, pid)
}
return peers
}