From 2fd2c30325e6b717c68ba4d11fb92f6760aea470 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Mon, 27 Sep 2021 15:38:24 +1000 Subject: [PATCH] move refund transaction signing onto cfd --- daemon/src/maker_cfd.rs | 28 ++++------------------------ daemon/src/model/cfd.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/daemon/src/maker_cfd.rs b/daemon/src/maker_cfd.rs index 9a88d54..08d2e50 100644 --- a/daemon/src/maker_cfd.rs +++ b/daemon/src/maker_cfd.rs @@ -13,9 +13,6 @@ use crate::{maker_inc_connections, monitor, setup_contract_actor}; use anyhow::Result; use async_trait::async_trait; use bdk::bitcoin::secp256k1::schnorrsig; -use bdk::bitcoin::{Amount, PublicKey}; -use cfd_protocol::secp256k1_zkp::SECP256K1; -use cfd_protocol::{finalize_spend_transaction, spending_tx_sighash}; use std::time::SystemTime; use tokio::sync::{mpsc, watch}; use xtra::prelude::*; @@ -421,27 +418,10 @@ impl Actor { insert_new_cfd_state_by_order_id(order_id, new_state.clone(), &mut conn).await?; - // TODO: Consider sending a message to ourselves to trigger broadcasting refund? - if let CfdState::MustRefund { dlc, .. } = new_state { - let sig_hash = spending_tx_sighash( - &dlc.refund.0, - &dlc.commit.2, - Amount::from_sat(dlc.commit.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.refund.1; - let counterparty_pubkey = dlc.identity_counterparty; - let signed_refund_tx = finalize_spend_transaction( - dlc.refund.0, - &dlc.commit.2, - (our_pubkey, our_sig), - (counterparty_pubkey, counterparty_sig), - )?; - + // TODO: Not sure that should be done here... + // Consider sending a message to ourselves to trigger broadcasting refund? + if let CfdState::MustRefund { .. } = new_state { + let signed_refund_tx = cfd.refund_tx()?; let txid = self .wallet .try_broadcast_transaction(signed_refund_tx) diff --git a/daemon/src/model/cfd.rs b/daemon/src/model/cfd.rs index c399dde..f8cf1cd 100644 --- a/daemon/src/model/cfd.rs +++ b/daemon/src/model/cfd.rs @@ -4,7 +4,8 @@ use anyhow::{bail, Result}; use bdk::bitcoin::secp256k1::{SecretKey, Signature}; use bdk::bitcoin::{Address, Amount, PublicKey, Transaction}; use bdk::descriptor::Descriptor; -use cfd_protocol::secp256k1_zkp::EcdsaAdaptorSignature; +use cfd_protocol::secp256k1_zkp::{EcdsaAdaptorSignature, SECP256K1}; +use cfd_protocol::{finalize_spend_transaction, spending_tx_sighash}; use rust_decimal::Decimal; use rust_decimal_macros::dec; use serde::{Deserialize, Serialize}; @@ -543,6 +544,35 @@ impl Cfd { Ok(new_state) } + + pub fn refund_tx(&self) -> Result { + let dlc = if let CfdState::MustRefund { dlc, .. } = self.state.clone() { + dlc + } else { + bail!("Refund transaction can only be constructed when in state MustRefund, but we are currently in {}", self.state.clone()) + }; + + let sig_hash = spending_tx_sighash( + &dlc.refund.0, + &dlc.commit.2, + Amount::from_sat(dlc.commit.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.refund.1; + let counterparty_pubkey = dlc.identity_counterparty; + let signed_refund_tx = finalize_spend_transaction( + dlc.refund.0, + &dlc.commit.2, + (our_pubkey, our_sig), + (counterparty_pubkey, counterparty_sig), + )?; + + Ok(signed_refund_tx) + } } #[derive(Debug, Clone)]