diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index b336bac..bacb875 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -362,8 +362,8 @@ interface Wallet { [Throws=WalletCreationError] constructor(Descriptor descriptor, Descriptor change_descriptor, Network network); - // [Name=new_or_load, Throws=WalletCreationError] - // constructor(Descriptor descriptor, Descriptor change_descriptor, Network network); + [Name=new_or_load, Throws=WalletCreationError] + constructor(Descriptor descriptor, Descriptor change_descriptor, ChangeSet? change_set, Network network); AddressInfo reveal_next_address(KeychainKind keychain); @@ -463,6 +463,9 @@ interface SqliteStore { [Throws=SqliteError] void write([ByRef] ChangeSet change_set); + + [Throws=SqliteError] + ChangeSet? read(); }; // ------------------------------------------------------------------------ diff --git a/bdk-ffi/src/error.rs b/bdk-ffi/src/error.rs index 3c74a7a..ac648e3 100644 --- a/bdk-ffi/src/error.rs +++ b/bdk-ffi/src/error.rs @@ -641,12 +641,14 @@ pub enum WalletCreationError { #[error("loaded genesis hash '{got}' does not match the expected one '{expected}'")] LoadedGenesisDoesNotMatch { expected: String, got: String }, + // From NewOrLoadError #[error("loaded network type is not {expected}, got {got:?}")] LoadedNetworkDoesNotMatch { expected: Network, got: Option, }, + // From NewOrLoadError #[error("loaded descriptor '{got}' does not match what was provided '{keychain:?}'")] LoadedDescriptorDoesNotMatch { got: String, keychain: KeychainKind }, } diff --git a/bdk-ffi/src/store.rs b/bdk-ffi/src/store.rs index 3912502..d77f282 100644 --- a/bdk-ffi/src/store.rs +++ b/bdk-ffi/src/store.rs @@ -6,7 +6,7 @@ use bdk_sqlite::{Store as BdkSqliteStore, Store}; use bdk_wallet::chain::ConfirmationTimeHeightAnchor; use bdk_wallet::KeychainKind; -use std::sync::{Mutex, MutexGuard}; +use std::sync::{Arc, Mutex, MutexGuard}; pub struct SqliteStore { inner_mutex: Mutex>, @@ -27,13 +27,17 @@ impl SqliteStore { self.inner_mutex.lock().expect("sqlite store") } - // pub fn read(&self) -> Result, Error> { - // self.0.read().map_err(SqliteError::from) - // } - pub fn write(&self, changeset: &ChangeSet) -> Result<(), SqliteError> { self.get_store() .write(&changeset.0) .map_err(SqliteError::from) } + + pub fn read(&self) -> Result>, SqliteError> { + self.get_store() + .read() + .map_err(SqliteError::from) + .map(|optional_bdk_change_set| optional_bdk_change_set.map(ChangeSet::from)) + .map(|optional_change_set| optional_change_set.map(Arc::new)) + } } diff --git a/bdk-ffi/src/wallet.rs b/bdk-ffi/src/wallet.rs index f6355ae..61c119a 100644 --- a/bdk-ffi/src/wallet.rs +++ b/bdk-ffi/src/wallet.rs @@ -15,7 +15,7 @@ use bdk_wallet::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf; use bdk_wallet::bitcoin::Network; use bdk_wallet::bitcoin::Psbt as BdkPsbt; use bdk_wallet::bitcoin::{OutPoint as BdkOutPoint, Sequence, Txid}; -// use bdk_wallet::chain::{CombinedChangeSet, ConfirmationTimeHeightAnchor}; +use bdk_wallet::chain::{CombinedChangeSet, ConfirmationTimeHeightAnchor}; use bdk_wallet::wallet::tx_builder::ChangeSpendPolicy; use bdk_wallet::wallet::Update as BdkUpdate; use bdk_wallet::Wallet as BdkWallet; @@ -44,18 +44,23 @@ impl Wallet { }) } - // pub fn new_or_load( - // descriptor: Arc, - // change_descriptor: Option>, - // change_set: Option>, - // network: Network, - // ) -> Result { - // let descriptor = descriptor.to_string_with_secret(); - // let change_descriptor = change_descriptor.to_string_with_secret(); - // let wallet: BdkWallet = BdkWallet::new_or_load(&descriptor, &change_descriptor, network)?; - // - // Ok(Wallet { inner_mutex: Mutex::new(wallet) }) - // } + pub fn new_or_load( + descriptor: Arc, + change_descriptor: Arc, + change_set: Option>, + network: Network, + ) -> Result { + let descriptor = descriptor.to_string_with_secret(); + let change_descriptor = change_descriptor.to_string_with_secret(); + let change_set: Option> = + change_set.map(|cs| cs.0.clone()); + let wallet: BdkWallet = + BdkWallet::new_or_load(&descriptor, &change_descriptor, change_set, network)?; + + Ok(Wallet { + inner_mutex: Mutex::new(wallet), + }) + } pub(crate) fn get_wallet(&self) -> MutexGuard { self.inner_mutex.lock().expect("wallet")