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? {