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

30 lines
546 B
Go
Raw Normal View History

2022-10-19 23:23:11 +03:00
package api
import (
peer "github.com/libp2p/go-libp2p-core/peer"
)
// 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] = peer.Encode(p)
}
}
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
}