diff --git a/examples/encrypted-communications/1.js b/examples/encrypted-communications/1.js index d15b811d..055881cc 100644 --- a/examples/encrypted-communications/1.js +++ b/examples/encrypted-communications/1.js @@ -1,63 +1,52 @@ 'use strict' -const libp2p = require('../../') +const Libp2p = require('../../') const TCP = require('libp2p-tcp') -const SPDY = require('libp2p-spdy') +const Mplex = require('libp2p-mplex') const SECIO = require('libp2p-secio') const PeerInfo = require('peer-info') -const waterfall = require('async/waterfall') -const parallel = require('async/parallel') -const pull = require('pull-stream') -const defaultsDeep = require('@nodeutils/defaults-deep') -class MyBundle extends libp2p { - constructor (_options) { - const defaults = { - modules: { - transport: [ TCP ], - streamMuxer: [ SPDY ], - connEncryption: [ SECIO ] +const pipe = require('it-pipe') + +const createNode = async () => { + const peerInfo = await PeerInfo.create() + peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') + + const node = await Libp2p.create({ + peerInfo, + modules: { + transport: [TCP], + streamMuxer: [Mplex], + connEncryption: [SECIO] + } + }) + + await node.start() + + return node +} + +;(async () => { + const [node1, node2] = await Promise.all([ + createNode(), + createNode() + ]) + + node2.handle('/a-protocol', ({ stream }) => { + pipe( + stream, + async function (source) { + for await (const msg of source) { + console.log(msg.toString()) + } } - } - - super(defaultsDeep(_options, defaults)) - } -} - -function createNode (callback) { - let node - - waterfall([ - (cb) => PeerInfo.create(cb), - (peerInfo, cb) => { - peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') - node = new MyBundle({ - peerInfo - }) - node.start(cb) - } - ], (err) => callback(err, node)) -} - -parallel([ - (cb) => createNode(cb), - (cb) => createNode(cb) -], (err, nodes) => { - if (err) { throw err } - - const node1 = nodes[0] - const node2 = nodes[1] - - node2.handle('/a-protocol', (protocol, conn) => { - pull( - conn, - pull.map((v) => v.toString()), - pull.log() ) }) - node1.dialProtocol(node2.peerInfo, '/a-protocol', (err, conn) => { - if (err) { throw err } - pull(pull.values(['This information is sent out encrypted to the other peer']), conn) - }) -}) + const { stream } = await node1.dialProtocol(node2.peerInfo, '/a-protocol') + + await pipe( + ['This information is sent out encrypted to the other peer'], + stream + ) +})(); diff --git a/examples/encrypted-communications/README.md b/examples/encrypted-communications/README.md index 844f1be6..b0866a35 100644 --- a/examples/encrypted-communications/README.md +++ b/examples/encrypted-communications/README.md @@ -4,7 +4,7 @@ libp2p can leverage the encrypted communications from the transports it uses (i. 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. -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. +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. # 1. Set up encrypted communications with SECIO @@ -12,24 +12,21 @@ We will build this example on top of example for [Protocol and Stream Multiplexi 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. -To add it to your libp2p bundle, all you have to do is: +To add it to your libp2p configuration, all you have to do is: ```JavaScript +const Libp2p = require('libp2p') const SECIO = require('libp2p-secio') -class MyBundle extends libp2p { - constructor (peerInfo) { - const defaults = { - modules: { - transport: [ TCP ], - streamMuxer: [ SPDY ], - // Attach secio as the crypto channel to use - connEncryption: [ SECIO ] - } +const createNode = () => { + return Libp2p.create({ + modules: { + transport: [ TCP ], + streamMuxer: [ Mplex ], + // Attach secio as the crypto channel to use + connEncryption: [ SECIO ] } - - super(defaultsDeep(_options, defaults)) - } + }) } ```