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.
feat!: improve wallet building methods
Browse files Browse the repository at this point in the history
Remove returning `Result` for builder methods on `CreateParams` and
`LoadParams`.
evanlinjin committed Jul 18, 2024
1 parent 6f049ee commit 1d7ddfe
Showing 15 changed files with 287 additions and 297 deletions.
5 changes: 2 additions & 3 deletions crates/hwi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
//! # use bdk_wallet::descriptor::Descriptor;
//! # use bdk_wallet::signer::SignerOrdering;
//! # use bdk_hwi::HWISigner;
//! # use bdk_wallet::{KeychainKind, SignOptions};
//! # use bdk_wallet::{KeychainKind, SignOptions, Wallet};
//! # use hwi::HWIClient;
//! # use std::sync::Arc;
//! # use std::str::FromStr;
@@ -20,8 +20,7 @@
//! let first_device = devices.remove(0)?;
//! let custom_signer = HWISigner::from_device(&first_device, Network::Testnet.into())?;
//!
//! # let mut wallet = bdk_wallet::CreateParams::new("", "", Network::Testnet)?
//! # .create_wallet_no_persist()?;
//! # let mut wallet = Wallet::create("", "").network(Network::Testnet).create_wallet_no_persist()?;
//! #
//! // Adding the hardware signer to the BDK wallet
//! wallet.add_signer(
18 changes: 11 additions & 7 deletions crates/wallet/README.md
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ To persist `Wallet` state data use a data store crate that reads and writes [`Ch

<!-- compile_fail because outpoint and txout are fake variables -->
```rust,no_run
use bdk_wallet::{bitcoin::Network, CreateParams, LoadParams, KeychainKind, ChangeSet};
use bdk_wallet::{bitcoin::Network, KeychainKind, ChangeSet, Wallet};
// Open or create a new file store for wallet data.
let mut db =
@@ -78,13 +78,17 @@ let mut db =
let network = Network::Testnet;
let descriptor = "wpkh(tprv8ZgxMBicQKsPdcAqYBpzAFwU5yxBUo88ggoBqu1qPcHUfSbKK1sKMLmC7EAk438btHQrSdu3jGGQa6PA71nvH5nkDexhLteJqkM4dQmWF9g/84'/1'/0'/0/*)";
let change_descriptor = "wpkh(tprv8ZgxMBicQKsPdcAqYBpzAFwU5yxBUo88ggoBqu1qPcHUfSbKK1sKMLmC7EAk438btHQrSdu3jGGQa6PA71nvH5nkDexhLteJqkM4dQmWF9g/84'/1'/0'/1/*)";
let load_params = LoadParams::with_descriptors(descriptor, change_descriptor, network)
.expect("must parse descriptors");
let create_params = CreateParams::new(descriptor, change_descriptor, network)
.expect("must parse descriptors");
let mut wallet = match load_params.load_wallet(&mut db).expect("wallet") {
let wallet_opt = Wallet::load()
.descriptors(descriptor, change_descriptor)
.network(network)
.load_wallet(&mut db)
.expect("wallet");
let mut wallet = match wallet_opt {
Some(wallet) => wallet,
None => create_params.create_wallet(&mut db).expect("wallet"),
None => Wallet::create(descriptor, change_descriptor)
.network(network)
.create_wallet(&mut db)
.expect("wallet"),
};
// Get a new address to receive bitcoin.
5 changes: 3 additions & 2 deletions crates/wallet/examples/compiler.rs
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ use bitcoin::Network;
use miniscript::policy::Concrete;
use miniscript::Descriptor;

use bdk_wallet::{CreateParams, KeychainKind};
use bdk_wallet::{KeychainKind, Wallet};

/// Miniscript policy is a high level abstraction of spending conditions. Defined in the
/// rust-miniscript library here https://docs.rs/miniscript/7.0.0/miniscript/policy/index.html
@@ -77,7 +77,8 @@ fn main() -> Result<(), Box<dyn Error>> {
);

// Create a new wallet from descriptors
let mut wallet = CreateParams::new(&descriptor, &internal_descriptor, Network::Regtest)?
let mut wallet = Wallet::create(descriptor, internal_descriptor)
.network(Network::Regtest)
.create_wallet_no_persist()?;

println!(
10 changes: 10 additions & 0 deletions crates/wallet/src/descriptor/mod.rs
Original file line number Diff line number Diff line change
@@ -112,6 +112,16 @@ impl IntoWalletDescriptor for &String {
}
}

impl IntoWalletDescriptor for String {
fn into_wallet_descriptor(
self,
secp: &SecpCtx,
network: Network,
) -> Result<(ExtendedDescriptor, KeyMap), DescriptorError> {
self.as_str().into_wallet_descriptor(secp, network)
}
}

impl IntoWalletDescriptor for ExtendedDescriptor {
fn into_wallet_descriptor(
self,
Loading

0 comments on commit 1d7ddfe

Please sign in to comment.