Skip to content

Commit 46d39be

Browse files
committed
Merge #1028: Add CreateTxError and use as error type for TxBuilder::finish()
00ec19e ci: fix MSRV pinning for rustls 0.21.9 (Steve Myers) 77f9977 feat(wallet): Add infallible Wallet get_address(), get_internal_address functions (Steve Myers) 9e7d99e refactor(bdk)!: add context specific error types, remove top level error mod (Steve Myers) Pull request description: ### Description To remove some places where there were `.expect("TODO")` I added a new `CreateTxError` type which is returned from `TxBuilder::finish()`. I also updated related tests and doc tests. Fixes #996 (comment) Also added fallible `Wallet::try_get_address()` and `try_get_internal_address()` to return `Result` with a possible `D:WriteError` when a PersistBackend is used. This should fix #996. I removed catch-all bdk::Error and replaced usages with new types and updated related functions, fixes #994. ### Notes to the reviewers ~~I didn't add all possible bdk::Error types that `Wallet::create_tx()` and `TxBuilder::finish()` functions might throw. It's probably not too much more work but will take a bit more research so I want to make sure this is the right general approach first.~~ I added `anyhow` to the dev-dependencies so I could remove some `.expect()` lines from the docs tests and make the examples closer to what an end user should do. I also used the `anyhow!()` macro to replace a few places that were using the `bdk::Error::Generic` in example code. I also moved the module level error.rs file to wallet/error.rs so no one would be tempted to make any new catch all errors and to make it clear that all the errors in it are wallet module related. ### Changelog notice Changed - Updated bdk module to use new context specific error types - wallet: MiniscriptPsbtError, CreateTxError, BuildFeeBumpError error enums - coin_selection: module Error enum - Renamed fallible Wallet address functions to try_get_address() and try_get_internal_address() Removed - Removed catch-all top level bdk::Error enum - Removed impl_error macro Added - Added infallible Wallet get_address(), get_internal_address functions ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### Bugfixes: * [x] This pull request breaks the existing API * [ ] I've added tests to reproduce the issue which are now passing * [ ] I'm linking the issue being fixed by this PR Top commit has no ACKs. Tree-SHA512: a87c0856d71f9c945d12b6de6d368f49bd62d73886ac46ac83d00ddb81f2c38c5233ba053e40c76dea73ee7bfc19dac510eec5d7c9026ae50a2dc7308ac4786f
2 parents cc552c5 + 00ec19e commit 46d39be

File tree

22 files changed

+720
-369
lines changed

22 files changed

+720
-369
lines changed

.github/workflows/cont_integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
run: |
3333
cargo update -p log --precise "0.4.18"
3434
cargo update -p tempfile --precise "3.6.0"
35-
cargo update -p rustls:0.21.8 --precise "0.21.1"
35+
cargo update -p rustls:0.21.9 --precise "0.21.1"
3636
cargo update -p rustls:0.20.9 --precise "0.20.8"
3737
cargo update -p tokio --precise "1.29.1"
3838
cargo update -p tokio-util --precise "0.7.8"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ cargo update -p log --precise "0.4.18"
7070
# tempfile 3.7.0 has MSRV 1.63.0+
7171
cargo update -p tempfile --precise "3.6.0"
7272
# rustls 0.21.7 has MSRV 1.60.0+
73-
cargo update -p rustls:0.21.8 --precise "0.21.1"
73+
cargo update -p rustls:0.21.9 --precise "0.21.1"
7474
# rustls 0.20.9 has MSRV 1.60.0+
7575
cargo update -p rustls:0.20.9 --precise "0.20.8"
7676
# tokio 1.33 has MSRV 1.63.0+

crates/bdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ env_logger = "0.7"
4949
assert_matches = "1.5.0"
5050
tempfile = "3"
5151
bdk_file_store = { path = "../file_store" }
52+
anyhow = "1"
5253

5354
[package.metadata.docs.rs]
5455
all-features = true

crates/bdk/examples/mnemonic_to_descriptors.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// You may not use this file except in accordance with one or both of these
77
// licenses.
88

9+
use anyhow::anyhow;
910
use bdk::bitcoin::bip32::DerivationPath;
1011
use bdk::bitcoin::secp256k1::Secp256k1;
1112
use bdk::bitcoin::Network;
@@ -14,21 +15,19 @@ use bdk::descriptor::IntoWalletDescriptor;
1415
use bdk::keys::bip39::{Language, Mnemonic, WordCount};
1516
use bdk::keys::{GeneratableKey, GeneratedKey};
1617
use bdk::miniscript::Tap;
17-
use bdk::Error as BDK_Error;
18-
use std::error::Error;
1918
use std::str::FromStr;
2019

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

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

