Lightweight library that lets you create Merkle trees with custom branching factors.
The library supports all hashing algorithms native to V out of the box.
You can also use custom hashing algorithms that are implemented according to the interface.
pub type HashFunction = fn (data []u8) []u8
pub struct MerkleTree {
branching_factor int = 2
hash_function HashFunction [required]
mut:
root Node
}
Builds Merkle tree structure and pre-computes hashes with given data blocks.
pub fn (mut m MerkleTree) build(blocks [][]u8)
Returns Merkle root of tree as byte array.
pub fn (mut m MerkleTree) get_root() []u8
In case you want to implement a custom hashing algorithm, please do so according to this blueprint.
pub type HashFunction = fn (data []u8) []u8
import merkletree { MerkleTree }
import crypto.sha256
fn main() {
mut tree := &MerkleTree{
hash_function: sha256.sum
}
tree.build([
'1'.bytes(),
'2'.bytes(),
'3'.bytes(),
'4'.bytes(),
])
print(tree.get_root().hex())
}
Create an issue or a pull request.
This project is licensed under the MIT license. Feel free to do whatever you want with the code!