2022-06-20 16:48:30 +10:00
|
|
|
import { PeerId } from "@libp2p/interface-peer-id";
|
2022-12-02 16:38:30 +11:00
|
|
|
import {
|
|
|
|
|
createDecoder,
|
|
|
|
|
createEncoder,
|
|
|
|
|
DecodedMessage,
|
|
|
|
|
DefaultPubSubTopic,
|
|
|
|
|
waitForRemotePeer,
|
|
|
|
|
} from "@waku/core";
|
2022-12-06 13:18:32 +11:00
|
|
|
import { createRelayNode } from "@waku/create";
|
|
|
|
|
import type { RelayNode } from "@waku/interfaces";
|
2022-11-04 11:45:15 +11:00
|
|
|
import { Protocols } from "@waku/interfaces";
|
2022-11-01 21:31:53 +11:00
|
|
|
import {
|
2022-12-05 15:14:17 +11:00
|
|
|
createDecoder as createEciesDecoder,
|
|
|
|
|
createEncoder as createEciesEncoder,
|
2022-11-04 11:45:15 +11:00
|
|
|
generatePrivateKey,
|
|
|
|
|
getPublicKey,
|
2022-12-05 15:14:17 +11:00
|
|
|
} from "@waku/message-encryption/ecies";
|
|
|
|
|
import {
|
|
|
|
|
createDecoder as createSymDecoder,
|
|
|
|
|
createEncoder as createSymEncoder,
|
|
|
|
|
generateSymmetricKey,
|
|
|
|
|
} from "@waku/message-encryption/symmetric";
|
2023-03-14 10:10:38 +05:30
|
|
|
import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes";
|
2022-02-04 14:12:00 +11:00
|
|
|
import { expect } from "chai";
|
|
|
|
|
import debug from "debug";
|
2021-03-19 14:58:06 +11:00
|
|
|
|
2021-04-15 13:44:00 +10:00
|
|
|
import {
|
2023-02-16 13:27:39 +11:00
|
|
|
base64ToUtf8,
|
2022-12-02 15:43:46 +11:00
|
|
|
delay,
|
2021-04-15 13:44:00 +10:00
|
|
|
makeLogFileName,
|
2022-09-11 00:57:10 +10:00
|
|
|
MessageRpcResponse,
|
2021-04-15 13:44:00 +10:00
|
|
|
NOISE_KEY_1,
|
|
|
|
|
NOISE_KEY_2,
|
2022-07-25 16:51:33 +10:00
|
|
|
NOISE_KEY_3,
|
2022-04-01 12:19:51 +11:00
|
|
|
Nwaku,
|
2022-12-02 15:43:46 +11:00
|
|
|
} from "../src/index.js";
|
2021-03-19 14:58:06 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const log = debug("waku:test");
|
2021-05-10 20:18:26 +10:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const TestContentTopic = "/test/1/waku-relay/utf8";
|
2023-02-02 11:37:28 +05:30
|
|
|
const TestEncoder = createEncoder({ contentTopic: TestContentTopic });
|
2022-11-23 16:25:50 +11:00
|
|
|
const TestDecoder = createDecoder(TestContentTopic);
|
2021-07-28 11:19:24 +10:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
describe("Waku Relay [node only]", () => {
|
2021-09-24 17:15:46 +10:00
|
|
|
// Node needed as we don't have a way to connect 2 js waku
|
|
|
|
|
// nodes in the browser yet
|
2022-02-04 14:12:00 +11:00
|
|
|
describe("2 js nodes", () => {
|
2021-05-20 15:48:05 +10:00
|
|
|
afterEach(function () {
|
2022-02-04 14:12:00 +11:00
|
|
|
if (this.currentTest?.state === "failed") {
|
2021-05-20 15:48:05 +10:00
|
|
|
console.log(`Test failed, log file name is ${makeLogFileName(this)}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-03-19 16:07:56 +11:00
|
|
|
|
2022-12-06 13:18:32 +11:00
|
|
|
let waku1: RelayNode;
|
|
|
|
|
let waku2: RelayNode;
|
2021-05-20 15:48:05 +10:00
|
|
|
beforeEach(async function () {
|
2021-09-01 15:49:46 +10:00
|
|
|
this.timeout(10000);
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
log("Starting JS Waku instances");
|
2021-05-20 15:48:05 +10:00
|
|
|
[waku1, waku2] = await Promise.all([
|
2022-12-06 13:18:32 +11:00
|
|
|
createRelayNode({ staticNoiseKey: NOISE_KEY_1 }).then((waku) =>
|
2022-06-23 14:03:52 +10:00
|
|
|
waku.start().then(() => waku)
|
|
|
|
|
),
|
2022-12-06 13:18:32 +11:00
|
|
|
createRelayNode({
|
2021-05-20 15:48:05 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_2,
|
2022-02-04 14:12:00 +11:00
|
|
|
libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } },
|
2022-06-23 14:03:52 +10:00
|
|
|
}).then((waku) => waku.start().then(() => waku)),
|
2021-05-20 15:48:05 +10:00
|
|
|
]);
|
2021-09-01 15:49:46 +10:00
|
|
|
log("Instances started, adding waku2 to waku1's address book");
|
2022-11-16 20:30:48 +11:00
|
|
|
await waku1.libp2p.peerStore.addressBook.set(
|
2022-06-20 16:48:30 +10:00
|
|
|
waku2.libp2p.peerId,
|
|
|
|
|
waku2.libp2p.getMultiaddrs()
|
|
|
|
|
);
|
2022-11-16 20:30:48 +11:00
|
|
|
await waku1.dial(waku2.libp2p.peerId);
|
2021-05-20 15:48:05 +10:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
log("Wait for mutual pubsub subscription");
|
2021-05-20 15:48:05 +10:00
|
|
|
await Promise.all([
|
2022-07-25 12:33:08 +10:00
|
|
|
waitForRemotePeer(waku1, [Protocols.Relay]),
|
|
|
|
|
waitForRemotePeer(waku2, [Protocols.Relay]),
|
2021-05-20 15:48:05 +10:00
|
|
|
]);
|
2022-02-04 14:12:00 +11:00
|
|
|
log("before each hook done");
|
2021-05-20 15:48:05 +10:00
|
|
|
});
|
2021-03-19 14:58:06 +11:00
|
|
|
|
2021-05-20 15:48:05 +10:00
|
|
|
afterEach(async function () {
|
2022-01-31 15:30:49 +11:00
|
|
|
!!waku1 &&
|
2022-02-04 14:12:00 +11:00
|
|
|
waku1.stop().catch((e) => console.log("Waku failed to stop", e));
|
2022-01-31 15:30:49 +11:00
|
|
|
!!waku2 &&
|
2022-02-04 14:12:00 +11:00
|
|
|
waku2.stop().catch((e) => console.log("Waku failed to stop", e));
|
2021-05-20 15:48:05 +10:00
|
|
|
});
|
2021-03-19 14:58:06 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("Subscribe", async function () {
|
|
|
|
|
log("Getting subscribers");
|
2022-07-25 16:33:23 +10:00
|
|
|
const subscribers1 = waku1.libp2p.pubsub
|
|
|
|
|
.getSubscribers(DefaultPubSubTopic)
|
|
|
|
|
.map((p) => p.toString());
|
|
|
|
|
const subscribers2 = waku2.libp2p.pubsub
|
|
|
|
|
.getSubscribers(DefaultPubSubTopic)
|
|
|
|
|
.map((p) => p.toString());
|
2021-03-19 14:58:06 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
log("Asserting mutual subscription");
|
2022-06-14 16:12:37 +10:00
|
|
|
expect(subscribers1).to.contain(waku2.libp2p.peerId.toString());
|
|
|
|
|
expect(subscribers2).to.contain(waku1.libp2p.peerId.toString());
|
2021-05-20 15:48:05 +10:00
|
|
|
});
|
2021-03-23 14:11:29 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("Register correct protocols", async function () {
|
2023-02-09 13:21:03 +11:00
|
|
|
const protocols = waku1.libp2p.getProtocols();
|
2021-03-23 14:11:29 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
expect(protocols).to.contain("/vac/waku/relay/2.0.0");
|
2021-05-20 15:48:05 +10:00
|
|
|
expect(protocols.findIndex((value) => value.match(/sub/))).to.eq(-1);
|
2021-05-10 12:27:20 +10:00
|
|
|
});
|
2021-03-23 14:11:29 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("Publish", async function () {
|
2021-05-20 15:48:05 +10:00
|
|
|
this.timeout(10000);
|
2021-03-23 14:11:29 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const messageText = "JS to JS communication works";
|
|
|
|
|
const messageTimestamp = new Date("1995-12-17T03:24:00");
|
2022-09-19 13:50:29 +10:00
|
|
|
const message = {
|
|
|
|
|
payload: utf8ToBytes(messageText),
|
|
|
|
|
timestamp: messageTimestamp,
|
|
|
|
|
};
|
2021-03-23 14:11:29 +11:00
|
|
|
|
2022-11-04 14:01:30 +11:00
|
|
|
const receivedMsgPromise: Promise<DecodedMessage> = new Promise(
|
|
|
|
|
(resolve) => {
|
2023-03-31 03:17:41 +02:00
|
|
|
waku2.relay.subscribe([TestDecoder], resolve);
|
2022-11-04 14:01:30 +11:00
|
|
|
}
|
|
|
|
|
);
|
2021-05-10 14:54:08 +10:00
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
await waku1.relay.send(TestEncoder, message);
|
2021-05-10 14:54:08 +10:00
|
|
|
|
2021-05-20 15:48:05 +10:00
|
|
|
const receivedMsg = await receivedMsgPromise;
|
2021-05-10 14:54:08 +10:00
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
expect(receivedMsg.contentTopic).to.eq(TestContentTopic);
|
2023-02-24 23:22:04 +11:00
|
|
|
expect(bytesToUtf8(receivedMsg.payload)).to.eq(messageText);
|
2021-05-28 16:00:34 +10:00
|
|
|
expect(receivedMsg.timestamp?.valueOf()).to.eq(
|
|
|
|
|
messageTimestamp.valueOf()
|
|
|
|
|
);
|
2021-05-10 14:54:08 +10:00
|
|
|
});
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("Filter on content topics", async function () {
|
2021-05-20 15:48:05 +10:00
|
|
|
this.timeout(10000);
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const fooMessageText = "Published on content topic foo";
|
|
|
|
|
const barMessageText = "Published on content topic bar";
|
2021-05-20 15:48:05 +10:00
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
const fooContentTopic = "foo";
|
|
|
|
|
const barContentTopic = "bar";
|
|
|
|
|
|
2023-02-02 11:37:28 +05:30
|
|
|
const fooEncoder = createEncoder({ contentTopic: fooContentTopic });
|
|
|
|
|
const barEncoder = createEncoder({ contentTopic: barContentTopic });
|
2022-09-19 13:50:29 +10:00
|
|
|
|
2022-11-23 16:25:50 +11:00
|
|
|
const fooDecoder = createDecoder(fooContentTopic);
|
|
|
|
|
const barDecoder = createDecoder(barContentTopic);
|
2021-05-20 15:48:05 +10:00
|
|
|
|
2022-11-04 14:01:30 +11:00
|
|
|
const fooMessages: DecodedMessage[] = [];
|
2023-03-31 03:17:41 +02:00
|
|
|
waku2.relay.subscribe([fooDecoder], (msg) => {
|
2022-09-19 13:50:29 +10:00
|
|
|
fooMessages.push(msg);
|
2021-05-20 15:48:05 +10:00
|
|
|
});
|
|
|
|
|
|
2022-11-04 14:01:30 +11:00
|
|
|
const barMessages: DecodedMessage[] = [];
|
2023-03-31 03:17:41 +02:00
|
|
|
waku2.relay.subscribe([barDecoder], (msg) => {
|
2022-09-19 13:50:29 +10:00
|
|
|
barMessages.push(msg);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await waku1.relay.send(barEncoder, {
|
|
|
|
|
payload: utf8ToBytes(barMessageText),
|
|
|
|
|
});
|
|
|
|
|
await waku1.relay.send(fooEncoder, {
|
|
|
|
|
payload: utf8ToBytes(fooMessageText),
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-19 16:20:33 +10:00
|
|
|
while (!fooMessages.length && !barMessages.length) {
|
|
|
|
|
await delay(100);
|
|
|
|
|
}
|
2022-09-19 13:50:29 +10:00
|
|
|
|
|
|
|
|
expect(fooMessages[0].contentTopic).to.eq(fooContentTopic);
|
2023-02-24 23:22:04 +11:00
|
|
|
expect(bytesToUtf8(fooMessages[0].payload)).to.eq(fooMessageText);
|
2022-09-19 13:50:29 +10:00
|
|
|
|
|
|
|
|
expect(barMessages[0].contentTopic).to.eq(barContentTopic);
|
2023-02-24 23:22:04 +11:00
|
|
|
expect(bytesToUtf8(barMessages[0].payload)).to.eq(barMessageText);
|
2022-09-19 13:50:29 +10:00
|
|
|
|
|
|
|
|
expect(fooMessages.length).to.eq(1);
|
|
|
|
|
expect(barMessages.length).to.eq(1);
|
2021-05-20 15:48:05 +10:00
|
|
|
});
|
2021-06-16 14:29:50 +10:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("Decrypt messages", async function () {
|
2022-01-17 14:11:05 +11:00
|
|
|
this.timeout(10000);
|
|
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
const asymText = "This message is encrypted using asymmetric";
|
|
|
|
|
const asymTopic = "/test/1/asymmetric/proto";
|
|
|
|
|
const symText = "This message is encrypted using symmetric encryption";
|
|
|
|
|
const symTopic = "/test/1/symmetric/proto";
|
2022-01-17 14:11:05 +11:00
|
|
|
|
|
|
|
|
const privateKey = generatePrivateKey();
|
|
|
|
|
const symKey = generateSymmetricKey();
|
|
|
|
|
const publicKey = getPublicKey(privateKey);
|
|
|
|
|
|
2023-02-02 11:37:28 +05:30
|
|
|
const eciesEncoder = createEciesEncoder({
|
|
|
|
|
contentTopic: asymTopic,
|
|
|
|
|
publicKey,
|
|
|
|
|
});
|
|
|
|
|
const symEncoder = createSymEncoder({
|
|
|
|
|
contentTopic: symTopic,
|
|
|
|
|
symKey,
|
|
|
|
|
});
|
2022-01-17 14:11:05 +11:00
|
|
|
|
2022-12-05 15:14:17 +11:00
|
|
|
const eciesDecoder = createEciesDecoder(asymTopic, privateKey);
|
2022-11-23 16:25:50 +11:00
|
|
|
const symDecoder = createSymDecoder(symTopic, symKey);
|
2022-01-17 14:11:05 +11:00
|
|
|
|
2022-11-04 14:01:30 +11:00
|
|
|
const msgs: DecodedMessage[] = [];
|
2023-03-31 03:17:41 +02:00
|
|
|
waku2.relay.subscribe([eciesDecoder], (wakuMsg) => {
|
2022-09-19 13:50:29 +10:00
|
|
|
msgs.push(wakuMsg);
|
|
|
|
|
});
|
2023-03-31 03:17:41 +02:00
|
|
|
waku2.relay.subscribe([symDecoder], (wakuMsg) => {
|
2022-01-17 14:11:05 +11:00
|
|
|
msgs.push(wakuMsg);
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-05 15:14:17 +11:00
|
|
|
await waku1.relay.send(eciesEncoder, { payload: utf8ToBytes(asymText) });
|
2022-03-06 23:20:59 +11:00
|
|
|
await delay(200);
|
2022-09-19 13:50:29 +10:00
|
|
|
await waku1.relay.send(symEncoder, { payload: utf8ToBytes(symText) });
|
2022-01-17 14:11:05 +11:00
|
|
|
|
|
|
|
|
while (msgs.length < 2) {
|
|
|
|
|
await delay(200);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
expect(msgs[0].contentTopic).to.eq(asymTopic);
|
|
|
|
|
expect(bytesToUtf8(msgs[0].payload!)).to.eq(asymText);
|
|
|
|
|
expect(msgs[1].contentTopic).to.eq(symTopic);
|
|
|
|
|
expect(bytesToUtf8(msgs[1].payload!)).to.eq(symText);
|
2022-01-17 14:11:05 +11:00
|
|
|
});
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("Delete observer", async function () {
|
2021-06-16 14:29:50 +10:00
|
|
|
this.timeout(10000);
|
|
|
|
|
|
|
|
|
|
const messageText =
|
2022-02-04 14:12:00 +11:00
|
|
|
"Published on content topic with added then deleted observer";
|
2022-09-19 13:50:29 +10:00
|
|
|
|
|
|
|
|
const contentTopic = "added-then-deleted-observer";
|
2021-06-16 14:29:50 +10:00
|
|
|
|
|
|
|
|
// The promise **fails** if we receive a message on this observer.
|
2022-11-04 14:01:30 +11:00
|
|
|
const receivedMsgPromise: Promise<DecodedMessage> = new Promise(
|
2021-06-16 14:29:50 +10:00
|
|
|
(resolve, reject) => {
|
2023-03-31 03:17:41 +02:00
|
|
|
const deleteObserver = waku2.relay.subscribe(
|
|
|
|
|
[createDecoder(contentTopic)],
|
2022-09-19 13:50:29 +10:00
|
|
|
reject
|
2023-03-31 03:17:41 +02:00
|
|
|
) as () => void;
|
2022-09-19 12:21:29 +10:00
|
|
|
deleteObserver();
|
2021-06-16 14:29:50 +10:00
|
|
|
setTimeout(resolve, 500);
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-02-02 11:37:28 +05:30
|
|
|
await waku1.relay.send(createEncoder({ contentTopic }), {
|
2022-09-19 13:50:29 +10:00
|
|
|
payload: utf8ToBytes(messageText),
|
|
|
|
|
});
|
2021-06-16 14:29:50 +10:00
|
|
|
|
|
|
|
|
await receivedMsgPromise;
|
|
|
|
|
// If it does not throw then we are good.
|
|
|
|
|
});
|
2021-05-10 14:54:08 +10:00
|
|
|
});
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
describe("Custom pubsub topic", () => {
|
2022-12-06 13:18:32 +11:00
|
|
|
let waku1: RelayNode;
|
|
|
|
|
let waku2: RelayNode;
|
|
|
|
|
let waku3: RelayNode;
|
2022-07-25 16:51:33 +10:00
|
|
|
afterEach(async function () {
|
|
|
|
|
!!waku1 &&
|
|
|
|
|
waku1.stop().catch((e) => console.log("Waku failed to stop", e));
|
|
|
|
|
!!waku2 &&
|
|
|
|
|
waku2.stop().catch((e) => console.log("Waku failed to stop", e));
|
|
|
|
|
!!waku3 &&
|
|
|
|
|
waku3.stop().catch((e) => console.log("Waku failed to stop", e));
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("Publish", async function () {
|
2021-06-09 12:25:56 +10:00
|
|
|
this.timeout(10000);
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const pubSubTopic = "/some/pubsub/topic";
|
2021-06-09 12:25:56 +10:00
|
|
|
|
|
|
|
|
// 1 and 2 uses a custom pubsub
|
2022-07-25 16:51:33 +10:00
|
|
|
// 3 uses the default pubsub
|
|
|
|
|
[waku1, waku2, waku3] = await Promise.all([
|
2022-12-06 13:18:32 +11:00
|
|
|
createRelayNode({
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic: pubSubTopic,
|
2021-06-09 12:25:56 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
2022-06-23 14:03:52 +10:00
|
|
|
}).then((waku) => waku.start().then(() => waku)),
|
2022-12-06 13:18:32 +11:00
|
|
|
createRelayNode({
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic: pubSubTopic,
|
2021-06-09 12:25:56 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_2,
|
2022-02-04 14:12:00 +11:00
|
|
|
libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } },
|
2022-06-23 14:03:52 +10:00
|
|
|
}).then((waku) => waku.start().then(() => waku)),
|
2022-12-06 13:18:32 +11:00
|
|
|
createRelayNode({
|
2022-07-25 16:51:33 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_3,
|
2022-06-23 14:03:52 +10:00
|
|
|
}).then((waku) => waku.start().then(() => waku)),
|
2021-06-09 12:25:56 +10:00
|
|
|
]);
|
|
|
|
|
|
2022-11-16 20:30:48 +11:00
|
|
|
await waku1.libp2p.peerStore.addressBook.set(
|
2022-06-20 16:48:30 +10:00
|
|
|
waku2.libp2p.peerId,
|
|
|
|
|
waku2.libp2p.getMultiaddrs()
|
|
|
|
|
);
|
2022-11-16 20:30:48 +11:00
|
|
|
await waku3.libp2p.peerStore.addressBook.set(
|
2022-06-20 16:48:30 +10:00
|
|
|
waku2.libp2p.peerId,
|
|
|
|
|
waku2.libp2p.getMultiaddrs()
|
|
|
|
|
);
|
2022-11-16 20:30:48 +11:00
|
|
|
await Promise.all([
|
|
|
|
|
waku1.dial(waku2.libp2p.peerId),
|
|
|
|
|
waku3.dial(waku2.libp2p.peerId),
|
|
|
|
|
]);
|
2021-06-09 12:25:56 +10:00
|
|
|
|
|
|
|
|
await Promise.all([
|
2022-07-25 12:33:08 +10:00
|
|
|
waitForRemotePeer(waku1, [Protocols.Relay]),
|
|
|
|
|
waitForRemotePeer(waku2, [Protocols.Relay]),
|
2021-06-09 12:25:56 +10:00
|
|
|
]);
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const messageText = "Communicating using a custom pubsub topic";
|
2021-06-09 12:25:56 +10:00
|
|
|
|
2022-11-04 14:01:30 +11:00
|
|
|
const waku2ReceivedMsgPromise: Promise<DecodedMessage> = new Promise(
|
2021-06-09 12:25:56 +10:00
|
|
|
(resolve) => {
|
2023-03-31 03:17:41 +02:00
|
|
|
waku2.relay.subscribe([TestDecoder], resolve);
|
2021-06-09 12:25:56 +10:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// The promise **fails** if we receive a message on the default
|
|
|
|
|
// pubsub topic.
|
2022-11-04 14:01:30 +11:00
|
|
|
const waku3NoMsgPromise: Promise<DecodedMessage> = new Promise(
|
2021-06-09 12:25:56 +10:00
|
|
|
(resolve, reject) => {
|
2023-03-31 03:17:41 +02:00
|
|
|
waku3.relay.subscribe([TestDecoder], reject);
|
2021-06-09 12:25:56 +10:00
|
|
|
setTimeout(resolve, 1000);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
await waku1.relay.send(TestEncoder, {
|
|
|
|
|
payload: utf8ToBytes(messageText),
|
|
|
|
|
});
|
2021-06-09 12:25:56 +10:00
|
|
|
|
|
|
|
|
const waku2ReceivedMsg = await waku2ReceivedMsgPromise;
|
|
|
|
|
await waku3NoMsgPromise;
|
|
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
expect(bytesToUtf8(waku2ReceivedMsg.payload!)).to.eq(messageText);
|
2023-03-10 16:43:21 +11:00
|
|
|
expect(waku2ReceivedMsg.pubSubTopic).to.eq(pubSubTopic);
|
2021-06-09 12:25:56 +10:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
describe("Interop: nwaku", function () {
|
2022-12-06 13:18:32 +11:00
|
|
|
let waku: RelayNode;
|
2022-04-01 12:19:51 +11:00
|
|
|
let nwaku: Nwaku;
|
2021-03-23 11:14:51 +11:00
|
|
|
|
2022-01-20 13:00:58 +11:00
|
|
|
beforeEach(async function () {
|
|
|
|
|
this.timeout(30_000);
|
2022-12-06 13:18:32 +11:00
|
|
|
waku = await createRelayNode({
|
2022-01-20 13:00:58 +11:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
2021-03-23 11:14:51 +11:00
|
|
|
});
|
2022-06-23 14:03:52 +10:00
|
|
|
await waku.start();
|
2021-03-23 11:14:51 +11:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
nwaku = new Nwaku(this.test?.ctx?.currentTest?.title + "");
|
2022-12-07 11:35:30 +05:30
|
|
|
await nwaku.start({ relay: true });
|
2021-03-19 14:58:06 +11:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
2022-07-25 12:33:08 +10:00
|
|
|
await waitForRemotePeer(waku, [Protocols.Relay]);
|
2021-03-19 15:03:31 +11:00
|
|
|
});
|
2021-03-19 14:58:06 +11:00
|
|
|
|
2022-01-20 13:00:58 +11:00
|
|
|
afterEach(async function () {
|
2023-04-17 10:29:36 +05:30
|
|
|
!!nwaku &&
|
|
|
|
|
nwaku.stop().catch((e) => console.log("Nwaku failed to stop", e));
|
2022-02-04 14:12:00 +11:00
|
|
|
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
|
2022-01-20 13:00:58 +11:00
|
|
|
});
|
2021-03-23 11:14:51 +11:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
it("nwaku subscribes", async function () {
|
2022-06-20 16:48:30 +10:00
|
|
|
let subscribers: PeerId[] = [];
|
2021-03-23 11:14:51 +11:00
|
|
|
|
2022-01-20 13:00:58 +11:00
|
|
|
while (subscribers.length === 0) {
|
|
|
|
|
await delay(200);
|
|
|
|
|
subscribers = waku.libp2p.pubsub.getSubscribers(DefaultPubSubTopic);
|
|
|
|
|
}
|
2021-04-15 14:28:18 +10:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
const nimPeerId = await nwaku.getPeerId();
|
2022-07-25 16:53:46 +10:00
|
|
|
expect(subscribers.map((p) => p.toString())).to.contain(
|
|
|
|
|
nimPeerId.toString()
|
|
|
|
|
);
|
2022-01-20 13:00:58 +11:00
|
|
|
});
|
2021-03-23 11:14:51 +11:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
it("Publishes to nwaku", async function () {
|
2022-01-20 13:00:58 +11:00
|
|
|
this.timeout(30000);
|
2021-03-23 11:14:51 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const messageText = "This is a message";
|
2022-09-19 13:50:29 +10:00
|
|
|
await waku.relay.send(TestEncoder, { payload: utf8ToBytes(messageText) });
|
2021-04-29 16:38:50 +10:00
|
|
|
|
2022-09-11 00:57:10 +10:00
|
|
|
let msgs: MessageRpcResponse[] = [];
|
2021-03-23 11:14:51 +11:00
|
|
|
|
2022-01-20 13:00:58 +11:00
|
|
|
while (msgs.length === 0) {
|
2022-02-04 14:12:00 +11:00
|
|
|
console.log("Waiting for messages");
|
2022-01-20 13:00:58 +11:00
|
|
|
await delay(200);
|
2022-04-01 12:19:51 +11:00
|
|
|
msgs = await nwaku.messages();
|
2022-01-20 13:00:58 +11:00
|
|
|
}
|
2021-03-23 11:14:51 +11:00
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
expect(msgs[0].contentTopic).to.equal(TestContentTopic);
|
|
|
|
|
expect(msgs[0].version).to.equal(0);
|
2023-02-16 13:27:39 +11:00
|
|
|
expect(base64ToUtf8(msgs[0].payload)).to.equal(messageText);
|
2022-01-20 13:00:58 +11:00
|
|
|
});
|
2021-04-15 11:19:26 +10:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
it("Nwaku publishes", async function () {
|
2022-01-20 13:00:58 +11:00
|
|
|
await delay(200);
|
2021-03-23 11:14:51 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const messageText = "Here is another message.";
|
2021-03-23 11:14:51 +11:00
|
|
|
|
2022-11-23 16:25:50 +11:00
|
|
|
const receivedMsgPromise: Promise<DecodedMessage> = new Promise(
|
|
|
|
|
(resolve) => {
|
2023-03-31 03:17:41 +02:00
|
|
|
waku.relay.subscribe<DecodedMessage>(TestDecoder, (msg) =>
|
2022-11-23 16:25:50 +11:00
|
|
|
resolve(msg)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-03-23 11:14:51 +11:00
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
await nwaku.sendMessage(
|
|
|
|
|
Nwaku.toMessageRpcQuery({
|
|
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
|
payload: utf8ToBytes(messageText),
|
|
|
|
|
})
|
|
|
|
|
);
|
2021-03-19 14:58:06 +11:00
|
|
|
|
2022-01-20 13:00:58 +11:00
|
|
|
const receivedMsg = await receivedMsgPromise;
|
2021-03-19 14:58:06 +11:00
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
expect(receivedMsg.contentTopic).to.eq(TestContentTopic);
|
2022-11-23 16:25:50 +11:00
|
|
|
expect(receivedMsg.version!).to.eq(0);
|
2022-09-19 13:50:29 +10:00
|
|
|
expect(bytesToUtf8(receivedMsg.payload!)).to.eq(messageText);
|
2021-03-19 15:03:31 +11:00
|
|
|
});
|
2021-03-25 16:39:21 +11:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
describe.skip("Two nodes connected to nwaku", function () {
|
2022-12-06 13:18:32 +11:00
|
|
|
let waku1: RelayNode;
|
|
|
|
|
let waku2: RelayNode;
|
2022-04-01 12:19:51 +11:00
|
|
|
let nwaku: Nwaku;
|
2021-03-25 16:39:21 +11:00
|
|
|
|
2021-04-20 15:07:23 +10:00
|
|
|
afterEach(async function () {
|
2023-04-17 10:29:36 +05:30
|
|
|
!!nwaku &&
|
|
|
|
|
nwaku.stop().catch((e) => console.log("Nwaku failed to stop", e));
|
2022-01-31 15:30:49 +11:00
|
|
|
!!waku1 &&
|
2022-02-04 14:12:00 +11:00
|
|
|
waku1.stop().catch((e) => console.log("Waku failed to stop", e));
|
2022-01-31 15:30:49 +11:00
|
|
|
!!waku2 &&
|
2022-02-04 14:12:00 +11:00
|
|
|
waku2.stop().catch((e) => console.log("Waku failed to stop", e));
|
2021-04-20 15:07:23 +10:00
|
|
|
});
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("Js publishes, other Js receives", async function () {
|
2021-04-20 15:07:23 +10:00
|
|
|
this.timeout(60_000);
|
2021-03-25 16:39:21 +11:00
|
|
|
[waku1, waku2] = await Promise.all([
|
2022-12-06 13:18:32 +11:00
|
|
|
createRelayNode({
|
2021-04-15 11:19:26 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
2022-09-14 22:24:00 +10:00
|
|
|
emitSelf: true,
|
2022-06-23 14:03:52 +10:00
|
|
|
}).then((waku) => waku.start().then(() => waku)),
|
2022-12-06 13:18:32 +11:00
|
|
|
createRelayNode({
|
2021-04-15 11:19:26 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_2,
|
2022-06-23 14:03:52 +10:00
|
|
|
}).then((waku) => waku.start().then(() => waku)),
|
2021-03-25 16:39:21 +11:00
|
|
|
]);
|
|
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
nwaku = new Nwaku(makeLogFileName(this));
|
|
|
|
|
await nwaku.start();
|
2021-03-25 16:39:21 +11:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
const nwakuMultiaddr = await nwaku.getMultiaddrWithId();
|
2021-03-25 16:39:21 +11:00
|
|
|
await Promise.all([
|
2022-04-01 12:19:51 +11:00
|
|
|
waku1.dial(nwakuMultiaddr),
|
|
|
|
|
waku2.dial(nwakuMultiaddr),
|
2021-03-25 16:39:21 +11:00
|
|
|
]);
|
|
|
|
|
|
2021-04-20 10:34:23 +10:00
|
|
|
// Wait for identify protocol to finish
|
|
|
|
|
await Promise.all([
|
2022-07-25 12:33:08 +10:00
|
|
|
waitForRemotePeer(waku1, [Protocols.Relay]),
|
|
|
|
|
waitForRemotePeer(waku2, [Protocols.Relay]),
|
2021-03-25 16:39:21 +11:00
|
|
|
]);
|
2021-04-15 15:23:33 +10:00
|
|
|
|
2021-04-20 15:07:23 +10:00
|
|
|
await delay(2000);
|
2021-03-25 16:39:21 +11:00
|
|
|
// Check that the two JS peers are NOT directly connected
|
2022-02-02 15:12:08 +11:00
|
|
|
expect(await waku1.libp2p.peerStore.has(waku2.libp2p.peerId)).to.be
|
|
|
|
|
.false;
|
|
|
|
|
expect(waku2.libp2p.peerStore.has(waku1.libp2p.peerId)).to.be.false;
|
2021-03-25 16:39:21 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const msgStr = "Hello there!";
|
2022-09-19 13:50:29 +10:00
|
|
|
const message = { payload: utf8ToBytes(msgStr) };
|
2021-03-25 16:39:21 +11:00
|
|
|
|
2022-11-04 14:01:30 +11:00
|
|
|
const waku2ReceivedMsgPromise: Promise<DecodedMessage> = new Promise(
|
2021-05-10 12:27:20 +10:00
|
|
|
(resolve) => {
|
2023-03-31 03:17:41 +02:00
|
|
|
waku2.relay.subscribe(TestDecoder, resolve);
|
2021-05-10 12:27:20 +10:00
|
|
|
}
|
|
|
|
|
);
|
2021-03-25 16:39:21 +11:00
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
await waku1.relay.send(TestEncoder, message);
|
2022-02-04 14:12:00 +11:00
|
|
|
console.log("Waiting for message");
|
2021-05-10 12:27:20 +10:00
|
|
|
const waku2ReceivedMsg = await waku2ReceivedMsgPromise;
|
2021-03-25 16:39:21 +11:00
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
expect(waku2ReceivedMsg.payload).to.eq(msgStr);
|
2021-03-25 16:39:21 +11:00
|
|
|
});
|
|
|
|
|
});
|
2021-03-19 15:03:31 +11:00
|
|
|
});
|
2021-03-19 14:58:06 +11:00
|
|
|
});
|