Skip to content

Commit 606fa73

Browse files
authored
WIP - feat: convergence (#94)
* feat: convergence * structure examples
1 parent e3870f3 commit 606fa73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2267
-91
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ libp2p is the product of a long and arduous quest to understand the evolution of
2828

2929
We are in the process of writting better documentation, blog posts, tutorials and a formal specification. Today you can find:
3030

31-
- [libp2p.io - The libp2p Website (WIP)](https://github.com/libp2p/website)
31+
- [libp2p.io](https://libp2p.io)
3232
- [Specification (WIP)](https://github.com/libp2p/specs)
3333
- Talks
3434
- [`libp2p <3 ethereum` at DEVCON2](https://ethereumfoundation.org/devcon/?session=libp2p) [video](https://www.youtube.com/watch?v=HxueJbeMVG4) [slides](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p-HEART-devp2p-IPFS-PLUS-Ethereum-networking.pdf) [demo-1](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo1-1.mp4) [demo-2](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo2-1.mp4)
@@ -41,8 +41,8 @@ To sum up, libp2p is a "network stack" -- a protocol suite -- that cleanly separ
4141

4242
With its modular nature, libp2p can be found being used in different projects with different sets of features, while preserving the same top level API. `js-libp2p` is only a skeleton and should not be installed directly, if you are looking for a prebundled libp2p stack, please check:
4343

44-
- [libp2p-ipfs-nodejs](https://github.com/ipfs/js-libp2p-ipfs-nodejs) - The libp2p build used by js-ipfs when run in Node.js
45-
- [libp2p-ipfs-browser](https://github.com/ipfs/js-libp2p-ipfs-browser) - The libp2p build used by js-ipfs when run in a Browser (that supports WebRTC)
44+
- [libp2p-ipfs-nodejs](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/libp2p-nodejs.js) - The libp2p build used by js-ipfs when run in Node.js
45+
- [libp2p-ipfs-browser](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/libp2p-browser.js) - The libp2p build used by js-ipfs when run in a Browser (that supports WebRTC)
4646

4747
If you have developed a libp2p bundle, please consider submitting it to this list so that it can be found easily by the users of libp2p.
4848

examples/README.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
### Examples list
1+
# `js-libp2p` Examples and Tutorials
22

3-
Here are some examples built with libp2p bundles.
3+
In this folder, you can find a variety of examples to help you get started in using js-libp2p, in Node.js and in the Browser. Every example as a specific purpose and some of each incorporate a full tutorial that you can follow through, helping you expand your knowledge about libp2p and p2p networks in general.
44

5-
- https://github.com/ipfs/js-libp2p-ipfs/tree/master/examples/echo
6-
- https://github.com/ipfs/js-libp2p-ipfs/tree/master/examples/chat
5+
Let us know if you find any issue or if you want to contribute and add a new tutorial, feel welcome to submit a PR, thank you!
6+
7+
## Examples
8+
9+
- [In Node.js](./nodejs)
10+
- [echo](./nodejs/echo)
11+
- [chat](./nodejs/chat)
12+
- [In the browser](./browser)
13+
- [mapper](./browser/mapper)

examples/archived/README.md

-4
This file was deleted.

examples/archived/node-1.js

-39
This file was deleted.

examples/archived/node-2.js

-31
This file was deleted.

examples/browser-chat/.gitkeep

Whitespace-only changes.

examples/browser-echo/README.md

Whitespace-only changes.

examples/browser/mapper/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bundle.js

examples/browser/mapper/package.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "mapper",
3+
"version": "0.0.0",
4+
"description": "",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"bundle": "browserify src/index.js --require browserify-zlib-next:zlib > public/bundle.js",
8+
"serve": "static public -p 9090 -H '{\"Cache-Control\": \"no-cache, must-revalidate\"}'",
9+
"mon": "nodemon --exec \"npm run start\" --ignore public/bundle.js",
10+
"start": "npm run bundle && npm run serve"
11+
},
12+
"license": "MIT",
13+
"devDependencies": {
14+
"browserify": "^14.0.0",
15+
"browserify-optional": "^1.0.0",
16+
"browserify-zlib-next": "^1.0.1",
17+
"concat-stream": "^1.6.0",
18+
"detect-dom-ready": "^1.0.2",
19+
"node-static": "^0.7.9",
20+
"nodemon": "^1.11.0"
21+
},
22+
"dependencies": {}
23+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<title>p2p mapper</title>
6+
</head>
7+
<body>
8+
<h1>p2p mapper</h1>
9+
<div id="my-peer"></div>
10+
<div id="swarm"></div>
11+
12+
<script src="bundle.js"></script>
13+
</body>
14+
</html>
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
const PeerInfo = require('peer-info')
4+
const Node = require('../../../../test/browser-bundle/browser-bundle.js')
5+
6+
function createNode (callback) {
7+
PeerInfo.create((err, peerInfo) => {
8+
if (err) {
9+
return callback(err)
10+
}
11+
12+
const peerIdStr = peerInfo.id.toB58String()
13+
const ma = `/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss/ipfs/${peerIdStr}`
14+
15+
peerInfo.multiaddrs.add(ma)
16+
17+
const node = new Node(peerInfo, undefined, { webRTCStar: true })
18+
19+
node.idStr = peerIdStr
20+
callback(null, node)
21+
})
22+
}
23+
24+
module.exports = createNode

examples/browser/mapper/src/index.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict'
2+
3+
const domReady = require('detect-dom-ready')
4+
const createNode = require('./create-node')
5+
6+
domReady(() => {
7+
const myPeerDiv = document.getElementById('my-peer')
8+
const swarmDiv = document.getElementById('swarm')
9+
10+
createNode((err, node) => {
11+
if (err) {
12+
return console.log('Could not create the Node, check if your browser has WebRTC Support', err)
13+
}
14+
15+
node.on('peer:discovery', (peerInfo) => {
16+
console.log('Discovered a peer')
17+
const idStr = peerInfo.id.toB58String()
18+
console.log('Discovered: ' + idStr)
19+
20+
node.dial(peerInfo, (err, conn) => {
21+
if (err) { return console.log('Failed to dial:', idStr) }
22+
})
23+
})
24+
25+
node.on('peer:connect', (peerInfo) => {
26+
const idStr = peerInfo.id.toB58String()
27+
console.log('Got connection to: ' + idStr)
28+
const connDiv = document.createElement('div')
29+
connDiv.innerHTML = 'Connected to: ' + idStr
30+
connDiv.id = idStr
31+
swarmDiv.append(connDiv)
32+
})
33+
34+
node.on('peer:disconnect', (peerInfo) => {
35+
const idStr = peerInfo.id.toB58String()
36+
console.log('Lost connection to: ' + idStr)
37+
document.getElementById(idStr).remove()
38+
})
39+
40+
node.start((err) => {
41+
if (err) {
42+
return console.log('WebRTC not supported')
43+
}
44+
45+
const idStr = node.peerInfo.id.toB58String()
46+
47+
const idDiv = document
48+
.createTextNode('Node is ready. ID: ' + idStr)
49+
50+
myPeerDiv.append(idDiv)
51+
52+
console.log('Node is listening o/')
53+
54+
// NOTE: to stop the node
55+
// node.stop((err) => {})
56+
})
57+
})
58+
})

examples/node-browser-chat/README.md

Whitespace-only changes.

examples/node-browser-echo/README.md

Whitespace-only changes.

examples/node-chat/.gitkeep

Whitespace-only changes.

examples/node-echo/README.md

Whitespace-only changes.

examples/nodejs/chat/dialer.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict'
2+
/* eslint-disable no-console */
3+
4+
const PeerId = require('peer-id')
5+
const PeerInfo = require('peer-info')
6+
const Node = require('../../../../test/nodejs-bundle/nodejs-bundle.js')
7+
const multiaddr = require('multiaddr')
8+
const pull = require('pull-stream')
9+
const async = require('async')
10+
const Pushable = require('pull-pushable')
11+
const p = Pushable()
12+
let idListener
13+
14+
async.parallel([
15+
(callback) => {
16+
PeerId.createFromJSON(require('./peer-id-dialer'), (err, idDialer) => {
17+
if (err) {
18+
throw err
19+
}
20+
callback(null, idDialer)
21+
})
22+
},
23+
(callback) => {
24+
PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
25+
if (err) {
26+
throw err
27+
}
28+
callback(null, idListener)
29+
})
30+
}
31+
], (err, ids) => {
32+
if (err) throw err
33+
const peerDialer = new PeerInfo(ids[0])
34+
peerDialer.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/0'))
35+
const nodeDialer = new Node(peerDialer)
36+
37+
const peerListener = new PeerInfo(ids[1])
38+
idListener = ids[1]
39+
peerListener.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/10333'))
40+
nodeDialer.start((err) => {
41+
if (err) {
42+
throw err
43+
}
44+
45+
console.log('Dialer ready, listening on:')
46+
47+
peerListener.multiaddrs.forEach((ma) => {
48+
console.log(ma.toString() + '/ipfs/' + idListener.toB58String())
49+
})
50+
51+
nodeDialer.dialByPeerInfo(peerListener, '/chat/1.0.0', (err, conn) => {
52+
if (err) {
53+
throw err
54+
}
55+
console.log('nodeA dialed to nodeB on protocol: /chat/1.0.0')
56+
console.log('Type a message and see what happens')
57+
// Write operation. Data sent as a buffer
58+
pull(
59+
p,
60+
conn
61+
)
62+
// Sink, data converted from buffer to utf8 string
63+
pull(
64+
conn,
65+
pull.map((data) => {
66+
return data.toString('utf8').replace('\n', '')
67+
}),
68+
pull.drain(console.log)
69+
)
70+
71+
process.stdin.setEncoding('utf8')
72+
process.openStdin().on('data', (chunk) => {
73+
var data = chunk.toString()
74+
p.push(data)
75+
})
76+
})
77+
})
78+
})

examples/nodejs/chat/listener.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict'
2+
/* eslint-disable no-console */
3+
4+
const PeerId = require('peer-id')
5+
const PeerInfo = require('peer-info')
6+
const Node = require('../../../../test/nodejs-bundle/nodejs-bundle.js')
7+
const multiaddr = require('multiaddr')
8+
const pull = require('pull-stream')
9+
const Pushable = require('pull-pushable')
10+
const p = Pushable()
11+
12+
PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
13+
if (err) {
14+
throw err
15+
}
16+
const peerListener = new PeerInfo(idListener)
17+
peerListener.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/10333'))
18+
const nodeListener = new Node(peerListener)
19+
20+
nodeListener.start((err) => {
21+
if (err) {
22+
throw err
23+
}
24+
25+
nodeListener.swarm.on('peer-mux-established', (peerInfo) => {
26+
console.log(peerInfo.id.toB58String())
27+
})
28+
29+
nodeListener.handle('/chat/1.0.0', (protocol, conn) => {
30+
pull(
31+
p,
32+
conn
33+
)
34+
35+
pull(
36+
conn,
37+
pull.map((data) => {
38+
return data.toString('utf8').replace('\n', '')
39+
}),
40+
pull.drain(console.log)
41+
)
42+
43+
process.stdin.setEncoding('utf8')
44+
process.openStdin().on('data', (chunk) => {
45+
var data = chunk.toString()
46+
p.push(data)
47+
})
48+
})
49+
50+
console.log('Listener ready, listening on:')
51+
peerListener.multiaddrs.forEach((ma) => {
52+
console.log(ma.toString() + '/ipfs/' + idListener.toB58String())
53+
})
54+
})
55+
})
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP",
3+
"privKey": "CAASpwkwggSjAgEAAoIBAQCaNSDOjPz6T8HZsf7LDpxiQRiN2OjeyIHUS05p8QWOr3EFUCFsC31R4moihE5HN+FxNalUyyFZU//yjf1pdnlMJqrVByJSMa+y2y4x2FucpoCAO97Tx+iWzwlZ2UXEUXM1Y81mhPbeWXy+wP2xElTgIER0Tsn/thoA0SD2u9wJuVvM7dB7cBcHYmqV6JH+KWCedRTum6O1BssqP/4Lbm2+rkrbZ4+oVRoU2DRLoFhKqwqLtylrbuj4XOI3XykMXV5+uQXz1JzubNOB9lsc6K+eRC+w8hhhDuFMgzkZ4qomCnx3uhO67KaICd8yqqBa6PJ/+fBM5Xk4hjyR40bwcf41AgMBAAECggEAZnrCJ6IYiLyyRdr9SbKXCNDb4YByGYPEi/HT1aHgIJfFE1PSMjxcdytxfyjP4JJpVtPjiT9JFVU2dd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0odqjFv0J2qki6fDs0HKwq4kojAXI1Md8Th0eobNjsy21fEEJT7uKMJdovI/SErI=",
4+
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCaNSDOjPz6T8HZsf7LDpxiQRiN2OjeyIHUS05p8QWOr3EFUCFsC31R4moihE5HN+FxNalUyyFZU//yjf1pdnlMJqrVByJSMa+y2y4x2FucpoCAO97Tx+iWzwlZ2UXEUXM1Y81mhPbeWXy+wP2xElTgIER0Tsn/thoA0SD2u9wJuVvM7dB7cBcHYmqV6JH+KWCedRTum6O1BssqP/4Lbm2+rkrbZ4+oVRoU2DRLoFhKqwqLtylrbuj4XOI3XykMXV5+uQXz1JzubNOB9lsc6K+eRC+w8hhhDuFMgzkZ4qomCnx3uhO67KaICd8yqqBa6PJ/+fBM5Xk4hjyR40bwcf41AgMBAAE="
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "QmcrQZ6RJdpYuGvZqD5QEHAv6qX4BrQLJLQPQUrTrzdcgm",
3+
"privKey": "CAASqAkwggSkAgEAAoIBAQDLZZcGcbe4urMBVlcHgN0fpBymY+xcr14ewvamG70QZODJ1h9sljlExZ7byLiqRB3SjGbfpZ1FweznwNxWtWpjHkQjTVXeoM4EEgDSNO/Cg7KNlU0EJvgPJXeEPycAZX9qASbVJ6EECQ40VR/7+SuSqsdL1hrmG1phpIju+D64gLyWpw9WEALfzMpH5I/KvdYDW3N4g6zOD2mZNp5y1gHeXINHWzMF596O72/6cxwyiXV1eJ000k1NVnUyrPjXtqWdVLRk5IU1LFpoQoXZU5X1hKj1a2qt/lZfH5eOrF/ramHcwhrYYw1txf8JHXWO/bbNnyemTHAvutZpTNrsWATfAgMBAAECggEAQj0obPnVyjxLFZFnsFLgMHDCv9Fk5V5bOYtmxfvcm50us6ye+T8HEYWGUa9RrGmYiLweuJD34gLgwyzE1RwptHPj3tdNsr4NubefOtXwixlWqdNIjKSgPlaGUL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