A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.
Launch Xperto-AIBlockchain technology has taken the world by storm, promising a decentralized future for various industries. As a developer, understanding how to build a blockchain from scratch can be an invaluable skill. In this guide, we'll explore how to create a simple blockchain using Node.js.
Before we dive into the code, let's set up our development environment:
npm init -y
npm install crypto-js
The fundamental unit of a blockchain is a block. Let's create a Block
class:
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(); this.nonce = 0; } calculateHash() { return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data) + this.nonce).toString(); } mineBlock(difficulty) { while (this.hash.substring(0, difficulty) !== Array(difficulty + 1).join("0")) { this.nonce++; this.hash = this.calculateHash(); } console.log("Block mined: " + this.hash); } }
This Block
class includes:
mineBlock
methodNow, let's create the Blockchain
class:
class Blockchain { constructor() { this.chain = [this.createGenesisBlock()]; this.difficulty = 2; } createGenesisBlock() { return new Block(0, "01/01/2023", "Genesis block", "0"); } getLatestBlock() { return this.chain[this.chain.length - 1]; } addBlock(newBlock) { newBlock.previousHash = this.getLatestBlock().hash; newBlock.mineBlock(this.difficulty); 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; } }
This Blockchain
class includes methods to:
Let's create a simple script to test our blockchain:
let myCoin = new Blockchain(); console.log('Mining block 1...'); myCoin.addBlock(new Block(1, "10/07/2023", { amount: 4 })); console.log('Mining block 2...'); myCoin.addBlock(new Block(2, "12/07/2023", { amount: 10 })); console.log('Is blockchain valid? ' + myCoin.isChainValid()); // Try to tamper with the blockchain myCoin.chain[1].data = { amount: 100 }; console.log('Is blockchain valid? ' + myCoin.isChainValid());
This script creates a new blockchain, adds two blocks, and then attempts to tamper with the data to demonstrate the blockchain's integrity check.
Decentralization: Our blockchain doesn't rely on a central authority. Each node in the network would maintain a copy of the entire chain.
Immutability: Once a block is added to the chain, it's extremely difficult to alter without changing all subsequent blocks.
Transparency: All transactions in the blockchain are visible to all participants.
Consensus: In a real-world scenario, nodes would need to agree on the state of the blockchain. Our simple example doesn't implement this, but it's a crucial aspect of blockchain technology.
This basic implementation serves as a starting point. To create a more robust blockchain, consider adding features like:
Remember, blockchain technology is complex and constantly evolving. Keep learning and experimenting to stay ahead in this exciting field!
14/10/2024 | NodeJS
08/10/2024 | NodeJS
31/08/2024 | NodeJS
08/10/2024 | NodeJS
08/10/2024 | NodeJS
08/10/2024 | NodeJS
08/10/2024 | NodeJS