diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/concord/cord02Community/ConcordCommunityList.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/concord/cord02Community/ConcordCommunityList.kt index 7629f174a9..98c09f7898 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/concord/cord02Community/ConcordCommunityList.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/concord/cord02Community/ConcordCommunityList.kt @@ -78,11 +78,21 @@ object ConcordCommunityList { entries: List, createdAt: Long, ): Event { - val json = ConcordJson.instance.encodeToString(ListSerializer(ConcordCommunityListEntry.serializer()), entries) - val content = signer.nip44Encrypt(json, signer.pubKey) + val content = signer.nip44Encrypt(encode(entries), signer.pubKey) return signer.sign(createdAt, ConcordKinds.COMMUNITY_LIST, emptyArray(), content) } + /** Serializes [entries] to the plaintext JSON that gets NIP-44 self-encrypted. */ + fun encode(entries: List): String = ConcordJson.instance.encodeToString(ListSerializer(ConcordCommunityListEntry.serializer()), entries) + + /** Parses the decrypted plaintext JSON back into entries, or empty on failure. */ + fun decode(json: String): List = + try { + ConcordJson.instance.decodeFromString(ListSerializer(ConcordCommunityListEntry.serializer()), json) + } catch (_: Exception) { + emptyList() + } + /** Decrypts and parses a kind-13302 list event with [signer], or empty on failure. */ suspend fun parse( event: Event, @@ -90,8 +100,7 @@ object ConcordCommunityList { ): List { if (event.kind != ConcordKinds.COMMUNITY_LIST) return emptyList() return try { - val json = signer.nip44Decrypt(event.content, signer.pubKey) - ConcordJson.instance.decodeFromString(ListSerializer(ConcordCommunityListEntry.serializer()), json) + decode(signer.nip44Decrypt(event.content, signer.pubKey)) } catch (_: Exception) { emptyList() } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/concord/cord02Community/ConcordCommunityListEvent.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/concord/cord02Community/ConcordCommunityListEvent.kt new file mode 100644 index 0000000000..96949c81d9 --- /dev/null +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/concord/cord02Community/ConcordCommunityListEvent.kt @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.concord.cord02Community + +import androidx.compose.runtime.Immutable +import com.vitorpamplona.quartz.concord.events.ConcordKinds +import com.vitorpamplona.quartz.nip01Core.core.Address +import com.vitorpamplona.quartz.nip01Core.core.Event +import com.vitorpamplona.quartz.nip01Core.core.HexKey +import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner +import com.vitorpamplona.quartz.utils.TimeUtils + +/** + * The member's private, self-encrypted list of joined Concord communities (kind + * [ConcordKinds.COMMUNITY_LIST] = 13302, CORD-05). A replaceable event whose + * `content` is the NIP-44 self-encryption of the [ConcordCommunityListEntry] JSON + * — including each community's secrets (`community_root`, salt, epoch, + * private-channel keys), so a single event both syncs membership across devices + * and carries the keys needed to re-derive every plane. + * + * This is the Concord analog of NIP-29's kind-10009 `SimpleGroupListEvent` and + * NIP-17's chatroom home base; it is what the Account's `ConcordChannelListState` + * observes. Only the owner's key decrypts it, so relays store ciphertext. + */ +@Immutable +class ConcordCommunityListEvent( + id: HexKey, + pubKey: HexKey, + createdAt: Long, + tags: Array>, + content: String, + sig: HexKey, +) : Event(id, pubKey, createdAt, KIND, tags, content, sig) { + override fun isContentEncoded() = true + + /** Decrypts this list's entries with [signer], or empty on failure / wrong key. */ + suspend fun decrypt(signer: NostrSigner): List = + try { + ConcordCommunityList.decode(signer.nip44Decrypt(content, signer.pubKey)) + } catch (_: Exception) { + emptyList() + } + + companion object { + const val KIND = ConcordKinds.COMMUNITY_LIST + const val ALT = "Private list of joined Concord communities" + + /** The replaceable coordinate for a member's list: `(13302, pubkey, "")`. */ + fun createAddress(pubKey: HexKey) = Address(KIND, pubKey, "") + + /** Builds a signed, self-encrypted list event from [entries]. */ + suspend fun create( + signer: NostrSigner, + entries: List, + createdAt: Long = TimeUtils.now(), + ): ConcordCommunityListEvent { + val content = signer.nip44Encrypt(ConcordCommunityList.encode(entries), signer.pubKey) + return signer.sign(createdAt, KIND, emptyArray(), content) + } + } +} diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/EventFactory.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/EventFactory.kt index a1de1467cb..2efaa46950 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/EventFactory.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/EventFactory.kt @@ -22,6 +22,7 @@ package com.vitorpamplona.quartz.utils +import com.vitorpamplona.quartz.concord.cord02Community.ConcordCommunityListEvent import com.vitorpamplona.quartz.experimental.agora.FundraiserEvent import com.vitorpamplona.quartz.experimental.attestations.attestation.AttestationEvent import com.vitorpamplona.quartz.experimental.attestations.proficiency.AttestorProficiencyEvent @@ -603,6 +604,7 @@ class EventFactory { RootSiteEvent.KIND -> RootSiteEvent(id, pubKey, createdAt, tags, content, sig) RepostEvent.KIND -> RepostEvent(id, pubKey, createdAt, tags, content, sig) RequestToVanishEvent.KIND -> RequestToVanishEvent(id, pubKey, createdAt, tags, content, sig) + ConcordCommunityListEvent.KIND -> ConcordCommunityListEvent(id, pubKey, createdAt, tags, content, sig) SealedRumorEvent.KIND -> SealedRumorEvent(id, pubKey, createdAt, tags, content, sig) SearchRelayListEvent.KIND -> SearchRelayListEvent(id, pubKey, createdAt, tags, content, sig) SimpleGroupListEvent.KIND -> SimpleGroupListEvent(id, pubKey, createdAt, tags, content, sig) diff --git a/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/concord/cord02Community/ConcordCommunityListEventTest.kt b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/concord/cord02Community/ConcordCommunityListEventTest.kt new file mode 100644 index 0000000000..4270f1a469 --- /dev/null +++ b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/concord/cord02Community/ConcordCommunityListEventTest.kt @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.concord.cord02Community + +import com.vitorpamplona.quartz.nip01Core.core.Event +import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair +import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal +import kotlinx.coroutines.test.runTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertIs +import kotlin.test.assertTrue + +class ConcordCommunityListEventTest { + private val signer = NostrSignerInternal(KeyPair()) + + private val entry = + ConcordCommunityListEntry( + id = "11".repeat(32), + owner = "0f".repeat(32), + ownerSalt = "aa".repeat(32), + root = "bb".repeat(32), + rootEpoch = 0, + relays = listOf("wss://relay.example"), + name = "Nostrichs", + ) + + @Test + fun eventFactoryReturnsTypedClassAndDecrypts() = + runTest { + val event = ConcordCommunityListEvent.create(signer, listOf(entry), createdAt = 1L) + assertEquals(ConcordCommunityListEvent.KIND, event.kind) + assertTrue(!event.content.contains("Nostrichs")) // encrypted on the wire + + // A round-trip through JSON parsing resolves to the typed class via EventFactory. + val reparsed = Event.fromJson(event.toJson()) + assertIs(reparsed) + + val entries = reparsed.decrypt(signer) + assertEquals(1, entries.size) + assertEquals("Nostrichs", entries[0].name) + assertEquals(listOf("wss://relay.example"), entries[0].relays) + } + + @Test + fun replaceableAddressIsKindPubkeyEmpty() { + val addr = ConcordCommunityListEvent.createAddress(signer.pubKey) + assertEquals(ConcordCommunityListEvent.KIND, addr.kind) + assertEquals(signer.pubKey, addr.pubKeyHex) + assertEquals("", addr.dTag) + } +}