mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2026-08-11 08:47:33 +00:00
feat(concord): make joined-list a registered Event (13302) for cache/account use
Promote the kind-13302 joined-communities list to a proper Event subclass so the Android LocalCache/Account layer can observe it the way RelayGroupListState observes SimpleGroupListEvent (10009): - ConcordCommunityListEvent : Event — createAddress (13302, pubkey, ""), a suspend create(signer, entries), and decrypt(signer); content stays NIP-44 self-encrypted and carries each community's secrets - register the kind in EventFactory so inbound events parse to the typed class - extract encode/decode JSON helpers in ConcordCommunityList (shared by both) Test: create -> the wire form hides the name, a JSON round-trip resolves to the typed class via EventFactory, decrypt recovers entries, and the replaceable address is (13302, pubkey, ""). Green on :quartz:jvmTest. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CzJ2Cwo8tg4oZq43oRa3ig
This commit is contained in:
+13
-4
@@ -78,11 +78,21 @@ object ConcordCommunityList {
|
||||
entries: List<ConcordCommunityListEntry>,
|
||||
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<ConcordCommunityListEntry>): 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<ConcordCommunityListEntry> =
|
||||
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<ConcordCommunityListEntry> {
|
||||
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()
|
||||
}
|
||||
|
||||
+79
@@ -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<Array<String>>,
|
||||
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<ConcordCommunityListEntry> =
|
||||
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<ConcordCommunityListEntry>,
|
||||
createdAt: Long = TimeUtils.now(),
|
||||
): ConcordCommunityListEvent {
|
||||
val content = signer.nip44Encrypt(ConcordCommunityList.encode(entries), signer.pubKey)
|
||||
return signer.sign(createdAt, KIND, emptyArray(), content)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
+70
@@ -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<ConcordCommunityListEvent>(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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user