Skip to content

Commit f57f94d

Browse files
committed
transactions: refactor RevaultTransaction into using traits
Benefits: - Enforce types at compilation - Better API (no Result at creation, errors are statics, more fine-grained) - Actual types (no enum variant) Drawbacks: - Boilerplate, partially handled by macros Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
1 parent 45178e8 commit f57f94d

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

src/transactions.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,61 @@ impl<'a, Pk: MiniscriptKey + ToPublicKey> RevaultSatisfier<'a, Pk> {
525525
}
526526
}
527527

528+
/// Verify this transaction validity against libbitcoinconsensus.
529+
/// Handles all the destructuring and txout research internally.
530+
///
531+
/// # Errors
532+
/// - If verification fails.
533+
pub fn verify_revault_transaction(
534+
revault_tx: &impl RevaultTransaction,
535+
previous_transactions: &[&impl RevaultTransaction],
536+
) -> Result<(), Error> {
537+
// Look for a referenced txout in the set of spent transactions
538+
// TODO: optimize this by walking the previous tx set only once ?
539+
fn get_prev_script_and_value<'a>(
540+
prevout: &OutPoint,
541+
transactions: &'a [&impl RevaultTransaction],
542+
) -> Option<(&'a [u8], u64)> {
543+
for prev_tx in transactions {
544+
let tx = prev_tx.inner_tx();
545+
if tx.txid() == prevout.txid {
546+
return tx
547+
.output
548+
.get(prevout.vout as usize)
549+
.and_then(|txo| Some((txo.script_pubkey.as_bytes(), txo.value)));
550+
}
551+
}
552+
553+
None
554+
}
555+
556+
for (index, txin) in revault_tx.inner_tx().input.iter().enumerate() {
557+
match get_prev_script_and_value(&txin.previous_output, &previous_transactions) {
558+
Some((ref raw_script_pubkey, ref value)) => {
559+
if let Err(err) = bitcoinconsensus::verify(
560+
*raw_script_pubkey,
561+
*value,
562+
revault_tx.serialize().as_slice(),
563+
index,
564+
) {
565+
return Err(Error::TransactionVerification(format!(
566+
"Bitcoinconsensus error: {:?}",
567+
err
568+
)));
569+
}
570+
}
571+
None => {
572+
return Err(Error::TransactionVerification(format!(
573+
"Unknown txout refered by txin '{:?}'",
574+
txin
575+
)));
576+
}
577+
}
578+
}
579+
580+
Ok(())
581+
}
582+
528583
#[cfg(test)]
529584
mod tests {
530585
use super::{

0 commit comments

Comments
 (0)