diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 733a0b2c..d1cf168a 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -114,6 +114,13 @@ Rotate shares without them? They are out of the room, but their share of the group key still helps sign until the shares are rotated without them. Rotating asks every remaining member to take part. Shares held before the rotation stop working once every device has deleted them. Rotate shares + Rotate the shares? + Taking part: %1$s + Left out until they are brought back: %1$s + Left out for good: %1$s + Each member taking part is asked once. Rotating counts as your own agreement, so you are not asked again. + This group has no key this device can rotate + This rotation cannot start: %1$s Shares Generation %1$d Holds a share diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt index 1a119258..0c99990f 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt @@ -43,6 +43,7 @@ import mobi.giza.compose.identity.Identity import mobi.giza.compose.identity.toKeyPair import mobi.giza.compose.managers.ShareRefreshManager import mobi.giza.compose.ui.view.state.SharesSummary +import mobi.giza.compose.ui.view.state.RotationPreview import mobi.giza.compose.database.model.types.ShareStanding import mobi.giza.compose.database.model.types.ShareRepairStage import mobi.giza.compose.database.model.types.ShareRefreshStage @@ -704,6 +705,20 @@ class DatabaseChatRepository( return failureOf(runCatching { ShareRefreshManager.proposeRefresh(database, localChatRoom, me, privateKey, key) }.exceptionOrNull()) } + override suspend fun rotationPreview(localChatRoom: LocalChatRoom, without: HexKey?): RotationPreview? = runCatching { + if (!ExperimentalFeatures.shareRefresh) return null + val me = localChatRoom.chatRoom.userPublicKey + val preview = ShareRefreshManager.preview(database, localChatRoom, me, without) ?: return null + val names = localChatRoom.localParticipants.associate { it.participant.participantPublicKey to it.profile?.humanReadableNameOrPubkey() } + fun nameOf(member: HexKey) = names[member] ?: member.take(8) + RotationPreview( + takingPart = preview.takingPart.map(::nameOf), + untilBroughtBack = preview.withheld.filterValues { it == ShareStanding.STALE }.keys.map(::nameOf), + forGood = preview.withheld.filterValues { it == ShareStanding.RETIRED }.keys.map(::nameOf), + refusal = preview.refusal, + ) + }.getOrNull() + override suspend fun approveShares(localChatRoom: LocalChatRoom, approval: SharesSummary.PendingApproval): String? { val (_, privateKey) = privateKeyOrNull() ?: return "This identity cannot sign" return failureOf(runCatching { diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/ShareRefreshManager.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/ShareRefreshManager.kt index 99fea167..61b551e2 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/ShareRefreshManager.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/ShareRefreshManager.kt @@ -111,13 +111,75 @@ object ShareRefreshManager { // ---- proposing ------------------------------------------------------------------------------------------------ + /** + * Who a refresh of [key] from [shares] would deal to and who it would leave + * out, with [withheld] asked for: every live participant not in it is a + * recipient, and every stale participant is withheld again, as what they + * were -- a refresh does not bring anybody back, a repair does. [refusal] is + * why it cannot run, or null. One function for [proposeRefresh] and + * [preview], so the dialog shows exactly what the proposal will say. + */ + private fun plan(key: DkgSession, shares: ShareGenerations.Shares, userPublicKey: HexKey, withheld: Map): Plan { + val recipients = shares.roster + .filter { it.standing == ShareStanding.LIVE && it.participantId !in withheld } + .map { it.participantId } + val allWithheld = withheld + shares.roster + .filter { it.standing == ShareStanding.STALE && it.participantId !in withheld } + .associate { it.participantId to ShareStanding.STALE } + val myId = shares.signerIds[userPublicKey] + val refusal = when { + shares.secretShare == null -> "this device holds no share of generation ${shares.generation}" + myId == null -> "this device is not a signer of generation ${shares.generation}" + else -> refusal(key, shares, recipients, allWithheld) + ?: "the proposer has to be a recipient".takeIf { myId !in recipients } + } + return Plan(recipients, allWithheld, refusal) + } + + private data class Plan(val recipients: List, val withheld: Map, val refusal: String?) + + /** A refresh as it would be proposed, by member rather than by id. */ + data class Preview( + val takingPart: List, + val withheld: Map, + /** Why it cannot be proposed, or null. */ + val refusal: String?, + ) + + /** + * What [proposeRefresh] would open from this device, without opening it: who + * takes part and who is left out, stale members included, and whether it + * would be refused. The proposer's dialog shows this, and pressing it is the + * proposer's agreement to take part (docs/rotate-shares-single-approval.md, + * Phase 4). [without] is a member to withhold for good, as after a removal. + */ + suspend fun preview(database: GizaDatabase, localChatRoom: LocalChatRoom, userPublicKey: HexKey, without: HexKey? = null): Preview? { + val key = FrostSigningManager.completedKey(database, localChatRoom.chatRoom.id) ?: return null + if (!isKeysRoom(database, localChatRoom, key)) return null + val shares = ShareGenerations.active(database, key) + val withheld = without?.let { member -> + val id = shares.signerIds[member] ?: return Preview(emptyList(), emptyMap(), "they hold no share of the group's key") + mapOf(id to ShareStanding.RETIRED) + } ?: emptyMap() + val plan = plan(key, shares, userPublicKey, withheld) + val memberOf = shares.roster.associate { it.participantId to it.memberPublicKey } + return Preview( + takingPart = plan.recipients.mapNotNull { memberOf[it] }, + withheld = plan.withheld.mapNotNull { (id, standing) -> memberOf[id]?.let { it to standing } }.toMap(), + refusal = plan.refusal ?: running(database, key)?.let { "a rotation of this key is already running" }, + ) + } + /** * Opens a refresh of [key], making this device the coordinator. Every live - * participant not in [withheld] is a recipient. + * participant not in [withheld] is a recipient, and every stale one is + * withheld again ([plan]). * - * The proposer is auto-approved for nothing: proposing a refresh is not - * agreeing to withhold whomever the form defaulted to, so it is asked at both - * gates like everyone else. + * **Proposing is the proposer's agreement to take part**: the proposer's + * device deals at once, and its owner is not asked again. That is safe only + * because the proposal is made from a dialog that named everyone it leaves + * out ([preview]) -- proposing is agreeing to withhold whoever the form + * showed, never whoever it defaulted to unseen. */ suspend fun proposeRefresh( database: GizaDatabase, @@ -134,19 +196,8 @@ object ShareRefreshManager { running(database, key)?.let { return it } val shares = ShareGenerations.active(database, key) - requireNotNull(shares.secretShare) { "This device holds no share of generation ${shares.generation}" } - val myId = requireNotNull(shares.signerIds[userPublicKey]) { "This device is not a signer of generation ${shares.generation}" } - - val recipients = shares.roster - .filter { it.standing == ShareStanding.LIVE && it.participantId !in withheld } - .map { it.participantId } - // Stale participants are withheld again, as what they were: a refresh does - // not bring anybody back. That is a repair. - val allWithheld = withheld + shares.roster - .filter { it.standing == ShareStanding.STALE && it.participantId !in withheld } - .associate { it.participantId to ShareStanding.STALE } - refusal(key, shares, recipients, allWithheld)?.let { throw IllegalStateException("Cannot refresh: $it") } - require(myId in recipients) { "The proposer has to be a recipient" } + val (recipients, allWithheld, refused) = plan(key, shares, userPublicKey, withheld) + refused?.let { throw IllegalStateException("Cannot refresh: $it") } val session = ShareRefreshSession( id = RandomInstance.bytes(32).toHex(), @@ -159,6 +210,7 @@ object ShareRefreshManager { withheld = ShareRefreshSession.formatWithheld(allWithheld), round1Random = RandomInstance.bytes(32).toHex(), auxRandom = RandomInstance.bytes(32).toHex(), + dealApprovedAt = Clock.System.now(), ) database.shareRefreshDao().upsert(session) announce(database, session, ChatMessage.TYPE_SHARE_REFRESH_STARTED, startedLine(session, shares), userPublicKey) diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt index f8a61f1f..57eac81f 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt @@ -22,6 +22,7 @@ import mobi.giza.compose.managers.SharedKeyDerivation import mobi.giza.compose.managers.SubgroupManager import mobi.giza.compose.nostr.MarmotProfile import mobi.giza.compose.ui.view.state.SharesSummary +import mobi.giza.compose.ui.view.state.RotationPreview interface ChatRepository { suspend fun observeChatRoomListByPublicKey(publicKey: String): Flow> @@ -287,6 +288,13 @@ interface ChatRepository { /** Proposes rotating every share of the room's key. Null on success, or why not. */ suspend fun rotateShares(localChatRoom: LocalChatRoom): String? + /** + * Who a rotation proposed from this device would take part and who it would + * leave out -- [without] for good, as after a removal -- or null when the room + * has no key this device can rotate. Nothing is sent. + */ + suspend fun rotationPreview(localChatRoom: LocalChatRoom, without: HexKey? = null): RotationPreview? + /** Answers what [approval] asks. Null on success, or why not. */ suspend fun approveShares(localChatRoom: LocalChatRoom, approval: SharesSummary.PendingApproval): String? @@ -494,6 +502,8 @@ interface ChatRepository { override suspend fun rotateShares(localChatRoom: LocalChatRoom): String? = null + override suspend fun rotationPreview(localChatRoom: LocalChatRoom, without: HexKey?): RotationPreview? = null + override suspend fun approveShares(localChatRoom: LocalChatRoom, approval: SharesSummary.PendingApproval): String? = null override suspend fun abandonRotation(localChatRoom: LocalChatRoom, sessionId: String): String? = null diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt index 2b3ac801..abbc23f5 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt @@ -124,6 +124,14 @@ import mobi.giza.compose.resources.not_now import mobi.giza.compose.resources.rotate_shares import mobi.giza.compose.resources.rotate_shares_without_body import mobi.giza.compose.resources.rotate_shares_without_title +import mobi.giza.compose.resources.rotate_shares_title +import mobi.giza.compose.resources.rotation_cannot_start +import mobi.giza.compose.resources.rotation_left_out_for_good +import mobi.giza.compose.resources.rotation_left_out_until_brought_back +import mobi.giza.compose.resources.rotation_proposing_is_taking_part +import mobi.giza.compose.resources.rotation_taking_part +import mobi.giza.compose.resources.rotation_no_key +import mobi.giza.compose.ui.view.state.RotationPreview import androidx.compose.material3.AlertDialog import mobi.giza.compose.resources.disband_group import mobi.giza.compose.resources.disband_group_explainer @@ -185,6 +193,7 @@ fun ChatRoomDetailScreen( val removalRequested = stringResource(Res.string.member_removal_requested) val rotationRequested = stringResource(Res.string.share_rotation_requested) val sharesActionSent = stringResource(Res.string.shares_action_sent) + val noKeyToRotate = stringResource(Res.string.rotation_no_key) // Offered after removing a member who still holds a share of the room's key: // the removal took them out of the room, not out of the key @@ -193,9 +202,19 @@ fun ChatRoomDetailScreen( AlertDialog( onDismissRequest = { chatRoomDetailViewModel.dismissRotationOffer() }, title = { Text(stringResource(Res.string.rotate_shares_without_title)) }, - text = { Text(stringResource(Res.string.rotate_shares_without_body)) }, + text = { + Column(verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap)) { + Text(stringResource(Res.string.rotate_shares_without_body)) + // The removed member is named by the title; the others it would leave out are not. + offer.preview?.let { RotationLeftOut(it.copy(forGood = emptyList())) } + offer.preview?.refusal?.let { Text(stringResource(Res.string.rotation_cannot_start, it), color = MaterialTheme.colorScheme.error) } + } + }, confirmButton = { - TextButton(onClick = { chatRoomDetailViewModel.rotateSharesWithout(offer) { failure -> notify(failure ?: rotationRequested) } }) { + TextButton( + enabled = offer.preview?.refusal == null, + onClick = { chatRoomDetailViewModel.rotateSharesWithout(offer) { failure -> notify(failure ?: rotationRequested) } } + ) { Text(stringResource(Res.string.rotate_shares)) } }, @@ -207,6 +226,40 @@ fun ChatRoomDetailScreen( ) } + // The proposer's one approval: the rotation as it will be proposed, with + // everyone it leaves out named (docs/rotate-shares-single-approval.md, Phase 4). + chatRoomDetailViewModel.rotationProposal?.let { proposal -> + AlertDialog( + onDismissRequest = { chatRoomDetailViewModel.dismissRotationProposal() }, + title = { Text(stringResource(Res.string.rotate_shares_title)) }, + text = { + Column(verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap)) { + Text(stringResource(Res.string.rotation_taking_part, proposal.preview.takingPart.joinToString(", "))) + RotationLeftOut(proposal.preview) + val refusal = proposal.preview.refusal + if (refusal != null) { + Text(stringResource(Res.string.rotation_cannot_start, refusal), color = MaterialTheme.colorScheme.error) + } else { + Text(stringResource(Res.string.rotation_proposing_is_taking_part)) + } + } + }, + confirmButton = { + TextButton( + enabled = proposal.preview.refusal == null, + onClick = { chatRoomDetailViewModel.rotateShares(proposal) { failure -> notify(failure ?: rotationRequested) } } + ) { + Text(stringResource(Res.string.rotate_shares)) + } + }, + dismissButton = { + TextButton(onClick = { chatRoomDetailViewModel.dismissRotationProposal() }) { + Text(stringResource(Res.string.not_now)) + } + }, + ) + } + ScreenStateTransition(chatRoomDetailViewModel.chatRoomDetailUIState) { uiState -> when (val chatRoomDetailUIState = uiState) { is ChatRoomDetailUIState.Error -> { @@ -446,8 +499,8 @@ fun ChatRoomDetailScreen( if (shares.canRotate) { FilledTonalButton( onClick = { - chatRoomDetailViewModel.rotateShares(chatRoomDetailUIState.localChatRoom) { failure -> - notify(failure ?: rotationRequested) + chatRoomDetailViewModel.previewRotation(chatRoomDetailUIState.localChatRoom) { + notify(noKeyToRotate) } } ) { @@ -1329,4 +1382,15 @@ private fun ChatRoomMessagingScreenPreview() { ) } } -} \ No newline at end of file +} + +/** Who a rotation leaves out, a line per standing, nothing when it leaves out nobody. */ +@Composable +private fun RotationLeftOut(preview: RotationPreview) { + if (preview.untilBroughtBack.isNotEmpty()) { + Text(stringResource(Res.string.rotation_left_out_until_brought_back, preview.untilBroughtBack.joinToString(", "))) + } + if (preview.forGood.isNotEmpty()) { + Text(stringResource(Res.string.rotation_left_out_for_good, preview.forGood.joinToString(", "))) + } +} diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModel.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModel.kt index 3cb13a0e..c2d7f2b1 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModel.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModel.kt @@ -28,6 +28,7 @@ import mobi.giza.compose.ui.composable.navigation.routes.HomeRoute import mobi.giza.compose.ui.composable.navigation.routes.Route import mobi.giza.compose.ui.view.state.ChatRoomDetailUIState import mobi.giza.compose.ui.view.state.SharesSummary +import mobi.giza.compose.ui.view.state.RotationPreview class ChatRoomDetailViewModel( val chatRoomId: String, @@ -124,6 +125,8 @@ class ChatRoomDetailViewModel( val heldShare = chatRepository.holdsShare(localChatRoom, memberPublicKey) val failure = runCatching { chatRepository.removeMember(localChatRoom, memberPublicKey) } .exceptionOrNull() + // Who else the rotation would leave out, for the offer to name. + val preview = if (failure == null && heldShare) chatRepository.rotationPreview(localChatRoom, memberPublicKey) else null initiateChatRoomDetail() viewModelScope.launch(Dispatchers.Main) { onDone(failure?.message) @@ -131,7 +134,7 @@ class ChatRoomDetailViewModel( // can still help sign until the shares are rotated without them. // A second act, offered rather than folded in -- it needs every // remaining holder (docs/frost-share-refresh.md, Phase 9). - if (failure == null && heldShare) rotationOffer = RotationOffer(localChatRoom, memberPublicKey) + if (failure == null && heldShare) rotationOffer = RotationOffer(localChatRoom, memberPublicKey, preview) } } } @@ -149,8 +152,35 @@ class ChatRoomDetailViewModel( } } - fun rotateShares(localChatRoom: LocalChatRoom, onDone: (String?) -> Unit) = - sharesAction(onDone) { chatRepository.rotateShares(localChatRoom) } + /** A rotation this device could propose, shown before it is: pressing it is the proposer's agreement to take part. */ + data class RotationProposal(val localChatRoom: LocalChatRoom, val preview: RotationPreview) + + var rotationProposal: RotationProposal? by mutableStateOf(null) + private set + + /** + * Asks who a rotation would take part and leave out, and shows it. Nothing is + * sent until [rotateShares]. [onUnavailable] is told when there is nothing to + * show -- the room has no key this device can rotate. + */ + fun previewRotation(localChatRoom: LocalChatRoom, onUnavailable: () -> Unit) { + viewModelScope.launch(Dispatchers.IO) { + val preview = chatRepository.rotationPreview(localChatRoom) + viewModelScope.launch(Dispatchers.Main) { + if (preview == null) onUnavailable() else rotationProposal = RotationProposal(localChatRoom, preview) + } + } + } + + fun dismissRotationProposal() { + rotationProposal = null + } + + /** Proposes the rotation [rotationProposal] showed. */ + fun rotateShares(proposal: RotationProposal, onDone: (String?) -> Unit) { + rotationProposal = null + sharesAction(onDone) { chatRepository.rotateShares(proposal.localChatRoom) } + } fun approveShares(localChatRoom: LocalChatRoom, approval: SharesSummary.PendingApproval, onDone: (String?) -> Unit) = sharesAction(onDone) { chatRepository.approveShares(localChatRoom, approval) } @@ -161,8 +191,11 @@ class ChatRoomDetailViewModel( fun askToBeRepaired(localChatRoom: LocalChatRoom, onDone: (String?) -> Unit) = sharesAction(onDone) { chatRepository.askToBeRepaired(localChatRoom) } - /** A removed member who still holds a share of the room's key, for the offer to rotate without them. */ - data class RotationOffer(val localChatRoom: LocalChatRoom, val memberPublicKey: HexKey) + /** + * A removed member who still holds a share of the room's key, for the offer to + * rotate without them, with who else it would leave out. + */ + data class RotationOffer(val localChatRoom: LocalChatRoom, val memberPublicKey: HexKey, val preview: RotationPreview? = null) var rotationOffer: RotationOffer? by mutableStateOf(null) private set diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/state/RotationPreview.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/state/RotationPreview.kt new file mode 100644 index 00000000..0d7d997d --- /dev/null +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/state/RotationPreview.kt @@ -0,0 +1,17 @@ +package mobi.giza.compose.ui.view.state + +/** + * A share rotation as the proposer's dialog shows it before anything is sent + * (docs/rotate-shares-single-approval.md, Phase 4): who takes part and who is + * left out, by name. Pressing the dialog's button is the proposer's agreement + * to take part, so it shows everything that agreement covers. + */ +data class RotationPreview( + val takingPart: List, + /** Withheld as stale: left out until a repair brings them back. */ + val untilBroughtBack: List, + /** Withheld as retired: left out for good. */ + val forGood: List, + /** Why the rotation cannot be proposed, or null when it can. */ + val refusal: String?, +) diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ShareRefreshRitualJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ShareRefreshRitualJvmTest.kt index 1e45a63a..03f6d9d2 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ShareRefreshRitualJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ShareRefreshRitualJvmTest.kt @@ -266,12 +266,66 @@ class ShareRefreshRitualJvmTest { val refresh = propose(all[0]) pump(all) - all.forEach { device -> - assertEquals(listOf(ShareRefreshEvents.PROPOSAL).takeIf { device.index == 0 } ?: emptyList(), outbox(device).map { it.kind }, "device ${device.index}") + all.drop(1).forEach { device -> + assertEquals(emptyList(), outbox(device).map { it.kind }, "device ${device.index}") assertNotNull(ShareRefreshManager.pendingApproval(device.db, device.refresh(refresh.id)!!), "device ${device.index} must be asked") } - // The proposer is asked like anyone: proposing is not agreeing to withhold whoever the form defaulted to. - assertTrue(all[0].db.chatMessageDao().getChatMessagesByChatRoomId(group.roomId).any { it.chatMessage.messageType == ChatMessage.TYPE_SHARE_REFRESH_APPROVAL_NEEDED }) + // Proposing is the proposer's agreement, made from a dialog that named + // everyone left out: its device deals at once, and its owner is not asked. + val proposer = all[0] + assertEquals(listOf(ShareRefreshEvents.PROPOSAL, ShareRefreshEvents.ROUND_1), outbox(proposer).map { it.kind }) + assertNull(ShareRefreshManager.pendingApproval(proposer.db, proposer.refresh(refresh.id)!!)) + assertTrue(proposer.db.chatMessageDao().getChatMessagesByChatRoomId(group.roomId).none { it.chatMessage.messageType == ChatMessage.TYPE_SHARE_REFRESH_APPROVAL_NEEDED }) + } + + // ---- Phase 4 of rotate-shares-single-approval: what the proposer is shown ---------------------------------------- + + /** The dialog shows what the proposal will say: the same recipients, the same members left out, as the same standing. */ + @Test + fun `the preview is the proposal`() = runBlocking { + suspend fun agrees(by: Device, without: HexKey? = null) { + val preview = assertNotNull(ShareRefreshManager.preview(by.db, by.room, by.publicKey, without)) + assertNull(preview.refusal, preview.refusal) + val refresh = if (without == null) propose(by) else ShareRefreshManager.proposeWithout(by.db, by.room, by.publicKey, by.privateKey, without) + val roster = by.active().roster.associate { it.participantId to it.memberPublicKey } + assertEquals(preview.takingPart, refresh.recipientIdList().map { roster.getValue(it) }) + assertEquals(preview.withheld, refresh.withheldStandings().mapKeys { roster.getValue(it.key) }) + } + + // Nobody stale, nobody removed. + val fresh = devicesFor(Group(3, 2)) + agrees(fresh[0]) + + // One stale, withheld again without being asked for. + val (_, rotated) = rotatedWithout(2) + val stale = ShareRefreshManager.preview(rotated[0].db, rotated[0].room, rotated[0].publicKey)!! + assertEquals(mapOf(rotated[2].publicKey to ShareStanding.STALE), stale.withheld) + agrees(rotated[0]) + } + + @Test + fun `the preview of a removal names the removed for good and the stale until they are back`() = runBlocking { + val group = Group(5, 2) + val all = devicesFor(group) + val first = propose(all[0], mapOf(group.idOf[4] to ShareStanding.STALE)) + pump(all) + approveAll(all, first.id) + approveSigning(all) + + val preview = assertNotNull(ShareRefreshManager.preview(all[0].db, all[0].room, all[0].publicKey, without = all[3].publicKey)) + assertNull(preview.refusal, preview.refusal) + assertEquals(mapOf(all[3].publicKey to ShareStanding.RETIRED, all[4].publicKey to ShareStanding.STALE), preview.withheld) + assertEquals(setOf(all[0], all[1], all[2]).map { it.publicKey }.toSet(), preview.takingPart.toSet()) + } + + @Test + fun `the preview of a rotation that would be refused says why and sends nothing`() = runBlocking { + val group = Group(2, 2) + val (alice, bob) = devicesFor(group) + val preview = assertNotNull(ShareRefreshManager.preview(alice.db, alice.room, alice.publicKey, without = bob.publicKey)) + assertTrue(preview.refusal!!.contains("fewer than the 2 it takes to sign"), preview.refusal) + assertTrue(outbox(alice).isEmpty()) + assertNull(alice.db.shareRefreshDao().getSessionsForKey(ceremonyId).firstOrNull()) } // ---- the approvals, counted (docs/rotate-shares-single-approval.md) --------------------------------------------- @@ -299,7 +353,7 @@ class ShareRefreshRitualJvmTest { assertTrue(outbox(device).any { it.kind == ShareRefreshEvents.ROUND_2 }, "device ${device.index} did not confirm") val lines = device.db.chatMessageDao().getChatMessagesByChatRoomId(group.roomId) .filter { it.chatMessage.messageType == ChatMessage.TYPE_SHARE_REFRESH_APPROVAL_NEEDED } - assertEquals(1, lines.size, "device ${device.index} must be asked once") + assertEquals(if (device.index == 0) 0 else 1, lines.size, "device ${device.index} must be asked once, the proposer never") } } @@ -441,8 +495,8 @@ class ShareRefreshRitualJvmTest { /** * What a recipient is asked before the old shares are deleted: to take part, * and -- until Phase 5 -- to sign the new generation's key state. The - * proposer is asked only to take part; its device proposes the key state, and - * a proposer's signature is its proposal. + * proposer is asked nothing: proposing was taking part, its device proposes + * the key state, and a proposer's signature is its proposal. */ @Test fun `a recipient is asked to take part and to sign the new key state`() = runBlocking { @@ -453,7 +507,7 @@ class ShareRefreshRitualJvmTest { val asks = answerEverything(all, refresh.id) val rotation = listOf(Ask.Rotation(mobi.giza.compose.database.model.types.ShareRefreshApprovalStep.DEAL)) - assertEquals(rotation, asks[0], "the proposer") + assertEquals(emptyList(), asks[0], "the proposer, whose proposal was its answer") all.drop(1).forEach { device -> assertEquals(rotation + Ask.KeyState, asks[device.index], "device ${device.index}") } all.forEach { device -> assertEquals(1, device.db.groupKeyStateDao().getByChatRoomId(group.roomId)?.generation, "device ${device.index}")