From 6c0b22f12990faa586ff6559cce0a843b8cadff1 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 27 Jun 2024 16:49:05 -0400 Subject: [PATCH] Fixes error when trying to decrypt the PrivateOutboxRelayList event for an account that is not currently active. --- .../service/NostrAccountDataSource.kt | 10 ++- .../NIP90ContentDiscoveryResponseEvent.kt | 2 +- .../events/PrivateOutboxRelayListEvent.kt | 68 +++++++++---------- .../quartz/signers/ExternalSignerLauncher.kt | 15 ++-- 4 files changed, 47 insertions(+), 48 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/NostrAccountDataSource.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/NostrAccountDataSource.kt index 586332510..0d91a22f8 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/NostrAccountDataSource.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/NostrAccountDataSource.kt @@ -104,7 +104,14 @@ object NostrAccountDataSource : AmethystNostrDataSource("AccountData") { types = COMMON_FEED_TYPES, filter = Filter( - kinds = listOf(StatusEvent.KIND, AdvertisedRelayListEvent.KIND, ChatMessageRelayListEvent.KIND, SearchRelayListEvent.KIND, PrivateOutboxRelayListEvent.KIND), + kinds = + listOf( + StatusEvent.KIND, + AdvertisedRelayListEvent.KIND, + ChatMessageRelayListEvent.KIND, + SearchRelayListEvent.KIND, + PrivateOutboxRelayListEvent.KIND, + ), authors = listOf(account.userProfile().pubkeyHex), limit = 10, ), @@ -122,7 +129,6 @@ object NostrAccountDataSource : AmethystNostrDataSource("AccountData") { ContactListEvent.KIND, AdvertisedRelayListEvent.KIND, ChatMessageRelayListEvent.KIND, - PrivateOutboxRelayListEvent.KIND, SearchRelayListEvent.KIND, FileServersEvent.KIND, MuteListEvent.KIND, diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/events/NIP90ContentDiscoveryResponseEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/events/NIP90ContentDiscoveryResponseEvent.kt index 535bed7b6..9e0ba0500 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/events/NIP90ContentDiscoveryResponseEvent.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/events/NIP90ContentDiscoveryResponseEvent.kt @@ -57,7 +57,7 @@ class NIP90ContentDiscoveryResponseEvent( } } } catch (e: Throwable) { - Log.w("GeneralList", "Error parsing the JSON ${e.message}") + Log.w("NIP90ContentDiscoveryResponseEvent", "Error parsing the JSON ${e.message}") } return events ?: listOf() diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/events/PrivateOutboxRelayListEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/events/PrivateOutboxRelayListEvent.kt index 0121ebec6..07551542a 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/events/PrivateOutboxRelayListEvent.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/events/PrivateOutboxRelayListEvent.kt @@ -41,27 +41,25 @@ class PrivateOutboxRelayListEvent( override fun dTag() = FIXED_D_TAG - fun relays(): List? { - return tags.mapNotNull { - if (it.size > 1 && it[0] == "relay") { - it[1] - } else { - null - } - }.plus( - privateTagsCache?.mapNotNull { + fun relays(): List? = + tags + .mapNotNull { if (it.size > 1 && it[0] == "relay") { it[1] } else { null } - } ?: emptyList(), - ).ifEmpty { null } - } + }.plus( + privateTagsCache?.mapNotNull { + if (it.size > 1 && it[0] == "relay") { + it[1] + } else { + null + } + } ?: emptyList(), + ).ifEmpty { null } - fun cachedPrivateTags(): Array>? { - return privateTagsCache - } + fun cachedPrivateTags(): Array>? = privateTagsCache fun privateTags( signer: NostrSigner, @@ -79,11 +77,15 @@ class PrivateOutboxRelayListEvent( try { signer.nip44Decrypt(content, pubKey) { - privateTagsCache = mapper.readValue>>(it) - privateTagsCache?.let { onReady(it) } + try { + privateTagsCache = mapper.readValue>>(it) + privateTagsCache?.let { onReady(it) } + } catch (e: Throwable) { + Log.w("PrivateOutboxRelayListEvent", "Error parsing the JSON: ${e.message}. Json `$it` from event `${toNostrUri()}`") + } } } catch (e: Throwable) { - Log.w("GeneralList", "Error parsing the JSON ${e.message}") + Log.w("PrivateOutboxRelayListEvent", "Error decrypting content: ${e.message}. Event: `${toNostrUri()}`") } } @@ -92,13 +94,9 @@ class PrivateOutboxRelayListEvent( const val FIXED_D_TAG = "" val TAGS = arrayOf(arrayOf("alt", "Relay list to store private content from this author")) - fun createAddressATag(pubKey: HexKey): ATag { - return ATag(KIND, pubKey, FIXED_D_TAG, null) - } + fun createAddressATag(pubKey: HexKey): ATag = ATag(KIND, pubKey, FIXED_D_TAG, null) - fun createAddressTag(pubKey: HexKey): String { - return ATag.assembleATag(KIND, pubKey, FIXED_D_TAG) - } + fun createAddressTag(pubKey: HexKey): String = ATag.assembleATag(KIND, pubKey, FIXED_D_TAG) fun encryptTags( privateTags: Array>? = null, @@ -114,11 +112,11 @@ class PrivateOutboxRelayListEvent( ) } - fun createTagArray(relays: List): Array> { - return relays.map { - arrayOf("relay", it) - }.toTypedArray() - } + fun createTagArray(relays: List): Array> = + relays + .map { + arrayOf("relay", it) + }.toTypedArray() fun updateRelayList( earlierVersion: PrivateOutboxRelayListEvent, @@ -128,11 +126,13 @@ class PrivateOutboxRelayListEvent( onReady: (PrivateOutboxRelayListEvent) -> Unit, ) { val tags = - earlierVersion.privateTagsCache?.filter { it[0] != "relay" }?.plus( - relays.map { - arrayOf("relay", it) - }, - )?.toTypedArray() ?: emptyArray() + earlierVersion.privateTagsCache + ?.filter { it[0] != "relay" } + ?.plus( + relays.map { + arrayOf("relay", it) + }, + )?.toTypedArray() ?: emptyArray() encryptTags(tags, signer) { signer.sign(createdAt, KIND, TAGS, it) { diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/signers/ExternalSignerLauncher.kt b/quartz/src/main/java/com/vitorpamplona/quartz/signers/ExternalSignerLauncher.kt index a3dcf9952..bd6b0644d 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/signers/ExternalSignerLauncher.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/signers/ExternalSignerLauncher.kt @@ -52,9 +52,7 @@ class Permission( val type: String, val kind: Int? = null, ) { - fun toJson(): String { - return "{\"type\":\"${type}\",\"kind\":$kind}" - } + fun toJson(): String = "{\"type\":\"${type}\",\"kind\":$kind}" } class Result( @@ -89,9 +87,7 @@ class Result( /** * Parses the json with a string of events to an Array of Event objects. */ - fun fromJsonArray(json: String): Array { - return mapper.readValue(json) - } + fun fromJsonArray(json: String): Array = mapper.readValue(json) } } @@ -272,9 +268,7 @@ class ExternalSignerLauncher( signerType: SignerType, data: Array, columnName: String = "signature", - ): kotlin.Result { - return getDataFromResolver(signerType, data, columnName, contentResolver) - } + ): kotlin.Result = getDataFromResolver(signerType, data, columnName, contentResolver) private fun getDataFromResolver( signerType: SignerType, @@ -298,8 +292,7 @@ class ExternalSignerLauncher( "1", null, null, - ) - .use { + ).use { if (it == null) { return kotlin.Result.success(null) }