Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(wallet)!: move wallet errors to wallet::error module, remove…
Browse files Browse the repository at this point in the history
… impl_error macro
notmandatory committed Oct 25, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent c4d6505 commit e6354ea
Showing 8 changed files with 57 additions and 35 deletions.
41 changes: 35 additions & 6 deletions crates/bdk/src/descriptor/error.rs
Original file line number Diff line number Diff line change
@@ -87,9 +87,38 @@ impl fmt::Display for Error {
#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl_error!(bitcoin::bip32::Error, Bip32);
impl_error!(bitcoin::base58::Error, Base58);
impl_error!(bitcoin::key::Error, Pk);
impl_error!(miniscript::Error, Miniscript);
impl_error!(bitcoin::hashes::hex::Error, Hex);
impl_error!(crate::descriptor::policy::PolicyError, Policy);
impl From<bitcoin::bip32::Error> for Error {
fn from(err: bitcoin::bip32::Error) -> Self {
Error::Bip32(err)
}
}

impl From<bitcoin::base58::Error> for Error {
fn from(err: bitcoin::base58::Error) -> Self {
Error::Base58(err)
}
}

impl From<bitcoin::key::Error> for Error {
fn from(err: bitcoin::key::Error) -> Self {
Error::Pk(err)
}
}

impl From<miniscript::Error> for Error {
fn from(err: miniscript::Error) -> Self {
Error::Miniscript(err)
}
}

impl From<bitcoin::hashes::hex::Error> for Error {
fn from(err: bitcoin::hashes::hex::Error) -> Self {
Error::Hex(err)
}
}

impl From<crate::descriptor::policy::PolicyError> for Error {
fn from(err: crate::descriptor::policy::PolicyError) -> Self {
Error::Policy(err)
}
}
13 changes: 11 additions & 2 deletions crates/bdk/src/keys/mod.rs
Original file line number Diff line number Diff line change
@@ -932,8 +932,17 @@ pub enum KeyError {
Miniscript(miniscript::Error),
}

impl_error!(miniscript::Error, Miniscript, KeyError);
impl_error!(bitcoin::bip32::Error, Bip32, KeyError);
impl From<miniscript::Error> for KeyError {
fn from(err: miniscript::Error) -> Self {
KeyError::Miniscript(err)
}
}

impl From<bip32::Error> for KeyError {
fn from(err: bip32::Error) -> Self {
KeyError::Bip32(err)
}
}

impl fmt::Display for KeyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2 changes: 0 additions & 2 deletions crates/bdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -27,8 +27,6 @@ extern crate serde_json;
#[cfg(feature = "keys-bip39")]
extern crate bip39;

#[macro_use]
pub mod error;
pub mod descriptor;
pub mod keys;
pub mod psbt;
2 changes: 1 addition & 1 deletion crates/bdk/src/wallet/coin_selection.rs
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
//! # use std::str::FromStr;
//! # use bitcoin::*;
//! # use bdk::wallet::{self, ChangeSet, coin_selection::*, coin_selection};
//! # use bdk::error::CreateTxError;
//! # use bdk::wallet::error::CreateTxError;
//! # use bdk_chain::PersistBackend;
//! # use bdk::*;
//! # use bdk::wallet::coin_selection::decide_change;
15 changes: 0 additions & 15 deletions crates/bdk/src/error.rs → crates/bdk/src/wallet/error.rs
Original file line number Diff line number Diff line change
@@ -43,19 +43,6 @@ impl fmt::Display for MiniscriptPsbtError {
#[cfg(feature = "std")]
impl std::error::Error for MiniscriptPsbtError {}

macro_rules! impl_error {
( $from:ty, $to:ident ) => {
impl_error!($from, $to, Error);
};
( $from:ty, $to:ident, $impl_for:ty ) => {
impl core::convert::From<$from> for $impl_for {
fn from(err: $from) -> Self {
<$impl_for>::$to(err)
}
}
};
}

#[derive(Debug)]
/// Error returned from [`TxBuilder::finish`]
///
@@ -259,8 +246,6 @@ impl<P> From<coin_selection::Error> for CreateTxError<P> {
#[cfg(feature = "std")]
impl<P: core::fmt::Display + core::fmt::Debug> std::error::Error for CreateTxError<P> {}

//

#[derive(Debug)]
/// Error returned from [`Wallet::build_fee_bump`]
///
9 changes: 5 additions & 4 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@ pub mod signer;
pub mod tx_builder;
pub(crate) mod utils;

pub mod error;
#[cfg(feature = "hardware-signer")]
#[cfg_attr(docsrs, doc(cfg(feature = "hardware-signer")))]
pub mod hardwaresigner;
@@ -69,11 +70,11 @@ use crate::descriptor::{
calc_checksum, into_wallet_descriptor_checked, DerivedDescriptor, DescriptorMeta,
ExtendedDescriptor, ExtractPolicy, IntoWalletDescriptor, Policy, XKeyUtils,
};
use crate::error::{BuildFeeBumpError, CreateTxError, MiniscriptPsbtError, SignError};
use crate::psbt::PsbtUtils;
use crate::signer::SignerError;
use crate::types::*;
use crate::wallet::coin_selection::Excess::{Change, NoChange};
use crate::wallet::error::{BuildFeeBumpError, CreateTxError, MiniscriptPsbtError, SignError};

const COINBASE_MATURITY: u32 = 100;

@@ -854,7 +855,7 @@ impl<D> Wallet<D> {
/// # use bitcoin::*;
/// # use bdk::*;
/// # use bdk::wallet::ChangeSet;
/// # use bdk::error::CreateTxError;
/// # use bdk::wallet::error::CreateTxError;
/// # use bdk_chain::PersistBackend;
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
/// # let mut wallet = doctest_wallet!();
@@ -1254,7 +1255,7 @@ impl<D> Wallet<D> {
/// # use bitcoin::*;
/// # use bdk::*;
/// # use bdk::wallet::ChangeSet;
/// # use bdk::error::CreateTxError;
/// # use bdk::wallet::error::CreateTxError;
/// # use bdk_chain::PersistBackend;
/// # use anyhow::Error;
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
@@ -1428,7 +1429,7 @@ impl<D> Wallet<D> {
/// # use bitcoin::*;
/// # use bdk::*;
/// # use bdk::wallet::ChangeSet;
/// # use bdk::error::{CreateTxError, SignError};
/// # use bdk::wallet::error::{CreateTxError, SignError};
/// # use bdk_chain::PersistBackend;
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
/// # let mut wallet = doctest_wallet!();
8 changes: 4 additions & 4 deletions crates/bdk/src/wallet/tx_builder.rs
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
//! # use bitcoin::*;
//! # use bdk::*;
//! # use bdk::wallet::ChangeSet;
//! # use bdk::error::CreateTxError;
//! # use bdk::wallet::error::CreateTxError;
//! # use bdk::wallet::tx_builder::CreateTx;
//! # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
//! # use bdk_chain::PersistBackend;
@@ -51,8 +51,8 @@ use bitcoin::{absolute, script::PushBytes, OutPoint, ScriptBuf, Sequence, Transa

use super::coin_selection::{CoinSelectionAlgorithm, DefaultCoinSelectionAlgorithm};
use super::ChangeSet;
use crate::error::{AddForeignUtxoError, AddUtxoError, AllowShrinkingError};
use crate::types::{FeeRate, KeychainKind, LocalUtxo, WeightedUtxo};
use crate::wallet::error::{AddForeignUtxoError, AddUtxoError, AllowShrinkingError};
use crate::wallet::CreateTxError;
use crate::{Utxo, Wallet};

@@ -85,7 +85,7 @@ impl TxBuilderContext for BumpFee {}
/// # use bitcoin::*;
/// # use core::str::FromStr;
/// # use bdk::wallet::ChangeSet;
/// # use bdk::error::CreateTxError;
/// # use bdk::wallet::error::CreateTxError;
/// # use bdk_chain::PersistBackend;
/// # let mut wallet = doctest_wallet!();
/// # let addr1 = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
@@ -652,7 +652,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs, CreateTx> {
/// # use bitcoin::*;
/// # use bdk::*;
/// # use bdk::wallet::ChangeSet;
/// # use bdk::error::CreateTxError;
/// # use bdk::wallet::error::CreateTxError;
/// # use bdk::wallet::tx_builder::CreateTx;
/// # use bdk_chain::PersistBackend;
/// # let to_address =
2 changes: 1 addition & 1 deletion crates/bdk/tests/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use assert_matches::assert_matches;
use bdk::descriptor::calc_checksum;
use bdk::error::{AddForeignUtxoError, CreateTxError, SignError};
use bdk::psbt::PsbtUtils;
use bdk::signer::{SignOptions, SignerError};
use bdk::wallet::coin_selection::{self, LargestFirstCoinSelection};
use bdk::wallet::error::{AddForeignUtxoError, CreateTxError, SignError};
use bdk::wallet::AddressIndex::*;
use bdk::wallet::{AddressIndex, AddressInfo, Balance, Wallet};
use bdk::{FeeRate, KeychainKind};

0 comments on commit e6354ea

Please sign in to comment.