Skip to content

Commit 506af15

Browse files
vasco-santosjacobheun
authored andcommitted
refactor: examples/encrypted-communications (#499)
* refactor: examples/encrypted-communications * chore: address review
1 parent 9f0f08f commit 506af15

File tree

2 files changed

+49
-63
lines changed

2 files changed

+49
-63
lines changed
Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,52 @@
11
'use strict'
22

3-
const libp2p = require('../../')
3+
const Libp2p = require('../../')
44
const TCP = require('libp2p-tcp')
5-
const SPDY = require('libp2p-spdy')
5+
const Mplex = require('libp2p-mplex')
66
const SECIO = require('libp2p-secio')
77
const PeerInfo = require('peer-info')
8-
const waterfall = require('async/waterfall')
9-
const parallel = require('async/parallel')
10-
const pull = require('pull-stream')
11-
const defaultsDeep = require('@nodeutils/defaults-deep')
12-
13-
class MyBundle extends libp2p {
14-
constructor (_options) {
15-
const defaults = {
16-
modules: {
17-
transport: [ TCP ],
18-
streamMuxer: [ SPDY ],
19-
connEncryption: [ SECIO ]
20-
}
21-
}
228

23-
super(defaultsDeep(_options, defaults))
24-
}
25-
}
9+
const pipe = require('it-pipe')
2610

27-
function createNode (callback) {
28-
let node
29-
30-
waterfall([
31-
(cb) => PeerInfo.create(cb),
32-
(peerInfo, cb) => {
33-
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
34-
node = new MyBundle({
35-
peerInfo
36-
})
37-
node.start(cb)
11+
const createNode = async () => {
12+
const peerInfo = await PeerInfo.create()
13+
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
14+
15+
const node = await Libp2p.create({
16+
peerInfo,
17+
modules: {
18+
transport: [TCP],
19+
streamMuxer: [Mplex],
20+
connEncryption: [SECIO]
3821
}
39-
], (err) => callback(err, node))
40-
}
22+
})
4123

42-
parallel([
43-
(cb) => createNode(cb),
44-
(cb) => createNode(cb)
45-
], (err, nodes) => {
46-
if (err) { throw err }
24+
await node.start()
4725

48-
const node1 = nodes[0]
49-
const node2 = nodes[1]
26+
return node
27+
}
5028

51-
node2.handle('/a-protocol', (protocol, conn) => {
52-
pull(
53-
conn,
54-
pull.map((v) => v.toString()),
55-
pull.log()
29+
;(async () => {
30+
const [node1, node2] = await Promise.all([
31+
createNode(),
32+
createNode()
33+
])
34+
35+
node2.handle('/a-protocol', ({ stream }) => {
36+
pipe(
37+
stream,
38+
async function (source) {
39+
for await (const msg of source) {
40+
console.log(msg.toString())
41+
}
42+
}
5643
)
5744
})
5845

59-
node1.dialProtocol(node2.peerInfo, '/a-protocol', (err, conn) => {
60-
if (err) { throw err }
61-
pull(pull.values(['This information is sent out encrypted to the other peer']), conn)
62-
})
63-
})
46+
const { stream } = await node1.dialProtocol(node2.peerInfo, '/a-protocol')
47+
48+
await pipe(
49+
['This information is sent out encrypted to the other peer'],
50+
stream
51+
)
52+
})();

examples/encrypted-communications/README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,29 @@ libp2p can leverage the encrypted communications from the transports it uses (i.
44

55
We call this usage a _connection upgrade_ where given a connection between peer A to peer B, a protocol handshake can be performed that gives that connection new properties.
66

7-
A byproduct of having these encrypted communications modules is that we can authenticate the peers we are dialing to. You might have noticed that every time we dial to a peer in libp2p space, we always use its PeerId at the end (e.g /ip4/127.0.0.1/tcp/89765/ipfs/QmWCbVw1XZ8hiYBwwshPce2yaTDYTqTaP7GCHGpry3ykWb), this PeerId is generated by hashing the Public Key of the peer. With this, we can create a crypto challenge when dialing to another peer and prove that peer is the owner of a PrivateKey that matches the Public Key we know.
7+
A byproduct of having these encrypted communications modules is that we can authenticate the peers we are dialing to. You might have noticed that every time we dial to a peer in libp2p space, we always use its PeerId at the end (e.g /ip4/127.0.0.1/tcp/89765/p2p/QmWCbVw1XZ8hiYBwwshPce2yaTDYTqTaP7GCHGpry3ykWb), this PeerId is generated by hashing the Public Key of the peer. With this, we can create a crypto challenge when dialing to another peer and prove that peer is the owner of a PrivateKey that matches the Public Key we know.
88

99
# 1. Set up encrypted communications with SECIO
1010

1111
We will build this example on top of example for [Protocol and Stream Multiplexing](../protocol-and-stream-multiplexing). You will need the module `libp2p-secio` to complete it, go ahead and `npm install libp2p-secio`.
1212

1313
SECIO is the crypto channel developed for IPFS, it is a TLS 1.3 like crypto channel that established an encrypted communication channel between two peers.
1414

15-
To add it to your libp2p bundle, all you have to do is:
15+
To add it to your libp2p configuration, all you have to do is:
1616

1717
```JavaScript
18+
const Libp2p = require('libp2p')
1819
const SECIO = require('libp2p-secio')
1920

20-
class MyBundle extends libp2p {
21-
constructor (peerInfo) {
22-
const defaults = {
23-
modules: {
24-
transport: [ TCP ],
25-
streamMuxer: [ SPDY ],
26-
// Attach secio as the crypto channel to use
27-
connEncryption: [ SECIO ]
28-
}
21+
const createNode = () => {
22+
return Libp2p.create({
23+
modules: {
24+
transport: [ TCP ],
25+
streamMuxer: [ Mplex ],
26+
// Attach secio as the crypto channel to use
27+
connEncryption: [ SECIO ]
2928
}
30-
31-
super(defaultsDeep(_options, defaults))
32-
}
29+
})
3330
}
3431
```
3532

0 commit comments

Comments
 (0)