A TypeScript implementation of a blockchain with Proof of Work mining and P2P networking capabilities.
- Proof of Work mining with adjustable difficulty
- Transaction signing and verification
- P2P networking for blockchain synchronization
- Mining rewards system
- Balance tracking for addresses
- Chain validation and consensus mechanism
- Node.js
- npm
npm install
Start a node:
npm run dev
To start additional nodes for P2P networking, use different P2P_PORT values:
P2P_PORT=5002 PEERS=ws://localhost:5001 npm run dev
-
Block
- Contains transactions
- Implements Proof of Work mining
- Validates block integrity
-
Blockchain
- Manages the chain of blocks
- Handles pending transactions
- Implements chain validation
- Manages mining rewards
- Tracks balances
-
Transaction
- Implements digital signatures
- Validates transaction integrity
- Supports mining rewards
-
P2P Server
- Manages peer connections
- Synchronizes blockchain across nodes
- Broadcasts new transactions and blocks
- Implements consensus mechanism
// Create a new blockchain instance
const blockchain = new Blockchain();
// Create and sign a transaction
const transaction = new Transaction(fromAddress, toAddress, amount);
transaction.signTransaction(privateKey);
blockchain.addTransaction(transaction);
// Mine pending transactions
blockchain.minePendingTransactions(minerAddress);
// Check balances
const balance = blockchain.getBalance(address);
The P2P server handles several types of messages:
- Chain synchronization
- New transaction broadcasts
- New block announcements
- Consensus management
- Proof of Work mining
- Digital signatures for transactions
- Chain integrity validation
- Consensus mechanism for conflict resolution
npm test
npm run build
This project is licensed under CC0-1.0 - see the LICENSE file for details.