From c938596bfa9151631a83452fda4e359909aeb780 Mon Sep 17 00:00:00 2001 From: chris-belcher Date: Fri, 31 Dec 2021 19:41:59 +0000 Subject: [PATCH] Remove unnecessary searches of wrong swapcoin list Previously the maker_protocol.rs functions handle_sign_receivers_contract_tx() and handle_private_key_handover() would search both the incoming_swapcoins list and the outgoing_swapcoins list, which was unnecessary and wrong. It was leftover from old code when both kinds of swapcoins were in the same list. This change leaves some functions in the IncomingSwapCoin or OutgoingSwapCoin structs unused. Delete them. --- src/contracts.rs | 38 ++++++++++++++++++++------------------ src/maker_protocol.rs | 33 +++++++++++---------------------- src/wallet_sync.rs | 7 ------- 3 files changed, 31 insertions(+), 47 deletions(-) diff --git a/src/contracts.rs b/src/contracts.rs index 517636fc..b94e84b6 100644 --- a/src/contracts.rs +++ b/src/contracts.rs @@ -572,24 +572,9 @@ impl SwapCoin for OutgoingSwapCoin { } } -macro_rules! sign_and_verify_contract { +macro_rules! verify_contract { ($coin:ident) => { impl $coin { - //"_with_my_privkey" as opposed to with other_privkey - pub fn sign_contract_tx_with_my_privkey( - &self, - contract_tx: &Transaction, - ) -> Result { - let multisig_redeemscript = self.get_multisig_redeemscript(); - Ok(sign_contract_tx( - contract_tx, - &multisig_redeemscript, - self.funding_amount, - &self.my_privkey, - ) - .map_err(|_| Error::Protocol("error with signing contract tx"))?) - } - pub fn verify_contract_tx_sig(&self, sig: &Signature) -> bool { verify_contract_tx_sig( &self.contract_tx, @@ -603,8 +588,25 @@ macro_rules! sign_and_verify_contract { }; } -sign_and_verify_contract!(IncomingSwapCoin); -sign_and_verify_contract!(OutgoingSwapCoin); +verify_contract!(IncomingSwapCoin); +verify_contract!(OutgoingSwapCoin); + +impl OutgoingSwapCoin { + //"_with_my_privkey" as opposed to with other_privkey + pub fn sign_contract_tx_with_my_privkey( + &self, + contract_tx: &Transaction, + ) -> Result { + let multisig_redeemscript = self.get_multisig_redeemscript(); + Ok(sign_contract_tx( + contract_tx, + &multisig_redeemscript, + self.funding_amount, + &self.my_privkey, + ) + .map_err(|_| Error::Protocol("error with signing contract tx"))?) + } +} impl SwapCoin for WatchOnlySwapCoin { fn get_multisig_redeemscript(&self) -> Script { diff --git a/src/maker_protocol.rs b/src/maker_protocol.rs index 357d043b..18fb4954 100644 --- a/src/maker_protocol.rs +++ b/src/maker_protocol.rs @@ -724,20 +724,15 @@ fn handle_sign_receivers_contract_tx( let mut sigs = Vec::::new(); for receivers_contract_tx_info in message.txes { sigs.push( - if let Some(c) = wallet + //the fact that the peer knows the correct multisig_redeemscript is what ensures + //security here, a random peer out there who isnt involved in a coinswap wont know + //what the multisig_redeemscript is + wallet .read() .unwrap() - .find_incoming_swapcoin(&receivers_contract_tx_info.multisig_redeemscript) - { - c.sign_contract_tx_with_my_privkey(&receivers_contract_tx_info.contract_tx)? - } else { - wallet - .read() - .unwrap() - .find_outgoing_swapcoin(&receivers_contract_tx_info.multisig_redeemscript) - .ok_or(Error::Protocol("multisig_redeemscript not found"))? - .sign_contract_tx_with_my_privkey(&receivers_contract_tx_info.contract_tx)? - }, + .find_outgoing_swapcoin(&receivers_contract_tx_info.multisig_redeemscript) + .ok_or(Error::Protocol("multisig_redeemscript not found"))? + .sign_contract_tx_with_my_privkey(&receivers_contract_tx_info.contract_tx)?, ); } Ok(Some(MakerToTakerMessage::ReceiversContractSig( @@ -799,16 +794,10 @@ fn handle_private_key_handover( ) -> Result, Error> { let mut wallet_ref = wallet.write().unwrap(); for swapcoin_private_key in message.swapcoin_private_keys { - if let Some(c) = - wallet_ref.find_incoming_swapcoin_mut(&swapcoin_private_key.multisig_redeemscript) - { - c.apply_privkey(swapcoin_private_key.key)? - } else { - wallet_ref - .find_outgoing_swapcoin_mut(&swapcoin_private_key.multisig_redeemscript) - .ok_or(Error::Protocol("multisig_redeemscript not found"))? - .apply_privkey(swapcoin_private_key.key)? - } + wallet_ref + .find_incoming_swapcoin_mut(&swapcoin_private_key.multisig_redeemscript) + .ok_or(Error::Protocol("multisig_redeemscript not found"))? + .apply_privkey(swapcoin_private_key.key)? } wallet_ref.update_swap_coins_list()?; log::info!("Successfully Completed Coinswap"); diff --git a/src/wallet_sync.rs b/src/wallet_sync.rs index 960f66ea..b00721cd 100644 --- a/src/wallet_sync.rs +++ b/src/wallet_sync.rs @@ -438,13 +438,6 @@ impl Wallet { self.incoming_swap_coins.get_mut(multisig_redeemscript) } - pub fn find_outgoing_swapcoin_mut( - &mut self, - multisig_redeemscript: &Script, - ) -> Option<&mut OutgoingSwapCoin> { - self.outgoing_swap_coins.get_mut(multisig_redeemscript) - } - pub fn add_incoming_swapcoin(&mut self, coin: IncomingSwapCoin) { self.incoming_swap_coins .insert(coin.get_multisig_redeemscript(), coin);