7a: Replication: Sharing Data With Other Computers

Now to the fun part: let's share this hypercore of chat messages with another machine (or peer). Hypercore supports this, and calls the process of two machine exchanging messages of a feed replication.

hypercore and the whole Dat ecosystem uses asymmetric key encryption scheme to sign and encrypt all the data. This guarantees data integrity when we share data with another peer.

So, let's dive deeper. There are 3 keys to have in mind with Hypercore :stars:

Public Key 🔑

This key is globally unique.

It is used to identify our feed publicly, and also allows us to verify that data has not been tampered with when replicating with peers we do not nessesarily trust.

We can also encrypt a message using someone else's public key so that nobody else but them can decrypt it.

Secret Key 🔐

In hypercore, only the feed owner (whoever has the secret key) can write to the log. This is kept only on the local machine and never shared with anyone.

The secret key is used to decrypt messages encrypted to our public key, as well as for signing messages we've written to prove we are the original author.

Discovery Key 🌍

We recognize our feed by its unique public key and allow only the peers who know this key to be able to exchange information.

We might not want other parties to know our public key, since it uniquely identifies us. To avoid such a scenario, we can make use of another key, the discovery key. This is a hash of the public key. This key is used to discover peers without leaking the public key.

Hypercore and the Public Key

If someone gives us their public key, we'd might like to be able to read their feed.

To achieve this, hypercore accepts a second parameter in its constructor:

hypercore(<storage>, <public key>, <options>)

Each machine compares what parts of the hypercore they already have locally, and shares those parts that the other side is missing. This is how all peers in a p2p network can eventually converge on the same set of data, even if some peers are not always online.

Exercise

Let's look at these keys in your feed!

Try extending single-chat.js to print them out:

feed.ready(function () {
  console.log('public key:', feed.key.toString('hex'))
  console.log('discovery key:', feed.discoveryKey.toString('hex'))
  console.log('secret key:', feed.secretKey.toString('hex'))
})

Once you solve this exercise continue to exercise 7b