mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-08-22 07:06:05 +00:00
fix: verify geohash signatures off main thread
This commit is contained in:
parent
57a7d3d955
commit
7e22480732
@ -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
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user