From 96f0ca9d9eae10fb70751b74384eba39b1dacc4f Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sat, 6 Jun 2026 14:13:38 +0700 Subject: [PATCH 1/3] add timeout when switch signer --- shared/src/commonMain/kotlin/su/reya/coop/Signer.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/shared/src/commonMain/kotlin/su/reya/coop/Signer.kt b/shared/src/commonMain/kotlin/su/reya/coop/Signer.kt index 5e87674..b89df78 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/Signer.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/Signer.kt @@ -2,10 +2,12 @@ package su.reya.coop import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock +import kotlinx.coroutines.withTimeoutOrNull import rust.nostr.sdk.AsyncNostrSigner import rust.nostr.sdk.Event import rust.nostr.sdk.PublicKey import rust.nostr.sdk.UnsignedEvent +import kotlin.time.Duration.Companion.seconds class UniversalSigner(initialSigner: AsyncNostrSigner) : AsyncNostrSigner { private val mutex = Mutex() @@ -26,7 +28,13 @@ class UniversalSigner(initialSigner: AsyncNostrSigner) : AsyncNostrSigner { */ suspend fun switch(newSigner: AsyncNostrSigner) = mutex.withLock { signer = newSigner - currentUser = newSigner.getPublicKeyAsync() + try { + currentUser = withTimeoutOrNull(20.seconds) { + newSigner.getPublicKeyAsync() + } + } catch (e: Exception) { + throw IllegalStateException("Failed to get public key from signer", e) + } } override suspend fun getPublicKeyAsync(): PublicKey? { -- 2.49.1 From 3b247190c09370e9a0e2e0a759dfcbf162dbc5d7 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sat, 6 Jun 2026 14:17:47 +0700 Subject: [PATCH 2/3] dont wait to login forever --- .../src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt b/shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt index fcba31b..6a6afd1 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt @@ -76,6 +76,11 @@ class NostrViewModel( private val seenPublicKeys = mutableSetOf() init { + // Skip the splash screen if a user is already logged in + if (nostr.signer.currentUser != null) { + _signerRequired.value = false + } + // Check if the notification banner has been dismissed checkNotificationBannerDismissedStatus() @@ -216,7 +221,9 @@ class NostrViewModel( private fun login() { viewModelScope.launch { try { - val secret = secretStore.get("user_signer") + val secret = withTimeoutOrNull(3.seconds) { + secretStore.get("user_signer") + } if (secret == null) { _signerRequired.value = true -- 2.49.1 From 4b3ca8b1c0c9f2a0c4b43b50ddfced72d8b0e1e2 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sat, 6 Jun 2026 14:23:23 +0700 Subject: [PATCH 3/3] optimize the signer --- .../src/commonMain/kotlin/su/reya/coop/Signer.kt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/shared/src/commonMain/kotlin/su/reya/coop/Signer.kt b/shared/src/commonMain/kotlin/su/reya/coop/Signer.kt index b89df78..dc98991 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/Signer.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/Signer.kt @@ -7,34 +7,37 @@ import rust.nostr.sdk.AsyncNostrSigner import rust.nostr.sdk.Event import rust.nostr.sdk.PublicKey import rust.nostr.sdk.UnsignedEvent +import kotlin.concurrent.Volatile import kotlin.time.Duration.Companion.seconds class UniversalSigner(initialSigner: AsyncNostrSigner) : AsyncNostrSigner { private val mutex = Mutex() + + @Volatile private var signer: AsyncNostrSigner = initialSigner + @Volatile var currentUser: PublicKey? = null private set /** * Get the current signer. */ - suspend fun get(): AsyncNostrSigner = mutex.withLock { - signer - } + fun get(): AsyncNostrSigner = signer /** * Switch to a new signer. */ suspend fun switch(newSigner: AsyncNostrSigner) = mutex.withLock { - signer = newSigner - try { - currentUser = withTimeoutOrNull(20.seconds) { + val pubkey = try { + withTimeoutOrNull(20.seconds) { newSigner.getPublicKeyAsync() } } catch (e: Exception) { throw IllegalStateException("Failed to get public key from signer", e) } + signer = newSigner + currentUser = pubkey } override suspend fun getPublicKeyAsync(): PublicKey? { -- 2.49.1