diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml
index e146d902..15a78802 100644
--- a/composeApp/src/commonMain/composeResources/values/strings.xml
+++ b/composeApp/src/commonMain/composeResources/values/strings.xml
@@ -644,4 +644,16 @@
Sent again
Couldn't bump the fee: there isn't enough left over to pay it.
While the wallet is new, a spend on mainnet can send at most %1$s sats.
+
+
+ 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.
+ 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 not be guarded: only the subgroup can spend from it.
diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/database/repository/DatabaseChatRepository.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/database/repository/DatabaseChatRepository.kt
index 041a248c..3ca893c5 100644
--- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/database/repository/DatabaseChatRepository.kt
+++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/database/repository/DatabaseChatRepository.kt
@@ -1,5 +1,7 @@
package com.machankura.bennu.compose.database.repository
+import com.machankura.bennu.compose.managers.GuardianDerivation
+import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading
import fr.acinq.bitcoin.Chain
import com.machankura.bennu.compose.managers.GroupWalletManager
import com.machankura.bennu.compose.managers.GuardianManager
@@ -144,6 +146,23 @@ class DatabaseChatRepository(
null
}
+ override suspend fun walletPolicyOf(chatRoomId: String): WalletPolicyReading? = try {
+ SubgroupManager.walletPolicyOf(database, chatRoomId)
+ } catch (e: Throwable) {
+ logger.e("Error reading the wallet policy of $chatRoomId", e)
+ null
+ }
+
+ // Null on a failure, which reads as keys this build cannot read: the picker
+ // then refuses a guarded subgroup rather than promising a grant it could not
+ // check.
+ override suspend fun guardiansOf(chatRoomId: String): List? = try {
+ SubgroupManager.guardiansOf(database, chatRoomId)
+ } catch (e: Throwable) {
+ logger.e("Error reading the guardians of $chatRoomId", e)
+ null
+ }
+
override suspend fun groupNostrProfile(chatRoomId: String): GroupNostrProfile? = try {
GroupNostrProfile.newestAmong(
signedEvents = database.groupSignedEventDao()
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 f94af125..fab5787a 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
@@ -1,5 +1,6 @@
package com.machankura.bennu.compose.managers
+import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading
import co.touchlab.kermit.Logger
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.core.HexKey
@@ -274,6 +275,31 @@ object SubgroupManager {
return SubgroupBirthCertificateEvent.parseGuardians(certificate.tags)
}
+ /**
+ * What [chatRoomId]'s own certificate says of its wallet --
+ * `docs/subgroup-wallet-policy.md` -- or null for a room with no certificate:
+ * a root group, or a member who holds no key state for it.
+ *
+ * Off the verified key state, like [guardiansOf]. A state whose certificate
+ * carries a policy that does not read is dropped before it is stored, so a
+ * certificate held here always reads; if one somehow does not, it is logged
+ * and read as none rather than guessed at.
+ *
+ * The one reader the picker, every parent signer and the wallet screen share,
+ * so the three cannot disagree about what a group is.
+ */
+ suspend fun walletPolicyOf(database: BennuDatabase, chatRoomId: String): WalletPolicyReading? {
+ val certificate = database.groupKeyStateDao().getByChatRoomId(chatRoomId)
+ ?.birthCertificateJson
+ ?.let { Event.fromJsonOrNull(it) }
+ ?: return null
+ return SubgroupBirthCertificateEvent.parseWalletPolicy(certificate.tags)
+ ?: run {
+ logger.w("$chatRoomId's certificate carries a wallet policy that does not read")
+ null
+ }
+ }
+
/**
* What [parentChatRoomId], signing as [thresholdPublicKey], grants
* [subgroupChatRoomId]: `GuardianDerivation.grant` over the keys its own
diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/repository/ChatRepository.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/repository/ChatRepository.kt
index 8892ed81..592304c7 100644
--- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/repository/ChatRepository.kt
+++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/repository/ChatRepository.kt
@@ -1,5 +1,7 @@
package com.machankura.bennu.compose.repository
+import com.machankura.bennu.compose.managers.GuardianDerivation
+import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading
import fr.acinq.bitcoin.Chain
import com.machankura.bennu.compose.managers.GroupWalletDerivation
import com.machankura.bennu.compose.managers.GroupWalletManager
@@ -88,6 +90,19 @@ interface ChatRepository {
*/
suspend fun parentOf(chatRoomId: String): HexKey?
+ /**
+ * What this room's own birth certificate says of its wallet, or null for a
+ * room with none. See `SubgroupManager.walletPolicyOf`.
+ */
+ suspend fun walletPolicyOf(chatRoomId: String): WalletPolicyReading?
+
+ /**
+ * The guardian keys this room's own certificate grants it, nearest first --
+ * empty for a room with none, null when they do not read. See
+ * `SubgroupManager.guardiansOf`.
+ */
+ suspend fun guardiansOf(chatRoomId: String): List?
+
/**
* The kind:0 this group signed about itself, or null while it has signed
* none.
@@ -387,6 +402,10 @@ interface ChatRepository {
override suspend fun parentOf(chatRoomId: String): HexKey? = null
+ override suspend fun walletPolicyOf(chatRoomId: String): WalletPolicyReading? = null
+
+ override suspend fun guardiansOf(chatRoomId: String): List? = emptyList()
+
override suspend fun groupNostrProfile(chatRoomId: String): GroupNostrProfile? = null
override suspend fun groupRelayLists(chatRoomId: String): List =
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 ed214e07..f1f113e3 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,12 @@
package com.machankura.bennu.compose.ui.composable
+import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy
+import com.machankura.bennu.compose.managers.GuardianDerivation
+import com.machankura.bennu.compose.database.model.subgroupWalletPolicy
+import org.jetbrains.compose.resources.stringResource
+import bennu.composeapp.generated.resources.the_subgroup_s_wallet_will_not_be_guarded
+import bennu.composeapp.generated.resources.the_subgroup_s_wallet_will_be_guarded
+import bennu.composeapp.generated.resources.Res
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
@@ -59,6 +66,23 @@ fun DkgJoinApprovalScreen(
style = MaterialTheme.typography.bodyLarge
)
+ // What the subgroup's wallet will be, as the coordinator chose it and the
+ // parent's quorum will sign it -- the one place an admin is told before
+ // joining is agreeing to it. A ceremony with no policy says nothing.
+ // See docs/subgroup-wallet-policy.md.
+ session.subgroupWalletPolicy?.let { policy ->
+ Text(
+ text = when (policy) {
+ SubgroupWalletPolicy.GUARDED -> stringResource(
+ Res.string.the_subgroup_s_wallet_will_be_guarded,
+ GuardianDerivation.ANCESTOR_DELAY.toInt()
+ )
+ SubgroupWalletPolicy.UNGUARDED -> stringResource(Res.string.the_subgroup_s_wallet_will_not_be_guarded)
+ },
+ style = MaterialTheme.typography.bodyLarge
+ )
+ }
+
Column(
modifier = Modifier.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap)
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 d464218f..5ec99389 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,5 +1,23 @@
package com.machankura.bennu.compose.ui.composable
+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
+import bennu.composeapp.generated.resources.this_group_and_the_d_above_it_can_spend
+import bennu.composeapp.generated.resources.this_group_can_spend_its_coins_once
+import bennu.composeapp.generated.resources.unguarded_wallet
+import bennu.composeapp.generated.resources.guarded_wallet
+import bennu.composeapp.generated.resources.the_subgroup_s_wallet
+import androidx.compose.ui.semantics.semantics
+import androidx.compose.ui.semantics.selected
+import androidx.compose.ui.semantics.role
+import androidx.compose.ui.semantics.Role
+import androidx.compose.material3.RadioButton
+import androidx.compose.foundation.selection.selectableGroup
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
@@ -70,9 +88,10 @@ import com.machankura.bennu.compose.ui.view.model.SelectSubgroupAdminsViewModel
import com.machankura.bennu.compose.ui.view.state.SelectSubgroupAdminsUIState
/**
- * Picks who runs a subgroup, what it is called, and on what quorum.
+ * Picks who runs a subgroup, what it is called, on what quorum, and whether its
+ * wallet is guarded.
*
- * The three things that cannot be changed later, gathered before anything is
+ * The four things that cannot be changed later, gathered before anything is
* published -- see `SelectSubgroupAdminsViewModel` for why the threshold in
* particular has nowhere else to live.
*/
@@ -274,6 +293,16 @@ fun SelectSubgroupAdminsScreen(
onQuorumChange = viewModel::setThreshold
)
}
+
+ item {
+ WalletPolicyChooser(
+ policy = viewModel.walletPolicy.value,
+ parentGuardianCount = state.parentGuardians?.size ?: 0,
+ onlyGuarded = state.onlyGuarded,
+ canGuard = state.canGuard,
+ onPolicyChange = viewModel::setWalletPolicy
+ )
+ }
}
}
}
@@ -331,6 +360,93 @@ private fun QuorumStepper(
}
}
+/**
+ * Whether the subgroup's wallet is guarded -- `docs/subgroup-wallet-policy.md`.
+ *
+ * Settled here for the reason the threshold is: it rides the proposal, every
+ * admin is shown it before joining, and the parent's quorum signs it into the
+ * certificate, so there is no later screen where it could be changed.
+ *
+ * Under a guarded parent both rows still show, with *unguarded* disabled and its
+ * reason in place of its description. Hiding it would make the picker look like
+ * an older build's, where nobody decided anything; showing it says a decision was
+ * made, and by whom.
+ */
+@Composable
+private fun WalletPolicyChooser(
+ policy: SubgroupWalletPolicy,
+ parentGuardianCount: Int,
+ onlyGuarded: Boolean,
+ canGuard: Boolean,
+ onPolicyChange: (SubgroupWalletPolicy) -> Unit,
+) {
+ Column(
+ modifier = Modifier
+ .fillMaxWidth()
+ .padding(top = MaterialTheme.spacing.space50)
+ .selectableGroup(),
+ verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.space50)
+ ) {
+ Text(
+ text = stringResource(Res.string.the_subgroup_s_wallet),
+ style = MaterialTheme.typography.titleSmall
+ )
+
+ val delay = GuardianDerivation.ANCESTOR_DELAY.toInt()
+ WalletPolicyRow(
+ title = stringResource(Res.string.guarded_wallet),
+ // Names the reach the parent is actually granting: itself, and each
+ // ancestor whose key its own certificate passes on.
+ 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)
+ } else {
+ stringResource(Res.string.this_group_and_the_d_above_it_can_spend, parentGuardianCount, delay)
+ },
+ isSelected = policy == SubgroupWalletPolicy.GUARDED,
+ isEnabled = canGuard,
+ onSelect = { onPolicyChange(SubgroupWalletPolicy.GUARDED) }
+ )
+ WalletPolicyRow(
+ title = stringResource(Res.string.unguarded_wallet),
+ description = if (onlyGuarded) {
+ stringResource(Res.string.this_group_is_guarded_so_its_subgroups_are_too)
+ } else {
+ stringResource(Res.string.only_the_subgroup_can_spend_from_its_wallet)
+ },
+ isSelected = policy == SubgroupWalletPolicy.UNGUARDED,
+ isEnabled = !onlyGuarded,
+ onSelect = { onPolicyChange(SubgroupWalletPolicy.UNGUARDED) }
+ )
+ }
+}
+
+@Composable
+private fun WalletPolicyRow(
+ title: String,
+ description: String,
+ isSelected: Boolean,
+ isEnabled: Boolean,
+ onSelect: () -> Unit,
+) {
+ // The card is the target, as the admin rows above are, and the radio button
+ // beside the title only says which one is chosen.
+ Card(
+ modifier = Modifier.fillMaxWidth().semantics { role = Role.RadioButton; selected = isSelected },
+ onClick = onSelect,
+ enabled = isEnabled
+ ) {
+ ListItem(
+ leadingContent = {
+ RadioButton(selected = isSelected, onClick = null, enabled = isEnabled)
+ },
+ headlineContent = { Text(title) },
+ supportingContent = { Text(description) }
+ )
+ }
+}
+
@ConformancePreviews
@Composable
private fun SelectSubgroupAdminsScreenPreview() {
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 2e641906..8d4afb36 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,10 @@
package com.machankura.bennu.compose.ui.view.model
+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
+import com.machankura.bennu.compose.nostr.subgroup.isDecided
+import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
@@ -28,9 +33,12 @@ import com.machankura.bennu.compose.ui.composable.navigation.routes.Route
import com.machankura.bennu.compose.ui.view.state.SelectSubgroupAdminsUIState
/**
- * Who runs a subgroup, what it is called, and on what quorum.
+ * Who runs a subgroup, what it is called, on what quorum, and whether its wallet
+ * is guarded.
*
- * All three are settled here and nowhere else. The name and the admin set are
+ * All four are settled here and nowhere else. The wallet policy rides the
+ * proposal like the threshold and is signed into the birth certificate by the
+ * parent's quorum -- `docs/subgroup-wallet-policy.md`. The name and the admin set are
* carried into the birth certificate, so they are what the parent's admins will
* be asked to approve; the threshold cannot move afterwards at all, because
* ChillDKG hashes it and the host keys into the session identity, so `t` is fixed
@@ -81,6 +89,14 @@ class SelectSubgroupAdminsViewModel(
val threshold: MutableState = mutableStateOf(ChatRoomType.MINIMUM_QUORUM)
+ /**
+ * What the subgroup's wallet is to be -- `docs/subgroup-wallet-policy.md`.
+ * Guarded unless the coordinator says otherwise, and only ever guarded under a
+ * parent that is guarded itself: [setWalletPolicy] will not move it, so the
+ * state never holds a choice the screen shows as disabled.
+ */
+ val walletPolicy: MutableState = mutableStateOf(SubgroupWalletPolicy.GUARDED)
+
/** Whether [retrying] has been read into the form, which happens once. */
private var prefilled = false
@@ -98,11 +114,24 @@ class SelectSubgroupAdminsViewModel(
.distinctBy { it.participant.participantPublicKey }
.filterNot { it.participant.participantPublicKey == activeUserPublicKey }
+ // The parent's own wallet policy and guardians, read once: whether it
+ // may make an unguarded child at all, and whether it can grant a
+ // guarded one anything.
+ val parentPolicy = chatRepository.walletPolicyOf(parentChatRoomId)
+ val parentGuardians = chatRepository.guardiansOf(parentChatRoomId)
+
uiState = SelectSubgroupAdminsUIState.Loaded(
parentRoom = parentRoom,
candidates = candidates,
+ parentPolicy = parentPolicy,
+ parentGuardians = parentGuardians,
)
+ // A parent that cannot grant keys can only make an unguarded child,
+ // and one that is guarded can only make a guarded one.
+ if (parentGuardians == null) walletPolicy.value = SubgroupWalletPolicy.UNGUARDED
+ if (parentPolicy.isDecided(SubgroupWalletPolicy.GUARDED)) walletPolicy.value = SubgroupWalletPolicy.GUARDED
+
// Before the key-package lookup, so anybody who can no longer be
// welcomed is unpicked by the same rule as a fresh pick would be.
prefillFromRetry(candidates.map { it.participant.participantPublicKey }.toSet())
@@ -137,6 +166,11 @@ class SelectSubgroupAdminsViewModel(
)
threshold.value = failed.threshold.coerceIn(quorumRange())
+
+ // Through the setter, so a policy the parent no longer allows -- the
+ // 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)
}
/**
@@ -188,6 +222,20 @@ class SelectSubgroupAdminsViewModel(
name.value = value
}
+ /**
+ * Chooses the subgroup's wallet policy. *Unguarded* under a guarded parent is
+ * ignored -- D4 -- and so is *guarded* under a parent that cannot grant keys.
+ */
+ fun setWalletPolicy(value: SubgroupWalletPolicy) {
+ if (isActionPending.value) return
+
+ val loaded = uiState as? SelectSubgroupAdminsUIState.Loaded ?: return
+ if (value == SubgroupWalletPolicy.UNGUARDED && loaded.onlyGuarded) return
+ if (value == SubgroupWalletPolicy.GUARDED && !loaded.canGuard) return
+
+ walletPolicy.value = value
+ }
+
/** The subgroup's admins: everyone ticked, plus the coordinator. */
fun adminCount(): Int = selectedPublicKeys.size + 1
@@ -211,6 +259,18 @@ class SelectSubgroupAdminsViewModel(
if (name.value.isBlank()) return "Give the subgroup a name."
+ // The policy before the admins and before the key-package lookup, which it
+ // does not wait on: an answer here sends the coordinator back
+ // to a choice, not to a person. Checked again on confirm, so a state that
+ // got past the screen is stopped before a ceremony is proposed.
+ if (walletPolicy.value == SubgroupWalletPolicy.UNGUARDED && loaded.onlyGuarded) {
+ return "This group is guarded, so its subgroups are too."
+ }
+ 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 (loaded.isResolvingKeyPackages) return "Checking who can be added…"
val unreachable = selectedPublicKeys.filter { it in loaded.withoutKeyPackage }
@@ -283,7 +343,11 @@ class SelectSubgroupAdminsViewModel(
// coordinator is added by `proposeRitual`: opening a ceremony is
// being in it, and is not theirs to tick.
participantPublicKeys = selectedPublicKeys.toSet(),
- subject = name.value.trim()
+ subject = name.value.trim(),
+ // 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
)
isActionPending.value = false
diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/SelectSubgroupAdminsUIState.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/SelectSubgroupAdminsUIState.kt
index e64974d1..ff5cecf0 100644
--- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/SelectSubgroupAdminsUIState.kt
+++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/SelectSubgroupAdminsUIState.kt
@@ -1,5 +1,9 @@
package com.machankura.bennu.compose.ui.view.state
+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.nostr.subgroup.WalletPolicyReading
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.machankura.bennu.compose.database.model.intermdiate.LocalChatRoom
import com.machankura.bennu.compose.database.model.intermdiate.LocalParticipant
@@ -29,9 +33,29 @@ sealed interface SelectSubgroupAdminsUIState {
val withoutKeyPackage: Set = emptySet(),
/** Null while the lookup is still out; a set once it has settled. */
val isResolvingKeyPackages: Boolean = true,
+ /**
+ * What the parent's own certificate says of its wallet, or null for a
+ * parent with none. A parent decided *guarded* can only make guarded
+ * subgroups -- `docs/subgroup-wallet-policy.md`, D4.
+ */
+ val parentPolicy: WalletPolicyReading? = null,
+ /**
+ * The guardian keys the parent's own certificate grants it, or null when
+ * they do not read -- in which case it cannot grant a child any, and a
+ * guarded subgroup is refused here rather than after a ceremony.
+ */
+ val parentGuardians: List? = emptyList(),
): SelectSubgroupAdminsUIState
data class Error(val message: String): SelectSubgroupAdminsUIState
data object Loading: SelectSubgroupAdminsUIState
}
+
+/** Whether this parent may only make guarded subgroups. */
+val SelectSubgroupAdminsUIState.Loaded.onlyGuarded: Boolean
+ get() = parentPolicy.isDecided(SubgroupWalletPolicy.GUARDED)
+
+/** Whether this parent can grant a child guardian keys at all. */
+val SelectSubgroupAdminsUIState.Loaded.canGuard: Boolean
+ get() = parentGuardians != null
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 cb027c70..33c75396 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,9 @@
package com.machankura.bennu.compose.ui.view.model
+import kotlin.test.assertNull
+import com.machankura.bennu.compose.managers.GuardianDerivation
+import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading
+import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -74,7 +78,10 @@ class SelectSubgroupAdminsViewModelJvmTest {
},
)
- private fun failed(parentChatRoomId: String? = this.parentChatRoomId) = DkgSession(
+ private fun failed(
+ parentChatRoomId: String? = this.parentChatRoomId,
+ walletPolicy: SubgroupWalletPolicy? = null,
+ ) = DkgSession(
id = failedId,
chatRoomId = this.parentChatRoomId,
coordinatorPublicKey = ada,
@@ -88,12 +95,22 @@ class SelectSubgroupAdminsViewModelJvmTest {
round1Random = "aa".repeat(32),
round2AuxRandom = "bb".repeat(32),
parentChatRoomId = parentChatRoomId,
+ walletPolicy = walletPolicy?.tagValue,
)
- private val chatRepository = object : ChatRepository by ChatRepository.NO_OP_CHAT_REPOSITORY {
+ private fun chatRepository(
+ parentPolicy: WalletPolicyReading? = null,
+ parentGuardians: List? = emptyList(),
+ ) = object : ChatRepository by ChatRepository.NO_OP_CHAT_REPOSITORY {
override suspend fun getChatRoomByIdentifier(id: HexKey): LocalChatRoom? =
parent.takeIf { id == parentChatRoomId }
+ override suspend fun walletPolicyOf(chatRoomId: String): WalletPolicyReading? =
+ parentPolicy.takeIf { chatRoomId == parentChatRoomId }
+
+ override suspend fun guardiansOf(chatRoomId: String): List? =
+ parentGuardians.takeIf { chatRoomId == parentChatRoomId }
+
// Never answers, so the lookup is still out while the test looks: what is
// under test is what the form starts from, not who has a key package.
override suspend fun observeMarmotKeyPackageForPublicKey(
@@ -102,7 +119,11 @@ class SelectSubgroupAdminsViewModelJvmTest {
): Flow = flow { awaitCancellation() }
}
- private fun viewModel(session: DkgSession?, retrying: String? = failedId) = SelectSubgroupAdminsViewModel(
+ private fun viewModel(
+ session: DkgSession?,
+ retrying: String? = failedId,
+ chatRepository: ChatRepository = chatRepository(),
+ ) = SelectSubgroupAdminsViewModel(
activeUserPublicKey = ada,
parentChatRoomId = parentChatRoomId,
retrying = retrying,
@@ -157,4 +178,96 @@ class SelectSubgroupAdminsViewModelJvmTest {
assertEquals("", viewModel.name.value)
assertEquals(emptySet(), viewModel.selectedPublicKeys.toSet())
}
+
+ private val guarded = WalletPolicyReading.Decided(SubgroupWalletPolicy.GUARDED)
+
+ /** A guarded parent's view model, loaded, with a name typed so a refusal is about the policy. */
+ private suspend fun underGuardedParent(session: DkgSession? = null) =
+ viewModel(session, chatRepository = chatRepository(parentPolicy = guarded)).apply {
+ initiate()
+ awaitLoaded()
+ setName("Tapscript")
+ }
+
+ @Test
+ fun `a subgroup is guarded unless the coordinator says otherwise`(): Unit = runBlocking {
+ val viewModel = viewModel(null, retrying = null)
+ viewModel.initiate()
+ viewModel.awaitLoaded()
+
+ assertEquals(SubgroupWalletPolicy.GUARDED, viewModel.walletPolicy.value)
+
+ viewModel.setWalletPolicy(SubgroupWalletPolicy.UNGUARDED)
+ assertEquals(SubgroupWalletPolicy.UNGUARDED, viewModel.walletPolicy.value, "a root group may make either")
+ }
+
+ @Test
+ fun `a guarded parent's subgroup is guarded, and stays so`(): Unit = runBlocking {
+ val viewModel = underGuardedParent()
+
+ assertEquals(SubgroupWalletPolicy.GUARDED, viewModel.walletPolicy.value)
+ viewModel.setWalletPolicy(SubgroupWalletPolicy.UNGUARDED)
+ assertEquals(SubgroupWalletPolicy.GUARDED, viewModel.walletPolicy.value, "D4: the choice is not the coordinator's")
+ assertNull(viewModel.refusal()?.takeIf { "guarded" in it })
+ }
+
+ @Test
+ fun `a guarded parent refuses an unguarded subgroup that got past the screen`(): Unit = runBlocking {
+ val viewModel = underGuardedParent()
+
+ // The state forced, as a bug or a skipped screen would leave it.
+ viewModel.walletPolicy.value = SubgroupWalletPolicy.UNGUARDED
+
+ assertEquals("This group is guarded, so its subgroups are too.", viewModel.refusal())
+ }
+
+ @Test
+ fun `a retry of an unguarded ceremony under a guarded parent comes back guarded`(): Unit = runBlocking {
+ val viewModel = underGuardedParent(failed(walletPolicy = SubgroupWalletPolicy.UNGUARDED))
+
+ assertEquals("Tapscript", viewModel.name.value)
+ assertEquals(SubgroupWalletPolicy.GUARDED, viewModel.walletPolicy.value)
+ }
+
+ @Test
+ fun `a retry starts from the failed ceremony's wallet policy`(): Unit = runBlocking {
+ val viewModel = viewModel(failed(walletPolicy = SubgroupWalletPolicy.UNGUARDED))
+ viewModel.initiate()
+ viewModel.awaitLoaded()
+
+ assertEquals(SubgroupWalletPolicy.UNGUARDED, viewModel.walletPolicy.value)
+ }
+
+ @Test
+ fun `an unguarded or undecided parent may make either`(): Unit = runBlocking {
+ listOf(WalletPolicyReading.Decided(SubgroupWalletPolicy.UNGUARDED), WalletPolicyReading.Undecided).forEach { policy ->
+ val viewModel = viewModel(null, retrying = null, chatRepository = chatRepository(parentPolicy = policy))
+ viewModel.initiate()
+ viewModel.awaitLoaded()
+
+ viewModel.setWalletPolicy(SubgroupWalletPolicy.UNGUARDED)
+ assertEquals(SubgroupWalletPolicy.UNGUARDED, viewModel.walletPolicy.value, "under $policy")
+ viewModel.setWalletPolicy(SubgroupWalletPolicy.GUARDED)
+ assertEquals(SubgroupWalletPolicy.GUARDED, viewModel.walletPolicy.value, "under $policy")
+ }
+ }
+
+ @Test
+ fun `a parent whose guardian keys do not read cannot make a guarded subgroup`(): Unit = runBlocking {
+ val viewModel = viewModel(null, retrying = null, chatRepository = chatRepository(parentGuardians = null))
+ viewModel.initiate()
+ viewModel.awaitLoaded()
+ viewModel.setName("Tapscript")
+
+ assertEquals(SubgroupWalletPolicy.UNGUARDED, viewModel.walletPolicy.value)
+ viewModel.setWalletPolicy(SubgroupWalletPolicy.GUARDED)
+ assertEquals(SubgroupWalletPolicy.UNGUARDED, viewModel.walletPolicy.value)
+
+ viewModel.walletPolicy.value = SubgroupWalletPolicy.GUARDED
+ assertEquals(
+ "This group's own guardian keys can't be read, so it can't guard a subgroup.",
+ viewModel.refusal(),
+ "refused here, not after a ceremony",
+ )
+ }
}