A hands-on learning project where I build a blockchain from scratch using Node.js, documenting every step of my journey.
This is my personal learning project to understand blockchain technology for building decentralized applications (DApps). I'm creating a blockchain that stores student records as an example of how blockchain can be used beyond cryptocurrency - for decentralized data storage, verification systems, and transparent record-keeping.
- Understand what a blockchain is and how it works
- Learn about cryptographic hashing (SHA256)
- Implement block creation and linking
- Build chain validation mechanisms
- Add proof of work (consensus mechanism)
- Implement smart contracts/business logic - Coming Soon
- Create a peer-to-peer network - Coming Soon
- Build APIs for DApp interaction - Coming Soon
What I Learned:
- A block is a container that holds data, timestamp, and links to previous blocks
- Each block has a unique hash (like a fingerprint) calculated using SHA256
- The hash depends on the block's content and the previous block's hash
What I Built:
- Created the
Blockclass with properties: index, timestamp, data, previousHash, hash - Implemented
calculateHash()method using crypto-js library
Challenges:
- Understanding how hashing works
- Learning why the hash includes the previous block's hash
Code:
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();
}
}What I Learned:
- A blockchain is a linked list of blocks
- The first block is called the "Genesis Block" and has no previous hash
- Each new block must reference the previous block's hash
- This creates an immutable chain - changing one block breaks the entire chain
What I Built:
- Created the
Blockchainclass - Implemented
createGenesisBlock()for the first block - Built
addBlock()method to add new blocks - Added
isValid()method to verify chain integrity
Challenges:
- Understanding why we need a genesis block
- Learning how to validate the entire chain
What I Learned:
- Blockchains can store any type of data (I chose student records)
- JSON format works well for complex data structures
- The chain validates itself by checking all block hashes
- Validation ensures no blocks have been tampered with
What I Built:
- Added student records as block data (USN, name, branch, semester)
- Tested adding multiple blocks
- Verified blockchain validation works
What I Learned:
- Mining uses computational power to "prove" work was done before creating a block
- Difficulty setting controls how hard it is to mine a block (number of leading zeros in hash)
- Nonce is incremented until the hash meets the difficulty requirement
- This prevents spam and secures the blockchain against tampering
What I Built:
- Added
nonceproperty to Block class - Implemented
mineBlock(difficulty)method that finds valid hash - Set blockchain difficulty level (currently 10)
- Tested mining with student records
Challenges:
- Understanding why we need proof of work
- Balancing difficulty (higher = more secure but slower)
- Watching the mining process in action
Code:
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);
}- Block creation with SHA256 hashing
- Genesis block initialization
- Adding blocks to the chain
- Chain validation (integrity checking)
- Student data storage
- Proof of Work (Mining) with adjustable difficulty
- Nonce-based hash generation
- Proof of Stake consensus mechanism
- Smart contract functionality
- Transaction pools and mempool
- Merkle trees for efficient verification
- Peer-to-peer networking
- REST API for DApp interaction
- Data query and retrieval system
- Access control and permissions
- REST API for DApp interface
- Peer-to-peer network
Prerequisites:
- Node.js installed
Setup:
# Clone the repository
git clone https://github.com/mashuibr/educhain.git
cd educhain
# Install dependencies
npm install
# Run the blockchain
node main.jseduchain/
βββ main.js # Complete blockchain implementation
βββ package.json # Dependencies (crypto-js)
βββ README.md # This learning journal
- One-way function: easy to compute, impossible to reverse
- Deterministic: same input always produces same output
- Any change in input completely changes the hash
- Each block contains the previous block's hash
- Changing data in a block changes its hash
- This breaks the link to the next block
- Making the chain tamper-evident
- Check if block's hash is correctly calculated
- Verify the previous hash matches the actual previous block
- Ensures chain integrity
- Bitcoin Whitepaper
- Blockchain Demo by Anders Brownworth
- Ethereum DApp documentation and tutorials
- Web3 and decentralized application guides
- YouTube tutorials on blockchain fundamentals
- crypto-js library documentation
- How can blockchain be used for data verification without cryptocurrency?
- What is proof of work and why is it needed for consensus?
- How do multiple nodes reach consensus in a decentralized system?
- How do DApps interact with the blockchain?
- What makes blockchain better than traditional databases for certain use cases?
- Basic block structure
- Blockchain class
- Chain validation
- Data storage (student records)
- Proof of work implementation
- Smart contract logic
- Data access system
- Network layer
- API endpoints
What Surprised Me:
- How simple the basic blockchain concept is
- Why cryptographic hashing is so important
- The elegant solution to creating immutable records
What's Challenging:
- Understanding the mathematics behind hashing
- Thinking about edge cases for validation
- Planning the next steps (proof of work seems complex!)
This project is part of my journey to:
- Learn blockchain fundamentals through hands-on coding
- Understand decentralized applications (DApps) and how they work
- Explore blockchain use cases beyond cryptocurrency
- Build a foundation for creating decentralized systems
- Document my learning process for others
Note: This is a learning project and not meant for production use. I'm building this to understand how blockchain works, one concept at a time.