Skip to content

Commit 3797e0a

Browse files
committed
feat: Allow wallet reset on any network
1 parent bbbe3d1 commit 3797e0a

File tree

8 files changed

+52
-53
lines changed

8 files changed

+52
-53
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import kotlinx.coroutines.withContext
1010
import kotlinx.datetime.Clock
1111
import org.lightningdevkit.ldknode.BalanceDetails
1212
import org.lightningdevkit.ldknode.Event
13-
import org.lightningdevkit.ldknode.Network
1413
import org.lightningdevkit.ldknode.Txid
1514
import to.bitkit.data.AppDb
1615
import to.bitkit.data.AppStorage
@@ -47,7 +46,6 @@ class WalletRepo @Inject constructor(
4746
private val settingsStore: SettingsStore,
4847
private val addressChecker: AddressChecker,
4948
private val lightningRepo: LightningRepo,
50-
private val network: Network
5149
) {
5250

5351
private val _walletState = MutableStateFlow(
@@ -198,10 +196,6 @@ class WalletRepo @Inject constructor(
198196
}
199197

200198
suspend fun wipeWallet(walletIndex: Int = 0): Result<Unit> = withContext(bgDispatcher) {
201-
if (network != Network.REGTEST) {
202-
return@withContext Result.failure(Exception("Can only wipe on regtest."))
203-
}
204-
205199
try { //TODO CLEAN ACTIVITY'S AND UPDATE STATE. CHECK ActivityListViewModel.removeAllActivities
206200
keychain.wipe()
207201
appStorage.clear()

app/src/main/java/to/bitkit/ui/MainActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,14 @@ class MainActivity : FragmentActivity() {
220220
if (showForgotPinSheet) {
221221
ForgotPinSheet(
222222
onDismiss = { appViewModel.setShowForgotPin(false) },
223-
onResetClick = { walletViewModel.wipeStorage() },
223+
onResetClick = { walletViewModel.wipeWallet() },
224224
)
225225
}
226226

227227
LaunchedEffect(appViewModel) {
228228
appViewModel.mainScreenEffect.collect {
229229
when (it) {
230-
MainScreenEffect.WipeStorage -> walletViewModel.wipeStorage()
230+
MainScreenEffect.WipeWallet -> walletViewModel.wipeWallet()
231231
else -> Unit
232232
}
233233
}

app/src/main/java/to/bitkit/ui/screens/DevSettingsScreen.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ fun DevSettingsScreen(
5959
SettingsTextButtonRow("Reset All Activities") { activity.removeAllActivities() }
6060
SettingsTextButtonRow("Generate Test Activities") { activity.generateRandomTestData() }
6161
SettingsTextButtonRow("Reset Settings Store") { settings.reset() }
62-
SettingsTextButtonRow(stringResource(R.string.security__wipe_app)) { viewModel.wipeStorage() }
6362
}
6463

6564
SectionHeader(title = "DEBUG")

app/src/main/java/to/bitkit/ui/settings/backups/ResetAndRestoreScreen.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ fun ResetAndRestoreScreen(
5858
showConfirmDialog = showDialog,
5959
onClickBackup = { app.showSheet(BottomSheetType.BackupNavigation) },
6060
onClickReset = { showDialog = true },
61-
onResetConfirm = {
62-
wallet.wipeStorage()
63-
},
61+
onResetConfirm = { wallet.wipeWallet() },
6462
onResetDismiss = { showDialog = false },
6563
onBack = { navController.popBackStack() },
6664
onClose = { navController.navigateToHome() },

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ class AppViewModel @Inject constructor(
813813
description = resourceProvider.getString(R.string.security__wiped_message),
814814
)
815815
delay(250) // small delay for UI feedback
816-
mainScreenEffect(MainScreenEffect.WipeStorage)
816+
mainScreenEffect(MainScreenEffect.WipeWallet)
817817
}
818818
}
819819
return false
@@ -915,7 +915,7 @@ sealed class SendEffect {
915915

916916
sealed class MainScreenEffect {
917917
data class NavigateActivityDetail(val activityId: String) : MainScreenEffect()
918-
data object WipeStorage : MainScreenEffect()
918+
data object WipeWallet : MainScreenEffect()
919919
data class ProcessClipboardAutoRead(val data: String) : MainScreenEffect()
920920
}
921921

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class WalletViewModel @Inject constructor(
255255
}
256256
}
257257

258-
fun wipeStorage() {
258+
fun wipeWallet() {
259259
viewModelScope.launch(bgDispatcher) {
260260
walletRepo.wipeWallet().onFailure { error ->
261261
ToastEventBus.send(error)

app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import org.junit.Test
88
import org.lightningdevkit.ldknode.BalanceDetails
99
import org.lightningdevkit.ldknode.ChannelDetails
1010
import org.lightningdevkit.ldknode.Event
11-
import org.lightningdevkit.ldknode.Network
1211
import org.mockito.kotlin.any
1312
import org.mockito.kotlin.doReturn
1413
import org.mockito.kotlin.mock
@@ -62,7 +61,6 @@ class WalletRepoTest : BaseUnitTest() {
6261
settingsStore = settingsStore,
6362
addressChecker = addressChecker,
6463
lightningRepo = lightningRepo,
65-
network = Network.REGTEST
6664
)
6765
}
6866

@@ -132,7 +130,6 @@ class WalletRepoTest : BaseUnitTest() {
132130
settingsStore = settingsStore,
133131
addressChecker = addressChecker,
134132
lightningRepo = lightningRepo,
135-
network = Network.BITCOIN
136133
)
137134

138135
val result = nonRegtestRepo.wipeWallet()
@@ -169,23 +166,25 @@ class WalletRepoTest : BaseUnitTest() {
169166
fun `refreshBip21 should generate new address when current has transactions`() = test {
170167
whenever(sut.getOnchainAddress()).thenReturn("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq")
171168
whenever(lightningRepo.newAddress()).thenReturn(Result.success("newAddress"))
172-
whenever(addressChecker.getAddressInfo(any())).thenReturn(AddressInfo(
173-
address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
174-
chain_stats = AddressStats(
175-
funded_txo_count = 1,
176-
funded_txo_sum = 2,
177-
spent_txo_count = 1,
178-
spent_txo_sum = 1,
179-
tx_count = 5
180-
),
181-
mempool_stats = AddressStats(
182-
funded_txo_count = 1,
183-
funded_txo_sum = 2,
184-
spent_txo_count = 1,
185-
spent_txo_sum = 1,
186-
tx_count = 5
169+
whenever(addressChecker.getAddressInfo(any())).thenReturn(
170+
AddressInfo(
171+
address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
172+
chain_stats = AddressStats(
173+
funded_txo_count = 1,
174+
funded_txo_sum = 2,
175+
spent_txo_count = 1,
176+
spent_txo_sum = 1,
177+
tx_count = 5
178+
),
179+
mempool_stats = AddressStats(
180+
funded_txo_count = 1,
181+
funded_txo_sum = 2,
182+
spent_txo_count = 1,
183+
spent_txo_sum = 1,
184+
tx_count = 5
185+
)
187186
)
188-
))
187+
)
189188

190189
val result = sut.refreshBip21()
191190

@@ -197,23 +196,25 @@ class WalletRepoTest : BaseUnitTest() {
197196
fun `refreshBip21 should keep address when current has no transactions`() = test {
198197
val existingAddress = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
199198
whenever(sut.getOnchainAddress()).thenReturn(existingAddress)
200-
whenever(addressChecker.getAddressInfo(any())).thenReturn(AddressInfo(
201-
address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
202-
chain_stats = AddressStats(
203-
funded_txo_count = 1,
204-
funded_txo_sum = 2,
205-
spent_txo_count = 1,
206-
spent_txo_sum = 1,
207-
tx_count = 0
208-
),
209-
mempool_stats = AddressStats(
210-
funded_txo_count = 1,
211-
funded_txo_sum = 2,
212-
spent_txo_count = 1,
213-
spent_txo_sum = 1,
214-
tx_count = 0
199+
whenever(addressChecker.getAddressInfo(any())).thenReturn(
200+
AddressInfo(
201+
address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
202+
chain_stats = AddressStats(
203+
funded_txo_count = 1,
204+
funded_txo_sum = 2,
205+
spent_txo_count = 1,
206+
spent_txo_sum = 1,
207+
tx_count = 0
208+
),
209+
mempool_stats = AddressStats(
210+
funded_txo_count = 1,
211+
funded_txo_sum = 2,
212+
spent_txo_count = 1,
213+
spent_txo_sum = 1,
214+
tx_count = 0
215+
)
215216
)
216-
))
217+
)
217218

218219
val result = sut.refreshBip21()
219220

@@ -278,7 +279,14 @@ class WalletRepoTest : BaseUnitTest() {
278279

279280
@Test
280281
fun `refreshBip21ForEvent should not refresh for other events`() = test {
281-
sut.refreshBip21ForEvent(Event.PaymentSuccessful(paymentId = "", paymentHash = "", paymentPreimage = "", feePaidMsat = 10uL))
282+
sut.refreshBip21ForEvent(
283+
Event.PaymentSuccessful(
284+
paymentId = "",
285+
paymentHash = "",
286+
paymentPreimage = "",
287+
feePaidMsat = 10uL
288+
)
289+
)
282290

283291
verify(lightningRepo, never()).newAddress()
284292
}

app/src/test/java/to/bitkit/ui/WalletViewModelTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ class WalletViewModelTest : BaseUnitTest() {
127127
}
128128

129129
@Test
130-
fun `wipeStorage should call walletRepo wipeWallet and lightningRepo wipeStorage, and set walletExists`() =
130+
fun `wipeWallet should call walletRepo wipeWallet`() =
131131
test {
132132
whenever(walletRepo.wipeWallet(walletIndex = 0)).thenReturn(Result.success(Unit))
133-
sut.wipeStorage()
133+
sut.wipeWallet()
134134

135135
verify(walletRepo).wipeWallet(walletIndex = 0)
136136
}

0 commit comments

Comments
 (0)