update to external usage of the lib
arnaucube root@arnaucube.com
Tue, 10 Sep 2019 18:03:27 +0200
3 files changed,
42 insertions(+),
15 deletions(-)
M
Cargo.toml
→
Cargo.toml
@@ -1,6 +1,6 @@
[package] name = "merkletree-rs" -version = "0.0.1" +version = "0.0.2" authors = ["arnaucube <root@arnaucube.com>"] edition = "2018" license = "GPL-3.0"
M
README.md
→
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()); ```
M
src/lib.rs
→
src/lib.rs
@@ -14,8 +14,8 @@
type Result<T> = std::result::Result<T, String>; pub struct TestValue { - bytes: Vec<u8>, - index_length: u32, + pub bytes: Vec<u8>, + pub index_length: u32, } pub trait Value { fn bytes(&self) -> &Vec<u8>;@@ -56,6 +56,12 @@ root: constants::EMPTYNODEVALUE,
num_levels, 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<()> {@@ -366,7 +372,7 @@ }
pub fn verify_proof( root: [u8; 32], - mp: Vec<u8>, + mp: &Vec<u8>, hi: [u8; 32], ht: [u8; 32], num_levels: u32,@@ -548,7 +554,7 @@ let mp = mt.generate_proof(val2.hi());
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 @@
// 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 @@ ht.copy_from_slice(
&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 @@ hi.copy_from_slice(
&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); }