#merkle-tree #keccak256 #merkle-proof #merkle-root #blockchain

merkletreerust

🌳 简单易用的默克尔树实现

4个版本

0.1.7 2023年2月21日
0.1.6 2023年2月21日
0.1.5 2023年2月7日
0.1.4 2023年2月7日

#7 in #keccak256

每月23次下载

MIT许可证

23KB
375


merkletree.js logo


MerkleTreeRust

安装

# Cargo.toml

[dependencies]
merkletreerust = "*"

入门指南

构建树,生成证明

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);

输出

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

图表

▾ 默克尔树可视化

Merkle Tree

▾ 默克尔树证明可视化

Merkle Tree Proof

▾ 无效默克尔树证明可视化

Merkle Tree Proof

▾ 比特币默克尔树可视化

Merkle Tree Proof

依赖关系

~76KB