use std::fs; use bdk::keys::bip39::Mnemonic; use bdk_esplora::{ esplora_client::{self, AsyncClient}, EsploraAsyncExt, }; use bdk_wallet::{ bitcoin::{Address, Amount, Network}, chain::ConfirmationTimeHeightAnchor, keys::{DerivableKey, ExtendedKey}, wallet::AddressInfo, KeychainKind, SignOptions, Wallet, }; use bdk_sqlite::{rusqlite::Connection, Store}; use crate::utils; const STOP_GAP: usize = 50; const PARALLEL_REQUESTS: usize = 5; /// A wallet that uses the Esplora client to interact with the Bitcoin network. pub struct EsploraWallet { client: AsyncClient, db: Store, name: String, wallet: Wallet, } impl EsploraWallet { /// Builds and signs a send transaction to send coins between addresses. /// /// Does NOT send it, you must call `broadcast` to do that. /// /// We could split the creation and signing easily if needed. pub(crate) fn build_and_sign_send_tx( &mut self, recipient: Address, amount: Amount, ) -> Result { let mut tx_builder = self.build_tx()?; tx_builder .add_recipient(recipient.script_pubkey(), amount) .enable_rbf(); let mut psbt = tx_builder.finish()?; let tx = self.sign(&mut psbt, true)?.extract_tx()?; Ok(tx) } pub(crate) fn build_tx( &mut self, ) -> Result< bdk_wallet::TxBuilder, anyhow::Error, > { Ok(self.wallet.build_tx()) } pub(crate) fn sign( &self, psbt: &mut bitcoin::Psbt, finalize: bool, ) -> Result { tracing::info!("{} signing PSBT", self.name); let options = SignOptions { try_finalize: finalize, ..Default::default() }; let finalized = self.wallet.sign(psbt, options)?; // make sure the PSBT is finalized if we asked for it if finalize { assert!(finalized) } Ok(psbt.to_owned()) } /// Syncs the wallet with the latest state of the Bitcoin blockchain pub(crate) async fn sync(&mut self) -> Result<(), anyhow::Error> { tracing::info!("{} full scan sync start", self.name); let request = self.wallet.start_full_scan(); let mut update = self .client .full_scan(request, STOP_GAP, PARALLEL_REQUESTS) .await?; let now = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs(); let _ = update.graph_update.update_last_seen_unconfirmed(now); self.wallet.apply_update(update)?; self.persist_local()?; tracing::info!("{} sync complete", self.name); Ok(()) } fn persist_local(&mut self) -> Result<(), anyhow::Error> { Ok(if let Some(changeset) = self.wallet.take_staged() { self.db.write(&changeset)?; }) } /// Gets the next unused address from the wallet. pub(crate) fn next_unused_address(&mut self) -> Result { let address = self.wallet.next_unused_address(KeychainKind::External); self.persist_local()?; tracing::info!( "Generated address: https://mutinynet.com/address/{}", address ); Ok(address) } /// Returns the balance of the wallet. pub(crate) fn balance(&self) -> bdk_wallet::wallet::Balance { tracing::info!( "{}'s balance is {}", self.name, self.wallet.balance().total() ); self.wallet.balance() } /// Broadcasts a signed transaction to the network. pub(crate) async fn broadcast( &self, tx: &bitcoin::Transaction, ) -> Result<(), esplora_client::Error> { tracing::info!( "{} broadcasting tx https://mutinynet.com/tx/{}", self.name, tx.compute_txid() ); self.client.broadcast(tx).await } pub(crate) fn wallet(&mut self) -> &Wallet { &self.wallet } } /// Creates a Bitcoin descriptor wallet with the mnemonic in the given user directory. pub(crate) fn create_wallet(name: &str, network: Network) -> anyhow::Result { let keys_dir = utils::home(name); let mnemonic_path = crate::utils::side_paths(keys_dir).1; // TODO: this tuple stinks let mnemonic_words = fs::read_to_string(mnemonic_path).expect("couldn't read bitcoin key file"); tracing::info!("Creating {name}'s wallet from mnemonic: {mnemonic_words}"); let mnemonic = Mnemonic::parse(mnemonic_words).unwrap(); // Generate the extended key let xkey: ExtendedKey = mnemonic .into_extended_key() .expect("couldn't turn mnemonic into xkey"); let xprv = xkey .into_xprv(Network::Signet) .expect("problem converting xkey to xprv") .to_string(); tracing::info!("Setting up esplora database for {name}"); let db_path = format!("/tmp/{name}-bdk-esplora-async-example.sqlite"); let conn = Connection::open(db_path)?; let mut db = Store::new(conn)?; let external_descriptor = format!("wpkh({xprv}/84'/1'/0'/0/*)"); let internal_descriptor = format!("wpkh({xprv}/84'/1'/0'/1/*)"); let changeset = db.read().expect("couldn't read esplora database"); let wallet = Wallet::new_or_load( &external_descriptor, &internal_descriptor, changeset, network, ) .expect("problem setting up wallet"); let client = esplora_client::Builder::new("https://mutinynet.com/api") .build_async() .expect("couldn't build esplora client"); let esplora = EsploraWallet { name: name.to_string(), wallet, db, client, }; Ok(esplora) }