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