feat: add new_or_load method on wallet type

This commit is contained in:
thunderbiscuit 2024-06-26 13:05:26 -04:00
parent 92d40a9f46
commit 33026108a7
No known key found for this signature in database
GPG Key ID: 88253696EB836462
4 changed files with 34 additions and 20 deletions

View File

@ -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();
};
// ------------------------------------------------------------------------

View File

@ -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<Network>,
},
// From NewOrLoadError
#[error("loaded descriptor '{got}' does not match what was provided '{keychain:?}'")]
LoadedDescriptorDoesNotMatch { got: String, keychain: KeychainKind },
}

View File

@ -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<BdkSqliteStore<KeychainKind, ConfirmationTimeHeightAnchor>>,
@ -27,13 +27,17 @@ impl SqliteStore {
self.inner_mutex.lock().expect("sqlite store")
}
// pub fn read(&self) -> Result<Option<CombinedChangeSet<KeychainKind, ConfirmationTimeHeightAnchor>, 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<Option<Arc<ChangeSet>>, 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))
}
}

View File

@ -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<Descriptor>,
// change_descriptor: Option<Arc<Descriptor>>,
// change_set: Option<CombinedChangeSet<KeychainKind, ConfirmationTimeHeightAnchor>>,
// network: Network,
// ) -> Result<Self, WalletCreationError> {
// 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<Descriptor>,
change_descriptor: Arc<Descriptor>,
change_set: Option<Arc<ChangeSet>>,
network: Network,
) -> Result<Self, WalletCreationError> {
let descriptor = descriptor.to_string_with_secret();
let change_descriptor = change_descriptor.to_string_with_secret();
let change_set: Option<CombinedChangeSet<KeychainKind, ConfirmationTimeHeightAnchor>> =
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<BdkWallet> {
self.inner_mutex.lock().expect("wallet")