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

@@ -8,10 +8,14 @@ edition = "2021"
[dependencies]
anyhow = "1.0.86"
async-trait = "0.1.52"
bdk = { version = "0.29.0", default-feature = false, features = ["all-keys"] }
bdk_esplora = "0.15.0"
bdk_sqlite = "0.2.0"
bdk_wallet = { version = "1.0.0-alpha.13", features = ["all-keys"] }
bdk = { version = "0.29.0", features = [
"compiler",
"use-esplora-async",
"electrum",
"std",
"keys-bip39",
"reqwest-default-tls",
], default-features = false }
bft-json-crdt = { path = "../crates/bft-json-crdt" }
bft-crdt-derive = { path = "../crates/bft-json-crdt/bft-crdt-derive" }
bitcoin = { version = "0.32.2", features = ["rand"] }
@@ -29,7 +33,7 @@ sha256 = "1.5.0"
tokio = { version = "1.37.0", features = ["full"] }
toml = "0.8.14"
tracing = "0.1"
tracing-subscriber = {version = "0.3", features = ["std", "env-filter"]}
tracing-subscriber = { version = "0.3", features = ["std", "env-filter"] }
[dev-dependencies]

View File

@@ -1,101 +0,0 @@
use crate::{bitcoin, utils};
use bdk::bitcoin::psbt::PartiallySignedTransaction;
use bdk::wallet::AddressIndex::New;
use bdk::wallet::{AddressIndex, AddressInfo};
use bdk::{
bitcoin::Network, database::MemoryDatabase, keys::ExtendedKey, template::Bip84, KeychainKind,
Wallet,
};
use bdk::{blockchain::ElectrumBlockchain, electrum_client, SyncOptions};
use bdk::{FeeRate, SignOptions, TransactionDetails};
/// DEPRECATED
///
/// This is a bdk example that uses the Electrum client to interact with the Bitcoin network.
/// Electrum is a light client that connects to a server to get information about the Bitcoin network.
/// The BDK itself does not have the ability to connect to e.g. esplora servers. As the Blockstream Electrum Signet
/// server does not appear to be picking up transactions properly at the moment, I've shifted over to using
/// the (more complex) `bdk_wallet` crate and the esplora client there (see the other bitcoin client).
///
/// Note:the types below are all completely different than the types in `bdk_wallet`.
pub async fn run() -> Result<(), anyhow::Error> {
let dave = utils::home(&"dave".to_string());
let sammy = utils::home(&"sammy".to_string());
let dave_key = bitcoin::keys::load_from_file(&dave).unwrap();
let sammy_key = bitcoin::keys::load_from_file(&sammy).unwrap();
let dave_wallet = bitcoin::clients::electrum::create_wallet(dave_key)?;
let sammy_wallet = bitcoin::clients::electrum::create_wallet(sammy_key)?;
let dave_address = dave_wallet.get_address(AddressIndex::Peek(0))?.to_string();
let sammy_address = sammy_wallet.get_address(AddressIndex::Peek(0))?.to_string();
println!("Dave's address: {}", dave_address);
println!("Sammy's address: {}", sammy_address);
let blockchain = ElectrumBlockchain::from(electrum_client::Client::new(
"ssl://electrum.blockstream.info:60002",
)?);
println!("Syncing...");
dave_wallet.sync(&blockchain, SyncOptions::default())?;
display_balance(&dave_wallet);
display_balance(&sammy_wallet);
let (mut psbt, details) =
build_sending_tx(&dave_wallet, sammy_wallet.get_address(New)?).expect("psbt build error");
println!("About to sign the transaction: {:?}", details);
dave_wallet.sign(&mut psbt, SignOptions::default())?;
let _signed_tx = psbt.extract_tx();
// println!("Broadcasting...");
// blockchain.broadcast(&signed_tx).expect("broadcast error");
// println!("Transaction ID: {:?}", signed_tx.txid());
Ok(())
}
/// Create a BDK wallet using BIP 84 descriptor ("m/84h/1h/0h/0" and "m/84h/1h/0h/1")
pub fn create_wallet(xkey: ExtendedKey) -> anyhow::Result<Wallet<MemoryDatabase>> {
let xprv = xkey
.into_xprv(Network::Testnet)
.expect("couldn't turn xkey into xprv");
let external_descriptor = Bip84(xprv, KeychainKind::External);
let internal_descriptor = Some(Bip84(xprv, KeychainKind::Internal));
let wallet = Wallet::new(
external_descriptor,
internal_descriptor,
Network::Testnet,
MemoryDatabase::default(),
)?;
Ok(wallet)
}
fn display_balance(wallet: &Wallet<MemoryDatabase>) {
println!(
"Wallet balance for {} after syncing: {:?} sats on network {}",
wallet
.get_address(bdk::wallet::AddressIndex::Peek(0))
.expect("couldn't get address"),
wallet.get_balance().expect("couldn't show balance"),
wallet.network(),
);
}
fn build_sending_tx(
wallet: &Wallet<MemoryDatabase>,
recipient: AddressInfo,
) -> anyhow::Result<(PartiallySignedTransaction, TransactionDetails), anyhow::Error> {
let mut builder = wallet.build_tx();
builder
.add_recipient(recipient.script_pubkey(), 1000)
.enable_rbf()
.do_not_spend_change()
.fee_rate(FeeRate::from_sat_per_vb(7.0));
Ok(builder.finish()?)
}

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)
}

