# Creating Your First Blockchain with Java. Part 1.


By [0x-michaelburry.eth](https://paragraph.com/@0x-michaelburry) · 2022-06-16

---

The aim of this tutorial series, is to help you understand blockchain technology by developing one. If you want to make a real cryptocurrency something like [coinmechanic.io](https://coinmechanic.io/) is a much smarter alternative!

In this tutorial we will :

*   Create your first (very) **basic ‘blockchain’**.
    
*   Implement a simple **proof of work** ( mining ) system.
    
*   **Marvel at the possibilities**.
    

( I will assume you have a basic understanding of [Object Oriented Programming](https://docs.oracle.com/javase/tutorial/java/concepts/) )

_It’s worth noting that this wont be a fully functioning, ready for production block chain. Instead this is a proof of concept implementation to help you understand what a blockchain is for future tutorials._

**This is part of** [**The Blockchain Development Mega Guide**](https://medium.com/@cryptokass/blockchain-development-mega-guide-5a316e6d10df)

Setting Up.
===========

We will be using Java but you should be able to follow along in any [OOP](https://en.wikipedia.org/wiki/Object-oriented_programming) language. I’ll be using Eclipse but you can use any new fancy text editor ( though you’ll miss out on a lot of good bloat ).

You will need:

*   Java and JDK installed. ( duh ).
    
*   Eclipse ( or another IDE/Text Editor ).Optionally, you can grab [GSON library by google](https://repo1.maven.org/maven2/com/google/code/gson/gson/2.6.2/gson-2.6.2.jar) (_who are they ???_). This will allow us to turn an object into Json \\o/. It’s a super useful library that we will also be using further down the line for peer2peer stuff, but feel free to use an alternate method.
    

Now you’re good to go :)

Making the Blockchain.
======================

A blockchain is just a chain/list of blocks. Each block in the blockchain will have its own digital fingerprint, contain digital fingerprint of the previous block, and have some data ( this data could be transactions for example ).

> **_Hash = Digital Fingerprint._**

**Each block doesn’t just contain the hash of the block before it, but its own hash is in part, calculated from the previous hash**. If the previous block’s data is changed then the previous block’s hash will change ( since it is calculated in part, by the data) in turn affecting all the hashes of the blocks there after. **Calculating and comparing the hashes allow us to see if a blockchain is invalid.**

What does this mean ? …Changing any data in this list, will change the signature and **break the chain**.

So Firsts lets create class Block that make up the blockchain:
--------------------------------------------------------------

As you can see our basic **Block** contains a `String hash` that will hold our digital signature. The variable `previousHash` to hold the previous block’s hash and`String data` to hold our block data.

Next we will need a way to generate a digital fingerprint,
----------------------------------------------------------

there are many cryptographic algorithms you can choose from, however SHA256 fits just fine for this example. We can `import java.security.MessageDigest;` to get access to the SHA256 algorithm.

We need to use SHA256 later down the line so lets create a handy helper method in a new **StringUtil** ‘utility’ _class_ :

**Don’t worry too much if you don’t understand the contents of this helper method**, _all you need to know is that it takes a string and applies SHA256 algorithm to it, and returns the generated signature as a string._

Now lets use our **applySha256** helper, in a new method in the **Block** _class_, to calculate the hash. We must calculate the hash from all parts of the block we don’t want to be tampered with. So for our block we will include the `previousHash`, the `data` and `timeStamp`.

and lets add this method to the **Block** _constructor_…

Time for some testing…
----------------------

In our main **NoobChain** class lets create some blocks and print the hashes to the screen to see that everything is in working order.

The first block is called the genesis block, and because there is no previous block we will just enter “0” as the previous hash.

The output should look similar to this:

Each block now has its own digital signature based on its information and the signature of the previous block.

Currently it’s not much of a block**chain,** so lets store our blocks in an _ArrayList_ and also import gson to view it as Json. _(_[_click here to find out how to import the gson library_](https://medium.com/@cryptokass/importing-gson-into-eclipse-ec8cf678ad52)_)_

Now our output should look something closer to what we expect a blockchain to look like.

Now we need a way to check the integrity of our blockchain.
-----------------------------------------------------------

Lets create an **isChainValid()** _Boolean_ method in the **NoobChain** _class_, that will loop through all blocks in the chain and compare the hashes. This method will need to check the hash variable is actually equal to the calculated hash, and the previous block’s hash is equal to the **previousHash** variable.

Any change to the blockchain’s blocks will cause this method to return false.

On the bitcoin network nodes share their blockchains and the **longest valid chain is accepted** by the network. What’s to stop someone tampering with data in an old block then creating a whole new longer blockchain and presenting that to the network ? **Proof of work**. The _hashcash_ proof of work system means it takes considerable time and computational power to create new blocks. Hence the attacker would need more computational power than the rest of the peers combined.

Lets start mining blocks !!!
============================

We will require _miners_ to do proof-of-work by **trying different variable values in the block until its hash starts with a certain number of 0’s.**

Lets add an _int_ called **nonce** to be included in our **calculateHash()** method, and the much needed **mineBlock()** method :

The **mineBlock()** method takes in an int called difficulty, this is the number of 0’s they must solve for. Low difficulty like 1 or 2 can be solved nearly instantly on most computers, i’d suggest something around 4–6 for testing. At the time of writing Litecoin’s difficulty is around 442,592.

We should update the **NoobChain** _class_ to trigger the **mineBlock()** _method_ for each new block. The **isChainValid**() _Boolean_ should also check if each block has a solved ( by mining ) hash.

Mining each block took some time! ( around 3 seconds ) You should mess around with the difficulty value to see how that effects the time it takes to mine each block ;)

If someone were to **tamper** 😒 with the data in your blockchain system:

*   Their blockchain would be invalid.
    
*   They would not be able to create a longer blockchain.
    
*   Honest blockchains in your network will have a time advantage on the longest chain.
    

**A tampered blockchain will not be able to catch up with a longer & valid chain. \***

\*unless they have vastly more computation speed than all other nodes in your network combined. A future quantum computer or something.

You’re all done with your basic blockchain!
===========================================

---

*Originally published on [0x-michaelburry.eth](https://paragraph.com/@0x-michaelburry/creating-your-first-blockchain-with-java-part-1)*
