2024-04-25 10:36:06 +08:00
|
|
|
use std::{collections::BTreeSet, io::Write, str::FromStr};
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2024-02-06 08:56:31 -06:00
|
|
|
use bdk_esplora::{esplora_client, EsploraAsyncExt};
|
|
|
|
use bdk_wallet::{
|
2024-04-24 18:12:45 -03:00
|
|
|
bitcoin::{Address, Amount, Network, Script},
|
2024-04-14 10:58:59 -04:00
|
|
|
KeychainKind, SignOptions, Wallet,
|
2023-05-18 14:04:48 +08:00
|
|
|
};
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-09-19 17:29:23 -05:00
|
|
|
use bdk_sqlite::{rusqlite::Connection, Store};
|
|
|
|
|
2024-04-26 11:41:17 -03:00
|
|
|
const SEND_AMOUNT: Amount = Amount::from_sat(5000);
|
2023-05-18 14:04:48 +08:00
|
|
|
const STOP_GAP: usize = 50;
|
|
|
|
const PARALLEL_REQUESTS: usize = 5;
|
2023-03-09 10:59:18 +13:00
|
|
|
|
|
|
|
#[tokio::main]
|
2023-11-16 10:22:37 -06:00
|
|
|
async fn main() -> Result<(), anyhow::Error> {
|
2023-09-19 17:29:23 -05:00
|
|
|
let db_path = "bdk-esplora-async-example.sqlite";
|
|
|
|
let conn = Connection::open(db_path)?;
|
|
|
|
let db = Store::new(conn)?;
|
2023-08-21 16:51:12 -05:00
|
|
|
let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
|
|
|
|
let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-10-30 11:38:20 +08:00
|
|
|
let mut wallet = Wallet::new_or_load(
|
|
|
|
external_descriptor,
|
2024-03-26 21:57:10 -04:00
|
|
|
internal_descriptor,
|
2023-10-30 11:38:20 +08:00
|
|
|
db,
|
2023-09-19 17:29:23 -05:00
|
|
|
Network::Signet,
|
2023-10-30 11:38:20 +08:00
|
|
|
)?;
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2024-04-14 10:58:59 -04:00
|
|
|
let address = wallet.next_unused_address(KeychainKind::External)?;
|
2023-05-18 14:04:48 +08:00
|
|
|
println!("Generated Address: {}", address);
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2024-06-01 17:46:24 -05:00
|
|
|
let balance = wallet.balance();
|
2023-05-18 14:04:48 +08:00
|
|
|
println!("Wallet balance before syncing: {} sats", balance.total());
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-05-18 14:04:48 +08:00
|
|
|
print!("Syncing...");
|
2023-09-19 17:29:23 -05:00
|
|
|
let client = esplora_client::Builder::new("http://signet.bitcoindevkit.net").build_async()?;
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2024-04-25 10:36:06 +08:00
|
|
|
fn generate_inspect(kind: KeychainKind) -> impl FnMut(u32, &Script) + Send + Sync + 'static {
|
|
|
|
let mut once = Some(());
|
|
|
|
let mut stdout = std::io::stdout();
|
|
|
|
move |spk_i, _| {
|
|
|
|
match once.take() {
|
|
|
|
Some(_) => print!("\nScanning keychain [{:?}]", kind),
|
|
|
|
None => print!(" {:<3}", spk_i),
|
|
|
|
};
|
|
|
|
stdout.flush().expect("must flush");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let request = wallet
|
|
|
|
.start_full_scan()
|
|
|
|
.inspect_spks_for_all_keychains({
|
|
|
|
let mut once = BTreeSet::<KeychainKind>::new();
|
|
|
|
move |keychain, spk_i, _| {
|
|
|
|
match once.insert(keychain) {
|
|
|
|
true => print!("\nScanning keychain [{:?}]", keychain),
|
|
|
|
false => print!(" {:<3}", spk_i),
|
|
|
|
}
|
|
|
|
std::io::stdout().flush().expect("must flush")
|
|
|
|
}
|
2023-05-18 14:04:48 +08:00
|
|
|
})
|
2024-04-25 10:36:06 +08:00
|
|
|
.inspect_spks_for_keychain(
|
|
|
|
KeychainKind::External,
|
|
|
|
generate_inspect(KeychainKind::External),
|
|
|
|
)
|
|
|
|
.inspect_spks_for_keychain(
|
|
|
|
KeychainKind::Internal,
|
|
|
|
generate_inspect(KeychainKind::Internal),
|
|
|
|
);
|
2024-04-02 10:19:56 -04:00
|
|
|
|
2024-03-25 13:39:21 +08:00
|
|
|
let mut update = client
|
2024-04-25 10:36:06 +08:00
|
|
|
.full_scan(request, STOP_GAP, PARALLEL_REQUESTS)
|
2024-03-25 13:39:21 +08:00
|
|
|
.await?;
|
2024-04-02 10:19:56 -04:00
|
|
|
let now = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs();
|
2024-04-25 10:36:06 +08:00
|
|
|
let _ = update.graph_update.update_last_seen_unconfirmed(now);
|
2024-03-25 13:39:21 +08:00
|
|
|
|
2023-05-18 14:04:48 +08:00
|
|
|
wallet.apply_update(update)?;
|
|
|
|
wallet.commit()?;
|
2023-07-19 17:42:52 +08:00
|
|
|
println!();
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2024-06-01 17:46:24 -05:00
|
|
|
let balance = wallet.balance();
|
2023-05-18 14:04:48 +08:00
|
|
|
println!("Wallet balance after syncing: {} sats", balance.total());
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2024-04-26 11:41:17 -03:00
|
|
|
if balance.total() < SEND_AMOUNT {
|
2023-05-18 14:04:48 +08:00
|
|
|
println!(
|
|
|
|
"Please send at least {} sats to the receiving address",
|
|
|
|
SEND_AMOUNT
|
|
|
|
);
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-06-26 12:15:09 +02:00
|
|
|
let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")?
|
2023-09-19 17:29:23 -05:00
|
|
|
.require_network(Network::Signet)?;
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-05-18 14:04:48 +08:00
|
|
|
let mut tx_builder = wallet.build_tx();
|
|
|
|
tx_builder
|
|
|
|
.add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
|
|
|
|
.enable_rbf();
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-07-26 19:46:40 -05:00
|
|
|
let mut psbt = tx_builder.finish()?;
|
2023-05-18 14:04:48 +08:00
|
|
|
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
|
|
|
|
assert!(finalized);
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-10-16 19:51:53 +11:00
|
|
|
let tx = psbt.extract_tx()?;
|
2023-05-18 14:04:48 +08:00
|
|
|
client.broadcast(&tx).await?;
|
deps(bdk): bump `bitcoin` to `0.32.0`, miniscript to `12.0.0`
deps(chain): bump `bitcoin` to `0.32.0`, miniscript to `12.0.0`
fix(chain): use `minimal_non_dust()` instead of `dust_value()`
fix(chain): use `compute_txid()` instead of `txid`
deps(testenv): bump `electrsd` to `0.28.0`
deps(electrum): bump `electrum-client` to `0.20.0`
fix(electrum): use `compute_txid()` instead of `txid`
deps(esplora): bump `esplora-client` to `0.8.0`
deps(bitcoind_rpc): bump `bitcoin` to `0.32.0`, `bitcoincore-rpc` to
`0.19.0`
fix(bitcoind_rpc): use `compute_txid()` instead of `txid`
fix(nursery/tmp_plan): use proper `sighash` errors, and fix the expected
`Signature` fields
fix(sqlite): use `compute_txid()` instead of `txid`
deps(hwi): bump `hwi` to `0.9.0`
deps(wallet): bump `bitcoin` to `0.32.0`, miniscript to `12.0.0`
fix(wallet): use `compute_txid()` and `minimal_non_dust()`
- update to use `compute_txid()` instead of deprecated `txid()`
- update to use `minimal_non_dust()` instead of `dust_value()`
- remove unused `bitcoin::hex::FromHex`.
fix(wallet): uses `.into` conversion on `Network` for `NetworkKind`
- uses `.into()` when appropriate, otherwise use the explicit
`NetworkKind`, and it's `.is_mainnet()` method.
fix(wallet): add P2wpkh, Taproot, InputsIndex errors to `SignerError`
fix(wallet): fields on taproot, and ecdsa `Signature` structure
fix(wallet/wallet): convert `Weight` to `usize` for now
- converts the `bitcoin-units::Weight` type to `usize` with help of
`to_wu()` method.
- it should be updated/refactored in the future to handle the `Weight`
type throughout the code instead of current `usize`, only converting
it for now.
- allows the usage of deprecated `is_provably_unspendable()`, needs
further discussion if suggested `is_op_return` is suitable.
- update the expect field to `signature`, as it was renamed from `sig`.
fix(wallet/wallet): use `is_op_return` instead of
`is_provably_unspendable`
fix(wallet/wallet): use `relative::Locktime` instead of `Sequence`
fix(wallet/descriptor): use `ParsePublicKeyError`
fix(wallet/descriptor): use `.into()` to convert from `AbsLockTime` and
`RelLockTime` to `absolute::LockTime` and `relative::LockTime`
fix(wallet/wallet): use `Message::from_digest()` instead of relying on
deprecated `ThirtyTwoByteHash` trait.
fix(wallet/descriptor+wallet): expect `Threshold` type, and handle it
internally
fix(wallet/wallet): remove `0x` prefix from expected `TxId` display
fix(examples): use `compute_txid()` instead of `txid`
fix(ci): remove usage of `bitcoin/no-std` feature
- remove comment: `# The `no-std` feature it's implied when the `std` feature is disabled.`
2024-05-22 18:34:30 -03:00
|
|
|
println!("Tx broadcasted! Txid: {}", tx.compute_txid());
|
2023-05-18 14:04:48 +08:00
|
|
|
|
|
|
|
Ok(())
|
2023-03-09 10:59:18 +13:00
|
|
|
}
|