-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.js
124 lines (119 loc) · 3.47 KB
/
block.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const { sha256 } = require("js-sha256");
const blockchain = [];
let difficulty = Math.floor(Math.random() * 4 + 1);
// Adding new block into blockchain
function addingBlock(transaction) {
const block = {
id: "",
nonce: "",
timeStemp: "",
transaction: "",
previousHash: "",
currentHash: "",
};
block.id = blockchain.length + 1;
block.timeStemp = Date().slice(16, 24);
block.transaction = generateTransactionHash(transaction);
block.previousHash = generatePreviousHash(block.id);
[block.currentHash, block.nonce] = generateCurrentHash(
block.id,
block.timeStemp,
block.transaction,
block.previousHash
);
blockchain.push(block);
}
function generateCurrentHash(id, timeStemp, transaction, previousHash) {
let tempNonce = 0;
while (
sha256("" + id + tempNonce + timeStemp + transaction + previousHash).slice(
0,
difficulty
) != Array(difficulty + 1).join("0")
) {
tempNonce++;
}
return [
sha256("" + id + tempNonce + timeStemp + transaction + previousHash),
tempNonce,
];
}
function generatePreviousHash(id) {
return id == 1
? "0000000000000000000000000000000000000000000000000000000000000000"
: blockchain[id - 2].currentHash;
}
function generateTransactionHash(transaction) {
let output = "";
if (transaction.length === 0) return sha256(" ");
if (transaction.length === 1) {
return sha256(transaction[0]);
}
if (transaction.length % 2 != 0) {
transaction.push(transaction[transaction.length - 1]);
}
let newPair = [];
for (let i = 0; i < transaction.length; i += 2) {
let leftNode = transaction[i];
let rightNode = transaction[i + 1];
let computedHash = sha256(sha256(leftNode) + sha256(rightNode));
newPair.push(computedHash);
}
output = generateTransactionHash(newPair);
return output;
}
addingBlock([
"Transaction :- Vedant to Raj => 50$",
"Transaction :- Raj to Raj => 65$",
"Transaction :- Vedant to Raj => 75$",
"Transaction :- Soni to Raj => 550$",
"Transaction :- Vedant to Raj => 59$",
]);
addingBlock([
"Transaction :- Vedant to Raj => 50$",
"Transaction :- Raj to Raj => 65$",
"Transaction :- Vedant to Raj => 15$",
"Transaction :- Vedant to Raj => 50$",
"Transaction :- Vedant to Raj => 50$",
]);
addingBlock([
"Transaction :- Vedant to Raj => 50$",
"Transaction :- Raj to Raj => 65$",
"Transaction :- Vedant to Raj => 50$",
"Transaction :- Vedant to Raj => 50$",
"Transaction :- Vedant to Raj => 5$",
]);
addingBlock([
"Transaction :- Vedant to Raj => 50$",
"Transaction :- Raj to Raj => 65$",
"Transaction :- Vedant to Raj => 50$",
"Transaction :- Vedant to Raj => 50$",
"Transaction :- Vedant to Raj => 53$",
]);
console.log(blockchain);
function verification() {
for (let i = 0; i < blockchain.length - 1; i++) {
if (blockchain[i].currentHash != blockchain[i + 1].previousHash) {
console.log("Block is tempered");
break;
} else {
console.log("Block mining is successfull ===>>");
}
}
}
verification();
console.log("After tempering random Block");
function tempereBlock() {
let blockID = Math.floor(Math.random() * 4);
blockchain[blockID].transaction = generateTransactionHash(["Double Payment"]);
[blockchain[blockID].currentHash, blockchain[blockID].nonce] =
generateCurrentHash(
blockchain[blockID].id,
blockchain[blockID].timeStemp,
blockchain[blockID].transaction,
blockchain[blockID].previousHash
);
}
tempereBlock();
console.log(blockchain);
verification();