diff --git a/src/lib/waku.ts b/src/lib/waku.ts index f9c3e0c9f7..be6a734ceb 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -37,6 +37,14 @@ export default class Waku { return new Waku(libp2p, new WakuRelay(libp2p.pubsub)); } + /** + * Dials to the provided peer. If successful, the known metadata of the peer will be added to the nodes peerStore, and the Connection will be returned + * @param peer The peer to dial + */ + async dial(peer: PeerId | Multiaddr | string) { + return this.libp2p.dialProtocol(peer, CODEC); + } + async dialWithMultiAddr(peerId: PeerId, multiaddr: Multiaddr[]) { this.libp2p.peerStore.addressBook.set(peerId, multiaddr); await this.libp2p.dialProtocol(peerId, CODEC); diff --git a/src/lib/waku_relay.spec.ts b/src/lib/waku_relay.spec.ts index ba1d02a86d..4f6250d783 100644 --- a/src/lib/waku_relay.spec.ts +++ b/src/lib/waku_relay.spec.ts @@ -2,6 +2,7 @@ import { expect } from 'chai'; import Pubsub from 'libp2p-interfaces/src/pubsub'; import { NOISE_KEY_1, NOISE_KEY_2 } from '../test_utils/constants'; +import { delay } from '../test_utils/delay'; import { makeLogFileName } from '../test_utils/log_file'; import { NimWaku } from '../test_utils/nim_waku'; @@ -179,6 +180,80 @@ describe('Waku Relay', () => { }); }); + describe('Js connects to nim', function () { + let waku: Waku; + let nimWaku: NimWaku; + + beforeEach(async function () { + this.timeout(10_000); + waku = await Waku.create(NOISE_KEY_1); + + nimWaku = new NimWaku(this.test!.ctx!.currentTest!.title); + await nimWaku.start(); + + await waku.dial(await nimWaku.getMultiaddrWithId()); + + await delay(100); + await new Promise((resolve) => + waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve) + ); + }); + + afterEach(async function () { + nimWaku ? nimWaku.stop() : null; + waku ? await waku.stop() : null; + }); + + it('nim subscribes to js', async function () { + const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC); + + const nimPeerId = await nimWaku.getPeerId(); + expect(subscribers).to.contain(nimPeerId.toB58String()); + }); + + it('Js publishes to nim', async function () { + const message = Message.fromUtf8String('This is a message'); + // TODO: nim-waku does not really follow the `StrictNoSign` policy hence we need to change + // it for nim-waku to process our messages. Can be removed once + // https://github.com/status-im/nim-waku/issues/422 is fixed + waku.libp2p.pubsub.globalSignaturePolicy = 'StrictSign'; + + await waku.relay.publish(message); + + await nimWaku.waitForLog('WakuMessage received'); + + const msgs = await nimWaku.messages(); + + expect(msgs[0].contentTopic).to.equal(message.contentTopic); + expect(msgs[0].version).to.equal(message.version); + + const payload = Buffer.from(msgs[0].payload); + expect(Buffer.compare(payload, message.payload!)).to.equal(0); + }); + + it('Nim publishes to js', async function () { + const message = Message.fromUtf8String('Here is another message.'); + + await waku.relay.subscribe(); + + await new Promise((resolve) => + waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve) + ); + + const receivedPromise = waitForNextData(waku.libp2p.pubsub); + + await nimWaku.sendMessage(message); + + const receivedMsg = await receivedPromise; + + expect(receivedMsg.contentTopic).to.eq(message.contentTopic); + expect(receivedMsg.version).to.eq(message.version); + + const payload = Buffer.from(receivedMsg.payload!); + expect(Buffer.compare(payload, message.payload!)).to.eq(0); + }); + }); + describe('Js connects to nim', function () { let waku: Waku; let nimWaku: NimWaku; @@ -190,9 +265,7 @@ describe('Waku Relay', () => { nimWaku = new NimWaku(makeLogFileName(this)); await nimWaku.start(); - const nimPeerId = await nimWaku.getPeerId(); - - await waku.dialWithMultiAddr(nimPeerId, [nimWaku.multiaddr]); + await waku.dial(await nimWaku.getMultiaddrWithId()); await new Promise((resolve) => waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve) diff --git a/src/test_utils/nim_waku.ts b/src/test_utils/nim_waku.ts index 34e7e6e9f1..e80f1aed9a 100644 --- a/src/test_utils/nim_waku.ts +++ b/src/test_utils/nim_waku.ts @@ -36,6 +36,7 @@ export class NimWaku { private pid?: number; private portsShift: number; private peerId?: PeerId; + private multiaddrWithId?: Multiaddr; private logPath: string; constructor(logName: string) { @@ -160,14 +161,25 @@ export class NimWaku { } async getPeerId(): Promise { - if (this.peerId) { - return this.peerId; + return await this.setPeerId().then((res) => res.peerId); + } + + async getMultiaddrWithId(): Promise { + return await this.setPeerId().then((res) => res.multiaddrWithId); + } + + private async setPeerId(): Promise<{ + peerId: PeerId; + multiaddrWithId: Multiaddr; + }> { + if (this.peerId && this.multiaddrWithId) { + return { peerId: this.peerId, multiaddrWithId: this.multiaddrWithId }; } - const res = await this.info(); - const strPeerId = multiaddr(res.listenStr).getPeerId(); - - return PeerId.createFromB58String(strPeerId); + this.multiaddrWithId = multiaddr(res.listenStr); + const peerIdStr = this.multiaddrWithId.getPeerId(); + this.peerId = PeerId.createFromB58String(peerIdStr); + return { peerId: this.peerId, multiaddrWithId: this.multiaddrWithId }; } get multiaddr(): Multiaddr {