diff --git a/Cargo.lock b/Cargo.lock index c421243b..aa23018a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -114,7 +114,7 @@ dependencies = [ [[package]] name = "bdk-ffi" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha.2" dependencies = [ "assert_matches", "bdk", diff --git a/bdk-ffi/Cargo.toml b/bdk-ffi/Cargo.toml index 09d0cac4..865c860e 100644 --- a/bdk-ffi/Cargo.toml +++ b/bdk-ffi/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bdk-ffi" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha.2" authors = ["Steve Myers ", "Sudarsan Balaji "] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index 921283fe..6d536499 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -134,6 +134,12 @@ interface Descriptor { string as_string_private(); }; +interface EsploraClient { + constructor(string url); + + [Throws=BdkError] + Update scan(Wallet wallet, u64 stop_gap, u64 parallel_requests); +}; interface Wallet { [Throws=BdkError] @@ -144,6 +150,9 @@ interface Wallet { Balance get_balance(); Network network(); + + [Throws=BdkError] + void apply_update(Update update); }; enum WalletType { @@ -151,6 +160,8 @@ enum WalletType { "FlatFile", }; +interface Update {}; + interface Address { [Throws=BdkError] constructor(string address, Network network); diff --git a/bdk-ffi/src/descriptor.rs b/bdk-ffi/src/descriptor.rs index 61f75753..d3ec5835 100644 --- a/bdk-ffi/src/descriptor.rs +++ b/bdk-ffi/src/descriptor.rs @@ -17,7 +17,7 @@ use std::str::FromStr; use std::sync::Arc; #[derive(Debug)] -pub(crate) struct Descriptor { +pub struct Descriptor { pub extended_descriptor: ExtendedDescriptor, pub key_map: KeyMap, } diff --git a/bdk-ffi/src/esplora.rs b/bdk-ffi/src/esplora.rs new file mode 100644 index 00000000..b100e98a --- /dev/null +++ b/bdk-ffi/src/esplora.rs @@ -0,0 +1,59 @@ +use crate::wallet::Update; +use crate::wallet::Wallet; +use bdk::wallet::Update as BdkUpdate; +use bdk::Error as BdkError; +use bdk_esplora::esplora_client::{BlockingClient, Builder}; +use bdk_esplora::EsploraExt; +use std::sync::Arc; + +pub struct EsploraClient(BlockingClient); + +impl EsploraClient { + pub fn new(url: String) -> Self { + let client = Builder::new(url.as_str()).build_blocking().unwrap(); + Self(client) + } + + pub fn scan( + &self, + wallet: Arc, + stop_gap: u64, + parallel_requests: u64, + ) -> Result, BdkError> { + let wallet = wallet.get_wallet(); + + let previous_tip = wallet.latest_checkpoint(); + let keychain_spks = wallet.spks_of_all_keychains().into_iter().collect(); + + let (update_graph, last_active_indices) = self + .0 + .scan_txs_with_keychains( + keychain_spks, + None, + None, + stop_gap as usize, + parallel_requests as usize, + ) + .unwrap(); + + let missing_heights = update_graph.missing_heights(wallet.local_chain()); + let chain_update = self + .0 + .update_local_chain(previous_tip, missing_heights) + .unwrap(); + + let update = BdkUpdate { + last_active_indices, + graph: update_graph, + chain: Some(chain_update), + }; + + Ok(Arc::new(Update(update))) + } + + // pub fn sync(); + + // pub fn broadcast(); + + // pub fn estimate_fee(); +} \ No newline at end of file diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index 4189daa1..a1bab788 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -48,11 +48,15 @@ mod keys; mod psbt; mod wallet; +mod esplora; + use crate::descriptor::Descriptor; use crate::keys::DerivationPath; use crate::keys::DescriptorPublicKey; use crate::keys::DescriptorSecretKey; use crate::keys::Mnemonic; +use crate::esplora::EsploraClient; +use crate::wallet::Update; use bdk::keys::bip39::WordCount; use bdk::Error as BdkError; use bdk::KeychainKind; diff --git a/bdk-ffi/src/wallet.rs b/bdk-ffi/src/wallet.rs index ababc7a6..3eb9cb54 100644 --- a/bdk-ffi/src/wallet.rs +++ b/bdk-ffi/src/wallet.rs @@ -298,6 +298,7 @@ use crate::descriptor::Descriptor; use crate::Balance; use crate::{AddressIndex, AddressInfo, Network}; +use bdk::wallet::Update as BdkUpdate; use bdk::Error as BdkError; use bdk::Wallet as BdkWallet; use std::sync::{Arc, Mutex, MutexGuard}; @@ -306,8 +307,8 @@ pub enum WalletType { FlatFile, } -pub(crate) struct Wallet { - pub(crate) inner_mutex: Mutex, +pub struct Wallet { + pub inner_mutex: Mutex, } impl Wallet { @@ -357,4 +358,13 @@ impl Wallet { pub fn network(&self) -> Network { self.get_wallet().network().into() } + + pub fn apply_update(&self, update: Arc) -> Result<(), BdkError> { + // self.get_wallet(). .apply_update(update.0).map_err(|e| BdkError::Generic(e.to_string())) + self.get_wallet() + .apply_update(update.0.clone()) + .map_err(|e| BdkError::Generic(e.to_string())) + } } + +pub struct Update(pub(crate) BdkUpdate); \ No newline at end of file