Connects to nim-waku using waku-relay protocol

This commit is contained in:
Franck Royer 2021-03-05 14:41:20 +11:00
parent 512115ae8c
commit be47223bae
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
8 changed files with 2565 additions and 160 deletions

View File

@ -10,7 +10,6 @@
"plugin:eslint-comments/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/typescript",
"plugin:functional/lite",
"prettier",
"prettier/@typescript-eslint"
],

2581
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -42,7 +42,15 @@
"node": ">=10"
},
"dependencies": {
"@bitauth/libauth": "^1.17.1"
"@bitauth/libauth": "^1.17.1",
"libp2p": "^0.30.9",
"libp2p-gossipsub": "^0.8.0",
"libp2p-mplex": "^0.10.2",
"libp2p-noise": "^2.0.5",
"libp2p-secio": "^0.13.1",
"libp2p-tcp": "^0.15.3",
"multiaddr": "^8.1.2",
"yarg": "^1.0.8"
},
"devDependencies": {
"@ava/typescript": "^1.1.1",

View File

@ -1,2 +1,74 @@
export * from './lib/async';
export * from './lib/number';
import Libp2p from 'libp2p';
import Mplex from 'libp2p-mplex';
import { NOISE } from 'libp2p-noise';
import Secio from 'libp2p-secio';
import TCP from 'libp2p-tcp';
import Multiaddr from 'multiaddr';
import multiaddr from 'multiaddr';
import { CODEC, WakuRelay } from './lib/waku_relay';
(async () => {
// Handle arguments
const { peer } = args();
const libp2p = await Libp2p.create({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0', '/ip4/0.0.0.0/tcp/0/ws'],
},
modules: {
transport: [TCP],
streamMuxer: [Mplex],
connEncryption: [NOISE, Secio],
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Key missing, see https://github.com/libp2p/js-libp2p/issues/830#issuecomment-791040021
pubsub: WakuRelay,
},
config: {
pubsub: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
enabled: true,
emitSelf: true,
signMessages: false,
strictSigning: false,
},
},
});
libp2p.connectionManager.on('peer:connect', (connection) => {
console.info(`Connected to ${connection.remotePeer.toB58String()}!`);
});
// Start libp2p
await libp2p.start();
console.log('listening on addresses:');
libp2p.multiaddrs.forEach((addr) => {
console.log(`${addr.toString()}/p2p/${libp2p.peerId.toB58String()}`);
});
console.log('\nNode supports protocols:');
libp2p.upgrader.protocols.forEach((_, p) => console.log(p));
// Dial nim-waku using waku relay protocol
if (process.argv.length >= 3) {
console.log(`dialing remote peer at ${peer}`);
await libp2p.dialProtocol(peer, CODEC);
console.log(`dialed ${peer}`);
}
})();
function args(): { peer: Multiaddr } {
const args = process.argv.slice(2);
if (args.length != 1) {
console.log(`Usage:
${process.argv[0]} ${process.argv[1]} <peer multiaddress>`);
process.exit(1);
}
const peer = multiaddr(args[0]);
return { peer };
}

15
src/lib/waku_relay.ts Normal file
View File

@ -0,0 +1,15 @@
import Libp2p from 'libp2p';
import Gossipsub from 'libp2p-gossipsub';
export const CODEC = '/vac/waku/relay/2.0.0-beta2';
export class WakuRelay extends Gossipsub {
constructor(libp2p: Libp2p) {
super(libp2p);
const multicodecs = [CODEC];
// This is the downside of using `libp2p-gossipsub` instead of `libp2p-interfaces/src/pubsub`
Object.assign(this, { multicodecs });
}
}

View File

@ -1,24 +0,0 @@
/**
* If you import a dependency which does not include its own type definitions,
* TypeScript will try to find a definition for it by following the `typeRoots`
* compiler option in tsconfig.json. For this project, we've configured it to
* fall back to this folder if nothing is found in node_modules/@types.
*
* Often, you can install the DefinitelyTyped
* (https://github.com/DefinitelyTyped/DefinitelyTyped) type definition for the
* dependency in question. However, if no one has yet contributed definitions
* for the package, you may want to declare your own. (If you're using the
* `noImplicitAny` compiler options, you'll be required to declare it.)
*
* This is an example type definition which allows import from `module-name`,
* e.g.:
* ```ts
* import something from 'module-name';
* something();
* ```
*/
declare module 'module-name' {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const whatever: any;
export = whatever;
}

15
src/types/types.d.ts vendored Normal file
View File

@ -0,0 +1,15 @@
declare module 'libp2p-tcp' {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const TCP: any;
export = TCP;
}
declare module 'libp2p-mplex' {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Mplex: any;
export = Mplex;
}
declare module 'libp2p-secio' {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Secio: any;
export = Secio;
}

View File

@ -33,6 +33,9 @@
"listFiles": false /* Print names of files part of the compilation. */,
"pretty": true /* Stylize errors and messages using color and context. */,
// Due to broken types in indirect dependencies
"skipLibCheck": true,
/* Experimental Options */
// "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
// "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,