Skip to content

Commit 2920a97

Browse files
committed
fix: reset paykit state on signout
1 parent e9a12c1 commit 2920a97

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import kotlinx.coroutines.sync.Mutex
2626
import kotlinx.coroutines.sync.withLock
2727
import kotlinx.coroutines.withContext
2828
import to.bitkit.data.PubkyStore
29+
import to.bitkit.data.SettingsStore
2930
import to.bitkit.data.keychain.Keychain
3031
import to.bitkit.di.IoDispatcher
3132
import to.bitkit.env.Env
@@ -52,14 +53,15 @@ sealed class PubkyContactError(message: String) : AppError(message) {
5253
data object InvalidFormat : PubkyContactError("Invalid pubky key format")
5354
}
5455

55-
@Suppress("TooManyFunctions", "LargeClass")
56+
@Suppress("TooManyFunctions", "LargeClass", "LongParameterList")
5657
@Singleton
5758
class PubkyRepo @Inject constructor(
5859
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
5960
private val pubkyService: PubkyService,
6061
private val keychain: Keychain,
6162
private val imageLoader: ImageLoader,
6263
private val pubkyStore: PubkyStore,
64+
private val settingsStore: SettingsStore,
6365
private val httpClient: HttpClient,
6466
) {
6567
companion object {
@@ -961,10 +963,24 @@ class PubkyRepo @Inject constructor(
961963
private suspend fun clearLocalState() = withContext(ioDispatcher) {
962964
runCatching { keychain.delete(Keychain.Key.PAYKIT_SESSION.name) }
963965
runCatching { keychain.delete(Keychain.Key.PUBKY_SECRET_KEY.name) }
966+
runCatching { clearPublicPaykitSharingState() }
967+
.onFailure { Logger.warn("Failed to clear public Paykit sharing state", it, context = TAG) }
964968
notifyBackupStateChanged()
965969
clearAuthenticatedState()
966970
}
967971

972+
private suspend fun clearPublicPaykitSharingState() {
973+
settingsStore.update {
974+
it.copy(
975+
hasConfirmedPublicPaykitEndpoints = false,
976+
sharesPublicPaykitEndpoints = false,
977+
publicPaykitBolt11 = "",
978+
publicPaykitBolt11PaymentHash = "",
979+
publicPaykitBolt11ExpiresAtMillis = 0,
980+
)
981+
}
982+
}
983+
968984
private fun requireAddableContactPublicKey(publicKey: String, allowExisting: Boolean = false): String {
969985
val prefixedKey = PubkyPublicKeyFormat.normalized(publicKey)
970986
contactValidationError(prefixedKey, allowExisting)?.let { throw it }

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import coil3.ImageLoader
55
import coil3.disk.DiskCache
66
import coil3.memory.MemoryCache
77
import com.synonym.paykit.FfiPaymentEntry
8+
import kotlinx.coroutines.flow.MutableStateFlow
89
import kotlinx.coroutines.flow.flowOf
910
import kotlinx.coroutines.runBlocking
1011
import org.junit.Before
@@ -20,6 +21,8 @@ import org.mockito.kotlin.verifyBlocking
2021
import org.mockito.kotlin.whenever
2122
import to.bitkit.data.PubkyStore
2223
import to.bitkit.data.PubkyStoreData
24+
import to.bitkit.data.SettingsData
25+
import to.bitkit.data.SettingsStore
2326
import to.bitkit.data.keychain.Keychain
2427
import to.bitkit.env.Env
2528
import to.bitkit.models.PubkyProfile
@@ -51,10 +54,19 @@ class PubkyRepoTest : BaseUnitTest() {
5154
private val keychain = mock<Keychain>()
5255
private val imageLoader = mock<ImageLoader>()
5356
private val pubkyStore = mock<PubkyStore>()
57+
private val settingsStore = mock<SettingsStore>()
58+
private val settingsFlow = MutableStateFlow(SettingsData())
5459

5560
@Before
5661
fun setUp() = runBlocking {
62+
settingsFlow.value = SettingsData()
5763
whenever(pubkyStore.data).thenReturn(flowOf(PubkyStoreData()))
64+
whenever(settingsStore.data).thenReturn(settingsFlow)
65+
whenever { settingsStore.update(any()) }.thenAnswer {
66+
val transform = it.getArgument<(SettingsData) -> SettingsData>(0)
67+
settingsFlow.value = transform(settingsFlow.value)
68+
Unit
69+
}
5870
sut = createSut()
5971
}
6072

@@ -64,6 +76,7 @@ class PubkyRepoTest : BaseUnitTest() {
6476
keychain = keychain,
6577
imageLoader = imageLoader,
6678
pubkyStore = pubkyStore,
79+
settingsStore = settingsStore,
6780
httpClient = mock(),
6881
)
6982

@@ -247,6 +260,27 @@ class PubkyRepoTest : BaseUnitTest() {
247260
verifyBlocking(pubkyStore) { reset() }
248261
}
249262

263+
@Test
264+
fun `signOut should clear public Paykit sharing settings`() = test {
265+
authenticateForTesting()
266+
settingsFlow.value = SettingsData(
267+
hasConfirmedPublicPaykitEndpoints = true,
268+
sharesPublicPaykitEndpoints = true,
269+
publicPaykitBolt11 = "lnbc1old",
270+
publicPaykitBolt11PaymentHash = "010203",
271+
publicPaykitBolt11ExpiresAtMillis = 123L,
272+
)
273+
274+
val result = sut.signOut()
275+
276+
assertTrue(result.isSuccess)
277+
assertFalse(settingsFlow.value.hasConfirmedPublicPaykitEndpoints)
278+
assertFalse(settingsFlow.value.sharesPublicPaykitEndpoints)
279+
assertEquals("", settingsFlow.value.publicPaykitBolt11)
280+
assertEquals("", settingsFlow.value.publicPaykitBolt11PaymentHash)
281+
assertEquals(0, settingsFlow.value.publicPaykitBolt11ExpiresAtMillis)
282+
}
283+
250284
@Test
251285
fun `removeBitkitPaymentEndpoints removes only bitkit managed endpoints`() = test {
252286
authenticateForTesting(publicKey = VALID_SELF_KEY)

0 commit comments

Comments
 (0)