diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseFrostSigningRepository.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseFrostSigningRepository.kt index ed728c30..1cb928e8 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseFrostSigningRepository.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseFrostSigningRepository.kt @@ -7,6 +7,7 @@ import com.vitorpamplona.quartz.nip01Core.core.Kind import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map import to.curare.compose.database.CurareDatabase import to.curare.compose.database.model.FrostSignerMessage import to.curare.compose.database.model.FrostSigningItem @@ -14,6 +15,7 @@ import to.curare.compose.database.model.FrostSigningSession import to.curare.compose.database.model.intermdiate.LocalChatRoom import to.curare.compose.database.model.intermdiate.LocalFrostSigningSession import to.curare.compose.managers.FrostSigningManager +import to.curare.compose.nostr.dkg.DkgRitualEvents import to.curare.compose.repository.FrostSigningRepository class DatabaseFrostSigningRepository( @@ -31,6 +33,14 @@ class DatabaseFrostSigningRepository( override fun observeMessages(sessionId: String): Flow> = database.frostSigningSessionDao().observeMessages(sessionId) + override fun observeShareHolders(dkgSessionId: String): Flow> = + database.dkgSessionDao().observeMessages(dkgSessionId).map { messages -> + messages + .filter { it.kind == DkgRitualEvents.HOST_KEY } + .map { it.participantPublicKey } + .toSet() + } + override fun observeItems(sessionId: String): Flow> = database.frostSigningSessionDao().observeItems(sessionId) diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt index f1f3487a..f16c42ba 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt @@ -32,6 +32,23 @@ interface FrostSigningRepository { fun observeMessages(sessionId: String): Flow> + /** + * Who holds a share of the key a ceremony made, and so can take part in a + * session signing with it. + * + * The members who put a host key into the ceremony -- the same rows + * `FrostSigningManager` places signer ids over, so this cannot disagree with + * whose nonces, partial signatures and refusals the session counts. A + * ceremony cannot complete without every participant's host key, so for a + * completed one this is the whole participant set. + * + * Not the room's roster. A room can hold members who were added after its + * ceremony and hold no share, and a subgroup's ceremony runs in the parent's + * room, which holds everybody the subgroup is not. A screen listing the room + * would show people the session is not waiting on and cannot be answered by. + */ + fun observeShareHolders(dkgSessionId: String): Flow> + /** * The events a session signs, in the order its proposal fixed. * @@ -139,6 +156,9 @@ interface FrostSigningRepository { override fun observeMessages(sessionId: String): Flow> = flowOf(emptyList()) + override fun observeShareHolders(dkgSessionId: String): Flow> = + flowOf(emptySet()) + override fun observeItems(sessionId: String): Flow> = flowOf(emptyList()) diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/FrostSigningScreen.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/FrostSigningScreen.kt index d711a8bd..0bf81af2 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/FrostSigningScreen.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/FrostSigningScreen.kt @@ -194,69 +194,72 @@ fun FrostSigningScreen( // Named rather than counted, for the same reason the ceremony's // ladder names people: "1 of 2" does not tell anyone whose door // to knock on, and a session stalls until somebody knocks. - state.localChatRoom.localParticipants - .distinctBy { it.participant.participantPublicKey } - .forEach { localParticipant -> - val member = localParticipant.participant.participantPublicKey + // + // The share holders, not the room. A member added to the room + // after its key was made holds no share, and so can neither + // sign nor refuse -- listing them would name a door that cannot + // answer. See FrostSigningUIState.Loaded.members. + state.members.forEach { localParticipant -> + val member = localParticipant.participant.participantPublicKey - Card { - ListItem( - leadingContent = { - ProfileAvatar( - publicKey = member, - profile = localParticipant.profile + Card { + ListItem( + leadingContent = { + ProfileAvatar( + publicKey = member, + profile = localParticipant.profile + ) + }, + trailingContent = { + when { + member in state.signed -> Icon( + Icons.Default.CheckCircle, + contentDescription = "Signed", + tint = MaterialTheme.colorScheme.primary ) - }, - trailingContent = { - when { - member in state.signed -> Icon( - Icons.Default.CheckCircle, - contentDescription = "Signed", - tint = MaterialTheme.colorScheme.primary - ) - member in state.offeredNonce -> Icon( - Icons.Default.HourglassEmpty, - contentDescription = "Ready to sign" - ) - - // Quiet rather than red: a refusal the - // session survives is not an error, and - // the one it does not survive ends the - // session, which the status line says. - member in state.declined -> Icon( - Icons.Default.Block, - contentDescription = "Declined", - tint = MaterialTheme.colorScheme.onSurfaceVariant - ) - - else -> Icon( - Icons.Default.RadioButtonUnchecked, - contentDescription = "Not yet", - tint = MaterialTheme.colorScheme.onSurfaceVariant - ) - } - }, - headlineContent = { - Text( - text = localParticipant.profile - ?.humanReadableNameOrPubkey() - ?: member + member in state.offeredNonce -> Icon( + Icons.Default.HourglassEmpty, + contentDescription = "Ready to sign" ) - }, - supportingContent = { - Text( - text = when { - member in state.signed -> stringResource(Res.string.signed_their_part) - member in state.offeredNonce -> stringResource(Res.string.ready_to_sign) - member in state.declined -> stringResource(Res.string.declined_to_sign) - else -> stringResource(Res.string.has_not_taken_part_yet) - } + + // Quiet rather than red: a refusal the + // session survives is not an error, and + // the one it does not survive ends the + // session, which the status line says. + member in state.declined -> Icon( + Icons.Default.Block, + contentDescription = "Declined", + tint = MaterialTheme.colorScheme.onSurfaceVariant + ) + + else -> Icon( + Icons.Default.RadioButtonUnchecked, + contentDescription = "Not yet", + tint = MaterialTheme.colorScheme.onSurfaceVariant ) } - ) - } + }, + headlineContent = { + Text( + text = localParticipant.profile + ?.humanReadableNameOrPubkey() + ?: member + ) + }, + supportingContent = { + Text( + text = when { + member in state.signed -> stringResource(Res.string.signed_their_part) + member in state.offeredNonce -> stringResource(Res.string.ready_to_sign) + member in state.declined -> stringResource(Res.string.declined_to_sign) + else -> stringResource(Res.string.has_not_taken_part_yet) + } + ) + } + ) } + } // Asked rather than re-derived: the manager owns when a session // is still waiting on its owner, and a second copy of that rule diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/FrostSigningViewModel.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/FrostSigningViewModel.kt index 3f123d96..1f730feb 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/FrostSigningViewModel.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/FrostSigningViewModel.kt @@ -13,9 +13,13 @@ import co.touchlab.kermit.Logger import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.core.HexKey import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.IO import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.launch +import to.curare.compose.database.model.FrostSignerMessage import to.curare.compose.database.model.FrostSigningItem import to.curare.compose.database.model.FrostSigningSession import to.curare.compose.nostr.frost.FrostSigningEvents @@ -44,7 +48,12 @@ class FrostSigningViewModel( * * A session moves on messages arriving from other members, so a screen that * read it once would sit still while the rest of the group signed around it. + * + * Who can take part is read off the ceremony the session names, not off the + * room: the room can hold members who were added after its key was made and + * hold no share of it, and the screen is a list of whose door to knock on. */ + @OptIn(ExperimentalCoroutinesApi::class) fun initiate() { viewModelScope.launch(Dispatchers.IO) { val localChatRoom = chatRepository.getChatRoomByIdentifier(chatRoomId) @@ -55,16 +64,24 @@ class FrostSigningViewModel( frostSigningUIState = FrostSigningUIState.Loaded(localChatRoom = localChatRoom) + val sessions = frostSigningRepository.observeSessionById(sessionId) + val shareHolders = sessions.flatMapLatest { session -> + session?.let { frostSigningRepository.observeShareHolders(it.dkgSessionId) } + ?: flowOf(emptySet()) + } + combine( - frostSigningRepository.observeSessionById(sessionId), + sessions, frostSigningRepository.observeMessages(sessionId), - frostSigningRepository.observeItems(sessionId) - ) { session, messages, items -> Triple(session, messages, items) } - .collect { (session, messages, items) -> + frostSigningRepository.observeItems(sessionId), + shareHolders + ) { session, messages, items, holders -> Observed(session, messages, items, holders) } + .collect { (session, messages, items, holders) -> frostSigningUIState = FrostSigningUIState.Loaded( localChatRoom = localChatRoom, session = session, items = items, + shareHolders = holders, offeredNonce = messages .filter { it.kind == FrostSigningEvents.NONCE } .map { it.signerPublicKey } @@ -82,6 +99,14 @@ class FrostSigningViewModel( } } + /** One emission of everything the screen is drawn from -- the fourth flow outgrew `Triple`. */ + private data class Observed( + val session: FrostSigningSession?, + val messages: List, + val items: List, + val shareHolders: Set, + ) + /** The events the group is being asked to sign, for showing them before they agree. */ fun proposedEvents(items: List): List = items.mapNotNull { Event.fromJsonOrNull(it.unsignedEventJson) } diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/state/FrostSigningUIState.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/state/FrostSigningUIState.kt index 7a6d410f..cbcedb3b 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/state/FrostSigningUIState.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/state/FrostSigningUIState.kt @@ -3,7 +3,9 @@ package to.curare.compose.ui.view.state import com.vitorpamplona.quartz.nip01Core.core.HexKey import to.curare.compose.database.model.FrostSigningItem import to.curare.compose.database.model.FrostSigningSession +import to.curare.compose.database.model.Participant import to.curare.compose.database.model.intermdiate.LocalChatRoom +import to.curare.compose.database.model.intermdiate.LocalParticipant sealed interface FrostSigningUIState { data class Loaded( @@ -31,7 +33,50 @@ sealed interface FrostSigningUIState { * one refusal from ending and one that is nowhere near. */ val declined: Set = emptySet(), - ): FrostSigningUIState + + /** + * Who holds a share of the key the session signs with -- the ceremony's + * participants, read off the ceremony rather than the room. Empty until + * the session has been collected, and so until [members] has anyone in it. + */ + val shareHolders: Set = emptySet(), + ): FrostSigningUIState { + /** + * The members the session can be answered by, as rows to draw. + * + * The share holders and nobody else. The room's roster is the wrong + * list twice over: a room can hold members added after its key was + * made, who hold no share and cannot sign or refuse, and a subgroup's + * ceremony room is the parent, which holds everybody the subgroup is + * not. The list is whose door to knock on, and a door that cannot + * answer does not belong on it. + * + * Profiles still come off the room, since that is where the rows with + * names in them are, in the order the room keeps them. A share holder + * the room no longer holds -- one who left after the ceremony -- still + * has a share and a place in the count, so they get a placeholder row + * rather than being dropped. + */ + val members: List get() { + val roomMembers = localChatRoom.localParticipants + .distinctBy { it.participant.participantPublicKey } + .associateBy { it.participant.participantPublicKey } + + val inRoom = roomMembers.values.filter { it.participant.participantPublicKey in shareHolders } + val elsewhere = (shareHolders - roomMembers.keys).sorted().map { publicKey -> + LocalParticipant( + Participant( + participantPublicKey = publicKey, + chatRoomId = localChatRoom.chatRoom.id, + relayHint = null + ), + null + ) + } + + return inRoom + elsewhere + } + } data class Error( val message: String diff --git a/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/FrostSigningMembersTest.kt b/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/FrostSigningMembersTest.kt new file mode 100644 index 00000000..731226d4 --- /dev/null +++ b/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/FrostSigningMembersTest.kt @@ -0,0 +1,112 @@ +package to.curare.compose.ui.view.state + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull +import to.curare.compose.database.model.ChatRoom +import to.curare.compose.database.model.Participant +import to.curare.compose.database.model.Profile +import to.curare.compose.database.model.intermdiate.LocalChatRoom +import to.curare.compose.database.model.intermdiate.LocalParticipant + +/** + * Who the signing screen lists as able to answer a proposal. + * + * A room is not the set that can sign with its key. A member added after the + * ceremony holds no share, and the proposal they receive is dropped before it + * becomes a session on their device -- so they can neither sign nor refuse, + * and a row for them names a door that cannot answer. The list is the + * ceremony's share holders, with the room supplying names. + */ +class FrostSigningMembersTest { + private val alice = "a".repeat(64) + private val bob = "b".repeat(64) + private val carol = "c".repeat(64) + + /** In the room, invited after the key was made, and holding no share of it. */ + private val newcomer = "d".repeat(64) + + private fun room(vararg members: String) = LocalChatRoom( + chatRoom = ChatRoom( + id = "11".repeat(32), + userPublicKey = alice, + subject = "#Ekklesia", + description = null, + mlsGroupState = null, + ), + localParticipants = members.map { publicKey -> + LocalParticipant( + participant = Participant( + participantPublicKey = publicKey, + chatRoomId = "11".repeat(32), + relayHint = null, + ), + profile = Profile( + publicKey = publicKey, + userName = publicKey.take(1).uppercase(), + nostrEventId = "e".repeat(64), + ), + ) + }, + ) + + private fun List.keys() = map { it.participant.participantPublicKey } + + @Test + fun `a member who holds no share is not listed`() { + val state = FrostSigningUIState.Loaded( + localChatRoom = room(alice, bob, newcomer, carol), + shareHolders = setOf(alice, bob, carol) + ) + + // The newcomer is in the room and not in the ceremony. The session is + // not waiting on them and cannot be answered by them. + assertEquals(listOf(alice, bob, carol), state.members.keys()) + } + + @Test + fun `names come off the room, in the room's order`() { + val state = FrostSigningUIState.Loaded( + localChatRoom = room(carol, alice, bob), + shareHolders = setOf(alice, bob, carol) + ) + + assertEquals(listOf(carol, alice, bob), state.members.keys()) + assertEquals(listOf("C", "A", "B"), state.members.map { it.profile?.userName }) + } + + @Test + fun `a share holder the room no longer holds still has a row`() { + // They left the room after the ceremony. Their share did not leave with + // them: the count still needs them, or needs to know they refused. + val state = FrostSigningUIState.Loaded( + localChatRoom = room(alice, bob), + shareHolders = setOf(alice, bob, carol) + ) + + assertEquals(listOf(alice, bob, carol), state.members.keys()) + assertNull(state.members.last().profile) + } + + @Test + fun `a room that holds a member twice lists them once`() { + // Participant rows are keyed on an autogenerated id, so a room can + // carry the same person twice; the list is of people. + val state = FrostSigningUIState.Loaded( + localChatRoom = room(alice, bob, bob), + shareHolders = setOf(alice, bob) + ) + + assertEquals(listOf(alice, bob), state.members.keys()) + } + + @Test + fun `nobody is listed until the ceremony has been read`() { + // The first emission has the room and not yet the session it names. + // Falling back to the room here would flash the wrong list -- everybody + // -- and then correct itself, which is worse than a moment of nobody. + val state = FrostSigningUIState.Loaded(localChatRoom = room(alice, bob, newcomer)) + + assertEquals(emptyList(), state.members) + } +} diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningDeclineJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningDeclineJvmTest.kt index b293db74..c9e7d254 100644 --- a/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningDeclineJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningDeclineJvmTest.kt @@ -16,6 +16,7 @@ import kotlin.test.assertNotNull import kotlin.test.assertNull import kotlin.test.assertTrue import kotlin.time.Clock +import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import to.curare.compose.database.CurareDatabase import to.curare.compose.database.builder.getRoomDatabase @@ -27,6 +28,7 @@ import to.curare.compose.database.model.FrostSigningItem import to.curare.compose.database.model.FrostSigningSession import to.curare.compose.database.model.NostrEvent import to.curare.compose.database.model.Profile +import to.curare.compose.database.repository.DatabaseFrostSigningRepository import to.curare.compose.database.model.intermdiate.LocalChatRoom import to.curare.compose.database.model.types.DkgRitualStage import to.curare.compose.database.model.types.FrostSigningStage @@ -434,6 +436,23 @@ class FrostSigningDeclineJvmTest { assertEquals(FrostSigningStage.COLLECTING_NONCES, session(session.id).stage) } + @Test + fun `who can answer is read off the ceremony, not off the room`() = runBlocking { + openDevice() + val session = propose() + + // A member invited to the room after the key was made holds no share: + // the proposal is dropped before it becomes a session on their device, + // and no refusal or nonce of theirs would count. What the screen lists + // is who put a host key into the ceremony -- the same rows the manager + // places signer ids over -- and the room's roster is never consulted. + val shareHolders = DatabaseFrostSigningRepository(db, this) + .observeShareHolders(session.dkgSessionId) + .first() + + assertEquals(setOf(alice, bob, carol), shareHolders) + } + @Test fun `a refusal after an approval is ignored`() = runBlocking { openDevice(owner = bob)