From 721baadde468f96cb4ab91fdb1f5cf3ed60a522e Mon Sep 17 00:00:00 2001 From: FroVolod Date: Sat, 9 Nov 2024 20:53:11 +0200 Subject: [PATCH 1/2] start --- .../send_signed_transaction/mod.rs | 100 ++++++++++++++++-- 1 file changed, 91 insertions(+), 9 deletions(-) diff --git a/src/commands/transaction/send_signed_transaction/mod.rs b/src/commands/transaction/send_signed_transaction/mod.rs index a007544f8..c3e01da96 100644 --- a/src/commands/transaction/send_signed_transaction/mod.rs +++ b/src/commands/transaction/send_signed_transaction/mod.rs @@ -1,9 +1,43 @@ +use color_eyre::eyre::WrapErr; +use strum::{EnumDiscriminants, EnumIter, EnumMessage}; + mod network; #[derive(Debug, Clone, interactive_clap::InteractiveClap)] -#[interactive_clap(input_context = crate::GlobalContext)] -#[interactive_clap(output_context = SignedTransactionContext)] +#[interactive_clap(context = crate::GlobalContext)] pub struct SignedTransaction { + #[interactive_clap(subcommand)] + /// Select the base64 signed transaction input method + signed_transaction_type: SignedTransactionType, +} + +#[derive(Debug, EnumDiscriminants, Clone, interactive_clap::InteractiveClap)] +#[interactive_clap(context = crate::GlobalContext)] +#[strum_discriminants(derive(EnumMessage, EnumIter))] +/// Select the Base64 signed transaction input method: +pub enum SignedTransactionType { + #[strum_discriminants(strum( + message = "base64-signed-transaction - Base64-encoded string (e.g. e30=)" + ))] + /// Base64-encoded string (e.g. e30=) + Base64SignedTransaction(Base64SignedTransaction), + #[strum_discriminants(strum( + message = "file - Read from file (e.g. reusable JSON or binary data)" + ))] + /// Read from file (e.g. reusable JSON or binary data) + File(File), +} + +#[derive(Debug, Clone)] +pub struct SignedTransactionContext { + config: crate::config::Config, + signed_transaction: near_primitives::transaction::SignedTransaction, +} + +#[derive(Debug, Clone, interactive_clap::InteractiveClap)] +#[interactive_clap(input_context = crate::GlobalContext)] +#[interactive_clap(output_context = Base64SignedTransactionContext)] +pub struct Base64SignedTransaction { /// Enter a signed transaction as base64-encoded string: signed_action: crate::types::signed_transaction::SignedTransactionAsBase64, #[interactive_clap(named_arg)] @@ -12,19 +46,67 @@ pub struct SignedTransaction { } #[derive(Debug, Clone)] -pub struct SignedTransactionContext { - config: crate::config::Config, +pub struct Base64SignedTransactionContext(SignedTransactionContext); + +impl Base64SignedTransactionContext { + pub fn from_previous_context( + previous_context: crate::GlobalContext, + scope: &::InteractiveClapContextScope, + ) -> color_eyre::eyre::Result { + Ok(Self(SignedTransactionContext { + config: previous_context.config, + signed_transaction: scope.signed_action.inner.clone(), + })) + } +} + +impl From for SignedTransactionContext { + fn from(item: Base64SignedTransactionContext) -> Self { + item.0 + } +} + +#[derive(Debug, Clone, interactive_clap::InteractiveClap)] +#[interactive_clap(input_context = crate::GlobalContext)] +#[interactive_clap(output_context = FileContext)] +pub struct File { + /// Enter the path to the file with the transaction as a string in base64 encoding: + file_path: crate::types::path_buf::PathBuf, + #[interactive_clap(named_arg)] + /// Select network + network_config: self::network::Network, +} + +#[derive(Debug, Clone)] +pub struct FileContext(SignedTransactionContext); + +#[derive(Debug, serde::Deserialize)] +struct FileSignedTransaction { + #[serde(alias = "Signed transaction (serialized as base64)")] signed_transaction: near_primitives::transaction::SignedTransaction, } -impl SignedTransactionContext { +impl FileContext { pub fn from_previous_context( previous_context: crate::GlobalContext, - scope: &::InteractiveClapContextScope, + scope: &::InteractiveClapContextScope, ) -> color_eyre::eyre::Result { - Ok(Self { + let data = std::fs::read_to_string(&scope.file_path) + .wrap_err_with(|| format!("File {:?} not found!", &scope.file_path))?; + + let signed_transaction = serde_json::from_str::(&data) + .wrap_err_with(|| format!("Error reading data from file: {:?}", &scope.file_path))? + .signed_transaction; + + Ok(Self(SignedTransactionContext { config: previous_context.config, - signed_transaction: scope.signed_action.inner.clone(), - }) + signed_transaction, + })) + } +} + +impl From for SignedTransactionContext { + fn from(item: FileContext) -> Self { + item.0 } } From 23064bb2881d2b5f88cab8ce32e364eeb0cfdae7 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Sat, 9 Nov 2024 21:17:11 +0200 Subject: [PATCH 2/2] refactored --- .../send_signed_transaction/mod.rs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/commands/transaction/send_signed_transaction/mod.rs b/src/commands/transaction/send_signed_transaction/mod.rs index c3e01da96..f8cfdc88f 100644 --- a/src/commands/transaction/send_signed_transaction/mod.rs +++ b/src/commands/transaction/send_signed_transaction/mod.rs @@ -17,15 +17,15 @@ pub struct SignedTransaction { /// Select the Base64 signed transaction input method: pub enum SignedTransactionType { #[strum_discriminants(strum( - message = "base64-signed-transaction - Base64-encoded string (e.g. e30=)" + message = "base64-signed-transaction - Base64-encoded string (e.g. e30=)" ))] /// Base64-encoded string (e.g. e30=) Base64SignedTransaction(Base64SignedTransaction), #[strum_discriminants(strum( - message = "file - Read from file (e.g. reusable JSON or binary data)" + message = "file-with-base64-signed-transaction - Read base64-encoded string from file (e.g. reusable JSON or binary data)" ))] - /// Read from file (e.g. reusable JSON or binary data) - File(File), + /// Read base64-encoded string from file (e.g. reusable JSON or binary data) + FileWithBase64SignedTransaction(FileWithBase64SignedTransaction), } #[derive(Debug, Clone)] @@ -68,8 +68,8 @@ impl From for SignedTransactionContext { #[derive(Debug, Clone, interactive_clap::InteractiveClap)] #[interactive_clap(input_context = crate::GlobalContext)] -#[interactive_clap(output_context = FileContext)] -pub struct File { +#[interactive_clap(output_context = FileWithBase64SignedTransactionContext)] +pub struct FileWithBase64SignedTransaction { /// Enter the path to the file with the transaction as a string in base64 encoding: file_path: crate::types::path_buf::PathBuf, #[interactive_clap(named_arg)] @@ -78,7 +78,7 @@ pub struct File { } #[derive(Debug, Clone)] -pub struct FileContext(SignedTransactionContext); +pub struct FileWithBase64SignedTransactionContext(SignedTransactionContext); #[derive(Debug, serde::Deserialize)] struct FileSignedTransaction { @@ -86,10 +86,10 @@ struct FileSignedTransaction { signed_transaction: near_primitives::transaction::SignedTransaction, } -impl FileContext { +impl FileWithBase64SignedTransactionContext { pub fn from_previous_context( previous_context: crate::GlobalContext, - scope: &::InteractiveClapContextScope, + scope: &::InteractiveClapContextScope, ) -> color_eyre::eyre::Result { let data = std::fs::read_to_string(&scope.file_path) .wrap_err_with(|| format!("File {:?} not found!", &scope.file_path))?; @@ -105,8 +105,8 @@ impl FileContext { } } -impl From for SignedTransactionContext { - fn from(item: FileContext) -> Self { +impl From for SignedTransactionContext { + fn from(item: FileWithBase64SignedTransactionContext) -> Self { item.0 } }