@@ -45,15 +45,15 @@ use crate::collections::HashSet;
45
45
use alloc:: { boxed:: Box , rc:: Rc , string:: String , vec:: Vec } ;
46
46
use bdk_chain:: PersistBackend ;
47
47
use core:: cell:: RefCell ;
48
+ use core:: fmt;
48
49
use core:: marker:: PhantomData ;
49
50
50
51
use 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 } ;
52
53
53
54
use super :: coin_selection:: { CoinSelectionAlgorithm , DefaultCoinSelectionAlgorithm } ;
54
55
use super :: ChangeSet ;
55
56
use crate :: types:: { FeeRate , KeychainKind , LocalUtxo , WeightedUtxo } ;
56
- use crate :: wallet:: error:: { AddForeignUtxoError , AddUtxoError , AllowShrinkingError } ;
57
57
use crate :: wallet:: CreateTxError ;
58
58
use crate :: { Utxo , Wallet } ;
59
59
@@ -534,7 +534,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D,
534
534
535
535
/// Choose the coin selection algorithm
536
536
///
537
- /// Overrides the [`DefaultCoinSelectionAlgorithm`](super::coin_selection::DefaultCoinSelectionAlgorithm) .
537
+ /// Overrides the [`DefaultCoinSelectionAlgorithm`].
538
538
///
539
539
/// 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.
540
540
pub fn coin_selection < P : CoinSelectionAlgorithm > (
@@ -551,7 +551,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D,
551
551
552
552
/// Finish building the transaction.
553
553
///
554
- /// Returns a new [`Psbt`] per [BIP174].
554
+ /// Returns a new [`Psbt`] per [` BIP174` ].
555
555
///
556
556
/// [`BIP174`]: https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki
557
557
pub fn finish ( self ) -> Result < Psbt , CreateTxError < D :: WriteError > >
@@ -609,6 +609,90 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D,
609
609
}
610
610
}
611
611
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
+
612
696
impl < ' a , D , Cs : CoinSelectionAlgorithm > TxBuilder < ' a , D , Cs , CreateTx > {
613
697
/// Replace the recipients already added with a new list
614
698
pub fn set_recipients ( & mut self , recipients : Vec < ( ScriptBuf , u64 ) > ) -> & mut Self {
0 commit comments