From b58a757a4457403eec324b19286e8f6204f2aca0 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Sat, 24 Oct 2026 07:44:52 +0200 Subject: [PATCH] feat(subgroup): the picker chooses the parent's own guardian delay, and the join gate says it (Phase 5) Under the guarded row, SelectSubgroupAdminsScreen offers the delay for this group's own leaf: presets for an hour, two (the default), a day, a week, a month, six months and the longest, and a custom field in blocks whose supporting text says what it works out to, or why it cannot be used. The view model keeps what was typed rather than clamping it, refusal() refuses a delay outside 6..65,535 for a guarded subgroup, a retry restores it, and confirm passes it to proposeRitual. The guarded row and the join gate stop quoting 12. Lands with Phase 6, which puts the chosen delay into the certificate. Co-Authored-By: Claude Opus 5.5 --- .../composeResources/values/strings.xml | 17 ++- .../ui/composable/DkgJoinApprovalScreen.kt | 7 +- .../composable/SelectSubgroupAdminsScreen.kt | 114 +++++++++++++++++- .../model/SelectSubgroupAdminsViewModel.kt | 71 ++++++++++- .../SelectSubgroupAdminsViewModelJvmTest.kt | 72 +++++++++++ 5 files changed, 272 insertions(+), 9 deletions(-) diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 4fd4e130..05ed982a 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -554,12 +554,12 @@ The subgroup's wallet Guarded wallet Unguarded wallet - This group can spend any coin in it once it has %1$d confirmations. - This group and the %1$d above it can spend any coin in it once it has %2$d confirmations. + This group can spend any coin in it once it has waited as long as the delay below. + This group, after the delay below, and the %1$d above it, after the delays they chose, can spend any coin in it. Only the subgroup can spend from its wallet. Groups above it can't reach it or anything beneath it. This group is guarded, so its subgroups are too. This group's own guardian keys can't be read, so it can't guard a subgroup. - The subgroup's wallet will be guarded: this group, and every group above it, can spend any coin in it once it has %1$d confirmations. + The subgroup's wallet will be guarded: this group can spend any coin in it once it has waited %1$s, and each group above it after the delay it chose. The subgroup's wallet will not be guarded: only the subgroup can spend from it. The group above this one decided this group's wallet is guarded. Each group above it can spend any coin in it that has waited as long as its own delay, without this group agreeing: The group above this one decided this group's wallet is not guarded. Only this group can spend from it. @@ -567,6 +567,17 @@ %1$s, %2$s %1$s: after %2$s + How long before this group can spend a coin + 1 hour + 2 hours + 1 day + 1 week + 1 month + 6 months + Longest + Custom + Delay in blocks + A coin the subgroup moves starts waiting again, so a long delay lets it keep its coins out of reach by moving them. A group above this one This group can spend a coin here after %1$s Guarded wallet, after %1$s diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/DkgJoinApprovalScreen.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/DkgJoinApprovalScreen.kt index 3d599c6b..7fb0325e 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/DkgJoinApprovalScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/DkgJoinApprovalScreen.kt @@ -1,5 +1,6 @@ package com.machankura.bennu.compose.ui.composable +import com.machankura.bennu.compose.ui.composable.widgets.delayText import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy import com.machankura.bennu.compose.managers.GuardianDerivation import com.machankura.bennu.compose.database.model.subgroupWalletPolicy @@ -73,9 +74,13 @@ fun DkgJoinApprovalScreen( session.subgroupWalletPolicy?.let { policy -> Text( text = when (policy) { + // The parent's own leaf's delay, as the coordinator chose it -- + // or the default an older build's ceremony is certified at. + // An inherited ancestor's is not here: a member deciding + // whether to join cannot check it. docs/guardian-delay.md. SubgroupWalletPolicy.GUARDED -> stringResource( Res.string.the_subgroup_s_wallet_will_be_guarded, - GuardianDerivation.DEFAULT_DELAY.toInt() + delayText(session.guardianDelay ?: GuardianDerivation.DEFAULT_DELAY) ) SubgroupWalletPolicy.UNGUARDED -> stringResource(Res.string.the_subgroup_s_wallet_will_not_be_guarded) }, diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/SelectSubgroupAdminsScreen.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/SelectSubgroupAdminsScreen.kt index 19edd6b4..b0173426 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/SelectSubgroupAdminsScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/SelectSubgroupAdminsScreen.kt @@ -1,9 +1,25 @@ package com.machankura.bennu.compose.ui.composable +import bennu.composeapp.generated.resources.a_coin_the_subgroup_moves_starts_waiting_again +import bennu.composeapp.generated.resources.delay_in_blocks +import bennu.composeapp.generated.resources.delay_custom +import bennu.composeapp.generated.resources.delay_longest +import bennu.composeapp.generated.resources.delay_six_months +import bennu.composeapp.generated.resources.delay_one_month +import bennu.composeapp.generated.resources.delay_one_week +import bennu.composeapp.generated.resources.delay_one_day +import bennu.composeapp.generated.resources.delay_two_hours +import bennu.composeapp.generated.resources.delay_one_hour +import bennu.composeapp.generated.resources.how_long_before_this_group_can_spend_a_coin +import com.machankura.bennu.compose.ui.composable.widgets.delayText +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.material3.FilterChip +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.foundation.layout.FlowRow +import androidx.compose.foundation.layout.ExperimentalLayoutApi import com.machankura.bennu.compose.ui.view.state.onlyGuarded import com.machankura.bennu.compose.ui.view.state.canGuard import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy -import com.machankura.bennu.compose.managers.GuardianDerivation import bennu.composeapp.generated.resources.this_group_s_guardian_keys_can_t_be_read import bennu.composeapp.generated.resources.this_group_is_guarded_so_its_subgroups_are_too import bennu.composeapp.generated.resources.only_the_subgroup_can_spend_from_its_wallet @@ -303,6 +319,22 @@ fun SelectSubgroupAdminsScreen( onPolicyChange = viewModel::setWalletPolicy ) } + + // The parent's own leaf's delay, and only for a guarded + // subgroup: docs/guardian-delay.md. + if (viewModel.walletPolicy.value == SubgroupWalletPolicy.GUARDED && state.canGuard) { + item { + GuardianDelayChooser( + input = viewModel.guardianDelayInput.value, + delay = viewModel.guardianDelay(), + isCustom = viewModel.isCustomDelay.value, + problem = viewModel.guardianDelayProblem(), + onPreset = viewModel::setGuardianDelay, + onCustom = viewModel::useCustomDelay, + onInput = viewModel::setGuardianDelayInput, + ) + } + } } } } @@ -392,7 +424,6 @@ private fun WalletPolicyChooser( style = MaterialTheme.typography.titleSmall ) - val delay = GuardianDerivation.DEFAULT_DELAY.toInt() WalletPolicyRow( title = stringResource(Res.string.guarded_wallet), // Names the reach the parent is actually granting: itself, and each @@ -400,9 +431,9 @@ private fun WalletPolicyChooser( description = if (!canGuard) { stringResource(Res.string.this_group_s_guardian_keys_can_t_be_read) } else if (parentGuardianCount == 0) { - stringResource(Res.string.this_group_can_spend_its_coins_once, delay) + stringResource(Res.string.this_group_can_spend_its_coins_once) } else { - stringResource(Res.string.this_group_and_the_d_above_it_can_spend, parentGuardianCount, delay) + stringResource(Res.string.this_group_and_the_d_above_it_can_spend, parentGuardianCount) }, isSelected = policy == SubgroupWalletPolicy.GUARDED, isEnabled = canGuard, @@ -422,6 +453,81 @@ private fun WalletPolicyChooser( } } +/** + * How long a coin in the guarded subgroup's wallet waits before this group can + * take it -- the parent's own leaf, `docs/guardian-delay.md`. Presets for what + * people mean, and a field for the exact number of blocks, which says what it + * works out to while it is typed. + */ +@OptIn(ExperimentalLayoutApi::class) +@Composable +private fun GuardianDelayChooser( + input: String, + delay: Long?, + isCustom: Boolean, + problem: String?, + onPreset: (Long) -> Unit, + onCustom: () -> Unit, + onInput: (String) -> Unit, +) { + Column( + modifier = Modifier.fillMaxWidth(), + verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap) + ) { + Text( + text = stringResource(Res.string.how_long_before_this_group_can_spend_a_coin), + style = MaterialTheme.typography.titleSmall + ) + FlowRow( + horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap), + verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap) + ) { + SelectSubgroupAdminsViewModel.DELAY_PRESETS.forEach { preset -> + FilterChip( + selected = !isCustom && delay == preset, + onClick = { onPreset(preset) }, + label = { Text(presetLabel(preset)) } + ) + } + FilterChip( + selected = isCustom, + onClick = onCustom, + label = { Text(stringResource(Res.string.delay_custom)) } + ) + } + if (isCustom) { + OutlinedTextField( + value = input, + onValueChange = onInput, + modifier = Modifier.fillMaxWidth(), + singleLine = true, + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), + label = { Text(stringResource(Res.string.delay_in_blocks)) }, + isError = problem != null, + supportingText = { Text(problem ?: delay?.let { delayText(it) }.orEmpty()) } + ) + } else if (delay != null && problem == null) { + Text(text = delayText(delay), style = MaterialTheme.typography.bodyMedium) + } + Text( + text = stringResource(Res.string.a_coin_the_subgroup_moves_starts_waiting_again), + style = MaterialTheme.typography.bodySmall + ) + } +} + +/** What a preset chip calls its delay: the time it was chosen to be, not its estimate. */ +@Composable +private fun presetLabel(blocks: Long): String = when (blocks) { + 6L -> stringResource(Res.string.delay_one_hour) + 12L -> stringResource(Res.string.delay_two_hours) + 144L -> stringResource(Res.string.delay_one_day) + 1_008L -> stringResource(Res.string.delay_one_week) + 4_320L -> stringResource(Res.string.delay_one_month) + 26_280L -> stringResource(Res.string.delay_six_months) + else -> stringResource(Res.string.delay_longest) +} + @Composable private fun WalletPolicyRow( title: String, diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/SelectSubgroupAdminsViewModel.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/SelectSubgroupAdminsViewModel.kt index 8d4afb36..8beb54ae 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/SelectSubgroupAdminsViewModel.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/SelectSubgroupAdminsViewModel.kt @@ -1,5 +1,7 @@ package com.machankura.bennu.compose.ui.view.model +import com.machankura.bennu.compose.text.groupedBlocks +import com.machankura.bennu.compose.managers.GuardianDerivation import com.machankura.bennu.compose.ui.view.state.canGuard import com.machankura.bennu.compose.ui.view.state.onlyGuarded import com.machankura.bennu.compose.database.model.subgroupWalletPolicy @@ -97,6 +99,20 @@ class SelectSubgroupAdminsViewModel( */ val walletPolicy: MutableState = mutableStateOf(SubgroupWalletPolicy.GUARDED) + /** + * How long a coin in a guarded subgroup's wallet waits before this group can + * take it, in blocks, as typed -- `docs/guardian-delay.md`. The parent's own + * leaf only: each ancestor above keeps the delay it chose. + * + * Held as the text rather than a number, and never clamped, so the field can + * say *at most 65,535 blocks* about the number somebody actually entered. + * [guardianDelay] reads it. + */ + val guardianDelayInput: MutableState = mutableStateOf(GuardianDerivation.DEFAULT_DELAY.toString()) + + /** Whether the delay is being typed rather than picked from a preset. */ + val isCustomDelay: MutableState = mutableStateOf(false) + /** Whether [retrying] has been read into the form, which happens once. */ private var prefilled = false @@ -171,6 +187,10 @@ class SelectSubgroupAdminsViewModel( // failed ceremony can predate the parent's own certificate -- is pulled // back the way the threshold is pulled into its range. failed.subgroupWalletPolicy?.let(::setWalletPolicy) + failed.guardianDelay?.let { delay -> + guardianDelayInput.value = delay.toString() + isCustomDelay.value = delay !in DELAY_PRESETS + } } /** @@ -236,6 +256,41 @@ class SelectSubgroupAdminsViewModel( walletPolicy.value = value } + /** Picks a preset delay. */ + fun setGuardianDelay(blocks: Long) { + if (isActionPending.value) return + + isCustomDelay.value = false + guardianDelayInput.value = blocks.toString() + } + + /** Switches to typing a delay, starting from the one already chosen. */ + fun useCustomDelay() { + if (isActionPending.value) return + + isCustomDelay.value = true + } + + /** What was typed as the delay, kept as typed. */ + fun setGuardianDelayInput(value: String) { + if (isActionPending.value) return + + guardianDelayInput.value = value.filter { it in '0'..'9' }.take(MAX_DELAY_DIGITS) + } + + /** The delay typed or picked, or null while the field holds no number. */ + fun guardianDelay(): Long? = guardianDelayInput.value.toLongOrNull() + + /** Why the delay cannot be used, or null when it can. */ + fun guardianDelayProblem(): String? { + val delay = guardianDelay() ?: return "Give the delay in blocks." + return when { + delay < GuardianDerivation.MIN_DELAY -> "A delay is at least ${GuardianDerivation.MIN_DELAY} blocks." + delay > GuardianDerivation.MAX_DELAY -> "A delay is at most ${GuardianDerivation.MAX_DELAY.groupedBlocks()} blocks." + else -> null + } + } + /** The subgroup's admins: everyone ticked, plus the coordinator. */ fun adminCount(): Int = selectedPublicKeys.size + 1 @@ -269,6 +324,7 @@ class SelectSubgroupAdminsViewModel( if (walletPolicy.value == SubgroupWalletPolicy.GUARDED && !loaded.canGuard) { return "This group's own guardian keys can't be read, so it can't guard a subgroup." } + if (walletPolicy.value == SubgroupWalletPolicy.GUARDED) guardianDelayProblem()?.let { return it } if (loaded.isResolvingKeyPackages) return "Checking who can be added…" @@ -347,7 +403,10 @@ class SelectSubgroupAdminsViewModel( // Carried on the proposal so every admin is told at the join gate, // and on the session so whoever proposes the certificate proposes // it under the same one. - walletPolicy = walletPolicy.value + walletPolicy = walletPolicy.value, + // The parent's own leaf, and only for a guarded subgroup. + // docs/guardian-delay.md. + guardianDelay = guardianDelay().takeIf { walletPolicy.value == SubgroupWalletPolicy.GUARDED } ) isActionPending.value = false @@ -379,6 +438,16 @@ class SelectSubgroupAdminsViewModel( } companion object { + /** + * The delays the picker offers, in blocks -- `docs/guardian-delay.md`, D4: + * an hour, two (the default), a day, a week, a month, six months, and the + * longest a leaf can carry. + */ + val DELAY_PRESETS: List = listOf(6L, 12L, 144L, 1_008L, 4_320L, 26_280L, GuardianDerivation.MAX_DELAY) + + /** Enough digits for anything past the ceiling, so the field can say so. */ + private const val MAX_DELAY_DIGITS = 7 + private const val TAG = "SelectSubgroupAdminsViewModel" fun factory( diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/view/model/SelectSubgroupAdminsViewModelJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/view/model/SelectSubgroupAdminsViewModelJvmTest.kt index 33c75396..4f2bb945 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/view/model/SelectSubgroupAdminsViewModelJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/view/model/SelectSubgroupAdminsViewModelJvmTest.kt @@ -1,5 +1,7 @@ package com.machankura.bennu.compose.ui.view.model +import kotlin.test.assertTrue +import kotlin.test.assertFalse import kotlin.test.assertNull import com.machankura.bennu.compose.managers.GuardianDerivation import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading @@ -81,6 +83,7 @@ class SelectSubgroupAdminsViewModelJvmTest { private fun failed( parentChatRoomId: String? = this.parentChatRoomId, walletPolicy: SubgroupWalletPolicy? = null, + guardianDelay: Long? = null, ) = DkgSession( id = failedId, chatRoomId = this.parentChatRoomId, @@ -96,6 +99,7 @@ class SelectSubgroupAdminsViewModelJvmTest { round2AuxRandom = "bb".repeat(32), parentChatRoomId = parentChatRoomId, walletPolicy = walletPolicy?.tagValue, + guardianDelay = guardianDelay, ) private fun chatRepository( @@ -270,4 +274,72 @@ class SelectSubgroupAdminsViewModelJvmTest { "refused here, not after a ceremony", ) } + + // ---- the guardian delay: docs/guardian-delay.md, Phase 5 --------------- + + private suspend fun loadedWithName(session: DkgSession? = null) = viewModel(session).apply { + initiate() + awaitLoaded() + setName("Tapscript") + } + + @Test + fun `a guarded subgroup's delay starts at the default`(): Unit = runBlocking { + val viewModel = loadedWithName() + + assertEquals(12L, viewModel.guardianDelay()) + assertFalse(viewModel.isCustomDelay.value) + assertNull(viewModel.guardianDelayProblem()) + } + + @Test + fun `a preset and a typed delay are what refusal judges`(): Unit = runBlocking { + val viewModel = loadedWithName() + + viewModel.setGuardianDelay(4_320) + assertEquals(4_320L, viewModel.guardianDelay()) + assertNull(viewModel.refusal()?.takeIf { "delay" in it }) + + viewModel.useCustomDelay() + viewModel.setGuardianDelayInput("2,016") + assertEquals(2_016L, viewModel.guardianDelay(), "grouping is dropped as it is typed") + assertTrue(viewModel.isCustomDelay.value) + } + + @Test + fun `a delay below six or above 65535 is refused with its reason`(): Unit = runBlocking { + val viewModel = loadedWithName() + viewModel.useCustomDelay() + + viewModel.setGuardianDelayInput("5") + assertEquals("A delay is at least 6 blocks.", viewModel.refusal()) + + viewModel.setGuardianDelayInput("65536") + assertEquals(65_536L, viewModel.guardianDelay(), "kept as typed, so the field can say what is wrong with it") + assertEquals("A delay is at most 65,535 blocks.", viewModel.refusal()) + + viewModel.setGuardianDelayInput("") + assertEquals("Give the delay in blocks.", viewModel.refusal()) + } + + @Test + fun `an unguarded subgroup is not held to a delay`(): Unit = runBlocking { + val viewModel = loadedWithName() + viewModel.useCustomDelay() + viewModel.setGuardianDelayInput("5") + viewModel.setWalletPolicy(SubgroupWalletPolicy.UNGUARDED) + + assertNull(viewModel.refusal()?.takeIf { "delay" in it }) + } + + @Test + fun `a retry starts from the failed ceremony's delay`(): Unit = runBlocking { + val preset = loadedWithName(failed(walletPolicy = SubgroupWalletPolicy.GUARDED, guardianDelay = 1_008)) + assertEquals(1_008L, preset.guardianDelay()) + assertFalse(preset.isCustomDelay.value, "a preset comes back as the preset") + + val typed = loadedWithName(failed(walletPolicy = SubgroupWalletPolicy.GUARDED, guardianDelay = 2_016)) + assertEquals(2_016L, typed.guardianDelay()) + assertTrue(typed.isCustomDelay.value, "anything else comes back in the field") + } }