-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockchain.js
47 lines (47 loc) · 1.54 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
const sha256 = require("sha256");
function Blockchain(){
this.chain = [];
this.pendingTransactions = [];
this.createNewBlock(100000,"PreviousBlockHash","CurrentBlockHash");
}
Blockchain.prototype.createNewBlock = function(nonce,prevHash,hash){
const newBlock = {
nonce: nonce,
prevHash: prevHash,
hash: hash,
index: this.chain.length+1,
timestamp: Date.now(),
transactions: this.pendingTransactions,
};
this.pendingTransactions = [];
this.chain.push(newBlock);
return newBlock;
}
Blockchain.prototype.getLastBlock = function(){
return this.chain[this.chain.length-1];
}
Blockchain.prototype.createNewTransactions = function(amount,sender,reciver){
var newTransactions={
amount: amount,
sender: sender,
reciver: reciver,
};
this.pendingTransactions.push(newTransactions);
return this.getLastBlock().index + 1;
}
Blockchain.prototype.createHashBlock = function(previousBlockHash,currentBlockData,nonce){
const dataAsString = previousBlockHash+nonce.toString()+JSON.stringify(currentBlockData);
const hash = sha256(dataAsString);
return hash;
}
Blockchain.prototype.proofOfWork = function(previousBlockHash,currentBlockData){
let nonce = 0;
let hash = this.createHashBlock(previousBlockHash,currentBlockData,nonce);
while(hash.substring(0,4)!=='0000'){
nonce++;
hash = this.createHashBlock(previousBlockHash,currentBlockData,nonce);
console.log(hash);
}
return nonce;
}
module.exports = Blockchain;