Skip to content

Commit 92f8579

Browse files
committed
refactor(wallet)!: move TxBuilder errors to tx_builder module
chore(policy): fixed spelling error Errors moved from wallet/error.rs to wallet/tx_builder: AddUtxoError, AddForeignUtxoError, AllowShrinkingError Fixed doc errors
1 parent 6c03ed1 commit 92f8579

File tree

5 files changed

+95
-101
lines changed

5 files changed

+95
-101
lines changed

crates/bdk/src/descriptor/policy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ pub enum PolicyError {
522522
impl fmt::Display for PolicyError {
523523
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
524524
match self {
525-
Self::NotEnoughItemsSelected(err) => write!(f, "Not enought items selected: {}", err),
525+
Self::NotEnoughItemsSelected(err) => write!(f, "Not enough items selected: {}", err),
526526
Self::IndexOutOfRange(index) => write!(f, "Index out of range: {}", index),
527527
Self::AddOnLeaf => write!(f, "Add on leaf"),
528528
Self::AddOnPartialComplete => write!(f, "Add on partial complete"),

crates/bdk/src/keys/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl<Ctx: ScriptContext> From<bip32::ExtendedPrivKey> for ExtendedKey<Ctx> {
413413
/// }
414414
/// ```
415415
///
416-
/// Types that don't internally encode the [`Network`](bitcoin::Network) in which they are valid need some extra
416+
/// Types that don't internally encode the [`Network`] in which they are valid need some extra
417417
/// steps to override the set of valid networks, otherwise only the network specified in the
418418
/// [`ExtendedPrivKey`] or [`ExtendedPubKey`] will be considered valid.
419419
///

crates/bdk/src/wallet/error.rs

+3-94
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
@@ -54,7 +54,7 @@ pub enum CreateTxError<P> {
5454
Persist(P),
5555
/// There was a problem while extracting and manipulating policies
5656
Policy(PolicyError),
57-
/// Spending policy is not compatible with this [`KeychainKind`](crate::types::KeychainKind)
57+
/// Spending policy is not compatible with this [`KeychainKind`]
5858
SpendingPolicyRequired(KeychainKind),
5959
/// Requested invalid transaction version '0'
6060
Version0,
@@ -248,7 +248,7 @@ impl<P: core::fmt::Display + core::fmt::Debug> std::error::Error for CreateTxErr
248248
#[derive(Debug)]
249249
/// Error returned from [`Wallet::build_fee_bump`]
250250
///
251-
/// [`Wallet::build_fee_bump`]: wallet::Wallet::build_fee_bump
251+
/// [`Wallet::build_fee_bump`]: super::Wallet::build_fee_bump
252252
pub enum BuildFeeBumpError {
253253
/// Happens when trying to spend an UTXO that is not in the internal database
254254
UnknownUtxo(OutPoint),
@@ -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

+88-4
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

@@ -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+
612696
impl<'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 {

crates/bdk/tests/wallet.rs

+2-1
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)