Repository navigation
Expand file tree
/
Copy pathblock.js
More file actions
76 lines (64 loc) · 1.89 KB
/
Copy pathblock.js
File metadata and controls
76 lines (64 loc) · 1.89 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const crypto = require("crypto");
exports.BlockHeader = class BlockHeader {
constructor(version, previousBlockHeader, merkleRoot, time, difficulty) {
this.version = version;
this.previousBlockHeader = previousBlockHeader;
this.merkleRoot = merkleRoot;
this.time = time;
this.difficulty = difficulty;
}
};
exports.Block = class Block {
constructor(blockHeader, index, transactions, tx) {
this.blockHeader = blockHeader;
this.index = index;
this.transactions = transactions;
this.tx = tx;
this.nonce = 0;
this.blockHeader.merkleRoot = this.calculateMerkleRoot(transactions);
}
calculateMerkleRoot(transactions) {
let transactionHashes = transactions.map((transaction) =>
crypto
.createHash("sha256")
.update(JSON.stringify(transaction))
.digest("hex")
);
while (transactionHashes.length > 1) {
if (transactionHashes.length % 2 !== 0) {
transactionHashes.push(transactionHashes[transactionHashes.length - 1]);
}
transactionHashes = this.pairwiseHash(transactionHashes);
}
return transactionHashes[0];
}
pairwiseHash(transactionHashes) {
let newHashes = [];
for (let i = 0; i < transactionHashes.length; i += 2) {
newHashes.push(
crypto
.createHash("sha256")
.update(transactionHashes[i] + transactionHashes[i + 1])
.digest("hex")
);
}
return newHashes;
}
};
exports.Transaction = class Transaction {
constructor(sender, receiver, amount, fee, signature, time, id) {
this.sender = sender;
this.receiver = receiver;
this.amount = amount;
this.signature = signature;
this.fee = fee;
this.time = time;
this.id = id;
}
calculateHash() {
return crypto
.createHash("sha256")
.update(this.sender + this.receiver + this.amount + this.fee + this.time)
.digest("hex");
}
};