-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
90 lines (70 loc) · 2.47 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const SHA256 = require('crypto-js/sha256');
/*class to create blocks*/
class Block{
//contructor will create each block
constructor(index, timestamp, data, previousHash = ''){
this.index = index; //where the block sits on chain
this.timestamp = timestamp; //time of creating the block
this.data = data, //any data associated with block
this.previousHash = previousHash; //contains hash of previous block
this.hash = this.calculateHash(); //will contain hash for current block
}
//calculating hash for current block
//returns 256 bit hash for the current block
calculateHash(){
return SHA256(this.index + this.timestamp + JSON.stringify(this.data) + this.previousHash).toString();
}
}
/*class to create blockchain*/
class Blockchain{
//contructor to initalize the blockchain
constructor(){
this.chain = [this.createGenesisBlock()];
}
//method to create first block of the chain
createGenesisBlock(){
return new Block(0,"01/01/2018","Gensis Block","0");
}
//returns last block of the chain
getLatestBlock(){
return this.chain[this.chain.length - 1];
}
//adds new block to the chain
addBlock(newBlock){
newBlock.previousHash = this.getLatestBlock().hash;
newBlock.hash = newBlock.calculateHash();
this.chain.push(newBlock);
}
//validating the chain
validateChain(){
for(let i=1; i < this.chain.length; i++){
const currentBlock = this.chain[i];
const previousBlock = this.chain[i - 1];
//checking the hash for current block
if(currentBlock.hash !== currentBlock.calculateHash()){
return false;
}
//checking the hash for previous block
if(currentBlock.previousHash !== previousBlock.hash){
return false;
}
}
return true;
}
}
/*Testing blockchain*/
//creating blockchain object
let altCoin = new Blockchain();
//adding blocks to current blockchain
altCoin.addBlock(new Block(1,"10/01/2018",{amount: 15}));
altCoin.addBlock(new Block(2,"15/01/2018",{amount: 25}));
altCoin.addBlock(new Block(3,"22/01/2018",{amount: 5}));
altCoin.addBlock(new Block(4,"30/01/2018",{amount: 100}));
altCoin.addBlock(new Block(5,"8/02/2018",{amount: 50}));
console.log(JSON.stringify(altCoin, null, 4));
console.log("\nIs blockchain valid? " + altCoin.validateChain());
//trying to alter the third block of the chain
altCoin.chain[3].data = {amount: 55};
altCoin.chain[3].hash = altCoin.chain[3].calculateHash();
console.log("\nAfter manipulating the 3rd block of the chain");
console.log("\nIs blockchain valid? " + altCoin.validateChain());