Let's Create A BlockChain On Node.js
Key Takeaways
This video tutorial demonstrates how to create a blockchain on Node.js, covering the basics of blockchain, hashing, and proof of work. It provides a step-by-step guide on creating a blockchain class, adding blocks and transactions, and validating proof of work.
Full Transcript
he'll villainize how awkward win welcome to a new video from my opinion today I'm gonna talk about a really nice topic I've been like excited about talking and bringing your video drawing on this channel on the channel actually for a really long time which is a blockchain so this visit sorry I'm gonna create a blockchain using no js' so we're just gonna start in here like give you what is the blockchain and give you a quick overview of what is the blockchain and why it's very important and like some of you might have heard about blockchain because it's it has been like growing so fast in the two last couple of years and obviously know about cryptocurrencies and stuff like that so cookies actually are based in to the blockchain so let's just dive into and start you or start talking about blocking so first what we need to know about is what is a block team so watching is originally a fastly growing endless chain of blocks what each block represents an immutable DM which makes it super powerful in terms of dealing with the sensitive the other so each block in the chain is immutable so once you create the block you can't change the block the data into the block can't actually change whatsoever so which widely used for crypto currencies like Bitcoin and lithium like so most of you knows about Bitcoin and Bitcoin is actually based on the blockchain technology it was a cryptographic cryptographic algorithm to prevent data whoring by block hashing where each block has its own hash so once you create the block you can hash the content of a block so that gonna be like a unique hash you be using something like coop segura cryptographic algorithms to create the hash then save the hash into the chain so that gonna prevent the chain or prevent each block on the chain from changing so whenever a block from the chain chains the change is gonna be like corrected and you can't actually access any other blocks from from the actual chain and that means about the chain is crafted so the sister we know that this is not a violet chain and it would know that there's like something bad with this chain or some kind of mismatch theater or some like hacker who try to access some kind of things or change them the other so this how actually tries to prevent things up from modification which wants to change to be safe from an unauthorized modification so this is actually how a chain would looks like in this example we're gonna to use no GS and we'll create a simple block chain for a very basic cryptocurrency like try to call it just for fun deep currency so this this currency don't take it like a very big deal because gonna be like a very very basic thing very simple so yeah this is when all we're gonna cover so here's how our killer box of chain would look like so there's a bunch of blocks and us can see there's some kind of like a lock into this block which means they are and more viable or immutable things once you've created it once you're touched into this chain you can't change it whatsoever so let's start creating or blocking using ideas so actually before starting I want to dis mention that you can go ahead and learn about es6 classes on javascript because i've introduced the last video tutorial about es6 JavaScript class syntax and I've just got given like a quick overview of how classes actually work how to initiate classes like basic things to start working with class to work and fill with classes from JavaScript right out of the box so make sure to go and watch the video or you can read through the recent story where everything is just well self-explanatory make sure to go ahead and use that because in this video tutorial on this particular tutorial gonna use the classes in order to create the blockchain class so who'd say things actually let's start by creating the block team so the blockchain is gonna be like a class which holds the chain that we're gonna use so you remember that's we're gonna create that a very basic thing so let just go ahead and create the class the class is gonna call like a blockchain and create a class also make sure as I've said to watch the video not to understand all of that if you're not already familiar with classes so class is obviously a constructor and we need to put in the constructor either so interest constructure we need to initialize some data for our plants so I will never be initiates a blockchain class we need to initialize or initiate at the era with that block set some default value for this deal so for the first thing that you need is to create the chain itself the chain what it is it's actually a simple array so a shame isn't a way in that and beside that way we're gonna put is like a bunch of blocks so a chain is a bunch of blocks links together how we're gonna link the chain together is we're gonna use the hashing system so I will show you how actually in a second so let's first creates the chain so we're gonna use this dog chain I'm gonna call this or give it like an empty array since we are just initializing things and it's just like this is gonna be like deep currency thing and this is actually block chains are widely used for so we need some transactions so remember once you are under like a crypto currency or under currency what you need is to send or to be able to send and receive money so this is called transactions so transactions have some kind of data from the recipients the sender and the amount of data that we want to send from the sender to the recipient so the recipient can receive this DM so this is luckily what our transactions there's a bunch of other things that you compliment in you but this is like the basic thing what you can do with it so we're gonna call it transactions and we go ahead and create that's also gonna call it this curls Soaker just a shortcut for transact recurring transactions transactions equals energy away simple easy peasy so this all we need to do is just initialize some things and these transactions are gonna be like the currencies actions also we need a bunch of methods for that's the first method is add new block so we need or this method gonna allow us to add a brand new block into actually the the change so this will allow us to add a brand new block to the King so actually for the block what we need or the basic structure of her blog is housing an index at times transactions away so like any transaction that are gonna happen in this block and obviously the hash of this block and the previous has actual the previous block hatch while we are storing the previous hash box so this is actually how the linking works so to link like a block with the next block or the the current block with a previous block so linking like this will create a change so just giving it the previous hash to the previous blocks hash and that said this is how you can create a chain and this is how you can validate or check if this chain is valid or not so here we're gonna allow us to create your blog so let me just go ahead and create that block you know it's gonna be like an object's simple so here we need to like pass in a couple of things the first thing what we need to pass is the actual like you know the previous hash if we if we need that block but we can grab this previous hash in a second actually we need also the timestamp the index and transactions so for the add a new block what it takes as an argument is the previous blocks hash so we can create the the actual chain and here the first thing it takes the index and the index comes from we save this change the length and we add one so the last element has been added to the chain plus one since we're gonna add a new element to the chain so this how we can measure the current blocks index also we need a timestamp the timestamp is going to actually represent the time that the actual block has been created and added to the chain so the timestamp got a key called date but now so the date the actual or the basic JavaScript date object and we are accessing it and we're just gonna give it or grab the now method which we're gonna give us the timestamp things since the UNIX timestamp you should know about that a little bit so you can check it out so here we've got actually the time that the block has be created another to the chain look the other thing is the transaction so we can call this transactions and the transactions are actually erase that we can access the best doctrines transactions obviously so you need to assign the current transactions and the last thing we need is the previous orbit the current hash of the actual block so we would have like this is gonna be like the hash over the current block so for now we're just gonna put it no I'll talk about this in a second and the other thing is going to be like the previous blocks hash which gonna just be the previous hash symbols that so now but just go ahead and have to the chain we can use this chain since we have already for that and give it the push and get it's gonna push and brand new block also the creatures actually is what I represent is lucky you can add a transactions we're gonna have another method here it's not gonna allow us to add new transactions into this array and like whenever we trying to add a new block all of that transactions that we have cubes so then all of them gonna go into this block the particular block that we have created after adding a bunch of or only one transaction into the array then after adding that the block into the chain what we need is to clear that transactions so we can add new transactions like gonna be add in the future to a new block that we're gonna assign or add to the chain simple easy so here can go with current transactions and here what we need is to like to clear this things so we just assign it to a new empty obey here we go now we have just gone through that and just for extending that let's just go ahead and return the block and that's it know what's on the new method which can allow us the new transaction so to touch you actually need to three things needs the sender I like all of them our strings so the sender the recipients so obviously the sender the recipient and the actual so recipients and the amounts of money bail the sender sins so here we go let's go ahead and just call it a transaction or just directly add that so we can go with current transactions first and we're gonna add an object into this so we can call like the sender the recipients and obviously the amount and the cool thing I like about ESX so you don't need to put in here amount in the mount so you specify the key and then the value that you want to put it into this key field so just putting the key and gonna automatically fill the key with this name all your strangers gonna go in to look for the same variable name in here and just grab the value and put it in here so ESX gonna do all of that for you obviously if you're using and like in our case in Europe since we aren't using no tears so this gonna be like the same thing for us here we are just adding in new transactions to this also we need like a couple of other things like is empty so this is empty but it dance is just gonna kill ahead and check if the actual like chain is empty right now so it returns true or false so we turn this top chain length so the length scale represents the length of the the items that the chain has if it does gonna equal to 0 I'm gonna return that gonna be like equal to true so it's actually empty if this is gonna like but like a bigger than 0 or greater than 0 so this gonna evolve we so choose just simple stats this is what we need you know it's and also we need another one we're gonna help us to get the last added block to the change so just getting the last add a block will help us a lot so if you're gonna return this top chain in this book chain what we need is to use the slice the slice method gonna allow us to get only one portion of this away and if as you can see here we need to give it a start number and an end just gonna return a new array if like like you can say from zero to five it is going to return from zero to find index the the actual items are and into this interval or it actually exists under this interval if we try to add like a negative number it is gonna start from the end and comes all the way down to the beginning so here we're gonna just say minus 1 and this gonna grab us but last element has been added to the array obviously just gonna return an array also with only one element so accessing that we need to grab it or to give it like the first index which is the 0 and here we're gonna just return the last out of block into the chain simple now we have got all that ready and actually good to go for us so what we need now this hashem is no now we'll just go ahead and add the hash into the actual block so to do that or to add the hash into the block so after creating a null so here we have the actual hash so what we can do or let me just go ahead and get rid of it and or we can go ahead and use or we can use the actual object hash class so let me just go ahead and include that so this called hash we can wire it from NPM so it's called object - and obviously make sure to go ahead and install that using NPM and make sure to initialize all the vampires and OGS I've totally forgot about talking about this but I have a full crew series on my website as well folks about no genius and how I work shall be everything were some issues over how to go ahead actually and check things up for that so as I've said go ahead and install this just type in NPM install object - and you are ready to go simple as this so here we'll grab the hash this function or this object here gonna allow us to hash things up using like standard hash algorithms the asks or the sha-256 if you're familiar with that cryptographic algorithms you can you can google this and I'm just going to give you crazy results so I'm just gonna assign this pop hash equals hand you're not happy here I'm gonna hash the current block that's what I've created remember the house represent the current block hashes so this is gonna help us to hash obviously it's called object hash which can allow you to hang in your objects into strings in this will return a string if you can notice in here and it takes an object's and options are like optional things so here I'm just gonna put be hash and we are actually using go so we can actually access hatch we can put into transactions we can add transactions in here we have gods like internal construction the last thing we need now is to export things up using the exports model so we can say export dots and we need to actually use the modular so for that so we will move on to use modular exports and this is gonna equal to blockchain so I'm just gonna assign the art of the class the class reference not an instance we're not creating an instance in here now all the work what we need is actually in do the main dodging yes and obviously I don't know why I've created this blog top jeaious I don't know anymore so just we just actually need the blockchain and here we got no just go ahead and acquire this block chain from your folder and you're gonna enemy need to create an instance of this class so let's just go ahead and initiate this class in the call blocking we create an instance calling the new so new blockchain and here we go now if God and we've got actually missed this we have initialize or a blockchain so let's just start working on this blockchain so how can I'm gonna work on this blockchain is like we are going to add blocks but blocks are not gonna be simply added to the blockchain so if you're familiar with Bitcoin or any other cryptocurrencies it's not that easy to add blocks so this is called actually my name so my name is adding blocks to the chain and like really fine bottle dating then adding them to the change so make leave the chain bigger and we're actually the blocks only has the transactions and stuff like that so although blocks has some kind of a tiara and the most important data inside of logs is the transactions which has the sender the recipient and the amount of money same cents between the sender and the recipient simple as this but what actually actually things actually work is mining so it takes a little bit of time well adding a block is just very simple so they even some kind of algorithms and way to make it a little bit harder to mine a block in order to make it a little bit hard to gain money or to gain bitcoins or if you're actually from bitcoins or mining and cryptocurrencies which we have like seen in the last couple of years especially in this year 2017 2018 that was like an insane thing happening on the industry so yeah just go ahead and check it out to learn more about it if you're not already so now what we want is to create like a couple of methods they're gonna allow us to blow on your blocks but we need to validate things up or to create hashes in order to be able to create a block or to create a block and add them to the change so just first things first I'm gonna create approve so this proof is gonna be like the the proof is gonna change check it check actually against so there's gonna be like the real proof whenever we reach that proof or we generate this proof then we are good to go so we can add the new block so let's just go ahead and just use a number you want any like any any cryptocurrency out there and house it along with us and a couple of other things for like checking things so this gonna be like the basic way is adding numbers that you can what usually you can find is a real one strings or friend to learn to me generally characters like a dollar sign and something that hashes and they hash that thing and you know it's a little bit harder but we're gonna just take the easiest step and we're gonna try to randomly generate this number so if you've gonna find this number then we are good to go and we can't and you block to the chain so not just go ahead and create the instance of the class or other method they're gonna allow us to validate for the proof so valid proof and he's gonna return true false whether the proof is like actually valid or not so we go I've got this function and I'm using es6 or a function in here so you can use a normal function it's not necessary to do that so every one we want is to create a guess hash so and I'm gonna equal the hash and I'm here for the hash make sure to include the or require actually the hash or the objects has more from doing that so it's called object cash and this can also few obvious be to hash need approve so we can pass in the proof and this are gonna be like our guest hash so we can just console.log our guest hash just to making it looks a little bit beach here so you will understand what I mean when I when I try to like run that in a second or in the minutes after finishing this you're just gonna give it like caching I'm gonna give it the guest hash and obviously the guest ask is only a string or a hash stream that has been hash of this proof so this is gonna return a string and gonna store it into the guest hash now we need to return since this function returns true or false whether this valid or now so I'm just gonna return guest hash equals hash of the real proof and real proof is constant we have to close into the global scope and 156 let's actually make this a little bit harder so me making 15 60 so just make a little bit harder and little bit longer to find the hash and you've got now just go ahead and create the function but to randomly generate the number and each time we get that proof and provide it to the ballot proof and if the ballot proof is actually valid or the proof I think I've got is actually valid so we can get out of this loop else we're just gonna continue looping and incrementing the index until we get into the right hash and we can just add the new block if we gets into the right hash show proof what is that okay no I'm just gonna call it proof of work and this proof we're gonna allow us to make a loop just gonna create slide prove equals zero so starts from zero and I'm going to create a while loop while true and this is like an infinite loop so it will be an infinite loop if we're not gonna use the breeds teats me to get all of this loop so make sure to do it or just like a bad approach to say while true if you're not gonna use the break statement into that loop so there's gonna be like a very very bad approach don't don't do that unless you are actually knowing what you're doing and you're gonna just go ahead and check if not valid proof and we pass in the proof so if the proof is not valid what we want is to increment the actual number so remember this lucky number and the our proof is a number it's not a string it's not very hard to guess about and convert between a s key or Unicode strings into numbers and vice versa and someone else also this like numbers all we need to do is just go ahead with a proof and we say prove plus plus and here we go else if like but the proof is valid all we need to do just call the break statement and we can get out of this loop so if we are out of this world we need to return the proof that's all we need to do now is just make both of these methods works together so are we doing here so you say F the proof would work so we are actually calling the loop statements in there and we try and actually do a randomly generated number and check against each number we generate using the ballot hash like method and we hash the number we check against the real proof and vice versa unlike a real world example it's not gonna be that easy so you gotta have like a lot of algorithms for checking and like a lot of services running so you can just submit the proof into the server and say that server works from now and also you're gonna need like a lot of GPU power so I'm saying GPU not the CPU because mining is like husband using GPUs power for hashing or creating like random hashes and check against the real old hash I don't know if it has actually works or not or if it's actually a ballot or not so here we say if proof of work equals and since this is gonna return the proof the actual proof number not a hash and since we have our proof in here so we can say equals proof in that everything is working pretty much fine what we need now is to add a new block into the actual chain let's first are the new transactions so we can do with the clock blockchain and the instance so we add new transaction and here we can add a new transaction before adding a new block so let's say like the center is going to be slam where we see it was gonna be all X or something and the amount of money or the amount of things that do this cryptocurrency like on bitcoins or something let's say like 200 Bitcoin that is a really ridiculous number but you know that doesn't try to do it and use it all so now let's go ahead and add a new box so we add in your block the only thing that we need to grab is the previous hash so we'll just go ahead and review this patch so we say previous hash equals and to grab the previous hash we need to access the last element on the change so the last block from the chain we grab that then we access the hand from this this lecture last block so we can say blockchain dot last block so remember this method grabs us the last block so we can grab this thing and you can say hash but we need to check if this like the first the first block I'm gonna add to the chain which means there's no last block exists so is that the first element that gonna add into the chain like the chain is empty right now and we're gonna add this in your blog this is gonna be like the first blog to be add into the actual array so when try to find this it's gonna return it into fine and we want we don't actually need to return any the point what we need to else set the previous hash if this like the first block we need to set it into a mode so we need to have like a quick if statements in here and you know like the in line of statements and we can say if last block top-hat then we can grab in the last block the hash else if there isn't so we're just gonna return null so you should already be familiar with this like in line if statements this means if this if this is valid which means if this is not in defined so do this else if this is like in defined else do this so this is gonna give all the way to one of these evil returning the hash from the last block or returns long so if we are in this case it just gonna return null and this previous has you're gonna be like no so here all we need to do is previous hash so if when whenever we add the first element then the second elements all of them can have the previous houses everything has the last block added before it so you've got a point actually how everything that should pretty much works so now we have got the prove it work the last thing we need is just a double check that everything is pretty much working fine imma just go ahead and cancel things up into the console or log everything up go to the console so here I'm gonna say the blockchain and we can grab in or array which is gonna be like the chain and here we go now with gods that let's control s and save everything up and let me just go ahead and write run this using nodes so say node main which is our main J's and pressing enter and as you can see we get a really nice house you think and here we go so we can't read property hash you can defined which is like the last block - so as I've said this is like an interface so you know we can't leave things up so only we need to check if this is like an undefined we need to return the null or else if it's not in the find you only need to grab in the half no testing again so let's just go around to the terminal if you're not usually if you just do a code because it's your make sure to go ahead and use CMD or any terminal whenever all s you are running this so yeah make sure to do that just know main and me would go so you can see we get the hashing or like hashing guess he for the hatch for the right hash or try to actually find the right hash and here we go the last thing we did is following the last hash that we have that's with the index first the timestamps gonna be like the milliseconds or the seconds since the timestamp of 1917 the unix timestamp and here we've got the transactions just like hidden objects in the previous hash it small so that was actually pretty much guys that was really basic things so there's a lot a lot of implementations of the blockchain there's a lot of things they can do for validation for proof of checking or proof of work and hashing and all of the algorithms that can work to in order to be able to add a new blog to the chain so much is actually guys I hopefully have enjoyed this video so that's actually very brief introduction and just trying to make a very simple block chain on ideas as well as JavaScript and just using the object hash like model in here that can allow us or helps us to watch from your hash and actual objects which is our block into the chain so again novices I would cut you all guys in the next video so you
Original Description
Read Tutorial:
http://go.ipenywis.com/ipenyff9b4
--- Official Links----
Official Website: http://ipenywis.com
Official Facebook: http://go.ipenywis.com/ipeny4907
Official Twitter: http://go.ipenywis.com/islem9a03
---Support US---
By Becoming a Patreon: http://go.ipenywis.com/ipeny5024
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CoderOne · CoderOne · 46 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
▶
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Laravel CMS | N-01 | Getting Started and Environment Set Up
CoderOne
Laravel CMS | N-02 | Routes, Controllers and Views
CoderOne
Laravel CMS | N-03 | Dealing With Assets and Page Layouts
CoderOne
Laravel CMS | N-04 | Migration, Models and Adding Categories
CoderOne
Laravel CMS | N-05 | Showing Added Categories, and Session Messages
CoderOne
Laravel CMS | N-06 | Edit\Remove Categories using AJAX and Popups
CoderOne
Laravel CMS | N-07 | Add New Post
CoderOne
Laravel CMS | N-08 | Creating a Basic Laravel Blog
CoderOne
HTML5 Canvas For Beginners | Getting Started | #01 with Webpack
CoderOne
HTML5 Canvas For Beginners | Drawing Basic Shapes | 02
CoderOne
HTML5 Canvas | Drawing Text With Circles | Part 01
CoderOne
HTML5 Canvas | Text Animation and Circle Collision | Part 02
CoderOne
Complete Guide To Web Development In 2018
CoderOne
AJAX & JSON Explained | Examples
CoderOne
Visual Studio Code Web Development Setup and Extensions
CoderOne
Electron Build Desktop Apps HTML/CSS/JS - Getting Started 01
CoderOne
Electron Build Desktop Apps HTML/CSS/JS | Creating Windows and Processes 02
CoderOne
Electron Build Desktop Apps HTML\CSS\JS | Dialog, Sessions and Cookies 03
CoderOne
Electron Build Desktop Apps HTML\CSS\JS | Custom App Menu & Context Menu 04
CoderOne
Electron Build Desktop Apps HTML\CSS\JS | Package, Build and Deploy your App 05
CoderOne
Node.js Brief Beginners Introduction #01
CoderOne
Node.js How Modules Works & NPM #02
CoderOne
Node.js Working With Events and Event Emitter #03
CoderOne
Node.js File System (Read and Write) FS 04
CoderOne
Node.js Create a Basic Server Using Express 05
CoderOne
Json Web Token Authentication JWT Explained Securing API
CoderOne
CSS Flex Box Design | Practical Examples |
CoderOne
API How It Works With Practical Examples
CoderOne
MongoDB Getting Started 01
CoderOne
MongoDB Working with APIs and Practical Examples
CoderOne
Create a Command Line Interface (CLI) Using Node JS #01
CoderOne
Create a Command Line Interface (CLI) Using Node JS 02
CoderOne
Build React Apps | Introduction And Getting Started #01
CoderOne
Build React Apps | Working With Components And Multiple Rendering #02
CoderOne
Build React Apps | App State Management | Practical Examples #03
CoderOne
Build React Apps | Getting Started With Flux (Intro) #04
CoderOne
Build React Apps | Create a Clock Timer Using React 05
CoderOne
Build React Apps | Create a Clock Timer Using React 05 | PART2|
CoderOne
CSS Preprocessors Getting Started | SASS and LESS | Practical Examples
CoderOne
Regex Introduction | Learn Regular Expressions 01
CoderOne
PHP VS Node.js Which is Best For Web Development
CoderOne
Drag and Drop Using Native Javascript 01
CoderOne
Drag And Drop Using Native Javascript 02
CoderOne
Master Git (Version Control) in One Video From Scratch
CoderOne
Let's Learn The New Javascript ES6 Class Syntax
CoderOne
Let's Create A BlockChain On Node.js
CoderOne
Best Online Code Editors For Web Developers
CoderOne
Let's Create a Modern Login Form on React #01
CoderOne
Let's Create a Modern Login Form on React #02
CoderOne
Laravel CMS | N-09 | Admin Registration
CoderOne
Laravel CMS | N-10 | Login and Authentication
CoderOne
Let's Create a Twitter Bot (Listen and Retweet)
CoderOne
Rapid Webpage Creation With Emmet (HTML & CSS)
CoderOne
Create Popups and Modals Using Native Javascript, HTML and CSS
CoderOne
Promises and Callbacks on Javascript With Practical Examples
CoderOne
Create Collapses using Native Javascript, HTML and CSS
CoderOne
Let's Learn Typescript (Javascript Devs) | Getting Started 01
CoderOne
Let's Learn Typescript | Interfaces and Generic Types (The Typing System) 02
CoderOne
Let's Learn Typescript | Work With Classes, Modules and Enums 03
CoderOne
Let's Learn Typescript | React and Webpack With TS 04
CoderOne
More on: Tool Use & Function Calling
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
X now offers an MCP server to make its platform easier for AI tools to use
TechCrunch AI
n8n Automation Repurpose Video Content: The 2025 Production Guide
Dev.to AI
You’re Still Paying $200/Month for AI Tools You Could Replace With a Free Local Setup Tonight
Medium · Data Science
Top 10 AI Tools Every College Student Should Know in 2026
Medium · AI
🎓
Tutor Explanation
DeepCamp AI