-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockChain.js
More file actions
191 lines (155 loc) · 4.71 KB
/
BlockChain.js
File metadata and controls
191 lines (155 loc) · 4.71 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const sha256 = require("sha256");
const uuid = require("uuid").v4;
function BlockChain() {
this.chain = []; // will keep all the transactions
this.pendingTransactions = []; // will keep the transactions which are not yet mined by block
// genesis block the start block
this.createNewBlock(
0,
"0",
this.hashBlock(this.proofOfWork("0", "0"), "0", "0")
);
// network related
this.currentNodeURL = "http://localhost:" + process.env.PORT;
this.networkNodeURLs = [];
}
// create New Block
BlockChain.prototype.createNewBlock = function (
nonce, // proof of workDone
previousBlockHash,
hash
) {
// a new Block
const newBlock = {
index: this.chain.length + 1,
timestamp: Date.now(),
transactions: this.pendingTransactions,
nonce,
previousBlockHash,
hash,
};
// add to the chain and empty the pending transactions
this.chain.push(newBlock);
this.pendingTransactions = [];
// return the newBlock created
return newBlock;
};
// get Last Block
BlockChain.prototype.getLastBlock = function () {
return this.chain[this.chain.length - 1];
};
// create a new Transaction
BlockChain.prototype.createNewTransaction = function (
amount,
sender,
recipient
) {
// creating a transaction
const newTransaction = {
amount,
sender,
recipient,
transactionId: uuid().split("-").join(""),
};
return newTransaction;
};
// add txns to pending txns
BlockChain.prototype.addTransactionsToPendingTransactions = function (
transactionObj
) {
this.pendingTransactions.push(transactionObj);
return this.getLastBlock()["index"] + 1;
};
// hash the blockdata
BlockChain.prototype.hashBlock = function (
nonce,
previousBlockHash,
currentBlockData
) {
const blockData =
previousBlockHash + nonce.toString() + JSON.stringify(currentBlockData);
const hash = sha256(blockData);
return hash;
};
// proof of work : will make the blockChain secure as it has to more computation and energy
// generate hash and increment nonce until the hash starting with 5 0's not there.
BlockChain.prototype.proofOfWork = function (
previousBlockHash,
currentBlockData
) {
let nonce = 0;
let hash = this.hashBlock(nonce, previousBlockHash, currentBlockData);
while (hash.substring(0, 5) !== "00000") {
nonce++;
hash = this.hashBlock(nonce, previousBlockHash, currentBlockData);
}
// return the nonce which will be used for next block and for validating a block we just have to hash and chcek if its valid hash ie starting 5 as 0's.
return nonce;
};
// validate the blockchain
BlockChain.prototype.chainIsValid = function (blockChain) {
// check if the previousHash of current == hash of the previous
let validate = true;
for (let i = 1; i < blockChain.chain.length; i++) {
const currentBlock = blockChain.chain[i];
const previousBlock = blockChain.chain[i - 1];
const currentBlockPreviousHash = currentBlock["previousBlockHash"];
const previousBlockHash = previousBlock["hash"];
// check the hash of currentData
const blockHash = this.hashBlock(
currentBlock["nonce"],
currentBlock["previousBlockHash"],
{
index: currentBlock["index"],
transactions: currentBlock["transactions"],
}
);
if (blockHash.substring(0, 5) !== "00000") validate = false;
// check previous hash with current's previous Hash
if (previousBlockHash !== currentBlockPreviousHash) validate = false;
}
return validate;
};
BlockChain.prototype.getBlock = function (blockHash) {
let correctBlock = null;
this.chain.forEach((block) => {
if (block.hash === blockHash) correctBlock = block;
});
return correctBlock;
};
BlockChain.prototype.getTransaction = function (transactionId) {
let correctTransaction = null;
let correctBlock = null;
this.chain.forEach((block) => {
block.transactions.forEach((transaction) => {
if (transaction.transactionId === transactionId) {
correctTransaction = transaction;
correctBlock = block;
}
});
});
return {
transaction: correctTransaction,
block: correctBlock,
};
};
BlockChain.prototype.getAddressData = function (address) {
const addressTransactions = [];
this.chain.forEach((block) => {
block.transactions.forEach((transaction) => {
if (transaction.sender === address || transaction.recipient === address) {
addressTransactions.push(transaction);
}
});
});
let balance = 0;
addressTransactions.forEach((transaction) => {
if (transaction.recipient === address) balance += transaction.amount;
else if (transaction.sender === address) balance -= transaction.amount;
});
return {
addressTransactions: addressTransactions,
addressBalance: balance,
};
};
module.exports = BlockChain;