View File

@@ -1,3 +1 @@
// Re-enable this if we want to use alongside a fullnode.
pub mod electrum;
pub mod esplora;

View File

@@ -1,5 +1,6 @@
use crate::bitcoin::clients;
use bdk_wallet::bitcoin::{Amount, Network};
use bdk::bitcoin::Network;
use bdk::wallet::AddressIndex;
use bdk::SignOptions;
use tracing_subscriber::filter::EnvFilter;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{fmt, layer::SubscriberExt};
@@ -7,52 +8,53 @@ use tracing_subscriber::{fmt, layer::SubscriberExt};
use super::clients::esplora::EsploraWallet;
pub(crate) async fn simple_transfer() -> Result<(), anyhow::Error> {
let (mut dave, mut sammy) = setup().await?;
let (mut dave, sammy) = setup().await?;
let send_amount = Amount::from_sat(500);
ensure_enough_sats(&dave, send_amount);
let send_amount = 500;
let _ = ensure_enough_sats(&dave, send_amount);
let sammy_address = sammy.next_unused_address()?.address;
let sammy_address = sammy.wallet.get_address(AddressIndex::New)?;
let tx = dave.build_and_sign_send_tx(sammy_address, send_amount)?;
dave.broadcast(&tx).await?;
dave.broadcast(&tx)?;
Ok(())
}
async fn setup() -> Result<(EsploraWallet, EsploraWallet), anyhow::Error> {
tracing_setup();
let mut dave = clients::esplora::create_wallet("dave", Network::Signet)?;
let mut sammy = clients::esplora::create_wallet("sammy", Network::Signet)?;
let _ = dave.balance();
dave.sync().await?;
let _ = dave.balance();
let _sammy = sammy.balance();
sammy.sync().await?;
let _ = sammy.balance();
let dave = EsploraWallet::create_wallet("dave", Network::Signet)?;
let sammy = EsploraWallet::create_wallet("sammy", Network::Signet)?;
let _ = dave.wallet.get_balance();
dave.sync()?;
let _ = dave.wallet.get_balance();
let _sammy = sammy.wallet.get_balance();
sammy.sync()?;
let _ = sammy.wallet.get_balance();
Ok((dave, sammy))
}
/// Exit if the wallet does not have enough sats to send.
fn ensure_enough_sats(wallet: &EsploraWallet, send_amount: bitcoin::Amount) {
if wallet.balance().total() < send_amount {
fn ensure_enough_sats(wallet: &EsploraWallet, send_amount: u64) -> anyhow::Result<()> {
if wallet.wallet.get_balance()?.get_total() < send_amount {
tracing::error!(
"Please send at least {} sats to the receiving address. Exiting.",
send_amount
);
std::process::exit(0);
}
Ok(())
}
pub(crate) async fn htlc() -> anyhow::Result<()> {
let (mut dave, mut sammy) = setup().await?;
let (dave, sammy) = setup().await?;
// format a new commitment transaction like in Lightning
let mut commitment_builder = dave.build_tx()?;
let amount = Amount::from_sat(500);
let recipient = sammy.next_unused_address()?.script_pubkey();
let mut commitment_builder = dave.wallet.build_tx();
let amount = 500;
let recipient = sammy.wallet.get_address(AddressIndex::New)?.script_pubkey();
commitment_builder.add_recipient(recipient, amount);
commitment_builder.enable_rbf();
let psbt = commitment_builder
let (psbt, _) = commitment_builder
.finish()
.expect("unable to build commitment");
@@ -65,17 +67,17 @@ pub(crate) async fn htlc() -> anyhow::Result<()> {
.expect("problem combining bitcoin PSBTs"); // these guys love mutability
let finalized = dave
.wallet()
.finalize_psbt(&mut dave_psbt, bdk_wallet::SignOptions::default())
.wallet
.finalize_psbt(&mut dave_psbt, SignOptions::default())
.expect("couldn't finalize");
assert!(finalized);
let tx = dave_psbt.extract_tx()?;
let tx = dave_psbt.extract_tx();
let _ = dave.broadcast(&tx).await.expect("couldn't broadcast");
let _ = dave.broadcast(&tx)?;
let _ = sammy.sync();
sammy.balance();
let _ = sammy.wallet.get_balance();
Ok(())
}

View File

@@ -1,7 +1,6 @@
use std::str::FromStr;
use bdk_wallet::miniscript::descriptor::Wsh;
use bdk_wallet::miniscript::policy::{self, Concrete, Liftable};
use bdk::miniscript::{descriptor::Wsh, policy::Concrete};
use bitcoin::Address;
/// A hash time locked contract between two parties.
@@ -35,8 +34,8 @@ impl Htlc {
}
}
pub(crate) fn to_miniscript_policy(&self) -> policy::Concrete<bitcoin::PublicKey> {
Concrete::<bitcoin::PublicKey>::from_str(&format!(
pub(crate) fn to_miniscript_policy(&self) -> Concrete<bdk::bitcoin::PublicKey> {
Concrete::<bdk::bitcoin::PublicKey>::from_str(&format!(
"or(10@and(sha256({secret_hash}),pk({redeem_identity})),1@and(older({expiry}),pk({refund_identity})))",
secret_hash = self.hashlock,
redeem_identity = self.redeem_identity,
@@ -45,7 +44,7 @@ impl Htlc {
)).expect("Policy compilation only fails on resource limits or mixed timelocks")
}
pub(crate) fn to_miniscript_descriptor(&self) -> Wsh<bitcoin::PublicKey> {
pub(crate) fn to_miniscript_descriptor(&self) -> Wsh<bdk::bitcoin::PublicKey> {
Wsh::new(
self.to_miniscript_policy()
.compile()

View File

@@ -45,8 +45,9 @@ async fn setup(name: &String) -> SideNode {
// First, load up the keys and create a bft-bft-crdt
let side_dir = utils::home(name);
let bft_crdt_keys = bft_crdt::keys::load_from_file(&side_dir);
let keys = bitcoin::keys::load_from_file(&side_dir).unwrap();
let bitcoin_wallet = bitcoin::clients::electrum::create_wallet(keys).unwrap();
// let keys = bitcoin::keys::load_from_file(&side_dir).unwrap();
// let bitcoin_wallet =
// bitcoin::clients::esplora::EsploraWallet::create_wallet(name, keys).unwrap();
let crdt = BaseCrdt::<TransactionList>::new(&bft_crdt_keys);
// Channels for internal communication, and a tokio task for stdin input
@@ -61,7 +62,7 @@ async fn setup(name: &String) -> SideNode {
let node = SideNode::new(
crdt,
bft_crdt_keys,
bitcoin_wallet,
// bitcoin_wallet,
incoming_receiver,
stdin_receiver,
handle,

View File

@@ -1,4 +1,3 @@
use bdk::database::MemoryDatabase;
use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp};
use fastcrypto::ed25519::Ed25519KeyPair;
use tokio::sync::mpsc;
@@ -8,7 +7,7 @@ use crate::{bft_crdt::websocket::Client, bft_crdt::TransactionList, utils};
pub struct SideNode {
crdt: BaseCrdt<TransactionList>,
bft_crdt_keys: fastcrypto::ed25519::Ed25519KeyPair,
_bitcoin_wallet: bdk::Wallet<MemoryDatabase>, // currently not read anywhere
// _bitcoin_wallet: bdk::Wallet<MemoryDatabase>, // currently not read anywhere
incoming_receiver: mpsc::Receiver<SignedOp>,
stdin_receiver: std::sync::mpsc::Receiver<String>,
handle: ezsockets::Client<Client>,
@@ -18,7 +17,7 @@ impl SideNode {
pub fn new(
crdt: BaseCrdt<TransactionList>,
bft_crdt_keys: Ed25519KeyPair,
bitcoin_wallet: bdk::Wallet<MemoryDatabase>,
// bitcoin_wallet: bdk::Wallet<MemoryDatabase>,
incoming_receiver: mpsc::Receiver<SignedOp>,
stdin_receiver: std::sync::mpsc::Receiver<String>,
handle: ezsockets::Client<Client>,
@@ -26,7 +25,7 @@ impl SideNode {
let node = Self {
crdt,
bft_crdt_keys,
_bitcoin_wallet: bitcoin_wallet,
// _bitcoin_wallet: bitcoin_wallet,
incoming_receiver,
stdin_receiver,
handle,

View File

@@ -2,9 +2,7 @@ use bft_json_crdt::{
json_crdt::{BaseCrdt, SignedOp},
keypair::make_keypair,
};
use side_node::{
bft_crdt::websocket::Client, bft_crdt::TransactionList, bitcoin, node::SideNode, utils,
};
use side_node::{bft_crdt::websocket::Client, bft_crdt::TransactionList, node::SideNode, utils};
use tokio::sync::mpsc;
#[tokio::test]
@@ -37,9 +35,9 @@ async fn test_distribute_via_websockets() {
async fn setup(_: &str) -> SideNode {
// First, load up the keys and create a bft-bft-crdt
let bft_crdt_keys = make_keypair();
let mnemonic_words = bitcoin::keys::make_mnemonic();
let keys = bitcoin::keys::get(mnemonic_words).unwrap();
let bitcoin_wallet = bitcoin::clients::electrum::create_wallet(keys).unwrap();
// let mnemonic_words = bitcoin::keys::make_mnemonic();
// let keys = bitcoin::keys::get(mnemonic_words).unwrap();
// let bitcoin_wallet = bitcoin::clients::electrum::create_wallet(keys).unwrap();
let crdt = BaseCrdt::<TransactionList>::new(&bft_crdt_keys);
// Channels for internal communication, and a tokio task for stdin input
@@ -51,7 +49,7 @@ async fn setup(_: &str) -> SideNode {
let node = SideNode::new(
crdt,
bft_crdt_keys,
bitcoin_wallet,
// bitcoin_wallet,
incoming_receiver,
stdin_receiver,
handle,