chore(examples): use Wallet::new_or_load method where appropriate

This commit is contained in:
志宇 committed 2023-11-16 07:07:48 +08:00
1 parent 24994a3ed4
commit 1886dc4fe7
3 files changed
+21 -35

No files matched your search

@@ -2,7 +2,6 @@ use std::{io::Write, str::FromStr};
use bdk::{
bitcoin::{Address, Network},
chain::PersistBackend,
wallet::{AddressIndex, Update},
SignOptions, Wallet,
};
@@ -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);