From 92f857903d2f131956d946f663528b00f1cf2b3d Mon Sep 17 00:00:00 2001 From: Steve Myers Date: Mon, 13 Nov 2023 23:20:26 -0600 Subject: [PATCH] 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 --- crates/bdk/src/descriptor/policy.rs | 2 +- crates/bdk/src/keys/mod.rs | 2 +- crates/bdk/src/wallet/error.rs | 97 +---------------------------- crates/bdk/src/wallet/tx_builder.rs | 92 +++++++++++++++++++++++++-- crates/bdk/tests/wallet.rs | 3 +- 5 files changed, 95 insertions(+), 101 deletions(-) diff --git a/crates/bdk/src/descriptor/policy.rs b/crates/bdk/src/descriptor/policy.rs index b211bc247e..29a0d10298 100644 --- a/crates/bdk/src/descriptor/policy.rs +++ b/crates/bdk/src/descriptor/policy.rs @@ -522,7 +522,7 @@ pub enum PolicyError { impl fmt::Display for PolicyError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::NotEnoughItemsSelected(err) => write!(f, "Not enought items selected: {}", err), + Self::NotEnoughItemsSelected(err) => write!(f, "Not enough items selected: {}", err), Self::IndexOutOfRange(index) => write!(f, "Index out of range: {}", index), Self::AddOnLeaf => write!(f, "Add on leaf"), Self::AddOnPartialComplete => write!(f, "Add on partial complete"), diff --git a/crates/bdk/src/keys/mod.rs b/crates/bdk/src/keys/mod.rs index 2050c7f8a0..541d439a62 100644 --- a/crates/bdk/src/keys/mod.rs +++ b/crates/bdk/src/keys/mod.rs @@ -413,7 +413,7 @@ impl From for ExtendedKey { /// } /// ``` /// -/// Types that don't internally encode the [`Network`](bitcoin::Network) in which they are valid need some extra +/// Types that don't internally encode the [`Network`] in which they are valid need some extra /// steps to override the set of valid networks, otherwise only the network specified in the /// [`ExtendedPrivKey`] or [`ExtendedPubKey`] will be considered valid. /// diff --git a/crates/bdk/src/wallet/error.rs b/crates/bdk/src/wallet/error.rs index aab6d37762..db58fef06f 100644 --- a/crates/bdk/src/wallet/error.rs +++ b/crates/bdk/src/wallet/error.rs @@ -16,7 +16,7 @@ use crate::descriptor::DescriptorError; use crate::wallet::coin_selection; use crate::{descriptor, FeeRate, KeychainKind}; use alloc::string::String; -use bitcoin::{absolute, psbt, OutPoint, ScriptBuf, Sequence, Txid}; +use bitcoin::{absolute, psbt, OutPoint, Sequence, Txid}; use core::fmt; /// Errors returned by miniscript when updating inconsistent PSBTs @@ -54,7 +54,7 @@ pub enum CreateTxError

{ Persist(P), /// There was a problem while extracting and manipulating policies Policy(PolicyError), - /// Spending policy is not compatible with this [`KeychainKind`](crate::types::KeychainKind) + /// Spending policy is not compatible with this [`KeychainKind`] SpendingPolicyRequired(KeychainKind), /// Requested invalid transaction version '0' Version0, @@ -248,7 +248,7 @@ impl std::error::Error for CreateTxErr #[derive(Debug)] /// Error returned from [`Wallet::build_fee_bump`] /// -/// [`Wallet::build_fee_bump`]: wallet::Wallet::build_fee_bump +/// [`Wallet::build_fee_bump`]: super::Wallet::build_fee_bump pub enum BuildFeeBumpError { /// Happens when trying to spend an UTXO that is not in the internal database UnknownUtxo(OutPoint), @@ -290,94 +290,3 @@ impl fmt::Display for BuildFeeBumpError { #[cfg(feature = "std")] impl std::error::Error for BuildFeeBumpError {} - -#[derive(Debug)] -/// Error returned from [`TxBuilder::add_utxo`] and [`TxBuilder::add_utxos`] -/// -/// [`TxBuilder::add_utxo`]: wallet::tx_builder::TxBuilder::add_utxo -/// [`TxBuilder::add_utxos`]: wallet::tx_builder::TxBuilder::add_utxos -pub enum AddUtxoError { - /// Happens when trying to spend an UTXO that is not in the internal database - UnknownUtxo(OutPoint), -} - -impl fmt::Display for AddUtxoError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::UnknownUtxo(outpoint) => write!( - f, - "UTXO not found in the internal database for txid: {} with vout: {}", - outpoint.txid, outpoint.vout - ), - } - } -} - -#[cfg(feature = "std")] -impl std::error::Error for AddUtxoError {} - -#[derive(Debug)] -/// Error returned from [`TxBuilder::add_foreign_utxo`]. -/// -/// [`TxBuilder::add_foreign_utxo`]: wallet::tx_builder::TxBuilder::add_foreign_utxo -pub enum AddForeignUtxoError { - /// Foreign utxo outpoint txid does not match PSBT input txid - InvalidTxid { - /// PSBT input txid - input_txid: Txid, - /// Foreign UTXO outpoint - foreign_utxo: OutPoint, - }, - /// Requested outpoint doesn't exist in the tx (vout greater than available outputs) - InvalidOutpoint(OutPoint), - /// Foreign utxo missing witness_utxo or non_witness_utxo - MissingUtxo, -} - -impl fmt::Display for AddForeignUtxoError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::InvalidTxid { - input_txid, - foreign_utxo, - } => write!( - f, - "Foreign UTXO outpoint txid: {} does not match PSBT input txid: {}", - foreign_utxo.txid, input_txid, - ), - Self::InvalidOutpoint(outpoint) => write!( - f, - "Requested outpoint doesn't exist for txid: {} with vout: {}", - outpoint.txid, outpoint.vout, - ), - Self::MissingUtxo => write!(f, "Foreign utxo missing witness_utxo or non_witness_utxo"), - } - } -} - -#[cfg(feature = "std")] -impl std::error::Error for AddForeignUtxoError {} - -#[derive(Debug)] -/// Error returned from [`TxBuilder::allow_shrinking`] -/// -/// [`TxBuilder::allow_shrinking`]: wallet::tx_builder::TxBuilder::allow_shrinking -pub enum AllowShrinkingError { - /// Script/PubKey was not in the original transaction - MissingScriptPubKey(ScriptBuf), -} - -impl fmt::Display for AllowShrinkingError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::MissingScriptPubKey(script_buf) => write!( - f, - "Script/PubKey was not in the original transaction: {}", - script_buf, - ), - } - } -} - -#[cfg(feature = "std")] -impl std::error::Error for AllowShrinkingError {} diff --git a/crates/bdk/src/wallet/tx_builder.rs b/crates/bdk/src/wallet/tx_builder.rs index 9d3e44488b..38e90422fd 100644 --- a/crates/bdk/src/wallet/tx_builder.rs +++ b/crates/bdk/src/wallet/tx_builder.rs @@ -45,15 +45,15 @@ use crate::collections::HashSet; use alloc::{boxed::Box, rc::Rc, string::String, vec::Vec}; use bdk_chain::PersistBackend; use core::cell::RefCell; +use core::fmt; use core::marker::PhantomData; use bitcoin::psbt::{self, PartiallySignedTransaction as Psbt}; -use bitcoin::{absolute, script::PushBytes, OutPoint, ScriptBuf, Sequence, Transaction}; +use bitcoin::{absolute, script::PushBytes, OutPoint, ScriptBuf, Sequence, Transaction, Txid}; use super::coin_selection::{CoinSelectionAlgorithm, DefaultCoinSelectionAlgorithm}; use super::ChangeSet; use crate::types::{FeeRate, KeychainKind, LocalUtxo, WeightedUtxo}; -use crate::wallet::error::{AddForeignUtxoError, AddUtxoError, AllowShrinkingError}; use crate::wallet::CreateTxError; use crate::{Utxo, Wallet}; @@ -534,7 +534,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D, /// Choose the coin selection algorithm /// - /// Overrides the [`DefaultCoinSelectionAlgorithm`](super::coin_selection::DefaultCoinSelectionAlgorithm). + /// Overrides the [`DefaultCoinSelectionAlgorithm`]. /// /// 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. pub fn coin_selection( @@ -551,7 +551,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D, /// Finish building the transaction. /// - /// Returns a new [`Psbt`] per [BIP174]. + /// Returns a new [`Psbt`] per [`BIP174`]. /// /// [`BIP174`]: https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki pub fn finish(self) -> Result> @@ -609,6 +609,90 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D, } } +#[derive(Debug)] +/// Error returned from [`TxBuilder::add_utxo`] and [`TxBuilder::add_utxos`] +pub enum AddUtxoError { + /// Happens when trying to spend an UTXO that is not in the internal database + UnknownUtxo(OutPoint), +} + +impl fmt::Display for AddUtxoError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::UnknownUtxo(outpoint) => write!( + f, + "UTXO not found in the internal database for txid: {} with vout: {}", + outpoint.txid, outpoint.vout + ), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for AddUtxoError {} + +#[derive(Debug)] +/// Error returned from [`TxBuilder::add_foreign_utxo`]. +pub enum AddForeignUtxoError { + /// Foreign utxo outpoint txid does not match PSBT input txid + InvalidTxid { + /// PSBT input txid + input_txid: Txid, + /// Foreign UTXO outpoint + foreign_utxo: OutPoint, + }, + /// Requested outpoint doesn't exist in the tx (vout greater than available outputs) + InvalidOutpoint(OutPoint), + /// Foreign utxo missing witness_utxo or non_witness_utxo + MissingUtxo, +} + +impl fmt::Display for AddForeignUtxoError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::InvalidTxid { + input_txid, + foreign_utxo, + } => write!( + f, + "Foreign UTXO outpoint txid: {} does not match PSBT input txid: {}", + foreign_utxo.txid, input_txid, + ), + Self::InvalidOutpoint(outpoint) => write!( + f, + "Requested outpoint doesn't exist for txid: {} with vout: {}", + outpoint.txid, outpoint.vout, + ), + Self::MissingUtxo => write!(f, "Foreign utxo missing witness_utxo or non_witness_utxo"), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for AddForeignUtxoError {} + +#[derive(Debug)] +/// Error returned from [`TxBuilder::allow_shrinking`] +pub enum AllowShrinkingError { + /// Script/PubKey was not in the original transaction + MissingScriptPubKey(ScriptBuf), +} + +impl fmt::Display for AllowShrinkingError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::MissingScriptPubKey(script_buf) => write!( + f, + "Script/PubKey was not in the original transaction: {}", + script_buf, + ), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for AllowShrinkingError {} + impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs, CreateTx> { /// Replace the recipients already added with a new list pub fn set_recipients(&mut self, recipients: Vec<(ScriptBuf, u64)>) -> &mut Self { diff --git a/crates/bdk/tests/wallet.rs b/crates/bdk/tests/wallet.rs index f0e023abaf..1ef4b45e72 100644 --- a/crates/bdk/tests/wallet.rs +++ b/crates/bdk/tests/wallet.rs @@ -3,7 +3,8 @@ use bdk::descriptor::calc_checksum; use bdk::psbt::PsbtUtils; use bdk::signer::{SignOptions, SignerError}; use bdk::wallet::coin_selection::{self, LargestFirstCoinSelection}; -use bdk::wallet::error::{AddForeignUtxoError, CreateTxError}; +use bdk::wallet::error::CreateTxError; +use bdk::wallet::tx_builder::AddForeignUtxoError; use bdk::wallet::AddressIndex::*; use bdk::wallet::{AddressIndex, AddressInfo, Balance, Wallet}; use bdk::{FeeRate, KeychainKind};