This is a blockchain that illustrates the basic principles of the functioning of blockchain technology. It is an implementation of my own minimal version of the blockchain (without decentralization and consensus, only basic mechanics).
- git
- You'll know you've installed it right if you can run:
git --version
- You'll know you've installed it right if you can run:
- Metamask
- This is a browser extension that lets you interact with the blockchain.
- Nodejs
- You'll know you've installed nodejs right if you can run:
node --version
And get an ouput like:vx.x.x
- You'll know you've installed nodejs right if you can run:
- Yarn instead of
npm
- You'll know you've installed yarn right if you can run:
yarn --version
And get an output like:x.x.x
- You might need to install it with npm
- You'll know you've installed yarn right if you can run:
- Blocks
- A blockchain
- Hashing
- Transactions and transaction creation
- Sign transactions
- Simple proof-of-work algorithm
- Verify blockchain (to prevent tampering)
- Generate wallet (private/public key)
yarn
Replace what is in main.js with the following...
To make transactions on this blockchain you need a keypair. The public key becomes your wallet address and the private key is used to sign transactions.
import EC from "elliptic/lib/elliptic/ec/index.js";
const ec = new EC("secp256k1");
const myKey = ec.genKeyPair();
The myKey
object now contains your public & private key:
console.log('Public key:', myKey.getPublic('hex'));
console.log('Private key:', myKey.getPrivate('hex'));
Now you can create a new instance of a Blockchain:
import Blockchain from "./blockchain.js";
import Transaction from "./transaction.js"
const myChain = new Blockchain();
// Transfer 100 coins from my wallet to "toAddress"
const tx = new Transaction(myKey.getPublic('hex'), 'toAddress', 100);
tx.signTransaction(myKey);
myChain.addTransaction(tx);
To finalize this transaction, we have to mine a new block. We give this method our wallet address because we will receive a mining reward:
myChain.minePendingTransactions(myKey.getPublic('hex'));