From fbfe9075f2ed55295b2272ab791398c9350fb8e0 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Sat, 24 Oct 2026 07:42:15 +0200 Subject: [PATCH] feat(wallet): every screen says each guardian's own delay (Phase 7) Before a parent can choose a delay, the screens stop saying "12": - the child's wallet screen, opened or not, lists each guardian nearest first with its own delay -- "#Council: after 144 blocks, about 1 day" -- named by the room this device holds for it; - an ancestor's Guarded wallets list says, per wallet, the delay that wallet gives this group, and GuardedWalletRow.withinReach counts coins past that delay rather than the default (GuardedWallet.delay); - the send screen tells a guardian that only coins past its leaf's lock can be spent; - the subgroup rows and a subgroup's parent row say "Guarded wallet, after ", off the certificate. The strings that quoted one number are reworded without it. Co-Authored-By: Claude Opus 5.5 --- .../composeResources/values/strings.xml | 13 ++- .../bennu/compose/managers/GuardianManager.kt | 8 ++ .../bennu/compose/managers/SubgroupManager.kt | 7 ++ .../ui/composable/ChatRoomDetailScreen.kt | 18 +++-- .../compose/ui/composable/GroupSendScreen.kt | 7 ++ .../ui/composable/GroupWalletScreen.kt | 79 ++++++++++++++----- .../ui/view/model/ChatRoomDetailViewModel.kt | 1 + .../ui/view/model/GroupSendViewModel.kt | 11 +++ .../ui/view/model/GroupWalletViewModel.kt | 15 +++- .../ui/view/state/ChatRoomDetailUIState.kt | 5 ++ .../ui/view/state/GroupWalletUIState.kt | 10 ++- .../managers/GuardianSigningJvmTest.kt | 22 ++++++ .../view/model/GroupWalletViewModelJvmTest.kt | 10 +++ 13 files changed, 171 insertions(+), 35 deletions(-) diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index cf0d7e0a..4fd4e130 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -465,20 +465,20 @@ Opening a wallet asks the group to agree that it keeps coins here. Any quorum of members can then spend them together, and nobody can spend them alone. Each member's share of the group's key is stored unencrypted on their device. Anyone with an unlocked device, or a copy of its data, holds that member's share. Couldn't ask the group to open a wallet. - The groups above this one have agreed to guard its wallet. In a guarded wallet each of them can spend any coin once it has %1$d confirmations, without this group agreeing. + The groups above this one have agreed to guard its wallet. In a guarded wallet each of them can spend any coin that has waited as long as its own delay, without this group agreeing: Open a guarded wallet Open without guardians Guarded wallets Spend Spend from %1$s's guarded wallet - Wallets of groups below this one. This group can spend a coin in them once it has %1$d confirmations. + Wallets of groups below this one. This group can spend a coin in each once the coin has waited as long as the delay that wallet gives this group. %1$s sats within this group's reach Tell the groups above Not yet shared with the groups above that you're in Share Shared with the groups above Couldn't share the wallet - Guarded: the groups above this one can spend any coin here once it has %1$d confirmations. + Guarded: the groups above this one can spend any coin here that has waited as long as their own delay: Balance %1$s sats %1$s sats on the way @@ -561,11 +561,16 @@ 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 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 once it has %1$d confirmations, without this group agreeing. + 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. %1$s, %2$s + %1$s: after %2$s + A group above this one + This group can spend a coin here after %1$s + Guarded wallet, after %1$s + Only coins that have waited %1$s can be spent from this guarded wallet. %1$s block %1$s blocks diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GuardianManager.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GuardianManager.kt index d1009ca9..6c59c6ad 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GuardianManager.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GuardianManager.kt @@ -67,6 +67,14 @@ object GuardianManager { /** Where the ancestor sits among the guardians: whose leaf, in every address, is its. */ val position: Int get() = account.guardians.indexOfFirst { it.chatRoomId.equals(ancestorChatRoomId, ignoreCase = true) } + /** + * How long a coin here waits before this ancestor can take it: the delay + * on its own leaf, which the group that granted it chose and which two of + * an ancestor's descendants can have set differently. + * `docs/guardian-delay.md`. + */ + val delay: Long get() = account.guardians[position].delay + /** The ancestor as the spender of a spend from this wallet, signing as [thresholdPublicKey] -- null to only build one. */ fun spender(thresholdPublicKey: HexKey? = null): GroupSpend.Spender.Guardian = GroupSpend.Spender.Guardian(position = position, lineage = lineage, thresholdPublicKey = thresholdPublicKey, name = name) diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/SubgroupManager.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/SubgroupManager.kt index 0683905c..f1158ce5 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/SubgroupManager.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/SubgroupManager.kt @@ -555,6 +555,13 @@ object SubgroupManager { val walletPolicy: WalletPolicyReading? get() = SubgroupBirthCertificateEvent.parseWalletPolicy(certificate.tags) + /** + * The delay the parent gave its own leaf in the subgroup's wallet, or + * null for a certificate that grants no keys -- `docs/guardian-delay.md`. + */ + val guardianDelay: Long? + get() = SubgroupBirthCertificateEvent.parseGuardians(certificate.tags)?.firstOrNull()?.delay + val isJoined: Boolean get() = room != null } diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/ChatRoomDetailScreen.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/ChatRoomDetailScreen.kt index 8b86d17a..c65a494b 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/ChatRoomDetailScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/ChatRoomDetailScreen.kt @@ -1,5 +1,7 @@ package com.machankura.bennu.compose.ui.composable +import bennu.composeapp.generated.resources.guarded_wallet_after +import com.machankura.bennu.compose.ui.composable.widgets.delayText import bennu.composeapp.generated.resources.unguarded_wallet import bennu.composeapp.generated.resources.guarded_wallet import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading @@ -311,7 +313,7 @@ fun ChatRoomDetailScreen( // this room's wallet at its birth. // Nothing for one that decided // nothing, as before policies. - walletPolicyLabel(chatRoomDetailUIState.walletPolicy)?.let { Text(it) } + walletPolicyLabel(chatRoomDetailUIState.walletPolicy, chatRoomDetailUIState.guardianDelay)?.let { Text(it) } } } ) @@ -1083,7 +1085,7 @@ private fun SubgroupCard( ) // What the parent decided about its wallet, from the certificate: // founding history, like the name the row falls back to. - walletPolicyLabel(subgroup.walletPolicy)?.let { Text(it) } + walletPolicyLabel(subgroup.walletPolicy, subgroup.guardianDelay)?.let { Text(it) } } } ) @@ -1091,12 +1093,16 @@ private fun SubgroupCard( } /** - * *Guarded wallet* or *Unguarded wallet* for a policy a parent decided, and - * null for one it left to the subgroup -- `docs/subgroup-wallet-policy.md`. + * *Guarded wallet, after 144 blocks, about 1 day* or *Unguarded wallet* for a + * policy a parent decided, and null for one it left to the subgroup -- + * `docs/subgroup-wallet-policy.md`. The delay is the parent's own leaf's, + * `docs/guardian-delay.md`. */ @Composable -private fun walletPolicyLabel(policy: WalletPolicyReading?): String? = when (policy) { - WalletPolicyReading.Decided(SubgroupWalletPolicy.GUARDED) -> stringResource(Res.string.guarded_wallet) +private fun walletPolicyLabel(policy: WalletPolicyReading?, guardianDelay: Long?): String? = when (policy) { + WalletPolicyReading.Decided(SubgroupWalletPolicy.GUARDED) -> guardianDelay + ?.let { stringResource(Res.string.guarded_wallet_after, delayText(it)) } + ?: stringResource(Res.string.guarded_wallet) WalletPolicyReading.Decided(SubgroupWalletPolicy.UNGUARDED) -> stringResource(Res.string.unguarded_wallet) else -> null } diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreen.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreen.kt index 4df3ce63..a960db03 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreen.kt @@ -1,5 +1,7 @@ package com.machankura.bennu.compose.ui.composable +import bennu.composeapp.generated.resources.only_coins_confirmed_for_can_be_spent +import com.machankura.bennu.compose.ui.composable.widgets.delayText import bennu.composeapp.generated.resources.while_the_wallet_is_new_at_most_s_sats import androidx.compose.foundation.selection.toggleable import androidx.compose.material3.minimumInteractiveComponentSize @@ -222,6 +224,11 @@ fun GroupSendScreen( Text(problemWith(amountProblem, chain)) } else Column { spendable?.let { Text(stringResource(Res.string.s_sats_confirmed_and_free_to_spend, it.grouped())) } + // Why a guardian's balance can be lower than the + // wallet's: only coins past its own leaf's lock. + groupSendViewModel.guardianDelay()?.let { + Text(stringResource(Res.string.only_coins_confirmed_for_can_be_spent, delayText(it, chain))) + } cap?.let { Text(stringResource(Res.string.while_the_wallet_is_new_at_most_s_sats, it.grouped())) } } } diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupWalletScreen.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupWalletScreen.kt index d49576f7..cbe8418d 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupWalletScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupWalletScreen.kt @@ -1,5 +1,9 @@ package com.machankura.bennu.compose.ui.composable +import bennu.composeapp.generated.resources.this_group_s_delay_in_this_wallet +import bennu.composeapp.generated.resources.a_group_above_this_one +import bennu.composeapp.generated.resources.guardian_after +import com.machankura.bennu.compose.ui.composable.widgets.delayText import bennu.composeapp.generated.resources.the_group_above_decided_this_wallet_is_not_guarded import bennu.composeapp.generated.resources.the_group_above_decided_this_wallet_is_guarded import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading @@ -258,6 +262,9 @@ fun GroupWalletScreen( guardedOffered = state.guarded != null, plainOffered = state.offersPlain, decided = (state.policy as? WalletPolicyReading.Decided)?.policy, + guardians = state.guarded?.account?.guardians.orEmpty(), + guardianNames = state.guardianNames, + chain = chain, onOpen = { guarded -> groupWalletViewModel.openWallet( guarded = guarded, @@ -359,6 +366,10 @@ private fun OpenWalletAction( plainOffered: Boolean, /** What the room's parent decided at its birth, or null when it left it to the room. */ decided: SubgroupWalletPolicy?, + /** The guardians a guarded wallet would name, nearest first, each with its own delay. */ + guardians: List, + guardianNames: Map, + chain: Chain, onOpen: (guarded: Boolean) -> Unit, ) { Column( @@ -381,14 +392,12 @@ private fun OpenWalletAction( // strand a quorum. See docs/subgroup-wallet-policy.md. Text( text = when (decided) { - SubgroupWalletPolicy.GUARDED -> stringResource( - Res.string.the_group_above_decided_this_wallet_is_guarded, - GuardianDerivation.DEFAULT_DELAY.toInt() - ) + SubgroupWalletPolicy.GUARDED -> stringResource(Res.string.the_group_above_decided_this_wallet_is_guarded) SubgroupWalletPolicy.UNGUARDED -> stringResource(Res.string.the_group_above_decided_this_wallet_is_not_guarded) }, style = MaterialTheme.typography.bodyMedium, ) + if (decided == SubgroupWalletPolicy.GUARDED) GuardianDelays(guardians, guardianNames, chain) val guarded = decided == SubgroupWalletPolicy.GUARDED if (guarded && guardedOffered || !guarded && plainOffered) { Button(onClick = { onOpen(guarded) }, enabled = !isPending) { @@ -403,12 +412,10 @@ private fun OpenWalletAction( // it rather than hidden: being guarded is then the group's own choice, // and its quorum signing the statement is it making that choice. Text( - text = stringResource( - Res.string.the_groups_above_this_one_have_agreed_to_guard_it, - GuardianDerivation.DEFAULT_DELAY.toInt() - ), + text = stringResource(Res.string.the_groups_above_this_one_have_agreed_to_guard_it), style = MaterialTheme.typography.bodyMedium, ) + GuardianDelays(guardians, guardianNames, chain) Button(onClick = { onOpen(true) }, enabled = !isPending) { Icon(Icons.Default.AccountBalanceWallet, contentDescription = Decorative) Spacer(modifier = Modifier.width(MaterialTheme.spacing.relatedGap)) @@ -455,14 +462,14 @@ private fun OpenWallet( if (state.wallet.account.isGuarded) { item { - Text( - text = stringResource( - Res.string.guarded_the_groups_above_this_one_can_spend, - GuardianDerivation.DEFAULT_DELAY.toInt() - ), - style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant, - ) + Column(verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap)) { + Text( + text = stringResource(Res.string.guarded_the_groups_above_this_one_can_spend), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + GuardianDelays(state.wallet.account.guardians, state.guardianNames, state.wallet.chain) + } } } @@ -669,7 +676,7 @@ private fun LazyListScope.guardianItems( } item { Text( - text = stringResource(Res.string.wallets_of_groups_below_this_one, GuardianDerivation.DEFAULT_DELAY.toInt()), + text = stringResource(Res.string.wallets_of_groups_below_this_one), style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, ) @@ -679,10 +686,15 @@ private fun LazyListScope.guardianItems( leadingContent = { Icon(Icons.Default.AccountBalanceWallet, contentDescription = Decorative) }, headlineContent = { Text(row.wallet.name ?: stringResource(Res.string.a_subgroup), maxLines = 1, overflow = TextOverflow.Ellipsis) }, supportingContent = { - Text( - row.withinReach?.let { stringResource(Res.string.s_sats_within_this_group_s_reach, it.grouped()) } - ?: stringResource(Res.string.balance_not_known_yet) - ) + Column { + Text( + row.withinReach?.let { stringResource(Res.string.s_sats_within_this_group_s_reach, it.grouped()) } + ?: stringResource(Res.string.balance_not_known_yet) + ) + // This group's own leaf, which two of its descendants can + // have set differently. docs/guardian-delay.md. + Text(stringResource(Res.string.this_group_s_delay_in_this_wallet, delayText(row.wallet.delay, row.wallet.chain))) + } }, trailingContent = onSpend?.let { spend -> { @@ -713,6 +725,31 @@ private fun LazyListScope.guardianItems( } } +/** + * Each guardian of a guarded wallet with its own delay, nearest first: + * *#Council: after 144 blocks, about 1 day*. One sentence with one number in it + * stopped being true once each group chose its own -- `docs/guardian-delay.md`. + */ +@Composable +private fun GuardianDelays( + guardians: List, + names: Map, + chain: Chain, +) { + Column(verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap)) { + guardians.forEach { guardian -> + Text( + text = stringResource( + Res.string.guardian_after, + names[guardian.chatRoomId] ?: stringResource(Res.string.a_group_above_this_one), + delayText(guardian.delay, chain), + ), + style = MaterialTheme.typography.bodyMedium, + ) + } + } +} + @Composable private fun BalanceCard(walletState: GroupWalletState?) { Card(modifier = Modifier.fillMaxWidth()) { diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/ChatRoomDetailViewModel.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/ChatRoomDetailViewModel.kt index cb19cf0e..ee05f2fb 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/ChatRoomDetailViewModel.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/ChatRoomDetailViewModel.kt @@ -93,6 +93,7 @@ class ChatRoomDetailViewModel( subgroups = chatRepository.subgroupsOf(chatRoomId), parentChatRoomId = chatRepository.parentOf(chatRoomId), walletPolicy = chatRepository.walletPolicyOf(chatRoomId), + guardianDelay = chatRepository.guardiansOf(chatRoomId)?.firstOrNull()?.delay, groupWallet = chatRepository.groupWallet(chatRoomId, NodeParamsManager.chain), canAddSubgroup = canSign, canGovern = canGovern, diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupSendViewModel.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupSendViewModel.kt index e3eb9088..9ed8509f 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupSendViewModel.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupSendViewModel.kt @@ -146,6 +146,17 @@ class GroupSendViewModel( fun amount(): SatsInput = parseSats(amountField.text) /** The confirmed coins no open spend holds, summed, or null before Electrum has answered. */ + /** + * How long a coin has to have waited before this group, as a guardian, can + * spend it -- its own leaf's delay -- or null when the group spends its own + * wallet. `docs/guardian-delay.md`. + */ + fun guardianDelay(): Long? { + val state = groupSendUIState as? GroupSendUIState.Ready ?: return null + val guardian = state.spender as? GroupSpend.Spender.Guardian ?: return null + return state.wallet.account.guardians.getOrNull(guardian.position)?.delay + } + fun spendable(): Satoshi? { val state = groupSendUIState as? GroupSendUIState.Ready ?: return null return state.walletState?.let { GroupSpendBuilder.spendableBalance(it, state.reserved, state.spender) } diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletViewModel.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletViewModel.kt index 77ceb227..3fd30148 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletViewModel.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletViewModel.kt @@ -1,5 +1,6 @@ package com.machankura.bennu.compose.ui.view.model +import com.machankura.bennu.compose.managers.GuardianDerivation import com.machankura.bennu.compose.nostr.subgroup.isDecided import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy import com.machankura.bennu.compose.database.model.GroupTransaction @@ -103,7 +104,7 @@ class GroupWalletViewModel( offersPlain = !policy.isDecided(SubgroupWalletPolicy.GUARDED), guardedWallets = guardedRows, shareable = shareable, - ) + ).let { it.copy(guardianNames = namesOf(it.guarded?.account?.guardians.orEmpty())) } groupWalletUIState = notOpened if (guarded.isNotEmpty()) { watching = viewModelScope.launch(Dispatchers.IO) { @@ -121,6 +122,7 @@ class GroupWalletViewModel( canSign = canSign, guardedWallets = guardedRows, shareable = shareable, + guardianNames = namesOf(wallet.account.guardians), ) groupWalletUIState = loaded @@ -147,6 +149,17 @@ class GroupWalletViewModel( * Each guarded wallet with what the chain says of it, on the identity's own * Electrum client -- or with nothing, for a profile that cannot watch. */ + /** + * What this device calls each of [guardians]' rooms, for the ones it holds: + * the wallet screen lists each guardian with its own delay, and a room id is + * not a name. A guardian whose room this device does not have is left out, + * and the screen says *a group above this one*. + */ + private suspend fun namesOf(guardians: List): Map = + guardians.mapNotNull { guardian -> + chatRepository.getChatRoomByIdentifier(guardian.chatRoomId)?.chatRoom?.subject?.let { guardian.chatRoomId to it } + }.toMap() + private suspend fun guardedStates(guarded: List): Flow> { val wallets = wallets if (wallets == null || guarded.isEmpty()) return flowOf(guarded.map { GuardedWalletRow(it, walletState = null) }) diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/ChatRoomDetailUIState.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/ChatRoomDetailUIState.kt index 895ed60f..be72765e 100755 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/ChatRoomDetailUIState.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/ChatRoomDetailUIState.kt @@ -54,6 +54,11 @@ sealed interface ChatRoomDetailUIState { * because it is a fact about where the room came from. */ val walletPolicy: WalletPolicyReading? = null, + /** + * The delay [parentChatRoomId] gave its own leaf in this room's wallet, + * or null for a room whose certificate grants no keys. + */ + val guardianDelay: Long? = null, /** * The wallet this group has opened on the app's network, or null for one * it has not. The row says only whether it is open; the coins are read on diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletUIState.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletUIState.kt index bd3cd127..b59d2c7b 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletUIState.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletUIState.kt @@ -4,7 +4,6 @@ import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading import com.machankura.bennu.compose.database.model.intermdiate.LocalChatRoom import com.machankura.bennu.compose.managers.GroupWalletDerivation import com.machankura.bennu.compose.managers.GroupWalletManager -import com.machankura.bennu.compose.managers.GuardianDerivation import com.machankura.bennu.compose.managers.GuardianManager import fr.acinq.bitcoin.Satoshi import fr.acinq.lightning.utils.sum @@ -38,6 +37,8 @@ sealed interface GroupWalletUIState { val canSign: Boolean, val guarded: GroupWalletManager.GuardedOpening? = null, val policy: WalletPolicyReading? = null, + /** What this device calls each guardian's room, by room id, for the ones it holds. */ + val guardianNames: Map = emptyMap(), /** Whether a plain wallet may be opened: false only when the parent decided this one is guarded. */ val offersPlain: Boolean = true, /** Guarded wallets of groups below this one. A group with no wallet of its own can still guard others'. */ @@ -65,6 +66,8 @@ sealed interface GroupWalletUIState { val guardedWallets: List = emptyList(), /** Guarded statements this device can carry to the groups above. */ val shareable: List = emptyList(), + /** What this device calls each guardian's room, by room id, for the ones it holds. */ + val guardianNames: Map = emptyMap(), ): GroupWalletUIState data class Error( @@ -82,11 +85,12 @@ data class GuardedWalletRow( ) { /** * What this group could spend now: coins confirmed at least as deep as its - * leaf's lock. Null while the chain is not known. + * own leaf's lock -- not the default's, since the group that granted the + * leaf chose it. Null while the chain is not known. */ val withinReach: Satoshi? get() = walletState?.let { state -> - state.utxos.filter { state.confirmations(it) >= GuardianDerivation.DEFAULT_DELAY }.map { it.amount }.sum() + state.utxos.filter { state.confirmations(it) >= wallet.delay }.map { it.amount }.sum() } } diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GuardianSigningJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GuardianSigningJvmTest.kt index 5a96044d..8fc815db 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GuardianSigningJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GuardianSigningJvmTest.kt @@ -1,5 +1,6 @@ package com.machankura.bennu.compose.managers +import com.machankura.bennu.compose.ui.view.state.GuardedWalletRow import com.machankura.bennu.compose.database.model.GroupKeyState import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy import androidx.room3.Room @@ -560,6 +561,27 @@ class GuardianSigningJvmTest { } } + /** + * Two ancestors who gave the same wallet different delays: a coin 100 blocks + * deep is within the reach of the one at 12 and not the one at 4,320. + * docs/guardian-delay.md, Phase 7. + */ + @Test + fun `a guarded wallet's reach is each ancestor's own delay`() { + val farKey = PrivateKey(ByteVector32(ByteArray(32) { 0x5e })).publicKey().toString() + val farRoom = SharedKeyDerivation.marmotGroupId(farKey) + val inherited = GuardianDerivation.grant(farKey, farRoom, emptyList(), roomId, delay = 4_320) + val guardians = GuardianDerivation.grant(thresholdPublicKey, roomId, inherited, guardedChildRoomId, delay = 12) + val near = assertNotNull(GuardianManager.verify(guardedStatement(guardians), roomId, thresholdPublicKey)) + val far = near.copy(ancestorChatRoomId = farRoom) + assertEquals(12L, near.delay) + assertEquals(4_320L, far.delay) + + val state = fundedState(near.account, fundingOf(near.account), height = 100, tip = 199) + assertEquals(Satoshi(50_000), GuardedWalletRow(near, state).withinReach) + assertEquals(Satoshi(0), GuardedWalletRow(far, state).withinReach) + } + @Test fun `an ancestor's quorum spends a descendant's coin through its leaf, on every device`(): Unit = runBlocking { val proposer = device(members[0], signerIndex = 0) diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletViewModelJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletViewModelJvmTest.kt index cf6f8ada..096be9ac 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletViewModelJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletViewModelJvmTest.kt @@ -268,6 +268,16 @@ class GroupWalletViewModelJvmTest { assertTrue(root.offersPlain) } + @Test + fun `each guardian is named by the room this device holds for it`() { + val state = assertIs( + viewModel(guardedRepository(Chain.Regtest), chain = Chain.Regtest).apply { initiate() }.settled() + ) + + // The fixture's repository answers every room id with the one room. + assertEquals(mapOf(guardians.single().chatRoomId to localChatRoom.chatRoom.subject!!), state.guardianNames) + } + @Test fun `a plain wallet is never proposed under a parent that decided guarded`() { val signing = RecordingSigning()