From 044929396375ab828da59f4169e78784af212eff Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Fri, 23 Oct 2026 18:13:02 +0200 Subject: [PATCH] fix(marmot): a direct message's envelope is opened, not taken for a signing decline FrostSigningEvents.DECLINE and MarmotDirectMessage.ENVELOPE_KIND are both 30330. Recognising protocol kinds first (ceremony-creation Phase 6) returned before the direct-message branch, so every direct message in a current-profile room was dropped unopened. A 30330 that holds a gift wrap is an envelope; only one that does not is a decline. ProtocolKindDispatchJvmTest now opens an envelope through fromGroupEventResult, which no test did before. Co-Authored-By: Claude Opus 5.5 --- .../compose/database/model/ChatMessage.kt | 12 +++- .../model/ProtocolKindDispatchJvmTest.kt | 60 +++++++++++++++---- docs/ceremony-creation.md | 8 +++ 3 files changed, 66 insertions(+), 14 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/database/model/ChatMessage.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/database/model/ChatMessage.kt index 0ad56b00..1a7ca4a3 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/database/model/ChatMessage.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/database/model/ChatMessage.kt @@ -888,8 +888,16 @@ data class ChatMessage( // before its proposal; acted on by the managers // `NostrDao.fileGroupEventResult` dispatches to, which write // their own transcript lines. - val isProtocolMessage = event.kind in DkgRitualEvents.ALL || - event.kind in FrostSigningEvents.ALL + // + // Except a direct message's envelope. It shares 30330 with a + // signing session's decline, and is told apart by what it + // carries: an envelope holds a gift wrap, a decline does not. + // Treating every 30330 as a decline dropped every direct + // message in a current-profile room. + val isDirectMessageEnvelope = MarmotDirectMessage.wrapInside(event) != null + val isProtocolMessage = !isDirectMessageEnvelope && ( + event.kind in DkgRitualEvents.ALL || event.kind in FrostSigningEvents.ALL + ) database.marmotInnerEventDao().upsert( MarmotInnerEvent( diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/database/model/ProtocolKindDispatchJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/database/model/ProtocolKindDispatchJvmTest.kt index a501b23b..339a2ba6 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/database/model/ProtocolKindDispatchJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/database/model/ProtocolKindDispatchJvmTest.kt @@ -8,6 +8,11 @@ import com.vitorpamplona.quartz.nip01Core.core.Kind import com.vitorpamplona.quartz.nip01Core.core.toHexKey import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair import kotlinx.coroutines.runBlocking +import kotlin.test.assertNotNull +import com.machankura.bennu.compose.nostr.MarmotDirectMessage +import com.vitorpamplona.quartz.nip17Dm.messages.ChatMessageEvent +import com.vitorpamplona.quartz.nip01Core.tags.people.PTag +import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerSync import kotlin.test.AfterTest import kotlin.test.Test import kotlin.test.assertEquals @@ -36,13 +41,27 @@ class ProtocolKindDispatchJvmTest { fun closeDb() = database.close() private val self = KeyPair() - private val sender = KeyPair().pubKey.toHexKey() + private val senderSigner = NostrSignerSync(KeyPair()) + private val sender = senderSigner.pubKey private val chatRoomId = "639f".repeat(16) private var next = 0 /** A kind:445 this device has stored, carrying one inner event of [kind]. */ - private suspend fun arrive(kind: Kind, content: String): Pair { + private suspend fun arrive(kind: Kind, content: String): Pair = arrive( + Event( + id = "%064x".format(2000 + next), + pubKey = sender, + createdAt = 1, + kind = kind, + tags = DkgRitualEvents.assembleTags(sessionId = "5e55".repeat(16)), + content = content, + sig = "", + ) + ) + + /** A kind:445 this device has stored, carrying [inner]. */ + private suspend fun arrive(inner: Event): Pair { val n = next++ val groupEventId = "%064x".format(1000 + n) @@ -77,16 +96,6 @@ class ProtocolKindDispatchJvmTest { content = "ciphertext", sig = "0".repeat(128), ) - val inner = Event( - id = "%064x".format(2000 + n), - pubKey = sender, - createdAt = 1, - kind = kind, - tags = DkgRitualEvents.assembleTags(sessionId = "5e55".repeat(16)), - content = content, - sig = "", - ) - val line = ChatMessage.fromGroupEventResult( database = database, activeKeyPair = self, @@ -129,6 +138,33 @@ class ProtocolKindDispatchJvmTest { ) } + /** + * A direct message's envelope shares 30330 with a signing session's decline. + * + * Treating every 30330 as a decline -- which is what recognising protocol + * kinds first did at first -- dropped every direct message in a + * current-profile room before it was opened. What tells the two apart is + * what the event carries. + */ + @Test + fun `a direct message envelope is opened, not taken for a decline`(): Unit = runBlocking { + val wrap = MarmotDirectMessage.wrap( + signer = senderSigner, + recipientPublicKey = self.pubKey.toHexKey(), + kind = ChatMessageEvent.KIND, + createdAt = 1, + tags = arrayOf(PTag.assemble(self.pubKey.toHexKey(), null)), + content = "the vote is at six", + ) + val envelope = MarmotDirectMessage.envelope(sender, self.pubKey.toHexKey(), wrap, createdAt = 1) + assertEquals(FrostSigningEvents.DECLINE, envelope.kind, "the collision this test exists for") + + val (_, line) = arrive(Event.fromJson(envelope.toJson())) + + val opened = assertNotNull(line, "the envelope must reach the direct-message path") + assertTrue(opened.content.contains("the vote is at six"), "and be opened: ${opened.content}") + } + @Test fun `a signing message writes no chat line`(): Unit = runBlocking { FrostSigningEvents.ALL.forEach { kind -> diff --git a/docs/ceremony-creation.md b/docs/ceremony-creation.md index 9f208e7e..b4e8db37 100644 --- a/docs/ceremony-creation.md +++ b/docs/ceremony-creation.md @@ -45,6 +45,14 @@ key** handed over to its finished ceremony, and back returned to its details. Not exercised on a device: the certificate and key state after Tapscript, and starting a ceremony from `CreateSharedKeyScreen` in a group without a key. +**A regression Phase 6 shipped, and its fix.** `FrostSigningEvents.DECLINE` and +`MarmotDirectMessage.ENVELOPE_KIND` are both 30330. Phase 6 returned early for +every protocol kind before the direct-message branch, so every direct message in +a current-profile room was dropped unopened. Found while mapping this fix onto +Curare. An event of kind 30330 that holds a gift wrap is now an envelope, never a +decline -- the test `MarmotDirectMessage.wrapInside` already made -- and +`ProtocolKindDispatchJvmTest` opens one. Any port of Phase 6 has to carry this. + The headline: **a ceremony is started only from a screen that knows what it is for, and the screen that shows a ceremony can never start one.** Today `DkgRitualScreen` does three jobs (showing progress, asking for approvals, and