A simple yet fully functional proof-of-work blockchain implementation written in TypeScript. This project demonstrates the core concepts of blockchain technology including mining, proof-of-work consensus, transaction validation, and blockchain integrity verification.
- Block Mining: Implements proof-of-work algorithm with adjustable difficulty
- Blockchain Validation: Verifies the integrity and continuity of the blockchain
- Account System: Manages account balances and transfers between accounts
- Transaction System: Simulates random transactions between accounts
- Genesis Block: Automatically creates the first block in the chain
- Continuous Mining: Automatically mines new blocks at regular intervals
Clone the repository and install dependencies:
git clone https://github.com/cr-eative-dev/PoW-Blockchain-TS.git
cd pow-blockchain-ts
npm install
To start the blockchain and begin mining:
npm run start
This will start continuous mining with random transactions between accounts occurring every few blocks.
To run the test suite:
npm test
src/block.ts
- Block class implementationsrc/blockchain.ts
- Core blockchain functionalitysrc/index.ts
- Main entry pointsrc/types.ts
- TypeScript type definitionssrc/utils.ts
- Utility functions for hashing and timestamp generationtests/general.test.ts
- Test cases for blockchain functionality
Each block contains:
- Index - Position in the blockchain
- Timestamp - When the block was created
- Previous Hash - Hash of the previous block
- Data - Block payload (can represent transactions or any data)
- Nonce - Number used for proof-of-work
- Hash - Current block's hash
The mining process works by:
- Creating a new block with the given data
- Incrementing the nonce value
- Calculating the block's hash
- Checking if the hash meets the difficulty requirement (starts with a specific number of zeros)
- If not, repeating steps 2-4 until a valid hash is found
The blockchain includes a simple account system that:
- Maintains a balance for each account
- Allows transferring funds between accounts
- Prevents transfers when the sender has insufficient funds
- Randomly generates transactions during continuous mining
{
index: 1,
timestamp: 1713275426,
previousHash: "000012a8c82d...",
data: "Example transaction data",
nonce: 3582,
hash: "0000a2b9c3d4..."
}