This project is a simple blockchain implementation written in JavaScript. It demonstrates core blockchain concepts such as transactions, mining, and chain validation.
- Transaction Handling: Create and sign transactions using elliptic curve cryptography (via the Elliptic library).
- Block Creation: Blocks store transactions and link to the previous block, forming a chain.
- Mining: Blocks are mined by solving a proof-of-work challenge based on difficulty.
- Chain Validation: The blockchain validates the integrity of each block and its transactions.
- Miner Rewards: Miners are rewarded with cryptocurrency for successfully mining a block.
crypto-js: For SHA256 hashing to secure blocks and transactions.elliptic: For elliptic curve cryptography (used to sign and verify transactions).
main.js: Contains the main blockchain logic, includingBlock,Transaction, andChainclasses.proofOfWork.js: Manages mining logic and proof-of-work challenges.package.json: Contains dependencies and project details.
-
Transactions:
- Transactions are signed by the sender's private key and verified using their public key.
-
Blocks:
- Blocks group transactions and reference the hash of the previous block, forming a chain.
-
Mining:
- Miners solve a proof-of-work puzzle by finding a hash with a required number of leading zeros.
- Once mined, the block is added to the chain and the miner receives a reward.
-
Validation:
- The blockchain can be validated to ensure each block and transaction is untampered by checking hashes.
- Install dependencies:
npm install
- Run the project:
node main.js
Here's how to create a transaction, sign it, add it to the blockchain, and mine it:
// Generate key pairs for sender and receiver
const keyPairSender = ec.genKeyPair();
const publicKeySender = keyPairSender.getPublic('hex');
const keyPairReceiver = ec.genKeyPair();
const publicKeyReceiver = keyPairReceiver.getPublic('hex');
// Create and sign a new transaction
const transaction = new Transaction(publicKeySender, publicKeyReceiver, 10);
transaction.sign(keyPairSender);
// Add the transaction to the blockchain and mine the block
CowCoin.addTransaction(transaction);
CowCoin.mineTransactionPoll(publicKeyReceiver);
// Display the blockchain and its transactions
console.log(CowCoin.chain);
console.log(CowCoin.chain[1].transactions);This project is licensed under the MIT License. The MIT License is a permissive free software license that allows for reuse within proprietary software, provided that all copies include the original license and copyright notice.
For more details, see the LICENSE file included in this repository.