Making My Own Bitcoin: LawsonCoin. An Intro to Blockchain

Lawson Hung
4 min readMar 27, 2020

The coronavirus has us all on lockdown. As a resident of New York City, one of the most compact and populated cities in the world, I see firsthand how the virus is impacting my city. Stores are closed and some may not even be able to reopen after Covid-19 has passed. It’s a true pandemic alright.

I thought I’d use the time to learn something new so I decided to learn about blockchain, a word that I hear thrown around a lot. I found a YouTube video to code along to and created a simple blockchain. I learned that Bitcoins are actually using blockchain technology, and I learned on an overarching scale how blockchain works.

https://www.youtube.com/watch?v=zVqczFZr12

To start, we’ll create a fresh directory. In the terminal, we have to run the following command:

$ npm install --save crypto-js

This downloads crypto-js from node package manager and installs is. Now we name a file index.js. We have to import crypto-js like so:

const SHA256 = require('crypto-js/sha256');

Then we can create a Block class:

class Block{constructor(index, timestamp, data, previousHash = ''){this.index = index;this.timestamp = timestamp;this.data = data;this.previousHash = previousHash;this.hash = this.calculateHash();}calculateHash(){return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data)).toString();}}

SHA256 is a function that is imported from crypto-js. And now we can create a Blockchain class:

class Blockchain{constructor(){this.chain = [this.createGenesisBlock()];}createGenesisBlock(){return new Block(0, "01/01/2020", "Genesis block", "0");}getLatestBlock(){return this.chain[this.chain.length - 1];}addBlock(newBlock){newBlock.previousHash = this.getLatestBlock().hash;newBlock.hash = newBlock.calculateHash();this.chain.push(newBlock);}}

This allows us to create new blockchains and link blocks to the previous blocks. createGenesisBlock() creates the genesis block as the name suggests, the generating block or the first block in the blockchain. You can get the latest block with getLatestBlock() and add new blocks with addBlock(newBlock).

Let’s now test it:

let lawsonCoin = new Blockchain();lawsonCoin.addBlock(new Block(1, "10/07/2019", { amount: 4 }));lawsonCoin.addBlock(new Block(2, "12/07/2019", { amount: 10 }));

We just created a new Bitcoin alternative, lawsonCoin! Let’s console.log it to test it.

console.log(JSON.stringify(lawsonCoin, null, 2));

We have to stringify the object lawsonCoin. In the terminal, run:

$ node index.js

This will run your Javascript file. You should get the following output

{"chain": [{"index": 0,"timestamp": "01/01/2020","data": "Genesis block","previousHash": "0","hash": "37fdbf3027570c6daca59c93c646bb19bae477f5c070abfe74d9170b313ba0df"},{"index": 1,"timestamp": "10/07/2019","data": {"amount": 4},"previousHash": "37fdbf3027570c6daca59c93c646bb19bae477f5c070abfe74d9170b313ba0df","hash": "cc589eb1d2e4c16e446a01426882c7cc962d76ba245728414b060f1ed474e31d"},{"index": 2,"timestamp": "12/07/2019","data": {"amount": 10},"previousHash": "cc589eb1d2e4c16e446a01426882c7cc962d76ba245728414b060f1ed474e31d","hash": "2204c0c76f89f7a8aee822bcb6ae3568ea004655ef4b5a99dc1468593881287a"}]}

You now have 3 blocks, each of which tells you the transaction history of the coin. It also tells you the hash of the previous coin, so it’s harder to change the integrity of it.

We want to make sure of it though, so in the Blockchain class:

class Blockchain{constructor(){...}...isChainValid(){for(let i = 1; i < this.chain.length; i++){const currentBlock = this.chain[i];const previousBlock = this.chain[i-1];if(currentBlock.hash !== currentBlock.calculateHash()){return false;}if(currentBlock.previousHash !== previousBlock.hash){return false;}}return true;}}

isChainValid just checks if the current block hash and the previous block hash are equal.

We can add the following lines to the end of the program to see if the blockchain is indeed valid:

console.log("Is blockchain valid?", lawsonCoin.isChainValid());// Tampering with datalawsonCoin.chain[1].data = { amount: 100 };// console.log("Is blockchain valid?", lawsonCoin.isChainValid());// If we try and recalculate the hash, still doesn't work because previous hashes are invalidlawsonCoin.chain[1].hash = lawsonCoin.chain[1].calculateHash();console.log("Is blockchain valid?", lawsonCoin.isChainValid());

You should get the following console.log's:

Is blockchain valid? trueIs blockchain valid? false

It’s true at first, and then turns false after we tamper with the data. This means that a blockchain is essentially unchangeable after created. So you can go back and see all of the previous transactions in the chain.

To make this even more secure, you can add proof-of-work as well to slow down the creation of new blocks. It also ensures that you double check your blockchain with other people who also have the blockchain. You’d essentially have to overtake 51% of the system in a blockchain to change it, very hard to do.

Here’s the code to my index. js file:

// Credits: https://www.youtube.com/watch?v=zVqczFZr124// Import this by running in terminal in this directory// $ npm install --save crypto-jsconst SHA256 = require('crypto-js/sha256');class Block{constructor(index, timestamp, data, previousHash = ''){this.index = index;this.timestamp = timestamp;this.data = data;this.previousHash = previousHash;this.hash = this.calculateHash();}calculateHash(){return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data)).toString();}}class Blockchain{constructor(){this.chain = [this.createGenesisBlock()];}createGenesisBlock(){return new Block(0, "01/01/2020", "Genesis block", "0");}getLatestBlock(){return this.chain[this.chain.length - 1];}addBlock(newBlock){newBlock.previousHash = this.getLatestBlock().hash;newBlock.hash = newBlock.calculateHash();this.chain.push(newBlock);}isChainValid(){for(let i = 1; i < this.chain.length; i++){const currentBlock = this.chain[i];const previousBlock = this.chain[i-1];if(currentBlock.hash !== currentBlock.calculateHash()){return false;}if(currentBlock.previousHash !== previousBlock.hash){return false;}}return true;}}let lawsonCoin = new Blockchain();lawsonCoin.addBlock(new Block(1, "10/07/2019", { amount: 4 }));lawsonCoin.addBlock(new Block(2, "12/07/2019", { amount: 10 }));// console.log(lawsonCoin);// console.log(JSON.stringify(lawsonCoin, null, 2));console.log("Is blockchain valid?", lawsonCoin.isChainValid());// Tampering with datalawsonCoin.chain[1].data = { amount: 100 };// console.log("Is blockchain valid?", lawsonCoin.isChainValid());// If we try and recalculate the hash, still doesn't work because previous hashes are invalidlawsonCoin.chain[1].hash = lawsonCoin.chain[1].calculateHash();console.log("Is blockchain valid?", lawsonCoin.isChainValid());

And the GitHub repo I pushed it to:

It was fun learning about Bitcoin and how it works! I was honestly surprised that in learning about blockchain, I inadvertently learned how Bitcoin works under the hood.

Happy coding!

--

--