forked from BlockchainIITBH/ChainRxn-Project1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain.js
More file actions
68 lines (58 loc) · 1.89 KB
/
blockchain.js
File metadata and controls
68 lines (58 loc) · 1.89 KB
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
const crypto = require('crypto');
class Block {
constructor(index, timestamp, data, previousHash = '') {
this.index = index; // Block number in the chain
this.timestamp = timestamp; // Time of creation
this.data = data; // Transaction data
this.previousHash = previousHash; // Hash of the previous block
this.hash = this.calculateHash(); // Hash of the current block
}
// Fake/simple hash function using SHA256 for demonstration
calculateHash() {
const toHash = this.index + this.timestamp + JSON.stringify(this.data) + this.previousHash;
return crypto.createHash('sha256').update(toHash).digest('hex');
}
}
// Blockchain class to manage the chain of blocks
class Blockchain {
constructor() {
this.chain = [this.createGenesisBlock()];
}
// Creates the Genesis block (first block in the chain)
createGenesisBlock() {
return new Block(0, new Date().toISOString(), 'Genesis Block', '0');
}
// Get the latest block in the chain
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
// Adds a new block with given data
addBlock(newData) {
const prevBlock = this.getLatestBlock();
const newBlock = new Block(
prevBlock.index + 1, // Increment index
new Date().toISOString(), // Current time
newData, // Transaction/message
prevBlock.hash // Link to previous block
);
this.chain.push(newBlock); // Add new block to the chain
}
// Checks for chain integrity (tampering detection)
isChainValid() {
for (let i = 1; i < this.chain.length; i++) {
const curr = this.chain[i];
const prev = this.chain[i - 1];
if (curr.hash !== curr.calculateHash()) {
return false;
}
if (curr.previousHash !== prev.hash) {
return false;
}
}
return true;
}
// Prints the entire blockchain
printChain() {
console.log(JSON.stringify(this.chain, null, 2));
}
}