diff --git a/README.md b/README.md index 4271eff..5ccbc28 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Bitcoin Whitepaper Exercises +# Bitcoin Whitepaper Exercises - This is Vijay's version In these exercises, you will learn about blockchains from the perspective of the [original Bitcoin Whitepaper](https://bitcoin.org/en/bitcoin-paper). diff --git a/hashing/hashing.js b/hashing/hashing.js index 83c81a5..243f2ee 100644 --- a/hashing/hashing.js +++ b/hashing/hashing.js @@ -28,8 +28,29 @@ Blockchain.blocks.push({ }); // TODO: insert each line into blockchain -// for (let line of poem) { -// } +for (let line of poem) { + createBlock(line) +} + +// Create function createBlock +// `index` +// `prevHash` +// `data` +// `timestamp` +// `hash` + +function createBlock(_data) { + let block = { + index: Blockchain.blocks.length, + prevHash: Blockchain.blocks[Blockchain.blocks.length - 1].hash, + data: _data, + timestamp: Date.now() + } + block.hash = blockHash(block) + Blockchain.blocks.push(block) + console.log(block) + return block +} // console.log(`Blockchain is valid: ${verifyChain(Blockchain)}`); @@ -39,5 +60,6 @@ Blockchain.blocks.push({ function blockHash(bl) { return crypto.createHash("sha256").update( // TODO: use block data to calculate hash + `${bl.index};${bl.prevHash};${bl.data};${bl.timestamp};` ).digest("hex"); }