Skip to content

Commit 21e38c2

Browse files
authored
Merge pull request #465 from synonymdev/fix/rotate-address
fix: gate sheets and rotate receive address on restore
2 parents cff199f + 195bd43 commit 21e38c2

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

app/src/main/java/to/bitkit/repositories/BackupRepo.kt

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ import kotlinx.coroutines.CoroutineScope
77
import kotlinx.coroutines.Job
88
import kotlinx.coroutines.SupervisorJob
99
import kotlinx.coroutines.delay
10+
import kotlinx.coroutines.flow.MutableStateFlow
11+
import kotlinx.coroutines.flow.StateFlow
12+
import kotlinx.coroutines.flow.asStateFlow
1013
import kotlinx.coroutines.flow.distinctUntilChanged
1114
import kotlinx.coroutines.flow.drop
1215
import kotlinx.coroutines.flow.first
1316
import kotlinx.coroutines.flow.map
17+
import kotlinx.coroutines.flow.update
1418
import kotlinx.coroutines.launch
1519
import kotlinx.coroutines.withContext
1620
import kotlinx.datetime.Clock
@@ -63,7 +67,8 @@ class BackupRepo @Inject constructor(
6367
private val dataListenerJobs = mutableListOf<Job>()
6468
private var periodicCheckJob: Job? = null
6569
private var isObserving = false
66-
private var isRestoring = false
70+
private val _isRestoring = MutableStateFlow(false)
71+
val isRestoring: StateFlow<Boolean> = _isRestoring.asStateFlow()
6772

6873
private var lastNotificationTime = 0L
6974

@@ -119,7 +124,7 @@ class BackupRepo @Inject constructor(
119124
old.synced == new.synced && old.required == new.required
120125
}
121126
.collect { status ->
122-
if (status.isRequired && !status.running && !isRestoring) {
127+
if (status.isRequired && !status.running && !isRestoring.value) {
123128
scheduleBackup(category)
124129
}
125130
}
@@ -137,7 +142,7 @@ class BackupRepo @Inject constructor(
137142
.distinctUntilChanged()
138143
.drop(1)
139144
.collect {
140-
if (isRestoring) return@collect
145+
if (isRestoring.value) return@collect
141146
markBackupRequired(BackupCategory.SETTINGS)
142147
}
143148
}
@@ -148,7 +153,7 @@ class BackupRepo @Inject constructor(
148153
.distinctUntilChanged()
149154
.drop(1)
150155
.collect {
151-
if (isRestoring) return@collect
156+
if (isRestoring.value) return@collect
152157
markBackupRequired(BackupCategory.WIDGETS)
153158
}
154159
}
@@ -160,7 +165,7 @@ class BackupRepo @Inject constructor(
160165
.distinctUntilChanged()
161166
.drop(1)
162167
.collect {
163-
if (isRestoring) return@collect
168+
if (isRestoring.value) return@collect
164169
markBackupRequired(BackupCategory.WALLET)
165170
}
166171
}
@@ -172,7 +177,7 @@ class BackupRepo @Inject constructor(
172177
.distinctUntilChanged()
173178
.drop(1)
174179
.collect {
175-
if (isRestoring) return@collect
180+
if (isRestoring.value) return@collect
176181
markBackupRequired(BackupCategory.METADATA)
177182
}
178183
}
@@ -185,7 +190,7 @@ class BackupRepo @Inject constructor(
185190
.distinctUntilChanged()
186191
.drop(1)
187192
.collect {
188-
if (isRestoring) return@collect
193+
if (isRestoring.value) return@collect
189194
markBackupRequired(BackupCategory.METADATA)
190195
}
191196
}
@@ -196,7 +201,7 @@ class BackupRepo @Inject constructor(
196201
blocktankRepo.blocktankState
197202
.drop(1)
198203
.collect {
199-
if (isRestoring) return@collect
204+
if (isRestoring.value) return@collect
200205
markBackupRequired(BackupCategory.BLOCKTANK)
201206
}
202207
}
@@ -207,7 +212,7 @@ class BackupRepo @Inject constructor(
207212
activityRepo.activitiesChanged
208213
.drop(1)
209214
.collect {
210-
if (isRestoring) return@collect
215+
if (isRestoring.value) return@collect
211216
markBackupRequired(BackupCategory.ACTIVITY)
212217
}
213218
}
@@ -220,7 +225,7 @@ class BackupRepo @Inject constructor(
220225
val lastSync = lightningService.status?.latestLightningWalletSyncTimestamp?.toLong()
221226
?.let { it * 1000 } // Convert seconds to millis
222227
?: return@collect
223-
if (isRestoring) return@collect
228+
if (isRestoring.value) return@collect
224229
cacheStore.updateBackupStatus(BackupCategory.LIGHTNING_CONNECTIONS) {
225230
it.copy(required = lastSync, synced = lastSync, running = false)
226231
}
@@ -265,7 +270,7 @@ class BackupRepo @Inject constructor(
265270

266271
// Double-check if backup is still needed
267272
val status = cacheStore.backupStatuses.first()[category] ?: BackupItemStatus()
268-
if (status.isRequired && !isRestoring) {
273+
if (status.isRequired && !isRestoring.value) {
269274
triggerBackup(category)
270275
} else {
271276
// Backup no longer needed, reset running flag
@@ -407,12 +412,13 @@ class BackupRepo @Inject constructor(
407412
): Result<Unit> = withContext(ioDispatcher) {
408413
Logger.debug("Full restore starting", context = TAG)
409414

410-
isRestoring = true
415+
_isRestoring.update { true }
411416

412417
return@withContext try {
413418
performRestore(BackupCategory.METADATA) { dataBytes ->
414419
val parsed = json.decodeFromString<MetadataBackupV1>(String(dataBytes))
415-
cacheStore.update { parsed.cache }
420+
val caches = parsed.cache.copy(onchainAddress = "") // Force onchain address rotation
421+
cacheStore.update { caches }
416422
Logger.debug("Restored caches: ${jsonLogOf(parsed.cache.copy(cachedRates = emptyList()))}", TAG)
417423
onCacheRestored()
418424
db.tagMetadataDao().upsert(parsed.tagMetadata)
@@ -454,7 +460,7 @@ class BackupRepo @Inject constructor(
454460
Logger.warn("Full restore error", e = e, context = TAG)
455461
Result.failure(e)
456462
} finally {
457-
isRestoring = false
463+
_isRestoring.update { false }
458464
}
459465
}
460466

app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import to.bitkit.models.TransactionSpeed
7979
import to.bitkit.models.toActivityFilter
8080
import to.bitkit.models.toTxType
8181
import to.bitkit.repositories.ActivityRepo
82+
import to.bitkit.repositories.BackupRepo
8283
import to.bitkit.repositories.BlocktankRepo
8384
import to.bitkit.repositories.ConnectivityRepo
8485
import to.bitkit.repositories.ConnectivityState
@@ -106,6 +107,7 @@ class AppViewModel @Inject constructor(
106107
private val keychain: Keychain,
107108
private val lightningRepo: LightningRepo,
108109
private val walletRepo: WalletRepo,
110+
private val backupRepo: BackupRepo,
109111
private val ldkNodeEventBus: LdkNodeEventBus,
110112
private val settingsStore: SettingsStore,
111113
private val currencyRepo: CurrencyRepo,
@@ -1578,6 +1580,8 @@ class AppViewModel @Inject constructor(
15781580
return
15791581
}
15801582

1583+
if (backupRepo.isRestoring.value) return
1584+
15811585
timedSheetsScope?.cancel()
15821586
timedSheetsScope = CoroutineScope(bgDispatcher + SupervisorJob())
15831587
timedSheetsScope?.launch {

0 commit comments

Comments
 (0)