use clap::Parser;
use lib::find::{all_voters, batch_key_check, convert_key_formats, find_vote};
use tracing::{info, Level};
use color_eyre::Result;
use std::{fs::File, io::BufWriter};
use std::{error::Error, path::PathBuf};
#[derive(Parser, Debug, Clone)]
#[clap(about, version, author)]
pub struct Args {
#[clap(short, long)]
pub fragments: Option<String>,
#[clap(short, long, requires = "fragments")]
voting_key: Option<String>,
#[clap(short, long, requires = "fragments")]
aggregate: Option<bool>,
#[clap(short, long)]
key_to_convert: Option<String>,
#[clap(short, long, requires = "fragments")]
key_file: Option<String>,
}
fn main() -> Result<(), Box<dyn Error>> {
color_eyre::install()?;
let args = Args::parse();
let format = tracing_subscriber::fmt::format()
.with_level(true) .with_target(true) .with_thread_ids(true) .with_thread_names(true) .compact(); tracing_subscriber::fmt()
.event_format(format)
.with_max_level(Level::INFO )
.init();
info!("Audit Tool.");
info!("Find my vote");
if let Some(voting_key) = args.voting_key {
let storage_path = PathBuf::from(
args.fragments
.clone()
.expect("enforced by clap: infallible"),
);
info!("finding vote history of voter {:?}", voting_key);
let matched_votes = find_vote(&storage_path, voting_key.clone())?;
let matched_votes_path = PathBuf::from("/tmp/offline")
.with_extension(format!("voting_history_of_{}.json", voting_key));
let file = File::options()
.write(true)
.create(true)
.truncate(true)
.open(matched_votes_path.clone())?;
let writer = BufWriter::new(file);
info!(
"writing voting history of voter {:?} to {:?}",
voting_key, matched_votes_path
);
serde_json::to_writer_pretty(writer, &matched_votes)?;
}
if let Some(_aggregate) = args.aggregate {
let storage_path = PathBuf::from(
args.fragments
.clone()
.expect("enforced by clap: infallible"),
);
info!("collecting all voting keys in ca and 0x format");
let (unique_voters_ca, unique_voters_0x) = all_voters(&storage_path)?;
let voters_file_0x =
PathBuf::from("/tmp/inspect").with_extension("validated_voters_0x.json");
let voters_file_ca =
PathBuf::from("/tmp/inspect").with_extension("validated_voters_ca.json");
let file = File::options()
.write(true)
.create(true)
.truncate(true)
.open(voters_file_ca)
.unwrap();
let writer = BufWriter::new(file);
serde_json::to_writer_pretty(writer, &unique_voters_ca)?;
let file = File::options()
.write(true)
.create(true)
.truncate(true)
.open(voters_file_0x)
.unwrap();
let writer = BufWriter::new(file);
serde_json::to_writer_pretty(writer, &unique_voters_0x)?;
info!("keys written to /tmp/inspect/validated_voters_*.json");
}
if let Some(keyfile) = args.key_file {
let storage_path = PathBuf::from(args.fragments.expect("enforced by clap: infallible"));
batch_key_check(&storage_path, keyfile)?;
}
if let Some(voting_key) = args.key_to_convert {
let converted_key = convert_key_formats(voting_key)?;
info!("Converted key: {}", converted_key);
}
Ok(())
}