From 6c38d668643a8e1000947411ae05a06ef0234ccd Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 25 Feb 2021 13:52:05 +1100 Subject: [PATCH] Remove `Tx` arguments from `add_signatures` functions The only reason we need this argument is because we need to access the output descriptor. We can save that one ahead of time at when we construct the type. --- swap/src/bitcoin/cancel.rs | 6 +++--- swap/src/bitcoin/punish.rs | 8 ++++---- swap/src/bitcoin/redeem.rs | 8 ++++---- swap/src/bitcoin/refund.rs | 8 ++++---- swap/src/database/alice.rs | 2 +- swap/src/protocol/alice/state.rs | 2 +- swap/src/protocol/alice/steps.rs | 8 ++++---- swap/src/protocol/alice/swap.rs | 6 +++--- swap/src/protocol/bob/state.rs | 11 ++++------- 9 files changed, 28 insertions(+), 31 deletions(-) diff --git a/swap/src/bitcoin/cancel.rs b/swap/src/bitcoin/cancel.rs index 1fd31777..d2187d45 100644 --- a/swap/src/bitcoin/cancel.rs +++ b/swap/src/bitcoin/cancel.rs @@ -58,6 +58,7 @@ pub struct TxCancel { inner: Transaction, digest: SigHash, pub(in crate::bitcoin) output_descriptor: Descriptor<::bitcoin::PublicKey>, + lock_output_descriptor: Descriptor<::bitcoin::PublicKey>, } impl TxCancel { @@ -99,6 +100,7 @@ impl TxCancel { inner: transaction, digest, output_descriptor: cancel_output_descriptor, + lock_output_descriptor: tx_lock.output_descriptor.clone(), } } @@ -120,7 +122,6 @@ impl TxCancel { pub fn add_signatures( self, - tx_lock: &TxLock, (A, sig_a): (PublicKey, Signature), (B, sig_b): (PublicKey, Signature), ) -> Result { @@ -144,8 +145,7 @@ impl TxCancel { }; let mut tx_cancel = self.inner; - tx_lock - .output_descriptor + self.lock_output_descriptor .satisfy(&mut tx_cancel.input[0], satisfier)?; Ok(tx_cancel) diff --git a/swap/src/bitcoin/punish.rs b/swap/src/bitcoin/punish.rs index c30e3448..08c0f0b9 100644 --- a/swap/src/bitcoin/punish.rs +++ b/swap/src/bitcoin/punish.rs @@ -2,13 +2,14 @@ use crate::bitcoin::{Address, PublicKey, PunishTimelock, Transaction, TxCancel}; use ::bitcoin::{util::bip143::SigHashCache, SigHash, SigHashType}; use anyhow::Result; use ecdsa_fun::Signature; -use miniscript::DescriptorTrait; +use miniscript::{Descriptor, DescriptorTrait}; use std::collections::HashMap; #[derive(Debug)] pub struct TxPunish { inner: Transaction, digest: SigHash, + cancel_output_descriptor: Descriptor<::bitcoin::PublicKey>, } impl TxPunish { @@ -29,6 +30,7 @@ impl TxPunish { Self { inner: tx_punish, digest, + cancel_output_descriptor: tx_cancel.output_descriptor.clone(), } } @@ -38,7 +40,6 @@ impl TxPunish { pub fn add_signatures( self, - tx_cancel: &TxCancel, (A, sig_a): (PublicKey, Signature), (B, sig_b): (PublicKey, Signature), ) -> Result { @@ -62,8 +63,7 @@ impl TxPunish { }; let mut tx_punish = self.inner; - tx_cancel - .output_descriptor + self.cancel_output_descriptor .satisfy(&mut tx_punish.input[0], satisfier)?; Ok(tx_punish) diff --git a/swap/src/bitcoin/redeem.rs b/swap/src/bitcoin/redeem.rs index c9ed27ad..081b0094 100644 --- a/swap/src/bitcoin/redeem.rs +++ b/swap/src/bitcoin/redeem.rs @@ -5,13 +5,14 @@ use crate::bitcoin::{ use ::bitcoin::{util::bip143::SigHashCache, SigHash, SigHashType, Txid}; use anyhow::{bail, Context, Result}; use ecdsa_fun::Signature; -use miniscript::DescriptorTrait; +use miniscript::{Descriptor, DescriptorTrait}; use std::collections::HashMap; #[derive(Debug, Clone)] pub struct TxRedeem { inner: Transaction, digest: SigHash, + lock_output_descriptor: Descriptor<::bitcoin::PublicKey>, } impl TxRedeem { @@ -30,6 +31,7 @@ impl TxRedeem { Self { inner: tx_redeem, digest, + lock_output_descriptor: tx_lock.output_descriptor.clone(), } } @@ -43,7 +45,6 @@ impl TxRedeem { pub fn add_signatures( self, - tx_lock: &TxLock, (A, sig_a): (PublicKey, Signature), (B, sig_b): (PublicKey, Signature), ) -> Result { @@ -67,8 +68,7 @@ impl TxRedeem { }; let mut tx_redeem = self.inner; - tx_lock - .output_descriptor + self.lock_output_descriptor .satisfy(&mut tx_redeem.input[0], satisfier)?; Ok(tx_redeem) diff --git a/swap/src/bitcoin/refund.rs b/swap/src/bitcoin/refund.rs index 18c6af12..e5124e7f 100644 --- a/swap/src/bitcoin/refund.rs +++ b/swap/src/bitcoin/refund.rs @@ -5,13 +5,14 @@ use crate::bitcoin::{ use ::bitcoin::{util::bip143::SigHashCache, SigHash, SigHashType, Txid}; use anyhow::{bail, Context, Result}; use ecdsa_fun::Signature; -use miniscript::DescriptorTrait; +use miniscript::{Descriptor, DescriptorTrait}; use std::collections::HashMap; #[derive(Debug)] pub struct TxRefund { inner: Transaction, digest: SigHash, + cancel_output_descriptor: Descriptor<::bitcoin::PublicKey>, } impl TxRefund { @@ -28,6 +29,7 @@ impl TxRefund { Self { inner: tx_punish, digest, + cancel_output_descriptor: tx_cancel.output_descriptor.clone(), } } @@ -41,7 +43,6 @@ impl TxRefund { pub fn add_signatures( self, - tx_cancel: &TxCancel, (A, sig_a): (PublicKey, Signature), (B, sig_b): (PublicKey, Signature), ) -> Result { @@ -65,8 +66,7 @@ impl TxRefund { }; let mut tx_refund = self.inner; - tx_cancel - .output_descriptor + self.cancel_output_descriptor .satisfy(&mut tx_refund.input[0], satisfier)?; Ok(tx_refund) diff --git a/swap/src/database/alice.rs b/swap/src/database/alice.rs index 61a785af..8a5ba68f 100644 --- a/swap/src/database/alice.rs +++ b/swap/src/database/alice.rs @@ -205,7 +205,7 @@ impl From for AliceState { let tx_refund = TxRefund::new(&tx_cancel, &state3.refund_address); AliceState::BtcPunishable { monero_wallet_restore_blockheight, - tx_refund, + tx_refund: Box::new(tx_refund), state3: Box::new(state3), } } diff --git a/swap/src/protocol/alice/state.rs b/swap/src/protocol/alice/state.rs index ade62fe1..bc578a4d 100644 --- a/swap/src/protocol/alice/state.rs +++ b/swap/src/protocol/alice/state.rs @@ -53,7 +53,7 @@ pub enum AliceState { }, BtcPunishable { monero_wallet_restore_blockheight: BlockHeight, - tx_refund: TxRefund, + tx_refund: Box, state3: Box, }, XmrRefunded, diff --git a/swap/src/protocol/alice/steps.rs b/swap/src/protocol/alice/steps.rs index 411abd68..b77479c2 100644 --- a/swap/src/protocol/alice/steps.rs +++ b/swap/src/protocol/alice/steps.rs @@ -124,7 +124,7 @@ pub fn build_bitcoin_redeem_transaction( let sig_b = adaptor.decrypt_signature(&s_a, encrypted_signature); let tx = tx_redeem - .add_signatures(&tx_lock, (a.public(), sig_a), (B, sig_b)) + .add_signatures((a.public(), sig_a), (B, sig_b)) .context("sig_{a,b} are invalid for tx_redeem")?; Ok(tx) @@ -179,7 +179,7 @@ where let tx_cancel = tx_cancel .clone() - .add_signatures(&tx_lock, (a.public(), sig_a), (B, sig_b)) + .add_signatures((a.public(), sig_a), (B, sig_b)) .expect("sig_{a,b} to be valid signatures for tx_cancel"); // TODO(Franck): Error handling is delicate, why can't we broadcast? @@ -224,7 +224,7 @@ where pub fn extract_monero_private_key( published_refund_tx: bitcoin::Transaction, - tx_refund: TxRefund, + tx_refund: &TxRefund, s_a: monero::Scalar, a: bitcoin::SecretKey, S_b_bitcoin: bitcoin::PublicKey, @@ -261,7 +261,7 @@ pub fn build_bitcoin_punish_transaction( let sig_b = tx_punish_sig_bob; let signed_tx_punish = tx_punish - .add_signatures(&tx_cancel, (a.public(), sig_a), (B, sig_b)) + .add_signatures((a.public(), sig_a), (B, sig_b)) .expect("sig_{a,b} to be valid signatures for tx_cancel"); Ok(signed_tx_punish) diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index 55484983..ad83b12e 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -343,7 +343,7 @@ async fn run_until_internal( match published_refund_tx { None => { let state = AliceState::BtcPunishable { - tx_refund, + tx_refund: Box::new(tx_refund), state3, monero_wallet_restore_blockheight, }; @@ -366,7 +366,7 @@ async fn run_until_internal( Some(published_refund_tx) => { let spend_key = extract_monero_private_key( published_refund_tx, - tx_refund, + &tx_refund, state3.s_a, state3.a.clone(), state3.S_b_bitcoin, @@ -445,7 +445,7 @@ async fn run_until_internal( Either::Left((published_refund_tx, _)) => { let spend_key = extract_monero_private_key( published_refund_tx?, - tx_refund, + &tx_refund, state3.s_a, state3.a.clone(), state3.S_b_bitcoin, diff --git a/swap/src/protocol/bob/state.rs b/swap/src/protocol/bob/state.rs index 3387eb35..e41debfd 100644 --- a/swap/src/protocol/bob/state.rs +++ b/swap/src/protocol/bob/state.rs @@ -459,7 +459,7 @@ impl State4 { let tx_cancel = tx_cancel .clone() - .add_signatures(&self.tx_lock, (self.A, sig_a), (self.b.public(), sig_b)) + .add_signatures((self.A, sig_a), (self.b.public(), sig_b)) .expect( "sig_{a,b} to be valid signatures for tx_cancel", @@ -482,7 +482,7 @@ impl State4 { let tx_cancel = tx_cancel .clone() - .add_signatures(&self.tx_lock, (self.A, sig_a), (self.b.public(), sig_b)) + .add_signatures((self.A, sig_a), (self.b.public(), sig_b)) .expect( "sig_{a,b} to be valid signatures for tx_cancel", @@ -562,11 +562,8 @@ impl State4 { let sig_a = adaptor.decrypt_signature(&self.s_b.to_secpfun_scalar(), self.tx_refund_encsig.clone()); - let signed_tx_refund = tx_refund.add_signatures( - &tx_cancel.clone(), - (self.A, sig_a), - (self.b.public(), sig_b), - )?; + let signed_tx_refund = + tx_refund.add_signatures((self.A, sig_a), (self.b.public(), sig_b))?; let txid = bitcoin_wallet .broadcast_signed_transaction(signed_tx_refund)