2022-02-04 14:12:00 +11:00
|
|
|
import { expect } from "chai";
|
|
|
|
import PeerId from "peer-id";
|
2021-03-22 16:02:10 +11:00
|
|
|
|
2021-08-10 11:32:14 +10:00
|
|
|
import {
|
|
|
|
makeLogFileName,
|
|
|
|
NOISE_KEY_1,
|
|
|
|
NOISE_KEY_2,
|
2022-04-01 12:19:51 +11:00
|
|
|
Nwaku,
|
2022-02-04 14:12:00 +11:00
|
|
|
} from "../test_utils/";
|
2022-05-17 18:23:50 +10:00
|
|
|
import { delay } from "../test_utils/delay";
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2022-05-20 10:43:49 +10:00
|
|
|
import { generateSymmetricKey } from "./crypto";
|
2022-02-04 14:12:00 +11:00
|
|
|
import { Protocols, Waku } from "./waku";
|
|
|
|
import { WakuMessage } from "./waku_message";
|
2022-01-20 13:00:58 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const TestContentTopic = "/test/1/waku/utf8";
|
2021-03-10 16:25:54 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
describe("Waku Dial [node only]", function () {
|
2022-04-01 12:19:51 +11:00
|
|
|
describe("Interop: nwaku", function () {
|
2022-01-20 13:00:58 +11:00
|
|
|
let waku: Waku;
|
2022-04-01 12:19:51 +11:00
|
|
|
let nwaku: Nwaku;
|
2021-08-09 12:27:52 +10:00
|
|
|
|
2022-01-20 13:00:58 +11:00
|
|
|
afterEach(async function () {
|
2022-04-01 12:19:51 +11:00
|
|
|
!!nwaku && nwaku.stop();
|
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-08-09 12:27:52 +10:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
it("connects to nwaku", async function () {
|
2022-01-20 13:00:58 +11:00
|
|
|
this.timeout(20_000);
|
2022-04-01 12:19:51 +11:00
|
|
|
nwaku = new Nwaku(makeLogFileName(this));
|
|
|
|
await nwaku.start();
|
|
|
|
const multiAddrWithId = await nwaku.getMultiaddrWithId();
|
2021-08-10 11:32:14 +10:00
|
|
|
|
2022-01-20 13:00:58 +11:00
|
|
|
waku = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
});
|
|
|
|
await waku.dial(multiAddrWithId);
|
2022-01-30 21:56:21 +11:00
|
|
|
await waku.waitForRemotePeer([Protocols.Relay]);
|
2022-01-20 13:00:58 +11:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
const nimPeerId = await nwaku.getPeerId();
|
2022-02-02 15:12:08 +11:00
|
|
|
expect(await waku.libp2p.peerStore.has(nimPeerId)).to.be.true;
|
2022-01-20 13:00:58 +11:00
|
|
|
});
|
2021-08-10 11:32:14 +10:00
|
|
|
});
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
describe("Bootstrap", function () {
|
2022-01-27 21:50:27 +11:00
|
|
|
let waku: Waku;
|
2022-04-01 12:19:51 +11:00
|
|
|
let nwaku: Nwaku;
|
2022-01-27 21:50:27 +11:00
|
|
|
|
|
|
|
afterEach(async function () {
|
2022-04-01 12:19:51 +11:00
|
|
|
!!nwaku && nwaku.stop();
|
2022-02-04 14:12:00 +11:00
|
|
|
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
|
2022-01-27 21:50:27 +11:00
|
|
|
});
|
2021-09-21 14:22:35 +10:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("Passing an array", async function () {
|
2021-08-10 11:32:14 +10:00
|
|
|
this.timeout(10_000);
|
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
nwaku = new Nwaku(makeLogFileName(this));
|
|
|
|
await nwaku.start();
|
|
|
|
const multiAddrWithId = await nwaku.getMultiaddrWithId();
|
2022-01-20 13:00:58 +11:00
|
|
|
|
2021-08-10 11:32:14 +10:00
|
|
|
waku = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
2022-03-09 18:38:28 +11:00
|
|
|
bootstrap: { peers: [multiAddrWithId] },
|
2021-08-10 11:32:14 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
const connectedPeerID: PeerId = await new Promise((resolve) => {
|
2022-02-04 14:12:00 +11:00
|
|
|
waku.libp2p.connectionManager.on("peer:connect", (connection) => {
|
2021-08-10 11:32:14 +10:00
|
|
|
resolve(connection.remotePeer);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-01-20 13:00:58 +11:00
|
|
|
expect(connectedPeerID.toB58String()).to.eq(multiAddrWithId.getPeerId());
|
2021-08-10 11:32:14 +10:00
|
|
|
});
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("Passing a function", async function () {
|
2021-08-10 11:32:14 +10:00
|
|
|
this.timeout(10_000);
|
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
nwaku = new Nwaku(makeLogFileName(this));
|
|
|
|
await nwaku.start();
|
2022-01-20 13:00:58 +11:00
|
|
|
|
2021-08-10 11:32:14 +10:00
|
|
|
waku = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
2022-01-13 14:28:45 +11:00
|
|
|
bootstrap: {
|
|
|
|
getPeers: async () => {
|
2022-04-01 12:19:51 +11:00
|
|
|
return [await nwaku.getMultiaddrWithId()];
|
2022-01-13 14:28:45 +11:00
|
|
|
},
|
2021-08-10 11:32:14 +10:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const connectedPeerID: PeerId = await new Promise((resolve) => {
|
2022-02-04 14:12:00 +11:00
|
|
|
waku.libp2p.connectionManager.on("peer:connect", (connection) => {
|
2021-08-10 11:32:14 +10:00
|
|
|
resolve(connection.remotePeer);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
const multiAddrWithId = await nwaku.getMultiaddrWithId();
|
2022-01-20 13:00:58 +11:00
|
|
|
expect(connectedPeerID.toB58String()).to.eq(multiAddrWithId.getPeerId());
|
2021-03-19 15:03:31 +11:00
|
|
|
});
|
|
|
|
});
|
2021-03-10 17:39:53 +11:00
|
|
|
});
|
2021-09-01 15:02:28 +10:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
describe("Decryption Keys", () => {
|
2021-09-01 15:02:28 +10:00
|
|
|
afterEach(function () {
|
2022-02-04 14:12:00 +11:00
|
|
|
if (this.currentTest?.state === "failed") {
|
2021-09-01 15:02:28 +10:00
|
|
|
console.log(`Test failed, log file name is ${makeLogFileName(this)}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let waku1: Waku;
|
|
|
|
let waku2: Waku;
|
|
|
|
beforeEach(async function () {
|
2022-01-31 15:30:49 +11:00
|
|
|
this.timeout(5000);
|
2021-09-01 15:02:28 +10:00
|
|
|
[waku1, waku2] = await Promise.all([
|
|
|
|
Waku.create({ staticNoiseKey: NOISE_KEY_1 }),
|
|
|
|
Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_2,
|
2022-02-04 14:12:00 +11:00
|
|
|
libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } },
|
2021-09-01 15:02:28 +10:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
waku1.addPeerToAddressBook(waku2.libp2p.peerId, waku2.libp2p.multiaddrs);
|
|
|
|
|
|
|
|
await Promise.all([
|
2022-01-30 21:56:21 +11:00
|
|
|
waku1.waitForRemotePeer([Protocols.Relay]),
|
|
|
|
waku2.waitForRemotePeer([Protocols.Relay]),
|
2021-09-01 15:02:28 +10:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async function () {
|
2022-02-04 14:12:00 +11:00
|
|
|
!!waku1 && waku1.stop().catch((e) => console.log("Waku failed to stop", e));
|
|
|
|
!!waku2 && waku2.stop().catch((e) => console.log("Waku failed to stop", e));
|
2021-09-01 15:02:28 +10:00
|
|
|
});
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("Used by Waku Relay", async function () {
|
2021-09-01 15:02:28 +10:00
|
|
|
this.timeout(10000);
|
|
|
|
|
|
|
|
const symKey = generateSymmetricKey();
|
|
|
|
|
|
|
|
waku2.addDecryptionKey(symKey);
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const messageText = "Message is encrypted";
|
|
|
|
const messageTimestamp = new Date("1995-12-17T03:24:00");
|
2021-09-01 15:02:28 +10:00
|
|
|
const message = await WakuMessage.fromUtf8String(
|
|
|
|
messageText,
|
|
|
|
TestContentTopic,
|
|
|
|
{
|
|
|
|
timestamp: messageTimestamp,
|
|
|
|
symKey,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const receivedMsgPromise: Promise<WakuMessage> = new Promise((resolve) => {
|
|
|
|
waku2.relay.addObserver(resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
await waku1.relay.send(message);
|
|
|
|
|
|
|
|
const receivedMsg = await receivedMsgPromise;
|
|
|
|
|
|
|
|
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
|
|
|
|
expect(receivedMsg.version).to.eq(message.version);
|
|
|
|
expect(receivedMsg.payloadAsUtf8).to.eq(messageText);
|
|
|
|
expect(receivedMsg.timestamp?.valueOf()).to.eq(messageTimestamp.valueOf());
|
|
|
|
});
|
|
|
|
});
|
2022-01-31 10:30:25 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
describe("Wait for remote peer / get peers", function () {
|
2022-01-31 10:30:25 +11:00
|
|
|
let waku: Waku;
|
2022-05-18 15:55:08 +10:00
|
|
|
let nwaku: Nwaku | undefined;
|
2022-01-31 10:30:25 +11:00
|
|
|
|
|
|
|
afterEach(async function () {
|
2022-05-18 15:55:08 +10:00
|
|
|
if (nwaku) {
|
|
|
|
nwaku.stop();
|
|
|
|
nwaku = undefined;
|
|
|
|
}
|
2022-02-04 14:12:00 +11:00
|
|
|
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
|
2022-01-31 10:30:25 +11:00
|
|
|
});
|
|
|
|
|
2022-05-17 18:23:50 +10:00
|
|
|
it("Relay - dialed first", async function () {
|
2022-01-31 10:30:25 +11:00
|
|
|
this.timeout(20_000);
|
2022-04-01 12:19:51 +11:00
|
|
|
nwaku = new Nwaku(makeLogFileName(this));
|
|
|
|
await nwaku.start();
|
|
|
|
const multiAddrWithId = await nwaku.getMultiaddrWithId();
|
2022-01-31 10:30:25 +11:00
|
|
|
|
|
|
|
waku = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
});
|
|
|
|
await waku.dial(multiAddrWithId);
|
2022-05-17 18:23:50 +10:00
|
|
|
await delay(1000);
|
2022-01-31 10:30:25 +11:00
|
|
|
await waku.waitForRemotePeer([Protocols.Relay]);
|
|
|
|
const peers = waku.relay.getPeers();
|
|
|
|
const nimPeerId = multiAddrWithId.getPeerId();
|
|
|
|
|
|
|
|
expect(nimPeerId).to.not.be.undefined;
|
|
|
|
expect(peers.has(nimPeerId as string)).to.be.true;
|
|
|
|
});
|
|
|
|
|
2022-05-17 18:23:50 +10:00
|
|
|
it("Relay - dialed after", async function () {
|
|
|
|
this.timeout(20_000);
|
|
|
|
nwaku = new Nwaku(makeLogFileName(this));
|
|
|
|
await nwaku.start();
|
|
|
|
const multiAddrWithId = await nwaku.getMultiaddrWithId();
|
|
|
|
|
|
|
|
waku = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
});
|
|
|
|
|
|
|
|
const waitPromise = waku.waitForRemotePeer([Protocols.Relay]);
|
|
|
|
await delay(1000);
|
|
|
|
await waku.dial(multiAddrWithId);
|
|
|
|
await waitPromise;
|
|
|
|
|
|
|
|
const peers = waku.relay.getPeers();
|
|
|
|
const nimPeerId = multiAddrWithId.getPeerId();
|
|
|
|
|
|
|
|
expect(nimPeerId).to.not.be.undefined;
|
|
|
|
expect(peers.has(nimPeerId as string)).to.be.true;
|
|
|
|
});
|
|
|
|
|
2022-05-18 15:55:08 +10:00
|
|
|
it("Relay - times out", function (done) {
|
|
|
|
this.timeout(5000);
|
|
|
|
Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
}).then((waku) => {
|
|
|
|
waku.waitForRemotePeer([Protocols.Relay], 200).then(
|
|
|
|
() => {
|
|
|
|
throw "Promise expected to reject on time out";
|
|
|
|
},
|
|
|
|
(reason) => {
|
|
|
|
expect(reason).to.eq("Timed out waiting for a remote peer.");
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-05-17 18:23:50 +10:00
|
|
|
it("Store - dialed first", async function () {
|
2022-01-31 10:30:25 +11:00
|
|
|
this.timeout(20_000);
|
2022-04-01 12:19:51 +11:00
|
|
|
nwaku = new Nwaku(makeLogFileName(this));
|
|
|
|
await nwaku.start({ persistMessages: true });
|
|
|
|
const multiAddrWithId = await nwaku.getMultiaddrWithId();
|
2022-01-31 10:30:25 +11:00
|
|
|
|
|
|
|
waku = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
});
|
|
|
|
await waku.dial(multiAddrWithId);
|
2022-05-17 18:23:50 +10:00
|
|
|
await delay(1000);
|
2022-01-31 10:30:25 +11:00
|
|
|
await waku.waitForRemotePeer([Protocols.Store]);
|
2022-02-02 15:12:08 +11:00
|
|
|
|
|
|
|
const peers = [];
|
|
|
|
for await (const peer of waku.store.peers) {
|
|
|
|
peers.push(peer.id.toB58String());
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:30:25 +11:00
|
|
|
const nimPeerId = multiAddrWithId.getPeerId();
|
|
|
|
|
|
|
|
expect(nimPeerId).to.not.be.undefined;
|
|
|
|
expect(peers.includes(nimPeerId as string)).to.be.true;
|
|
|
|
});
|
|
|
|
|
2022-05-18 15:55:08 +10:00
|
|
|
it("Store - dialed after - with timeout", async function () {
|
2022-05-17 18:23:50 +10:00
|
|
|
this.timeout(20_000);
|
|
|
|
nwaku = new Nwaku(makeLogFileName(this));
|
|
|
|
await nwaku.start({ persistMessages: true });
|
|
|
|
const multiAddrWithId = await nwaku.getMultiaddrWithId();
|
|
|
|
|
|
|
|
waku = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
});
|
2022-05-18 15:55:08 +10:00
|
|
|
const waitPromise = waku.waitForRemotePeer([Protocols.Store], 2000);
|
2022-05-17 18:23:50 +10:00
|
|
|
await delay(1000);
|
|
|
|
await waku.dial(multiAddrWithId);
|
|
|
|
await waitPromise;
|
|
|
|
|
|
|
|
const peers = [];
|
|
|
|
for await (const peer of waku.store.peers) {
|
|
|
|
peers.push(peer.id.toB58String());
|
|
|
|
}
|
|
|
|
|
|
|
|
const nimPeerId = multiAddrWithId.getPeerId();
|
|
|
|
|
|
|
|
expect(nimPeerId).to.not.be.undefined;
|
|
|
|
expect(peers.includes(nimPeerId as string)).to.be.true;
|
|
|
|
});
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("LightPush", async function () {
|
2022-01-31 10:30:25 +11:00
|
|
|
this.timeout(20_000);
|
2022-04-01 12:19:51 +11:00
|
|
|
nwaku = new Nwaku(makeLogFileName(this));
|
|
|
|
await nwaku.start({ lightpush: true });
|
|
|
|
const multiAddrWithId = await nwaku.getMultiaddrWithId();
|
2022-01-31 10:30:25 +11:00
|
|
|
|
|
|
|
waku = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
});
|
|
|
|
await waku.dial(multiAddrWithId);
|
|
|
|
await waku.waitForRemotePeer([Protocols.LightPush]);
|
2022-02-02 15:12:08 +11:00
|
|
|
|
|
|
|
const peers = [];
|
|
|
|
for await (const peer of waku.lightPush.peers) {
|
|
|
|
peers.push(peer.id.toB58String());
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:30:25 +11:00
|
|
|
const nimPeerId = multiAddrWithId.getPeerId();
|
|
|
|
|
|
|
|
expect(nimPeerId).to.not.be.undefined;
|
|
|
|
expect(peers.includes(nimPeerId as string)).to.be.true;
|
|
|
|
});
|
2022-05-27 20:19:35 +10:00
|
|
|
|
|
|
|
it("Filter", async function () {
|
|
|
|
this.timeout(20_000);
|
|
|
|
nwaku = new Nwaku(makeLogFileName(this));
|
|
|
|
await nwaku.start({ filter: true });
|
|
|
|
const multiAddrWithId = await nwaku.getMultiaddrWithId();
|
|
|
|
|
|
|
|
waku = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
});
|
|
|
|
await waku.dial(multiAddrWithId);
|
|
|
|
await waku.waitForRemotePeer([Protocols.Filter]);
|
|
|
|
|
|
|
|
const peers = [];
|
|
|
|
for await (const peer of waku.filter.peers) {
|
|
|
|
peers.push(peer.id.toB58String());
|
|
|
|
}
|
|
|
|
|
|
|
|
const nimPeerId = multiAddrWithId.getPeerId();
|
|
|
|
|
|
|
|
expect(nimPeerId).to.not.be.undefined;
|
|
|
|
expect(peers.includes(nimPeerId as string)).to.be.true;
|
|
|
|
});
|
2022-01-31 10:30:25 +11:00
|
|
|
});
|