2021-03-22 16:02:10 +11:00
|
|
|
import { expect } from 'chai';
|
2021-07-14 12:25:23 +10:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore: No types available
|
2021-04-15 11:19:26 +10:00
|
|
|
import TCP from 'libp2p-tcp';
|
2021-08-10 11:32:14 +10:00
|
|
|
import PeerId from 'peer-id';
|
2021-03-22 16:02:10 +11:00
|
|
|
|
2021-08-10 11:32:14 +10:00
|
|
|
import {
|
|
|
|
makeLogFileName,
|
|
|
|
NimWaku,
|
|
|
|
NOISE_KEY_1,
|
|
|
|
NOISE_KEY_2,
|
|
|
|
} from '../test_utils/';
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-05-10 15:26:14 +10:00
|
|
|
import { Waku } from './waku';
|
2021-09-01 15:02:28 +10:00
|
|
|
import { WakuMessage } from './waku_message';
|
|
|
|
import { generateSymmetricKey } from './waku_message/version_1';
|
|
|
|
|
|
|
|
const TestContentTopic = '/test/1/waku/utf8';
|
2021-03-10 16:25:54 +11:00
|
|
|
|
2021-04-15 11:19:26 +10:00
|
|
|
describe('Waku Dial', function () {
|
2021-08-09 12:27:52 +10:00
|
|
|
let waku: Waku;
|
2021-08-10 11:32:14 +10:00
|
|
|
let waku2: Waku;
|
2021-08-09 12:27:52 +10:00
|
|
|
let nimWaku: NimWaku;
|
|
|
|
|
|
|
|
afterEach(async function () {
|
|
|
|
this.timeout(10_000);
|
|
|
|
|
|
|
|
nimWaku ? nimWaku.stop() : null;
|
2021-08-10 11:32:14 +10:00
|
|
|
|
|
|
|
await Promise.all([waku ? waku.stop() : null, waku2 ? waku2.stop() : null]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Bootstrap', function () {
|
|
|
|
it('Passing an array', async function () {
|
|
|
|
this.timeout(10_000);
|
|
|
|
|
|
|
|
waku = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
libp2p: {
|
|
|
|
addresses: { listen: ['/ip4/0.0.0.0/tcp/0'] },
|
|
|
|
modules: { transport: [TCP] },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const multiAddrWithId = waku.getLocalMultiaddrWithID();
|
|
|
|
|
|
|
|
waku2 = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_2,
|
|
|
|
libp2p: {
|
|
|
|
modules: { transport: [TCP] },
|
|
|
|
},
|
|
|
|
bootstrap: [multiAddrWithId],
|
|
|
|
});
|
|
|
|
|
|
|
|
const connectedPeerID: PeerId = await new Promise((resolve) => {
|
|
|
|
waku.libp2p.connectionManager.on('peer:connect', (connection) => {
|
|
|
|
resolve(connection.remotePeer);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(connectedPeerID.toB58String()).to.eq(
|
|
|
|
waku2.libp2p.peerId.toB58String()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Bootstrap', function () {
|
|
|
|
it('Passing a function', async function () {
|
|
|
|
this.timeout(10_000);
|
|
|
|
|
|
|
|
waku = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
libp2p: {
|
|
|
|
addresses: { listen: ['/ip4/0.0.0.0/tcp/0'] },
|
|
|
|
modules: { transport: [TCP] },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const multiAddrWithId = waku.getLocalMultiaddrWithID();
|
|
|
|
|
|
|
|
waku2 = await Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_2,
|
|
|
|
libp2p: {
|
|
|
|
modules: { transport: [TCP] },
|
|
|
|
},
|
|
|
|
bootstrap: () => {
|
|
|
|
return [multiAddrWithId];
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const connectedPeerID: PeerId = await new Promise((resolve) => {
|
|
|
|
waku.libp2p.connectionManager.on('peer:connect', (connection) => {
|
|
|
|
resolve(connection.remotePeer);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(connectedPeerID.toB58String()).to.eq(
|
|
|
|
waku2.libp2p.peerId.toB58String()
|
|
|
|
);
|
|
|
|
});
|
2021-08-09 12:27:52 +10:00
|
|
|
});
|
|
|
|
|
2021-03-22 16:02:10 +11:00
|
|
|
describe('Interop: Nim', function () {
|
|
|
|
it('nim connects to js', async function () {
|
|
|
|
this.timeout(10_000);
|
2021-08-09 12:27:52 +10:00
|
|
|
|
|
|
|
waku = await Waku.create({
|
2021-04-15 11:19:26 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
2021-06-08 22:01:48 +10:00
|
|
|
libp2p: {
|
|
|
|
addresses: { listen: ['/ip4/0.0.0.0/tcp/0'] },
|
|
|
|
modules: { transport: [TCP] },
|
|
|
|
},
|
2021-04-15 11:19:26 +10:00
|
|
|
});
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-04-06 11:06:10 +10:00
|
|
|
const multiAddrWithId = waku.getLocalMultiaddrWithID();
|
2021-03-11 10:54:35 +11:00
|
|
|
|
2021-08-09 12:27:52 +10:00
|
|
|
nimWaku = new NimWaku(makeLogFileName(this));
|
2021-03-19 15:03:31 +11:00
|
|
|
await nimWaku.start({ staticnode: multiAddrWithId });
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-03-19 15:03:31 +11:00
|
|
|
const nimPeers = await nimWaku.peers();
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-03-22 16:02:10 +11:00
|
|
|
expect(nimPeers).to.deep.equal([
|
2021-03-19 15:03:31 +11:00
|
|
|
{
|
|
|
|
multiaddr: multiAddrWithId,
|
2021-07-26 11:04:34 +10:00
|
|
|
protocol: '/vac/waku/relay/2.0.0',
|
2021-03-19 15:03:31 +11:00
|
|
|
connected: true,
|
|
|
|
},
|
|
|
|
]);
|
2021-03-11 15:02:29 +11:00
|
|
|
|
2021-03-19 15:03:31 +11:00
|
|
|
const nimPeerId = await nimWaku.getPeerId();
|
|
|
|
const jsPeers = waku.libp2p.peerStore.peers;
|
2021-03-11 15:02:29 +11:00
|
|
|
|
2021-03-22 16:02:10 +11:00
|
|
|
expect(jsPeers.has(nimPeerId.toB58String())).to.be.true;
|
2021-03-19 15:03:31 +11:00
|
|
|
});
|
|
|
|
});
|
2021-03-10 17:39:53 +11:00
|
|
|
});
|
2021-09-01 15:02:28 +10:00
|
|
|
|
|
|
|
describe('Decryption Keys', () => {
|
|
|
|
afterEach(function () {
|
|
|
|
if (this.currentTest?.state === 'failed') {
|
|
|
|
console.log(`Test failed, log file name is ${makeLogFileName(this)}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let waku1: Waku;
|
|
|
|
let waku2: Waku;
|
|
|
|
beforeEach(async function () {
|
|
|
|
[waku1, waku2] = await Promise.all([
|
|
|
|
Waku.create({ staticNoiseKey: NOISE_KEY_1 }),
|
|
|
|
Waku.create({
|
|
|
|
staticNoiseKey: NOISE_KEY_2,
|
|
|
|
libp2p: { addresses: { listen: ['/ip4/0.0.0.0/tcp/0/ws'] } },
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
waku1.addPeerToAddressBook(waku2.libp2p.peerId, waku2.libp2p.multiaddrs);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
new Promise((resolve) =>
|
|
|
|
waku1.libp2p.pubsub.once('pubsub:subscription-change', () =>
|
|
|
|
resolve(null)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
new Promise((resolve) =>
|
|
|
|
waku2.libp2p.pubsub.once('pubsub:subscription-change', () =>
|
|
|
|
resolve(null)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async function () {
|
|
|
|
this.timeout(5000);
|
|
|
|
await waku1.stop();
|
|
|
|
await waku2.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Used by Waku Relay', async function () {
|
|
|
|
this.timeout(10000);
|
|
|
|
|
|
|
|
const symKey = generateSymmetricKey();
|
|
|
|
|
|
|
|
waku2.addDecryptionKey(symKey);
|
|
|
|
|
|
|
|
const messageText = 'Message is encrypted';
|
|
|
|
const messageTimestamp = new Date('1995-12-17T03:24: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());
|
|
|
|
});
|
|
|
|
});
|