diff --git a/src/lib/node.spec.ts b/src/lib/node.spec.ts index e4d4705310..1b93a913eb 100644 --- a/src/lib/node.spec.ts +++ b/src/lib/node.spec.ts @@ -84,8 +84,6 @@ test('Nim-interop: js node subscribes to default waku topic (only checking js si const peerId = node.peerId.toB58String(); - console.log(`js peer id: ${peerId}`); - const localMultiaddr = node.multiaddrs.find((addr) => addr.toString().match(/127\.0\.0\.1/) ); @@ -104,6 +102,40 @@ test('Nim-interop: js node subscribes to default waku topic (only checking js si t.true(subscribers.includes(nimPeerId)); }); +test('Nim-interop: nim node sends message', async (t) => { + const node = await createNode(); + + const peerId = node.peerId.toB58String(); + + console.log(`js peer id: ${peerId}`); + + const localMultiaddr = node.multiaddrs.find((addr) => + addr.toString().match(/127\.0\.0\.1/) + ); + const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; + + const nimWaku = new NimWaku(); + await nimWaku.start({ staticnode: multiAddrWithId }); + + const wakuRelayNode = new WakuRelay(node.pubsub); + await wakuRelayNode.subscribe(); + + // Setup the promise before publishing to ensure the event is not missed + const promise = waitForNextData(node.pubsub); + + const message = Message.fromString('This is a message.'); + + await delay(500); + + await nimWaku.sendMessage(message); + + await delay(1000); + + const received = await promise; + + t.true(received.isEqualTo(message)); +}); + function waitForNextData(pubsub: Pubsub): Promise { return new Promise((resolve) => { pubsub.once(TOPIC, resolve); diff --git a/src/lib/waku_message.ts b/src/lib/waku_message.ts index 114dd1e452..cc5ee9d93d 100644 --- a/src/lib/waku_message.ts +++ b/src/lib/waku_message.ts @@ -7,7 +7,7 @@ export class Message { public contentTopic: number; public version: number; - private constructor(private protobuf: WakuMessage) { + private constructor(public protobuf: WakuMessage) { this.protobuf = protobuf; const msg = protobuf.toObject(); @@ -27,7 +27,7 @@ export class Message { // This is the content topic commonly used at this time wakuMsg.setContentTopic(1); - wakuMsg.setPayload(Buffer.from(message)); + wakuMsg.setPayload(message); return new Message(wakuMsg); } diff --git a/src/test_utils/nim_waku.ts b/src/test_utils/nim_waku.ts index 91ff0fde6d..c7c3ee1e8a 100644 --- a/src/test_utils/nim_waku.ts +++ b/src/test_utils/nim_waku.ts @@ -7,6 +7,9 @@ import Multiaddr from 'multiaddr'; import multiaddr from 'multiaddr'; import PeerId from 'peer-id'; +import { Message } from '../lib/waku_message'; +import { TOPIC } from '../lib/waku_relay'; + import waitForLine from './log_file'; const openAsync = promisify(fs.open); @@ -77,6 +80,37 @@ export class NimWaku { return res.result; } + async sendMessage(message: Message) { + this.checkProcess(); + + let payload; + if (typeof message.payload === 'string') { + payload = strToHex(message.payload); + } else { + payload = bufToHex(message.payload); + } + + const rpcMessage = { + payload, + contentTopic: message.contentTopic, + }; + + const res = await this.rpcCall('post_waku_v2_relay_v1_message', [ + TOPIC, + rpcMessage, + ]); + + return res.result; + } + + async messages() { + this.checkProcess(); + + const res = await this.rpcCall('get_waku_v2_relay_v1_messages', [TOPIC]); + + return res.result; + } + get peerId(): PeerId { return NIM_WAKU_PEER_ID; } @@ -143,3 +177,27 @@ export function mergeArguments(args: Args): Args { return res; } + +// TODO: Test this +function strToHex(str: string): string { + let hex: string; + try { + hex = unescape(encodeURIComponent(str)) + .split('') + .map(function (v) { + return v.charCodeAt(0).toString(16).toUpperCase(); + }) + .join(''); + } catch (e) { + hex = str; + console.log('invalid text input: ' + str); + } + return '0x' + hex; +} + +// TODO: Test this +function bufToHex(buffer: Uint8Array) { + return Array.prototype.map + .call(buffer, (x) => ('00' + x.toString(16)).slice(-2)) + .join(''); +}