Skip to content

Commit

Permalink
chore(examples): use Wallet::new_or_load method where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlinjin committed Oct 30, 2023
1 parent a109be3 commit d3a2f94
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
19 changes: 7 additions & 12 deletions example-crates/wallet_electrum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::io::Write;
use std::str::FromStr;

use bdk::bitcoin::Address;
use bdk::chain::PersistBackend;
use bdk::wallet::Update;
use bdk::SignOptions;
use bdk::{bitcoin::Network, Wallet};
Expand All @@ -19,20 +18,16 @@ use bdk_file_store::Store;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let db_path = std::env::temp_dir().join("bdk-electrum-example");
let mut db = Store::<bdk::wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;
let db = Store::<bdk::wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;
let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";

let mut wallet = if db.is_empty()? {
Wallet::new(
external_descriptor,
Some(internal_descriptor),
db,
Network::Testnet,
)?
} else {
Wallet::load(external_descriptor, Some(internal_descriptor), db)?
};
let mut wallet = Wallet::new_or_load(
external_descriptor,
Some(internal_descriptor),
db,
Network::Testnet,
)?;

let address = wallet.get_address(bdk::wallet::AddressIndex::New);
println!("Generated Address: {}", address);
Expand Down
19 changes: 7 additions & 12 deletions example-crates/wallet_esplora_async/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{io::Write, str::FromStr};

use bdk::{
bitcoin::{Address, Network},
chain::PersistBackend,
wallet::{AddressIndex, Update},
SignOptions, Wallet,
};
Expand All @@ -17,20 +16,16 @@ const PARALLEL_REQUESTS: usize = 5;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let db_path = std::env::temp_dir().join("bdk-esplora-async-example");
let mut db = Store::<bdk::wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;
let db = Store::<bdk::wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;
let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";

let mut wallet = if db.is_empty()? {
Wallet::new(
external_descriptor,
Some(internal_descriptor),
db,
Network::Testnet,
)?
} else {
Wallet::load(external_descriptor, Some(internal_descriptor), db)?
};
let mut wallet = Wallet::new_or_load(
external_descriptor,
Some(internal_descriptor),
db,
Network::Testnet,
)?;

let address = wallet.get_address(AddressIndex::New);
println!("Generated Address: {}", address);
Expand Down
19 changes: 7 additions & 12 deletions example-crates/wallet_esplora_blocking/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::{io::Write, str::FromStr};

use bdk::{
bitcoin::{Address, Network},
chain::PersistBackend,
wallet::{AddressIndex, Update},
SignOptions, Wallet,
};
Expand All @@ -16,20 +15,16 @@ use bdk_file_store::Store;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let db_path = std::env::temp_dir().join("bdk-esplora-example");
let mut db = Store::<bdk::wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;
let db = Store::<bdk::wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;
let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";

let mut wallet = if db.is_empty()? {
Wallet::new(
external_descriptor,
Some(internal_descriptor),
db,
Network::Testnet,
)?
} else {
Wallet::load(external_descriptor, Some(internal_descriptor), db)?
};
let mut wallet = Wallet::new_or_load(
external_descriptor,
Some(internal_descriptor),
db,
Network::Testnet,
)?;

let address = wallet.get_address(AddressIndex::New);
println!("Generated Address: {}", address);
Expand Down

0 comments on commit d3a2f94

Please sign in to comment.