Well well well, what do we have here?
Just what the world needs another blog!
Guess I’m doing this to scratch a personal itch more than anything. Let’s see where my scatter brain takes us shall we?
Web3 Course Walkthrough on freeCodeCamp.org
Spent a couple of hours today learning more about blockchains with this awesome freeCodeCamp.org tutorial. Very easy to setup and understand the mechanics of blockchains.
Definitely worth checking out their YouTube video here.
mine-block.js
import {
getBlockchain,
getTransactions,
writeBlockchain,
writeTransactions
} from './blockchain-helpers.js';
import sha256 from 'crypto-js/sha256.js';
// Add your code below
const blockchain = getBlockchain();
const lastBlock = blockchain[blockchain.length-1];
const transactions = getTransactions();
let hash = '';
let nonce = 0;
while(!hash.startsWith('00')) {
nonce++;
hash = sha256(nonce + lastBlock.hash + JSON.stringify(transactions)).toString()
}
const newBlock = {
nonce,
hash,
previousHash: lastBlock.hash,
transactions,
};
blockchain.push(newBlock);
writeBlockchain(blockchain);
writeTransactions([]);