-
Notifications
You must be signed in to change notification settings - Fork 31.1k
Expand file tree
/
Copy pathBlockchainBlock.js
More file actions
53 lines (49 loc) · 1.54 KB
/
Copy pathBlockchainBlock.js
File metadata and controls
53 lines (49 loc) · 1.54 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
import crypto from 'crypto';
export default class Block {
/**
* @param {Object} blockParams - The block parameters
* @param {number} blockParams.index - The position of the block in the blockchain
* @param {string} [blockParams.timestamp] - The creation time of the block in ISO format
* @param {Array|String} blockParams.data - The transaction data stored in this block
* @param {string} blockParams.previousHash - The hash of the previous block in the chain
* @param {number} [blockParams.nonce=0] - The number used once for proof of work calculations
*/
constructor({
index,
timestamp = new Date().toISOString(),
data,
previousHash,
nonce = 0,
}) {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.nonce = nonce;
this.hash = this.calculateHash();
}
/**
* @private
* @returns {string} The hexadecimal representation of the block's hash
*/
calculateHash() {
const blockData = JSON.stringify({
index: this.index,
timestamp: this.timestamp,
data: this.data,
previousHash: this.previousHash,
nonce: this.nonce,
});
return crypto.createHash('sha256').update(blockData).digest('hex');
}
/**
* @param {number} difficulty - The number of leading zeros required in the hash
*/
mineBlock(difficulty) {
const target = '0'.repeat(difficulty);
while (this.hash.substring(0, difficulty) !== target) {
this.nonce += 1;
this.hash = this.calculateHash();
}
}
}