diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 7cdce1a8..62f35455 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -379,6 +379,10 @@ The group has its key — finish setting it up The Lightning wallet's key ceremony needs you The Lightning wallet's key is ready + Open the shared key ceremony + Open the Lightning wallet's key ceremony + Open the proposal + You approved this Open Ask again @@ -571,7 +575,6 @@ Waiting for everyone's contributions Waiting for everyone to confirm The group has a key for a Lightning wallet. - Abandoned: %1$s Join the ceremony Send your contributions Confirm what you saw 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 944ddd70..49b23101 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 @@ -1215,7 +1215,7 @@ data class ChatMessage( * writes carries it. */ /** A member as a transcript line names them: their profile's name, or the start of their key. */ - private suspend fun memberName(database: BennuDatabase, publicKey: HexKey): String = + internal suspend fun memberName(database: BennuDatabase, publicKey: HexKey): String = database.profileDao().getProfileByPublicKey(publicKey)?.humanReadableNameOrPubkey() ?: publicKey.take(8) internal suspend fun applyInnerEvent( diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/IcebergRitualManager.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/IcebergRitualManager.kt index 10293faa..ad52d401 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/IcebergRitualManager.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/IcebergRitualManager.kt @@ -277,7 +277,10 @@ object IcebergRitualManager { } } GroupLightningEvents.CEREMONY_FAILURE -> { - fail(database, session, "Abandoned by ${sender.take(8)}: ${innerEvent.content}", sender) + // The sender's own account of why, which is a sentence from any build + // with [describe]. The line names the sender; the reason is not the + // place for a key prefix. + fail(database, session, innerEvent.content.ifBlank { "Something went wrong on their device." }, sender) return false } } @@ -337,9 +340,9 @@ object IcebergRitualManager { if (confirmations.size < params.n) return val byIndex = confirmations.associate { m -> indexOf(session, m.participantPublicKey)!! to CommonJson.decodeFromString(Round3.serializer(), m.payload) } val disagreeing = byIndex.filterValues { it.transcript != transcript.toHex() }.keys - check(disagreeing.isEmpty()) { "participants ${disagreeing.sorted()} saw a different set of commitments" } + if (disagreeing.isNotEmpty()) throw Disagreement(disagreeing.sorted()) val publicShares = (1..params.n).map { PublicShare(byIndex.getValue(it).publicShare) } - check(publicShares[me - 1] == myPublicShare) { "the public share published for this device is not the one its share gives" } + if (publicShares[me - 1] != myPublicShare) throw WrongPublicShare() val groupKey = when (val key = IcebergDkg.groupPublicKey(params, publicShares)) { is Either.Left -> throw key.value is Either.Right -> key.value @@ -363,8 +366,43 @@ object IcebergRitualManager { throw e } catch (e: Throwable) { logger.e("Iceberg ceremony ${session.id} failed", e) - fail(database, session, e.message ?: e::class.simpleName ?: "Unknown error") - broadcast(database, localChatRoom, session, GroupLightningEvents.CEREMONY_FAILURE, e.message ?: "Ceremony failed") + // Said, not thrown: the exception is in the log, and what the group is told + // -- here, and on every other device through the failure message -- is a + // sentence naming the member at fault where the fault names one. + val reason = describe(database, session, e) + fail(database, session, reason) + broadcast(database, localChatRoom, session, GroupLightningEvents.CEREMONY_FAILURE, reason) + } + } + + /** Some participants' transcript hashes are not this device's: they saw different commitments. */ + private class Disagreement(val participants: List) : Exception("participants $participants saw a different set of commitments") + + private class WrongPublicShare : Exception("the public share published for this device is not the one its share gives") + + /** + * Why a ceremony stopped on this device, as a sentence for the group: in its failure line's + * reason, on its screen, and in the failure message the other devices read. Shaped like + * ChillDKG's blame -- the member at fault is the actionable part -- but with their name rather + * than their index, since the index is an order of keys nobody sees. + */ + internal suspend fun describe(database: BennuDatabase, session: IcebergKeySession, failure: Throwable): String { + suspend fun named(index: Int?): String = index + ?.let { session.participants().getOrNull(it - 1) } + ?.let { "@" + ChatMessage.memberName(database, it) } + ?: "A member" + + return when (failure) { + is IcebergDkg.Fault.MissingCommitment -> "${named(failure.participant)} sent no commitment to their contributions." + is IcebergDkg.Fault.MissingReveal -> "${named(failure.participant)} sent this device nothing in the second round." + is IcebergDkg.Fault.BadReveal -> "${named(failure.participant)} sent a contribution that does not match what they committed to." + is IcebergDkg.Fault.MalformedMessage -> "${named(failure.participant)} sent a message this device could not read." + is IcebergDkg.Fault.InconsistentShares -> "The members' public shares do not make one key." + is IcebergDkg.Fault.RepairMismatch -> "A repaired share does not match the public share published for it." + is Disagreement -> failure.participants.map { named(it) }.joinToString(", ") + + " saw a different set of commitments from this device." + is WrongPublicShare -> "The public share published for this device is not the one its share gives." + else -> "Something went wrong on this device." } } diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/IcebergCeremonyScreen.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/IcebergCeremonyScreen.kt index 6d4c2974..a87bc6db 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/IcebergCeremonyScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/IcebergCeremonyScreen.kt @@ -1,5 +1,7 @@ package com.machankura.bennu.compose.ui.composable +import androidx.compose.material.icons.filled.ErrorOutline +import androidx.compose.material3.CardDefaults import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row @@ -33,7 +35,8 @@ import androidx.compose.ui.text.style.TextOverflow import androidx.lifecycle.viewmodel.compose.viewModel import bennu.composeapp.generated.resources.Res import bennu.composeapp.generated.resources.a_lightning_wallet_needs_four_signers -import bennu.composeapp.generated.resources.abandoned_s +import bennu.composeapp.generated.resources.the_ceremony_was_abandoned +import bennu.composeapp.generated.resources.nothing_was_created_and_no_key_exists_it_is import bennu.composeapp.generated.resources.confirm_what_you_saw import bennu.composeapp.generated.resources.confirming_publishes_your_public_share import bennu.composeapp.generated.resources.couldn_t_approve_this_step @@ -197,7 +200,7 @@ private fun Ceremony(modifier: Modifier, state: IcebergCeremonyUIState.Loaded, i IcebergRitualStage.COLLECTING_REVEALS -> stringResource(Res.string.waiting_for_contributions) IcebergRitualStage.COLLECTING_CONFIRMATIONS -> stringResource(Res.string.waiting_for_everyone_to_confirm) IcebergRitualStage.COMPLETE -> stringResource(Res.string.the_group_has_a_lightning_wallet_key) - IcebergRitualStage.FAILED -> stringResource(Res.string.abandoned_s, session.failureReason.orEmpty()) + IcebergRitualStage.FAILED -> stringResource(Res.string.the_ceremony_was_abandoned) }, style = MaterialTheme.typography.titleMedium, ) @@ -207,6 +210,31 @@ private fun Ceremony(modifier: Modifier, state: IcebergCeremonyUIState.Loaded, i color = MaterialTheme.colorScheme.onSurfaceVariant, ) } + // As the shared key's ceremony says it: why, in the sentence the device that + // stopped it wrote, and that nothing was made. + if (session.stage == IcebergRitualStage.FAILED) { + item { + Card( + modifier = Modifier.fillMaxWidth(), + colors = CardDefaults.cardColors( + containerColor = MaterialTheme.colorScheme.errorContainer, + contentColor = MaterialTheme.colorScheme.onErrorContainer, + ), + ) { + Row( + modifier = Modifier.fillMaxWidth().padding(MaterialTheme.spacing.containerPadding), + horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap), + verticalAlignment = Alignment.CenterVertically, + ) { + Icon(Icons.Default.ErrorOutline, contentDescription = Decorative) + Column(verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap)) { + session.failureReason?.let { Text(it, style = MaterialTheme.typography.bodyMedium) } + Text(stringResource(Res.string.nothing_was_created_and_no_key_exists_it_is), style = MaterialTheme.typography.labelMedium) + } + } + } + } + } state.pending?.let { step -> item { Card(modifier = Modifier.fillMaxWidth()) { diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/widgets/chat/ChatTranscript.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/widgets/chat/ChatTranscript.kt index 1a3674c8..bd6160f8 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/widgets/chat/ChatTranscript.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/widgets/chat/ChatTranscript.kt @@ -35,6 +35,10 @@ import bennu.composeapp.generated.resources.join_the_ceremony import bennu.composeapp.generated.resources.send_your_contributions import bennu.composeapp.generated.resources.confirm_what_you_saw import bennu.composeapp.generated.resources.open_a_lightning_wallet +import bennu.composeapp.generated.resources.open_the_shared_key_ceremony +import bennu.composeapp.generated.resources.open_the_lightning_wallet_s_key_ceremony +import bennu.composeapp.generated.resources.open_the_proposal +import bennu.composeapp.generated.resources.you_approved_this import androidx.compose.material.icons.filled.AccessTime import androidx.compose.material.icons.filled.Block import androidx.compose.material.icons.filled.Check @@ -296,7 +300,8 @@ fun ChatTranscript( // subgroup's got buried under another. onClick = localChatMessage.chatMessage.dkgSessionId?.let { dkgSessionId -> { onOpenSharedKey(dkgSessionId) } - } + }, + opens = stringResource(Res.string.open_the_shared_key_ceremony), ) return@items } @@ -308,7 +313,8 @@ fun ChatTranscript( localChatMessage = localChatMessage, isAnswered = localChatMessage.chatMessage.id in answeredRequests, isSettled = localChatMessage.chatMessage.id in settledRequests, - onClick = { onOpenIcebergCeremony(localChatMessage.chatMessage.dkgSessionId) } + onClick = { onOpenIcebergCeremony(localChatMessage.chatMessage.dkgSessionId) }, + opens = stringResource(Res.string.open_the_lightning_wallet_s_key_ceremony), ) return@items } @@ -338,7 +344,8 @@ fun ChatTranscript( localChatMessage = localChatMessage, isAnswered = true, isSettled = true, - onClick = {} + onClick = null, + opens = null, ) return@items } @@ -356,7 +363,8 @@ fun ChatTranscript( localChatMessage = localChatMessage, isAnswered = true, isSettled = true, - onClick = {} + onClick = null, + opens = null, ) return@items } @@ -377,7 +385,8 @@ fun ChatTranscript( localChatMessage = localChatMessage, isAnswered = true, isSettled = true, - onClick = {} + onClick = null, + opens = null, ) return@items } @@ -392,7 +401,8 @@ fun ChatTranscript( localChatMessage = localChatMessage, isAnswered = true, isSettled = true, - onClick = {} + onClick = null, + opens = null, ) return@items } @@ -411,7 +421,8 @@ fun ChatTranscript( onOpenSigning( localChatMessage.chatMessage.frostSigningSessionId ) - } + }, + opens = stringResource(Res.string.open_the_proposal), ) return@items } @@ -842,8 +853,14 @@ private fun RitualNotice( localChatMessage: LocalChatMessage, isAnswered: Boolean, isSettled: Boolean, - /** Null for a line that names no ceremony, which then opens nothing. */ + /** Null for a line that leads nowhere, or names no ceremony, which then opens nothing. */ onClick: (() -> Unit)?, + /** + * What a tap opens, as the chevron's description; null draws no chevron. Each family + * opens its own screen, and one description for all of them told a screen reader + * every signing line opened the shared key ceremony. + */ + opens: String?, ) { val chatMessage = localChatMessage.chatMessage @@ -915,11 +932,10 @@ private fun RitualNotice( // So is a settled one -- declined, abandoned, or signed by a quorum that did // not need this member. Nothing was answered there, so it gets no tick, but // offering to review it would be offering a decision that has already gone by. - val isRequest = ( - chatMessage.messageType in ChatMessage.DKG_REQUEST_TYPES || - chatMessage.messageType in ChatMessage.ICEBERG_REQUEST_TYPES || - chatMessage.messageType in ChatMessage.FROST_REQUEST_TYPES - ) && !isAnswered && !isSettled + val asks = chatMessage.messageType in ChatMessage.DKG_REQUEST_TYPES || + chatMessage.messageType in ChatMessage.ICEBERG_REQUEST_TYPES || + chatMessage.messageType in ChatMessage.FROST_REQUEST_TYPES + val isRequest = asks && !isAnswered && !isSettled val tint = when { chatMessage.messageType == ChatMessage.TYPE_DKG_FAILED || @@ -996,18 +1012,23 @@ private fun RitualNotice( style = MaterialTheme.typography.labelLarge, color = MaterialTheme.colorScheme.primary ) - } else if (isAnswered) { + } else if (asks && isAnswered) { + // Only a request can have been approved. The lines that report rather + // than ask are passed as answered to keep them quiet, and a tick on + // "the group made a subgroup" would claim the reader approved it. Icon( imageVector = Icons.Default.Check, - contentDescription = "You approved this", + contentDescription = stringResource(Res.string.you_approved_this), tint = MaterialTheme.colorScheme.primary ) } - Icon( - imageVector = Icons.Default.ChevronRight, - contentDescription = "Open the shared key ceremony", - tint = tint - ) + if (opens != null && onClick != null) { + Icon( + imageVector = Icons.Default.ChevronRight, + contentDescription = opens, + tint = tint + ) + } } } diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/IcebergCeremonyJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/IcebergCeremonyJvmTest.kt index 6a78d199..42f7d30f 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/IcebergCeremonyJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/IcebergCeremonyJvmTest.kt @@ -293,4 +293,54 @@ class IcebergCeremonyJvmTest { assertTrue(refused.isFailure) assertTrue(refused.exceptionOrNull()!!.message!!.contains("onchain wallet")) } + + /** Opening a ceremony is agreeing to be in it, so its opener is never asked to join it. */ + @Test + fun `the member who opened a ceremony is not asked to join it`() = runBlocking { + val all = group() + val proposed = IcebergRitualManager.proposeCeremony(all[0].db, all[0].room, all[0].publicKey, all[0].privateKey, 2, chain) + pump(all, GroupLightningEvents.CEREMONY) + + fun joinRequests(d: Device) = runBlocking { + d.db.chatMessageDao().getChatMessagesByChatRoomId(roomId).map { it.chatMessage } + .filter { it.messageType == ChatMessage.TYPE_ICEBERG_APPROVAL_NEEDED_JOIN && it.dkgSessionId == proposed.id } + } + assertTrue(joinRequests(all[0]).isEmpty(), "the opener has nothing to approve") + all.drop(1).forEach { assertEquals(1, joinRequests(it).size, "device ${it.index} is asked once") } + } + + /** + * A fault is said as a sentence naming the member at fault -- not their index in an order of + * keys nobody sees, and not an exception's message -- and that sentence is what the other + * devices are sent and keep. docs/group-lightning-transcript.md, Phase 3. + */ + @Test + fun `an abandoned ceremony says why in a sentence, naming the member`() = runBlocking { + val all = group() + val proposed = IcebergRitualManager.proposeCeremony(all[0].db, all[0].room, all[0].publicKey, all[0].privateKey, 2, chain) + pump(all, GroupLightningEvents.CEREMONY) + val session = all[1].ceremony(proposed.id)!! + val accused = session.participants()[1] + val name = ChatMessage.memberName(all[1].db, accused) + + val reason = IcebergRitualManager.describe(all[1].db, session, IcebergDkg.Fault.BadReveal(2)) + assertEquals("@$name sent a contribution that does not match what they committed to.", reason) + assertEquals("Something went wrong on this device.", IcebergRitualManager.describe(all[1].db, session, IllegalStateException("index 7 out of bounds"))) + + // Device 1 stops the ceremony with that reason; device 2 hears it. + val tags = arrayOf(arrayOf("iceberg_session", proposed.id)) + val createdAt = 1_700_000_000L + val failure = Event( + EventHasher.hashId(pubKey = all[1].publicKey, createdAt = createdAt, kind = GroupLightningEvents.CEREMONY_FAILURE, tags = tags, content = reason), + all[1].publicKey, createdAt, GroupLightningEvents.CEREMONY_FAILURE, tags, reason, "", + ) + IcebergRitualManager.processCeremonyPayload(all[2].db, all[2].room, failure, all[2].publicKey, all[2].privateKey) + + val failed = all[2].ceremony(proposed.id)!! + assertEquals(IcebergRitualStage.FAILED, failed.stage) + assertEquals(reason, failed.failureReason, "the sender's sentence, with no key prefix in front of it") + val line = all[2].db.chatMessageDao().getChatMessagesByChatRoomId(roomId).map { it.chatMessage } + .single { it.messageType == ChatMessage.TYPE_ICEBERG_FAILED } + assertEquals(all[1].publicKey, line.senderPublicKey, "the line names who stopped it") + } }