mirror of
https://github.com/logos-messaging/logos-messaging-go.git
synced 2026-01-07 00:13:13 +00:00
This commit adds a public method that takes a url and returns a list of multiaddr. Once we better understand how to integrate it we can make it so that is passed as a config when initializing waku, this commit only provides the basic functionality.
44 lines
896 B
Go
44 lines
896 B
Go
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"
|
|
)
|
|
|
|
// RetrieveNodes returns a list of multiaddress given a url to a DNS discoverable
|
|
// ENR tree
|
|
func RetrieveNodes(url string) ([]string, error) {
|
|
var multiAddrs []string
|
|
|
|
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
|
|
}
|
|
|
|
func enodeToMultiAddr(node *enode.Node) (string, error) {
|
|
peerID, err := peer.IDFromPublicKey(&ECDSAPublicKey{node.Pubkey()})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", node.IP(), node.TCP(), peerID), nil
|
|
}
|