@@ -44,6 +44,8 @@ use descriptor::error::Error as DescriptorError;
44
44
use miniscript:: psbt:: { PsbtExt , PsbtInputExt , PsbtInputSatisfier } ;
45
45
46
46
use bdk_chain:: tx_graph:: CalculateFeeError ;
47
+ #[ cfg( feature = "bitcoinconsensus" ) ]
48
+ use bdk_chain:: tx_graph:: VerifyTxError ;
47
49
48
50
pub mod coin_selection;
49
51
pub mod export;
@@ -2488,6 +2490,45 @@ impl<D> Wallet<D> {
2488
2490
. batch_insert_relevant_unconfirmed ( unconfirmed_txs) ;
2489
2491
self . persist . stage ( ChangeSet :: from ( indexed_graph_changeset) ) ;
2490
2492
}
2493
+
2494
+ /// Verify the given transaction is able to spend its inputs.
2495
+ ///
2496
+ /// This method uses [`rust-bitcoinconsensus`] to verify a [`Transaction`], guaranteeing
2497
+ /// that if the method succeeds, the transaction meets consensus criteria as defined in
2498
+ /// Bitcoin's `libbitcoinconsensus`.
2499
+ ///
2500
+ /// # Example
2501
+ ///
2502
+ /// ```rust
2503
+ /// # use bdk::SignOptions;
2504
+ /// # use bdk::wallet::AddressIndex;
2505
+ /// # let mut wallet = bdk::doctest_wallet!();
2506
+ /// # let address = wallet.get_address(AddressIndex::New);
2507
+ /// let mut builder = wallet.build_tx();
2508
+ /// builder.add_recipient(address.script_pubkey(), 210_000);
2509
+ /// let mut psbt = builder.finish().unwrap();
2510
+ /// let _ = wallet.sign(&mut psbt, SignOptions::default()).unwrap();
2511
+ /// let tx = psbt.extract_tx();
2512
+ /// assert!(wallet.verify_tx(&tx).is_ok());
2513
+ /// ```
2514
+ ///
2515
+ /// **Note** that validation by the Bitcoin network can ultimately fail in other ways,
2516
+ /// for example if a timelock hasn't been met. Also, verifying that a transaction
2517
+ /// can spend its inputs doesn't guarantee it will be accepted to mempools or propagated
2518
+ /// by nodes on the peer-to-peer network.
2519
+ ///
2520
+ /// # Errors
2521
+ ///
2522
+ /// If the previous output isn't found for one or more `tx` inputs.
2523
+ ///
2524
+ /// If [`Script`] verification fails.
2525
+ ///
2526
+ /// [`rust-bitcoinconsensus`]: https://docs.rs/bitcoinconsensus/latest/bitcoinconsensus/
2527
+ #[ cfg( feature = "bitcoinconsensus" ) ]
2528
+ #[ cfg_attr( docsrs, doc( cfg( feature = "bitcoinconsensus" ) ) ) ]
2529
+ pub fn verify_tx ( & self , tx : & Transaction ) -> Result < ( ) , VerifyTxError > {
2530
+ self . tx_graph ( ) . verify_tx ( tx)
2531
+ }
2491
2532
}
2492
2533
2493
2534
impl < D > AsRef < bdk_chain:: tx_graph:: TxGraph < ConfirmationTimeHeightAnchor > > for Wallet < D > {
0 commit comments