From 4faacc1c0dda12856bf9cfe720d89800aab73275 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Tue, 10 Sep 2019 18:03:27 +0200 Subject: [PATCH] update to external usage of the lib --- Cargo.toml | 2 +- README.md | 35 ++++++++++++++++++++++++++++------- src/lib.rs | 20 +++++++++++++------- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3736f15..d3cb8c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "merkletree-rs" -version = "0.0.1" +version = "0.0.2" authors = ["arnaucube "] edition = "2018" license = "GPL-3.0" diff --git a/README.md b/README.md index a403d4b..9ce1e52 100644 --- a/README.md +++ b/README.md @@ -7,32 +7,53 @@ Compatible with the Go version: https://github.com/arnaucube/go-merkletree ## Usage +Import lib: +```rust +extern crate merkletree_rs; +use merkletree_rs::{db, MerkleTree, TestValue, Value}; +``` + Create new tree: ```rust // to build the storage, the first parameter is the path and the second parameter specifies if wants to use a in_memory database or a directory of the filesystem let mut sto = db::Db::new("test".to_string(), true); -let mut mt: MerkleTree::new(&mut sto, 140); +let mut mt = MerkleTree::new(&mut sto, 140 as u32); ``` Add value to leaf: ```rust -let val = TestValue { - bytes: "this is a test leaf".as_bytes().to_vec(), - index_length: 15, +let val: TestValue = TestValue { + bytes: "this is a test leaf".as_bytes().to_vec(), + index_length: 15, }; -mt.add(&val); +mt.add(&val).unwrap(); ``` Get proof: ```rust let mp = mt.generate_proof(val.hi()); +println!("{:?}", mp); ``` Verify proof: ```rust // check if the value exist -let v = verify_proof(mt.root, mp, val.hi(), val.ht(), mt.num_levels); +let v = + merkletree_rs::verify_proof(mt.get_root(), &mp, val.hi(), val.ht(), mt.get_num_levels()); +println!("{:?}", v); // check if the don't value exist (in that case, the 'ht' will be an empty value) -let v = verify_proof(mt.root, mp, val.hi(), EMPTYNODEVALUE, mt.num_levels); +let v = merkletree_rs::verify_proof( + mt.get_root(), + &mp, + val.hi(), + merkletree_rs::constants::EMPTYNODEVALUE, + mt.get_num_levels(), +); +println!("{:?}", v); +``` + +Print current MerkleRoot: +```rust +println!("{:?}", mt.get_root()); ``` diff --git a/src/lib.rs b/src/lib.rs index cee5f5c..477c0a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,8 +14,8 @@ pub mod utils; type Result = std::result::Result; pub struct TestValue { - bytes: Vec, - index_length: u32, + pub bytes: Vec, + pub index_length: u32, } pub trait Value { fn bytes(&self) -> &Vec; @@ -57,6 +57,12 @@ impl<'a> MerkleTree<'a> { sto: database, } } + pub fn get_root(&mut self) -> [u8; 32] { + self.root + } + pub fn get_num_levels(&mut self) -> u32 { + self.num_levels + } pub fn add(&mut self, v: &TestValue) -> Result<()> { // add the leaf that we are adding @@ -366,7 +372,7 @@ impl<'a> MerkleTree<'a> { pub fn verify_proof( root: [u8; 32], - mp: Vec, + mp: &Vec, hi: [u8; 32], ht: [u8; 32], num_levels: u32, @@ -548,7 +554,7 @@ mod tests { assert_eq!("0000000000000000000000000000000000000000000000000000000000000001fd8e1a60cdb23c0c7b2cf8462c99fafd905054dccb0ed75e7c8a7d6806749b6b", mp.to_hex()); // verify - let v = verify_proof(mt.root, mp, val2.hi(), val2.ht(), mt.num_levels); + let v = verify_proof(mt.root, &mp, val2.hi(), val2.ht(), mt.num_levels); assert_eq!(true, v); } @@ -582,7 +588,7 @@ mod tests { // verify that is a proof of an empty leaf (constants::EMPTYNODEVALUE) let v = verify_proof( mt.root, - mp, + &mp, val3.hi(), constants::EMPTYNODEVALUE, mt.num_levels, @@ -609,7 +615,7 @@ mod tests { &hex::decode("786677808ba77bdd9090a969f1ef2cbd1ac5aecd9e654f340500159219106878") .unwrap(), ); - let v = verify_proof(root, mp, hi, ht, 140); + let v = verify_proof(root, &mp, hi, ht, 140); assert_eq!(true, v); } @@ -627,7 +633,7 @@ mod tests { &hex::decode("a69792a4cff51f40b7a1f7ae596c6ded4aba241646a47538898f17f2a8dff647") .unwrap(), ); - let v = verify_proof(root, mp, hi, constants::EMPTYNODEVALUE, 140); + let v = verify_proof(root, &mp, hi, constants::EMPTYNODEVALUE, 140); assert_eq!(true, v); }