From: Daniela Brozzoni Date: Tue, 22 Aug 2023 12:03:47 +0000 (+0200) Subject: refactor: Rename `wallet_esplora` to... X-Git-Tag: v1.0.0-alpha.2~22^2 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/struct.CommandStringError.html?a=commitdiff_plain;h=2263a58448f5f04a74e076132aea93f21c584bc0;p=bdk refactor: Rename `wallet_esplora` to... ...`wallet_esplora_blocking` --- diff --git a/Cargo.toml b/Cargo.toml index c5f2692d..9fafb8b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ members = [ "example-crates/example_cli", "example-crates/example_electrum", "example-crates/wallet_electrum", - "example-crates/wallet_esplora", + "example-crates/wallet_esplora_blocking", "example-crates/wallet_esplora_async", "nursery/tmp_plan", "nursery/coin_select" diff --git a/README.md b/README.md index 4edb716d..ae230abb 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ The project is split up into several crates in the `/crates` directory: Fully working examples of how to use these components are in `/example-crates`: - [`example_cli`](./example-crates/example_cli): Library used by the `example_*` crates. Provides utilities for syncing, showing the balance, generating addresses and creating transactions without using the bdk `Wallet`. - [`example_electrum`](./example-crates/example_electrum): A command line Bitcoin wallet application built on top of `example_cli` and the `electrum` crate. It shows the power of the bdk tools (`chain` + `file_store` + `electrum`), without depending on the main `bdk` library. -- [`wallet_esplora`](./example-crates/wallet_esplora): Uses the `Wallet` to sync and spend using the Esplora blocking interface. +- [`wallet_esplora_blocking`](./example-crates/wallet_esplora_blocking): Uses the `Wallet` to sync and spend using the Esplora blocking interface. - [`wallet_esplora_async`](./example-crates/wallet_esplora_async): Uses the `Wallet` to sync and spend using the Esplora asynchronous interface. - [`wallet_electrum`](./example-crates/wallet_electrum): Uses the `Wallet` to sync and spend using Electrum. diff --git a/example-crates/wallet_esplora/Cargo.toml b/example-crates/wallet_esplora/Cargo.toml deleted file mode 100644 index 1dcef389..00000000 --- a/example-crates/wallet_esplora/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "wallet_esplora" -version = "0.2.0" -edition = "2021" -publish = false - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -bdk = { path = "../../crates/bdk" } -bdk_esplora = { path = "../../crates/esplora", features = ["blocking"] } -bdk_file_store = { path = "../../crates/file_store" } diff --git a/example-crates/wallet_esplora/src/main.rs b/example-crates/wallet_esplora/src/main.rs deleted file mode 100644 index a9691f36..00000000 --- a/example-crates/wallet_esplora/src/main.rs +++ /dev/null @@ -1,99 +0,0 @@ -const DB_MAGIC: &str = "bdk_wallet_esplora_example"; -const SEND_AMOUNT: u64 = 1000; -const STOP_GAP: usize = 5; -const PARALLEL_REQUESTS: usize = 1; - -use std::{io::Write, str::FromStr}; - -use bdk::{ - bitcoin::{Address, Network}, - chain::keychain::WalletUpdate, - wallet::AddressIndex, - SignOptions, Wallet, -}; -use bdk_esplora::{esplora_client, EsploraExt}; -use bdk_file_store::Store; - -fn main() -> Result<(), Box> { - let db_path = std::env::temp_dir().join("bdk-esplora-example"); - let db = Store::::new_from_path(DB_MAGIC.as_bytes(), db_path)?; - let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/0/*)"; - let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/1/*)"; - - let mut wallet = Wallet::new( - external_descriptor, - Some(internal_descriptor), - db, - Network::Testnet, - )?; - - let address = wallet.get_address(AddressIndex::New); - println!("Generated Address: {}", address); - - let balance = wallet.get_balance(); - println!("Wallet balance before syncing: {} sats", balance.total()); - - print!("Syncing..."); - let client = - esplora_client::Builder::new("https://blockstream.info/testnet/api").build_blocking()?; - - let prev_tip = wallet.latest_checkpoint(); - let keychain_spks = wallet - .spks_of_all_keychains() - .into_iter() - .map(|(k, k_spks)| { - let mut once = Some(()); - let mut stdout = std::io::stdout(); - let k_spks = k_spks - .inspect(move |(spk_i, _)| match once.take() { - Some(_) => print!("\nScanning keychain [{:?}]", k), - None => print!(" {:<3}", spk_i), - }) - .inspect(move |_| stdout.flush().expect("must flush")); - (k, k_spks) - }) - .collect(); - - let (update_graph, last_active_indices) = - client.update_tx_graph(keychain_spks, None, None, STOP_GAP, PARALLEL_REQUESTS)?; - let missing_heights = wallet.tx_graph().missing_heights(wallet.local_chain()); - let chain_update = client.update_local_chain(prev_tip, missing_heights)?; - let update = WalletUpdate { - last_active_indices, - graph: update_graph, - ..WalletUpdate::new(chain_update) - }; - - wallet.apply_update(update)?; - wallet.commit()?; - println!(); - - let balance = wallet.get_balance(); - println!("Wallet balance after syncing: {} sats", balance.total()); - - if balance.total() < SEND_AMOUNT { - println!( - "Please send at least {} sats to the receiving address", - SEND_AMOUNT - ); - std::process::exit(0); - } - - let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")? - .require_network(Network::Testnet)?; - - let mut tx_builder = wallet.build_tx(); - tx_builder - .add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT) - .enable_rbf(); - - let (mut psbt, _) = tx_builder.finish()?; - let finalized = wallet.sign(&mut psbt, SignOptions::default())?; - assert!(finalized); - - let tx = psbt.extract_tx(); - client.broadcast(&tx)?; - println!("Tx broadcasted! Txid: {}", tx.txid()); - - Ok(()) -} diff --git a/example-crates/wallet_esplora_blocking/Cargo.toml b/example-crates/wallet_esplora_blocking/Cargo.toml new file mode 100644 index 00000000..f07f64e4 --- /dev/null +++ b/example-crates/wallet_esplora_blocking/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "wallet_esplora_blocking" +version = "0.2.0" +edition = "2021" +publish = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bdk = { path = "../../crates/bdk" } +bdk_esplora = { path = "../../crates/esplora", features = ["blocking"] } +bdk_file_store = { path = "../../crates/file_store" } diff --git a/example-crates/wallet_esplora_blocking/src/main.rs b/example-crates/wallet_esplora_blocking/src/main.rs new file mode 100644 index 00000000..a9691f36 --- /dev/null +++ b/example-crates/wallet_esplora_blocking/src/main.rs @@ -0,0 +1,99 @@ +const DB_MAGIC: &str = "bdk_wallet_esplora_example"; +const SEND_AMOUNT: u64 = 1000; +const STOP_GAP: usize = 5; +const PARALLEL_REQUESTS: usize = 1; + +use std::{io::Write, str::FromStr}; + +use bdk::{ + bitcoin::{Address, Network}, + chain::keychain::WalletUpdate, + wallet::AddressIndex, + SignOptions, Wallet, +}; +use bdk_esplora::{esplora_client, EsploraExt}; +use bdk_file_store::Store; + +fn main() -> Result<(), Box> { + let db_path = std::env::temp_dir().join("bdk-esplora-example"); + let db = Store::::new_from_path(DB_MAGIC.as_bytes(), db_path)?; + let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/0/*)"; + let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/1/*)"; + + let mut wallet = Wallet::new( + external_descriptor, + Some(internal_descriptor), + db, + Network::Testnet, + )?; + + let address = wallet.get_address(AddressIndex::New); + println!("Generated Address: {}", address); + + let balance = wallet.get_balance(); + println!("Wallet balance before syncing: {} sats", balance.total()); + + print!("Syncing..."); + let client = + esplora_client::Builder::new("https://blockstream.info/testnet/api").build_blocking()?; + + let prev_tip = wallet.latest_checkpoint(); + let keychain_spks = wallet + .spks_of_all_keychains() + .into_iter() + .map(|(k, k_spks)| { + let mut once = Some(()); + let mut stdout = std::io::stdout(); + let k_spks = k_spks + .inspect(move |(spk_i, _)| match once.take() { + Some(_) => print!("\nScanning keychain [{:?}]", k), + None => print!(" {:<3}", spk_i), + }) + .inspect(move |_| stdout.flush().expect("must flush")); + (k, k_spks) + }) + .collect(); + + let (update_graph, last_active_indices) = + client.update_tx_graph(keychain_spks, None, None, STOP_GAP, PARALLEL_REQUESTS)?; + let missing_heights = wallet.tx_graph().missing_heights(wallet.local_chain()); + let chain_update = client.update_local_chain(prev_tip, missing_heights)?; + let update = WalletUpdate { + last_active_indices, + graph: update_graph, + ..WalletUpdate::new(chain_update) + }; + + wallet.apply_update(update)?; + wallet.commit()?; + println!(); + + let balance = wallet.get_balance(); + println!("Wallet balance after syncing: {} sats", balance.total()); + + if balance.total() < SEND_AMOUNT { + println!( + "Please send at least {} sats to the receiving address", + SEND_AMOUNT + ); + std::process::exit(0); + } + + let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")? + .require_network(Network::Testnet)?; + + let mut tx_builder = wallet.build_tx(); + tx_builder + .add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT) + .enable_rbf(); + + let (mut psbt, _) = tx_builder.finish()?; + let finalized = wallet.sign(&mut psbt, SignOptions::default())?; + assert!(finalized); + + let tx = psbt.extract_tx(); + client.broadcast(&tx)?; + println!("Tx broadcasted! Txid: {}", tx.txid()); + + Ok(()) +}