Skip to content

Latest commit

 

History

History
 
 

connection-encryption

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

@libp2p/example-connection-encryption

libp2p.io Discuss codecov CI

An example of how to configure connection encrypters

Table of contents

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.

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 { yamux } from '@chainsafe/libp2p-yamux',
import { noise } from '@chainsafe/libp2p-noise'

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

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

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.