diff --git a/package-lock.json b/package-lock.json index 4bd3559169..bf5313a586 100644 --- a/package-lock.json +++ b/package-lock.json @@ -849,6 +849,15 @@ "integrity": "sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==", "dev": true }, + "@types/axios": { + "version": "0.14.0", + "resolved": "https://registry.npmjs.org/@types/axios/-/axios-0.14.0.tgz", + "integrity": "sha1-7CMA++fX3d1+udOr+HmZlkyvzkY=", + "dev": true, + "requires": { + "axios": "*" + } + }, "@types/bl": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/@types/bl/-/bl-2.1.0.tgz", @@ -903,11 +912,6 @@ "dev": true, "optional": true }, - "@types/prompt-sync": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/@types/prompt-sync/-/prompt-sync-4.1.0.tgz", - "integrity": "sha1-utMynv9bQRXjTvRpgjckTUEdRHA=" - }, "@types/retry": { "version": "0.12.0", "resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.0.tgz", @@ -1733,6 +1737,15 @@ "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.11.0.tgz", "integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==" }, + "axios": { + "version": "0.21.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", + "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", + "dev": true, + "requires": { + "follow-redirects": "^1.10.0" + } + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -5018,6 +5031,12 @@ "integrity": "sha512-zAoAQiudy+r5SvnSw3KJy5os/oRJYHzrzja/tBDqrZtNhUw8bt6y8OBzMWcjWr+8liV8Eb6yOhw8WZ7VFZ5ZzA==", "dev": true }, + "follow-redirects": { + "version": "1.13.3", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz", + "integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==", + "dev": true + }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", diff --git a/package.json b/package.json index 770facd7e9..509285f46a 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ }, "dependencies": { "@bitauth/libauth": "^1.17.1", - "@types/prompt-sync": "^4.1.0", "debug": "^4.3.1", "libp2p": "^0.30.0", "libp2p-gossipsub": "^0.7.0", @@ -60,11 +59,13 @@ "devDependencies": { "@ava/typescript": "^1.1.1", "@istanbuljs/nyc-config-typescript": "^1.0.1", + "@types/axios": "^0.14.0", "@types/node": "^14.14.31", "@typescript-eslint/eslint-plugin": "^4.0.1", "@typescript-eslint/parser": "^4.0.1", "ava": "^3.15.0", "ava-fast-check": "^4.0.2", + "axios": "^0.21.1", "codecov": "^3.5.0", "cspell": "^4.1.0", "cz-conventional-changelog": "^3.3.0", diff --git a/src/lib/node.spec.ts b/src/lib/node.spec.ts index 8058c3d068..3dde03ca11 100644 --- a/src/lib/node.spec.ts +++ b/src/lib/node.spec.ts @@ -1,14 +1,13 @@ import test from 'ava'; import Pubsub from 'libp2p-interfaces/src/pubsub'; +import { delay } from '../test_utils/delay'; +import { NimWaku } from '../test_utils/nim_waku'; + import { createNode } from './node'; import { Message } from './waku_message'; import { CODEC, TOPIC, WakuRelay } from './waku_relay'; -function delay(ms: number) { - return new Promise((resolve) => setTimeout(resolve, ms)); -} - test('Can publish message', async (t) => { const message = Message.fromString('Bird bird bird, bird is the word!'); @@ -51,6 +50,22 @@ test('Does not register any sub protocol', async (t) => { t.truthy(protocols.findIndex((value) => value.match(/sub/))); }); +test('Nim-waku: connects', async (t) => { + const nimWaku = new NimWaku(); + await nimWaku.start(); + + const node = await createNode(); + console.log(node.peerId.toB58String()); + + node.peerStore.addressBook.set(nimWaku.peerId, [nimWaku.multiaddr]); + await node.dial(nimWaku.peerId); + + const peers = await nimWaku.peers(); + + console.log(peers); + t.is(peers.length, 1); +}); + function waitForNextData(pubsub: Pubsub): Promise { return new Promise((resolve) => { pubsub.once(TOPIC, resolve); diff --git a/src/lib/waku_relay.ts b/src/lib/waku_relay.ts index ebd39fafe3..ae53ad0ebd 100644 --- a/src/lib/waku_relay.ts +++ b/src/lib/waku_relay.ts @@ -22,6 +22,7 @@ export class WakuRelayPubsub extends Gossipsub { } } +// TODO: Implement dial for an address with format '/ip4/127.0.0.1/tcp/60000/p2p/16Uiu2HAkyzsXzENw5XBDYEQQAeQTCYjBJpMLgBmEXuwbtcrgxBJ4' // This class provides an interface to execute the waku relay protocol export class WakuRelay { constructor(private pubsub: Pubsub) {} diff --git a/src/test_utils/delay.ts b/src/test_utils/delay.ts new file mode 100644 index 0000000000..98eaa4f54d --- /dev/null +++ b/src/test_utils/delay.ts @@ -0,0 +1,3 @@ +export function delay(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} diff --git a/src/test_utils/nim_waku.ts b/src/test_utils/nim_waku.ts new file mode 100644 index 0000000000..8b77fc88e6 --- /dev/null +++ b/src/test_utils/nim_waku.ts @@ -0,0 +1,96 @@ +import { ChildProcess, spawn } from 'child_process'; + +import axios from 'axios'; +import Multiaddr from 'multiaddr'; +import multiaddr from 'multiaddr'; +import PeerId from 'peer-id'; + +import { delay } from './delay'; + +const NIM_WAKU_BIN = '/home/froyer/src/status-im/nim-waku/build/wakunode2'; +const NIM_WAKU_RPC_URL = 'http://localhost:8545/'; +const NIM_WAKU_PEER_ID = PeerId.createFromB58String( + '16Uiu2HAkyzsXzENw5XBDYEQQAeQTCYjBJpMLgBmEXuwbtcrgxBJ4' +); +const NIM_WAKKU_LISTEN_ADDR = multiaddr('/ip4/127.0.0.1/tcp/60000/'); + +export class NimWaku { + private process?: ChildProcess; + + async start() { + // Start a local only node with the right RPC commands + // The fixed nodekey ensures the node has a fixed Peerid: 16Uiu2HAkyzsXzENw5XBDYEQQAeQTCYjBJpMLgBmEXuwbtcrgxBJ4 + + this.process = spawn( + NIM_WAKU_BIN, + [ + '--nat=none', + '--listen-address=127.0.0.1', + '--relay=true', + '--rpc=true', + '--rpc-admin=true', + '--nodekey=B2C4E3DB22EA6EB6850689F7B3DF3DDA73F59C87EFFD902BEDCEE90A3A2341A6', + ], + { cwd: '/home/froyer/src/status-im/nim-waku/' } + ); + + this.process.on('exit', (signal) => { + console.log(`nim-waku stopped: ${signal}`); + }); + + // TODO: Wait for line "RPC Server started " + await delay(2000); + console.log('Nim waku is hopefully started'); + } + + /** Calls nim-waku2 JSON-RPC API `get_waku_v2_admin_v1_peers` to check + * for known peers + * @throws if nim-waku2 isn't started. + */ + async peers(): Promise { + this.checkProcess(); + + const res = await this.rpcCall('get_waku_v2_admin_v1_peers', []); + + return res.result; + } + + async info() { + this.checkProcess(); + + const res = await this.rpcCall('get_waku_v2_debug_v1_info', []); + + return res.result; + } + + get peerId(): PeerId { + return NIM_WAKU_PEER_ID; + } + + get multiaddr(): Multiaddr { + return NIM_WAKKU_LISTEN_ADDR; + } + + private async rpcCall(method: string, params: any[]) { + const res = await axios.post( + NIM_WAKU_RPC_URL, + { + jsonrpc: '2.0', + id: 1, + method, + params, + }, + { + headers: { 'Content-Type': 'application/json' }, + } + ); + + return res.data; + } + + private checkProcess() { + if (!this.process) { + throw "Nim Waku isn't started"; + } + } +}