Skip to content

Commit

Permalink
refactor(bdk)!: add context specific error types, remove top level er…
Browse files Browse the repository at this point in the history
…ror mod

refactor(bdk)!: remove impl_error macro
refactor(wallet)!: add MiniscriptPsbtError, CreateTxError, BuildFeeBumpError error enums
refactor(coin_selection)!: add module Error enum
test(bdk): use anyhow dev-dependency for all tests
  • Loading branch information
notmandatory committed Nov 16, 2023
1 parent cc552c5 commit 9e7d99e
Show file tree
Hide file tree
Showing 20 changed files with 678 additions and 360 deletions.
1 change: 1 addition & 0 deletions crates/bdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ env_logger = "0.7"
assert_matches = "1.5.0"
tempfile = "3"
bdk_file_store = { path = "../file_store" }
anyhow = "1"

[package.metadata.docs.rs]
all-features = true
Expand Down
7 changes: 3 additions & 4 deletions crates/bdk/examples/mnemonic_to_descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// You may not use this file except in accordance with one or both of these
// licenses.

use anyhow::anyhow;
use bdk::bitcoin::bip32::DerivationPath;
use bdk::bitcoin::secp256k1::Secp256k1;
use bdk::bitcoin::Network;
Expand All @@ -14,21 +15,19 @@ use bdk::descriptor::IntoWalletDescriptor;
use bdk::keys::bip39::{Language, Mnemonic, WordCount};
use bdk::keys::{GeneratableKey, GeneratedKey};
use bdk::miniscript::Tap;
use bdk::Error as BDK_Error;
use std::error::Error;
use std::str::FromStr;

/// This example demonstrates how to generate a mnemonic phrase
/// using BDK and use that to generate a descriptor string.
fn main() -> Result<(), Box<dyn Error>> {
fn main() -> Result<(), anyhow::Error> {
let secp = Secp256k1::new();

// In this example we are generating a 12 words mnemonic phrase
// but it is also possible generate 15, 18, 21 and 24 words
// using their respective `WordCount` variant.
let mnemonic: GeneratedKey<_, Tap> =
Mnemonic::generate((WordCount::Words12, Language::English))
.map_err(|_| BDK_Error::Generic("Mnemonic generation error".to_string()))?;
.map_err(|_| anyhow!("Mnemonic generation error"))?;

println!("Mnemonic phrase: {}", *mnemonic);
let mnemonic_with_passphrase = (mnemonic, None);
Expand Down
42 changes: 35 additions & 7 deletions crates/bdk/src/descriptor/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
// licenses.

//! Descriptor errors
use core::fmt;

/// Errors related to the parsing and usage of descriptors
Expand Down Expand Up @@ -87,9 +86,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)
}
}
3 changes: 2 additions & 1 deletion crates/bdk/src/descriptor/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@
//! let signers = Arc::new(SignersContainer::build(key_map, &extended_desc, &secp));
//! let policy = extended_desc.extract_policy(&signers, BuildSatisfaction::None, &secp)?;
//! println!("policy: {}", serde_json::to_string(&policy).unwrap());
//! # Ok::<(), bdk::Error>(())
//! # Ok::<(), anyhow::Error>(())
//! ```
use crate::collections::{BTreeMap, HashSet, VecDeque};
use alloc::string::String;
use alloc::vec::Vec;
use core::cmp::max;

use core::fmt;

use serde::ser::SerializeMap;
Expand Down
201 changes: 0 additions & 201 deletions crates/bdk/src/error.rs

This file was deleted.

15 changes: 12 additions & 3 deletions crates/bdk/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ impl<Ctx: ScriptContext> From<bip32::ExtendedPrivKey> for ExtendedKey<Ctx> {
/// }
/// ```
///
/// 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.
///
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions crates/bdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ extern crate serde_json;
#[cfg(feature = "keys-bip39")]
extern crate bip39;

#[allow(unused_imports)]
#[macro_use]
pub(crate) mod error;
pub mod descriptor;
pub mod keys;
pub mod psbt;
Expand All @@ -38,7 +35,6 @@ pub mod wallet;

pub use descriptor::template;
pub use descriptor::HdKeyPaths;
pub use error::Error;
pub use types::*;
pub use wallet::signer;
pub use wallet::signer::SignOptions;
Expand Down
Loading

0 comments on commit 9e7d99e

Please sign in to comment.