The purpose of this implementation is to hash and sign each type of tree node using algorithms algorithms.
Available types are:
- LeafNode - the node which usually contains the data
- TreeNode - the tree node between leaf node and top node
- TopNode - top node - usually needs to be signed
To check the node type:
node.is(LeafNode.class);
or node.as(LeafNode.class).isPresent();
To get parent of the leaf node for instance:
LeafNode parentNode = node.as(LeafNode.class).map(LeafNode::getParent).orElse(null);
https://en.wikipedia.org/wiki/Merkle_tree
See MerkleTreeConfiguration constructors First let's create a function to transform byte to String signature
For instance, when you have local methods:
To hash the leaf data
private static byte[] leafNodeHasher(String data);
To combine two byte arrays (hash the tree and top node
private static byte[] fromByteArray(byte[] left, byte[] right);
Then you can initiate the configuration:
MerkleTreeConfiguration<byte[], byte[], String> merkleTreeConfiguration = new MerkleTreeConfiguration<byte[], byte[], String>(MerkleTreeBuilderTest::leafNodeHasher, MerkleTreeBuilderTest::fromByteArray);
Create the builder:
MerkleTreeBuilder<byte[], byte[], String> merkleTreeBuilder = merkleTreeConfiguration.getBuilder();
Next add data:
merkleTreeBuilder.addDataBlock("lorem");
merkleTreeBuilder.addDataBlock("ipsum");
merkleTreeBuilder.addDataBlock("est");
//etc..
When you are ready to build the tree just call the method below. Note that it will clean up data added
List<LeafNode<byte[]>> result = merkleTreeBuilder.buildTree();
Check out the tests for use cases and acessing the nodes.