use std::env::args;

use log::info;

use avalanche_types::key::hot;

/// cargo run --example key_hot_info_validate -- ./artifacts/ewoq.key.json 9999
fn main() {
    // ref. https://github.com/env-logger-rs/env_logger/issues/47
    env_logger::init_from_env(
        env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
    );

    let network_id = args().nth(2).expect("no network ID given");
    let network_id = network_id.parse::<u32>().unwrap();

    info!("loading key");
    let key_path = args().nth(1).expect("no key path given");
    let entry = hot::PrivateKeyInfoEntry::load(&key_path).unwrap();
    println!("{}", entry);

    let k = hot::Key::from_private_key(&entry.private_key).unwrap();
    assert_eq!(entry.private_key, k.private_key);
    assert_eq!(
        entry
            .addresses
            .get(&format!("{}", network_id))
            .unwrap()
            .x_address,
        k.address("X", network_id).unwrap()
    );
    assert_eq!(
        entry
            .addresses
            .get(&format!("{}", network_id))
            .unwrap()
            .p_address,
        k.address("P", network_id).unwrap()
    );
    assert_eq!(
        entry
            .addresses
            .get(&format!("{}", network_id))
            .unwrap()
            .c_address,
        k.address("C", network_id).unwrap()
    );
    assert_eq!(entry.short_address, k.short_address);
    assert_eq!(entry.eth_address, k.eth_address);

    info!("SUCCESS");
}
