From abb2c550ab160c8b9d1301708427917f751de92c Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Sat, 24 Oct 2026 12:09:26 +0200 Subject: [PATCH] feat(wallet): a person's page lists each guarded wallet once, with the group that guards it (Phase 6) Wallets your groups guard drew one row per holding group, so a wallet held by a group and its parent appeared twice, each row calling its holder the guardian. MemberWalletManager.byWallet gathers the rows into one entry per wallet: headed by its label, "Guarded by #M" for the group that signed its certificate, and under it each group here with a leaf in it, nearest first -- "#P: after 144 blocks, about 1 day" -- with Spend where this device holds a share, described as "Spend from Savings as #P". GuardedHere.guardianName, which was the holder's room name, is now holderName, so it is not mistaken for the direct guardian's. Co-Authored-By: Claude Opus 5.5 --- .../composeResources/values/strings.xml | 3 + .../compose/managers/MemberWalletManager.kt | 43 +++++++++++- .../ui/composable/ProfilePreviewScreen.kt | 67 ++++++++++++++----- .../model/PersonGuardedWalletsViewModel.kt | 8 ++- .../managers/MemberWalletPagesJvmTest.kt | 51 +++++++++++++- .../composable/ProfilePreviewScreenJvmTest.kt | 32 +++++++++ 6 files changed, 180 insertions(+), 24 deletions(-) diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index dab040fe..8133d81d 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -671,6 +671,9 @@ this group a group below %1$s a group below this one + another group + the wallet guarded by %1$s + Spend from %1$s as %2$s Members' wallets %1$s, %2$s diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/MemberWalletManager.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/MemberWalletManager.kt index 06604c35..bbb4de52 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/MemberWalletManager.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/MemberWalletManager.kt @@ -778,12 +778,51 @@ object MemberWalletManager { /** A member's wallet as one of the guarding groups on this device holds it. */ data class GuardedHere( val wallet: GuardianManager.GuardedWallet, - /** The guarding group's room name here. */ - val guardianName: String?, + /** + * The holding group's room name here -- the group with a leaf in it, which + * is not always the one that directly guards it: that is + * `GuardedSubject.Member.guardianName`. + */ + val holderName: String?, /** Whether this device holds a share of that group's key, and so can propose a spend through its leaf. */ val canSpend: Boolean, ) + /** + * One of a member's wallets on their page, with every group here that holds + * a leaf in it -- `docs/member-wallet-guarding-group.md`, D7. The page used to + * draw a row per holder, so a wallet held by a group and its parent appeared + * twice, each row calling its holder the guardian. + */ + data class GuardedHereWallet( + val walletId: String, + val label: String?, + /** The group that directly guards it, as any holder here can name it, or null. */ + val guardianName: String?, + /** Where no holder can name it: a group it is known to sit below, or null. */ + val below: String?, + /** The groups here holding a leaf in it, nearest first. */ + val holders: List, + ) + + /** [rows] -- one per holding group, as [guardedByGroupsHere] gives them -- as one entry per wallet. */ + fun byWallet(rows: List): List = + rows.groupBy { it.wallet.descendantChatRoomId.lowercase() }.map { (_, held) -> + val holders = held.sortedBy { it.wallet.position } + val members = holders.mapNotNull { it.wallet.subject as? GuardianManager.GuardedSubject.Member } + GuardedHereWallet( + walletId = holders.first().wallet.descendantChatRoomId, + label = members.firstNotNullOfOrNull { it.label }, + guardianName = members.firstNotNullOfOrNull { it.guardianName }, + // The nearest holder's own child it sits under, or the holder + // itself: either is a group it is truly below. + below = holders.firstNotNullOfOrNull { holder -> + (holder.wallet.subject as? GuardianManager.GuardedSubject.Member)?.branchName ?: holder.holderName?.takeUnless { holder.wallet.isGuardedHere } + }, + holders = holders, + ) + } + private suspend fun noticesIn(database: BennuDatabase, chatRoomId: String): List = database.marmotInnerEventDao() .getByChatRoomAndKinds(chatRoomId, listOf(GroupKeyNoticeEvent.KIND)) diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/ProfilePreviewScreen.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/ProfilePreviewScreen.kt index 1814fa9e..7300f628 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/ProfilePreviewScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/ProfilePreviewScreen.kt @@ -2,13 +2,19 @@ package com.machankura.bennu.compose.ui.composable import com.machankura.bennu.compose.repository.MemberWalletRepository import com.machankura.bennu.compose.managers.MemberWalletManager -import com.machankura.bennu.compose.managers.GuardianManager import com.machankura.bennu.compose.ui.view.model.PersonGuardedWalletsViewModel import com.machankura.bennu.compose.ui.composable.navigation.routes.GroupSendRoute import bennu.composeapp.generated.resources.wallets_your_groups_guard import bennu.composeapp.generated.resources.guarded_by_s import bennu.composeapp.generated.resources.a_group -import bennu.composeapp.generated.resources.s_dot_s +import bennu.composeapp.generated.resources.a_group_below_s +import bennu.composeapp.generated.resources.another_group +import bennu.composeapp.generated.resources.guardian_after +import bennu.composeapp.generated.resources.spend_from_s_as_s +import bennu.composeapp.generated.resources.the_wallet_guarded_by_s +import com.machankura.bennu.compose.ui.composable.widgets.delayText +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.semantics.semantics import bennu.composeapp.generated.resources.spend import androidx.compose.material.icons.filled.AccountBalanceWallet import androidx.compose.material3.ListItem @@ -213,14 +219,14 @@ fun ProfilePreviewScreen( // keys; this is the guardians' view of somebody else's. if (profilePublicKey != activeUserPublicKey && personWalletsViewModel.rows.isNotEmpty()) { GuardedByYourGroups( - rows = personWalletsViewModel.rows, - onSpend = { row -> + wallets = personWalletsViewModel.rows, + onSpend = { holder -> onNavigateToRoute( GroupSendRoute( activeUserPublicKey = activeUserPublicKey, - chatRoomId = row.wallet.ancestorChatRoomId, + chatRoomId = holder.wallet.ancestorChatRoomId, relayHint = null, - guardedChatRoomId = row.wallet.descendantChatRoomId, + guardedChatRoomId = holder.wallet.descendantChatRoomId, ) ) }, @@ -263,28 +269,53 @@ fun ProfilePreviewScreen( } /** - * The person's guarded wallets as this profile's groups hold them: one row per - * guarding group, and -- where this device holds a share of that group's key -- - * the way to spend through its leaf, which is the guardian spend the group's - * wallet screen already offers. + * The person's guarded wallets as this profile's groups hold them: one entry per + * wallet, saying which group directly guards it, and under it each group here + * holding a leaf in it, with that leaf's delay and -- where this device holds a + * share of that group's key -- the way to spend through it, which is the + * guardian spend the group's wallet screen already offers. + * `docs/member-wallet-guarding-group.md`, D7. */ @Composable private fun GuardedByYourGroups( - rows: List, + wallets: List, onSpend: (MemberWalletManager.GuardedHere) -> Unit, ) { Column(verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap)) { Text(stringResource(Res.string.wallets_your_groups_guard), style = MaterialTheme.typography.titleMedium) - rows.forEach { row -> - val guardian = stringResource(Res.string.guarded_by_s, row.guardianName ?: stringResource(Res.string.a_group)) - val label = (row.wallet.subject as? GuardianManager.GuardedSubject.Member)?.label + wallets.forEach { wallet -> + val guardian = wallet.guardianName + ?: wallet.below?.let { stringResource(Res.string.a_group_below_s, it) } + ?: stringResource(Res.string.another_group) + val guardedBy = stringResource(Res.string.guarded_by_s, guardian) ListItem( leadingContent = { Icon(Icons.Default.AccountBalanceWallet, contentDescription = Decorative) }, - headlineContent = { Text(label?.let { stringResource(Res.string.s_dot_s, it, guardian) } ?: guardian) }, - trailingContent = if (row.canSpend) { - { TextButton(onClick = { onSpend(row) }) { Text(stringResource(Res.string.spend)) } } - } else null, + // Named by its label, or with none by the group that guards it. + headlineContent = { Text(wallet.label ?: guardedBy) }, + supportingContent = wallet.label?.let { { Text(guardedBy) } }, ) + wallet.holders.forEach { holder -> + val holderName = holder.holderName ?: stringResource(Res.string.a_group) + val spendLabel = stringResource( + Res.string.spend_from_s_as_s, + wallet.label ?: stringResource(Res.string.the_wallet_guarded_by_s, guardian), + holderName, + ) + ListItem( + // Under its wallet, so the holders read as the wallet's. + modifier = Modifier.padding(start = MaterialTheme.spacing.containerPadding), + headlineContent = { + Text(stringResource(Res.string.guardian_after, holderName, delayText(holder.wallet.delay, holder.wallet.chain))) + }, + trailingContent = if (holder.canSpend) { + { + TextButton(onClick = { onSpend(holder) }, modifier = Modifier.semantics { contentDescription = spendLabel }) { + Text(stringResource(Res.string.spend)) + } + } + } else null, + ) + } } } } diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/PersonGuardedWalletsViewModel.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/PersonGuardedWalletsViewModel.kt index fcc13d8f..283ed521 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/PersonGuardedWalletsViewModel.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/PersonGuardedWalletsViewModel.kt @@ -16,7 +16,8 @@ import kotlinx.coroutines.launch /** * A person's guarded wallets as the groups this profile is in guard them -- - * the person's page's *Wallets your groups guard*. Where a group's page lists a + * the person's page's *Wallets your groups guard*, one entry per wallet and + * each with the groups that hold a leaf in it. Where a group's page lists a * member's wallet, its row leads here, since a person is someone to look at, * not a room to go into. */ @@ -24,11 +25,12 @@ class PersonGuardedWalletsViewModel( private val ownerPublicKey: HexKey, private val memberWalletRepository: MemberWalletRepository, ) : ViewModel() { - var rows: List by mutableStateOf(emptyList()) + /** One per wallet, each with the groups here holding a leaf in it. */ + var rows: List by mutableStateOf(emptyList()) private set fun initiate() { - viewModelScope.launch(Dispatchers.IO) { rows = memberWalletRepository.guardedByGroupsHere(ownerPublicKey) } + viewModelScope.launch(Dispatchers.IO) { rows = MemberWalletManager.byWallet(memberWalletRepository.guardedByGroupsHere(ownerPublicKey)) } } companion object { diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/MemberWalletPagesJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/MemberWalletPagesJvmTest.kt index 35703654..77b69f64 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/MemberWalletPagesJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/MemberWalletPagesJvmTest.kt @@ -22,6 +22,7 @@ import kotlinx.coroutines.runBlocking import kotlin.test.AfterTest import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertNull import kotlin.test.assertTrue import kotlin.time.Instant @@ -100,7 +101,55 @@ class MemberWalletPagesJvmTest { assertEquals(setOf(groupRoom, parentRoom), here.map { it.wallet.ancestorChatRoomId }.toSet()) assertEquals(true, here.single { it.wallet.ancestorChatRoomId == groupRoom }.canSpend, "a share of the group's key") assertEquals(false, here.single { it.wallet.ancestorChatRoomId == parentRoom }.canSpend, "none of the root's") - assertEquals("#Root", here.single { it.wallet.ancestorChatRoomId == parentRoom }.guardianName) + assertEquals("#Root", here.single { it.wallet.ancestorChatRoomId == parentRoom }.holderName) assertTrue(MemberWalletManager.guardedByGroupsHere(db, GroupFixtures.owner, bobPublicKey).isEmpty()) } + + // ---- One entry per wallet: docs/member-wallet-guarding-group.md, Phase 6 + + @Test + fun `a wallet held by a group and its parent is one entry, guarded by the group, with both holders`(): Unit = runBlocking { + signingRoom(db, group, members = listOf(alicePublicKey)) + fileKeyState(db, group, guardedCertificate(parent, group)) + GroupFixtures.openToMembers(db, group) + signingRoom(db, parent, members = listOf(alicePublicKey), holdsShare = false, subject = "#Root") + fileKeyState(db, parent) + GroupFixtures.openToMembers(db, parent) + + val wallet = MemberWalletManager.request(db, groupRoom, alice, chain, label = "Savings") + val certificate = certify(wallet.requestJson) + db.marmotInnerEventDao().upsert( + MarmotInnerEvent( + id = "9".repeat(64), publicKey = alicePublicKey, kind = GuardedWalletNoticeEvent.KIND, + createdAt = Instant.fromEpochSeconds(1_700_000_100), tags = arrayOf(arrayOf("h", parentRoom)), + content = certificate.toJson(), chatRoomId = parentRoom, + ) + ) + + val entry = MemberWalletManager.byWallet(MemberWalletManager.guardedByGroupsHere(db, GroupFixtures.owner, alicePublicKey)).single() + assertEquals(wallet.walletId, entry.walletId) + assertEquals("Savings", entry.label) + assertEquals("#Child", entry.guardianName, "the founding name the parent certified") + assertEquals(listOf(groupRoom, parentRoom), entry.holders.map { it.wallet.ancestorChatRoomId }) + assertEquals(listOf(true, false), entry.holders.map { it.canSpend }, "a share of the group's key, none of the root's") + } + + @Test + fun `Savings held by P and R, and Rent held by R, are two entries`() { + val savingsAtP = MemberWalletManager.GuardedHere(LineageExample.heldBy(LineageExample.p, LineageExample.savings, LineageExample.savingsGuardians, label = "Savings", guardianName = "#M"), holderName = "#P", canSpend = true) + val savingsAtR = MemberWalletManager.GuardedHere(LineageExample.heldBy(LineageExample.r, LineageExample.savings, LineageExample.savingsGuardians, label = "Savings", guardianName = null, branchName = "#P"), holderName = "#R", canSpend = true) + val rentAtR = MemberWalletManager.GuardedHere(LineageExample.heldBy(LineageExample.r, LineageExample.rent, LineageExample.rentGuardians), holderName = "#R", canSpend = false) + + val entries = MemberWalletManager.byWallet(listOf(savingsAtR, rentAtR, savingsAtP)) + assertEquals(listOf(LineageExample.savings, LineageExample.rent), entries.map { it.walletId }) + + val savings = entries.first() + assertEquals("#M", savings.guardianName, "named by whichever holder can") + assertEquals(listOf("#P", "#R"), savings.holders.map { it.holderName }, "nearest first") + + val rent = entries.last() + assertNull(rent.guardianName) + assertEquals("#R", rent.below, "N is unnamed here, but it is below R") + assertEquals(listOf(false), rent.holders.map { it.canSpend }) + } } diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/ProfilePreviewScreenJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/ProfilePreviewScreenJvmTest.kt index e06e1082..fa123418 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/ProfilePreviewScreenJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/ProfilePreviewScreenJvmTest.kt @@ -21,6 +21,11 @@ import com.machankura.bennu.compose.database.model.Profile import com.machankura.bennu.compose.database.model.SynchronizeNostrEventRequest import com.machankura.bennu.compose.database.model.intermdiate.LocalChatRoom import com.machankura.bennu.compose.repository.ChatRepository +import com.machankura.bennu.compose.repository.MemberWalletRepository +import com.machankura.bennu.compose.managers.LineageExample +import com.machankura.bennu.compose.managers.MemberWalletManager +import androidx.compose.ui.test.onAllNodesWithText +import androidx.compose.ui.test.onNodeWithContentDescription import com.machankura.bennu.compose.extensions.hexToNpubHrp import com.machankura.bennu.compose.repository.NostrRepository import com.machankura.bennu.compose.ui.composable.navigation.routes.ChatRoomMessagingRoute @@ -194,6 +199,7 @@ class ProfilePreviewScreenJvmTest { canSign: Boolean = true, nostrRepository: NostrRepository = NostrRepository.NO_OP_NOSTR_REPOSITORY, chatRepository: ChatRepository = ChatRepository.NO_OP_CHAT_REPOSITORY, + memberWalletRepository: MemberWalletRepository = MemberWalletRepository.NO_OP, onStartChat: (Route) -> Unit = {}, assertions: ComposeUiTest.() -> Unit, ) = runDesktopComposeUiTest(width = 400, height = 1200) { @@ -210,6 +216,7 @@ class ProfilePreviewScreenJvmTest { chatRepository = chatRepository, onStartChat = onStartChat, onNavigateBack = {}, + memberWalletRepository = memberWalletRepository, ) } } @@ -219,4 +226,29 @@ class ProfilePreviewScreenJvmTest { assertions() } + + // --- the wallets this profile's groups guard: docs/member-wallet-guarding-group.md, D7 --- + + @OptIn(ExperimentalTestApi::class) + @Test + fun `a wallet held by two groups here is one entry, guarded by the group that signed it`() { + val held = listOf( + MemberWalletManager.GuardedHere(LineageExample.heldBy(LineageExample.r, LineageExample.savings, LineageExample.savingsGuardians, label = "Savings", ownerPublicKey = profilePublicKey, branchName = "#P"), holderName = "#R", canSpend = false), + MemberWalletManager.GuardedHere(LineageExample.heldBy(LineageExample.p, LineageExample.savings, LineageExample.savingsGuardians, label = "Savings", ownerPublicKey = profilePublicKey, guardianName = "#M", branchName = "#M"), holderName = "#P", canSpend = true), + ) + val repository = object : MemberWalletRepository by MemberWalletRepository.NO_OP { + override suspend fun guardedByGroupsHere(ownerPublicKey: String): List = held + } + + render(ProfilePreviewUIState.Loaded(profile = alice, readiness = Readiness.CanStartChat), memberWalletRepository = repository) { + waitUntil(timeoutMillis = 5_000) { onAllNodesWithText("Savings").fetchSemanticsNodes().isNotEmpty() } + onNodeWithText("Wallets your groups guard").assertIsDisplayed() + assertEquals(1, onAllNodesWithText("Savings").fetchSemanticsNodes().size, "one entry for the wallet") + onNodeWithText("Guarded by #M").assertIsDisplayed() + onNodeWithText("#P: after 12 blocks").assertIsDisplayed() + onNodeWithText("#R: after 12 blocks").assertIsDisplayed() + onNodeWithContentDescription("Spend from Savings as #P").assertIsDisplayed() + onNodeWithContentDescription("Spend from Savings as #R").assertDoesNotExist() + } + } }