Skip to content

Commit fa49b5a

Browse files
committed
fix: hold spark sends while a wallet restore is pending
1 parent 4b743f5 commit fa49b5a

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎app/src/main/java/to/bitkit/repositories/LightningRepo.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,9 @@ class LightningRepo @Inject constructor(
14501450
onBeforeSend: suspend () -> Boolean,
14511451
): Result<PaymentId> {
14521452
if (shouldPayWithSpark(bolt11, sats)) {
1453+
runCatching { requireWalletRestoreComplete() }.onFailure { return Result.failure(it) }
14531454
if (!onBeforeSend()) return Result.failure(PaymentAbortedBeforeSend())
1455+
runCatching { requireWalletRestoreComplete() }.onFailure { return Result.failure(it) }
14541456
return sparkRepo.payInvoice(bolt11, sats)
14551457
}
14561458
return payInvoiceWithLdk(bolt11, sats, paymentDeadlineAt, maxTotalRoutingFeeMsat, onBeforeSend)

‎app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,37 @@ class LightningRepoTest : BaseUnitTest() {
11021102
verify(lightningService, never()).send(any(), anyOrNull(), anyOrNull(), anyOrNull())
11031103
}
11041104

1105+
@Test
1106+
fun `pending wallet recovery prevents spark payment dispatch`() = test {
1107+
startNodeForTesting()
1108+
whenever(sparkRepo.canSend(1_000uL)).thenReturn(true)
1109+
whenever(keychain.exists(Keychain.Key.PAYKIT_PENDING_BACKUP_RESTORE.name)).thenReturn(true)
1110+
var beforeSend = false
1111+
1112+
val result = sut.payInvoice("invoice", 1_000uL, onBeforeSend = {
1113+
beforeSend = true
1114+
true
1115+
})
1116+
1117+
assertIs<WalletBackupRestorePendingError>(result.exceptionOrNull())
1118+
assertFalse(beforeSend)
1119+
verify(sparkRepo, never()).payInvoice(any(), anyOrNull())
1120+
}
1121+
1122+
@Test
1123+
fun `restore beginning during spark payment authorization prevents dispatch`() = test {
1124+
startNodeForTesting()
1125+
whenever(sparkRepo.canSend(1_000uL)).thenReturn(true)
1126+
1127+
val result = sut.payInvoice("invoice", 1_000uL, onBeforeSend = {
1128+
whenever(keychain.exists(Keychain.Key.PAYKIT_PENDING_BACKUP_RESTORE.name)).thenReturn(true)
1129+
true
1130+
})
1131+
1132+
assertIs<WalletBackupRestorePendingError>(result.exceptionOrNull())
1133+
verify(sparkRepo, never()).payInvoice(any(), anyOrNull())
1134+
}
1135+
11051136
@Test
11061137
fun `restore beginning during payment authorization prevents dispatch`() = test {
11071138
startNodeForTesting()

0 commit comments

Comments
 (0)