Creating a blockchain with Javascript

javascript blockchain

write your own Blockchain with JavaScript

Learn how to write your own Blockchain with JavaScript. In this video I’ll show you what is behind a blockchain by using simple code.
It’s not a complete implementation, but enough to understand how blockchains work and how they guarantee that blocks can never be changed.
After creating our blockchain we’ll try and tamper with it. This will demonstrate a blockchains ability to detect fraud.

Transcript

hi everyone my name is Kavya and in this
video we’re going to write a very tiny
blockchain in JavaScript now it won’t be
anything too fancy but it will be just
enough so you can understand how a
blockchain actually works we’ll call
this blockchain sabzi coin and let’s get
started okay so here I’ve opened up an
empty directory in Visual Studio code
and let’s start by creating a new
JavaScript file to store all of our code
I’m going to call this main Jas and I’m
going to start by defining what a block
on our blockchain will look like so I’m
going to create a class called block and
I’m going to give it a constructor and
this constructor will receive the
properties of this block and so each
block will have an index a timestamp
some data and the previous hatch I’m
going to set this by default to empty
now these are pretty self-explanatory
the index is option I’m going to tell us
where the block sits on the chain the
timestamp will tell us when the block
was created and then we have data and
this might include any type of data that
you want to associate with this block
now in case of recurrency you might want
to store the details of the transaction
in here such as how much money was
transferred and who was the sender and
the receiver and then the previous hash
is a string that contains the hash of
the block before this one now this is
very important and it ensures the
integrity of our blockchain so I’m going
to keep track of all these values so I’m
going to say this dot index equals index
this dot timestamp equals timestamp this
dot data equals theta this dot previous
to hash equals previous hash all right
and now I’m also going to add another
property to our class and that’s going
to be the hash property now this will
contain the hash of our block so we need
a way to calculate it so I’m going to
set an empty for now
going to create a new method and I’m
going to call this one calculate cash
and all that this function is going to
do is calculate the hash function of
this block so it’s going to take the
properties of this block run them
through them hash function and then
return the hash this will identify our
block on the blockchain now I’m going to
use sha-256 as a hash function but this
is not available in JavaScript by
default so I actually need to import a
library they can do this so I’m going to
open up a terminal and I’m going to
install crypto j/s I’m going to do that
through NPM so I’m going to run NPM
installed – save crypto – Jas and this
will download the library and we’ll put
them in into the node modules directory
so we will have crypto jazz and we and
here we have all the hash functions that
it supports so I’m going to close up my
terminal and now I can import the
sha-256 function so I’m going to take
constant sha-256 equals require crypto j
s / sha-256
all right there we go so now we have
imported the library and we can start
using it so this function right here
calculate hash it should return a
sha-256 hash of our index plus our
previous hash plus the timestamp of our
block plus and then I’m going to
stringify the data object the data
object so I’m going to stringify this
JSON and I’m going to take the output of
shell 256 and I’m going to cast it to a
strength because otherwise we get an
object that this library returns so now
I can use the task function so I’m going
to say that the hash equals this dot
calculate hash and so when we create our
block we pass along these parameters and
it will calculate the hash of our block
so that was our block class let’s now
create a new class for our block chain
so class
blockchain I will give it a constructor
as well and the constructor is
responsible for initializing our
blockchain so I’m going to create a
property chain inside this class and
that is going to be an array of blocks
.
.
.
.
.
.