This project implements a basic blockchain in JavaScript for research purposes to understand its inner workings. This is by no means a complete implementation and it is by no means secure.
- Generate wallet (private/public key).
- Sign transactions.
- Genesis block.
- Add transactions to the block.
- Simple proof-of-work algorithm (mining).
- Verify blockchain (to prevent tampering).
Take a look or run the main.js file to see step by step the full process.
cd src
node main.js
npm install
To make transactions on this blockchain you need a keypair (private and public key). The public key becomes your wallet address and the private key is used to sign transactions (see: keygenerator.js).
const EC = require('elliptic').ec;
const ec = new EC('secp256k1');
const myKeyPair = ec.genKeyPair();
The myKey object now contains your public & private key:
console.log('Public key:', myKeyPair.getPublic('hex'));
console.log('Private key:', myKeyPair.getPrivate('hex'));
Now you can create a new instance of a Blockchain:
const {Blockchain, Transaction} = require('blockchain');
const myChain = new Blockchain();
// Transfer 100 coins from my wallet to "<toAddress1>".
// NOTE: <toAddress> should be another public key.
const tx = new Transaction(myKeyPair.getPublic('hex'), '<toAddress1>', 100);
tx.signTransaction(myKeyPair);
myChain.addTransaction(tx);
To finalize this transaction, we have to mine a new block:
myChain.minePendingTransactions('<minerAddress>');
This project is baseo on SavjeeCoin project.
Modifications to the original project:
- Full documentation on classes, methods and parameters.
- Additional documentation.
- Added eslint rules to force documentation (jsdoc).
- Add TransactionsRoot field to the block header (Merkle tree).
- Generate keypairs by mnemonic phrase.