2021-06-16 13:14:22 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2024-10-04 11:10:19 +05:30
|
|
|
"errors"
|
|
|
|
|
2022-10-19 15:39:32 -04:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-03-13 20:37:28 -04:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2021-06-16 13:14:22 +03:00
|
|
|
)
|
|
|
|
|
2024-09-23 14:41:07 -07:00
|
|
|
type DialError struct {
|
|
|
|
Err error
|
|
|
|
PeerID peer.ID
|
|
|
|
}
|
|
|
|
|
2024-10-04 11:10:19 +05:30
|
|
|
// ErrNoPeersAvailable is emitted when no suitable peers are found for
|
|
|
|
// some protocol
|
|
|
|
var ErrNoPeersAvailable = errors.New("no suitable peers found")
|
|
|
|
|
2023-09-11 10:24:05 -04:00
|
|
|
// GetPeerID is used to extract the peerID from a multiaddress
|
2023-03-13 20:37:28 -04: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
|
|
|
|
}
|