Making My Own Bitcoin: LawsonCoin. An Intro to Blockchain

$ npm install --save crypto-js
const 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);}}
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(JSON.stringify(lawsonCoin, null, 2));
$ node index.js
{"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"}]}
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;}}
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());
Is blockchain valid? trueIs blockchain valid? false
// 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());

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

F#, Fable, and Sorry!

How to update the Redux store from component

Redux default store

The Best Luxury Cars Just BecameAffordable.. https://t.co/Fplr9kOX9T https://t.co/0LPT8cDcfe

Lexical Environment in JavaScript

Pointer Arithmetic In C++

How to check if a JavaScript Object is Empty?

Sonar cloud integration with Github repository for React Native App — Part1.

Bluebird’s map and mapSeries explained by Cooking Pasta

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lawson Hung

Lawson Hung

Software Engineer

More from Medium

The Monthly Money Maximizer

JavaScript Advanced Concepts

Hello Javascript, goodbye Ruby

How do you make JavaScript’s array unique?There is 18 methods.