update noise examples

This commit is contained in:
Sasha 2023-05-16 01:09:26 +02:00
parent d7bb3016ff
commit a6af2a9ff2
No known key found for this signature in database
6 changed files with 1619 additions and 7970 deletions

View File

@ -1,7 +1,6 @@
import { createLightNode } from "@waku/create"; import { createLightNode } from "@waku/create";
import * as utils from "@waku/utils/bytes"; import * as utils from "@waku/utils/bytes";
import { waitForRemotePeer } from "@waku/core"; import { waitForRemotePeer } from "@waku/core";
import { Protocols } from "@waku/interfaces";
import * as noise from "@waku/noise"; import * as noise from "@waku/noise";
import protobuf from "protobufjs"; import protobuf from "protobufjs";
import QRCode from "qrcode"; import QRCode from "qrcode";
@ -25,17 +24,16 @@ async function main() {
try { try {
await node.start(); await node.start();
await waitForRemotePeer(node, [Protocols.Filter, Protocols.LightPush]); await waitForRemotePeer(node, ["filter", "lightpush"]);
ui.waku.connected(); ui.waku.connected();
const responder = getResponder(node);
const myStaticKey = noise.generateX25519KeyPair(); const myStaticKey = noise.generateX25519KeyPair();
const urlPairingInfo = getPairingInfoFromURL(); const urlPairingInfo = getPairingInfoFromURL();
const pairingObj = new noise.WakuPairing( const pairingObj = new noise.WakuPairing(
node.lightPush, node.lightPush,
responder, node.filter,
myStaticKey, myStaticKey,
urlPairingInfo || new noise.ResponderParameters() urlPairingInfo || new noise.ResponderParameters()
); );
@ -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) { async function scheduleHandshakeAuthConfirmation(pairingObj, ui) {
const authCode = await pairingObj.getAuthCode(); const authCode = await pairingObj.getAuthCode();
ui.handshake.connecting(); ui.handshake.connecting();

File diff suppressed because it is too large Load Diff

View File

@ -9,11 +9,10 @@
"start": "webpack-dev-server" "start": "webpack-dev-server"
}, },
"dependencies": { "dependencies": {
"@waku/core": "0.0.16", "@waku/core": "0.0.17",
"@waku/create": "0.0.12", "@waku/create": "0.0.13",
"@waku/interfaces": "0.0.11", "@waku/noise": "0.0.3-31510da",
"@waku/noise": "0.0.3", "@waku/utils": "0.0.5",
"@waku/utils": "0.0.4",
"protobufjs": "^7.1.2", "protobufjs": "^7.1.2",
"qrcode": "^1.5.1" "qrcode": "^1.5.1"
}, },

View File

@ -1,7 +1,6 @@
import { createLightNode } from "@waku/create"; import { createLightNode } from "@waku/create";
import * as utils from "@waku/utils/bytes"; import * as utils from "@waku/utils/bytes";
import { waitForRemotePeer } from "@waku/core"; import { waitForRemotePeer } from "@waku/core";
import { Protocols } from "@waku/interfaces";
import * as noise from "@waku/noise"; import * as noise from "@waku/noise";
import protobuf from "protobufjs"; import protobuf from "protobufjs";
import QRCode from "qrcode"; import QRCode from "qrcode";
@ -22,17 +21,16 @@ async function main() {
try { try {
await node.start(); await node.start();
await waitForRemotePeer(node, [Protocols.Filter, Protocols.LightPush]); await waitForRemotePeer(node, ["filter", "lightpush"]);
ui.waku.connected(); ui.waku.connected();
const responder = getResponder(node);
const myStaticKey = noise.generateX25519KeyPair(); const myStaticKey = noise.generateX25519KeyPair();
const urlPairingInfo = getPairingInfoFromURL(); const urlPairingInfo = getPairingInfoFromURL();
const pairingObj = new noise.WakuPairing( const pairingObj = new noise.WakuPairing(
node.lightPush, node.lightPush,
responder, node.filter,
myStaticKey, myStaticKey,
urlPairingInfo || new noise.ResponderParameters() urlPairingInfo || new noise.ResponderParameters()
); );
@ -212,59 +210,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) { async function scheduleHandshakeAuthConfirmation(pairingObj, ui) {
const authCode = await pairingObj.getAuthCode(); const authCode = await pairingObj.getAuthCode();
ui.handshake.connecting(); ui.handshake.connecting();

File diff suppressed because it is too large Load Diff

View File

@ -9,11 +9,10 @@
"start": "webpack-dev-server" "start": "webpack-dev-server"
}, },
"dependencies": { "dependencies": {
"@waku/core": "0.0.16", "@waku/core": "0.0.17",
"@waku/create": "0.0.12", "@waku/create": "0.0.13",
"@waku/interfaces": "0.0.11", "@waku/noise": "0.0.3-31510da",
"@waku/noise": "0.0.3", "@waku/utils": "0.0.5",
"@waku/utils": "0.0.4",
"protobufjs": "^7.1.2", "protobufjs": "^7.1.2",
"qrcode": "^1.5.1" "qrcode": "^1.5.1"
}, },