Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
guysv committed May 12, 2019
0 parents commit 48c274b
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 0 deletions.
43 changes: 43 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
69 changes: 69 additions & 0 deletions src/dialer.js
Original file line number Diff line number Diff line change
@@ -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???

This comment has been minimized.

Copy link
@jacobheun

jacobheun May 15, 2019

@guysv, I'd recommend using async/waterfall instead of series here. It's not actually running anything through the connection right now because conn is actually an array of results from each of the series tasks. waterfall will use the last task as the result, so conn will be the actual connection. conn.conn "works" because it's undefined and pull ignores it.

I ran it locally with those changes (and also added some logging in the listeners echo back so you can see it getting the message and returning it) and it ran as expected.

As more work gets done, I think a nice addition to the demo would be to disconnect from the rendezvous node before sending the echo payload. This would help better demonstrate that it's a direct connection to the other peer.

pull.collect((err, data) => {
if (err) { throw err }
console.log('received echo:', data.toString())
})
)
})
5 changes: 5 additions & 0 deletions src/id-d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "QmYd8Sv6Y7BgjwoBAPwoRzPufQEJZV6iGxwQm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EAuiHrrv7oWD0bbQLoUC3FAFaqENikvQKkSGQArtSYwPITi7A0mFY25yQA3iENYEAJZk/UsVuZAI+Ywl7Kx7I5iBuR+7wz50vUCTty2r7fllJndlIeSbSMC/ik0XRFCElQKHG8TsRdBMtd4yYn3cifcdax50KpPVtcKk93WuVTkbUCgYBlvqaorCcPL2W4FgGx7ut62hK1B23oPvS2XC23oIvURQfzWCunBJCf+FOAVaNC/E3TcmJR4/dlEt7djQvp7DCbnb9aR2WvAksRgUNQQztW54HsnmWYMYtXt9iCBwAcjFLXB9L5ph/dbxKOdP1yFJEclvXUppmR56zh4I+FwNqVhQKBgFv0TS9B1rXTZK4gabf/I2QjuPPwx4B24pCqoLlBtBGIuHrHqWb0upMIFw6ZZB4FCqg4usJaeWPWBZcpfHBEEAExGZ86JdtB3PBVDTrGJISbMpjVMa/KY0TrEyXkIYzzN++4f29ShTeQL3wBXCXInxdrWgfbq2j4AdAA1lzf0abU",
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC8JW40COLQ/6S4O0kwPU9eSaBdH6eRnyf14f0KsYxMw9+386syhfi7vJ6ZaPRQkwE81WQPK7v20R9rZoDaDYqoX7FxnBKqATmGYfZLY7GWqjdHBgk9+rOa7wXw+8cf4K6prtms1U3JDIxNiP0ORxiuYUC9Kol4i32nqc5YGW8QgiNhxTGd6XfuKzbOBK25TiJyl1x62nwyFUlLotXQxBFXE79AxNw+oh9fPcUSo/+eDWOjYwbsv95saVa+dI7AtoSp48QVKElJt/g5ARM/bDBz/QQpICK4MtITWNtckHn+3V1o+vL/0McOQp4oSNLdYWNzuYqUOkytZmI39GbnXFxJAgMBAAE="
}
5 changes: 5 additions & 0 deletions src/id-l.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "QmdoPFts9fNq1UxP6xWe14XPN6MeL4QJyE2ALd4h2sq9Sw",
"privKey": "CAASpgkwggSiAgEAAoIBAQDCIRPUXo8u+OcJk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A8k3+NClc63+30ALYnb7rE6rNnC7iLywtjOopK2Uirv1bUaYP//7ziRTmCodTKAs1Nd6fqrgRy1FXB7kiIRmMQP1SP7gVsUKrarEuwE7YYfkNQW5bIR7GTXBhhgA78vtAoGAJscjvu1K0HTQSk0oJT/myPGX4/EgL3tYzL9mc3QSTIapDKAV1hhiHhaAZsJxiP26Jr+O+lbfKUa7LDLdsWpwnLdpEHqLNBLHZwujlJg6ZaJypAYrVbDj1BTtIpbrGDfC0KuXO/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="
}
5 changes: 5 additions & 0 deletions src/id-r.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "QmWNFVgnkgqbh5rMMRjBX9ye1K8wNtTjmYGtAXUkRG4ESe",
"privKey": "CAASpwkwggSjAgEAAoIBAQDwLfOE+0NBkdZheQ2Ct/r7qZHKPcvUevW1bOE1eQ1X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PIynxpYlb0BfQdXWPPQ1KIf4D+Uz5zVjO4JQnxmZQ8Z6zDuwq+90qtWSv4Al7CrGeMJ46b7L946aVBRJRV2k8EOATczCrrQ7bB1bD5QKBgEFCEpGBp7iq/rw9SJQaZ7U3xUaLdrjG1Av3mdj3BKoQb1c0SjFsHh8kW8HfV4dM6Ut+xYhka0A0Yvs+IZpw+2fTK7yUrQWN45V4mUBK6UeaRwG5/zbWC+ttq+XxJ1Zrbraev8ZK/xyGRWbWChZGUjc/v0FEBPeAM/t+CsyiJ6CRAoGAF0dZ8mpO1KSBY9VsxSudpEUuLu00v2azImLvTqE1ksWWNxqeUeGKWYSUg41/U3PERSUp9KztHitIFFGiwh+mQxB5OsT6loTsIbcIhpBR1gzZL7yjXnaS92XHyzFMaU6egN2ihNtRpKDnJE2OXN8WGXwZZRPSo9iTwWVyOuBG+zE=",
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDwLfOE+0NBkdZheQ2Ct/r7qZHKPcvUevW1bOE1eQ1XOGNocnfvuJ8jgRR5pubTQxKZlZXm4kMaXFxumgsLTNgocRQRt28qzlNuDV++SQAl3Y1EhAMmr9KpLcnu0hDZRnSJ9/fRW3JEmH7smEjqo/+EQKy6SEhoKledgqmflAWVHacBFF6IWm6OyYmx78+DDG3fYiyWtmbacq/t5+HoFRUrbGp9+il/dcLEgUa5Rxg8v5K/rzBHpc/BMZ3eh12EG7sr6gfMPUNoxe6XgtlbR0bMQkVSDbWBTvWqbVHzDwwxXvorXUIVVLAcpVEaikf8iKHcaHzv4QkormfQIGcbN0lFAgMBAAE="
}
50 changes: 50 additions & 0 deletions src/listener.js
Original file line number Diff line number Diff line change
@@ -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())
})
})
46 changes: 46 additions & 0 deletions src/rendezvous.js
Original file line number Diff line number Diff line change
@@ -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())
})
})

0 comments on commit 48c274b

Please sign in to comment.