Skip to content

Commit

Permalink
Check spend tx against libbitcoinconsensus
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardparis committed Jun 17, 2022
1 parent 6af6b56 commit f1a5109
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = ["Antoine Poinsot <darosior@protonmail.com>"]
edition = "2018"

[dependencies]
bitcoinconsensus = "0.19.0-2"
revault_tx = { version = "0.5", features = ["use-serde"] }
revault_net = "0.3"

Expand Down
4 changes: 3 additions & 1 deletion src/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ impl CoordinatorClient {
transport.send_req(&req)
}

// Get Spend transaction spending the vault with the given deposit outpoint.
/// Get Spend transaction spending the vault with the given deposit outpoint.
/// Beware that the spend transaction may be invalid and needs to be verified against
/// libbitcoinconsensus.
pub fn get_spend_transaction(
&self,
deposit_outpoint: OutPoint,
Expand Down
35 changes: 33 additions & 2 deletions src/poller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use revault_tx::{
CancelTransaction, RevaultPresignedTransaction, RevaultTransaction, UnvaultTransaction,
},
txins::{DepositTxIn, RevaultTxIn, UnvaultTxIn},
txouts::DepositTxOut,
txouts::{DepositTxOut, RevaultTxOut},
};

use revault_net::noise::SecretKey as NoisePrivkey;
Expand Down Expand Up @@ -400,7 +400,38 @@ fn check_for_unvault(

let candidate_tx = if let Some(client) = coordinator_client {
match client.get_spend_transaction(db_vault.deposit_outpoint.clone()) {
Ok(res) => res,
Ok(Some(tx)) => {
let spent_unvault_outpoint = unvault_txin.outpoint();
if let Some(i) = tx
.input
.iter()
.position(|input| input.previous_output == spent_unvault_outpoint)
{
let txout = unvault_txin.txout().txout();
if let Err(e) = bitcoinconsensus::verify(
&txout.script_pubkey.as_bytes(),
txout.value,
&encode::serialize(&tx),
i,
) {
log::error!(
"Coordinator sent a suspicious tx {}, libbitcoinconsensus error: {:?}",
tx.txid(),
e
);
None
} else {
Some(tx)
}
} else {
log::error!(
"Coordinator sent a suspicious tx {}, the transaction does not spend the vault",
tx.txid(),
);
None
}
}
Ok(None) => None,
Err(_e) => {
// Because we do not trust the coordinator, we consider it refuses to deliver the
// spend tx if a communication error happened.
Expand Down

0 comments on commit f1a5109

Please sign in to comment.