From f31f3077674059b79911aeb798318a7747101655 Mon Sep 17 00:00:00 2001 From: wiesche <120273695+wiesche89@users.noreply.github.com> Date: Fri, 18 Sep 2026 21:29:34 +0200 Subject: [PATCH] unpack plain slatepacks without password (#805) * unpack plain slatepacks without password * cli: better open wallet handling on wallet subcommand --------- Co-authored-by: ardocrat --- controller/src/command.rs | 38 +++++++++--------- src/cmd/wallet_args.rs | 82 ++++++++++++++++++++++++++------------- 2 files changed, 74 insertions(+), 46 deletions(-) diff --git a/controller/src/command.rs b/controller/src/command.rs index b645892..83d0e26 100644 --- a/controller/src/command.rs +++ b/controller/src/command.rs @@ -24,7 +24,7 @@ use crate::impls::SlateGetter as _; use crate::keychain; use crate::libwallet::api_impl::types::update_tx_slate_state; use crate::libwallet::{ - self, InitTxArgs, IssueInvoiceTxArgs, NodeClient, PaymentProof, Slate, SlateState, + self, InitTxArgs, IssueInvoiceTxArgs, NodeClient, PaymentProof, Slate, SlateState, Slatepack, SlatepackAddress, Slatepacker, SlatepackerArgs, WalletLCProvider, }; use crate::util::secp::key::SecretKey; @@ -783,32 +783,34 @@ where Ok(()) } +pub fn read_slatepack(args: ReceiveArgs) -> Result { + let packer = Slatepacker::new(SlatepackerArgs { + sender: None, + recipients: vec![], + dec_key: None, + }); + let slatepack = match args.input_file { + Some(f) => PathToSlatepack::new(f.into(), &packer, true).get_slatepack(false)?, + None => match args.input_slatepack_message { + Some(message) => packer.deser_slatepack(message.as_bytes(), false)?, + None => { + return Err(Error::ArgumentError("Invalid Slatepack Input".into()).into()); + } + }, + }; + Ok(slatepack) +} + pub fn unpack( owner_api: &mut Owner, keychain_mask: Option<&SecretKey>, - args: ReceiveArgs, + mut slatepack: Slatepack, ) -> Result<(), Error> where L: WalletLCProvider<'static, C, K> + 'static, C: NodeClient + 'static, K: keychain::Keychain + 'static, { - let mut slatepack = match args.input_file { - Some(f) => { - let packer = Slatepacker::new(SlatepackerArgs { - sender: None, - recipients: vec![], - dec_key: None, - }); - PathToSlatepack::new(f.into(), &packer, true).get_slatepack(false)? - } - None => match args.input_slatepack_message { - Some(mes) => owner_api.decode_slatepack_message(keychain_mask, mes, vec![])?, - None => { - return Err(Error::ArgumentError("Invalid Slatepack Input".into()).into()); - } - }, - }; println!(); println!("SLATEPACK CONTENTS"); println!("------------------"); diff --git a/src/cmd/wallet_args.rs b/src/cmd/wallet_args.rs index 3446eaf..35cf7b6 100644 --- a/src/cmd/wallet_args.rs +++ b/src/cmd/wallet_args.rs @@ -26,6 +26,7 @@ use grin_core::core::amount_to_hr_string; use grin_keychain as keychain; use grin_wallet_api::Owner; use grin_wallet_config::{GlobalWalletConfig, TorConfig, WalletConfig}; +use grin_wallet_controller::command::GlobalArgs; use grin_wallet_controller::{command, Error}; use grin_wallet_impls::{DefaultLCProvider, DefaultWalletImpl}; use grin_wallet_libwallet::{self, Slate, SlatepackAddress, SlatepackArmor}; @@ -1059,37 +1060,36 @@ where wallet_inst_cb(wallet.clone()); // don't open wallet for certain lifecycle commands - let mut open_wallet = true; - match wallet_args.subcommand() { - ("init", Some(_)) => open_wallet = false, - ("recover", _) => open_wallet = false, - ("cli", _) => open_wallet = false, + let keychain_mask = match wallet_args.subcommand() { + ("init", Some(_)) => None, + ("recover", _) => None, + ("cli", _) => None, + ("unpack", Some(args)) => { + let args = arg_parse!(parse_unpack_args(args)); + let slatepack = command::read_slatepack(args)?; + let mask = if slatepack.mode == 1 { + open_wallet(&wallet, &global_wallet_args, wallet_args)? + } else { + None + }; + let mut owner_api = Owner::new(wallet.clone(), None, config.config_file_path.clone()); + command::unpack(&mut owner_api, mask.as_ref(), slatepack)?; + return Ok("unpack".to_string()); + } ("owner_api", _) => { // If wallet exists and password is present then open it. Otherwise, that's fine too. - let mut wallet_lock = wallet.lock(); - let lc = wallet_lock.lc_provider()?; - open_wallet = wallet_args.is_present("pass") && lc.wallet_exists(None)?; - } - _ => {} - } - - let keychain_mask = match open_wallet { - true => { - let mut wallet_lock = wallet.lock(); - let lc = wallet_lock.lc_provider()?; - let mask = lc.open_wallet( - None, - prompt_password(&global_wallet_args.password)?, - false, - false, - )?; - if let Some(account) = wallet_args.value_of("account") { - let wallet_inst = lc.wallet_inst()?; - wallet_inst.set_parent_key_id_by_name(account)?; + let open = { + let mut wallet_lock = wallet.lock(); + let lc = wallet_lock.lc_provider()?; + wallet_args.is_present("pass") && lc.wallet_exists(None)? + }; + if open { + open_wallet(&wallet, &global_wallet_args, wallet_args)? + } else { + None } - mask } - false => None, + _ => open_wallet(&wallet, &global_wallet_args, wallet_args)?, }; let res = match wallet_args.subcommand() { @@ -1123,6 +1123,31 @@ where } } +fn open_wallet( + wallet: &Arc>>>, + global_wallet_args: &GlobalArgs, + wallet_args: &ArgMatches, +) -> Result, Error> +where + L: WalletLCProvider<'static, C, K> + 'static, + C: NodeClient + 'static, + K: keychain::Keychain + 'static, +{ + let mut wallet_lock = wallet.lock(); + let lc = wallet_lock.lc_provider()?; + let mask = lc.open_wallet( + None, + prompt_password(&global_wallet_args.password)?, + false, + false, + )?; + if let Some(account) = wallet_args.value_of("account") { + let wallet_inst = lc.wallet_inst()?; + wallet_inst.set_parent_key_id_by_name(account)?; + } + Ok(mask) +} + pub fn parse_and_execute( owner_api: &mut Owner, keychain_mask: Option, @@ -1231,7 +1256,8 @@ where } ("unpack", Some(args)) => { let a = arg_parse!(parse_unpack_args(&args)); - command::unpack(owner_api, km, a) + let slatepack = command::read_slatepack(a)?; + command::unpack(owner_api, km, slatepack) } ("finalize", Some(args)) => { let a = arg_parse!(parse_finalize_args(&args));