Skip to content

Commit

Permalink
chore: use filter as IReceiver for noise (#239)
Browse files Browse the repository at this point in the history
* update noise examples
* use latest waku
  • Loading branch information
weboko committed Jun 1, 2023
1 parent d7bb301 commit cb8fbe8
Show file tree
Hide file tree
Showing 6 changed files with 5,086 additions and 10,863 deletions.
59 changes: 2 additions & 57 deletions examples/noise-js/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createLightNode } from "@waku/create";
import * as utils from "@waku/utils/bytes";
import { waitForRemotePeer } from "@waku/core";
import { Protocols } from "@waku/interfaces";
import * as noise from "@waku/noise";
import protobuf from "protobufjs";
import QRCode from "qrcode";
Expand All @@ -25,17 +24,16 @@ async function main() {

try {
await node.start();
await waitForRemotePeer(node, [Protocols.Filter, Protocols.LightPush]);
await waitForRemotePeer(node, ["filter", "lightpush"]);

ui.waku.connected();

const responder = getResponder(node);
const myStaticKey = noise.generateX25519KeyPair();
const urlPairingInfo = getPairingInfoFromURL();

const pairingObj = new noise.WakuPairing(
node.lightPush,
responder,
node.filter,
myStaticKey,
urlPairingInfo || new noise.ResponderParameters()
);
Expand Down Expand Up @@ -117,59 +115,6 @@ function getPairingInfoFromURL() {
);
}

function getResponder(node) {
const msgQueue = new Array();
const subscriptions = new Map();
const intervals = new Map();

const responder = {
async subscribe(decoder) {
const subscription = await node.filter.subscribe(
[decoder],
(wakuMessage) => {
msgQueue.push(wakuMessage);
}
);
subscriptions.set(decoder.contentTopic, subscription);
},
async nextMessage(contentTopic) {
if (msgQueue.length != 0) {
const oldestMsg = msgQueue.shift();
if (oldestMsg.contentTopic === contentTopic) {
return oldestMsg;
}
}

return new Promise((resolve) => {
const interval = setInterval(() => {
if (msgQueue.length != 0) {
clearInterval(interval);
const oldestMsg = msgQueue.shift();
if (oldestMsg.contentTopic === contentTopic) {
resolve(oldestMsg);
}
}
}, 100);
intervals.set(contentTopic, interval);
});
},
async stop(contentTopic) {
if (intervals.has(contentTopic)) {
clearInterval(intervals.get(contentTopic));
intervals.delete(contentTopic);
}
if (subscriptions.has(contentTopic)) {
await subscriptions.get(contentTopic)();
subscriptions.delete(contentTopic);
} else {
console.log("Subscriptipon doesnt exist");
}
},
};

return responder;
}

async function scheduleHandshakeAuthConfirmation(pairingObj, ui) {
const authCode = await pairingObj.getAuthCode();
ui.handshake.connecting();
Expand Down
Loading

0 comments on commit cb8fbe8

Please sign in to comment.