utility_cli_rs/commands/extensions/register_rsa_keys/
mod.rs

1use color_eyre::eyre::Context;
2
3use super::Miner;
4
5pub mod constructor_mode;
6
7#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
8#[interactive_clap(input_context = crate::GlobalContext)]
9#[interactive_clap(output_context = RegisterRsaKeysContext)]
10pub struct RegisterRsaKeysCommand {
11    #[interactive_clap(skip_default_input_arg)]
12    /// What is the CA(root/treasury) account ID?
13    account_id: crate::types::account_id::AccountId,
14    #[interactive_clap(named_arg)]
15    /// Specify a path to pem file
16    use_file: PemFile,
17}
18
19#[derive(Debug, Clone)]
20pub struct RegisterRsaKeysContext {
21    global_context: crate::GlobalContext,
22    receiver_account_id: unc_primitives::types::AccountId,
23    signer_account_id: unc_primitives::types::AccountId,
24}
25
26impl RegisterRsaKeysContext {
27    pub fn from_previous_context(
28        previous_context: crate::GlobalContext,
29        scope: &<RegisterRsaKeysCommand as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
30    ) -> color_eyre::eyre::Result<Self> {
31        Ok(Self {
32            global_context: previous_context,
33            receiver_account_id: scope.account_id.clone().into(),
34            signer_account_id: scope.account_id.clone().into(),
35        })
36    }
37}
38
39impl RegisterRsaKeysCommand {
40    pub fn input_account_id(
41        context: &crate::GlobalContext,
42    ) -> color_eyre::eyre::Result<Option<crate::types::account_id::AccountId>> {
43        crate::common::input_signer_account_id_from_used_account_list(
44            &context.config.credentials_home_dir,
45            "What is the CA(root/treasury) account ID?",
46        )
47    }
48}
49
50#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
51#[interactive_clap(input_context = RegisterRsaKeysContext)]
52#[interactive_clap(output_context = RsaFileContext)]
53pub struct PemFile {
54    /// What is a file location of the pem?
55    pub file_path: crate::types::path_buf::PathBuf,
56    #[interactive_clap(subcommand)]
57    constructor: self::constructor_mode::ConstructorMode,
58}
59
60#[derive(Debug, Clone)]
61pub struct RsaFileContext {
62    pub global_context: crate::GlobalContext,
63    pub receiver_account_id: unc_primitives::types::AccountId,
64    pub signer_account_id: unc_primitives::types::AccountId,
65    pub miners: Vec<Miner>,
66}
67
68impl RsaFileContext {
69    pub fn from_previous_context(
70        previous_context: RegisterRsaKeysContext,
71        scope: &<PemFile as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
72    ) -> color_eyre::eyre::Result<Self> {
73        let data = std::fs::read_to_string(&scope.file_path).wrap_err_with(|| {
74            format!("Failed to open or read the file: {:?}.", &scope.file_path.0,)
75        })?;
76        let miner_json: Vec<super::Miner> = serde_json::from_str(&data).wrap_err_with(|| {
77            format!(
78                "Error json codec reading data from file: {:?}",
79                &scope.file_path.0
80            )
81        })?;
82
83        Ok(Self {
84            global_context: previous_context.global_context,
85            receiver_account_id: previous_context.receiver_account_id,
86            signer_account_id: previous_context.signer_account_id,
87            miners: miner_json,
88        })
89    }
90}