@@ -45,15 +45,15 @@ use crate::collections::HashSet;
4545use alloc:: { boxed:: Box , rc:: Rc , string:: String , vec:: Vec } ;
4646use bdk_chain:: PersistBackend ;
4747use core:: cell:: RefCell ;
48+ use core:: fmt;
4849use core:: marker:: PhantomData ;
4950
5051use bitcoin:: psbt:: { self , PartiallySignedTransaction as Psbt } ;
51- use bitcoin:: { absolute, script:: PushBytes , OutPoint , ScriptBuf , Sequence , Transaction } ;
52+ use bitcoin:: { absolute, script:: PushBytes , OutPoint , ScriptBuf , Sequence , Transaction , Txid } ;
5253
5354use super :: coin_selection:: { CoinSelectionAlgorithm , DefaultCoinSelectionAlgorithm } ;
5455use super :: ChangeSet ;
5556use crate :: types:: { FeeRate , KeychainKind , LocalUtxo , WeightedUtxo } ;
56- use crate :: wallet:: error:: { AddForeignUtxoError , AddUtxoError , AllowShrinkingError } ;
5757use crate :: wallet:: CreateTxError ;
5858use crate :: { Utxo , Wallet } ;
5959
@@ -534,7 +534,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D,
534534
535535 /// Choose the coin selection algorithm
536536 ///
537- /// Overrides the [`DefaultCoinSelectionAlgorithm`](super::coin_selection::DefaultCoinSelectionAlgorithm) .
537+ /// Overrides the [`DefaultCoinSelectionAlgorithm`].
538538 ///
539539 /// Note that this function consumes the builder and returns it so it is usually best to put this as the first call on the builder.
540540 pub fn coin_selection < P : CoinSelectionAlgorithm > (
@@ -551,7 +551,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D,
551551
552552 /// Finish building the transaction.
553553 ///
554- /// Returns a new [`Psbt`] per [BIP174].
554+ /// Returns a new [`Psbt`] per [` BIP174` ].
555555 ///
556556 /// [`BIP174`]: https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki
557557 pub fn finish ( self ) -> Result < Psbt , CreateTxError < D :: WriteError > >
@@ -609,6 +609,90 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D,
609609 }
610610}
611611
612+ #[ derive( Debug ) ]
613+ /// Error returned from [`TxBuilder::add_utxo`] and [`TxBuilder::add_utxos`]
614+ pub enum AddUtxoError {
615+ /// Happens when trying to spend an UTXO that is not in the internal database
616+ UnknownUtxo ( OutPoint ) ,
617+ }
618+
619+ impl fmt:: Display for AddUtxoError {
620+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
621+ match self {
622+ Self :: UnknownUtxo ( outpoint) => write ! (
623+ f,
624+ "UTXO not found in the internal database for txid: {} with vout: {}" ,
625+ outpoint. txid, outpoint. vout
626+ ) ,
627+ }
628+ }
629+ }
630+
631+ #[ cfg( feature = "std" ) ]
632+ impl std:: error:: Error for AddUtxoError { }
633+
634+ #[ derive( Debug ) ]
635+ /// Error returned from [`TxBuilder::add_foreign_utxo`].
636+ pub enum AddForeignUtxoError {
637+ /// Foreign utxo outpoint txid does not match PSBT input txid
638+ InvalidTxid {
639+ /// PSBT input txid
640+ input_txid : Txid ,
641+ /// Foreign UTXO outpoint
642+ foreign_utxo : OutPoint ,
643+ } ,
644+ /// Requested outpoint doesn't exist in the tx (vout greater than available outputs)
645+ InvalidOutpoint ( OutPoint ) ,
646+ /// Foreign utxo missing witness_utxo or non_witness_utxo
647+ MissingUtxo ,
648+ }
649+
650+ impl fmt:: Display for AddForeignUtxoError {
651+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
652+ match self {
653+ Self :: InvalidTxid {
654+ input_txid,
655+ foreign_utxo,
656+ } => write ! (
657+ f,
658+ "Foreign UTXO outpoint txid: {} does not match PSBT input txid: {}" ,
659+ foreign_utxo. txid, input_txid,
660+ ) ,
661+ Self :: InvalidOutpoint ( outpoint) => write ! (
662+ f,
663+ "Requested outpoint doesn't exist for txid: {} with vout: {}" ,
664+ outpoint. txid, outpoint. vout,
665+ ) ,
666+ Self :: MissingUtxo => write ! ( f, "Foreign utxo missing witness_utxo or non_witness_utxo" ) ,
667+ }
668+ }
669+ }
670+
671+ #[ cfg( feature = "std" ) ]
672+ impl std:: error:: Error for AddForeignUtxoError { }
673+
674+ #[ derive( Debug ) ]
675+ /// Error returned from [`TxBuilder::allow_shrinking`]
676+ pub enum AllowShrinkingError {
677+ /// Script/PubKey was not in the original transaction
678+ MissingScriptPubKey ( ScriptBuf ) ,
679+ }
680+
681+ impl fmt:: Display for AllowShrinkingError {
682+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
683+ match self {
684+ Self :: MissingScriptPubKey ( script_buf) => write ! (
685+ f,
686+ "Script/PubKey was not in the original transaction: {}" ,
687+ script_buf,
688+ ) ,
689+ }
690+ }
691+ }
692+
693+ #[ cfg( feature = "std" ) ]
694+ impl std:: error:: Error for AllowShrinkingError { }
695+
612696impl < ' a , D , Cs : CoinSelectionAlgorithm > TxBuilder < ' a , D , Cs , CreateTx > {
613697 /// Replace the recipients already added with a new list
614698 pub fn set_recipients ( & mut self , recipients : Vec < ( ScriptBuf , u64 ) > ) -> & mut Self {
0 commit comments