Skip to content

Commit

Permalink
feat(wallet): add scan method on blocking esplora client
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Oct 25, 2023
1 parent 7463fa7 commit 6beb438
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 69 deletions.
56 changes: 28 additions & 28 deletions bdk-ffi/Cargo.lock

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

69 changes: 39 additions & 30 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,6 @@ interface Wallet {

interface Update {};

// ------------------------------------------------------------------------
// bdk crate - bitcoin reexports
// ------------------------------------------------------------------------

enum Network {
"Bitcoin",
"Testnet",
"Signet",
"Regtest",
};

enum WordCount {
"Words12",
"Words15",
"Words18",
"Words21",
"Words24",
};

interface Address {
[Throws=BdkError]
constructor(string address, Network network);

Network network();

string to_qr_uri();

string as_string();
};

// ------------------------------------------------------------------------
// bdk crate - descriptor module
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -208,4 +178,43 @@ interface Descriptor {

interface EsploraClient {
constructor(string url);

[Throws=BdkError]
Update scan(Wallet wallet, u64 stop_gap, u64 parallel_requests);
};

// ------------------------------------------------------------------------
// bdk crate - bitcoin reexports
// ------------------------------------------------------------------------

interface Script {
constructor(sequence<u8> raw_output_script);

sequence<u8> to_bytes();
};

enum Network {
"Bitcoin",
"Testnet",
"Signet",
"Regtest",
};

enum WordCount {
"Words12",
"Words15",
"Words18",
"Words21",
"Words24",
};

interface Address {
[Throws=BdkError]
constructor(string address, Network network);

Network network();

string to_qr_uri();

string as_string();
};
22 changes: 22 additions & 0 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf;

/// A Bitcoin script.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Script(pub(crate) BdkScriptBuf);

impl Script {
pub fn new(raw_output_script: Vec<u8>) -> Self {
let script: BdkScriptBuf = BdkScriptBuf::from(raw_output_script);
Script(script)
}

pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes()
}
}

impl From<BdkScriptBuf> for Script {
fn from(script: BdkScriptBuf) -> Self {
Script(script)
}
}
44 changes: 43 additions & 1 deletion bdk-ffi/src/esplora.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::wallet::{Update, 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);

Expand All @@ -8,7 +13,44 @@ impl EsploraClient {
Self(client)
}

// pub fn scan();
// This is a temporary solution for scanning. The long-term solution involves not passing
// the wallet to the client at all.
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();

Expand Down
3 changes: 2 additions & 1 deletion bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod bitcoin;
mod descriptor;
mod esplora;
mod keys;
Expand All @@ -15,6 +16,7 @@ use bdk::KeychainKind;
use std::sync::Arc;

// TODO 6: Why are these imports required?
use crate::bitcoin::Script;
use crate::descriptor::Descriptor;
use crate::esplora::EsploraClient;
use crate::keys::DerivationPath;
Expand All @@ -24,7 +26,6 @@ use crate::keys::Mnemonic;
use crate::wallet::Update;
use crate::wallet::Wallet;
use bdk::keys::bip39::WordCount;
// use bdk_esplora::EsploraExt;

uniffi::include_scaffolding!("bdk");

Expand Down
3 changes: 1 addition & 2 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ impl Wallet {
}

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()))
Expand Down Expand Up @@ -639,7 +638,7 @@ pub struct Update(pub(crate) BdkUpdate);
// .map(Arc::new)
// }
// }
//

// // The goal of these tests to to ensure `bdk-ffi` intermediate code correctly calls `bdk` APIs.
// // These tests should not be used to verify `bdk` behavior that is already tested in the `bdk`
// // crate.
Expand Down
Loading

0 comments on commit 6beb438

Please sign in to comment.