Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions src/bin/save_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{

#[derive(Serialize)]
pub struct MerkleProofJson {
siblings: Vec<[String; 1]>,
siblings: Vec<String>,
pathIndices: Vec<String>,
root: String,
}
Expand All @@ -30,18 +30,39 @@ trait ToJson {
fn to_json(&self) -> MerkleProofJsonWithAddress;
}

fn to_hex<F: PrimeField>(x: F) -> String {
let bytes = x
.into_bigint()
.to_bytes_be()
.iter()
.cloned()
.collect::<Vec<u8>>();
format!("0x{}", hex::encode(bytes))
}

fn to_address_hex<F: PrimeField>(x: F) -> String {
let bytes = x
.into_bigint()
.to_bytes_be()
.iter()
.skip(32 - 20)
.cloned()
.collect::<Vec<u8>>();

format!("0x{}", hex::encode(bytes))
}

impl<F: PrimeField> ToJson for MerkleProof<F> {
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_address_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::<Vec<[String; 1]>>(),
.map(|sibling| to_hex(*sibling))
.collect::<Vec<String>>(),
pathIndices: self
.path_indices
.iter()
Expand Down Expand Up @@ -102,7 +123,7 @@ fn save_tree<F: PrimeField>(leaves: Vec<F>, depth: usize, out_file: &str) -> F {
let addresses_json = serde_json::to_string(
&leaves
.iter()
.map(|leaf| leaf.to_string())
.map(|leaf| to_address_hex(*leaf))
.collect::<Vec<String>>(),
)
.unwrap();
Expand Down Expand Up @@ -182,9 +203,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 += "}";
Expand Down