From e3bd99e84e082915ecb74d6468b47f18579f9c47 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 16 Jan 2024 13:32:05 -0600 Subject: [PATCH] feat: add sync to esplora client --- bdk-ffi/Cargo.lock | 38 ++++++++++++++------------------------ bdk-ffi/Cargo.toml | 3 ++- bdk-ffi/src/error.rs | 4 ++++ bdk-ffi/src/esplora.rs | 37 ++++++++++++++++++++++++++++++++++++- 4 files changed, 56 insertions(+), 26 deletions(-) diff --git a/bdk-ffi/Cargo.lock b/bdk-ffi/Cargo.lock index e8df64f6..695105a5 100644 --- a/bdk-ffi/Cargo.lock +++ b/bdk-ffi/Cargo.lock @@ -142,11 +142,9 @@ dependencies = [ [[package]] name = "bdk" version = "1.0.0-alpha.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83a517d8f5d91c70973a587518c3daf47b7e030aece66dd3d93a169936d3bd61" +source = "git+https://github.com/notmandatory/bdk.git?branch=feat/wallet_start_update#614f2b688e38216fa5e155e7e45e0061238334bb" dependencies = [ - "bdk_chain", - "bip39", + "bdk_chain 0.7.0 (git+https://github.com/notmandatory/bdk.git?branch=feat/wallet_start_update)", "bitcoin", "getrandom", "js-sys", @@ -171,6 +169,15 @@ name = "bdk_chain" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1db83c5c6484e00d3e43a3691c6f405b15211cd80691e3b801b79077b8275277" +dependencies = [ + "bitcoin", + "miniscript", +] + +[[package]] +name = "bdk_chain" +version = "0.7.0" +source = "git+https://github.com/notmandatory/bdk.git?branch=feat/wallet_start_update#614f2b688e38216fa5e155e7e45e0061238334bb" dependencies = [ "bitcoin", "miniscript", @@ -183,7 +190,7 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67e79cd13295983f25b5424c4245d7c8a894bcf2d789e9b3f8e57f60ceccdadd" dependencies = [ - "bdk_chain", + "bdk_chain 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "esplora-client", ] @@ -202,17 +209,6 @@ dependencies = [ "serde", ] -[[package]] -name = "bip39" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93f2635620bf0b9d4576eb7bb9a38a55df78bd1205d26fa994b25911a69f212f" -dependencies = [ - "bitcoin_hashes 0.11.0", - "serde", - "unicode-normalization", -] - [[package]] name = "bitcoin" version = "0.30.1" @@ -222,7 +218,7 @@ dependencies = [ "base64 0.13.1", "bech32", "bitcoin-private", - "bitcoin_hashes 0.12.0", + "bitcoin_hashes", "hex_lit", "secp256k1", "serde", @@ -240,12 +236,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73290177011694f38ec25e165d0387ab7ea749a4b81cd4c80dae5988229f7a57" -[[package]] -name = "bitcoin_hashes" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90064b8dee6815a6470d60bad07bbbaee885c0e12d04177138fa3291a01b7bc4" - [[package]] name = "bitcoin_hashes" version = "0.12.0" @@ -848,7 +838,7 @@ version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f" dependencies = [ - "bitcoin_hashes 0.12.0", + "bitcoin_hashes", "rand", "secp256k1-sys", "serde", diff --git a/bdk-ffi/Cargo.toml b/bdk-ffi/Cargo.toml index a39d5692..9b477477 100644 --- a/bdk-ffi/Cargo.toml +++ b/bdk-ffi/Cargo.toml @@ -18,7 +18,8 @@ path = "uniffi-bindgen.rs" default = ["uniffi/cli"] [dependencies] -bdk = { version = "1.0.0-alpha.3", features = ["all-keys", "keys-bip39"] } +bdk = { git = "https://github.com/notmandatory/bdk.git", branch = "feat/wallet_start_update" } +# bdk = { version = "1.0.0-alpha.3", features = ["all-keys", "keys-bip39"] } bdk_esplora = { version = "0.5.0", default-features = false, features = ["std", "blocking"] } # TODO 22: The bdk_esplora crate uses esplora_client which uses reqwest for async. By default it uses the system diff --git a/bdk-ffi/src/error.rs b/bdk-ffi/src/error.rs index d19fcebd..b8a3f048 100644 --- a/bdk-ffi/src/error.rs +++ b/bdk-ffi/src/error.rs @@ -67,6 +67,10 @@ impl From> for Alpha3Error { } } +impl From for Alpha3Error { + fn from(err: bdk_esplora::esplora_client::Error) -> Self { Alpha3Error::Generic } +} + impl From> for Alpha3Error { fn from(_: CreateTxError) -> Self { Alpha3Error::Generic diff --git a/bdk-ffi/src/esplora.rs b/bdk-ffi/src/esplora.rs index 73e4a2be..905c01b2 100644 --- a/bdk-ffi/src/esplora.rs +++ b/bdk-ffi/src/esplora.rs @@ -50,7 +50,42 @@ impl EsploraClient { Ok(Arc::new(Update(update))) } - // pub fn sync(); + pub fn sync( + &self, + wallet: Arc, + parallel_requests: u64, + ) -> Result, Alpha3Error> { + let wallet = wallet.get_wallet(); + + // 1. Get data required to do a wallet sync + let sync_request = wallet.sync_request(true); + + // 2. Sync wallet SPKs (addresses), unconfirmed transactions, and UTXOs + let graph_update = self.0.sync( + sync_request.spks, + sync_request.txids, + sync_request.outpoints, + parallel_requests as usize, + ).map_err(Alpha3Error::from)?; + + // 3. From wallet transactions determine missing blockchain heights + let missing_heights = graph_update.missing_heights(wallet.local_chain()); + + // 4. Get blockchain update from the original request checkpoint and missing heights + let chain_update = self.0.update_local_chain( + sync_request.checkpoint, + missing_heights, + ).map_err(Alpha3Error::from)?; + + // 5. Create and return the wallet update + let update = BdkUpdate { + graph: graph_update, + chain: Some(chain_update), + ..BdkUpdate::default() + }; + + Ok(Arc::new(Update(update))) + } pub fn broadcast(&self, transaction: &Transaction) -> Result<(), Alpha3Error> { let bdk_transaction: BdkTransaction = transaction.into();