diff --git a/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/Context.kt b/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/Context.kt index e2540ebbcb..24e6c9dd07 100644 --- a/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/Context.kt +++ b/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/Context.kt @@ -125,6 +125,24 @@ class Context( fun anyRelays(): Set = relays.normalized("all") + /** + * Seed relays for "look up someone we know nothing about" queries — + * fetching another user's kind:10002 / 10050 / 10051 / 30443 before we + * can deliver something to them. + * + * Strategy: union our own configured relays with Amethyst's hard-coded + * defaults (DefaultNIP65RelaySet + DefaultDMRelayList). The defaults are + * what every fresh Amethyst account publishes to first, so they're the + * most reliable place to find a stranger's replaceable events even when + * we and they have completely disjoint relay configurations. + */ + fun bootstrapRelays(): Set = + buildSet { + addAll(anyRelays()) + addAll(com.vitorpamplona.amethyst.commons.defaults.DefaultNIP65RelaySet) + addAll(com.vitorpamplona.amethyst.commons.defaults.DefaultDMRelayList) + } + /** * Publish an event to the given relays and wait for OK confirmations. * diff --git a/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/commands/GroupAddMemberCommand.kt b/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/commands/GroupAddMemberCommand.kt index 89e1650a60..effd7c9c74 100644 --- a/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/commands/GroupAddMemberCommand.kt +++ b/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/commands/GroupAddMemberCommand.kt @@ -23,13 +23,20 @@ package com.vitorpamplona.amethyst.cli.commands import com.vitorpamplona.amethyst.cli.Context import com.vitorpamplona.amethyst.cli.DataDir import com.vitorpamplona.amethyst.cli.Json +import com.vitorpamplona.quartz.marmot.RecipientRelayFetcher +import com.vitorpamplona.quartz.marmot.mip00KeyPackages.KeyPackageFetcher /** * `group add [ ...]` — fetch each invitee's - * KeyPackage from the union of (our relays + any known KeyPackage relays - * for them) and run the full add-member flow for each one: build commit, - * publish commit to the group's relays, then wrap + publish the Welcome - * gift wrap. + * KeyPackage from the union of (their advertised KeyPackage relays + + * their NIP-65 outbox + our bootstrap relays) and run the full add-member + * flow for each one: build commit, publish commit to the group's relays, + * then wrap + publish the Welcome gift wrap to the invitee's DM inbox + * (kind:10050) with their NIP-65 read relays as a fallback. + * + * Two different users could have completely disjoint relay configurations + * and still successfully marmot each other — we discover where each + * invitee is actually listening before routing anything to them. */ object GroupAddMemberCommand { suspend fun run( @@ -50,15 +57,42 @@ object GroupAddMemberCommand { val invitees = rest.drop(1).map { ctx.requireUserHex(it) } val groupRelays = ctx.marmotGroupRelays(gid).ifEmpty { ctx.outboxRelays() } + // Computed once: the seed relays we query for any stranger's + // published relay-routing events. Union of our own configured + // relays and Amethyst's hard-coded defaults so we stay useful + // when an invitee shares nothing with us but used Amethyst to + // bootstrap. + val seed = ctx.bootstrapRelays() val report = mutableListOf>() for (pub in invitees) { - val relays = - com.vitorpamplona.quartz.marmot.mip00KeyPackages.KeyPackageFetcher - .fetchRelaysFor(emptySet(), emptySet(), ctx.anyRelays()) + // Discover where *this* invitee actually reads from. Without + // this the inviter can only broadcast to their own relays, + // which silently fails the moment the two users have + // disjoint relay configs. + val recipient = + RecipientRelayFetcher.fetchRelayLists( + client = ctx.client, + pubKey = pub, + seedRelays = seed, + ) + + // KeyPackage discovery (MIP-00): prefer the invitee's own + // kind:10051, then their kind:10002 write marker, then our + // bootstrap pool as a last-resort fallback. + val kpRelays = + KeyPackageFetcher.fetchRelaysFor( + targetKeyPackageRelays = recipient.keyPackage, + targetOutbox = recipient.nip65Write(), + myOutbox = seed, + ) val kpEvent = - com.vitorpamplona.quartz.marmot.mip00KeyPackages.KeyPackageFetcher - .fetchKeyPackage(ctx.client, pub, relays, timeoutMs = 10_000) + KeyPackageFetcher.fetchKeyPackage( + client = ctx.client, + targetPubKey = pub, + relays = kpRelays, + timeoutMs = 10_000, + ) if (kpEvent == null) { report.add(mapOf("pubkey" to pub, "status" to "no_key_package")) continue @@ -74,10 +108,32 @@ object GroupAddMemberCommand { // Order matters: commit first (so invitee doesn't join at a future epoch), // then welcome. val commitAck = ctx.publish(commitEvent.signedEvent, groupRelays) - val welcomeAck = + val welcomeTargets: Set = if (welcomeDelivery != null) { - val inbox = ctx.inboxRelays().ifEmpty { ctx.outboxRelays() } - ctx.publish(welcomeDelivery.giftWrapEvent, inbox) + // Welcome gift wrap (kind:1059 wrapping kind:444) must + // land on a relay the invitee actually polls for their + // NIP-59 inbox. Priority: + // 1. kind:10050 (their explicit DM inbox) + // 2. kind:10002 read markers (NIP-65 fallback, + // matches User.dmInboxRelays()) + // 3. Amethyst's DefaultDMRelayList (best-effort if + // the invitee has published nothing — freshly- + // bootstrapped Amethyst accounts listen on these) + // Our own outbox is added as belt-and-braces so we + // can re-ingest the welcome ourselves too. + buildSet { + addAll(recipient.dmInboxOrFallback()) + if (isEmpty()) { + addAll(com.vitorpamplona.amethyst.commons.defaults.DefaultDMRelayList) + } + addAll(ctx.outboxRelays()) + } + } else { + emptySet() + } + val welcomeAck = + if (welcomeDelivery != null && welcomeTargets.isNotEmpty()) { + ctx.publish(welcomeDelivery.giftWrapEvent, welcomeTargets) } else { emptyMap() } @@ -91,6 +147,8 @@ object GroupAddMemberCommand { "welcome_event_id" to welcomeDelivery?.giftWrapEvent?.id, "commit_accepted_by" to commitAck.filterValues { it }.keys.map { it.url }, "welcome_accepted_by" to welcomeAck.filterValues { it }.keys.map { it.url }, + "welcome_targets" to welcomeTargets.map { it.url }, + "key_package_relays" to kpRelays.map { it.url }, ), ) } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/marmot/RecipientRelayFetcher.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/marmot/RecipientRelayFetcher.kt new file mode 100644 index 0000000000..9014af621e --- /dev/null +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/marmot/RecipientRelayFetcher.kt @@ -0,0 +1,134 @@ +/* + * 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.marmot + +import com.vitorpamplona.quartz.marmot.mip00KeyPackages.KeyPackageRelayListEvent +import com.vitorpamplona.quartz.nip01Core.core.HexKey +import com.vitorpamplona.quartz.nip01Core.relay.client.INostrClient +import com.vitorpamplona.quartz.nip01Core.relay.client.accessories.fetchAll +import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter +import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl +import com.vitorpamplona.quartz.nip17Dm.settings.ChatMessageRelayListEvent +import com.vitorpamplona.quartz.nip65RelayList.AdvertisedRelayListEvent + +/** + * One-shot lookup of a user's published relay-routing events. Used by stateless + * callers (the `amy` CLI, automated agents) that don't keep a long-lived + * `LocalCache` around to subscribe to these events as they arrive. + * + * The Amethyst Android app does the equivalent via `cache.getOrCreateUser(pk) + * .dmInboxRelays()` / `.outboxRelays()` after its background subscriptions + * have populated the cache. The CLI doesn't have that pipeline, so it has to + * pull the same three replaceable events on demand whenever it needs to + * deliver something to someone whose relays it doesn't already know. + * + * Specifically required for Marmot (MIP-00..03): + * - kind:10050 — where to deliver the NIP-59 Welcome gift wrap. + * - kind:10051 — where to read MIP-00 KeyPackages from. + * - kind:10002 — fallback for both, plus a generic outbox/inbox advertisement. + */ +object RecipientRelayFetcher { + data class Lists( + /** kind:10050 relays — where the user's NIP-17 / NIP-59 inbox lives. */ + val dmInbox: List, + /** kind:10051 relays — where the user hosts MIP-00 KeyPackages. */ + val keyPackage: List, + /** Latest kind:10002 the user published (null if none seen). */ + val nip65: AdvertisedRelayListEvent?, + ) { + /** Read-marker relays from kind:10002. Mirrors `User.inboxRelays()`. */ + fun nip65Read(): List = nip65?.readRelaysNorm().orEmpty() + + /** Write-marker relays from kind:10002. Mirrors `User.outboxRelays()`. */ + fun nip65Write(): List = nip65?.writeRelaysNorm().orEmpty() + + /** + * Where to deliver a NIP-59 gift wrap addressed to this user. + * Mirrors `User.dmInboxRelays()`: prefer kind:10050, fall back to the + * NIP-65 read marker. + */ + fun dmInboxOrFallback(): List = dmInbox.ifEmpty { nip65Read() } + } + + /** + * Drain the latest kind:10050, 10051, 10002 events for [pubKey] from + * [seedRelays] and return them grouped by kind. Replaceable-event + * semantics: when a relay returns multiple versions, the newest + * `created_at` wins. + * + * Returns empty lists if no relays were given or no events arrived in + * time — callers decide how to fall back. + */ + suspend fun fetchRelayLists( + client: INostrClient, + pubKey: HexKey, + seedRelays: Set, + timeoutMs: Long = 8_000L, + ): Lists { + if (seedRelays.isEmpty()) return Lists(emptyList(), emptyList(), null) + + val filter = + Filter( + kinds = + listOf( + ChatMessageRelayListEvent.KIND, + KeyPackageRelayListEvent.KIND, + AdvertisedRelayListEvent.KIND, + ), + authors = listOf(pubKey), + ) + + val events = + client.fetchAll( + filters = seedRelays.associateWith { listOf(filter) }, + timeoutMs = timeoutMs, + ) + + var dm: ChatMessageRelayListEvent? = null + var kp: KeyPackageRelayListEvent? = null + var nip65: AdvertisedRelayListEvent? = null + for (event in events) { + // Authors filter is enforced relay-side, but a malicious or buggy + // relay could echo something else. Keep the guard so we never + // route someone else's wrapped welcome to the wrong inbox. + if (event.pubKey != pubKey) continue + when (event) { + is ChatMessageRelayListEvent -> { + if (dm == null || event.createdAt > dm.createdAt) dm = event + } + + is KeyPackageRelayListEvent -> { + if (kp == null || event.createdAt > kp.createdAt) kp = event + } + + is AdvertisedRelayListEvent -> { + if (nip65 == null || event.createdAt > nip65.createdAt) nip65 = event + } + } + } + + return Lists( + dmInbox = dm?.relays().orEmpty(), + keyPackage = kp?.relays().orEmpty(), + nip65 = nip65, + ) + } +}