3332
println!("Mnemonic phrase: {}", *mnemonic);
3433
let mnemonic_with_passphrase = (mnemonic, None);

crates/bdk/src/descriptor/error.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// licenses.
1111

1212
//! Descriptor errors
13-
1413
use core::fmt;
1514

1615
/// Errors related to the parsing and usage of descriptors
@@ -87,9 +86,38 @@ impl fmt::Display for Error {
8786
#[cfg(feature = "std")]
8887
impl std::error::Error for Error {}
8988

90-
impl_error!(bitcoin::bip32::Error, Bip32);
91-
impl_error!(bitcoin::base58::Error, Base58);
92-
impl_error!(bitcoin::key::Error, Pk);
93-
impl_error!(miniscript::Error, Miniscript);
94-
impl_error!(bitcoin::hashes::hex::Error, Hex);
95-
impl_error!(crate::descriptor::policy::PolicyError, Policy);
89+
impl From<bitcoin::bip32::Error> for Error {
90+
fn from(err: bitcoin::bip32::Error) -> Self {
91+
Error::Bip32(err)
92+
}
93+
}
94+
95+
impl From<bitcoin::base58::Error> for Error {
96+
fn from(err: bitcoin::base58::Error) -> Self {
97+
Error::Base58(err)
98+
}
99+
}
100+
101+
impl From<bitcoin::key::Error> for Error {
102+
fn from(err: bitcoin::key::Error) -> Self {
103+
Error::Pk(err)
104+
}
105+
}
106+
107+
impl From<miniscript::Error> for Error {
108+
fn from(err: miniscript::Error) -> Self {
109+
Error::Miniscript(err)
110+
}
111+
}
112+
113+
impl From<bitcoin::hashes::hex::Error> for Error {
114+
fn from(err: bitcoin::hashes::hex::Error) -> Self {
115+
Error::Hex(err)
116+
}
117+
}
118+
119+
impl From<crate::descriptor::policy::PolicyError> for Error {
120+
fn from(err: crate::descriptor::policy::PolicyError) -> Self {
121+
Error::Policy(err)
122+
}
123+
}

crates/bdk/src/descriptor/policy.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
//! let signers = Arc::new(SignersContainer::build(key_map, &extended_desc, &secp));
3434
//! let policy = extended_desc.extract_policy(&signers, BuildSatisfaction::None, &secp)?;
3535
//! println!("policy: {}", serde_json::to_string(&policy).unwrap());
36-
//! # Ok::<(), bdk::Error>(())
36+
//! # Ok::<(), anyhow::Error>(())
3737
//! ```
3838
3939
use crate::collections::{BTreeMap, HashSet, VecDeque};
4040
use alloc::string::String;
4141
use alloc::vec::Vec;
4242
use core::cmp::max;
43+
4344
use core::fmt;
4445

4546
use serde::ser::SerializeMap;

crates/bdk/src/error.rs

Lines changed: 0 additions & 201 deletions
This file was deleted.

crates/bdk/src/keys/mod.rs

Lines changed: 12 additions & 3 deletions
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
///
@@ -932,8 +932,17 @@ pub enum KeyError {
932932
Miniscript(miniscript::Error),
933933
}
934934

935-
impl_error!(miniscript::Error, Miniscript, KeyError);
936-
impl_error!(bitcoin::bip32::Error, Bip32, KeyError);
935+
impl From<miniscript::Error> for KeyError {
936+
fn from(err: miniscript::Error) -> Self {
937+
KeyError::Miniscript(err)
938+
}
939+
}
940+
941+
impl From<bip32::Error> for KeyError {
942+
fn from(err: bip32::Error) -> Self {
943+
KeyError::Bip32(err)
944+
}
945+
}
937946

938947
impl fmt::Display for KeyError {
939948
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

crates/bdk/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ extern crate serde_json;
2727
#[cfg(feature = "keys-bip39")]
2828
extern crate bip39;
2929

30-
#[allow(unused_imports)]
31-
#[macro_use]
32-
pub(crate) mod error;
3330
pub mod descriptor;
3431
pub mod keys;
3532
pub mod psbt;
@@ -38,7 +35,6 @@ pub mod wallet;
3835

3936
pub use descriptor::template;
4037
pub use descriptor::HdKeyPaths;
41-
pub use error::Error;
4238
pub use types::*;
4339
pub use wallet::signer;
4440
pub use wallet::signer::SignOptions;

0 commit comments

Comments
 (0)