merkletreerust

Crates.iomerkletreerust
lib.rsmerkletreerust
version0.1.7
sourcesrc
created_at2023-02-07 07:59:52.062833
updated_at2023-02-21 08:25:55.833734
description🌳 The simple and easy implementation of Merkle Tree
homepage
repositoryhttps://github.com/hxfjsw/merkletreerust.git
max_upload_size
id778577
size23,232
(hxfjsw)

documentation

README


merkletree.js logo


MerkleTreeRust

Install

# Cargo.toml

[dependencies]
merkletreerust = "*"

Getting started

Construct tree, generate proof

use merkeltreerust::merkeltree::MerkelTree;
use merkeltreerust::option::Options;
use merkeltreerust::buffer::Buffer;
use merkeltreerust::error::MerkelTreeError;
use tiny_keccak::{Hasher, Keccak};

let whitelist_address: Vec<Buffer> = vec![
    "0x6dC0c0be4c8B2dFE750156dc7d59FaABFb5B923D".parse::<Buffer>()?,
    "0xa8d17cc9caf29af964d19267ddeb4dff122697b0".parse::<Buffer>()?
];

let options = Options { duplicate_odd: false, sort_pairs: true, sort_leaves: true, sort: true, hash_leaves: true };
let hash_fn = |buf: &[u8]| {
    let mut k256 = Keccak::v256();
    let mut result = [0; 32];
    k256.update(buf);
    k256.finalize(&mut result);
    result.to_vec()
};
let merkle_tree = MerkelTree::new(whitelist_address, hash_fn, options);

let root = merkle_tree.get_root()?.to_hex();
let leaf = "0x6dC0c0be4c8B2dFE750156dc7d59FaABFb5B923D";
let proof = merkle_tree.get_hex_proof(leaf.parse::<Buffer>()?)?;

println!("root: {:?}", root);
println!("leaf: {:?}", leaf);
println!("proof: {:?}", proof);

Output:

root:0xdb44a1f32851683f64d15a563ecd3686b67de2075821b6196dbaf7d25604592f
leaf:0x6dC0c0be4c8B2dFE750156dc7d59FaABFb5B923D
proof:["0x7fa4f9a213fc25511745e0fe7627ab0d7145664238bd854fb781559c2ddbf9c4"]

Diagrams

â–¾ Visualization of Merkle Tree

Merkle Tree

â–¾ Visualization of Merkle Tree Proof

Merkle Tree Proof

â–¾ Visualization of Invalid Merkle Tree Proofs

Merkle Tree Proof

â–¾ Visualization of Bitcoin Merkle Tree

Merkle Tree Proof
Commit count: 19

cargo fmt