From 6fb54a22f74f13c216a96b59ca209ed28a8d55af Mon Sep 17 00:00:00 2001 From: jotabulacios <45471455+jotabulacios@users.noreply.github.com> Date: Tue, 7 Jan 2025 10:05:32 -0300 Subject: [PATCH] Update Merkle Tree CLI example (#955) * generate_merkle_proof now creates the root file * improve docs * update docs * fix typos and reorganize Readme * fix small typo --------- Co-authored-by: Diego K <43053772+diegokingston@users.noreply.github.com> --- examples/merkle-tree-cli/README.md | 42 +++++++++++++++++++++------- examples/merkle-tree-cli/src/main.rs | 26 +++++++++++++++-- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/examples/merkle-tree-cli/README.md b/examples/merkle-tree-cli/README.md index 5ad68a199..bd1d0a621 100644 --- a/examples/merkle-tree-cli/README.md +++ b/examples/merkle-tree-cli/README.md @@ -28,18 +28,49 @@ For example, the provided **`sample_tree.csv`** looks like this: ```bash cargo run --release generate-tree sample_tree.csv ``` +This will: +- Generate a `json` file with the tree structure and save it to the same directory as the `csv` file. + +- Save the root of the tree in a `txt` file named `_root.txt`. + + For example: + + ``` + sample_tree_root.txt + ``` + will contain + ``` + 0xa3bbbb9eac9f79d18862b802ea79f87e75efc37d4f4af4464976784c14a851b69c09aa04b1e8a8d1eb9825b713dc6ca + ``` + + +- Print the root of the tree in the terminal + ### To generate proof for a Merkle Tree you can use: ```bash cargo run --release generate-proof ``` +This will: +- Generate a `json` file with the proof for the leaf at the specified position and save it to the same directory as the `csv` file. + +- Save the value of the leaf in a `txt` file named `_leaf_.txt`. + **`generate-proof` example:** ```bash cargo run --release generate-proof sample_tree.csv 0 ``` +This will generate: + +- `sample_tree_proof_0.json` will contain the proof for the leaf at position 0. + +- `sample_tree_leaf_0.txt` will contain the value of the leaf at position 0. For example: + ``` + 0x12345 + ``` ### To verify a proof you can use: @@ -47,18 +78,9 @@ cargo run --release generate-proof sample_tree.csv 0 cargo run --release verify-proof ``` -The format of a root `txt` file is a simple text file which only containts the root as a hex string. Using the root that yields the merkle tree generated from the `sample_tree` provided, **`root.txt`** would look like this: -``` -0xa3bbbb9eac9f79d18862b802ea79f87e75efc37d4f4af4464976784c14a851b69c09aa04b1e8a8d1eb9825b713dc6ca -``` - -Likewise, the format of a leaf `txt` file is a simple text file which only contains the leaf as a hex string. Using the first element (index 0) of the provided `sample_tree.csv` as out leaf, **`leaf.txt`** would look like this: -``` -0x12345 -``` **`verify-proof` example:** ```bash -cargo run --release verify-proof root.txt 0 sample_tree_proof_0.json leaf.txt +cargo run --release verify-proof sample_tree_root.txt 0 sample_tree_proof_0.json sample_tree_leaf_0.txt ``` diff --git a/examples/merkle-tree-cli/src/main.rs b/examples/merkle-tree-cli/src/main.rs index 8b62eba00..767d5a5d7 100644 --- a/examples/merkle-tree-cli/src/main.rs +++ b/examples/merkle-tree-cli/src/main.rs @@ -41,6 +41,12 @@ fn generate_merkle_tree(tree_path: String) -> Result<(), io::Error> { let mut writer = BufWriter::new(file); serde_json::to_writer_pretty(&mut writer, &merkle_tree)?; println!("Saved tree to file"); + + let root_file_path = tree_path.replace(".csv", "_root.txt"); + let mut root_file = File::create(root_file_path)?; + root_file.write_all(root.as_bytes())?; + println!("Saved root file"); + Ok(()) } @@ -54,10 +60,26 @@ fn generate_merkle_proof(tree_path: String, pos: usize) -> Result<(), io::Error> }; let proof_path = tree_path.replace(".csv", format!("_proof_{pos}.json").as_str()); - let file = File::create(proof_path)?; + let file = File::create(&proof_path)?; let mut writer = BufWriter::new(file); serde_json::to_writer_pretty(&mut writer, &proof)?; - writer.flush() + writer.flush()?; + + let leaf_value = values + .get(pos) + .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Invalid position"))? + .representative() + .to_string(); + + let leaf_file_path = tree_path.replace(".csv", format!("_leaf_{pos}.txt").as_str()); + let mut leaf_file = File::create(&leaf_file_path)?; + leaf_file.write_all(leaf_value.as_bytes())?; + println!( + "Generated proof and saved to {}. Leaf saved to {}", + proof_path, leaf_file_path + ); + + Ok(()) } fn verify_merkle_proof(