2021-06-16 10:14:22 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-03-14 00:37:28 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2021-06-16 10:14:22 +00:00
|
|
|
)
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
// GetPeerID is used to extract the peerID from a multiaddress
|
2023-03-14 00:37:28 +00:00
|
|
|
func GetPeerID(m multiaddr.Multiaddr) (peer.ID, error) {
|
|
|
|
peerIDStr, err := m.ValueForProtocol(multiaddr.P_P2P)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerID, err := peer.Decode(peerIDStr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return peerID, nil
|
|
|
|
}
|