Video details

Blockchain 101 (or How to Build a Blockchain with JavaScript)

JavaScript
07.08.2021
English

In this webinar, you will learn about how a blockchain is built, the most common inner workings, and the different use cases and applications. You will learn from the live code demo how to build a basic blockchain with JavaScript.
PUBLICATION PERMISSIONS: Original video was published with the Creative Commons Attribution license (reuse allowed). Link: https://www.youtube.com/watch?v=9HrEwEhIzGo

Transcript

Welcome, everyone again. Thank you so much for joining this Wise Line Academy course. As you may have seen, probably in the description and as the name implies, its purpose is to serve as an introduction to blockchain how this technology works. What are the fundamental concepts? What are the main mechanisms behind this and how they relate to each other? We're going to be having some theory parts, as you'll see, and afterwards falling, we'll have some practical coating parts with the gold to apply what we just saw in the previous concepts. This is the agenda of the course. Let's just go through it quickly because as you'll see, they are grouped depending on the topics. We'll be looking at the blockchain basics, water blocks, how is the chain formed and what are the notes that comprise this blockchain? Afterwards we'll be looking at transactions, which are a very important part of how the blockchain works, also about digital signatures, which are essential for the control of these transactions. And finally, a couple of more abstract concepts such as block mining, what are wallets and followed by the TNA session. And before starting, as I mentioned, we'll have some practical parts. And the idea of this, apart from applying the concepts, is that at the end we have a very simplified but functional example of the main functions or the main functionality of a blockchain, so that you can have a clear idea. Next slide, please. So with that being said, I'll leave you with someone who will talk about the fundamental concepts of a blockchain. Thank you for. So let's start with asking the question of what is locked in. So. So a simple technical description would be saying that it is the structure that holds historical transactional records and it ensures security and transparency. But we can see it as a database database that holds transactional data as simple as that. And with transactional data, I mean, not only currencies like data like sending money or sending Bitcoin, it could be information. So its main purpose is to have a track of this history across the time. Next slide, please. So as you know, blockchain is composed by blocks. It's what its name says. But what is the block? So a block is the fundamental part of the blockchain. It's main purpose to hold information. So it holds information about the transactions that has been made across the time of existence of blockchain, but also holds information about the block itself, such as the time stamps, which is when this block was created. We will cover more about the blog creation later, but yes, also, the blocks are identified by hash, which is unique and also by a height. And the height is basically how deep the block is in the blockchain, the deeper in the blockchain, the more recent this data is. So in this blockchain, each one of the blocks is related by each other by this hash. So can we go to the next slide, please. So as I mentioned, each one of the blocks is linked to each other by the hash. This hash is calculated accordingly to the information that the block contains. So this can be the transactional data the time it was created. So this is to create a unique string, a unique, a unique key to identify a block. So this doesn't repeat. So if the information changes inside the block, so will the hash of the block. So since I mentioned that each one of the blocks is related to each other by the hash of the previous block, here we have an example. So here we have three blocks, one, two, and three, and the block three is related to number two by hash, and the number two is related to the number one by its hash and the number one. As you can see, since it doesn't have a previous block, it is called the Genesis block, which is the first block in the blockchain. This have a Dom previous hash. So because it doesn't point to anything. So according to in order to maintain consistency, for example, if we change the value of the transactions or anything, any data inside the block, the block number two, the hash will change. Right. So instead of being six be you one, it will be completely another thing. So the block number three will not know who to refer to because he has six B one doesn't exist anymore. So the blockchain will lose consistency. It will lose sense. Also, another thing that is important about blockchain is that it is a centralized lecture. What does that mean? We will cover more about that later. But what it means is that no one holds authority over the network, and by that it means that there is not a single person that decides what goes in and how it goes inside the blockchain. So can we go to the next type? Yeah. To make it simpler. We made an analogy. So basically, this is a book. We want to add a new page inside the book. So we have a couple of Editors that basically made the consensus to decide whether this page is added or not inside the book. So they together make a decision. For example, in this example, they say, yeah, this page that can be added to this book. So once they make our consensus, he decided to the book. So if the editor decides that or at least the majority decide that it doesn't have to be inside the work. Well, it is not added. So in this same way works the blockchain. So in this case, the book would be the blockchain. The base would be the blocks. The Editors would be the notes. We will talk more of this in the next slide, please. So. The notes are basically the participants inside the blockchain. So these are the ones that I mentioned. They make a consensus for whether the new information should be there or not inside the blockchain. These are the ones that receive the messages. The messages could be new transactions and they emit Disney information through the whole blockchain. And once this message is emitted this new transaction, they decide whether this information is consistent or not, if it should be added or not inside the blockchain. So each one of them the notes are connected through each other using a peer to peer protocol, so there's no central hop. There's not a single note that can take arbitrary the whole decisions inside the blockchain. So each one of these holds a copy of the whole blockchain. This helps them decide whether the new information makes sense or not for the rest of the blockchain. So can we go to the next slide? So in summary, we can say that blockchain is a centralized database. Basically, no one holds authority over the whole network. It is appear to be our network. As we just mentioned, each one of the nodes are connected. Each one can meet and can receive messages. They can both whether a new transaction should be added or not inside the blockchain and also a mutable. This has to do that. It is hard to change the information of the blocks. It would lose sense if we try to do that. So the information inside the blockchain cannot be changing. Next slide. So I'll leave you with a bit which will help us through show. We now an example of how blockchain works to go. Okay, so this is it. Once again, let me share my screen. We can start calling a little bit. Wait a second. Okay, so let's get started. I want to introduce you. Hope we are going to we're going to start developing this practical course. We will split this practical exercise into three parts. So can you make your font larger, please? Yeah. Sorry. Thank you. Thanks for the reminder. Is that okay? That's perfect. Thank you. Okay. Cool. So I was mentioning we will split this course into three parts as the theory goes forward. Let's start first with the previous concept that we have seen before. That is, for example, how a blog is created. Where are the main properties of that I lost and another file that we will be creating for setting up pretty much on the configuration for our blockchain application. Let me just this is a very small application building JavaScript. So we are not using any hard thing to understand just plain JavaScript and we are using nods to just install some dependencies for our application to run. Right. So having said that, let's begin with the course. If you see, we have two files so far this file will describe pretty much how a block will look like for our application. And Steve was mentioning we will be starting describing the main properties for a lock. So you see, we have a timestamp. This is just to identify a unique identified for this block. Right? And we will have a second property for holding the transaction. Remember that a block can have transaction. And for example, in talking about blockchain, specifically, blocks not only can have transactions, but for example, it can have any type of data that you want to process within the blockchain. There are some examples that, for example, manage official documents to make sure that they are authentic. And there are lots of examples that are not related, just cryptocurrencies. Right. So for this example, we are just using some coins to store in here some transactions transferring some coins, and we will have another property that is the previous hash, as the one was mentioning. Every block is chained together with the previous one so that we can make sure that we have that chain pattern. Right. And let's add a third property, a fourth property. This is the non. And we are going to explain later what's the purpose of this property. For now, let's define it as an integer with zero value as a default, and we are going to use another property. We're going to create another property called the cash. And for now we are going to set an EMP string for it. Right. If you can see we have one method in here and let's start calling it. Remember that each block needs to have a hash representation. A hash is nothing more than a string representation passing some input data. In this case, we are going to pass the properties of a blog to the hash function and for the cash function algorithm. We are going to use the chat for 256 algorithm, and this is just to generate the cash. Right. So let's start creating our hash for this log. And what we're going to do is say call the function chat on 256, and we are going to to start passing for example, the time stamp. And we are going to concatenate pretty much every property of block. We are going to say this transaction, but this is an a. Right. Sorry, I just miss here username. So. Right. And we are going to assist an arrive. We are going to get on a string fight his way so that it converts into a string. And we can just concatenate with other string price. And we are going to say these previous cash, and we are going to concatenate the non. Now that we are using the non property, we are going to touch in depth how this is used. But for now these loans will give will give us the opportunity to start generating a different path every time that we attempt to minor block. When it comes to mining blocks. Remember that we need to start calculating a hash for the blog that is going to be added to the chain. So don't worry, we're going to show you how this. This works for the lessons, but for now this is the variable that will let us generate a different hash for the given block. Right. So okay, that's it for the block file. Let's go jump to the blockchain file. And here we are going to pretty much say how our application is going to behave. So let's start finding some properties. I'm going to create the property called chain. And obviously, as you may know, this is blockchain. We will need a way to store every block that is added to the chain. So for that, we are going to store those blocks in this property. And obviously this is an a right for that. For now, I'm just creating this as an empty. And I'm going to create another property here that is difficulty. And for blockchain that use something called for the consensus protocol, maybe we are going to see this for the lessons. For this example, we are simulating a blockchain that use proof of work. For example, in the case of Bitcoin that uses proof of work, it means that for every block that needs to be added to the chain, some minors or computers that are in the peer to peer network needs to find or to solve a mathematical problem. And the one who solves that mathematical problem is the one that has the right to add the given block into the chain. Right. So this variable in here will help or to set the difficulty for mining block. You will see later how this work in action. But so now let's have this in mind. We are going to create a third property for sending transactions and every transaction before it can be submitted to the blockchain need to be evaluated before. Right. So what's evaluated in the process? We need to evaluate that, for example, transaction hold valid information that is a valid recipient and a valid sender. Right. Obviously, for example, validating that points are enough to send depending the people who is sending the coins. And in real world, this is a pretty much complex property because it is involved a lot in there. But for now, let's just find this arrive that will hold every transaction that we went to the blockchain. And we are going to add a very, very simple validation whether the transaction is valid or not, they can be added to the chain. Right. And finally, we are going to create a four property called mine reward mining reward. And for the consensus protocol, that is, when a note minds block and it can be added to the chain, the computer or the note that wins or solve this mathematical problem receives a reward for the work. Right. So for this example, we're going to say that, sorry, this is not an MP, right. This is just a number. And we are going to say that the note that is capable to mine a certain block will receive 100 coins as the reward for for mine. Right. So here you can see the the concept of the mining in blockchain write you, for example, deploy a note with the software that can mind transactions and you receive coins for your computer to do the work. Right. So that's the idea behind in. And if you see below, we have two more methods. Create a Genesis block. As Steven mentioned, the first block added to the chain is called the Genesis block. So let's implement this function. And what we are going to do here is just create a block class that is from the block file and we are going to say new lock and we are going to pass the time time for JavaScript. We use the date that now function. It returns the Unix timestamps and we are going to pass a transactions. Alright. This is the first block in the chain. We don't have any transactions coming through the network. Right. So we just said an MPI and for the previous cash here we set an ink string because, you know, this is the first block. There are no previous blogs behind it. So that's it. We have our Genesis block created and let's go back to the constructor and for this, let's create the MRI. But this time let's use the function that we created and that's it. Once we initialize this blockchain class, we will have our Genesis not created right at the instance moment. And let's code the last method for that blockchain is very useful to know which is the latest block in the chain that is called the the height assess Evan mentioned. So let's return the last item one chain variable. And for that we are going to use the last few days on this property. Right. And yeah, that's pretty much it we are going to continue with the theory. This is the way that we are going to work. Once we cover more concepts, we go back to the practical lessons so we can see those in action. Right. So I think I leave you guys with Steven, right? Yes. There you go. Okay. So now we will continue with the transactions of the blockchain. How they work. Next, slide this. So next one, please. So basically, as we mentioned, the blocks contains transactional information so they can have multiple transactions inside of them. So a similar transaction is basically a small unit of a public record. So this public record could be an exchange of currency, but not necessarily. It could mean an exchange of information signature or something else. So as I mentioned before, if you try to change information of a block, it will change the hash of the block. But this also applies for the transactions because each one of the transactions has a hash and also has information related to the person who sends the transaction and who receives it. And also information like the amount of this example is, for example, an exchange of currency. So it says the amount of currently sent and also the time stamp, which refers to the time the transaction was created. So can we go to the next slide. So the detail signing this is for sending transactions. We can think of this, like when you create an account in the back. So for that you need an account. So to start send introductions need an account. So for this you in blocking you need two things the public key and the private key. These are mathematically generated. These in a bank would mean your account. For example, in your plastic, you have a number for your card and also the three numbers in the back. Right. So the public key is basically the one that you could share with the this is the information that you could identify yourself publicly. So for example, transported to the bank example, you can share, for example, your account number or your 60 numbers of your card, so you can ask for a transaction of money. Right. And the private key should be always kept secret. So for example, the three numbers in the back are the date of your card in this example. So this allows for accountability. The point in blockchain is not to secure or encrypt the message itself. It is to secure the point of or in, for example, to ensure that the transactions that are sent are sent by you and no one else. So that's why you shouldn't share your private key. Because if someone else has your private key, they could send transactions in your name. So next slide, please. So this is a real example. For example, you try to send a message across the network, so you sign a message with your own key, so then you send it to the network so they can all verify that that message actually comes from you. So next one, please. So we will cover this part in the coding example with. Okay. So going back to our our files, let's continue with what Steven was mentioning about the transaction and sign in. So for that, let's add after method into the plug file. So this will validate the current transactions for this blog. Let's just think for a moment that this method is implemented. I'm going to write check in here and we're going to use for each transaction on the block. If you see we are a for loop to iterate over the transactions property of the block. And for each transaction, we will need to check if that transaction is valid. So this method is not implemented yet, but later we're on implemented. Right. This will return a Bolian value saying that is valid or not true or false. If that is not the case, we are just returning fold. If everything goes well, we'll just return through. Right. And going back to the blockchain file. We are going to sorry, no, the blockchain file. Let's add a third file here to describe whole transactions will behave in this example. So you hit the same goes for transaction class. We define a constructor having three pro partition here we have from address. We have two address and an amount. This is mostly the basic properties that a transaction should comply with, and obviously from address is the one who tried to send funds to at 200. This is the recipient or the receiver and the amount of coins that they want to transfer. Right. And the same goes for the transaction. We will need to have a hash representation of this transaction object because we need to make sure that the transaction once is added to the blockchain. We need a way to validate that this transaction has been changed over time. Right. So we are pretty much doing the same with the blog. We are going to use the chat 256 algorithms to create a hash representation, and we are going to pass the same properties that we have in the constructor that is, but from others, we want to concatenate the to address and also the amount this amount. Okay. Cool. And let's just add your method here. This is for signing the transaction. As Steven mentioned, we will need a way to identify ourselves into the blockchain, and that is the purpose of the keepers. So every keeper holds obviously two kids. One is the public one and the other one is the private one. So for example, you can see this as an analogy. For example, the public key is your user ID in Facebook and your private key is your password, right? You share your username or your user ID, your Facebook, but not your password, right. This is the same for blockchain you share with others your public key so they can send you phones or recognize you within the network. And your password is just to authorize to send coins within the network and you only hold that private key. Right. So let's code this sign transaction method in here. We just receive a key pair. This is just we're using a library called Elliptic. This is for managing cryptographic functions for keepers in JavaScript. You can just look for it on NPM and it's the whole commentation. You've had an APS. So we're using that keeper. And the first thing we are going to do later when we start coding the mining process, we will need to ensure that we will need to identify whether this transaction is for reward mining for those blocks that, for example, mine for those accounts for those notes that mine some blocks. If that is the case, we will just set the from address to know. You will see further in action and with return through. This is a valid transaction. Right. And let's just I make use of the key fare that we're receiving here. We are going to get the access string representation of the key pair. You can get the key representation in any coding you want. For now we are going to use the encoding and we're going to say if the representation of the key pair that we're receiving here is different from the address that is sending the transaction. Obviously, we don't have the permissions to sign transaction for others. Right. So for example, you create a a transaction and let's say that your friend was one to send those codes for you. That is not possible because you need a way to authenticate yourself and your friend doesn't have your private key. Right. So that way we make sure that no one can sign transaction for others. And let's just add a couple of proper secure. Let's create a property called cash. And for that, let's say this dot calculate cash. I'm going to use the function that I just created before is one. And once we have the property hash, we are going to create the signature for this cash. So in a few words, what we sign in here is just the cash representation of this block. That's the way that cryptographic function works. So let's say let's create a variable called sign and let's return. Let's use the keeper repair, and we are going to use a method called sign. Obviously, this is going to sign the transaction page for us. And let's let's copy this in here. We're going to sign the hash of this transaction, and we are going to pass a second parameter that is the encoding that we're going to use for having for creating the signal to right there as well. Lots of encoding. For this example, we are going to use Pay 64. And once we have the signature, we are going to use another method to just create the signature property in here. Let's call it signature, and the signature will hold this property here will hold a signature that we just created before, but in another format. Right. Sorry. Is sign dot to there. This is another encoding that we use to represent the signature. Sorry. This is I may use to their in here. We're going to use the encoding once again. This is pretty much a standard in pretty much all block chains out there. They represent the signature next and coding. And obviously you can, for example, verify if the given signature corresponds to any public key. We are going to do that for the lessons. And once we have the signature, let's just print it out and that's it for our transaction class. And I think we can continue with the theory. Let's do very quickly recap what we do. We create the transaction class. We obviously add a method to calculate the hash representation for the transaction class, and we add a sign transaction method which will receive a key pair. Obviously, this key pair will need to match to the from address, so that we just make sure that the person who sends the coins is the same that wants to sign the transaction or in other words, to authorize this transaction. We created the signal to property for the transaction class, and obviously we send the transaction. We signed the hash with this transaction object, and that's pretty much it. Let's continue with the theory. I'm sorry to interrupt before we continue just to open a small, very small space for any blocking questions. Now that we're passing into the last block of theory, I know we've been covering a lot of topics and someone just asked in the channel. Well in the chat. So we've already shared the repo, but we can share the link again. I know we can be going a little bit fast because of time constraints, but the idea is that you just follow along with us right now, and if it's too fast, you can revisit the code. But is everything good so far, anyone has something in which they are lost that they would like to clarify. I. Just saw the question that you posted on the chat. Yeah, just for keeping things as easily as possible. We just manage one address per wallet. But for example, in more complex examples, we certainly can have multiple keepers that can manage some fonts. Or in this case, we have a centralized wallet to manage some funds. But for this example, let's just think let's just use I want keeper just to keep things as is as possible. But yeah, that's possible. That's a good question in it. Awesome. And cause I just shared the repo again in the chat. So if anyone was missing it, I look at it right there. Sorry about once you add something really quick. I don't think all the coding we're doing a specific point or for a specific technology. This is more of an abstraction. It has a lot a huge amount of weak points. So this should in any way be used as a production environment of any kind. But it is more to illustrate how this basic theoretical parts of blockchain work together in a more practical way in the code. So it was just a quick note. And anyway, don't try to look for what is for many other blockchain is just to illustrate in a more practical waves. Exactly. And as part of that, we even mentioned not all transactions in a block chain are even asset transfer. We're doing that example because it's like the most day to day one or the most familiar to most people. But yeah, it's just a way for us to illustrate it. And Luis and Skin, should we be coding alone along? No, it's what I'm saying. I mean, if you want to, you're welcome to do it. But if we're going too fast, don't feel like you're falling behind in the repo. You'll see the final version with all the code. So if you start plugging behind, you won't miss anything. You can have the complete code in there. And well, maybe for time constraints, we can continue answering the questions in the chat bot. I think we can continue with the theory. So as you can see, we're meeting the block mining pallets and that's it. So I'll leave you with them. Okay. Thank you. And could you go to the next slide, please? Okay. So I'm going to be talking about two other concepts are basics to the blockchain technologies which are mining and contentious. Okay, so let's talk about mining. As we seen before, there's a collection. Transactions are to be added to the blockchain in the cognate sample. This was a viable called pending transactions, so not have to take action for that. Pull up sections. Verify them. That's the way that blocks or mine. This is different from coins to coins, but the most common example is proof of work. This is used by Bitcoin, so it is process. The minor has to hash that new block and is cashing over and over again until it complies with arbitrary constraint. These constraints used to make it harder to to get a valid block. So once the first minor gets this cache, it can be sent to the blockchain and this not is rewarded. So are in this game pay for the workers, auditors and in this process also new coins are generated. That's the reason we call this mining. We are creating some new points on every time we mine a new blog, not slightly. So yeah, one looks mine. A number of nodes have to be that this block is correct. This is what we call consensus, and we blow this because we don't rely on a single entity. Our POS is not put on a single node. Instead of that, we have to rely on part of the whole network. So it is only on the number of nodes. Agree the blocks valid, but we can include it to the chain like this. So in this so you can see the whole transaction process. It starts with create a transaction and sign it. So for example, let's say that Alice wants to send both some 100 coin. Alice has to create a transaction and she got to sign that transaction use. Do you think her private key? After that the transaction gets made and some not mindset block included this transaction. After that we can send that block. We broadcast this lock to some of the other notes and these nodes have to verify that locks content. This is the consensus process. Once that does not reach consensus, we can be sure that this block is valid. So onto that we produce new coins which are rewarded to the miners. And after that this new block is added to the blockchain and distributed to the whole chain. And this is the last step for transaction. And once that's done, we can say that this transaction is confirmed. Would you go to the left like this? Yeah. So this last topic is Qualit. That's like this. Yeah. So I want to secure place store and that's your private keys which are used for others generation and assignment transactions. Maybe a better analogy rather than wallet is keychain or checkbook because the coins are not stored in the wallet. You use the wallet to sign in protections. So similar to a checkbook, you write checks using your signatures, and you use that to send funds. If you lost your wallet. I'm talking about that blockchain wallet. You should be able to recover all the refunds if you have the keys associated to it. Yeah. If someone sends your cryptocurrency, it means it is assigned to the address of a blockchain wallet we recorded in a Duster layer, not divided itself. Let's like this. I'm talking about five C and security. The blockchain technology has some advantages advantage about this. Some of them are that they do not require personal data to be provided. They just rely on hashes and pass. Another party has to store your sensible information. So in case you want to share that, you shouldn't have to information store blockchain is considered immutable. Once it is recorded. It cannot be changed. To change the state of our blockchain, you have to create any blocks and not that whole history of transformations get store. So that makes it more secure. And finally, this introduction is centralization incases availability. I remove single points of failure. So even if in a given network there are some manages entities, they won't be able to do something bad by themselves because of the mechanisms that we talked about before, such as consensus. This makes option technology more secure and reliable. I think we can go back to the calling time. Yeah. Thank you. Nami, let me just share my screen once again. Okay. Cool. So I know that we have seen how a consensus process is made of. Let's just try to reproduce set into or a practical example. Let's do a quick recap. So now we have the blog file blockchain file transaction and we will add a fourth one called wallet. This is for mapping what we just saw in theory regarding wallets. And let's go back to our blog file. And we're gonna a four method that is called the mind block. And for this method, remember that the consensus process needs to in a way. So a mathematical problem. So for that, what I'm going to do is just use the brute force. This is certainly some blockchain use this protocol. Most of them are the proof of work. And this is just trying to recreating a hash, obviously changing one particularly that on the hash function. Remember that we have the non property. So this non property will help us to create a different hash each time that we attempt to create one during the consensus process. Right. So why we have to create a new hash every time? Remember the property that we have in the blockchain class called difficulty. So for example, in proof of work block chains, what we will need to do is attempt to create a new hash. Let's start coding. I'm going to say is hash, and I'm gonna pick a slice of this string. We are going to use the substring function in JavaScript and I'm gonna pick from the zero index to the difficulty property that we have in the blockchain. So as you can see, we received the difficulty property here. So we use that to cut the string out. And what we are going to do with that string is we are going to compare and here I'm going to use a function in JavaScript with the right class. And I'm going to say the arrive. I'm going to create an arrive with length of difficulty plus one. And once I have created this, alright, I'm gonna join I'm going to use the joint function. What this is going to do is create an arrive with this link and then convert every arrive into and a string containing the number of zeros which I give in this as this property here. Right. So for example, if we pass the difficulty as three, we will receive a number for you here and what we have as an output. We will have an string with three zeros. And remember, the difficulty is to set the difficulty of the blockchain. But why we need to set that difficulty? Remember the mathematical problem that we need to solve for adding some blocks in the consensus process. So for example, in blockchain, I think we need to create a hash that has I think eleven zeroes at the beginning. So as you can see at the first attempt, we do it's like impossible to have this resolved. Right. So miners need to have powerful resources to compute these kind of problems. So that is the idea to mining, right. Because computers are trying so hard to find those hashes. That mean that if we put in this case having a hash for finding a hash that will meet three zeroes at the beginning. Right. This is very simple difficulty for for keep examples easy. But as I mentioned for Bitcoin, I think there is eleven zeroes before each hash. So you can imagine the incredible amount of power that we need to find those kind of hashes. Right. And obviously the note that is able to find this or to solve this problem is the one who claims the reward and can add the next can the next node into the chain. Right. So for now we are going to use the non property here. And each time we attempt to create a new hash, we are going to increase the month. And if you remember, we initialize the notes to zero, because every time we're going to increase this property. Right. And once we increase this property, this will let us to create a different hash. If you remember we are passing it in here. And if we change any date on here, we are going to get a different output cash from this algorithm function. So once we increase the cash, we are going to say this dot has and we are going to recalculate the hash using our function from about. So this one over here. So we increase has the first iteration we create another cash. If this is not meeting the criteria that we want to cash. Right. So that's it for creating for mining. Let's just add a couple of console logs just to at the end that we can run this application. We can see pretty much what's happening. What's happening behind sense. Right. We're going to log the nodes at which we find the hash, that criteria and the cash itself. So let's go back to the blockchain file. And for this I'm going to add four methods in here. We can have pretty much some validation here that don't matter too much. But let's create a method that will let us add a transaction. Right. So we receive the transaction object. And this is obviously as a part of the consensus process, right. To form some blocks with transaction. So before we can add a transaction to the blockchain or to a block, we need to validate the from address and the two address. Right. So every transaction as I mentioned before, needs to have about it from address and a valid to address. If that is not the case, we cannot add this transaction to the chain and we are going to valid it. We're going to use this method that we are going to create later in here. But for now we are going to use this method to ensure that each transaction is valid. Right. If that is not valid, obviously we're going to throw an error. And the last thing we're going to do is if all these checks are passing in here, we're going to say that the transaction is valid, and for that what we are going to do is use the pending transactions. Alright. Is that pending transaction equal to sorry push and the transaction that we are receiving here. Right. We use transaction. And according to this comment, the main pool is the same as pending transactions. Right. So Ben Pool is fairly the standard name in blockchain name the pending transaction. So in these places where minor start picking the transactions as they come in. Right. So one way miners can start filtering out transaction is according to one property called the fee. When you send a transaction in the blockchain, you need to specify some sort of attack for using the blockchain. And that is called the fee. And every transaction that you send through the network needs to have this property. The fee and validators are taking those transactions that have the higher fees. So the higher fees you specify in your transaction, the more likely a note validator note pick your transactions to be processed. Right. So there are cases, for example, in blockchain that you just specify a very minimum a fee. Your transaction can lay on this property and this main full four days until a note can start considering your transactions to be processed. Right. So this is a very important thing to remember the fees for each transaction. And let's add another method. This is my independent transaction. As I told you, this transaction needs to pass through process for validate validate them. But first of all, let's just create available in here. That is going to point to the latest block in the chain. Remember that before we can add transactions to a block, and afterwards we had that block into the chain, we need to chain together. Then you've locked to the latest one. So what we are going to do here is just when I just stop some code just to speed up things. And what I'm going to do here is just created getting the last block in the chain using the util that we have defined about. And then we're going to create a new block. But in the time stamp, we are going to pass a pending transaction because these transactions are the ones that need to be included in this new block. And if you see, we make use of the hash of the previous block. Remember that the the property here is a previous cash. So we point to the previous cash in this plus. And this is once we have the block variable created, we are going to say lock mind block. This is the function that we created a couple of minutes ago this one and we're going to pass. The difficulty is plus the faculty. And okay, once we have mine that block, we are going to push the block in in the chain. We are going to say that this block was mine successfully. And once we have mind this block, we can add it to the chain. Right. And they remember that once we want a validator mind blog, it receives a reward. So remember that after we minor block, what we are going to do is add a new reward transaction to the pending transaction. So once we mind pretty much all transactions that user sent to a network, the first one section of the next block will be a reward for the block for the validator that actually mind the last block. Right. So that's it. We have our mind pending transactions. And these two methods here are just like very util. So we are going to forget in a balance the balance is the amount of coins that you have in the given address. So what we are going to do here, re iterate over each block in the chain. And then for each transaction on the current block. And we are going to say that if if transaction sorry, I lost my person. Yeah. If transaction from address is different from the others that we receiving this method, this is the others to know the balance of from address is equal to the address that we want to know that is that we send coins out. Right. So we're going to say the violence profit that we have above, we are going to say violence equals minus one and the amount. Right. Because if the address the given address is on the front property, this means that that address send coins to another person. That is the case. Where is transaction two address is equal to the address. It means that we receive coins from this from this transaction. Right. So what we're going to say here is just at the amount to the violence, and at the end what we are going to do is just return the balance. Right. And we have added the last method in this class used to validate that. Pretty much our blockchain object will be validated by these functions. So if you see we are iterating over each block in the chain, and we are going to get the current block and the previous block. And this is going to do three validations in here. We are going to check if all transactions in the block are valid. And how we do that is just saying the has valid transactions from the block. Remember that we have coded that method in here and we are going to add a third validation here to check if the current block cache is valid. And for that we are going to say the current block hash is different from. And again, we're going to recalculate the hash because this function is going to give us the current cash state of this block. Right. If we actually change some data in here, this is going to be different from the actual path. Right. So that way we make sure that or we realize that someone else change that time into the block. Right. So obviously, if that happens, the change is not valid because something bad happened, something that intentionally this change. And obviously this is contrary Invalid. If that is the case, return folds. If not, we are going to have a third validation that is to check the previous cash. Remember that if we change the date of any block it has will be recalculated. So the blocks that were changed together back with it will invalidate the previous cash. Right. So if you see we take the previous cash block, we calculate the hash, and with the current block, we get the previous cash property. And if someone change data between those blocks, we will know with this validation. Right. Because obviously our calculate cash function will throw a different note with some data change. And finally let's go back to our transaction file. And let's add the last met with here to check if the transaction is valid or not. And for that we're going to use a different method from the sorry, not the key pair. But we are using another library representing the key pairs. That is the Lip Tic one. And we're going to say we're going to obtain the public key from the address of the transaction right in an XML format. So using this matter here getting the public key. Once we have the public key, we are going to verify. And what we are going to verify here is the cash for this transaction corresponds to the and so this needs to be in here. Right. So this way we ensure that the given address actually signed the transaction. Right. This verify method will take the cash of this transaction and the actual signature of the transaction. And this method behind Sense uses cryptographic functions to the rep. If the given address was created, the given signature was created using this address. Right. And finally, we're going to create a four file called the Wallet address. And once we have seen, in theory, call it behaves. I'm going to add two functions that is create wallet. Hope to create a key pair using the lips function in JavaScript and how to validate if the given public key belongs to the private key that we are saying. Right. So we go back to theory. Okay. So with that, we've covered all of the topics that we have for today. So I know we're having an interesting conversation in the chat, but now it's time to ask all your questions. We're having the session right now. Also, can we go to the next slide, please? Jacob, we're going to take advantage of this time. Well, we discuss and we answer all of your questions to put this feedback survey on the screen so that you can start answering it. It helps a lot to know your opinion on the course on the contents of how it was presented, but we'll just leave it there so that you have it as a reference. But we can start taking any questions that you may have. I. Do have a question if I may. You guys, thanks a lot. Production. It was rate. The first question it comes to my mind is you mentioned that we have a public and private key and I cannot stop thinking of I had a leakage my private keys. So is there a chance since I understand that we cannot change anything in the blockchain? Is there a chance to replace that with a new private key as soon as I got it stolen? Let's say somehow because that might represent a security bridge, right? I mean, the whole mechanism if it's not the other one I have is I've seen in the news that sometimes there are some providers. Please forgive me if I don't say it properly that they were offering, like virtual wallets. You had your wallet, I think through them all of a sudden disappear. So is all of the information for those all of the customers are relying on them already lost, already gone. Or is there a way to recover that? Okay. So maybe I can take on the first question because of the nature of how the addresses work. And this is common for most blockchains I come across because it's so intrinsic to how the technology works is that your private key is basically what gives you complete access to your address. Because even as we mentioned, public keys are derived from your private key. So they have, like, a mathematically relation that cannot be changed. And therefore it's not, as we put that as an example, to have a more day to day scenario. But it's not as a password that you can just change. Most addresses in a blockchain are directly derived from your private key, and that's why you can share your public key. And that's how the rest of the network will verify, as we saw. Right. So if for any reason your privately gets compromised and someone has access to it, they get complete access to your funds. So that's why it's so important. And there's such a an emphasis. And you're keeping your private key safe because it's the only way you can recover those funds. If you lose that private key, you won't be able to recover them either. So this is like a double edge sort it gives you. And it's one of the of the reasons why blockchain into CS thing. It's a good technology to be used because you are in complete control, but therefore you also have the complete responsibility to keep it safe. If you get your private fee exposed, the best thing to do is to just transfer those funds as fast as possible to another account that you have access and no one else has. So I think that answers the first question. Is that where you were? Yes. Thanks. Okay. And the second question, I think you refer to exchanges, right. All these platforms that help you just manage your accounts and your wallets and that you've seen some cases in which they get hacked and the funds are lost. Is that where you and I think I read in the news, like, perhaps three months back, something like that, that there was a broker and all of a sudden it was gone and nobody knew what happened there. So I think it's more related to that. Yeah. Okay. Yeah. So when you use one of these platforms, they have a lot of benefits. Of course, they help you manage your wallets. And they are like a bridge for you to buy new cryptocurrencies or trade between them. Because for most users, interact in directly with a blockchain is simply not friendly at all. So this platform succeeds. For this. They also give you the opportunity to store your keys for you. And that's where you can get some recovery options with them. But you also like this could be the weakest link in the security of the blockchain, because in the blockchain itself, it can't be hacked. Or just as you have, like the majority of the power or the presence in the blockchain is you could maybe influence it. But in bitcoins such as Bitcoin interim, all of those is virtually impossible to have the majority of the computing power. But in the case. It's basically not what we were mentioning. Now, it's one of the greatest things of blockchain is that information is immutable. But in the moment that you have an intermediary, such as these brokers having your keys, then you're trusting the security in them. So in this case, I'm not sure exactly what was the news, but in those cases, what got compromised was the platform itself. Someone got the private fees from them or even the in some cases, even the owners of the platform have made use of this. And as I mentioned, if you have the private fee, you can do whatever you want with these funds. So I think it's an important distinction to make if you hear this kind of news, it's not that the blockchain was compromised in most of the cases, it was one of these platforms. Cool. Awesome. Thanks for that. No problem. Any other questions? Something you like stuff we might have fell behind in the chat. So if anyone didn't get your question answered in the chat, please go ahead. If not, then I think the video wanted to do a last recap. Right? With everything with this is the practical part. Maybe we have time just to close everything. Yeah. Very useful. I'm going to show you what we did. Guys. This in action. I'm going to run the application. Let me just share my screen so that you can see how all these concepts are placed into a real world example. Okay. Are you seeing my list code, right? Yes. Awesome. Cool. So once we have period the four files that we have in here, I was just missing the fourth one. I'm going to explain very quickly what I did. I'm going to create another method code, create wallet and I'm going to use I want to make use of this library that I mentioned earlier. The list library to create every pair, a public and a private one. And I'm going to create another function to derive the public key from the private one. This is very useful and some blockchains to verify the simplicity of both part of a pair. And I'm going to return through if the given private key matches the public key. Right. And once we have that in place, we can just run our example. I have prepared a very to the book our application, and I'm going to create an instance of blockchain class. So this will mind the transactions, create a transaction for us and and all that stuff I'm going to use create wallet method that we used earlier to create wallets for my wallet and for Alice wallet. Following example that we saw that we saw on the slides. And what I'm going to do is just output in those keepers. And I'm going to create the first transaction passing the public key from my wallet. I'm going to transfer some funds to Alice wallet using her private key. And I'm going to transfer this amount of coins, 60 coins. Then I'm going to assign the transaction with my wallet keeper. Since I'm the one who signed this transaction. Once I sign that transaction, I'm going to add the transaction to the blockchain, and I'm gonna after I add these transactions to blockchain, I'm gonna mind that transaction using our simple blockchain object, obviously passing my wallet book. Right. And I'm going to do the same for other two transactions. So in total we will have three transactions created. Each transaction will be included in Trave log when we call the Mind pending transaction. Remember that the consensus process is in action and every transaction that is in the pending transactions right will be included in the next block. Right. And I'm going to do a couple of things below. Here what I'm going to do is just after I did the transaction, I'm going to check for the balance Alice balance, and I'm going to use the get balance. Right. So this will output how many coins they have for the given public key. And then I'm going to call the valid change. This would remember that this is just to validate if everything in the network is okay. If none of the blocks has been altered or modified or even transactions, this will output through. And afterwards I'm going to change one based on one of the transactions. I'm going to modify it and say that I just transferred 200 funds, 200 coins. And obviously as I changed some data, this function will output falls because remember that we calculate the hash for each transaction and for each node and the hashes will be different right after I change this property. And at the end I'm going to choose print, preprint and JSON format what our simple blockchain object looks like. Right. So let's do the debug. I'm gonna room. And as you can see, let me choose make okay with you. So if you can see we are just opening some stuff in here. We create the wallet, the my wallet, public key and private key, and the same for the Alice. All right. So if you see there are not lots of blocks, there are three blocks mine. We have the non at which we found the cash and we have the corresponding hashes of each block. Right. And if you see we are my each block, the three blocks are in mind. And once we have pretty much all the blocks, we all put the balance of Alice. And if you can see if you adding every amount in the transaction, you will have this number here as a total balance for is the first call to the Invalid chain is so put in true. And the second one is false because we already changed some data. Right. And at the end we have pretty much the whole object. If you can see this is the Genesis block. It doesn't have any transaction. And for each block, if you see, we will have remember the difficulty property that we saw. If you see every block has three zeros at the beginning. This is the first block. It has three0, then X one has three zeros and it's completely different. And obviously we have the reference to each one. We have to the the first block to the second block. It is referencing to this blog of here the Genesis block and so on. For each one. We have the transactions and we have obviously the at the end, we have some at the depending transactions arrive. Remember that we had a reward for each validator that solve the problem and we have these transaction brief reward for the last minor. And that's pretty much it. As you see, we are just implemented a very basic blockchain using JavaScript. We will give you the link so you can play around with it and I leave you with Bernardo. Thank you, Mary. We wanted to take the opportunity to wrap up everything. Hopefully that last example helped things come together. All the concepts. We're running out of time. But if anyone has the last question, we're leaving the feedback service here. Jacko has been sharing it as well. In the chat. Someone asked if we are planning to have further courses related to this. And yes, stay tuned for that. This was meant to be an introduction only we're planning to make make some courses with more specific topics. Someone mentioned smart contracts that could be part of it, and we'll just dive in a little deeper now that you have the foundations. So definitely stay tuned. I don't know if anyone has any extra comments from the team. Something you want to add. Nope. Just noting you guys for assistance this course, so hopefully you can you enjoy this. Awesome. Thank you very much. Yeah. All resources will be shared. The repo, the slides and the recording. So thanks a lot for joining a anything to add. Nothing else. Thank you, everyone. Thank you. It was great. I hope you all have a great afternoon to have a great day. Bye bye.