Skip to content

Commit

Permalink
esplora: add EsploraClient
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Oct 16, 2023
1 parent 0a96ab6 commit 92961de
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bdk-ffi"
version = "1.0.0-alpha.1"
version = "1.0.0-alpha.2"
authors = ["Steve Myers <[email protected]>", "Sudarsan Balaji <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand Down
11 changes: 11 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -144,13 +150,18 @@ interface Wallet {
Balance get_balance();

Network network();

[Throws=BdkError]
void apply_update(Update update);
};

enum WalletType {
"Memory",
"FlatFile",
};

interface Update {};

interface Address {
[Throws=BdkError]
constructor(string address, Network network);
Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
59 changes: 59 additions & 0 deletions bdk-ffi/src/esplora.rs
Original file line number Diff line number Diff line change
@@ -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<Wallet>,
stop_gap: u64,
parallel_requests: u64,
) -> Result<Arc<Update>, 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();
}
4 changes: 4 additions & 0 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -306,8 +307,8 @@ pub enum WalletType {
FlatFile,
}

pub(crate) struct Wallet {
pub(crate) inner_mutex: Mutex<BdkWallet>,
pub struct Wallet {
pub inner_mutex: Mutex<BdkWallet>,
}

impl Wallet {
Expand Down Expand Up @@ -357,4 +358,13 @@ impl Wallet {
pub fn network(&self) -> Network {
self.get_wallet().network().into()
}

pub fn apply_update(&self, update: Arc<Update>) -> 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);

0 comments on commit 92961de

Please sign in to comment.