diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 15a78802..9c35a530 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -656,4 +656,6 @@ 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 not guarded. Only this group can spend from it. 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 ac7b0b4d..214819e1 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.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 +import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy import androidx.compose.foundation.layout.Row import com.machankura.bennu.compose.text.ProposedSpend import com.machankura.bennu.compose.ui.view.state.SpendRow @@ -237,11 +241,13 @@ fun GroupWalletScreen( // Hidden rather than disabled, the way every gated way in // on the group's screens is: a device with no share cannot // propose, and a room with no key has nothing to open. - action = if (state.canSign && state.account != null) { + action = if (state.canSign && state.account != null && (state.offersPlain || state.guarded != null)) { { OpenWalletAction( isPending = groupWalletViewModel.isActionPending.value, guardedOffered = state.guarded != null, + plainOffered = state.offersPlain, + decided = (state.policy as? WalletPolicyReading.Decided)?.policy, onOpen = { guarded -> groupWalletViewModel.openWallet( guarded = guarded, @@ -337,7 +343,14 @@ fun GroupWalletScreen( * an unencrypted share on a member's device means once there are coins behind it. */ @Composable -private fun OpenWalletAction(isPending: Boolean, guardedOffered: Boolean, onOpen: (guarded: Boolean) -> Unit) { +private fun OpenWalletAction( + isPending: Boolean, + guardedOffered: Boolean, + plainOffered: Boolean, + /** What the room's parent decided at its birth, or null when it left it to the room. */ + decided: SubgroupWalletPolicy?, + onOpen: (guarded: Boolean) -> Unit, +) { Column( horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap) @@ -351,11 +364,34 @@ private fun OpenWalletAction(isPending: Boolean, guardedOffered: Boolean, onOpen style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, ) - if (guardedOffered) { - // The default for a subgroup whose parent agreed to guard it, and the - // plain wallet beside it rather than hidden: being guarded is the - // group's own choice, and its quorum signing the statement is it - // making that choice. + if (decided != null) { + // The parent decided at the room's birth, and its quorum signed it: + // one way in, and the line says whose decision it was. Every other + // member refuses the other statement, so offering it would only + // 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.ANCESTOR_DELAY.toInt() + ) + SubgroupWalletPolicy.UNGUARDED -> stringResource(Res.string.the_group_above_decided_this_wallet_is_not_guarded) + }, + style = MaterialTheme.typography.bodyMedium, + ) + val guarded = decided == SubgroupWalletPolicy.GUARDED + if (guarded && guardedOffered || !guarded && plainOffered) { + Button(onClick = { onOpen(guarded) }, enabled = !isPending) { + Icon(Icons.Default.AccountBalanceWallet, contentDescription = Decorative) + Spacer(modifier = Modifier.width(MaterialTheme.spacing.relatedGap)) + Text(stringResource(if (guarded) Res.string.open_a_guarded_wallet else Res.string.open_a_group_wallet)) + } + } + } else if (guardedOffered) { + // Undecided: a subgroup certified before parents decided. The default + // for one whose parent agreed to guard it, and the plain wallet beside + // 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, 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 8ead9bb5..77ceb227 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,7 @@ package com.machankura.bennu.compose.ui.view.model +import com.machankura.bennu.compose.nostr.subgroup.isDecided +import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy import com.machankura.bennu.compose.database.model.GroupTransaction import com.machankura.bennu.compose.ui.view.state.SpendRow import com.machankura.bennu.compose.ui.view.state.SpendStatus @@ -84,13 +86,21 @@ class GroupWalletViewModel( watching?.cancel() if (wallet == null) { + // What the room's parent decided at its birth: a decided policy + // leaves one way in, and an undecided or absent one both, as + // before policies. docs/subgroup-wallet-policy.md. + val policy = chatRepository.walletPolicyOf(chatRoomId) + val offersGuarded = !policy.isDecided(SubgroupWalletPolicy.UNGUARDED) && + GroupWalletRollout.offersGuardiansOn(chain) val notOpened = GroupWalletUIState.NotOpened( localChatRoom = localChatRoom, account = chatRepository.groupWalletAccount(chatRoomId, chain), canSign = canSign, - guarded = if (GroupWalletRollout.offersGuardiansOn(chain)) { + guarded = if (offersGuarded) { chatRepository.groupWalletGuardedOpening(chatRoomId, chain) } else null, + policy = policy, + offersPlain = !policy.isDecided(SubgroupWalletPolicy.GUARDED), guardedWallets = guardedRows, shareable = shareable, ) @@ -262,6 +272,9 @@ class GroupWalletViewModel( val opening = state.guarded ?: return GroupWalletStatementEvent.template(opening.account, opening.certificate) } else { + // Not under a parent that decided this wallet is guarded: no state + // offers it, and every other member would refuse it anyway. + if (!state.offersPlain) return GroupWalletStatementEvent.template(state.account ?: return) } // A second statement for the same account is harmless -- the same values 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 12691e90..baf38de0 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 @@ -1,5 +1,6 @@ package com.machankura.bennu.compose.ui.view.state +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 @@ -25,12 +26,20 @@ sealed interface GroupWalletUIState { * [guarded] is the guarded account a subgroup could open instead, when its * birth certificate grants it guardian keys and guarded wallets are offered * on this network -- see `docs/group-wallet-guardians.md`. + * + * [policy] is what the room's own certificate decided about its wallet, or + * null for a room with none -- `docs/subgroup-wallet-policy.md`. A decided + * policy leaves one way in: [guarded] alone for *guarded*, the plain + * [account] alone for *unguarded*, which [offersPlain] and [guarded] say. */ data class NotOpened( val localChatRoom: LocalChatRoom, val account: GroupWalletDerivation.Account?, val canSign: Boolean, val guarded: GroupWalletManager.GuardedOpening? = null, + val policy: WalletPolicyReading? = null, + /** 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'. */ val guardedWallets: List = emptyList(), /** Guarded statements this device can carry to the groups above. */ 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 09d3df5a..cf6f8ada 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 @@ -1,5 +1,8 @@ package com.machankura.bennu.compose.ui.view.model +import kotlin.test.assertTrue +import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading +import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy import com.machankura.bennu.compose.database.model.ChatRoom import com.machankura.bennu.compose.database.model.FrostSigningSession import com.machankura.bennu.compose.database.model.GroupSignedEvent @@ -181,7 +184,12 @@ class GroupWalletViewModelJvmTest { "b".repeat(64), emptyList(), chatRoomId, ) - private fun guardedRepository(chain: Chain) = object : ChatRepository by repository(opened = null) { + private fun guardedRepository( + chain: Chain, + policy: WalletPolicyReading? = WalletPolicyReading.Undecided, + ) = object : ChatRepository by repository(opened = null) { + override suspend fun walletPolicyOf(chatRoomId: String): WalletPolicyReading? = policy + override suspend fun groupWalletGuardedOpening(chatRoomId: String, chain: Chain) = com.machankura.bennu.compose.managers.GroupWalletManager.GuardedOpening( GroupWalletDerivation.account( @@ -222,6 +230,59 @@ class GroupWalletViewModelJvmTest { assertEquals(certificate.id, reading.certificate?.id) } + // ---- the parent's decision: docs/subgroup-wallet-policy.md ------------- + + @Test + fun `a subgroup whose parent decided guarded is offered the guarded wallet alone`() { + val state = assertIs( + viewModel(guardedRepository(Chain.Regtest, WalletPolicyReading.Decided(SubgroupWalletPolicy.GUARDED)), chain = Chain.Regtest) + .apply { initiate() }.settled() + ) + + assertNotNull(state.guarded) + assertFalse(state.offersPlain) + } + + @Test + fun `a subgroup whose parent decided unguarded is offered the plain wallet alone`() { + val state = assertIs( + viewModel(guardedRepository(Chain.Regtest, WalletPolicyReading.Decided(SubgroupWalletPolicy.UNGUARDED)), chain = Chain.Regtest) + .apply { initiate() }.settled() + ) + + assertNull(state.guarded) + assertTrue(state.offersPlain) + assertEquals(account, state.account) + } + + @Test + fun `an undecided subgroup is offered both, and a root group the plain one`() { + val undecided = assertIs( + viewModel(guardedRepository(Chain.Regtest), chain = Chain.Regtest).apply { initiate() }.settled() + ) + assertNotNull(undecided.guarded) + assertTrue(undecided.offersPlain) + + val root = assertIs(viewModel(repository(opened = null)).apply { initiate() }.settled()) + assertNull(root.guarded) + assertTrue(root.offersPlain) + } + + @Test + fun `a plain wallet is never proposed under a parent that decided guarded`() { + val signing = RecordingSigning() + val model = viewModel( + guardedRepository(Chain.Regtest, WalletPolicyReading.Decided(SubgroupWalletPolicy.GUARDED)), signing, chain = Chain.Regtest + ).apply { initiate() } + model.settled() + + model.openWallet(guarded = false, onSuccess = {}, onFailure = {}) + runBlocking { delay(200) } + + assertNull(signing.proposed) + assertFalse(model.isActionPending.value) + } + // ---- spends ------------------------------------------------------------ private val coinTx = fr.acinq.bitcoin.Transaction(