fix(dkg): a parent's second subgroup is not shown its first one's signing sessions

A subgroup's ceremony runs in its parent's room, so that room holds a
certificate and a key-state signing session for every subgroup made under
it. The ceremony screen took the newest of each kind regardless of which
subgroup it was for, so a parent's second subgroup opened on the first
one's finished certificate, with "Ask the parent group to certify"
already disabled.

Each session is now matched on what its event names: the certificate on
the subgroup's threshold key, the key state on the ceremony. The newest
match still wins, so a failed request can be asked for again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-10-23 17:59:34 +02:00
1 parent dba3df697b
commit af4304aaae
2 files changed
+159 -16

No files matched your search

@@ -135,20 +135,25 @@ class DkgRitualViewModel(
val sessions = dkgRepository.observeSessionById(dkgSessionId)
// The session in which the group agrees what its admin room will sign
// with. The room a ceremony runs in signs nothing else, so the newest
// session in it is the key state's -- but it is matched on the kind it
// carries anyway, so a room that one day signs something else does not
// confuse the two.
// with. Matched on the ceremony the state names, not on being the newest
// key state in the room: a subgroup's ceremony runs in its parent's room,
// and so does every other subgroup's, so the newest one there is as
// likely a sibling's as this one's.
val keyStateSession = dkgRepository.observeSigningSessions(chatRoomId)
.map { it.firstCarrying(GroupKeyStateEvent.KIND) }
.map { sessions ->
sessions.firstCarrying(GroupKeyStateEvent.KIND) {
GroupKeyStateEvent.parseDkgSessionId(it.tags) == dkgSessionId
}
}
// Everything about one ceremony, re-pointed when the ceremony or its
// purpose changes -- which for a member arriving cold is once -- and not
// on every change to the session row.
// Everything about one ceremony, re-pointed when the ceremony, its
// purpose or its key changes -- which for a member arriving cold is at
// most twice, the key arriving as the ceremony finishes -- and not on
// every change to the session row.
val parts = sessions
.map { it?.id to it?.parentChatRoomId }
.map { Triple(it?.id, it?.parentChatRoomId, it?.thresholdPublicKey) }
.distinctUntilChanged()
.flatMapLatest { (sessionId, parent) -> ceremonyParts(sessionId, parent) }
.flatMapLatest { (sessionId, parent, key) -> ceremonyParts(sessionId, parent, key) }
return combine(
sessions,
@@ -200,8 +205,19 @@ class DkgRitualViewModel(
* rather than this room's: it is made where the parent's key can sign it,
* which is never the ceremony's room. An ordinary ceremony passes no parent
* and gets nothing watched there, which is exactly right.
*
* The parent's room holds a certificate session for every subgroup it has
* been asked about, so the one shown is the one naming [thresholdPublicKey].
* Taking the newest regardless is how a parent's second subgroup opened on
* its first one's finished certificate, with the button to ask for its own
* already disabled. Before the ceremony has a key there is nothing to have
* certified, and no session is this one's.
*/
private fun ceremonyParts(sessionId: String?, parentChatRoomId: HexKey?): Flow<CeremonyParts> {
private fun ceremonyParts(
sessionId: String?,
parentChatRoomId: HexKey?,
thresholdPublicKey: HexKey?,
): Flow<CeremonyParts> {
if (sessionId == null) return flowOf(CeremonyParts(sessionId = null))
val certified = parentChatRoomId
@@ -209,9 +225,14 @@ class DkgRitualViewModel(
?: flowOf(false)
val certificateSession = parentChatRoomId
?.takeIf { thresholdPublicKey != null }
?.let { parent ->
dkgRepository.observeSigningSessions(parent)
.map { it.firstCarrying(SubgroupBirthCertificateEvent.KIND) }
dkgRepository.observeSigningSessions(parent).map { sessions ->
sessions.firstCarrying(SubgroupBirthCertificateEvent.KIND) {
SubgroupBirthCertificateEvent.parseThresholdPublicKey(it.tags)
.equals(thresholdPublicKey, ignoreCase = true)
}
}
}
?: flowOf(null)
@@ -240,11 +261,19 @@ class DkgRitualViewModel(
}
}
/** The signing session carrying an event of [kind], if there is one. */
private fun List<LocalFrostSigningSession>.firstCarrying(kind: Kind): FrostSigningSession? =
/**
* The newest signing session carrying an event of [kind] that is [about]
* this ceremony, if there is one.
*/
private fun List<LocalFrostSigningSession>.firstCarrying(
kind: Kind,
about: (Event) -> Boolean,
): FrostSigningSession? =
firstOrNull { local ->
local.items.any { item ->
runCatching { Event.fromJson(item.unsignedEventJson).kind }.getOrNull() == kind
runCatching { Event.fromJson(item.unsignedEventJson) }
.getOrNull()
?.let { it.kind == kind && about(it) } == true
}
}?.session
@@ -1,7 +1,10 @@
package com.machankura.bennu.compose.ui.view.model
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerSync
import fr.acinq.bitcoin.ByteVector32
import fr.acinq.bitcoin.PrivateKey
import fr.acinq.phoenix.utils.preferences.InternalPrefs
@@ -27,13 +30,19 @@ import org.junit.rules.TemporaryFolder
import com.machankura.bennu.compose.database.model.ChatRoom
import com.machankura.bennu.compose.database.model.DkgParticipantMessage
import com.machankura.bennu.compose.database.model.DkgSession
import com.machankura.bennu.compose.database.model.FrostSigningItem
import com.machankura.bennu.compose.database.model.FrostSigningSession
import com.machankura.bennu.compose.database.model.intermdiate.LocalChatRoom
import com.machankura.bennu.compose.database.model.intermdiate.LocalFrostSigningSession
import com.machankura.bennu.compose.database.model.types.DkgApprovalStep
import com.machankura.bennu.compose.database.model.types.DkgRitualStage
import com.machankura.bennu.compose.database.model.types.FrostSigningStage
import com.machankura.bennu.compose.identity.Identity
import com.machankura.bennu.compose.identity.IdentityKind
import com.machankura.bennu.compose.identity.toWalletId
import com.machankura.bennu.compose.nostr.dkg.DkgRitualEvents
import com.machankura.bennu.compose.nostr.frost.GroupKeyStateEvent
import com.machankura.bennu.compose.nostr.subgroup.SubgroupBirthCertificateEvent
import com.machankura.bennu.compose.repository.ChatRepository
import com.machankura.bennu.compose.repository.DkgRepository
import com.machankura.bennu.compose.ui.composable.navigation.routes.CreateSharedKeyRoute
@@ -46,6 +55,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.assertNull
import kotlin.test.assertTrue
/**
@@ -112,6 +122,7 @@ class DkgRitualViewModelJvmTest {
private val sessionRow = MutableStateFlow<DkgSession?>(session())
private val messages = MutableStateFlow<List<DkgParticipantMessage>>(emptyList())
private val signingSessions = MutableStateFlow<List<LocalFrostSigningSession>>(emptyList())
/** How many times the screen started watching its ceremony. */
private var sessionSubscriptions = 0
@@ -126,6 +137,9 @@ class DkgRitualViewModelJvmTest {
override fun observeMessages(sessionId: String): Flow<List<DkgParticipantMessage>> = messages
override fun observeSigningSessions(chatRoomId: String): Flow<List<LocalFrostSigningSession>> =
signingSessions
// A deliberately slow answer, so a read-modify-write around it would have
// the widest possible window to lose somebody else's write in.
override suspend fun pendingApproval(session: DkgSession): DkgApprovalStep? {
@@ -309,4 +323,104 @@ class DkgRitualViewModelJvmTest {
assertEquals(listOf(DkgApprovalStep.HOST_KEY), approved)
assertFalse(viewModel.isActionPending.value)
}
/** A signing session in this room over one event of [kind]. */
private fun signing(
id: String,
kind: Int,
tags: Array<Array<String>>,
content: String,
stage: FrostSigningStage = FrostSigningStage.COMPLETE,
): LocalFrostSigningSession {
val event = NostrSignerSync(KeyPair()).sign<Event>(
createdAt = 1_700_000_000L,
kind = kind,
tags = tags,
content = content,
)
return LocalFrostSigningSession(
session = FrostSigningSession(
id = id,
chatRoomId = chatRoomId,
coordinatorPublicKey = bob,
userPublicKey = ada,
dkgSessionId = sessionId,
threshold = 2,
participantCount = 3,
signerId = 1,
stage = stage,
),
unorderedItems = listOf(
FrostSigningItem(
sessionId = id,
itemIndex = 0,
unsignedEventJson = event.toJson(),
eventId = event.id,
nonceRandom = "cc".repeat(32),
)
),
)
}
@Test
fun `a parent's second subgroup is not shown its first one's signing sessions`(): Unit = runBlocking {
// The ceremony is held in the parent's room, so this room is the parent's
// -- and it already holds the first subgroup's finished certificate and
// key state, newest first, as the store returns them.
val firstKey = "02" + "1f".repeat(32)
val secondKey = "02" + "2f".repeat(32)
sessionRow.value = session(DkgRitualStage.COMPLETE).copy(
parentChatRoomId = chatRoomId,
thresholdPublicKey = secondKey,
)
signingSessions.value = listOf(
signing(
id = "first-key-state",
kind = GroupKeyStateEvent.KIND,
tags = GroupKeyStateEvent.assembleTags(chatRoomId = "f1".repeat(32), dkgSessionId = "f1"),
content = firstKey,
),
signing(
id = "first-certificate",
kind = SubgroupBirthCertificateEvent.KIND,
tags = SubgroupBirthCertificateEvent.assembleTags(
subgroupChatRoomId = "f1".repeat(32),
parentChatRoomId = chatRoomId,
thresholdPublicKey = firstKey,
adminPublicKeys = listOf(ada, bob),
name = "First",
),
content = "f1".repeat(32),
),
)
val viewModel = viewModel()
viewModel.initiate()
val before = viewModel.awaitLoaded()
assertNull(before.certificateSession, "the first subgroup's certificate is not this one's")
assertNull(before.keyStateSession, "nor is its key state")
// Its own, once asked for, is the one shown.
signingSessions.value = listOf(
signing(
id = "second-certificate",
kind = SubgroupBirthCertificateEvent.KIND,
tags = SubgroupBirthCertificateEvent.assembleTags(
subgroupChatRoomId = "f2".repeat(32),
parentChatRoomId = chatRoomId,
thresholdPublicKey = secondKey,
adminPublicKeys = listOf(ada, bob),
name = "Second",
),
content = "f2".repeat(32),
stage = FrostSigningStage.COLLECTING_NONCES,
),
) + signingSessions.value
assertEquals(
"second-certificate",
viewModel.awaitLoaded { it.certificateSession != null }.certificateSession?.id,
)
}
}