Merge pull request #156 from status-im/55-auto-select

This commit is contained in:
Franck Royer 2021-05-15 19:57:32 +10:00 committed by GitHub
commit cab9be4782
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 92 additions and 27 deletions

View File

@ -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.

View File

@ -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`).

View File

@ -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<void> {
const opts = processArguments();
let opts = processArguments();
if (opts.autoDial) {
opts = await addFleetNodes(opts);
}
const waku = await Waku.create({
listenAddresses: [opts.listenAddr],
@ -49,10 +59,12 @@ export default async function startChat(): Promise<void> {
[ChatContentTopic]
);
if (opts.staticNode) {
console.log(`Dialing ${opts.staticNode}`);
await waku.dial(opts.staticNode);
}
await Promise.all(
opts.staticNodes.map((addr) => {
console.log(`Dialing ${addr}`);
return waku.dial(addr);
})
);
// If we connect to a peer with WakuStore, we run the protocol
// TODO: Instead of doing it `once` it should always be done but
@ -89,26 +101,32 @@ export default async function startChat(): Promise<void> {
}
interface Options {
staticNode?: Multiaddr;
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' };
let opts: Options = {
listenAddr: '/ip4/0.0.0.0/tcp/0',
staticNodes: [],
autoDial: false,
};
while (passedArgs.length) {
const arg = passedArgs.shift();
switch (arg) {
case '--staticNode':
opts = Object.assign(opts, {
staticNode: multiaddr(passedArgs.shift()!),
});
opts.staticNodes.push(multiaddr(passedArgs.shift()!));
break;
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<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 { 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);

8
package-lock.json generated
View File

@ -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",

View File

@ -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",

View File

@ -1,3 +1,5 @@
export { getStatusFleetNodes } from './lib/discover';
export { Waku } from './lib/waku';
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']);
}
}