Skip to content

Commit ea74d75

Browse files
committed
Fix orphaned SQLite files, error-swallowing set_wallet_type, and unconditional cache clear
- Defer BdkStore creation past fingerprint duplicate check so the upgrade early-return path no longer orphans a SQLite file on disk - Make set_wallet_type return Result and write DB before updating in-memory state, so failures are surfaced instead of silently reverted - Check walletId before clearing cached WalletManager on Android, matching existing iOS behavior
1 parent 6b2e143 commit ea74d75

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

android/app/src/main/java/org/bitcoinppl/cove/AppManager.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,11 @@ class AppManager private constructor() : FfiReconcile {
428428

429429
// upgrade watch-only → cold in-place
430430
if (walletManager?.id == id && walletManager?.walletMetadata?.walletType != WalletType.HOT) {
431-
walletManager?.rust?.setWalletType(WalletType.COLD)
431+
try {
432+
walletManager?.rust?.setWalletType(WalletType.COLD)
433+
} catch (e: Exception) {
434+
Log.e(tag, "Failed to set wallet type to cold", e)
435+
}
432436
}
433437
} finally {
434438
wallet.close()
@@ -626,7 +630,9 @@ class AppManager private constructor() : FfiReconcile {
626630
}
627631

628632
is AppStateReconcileMessage.ClearCachedWalletManager -> {
629-
clearWalletManager()
633+
if (walletManager?.id == message.v1) {
634+
clearWalletManager()
635+
}
630636
}
631637

632638
is AppStateReconcileMessage.ShowLoadingPopup -> {

ios/Cove/CoveApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ struct CoveApp: App {
338338

339339
// upgrade watch-only → cold in-place
340340
if app.walletManager?.id == id, app.walletManager?.walletMetadata.walletType != .hot {
341-
app.walletManager?.rust.setWalletType(walletType: .cold)
341+
try app.walletManager?.rust.setWalletType(walletType: .cold)
342342
}
343343
} catch let WalletError.WalletAlreadyExists(id) {
344344
app.alertState = TaggedItem(.duplicateWallet(walletId: id))

rust/src/manager/wallet_manager.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ pub enum WalletManagerError {
197197
#[error("unable to get next address: {0}")]
198198
NextAddressError(String),
199199

200+
#[error("unable to set wallet type: {0}")]
201+
SetWalletTypeError(String),
202+
200203
#[error("unable to get height")]
201204
GetHeightError,
202205

@@ -912,18 +915,19 @@ impl RustWalletManager {
912915
}
913916

914917
#[uniffi::method]
915-
pub fn set_wallet_type(&self, wallet_type: WalletType) {
916-
let metadata = {
917-
let mut metadata = self.metadata.write();
918-
metadata.wallet_type = wallet_type;
919-
metadata.clone()
920-
};
918+
pub fn set_wallet_type(&self, wallet_type: WalletType) -> Result<(), Error> {
919+
let mut metadata = self.metadata.read().clone();
920+
metadata.wallet_type = wallet_type;
921921

922-
self.reconciler.send(Message::WalletMetadataChanged(metadata.clone()));
922+
Database::global()
923+
.wallets
924+
.update_wallet_metadata(metadata.clone())
925+
.map_err(|e| Error::SetWalletTypeError(format!("{e:?}")))?;
923926

924-
if let Err(error) = Database::global().wallets.update_wallet_metadata(metadata) {
925-
error!("Unable to update wallet metadata: {error:?}");
926-
}
927+
*self.metadata.write() = metadata.clone();
928+
self.reconciler.send(Message::WalletMetadataChanged(metadata));
929+
930+
Ok(())
927931
}
928932

929933
#[uniffi::method]

rust/src/wallet.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,6 @@ impl Wallet {
269269
let id = WalletId::new();
270270
let mut metadata = WalletMetadata::new_for_hardware(id.clone(), "", None);
271271

272-
let mut store = BdkStore::try_new(&id, network).map_err_str(WalletError::LoadError)?;
273-
274272
let pubport_descriptors = match pubport {
275273
Format::Descriptor(descriptors) => descriptors,
276274
Format::Json(json) => {
@@ -334,6 +332,8 @@ impl Wallet {
334332
}
335333
}
336334

335+
let mut store = BdkStore::try_new(&id, network).map_err_str(WalletError::LoadError)?;
336+
337337
let fingerprint = fingerprint.map(|s| s.to_string());
338338

339339
metadata.name = match &fingerprint {

0 commit comments

Comments
 (0)