Major refactor of the Esplora client

Now we're back to using regular bdk types, which is a big advantage.
This commit is contained in:
Dave Hrycyszyn
2024-07-25 19:54:37 +01:00
parent e4e8298fcd
commit 4b63245bfe
11 changed files with 133 additions and 536 deletions

View File

@@ -1,34 +1,35 @@
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},
use bdk::{
bitcoin::{psbt::PartiallySignedTransaction, Network, Transaction},
blockchain::EsploraBlockchain,
database::MemoryDatabase,
keys::{bip39::Mnemonic, DerivableKey, ExtendedKey},
template::Bip84,
wallet::AddressInfo,
KeychainKind, SignOptions, Wallet,
KeychainKind, SignOptions, SyncOptions, 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<KeychainKind, ConfirmationTimeHeightAnchor>,
pub(crate) blockchain: bdk::blockchain::EsploraBlockchain,
name: String,
wallet: Wallet,
pub(crate) wallet: Wallet<MemoryDatabase>,
}
impl EsploraWallet {
pub(crate) fn sync(&self) -> anyhow::Result<()> {
self.wallet.sync(&self.blockchain, SyncOptions::default())?;
Ok(())
}
pub(crate) fn broadcast(&self, tx: &Transaction) -> anyhow::Result<()> {
let _ = self.blockchain.broadcast(&tx);
Ok(())
}
/// Builds and signs a send transaction to send coins between addresses.
///
/// Does NOT send it, you must call `broadcast` to do that.
@@ -36,32 +37,23 @@ impl EsploraWallet {
/// 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<bitcoin::Transaction, anyhow::Error> {
let mut tx_builder = self.build_tx()?;
recipient: AddressInfo,
amount: u64,
) -> Result<bdk::bitcoin::Transaction, anyhow::Error> {
let mut tx_builder = self.wallet.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()?;
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<bdk_wallet::wallet::coin_selection::BranchAndBoundCoinSelection>,
anyhow::Error,
> {
Ok(self.wallet.build_tx())
}
pub(crate) fn sign(
&self,
psbt: &mut bitcoin::Psbt,
psbt: &mut PartiallySignedTransaction,
finalize: bool,
) -> Result<bitcoin::Psbt, anyhow::Error> {
) -> Result<PartiallySignedTransaction, anyhow::Error> {
tracing::info!("{} signing PSBT", self.name);
let options = SignOptions {
@@ -78,114 +70,41 @@ impl EsploraWallet {
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(())
}
/// Creates a Bitcoin descriptor wallet with the mnemonic in the given user directory.
pub(crate) fn create_wallet(name: &str, network: Network) -> anyhow::Result<EsploraWallet> {
let keys_dir = utils::home(name);
fn persist_local(&mut self) -> Result<(), anyhow::Error> {
Ok(if let Some(changeset) = self.wallet.take_staged() {
self.db.write(&changeset)?;
})
}
let mnemonic_path = crate::utils::side_paths(keys_dir).1; // TODO: this tuple stinks
let words = fs::read_to_string(mnemonic_path).expect("couldn't read bitcoin key file");
/// Gets the next unused address from the wallet.
pub(crate) fn next_unused_address(&mut self) -> Result<AddressInfo, anyhow::Error> {
let address = self.wallet.next_unused_address(KeychainKind::External);
self.persist_local()?;
tracing::info!(
"Generated address: https://mutinynet.com/address/{}",
address
);
Ok(address)
}
tracing::info!("Creating {name}'s wallet from mnemonic: {words}");
let xkey: ExtendedKey = Mnemonic::parse(words)
.expect("couldn't parse mnemonic words")
.into_extended_key()
.expect("couldn't turn mnemonic into extended key");
/// 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()
}
let xprv = xkey
.into_xprv(Network::Testnet)
.expect("couldn't turn xkey into xprv");
/// 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
}
let external_descriptor = Bip84(xprv, KeychainKind::External);
let internal_descriptor = Some(Bip84(xprv, KeychainKind::Internal));
pub(crate) fn wallet(&mut self) -> &Wallet {
&self.wallet
let wallet = Wallet::new(
external_descriptor,
internal_descriptor,
network,
MemoryDatabase::default(),
)?;
let blockchain = EsploraBlockchain::new("https://mutinynet.com/api", 20);
let esplora = EsploraWallet {
name: name.to_string(),
wallet,
blockchain,
};
Ok(esplora)
}
}
/// Creates a Bitcoin descriptor wallet with the mnemonic in the given user directory.
pub(crate) fn create_wallet(name: &str, network: Network) -> anyhow::Result<EsploraWallet> {
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)
}