ehsomma / myblockchain

Simple Blockchain implementation in Javascript. For research purposes only.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MyBlockChain

.github/workflows/ci.yml Coverage Status GitHub Issues License

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.

Features

  • 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).

Example

Take a look or run the main.js file to see step by step the full process.

cd src
node main.js

Getting Started

Fork or download the repo


Install the dependencies

npm install

Generate a keypair

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'));

Create a blockchain instance

Now you can create a new instance of a Blockchain:

const {Blockchain, Transaction} = require('blockchain');
const myChain = new Blockchain();

Adding transactions

// 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);

Mining blocks

To finalize this transaction, we have to mine a new block:

myChain.minePendingTransactions('<minerAddress>');

Original project

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).

TODO:

  • Add TransactionsRoot field to the block header (Merkle tree).
  • Generate keypairs by mnemonic phrase.

About

Simple Blockchain implementation in Javascript. For research purposes only.

License:MIT License


Languages

Language:JavaScript 100.0%