Check out a free preview of the full LevelDB & Crypto course
The "Working with merkle DAGs" Lesson is part of the full, LevelDB & Crypto course featured in this preview video. Here's what you'd learn in this lesson:
James demonstrates how to use merkle DAGs with node using shasum, which checks for SHA checksums.
Transcript from the "Working with merkle DAGs" Lesson
[00:00:00]
>> Merkle DAGs are actually very simple and we can build one on the command line with just simple tools like Shazam, because it doesn't actually matter at all, how we format things like our links. All that matters is that we have a system for doing that so we can embed hashes into our documents.
[00:00:19]
However we like we could just say a message it's like, yo, here's the hash and all of the properties of Merkle DAGs will still hold so long as we have a consistent way of verifying them. Even if it's ourselves running a bunch of commands, whatever. Okay, so we'll call it mdag.
[00:00:43]
Okay, so what we can do now, the first thing we need to build a Merkel DAG is a message. So here's a message, Hello. All right, so this is five characters plus a new line if we want to take the hash of that, we can run this command shasum and we get a hash back.
[00:01:07]
So now this is our first message in our Merkel DAG. If we want to make another message that sort of says, maybe there's some relationship between these two documents like this one comes first and then this one comes second. So what we can do is we can embed that hash into our second document and then it will point back at the first document.
[00:01:29]
So we can come up with a little impromptu, I am the second doc, can be our message. And then on the second line, we'll just include the hash of the previous message. So when we do that, we get a new hash. And what's cool now is, if we modified the first message, it would be a completely different hash and so we can always verify the contents of every message.
[00:02:04]
So this is sort of like an example of a data structure. Like if we have a third message, I'm the 3rd doc we can include. We can either point at the first message in which case our data structure would sort of branch, cause it's a graph not just a log.
[00:02:19]
But if we want to keep it flat, we can just embed the second hash to our message. Now we have a new hash. So you could imagine we could store each of these messages under a file name, that's that hash or we could store them in level DB or store them any place.
[00:02:39]
That's basically all that you need to build a Merkel DAG. It's very, very simple idea with the hash.
>> What do you do with the final hash you got, how will you use it?
>> Right, so if I, if I know that this is the content. If anyone in the world hands me this message I can verify that the hash should be this right here.
[00:03:04]
So, if I know through some out of band mechanism that the head of this very long potentially log or graph of data is that, then this hash is all the information that I need to verify every document in that whole graph. So if you know the hash of the head, you can get all of the other documents from untrusted peers on the network.
[00:03:30]
If you want your structure to have other properties, like if you want to ensure that the users that are allowed to publish new messages are trusted. You can use things like signatures, you can embed those in with the rest of this content and all of the properties of hashes still hold, but then you also get the benefits of identity and verification.
[00:03:57]
So in that kind of a network identities are just public keys and anyone who knows the public key and anyone who knows the head of the hash can verify every other piece of information about the network. So you don't need to have like a server that's trusted that's can dispel the truth.
[00:04:21]
You can sort of, with these tricks, know what's true for yourself maybe just with a couple of extra pieces of information.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops