-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.js
More file actions
30 lines (26 loc) · 766 Bytes
/
block.js
File metadata and controls
30 lines (26 loc) · 766 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
const crypto = require('crypto');
const { scripthash } = require('./wallet');
class Block {
constructor(index, timestamp, transactions, previousHash = '', nonce = 0) {
this.index = index;
this.timestamp = timestamp;
this.transactions = transactions;
this.previousHash = previousHash;
this.nonce = nonce;
this.hash = this.calculateHash();
}
calculateHash() {
return scripthash(
this.index + this.timestamp + JSON.stringify(this.transactions) + this.previousHash,
this.nonce
);
}
mineBlock(difficulty) {
while (!this.hash.startsWith('0'.repeat(difficulty))) {
this.nonce++;
this.hash = this.calculateHash();
}
console.log(`✅ Block mined: ${this.hash}`);
}
}
module.exports = Block;