1
0
mirror of https://github.com/fluencelabs/js-libp2p synced 2025-03-31 14:51:05 +00:00
Alex Potsides de30c2cec7
feat!: limit protocol streams per-connection ()
* feat: limit protocol streams per-connection

Uses the `maxInboundStreams` and `maxOutboundStreams` of the `registrar.handle`
opts to limit the number of concurrent streams open on each connection
on a per-protocol basis.

Both values default to 1 so some tuning will be necessary to set
appropriate values for some protocols.

* chore: make error codes consistent

* chore: fix up examples
2022-06-17 15:46:31 +02:00
..
2022-03-28 14:30:27 +01:00
2022-03-28 14:30:27 +01:00

Connection Encryption

libp2p can leverage the encrypted communications from the transports it uses (i.e WebRTC). To ensure that every connection is encrypted, independently of how it was set up, libp2p also supports a set of modules that encrypt every communication established.

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/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

We will build this example on top of example for Protocol and Stream Multiplexing. You will need the @chainsafe/libp2p-noise module to complete it, go ahead and npm install @chainsafe/libp2p-noise.

To add them to your libp2p configuration, all you have to do is:

import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
import { Mplex } from '@libp2p/mplex'
import { Noise } from '@chainsafe/libp2p-noise'

const createNode = async () => {
  return await createLibp2p({
    transports: [ new TCP() ],
    streamMuxers: [ new Mplex() ],
    // Attach noise as the crypto channel to use
    conectionEncrypters: [ new Noise() ]
  })
}

And that's it, from now on, all your libp2p communications are encrypted. Try running the example 1.js to see it working.