forked from spandey1296/Learn-Share-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ReplayBlockchain.js
60 lines (53 loc) · 1.78 KB
/
ReplayBlockchain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const SHA256 = require("crypto-js/sha256");
class BlockCrypto {
constructor(index, current_time, info, prevHash = " ") {
this.index = index;
this.current_time = current_time;
this.info = info;
this.prevHash = prevHash;
this.hash = this.computeHash();
}
computeHash() {
return SHA256(
this.info + this.prevHash + this.current_time + JSON.stringify(this.info)
).toString();
}
}
class Blockchain {
constructor() {
this.block1chain = [this.initGenesisBlock()];
}
initGenesisBlock() {
return new BlockCrypto(0, "11/08/2021", "first block", "0");
}
latestBlock() {
return this.block1chain[this.block1chain.length - 1];
}
addNewBlock(newBlock) {
newBlock.prevHash = this.latestBlock().hash;
newBlock.hash = newBlock.computeHash();
this.block1chain.push(newBlock);
}
checkValidity(){
// Checking validity
for(let i = 1; i < this.block1chain.length; i++) {
const currentBlock = this.block1chain[i];
const nextBlock= this.block1chain[i-1];
// Checking current block hash
if(currentBlock.hash !== currentBlock.computeHash()) {
return false;
}
// Comparing current block hash with the next block
if(currentBlock.prevHash !== nextBlock.hash) {
return false;
}
return true;
}
}
}
let thecoin = new Blockchain();
thecoin.addNewBlock(new BlockCrypto(1, "11/08/2021", {sender: "Dishant Lodaliya", recipient: "Brijesh Patel", quantity: 20}));
thecoin.addNewBlock(new BlockCrypto(2, "12/08/2021", {sender: "Akshat Mehta", recipient: "Jeet Sodha", quantity: 349}));
thecoin.addNewBlock(new BlockCrypto(2, "12/08/2021", {sender: "Deep Menpara", recipient: "Dishant Lodaliya", quantity: 349}));
console.log(thecoin.checkValidity());
console.log(JSON.stringify(thecoin, null,4));