From 79bef35460af080a5bde878b5846f41154f5c78f Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 14 May 2021 13:23:44 +1000 Subject: [PATCH] Add and use `getStatusFleetNodes` to connect to Status' wakunodes --- CHANGELOG.md | 3 +++ README.md | 2 +- examples/cli-chat/src/chat.ts | 33 ++++++++++++++++++++++++++++++--- examples/web-chat/src/App.tsx | 20 ++++++++++++-------- package-lock.json | 8 ++------ package.json | 2 +- src/index.ts | 2 ++ src/lib/discover.ts | 33 +++++++++++++++++++++++++++++++++ 8 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 src/lib/discover.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index e88008c986..04da2f54d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- `getStatusFleetNodes` to connect to Status' nim-waku nodes. + ### Changed - Clarify content topic format in README.md. diff --git a/README.md b/README.md index 2a4d1b2da2..a082ed03b3 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ npm install # Install dependencies for js-waku npm run build # Build js-waku cd examples/cli-chat npm install # Install dependencies for the cli app -npm run start -- --staticNode /ip4/134.209.139.210/tcp/30303/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ +npm run start -- --autoDial ``` You can also specify an optional `listenAddr` parameter (.e.g `--listenAddr /ip4/0.0.0.0/tcp/7777/ws`). diff --git a/examples/cli-chat/src/chat.ts b/examples/cli-chat/src/chat.ts index 89ed1a36ba..4bc9a53585 100644 --- a/examples/cli-chat/src/chat.ts +++ b/examples/cli-chat/src/chat.ts @@ -1,14 +1,24 @@ import readline from 'readline'; import util from 'util'; -import { ChatMessage, StoreCodec, Waku, WakuMessage } from 'js-waku'; +import { + ChatMessage, + getStatusFleetNodes, + StoreCodec, + Waku, + WakuMessage, +} from 'js-waku'; import TCP from 'libp2p-tcp'; import { multiaddr, Multiaddr } from 'multiaddr'; const ChatContentTopic = 'dingpu'; export default async function startChat(): Promise { - const opts = processArguments(); + let opts = processArguments(); + + if (opts.autoDial) { + opts = await addFleetNodes(opts); + } const waku = await Waku.create({ listenAddresses: [opts.listenAddr], @@ -93,12 +103,17 @@ export default async function startChat(): Promise { interface Options { staticNodes: Multiaddr[]; listenAddr: string; + autoDial: boolean; } function processArguments(): Options { const passedArgs = process.argv.slice(2); - let opts: Options = { listenAddr: '/ip4/0.0.0.0/tcp/0', staticNodes: [] }; + let opts: Options = { + listenAddr: '/ip4/0.0.0.0/tcp/0', + staticNodes: [], + autoDial: false, + }; while (passedArgs.length) { const arg = passedArgs.shift(); @@ -109,6 +124,9 @@ function processArguments(): Options { case '--listenAddr': opts = Object.assign(opts, { listenAddr: passedArgs.shift() }); break; + case '--autoDial': + opts.autoDial = true; + break; default: console.log(`Unsupported argument: ${arg}`); process.exit(1); @@ -128,3 +146,12 @@ export function formatMessage(chatMsg: ChatMessage): string { }); return `<${timestamp}> ${chatMsg.nick}: ${chatMsg.payloadAsUtf8}`; } + +async function addFleetNodes(opts: Options): Promise { + await getStatusFleetNodes().then((nodes) => + nodes.map((addr) => { + opts.staticNodes.push(multiaddr(addr)); + }) + ); + return opts; +} diff --git a/examples/web-chat/src/App.tsx b/examples/web-chat/src/App.tsx index 9e83436f00..eac45a4af3 100644 --- a/examples/web-chat/src/App.tsx +++ b/examples/web-chat/src/App.tsx @@ -1,7 +1,13 @@ import PeerId from 'peer-id'; import { useEffect, useState } from 'react'; import './App.css'; -import { ChatMessage, WakuMessage, StoreCodec, Waku } from 'js-waku'; +import { + ChatMessage, + WakuMessage, + StoreCodec, + Waku, + getStatusFleetNodes, +} from 'js-waku'; import handleCommand from './command'; import Room from './Room'; import { WakuContext } from './WakuContext'; @@ -146,13 +152,11 @@ async function initWaku(setter: (waku: Waku) => void) { setter(waku); - waku.addPeerToAddressBook( - '16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ', - ['/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/443/wss'] - ); - waku.addPeerToAddressBook( - '16Uiu2HAmSyrYVycqBCWcHyNVQS6zYQcdQbwyov1CDijboVRsQS37', - ['/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/443/wss'] + const nodes = await getStatusFleetNodes(); + await Promise.all( + nodes.map((addr) => { + return waku.dial(addr); + }) ); } catch (e) { console.log('Issue starting waku ', e); diff --git a/package-lock.json b/package-lock.json index a26a5377e0..bc48ae062a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "MIT OR Apache-2.0", "dependencies": { "@bitauth/libauth": "^1.17.1", + "axios": "^0.21.1", "debug": "^4.3.1", "it-concat": "^2.0.0", "it-length-prefixed": "^5.0.2", @@ -38,7 +39,6 @@ "@typescript-eslint/eslint-plugin": "^4.0.1", "@typescript-eslint/parser": "^4.0.1", "app-root-path": "^3.0.0", - "axios": "^0.21.1", "chai": "^4.3.4", "codecov": "^3.5.0", "cspell": "^4.1.0", @@ -1570,7 +1570,6 @@ "version": "0.21.1", "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", - "dev": true, "dependencies": { "follow-redirects": "^1.10.0" } @@ -5200,7 +5199,6 @@ "version": "1.13.3", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz", "integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==", - "dev": true, "engines": { "node": ">=4.0" } @@ -14995,7 +14993,6 @@ "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" } @@ -17954,8 +17951,7 @@ "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 + "integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==" }, "for-in": { "version": "1.0.2", diff --git a/package.json b/package.json index 0ed8831ebf..dda038fc46 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ }, "dependencies": { "@bitauth/libauth": "^1.17.1", + "axios": "^0.21.1", "debug": "^4.3.1", "it-concat": "^2.0.0", "it-length-prefixed": "^5.0.2", @@ -84,7 +85,6 @@ "@typescript-eslint/eslint-plugin": "^4.0.1", "@typescript-eslint/parser": "^4.0.1", "app-root-path": "^3.0.0", - "axios": "^0.21.1", "chai": "^4.3.4", "codecov": "^3.5.0", "cspell": "^4.1.0", diff --git a/src/index.ts b/src/index.ts index 86d529950e..b507f04fe5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ +export { getStatusFleetNodes } from './lib/discover'; + export { Waku } from './lib/waku'; export { WakuMessage } from './lib/waku_message'; diff --git a/src/lib/discover.ts b/src/lib/discover.ts new file mode 100644 index 0000000000..cfa46052c2 --- /dev/null +++ b/src/lib/discover.ts @@ -0,0 +1,33 @@ +/** + * Returns multiaddrs (inc. ip) of nim-waku nodes ran by Status. + * Used as a temporary discovery helper until more parties run their own nodes. + */ +import axios from 'axios'; + +export enum Protocol { + websocket = 'websocket', + tcp = 'tcp', +} + +export enum Environment { + Test = 'test', + Prod = 'prod', +} + +export async function getStatusFleetNodes( + env: Environment = Environment.Test, + protocol: Protocol = Protocol.websocket +): Promise { + const res = await axios.get('https://fleets.status.im/', { + headers: { 'Content-Type': 'application/json' }, + }); + + const wakuFleet = res.data.fleets[`wakuv2.${env}`]; + + switch (protocol) { + case Protocol.tcp: + return Object.values(wakuFleet['waku']); + default: + return Object.values(wakuFleet['waku-websocket']); + } +}