Browse Source

move refund transaction signing onto cfd

fix-bad-api-calls
Daniel Karzel 3 years ago
parent
commit
2fd2c30325
No known key found for this signature in database GPG Key ID: 30C3FC2E438ADB6E
  1. 28
      daemon/src/maker_cfd.rs
  2. 32
      daemon/src/model/cfd.rs

28
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)

32
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<Transaction> {
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)]

Loading…
Cancel
Save