diff --git a/src/wallet_direct_send.rs b/src/wallet_direct_send.rs index 0cc5c480..5947d6a8 100644 --- a/src/wallet_direct_send.rs +++ b/src/wallet_direct_send.rs @@ -7,7 +7,7 @@ use bitcoincore_rpc::json::ListUnspentResultEntry; use bitcoincore_rpc::Client; use crate::error::Error; -use crate::wallet_sync::{UTXOSpendInfo, Wallet, NETWORK, SignTransactionInputInfo}; +use crate::wallet_sync::{UTXOSpendInfo, Wallet, NETWORK}; #[derive(Debug)] pub enum SendAmount { @@ -149,7 +149,10 @@ impl Wallet { } } if tx_inputs.len() != coins_to_spend.len() { - panic!("unable to find all given inputs, only found = {:?}", tx_inputs); + panic!( + "unable to find all given inputs, only found = {:?}", + tx_inputs + ); } let dest_addr = match destination { Destination::Wallet => self.get_next_external_address(rpc)?, @@ -185,29 +188,10 @@ impl Wallet { lock_time: 0, version: 2, }; - self.sign_transaction(&mut tx, &mut unspent_inputs.iter() - .map(|(lure, usi)| - match usi { - UTXOSpendInfo::SeedUTXO { path } => { - SignTransactionInputInfo::SeedCoin { - path: path.clone(), - input_value: lure.amount.as_sat() - } - } - UTXOSpendInfo::SwapCoin { multisig_redeemscript } => { - SignTransactionInputInfo::SwapCoin { - multisig_redeemscript: multisig_redeemscript.clone() - } - } - UTXOSpendInfo::TimelockContract { swapcoin_multisig_redeemscript } => { - panic!("not implemented yet"); - } - } - ) + self.sign_transaction( + &mut tx, + &mut unspent_inputs.iter().map(|(_u, usi)| usi.clone()), ); - - println!("tx = {:?}", tx); - Ok(tx) } } diff --git a/src/wallet_sync.rs b/src/wallet_sync.rs index 94a307e4..45af8bd6 100644 --- a/src/wallet_sync.rs +++ b/src/wallet_sync.rs @@ -99,7 +99,10 @@ const WATCH_ONLY_SWAPCOIN_LABEL: &str = "watchonly_swapcoin_label"; //about a UTXO required to spend it #[derive(Debug, Clone)] pub enum UTXOSpendInfo { - SeedUTXO { path: String }, + SeedCoin { + path: String, + input_value: u64, + }, SwapCoin { multisig_redeemscript: Script, }, @@ -108,11 +111,6 @@ pub enum UTXOSpendInfo { }, } -pub enum SignTransactionInputInfo { - SeedCoin { path: String, input_value: u64 }, - SwapCoin { multisig_redeemscript: Script }, -} - //swapcoins are UTXOs + metadata which are not from the deterministic wallet //they are made in the process of a coinswap #[derive(serde::Serialize, serde::Deserialize, Debug, Clone)] @@ -768,8 +766,9 @@ impl Wallet { .derive_priv(&secp, &DerivationPath::from_str(DERIVATION_PATH).unwrap()) .unwrap(); if fingerprint == master_private_key.fingerprint(&secp).to_string() { - Some(UTXOSpendInfo::SeedUTXO { - path: format!("m/{}/{}", addr_type, index) + Some(UTXOSpendInfo::SeedCoin { + path: format!("m/{}/{}", addr_type, index), + input_value: u.amount.as_sat(), }) } else { None @@ -1050,7 +1049,7 @@ impl Wallet { pub fn sign_transaction( &self, spending_tx: &mut Transaction, - inputs_info: &mut dyn Iterator, + inputs_info: &mut dyn Iterator, ) { let secp = Secp256k1::new(); let master_private_key = self @@ -1063,7 +1062,7 @@ impl Wallet { spending_tx.input.iter_mut().zip(inputs_info).enumerate() { match input_info { - SignTransactionInputInfo::SwapCoin { + UTXOSpendInfo::SwapCoin { multisig_redeemscript, } => { self.find_incoming_swapcoin(&multisig_redeemscript) @@ -1071,7 +1070,7 @@ impl Wallet { .sign_transaction_input(ix, &tx_clone, &mut input, &multisig_redeemscript) .unwrap(); } - SignTransactionInputInfo::SeedCoin { path, input_value } => { + UTXOSpendInfo::SeedCoin { path, input_value } => { let privkey = master_private_key .derive_priv(&secp, &DerivationPath::from_str(&path).unwrap()) .unwrap() @@ -1094,6 +1093,11 @@ impl Wallet { input.witness[0].push(SigHashType::All as u8); input.witness.push(pubkey.to_bytes()); } + UTXOSpendInfo::TimelockContract { + swapcoin_multisig_redeemscript: _, + } => { + panic!("not implemented yet"); + } } } } @@ -1245,7 +1249,7 @@ impl Wallet { .map(|input_info| (input_info, input_info["bip32_derivs"].as_array().unwrap())) .map(|(input_info, bip32_info)| { if bip32_info.len() == 2 { - SignTransactionInputInfo::SwapCoin { + UTXOSpendInfo::SwapCoin { multisig_redeemscript: Builder::from( Vec::from_hex( &input_info["witness_script"]["hex"].as_str().unwrap(), @@ -1255,7 +1259,7 @@ impl Wallet { .into_script(), } } else { - SignTransactionInputInfo::SeedCoin { + UTXOSpendInfo::SeedCoin { path: bip32_info[0]["path"].as_str().unwrap().to_string(), input_value: convert_json_rpc_bitcoin_to_satoshis( &input_info["witness_utxo"]["amount"], @@ -1530,4 +1534,3 @@ fn get_hd_path_from_descriptor<'a>(descriptor: &'a str) -> Option<(&'a str, u32, } Some((path_chunks[0], addr_type.unwrap(), index.unwrap())) } -