From e01d84f57891aeab3a82543819af14d163cd16cc Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 25 Nov 2021 12:05:43 +1100 Subject: [PATCH] Push down signing of commit transaction into `Dlc` See https://github.com/itchysats/itchysats/issues/646. --- daemon/src/model/cfd.rs | 48 ++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/daemon/src/model/cfd.rs b/daemon/src/model/cfd.rs index 9217926..13e4327 100644 --- a/daemon/src/model/cfd.rs +++ b/daemon/src/model/cfd.rs @@ -1073,28 +1073,7 @@ impl Cfd { ) }; - let sig_hash = spending_tx_sighash( - &dlc.commit.0, - &dlc.lock.1, - Amount::from_sat(dlc.lock.0.output[0].value), - ); - let our_sig = SECP256K1.sign(&sig_hash, &dlc.identity); - let our_pubkey = PublicKey::new(bdk::bitcoin::secp256k1::PublicKey::from_secret_key( - SECP256K1, - &dlc.identity, - )); - - let counterparty_sig = dlc.commit.1.decrypt(&dlc.publish)?; - let counterparty_pubkey = dlc.identity_counterparty; - - let signed_commit_tx = finalize_spend_transaction( - dlc.commit.0, - &dlc.lock.1, - (our_pubkey, our_sig), - (counterparty_pubkey, counterparty_sig), - )?; - - Ok(signed_commit_tx) + dlc.signed_commit_tx() } pub fn cet(&self) -> Result> { @@ -1589,6 +1568,31 @@ impl Dlc { Ok(signed_refund_tx) } + + pub fn signed_commit_tx(&self) -> Result { + let sig_hash = spending_tx_sighash( + &self.commit.0, + &self.lock.1, + Amount::from_sat(self.lock.0.output[0].value), + ); + let our_sig = SECP256K1.sign(&sig_hash, &self.identity); + let our_pubkey = PublicKey::new(bdk::bitcoin::secp256k1::PublicKey::from_secret_key( + SECP256K1, + &self.identity, + )); + + let counterparty_sig = self.commit.1.decrypt(&self.publish)?; + let counterparty_pubkey = self.identity_counterparty; + + let signed_commit_tx = finalize_spend_transaction( + self.commit.0.clone(), + &self.lock.1, + (our_pubkey, our_sig), + (counterparty_pubkey, counterparty_sig), + )?; + + Ok(signed_commit_tx) + } } /// Information which we need to remember in order to construct a