2023-07-07 15:51:15 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EncapsulatePeerID takes a peer.ID and adds a p2p component to all multiaddresses it receives
|
|
|
|
func EncapsulatePeerID(peerID peer.ID, addrs ...multiaddr.Multiaddr) []multiaddr.Multiaddr {
|
2024-01-12 17:40:27 +00:00
|
|
|
hostInfo, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", peerID.String()))
|
2023-07-07 15:51:15 +00:00
|
|
|
var result []multiaddr.Multiaddr
|
|
|
|
for _, addr := range addrs {
|
|
|
|
result = append(result, addr.Encapsulate(hostInfo))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2023-10-16 16:28:54 +00:00
|
|
|
|
2024-04-18 11:21:31 +00:00
|
|
|
func MultiAddrSetEquals(m1 map[string]multiaddr.Multiaddr, m2 map[string]multiaddr.Multiaddr) bool {
|
|
|
|
if len(m1) != len(m2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for k1, v1 := range m1 {
|
|
|
|
v2, ok := m2[k1]
|
|
|
|
if !ok || !v1.Equal(v2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func MultiAddrSet(addr ...multiaddr.Multiaddr) map[string]multiaddr.Multiaddr {
|
|
|
|
r := make(map[string]multiaddr.Multiaddr)
|
2023-10-16 16:28:54 +00:00
|
|
|
for _, a := range addr {
|
2024-04-18 11:21:31 +00:00
|
|
|
r[a.String()] = a
|
2023-10-16 16:28:54 +00:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|