-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.js
77 lines (77 loc) · 2.07 KB
/
blockchain.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
import { SHA256 } from "bun";
// Create timestamp in format ISO 8601 yyyy-mm-ddThh:mm:ss.sssZ
const getCurrentDate = () => {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const milliseconds = date.getMilliseconds();
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${milliseconds}Z`;
}
// Create a blockchain class
class Blockchain {
constructor() {
// Set genesis block
this.chain = [];
this.createBlock(1, "0");
}
// Create a new block
createBlock(proof, previousHash) {
const block = {
index: this.chain.length + 1,
timestamp: getCurrentDate(),
proof: proof,
previousHash: previousHash,
};
// Add the block to the chain
this.chain.push(block);
return block;
}
// Get the last block
getpreviousBlock() {
return this.chain[this.chain.length - 1];
}
// Proof of work for the blockchain
proofOfWork(previousProof) {
let newProof = 1;
let checkProof = false;
while (!checkProof) {
// Create a hash of the previous proof and the new proof
const hash = SHA256.hash(
String(newProof ** 2 - previousProof ** 2),
"hex"
);
if (hash.substring(0, 4) === "0000") {
checkProof = true;
} else {
newProof++;
}
}
return newProof;
}
// Calculate hash of a block
hash(block) {
const blockString = JSON.stringify(block);
return SHA256.hash(blockString, "hex");
}
// Validate the blockchain
isChainValid(chain) {
for (let i = 1; i < chain.length; i++) {
const currentBlock = chain[i];
const previousBlock = chain[i - 1];
const hash = this.hash(previousBlock);
if (hash !== currentBlock.previousHash) {
return false;
}
if (currentBlock.proof !== this.proofOfWork(previousBlock.proof)) {
return false;
}
}
return true;
}
}
// Export the Blockchain class
export { Blockchain };