2021-09-27 17:52:56 +00:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/dnsdisc"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2021-09-27 18:16:06 +00:00
|
|
|
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2021-09-27 17:52:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RetrieveNodes returns a list of multiaddress given a url to a DNS discoverable
|
|
|
|
// ENR tree
|
2021-09-27 18:16:06 +00:00
|
|
|
func RetrieveNodes(url string) ([]ma.Multiaddr, error) {
|
|
|
|
var multiAddrs []ma.Multiaddr
|
2021-09-27 17:52:56 +00:00
|
|
|
|
|
|
|
client := dnsdisc.NewClient(dnsdisc.Config{})
|
|
|
|
|
|
|
|
tree, err := client.SyncTree(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range tree.Nodes() {
|
|
|
|
m, err := enodeToMultiAddr(node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
multiAddrs = append(multiAddrs, m)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return multiAddrs, nil
|
|
|
|
}
|
|
|
|
|
2021-09-27 18:16:06 +00:00
|
|
|
func enodeToMultiAddr(node *enode.Node) (ma.Multiaddr, error) {
|
2021-09-27 17:52:56 +00:00
|
|
|
peerID, err := peer.IDFromPublicKey(&ECDSAPublicKey{node.Pubkey()})
|
|
|
|
if err != nil {
|
2021-09-27 18:16:06 +00:00
|
|
|
return nil, err
|
2021-09-27 17:52:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 18:16:06 +00:00
|
|
|
return ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", node.IP(), node.TCP(), peerID))
|
2021-09-27 17:52:56 +00:00
|
|
|
}
|