Skip to content

Commit 39476fb

Browse files
committed
refactor(wallet)!: move TxBuilder errors to tx_builder module
Errors moved from wallet/error.rs to wallet/tx_builder: AddUtxoError, AddForeignUtxoError, AllowShrinkingError
1 parent 6c03ed1 commit 39476fb

File tree

3 files changed

+96
-95
lines changed

3 files changed

+96
-95
lines changed

crates/bdk/src/wallet/error.rs

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::descriptor::DescriptorError;
1616
use crate::wallet::coin_selection;
1717
use crate::{descriptor, FeeRate, KeychainKind};
1818
use alloc::string::String;
19-
use bitcoin::{absolute, psbt, OutPoint, ScriptBuf, Sequence, Txid};
19+
use bitcoin::{absolute, psbt, OutPoint, Sequence, Txid};
2020
use core::fmt;
2121

2222
/// Errors returned by miniscript when updating inconsistent PSBTs
@@ -290,94 +290,3 @@ impl fmt::Display for BuildFeeBumpError {
290290

291291
#[cfg(feature = "std")]
292292
impl std::error::Error for BuildFeeBumpError {}
293-
294-
#[derive(Debug)]
295-
/// Error returned from [`TxBuilder::add_utxo`] and [`TxBuilder::add_utxos`]
296-
///
297-
/// [`TxBuilder::add_utxo`]: wallet::tx_builder::TxBuilder::add_utxo
298-
/// [`TxBuilder::add_utxos`]: wallet::tx_builder::TxBuilder::add_utxos
299-
pub enum AddUtxoError {
300-
/// Happens when trying to spend an UTXO that is not in the internal database
301-
UnknownUtxo(OutPoint),
302-
}
303-
304-
impl fmt::Display for AddUtxoError {
305-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
306-
match self {
307-
Self::UnknownUtxo(outpoint) => write!(
308-
f,
309-
"UTXO not found in the internal database for txid: {} with vout: {}",
310-
outpoint.txid, outpoint.vout
311-
),
312-
}
313-
}
314-
}
315-
316-
#[cfg(feature = "std")]
317-
impl std::error::Error for AddUtxoError {}
318-
319-
#[derive(Debug)]
320-
/// Error returned from [`TxBuilder::add_foreign_utxo`].
321-
///
322-
/// [`TxBuilder::add_foreign_utxo`]: wallet::tx_builder::TxBuilder::add_foreign_utxo
323-
pub enum AddForeignUtxoError {
324-
/// Foreign utxo outpoint txid does not match PSBT input txid
325-
InvalidTxid {
326-
/// PSBT input txid
327-
input_txid: Txid,
328-
/// Foreign UTXO outpoint
329-
foreign_utxo: OutPoint,
330-
},
331-
/// Requested outpoint doesn't exist in the tx (vout greater than available outputs)
332-
InvalidOutpoint(OutPoint),
333-
/// Foreign utxo missing witness_utxo or non_witness_utxo
334-
MissingUtxo,
335-
}
336-
337-
impl fmt::Display for AddForeignUtxoError {
338-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
339-
match self {
340-
Self::InvalidTxid {
341-
input_txid,
342-
foreign_utxo,
343-
} => write!(
344-
f,
345-
"Foreign UTXO outpoint txid: {} does not match PSBT input txid: {}",
346-
foreign_utxo.txid, input_txid,
347-
),
348-
Self::InvalidOutpoint(outpoint) => write!(
349-
f,
350-
"Requested outpoint doesn't exist for txid: {} with vout: {}",
351-
outpoint.txid, outpoint.vout,
352-
),
353-
Self::MissingUtxo => write!(f, "Foreign utxo missing witness_utxo or non_witness_utxo"),
354-
}
355-
}
356-
}
357-
358-
#[cfg(feature = "std")]
359-
impl std::error::Error for AddForeignUtxoError {}
360-
361-
#[derive(Debug)]
362-
/// Error returned from [`TxBuilder::allow_shrinking`]
363-
///
364-
/// [`TxBuilder::allow_shrinking`]: wallet::tx_builder::TxBuilder::allow_shrinking
365-
pub enum AllowShrinkingError {
366-
/// Script/PubKey was not in the original transaction
367-
MissingScriptPubKey(ScriptBuf),
368-
}
369-
370-
impl fmt::Display for AllowShrinkingError {
371-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
372-
match self {
373-
Self::MissingScriptPubKey(script_buf) => write!(
374-
f,
375-
"Script/PubKey was not in the original transaction: {}",
376-
script_buf,
377-
),
378-
}
379-
}
380-
}
381-
382-
#[cfg(feature = "std")]
383-
impl std::error::Error for AllowShrinkingError {}

crates/bdk/src/wallet/tx_builder.rs

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ use crate::collections::HashSet;
4545
use alloc::{boxed::Box, rc::Rc, string::String, vec::Vec};
4646
use bdk_chain::PersistBackend;
4747
use core::cell::RefCell;
48+
use core::fmt;
4849
use core::marker::PhantomData;
4950

5051
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};
5253

5354
use super::coin_selection::{CoinSelectionAlgorithm, DefaultCoinSelectionAlgorithm};
5455
use super::ChangeSet;
5556
use crate::types::{FeeRate, KeychainKind, LocalUtxo, WeightedUtxo};
56-
use crate::wallet::error::{AddForeignUtxoError, AddUtxoError, AllowShrinkingError};
5757
use crate::wallet::CreateTxError;
5858
use 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+
612703
impl<'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 {

crates/bdk/tests/wallet.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use bdk::descriptor::calc_checksum;
33
use bdk::psbt::PsbtUtils;
44
use bdk::signer::{SignOptions, SignerError};
55
use bdk::wallet::coin_selection::{self, LargestFirstCoinSelection};
6-
use bdk::wallet::error::{AddForeignUtxoError, CreateTxError};
6+
use bdk::wallet::error::CreateTxError;
7+
use bdk::wallet::tx_builder::AddForeignUtxoError;
78
use bdk::wallet::AddressIndex::*;
89
use bdk::wallet::{AddressIndex, AddressInfo, Balance, Wallet};
910
use bdk::{FeeRate, KeychainKind};

0 commit comments

Comments
 (0)