-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.js
More file actions
31 lines (25 loc) · 874 Bytes
/
Copy pathblock.js
File metadata and controls
31 lines (25 loc) · 874 Bytes
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
const crypto = require('crypto');
class Block {
constructor(timestamp, transactions, previousHash = '', difficulty = 2) {
this.timestamp = timestamp;
this.transactions = transactions;
this.previousHash = previousHash;
this.nonce = 0;
this.difficulty = difficulty;
this.minerReward = minerReward;
this.totalFees = totalFees;
this.hash = this.calculateHash();
}
calculateHash() {
const data = this.previousHash + this.timestamp + JSON.stringify(this.transactions)+ this.nonce+this.minerReward+this.totalFees;
return crypto.createHash('sha256').update(data).digest('hex');
}
mineBlock() {
while (this.hash.substring(0, this.difficulty) !== Array(this.difficulty + 1).join("0")) {
this.nonce++;
this.hash = this.calculateHash();
}
console.log(`Bloco minerado: ${this.hash}`);
}
}
module.exports = Block;