diff --git a/chat/index.ts b/chat/index.ts new file mode 100644 index 0000000000..632a71fc7c --- /dev/null +++ b/chat/index.ts @@ -0,0 +1,38 @@ +import Waku from '../build/main/lib/waku'; +import { TOPIC } from '../build/main/lib/waku_relay'; +import { Message } from '../build/main/lib/waku_message'; + +import readline from 'readline'; + +;(async function() { + + const waku = await Waku.create(); + console.log('Waku started'); + await waku.dial('/ip4/134.209.139.210/tcp/30303/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ'); + // await waku.dial('/ip4/134.209.113.86/tcp/9000/p2p/16Uiu2HAmVVi6Q4j7MAKVibquW8aA27UNrA4Q8Wkz9EetGViu8ZF1'); + console.log('Static node has been dialed'); + + // TODO: Automatically subscribe + await waku.relay.subscribe(); + console.log('Subscribed to waku relay'); + + // TODO: Bubble event to waku, infere topic, decode msg + waku.libp2p.pubsub.on(TOPIC, event => { + const msg = Message.fromBinary(event.data); + console.log(msg.utf8Payload()); + }); + + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + + console.log('Ready to chat!'); + rl.prompt(); + rl.on('line', async (line) => { + rl.prompt(); + const msg = Message.fromUtf8String(line); + await waku.relay.publish(msg); + }); + +})(); diff --git a/package.json b/package.json index de887e04dd..8829b8b5e6 100644 --- a/package.json +++ b/package.json @@ -13,12 +13,14 @@ "build:1-proto": "buf generate", "build:main": "tsc -p tsconfig.json", "build:module": "tsc -p tsconfig.module.json", + "build:chat": "tsc -p tsconfig.chat.json", "fix": "run-s fix:*", "fix:prettier": "prettier \"src/**/*.ts\" --write", "fix:lint": "eslint src --ext .ts --fix", "pretest": "run-s pretest:*", "pretest:1-init-git-submodules": "[ -f './nim-waku/build/wakunode2' ] || git submodule update --init --recursive", "pretest:2-build-nim-waku": "cd nim-waku; [ -f './build/wakunode2' ] || make -j$(nproc --all 2>/dev/null || echo 2) wakunode2", + "start-chat": "ts-node chat/index.ts", "test": "run-s build test:*", "test:lint": "eslint src --ext .ts", "test:prettier": "prettier \"src/**/*.ts\" --list-different", diff --git a/src/lib/waku_relay.ts b/src/lib/waku_relay.ts index 76a03caec3..f8e91350f6 100644 --- a/src/lib/waku_relay.ts +++ b/src/lib/waku_relay.ts @@ -20,7 +20,7 @@ export class WakuRelayPubsub extends Gossipsub { */ constructor(libp2p: Libp2p) { super(libp2p, { - emitSelf: true, + emitSelf: false, // Ensure that no signature is expected in the messages. globalSignaturePolicy: SignaturePolicy.StrictNoSign, }); diff --git a/tsconfig.chat.json b/tsconfig.chat.json new file mode 100644 index 0000000000..7a9b8796f6 --- /dev/null +++ b/tsconfig.chat.json @@ -0,0 +1,53 @@ +{ + "compilerOptions": { + "incremental": true, + "target": "es2017", + "outDir": "build/chat", + "rootDir": "chat", + "moduleResolution": "node", + "module": "commonjs", + "declaration": true, + "inlineSourceMap": true, + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, + "resolveJsonModule": true /* Include modules imported with .json extension. */, + + "strict": true /* Enable all strict type-checking options. */, + + /* Strict Type-Checking Options */ + // "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */, + // "strictNullChecks": true /* Enable strict null checks. */, + // "strictFunctionTypes": true /* Enable strict checking of function types. */, + // "strictPropertyInitialization": true /* Enable strict checking of property initialization in classes. */, + // "noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */, + // "alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */, + + /* Additional Checks */ + "noUnusedLocals": true /* Report errors on unused locals. */, + "noUnusedParameters": true /* Report errors on unused parameters. */, + "noImplicitReturns": true /* Report error when not all code paths in function return a value. */, + "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */, + + /* Debugging Options */ + "traceResolution": false /* Report module resolution log messages. */, + "listEmittedFiles": false /* Print names of generated files part of the compilation. */, + "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. */, + + "lib": ["es2017"], + "types": ["node", "mocha"], + "typeRoots": ["node_modules/@types"] + }, + "include": ["chat/**/*.ts"], + "exclude": ["node_modules/**"], + "compileOnSave": false, + "ts-node": { + "files": true + } +}