-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerkleTree.java
More file actions
62 lines (50 loc) · 2.15 KB
/
MerkleTree.java
File metadata and controls
62 lines (50 loc) · 2.15 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
package org.example;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class MerkleTree {
private final String root;
public MerkleTree(List<Transaction> transactions) {
this.root = buildTree(transactions.stream()
.map(Transaction::getTransactionHash)
.collect(Collectors.toList()));
}
private String buildTree(List<String> transactionHashes) {
List<String> currentLevel = transactionHashes;
while (!currentLevel.isEmpty() && currentLevel.size() > 1) {
List<String> nextLevel = new ArrayList<>();
for (int i = 0; i < currentLevel.size() - 1; i += 2) {
String concatenated = currentLevel.get(i) + currentLevel.get(i + 1);
String hash = calculateHash(concatenated);
nextLevel.add(hash);
}
if (currentLevel.size() % 2 != 0) {
// If there's an odd number of elements, duplicate the last one
String lastElement = currentLevel.get(currentLevel.size() - 1);
String duplicatedHash = calculateHash(lastElement + lastElement);
nextLevel.add(duplicatedHash);
}
currentLevel = nextLevel;
}
return currentLevel.isEmpty() ? null : currentLevel.get(0); // The root of the Merkle tree
}
static String calculateHash(String data) {
// This is a simplified hash function; you might want to use a stronger one
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(data.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
hexString.append(String.format("%02x", b));
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public String getRoot() {
return root;
}
}