diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1c73b37 --- /dev/null +++ b/.gitignore @@ -0,0 +1,43 @@ +docs +**/node_modules/ +**/*.log +test/repo-tests* +**/bundle.js + +# Logs +logs +*.log + +coverage + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +build + +# Dependency directory +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git +node_modules + +lib +dist +test/test-data/go-ipfs-repo/LOCK +test/test-data/go-ipfs-repo/LOG +test/test-data/go-ipfs-repo/LOG.old + +# while testing npm5 +package-lock.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..876b750 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "js-libp2p-webrtc-star-demo", + "version": "1.0.0", + "description": "", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "devDependencies": { + "libp2p": "guysv/js-libp2p#pass-node-to-transports", + "libp2p-exchange-direct": "0.0.3", + "libp2p-exchange-rendezvous": "guysv/libp2p-exchange-rendezvous#no-crypto", + "libp2p-webrtc-star": "guysv/js-libp2p-webrtc-star#feat/use-interface-data-exchange", + "wrtc": "^0.4.0", + "libp2p-mplex": "^0.8.5", + "libp2p-tcp": "^0.13.0", + "peer-id": "github:mkg20001/js-peer-id", + "async": "^2.6.2", + "pull-stream": "^3.6.11" + } +} diff --git a/src/dialer.js b/src/dialer.js new file mode 100644 index 0000000..9534523 --- /dev/null +++ b/src/dialer.js @@ -0,0 +1,69 @@ +'use strict' +/* eslint-disable no-console */ + +/* + * Listener Node + */ + +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') +const Node = require('./bundle') +const pull = require('pull-stream') +const series = require('async/series') + +let listenerId +let listenerNode + +series([ + (cb) => { + PeerId.createFromJSON(require('./id-d'), (err, id) => { + if (err) { return cb(err) } + listenerId = id + cb() + }) + }, + (cb) => { + const listenerPeerInfo = new PeerInfo(listenerId) + listenerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') + listenerPeerInfo.multiaddrs.add('/p2p-webrtc-star') + listenerNode = new Node({ + peerInfo: listenerPeerInfo, + config: { + relay: { + enabled: false + } + } + }) + listenerNode.start(cb) + }, + (cb) => { + listenerNode.dial(`/ip4/0.0.0.0/tcp/10333/ipfs/${require('./id-r').id}`, cb) + }, + (cb) => { + setTimeout(cb, 5000) + }, + (cb) => { + // console.log("b") + listenerNode.dialProtocol(`/p2p-webrtc-star/ipfs/${require('./id-l').id}`, '/echo/1.0.0', cb) + // console.log(`/p2p-circuit/ipfs/${require('./id-l').id}`) + // cb() + } +], (err, conn) => { + if (err) { throw err } + + console.log('Listening on:') + listenerNode.peerInfo.multiaddrs.forEach((ma) => { + console.log(ma.toString() + '/p2p/' + listenerId.toB58String()) + }) + + console.log(`Sending data to /p2p-webrtc-star/ipfs/${require('./id-l').id}`) + + pull( + pull.values(['hey']), + conn.conn, // TODO: why conn.conn??? + pull.collect((err, data) => { + if (err) { throw err } + console.log('received echo:', data.toString()) + }) + ) +}) diff --git a/src/id-d.json b/src/id-d.json new file mode 100644 index 0000000..d2ae774 --- /dev/null +++ b/src/id-d.json @@ -0,0 +1,5 @@ +{ + "id": "QmYd8Sv6Y7BgjwoBAPwoRzPufQEJZV6iGxwQmiUxCdDSFj", + "privKey": "CAASqAkwggSkAgEAAoIBAQC8JW40COLQ/6S4O0kwPU9eSaBdH6eRnyf14f0KsYxMw9+386syhfi7vJ6ZaPRQkwE81WQPK7v20R9rZoDaDYqoX7FxnBKqATmGYfZLY7GWqjdHBgk9+rOa7wXw+8cf4K6prtms1U3JDIxNiP0ORxiuYUC9Kol4i32nqc5YGW8QgiNhxTGd6XfuKzbOBK25TiJyl1x62nwyFUlLotXQxBFXE79AxNw+oh9fPcUSo/+e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ztW54HsnmWYMYtXt9iCBwAcjFLXB9L5ph/dbxKOdP1yFJEclvXUppmR56zh4I+FwNqVhQKBgFv0TS9B1rXTZK4gabf/I2QjuPPwx4B24pCqoLlBtBGIuHrHqWb0upMIFw6ZZB4FCqg4usJaeWPWBZcpfHBEEAExGZ86JdtB3PBVDTrGJISbMpjVMa/KY0TrEyXkIYzzN++4f29ShTeQL3wBXCXInxdrWgfbq2j4AdAA1lzf0abU", + "pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC8JW40COLQ/6S4O0kwPU9eSaBdH6eRnyf14f0KsYxMw9+386syhfi7vJ6ZaPRQkwE81WQPK7v20R9rZoDaDYqoX7FxnBKqATmGYfZLY7GWqjdHBgk9+rOa7wXw+8cf4K6prtms1U3JDIxNiP0ORxiuYUC9Kol4i32nqc5YGW8QgiNhxTGd6XfuKzbOBK25TiJyl1x62nwyFUlLotXQxBFXE79AxNw+oh9fPcUSo/+eDWOjYwbsv95saVa+dI7AtoSp48QVKElJt/g5ARM/bDBz/QQpICK4MtITWNtckHn+3V1o+vL/0McOQp4oSNLdYWNzuYqUOkytZmI39GbnXFxJAgMBAAE=" +} diff --git a/src/id-l.json b/src/id-l.json new file mode 100644 index 0000000..d50f3c2 --- /dev/null +++ b/src/id-l.json @@ -0,0 +1,5 @@ +{ + "id": "QmdoPFts9fNq1UxP6xWe14XPN6MeL4QJyE2ALd4h2sq9Sw", + "privKey": "CAASpgkwggSiAgEAAoIBAQDCIRPUXo8u+OcJkKV7a5foj3iXbC9d+kPNEKdWYEs/F/pv/hLpyEn6uHN/sFe7VEOzZlO7LwRHHylipVONYOeekXwVXpol5JCFZi9/JlDISYzyQgTxmtnx8WU58u+1CWTk/qdohQrrRzW409ob45TlUEcqDT70r8lsxAFrPGE+f7FcfAeG8kgqF2ytGyq+e/n46ByFWn/VMTwBsolKqzbw8yw9iQn0R5+ZuGk1RWGnYWTYGlARhHpOJENI9SjKwnFSmnMjkBfI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j1BTtIpbrGDfC0KuXO/Ao6Ab/aEphQhjaozMUipANb/yl9DerT/sXAaUCgYBiKZL0KxqVzqKGpUjBJfhZivxsjsCfGIcfx2qKknQg4nuh5Ho6j4vWYoaVZt6012GuihX/foRY68X+PamBOk3qYKnkiK1zNsVyMh7WHiQ2835eNOgr2Dy5PdYHtmmL6NSaZdn+T+WskQYZ1DOaYPRI3PpualqGjFUNN4QhB4BOJA==", + "pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDCIRPUXo8u+OcJkKV7a5foj3iXbC9d+kPNEKdWYEs/F/pv/hLpyEn6uHN/sFe7VEOzZlO7LwRHHylipVONYOeekXwVXpol5JCFZi9/JlDISYzyQgTxmtnx8WU58u+1CWTk/qdohQrrRzW409ob45TlUEcqDT70r8lsxAFrPGE+f7FcfAeG8kgqF2ytGyq+e/n46ByFWn/VMTwBsolKqzbw8yw9iQn0R5+ZuGk1RWGnYWTYGlARhHpOJENI9SjKwnFSmnMjkBfI04ulnmUwNmp/sNAEfiUwAZ5JcCdGlNd5QJQWVWbdKmAGNHi0h1mDryDBuHoI0RAyufCASGciPGEXAgMBAAE=" +} diff --git a/src/id-r.json b/src/id-r.json new file mode 100644 index 0000000..0b8c831 --- /dev/null +++ b/src/id-r.json @@ -0,0 +1,5 @@ +{ + "id": "QmWNFVgnkgqbh5rMMRjBX9ye1K8wNtTjmYGtAXUkRG4ESe", + "privKey": "CAASpwkwggSjAgEAAoIBAQDwLfOE+0NBkdZheQ2Ct/r7qZHKPcvUevW1bOE1eQ1XOGNocnfvuJ8jgRR5pubTQxKZlZXm4kMaXFxumgsLTNgocRQRt28qzlNuDV++SQAl3Y1EhAMmr9KpLcnu0hDZRnSJ9/fRW3JEmH7smEjqo/+EQKy6SEhoKledgqmflAWVHacBFF6IWm6OyYmx78+DDG3fYiyWtmbacq/t5+HoFRUrbGp9+il/dcLEgUa5Rxg8v5K/rzBHpc/BMZ3eh12EG7sr6gfMPUN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RwG5/zbWC+ttq+XxJ1Zrbraev8ZK/xyGRWbWChZGUjc/v0FEBPeAM/t+CsyiJ6CRAoGAF0dZ8mpO1KSBY9VsxSudpEUuLu00v2azImLvTqE1ksWWNxqeUeGKWYSUg41/U3PERSUp9KztHitIFFGiwh+mQxB5OsT6loTsIbcIhpBR1gzZL7yjXnaS92XHyzFMaU6egN2ihNtRpKDnJE2OXN8WGXwZZRPSo9iTwWVyOuBG+zE=", + "pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDwLfOE+0NBkdZheQ2Ct/r7qZHKPcvUevW1bOE1eQ1XOGNocnfvuJ8jgRR5pubTQxKZlZXm4kMaXFxumgsLTNgocRQRt28qzlNuDV++SQAl3Y1EhAMmr9KpLcnu0hDZRnSJ9/fRW3JEmH7smEjqo/+EQKy6SEhoKledgqmflAWVHacBFF6IWm6OyYmx78+DDG3fYiyWtmbacq/t5+HoFRUrbGp9+il/dcLEgUa5Rxg8v5K/rzBHpc/BMZ3eh12EG7sr6gfMPUNoxe6XgtlbR0bMQkVSDbWBTvWqbVHzDwwxXvorXUIVVLAcpVEaikf8iKHcaHzv4QkormfQIGcbN0lFAgMBAAE=" +} diff --git a/src/listener.js b/src/listener.js new file mode 100644 index 0000000..06bc33e --- /dev/null +++ b/src/listener.js @@ -0,0 +1,50 @@ +'use strict' +/* eslint-disable no-console */ + +/* + * Listener Node + */ + +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') +const Node = require('./bundle') +const pull = require('pull-stream') +const series = require('async/series') + +let listenerId +let listenerNode + +series([ + (cb) => { + PeerId.createFromJSON(require('./id-l'), (err, id) => { + if (err) { return cb(err) } + listenerId = id + cb() + }) + }, + (cb) => { + const listenerPeerInfo = new PeerInfo(listenerId) + listenerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') + listenerPeerInfo.multiaddrs.add('/p2p-webrtc-star') + listenerNode = new Node({ + peerInfo: listenerPeerInfo, + config: { + relay: { + enabled: false + } + } + }) + listenerNode.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn)) + listenerNode.start(cb) + }, + (cb) => { + listenerNode.dial(`/ip4/0.0.0.0/tcp/10333/ipfs/${require('./id-r').id}`, cb) + } +], (err) => { + if (err) { throw err } + + console.log('Listening on:') + listenerNode.peerInfo.multiaddrs.forEach((ma) => { + console.log(ma.toString() + '/p2p/' + listenerId.toB58String()) + }) +}) diff --git a/src/rendezvous.js b/src/rendezvous.js new file mode 100644 index 0000000..cf9ff57 --- /dev/null +++ b/src/rendezvous.js @@ -0,0 +1,46 @@ +'use strict' +/* eslint-disable no-console */ + +/* + * Listener Node + */ + +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') +const Node = require('./bundle') +// const pull = require('pull-stream') +const series = require('async/series') + +let listenerId +let listenerNode + +series([ + (cb) => { + PeerId.createFromJSON(require('./id-r'), (err, id) => { + if (err) { return cb(err) } + listenerId = id + cb() + }) + }, + (cb) => { + const listenerPeerInfo = new PeerInfo(listenerId) + listenerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/10333') + listenerPeerInfo.multiaddrs.add('/p2p-webrtc-star') + listenerNode = new Node({ + peerInfo: listenerPeerInfo, + config: { + relay: { + enabled: false + } + } + }) + listenerNode.start(cb) + } +], (err) => { + if (err) { throw err } + + console.log('Listening on:') + listenerNode.peerInfo.multiaddrs.forEach((ma) => { + console.log(ma.toString() + '/p2p/' + listenerId.toB58String()) + }) +})