Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(light-js): add discovery of a peer #250

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions examples/light-js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
<div id="remote-peer-id"></div>

<label for="remote-multiaddr">Remote peer's multiaddr</label>
<input
id="remote-multiaddr"
type="text"
value="/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm"
/>
<input id="remote-multiaddr" type="text" value="" />
<button disabled id="dial" type="button">Dial</button>
<br />
<button disabled id="subscribe" type="button">Subscribe with Filter</button>
Expand All @@ -50,6 +46,10 @@
utf8ToBytes,
bytesToUtf8,
} from "https://unpkg.com/@waku/sdk@0.0.16/bundle/index.js";
import {
enrTree,
DnsNodeDiscovery,
} from "https://unpkg.com/@waku/dns-discovery@0.0.14/bundle/index.js";

const peerIdDiv = document.getElementById("peer-id");
const remotePeerIdDiv = document.getElementById("remote-peer-id");
Expand All @@ -74,6 +74,14 @@
div.innerHTML += "</ul>";
};

try {
await searchForPeer(statusDiv, remoteMultiAddrDiv);
} catch (e) {
console.log("Failed to find a peer", e);
remoteMultiAddrDiv.value =
"/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm";
}

statusDiv.innerHTML = "<p>Creating Waku node.</p>";
const node = await createLightNode();

Expand Down Expand Up @@ -130,6 +138,21 @@
console.log("Message sent!");
textInput.value = null;
};

async function searchForPeer(statusNode, multiaddrNode) {
statusDiv.innerHTML = "<p>Discovering peer</p>";

const dnsDiscovery = await DnsNodeDiscovery.dnsOverHttp();
const peersIterator = await dnsDiscovery.getNextPeer(
[enrTree["TEST"]],
{ lightPush: 1, filter: 1 }
);
const peerEnr = await peersIterator.next();
const ma = peerEnr.value.multiaddrs.map((v) => v.toString())[1];
const peerId = peerEnr.value.peerId.toString();

multiaddrNode.value = `${ma}/p2p/${peerId}`;
}
</script>
</body>
</html>