From e8017fa5c575e8f6739351d35a5b53ca91f761ad Mon Sep 17 00:00:00 2001 From: Daniel Tehrani Date: Sun, 22 Oct 2023 16:59:24 +0800 Subject: [PATCH 1/2] feat: save addresses as hex --- src/bin/save_proofs.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/bin/save_proofs.rs b/src/bin/save_proofs.rs index b08b9b8..5303d30 100644 --- a/src/bin/save_proofs.rs +++ b/src/bin/save_proofs.rs @@ -15,7 +15,7 @@ use std::{ #[derive(Serialize)] pub struct MerkleProofJson { - siblings: Vec<[String; 1]>, + siblings: Vec, pathIndices: Vec, root: String, } @@ -30,18 +30,28 @@ trait ToJson { fn to_json(&self) -> MerkleProofJsonWithAddress; } +fn to_hex(x: F) -> String { + let bytes = x + .into_bigint() + .to_bytes_be() + .iter() + .filter(|x| *x != &0u8) + .cloned() + .collect::>(); + format!("0x{}", hex::encode(bytes)) +} + impl ToJson for MerkleProof { fn to_json(&self) -> MerkleProofJsonWithAddress { - let address_bytes = &self.leaf.into_bigint().to_bytes_be()[12..]; MerkleProofJsonWithAddress { - address: format!("0x{}", hex::encode(address_bytes)), + address: to_hex(self.leaf), merkleProof: MerkleProofJson { - root: self.root.to_string(), + root: to_hex(self.root), siblings: self .siblings .iter() - .map(|sibling| [sibling.to_string()]) - .collect::>(), + .map(|sibling| to_hex(*sibling)) + .collect::>(), pathIndices: self .path_indices .iter() @@ -102,7 +112,7 @@ fn save_tree(leaves: Vec, depth: usize, out_file: &str) -> F { let addresses_json = serde_json::to_string( &leaves .iter() - .map(|leaf| leaf.to_string()) + .map(|leaf| to_hex(*leaf)) .collect::>(), ) .unwrap(); @@ -182,9 +192,9 @@ fn main() { let mut root_to_label_mapping = "{\n".to_string(); for (i, (root, name)) in roots.iter().enumerate() { if i == roots.len() - 1 { - root_to_label_mapping += &format!("\"{}\": \"{}\"\n", root, name); + root_to_label_mapping += &format!("\"{}\": \"{}\"\n", to_hex(*root), name); } else { - root_to_label_mapping += &format!("\"{}\": \"{}\",\n", root, name); + root_to_label_mapping += &format!("\"{}\": \"{}\",\n", to_hex(*root), name); } } root_to_label_mapping += "}"; From 94396544a157bba7b230c3435b4bb385f7dabf67 Mon Sep 17 00:00:00 2001 From: Daniel Tehrani Date: Mon, 23 Oct 2023 22:13:20 +0800 Subject: [PATCH 2/2] fix: address byte to hex conversion --- src/bin/save_proofs.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/bin/save_proofs.rs b/src/bin/save_proofs.rs index 5303d30..db9ef9c 100644 --- a/src/bin/save_proofs.rs +++ b/src/bin/save_proofs.rs @@ -35,16 +35,27 @@ fn to_hex(x: F) -> String { .into_bigint() .to_bytes_be() .iter() - .filter(|x| *x != &0u8) .cloned() .collect::>(); format!("0x{}", hex::encode(bytes)) } +fn to_address_hex(x: F) -> String { + let bytes = x + .into_bigint() + .to_bytes_be() + .iter() + .skip(32 - 20) + .cloned() + .collect::>(); + + format!("0x{}", hex::encode(bytes)) +} + impl ToJson for MerkleProof { fn to_json(&self) -> MerkleProofJsonWithAddress { MerkleProofJsonWithAddress { - address: to_hex(self.leaf), + address: to_address_hex(self.leaf), merkleProof: MerkleProofJson { root: to_hex(self.root), siblings: self @@ -112,7 +123,7 @@ fn save_tree(leaves: Vec, depth: usize, out_file: &str) -> F { let addresses_json = serde_json::to_string( &leaves .iter() - .map(|leaf| to_hex(*leaf)) + .map(|leaf| to_address_hex(*leaf)) .collect::>(), ) .unwrap();