@@ -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
@@ -609,6 +609,97 @@ 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+ ///
615+ /// [`TxBuilder::add_utxo`]: wallet::tx_builder::TxBuilder::add_utxo
616+ /// [`TxBuilder::add_utxos`]: wallet::tx_builder::TxBuilder::add_utxos
617+ pub enum AddUtxoError {
618+ /// Happens when trying to spend an UTXO that is not in the internal database
619+ UnknownUtxo ( OutPoint ) ,
620+ }
621+
622+ impl fmt:: Display for AddUtxoError {
623+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
624+ match self {
625+ Self :: UnknownUtxo ( outpoint) => write ! (
626+ f,
627+ "UTXO not found in the internal database for txid: {} with vout: {}" ,
628+ outpoint. txid, outpoint. vout
629+ ) ,
630+ }
631+ }
632+ }
633+
634+ #[ cfg( feature = "std" ) ]
635+ impl std:: error:: Error for AddUtxoError { }
636+
637+ #[ derive( Debug ) ]
638+ /// Error returned from [`TxBuilder::add_foreign_utxo`].
639+ ///
640+ /// [`TxBuilder::add_foreign_utxo`]: wallet::tx_builder::TxBuilder::add_foreign_utxo
641+ pub enum AddForeignUtxoError {
642+ /// Foreign utxo outpoint txid does not match PSBT input txid
643+ InvalidTxid {
644+ /// PSBT input txid
645+ input_txid : Txid ,
646+ /// Foreign UTXO outpoint
647+ foreign_utxo : OutPoint ,
648+ } ,
649+ /// Requested outpoint doesn't exist in the tx (vout greater than available outputs)
650+ InvalidOutpoint ( OutPoint ) ,
651+ /// Foreign utxo missing witness_utxo or non_witness_utxo
652+ MissingUtxo ,
653+ }
654+
655+ impl fmt:: Display for AddForeignUtxoError {
656+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
657+ match self {
658+ Self :: InvalidTxid {
659+ input_txid,
660+ foreign_utxo,
661+ } => write ! (
662+ f,
663+ "Foreign UTXO outpoint txid: {} does not match PSBT input txid: {}" ,
664+ foreign_utxo. txid, input_txid,
665+ ) ,
666+ Self :: InvalidOutpoint ( outpoint) => write ! (
667+ f,
668+ "Requested outpoint doesn't exist for txid: {} with vout: {}" ,
669+ outpoint. txid, outpoint. vout,
670+ ) ,
671+ Self :: MissingUtxo => write ! ( f, "Foreign utxo missing witness_utxo or non_witness_utxo" ) ,
672+ }
673+ }
674+ }
675+
676+ #[ cfg( feature = "std" ) ]
677+ impl std:: error:: Error for AddForeignUtxoError { }
678+
679+ #[ derive( Debug ) ]
680+ /// Error returned from [`TxBuilder::allow_shrinking`]
681+ ///
682+ /// [`TxBuilder::allow_shrinking`]: wallet::tx_builder::TxBuilder::allow_shrinking
683+ pub enum AllowShrinkingError {
684+ /// Script/PubKey was not in the original transaction
685+ MissingScriptPubKey ( ScriptBuf ) ,
686+ }
687+
688+ impl fmt:: Display for AllowShrinkingError {
689+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
690+ match self {
691+ Self :: MissingScriptPubKey ( script_buf) => write ! (
692+ f,
693+ "Script/PubKey was not in the original transaction: {}" ,
694+ script_buf,
695+ ) ,
696+ }
697+ }
698+ }
699+
700+ #[ cfg( feature = "std" ) ]
701+ impl std:: error:: Error for AllowShrinkingError { }
702+
612703impl < ' a , D , Cs : CoinSelectionAlgorithm > TxBuilder < ' a , D , Cs , CreateTx > {
613704 /// Replace the recipients already added with a new list
614705 pub fn set_recipients ( & mut self , recipients : Vec < ( ScriptBuf , u64 ) > ) -> & mut Self {
0 commit comments