From 7e2248073209d17d6b014e21091d2e2d3fe40f41 Mon Sep 17 00:00:00 2001 From: a1denvalu3 Date: Thu, 30 Jul 2026 19:30:20 +0200 Subject: [PATCH] fix: verify geohash signatures off main thread --- .../bitchat/android/nostr/GeohashMessageHandler.kt | 11 +++++++++-- .../nostr/GeohashMessageHandlerSignatureTest.kt | 10 +++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/bitchat/android/nostr/GeohashMessageHandler.kt b/app/src/main/java/com/bitchat/android/nostr/GeohashMessageHandler.kt index 19e5a369..5d80e22d 100644 --- a/app/src/main/java/com/bitchat/android/nostr/GeohashMessageHandler.kt +++ b/app/src/main/java/com/bitchat/android/nostr/GeohashMessageHandler.kt @@ -5,8 +5,11 @@ import android.util.Log import com.bitchat.android.model.BitchatMessage import com.bitchat.android.ui.ChatState import com.bitchat.android.ui.MessageManager +import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import java.util.Date /** @@ -21,7 +24,8 @@ class GeohashMessageHandler( private val messageManager: MessageManager, private val repo: GeohashRepository, private val scope: CoroutineScope, - private val dataManager: com.bitchat.android.ui.DataManager + private val dataManager: com.bitchat.android.ui.DataManager, + private val signatureVerificationDispatcher: CoroutineDispatcher = Dispatchers.Default ) { companion object { private const val TAG = "GeohashMessageHandler" } @@ -47,7 +51,10 @@ class GeohashMessageHandler( if (event.kind != NostrKind.EPHEMERAL_EVENT && event.kind != NostrKind.GEOHASH_PRESENCE) return@launch val tagGeo = event.tags.firstOrNull { it.size >= 2 && it[0] == "g" }?.getOrNull(1) if (tagGeo == null || !tagGeo.equals(subscribedGeohash, true)) return@launch - if (!event.isValidSignature()) { + val hasValidSignature = withContext(signatureVerificationDispatcher) { + event.isValidSignature() + } + if (!hasValidSignature) { Log.w(TAG, "Rejecting geohash event ${event.id.take(8)}... with invalid signature") return@launch } diff --git a/app/src/test/kotlin/com/bitchat/android/nostr/GeohashMessageHandlerSignatureTest.kt b/app/src/test/kotlin/com/bitchat/android/nostr/GeohashMessageHandlerSignatureTest.kt index e0e2c8cd..d9a557d8 100644 --- a/app/src/test/kotlin/com/bitchat/android/nostr/GeohashMessageHandlerSignatureTest.kt +++ b/app/src/test/kotlin/com/bitchat/android/nostr/GeohashMessageHandlerSignatureTest.kt @@ -42,7 +42,15 @@ class GeohashMessageHandlerSignatureTest { dataManager = DataManager(context = application) messageManager = MessageManager(state = chatState) repo = GeohashRepository(application, chatState, dataManager) - handler = GeohashMessageHandler(application, chatState, messageManager, repo, testScope, dataManager) + handler = GeohashMessageHandler( + application, + chatState, + messageManager, + repo, + testScope, + dataManager, + testDispatcher + ) } private fun buildSignedEvent(identity: NostrIdentity, content: String): NostrEvent {