From 9014cb9a13e795a7ab83331bd806ec6af07c5b87 Mon Sep 17 00:00:00 2001 From: rishflab Date: Mon, 21 Dec 2020 11:17:54 +1100 Subject: [PATCH] Fix tx_cancel building and signing In an earlier commit we moved the signing and building of tx_cancel into a reusable function. This was a mistake as the signing logic is different for Alice and Bob. --- xmr-btc/src/alice.rs | 22 +++++++++++++--------- xmr-btc/src/bitcoin.rs | 34 ---------------------------------- xmr-btc/src/bob.rs | 27 ++++++++++++++++++--------- 3 files changed, 31 insertions(+), 52 deletions(-) diff --git a/xmr-btc/src/alice.rs b/xmr-btc/src/alice.rs index 3ac3cc24..3bf77cfb 100644 --- a/xmr-btc/src/alice.rs +++ b/xmr-btc/src/alice.rs @@ -720,15 +720,19 @@ impl State3 { where W: BroadcastSignedTransaction, { - crate::bitcoin::publish_cancel_transaction( - self.tx_lock.clone(), - self.a.clone(), - self.B, - self.refund_timelock, - self.tx_punish_sig_bob.clone(), - bitcoin_wallet, - ) - .await + let tx_cancel = self.tx_cancel(); + + let sig_a = self.a.sign(tx_cancel.digest()); + let sig_b = self.tx_cancel_sig_bob.clone(); + + let signed_tx_cancel = tx_cancel + .clone() + .add_signatures(&self.tx_lock, (self.a.public(), sig_a), (self.B, sig_b)) + .expect("sig_{a,b} to be valid signatures for tx_cancel"); + + bitcoin_wallet + .broadcast_signed_transaction(signed_tx_cancel) + .await } } diff --git a/xmr-btc/src/bitcoin.rs b/xmr-btc/src/bitcoin.rs index 1c959c6a..562b262f 100644 --- a/xmr-btc/src/bitcoin.rs +++ b/xmr-btc/src/bitcoin.rs @@ -243,37 +243,3 @@ where tokio::time::delay_for(std::time::Duration::from_secs(1)).await; } } - -pub async fn publish_cancel_transaction( - tx_lock: TxLock, - secret_key: crate::bitcoin::SecretKey, - public_key: crate::bitcoin::PublicKey, - refund_timelock: u32, - tx_cancel_sig: crate::bitcoin::Signature, - bitcoin_wallet: &W, -) -> Result -where - W: BroadcastSignedTransaction, -{ - let tx_cancel = - crate::bitcoin::TxCancel::new(&tx_lock, refund_timelock, public_key, secret_key.public()); - - let sig_theirs = tx_cancel_sig.clone(); - let sig_ours = secret_key.sign(tx_cancel.digest()); - - let tx_cancel_txn = tx_cancel - .clone() - .add_signatures( - &tx_lock, - (public_key, sig_theirs), - (secret_key.public(), sig_ours), - ) - .expect( - "sig_{a,b} to be valid signatures for - tx_cancel", - ); - - bitcoin_wallet - .broadcast_signed_transaction(tx_cancel_txn) - .await -} diff --git a/xmr-btc/src/bob.rs b/xmr-btc/src/bob.rs index f57576fc..317e9f6c 100644 --- a/xmr-btc/src/bob.rs +++ b/xmr-btc/src/bob.rs @@ -660,19 +660,28 @@ impl State4 { self.b.encsign(self.S_a_bitcoin, tx_redeem.digest()) } + pub fn tx_cancel(&self) -> TxCancel { + crate::bitcoin::TxCancel::new(&self.tx_lock, self.refund_timelock, self.A, self.b.public()) + } + pub async fn submit_tx_cancel(&self, bitcoin_wallet: &W) -> Result where W: BroadcastSignedTransaction, { - crate::bitcoin::publish_cancel_transaction( - self.tx_lock.clone(), - self.b.clone(), - self.A, - self.refund_timelock, - self.tx_cancel_sig_a.clone(), - bitcoin_wallet, - ) - .await + let tx_cancel = self.tx_cancel(); + + let sig_b = self.b.sign(tx_cancel.digest()); + let sig_a = self.tx_cancel_sig_a.clone(); + + let signed_tx_cancel = tx_cancel.clone().add_signatures( + &self.tx_lock, + (self.A, sig_a), + (self.b.public(), sig_b), + )?; + + bitcoin_wallet + .broadcast_signed_transaction(signed_tx_cancel) + .await } pub async fn watch_for_redeem_btc(&self, bitcoin_wallet: &W) -> Result