From 0a33340f51384655fbdc51e6863262c8dc043f60 Mon Sep 17 00:00:00 2001 From: Gianmarco Fraccaroli Date: Fri, 30 Aug 2024 16:17:43 +0200 Subject: [PATCH] improve wallet support --- crates/apps_lib/src/cli.rs | 47 +++++++++++++++----- crates/apps_lib/src/cli/client.rs | 5 ++- crates/apps_lib/src/client/utils.rs | 1 - crates/tests/src/integration/ledger_tests.rs | 2 +- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/crates/apps_lib/src/cli.rs b/crates/apps_lib/src/cli.rs index 44fa726765b..e032a889b90 100644 --- a/crates/apps_lib/src/cli.rs +++ b/crates/apps_lib/src/cli.rs @@ -33,6 +33,7 @@ const WALLET_CMD: &str = "wallet"; const RELAYER_CMD: &str = "relayer"; pub mod cmds { + use super::args::CliTypes; use super::utils::*; use super::{ args, ArgMatches, CLIENT_CMD, NODE_CMD, RELAYER_CMD, WALLET_CMD, @@ -3195,21 +3196,21 @@ pub mod cmds { } #[derive(Clone, Debug)] - pub struct SignOffline(pub args::SignOffline); + pub struct SignOffline(pub args::SignOffline); impl SubCmd for SignOffline { const CMD: &'static str = "sign-offline"; fn parse(matches: &ArgMatches) -> Option { - matches - .subcommand_matches(Self::CMD) - .map(|matches| Self(args::SignOffline::parse(matches))) + matches.subcommand_matches(Self::CMD).map(|matches| { + Self(args::SignOffline::::parse(matches)) + }) } fn def() -> App { App::new(Self::CMD) .about(wrap!("Offlne sign a transaction.")) - .add_args::() + .add_args::>() } } @@ -3468,7 +3469,7 @@ pub mod args { DefaultFn(|| PortId::from_str("transfer").unwrap()), ); pub const PRE_GENESIS: ArgFlag = flag("pre-genesis"); - pub const PRIVATE_KEYS: ArgMulti = + pub const PRIVATE_KEYS: ArgMulti = arg_multi("secret-keys"); pub const PROPOSAL_PGF_STEWARD: ArgFlag = flag("pgf-stewards"); pub const PROPOSAL_PGF_FUNDING: ArgFlag = flag("pgf-funding"); @@ -8063,18 +8064,18 @@ pub mod args { } #[derive(Clone, Debug)] - pub struct SignOffline { + pub struct SignOffline { pub tx_path: PathBuf, - pub secret_keys: Vec, - pub owner: Address, + pub secret_keys: Vec, + pub owner: C::Address, pub output_folder_path: Option, } - impl Args for SignOffline { + impl Args for SignOffline { fn parse(matches: &ArgMatches) -> Self { let tx_path = DATA_PATH.parse(matches); let secret_keys = PRIVATE_KEYS.parse(matches); - let owner = RAW_ADDRESS.parse(matches); + let owner = OWNER.parse(matches); let output_folder_path = OUTPUT_FOLDER_PATH.parse(matches); Self { @@ -8095,7 +8096,7 @@ pub mod args { "The set of private keys to use to sign the transaction. The \ order matters." ))) - .arg(RAW_ADDRESS.def().help(wrap!("The owner's address."))) + .arg(OWNER.def().help(wrap!("The owner's address."))) .arg( OUTPUT_FOLDER_PATH .def() @@ -8104,6 +8105,28 @@ pub mod args { } } + impl CliToSdk> for SignOffline { + type Error = std::io::Error; + + fn to_sdk( + self, + ctx: &mut Context, + ) -> Result, Self::Error> { + let chain_ctx = ctx.borrow_mut_chain_or_exit(); + + Ok(SignOffline:: { + tx_path: self.tx_path, + secret_keys: self + .secret_keys + .iter() + .map(|key| chain_ctx.get_cached(key)) + .collect(), + owner: chain_ctx.get(&self.owner), + output_folder_path: self.output_folder_path, + }) + } + } + #[derive(Clone, Debug)] pub struct DefaultBaseDir {} diff --git a/crates/apps_lib/src/cli/client.rs b/crates/apps_lib/src/cli/client.rs index c1c23e2078f..9e23e1ec245 100644 --- a/crates/apps_lib/src/cli/client.rs +++ b/crates/apps_lib/src/cli/client.rs @@ -816,7 +816,10 @@ impl CliApi { utils::pk_to_tm_address(global_args, args) } ClientUtils::SignOffline(SignOffline(args)) => { - utils::sign_offline(global_args, args).await + let mut ctx = cli::Context::new::(global_args) + .expect("expected to construct a context"); + let args = args.to_sdk(&mut ctx)?; + utils::sign_offline(args).await } ClientUtils::DefaultBaseDir(DefaultBaseDir(args)) => { utils::default_base_dir(global_args, args) diff --git a/crates/apps_lib/src/client/utils.rs b/crates/apps_lib/src/client/utils.rs index 95f54c4ce78..6a227ea3625 100644 --- a/crates/apps_lib/src/client/utils.rs +++ b/crates/apps_lib/src/client/utils.rs @@ -1035,7 +1035,6 @@ pub async fn sign_genesis_tx( /// Offline sign a transactions. pub async fn sign_offline( - _global_args: args::Global, args::SignOffline { tx_path, secret_keys, diff --git a/crates/tests/src/integration/ledger_tests.rs b/crates/tests/src/integration/ledger_tests.rs index 5e8c5d78806..c245ebc680d 100644 --- a/crates/tests/src/integration/ledger_tests.rs +++ b/crates/tests/src/integration/ledger_tests.rs @@ -1632,7 +1632,7 @@ fn offline_sign() -> Result<()> { "sign-offline", "--data-path", &offline_tx, - "--address", + "--owner", &bertha_address, "--secret-keys", &bertha_sk,