Add and use `getStatusFleetNodes` to connect to Status' wakunodes

This commit is contained in:
Franck Royer 2021-05-14 13:23:44 +10:00
parent c6ce32dc54
commit 79bef35460
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
8 changed files with 84 additions and 19 deletions

View File

@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added
- `getStatusFleetNodes` to connect to Status' nim-waku nodes.
### Changed ### Changed
- Clarify content topic format in README.md. - Clarify content topic format in README.md.

View File

@ -164,7 +164,7 @@ npm install # Install dependencies for js-waku
npm run build # Build js-waku npm run build # Build js-waku
cd examples/cli-chat cd examples/cli-chat
npm install # Install dependencies for the cli app 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`). You can also specify an optional `listenAddr` parameter (.e.g `--listenAddr /ip4/0.0.0.0/tcp/7777/ws`).

View File

@ -1,14 +1,24 @@
import readline from 'readline'; import readline from 'readline';
import util from 'util'; 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 TCP from 'libp2p-tcp';
import { multiaddr, Multiaddr } from 'multiaddr'; import { multiaddr, Multiaddr } from 'multiaddr';
const ChatContentTopic = 'dingpu'; const ChatContentTopic = 'dingpu';
export default async function startChat(): Promise<void> { export default async function startChat(): Promise<void> {
const opts = processArguments(); let opts = processArguments();
if (opts.autoDial) {
opts = await addFleetNodes(opts);
}
const waku = await Waku.create({ const waku = await Waku.create({
listenAddresses: [opts.listenAddr], listenAddresses: [opts.listenAddr],
@ -93,12 +103,17 @@ export default async function startChat(): Promise<void> {
interface Options { interface Options {
staticNodes: Multiaddr[]; staticNodes: Multiaddr[];
listenAddr: string; listenAddr: string;
autoDial: boolean;
} }
function processArguments(): Options { function processArguments(): Options {
const passedArgs = process.argv.slice(2); 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) { while (passedArgs.length) {
const arg = passedArgs.shift(); const arg = passedArgs.shift();
@ -109,6 +124,9 @@ function processArguments(): Options {
case '--listenAddr': case '--listenAddr':
opts = Object.assign(opts, { listenAddr: passedArgs.shift() }); opts = Object.assign(opts, { listenAddr: passedArgs.shift() });
break; break;
case '--autoDial':
opts.autoDial = true;
break;
default: default:
console.log(`Unsupported argument: ${arg}`); console.log(`Unsupported argument: ${arg}`);
process.exit(1); process.exit(1);
@ -128,3 +146,12 @@ export function formatMessage(chatMsg: ChatMessage): string {
}); });
return `<${timestamp}> ${chatMsg.nick}: ${chatMsg.payloadAsUtf8}`; return `<${timestamp}> ${chatMsg.nick}: ${chatMsg.payloadAsUtf8}`;
} }
async function addFleetNodes(opts: Options): Promise<Options> {
await getStatusFleetNodes().then((nodes) =>
nodes.map((addr) => {
opts.staticNodes.push(multiaddr(addr));
})
);
return opts;
}

View File

@ -1,7 +1,13 @@
import PeerId from 'peer-id'; import PeerId from 'peer-id';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import './App.css'; 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 handleCommand from './command';
import Room from './Room'; import Room from './Room';
import { WakuContext } from './WakuContext'; import { WakuContext } from './WakuContext';
@ -146,13 +152,11 @@ async function initWaku(setter: (waku: Waku) => void) {
setter(waku); setter(waku);
waku.addPeerToAddressBook( const nodes = await getStatusFleetNodes();
'16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ', await Promise.all(
['/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/443/wss'] nodes.map((addr) => {
); return waku.dial(addr);
waku.addPeerToAddressBook( })
'16Uiu2HAmSyrYVycqBCWcHyNVQS6zYQcdQbwyov1CDijboVRsQS37',
['/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/443/wss']
); );
} catch (e) { } catch (e) {
console.log('Issue starting waku ', e); console.log('Issue starting waku ', e);

8
package-lock.json generated
View File

@ -10,6 +10,7 @@
"license": "MIT OR Apache-2.0", "license": "MIT OR Apache-2.0",
"dependencies": { "dependencies": {
"@bitauth/libauth": "^1.17.1", "@bitauth/libauth": "^1.17.1",
"axios": "^0.21.1",
"debug": "^4.3.1", "debug": "^4.3.1",
"it-concat": "^2.0.0", "it-concat": "^2.0.0",
"it-length-prefixed": "^5.0.2", "it-length-prefixed": "^5.0.2",
@ -38,7 +39,6 @@
"@typescript-eslint/eslint-plugin": "^4.0.1", "@typescript-eslint/eslint-plugin": "^4.0.1",
"@typescript-eslint/parser": "^4.0.1", "@typescript-eslint/parser": "^4.0.1",
"app-root-path": "^3.0.0", "app-root-path": "^3.0.0",
"axios": "^0.21.1",
"chai": "^4.3.4", "chai": "^4.3.4",
"codecov": "^3.5.0", "codecov": "^3.5.0",
"cspell": "^4.1.0", "cspell": "^4.1.0",
@ -1570,7 +1570,6 @@
"version": "0.21.1", "version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"dev": true,
"dependencies": { "dependencies": {
"follow-redirects": "^1.10.0" "follow-redirects": "^1.10.0"
} }
@ -5200,7 +5199,6 @@
"version": "1.13.3", "version": "1.13.3",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz",
"integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==", "integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==",
"dev": true,
"engines": { "engines": {
"node": ">=4.0" "node": ">=4.0"
} }
@ -14995,7 +14993,6 @@
"version": "0.21.1", "version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"dev": true,
"requires": { "requires": {
"follow-redirects": "^1.10.0" "follow-redirects": "^1.10.0"
} }
@ -17954,8 +17951,7 @@
"follow-redirects": { "follow-redirects": {
"version": "1.13.3", "version": "1.13.3",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz",
"integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==", "integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA=="
"dev": true
}, },
"for-in": { "for-in": {
"version": "1.0.2", "version": "1.0.2",

View File

@ -56,6 +56,7 @@
}, },
"dependencies": { "dependencies": {
"@bitauth/libauth": "^1.17.1", "@bitauth/libauth": "^1.17.1",
"axios": "^0.21.1",
"debug": "^4.3.1", "debug": "^4.3.1",
"it-concat": "^2.0.0", "it-concat": "^2.0.0",
"it-length-prefixed": "^5.0.2", "it-length-prefixed": "^5.0.2",
@ -84,7 +85,6 @@
"@typescript-eslint/eslint-plugin": "^4.0.1", "@typescript-eslint/eslint-plugin": "^4.0.1",
"@typescript-eslint/parser": "^4.0.1", "@typescript-eslint/parser": "^4.0.1",
"app-root-path": "^3.0.0", "app-root-path": "^3.0.0",
"axios": "^0.21.1",
"chai": "^4.3.4", "chai": "^4.3.4",
"codecov": "^3.5.0", "codecov": "^3.5.0",
"cspell": "^4.1.0", "cspell": "^4.1.0",

View File

@ -1,3 +1,5 @@
export { getStatusFleetNodes } from './lib/discover';
export { Waku } from './lib/waku'; export { Waku } from './lib/waku';
export { WakuMessage } from './lib/waku_message'; export { WakuMessage } from './lib/waku_message';

33
src/lib/discover.ts Normal file
View File

@ -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<string[]> {
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']);
}
}