-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.js
50 lines (40 loc) · 920 Bytes
/
block.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
48
49
50
const { sha256 } = require("js-sha256");
const { newMerkleTree } = require("./merkle");
const crypto = require("crypto");
class Block {
constructor(
Timestamp,
Hash,
Transactions,
PrevHash,
Nonce,
Height,
MerkleRoot = null
) {
this.Hash = Hash;
this.Header = {
Timestamp: Timestamp,
PrevHash: PrevHash,
Nonce: Nonce,
Height: Height,
MerkleRoot: MerkleRoot,
};
this.Transactions = Transactions;
}
hashTransactions() {
let hashes = [];
if (!this.Header.MerkleRoot) {
this.Transactions.map((item) => {
hashes.push(item.ID);
});
let tree = newMerkleTree(hashes);
this.Header.MerkleRoot = tree.rootNode.data;
}
const blockHash = crypto
.createHash("sha256")
.update(JSON.stringify(this.Header))
.digest("hex");
return blockHash;
}
}
module.exports = { Block };