mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-06-25 22:22:25 +00:00
![Byron Hambly](/assets/img/avatar_default.png)
* ci: add cargo check on rust stable * refactor: upgrade secp256kfun and fix resulting issues * build(deps): update sigma_fun and ecdsa_fun to a52142cf7f #1520 #1521 * chore: fix clippy issue * update to 91112f80b24 * bump to 294de1721add * chore(deps): remove spectral spectral fails to compile on rust stable 1.76 due to dep on deprecated rustc-serialize * secp256kfun: update to 7da9d277 and set rev in manifest * update to 6fdc5d8 * switch to crates.io versions of ecdsa_fun and sigma_fun * ci: update toolchain to 1.74 and fix draft action * clippy fixes --------- Co-authored-by: binarybaron <86064887+binarybaron@users.noreply.github.com>
110 lines
3.0 KiB
Rust
110 lines
3.0 KiB
Rust
use crate::bitcoin::wallet::Watchable;
|
|
use crate::bitcoin::{self, Address, Amount, PunishTimelock, Transaction, TxCancel, Txid};
|
|
use ::bitcoin::util::sighash::SighashCache;
|
|
use ::bitcoin::{secp256k1, EcdsaSighashType, Sighash};
|
|
use anyhow::{Context, Result};
|
|
use bdk::bitcoin::Script;
|
|
use bdk::miniscript::Descriptor;
|
|
use std::collections::HashMap;
|
|
|
|
#[derive(Debug)]
|
|
pub struct TxPunish {
|
|
inner: Transaction,
|
|
digest: Sighash,
|
|
cancel_output_descriptor: Descriptor<::bitcoin::PublicKey>,
|
|
watch_script: Script,
|
|
}
|
|
|
|
impl TxPunish {
|
|
pub fn new(
|
|
tx_cancel: &TxCancel,
|
|
punish_address: &Address,
|
|
punish_timelock: PunishTimelock,
|
|
spending_fee: Amount,
|
|
) -> Self {
|
|
let tx_punish =
|
|
tx_cancel.build_spend_transaction(punish_address, Some(punish_timelock), spending_fee);
|
|
|
|
let digest = SighashCache::new(&tx_punish)
|
|
.segwit_signature_hash(
|
|
0, // Only one input: cancel transaction
|
|
&tx_cancel
|
|
.output_descriptor
|
|
.script_code()
|
|
.expect("scriptcode"),
|
|
tx_cancel.amount().to_sat(),
|
|
EcdsaSighashType::All,
|
|
)
|
|
.expect("sighash");
|
|
|
|
Self {
|
|
inner: tx_punish,
|
|
digest,
|
|
cancel_output_descriptor: tx_cancel.output_descriptor.clone(),
|
|
watch_script: punish_address.script_pubkey(),
|
|
}
|
|
}
|
|
|
|
pub fn digest(&self) -> Sighash {
|
|
self.digest
|
|
}
|
|
|
|
pub fn complete(
|
|
self,
|
|
tx_punish_sig_bob: bitcoin::Signature,
|
|
a: bitcoin::SecretKey,
|
|
B: bitcoin::PublicKey,
|
|
) -> Result<Transaction> {
|
|
let sig_a = a.sign(self.digest());
|
|
let sig_b = tx_punish_sig_bob;
|
|
|
|
let satisfier = {
|
|
let mut satisfier = HashMap::with_capacity(2);
|
|
|
|
let A = a.public().try_into()?;
|
|
let B = B.try_into()?;
|
|
|
|
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
|
|
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
|
|
// The order in which these are inserted doesn't matter
|
|
satisfier.insert(
|
|
A,
|
|
::bitcoin::EcdsaSig {
|
|
sig: sig_a,
|
|
hash_ty: EcdsaSighashType::All,
|
|
},
|
|
);
|
|
satisfier.insert(
|
|
B,
|
|
::bitcoin::EcdsaSig {
|
|
sig: sig_b,
|
|
hash_ty: EcdsaSighashType::All,
|
|
},
|
|
);
|
|
|
|
satisfier
|
|
};
|
|
|
|
let mut tx_punish = self.inner;
|
|
self.cancel_output_descriptor
|
|
.satisfy(&mut tx_punish.input[0], satisfier)
|
|
.context("Failed to satisfy inputs with given signatures")?;
|
|
|
|
Ok(tx_punish)
|
|
}
|
|
|
|
pub fn weight() -> usize {
|
|
548
|
|
}
|
|
}
|
|
|
|
impl Watchable for TxPunish {
|
|
fn id(&self) -> Txid {
|
|
self.inner.txid()
|
|
}
|
|
|
|
fn script(&self) -> Script {
|
|
self.watch_script.clone()
|
|
}
|
|
}
|