Write proof to file

This commit is contained in:
hu55a1n1 2023-12-22 05:54:14 -08:00
parent 215ec5f0a7
commit eaa45ca32f

View file

@ -15,6 +15,9 @@
)] )]
use std::error::Error; use std::error::Error;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::PathBuf;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use cosmrs::AccountId; use cosmrs::AccountId;
@ -56,6 +59,10 @@ enum Command {
/// (only makes sense when dealing with maps) /// (only makes sense when dealing with maps)
#[clap(long)] #[clap(long)]
storage_namespace: Option<String>, storage_namespace: Option<String>,
/// Output file to store merkle proof
#[clap(long)]
proof_file: Option<PathBuf>,
}, },
} }
@ -72,6 +79,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
contract_address, contract_address,
storage_key, storage_key,
storage_namespace, storage_namespace,
proof_file,
} => { } => {
let client = TmRpcClient::builder(rpc_url).build()?; let client = TmRpcClient::builder(rpc_url).build()?;
let status = client.status().await?; let status = client.status().await?;
@ -83,8 +91,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
.abci_query(Some(path), data, Some(proof_height), true) .abci_query(Some(path), data, Some(proof_height), true)
.await?; .await?;
let value = verify_proof(latest_app_hash, result)?; let value = verify_proof(latest_app_hash, result.clone())?;
println!("{}", String::from_utf8(value)?); println!("{}", String::from_utf8(value)?);
if let Some(proof_file) = proof_file {
write_proof_to_file(proof_file, result)?;
}
} }
}; };
@ -144,3 +156,11 @@ fn verify_proof(latest_app_hash: AppHash, result: AbciQuery) -> Result<Vec<u8>,
Ok(result.value) Ok(result.value)
} }
fn write_proof_to_file(proof_file: PathBuf, output: AbciQuery) -> Result<(), Box<dyn Error>> {
let file = File::create(proof_file)?;
let mut writer = BufWriter::new(file);
serde_json::to_writer(&mut writer, &output)?;
writer.flush()?;
Ok(())
}