1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::Result;
use chain_addr::AddressReadable;
use chain_crypto::bech32::Bech32;
use chain_crypto::Ed25519;
use chain_crypto::PublicKey;
use chain_impl_mockchain::vote::CommitteeId;
use clap::Parser;
use jormungandr_lib::interfaces::CommitteeIdDef;
use std::io::stdout;
use std::io::Write;

#[derive(Parser, Debug)]
pub struct CommitteeIdCommandArgs {
    #[clap(short = 'a', long = "address")]
    pub address: Option<String>,

    #[clap(short = 'p', long = "public_key", conflicts_with = "address")]
    pub public_key: Option<String>,

    #[clap(short = 't', long = "testing")]
    pub testing: bool,
}

impl CommitteeIdCommandArgs {
    pub fn exec(self) -> Result<()> {
        std::env::set_var("RUST_BACKTRACE", "full");

        let committee_id = {
            if let Some(address_str) = self.address {
                let prefix = {
                    if self.testing {
                        "ta"
                    } else {
                        "ca"
                    }
                };
                let address = AddressReadable::from_string(prefix, &address_str)?.to_address();
                CommitteeIdDef::from(CommitteeId::from(address.public_key().unwrap().clone()))
            } else {
                let pkey: PublicKey<Ed25519> =
                    Bech32::try_from_bech32_str(self.public_key.as_ref().unwrap())?;
                CommitteeIdDef::from(CommitteeId::from(pkey))
            }
        };

        writeln!(
            stdout(),
            "{}",
            serde_json::to_string(&committee_id)?
                .as_str()
                .replace("'\"'", "")
        )
        .map_err(Into::into)
    }
}